Merge branch '7.8'
[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 stObj->pipe = st->pipe;
557 pipe_texture_reference(&stImage->pt, tex);
558
559 _mesa_dirty_texobj(ctx, texObj, GL_TRUE);
560 _mesa_unlock_texture(ctx, texObj);
561
562 return TRUE;
563 }
564
565 static void
566 st_context_destroy(struct st_context_iface *stctxi)
567 {
568 struct st_context *st = (struct st_context *) stctxi;
569 st_destroy_context(st);
570 }
571
572 static struct st_context_iface *
573 st_api_create_context(struct st_api *stapi, struct st_manager *smapi,
574 const struct st_visual *visual,
575 struct st_context_iface *shared_stctxi)
576 {
577 struct st_context *shared_ctx = (struct st_context *) shared_stctxi;
578 struct st_context *st;
579 struct pipe_context *pipe;
580 __GLcontextModes mode;
581
582 pipe = smapi->screen->context_create(smapi->screen, NULL);
583 if (!pipe)
584 return NULL;
585
586 st_visual_to_context_mode(visual, &mode);
587 st = st_create_context(pipe, &mode, shared_ctx);
588 if (!st) {
589 pipe->destroy(pipe);
590 return NULL;
591 }
592
593 st->iface.destroy = st_context_destroy;
594
595 st->iface.notify_invalid_framebuffer =
596 st_context_notify_invalid_framebuffer;
597 st->iface.flush = st_context_flush;
598
599 st->iface.teximage = st_context_teximage;
600 st->iface.copy = NULL;
601
602 st->iface.st_context_private = (void *) smapi;
603
604 return &st->iface;
605 }
606
607 static boolean
608 st_api_make_current(struct st_api *stapi, struct st_context_iface *stctxi,
609 struct st_framebuffer_iface *stdrawi,
610 struct st_framebuffer_iface *streadi)
611 {
612 struct st_context *st = (struct st_context *) stctxi;
613 struct st_framebuffer *stdraw, *stread, *stfb;
614 boolean ret;
615
616 _glapi_check_multithread();
617
618 if (st) {
619 /* reuse/create the draw fb */
620 stfb = st_framebuffer(st->ctx->WinSysDrawBuffer);
621 if (stfb && stfb->iface == stdrawi) {
622 stdraw = NULL;
623 st_framebuffer_reference(&stdraw, stfb);
624 }
625 else {
626 stdraw = st_framebuffer_create(stdrawi);
627 }
628
629 /* reuse/create the read fb */
630 stfb = st_framebuffer(st->ctx->WinSysReadBuffer);
631 if (!stfb || stfb->iface != streadi)
632 stfb = stdraw;
633 if (stfb && stfb->iface == streadi) {
634 stread = NULL;
635 st_framebuffer_reference(&stread, stfb);
636 }
637 else {
638 stread = st_framebuffer_create(streadi);
639 }
640
641 if (stdraw && stread) {
642 st_framebuffer_validate(stdraw, st);
643 if (stread != stdraw)
644 st_framebuffer_validate(stread, st);
645
646 /* modify the draw/read buffers of the context */
647 st_visual_to_default_buffer(stdraw->iface->visual,
648 &st->ctx->Color.DrawBuffer[0], NULL);
649 st_visual_to_default_buffer(stread->iface->visual,
650 &st->ctx->Pixel.ReadBuffer, NULL);
651
652 ret = _mesa_make_current(st->ctx, &stdraw->Base, &stread->Base);
653 }
654 else {
655 ret = FALSE;
656 }
657
658 st_framebuffer_reference(&stdraw, NULL);
659 st_framebuffer_reference(&stread, NULL);
660 }
661 else {
662 ret = _mesa_make_current(NULL, NULL, NULL);
663 }
664
665 return ret;
666 }
667
668 static struct st_context_iface *
669 st_api_get_current(struct st_api *stapi)
670 {
671 GET_CURRENT_CONTEXT(ctx);
672 struct st_context *st = (ctx) ? ctx->st : NULL;
673
674 return (st) ? &st->iface : NULL;
675 }
676
677 static boolean
678 st_api_is_visual_supported(struct st_api *stapi,
679 const struct st_visual *visual)
680 {
681 return TRUE;
682 }
683
684 static st_proc_t
685 st_api_get_proc_address(struct st_api *stapi, const char *procname)
686 {
687 return (st_proc_t) _glapi_get_proc_address(procname);
688 }
689
690 static void
691 st_api_destroy(struct st_api *stapi)
692 {
693 FREE(stapi);
694 }
695
696 /**
697 * Flush the front buffer if the current context renders to the front buffer.
698 */
699 void
700 st_manager_flush_frontbuffer(struct st_context *st)
701 {
702 struct st_framebuffer *stfb = st_framebuffer(st->ctx->DrawBuffer);
703 struct st_renderbuffer *strb = NULL;
704
705 if (stfb)
706 strb = st_renderbuffer(stfb->Base.Attachment[BUFFER_FRONT_LEFT].Renderbuffer);
707 if (!strb)
708 return;
709
710 /* st_public.h */
711 if (!stfb->iface) {
712 struct pipe_surface *front_surf = strb->surface;
713 st->pipe->screen->flush_frontbuffer(st->pipe->screen,
714 front_surf, st->winsys_drawable_handle);
715 return;
716 }
717
718 stfb->iface->flush_front(stfb->iface, ST_ATTACHMENT_FRONT_LEFT);
719 }
720
721 /**
722 * Re-validate the framebuffers.
723 */
724 void
725 st_manager_validate_framebuffers(struct st_context *st)
726 {
727 struct st_framebuffer *stdraw = st_framebuffer(st->ctx->DrawBuffer);
728 struct st_framebuffer *stread = st_framebuffer(st->ctx->ReadBuffer);
729
730 /* st_public.h */
731 if ((stdraw && !stdraw->iface) || (stread && !stread->iface)) {
732 struct pipe_screen *screen = st->pipe->screen;
733 if (screen->update_buffer)
734 screen->update_buffer(screen, st->pipe->priv);
735 return;
736 }
737
738 if (stdraw)
739 st_framebuffer_validate(stdraw, st);
740 if (stread && stread != stdraw)
741 st_framebuffer_validate(stread, st);
742 }
743
744 /**
745 * Add a color renderbuffer on demand.
746 */
747 boolean
748 st_manager_add_color_renderbuffer(struct st_context *st, GLframebuffer *fb,
749 gl_buffer_index idx)
750 {
751 struct st_framebuffer *stfb = st_framebuffer(fb);
752
753 /* FBO or st_public.h */
754 if (!stfb || !stfb->iface)
755 return FALSE;
756
757 if (stfb->Base.Attachment[idx].Renderbuffer)
758 return TRUE;
759
760 switch (idx) {
761 case BUFFER_FRONT_LEFT:
762 case BUFFER_BACK_LEFT:
763 case BUFFER_FRONT_RIGHT:
764 case BUFFER_BACK_RIGHT:
765 break;
766 default:
767 return FALSE;
768 break;
769 }
770
771 if (!st_framebuffer_add_renderbuffer(stfb, idx))
772 return FALSE;
773
774 st_framebuffer_update_attachments(stfb);
775 st_invalidate_state(st->ctx, _NEW_BUFFERS);
776
777 return TRUE;
778 }
779
780 /**
781 * Create an st_api to manage the state tracker.
782 */
783 struct st_api *
784 st_manager_create_api(void)
785 {
786 struct st_api *stapi;
787
788 stapi = CALLOC_STRUCT(st_api);
789 if (stapi) {
790 stapi->destroy = st_api_destroy;
791 stapi->get_proc_address = st_api_get_proc_address;
792 stapi->is_visual_supported = st_api_is_visual_supported;
793
794 stapi->create_context = st_api_create_context;
795 stapi->make_current = st_api_make_current;
796 stapi->get_current = st_api_get_current;
797 }
798
799 return stapi;
800 }