st/mesa: only mark framebuffer as sRGB capable if Mesa supports the format
[mesa.git] / src / mesa / state_tracker / st_manager.c
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 2010 LunarG Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 * Chia-I Wu <olv@lunarg.com>
26 */
27
28 #include "main/mtypes.h"
29 #include "main/context.h"
30 #include "main/texobj.h"
31 #include "main/teximage.h"
32 #include "main/texstate.h"
33 #include "main/errors.h"
34 #include "main/framebuffer.h"
35 #include "main/fbobject.h"
36 #include "main/renderbuffer.h"
37 #include "main/version.h"
38 #include "st_texture.h"
39
40 #include "st_context.h"
41 #include "st_format.h"
42 #include "st_cb_fbo.h"
43 #include "st_cb_flush.h"
44 #include "st_manager.h"
45
46 #include "state_tracker/st_gl_api.h"
47
48 #include "pipe/p_context.h"
49 #include "pipe/p_screen.h"
50 #include "util/u_format.h"
51 #include "util/u_pointer.h"
52 #include "util/u_inlines.h"
53 #include "util/u_atomic.h"
54 #include "util/u_surface.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 if (fb && _mesa_is_winsys_fbo(fb))
67 return (struct st_framebuffer *) fb;
68 return NULL;
69 }
70
71 /**
72 * Map an attachment to a buffer index.
73 */
74 static INLINE gl_buffer_index
75 attachment_to_buffer_index(enum st_attachment_type statt)
76 {
77 gl_buffer_index index;
78
79 switch (statt) {
80 case ST_ATTACHMENT_FRONT_LEFT:
81 index = BUFFER_FRONT_LEFT;
82 break;
83 case ST_ATTACHMENT_BACK_LEFT:
84 index = BUFFER_BACK_LEFT;
85 break;
86 case ST_ATTACHMENT_FRONT_RIGHT:
87 index = BUFFER_FRONT_RIGHT;
88 break;
89 case ST_ATTACHMENT_BACK_RIGHT:
90 index = BUFFER_BACK_RIGHT;
91 break;
92 case ST_ATTACHMENT_DEPTH_STENCIL:
93 index = BUFFER_DEPTH;
94 break;
95 case ST_ATTACHMENT_ACCUM:
96 index = BUFFER_ACCUM;
97 break;
98 case ST_ATTACHMENT_SAMPLE:
99 default:
100 index = BUFFER_COUNT;
101 break;
102 }
103
104 return index;
105 }
106
107 /**
108 * Map a buffer index to an attachment.
109 */
110 static INLINE enum st_attachment_type
111 buffer_index_to_attachment(gl_buffer_index index)
112 {
113 enum st_attachment_type statt;
114
115 switch (index) {
116 case BUFFER_FRONT_LEFT:
117 statt = ST_ATTACHMENT_FRONT_LEFT;
118 break;
119 case BUFFER_BACK_LEFT:
120 statt = ST_ATTACHMENT_BACK_LEFT;
121 break;
122 case BUFFER_FRONT_RIGHT:
123 statt = ST_ATTACHMENT_FRONT_RIGHT;
124 break;
125 case BUFFER_BACK_RIGHT:
126 statt = ST_ATTACHMENT_BACK_RIGHT;
127 break;
128 case BUFFER_DEPTH:
129 statt = ST_ATTACHMENT_DEPTH_STENCIL;
130 break;
131 case BUFFER_ACCUM:
132 statt = ST_ATTACHMENT_ACCUM;
133 break;
134 default:
135 statt = ST_ATTACHMENT_INVALID;
136 break;
137 }
138
139 return statt;
140 }
141
142 /**
143 * Make sure a context picks up the latest cached state of the
144 * drawables it binds to.
145 */
146 static void
147 st_context_validate(struct st_context *st,
148 struct st_framebuffer *stdraw,
149 struct st_framebuffer *stread)
150 {
151 if (stdraw && stdraw->stamp != st->draw_stamp) {
152 st->dirty.st |= ST_NEW_FRAMEBUFFER;
153 _mesa_resize_framebuffer(st->ctx, &stdraw->Base,
154 stdraw->Base.Width,
155 stdraw->Base.Height);
156 st->draw_stamp = stdraw->stamp;
157 }
158
159 if (stread && stread->stamp != st->read_stamp) {
160 if (stread != stdraw) {
161 st->dirty.st |= ST_NEW_FRAMEBUFFER;
162 _mesa_resize_framebuffer(st->ctx, &stread->Base,
163 stread->Base.Width,
164 stread->Base.Height);
165 }
166 st->read_stamp = stread->stamp;
167 }
168 }
169
170 /**
171 * Validate a framebuffer to make sure up-to-date pipe_textures are used.
172 * The context is only used for creating pipe surfaces and for calling
173 * _mesa_resize_framebuffer().
174 * (That should probably be rethought, since those surfaces become
175 * drawable state, not context state, and can be freed by another pipe
176 * context).
177 */
178 static void
179 st_framebuffer_validate(struct st_framebuffer *stfb,
180 struct st_context *st)
181 {
182 struct pipe_resource *textures[ST_ATTACHMENT_COUNT];
183 uint width, height;
184 unsigned i;
185 boolean changed = FALSE;
186 int32_t new_stamp = p_atomic_read(&stfb->iface->stamp);
187
188 if (stfb->iface_stamp == new_stamp)
189 return;
190
191 /* validate the fb */
192 do {
193 if (!stfb->iface->validate(&st->iface, stfb->iface, stfb->statts,
194 stfb->num_statts, textures))
195 return;
196
197 stfb->iface_stamp = new_stamp;
198 new_stamp = p_atomic_read(&stfb->iface->stamp);
199 } while(stfb->iface_stamp != new_stamp);
200
201 width = stfb->Base.Width;
202 height = stfb->Base.Height;
203
204 for (i = 0; i < stfb->num_statts; i++) {
205 struct st_renderbuffer *strb;
206 struct pipe_surface *ps, surf_tmpl;
207 gl_buffer_index idx;
208
209 if (!textures[i])
210 continue;
211
212 idx = attachment_to_buffer_index(stfb->statts[i]);
213 if (idx >= BUFFER_COUNT) {
214 pipe_resource_reference(&textures[i], NULL);
215 continue;
216 }
217
218 strb = st_renderbuffer(stfb->Base.Attachment[idx].Renderbuffer);
219 assert(strb);
220 if (strb->texture == textures[i]) {
221 pipe_resource_reference(&textures[i], NULL);
222 continue;
223 }
224
225 u_surface_default_template(&surf_tmpl, textures[i]);
226 ps = st->pipe->create_surface(st->pipe, textures[i], &surf_tmpl);
227 if (ps) {
228 pipe_surface_reference(&strb->surface, ps);
229 pipe_resource_reference(&strb->texture, ps->texture);
230 /* ownership transfered */
231 pipe_surface_reference(&ps, NULL);
232
233 changed = TRUE;
234
235 strb->Base.Width = strb->surface->width;
236 strb->Base.Height = strb->surface->height;
237
238 width = strb->Base.Width;
239 height = strb->Base.Height;
240 }
241
242 pipe_resource_reference(&textures[i], NULL);
243 }
244
245 if (changed) {
246 ++stfb->stamp;
247 _mesa_resize_framebuffer(st->ctx, &stfb->Base, width, height);
248 }
249 }
250
251 /**
252 * Update the attachments to validate by looping the existing renderbuffers.
253 */
254 static void
255 st_framebuffer_update_attachments(struct st_framebuffer *stfb)
256 {
257 gl_buffer_index idx;
258
259 stfb->num_statts = 0;
260 for (idx = 0; idx < BUFFER_COUNT; idx++) {
261 struct st_renderbuffer *strb;
262 enum st_attachment_type statt;
263
264 strb = st_renderbuffer(stfb->Base.Attachment[idx].Renderbuffer);
265 if (!strb || strb->software)
266 continue;
267
268 statt = buffer_index_to_attachment(idx);
269 if (statt != ST_ATTACHMENT_INVALID &&
270 st_visual_have_buffers(stfb->iface->visual, 1 << statt))
271 stfb->statts[stfb->num_statts++] = statt;
272 }
273 stfb->stamp++;
274 }
275
276 /**
277 * Add a renderbuffer to the framebuffer.
278 */
279 static boolean
280 st_framebuffer_add_renderbuffer(struct st_framebuffer *stfb,
281 gl_buffer_index idx)
282 {
283 struct gl_renderbuffer *rb;
284 enum pipe_format format;
285 boolean sw;
286
287 if (!stfb->iface)
288 return FALSE;
289
290 /* do not distinguish depth/stencil buffers */
291 if (idx == BUFFER_STENCIL)
292 idx = BUFFER_DEPTH;
293
294 switch (idx) {
295 case BUFFER_DEPTH:
296 format = stfb->iface->visual->depth_stencil_format;
297 sw = FALSE;
298 break;
299 case BUFFER_ACCUM:
300 format = stfb->iface->visual->accum_format;
301 sw = TRUE;
302 break;
303 default:
304 format = stfb->iface->visual->color_format;
305 if (stfb->Base.Visual.sRGBCapable)
306 format = util_format_srgb(format);
307 sw = FALSE;
308 break;
309 }
310
311 if (format == PIPE_FORMAT_NONE)
312 return FALSE;
313
314 rb = st_new_renderbuffer_fb(format, stfb->iface->visual->samples, sw);
315 if (!rb)
316 return FALSE;
317
318 if (idx != BUFFER_DEPTH) {
319 _mesa_add_renderbuffer(&stfb->Base, idx, rb);
320 }
321 else {
322 if (util_format_get_component_bits(format, UTIL_FORMAT_COLORSPACE_ZS, 0))
323 _mesa_add_renderbuffer(&stfb->Base, BUFFER_DEPTH, rb);
324 if (util_format_get_component_bits(format, UTIL_FORMAT_COLORSPACE_ZS, 1))
325 _mesa_add_renderbuffer(&stfb->Base, BUFFER_STENCIL, rb);
326 }
327
328 return TRUE;
329 }
330
331 /**
332 * Intialize a struct gl_config from a visual.
333 */
334 static void
335 st_visual_to_context_mode(const struct st_visual *visual,
336 struct gl_config *mode)
337 {
338 memset(mode, 0, sizeof(*mode));
339
340 if (st_visual_have_buffers(visual, ST_ATTACHMENT_BACK_LEFT_MASK))
341 mode->doubleBufferMode = GL_TRUE;
342 if (st_visual_have_buffers(visual,
343 ST_ATTACHMENT_FRONT_RIGHT_MASK | ST_ATTACHMENT_BACK_RIGHT_MASK))
344 mode->stereoMode = GL_TRUE;
345
346 if (visual->color_format != PIPE_FORMAT_NONE) {
347 mode->rgbMode = GL_TRUE;
348
349 mode->redBits =
350 util_format_get_component_bits(visual->color_format,
351 UTIL_FORMAT_COLORSPACE_RGB, 0);
352 mode->greenBits =
353 util_format_get_component_bits(visual->color_format,
354 UTIL_FORMAT_COLORSPACE_RGB, 1);
355 mode->blueBits =
356 util_format_get_component_bits(visual->color_format,
357 UTIL_FORMAT_COLORSPACE_RGB, 2);
358 mode->alphaBits =
359 util_format_get_component_bits(visual->color_format,
360 UTIL_FORMAT_COLORSPACE_RGB, 3);
361
362 mode->rgbBits = mode->redBits +
363 mode->greenBits + mode->blueBits + mode->alphaBits;
364 }
365
366 if (visual->depth_stencil_format != PIPE_FORMAT_NONE) {
367 mode->depthBits =
368 util_format_get_component_bits(visual->depth_stencil_format,
369 UTIL_FORMAT_COLORSPACE_ZS, 0);
370 mode->stencilBits =
371 util_format_get_component_bits(visual->depth_stencil_format,
372 UTIL_FORMAT_COLORSPACE_ZS, 1);
373
374 mode->haveDepthBuffer = mode->depthBits > 0;
375 mode->haveStencilBuffer = mode->stencilBits > 0;
376 }
377
378 if (visual->accum_format != PIPE_FORMAT_NONE) {
379 mode->haveAccumBuffer = GL_TRUE;
380
381 mode->accumRedBits =
382 util_format_get_component_bits(visual->accum_format,
383 UTIL_FORMAT_COLORSPACE_RGB, 0);
384 mode->accumGreenBits =
385 util_format_get_component_bits(visual->accum_format,
386 UTIL_FORMAT_COLORSPACE_RGB, 1);
387 mode->accumBlueBits =
388 util_format_get_component_bits(visual->accum_format,
389 UTIL_FORMAT_COLORSPACE_RGB, 2);
390 mode->accumAlphaBits =
391 util_format_get_component_bits(visual->accum_format,
392 UTIL_FORMAT_COLORSPACE_RGB, 3);
393 }
394
395 if (visual->samples > 1) {
396 mode->sampleBuffers = 1;
397 mode->samples = visual->samples;
398 }
399 }
400
401 /**
402 * Create a framebuffer from a manager interface.
403 */
404 static struct st_framebuffer *
405 st_framebuffer_create(struct st_context *st,
406 struct st_framebuffer_iface *stfbi)
407 {
408 struct st_framebuffer *stfb;
409 struct gl_config mode;
410 gl_buffer_index idx;
411
412 if (!stfbi)
413 return NULL;
414
415 stfb = CALLOC_STRUCT(st_framebuffer);
416 if (!stfb)
417 return NULL;
418
419 st_visual_to_context_mode(stfbi->visual, &mode);
420
421 /*
422 * For desktop GL, sRGB framebuffer write is controlled by both the
423 * capability of the framebuffer and GL_FRAMEBUFFER_SRGB. We should
424 * advertise the capability when the pipe driver (and core Mesa) supports
425 * it so that applications can enable sRGB write when they want to.
426 *
427 * This is not to be confused with GLX_FRAMEBUFFER_SRGB_CAPABLE_ARB. When
428 * the attribute is GLX_TRUE, it tells the st manager to pick a color
429 * format such that util_format_srgb(visual->color_format) can be supported
430 * by the pipe driver. We still need to advertise the capability here.
431 *
432 * For GLES, however, sRGB framebuffer write is controlled only by the
433 * capability of the framebuffer. There is GL_EXT_sRGB_write_control to
434 * give applications the control back, but sRGB write is still enabled by
435 * default. To avoid unexpected results, we should not advertise the
436 * capability. This could change when we add support for
437 * EGL_KHR_gl_colorspace.
438 */
439 if (_mesa_is_desktop_gl(st->ctx)) {
440 struct pipe_screen *screen = st->pipe->screen;
441 const enum pipe_format srgb_format =
442 util_format_srgb(stfbi->visual->color_format);
443
444 if (srgb_format != PIPE_FORMAT_NONE &&
445 st_pipe_format_to_mesa_format(srgb_format) != MESA_FORMAT_NONE &&
446 screen->is_format_supported(screen, srgb_format,
447 PIPE_TEXTURE_2D, stfbi->visual->samples,
448 PIPE_BIND_RENDER_TARGET))
449 mode.sRGBCapable = GL_TRUE;
450 }
451
452 _mesa_initialize_window_framebuffer(&stfb->Base, &mode);
453
454 stfb->iface = stfbi;
455 stfb->iface_stamp = p_atomic_read(&stfbi->stamp) - 1;
456
457 /* add the color buffer */
458 idx = stfb->Base._ColorDrawBufferIndexes[0];
459 if (!st_framebuffer_add_renderbuffer(stfb, idx)) {
460 free(stfb);
461 return NULL;
462 }
463
464 st_framebuffer_add_renderbuffer(stfb, BUFFER_DEPTH);
465 st_framebuffer_add_renderbuffer(stfb, BUFFER_ACCUM);
466
467 stfb->stamp = 0;
468 st_framebuffer_update_attachments(stfb);
469
470 return stfb;
471 }
472
473 /**
474 * Reference a framebuffer.
475 */
476 static void
477 st_framebuffer_reference(struct st_framebuffer **ptr,
478 struct st_framebuffer *stfb)
479 {
480 struct gl_framebuffer *fb = &stfb->Base;
481 _mesa_reference_framebuffer((struct gl_framebuffer **) ptr, fb);
482 }
483
484 static void
485 st_context_flush(struct st_context_iface *stctxi, unsigned flags,
486 struct pipe_fence_handle **fence)
487 {
488 struct st_context *st = (struct st_context *) stctxi;
489 unsigned pipe_flags = 0;
490
491 if (flags & ST_FLUSH_END_OF_FRAME) {
492 pipe_flags |= PIPE_FLUSH_END_OF_FRAME;
493 }
494
495 st_flush(st, fence, pipe_flags);
496 if (flags & ST_FLUSH_FRONT)
497 st_manager_flush_frontbuffer(st);
498 }
499
500 static boolean
501 st_context_teximage(struct st_context_iface *stctxi,
502 enum st_texture_type tex_type,
503 int level, enum pipe_format pipe_format,
504 struct pipe_resource *tex, boolean mipmap)
505 {
506 struct st_context *st = (struct st_context *) stctxi;
507 struct gl_context *ctx = st->ctx;
508 struct gl_texture_object *texObj;
509 struct gl_texture_image *texImage;
510 struct st_texture_object *stObj;
511 struct st_texture_image *stImage;
512 GLenum internalFormat;
513 GLuint width, height, depth;
514 GLenum target;
515
516 switch (tex_type) {
517 case ST_TEXTURE_1D:
518 target = GL_TEXTURE_1D;
519 break;
520 case ST_TEXTURE_2D:
521 target = GL_TEXTURE_2D;
522 break;
523 case ST_TEXTURE_3D:
524 target = GL_TEXTURE_3D;
525 break;
526 case ST_TEXTURE_RECT:
527 target = GL_TEXTURE_RECTANGLE_ARB;
528 break;
529 default:
530 return FALSE;
531 }
532
533 texObj = _mesa_get_current_tex_object(ctx, target);
534
535 _mesa_lock_texture(ctx, texObj);
536
537 stObj = st_texture_object(texObj);
538 /* switch to surface based */
539 if (!stObj->surface_based) {
540 _mesa_clear_texture_object(ctx, texObj);
541 stObj->surface_based = GL_TRUE;
542 }
543
544 texImage = _mesa_get_tex_image(ctx, texObj, target, level);
545 stImage = st_texture_image(texImage);
546 if (tex) {
547 mesa_format texFormat = st_pipe_format_to_mesa_format(pipe_format);
548
549 if (util_format_has_alpha(tex->format))
550 internalFormat = GL_RGBA;
551 else
552 internalFormat = GL_RGB;
553
554 _mesa_init_teximage_fields(ctx, texImage,
555 tex->width0, tex->height0, 1, 0,
556 internalFormat, texFormat);
557
558 width = tex->width0;
559 height = tex->height0;
560 depth = tex->depth0;
561
562 /* grow the image size until we hit level = 0 */
563 while (level > 0) {
564 if (width != 1)
565 width <<= 1;
566 if (height != 1)
567 height <<= 1;
568 if (depth != 1)
569 depth <<= 1;
570 level--;
571 }
572 }
573 else {
574 _mesa_clear_texture_image(ctx, texImage);
575 width = height = depth = 0;
576 }
577
578 pipe_resource_reference(&stImage->pt, tex);
579 stObj->width0 = width;
580 stObj->height0 = height;
581 stObj->depth0 = depth;
582 stObj->surface_format = pipe_format;
583
584 _mesa_dirty_texobj(ctx, texObj);
585 _mesa_unlock_texture(ctx, texObj);
586
587 return TRUE;
588 }
589
590 static void
591 st_context_copy(struct st_context_iface *stctxi,
592 struct st_context_iface *stsrci, unsigned mask)
593 {
594 struct st_context *st = (struct st_context *) stctxi;
595 struct st_context *src = (struct st_context *) stsrci;
596
597 _mesa_copy_context(src->ctx, st->ctx, mask);
598 }
599
600 static boolean
601 st_context_share(struct st_context_iface *stctxi,
602 struct st_context_iface *stsrci)
603 {
604 struct st_context *st = (struct st_context *) stctxi;
605 struct st_context *src = (struct st_context *) stsrci;
606
607 return _mesa_share_state(st->ctx, src->ctx);
608 }
609
610 static void
611 st_context_destroy(struct st_context_iface *stctxi)
612 {
613 struct st_context *st = (struct st_context *) stctxi;
614 st_destroy_context(st);
615 }
616
617 static struct st_context_iface *
618 st_api_create_context(struct st_api *stapi, struct st_manager *smapi,
619 const struct st_context_attribs *attribs,
620 enum st_context_error *error,
621 struct st_context_iface *shared_stctxi)
622 {
623 struct st_context *shared_ctx = (struct st_context *) shared_stctxi;
624 struct st_context *st;
625 struct pipe_context *pipe;
626 struct gl_config mode;
627 gl_api api;
628
629 if (!(stapi->profile_mask & (1 << attribs->profile)))
630 return NULL;
631
632 switch (attribs->profile) {
633 case ST_PROFILE_DEFAULT:
634 api = API_OPENGL_COMPAT;
635 break;
636 case ST_PROFILE_OPENGL_ES1:
637 api = API_OPENGLES;
638 break;
639 case ST_PROFILE_OPENGL_ES2:
640 api = API_OPENGLES2;
641 break;
642 case ST_PROFILE_OPENGL_CORE:
643 api = API_OPENGL_CORE;
644 break;
645 default:
646 *error = ST_CONTEXT_ERROR_BAD_API;
647 return NULL;
648 break;
649 }
650
651 pipe = smapi->screen->context_create(smapi->screen, NULL);
652 if (!pipe) {
653 *error = ST_CONTEXT_ERROR_NO_MEMORY;
654 return NULL;
655 }
656
657 st_visual_to_context_mode(&attribs->visual, &mode);
658 st = st_create_context(api, pipe, &mode, shared_ctx, &attribs->options);
659 if (!st) {
660 *error = ST_CONTEXT_ERROR_NO_MEMORY;
661 pipe->destroy(pipe);
662 return NULL;
663 }
664
665 if (attribs->flags & ST_CONTEXT_FLAG_DEBUG){
666 struct gl_debug_state *debug = _mesa_get_debug_state(st->ctx);
667 if (!debug) {
668 *error = ST_CONTEXT_ERROR_NO_MEMORY;
669 return NULL;
670 }
671 st->ctx->Const.ContextFlags |= GL_CONTEXT_FLAG_DEBUG_BIT;
672 debug->DebugOutput = GL_TRUE;
673 }
674
675 if (attribs->flags & ST_CONTEXT_FLAG_FORWARD_COMPATIBLE)
676 st->ctx->Const.ContextFlags |= GL_CONTEXT_FLAG_FORWARD_COMPATIBLE_BIT;
677
678 /* need to perform version check */
679 if (attribs->major > 1 || attribs->minor > 0) {
680 /* Is the actual version less than the requested version?
681 */
682 if (st->ctx->Version < attribs->major * 10 + attribs->minor) {
683 *error = ST_CONTEXT_ERROR_BAD_VERSION;
684 st_destroy_context(st);
685 return NULL;
686 }
687 }
688
689 st->invalidate_on_gl_viewport =
690 smapi->get_param(smapi, ST_MANAGER_BROKEN_INVALIDATE);
691
692 st->iface.destroy = st_context_destroy;
693 st->iface.flush = st_context_flush;
694 st->iface.teximage = st_context_teximage;
695 st->iface.copy = st_context_copy;
696 st->iface.share = st_context_share;
697 st->iface.st_context_private = (void *) smapi;
698 st->iface.cso_context = st->cso_context;
699 st->iface.pipe = st->pipe;
700
701 *error = ST_CONTEXT_SUCCESS;
702 return &st->iface;
703 }
704
705 static struct st_context_iface *
706 st_api_get_current(struct st_api *stapi)
707 {
708 GET_CURRENT_CONTEXT(ctx);
709 struct st_context *st = (ctx) ? ctx->st : NULL;
710
711 return (st) ? &st->iface : NULL;
712 }
713
714 static struct st_framebuffer *
715 st_framebuffer_reuse_or_create(struct st_context *st,
716 struct gl_framebuffer *fb,
717 struct st_framebuffer_iface *stfbi)
718 {
719 struct st_framebuffer *cur = st_ws_framebuffer(fb), *stfb = NULL;
720
721 /* dummy framebuffers cant be used as st_framebuffer */
722 if (cur && &cur->Base != _mesa_get_incomplete_framebuffer() &&
723 cur->iface == stfbi) {
724 /* reuse the current stfb */
725 st_framebuffer_reference(&stfb, cur);
726 }
727 else {
728 /* create a new one */
729 stfb = st_framebuffer_create(st, stfbi);
730 }
731
732 return stfb;
733 }
734
735 static boolean
736 st_api_make_current(struct st_api *stapi, struct st_context_iface *stctxi,
737 struct st_framebuffer_iface *stdrawi,
738 struct st_framebuffer_iface *streadi)
739 {
740 struct st_context *st = (struct st_context *) stctxi;
741 struct st_framebuffer *stdraw, *stread;
742 boolean ret;
743
744 _glapi_check_multithread();
745
746 if (st) {
747 /* reuse or create the draw fb */
748 stdraw = st_framebuffer_reuse_or_create(st,
749 st->ctx->WinSysDrawBuffer, stdrawi);
750 if (streadi != stdrawi) {
751 /* do the same for the read fb */
752 stread = st_framebuffer_reuse_or_create(st,
753 st->ctx->WinSysReadBuffer, streadi);
754 }
755 else {
756 stread = NULL;
757 /* reuse the draw fb for the read fb */
758 if (stdraw)
759 st_framebuffer_reference(&stread, stdraw);
760 }
761
762 if (stdraw && stread) {
763 st_framebuffer_validate(stdraw, st);
764 if (stread != stdraw)
765 st_framebuffer_validate(stread, st);
766
767 ret = _mesa_make_current(st->ctx, &stdraw->Base, &stread->Base);
768
769 st->draw_stamp = stdraw->stamp - 1;
770 st->read_stamp = stread->stamp - 1;
771 st_context_validate(st, stdraw, stread);
772 }
773 else {
774 struct gl_framebuffer *incomplete = _mesa_get_incomplete_framebuffer();
775 ret = _mesa_make_current(st->ctx, incomplete, incomplete);
776 }
777
778 st_framebuffer_reference(&stdraw, NULL);
779 st_framebuffer_reference(&stread, NULL);
780 }
781 else {
782 ret = _mesa_make_current(NULL, NULL, NULL);
783 }
784
785 return ret;
786 }
787
788 static st_proc_t
789 st_api_get_proc_address(struct st_api *stapi, const char *procname)
790 {
791 return (st_proc_t) _glapi_get_proc_address(procname);
792 }
793
794 static void
795 st_api_destroy(struct st_api *stapi)
796 {
797 }
798
799 /**
800 * Flush the front buffer if the current context renders to the front buffer.
801 */
802 void
803 st_manager_flush_frontbuffer(struct st_context *st)
804 {
805 struct st_framebuffer *stfb = st_ws_framebuffer(st->ctx->DrawBuffer);
806 struct st_renderbuffer *strb = NULL;
807
808 if (stfb)
809 strb = st_renderbuffer(stfb->Base.Attachment[BUFFER_FRONT_LEFT].Renderbuffer);
810 if (!strb)
811 return;
812
813 /* never a dummy fb */
814 assert(&stfb->Base != _mesa_get_incomplete_framebuffer());
815 stfb->iface->flush_front(&st->iface, stfb->iface, ST_ATTACHMENT_FRONT_LEFT);
816 }
817
818 /**
819 * Return the surface of an EGLImage.
820 * FIXME: I think this should operate on resources, not surfaces
821 */
822 struct pipe_surface *
823 st_manager_get_egl_image_surface(struct st_context *st, void *eglimg)
824 {
825 struct st_manager *smapi =
826 (struct st_manager *) st->iface.st_context_private;
827 struct st_egl_image stimg;
828 struct pipe_surface *ps, surf_tmpl;
829
830 if (!smapi || !smapi->get_egl_image)
831 return NULL;
832
833 memset(&stimg, 0, sizeof(stimg));
834 if (!smapi->get_egl_image(smapi, eglimg, &stimg))
835 return NULL;
836
837 u_surface_default_template(&surf_tmpl, stimg.texture);
838 surf_tmpl.u.tex.level = stimg.level;
839 surf_tmpl.u.tex.first_layer = stimg.layer;
840 surf_tmpl.u.tex.last_layer = stimg.layer;
841 ps = st->pipe->create_surface(st->pipe, stimg.texture, &surf_tmpl);
842 pipe_resource_reference(&stimg.texture, NULL);
843
844 return ps;
845 }
846
847 /**
848 * Re-validate the framebuffers.
849 */
850 void
851 st_manager_validate_framebuffers(struct st_context *st)
852 {
853 struct st_framebuffer *stdraw = st_ws_framebuffer(st->ctx->DrawBuffer);
854 struct st_framebuffer *stread = st_ws_framebuffer(st->ctx->ReadBuffer);
855
856 if (stdraw)
857 st_framebuffer_validate(stdraw, st);
858 if (stread && stread != stdraw)
859 st_framebuffer_validate(stread, st);
860
861 st_context_validate(st, stdraw, stread);
862 }
863
864 /**
865 * Add a color renderbuffer on demand.
866 */
867 boolean
868 st_manager_add_color_renderbuffer(struct st_context *st,
869 struct gl_framebuffer *fb,
870 gl_buffer_index idx)
871 {
872 struct st_framebuffer *stfb = st_ws_framebuffer(fb);
873
874 /* FBO */
875 if (!stfb)
876 return FALSE;
877
878 if (stfb->Base.Attachment[idx].Renderbuffer)
879 return TRUE;
880
881 switch (idx) {
882 case BUFFER_FRONT_LEFT:
883 case BUFFER_BACK_LEFT:
884 case BUFFER_FRONT_RIGHT:
885 case BUFFER_BACK_RIGHT:
886 break;
887 default:
888 return FALSE;
889 break;
890 }
891
892 if (!st_framebuffer_add_renderbuffer(stfb, idx))
893 return FALSE;
894
895 st_framebuffer_update_attachments(stfb);
896
897 /*
898 * Force a call to the state tracker manager to validate the
899 * new renderbuffer. It might be that there is a window system
900 * renderbuffer available.
901 */
902 if(stfb->iface)
903 stfb->iface_stamp = p_atomic_read(&stfb->iface->stamp) - 1;
904
905 st_invalidate_state(st->ctx, _NEW_BUFFERS);
906
907 return TRUE;
908 }
909
910 static const struct st_api st_gl_api = {
911 "Mesa " PACKAGE_VERSION,
912 ST_API_OPENGL,
913 ST_PROFILE_DEFAULT_MASK |
914 ST_PROFILE_OPENGL_CORE_MASK |
915 ST_PROFILE_OPENGL_ES1_MASK |
916 ST_PROFILE_OPENGL_ES2_MASK |
917 0,
918 ST_API_FEATURE_MS_VISUALS_MASK,
919 st_api_destroy,
920 st_api_get_proc_address,
921 st_api_create_context,
922 st_api_make_current,
923 st_api_get_current,
924 };
925
926 struct st_api *
927 st_gl_api_create(void)
928 {
929 return (struct st_api *) &st_gl_api;
930 }