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