Squashed commit of the following:
[mesa.git] / src / gallium / state_trackers / vega / vg_manager.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.9
4 *
5 * Copyright (C) 2010 LunarG Inc.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 * DEALINGS IN THE SOFTWARE.
24 *
25 * Authors:
26 * Chia-I Wu <olv@lunarg.com>
27 */
28
29 #include "state_tracker/st_api.h"
30
31 #include "pipe/p_context.h"
32 #include "pipe/p_screen.h"
33 #include "util/u_memory.h"
34 #include "util/u_inlines.h"
35
36 #include "vg_manager.h"
37 #include "vg_context.h"
38 #include "vg_tracker.h" /* for st_resize_framebuffer */
39 #include "image.h"
40
41 /**
42 * Flush the front buffer if the current context renders to the front buffer.
43 */
44 void
45 vg_manager_flush_frontbuffer(struct vg_context *ctx)
46 {
47 struct st_framebuffer *stfb = ctx->draw_buffer;
48
49 if (!stfb)
50 return;
51
52 /* st_public.h is used */
53 if (!stfb->iface) {
54 struct pipe_screen *screen = ctx->pipe->screen;
55 if (screen->flush_frontbuffer) {
56 screen->flush_frontbuffer(screen,
57 stfb->strb->surface, ctx->pipe->priv);
58 }
59 return;
60 }
61
62 switch (stfb->strb_att) {
63 case ST_ATTACHMENT_FRONT_LEFT:
64 case ST_ATTACHMENT_FRONT_RIGHT:
65 stfb->iface->flush_front(stfb->iface, stfb->strb_att);
66 break;
67 default:
68 break;
69 }
70 }
71
72 /**
73 * Re-validate the framebuffer.
74 */
75 void
76 vg_manager_validate_framebuffer(struct vg_context *ctx)
77 {
78 struct pipe_screen *screen = ctx->pipe->screen;
79 struct st_framebuffer *stfb = ctx->draw_buffer;
80 struct st_renderbuffer *rb;
81 struct pipe_resource *pt;
82
83 /* no binding surface */
84 if (!stfb)
85 return;
86
87 /* st_public.h is used */
88 if (!stfb->iface) {
89 struct pipe_screen *screen = ctx->pipe->screen;
90 if (screen->update_buffer)
91 screen->update_buffer(screen, ctx->pipe->priv);
92 return;
93 }
94
95 if (!p_atomic_read(&ctx->draw_buffer_invalid))
96 return;
97
98 /* validate the fb */
99 if (!stfb->iface->validate(stfb->iface, &stfb->strb_att, 1, &pt) || !pt)
100 return;
101
102 rb = stfb->strb;
103 if (rb->texture == pt) {
104 pipe_resource_reference(&pt, NULL);
105 return;
106 }
107
108 /* unreference existing ones */
109 pipe_surface_reference(&rb->surface, NULL);
110 pipe_resource_reference(&rb->texture, NULL);
111
112 rb->texture = pt;
113 rb->surface = screen->get_tex_surface(screen, rb->texture, 0, 0, 0,
114 PIPE_BIND_RENDER_TARGET |
115 PIPE_BIND_BLIT_SOURCE |
116 PIPE_BIND_BLIT_DESTINATION);
117
118 rb->width = rb->surface->width;
119 rb->height = rb->surface->height;
120
121 st_resize_framebuffer(stfb, rb->width, rb->height);
122
123 p_atomic_set(&ctx->draw_buffer_invalid, FALSE);
124 }
125
126
127 static void
128 vg_context_notify_invalid_framebuffer(struct st_context_iface *stctxi,
129 struct st_framebuffer_iface *stfbi)
130 {
131 struct vg_context *ctx = (struct vg_context *) stctxi;
132 p_atomic_set(&ctx->draw_buffer_invalid, TRUE);
133 }
134
135 static void
136 vg_context_flush(struct st_context_iface *stctxi, unsigned flags,
137 struct pipe_fence_handle **fence)
138 {
139 struct vg_context *ctx = (struct vg_context *) stctxi;
140 ctx->pipe->flush(ctx->pipe, flags, fence);
141 if (flags & PIPE_FLUSH_RENDER_CACHE)
142 vg_manager_flush_frontbuffer(ctx);
143 }
144
145 static void
146 vg_context_destroy(struct st_context_iface *stctxi)
147 {
148 struct vg_context *ctx = (struct vg_context *) stctxi;
149 vg_destroy_context(ctx);
150 }
151
152 static struct st_context_iface *
153 vg_api_create_context(struct st_api *stapi, struct st_manager *smapi,
154 const struct st_visual *visual,
155 struct st_context_iface *shared_stctxi)
156 {
157 struct vg_context *shared_ctx = (struct vg_context *) shared_stctxi;
158 struct vg_context *ctx;
159 struct pipe_context *pipe;
160
161 pipe = smapi->screen->context_create(smapi->screen, NULL);
162 if (!pipe)
163 return NULL;
164 ctx = vg_create_context(pipe, NULL, shared_ctx);
165 if (!ctx) {
166 pipe->destroy(pipe);
167 return NULL;
168 }
169
170 ctx->iface.destroy = vg_context_destroy;
171
172 ctx->iface.notify_invalid_framebuffer =
173 vg_context_notify_invalid_framebuffer;
174 ctx->iface.flush = vg_context_flush;
175
176 ctx->iface.teximage = NULL;
177 ctx->iface.copy = NULL;
178
179 ctx->iface.st_context_private = (void *) smapi;
180
181 return &ctx->iface;
182 }
183
184 static struct st_renderbuffer *
185 create_renderbuffer(enum pipe_format format)
186 {
187 struct st_renderbuffer *strb;
188
189 strb = CALLOC_STRUCT(st_renderbuffer);
190 if (strb)
191 strb->format = format;
192
193 return strb;
194 }
195
196 static void
197 destroy_renderbuffer(struct st_renderbuffer *strb)
198 {
199 pipe_surface_reference(&strb->surface, NULL);
200 pipe_resource_reference(&strb->texture, NULL);
201 free(strb);
202 }
203
204 /**
205 * Decide the buffer to render to.
206 */
207 static enum st_attachment_type
208 choose_attachment(struct st_framebuffer_iface *stfbi)
209 {
210 enum st_attachment_type statt;
211
212 statt = stfbi->visual->render_buffer;
213 if (statt != ST_ATTACHMENT_INVALID) {
214 /* use the buffer given by the visual, unless it is unavailable */
215 if (!st_visual_have_buffers(stfbi->visual, 1 << statt)) {
216 switch (statt) {
217 case ST_ATTACHMENT_BACK_LEFT:
218 statt = ST_ATTACHMENT_FRONT_LEFT;
219 break;
220 case ST_ATTACHMENT_BACK_RIGHT:
221 statt = ST_ATTACHMENT_FRONT_RIGHT;
222 break;
223 default:
224 break;
225 }
226
227 if (!st_visual_have_buffers(stfbi->visual, 1 << statt))
228 statt = ST_ATTACHMENT_INVALID;
229 }
230 }
231
232 return statt;
233 }
234
235 /**
236 * Bind the context to the given framebuffers.
237 */
238 static boolean
239 vg_context_bind_framebuffers(struct st_context_iface *stctxi,
240 struct st_framebuffer_iface *stdrawi,
241 struct st_framebuffer_iface *streadi)
242 {
243 struct vg_context *ctx = (struct vg_context *) stctxi;
244 struct st_framebuffer *stfb;
245 enum st_attachment_type strb_att;
246
247 /* the draw and read framebuffers must be the same */
248 if (stdrawi != streadi)
249 return FALSE;
250
251 p_atomic_set(&ctx->draw_buffer_invalid, TRUE);
252
253 strb_att = (stdrawi) ? choose_attachment(stdrawi) : ST_ATTACHMENT_INVALID;
254
255 if (ctx->draw_buffer) {
256 stfb = ctx->draw_buffer;
257
258 /* free the existing fb */
259 if (!stdrawi ||
260 stfb->strb_att != strb_att ||
261 stfb->strb->format != stdrawi->visual->color_format ||
262 stfb->dsrb->format != stdrawi->visual->depth_stencil_format) {
263 destroy_renderbuffer(stfb->strb);
264 destroy_renderbuffer(stfb->dsrb);
265 free(stfb);
266
267 ctx->draw_buffer = NULL;
268 }
269 }
270
271 if (!stdrawi)
272 return TRUE;
273
274 if (strb_att == ST_ATTACHMENT_INVALID)
275 return FALSE;
276
277 /* create a new fb */
278 if (!ctx->draw_buffer) {
279 stfb = CALLOC_STRUCT(st_framebuffer);
280 if (!stfb)
281 return FALSE;
282
283 stfb->strb = create_renderbuffer(stdrawi->visual->color_format);
284 if (!stfb->strb) {
285 free(stfb);
286 return FALSE;
287 }
288
289 stfb->dsrb = create_renderbuffer(stdrawi->visual->depth_stencil_format);
290 if (!stfb->dsrb) {
291 free(stfb->strb);
292 free(stfb);
293 return FALSE;
294 }
295
296 stfb->width = 0;
297 stfb->height = 0;
298 stfb->strb_att = strb_att;
299
300 ctx->draw_buffer = stfb;
301 }
302
303 ctx->draw_buffer->iface = stdrawi;
304
305 return TRUE;
306 }
307
308 static boolean
309 vg_api_make_current(struct st_api *stapi, struct st_context_iface *stctxi,
310 struct st_framebuffer_iface *stdrawi,
311 struct st_framebuffer_iface *streadi)
312 {
313 struct vg_context *ctx = (struct vg_context *) stctxi;
314
315 if (stctxi)
316 vg_context_bind_framebuffers(stctxi, stdrawi, streadi);
317 vg_set_current_context(ctx);
318
319 return TRUE;
320 }
321
322 static struct st_context_iface *
323 vg_api_get_current(struct st_api *stapi)
324 {
325 struct vg_context *ctx = vg_current_context();
326
327 return (ctx) ? &ctx->iface : NULL;
328 }
329
330 static boolean
331 vg_api_is_visual_supported(struct st_api *stapi,
332 const struct st_visual *visual)
333 {
334 /* the impl requires a depth/stencil buffer */
335 if (visual->depth_stencil_format == PIPE_FORMAT_NONE)
336 return FALSE;
337
338 return TRUE;
339 }
340
341 static st_proc_t
342 vg_api_get_proc_address(struct st_api *stapi, const char *procname)
343 {
344 /* TODO */
345 return (st_proc_t) NULL;
346 }
347
348 static void
349 vg_api_destroy(struct st_api *stapi)
350 {
351 free(stapi);
352 }
353
354 static struct st_api *
355 vg_module_create_api(void)
356 {
357 struct st_api *stapi;
358
359 stapi = CALLOC_STRUCT(st_api);
360 if (stapi) {
361 stapi->destroy = vg_api_destroy;
362 stapi->get_proc_address = vg_api_get_proc_address;
363 stapi->is_visual_supported = vg_api_is_visual_supported;
364
365 stapi->create_context = vg_api_create_context;
366 stapi->make_current = vg_api_make_current;
367 stapi->get_current = vg_api_get_current;
368 }
369
370 return stapi;
371 }
372
373 PUBLIC const struct st_module st_module_OpenVG = {
374 .api = ST_API_OPENVG,
375 .create_api = vg_module_create_api,
376 };