draw: corrections to allow for different cliptest cases
[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 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_gl_api.h"
30
31 #include "pipe/p_context.h"
32 #include "pipe/p_screen.h"
33 #include "util/u_format.h"
34 #include "util/u_pointer.h"
35 #include "util/u_inlines.h"
36 #include "util/u_atomic.h"
37
38 #include "main/mtypes.h"
39 #include "main/context.h"
40 #include "main/texobj.h"
41 #include "main/teximage.h"
42 #include "main/texstate.h"
43 #include "main/texfetch.h"
44 #include "main/framebuffer.h"
45 #include "main/fbobject.h"
46 #include "main/renderbuffer.h"
47 #include "main/version.h"
48 #include "st_texture.h"
49
50 #include "st_context.h"
51 #include "st_format.h"
52 #include "st_cb_fbo.h"
53 #include "st_cb_flush.h"
54 #include "st_manager.h"
55
56 /**
57 * Cast wrapper to convert a GLframebuffer to an st_framebuffer.
58 * Return NULL if the GLframebuffer is a user-created framebuffer.
59 * We'll only return non-null for window system framebuffers.
60 * Note that this function may fail.
61 */
62 static INLINE struct st_framebuffer *
63 st_ws_framebuffer(GLframebuffer *fb)
64 {
65 /* FBO cannot be casted. See st_new_framebuffer */
66 return (struct st_framebuffer *) ((fb && !fb->Name) ? fb : NULL);
67 }
68
69 /**
70 * Map an attachment to a buffer index.
71 */
72 static INLINE gl_buffer_index
73 attachment_to_buffer_index(enum st_attachment_type statt)
74 {
75 gl_buffer_index index;
76
77 switch (statt) {
78 case ST_ATTACHMENT_FRONT_LEFT:
79 index = BUFFER_FRONT_LEFT;
80 break;
81 case ST_ATTACHMENT_BACK_LEFT:
82 index = BUFFER_BACK_LEFT;
83 break;
84 case ST_ATTACHMENT_FRONT_RIGHT:
85 index = BUFFER_FRONT_RIGHT;
86 break;
87 case ST_ATTACHMENT_BACK_RIGHT:
88 index = BUFFER_BACK_RIGHT;
89 break;
90 case ST_ATTACHMENT_DEPTH_STENCIL:
91 index = BUFFER_DEPTH;
92 break;
93 case ST_ATTACHMENT_ACCUM:
94 index = BUFFER_ACCUM;
95 break;
96 case ST_ATTACHMENT_SAMPLE:
97 default:
98 index = BUFFER_COUNT;
99 break;
100 }
101
102 return index;
103 }
104
105 /**
106 * Map a buffer index to an attachment.
107 */
108 static INLINE enum st_attachment_type
109 buffer_index_to_attachment(gl_buffer_index index)
110 {
111 enum st_attachment_type statt;
112
113 switch (index) {
114 case BUFFER_FRONT_LEFT:
115 statt = ST_ATTACHMENT_FRONT_LEFT;
116 break;
117 case BUFFER_BACK_LEFT:
118 statt = ST_ATTACHMENT_BACK_LEFT;
119 break;
120 case BUFFER_FRONT_RIGHT:
121 statt = ST_ATTACHMENT_FRONT_RIGHT;
122 break;
123 case BUFFER_BACK_RIGHT:
124 statt = ST_ATTACHMENT_BACK_RIGHT;
125 break;
126 case BUFFER_DEPTH:
127 statt = ST_ATTACHMENT_DEPTH_STENCIL;
128 break;
129 case BUFFER_ACCUM:
130 statt = ST_ATTACHMENT_ACCUM;
131 break;
132 default:
133 statt = ST_ATTACHMENT_INVALID;
134 break;
135 }
136
137 return statt;
138 }
139
140 /**
141 * Validate a framebuffer to make sure up-to-date pipe_textures are used.
142 */
143 static void
144 st_framebuffer_validate(struct st_framebuffer *stfb, struct st_context *st)
145 {
146 struct pipe_screen *screen = st->pipe->screen;
147 struct pipe_resource *textures[ST_ATTACHMENT_COUNT];
148 uint width, height;
149 unsigned i;
150 boolean changed = FALSE;
151
152 if (!p_atomic_read(&stfb->revalidate))
153 return;
154
155 /* validate the fb */
156 if (!stfb->iface->validate(stfb->iface, stfb->statts, stfb->num_statts, textures))
157 return;
158
159 width = stfb->Base.Width;
160 height = stfb->Base.Height;
161
162 for (i = 0; i < stfb->num_statts; i++) {
163 struct st_renderbuffer *strb;
164 struct pipe_surface *ps;
165 gl_buffer_index idx;
166
167 if (!textures[i])
168 continue;
169
170 idx = attachment_to_buffer_index(stfb->statts[i]);
171 if (idx >= BUFFER_COUNT) {
172 pipe_resource_reference(&textures[i], NULL);
173 continue;
174 }
175
176 strb = st_renderbuffer(stfb->Base.Attachment[idx].Renderbuffer);
177 assert(strb);
178 if (strb->texture == textures[i]) {
179 pipe_resource_reference(&textures[i], NULL);
180 continue;
181 }
182
183 ps = screen->get_tex_surface(screen, textures[i], 0, 0, 0,
184 PIPE_BIND_RENDER_TARGET);
185 if (ps) {
186 pipe_surface_reference(&strb->surface, ps);
187 pipe_resource_reference(&strb->texture, ps->texture);
188 /* ownership transfered */
189 pipe_surface_reference(&ps, NULL);
190
191 changed = TRUE;
192
193 strb->Base.Width = strb->surface->width;
194 strb->Base.Height = strb->surface->height;
195
196 width = strb->Base.Width;
197 height = strb->Base.Height;
198 }
199
200 pipe_resource_reference(&textures[i], NULL);
201 }
202
203 if (changed) {
204 st->dirty.st |= ST_NEW_FRAMEBUFFER;
205 _mesa_resize_framebuffer(st->ctx, &stfb->Base, width, height);
206
207 assert(stfb->Base.Width == width);
208 assert(stfb->Base.Height == height);
209 }
210
211 p_atomic_set(&stfb->revalidate, FALSE);
212 }
213
214 /**
215 * Update the attachments to validate by looping the existing renderbuffers.
216 */
217 static void
218 st_framebuffer_update_attachments(struct st_framebuffer *stfb)
219 {
220 gl_buffer_index idx;
221
222 stfb->num_statts = 0;
223 for (idx = 0; idx < BUFFER_COUNT; idx++) {
224 struct st_renderbuffer *strb;
225 enum st_attachment_type statt;
226
227 strb = st_renderbuffer(stfb->Base.Attachment[idx].Renderbuffer);
228 if (!strb || strb->software)
229 continue;
230
231 statt = buffer_index_to_attachment(idx);
232 if (statt != ST_ATTACHMENT_INVALID &&
233 st_visual_have_buffers(stfb->iface->visual, 1 << statt))
234 stfb->statts[stfb->num_statts++] = statt;
235 }
236
237 p_atomic_set(&stfb->revalidate, TRUE);
238 }
239
240 /**
241 * Add a renderbuffer to the framebuffer.
242 */
243 static boolean
244 st_framebuffer_add_renderbuffer(struct st_framebuffer *stfb,
245 gl_buffer_index idx)
246 {
247 struct gl_renderbuffer *rb;
248 enum pipe_format format;
249 int samples;
250 boolean sw;
251
252 if (!stfb->iface)
253 return FALSE;
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->depthBits =
335 util_format_get_component_bits(visual->depth_stencil_format,
336 UTIL_FORMAT_COLORSPACE_ZS, 0);
337 mode->stencilBits =
338 util_format_get_component_bits(visual->depth_stencil_format,
339 UTIL_FORMAT_COLORSPACE_ZS, 1);
340
341 mode->haveDepthBuffer = mode->depthBits > 0;
342 mode->haveStencilBuffer = mode->stencilBits > 0;
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 /* for FBO-only context */
431 if (!stfbi) {
432 GLframebuffer *base = _mesa_get_incomplete_framebuffer();
433
434 stfb->Base = *base;
435
436 return stfb;
437 }
438
439 st_visual_to_context_mode(stfbi->visual, &mode);
440 _mesa_initialize_window_framebuffer(&stfb->Base, &mode);
441
442 /* modify the draw/read buffers of the fb */
443 st_visual_to_default_buffer(stfbi->visual, &stfb->Base.ColorDrawBuffer[0],
444 &stfb->Base._ColorDrawBufferIndexes[0]);
445 st_visual_to_default_buffer(stfbi->visual, &stfb->Base.ColorReadBuffer,
446 &stfb->Base._ColorReadBufferIndex);
447
448 stfb->iface = stfbi;
449
450 /* add the color buffer */
451 idx = stfb->Base._ColorDrawBufferIndexes[0];
452 if (!st_framebuffer_add_renderbuffer(stfb, idx)) {
453 FREE(stfb);
454 return NULL;
455 }
456
457 st_framebuffer_add_renderbuffer(stfb, BUFFER_DEPTH);
458 st_framebuffer_add_renderbuffer(stfb, BUFFER_ACCUM);
459
460 st_framebuffer_update_attachments(stfb);
461
462 stfb->Base.Initialized = GL_TRUE;
463
464 return stfb;
465 }
466
467 /**
468 * Reference a framebuffer.
469 */
470 static void
471 st_framebuffer_reference(struct st_framebuffer **ptr,
472 struct st_framebuffer *stfb)
473 {
474 GLframebuffer *fb = &stfb->Base;
475 _mesa_reference_framebuffer((GLframebuffer **) ptr, fb);
476 }
477
478 static void
479 st_context_notify_invalid_framebuffer(struct st_context_iface *stctxi,
480 struct st_framebuffer_iface *stfbi)
481 {
482 struct st_context *st = (struct st_context *) stctxi;
483 struct st_framebuffer *stfb;
484
485 /* either draw or read winsys fb */
486 stfb = st_ws_framebuffer(st->ctx->WinSysDrawBuffer);
487 if (!stfb || stfb->iface != stfbi)
488 stfb = st_ws_framebuffer(st->ctx->WinSysReadBuffer);
489 assert(stfb && stfb->iface == stfbi);
490
491 p_atomic_set(&stfb->revalidate, TRUE);
492 }
493
494 static void
495 st_context_flush(struct st_context_iface *stctxi, unsigned flags,
496 struct pipe_fence_handle **fence)
497 {
498 struct st_context *st = (struct st_context *) stctxi;
499 st_flush(st, flags, fence);
500 if (flags & PIPE_FLUSH_RENDER_CACHE)
501 st_manager_flush_frontbuffer(st);
502 }
503
504 static boolean
505 st_context_teximage(struct st_context_iface *stctxi, enum st_texture_type target,
506 int level, enum pipe_format internal_format,
507 struct pipe_resource *tex, boolean mipmap)
508 {
509 struct st_context *st = (struct st_context *) stctxi;
510 GLcontext *ctx = st->ctx;
511 struct gl_texture_unit *texUnit = _mesa_get_current_tex_unit(ctx);
512 struct gl_texture_object *texObj;
513 struct gl_texture_image *texImage;
514 struct st_texture_object *stObj;
515 struct st_texture_image *stImage;
516 GLenum internalFormat;
517 GLuint width, height, depth;
518
519 switch (target) {
520 case ST_TEXTURE_1D:
521 target = GL_TEXTURE_1D;
522 break;
523 case ST_TEXTURE_2D:
524 target = GL_TEXTURE_2D;
525 break;
526 case ST_TEXTURE_3D:
527 target = GL_TEXTURE_3D;
528 break;
529 case ST_TEXTURE_RECT:
530 target = GL_TEXTURE_RECTANGLE_ARB;
531 break;
532 default:
533 return FALSE;
534 break;
535 }
536
537 texObj = _mesa_select_tex_object(ctx, texUnit, target);
538 _mesa_lock_texture(ctx, texObj);
539
540 stObj = st_texture_object(texObj);
541 /* switch to surface based */
542 if (!stObj->surface_based) {
543 _mesa_clear_texture_object(ctx, texObj);
544 stObj->surface_based = GL_TRUE;
545 }
546
547 texImage = _mesa_get_tex_image(ctx, texObj, target, level);
548 stImage = st_texture_image(texImage);
549 if (tex) {
550 /*
551 * XXX When internal_format and tex->format differ, st_finalize_texture
552 * needs to allocate a new texture with internal_format and copy the
553 * texture here into the new one. It will result in surface_copy being
554 * called on surfaces whose formats differ.
555 *
556 * To avoid that, internal_format is (wrongly) ignored here. A sane fix
557 * is to use a sampler view.
558 */
559 if (!st_sampler_compat_formats(tex->format, internal_format))
560 internal_format = tex->format;
561
562 if (util_format_get_component_bits(internal_format,
563 UTIL_FORMAT_COLORSPACE_RGB, 3) > 0)
564 internalFormat = GL_RGBA;
565 else
566 internalFormat = GL_RGB;
567 _mesa_init_teximage_fields(ctx, target, texImage,
568 tex->width0, tex->height0, 1, 0, internalFormat);
569 texImage->TexFormat = st_ChooseTextureFormat(ctx, internalFormat,
570 GL_RGBA, GL_UNSIGNED_BYTE);
571 _mesa_set_fetch_functions(texImage, 2);
572
573 width = tex->width0;
574 height = tex->height0;
575 depth = tex->depth0;
576
577 /* grow the image size until we hit level = 0 */
578 while (level > 0) {
579 if (width != 1)
580 width <<= 1;
581 if (height != 1)
582 height <<= 1;
583 if (depth != 1)
584 depth <<= 1;
585 level--;
586 }
587 }
588 else {
589 _mesa_clear_texture_image(ctx, texImage);
590 width = height = depth = 0;
591 }
592
593 pipe_resource_reference(&stImage->pt, tex);
594 stObj->width0 = width;
595 stObj->height0 = height;
596 stObj->depth0 = depth;
597
598 _mesa_dirty_texobj(ctx, texObj, GL_TRUE);
599 _mesa_unlock_texture(ctx, texObj);
600
601 return TRUE;
602 }
603
604 static void
605 st_context_destroy(struct st_context_iface *stctxi)
606 {
607 struct st_context *st = (struct st_context *) stctxi;
608 st_destroy_context(st);
609 }
610
611 static struct st_context_iface *
612 st_api_create_context(struct st_api *stapi, struct st_manager *smapi,
613 const struct st_context_attribs *attribs,
614 struct st_context_iface *shared_stctxi)
615 {
616 struct st_context *shared_ctx = (struct st_context *) shared_stctxi;
617 struct st_context *st;
618 struct pipe_context *pipe;
619 __GLcontextModes mode;
620 gl_api api;
621
622 if (!(stapi->profile_mask & (1 << attribs->profile)))
623 return NULL;
624
625 switch (attribs->profile) {
626 case ST_PROFILE_DEFAULT:
627 api = API_OPENGL;
628 break;
629 case ST_PROFILE_OPENGL_ES1:
630 api = API_OPENGLES;
631 break;
632 case ST_PROFILE_OPENGL_ES2:
633 api = API_OPENGLES2;
634 break;
635 case ST_PROFILE_OPENGL_CORE:
636 default:
637 return NULL;
638 break;
639 }
640
641 pipe = smapi->screen->context_create(smapi->screen, NULL);
642 if (!pipe)
643 return NULL;
644
645 st_visual_to_context_mode(&attribs->visual, &mode);
646 st = st_create_context(api, pipe, &mode, shared_ctx);
647 if (!st) {
648 pipe->destroy(pipe);
649 return NULL;
650 }
651
652 /* need to perform version check */
653 if (attribs->major > 1 || attribs->minor > 0) {
654 _mesa_compute_version(st->ctx);
655
656 if (st->ctx->VersionMajor < attribs->major ||
657 st->ctx->VersionMajor < attribs->minor) {
658 st_destroy_context(st);
659 return NULL;
660 }
661 }
662
663 st->invalidate_on_gl_viewport =
664 smapi->get_param(smapi, ST_MANAGER_BROKEN_INVALIDATE);
665
666 st->iface.destroy = st_context_destroy;
667 st->iface.notify_invalid_framebuffer =
668 st_context_notify_invalid_framebuffer;
669 st->iface.flush = st_context_flush;
670 st->iface.teximage = st_context_teximage;
671 st->iface.copy = NULL;
672 st->iface.st_context_private = (void *) smapi;
673
674 return &st->iface;
675 }
676
677 static boolean
678 st_api_make_current(struct st_api *stapi, struct st_context_iface *stctxi,
679 struct st_framebuffer_iface *stdrawi,
680 struct st_framebuffer_iface *streadi)
681 {
682 struct st_context *st = (struct st_context *) stctxi;
683 struct st_framebuffer *stdraw, *stread, *stfb;
684 boolean ret;
685
686 _glapi_check_multithread();
687
688 if (st) {
689 /* reuse/create the draw fb */
690 stfb = st_ws_framebuffer(st->ctx->WinSysDrawBuffer);
691 if (stfb && stfb->iface == stdrawi) {
692 stdraw = NULL;
693 st_framebuffer_reference(&stdraw, stfb);
694 }
695 else {
696 stdraw = st_framebuffer_create(stdrawi);
697 }
698
699 /* reuse/create the read fb */
700 stfb = st_ws_framebuffer(st->ctx->WinSysReadBuffer);
701 if (!stfb || stfb->iface != streadi)
702 stfb = stdraw;
703 if (stfb && stfb->iface == streadi) {
704 stread = NULL;
705 st_framebuffer_reference(&stread, stfb);
706 }
707 else {
708 stread = st_framebuffer_create(streadi);
709 }
710
711 if (stdraw && stread) {
712 st_framebuffer_validate(stdraw, st);
713 if (stread != stdraw)
714 st_framebuffer_validate(stread, st);
715
716 /* modify the draw/read buffers of the context */
717 if (stdraw->iface) {
718 st_visual_to_default_buffer(stdraw->iface->visual,
719 &st->ctx->Color.DrawBuffer[0], NULL);
720 }
721 if (stread->iface) {
722 st_visual_to_default_buffer(stread->iface->visual,
723 &st->ctx->Pixel.ReadBuffer, NULL);
724 }
725
726 ret = _mesa_make_current(st->ctx, &stdraw->Base, &stread->Base);
727 }
728 else {
729 ret = FALSE;
730 }
731
732 st_framebuffer_reference(&stdraw, NULL);
733 st_framebuffer_reference(&stread, NULL);
734 }
735 else {
736 ret = _mesa_make_current(NULL, NULL, NULL);
737 }
738
739 return ret;
740 }
741
742 static struct st_context_iface *
743 st_api_get_current(struct st_api *stapi)
744 {
745 GET_CURRENT_CONTEXT(ctx);
746 struct st_context *st = (ctx) ? ctx->st : NULL;
747
748 return (st) ? &st->iface : NULL;
749 }
750
751 static st_proc_t
752 st_api_get_proc_address(struct st_api *stapi, const char *procname)
753 {
754 return (st_proc_t) _glapi_get_proc_address(procname);
755 }
756
757 static void
758 st_api_destroy(struct st_api *stapi)
759 {
760 }
761
762 /**
763 * Flush the front buffer if the current context renders to the front buffer.
764 */
765 void
766 st_manager_flush_frontbuffer(struct st_context *st)
767 {
768 struct st_framebuffer *stfb = st_ws_framebuffer(st->ctx->DrawBuffer);
769 struct st_renderbuffer *strb = NULL;
770
771 if (stfb)
772 strb = st_renderbuffer(stfb->Base.Attachment[BUFFER_FRONT_LEFT].Renderbuffer);
773 if (!strb)
774 return;
775
776 /* never a dummy fb */
777 assert(stfb->iface);
778 stfb->iface->flush_front(stfb->iface, ST_ATTACHMENT_FRONT_LEFT);
779 }
780
781 /**
782 * Return the surface of an EGLImage.
783 */
784 struct pipe_surface *
785 st_manager_get_egl_image_surface(struct st_context *st,
786 void *eglimg, unsigned usage)
787 {
788 struct st_manager *smapi =
789 (struct st_manager *) st->iface.st_context_private;
790 struct st_egl_image stimg;
791 struct pipe_surface *ps;
792
793 if (!smapi || !smapi->get_egl_image)
794 return NULL;
795
796 memset(&stimg, 0, sizeof(stimg));
797 if (!smapi->get_egl_image(smapi, eglimg, &stimg))
798 return NULL;
799
800 ps = smapi->screen->get_tex_surface(smapi->screen,
801 stimg.texture, stimg.face, stimg.level, stimg.zslice, usage);
802 pipe_resource_reference(&stimg.texture, NULL);
803
804 return ps;
805 }
806
807 /**
808 * Re-validate the framebuffers.
809 */
810 void
811 st_manager_validate_framebuffers(struct st_context *st)
812 {
813 struct st_framebuffer *stdraw = st_ws_framebuffer(st->ctx->DrawBuffer);
814 struct st_framebuffer *stread = st_ws_framebuffer(st->ctx->ReadBuffer);
815
816 if (stdraw)
817 st_framebuffer_validate(stdraw, st);
818 if (stread && stread != stdraw)
819 st_framebuffer_validate(stread, st);
820 }
821
822 /**
823 * Add a color renderbuffer on demand.
824 */
825 boolean
826 st_manager_add_color_renderbuffer(struct st_context *st, GLframebuffer *fb,
827 gl_buffer_index idx)
828 {
829 struct st_framebuffer *stfb = st_ws_framebuffer(fb);
830
831 /* FBO */
832 if (!stfb)
833 return FALSE;
834
835 if (stfb->Base.Attachment[idx].Renderbuffer)
836 return TRUE;
837
838 switch (idx) {
839 case BUFFER_FRONT_LEFT:
840 case BUFFER_BACK_LEFT:
841 case BUFFER_FRONT_RIGHT:
842 case BUFFER_BACK_RIGHT:
843 break;
844 default:
845 return FALSE;
846 break;
847 }
848
849 if (!st_framebuffer_add_renderbuffer(stfb, idx))
850 return FALSE;
851
852 st_framebuffer_update_attachments(stfb);
853 st_invalidate_state(st->ctx, _NEW_BUFFERS);
854
855 return TRUE;
856 }
857
858 static const struct st_api st_gl_api = {
859 ST_API_OPENGL,
860 #if FEATURE_GL
861 ST_PROFILE_DEFAULT_MASK |
862 #endif
863 #if FEATURE_ES1
864 ST_PROFILE_OPENGL_ES1_MASK |
865 #endif
866 #if FEATURE_ES2
867 ST_PROFILE_OPENGL_ES2_MASK |
868 #endif
869 0,
870 st_api_destroy,
871 st_api_get_proc_address,
872 st_api_create_context,
873 st_api_make_current,
874 st_api_get_current,
875 };
876
877 struct st_api *
878 st_gl_api_create(void)
879 {
880 return (struct st_api *) &st_gl_api;
881 }