ilo: replace a boolean by bool
[mesa.git] / src / gallium / state_trackers / vega / vg_manager.c
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright 2009 VMware, Inc. All Rights Reserved.
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 #include "util/u_box.h"
36 #include "util/u_surface.h"
37
38 #include "vg_api.h"
39 #include "vg_manager.h"
40 #include "vg_context.h"
41 #include "api.h"
42 #include "handle.h"
43
44 static boolean
45 vg_context_update_color_rb(struct vg_context *ctx, struct pipe_resource *pt)
46 {
47 struct st_renderbuffer *strb = ctx->draw_buffer->strb;
48 struct pipe_context *pipe = ctx->pipe;
49 struct pipe_surface surf_tmpl;
50
51 if (strb->texture == pt) {
52 pipe_resource_reference(&pt, NULL);
53 return FALSE;
54 }
55
56 /* unreference existing ones */
57 pipe_surface_reference(&strb->surface, NULL);
58 pipe_resource_reference(&strb->texture, NULL);
59 strb->width = strb->height = 0;
60
61 strb->texture = pt;
62
63 u_surface_default_template(&surf_tmpl, strb->texture);
64 strb->surface = pipe->create_surface(pipe, strb->texture, &surf_tmpl);
65
66 if (!strb->surface) {
67 pipe_resource_reference(&strb->texture, NULL);
68 return TRUE;
69 }
70
71 strb->width = pt->width0;
72 strb->height = pt->height0;
73
74 return TRUE;
75 }
76
77 /**
78 * Flush the front buffer if the current context renders to the front buffer.
79 */
80 void
81 vg_manager_flush_frontbuffer(struct vg_context *ctx)
82 {
83 struct st_framebuffer *stfb = ctx->draw_buffer;
84
85 if (!stfb)
86 return;
87
88 switch (stfb->strb_att) {
89 case ST_ATTACHMENT_FRONT_LEFT:
90 case ST_ATTACHMENT_FRONT_RIGHT:
91 stfb->iface->flush_front(&ctx->iface, stfb->iface, stfb->strb_att);
92 break;
93 default:
94 break;
95 }
96 }
97
98 /**
99 * Re-validate the framebuffer.
100 */
101 void
102 vg_manager_validate_framebuffer(struct vg_context *ctx)
103 {
104 struct st_framebuffer *stfb = ctx->draw_buffer;
105 struct pipe_resource *pt;
106 int32_t new_stamp;
107
108 /* no binding surface */
109 if (!stfb)
110 return;
111
112 new_stamp = p_atomic_read(&stfb->iface->stamp);
113 if (stfb->iface_stamp != new_stamp) {
114 do {
115 /* validate the fb */
116 if (!stfb->iface->validate(stfb->iface, &stfb->strb_att,
117 1, &pt) || !pt)
118 return;
119
120 stfb->iface_stamp = new_stamp;
121 new_stamp = p_atomic_read(&stfb->iface->stamp);
122
123 } while (stfb->iface_stamp != new_stamp);
124
125 if (vg_context_update_color_rb(ctx, pt) ||
126 stfb->width != pt->width0 ||
127 stfb->height != pt->height0)
128 ++stfb->stamp;
129
130 stfb->width = pt->width0;
131 stfb->height = pt->height0;
132 }
133
134 if (ctx->draw_stamp != stfb->stamp) {
135 ctx->state.dirty |= FRAMEBUFFER_DIRTY;
136 ctx->draw_stamp = stfb->stamp;
137 }
138 }
139
140 static void
141 vg_context_flush(struct st_context_iface *stctxi, unsigned flags,
142 struct pipe_fence_handle **fence)
143 {
144 struct vg_context *ctx = (struct vg_context *) stctxi;
145 unsigned pipe_flags = 0;
146
147 if (flags & ST_FLUSH_END_OF_FRAME) {
148 pipe_flags |= PIPE_FLUSH_END_OF_FRAME;
149 }
150
151 ctx->pipe->flush(ctx->pipe, fence, pipe_flags);
152 if (flags & ST_FLUSH_FRONT)
153 vg_manager_flush_frontbuffer(ctx);
154 }
155
156 static void
157 vg_context_destroy(struct st_context_iface *stctxi)
158 {
159 struct vg_context *ctx = (struct vg_context *) stctxi;
160 struct pipe_context *pipe = ctx->pipe;
161
162 vg_destroy_context(ctx);
163 pipe->destroy(pipe);
164 }
165
166 static struct st_context_iface *
167 vg_api_create_context(struct st_api *stapi, struct st_manager *smapi,
168 const struct st_context_attribs *attribs,
169 enum st_context_error *error,
170 struct st_context_iface *shared_stctxi)
171 {
172 struct vg_context *shared_ctx = (struct vg_context *) shared_stctxi;
173 struct vg_context *ctx;
174 struct pipe_context *pipe;
175
176 if (!(stapi->profile_mask & (1 << attribs->profile))) {
177 *error = ST_CONTEXT_ERROR_BAD_API;
178 return NULL;
179 }
180
181 /* only 1.0 is supported */
182 if (attribs->major > 1 || (attribs->major == 1 && attribs->minor > 0)) {
183 *error = ST_CONTEXT_ERROR_BAD_VERSION;
184 return NULL;
185 }
186
187 /* for VGHandle / pointer lookups */
188 init_handles();
189
190 pipe = smapi->screen->context_create(smapi->screen, NULL);
191 if (!pipe) {
192 *error = ST_CONTEXT_ERROR_NO_MEMORY;
193 return NULL;
194 }
195 ctx = vg_create_context(pipe, NULL, shared_ctx);
196 if (!ctx) {
197 pipe->destroy(pipe);
198 *error = ST_CONTEXT_ERROR_NO_MEMORY;
199 return NULL;
200 }
201
202 ctx->iface.destroy = vg_context_destroy;
203
204 ctx->iface.flush = vg_context_flush;
205
206 ctx->iface.teximage = NULL;
207 ctx->iface.copy = NULL;
208
209 ctx->iface.st_context_private = (void *) smapi;
210
211 return &ctx->iface;
212 }
213
214 static struct st_renderbuffer *
215 create_renderbuffer(enum pipe_format format)
216 {
217 struct st_renderbuffer *strb;
218
219 strb = CALLOC_STRUCT(st_renderbuffer);
220 if (strb)
221 strb->format = format;
222
223 return strb;
224 }
225
226 static void
227 destroy_renderbuffer(struct st_renderbuffer *strb)
228 {
229 pipe_surface_reference(&strb->surface, NULL);
230 pipe_resource_reference(&strb->texture, NULL);
231 FREE(strb);
232 }
233
234 /**
235 * Decide the buffer to render to.
236 */
237 static enum st_attachment_type
238 choose_attachment(struct st_framebuffer_iface *stfbi)
239 {
240 enum st_attachment_type statt;
241
242 statt = stfbi->visual->render_buffer;
243 if (statt != ST_ATTACHMENT_INVALID) {
244 /* use the buffer given by the visual, unless it is unavailable */
245 if (!st_visual_have_buffers(stfbi->visual, 1 << statt)) {
246 switch (statt) {
247 case ST_ATTACHMENT_BACK_LEFT:
248 statt = ST_ATTACHMENT_FRONT_LEFT;
249 break;
250 case ST_ATTACHMENT_BACK_RIGHT:
251 statt = ST_ATTACHMENT_FRONT_RIGHT;
252 break;
253 default:
254 break;
255 }
256
257 if (!st_visual_have_buffers(stfbi->visual, 1 << statt))
258 statt = ST_ATTACHMENT_INVALID;
259 }
260 }
261
262 return statt;
263 }
264
265 /**
266 * Bind the context to the given framebuffers.
267 */
268 static boolean
269 vg_context_bind_framebuffers(struct st_context_iface *stctxi,
270 struct st_framebuffer_iface *stdrawi,
271 struct st_framebuffer_iface *streadi)
272 {
273 struct vg_context *ctx = (struct vg_context *) stctxi;
274 struct st_framebuffer *stfb;
275 enum st_attachment_type strb_att;
276
277 /* the draw and read framebuffers must be the same */
278 if (stdrawi != streadi)
279 return FALSE;
280
281 strb_att = (stdrawi) ? choose_attachment(stdrawi) : ST_ATTACHMENT_INVALID;
282
283 if (ctx->draw_buffer) {
284 stfb = ctx->draw_buffer;
285
286 /* free the existing fb */
287 if (!stdrawi ||
288 stfb->strb_att != strb_att ||
289 stfb->strb->format != stdrawi->visual->color_format) {
290 destroy_renderbuffer(stfb->strb);
291 destroy_renderbuffer(stfb->dsrb);
292 FREE(stfb);
293
294 ctx->draw_buffer = NULL;
295 }
296 }
297
298 if (!stdrawi)
299 return TRUE;
300
301 if (strb_att == ST_ATTACHMENT_INVALID)
302 return FALSE;
303
304 /* create a new fb */
305 if (!ctx->draw_buffer) {
306 stfb = CALLOC_STRUCT(st_framebuffer);
307 if (!stfb)
308 return FALSE;
309
310 stfb->strb = create_renderbuffer(stdrawi->visual->color_format);
311 if (!stfb->strb) {
312 FREE(stfb);
313 return FALSE;
314 }
315
316 stfb->dsrb = create_renderbuffer(ctx->ds_format);
317 if (!stfb->dsrb) {
318 FREE(stfb->strb);
319 FREE(stfb);
320 return FALSE;
321 }
322
323 stfb->width = 0;
324 stfb->height = 0;
325 stfb->strb_att = strb_att;
326 stfb->stamp = 1;
327 stfb->iface_stamp = p_atomic_read(&stdrawi->stamp) - 1;
328
329 ctx->draw_buffer = stfb;
330 }
331
332 ctx->draw_buffer->iface = stdrawi;
333 ctx->draw_stamp = ctx->draw_buffer->stamp - 1;
334
335 return TRUE;
336 }
337
338 static boolean
339 vg_api_make_current(struct st_api *stapi, struct st_context_iface *stctxi,
340 struct st_framebuffer_iface *stdrawi,
341 struct st_framebuffer_iface *streadi)
342 {
343 struct vg_context *ctx = (struct vg_context *) stctxi;
344
345 if (stctxi)
346 vg_context_bind_framebuffers(stctxi, stdrawi, streadi);
347 vg_set_current_context(ctx);
348
349 return TRUE;
350 }
351
352 static struct st_context_iface *
353 vg_api_get_current(struct st_api *stapi)
354 {
355 struct vg_context *ctx = vg_current_context();
356
357 return (ctx) ? &ctx->iface : NULL;
358 }
359
360 static st_proc_t
361 vg_api_get_proc_address(struct st_api *stapi, const char *procname)
362 {
363 return api_get_proc_address(procname);
364 }
365
366 static void
367 vg_api_destroy(struct st_api *stapi)
368 {
369 }
370
371 static const struct st_api vg_api = {
372 "Vega " PACKAGE_VERSION,
373 ST_API_OPENVG,
374 ST_PROFILE_DEFAULT_MASK,
375 0,
376 vg_api_destroy,
377 vg_api_get_proc_address,
378 vg_api_create_context,
379 vg_api_make_current,
380 vg_api_get_current,
381 };
382
383 const struct st_api *
384 vg_api_get(void)
385 {
386 return &vg_api;
387 }