Merge branch '7.8' into master
[mesa.git] / src / mesa / state_tracker / st_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_format.h"
33 #include "util/u_pointer.h"
34 #include "util/u_inlines.h"
35 #include "util/u_atomic.h"
36
37 #include "main/mtypes.h"
38 #include "main/context.h"
39 #include "main/texobj.h"
40 #include "main/teximage.h"
41 #include "main/texstate.h"
42 #include "main/texfetch.h"
43 #include "main/fbobject.h"
44 #include "main/framebuffer.h"
45 #include "main/renderbuffer.h"
46 #include "st_texture.h"
47
48 #include "st_context.h"
49 #include "st_format.h"
50 #include "st_cb_fbo.h"
51 #include "st_manager.h"
52
53 /* these functions are defined in st_context.c */
54 struct st_context *
55 st_create_context(struct pipe_context *pipe,
56 const __GLcontextModes *visual,
57 struct st_context *share);
58 void st_destroy_context(struct st_context *st);
59 void st_flush(struct st_context *st, uint pipeFlushFlags,
60 struct pipe_fence_handle **fence);
61
62 /**
63 * Note that this function may fail.
64 */
65 static INLINE struct st_framebuffer *
66 st_framebuffer(GLframebuffer *fb)
67 {
68 /* FBO cannot be casted. See st_new_framebuffer */
69 return (struct st_framebuffer *) ((fb && !fb->Name) ? fb : NULL);
70 }
71
72 /**
73 * Map an attachment to a buffer index.
74 */
75 static INLINE gl_buffer_index
76 attachment_to_buffer_index(enum st_attachment_type statt)
77 {
78 gl_buffer_index index;
79
80 switch (statt) {
81 case ST_ATTACHMENT_FRONT_LEFT:
82 index = BUFFER_FRONT_LEFT;
83 break;
84 case ST_ATTACHMENT_BACK_LEFT:
85 index = BUFFER_BACK_LEFT;
86 break;
87 case ST_ATTACHMENT_FRONT_RIGHT:
88 index = BUFFER_FRONT_RIGHT;
89 break;
90 case ST_ATTACHMENT_BACK_RIGHT:
91 index = BUFFER_BACK_RIGHT;
92 break;
93 case ST_ATTACHMENT_DEPTH_STENCIL:
94 index = BUFFER_DEPTH;
95 break;
96 case ST_ATTACHMENT_ACCUM:
97 index = BUFFER_ACCUM;
98 break;
99 case ST_ATTACHMENT_SAMPLE:
100 default:
101 index = BUFFER_COUNT;
102 break;
103 }
104
105 return index;
106 }
107
108 /**
109 * Map a buffer index to an attachment.
110 */
111 static INLINE enum st_attachment_type
112 buffer_index_to_attachment(gl_buffer_index index)
113 {
114 enum st_attachment_type statt;
115
116 switch (index) {
117 case BUFFER_FRONT_LEFT:
118 statt = ST_ATTACHMENT_FRONT_LEFT;
119 break;
120 case BUFFER_BACK_LEFT:
121 statt = ST_ATTACHMENT_BACK_LEFT;
122 break;
123 case BUFFER_FRONT_RIGHT:
124 statt = ST_ATTACHMENT_FRONT_RIGHT;
125 break;
126 case BUFFER_BACK_RIGHT:
127 statt = ST_ATTACHMENT_BACK_RIGHT;
128 break;
129 case BUFFER_DEPTH:
130 statt = ST_ATTACHMENT_DEPTH_STENCIL;
131 break;
132 case BUFFER_ACCUM:
133 statt = ST_ATTACHMENT_ACCUM;
134 break;
135 default:
136 statt = ST_ATTACHMENT_INVALID;
137 break;
138 }
139
140 return statt;
141 }
142
143 /**
144 * Validate a framebuffer to make sure up-to-date pipe_textures are used.
145 */
146 static void
147 st_framebuffer_validate(struct st_framebuffer *stfb, struct st_context *st)
148 {
149 struct pipe_screen *screen = st->pipe->screen;
150 struct pipe_texture *textures[ST_ATTACHMENT_COUNT];
151 uint width, height;
152 unsigned i;
153 boolean changed = FALSE;
154
155 if (!p_atomic_read(&stfb->revalidate))
156 return;
157
158 /* validate the fb */
159 if (!stfb->iface->validate(stfb->iface, stfb->statts, stfb->num_statts, textures))
160 return;
161
162 width = stfb->Base.Width;
163 height = stfb->Base.Height;
164
165 for (i = 0; i < stfb->num_statts; i++) {
166 struct st_renderbuffer *strb;
167 struct pipe_surface *ps;
168 gl_buffer_index idx;
169
170 if (!textures[i])
171 continue;
172
173 idx = attachment_to_buffer_index(stfb->statts[i]);
174 if (idx >= BUFFER_COUNT) {
175 pipe_texture_reference(&textures[i], NULL);
176 continue;
177 }
178
179 strb = st_renderbuffer(stfb->Base.Attachment[idx].Renderbuffer);
180 assert(strb);
181 if (strb->texture == textures[i]) {
182 pipe_texture_reference(&textures[i], NULL);
183 continue;
184 }
185
186 ps = screen->get_tex_surface(screen, textures[i], 0, 0, 0,
187 PIPE_BUFFER_USAGE_GPU_READ | PIPE_BUFFER_USAGE_GPU_WRITE);
188 if (ps) {
189 pipe_surface_reference(&strb->surface, ps);
190 pipe_texture_reference(&strb->texture, ps->texture);
191 /* ownership transfered */
192 pipe_surface_reference(&ps, NULL);
193
194 changed = TRUE;
195
196 strb->Base.Width = strb->surface->width;
197 strb->Base.Height = strb->surface->height;
198
199 width = strb->Base.Width;
200 height = strb->Base.Height;
201 }
202
203 pipe_texture_reference(&textures[i], NULL);
204 }
205
206 if (changed) {
207 st->dirty.st |= ST_NEW_FRAMEBUFFER;
208 _mesa_resize_framebuffer(st->ctx, &stfb->Base, width, height);
209
210 assert(stfb->Base.Width == width);
211 assert(stfb->Base.Height == height);
212 }
213
214 p_atomic_set(&stfb->revalidate, FALSE);
215 }
216
217 /**
218 * Update the attachments to validate by looping the existing renderbuffers.
219 */
220 static void
221 st_framebuffer_update_attachments(struct st_framebuffer *stfb)
222 {
223 gl_buffer_index idx;
224
225 stfb->num_statts = 0;
226 for (idx = 0; idx < BUFFER_COUNT; idx++) {
227 struct st_renderbuffer *strb;
228 enum st_attachment_type statt;
229
230 strb = st_renderbuffer(stfb->Base.Attachment[idx].Renderbuffer);
231 if (!strb || strb->software)
232 continue;
233
234 statt = buffer_index_to_attachment(idx);
235 if (statt != ST_ATTACHMENT_INVALID &&
236 st_visual_have_buffers(stfb->iface->visual, 1 << statt))
237 stfb->statts[stfb->num_statts++] = statt;
238 }
239
240 p_atomic_set(&stfb->revalidate, TRUE);
241 }
242
243 /**
244 * Add a renderbuffer to the framebuffer.
245 */
246 static boolean
247 st_framebuffer_add_renderbuffer(struct st_framebuffer *stfb,
248 gl_buffer_index idx)
249 {
250 struct gl_renderbuffer *rb;
251 enum pipe_format format;
252 int samples;
253 boolean sw;
254
255 /* do not distinguish depth/stencil buffers */
256 if (idx == BUFFER_STENCIL)
257 idx = BUFFER_DEPTH;
258
259 switch (idx) {
260 case BUFFER_DEPTH:
261 format = stfb->iface->visual->depth_stencil_format;
262 sw = FALSE;
263 break;
264 case BUFFER_ACCUM:
265 format = stfb->iface->visual->accum_format;
266 sw = TRUE;
267 break;
268 default:
269 format = stfb->iface->visual->color_format;
270 sw = FALSE;
271 break;
272 }
273
274 if (format == PIPE_FORMAT_NONE)
275 return FALSE;
276
277 samples = stfb->iface->visual->samples;
278 if (!samples)
279 samples = st_get_msaa();
280
281 rb = st_new_renderbuffer_fb(format, samples, sw);
282 if (!rb)
283 return FALSE;
284
285 if (idx != BUFFER_DEPTH) {
286 _mesa_add_renderbuffer(&stfb->Base, idx, rb);
287 }
288 else {
289 if (util_format_get_component_bits(format, UTIL_FORMAT_COLORSPACE_ZS, 0))
290 _mesa_add_renderbuffer(&stfb->Base, BUFFER_DEPTH, rb);
291 if (util_format_get_component_bits(format, UTIL_FORMAT_COLORSPACE_ZS, 1))
292 _mesa_add_renderbuffer(&stfb->Base, BUFFER_STENCIL, rb);
293 }
294
295 return TRUE;
296 }
297
298 /**
299 * Intialize a __GLcontextModes from a visual.
300 */
301 static void
302 st_visual_to_context_mode(const struct st_visual *visual,
303 __GLcontextModes *mode)
304 {
305 memset(mode, 0, sizeof(*mode));
306
307 if (st_visual_have_buffers(visual, ST_ATTACHMENT_BACK_LEFT_MASK))
308 mode->doubleBufferMode = GL_TRUE;
309 if (st_visual_have_buffers(visual,
310 ST_ATTACHMENT_FRONT_RIGHT_MASK | ST_ATTACHMENT_BACK_RIGHT_MASK))
311 mode->stereoMode = GL_TRUE;
312
313 if (visual->color_format != PIPE_FORMAT_NONE) {
314 mode->rgbMode = GL_TRUE;
315
316 mode->redBits =
317 util_format_get_component_bits(visual->color_format,
318 UTIL_FORMAT_COLORSPACE_RGB, 0);
319 mode->greenBits =
320 util_format_get_component_bits(visual->color_format,
321 UTIL_FORMAT_COLORSPACE_RGB, 1);
322 mode->blueBits =
323 util_format_get_component_bits(visual->color_format,
324 UTIL_FORMAT_COLORSPACE_RGB, 2);
325 mode->alphaBits =
326 util_format_get_component_bits(visual->color_format,
327 UTIL_FORMAT_COLORSPACE_RGB, 3);
328
329 mode->rgbBits = mode->redBits +
330 mode->greenBits + mode->blueBits + mode->alphaBits;
331 }
332
333 if (visual->depth_stencil_format != PIPE_FORMAT_NONE) {
334 mode->haveDepthBuffer = GL_TRUE;
335 mode->haveStencilBuffer = GL_TRUE;
336
337 mode->depthBits =
338 util_format_get_component_bits(visual->depth_stencil_format,
339 UTIL_FORMAT_COLORSPACE_ZS, 0);
340 mode->stencilBits =
341 util_format_get_component_bits(visual->depth_stencil_format,
342 UTIL_FORMAT_COLORSPACE_ZS, 1);
343 }
344
345 if (visual->accum_format != PIPE_FORMAT_NONE) {
346 mode->haveAccumBuffer = GL_TRUE;
347
348 mode->accumRedBits =
349 util_format_get_component_bits(visual->accum_format,
350 UTIL_FORMAT_COLORSPACE_RGB, 0);
351 mode->accumGreenBits =
352 util_format_get_component_bits(visual->accum_format,
353 UTIL_FORMAT_COLORSPACE_RGB, 1);
354 mode->accumBlueBits =
355 util_format_get_component_bits(visual->accum_format,
356 UTIL_FORMAT_COLORSPACE_RGB, 2);
357 mode->accumAlphaBits =
358 util_format_get_component_bits(visual->accum_format,
359 UTIL_FORMAT_COLORSPACE_RGB, 3);
360 }
361
362 if (visual->samples) {
363 mode->sampleBuffers = 1;
364 mode->samples = visual->samples;
365 }
366 }
367
368 /**
369 * Determine the default draw or read buffer from a visual.
370 */
371 static void
372 st_visual_to_default_buffer(const struct st_visual *visual,
373 GLenum *buffer, GLint *index)
374 {
375 enum st_attachment_type statt;
376 GLenum buf;
377 gl_buffer_index idx;
378
379 statt = visual->render_buffer;
380 /* do nothing if an invalid render buffer is specified */
381 if (statt == ST_ATTACHMENT_INVALID ||
382 !st_visual_have_buffers(visual, 1 << statt))
383 return;
384
385 switch (statt) {
386 case ST_ATTACHMENT_FRONT_LEFT:
387 buf = GL_FRONT_LEFT;
388 idx = BUFFER_FRONT_LEFT;
389 break;
390 case ST_ATTACHMENT_BACK_LEFT:
391 buf = GL_BACK_LEFT;
392 idx = BUFFER_BACK_LEFT;
393 break;
394 case ST_ATTACHMENT_FRONT_RIGHT:
395 buf = GL_FRONT_RIGHT;
396 idx = BUFFER_FRONT_RIGHT;
397 break;
398 case ST_ATTACHMENT_BACK_RIGHT:
399 buf = GL_BACK_RIGHT;
400 idx = BUFFER_BACK_RIGHT;
401 break;
402 default:
403 buf = GL_NONE;
404 idx = BUFFER_COUNT;
405 break;
406 }
407
408 if (buf != GL_NONE) {
409 if (buffer)
410 *buffer = buf;
411 if (index)
412 *index = idx;
413 }
414 }
415
416 /**
417 * Create a framebuffer from a manager interface.
418 */
419 static struct st_framebuffer *
420 st_framebuffer_create(struct st_framebuffer_iface *stfbi)
421 {
422 struct st_framebuffer *stfb;
423 __GLcontextModes mode;
424 gl_buffer_index idx;
425
426 stfb = CALLOC_STRUCT(st_framebuffer);
427 if (!stfb)
428 return NULL;
429
430 st_visual_to_context_mode(stfbi->visual, &mode);
431 _mesa_initialize_window_framebuffer(&stfb->Base, &mode);
432
433 /* modify the draw/read buffers of the fb */
434 st_visual_to_default_buffer(stfbi->visual, &stfb->Base.ColorDrawBuffer[0],
435 &stfb->Base._ColorDrawBufferIndexes[0]);
436 st_visual_to_default_buffer(stfbi->visual, &stfb->Base.ColorReadBuffer,
437 &stfb->Base._ColorReadBufferIndex);
438
439 stfb->iface = stfbi;
440
441 /* add the color buffer */
442 idx = stfb->Base._ColorDrawBufferIndexes[0];
443 if (!st_framebuffer_add_renderbuffer(stfb, idx)) {
444 FREE(stfb);
445 return NULL;
446 }
447
448 st_framebuffer_add_renderbuffer(stfb, BUFFER_DEPTH);
449 st_framebuffer_add_renderbuffer(stfb, BUFFER_ACCUM);
450
451 st_framebuffer_update_attachments(stfb);
452
453 stfb->Base.Initialized = GL_TRUE;
454
455 return stfb;
456 }
457
458 /**
459 * Reference a framebuffer.
460 */
461 static void
462 st_framebuffer_reference(struct st_framebuffer **ptr,
463 struct st_framebuffer *stfb)
464 {
465 GLframebuffer *fb = &stfb->Base;
466 _mesa_reference_framebuffer((GLframebuffer **) ptr, fb);
467 }
468
469 static void
470 st_context_notify_invalid_framebuffer(struct st_context_iface *stctxi,
471 struct st_framebuffer_iface *stfbi)
472 {
473 struct st_context *st = (struct st_context *) stctxi;
474 struct st_framebuffer *stfb;
475
476 /* either draw or read winsys fb */
477 stfb = st_framebuffer(st->ctx->WinSysDrawBuffer);
478 if (!stfb || stfb->iface != stfbi)
479 stfb = st_framebuffer(st->ctx->WinSysReadBuffer);
480 assert(stfb && stfb->iface == stfbi);
481
482 p_atomic_set(&stfb->revalidate, TRUE);
483 }
484
485 static void
486 st_context_flush(struct st_context_iface *stctxi, unsigned flags,
487 struct pipe_fence_handle **fence)
488 {
489 struct st_context *st = (struct st_context *) stctxi;
490 st_flush(st, flags, fence);
491 if (flags & PIPE_FLUSH_RENDER_CACHE)
492 st_manager_flush_frontbuffer(st);
493 }
494
495 static boolean
496 st_context_teximage(struct st_context_iface *stctxi, enum st_texture_type target,
497 int level, enum pipe_format internal_format,
498 struct pipe_texture *tex, boolean mipmap)
499 {
500 struct st_context *st = (struct st_context *) stctxi;
501 GLcontext *ctx = st->ctx;
502 struct gl_texture_unit *texUnit = _mesa_get_current_tex_unit(ctx);
503 struct gl_texture_object *texObj;
504 struct gl_texture_image *texImage;
505 struct st_texture_object *stObj;
506 struct st_texture_image *stImage;
507 GLenum internalFormat;
508
509 switch (target) {
510 case ST_TEXTURE_1D:
511 target = GL_TEXTURE_1D;
512 break;
513 case ST_TEXTURE_2D:
514 target = GL_TEXTURE_2D;
515 break;
516 case ST_TEXTURE_3D:
517 target = GL_TEXTURE_3D;
518 break;
519 case ST_TEXTURE_RECT:
520 target = GL_TEXTURE_RECTANGLE_ARB;
521 break;
522 default:
523 return FALSE;
524 break;
525 }
526
527 if (util_format_get_component_bits(internal_format,
528 UTIL_FORMAT_COLORSPACE_RGB, 3) > 0)
529 internalFormat = GL_RGBA;
530 else
531 internalFormat = GL_RGB;
532
533 texObj = _mesa_select_tex_object(ctx, texUnit, target);
534 _mesa_lock_texture(ctx, texObj);
535
536 stObj = st_texture_object(texObj);
537 /* switch to surface based */
538 if (!stObj->surface_based) {
539 _mesa_clear_texture_object(ctx, texObj);
540 stObj->surface_based = GL_TRUE;
541 }
542
543 texImage = _mesa_get_tex_image(ctx, texObj, target, level);
544 stImage = st_texture_image(texImage);
545 if (tex) {
546 _mesa_init_teximage_fields(ctx, target, texImage,
547 tex->width0, tex->height0, 1, 0, internalFormat);
548 texImage->TexFormat = st_ChooseTextureFormat(ctx, internalFormat,
549 GL_RGBA, GL_UNSIGNED_BYTE);
550 _mesa_set_fetch_functions(texImage, 2);
551 }
552 else {
553 _mesa_clear_texture_image(ctx, texImage);
554 }
555
556 pipe_texture_reference(&stImage->pt, tex);
557
558 _mesa_dirty_texobj(ctx, texObj, GL_TRUE);
559 _mesa_unlock_texture(ctx, texObj);
560
561 return TRUE;
562 }
563
564 static void
565 st_context_destroy(struct st_context_iface *stctxi)
566 {
567 struct st_context *st = (struct st_context *) stctxi;
568 st_destroy_context(st);
569 }
570
571 static struct st_context_iface *
572 st_api_create_context(struct st_api *stapi, struct st_manager *smapi,
573 const struct st_visual *visual,
574 struct st_context_iface *shared_stctxi)
575 {
576 struct st_context *shared_ctx = (struct st_context *) shared_stctxi;
577 struct st_context *st;
578 struct pipe_context *pipe;
579 __GLcontextModes mode;
580
581 pipe = smapi->screen->context_create(smapi->screen, NULL);
582 if (!pipe)
583 return NULL;
584
585 st_visual_to_context_mode(visual, &mode);
586 st = st_create_context(pipe, &mode, shared_ctx);
587 if (!st) {
588 pipe->destroy(pipe);
589 return NULL;
590 }
591
592 st->iface.destroy = st_context_destroy;
593
594 st->iface.notify_invalid_framebuffer =
595 st_context_notify_invalid_framebuffer;
596 st->iface.flush = st_context_flush;
597
598 st->iface.teximage = st_context_teximage;
599 st->iface.copy = NULL;
600
601 st->iface.st_context_private = (void *) smapi;
602
603 return &st->iface;
604 }
605
606 static boolean
607 st_api_make_current(struct st_api *stapi, struct st_context_iface *stctxi,
608 struct st_framebuffer_iface *stdrawi,
609 struct st_framebuffer_iface *streadi)
610 {
611 struct st_context *st = (struct st_context *) stctxi;
612 struct st_framebuffer *stdraw, *stread, *stfb;
613 boolean ret;
614
615 _glapi_check_multithread();
616
617 if (st) {
618 /* reuse/create the draw fb */
619 stfb = st_framebuffer(st->ctx->WinSysDrawBuffer);
620 if (stfb && stfb->iface == stdrawi) {
621 stdraw = NULL;
622 st_framebuffer_reference(&stdraw, stfb);
623 }
624 else {
625 stdraw = st_framebuffer_create(stdrawi);
626 }
627
628 /* reuse/create the read fb */
629 stfb = st_framebuffer(st->ctx->WinSysReadBuffer);
630 if (!stfb || stfb->iface != streadi)
631 stfb = stdraw;
632 if (stfb && stfb->iface == streadi) {
633 stread = NULL;
634 st_framebuffer_reference(&stread, stfb);
635 }
636 else {
637 stread = st_framebuffer_create(streadi);
638 }
639
640 if (stdraw && stread) {
641 st_framebuffer_validate(stdraw, st);
642 if (stread != stdraw)
643 st_framebuffer_validate(stread, st);
644
645 /* modify the draw/read buffers of the context */
646 st_visual_to_default_buffer(stdraw->iface->visual,
647 &st->ctx->Color.DrawBuffer[0], NULL);
648 st_visual_to_default_buffer(stread->iface->visual,
649 &st->ctx->Pixel.ReadBuffer, NULL);
650
651 ret = _mesa_make_current(st->ctx, &stdraw->Base, &stread->Base);
652 }
653 else {
654 ret = FALSE;
655 }
656
657 st_framebuffer_reference(&stdraw, NULL);
658 st_framebuffer_reference(&stread, NULL);
659 }
660 else {
661 ret = _mesa_make_current(NULL, NULL, NULL);
662 }
663
664 return ret;
665 }
666
667 static struct st_context_iface *
668 st_api_get_current(struct st_api *stapi)
669 {
670 GET_CURRENT_CONTEXT(ctx);
671 struct st_context *st = (ctx) ? ctx->st : NULL;
672
673 return (st) ? &st->iface : NULL;
674 }
675
676 static boolean
677 st_api_is_visual_supported(struct st_api *stapi,
678 const struct st_visual *visual)
679 {
680 return TRUE;
681 }
682
683 static st_proc_t
684 st_api_get_proc_address(struct st_api *stapi, const char *procname)
685 {
686 return (st_proc_t) _glapi_get_proc_address(procname);
687 }
688
689 static void
690 st_api_destroy(struct st_api *stapi)
691 {
692 FREE(stapi);
693 }
694
695 /**
696 * Flush the front buffer if the current context renders to the front buffer.
697 */
698 void
699 st_manager_flush_frontbuffer(struct st_context *st)
700 {
701 struct st_framebuffer *stfb = st_framebuffer(st->ctx->DrawBuffer);
702 struct st_renderbuffer *strb = NULL;
703
704 if (stfb)
705 strb = st_renderbuffer(stfb->Base.Attachment[BUFFER_FRONT_LEFT].Renderbuffer);
706 if (!strb)
707 return;
708
709 /* st_public.h */
710 if (!stfb->iface) {
711 struct pipe_surface *front_surf = strb->surface;
712 st->pipe->screen->flush_frontbuffer(st->pipe->screen,
713 front_surf, st->winsys_drawable_handle);
714 return;
715 }
716
717 stfb->iface->flush_front(stfb->iface, ST_ATTACHMENT_FRONT_LEFT);
718 }
719
720 /**
721 * Re-validate the framebuffers.
722 */
723 void
724 st_manager_validate_framebuffers(struct st_context *st)
725 {
726 struct st_framebuffer *stdraw = st_framebuffer(st->ctx->DrawBuffer);
727 struct st_framebuffer *stread = st_framebuffer(st->ctx->ReadBuffer);
728
729 /* st_public.h */
730 if ((stdraw && !stdraw->iface) || (stread && !stread->iface)) {
731 struct pipe_screen *screen = st->pipe->screen;
732 if (screen->update_buffer)
733 screen->update_buffer(screen, st->pipe->priv);
734 return;
735 }
736
737 if (stdraw)
738 st_framebuffer_validate(stdraw, st);
739 if (stread && stread != stdraw)
740 st_framebuffer_validate(stread, st);
741 }
742
743 /**
744 * Add a color renderbuffer on demand.
745 */
746 boolean
747 st_manager_add_color_renderbuffer(struct st_context *st, GLframebuffer *fb,
748 gl_buffer_index idx)
749 {
750 struct st_framebuffer *stfb = st_framebuffer(fb);
751
752 /* FBO or st_public.h */
753 if (!stfb || !stfb->iface)
754 return FALSE;
755
756 if (stfb->Base.Attachment[idx].Renderbuffer)
757 return TRUE;
758
759 switch (idx) {
760 case BUFFER_FRONT_LEFT:
761 case BUFFER_BACK_LEFT:
762 case BUFFER_FRONT_RIGHT:
763 case BUFFER_BACK_RIGHT:
764 break;
765 default:
766 return FALSE;
767 break;
768 }
769
770 if (!st_framebuffer_add_renderbuffer(stfb, idx))
771 return FALSE;
772
773 st_framebuffer_update_attachments(stfb);
774 st_invalidate_state(st->ctx, _NEW_BUFFERS);
775
776 return TRUE;
777 }
778
779 /**
780 * Create an st_api to manage the state tracker.
781 */
782 struct st_api *
783 st_manager_create_api(void)
784 {
785 struct st_api *stapi;
786
787 stapi = CALLOC_STRUCT(st_api);
788 if (stapi) {
789 stapi->destroy = st_api_destroy;
790 stapi->get_proc_address = st_api_get_proc_address;
791 stapi->is_visual_supported = st_api_is_visual_supported;
792
793 stapi->create_context = st_api_create_context;
794 stapi->make_current = st_api_make_current;
795 stapi->get_current = st_api_get_current;
796 }
797
798 return stapi;
799 }