3b04816df04dfc8533040b92aa88e5356307e799
[mesa.git] / src / gallium / state_trackers / vega / vg_manager.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.9
4 *
5 * Copyright 2009 VMware, Inc. All Rights Reserved.
6 * Copyright (C) 2010 LunarG Inc.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
25 *
26 * Authors:
27 * Chia-I Wu <olv@lunarg.com>
28 */
29
30 #include "state_tracker/st_api.h"
31
32 #include "pipe/p_context.h"
33 #include "pipe/p_screen.h"
34 #include "util/u_memory.h"
35 #include "util/u_inlines.h"
36 #include "util/u_format.h"
37 #include "util/u_sampler.h"
38
39 #include "vg_api.h"
40 #include "vg_manager.h"
41 #include "vg_context.h"
42 #include "image.h"
43 #include "mask.h"
44 #include "api.h"
45
46 static struct pipe_resource *
47 create_texture(struct pipe_context *pipe, enum pipe_format format,
48 VGint width, VGint height)
49 {
50 struct pipe_resource templ;
51
52 memset(&templ, 0, sizeof(templ));
53
54 if (format != PIPE_FORMAT_NONE) {
55 templ.format = format;
56 }
57 else {
58 templ.format = PIPE_FORMAT_B8G8R8A8_UNORM;
59 }
60
61 templ.target = PIPE_TEXTURE_2D;
62 templ.width0 = width;
63 templ.height0 = height;
64 templ.depth0 = 1;
65 templ.last_level = 0;
66
67 if (util_format_get_component_bits(format, UTIL_FORMAT_COLORSPACE_ZS, 1)) {
68 templ.bind = PIPE_BIND_DEPTH_STENCIL;
69 } else {
70 templ.bind = (PIPE_BIND_DISPLAY_TARGET |
71 PIPE_BIND_RENDER_TARGET |
72 PIPE_BIND_SAMPLER_VIEW);
73 }
74
75 return pipe->screen->resource_create(pipe->screen, &templ);
76 }
77
78 static struct pipe_sampler_view *
79 create_tex_and_view(struct pipe_context *pipe, enum pipe_format format,
80 VGint width, VGint height)
81 {
82 struct pipe_resource *texture;
83 struct pipe_sampler_view view_templ;
84 struct pipe_sampler_view *view;
85
86 texture = create_texture(pipe, format, width, height);
87
88 if (!texture)
89 return NULL;
90
91 u_sampler_view_default_template(&view_templ, texture, texture->format);
92 view = pipe->create_sampler_view(pipe, texture, &view_templ);
93 /* want the texture to go away if the view is freed */
94 pipe_resource_reference(&texture, NULL);
95
96 return view;
97 }
98
99 static void
100 setup_new_alpha_mask(struct vg_context *ctx, struct st_framebuffer *stfb)
101 {
102 struct pipe_context *pipe = ctx->pipe;
103 struct pipe_sampler_view *old_sampler_view = stfb->alpha_mask_view;
104
105 /*
106 we use PIPE_FORMAT_B8G8R8A8_UNORM because we want to render to
107 this texture and use it as a sampler, so while this wastes some
108 space it makes both of those a lot simpler
109 */
110 stfb->alpha_mask_view = create_tex_and_view(pipe,
111 PIPE_FORMAT_B8G8R8A8_UNORM, stfb->width, stfb->height);
112
113 if (!stfb->alpha_mask_view) {
114 if (old_sampler_view)
115 pipe_sampler_view_reference(&old_sampler_view, NULL);
116 return;
117 }
118
119 /* XXX could this call be avoided? */
120 vg_validate_state(ctx);
121
122 /* alpha mask starts with 1.f alpha */
123 mask_fill(0, 0, stfb->width, stfb->height, 1.f);
124
125 /* if we had an old surface copy it over */
126 if (old_sampler_view) {
127 struct pipe_subresource subsurf, subold_surf;
128 subsurf.face = 0;
129 subsurf.level = 0;
130 subold_surf.face = 0;
131 subold_surf.level = 0;
132 pipe->resource_copy_region(pipe,
133 stfb->alpha_mask_view->texture,
134 subsurf,
135 0, 0, 0,
136 old_sampler_view->texture,
137 subold_surf,
138 0, 0, 0,
139 MIN2(old_sampler_view->texture->width0,
140 stfb->alpha_mask_view->texture->width0),
141 MIN2(old_sampler_view->texture->height0,
142 stfb->alpha_mask_view->texture->height0));
143 }
144
145 /* Free the old texture
146 */
147 if (old_sampler_view)
148 pipe_sampler_view_reference(&old_sampler_view, NULL);
149 }
150
151 static boolean
152 vg_context_update_depth_stencil_rb(struct vg_context * ctx,
153 uint width, uint height)
154 {
155 struct st_renderbuffer *dsrb = ctx->draw_buffer->dsrb;
156 struct pipe_context *pipe = ctx->pipe;
157 unsigned surface_usage;
158
159 if ((dsrb->width == width && dsrb->height == height) && dsrb->texture)
160 return FALSE;
161
162 /* unreference existing ones */
163 pipe_surface_reference(&dsrb->surface, NULL);
164 pipe_resource_reference(&dsrb->texture, NULL);
165 dsrb->width = dsrb->height = 0;
166
167 /* Probably need dedicated flags for surface usage too:
168 */
169 surface_usage = PIPE_BIND_DEPTH_STENCIL; /* XXX: was: RENDER_TARGET */
170
171 dsrb->texture = create_texture(pipe, dsrb->format, width, height);
172 if (!dsrb->texture)
173 return TRUE;
174
175 dsrb->surface = pipe->screen->get_tex_surface(pipe->screen,
176 dsrb->texture,
177 0, 0, 0,
178 surface_usage);
179 if (!dsrb->surface) {
180 pipe_resource_reference(&dsrb->texture, NULL);
181 return TRUE;
182 }
183
184 dsrb->width = width;
185 dsrb->height = height;
186
187 assert(dsrb->surface->width == width);
188 assert(dsrb->surface->height == height);
189
190 return TRUE;
191 }
192
193 static boolean
194 vg_context_update_color_rb(struct vg_context *ctx, struct pipe_resource *pt)
195 {
196 struct st_renderbuffer *strb = ctx->draw_buffer->strb;
197 struct pipe_screen *screen = ctx->pipe->screen;
198
199 if (strb->texture == pt) {
200 pipe_resource_reference(&pt, NULL);
201 return FALSE;
202 }
203
204 /* unreference existing ones */
205 pipe_surface_reference(&strb->surface, NULL);
206 pipe_resource_reference(&strb->texture, NULL);
207 strb->width = strb->height = 0;
208
209 strb->texture = pt;
210 strb->surface = screen->get_tex_surface(screen, strb->texture, 0, 0, 0,
211 PIPE_BIND_RENDER_TARGET);
212 if (!strb->surface) {
213 pipe_resource_reference(&strb->texture, NULL);
214 return TRUE;
215 }
216
217 strb->width = pt->width0;
218 strb->height = pt->height0;
219
220 return TRUE;
221 }
222
223 static void
224 vg_context_update_draw_buffer(struct vg_context *ctx, struct pipe_resource *pt)
225 {
226 struct st_framebuffer *stfb = ctx->draw_buffer;
227 boolean new_cbuf, new_zsbuf, new_size;
228
229 new_cbuf = vg_context_update_color_rb(ctx, pt);
230 new_zsbuf =
231 vg_context_update_depth_stencil_rb(ctx, pt->width0, pt->height0);
232
233 new_size = (stfb->width != pt->width0 || stfb->height != pt->height0);
234 stfb->width = pt->width0;
235 stfb->height = pt->height0;
236
237 if (new_cbuf || new_zsbuf || new_size) {
238 struct pipe_framebuffer_state *state = &ctx->state.g3d.fb;
239
240 memset(state, 0, sizeof(struct pipe_framebuffer_state));
241 state->width = stfb->width;
242 state->height = stfb->height;
243 state->nr_cbufs = 1;
244 state->cbufs[0] = stfb->strb->surface;
245 state->zsbuf = stfb->dsrb->surface;
246
247 cso_set_framebuffer(ctx->cso_context, state);
248 }
249
250 if (new_zsbuf || new_size) {
251 ctx->state.dirty |= VIEWPORT_DIRTY;
252 ctx->state.dirty |= DEPTH_STENCIL_DIRTY;/*to reset the scissors*/
253
254 ctx->pipe->clear(ctx->pipe, PIPE_CLEAR_DEPTHSTENCIL, NULL, 0.0, 0);
255
256 /* we need all the other state already set */
257
258 setup_new_alpha_mask(ctx, stfb);
259
260 pipe_sampler_view_reference( &stfb->blend_texture_view, NULL);
261 stfb->blend_texture_view = create_tex_and_view(ctx->pipe,
262 PIPE_FORMAT_B8G8R8A8_UNORM, stfb->width, stfb->height);
263 }
264 }
265
266 /**
267 * Flush the front buffer if the current context renders to the front buffer.
268 */
269 void
270 vg_manager_flush_frontbuffer(struct vg_context *ctx)
271 {
272 struct st_framebuffer *stfb = ctx->draw_buffer;
273
274 if (!stfb)
275 return;
276
277 switch (stfb->strb_att) {
278 case ST_ATTACHMENT_FRONT_LEFT:
279 case ST_ATTACHMENT_FRONT_RIGHT:
280 stfb->iface->flush_front(stfb->iface, stfb->strb_att);
281 break;
282 default:
283 break;
284 }
285 }
286
287 /**
288 * Re-validate the framebuffer.
289 */
290 void
291 vg_manager_validate_framebuffer(struct vg_context *ctx)
292 {
293 struct st_framebuffer *stfb = ctx->draw_buffer;
294 struct pipe_resource *pt;
295
296 /* no binding surface */
297 if (!stfb)
298 return;
299
300 if (!p_atomic_read(&ctx->draw_buffer_invalid))
301 return;
302
303 /* validate the fb */
304 if (!stfb->iface->validate(stfb->iface, &stfb->strb_att, 1, &pt) || !pt)
305 return;
306
307 /*
308 * unset draw_buffer_invalid first because vg_context_update_draw_buffer
309 * will cause the framebuffer to be validated again because of a call to
310 * vg_validate_state
311 */
312 p_atomic_set(&ctx->draw_buffer_invalid, FALSE);
313 vg_context_update_draw_buffer(ctx, pt);
314 }
315
316
317 static void
318 vg_context_notify_invalid_framebuffer(struct st_context_iface *stctxi,
319 struct st_framebuffer_iface *stfbi)
320 {
321 struct vg_context *ctx = (struct vg_context *) stctxi;
322 p_atomic_set(&ctx->draw_buffer_invalid, TRUE);
323 }
324
325 static void
326 vg_context_flush(struct st_context_iface *stctxi, unsigned flags,
327 struct pipe_fence_handle **fence)
328 {
329 struct vg_context *ctx = (struct vg_context *) stctxi;
330 ctx->pipe->flush(ctx->pipe, flags, fence);
331 if (flags & PIPE_FLUSH_RENDER_CACHE)
332 vg_manager_flush_frontbuffer(ctx);
333 }
334
335 static void
336 vg_context_destroy(struct st_context_iface *stctxi)
337 {
338 struct vg_context *ctx = (struct vg_context *) stctxi;
339 vg_destroy_context(ctx);
340 }
341
342 static struct st_context_iface *
343 vg_api_create_context(struct st_api *stapi, struct st_manager *smapi,
344 const struct st_visual *visual,
345 struct st_context_iface *shared_stctxi)
346 {
347 struct vg_context *shared_ctx = (struct vg_context *) shared_stctxi;
348 struct vg_context *ctx;
349 struct pipe_context *pipe;
350
351 pipe = smapi->screen->context_create(smapi->screen, NULL);
352 if (!pipe)
353 return NULL;
354 ctx = vg_create_context(pipe, NULL, shared_ctx);
355 if (!ctx) {
356 pipe->destroy(pipe);
357 return NULL;
358 }
359
360 ctx->iface.destroy = vg_context_destroy;
361
362 ctx->iface.notify_invalid_framebuffer =
363 vg_context_notify_invalid_framebuffer;
364 ctx->iface.flush = vg_context_flush;
365
366 ctx->iface.teximage = NULL;
367 ctx->iface.copy = NULL;
368
369 ctx->iface.st_context_private = (void *) smapi;
370
371 return &ctx->iface;
372 }
373
374 static struct st_renderbuffer *
375 create_renderbuffer(enum pipe_format format)
376 {
377 struct st_renderbuffer *strb;
378
379 strb = CALLOC_STRUCT(st_renderbuffer);
380 if (strb)
381 strb->format = format;
382
383 return strb;
384 }
385
386 static void
387 destroy_renderbuffer(struct st_renderbuffer *strb)
388 {
389 pipe_surface_reference(&strb->surface, NULL);
390 pipe_resource_reference(&strb->texture, NULL);
391 free(strb);
392 }
393
394 /**
395 * Decide the buffer to render to.
396 */
397 static enum st_attachment_type
398 choose_attachment(struct st_framebuffer_iface *stfbi)
399 {
400 enum st_attachment_type statt;
401
402 statt = stfbi->visual->render_buffer;
403 if (statt != ST_ATTACHMENT_INVALID) {
404 /* use the buffer given by the visual, unless it is unavailable */
405 if (!st_visual_have_buffers(stfbi->visual, 1 << statt)) {
406 switch (statt) {
407 case ST_ATTACHMENT_BACK_LEFT:
408 statt = ST_ATTACHMENT_FRONT_LEFT;
409 break;
410 case ST_ATTACHMENT_BACK_RIGHT:
411 statt = ST_ATTACHMENT_FRONT_RIGHT;
412 break;
413 default:
414 break;
415 }
416
417 if (!st_visual_have_buffers(stfbi->visual, 1 << statt))
418 statt = ST_ATTACHMENT_INVALID;
419 }
420 }
421
422 return statt;
423 }
424
425 /**
426 * Bind the context to the given framebuffers.
427 */
428 static boolean
429 vg_context_bind_framebuffers(struct st_context_iface *stctxi,
430 struct st_framebuffer_iface *stdrawi,
431 struct st_framebuffer_iface *streadi)
432 {
433 struct vg_context *ctx = (struct vg_context *) stctxi;
434 struct st_framebuffer *stfb;
435 enum st_attachment_type strb_att;
436
437 /* the draw and read framebuffers must be the same */
438 if (stdrawi != streadi)
439 return FALSE;
440
441 p_atomic_set(&ctx->draw_buffer_invalid, TRUE);
442
443 strb_att = (stdrawi) ? choose_attachment(stdrawi) : ST_ATTACHMENT_INVALID;
444
445 if (ctx->draw_buffer) {
446 stfb = ctx->draw_buffer;
447
448 /* free the existing fb */
449 if (!stdrawi ||
450 stfb->strb_att != strb_att ||
451 stfb->strb->format != stdrawi->visual->color_format ||
452 stfb->dsrb->format != stdrawi->visual->depth_stencil_format) {
453 destroy_renderbuffer(stfb->strb);
454 destroy_renderbuffer(stfb->dsrb);
455 free(stfb);
456
457 ctx->draw_buffer = NULL;
458 }
459 }
460
461 if (!stdrawi)
462 return TRUE;
463
464 if (strb_att == ST_ATTACHMENT_INVALID)
465 return FALSE;
466
467 /* create a new fb */
468 if (!ctx->draw_buffer) {
469 stfb = CALLOC_STRUCT(st_framebuffer);
470 if (!stfb)
471 return FALSE;
472
473 stfb->strb = create_renderbuffer(stdrawi->visual->color_format);
474 if (!stfb->strb) {
475 free(stfb);
476 return FALSE;
477 }
478
479 stfb->dsrb = create_renderbuffer(stdrawi->visual->depth_stencil_format);
480 if (!stfb->dsrb) {
481 free(stfb->strb);
482 free(stfb);
483 return FALSE;
484 }
485
486 stfb->width = 0;
487 stfb->height = 0;
488 stfb->strb_att = strb_att;
489
490 ctx->draw_buffer = stfb;
491 }
492
493 ctx->draw_buffer->iface = stdrawi;
494
495 return TRUE;
496 }
497
498 static boolean
499 vg_api_make_current(struct st_api *stapi, struct st_context_iface *stctxi,
500 struct st_framebuffer_iface *stdrawi,
501 struct st_framebuffer_iface *streadi)
502 {
503 struct vg_context *ctx = (struct vg_context *) stctxi;
504
505 if (stctxi)
506 vg_context_bind_framebuffers(stctxi, stdrawi, streadi);
507 vg_set_current_context(ctx);
508
509 return TRUE;
510 }
511
512 static struct st_context_iface *
513 vg_api_get_current(struct st_api *stapi)
514 {
515 struct vg_context *ctx = vg_current_context();
516
517 return (ctx) ? &ctx->iface : NULL;
518 }
519
520 static boolean
521 vg_api_is_visual_supported(struct st_api *stapi,
522 const struct st_visual *visual)
523 {
524 /* the impl requires a depth/stencil buffer */
525 return util_format_is_depth_and_stencil(visual->depth_stencil_format);
526 }
527
528 static st_proc_t
529 vg_api_get_proc_address(struct st_api *stapi, const char *procname)
530 {
531 return api_get_proc_address(procname);
532 }
533
534 static void
535 vg_api_destroy(struct st_api *stapi)
536 {
537 }
538
539 static const struct st_api vg_api = {
540 vg_api_destroy,
541 vg_api_get_proc_address,
542 vg_api_is_visual_supported,
543 vg_api_create_context,
544 vg_api_make_current,
545 vg_api_get_current,
546 };
547
548 const struct st_api *
549 vg_api_get(void)
550 {
551 return &vg_api;
552 }