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