7bc0006769c55548ba782c9563e813a305d16461
[profile/ivi/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_texture *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_texture_reference(&pt, NULL);
105       return;
106    }
107
108    /* unreference existing ones */
109    pipe_surface_reference(&rb->surface, NULL);
110    pipe_texture_reference(&rb->texture, NULL);
111
112    rb->texture = pt;
113    rb->surface = screen->get_tex_surface(screen, rb->texture, 0, 0, 0,
114          PIPE_BUFFER_USAGE_GPU_READ | PIPE_BUFFER_USAGE_GPU_WRITE);
115
116    rb->width = rb->surface->width;
117    rb->height = rb->surface->height;
118
119    st_resize_framebuffer(stfb, rb->width, rb->height);
120
121    p_atomic_set(&ctx->draw_buffer_invalid, FALSE);
122 }
123
124
125 static void
126 vg_context_notify_invalid_framebuffer(struct st_context_iface *stctxi,
127                                       struct st_framebuffer_iface *stfbi)
128 {
129    struct vg_context *ctx = (struct vg_context *) stctxi;
130    p_atomic_set(&ctx->draw_buffer_invalid, TRUE);
131 }
132
133 static void
134 vg_context_flush(struct st_context_iface *stctxi, unsigned flags,
135                  struct pipe_fence_handle **fence)
136 {
137    struct vg_context *ctx = (struct vg_context *) stctxi;
138    ctx->pipe->flush(ctx->pipe, flags, fence);
139    if (flags & PIPE_FLUSH_RENDER_CACHE)
140       vg_manager_flush_frontbuffer(ctx);
141 }
142
143 static void
144 vg_context_destroy(struct st_context_iface *stctxi)
145 {
146    struct vg_context *ctx = (struct vg_context *) stctxi;
147    vg_destroy_context(ctx);
148 }
149
150 static struct st_context_iface *
151 vg_api_create_context(struct st_api *stapi, struct st_manager *smapi,
152                       const struct st_visual *visual,
153                       struct st_context_iface *shared_stctxi)
154 {
155    struct vg_context *shared_ctx = (struct vg_context *) shared_stctxi;
156    struct vg_context *ctx;
157    struct pipe_context *pipe;
158
159    pipe = smapi->screen->context_create(smapi->screen, NULL);
160    if (!pipe)
161       return NULL;
162    ctx = vg_create_context(pipe, NULL, shared_ctx);
163    if (!ctx) {
164       pipe->destroy(pipe);
165       return NULL;
166    }
167
168    ctx->iface.destroy = vg_context_destroy;
169
170    ctx->iface.notify_invalid_framebuffer =
171       vg_context_notify_invalid_framebuffer;
172    ctx->iface.flush = vg_context_flush;
173
174    ctx->iface.teximage = NULL;
175    ctx->iface.copy = NULL;
176
177    ctx->iface.st_context_private = (void *) smapi;
178
179    return &ctx->iface;
180 }
181
182 static struct st_renderbuffer *
183 create_renderbuffer(enum pipe_format format)
184 {
185    struct st_renderbuffer *strb;
186
187    strb = CALLOC_STRUCT(st_renderbuffer);
188    if (strb)
189       strb->format = format;
190
191    return strb;
192 }
193
194 static void
195 destroy_renderbuffer(struct st_renderbuffer *strb)
196 {
197    pipe_surface_reference(&strb->surface, NULL);
198    pipe_texture_reference(&strb->texture, NULL);
199    free(strb);
200 }
201
202 /**
203  * Decide the buffer to render to.
204  */
205 static enum st_attachment_type
206 choose_attachment(struct st_framebuffer_iface *stfbi)
207 {
208    enum st_attachment_type statt;
209
210    statt = stfbi->visual->render_buffer;
211    if (statt != ST_ATTACHMENT_INVALID) {
212       /* use the buffer given by the visual, unless it is unavailable */
213       if (!st_visual_have_buffers(stfbi->visual, 1 << statt)) {
214          switch (statt) {
215          case ST_ATTACHMENT_BACK_LEFT:
216             statt = ST_ATTACHMENT_FRONT_LEFT;
217             break;
218          case ST_ATTACHMENT_BACK_RIGHT:
219             statt = ST_ATTACHMENT_FRONT_RIGHT;
220             break;
221          default:
222             break;
223          }
224
225          if (!st_visual_have_buffers(stfbi->visual, 1 << statt))
226             statt = ST_ATTACHMENT_INVALID;
227       }
228    }
229
230    return statt;
231 }
232
233 /**
234  * Bind the context to the given framebuffers.
235  */
236 static boolean
237 vg_context_bind_framebuffers(struct st_context_iface *stctxi,
238                              struct st_framebuffer_iface *stdrawi,
239                              struct st_framebuffer_iface *streadi)
240 {
241    struct vg_context *ctx = (struct vg_context *) stctxi;
242    struct st_framebuffer *stfb;
243    enum st_attachment_type strb_att;
244
245    /* the draw and read framebuffers must be the same */
246    if (stdrawi != streadi)
247       return FALSE;
248
249    p_atomic_set(&ctx->draw_buffer_invalid, TRUE);
250
251    strb_att = (stdrawi) ? choose_attachment(stdrawi) : ST_ATTACHMENT_INVALID;
252
253    if (ctx->draw_buffer) {
254       stfb = ctx->draw_buffer;
255
256       /* free the existing fb */
257       if (!stdrawi ||
258           stfb->strb_att != strb_att ||
259           stfb->strb->format != stdrawi->visual->color_format ||
260           stfb->dsrb->format != stdrawi->visual->depth_stencil_format) {
261          destroy_renderbuffer(stfb->strb);
262          destroy_renderbuffer(stfb->dsrb);
263          free(stfb);
264
265          ctx->draw_buffer = NULL;
266       }
267    }
268
269    if (!stdrawi)
270       return TRUE;
271
272    if (strb_att == ST_ATTACHMENT_INVALID)
273       return FALSE;
274
275    /* create a new fb */
276    if (!ctx->draw_buffer) {
277       stfb = CALLOC_STRUCT(st_framebuffer);
278       if (!stfb)
279          return FALSE;
280
281       stfb->strb = create_renderbuffer(stdrawi->visual->color_format);
282       if (!stfb->strb) {
283          free(stfb);
284          return FALSE;
285       }
286
287       stfb->dsrb = create_renderbuffer(stdrawi->visual->depth_stencil_format);
288       if (!stfb->dsrb) {
289          free(stfb->strb);
290          free(stfb);
291          return FALSE;
292       }
293
294       stfb->width = 0;
295       stfb->height = 0;
296       stfb->strb_att = strb_att;
297
298       ctx->draw_buffer = stfb;
299    }
300
301    ctx->draw_buffer->iface = stdrawi;
302
303    return TRUE;
304 }
305
306 static boolean
307 vg_api_make_current(struct st_api *stapi, struct st_context_iface *stctxi,
308                     struct st_framebuffer_iface *stdrawi,
309                     struct st_framebuffer_iface *streadi)
310 {
311    struct vg_context *ctx = (struct vg_context *) stctxi;
312
313    if (stctxi)
314       vg_context_bind_framebuffers(stctxi, stdrawi, streadi);
315    vg_set_current_context(ctx);
316
317    return TRUE;
318 }
319
320 static struct st_context_iface *
321 vg_api_get_current(struct st_api *stapi)
322 {
323    struct vg_context *ctx = vg_current_context();
324
325    return (ctx) ? &ctx->iface : NULL;
326 }
327
328 static boolean
329 vg_api_is_visual_supported(struct st_api *stapi,
330                            const struct st_visual *visual)
331 {
332    /* the impl requires a depth/stencil buffer */
333    if (visual->depth_stencil_format == PIPE_FORMAT_NONE)
334       return FALSE;
335
336    return TRUE;
337 }
338
339 static st_proc_t
340 vg_api_get_proc_address(struct st_api *stapi, const char *procname)
341 {
342    /* TODO */
343    return (st_proc_t) NULL;
344 }
345
346 static void
347 vg_api_destroy(struct st_api *stapi)
348 {
349    free(stapi);
350 }
351
352 static struct st_api *
353 vg_module_create_api(void)
354 {
355    struct st_api *stapi;
356
357    stapi = CALLOC_STRUCT(st_api);
358    if (stapi) {
359       stapi->destroy = vg_api_destroy;
360       stapi->get_proc_address = vg_api_get_proc_address;
361       stapi->is_visual_supported = vg_api_is_visual_supported;
362
363       stapi->create_context = vg_api_create_context;
364       stapi->make_current = vg_api_make_current;
365       stapi->get_current = vg_api_get_current;
366    }
367
368    return stapi;
369 }
370
371 PUBLIC const struct st_module st_module_OpenVG = {
372    .api = ST_API_OPENVG,
373    .create_api = vg_module_create_api,
374 };