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