gallium: Add st_api::name.
[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 struct gl_framebuffer to an st_framebuffer.
58 * Return NULL if the struct gl_framebuffer 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(struct gl_framebuffer *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 struct gl_config from a visual.
300 */
301 static void
302 st_visual_to_context_mode(const struct st_visual *visual,
303 struct gl_config *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 struct gl_config 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 struct gl_framebuffer *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 struct gl_framebuffer *fb = &stfb->Base;
475 _mesa_reference_framebuffer((struct gl_framebuffer **) 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
490 if (stfb && stfb->iface == stfbi) {
491 p_atomic_set(&stfb->revalidate, TRUE);
492 }
493 else {
494 /* This function is probably getting called when we've detected a
495 * change in a window's size but the currently bound context is
496 * not bound to that window.
497 * If the st_framebuffer_iface structure had a pointer to the
498 * corresponding st_framebuffer we'd be able to handle this.
499 */
500 }
501 }
502
503 static void
504 st_context_flush(struct st_context_iface *stctxi, unsigned flags,
505 struct pipe_fence_handle **fence)
506 {
507 struct st_context *st = (struct st_context *) stctxi;
508 st_flush(st, flags, fence);
509 if (flags & PIPE_FLUSH_RENDER_CACHE)
510 st_manager_flush_frontbuffer(st);
511 }
512
513 static boolean
514 st_context_teximage(struct st_context_iface *stctxi, enum st_texture_type target,
515 int level, enum pipe_format internal_format,
516 struct pipe_resource *tex, boolean mipmap)
517 {
518 struct st_context *st = (struct st_context *) stctxi;
519 struct gl_context *ctx = st->ctx;
520 struct gl_texture_unit *texUnit = _mesa_get_current_tex_unit(ctx);
521 struct gl_texture_object *texObj;
522 struct gl_texture_image *texImage;
523 struct st_texture_object *stObj;
524 struct st_texture_image *stImage;
525 GLenum internalFormat;
526 GLuint width, height, depth;
527
528 switch (target) {
529 case ST_TEXTURE_1D:
530 target = GL_TEXTURE_1D;
531 break;
532 case ST_TEXTURE_2D:
533 target = GL_TEXTURE_2D;
534 break;
535 case ST_TEXTURE_3D:
536 target = GL_TEXTURE_3D;
537 break;
538 case ST_TEXTURE_RECT:
539 target = GL_TEXTURE_RECTANGLE_ARB;
540 break;
541 default:
542 return FALSE;
543 break;
544 }
545
546 texObj = _mesa_select_tex_object(ctx, texUnit, target);
547 _mesa_lock_texture(ctx, texObj);
548
549 stObj = st_texture_object(texObj);
550 /* switch to surface based */
551 if (!stObj->surface_based) {
552 _mesa_clear_texture_object(ctx, texObj);
553 stObj->surface_based = GL_TRUE;
554 }
555
556 texImage = _mesa_get_tex_image(ctx, texObj, target, level);
557 stImage = st_texture_image(texImage);
558 if (tex) {
559 /*
560 * XXX When internal_format and tex->format differ, st_finalize_texture
561 * needs to allocate a new texture with internal_format and copy the
562 * texture here into the new one. It will result in surface_copy being
563 * called on surfaces whose formats differ.
564 *
565 * To avoid that, internal_format is (wrongly) ignored here. A sane fix
566 * is to use a sampler view.
567 */
568 if (!st_sampler_compat_formats(tex->format, internal_format))
569 internal_format = tex->format;
570
571 if (util_format_get_component_bits(internal_format,
572 UTIL_FORMAT_COLORSPACE_RGB, 3) > 0)
573 internalFormat = GL_RGBA;
574 else
575 internalFormat = GL_RGB;
576 _mesa_init_teximage_fields(ctx, target, texImage,
577 tex->width0, tex->height0, 1, 0, internalFormat);
578 texImage->TexFormat = st_ChooseTextureFormat(ctx, internalFormat,
579 GL_RGBA, GL_UNSIGNED_BYTE);
580 _mesa_set_fetch_functions(texImage, 2);
581
582 width = tex->width0;
583 height = tex->height0;
584 depth = tex->depth0;
585
586 /* grow the image size until we hit level = 0 */
587 while (level > 0) {
588 if (width != 1)
589 width <<= 1;
590 if (height != 1)
591 height <<= 1;
592 if (depth != 1)
593 depth <<= 1;
594 level--;
595 }
596 }
597 else {
598 _mesa_clear_texture_image(ctx, texImage);
599 width = height = depth = 0;
600 }
601
602 pipe_resource_reference(&stImage->pt, tex);
603 stObj->width0 = width;
604 stObj->height0 = height;
605 stObj->depth0 = depth;
606
607 _mesa_dirty_texobj(ctx, texObj, GL_TRUE);
608 _mesa_unlock_texture(ctx, texObj);
609
610 return TRUE;
611 }
612
613 static void
614 st_context_destroy(struct st_context_iface *stctxi)
615 {
616 struct st_context *st = (struct st_context *) stctxi;
617 st_destroy_context(st);
618 }
619
620 static struct st_context_iface *
621 st_api_create_context(struct st_api *stapi, struct st_manager *smapi,
622 const struct st_context_attribs *attribs,
623 struct st_context_iface *shared_stctxi)
624 {
625 struct st_context *shared_ctx = (struct st_context *) shared_stctxi;
626 struct st_context *st;
627 struct pipe_context *pipe;
628 struct gl_config mode;
629 gl_api api;
630
631 if (!(stapi->profile_mask & (1 << attribs->profile)))
632 return NULL;
633
634 switch (attribs->profile) {
635 case ST_PROFILE_DEFAULT:
636 api = API_OPENGL;
637 break;
638 case ST_PROFILE_OPENGL_ES1:
639 api = API_OPENGLES;
640 break;
641 case ST_PROFILE_OPENGL_ES2:
642 api = API_OPENGLES2;
643 break;
644 case ST_PROFILE_OPENGL_CORE:
645 default:
646 return NULL;
647 break;
648 }
649
650 pipe = smapi->screen->context_create(smapi->screen, NULL);
651 if (!pipe)
652 return NULL;
653
654 st_visual_to_context_mode(&attribs->visual, &mode);
655 st = st_create_context(api, pipe, &mode, shared_ctx);
656 if (!st) {
657 pipe->destroy(pipe);
658 return NULL;
659 }
660
661 /* need to perform version check */
662 if (attribs->major > 1 || attribs->minor > 0) {
663 _mesa_compute_version(st->ctx);
664
665 if (st->ctx->VersionMajor < attribs->major ||
666 st->ctx->VersionMajor < attribs->minor) {
667 st_destroy_context(st);
668 return NULL;
669 }
670 }
671
672 st->invalidate_on_gl_viewport =
673 smapi->get_param(smapi, ST_MANAGER_BROKEN_INVALIDATE);
674
675 st->iface.destroy = st_context_destroy;
676 st->iface.notify_invalid_framebuffer =
677 st_context_notify_invalid_framebuffer;
678 st->iface.flush = st_context_flush;
679 st->iface.teximage = st_context_teximage;
680 st->iface.copy = NULL;
681 st->iface.st_context_private = (void *) smapi;
682
683 return &st->iface;
684 }
685
686 static boolean
687 st_api_make_current(struct st_api *stapi, struct st_context_iface *stctxi,
688 struct st_framebuffer_iface *stdrawi,
689 struct st_framebuffer_iface *streadi)
690 {
691 struct st_context *st = (struct st_context *) stctxi;
692 struct st_framebuffer *stdraw, *stread, *stfb;
693 boolean ret;
694
695 _glapi_check_multithread();
696
697 if (st) {
698 /* reuse/create the draw fb */
699 stfb = st_ws_framebuffer(st->ctx->WinSysDrawBuffer);
700 if (stfb && stfb->iface == stdrawi) {
701 stdraw = NULL;
702 st_framebuffer_reference(&stdraw, stfb);
703 }
704 else {
705 stdraw = st_framebuffer_create(stdrawi);
706 }
707
708 /* reuse/create the read fb */
709 stfb = st_ws_framebuffer(st->ctx->WinSysReadBuffer);
710 if (!stfb || stfb->iface != streadi)
711 stfb = stdraw;
712 if (stfb && stfb->iface == streadi) {
713 stread = NULL;
714 st_framebuffer_reference(&stread, stfb);
715 }
716 else {
717 stread = st_framebuffer_create(streadi);
718 }
719
720 if (stdraw && stread) {
721 st_framebuffer_validate(stdraw, st);
722 if (stread != stdraw)
723 st_framebuffer_validate(stread, st);
724
725 /* modify the draw/read buffers of the context */
726 if (stdraw->iface) {
727 st_visual_to_default_buffer(stdraw->iface->visual,
728 &st->ctx->Color.DrawBuffer[0], NULL);
729 }
730 if (stread->iface) {
731 st_visual_to_default_buffer(stread->iface->visual,
732 &st->ctx->Pixel.ReadBuffer, NULL);
733 }
734
735 ret = _mesa_make_current(st->ctx, &stdraw->Base, &stread->Base);
736 }
737 else {
738 ret = FALSE;
739 }
740
741 st_framebuffer_reference(&stdraw, NULL);
742 st_framebuffer_reference(&stread, NULL);
743 }
744 else {
745 ret = _mesa_make_current(NULL, NULL, NULL);
746 }
747
748 return ret;
749 }
750
751 static struct st_context_iface *
752 st_api_get_current(struct st_api *stapi)
753 {
754 GET_CURRENT_CONTEXT(ctx);
755 struct st_context *st = (ctx) ? ctx->st : NULL;
756
757 return (st) ? &st->iface : NULL;
758 }
759
760 static st_proc_t
761 st_api_get_proc_address(struct st_api *stapi, const char *procname)
762 {
763 return (st_proc_t) _glapi_get_proc_address(procname);
764 }
765
766 static void
767 st_api_destroy(struct st_api *stapi)
768 {
769 }
770
771 /**
772 * Flush the front buffer if the current context renders to the front buffer.
773 */
774 void
775 st_manager_flush_frontbuffer(struct st_context *st)
776 {
777 struct st_framebuffer *stfb = st_ws_framebuffer(st->ctx->DrawBuffer);
778 struct st_renderbuffer *strb = NULL;
779
780 if (stfb)
781 strb = st_renderbuffer(stfb->Base.Attachment[BUFFER_FRONT_LEFT].Renderbuffer);
782 if (!strb)
783 return;
784
785 /* never a dummy fb */
786 assert(stfb->iface);
787 stfb->iface->flush_front(stfb->iface, ST_ATTACHMENT_FRONT_LEFT);
788 }
789
790 /**
791 * Return the surface of an EGLImage.
792 */
793 struct pipe_surface *
794 st_manager_get_egl_image_surface(struct st_context *st,
795 void *eglimg, unsigned usage)
796 {
797 struct st_manager *smapi =
798 (struct st_manager *) st->iface.st_context_private;
799 struct st_egl_image stimg;
800 struct pipe_surface *ps;
801
802 if (!smapi || !smapi->get_egl_image)
803 return NULL;
804
805 memset(&stimg, 0, sizeof(stimg));
806 if (!smapi->get_egl_image(smapi, eglimg, &stimg))
807 return NULL;
808
809 ps = smapi->screen->get_tex_surface(smapi->screen,
810 stimg.texture, stimg.face, stimg.level, stimg.zslice, usage);
811 pipe_resource_reference(&stimg.texture, NULL);
812
813 return ps;
814 }
815
816 /**
817 * Re-validate the framebuffers.
818 */
819 void
820 st_manager_validate_framebuffers(struct st_context *st)
821 {
822 struct st_framebuffer *stdraw = st_ws_framebuffer(st->ctx->DrawBuffer);
823 struct st_framebuffer *stread = st_ws_framebuffer(st->ctx->ReadBuffer);
824
825 if (stdraw)
826 st_framebuffer_validate(stdraw, st);
827 if (stread && stread != stdraw)
828 st_framebuffer_validate(stread, st);
829 }
830
831 /**
832 * Add a color renderbuffer on demand.
833 */
834 boolean
835 st_manager_add_color_renderbuffer(struct st_context *st, struct gl_framebuffer *fb,
836 gl_buffer_index idx)
837 {
838 struct st_framebuffer *stfb = st_ws_framebuffer(fb);
839
840 /* FBO */
841 if (!stfb)
842 return FALSE;
843
844 if (stfb->Base.Attachment[idx].Renderbuffer)
845 return TRUE;
846
847 switch (idx) {
848 case BUFFER_FRONT_LEFT:
849 case BUFFER_BACK_LEFT:
850 case BUFFER_FRONT_RIGHT:
851 case BUFFER_BACK_RIGHT:
852 break;
853 default:
854 return FALSE;
855 break;
856 }
857
858 if (!st_framebuffer_add_renderbuffer(stfb, idx))
859 return FALSE;
860
861 st_framebuffer_update_attachments(stfb);
862 st_invalidate_state(st->ctx, _NEW_BUFFERS);
863
864 return TRUE;
865 }
866
867 static const struct st_api st_gl_api = {
868 "Mesa " MESA_VERSION_STRING,
869 ST_API_OPENGL,
870 #if FEATURE_GL
871 ST_PROFILE_DEFAULT_MASK |
872 #endif
873 #if FEATURE_ES1
874 ST_PROFILE_OPENGL_ES1_MASK |
875 #endif
876 #if FEATURE_ES2
877 ST_PROFILE_OPENGL_ES2_MASK |
878 #endif
879 0,
880 st_api_destroy,
881 st_api_get_proc_address,
882 st_api_create_context,
883 st_api_make_current,
884 st_api_get_current,
885 };
886
887 struct st_api *
888 st_gl_api_create(void)
889 {
890 return (struct st_api *) &st_gl_api;
891 }