2 * Mesa 3-D graphics library
4 * Copyright (C) 2010 LunarG Inc.
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:
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
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.
25 * Chia-I Wu <olv@lunarg.com>
28 #include "main/mtypes.h"
29 #include "main/extensions.h"
30 #include "main/context.h"
31 #include "main/debug_output.h"
32 #include "main/framebuffer.h"
33 #include "main/glthread.h"
34 #include "main/texobj.h"
35 #include "main/teximage.h"
36 #include "main/texstate.h"
37 #include "main/errors.h"
38 #include "main/framebuffer.h"
39 #include "main/fbobject.h"
40 #include "main/renderbuffer.h"
41 #include "main/version.h"
42 #include "util/hash_table.h"
43 #include "st_texture.h"
45 #include "st_context.h"
47 #include "st_extensions.h"
48 #include "st_format.h"
49 #include "st_cb_bitmap.h"
50 #include "st_cb_flush.h"
51 #include "st_manager.h"
52 #include "st_sampler_view.h"
55 #include "pipe/p_context.h"
56 #include "pipe/p_screen.h"
57 #include "util/format/u_format.h"
58 #include "util/u_helpers.h"
59 #include "util/u_pointer.h"
60 #include "util/u_inlines.h"
61 #include "util/u_atomic.h"
62 #include "util/u_surface.h"
63 #include "util/list.h"
64 #include "util/u_memory.h"
65 #include "util/perf/cpu_trace.h"
71 struct hash_table *drawable_ht; /* pipe_frontend_drawable objects hash table */
72 simple_mtx_t st_mutex;
76 * Cast wrapper to convert a struct gl_framebuffer to an gl_framebuffer.
77 * Return NULL if the struct gl_framebuffer is a user-created framebuffer.
78 * We'll only return non-null for window system framebuffers.
79 * Note that this function may fail.
81 static inline struct gl_framebuffer *
82 st_ws_framebuffer(struct gl_framebuffer *fb)
84 /* FBO cannot be casted. See st_new_framebuffer */
85 if (fb && _mesa_is_winsys_fbo(fb) &&
86 fb != _mesa_get_incomplete_framebuffer())
92 * Map an attachment to a buffer index.
94 static inline gl_buffer_index
95 attachment_to_buffer_index(enum st_attachment_type statt)
97 gl_buffer_index index;
100 case ST_ATTACHMENT_FRONT_LEFT:
101 index = BUFFER_FRONT_LEFT;
103 case ST_ATTACHMENT_BACK_LEFT:
104 index = BUFFER_BACK_LEFT;
106 case ST_ATTACHMENT_FRONT_RIGHT:
107 index = BUFFER_FRONT_RIGHT;
109 case ST_ATTACHMENT_BACK_RIGHT:
110 index = BUFFER_BACK_RIGHT;
112 case ST_ATTACHMENT_DEPTH_STENCIL:
113 index = BUFFER_DEPTH;
115 case ST_ATTACHMENT_ACCUM:
116 index = BUFFER_ACCUM;
119 index = BUFFER_COUNT;
128 * Map a buffer index to an attachment.
130 static inline enum st_attachment_type
131 buffer_index_to_attachment(gl_buffer_index index)
133 enum st_attachment_type statt;
136 case BUFFER_FRONT_LEFT:
137 statt = ST_ATTACHMENT_FRONT_LEFT;
139 case BUFFER_BACK_LEFT:
140 statt = ST_ATTACHMENT_BACK_LEFT;
142 case BUFFER_FRONT_RIGHT:
143 statt = ST_ATTACHMENT_FRONT_RIGHT;
145 case BUFFER_BACK_RIGHT:
146 statt = ST_ATTACHMENT_BACK_RIGHT;
149 statt = ST_ATTACHMENT_DEPTH_STENCIL;
152 statt = ST_ATTACHMENT_ACCUM;
155 statt = ST_ATTACHMENT_INVALID;
164 * Make sure a context picks up the latest cached state of the
165 * drawables it binds to.
168 st_context_validate(struct st_context *st,
169 struct gl_framebuffer *stdraw,
170 struct gl_framebuffer *stread)
172 if (stdraw && stdraw->stamp != st->draw_stamp) {
173 st->ctx->NewDriverState |= ST_NEW_FRAMEBUFFER;
174 _mesa_resize_framebuffer(st->ctx, stdraw,
177 st->draw_stamp = stdraw->stamp;
180 if (stread && stread->stamp != st->read_stamp) {
181 if (stread != stdraw) {
182 st->ctx->NewDriverState |= ST_NEW_FRAMEBUFFER;
183 _mesa_resize_framebuffer(st->ctx, stread,
187 st->read_stamp = stread->stamp;
193 st_set_ws_renderbuffer_surface(struct gl_renderbuffer *rb,
194 struct pipe_surface *surf)
196 pipe_surface_reference(&rb->surface_srgb, NULL);
197 pipe_surface_reference(&rb->surface_linear, NULL);
199 if (util_format_is_srgb(surf->format))
200 pipe_surface_reference(&rb->surface_srgb, surf);
202 pipe_surface_reference(&rb->surface_linear, surf);
204 rb->surface = surf; /* just assign, don't ref */
205 pipe_resource_reference(&rb->texture, surf->texture);
207 rb->Width = surf->width;
208 rb->Height = surf->height;
213 * Validate a framebuffer to make sure up-to-date pipe_textures are used.
214 * The context is only used for creating pipe surfaces and for calling
215 * _mesa_resize_framebuffer().
216 * (That should probably be rethought, since those surfaces become
217 * drawable state, not context state, and can be freed by another pipe
221 st_framebuffer_validate(struct gl_framebuffer *stfb,
222 struct st_context *st)
224 struct pipe_resource *textures[ST_ATTACHMENT_COUNT];
225 struct pipe_resource *resolve = NULL;
228 bool changed = false;
231 new_stamp = p_atomic_read(&stfb->drawable->stamp);
232 if (stfb->drawable_stamp == new_stamp)
235 memset(textures, 0, stfb->num_statts * sizeof(textures[0]));
237 /* validate the fb */
239 if (!stfb->drawable->validate(st, stfb->drawable, stfb->statts,
240 stfb->num_statts, textures, &resolve))
243 stfb->drawable_stamp = new_stamp;
244 new_stamp = p_atomic_read(&stfb->drawable->stamp);
245 } while(stfb->drawable_stamp != new_stamp);
248 height = stfb->Height;
250 for (i = 0; i < stfb->num_statts; i++) {
251 struct gl_renderbuffer *rb;
252 struct pipe_surface *ps, surf_tmpl;
258 idx = attachment_to_buffer_index(stfb->statts[i]);
259 if (idx >= BUFFER_COUNT) {
260 pipe_resource_reference(&textures[i], NULL);
264 rb = stfb->Attachment[idx].Renderbuffer;
266 if (rb->texture == textures[i] &&
267 rb->Width == textures[i]->width0 &&
268 rb->Height == textures[i]->height0) {
269 pipe_resource_reference(&textures[i], NULL);
273 u_surface_default_template(&surf_tmpl, textures[i]);
274 ps = st->pipe->create_surface(st->pipe, textures[i], &surf_tmpl);
276 st_set_ws_renderbuffer_surface(rb, ps);
277 pipe_surface_reference(&ps, NULL);
285 pipe_resource_reference(&textures[i], NULL);
288 changed |= resolve != stfb->resolve;
289 /* ref is removed here */
290 pipe_resource_reference(&stfb->resolve, NULL);
291 /* ref is taken here */
292 stfb->resolve = resolve;
296 _mesa_resize_framebuffer(st->ctx, stfb, width, height);
301 * Return true if the visual has the specified buffers.
304 st_visual_have_buffers(const struct st_visual *visual, unsigned mask)
306 return ((visual->buffer_mask & mask) == mask);
310 * Update the attachments to validate by looping the existing renderbuffers.
313 st_framebuffer_update_attachments(struct gl_framebuffer *stfb)
317 stfb->num_statts = 0;
319 for (enum st_attachment_type i = 0; i < ST_ATTACHMENT_COUNT; i++)
320 stfb->statts[i] = ST_ATTACHMENT_INVALID;
322 for (idx = 0; idx < BUFFER_COUNT; idx++) {
323 struct gl_renderbuffer *rb;
324 enum st_attachment_type statt;
326 rb = stfb->Attachment[idx].Renderbuffer;
327 if (!rb || rb->software)
330 statt = buffer_index_to_attachment(idx);
331 if (statt != ST_ATTACHMENT_INVALID &&
332 st_visual_have_buffers(stfb->drawable->visual, 1 << statt))
333 stfb->statts[stfb->num_statts++] = statt;
339 * Allocate a renderbuffer for an on-screen window (not a user-created
340 * renderbuffer). The window system code determines the format.
342 static struct gl_renderbuffer *
343 st_new_renderbuffer_fb(enum pipe_format format, unsigned samples, bool sw)
345 struct gl_renderbuffer *rb;
347 rb = CALLOC_STRUCT(gl_renderbuffer);
349 _mesa_error(NULL, GL_OUT_OF_MEMORY, "creating renderbuffer");
353 _mesa_init_renderbuffer(rb, 0);
354 rb->ClassID = 0x4242; /* just a unique value */
355 rb->NumSamples = samples;
356 rb->NumStorageSamples = samples;
357 rb->Format = st_pipe_format_to_mesa_format(format);
358 rb->_BaseFormat = _mesa_get_format_base_format(rb->Format);
362 case PIPE_FORMAT_B10G10R10A2_UNORM:
363 case PIPE_FORMAT_R10G10B10A2_UNORM:
364 rb->InternalFormat = GL_RGB10_A2;
366 case PIPE_FORMAT_R10G10B10X2_UNORM:
367 case PIPE_FORMAT_B10G10R10X2_UNORM:
368 rb->InternalFormat = GL_RGB10;
370 case PIPE_FORMAT_R8G8B8A8_UNORM:
371 case PIPE_FORMAT_B8G8R8A8_UNORM:
372 case PIPE_FORMAT_A8R8G8B8_UNORM:
373 rb->InternalFormat = GL_RGBA8;
375 case PIPE_FORMAT_R8G8B8X8_UNORM:
376 case PIPE_FORMAT_B8G8R8X8_UNORM:
377 case PIPE_FORMAT_X8R8G8B8_UNORM:
378 case PIPE_FORMAT_R8G8B8_UNORM:
379 rb->InternalFormat = GL_RGB8;
381 case PIPE_FORMAT_R8G8B8A8_SRGB:
382 case PIPE_FORMAT_B8G8R8A8_SRGB:
383 case PIPE_FORMAT_A8R8G8B8_SRGB:
384 rb->InternalFormat = GL_SRGB8_ALPHA8;
386 case PIPE_FORMAT_R8G8B8X8_SRGB:
387 case PIPE_FORMAT_B8G8R8X8_SRGB:
388 case PIPE_FORMAT_X8R8G8B8_SRGB:
389 rb->InternalFormat = GL_SRGB8;
391 case PIPE_FORMAT_B5G5R5A1_UNORM:
392 rb->InternalFormat = GL_RGB5_A1;
394 case PIPE_FORMAT_B4G4R4A4_UNORM:
395 rb->InternalFormat = GL_RGBA4;
397 case PIPE_FORMAT_B5G6R5_UNORM:
398 rb->InternalFormat = GL_RGB565;
400 case PIPE_FORMAT_Z16_UNORM:
401 rb->InternalFormat = GL_DEPTH_COMPONENT16;
403 case PIPE_FORMAT_Z32_UNORM:
404 rb->InternalFormat = GL_DEPTH_COMPONENT32;
406 case PIPE_FORMAT_Z24_UNORM_S8_UINT:
407 case PIPE_FORMAT_S8_UINT_Z24_UNORM:
408 rb->InternalFormat = GL_DEPTH24_STENCIL8_EXT;
410 case PIPE_FORMAT_Z24X8_UNORM:
411 case PIPE_FORMAT_X8Z24_UNORM:
412 rb->InternalFormat = GL_DEPTH_COMPONENT24;
414 case PIPE_FORMAT_S8_UINT:
415 rb->InternalFormat = GL_STENCIL_INDEX8_EXT;
417 case PIPE_FORMAT_R16G16B16A16_SNORM:
419 rb->InternalFormat = GL_RGBA16_SNORM;
421 case PIPE_FORMAT_R16G16B16A16_UNORM:
422 rb->InternalFormat = GL_RGBA16;
424 case PIPE_FORMAT_R16G16B16_UNORM:
425 rb->InternalFormat = GL_RGB16;
427 case PIPE_FORMAT_R8_UNORM:
428 rb->InternalFormat = GL_R8;
430 case PIPE_FORMAT_R8G8_UNORM:
431 rb->InternalFormat = GL_RG8;
433 case PIPE_FORMAT_R16_UNORM:
434 rb->InternalFormat = GL_R16;
436 case PIPE_FORMAT_R16G16_UNORM:
437 rb->InternalFormat = GL_RG16;
439 case PIPE_FORMAT_R32G32B32A32_FLOAT:
440 rb->InternalFormat = GL_RGBA32F;
442 case PIPE_FORMAT_R32G32B32X32_FLOAT:
443 case PIPE_FORMAT_R32G32B32_FLOAT:
444 rb->InternalFormat = GL_RGB32F;
446 case PIPE_FORMAT_R16G16B16A16_FLOAT:
447 rb->InternalFormat = GL_RGBA16F;
449 case PIPE_FORMAT_R16G16B16X16_FLOAT:
450 rb->InternalFormat = GL_RGB16F;
454 "Unexpected format %s in st_new_renderbuffer_fb",
455 util_format_name(format));
466 * Add a renderbuffer to the framebuffer. The framebuffer is one that
467 * corresponds to a window and is not a user-created FBO.
470 st_framebuffer_add_renderbuffer(struct gl_framebuffer *stfb,
471 gl_buffer_index idx, bool prefer_srgb)
473 struct gl_renderbuffer *rb;
474 enum pipe_format format;
477 assert(_mesa_is_winsys_fbo(stfb));
479 /* do not distinguish depth/stencil buffers */
480 if (idx == BUFFER_STENCIL)
485 format = stfb->drawable->visual->depth_stencil_format;
489 format = stfb->drawable->visual->accum_format;
493 format = stfb->drawable->visual->color_format;
495 format = util_format_srgb(format);
500 if (format == PIPE_FORMAT_NONE)
503 rb = st_new_renderbuffer_fb(format, stfb->drawable->visual->samples, sw);
507 if (idx != BUFFER_DEPTH) {
508 _mesa_attach_and_own_rb(stfb, idx, rb);
512 bool rb_ownership_taken = false;
513 if (util_format_get_component_bits(format, UTIL_FORMAT_COLORSPACE_ZS, 0)) {
514 _mesa_attach_and_own_rb(stfb, BUFFER_DEPTH, rb);
515 rb_ownership_taken = true;
518 if (util_format_get_component_bits(format, UTIL_FORMAT_COLORSPACE_ZS, 1)) {
519 if (rb_ownership_taken)
520 _mesa_attach_and_reference_rb(stfb, BUFFER_STENCIL, rb);
522 _mesa_attach_and_own_rb(stfb, BUFFER_STENCIL, rb);
530 * Intialize a struct gl_config from a visual.
533 st_visual_to_context_mode(const struct st_visual *visual,
534 struct gl_config *mode)
536 memset(mode, 0, sizeof(*mode));
538 if (st_visual_have_buffers(visual, ST_ATTACHMENT_BACK_LEFT_MASK))
539 mode->doubleBufferMode = GL_TRUE;
541 if (st_visual_have_buffers(visual,
542 ST_ATTACHMENT_FRONT_RIGHT_MASK | ST_ATTACHMENT_BACK_RIGHT_MASK))
543 mode->stereoMode = GL_TRUE;
545 if (visual->color_format != PIPE_FORMAT_NONE) {
547 util_format_get_component_bits(visual->color_format,
548 UTIL_FORMAT_COLORSPACE_RGB, 0);
550 util_format_get_component_bits(visual->color_format,
551 UTIL_FORMAT_COLORSPACE_RGB, 1);
553 util_format_get_component_bits(visual->color_format,
554 UTIL_FORMAT_COLORSPACE_RGB, 2);
556 util_format_get_component_bits(visual->color_format,
557 UTIL_FORMAT_COLORSPACE_RGB, 3);
559 mode->rgbBits = mode->redBits +
560 mode->greenBits + mode->blueBits + mode->alphaBits;
561 mode->sRGBCapable = util_format_is_srgb(visual->color_format);
562 mode->floatMode = util_format_is_float(visual->color_format);
565 if (visual->depth_stencil_format != PIPE_FORMAT_NONE) {
567 util_format_get_component_bits(visual->depth_stencil_format,
568 UTIL_FORMAT_COLORSPACE_ZS, 0);
570 util_format_get_component_bits(visual->depth_stencil_format,
571 UTIL_FORMAT_COLORSPACE_ZS, 1);
574 if (visual->accum_format != PIPE_FORMAT_NONE) {
576 util_format_get_component_bits(visual->accum_format,
577 UTIL_FORMAT_COLORSPACE_RGB, 0);
578 mode->accumGreenBits =
579 util_format_get_component_bits(visual->accum_format,
580 UTIL_FORMAT_COLORSPACE_RGB, 1);
581 mode->accumBlueBits =
582 util_format_get_component_bits(visual->accum_format,
583 UTIL_FORMAT_COLORSPACE_RGB, 2);
584 mode->accumAlphaBits =
585 util_format_get_component_bits(visual->accum_format,
586 UTIL_FORMAT_COLORSPACE_RGB, 3);
589 if (visual->samples > 1) {
590 mode->samples = visual->samples;
596 * Create a framebuffer from a manager interface.
598 static struct gl_framebuffer *
599 st_framebuffer_create(struct st_context *st,
600 struct pipe_frontend_drawable *drawable)
602 struct gl_framebuffer *stfb;
603 struct gl_config mode;
605 bool prefer_srgb = false;
610 stfb = CALLOC_STRUCT(gl_framebuffer);
614 st_visual_to_context_mode(drawable->visual, &mode);
617 * For desktop GL, sRGB framebuffer write is controlled by both the
618 * capability of the framebuffer and GL_FRAMEBUFFER_SRGB. We should
619 * advertise the capability when the pipe driver (and core Mesa) supports
620 * it so that applications can enable sRGB write when they want to.
622 * This is not to be confused with GLX_FRAMEBUFFER_SRGB_CAPABLE_ARB. When
623 * the attribute is GLX_TRUE, it tells the st manager to pick a color
624 * format such that util_format_srgb(visual->color_format) can be supported
625 * by the pipe driver. We still need to advertise the capability here.
627 * For GLES, however, sRGB framebuffer write is initially only controlled
628 * by the capability of the framebuffer, with GL_EXT_sRGB_write_control
629 * control is given back to the applications, but GL_FRAMEBUFFER_SRGB is
630 * still enabled by default since this is the behaviour when
631 * EXT_sRGB_write_control is not available. Since GL_EXT_sRGB_write_control
632 * brings GLES on par with desktop GLs EXT_framebuffer_sRGB, in mesa this
633 * is also expressed by using the same extension flag
635 if (_mesa_has_EXT_framebuffer_sRGB(st->ctx)) {
636 struct pipe_screen *screen = st->screen;
637 const enum pipe_format srgb_format =
638 util_format_srgb(drawable->visual->color_format);
640 if (srgb_format != PIPE_FORMAT_NONE &&
641 st_pipe_format_to_mesa_format(srgb_format) != MESA_FORMAT_NONE &&
642 screen->is_format_supported(screen, srgb_format,
643 PIPE_TEXTURE_2D, drawable->visual->samples,
644 drawable->visual->samples,
645 (PIPE_BIND_DISPLAY_TARGET |
646 PIPE_BIND_RENDER_TARGET))) {
647 mode.sRGBCapable = GL_TRUE;
648 /* Since GL_FRAMEBUFFER_SRGB is enabled by default on GLES we must not
649 * create renderbuffers with an sRGB format derived from the
650 * visual->color_format, but we still want sRGB for desktop GL.
652 prefer_srgb = _mesa_is_desktop_gl(st->ctx);
656 _mesa_initialize_window_framebuffer(stfb, &mode);
658 stfb->drawable = drawable;
659 stfb->drawable_ID = drawable->ID;
660 stfb->drawable_stamp = p_atomic_read(&drawable->stamp) - 1;
662 /* add the color buffer */
663 idx = stfb->_ColorDrawBufferIndexes[0];
664 if (!st_framebuffer_add_renderbuffer(stfb, idx, prefer_srgb)) {
669 st_framebuffer_add_renderbuffer(stfb, BUFFER_DEPTH, false);
670 st_framebuffer_add_renderbuffer(stfb, BUFFER_ACCUM, false);
673 st_framebuffer_update_attachments(stfb);
680 drawable_hash(const void *key)
682 return (uintptr_t)key;
687 drawable_equal(const void *a, const void *b)
689 return (struct pipe_frontend_drawable *)a == (struct pipe_frontend_drawable *)b;
694 drawable_lookup(struct pipe_frontend_screen *fscreen,
695 const struct pipe_frontend_drawable *drawable)
697 struct st_screen *screen =
698 (struct st_screen *)fscreen->st_screen;
699 struct hash_entry *entry;
702 assert(screen->drawable_ht);
704 simple_mtx_lock(&screen->st_mutex);
705 entry = _mesa_hash_table_search(screen->drawable_ht, drawable);
706 simple_mtx_unlock(&screen->st_mutex);
708 return entry != NULL;
713 drawable_insert(struct pipe_frontend_screen *fscreen,
714 struct pipe_frontend_drawable *drawable)
716 struct st_screen *screen =
717 (struct st_screen *)fscreen->st_screen;
718 struct hash_entry *entry;
721 assert(screen->drawable_ht);
723 simple_mtx_lock(&screen->st_mutex);
724 entry = _mesa_hash_table_insert(screen->drawable_ht, drawable, drawable);
725 simple_mtx_unlock(&screen->st_mutex);
727 return entry != NULL;
732 drawable_remove(struct pipe_frontend_screen *fscreen,
733 struct pipe_frontend_drawable *drawable)
735 struct st_screen *screen =
736 (struct st_screen *)fscreen->st_screen;
737 struct hash_entry *entry;
739 if (!screen || !screen->drawable_ht)
742 simple_mtx_lock(&screen->st_mutex);
743 entry = _mesa_hash_table_search(screen->drawable_ht, drawable);
747 _mesa_hash_table_remove(screen->drawable_ht, entry);
750 simple_mtx_unlock(&screen->st_mutex);
755 * The framebuffer interface object is no longer valid.
756 * Remove the object from the framebuffer interface hash table.
759 st_api_destroy_drawable(struct pipe_frontend_drawable *drawable)
764 drawable_remove(drawable->fscreen, drawable);
769 * Purge the winsys buffers list to remove any references to
770 * non-existing framebuffer interface objects.
773 st_framebuffers_purge(struct st_context *st)
775 struct pipe_frontend_screen *fscreen = st->frontend_screen;
776 struct gl_framebuffer *stfb, *next;
780 LIST_FOR_EACH_ENTRY_SAFE_REV(stfb, next, &st->winsys_buffers, head) {
781 struct pipe_frontend_drawable *drawable = stfb->drawable;
786 * If the corresponding framebuffer interface object no longer exists,
787 * remove the framebuffer object from the context's winsys buffers list,
788 * and unreference the framebuffer object, so its resources can be
791 if (!drawable_lookup(fscreen, drawable)) {
792 list_del(&stfb->head);
793 _mesa_reference_framebuffer(&stfb, NULL);
800 st_context_flush(struct st_context *st, unsigned flags,
801 struct pipe_fence_handle **fence,
802 void (*before_flush_cb) (void*), void* args)
804 unsigned pipe_flags = 0;
808 if (flags & ST_FLUSH_END_OF_FRAME)
809 pipe_flags |= PIPE_FLUSH_END_OF_FRAME;
810 if (flags & ST_FLUSH_FENCE_FD)
811 pipe_flags |= PIPE_FLUSH_FENCE_FD;
813 /* We can do these in any order because FLUSH_VERTICES will also flush
814 * the bitmap cache if there are any unflushed vertices.
816 st_flush_bitmap_cache(st);
817 FLUSH_VERTICES(st->ctx, 0, 0);
819 /* Notify the caller that we're ready to flush */
821 before_flush_cb(args);
822 st_flush(st, fence, pipe_flags);
824 if ((flags & ST_FLUSH_WAIT) && fence && *fence) {
825 st->screen->fence_finish(st->screen, NULL, *fence,
826 OS_TIMEOUT_INFINITE);
827 st->screen->fence_reference(st->screen, fence, NULL);
830 if (flags & ST_FLUSH_FRONT)
831 st_manager_flush_frontbuffer(st);
835 * Replace the texture image of a texture object at the specified level.
837 * This is only for GLX_EXT_texture_from_pixmap and equivalent features
841 st_context_teximage(struct st_context *st, GLenum target,
842 int level, enum pipe_format pipe_format,
843 struct pipe_resource *tex, bool mipmap)
845 struct gl_context *ctx = st->ctx;
846 struct gl_texture_object *texObj;
847 struct gl_texture_image *texImage;
848 GLenum internalFormat;
849 GLuint width, height, depth;
851 texObj = _mesa_get_current_tex_object(ctx, target);
853 _mesa_lock_texture(ctx, texObj);
855 /* switch to surface based */
856 if (!texObj->surface_based) {
857 _mesa_clear_texture_object(ctx, texObj, NULL);
858 texObj->surface_based = GL_TRUE;
861 texImage = _mesa_get_tex_image(ctx, texObj, target, level);
863 mesa_format texFormat = st_pipe_format_to_mesa_format(pipe_format);
865 if (util_format_has_alpha(tex->format))
866 internalFormat = GL_RGBA;
868 internalFormat = GL_RGB;
870 _mesa_init_teximage_fields(ctx, texImage,
871 tex->width0, tex->height0, 1, 0,
872 internalFormat, texFormat);
875 height = tex->height0;
878 /* grow the image size until we hit level = 0 */
890 _mesa_clear_texture_image(ctx, texImage);
891 width = height = depth = 0;
893 _mesa_update_texture_object_swizzle(ctx, texObj);
895 pipe_resource_reference(&texObj->pt, tex);
896 st_texture_release_all_sampler_views(st, texObj);
897 pipe_resource_reference(&texImage->pt, tex);
898 texObj->surface_format = pipe_format;
900 texObj->needs_validation = true;
902 _mesa_dirty_texobj(ctx, texObj);
903 ctx->Shared->HasExternallySharedImages = true;
904 _mesa_unlock_texture(ctx, texObj);
911 * Invalidate states to notify the frontend that driver states have been
912 * changed behind its back.
915 st_context_invalidate_state(struct st_context *st, unsigned flags)
917 struct gl_context *ctx = st->ctx;
919 if (flags & ST_INVALIDATE_FS_SAMPLER_VIEWS)
920 ctx->NewDriverState |= ST_NEW_FS_SAMPLER_VIEWS;
921 if (flags & ST_INVALIDATE_FS_CONSTBUF0)
922 ctx->NewDriverState |= ST_NEW_FS_CONSTANTS;
923 if (flags & ST_INVALIDATE_VS_CONSTBUF0)
924 ctx->NewDriverState |= ST_NEW_VS_CONSTANTS;
925 if (flags & ST_INVALIDATE_VERTEX_BUFFERS) {
926 ctx->Array.NewVertexElements = true;
927 ctx->NewDriverState |= ST_NEW_VERTEX_ARRAYS;
929 if (flags & ST_INVALIDATE_FB_STATE)
930 ctx->NewDriverState |= ST_NEW_FB_STATE;
935 st_screen_destroy(struct pipe_frontend_screen *fscreen)
937 struct st_screen *screen = fscreen->st_screen;
939 if (screen && screen->drawable_ht) {
940 _mesa_hash_table_destroy(screen->drawable_ht, NULL);
941 simple_mtx_destroy(&screen->st_mutex);
943 fscreen->st_screen = NULL;
949 * Create a rendering context.
952 st_api_create_context(struct pipe_frontend_screen *fscreen,
953 const struct st_context_attribs *attribs,
954 enum st_context_error *error,
955 struct st_context *shared_ctx)
957 struct st_context *st;
958 struct pipe_context *pipe;
959 struct gl_config mode, *mode_ptr = &mode;
960 bool no_error = false;
962 _mesa_initialize(attribs->options.mesa_extension_override);
964 /* Create a hash table for the framebuffer interface objects
965 * if it has not been created for this st manager.
967 if (fscreen->st_screen == NULL) {
968 struct st_screen *screen;
970 screen = CALLOC_STRUCT(st_screen);
971 simple_mtx_init(&screen->st_mutex, mtx_plain);
972 screen->drawable_ht = _mesa_hash_table_create(NULL,
975 fscreen->st_screen = screen;
978 if (attribs->flags & ST_CONTEXT_FLAG_NO_ERROR)
981 /* OpenGL ES 2.0+ does not support sampler state LOD bias. If we are creating
982 * a GLES context, communicate that to the the driver to allow optimization.
984 bool is_gles = attribs->profile == API_OPENGLES2;
985 unsigned lod_bias_flag = is_gles ? PIPE_CONTEXT_NO_LOD_BIAS : 0;
987 pipe = fscreen->screen->context_create(fscreen->screen, NULL,
988 PIPE_CONTEXT_PREFER_THREADED |
990 attribs->context_flags);
992 *error = ST_CONTEXT_ERROR_NO_MEMORY;
996 st_visual_to_context_mode(&attribs->visual, &mode);
997 if (attribs->visual.color_format == PIPE_FORMAT_NONE)
999 st = st_create_context(attribs->profile, pipe, mode_ptr, shared_ctx,
1000 &attribs->options, no_error,
1001 !!fscreen->validate_egl_image);
1003 *error = ST_CONTEXT_ERROR_NO_MEMORY;
1004 pipe->destroy(pipe);
1008 if (attribs->flags & ST_CONTEXT_FLAG_DEBUG) {
1009 if (!_mesa_set_debug_state_int(st->ctx, GL_DEBUG_OUTPUT, GL_TRUE)) {
1010 *error = ST_CONTEXT_ERROR_NO_MEMORY;
1014 st->ctx->Const.ContextFlags |= GL_CONTEXT_FLAG_DEBUG_BIT;
1017 if (st->ctx->Const.ContextFlags & GL_CONTEXT_FLAG_DEBUG_BIT) {
1018 _mesa_update_debug_callback(st->ctx);
1021 if (attribs->flags & ST_CONTEXT_FLAG_FORWARD_COMPATIBLE)
1022 st->ctx->Const.ContextFlags |= GL_CONTEXT_FLAG_FORWARD_COMPATIBLE_BIT;
1024 if (attribs->context_flags & PIPE_CONTEXT_ROBUST_BUFFER_ACCESS) {
1025 st->ctx->Const.ContextFlags |= GL_CONTEXT_FLAG_ROBUST_ACCESS_BIT_ARB;
1026 st->ctx->Const.RobustAccess = GL_TRUE;
1029 if (attribs->context_flags & PIPE_CONTEXT_LOSE_CONTEXT_ON_RESET) {
1030 st->ctx->Const.ResetStrategy = GL_LOSE_CONTEXT_ON_RESET_ARB;
1031 st_install_device_reset_callback(st);
1034 if (attribs->flags & ST_CONTEXT_FLAG_RELEASE_NONE)
1035 st->ctx->Const.ContextReleaseBehavior = GL_NONE;
1037 /* need to perform version check */
1038 if (attribs->major > 1 || attribs->minor > 0) {
1039 /* Is the actual version less than the requested version?
1041 if (st->ctx->Version < attribs->major * 10U + attribs->minor) {
1042 *error = ST_CONTEXT_ERROR_BAD_VERSION;
1043 st_destroy_context(st);
1048 st->can_scissor_clear = !!st->screen->get_param(st->screen, PIPE_CAP_CLEAR_SCISSORED);
1050 st->ctx->invalidate_on_gl_viewport =
1051 fscreen->get_param(fscreen, ST_MANAGER_BROKEN_INVALIDATE);
1053 st->frontend_screen = fscreen;
1055 if (st->ctx->IntelBlackholeRender &&
1056 st->screen->get_param(st->screen, PIPE_CAP_FRONTEND_NOOP))
1057 st->pipe->set_frontend_noop(st->pipe, st->ctx->IntelBlackholeRender);
1059 *error = ST_CONTEXT_SUCCESS;
1065 * Get the currently bound context in the calling thread.
1068 st_api_get_current(void)
1070 GET_CURRENT_CONTEXT(ctx);
1072 return ctx ? ctx->st : NULL;
1076 static struct gl_framebuffer *
1077 st_framebuffer_reuse_or_create(struct st_context *st,
1078 struct pipe_frontend_drawable *drawable)
1080 struct gl_framebuffer *cur = NULL, *stfb = NULL;
1085 /* Check if there is already a framebuffer object for the specified
1086 * framebuffer interface in this context. If there is one, use it.
1088 LIST_FOR_EACH_ENTRY(cur, &st->winsys_buffers, head) {
1089 if (cur->drawable_ID == drawable->ID) {
1090 _mesa_reference_framebuffer(&stfb, cur);
1095 /* If there is not already a framebuffer object, create one */
1097 cur = st_framebuffer_create(st, drawable);
1100 /* add the referenced framebuffer interface object to
1101 * the framebuffer interface object hash table.
1103 if (!drawable_insert(drawable->fscreen, drawable)) {
1104 _mesa_reference_framebuffer(&cur, NULL);
1108 /* add to the context's winsys buffers list */
1109 list_add(&cur->head, &st->winsys_buffers);
1111 _mesa_reference_framebuffer(&stfb, cur);
1120 * Bind the context to the calling thread with draw and read as drawables.
1122 * The framebuffers might be NULL, meaning the context is surfaceless.
1125 st_api_make_current(struct st_context *st,
1126 struct pipe_frontend_drawable *stdrawi,
1127 struct pipe_frontend_drawable *streadi)
1129 struct gl_framebuffer *stdraw, *stread;
1133 /* reuse or create the draw fb */
1134 stdraw = st_framebuffer_reuse_or_create(st, stdrawi);
1135 if (streadi != stdrawi) {
1136 /* do the same for the read fb */
1137 stread = st_framebuffer_reuse_or_create(st, streadi);
1141 /* reuse the draw fb for the read fb */
1143 _mesa_reference_framebuffer(&stread, stdraw);
1146 /* If framebuffers were asked for, we'd better have allocated them */
1147 if ((stdrawi && !stdraw) || (streadi && !stread))
1150 if (stdraw && stread) {
1151 st_framebuffer_validate(stdraw, st);
1152 if (stread != stdraw)
1153 st_framebuffer_validate(stread, st);
1155 ret = _mesa_make_current(st->ctx, stdraw, stread);
1157 st->draw_stamp = stdraw->stamp - 1;
1158 st->read_stamp = stread->stamp - 1;
1159 st_context_validate(st, stdraw, stread);
1162 struct gl_framebuffer *incomplete = _mesa_get_incomplete_framebuffer();
1163 ret = _mesa_make_current(st->ctx, incomplete, incomplete);
1166 _mesa_reference_framebuffer(&stdraw, NULL);
1167 _mesa_reference_framebuffer(&stread, NULL);
1169 /* Purge the context's winsys_buffers list in case any
1170 * of the referenced drawables no longer exist.
1172 st_framebuffers_purge(st);
1175 GET_CURRENT_CONTEXT(ctx);
1178 /* Before releasing the context, release its associated
1179 * winsys buffers first. Then purge the context's winsys buffers list
1180 * to free the resources of any winsys buffers that no longer have
1181 * an existing drawable.
1183 ret = _mesa_make_current(ctx, NULL, NULL);
1184 st_framebuffers_purge(ctx->st);
1187 ret = _mesa_make_current(NULL, NULL, NULL);
1195 * Flush the front buffer if the current context renders to the front buffer.
1198 st_manager_flush_frontbuffer(struct st_context *st)
1200 struct gl_framebuffer *stfb = st_ws_framebuffer(st->ctx->DrawBuffer);
1201 struct gl_renderbuffer *rb = NULL;
1206 /* If the context uses a doublebuffered visual, but the buffer is
1207 * single-buffered, guess that it's a pbuffer, which doesn't need
1210 if (st->ctx->Visual.doubleBufferMode &&
1211 !stfb->Visual.doubleBufferMode)
1214 /* Check front buffer used at the GL API level. */
1215 enum st_attachment_type statt = ST_ATTACHMENT_FRONT_LEFT;
1216 rb = stfb->Attachment[BUFFER_FRONT_LEFT].Renderbuffer;
1218 /* Check back buffer redirected by EGL_KHR_mutable_render_buffer. */
1219 statt = ST_ATTACHMENT_BACK_LEFT;
1220 rb = stfb->Attachment[BUFFER_BACK_LEFT].Renderbuffer;
1223 /* Do we have a front color buffer and has it been drawn to since last
1224 * frontbuffer flush?
1226 if (rb && rb->defined &&
1227 stfb->drawable->flush_front(st, stfb->drawable, statt)) {
1228 rb->defined = GL_FALSE;
1230 /* Trigger an update of rb->defined on next draw */
1231 st->ctx->NewDriverState |= ST_NEW_FB_STATE;
1237 * Re-validate the framebuffers.
1240 st_manager_validate_framebuffers(struct st_context *st)
1242 struct gl_framebuffer *stdraw = st_ws_framebuffer(st->ctx->DrawBuffer);
1243 struct gl_framebuffer *stread = st_ws_framebuffer(st->ctx->ReadBuffer);
1246 st_framebuffer_validate(stdraw, st);
1247 if (stread && stread != stdraw)
1248 st_framebuffer_validate(stread, st);
1250 st_context_validate(st, stdraw, stread);
1255 * Flush any outstanding swapbuffers on the current draw framebuffer.
1258 st_manager_flush_swapbuffers(void)
1260 GET_CURRENT_CONTEXT(ctx);
1261 struct st_context *st = (ctx) ? ctx->st : NULL;
1262 struct gl_framebuffer *stfb;
1267 stfb = st_ws_framebuffer(ctx->DrawBuffer);
1268 if (!stfb || !stfb->drawable->flush_swapbuffers)
1271 stfb->drawable->flush_swapbuffers(st, stfb->drawable);
1276 * Add a color renderbuffer on demand. The FBO must correspond to a window,
1277 * not a user-created FBO.
1280 st_manager_add_color_renderbuffer(struct gl_context *ctx,
1281 struct gl_framebuffer *fb,
1282 gl_buffer_index idx)
1284 struct gl_framebuffer *stfb = st_ws_framebuffer(fb);
1290 assert(_mesa_is_winsys_fbo(fb));
1292 if (stfb->Attachment[idx].Renderbuffer)
1296 case BUFFER_FRONT_LEFT:
1297 case BUFFER_BACK_LEFT:
1298 case BUFFER_FRONT_RIGHT:
1299 case BUFFER_BACK_RIGHT:
1305 if (!st_framebuffer_add_renderbuffer(stfb, idx,
1306 stfb->Visual.sRGBCapable))
1309 st_framebuffer_update_attachments(stfb);
1312 * Force a call to the frontend manager to validate the
1313 * new renderbuffer. It might be that there is a window system
1314 * renderbuffer available.
1317 stfb->drawable_stamp = p_atomic_read(&stfb->drawable->stamp) - 1;
1319 st_invalidate_buffers(st_context(ctx));
1326 get_version(struct pipe_screen *screen,
1327 struct st_config_options *options, gl_api api)
1329 struct gl_constants consts = {0};
1330 struct gl_extensions extensions = {0};
1333 if (_mesa_override_gl_version_contextless(&consts, &api, &version)) {
1337 _mesa_init_constants(&consts, api);
1338 _mesa_init_extensions(&extensions);
1340 st_init_limits(screen, &consts, &extensions, api);
1341 st_init_extensions(screen, &consts, &extensions, options, api);
1342 version = _mesa_get_version(&extensions, &consts, api);
1343 free(consts.SpirVExtensions);
1349 * Query supported OpenGL versions. (if applicable)
1350 * The format is (major*10+minor).
1353 st_api_query_versions(struct pipe_frontend_screen *fscreen,
1354 struct st_config_options *options,
1355 int *gl_core_version,
1356 int *gl_compat_version,
1357 int *gl_es1_version,
1358 int *gl_es2_version)
1360 *gl_core_version = get_version(fscreen->screen, options, API_OPENGL_CORE);
1361 *gl_compat_version = get_version(fscreen->screen, options, API_OPENGL_COMPAT);
1362 *gl_es1_version = get_version(fscreen->screen, options, API_OPENGLES);
1363 *gl_es2_version = get_version(fscreen->screen, options, API_OPENGLES2);
1368 st_manager_invalidate_drawables(struct gl_context *ctx)
1370 struct gl_framebuffer *stdraw;
1371 struct gl_framebuffer *stread;
1374 * Normally we'd want the frontend manager to mark the drawables
1375 * invalid only when needed. This will force the frontend manager
1376 * to revalidate the drawable, rather than just update the context with
1377 * the latest cached drawable info.
1380 stdraw = st_ws_framebuffer(ctx->DrawBuffer);
1381 stread = st_ws_framebuffer(ctx->ReadBuffer);
1384 stdraw->drawable_stamp = p_atomic_read(&stdraw->drawable->stamp) - 1;
1385 if (stread && stread != stdraw)
1386 stread->drawable_stamp = p_atomic_read(&stread->drawable->stamp) - 1;