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