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