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/glthread.h"
33 #include "main/texobj.h"
34 #include "main/teximage.h"
35 #include "main/texstate.h"
36 #include "main/errors.h"
37 #include "main/framebuffer.h"
38 #include "main/fbobject.h"
39 #include "main/renderbuffer.h"
40 #include "main/version.h"
41 #include "util/hash_table.h"
42 #include "st_texture.h"
44 #include "st_context.h"
46 #include "st_extensions.h"
47 #include "st_format.h"
48 #include "st_cb_fbo.h"
49 #include "st_cb_flush.h"
50 #include "st_manager.h"
52 #include "state_tracker/st_gl_api.h"
54 #include "pipe/p_context.h"
55 #include "pipe/p_screen.h"
56 #include "util/u_format.h"
57 #include "util/u_pointer.h"
58 #include "util/u_inlines.h"
59 #include "util/u_atomic.h"
60 #include "util/u_surface.h"
61 #include "util/list.h"
64 struct st_manager_private
66 struct hash_table
*stfbi_ht
; /* framebuffer iface objects hash table */
70 static void st_manager_destroy(struct st_manager
*);
73 * Map an attachment to a buffer index.
75 static inline gl_buffer_index
76 attachment_to_buffer_index(enum st_attachment_type statt
)
78 gl_buffer_index index
;
81 case ST_ATTACHMENT_FRONT_LEFT
:
82 index
= BUFFER_FRONT_LEFT
;
84 case ST_ATTACHMENT_BACK_LEFT
:
85 index
= BUFFER_BACK_LEFT
;
87 case ST_ATTACHMENT_FRONT_RIGHT
:
88 index
= BUFFER_FRONT_RIGHT
;
90 case ST_ATTACHMENT_BACK_RIGHT
:
91 index
= BUFFER_BACK_RIGHT
;
93 case ST_ATTACHMENT_DEPTH_STENCIL
:
96 case ST_ATTACHMENT_ACCUM
:
99 case ST_ATTACHMENT_SAMPLE
:
101 index
= BUFFER_COUNT
;
109 * Map a buffer index to an attachment.
111 static inline enum st_attachment_type
112 buffer_index_to_attachment(gl_buffer_index index
)
114 enum st_attachment_type statt
;
117 case BUFFER_FRONT_LEFT
:
118 statt
= ST_ATTACHMENT_FRONT_LEFT
;
120 case BUFFER_BACK_LEFT
:
121 statt
= ST_ATTACHMENT_BACK_LEFT
;
123 case BUFFER_FRONT_RIGHT
:
124 statt
= ST_ATTACHMENT_FRONT_RIGHT
;
126 case BUFFER_BACK_RIGHT
:
127 statt
= ST_ATTACHMENT_BACK_RIGHT
;
130 statt
= ST_ATTACHMENT_DEPTH_STENCIL
;
133 statt
= ST_ATTACHMENT_ACCUM
;
136 statt
= ST_ATTACHMENT_INVALID
;
144 * Make sure a context picks up the latest cached state of the
145 * drawables it binds to.
148 st_context_validate(struct st_context
*st
,
149 struct st_framebuffer
*stdraw
,
150 struct st_framebuffer
*stread
)
152 if (stdraw
&& stdraw
->stamp
!= st
->draw_stamp
) {
153 st
->dirty
|= ST_NEW_FRAMEBUFFER
;
154 _mesa_resize_framebuffer(st
->ctx
, &stdraw
->Base
,
156 stdraw
->Base
.Height
);
157 st
->draw_stamp
= stdraw
->stamp
;
160 if (stread
&& stread
->stamp
!= st
->read_stamp
) {
161 if (stread
!= stdraw
) {
162 st
->dirty
|= ST_NEW_FRAMEBUFFER
;
163 _mesa_resize_framebuffer(st
->ctx
, &stread
->Base
,
165 stread
->Base
.Height
);
167 st
->read_stamp
= stread
->stamp
;
172 * Validate a framebuffer to make sure up-to-date pipe_textures are used.
173 * The context is only used for creating pipe surfaces and for calling
174 * _mesa_resize_framebuffer().
175 * (That should probably be rethought, since those surfaces become
176 * drawable state, not context state, and can be freed by another pipe
180 st_framebuffer_validate(struct st_framebuffer
*stfb
,
181 struct st_context
*st
)
183 struct pipe_resource
*textures
[ST_ATTACHMENT_COUNT
];
186 boolean changed
= FALSE
;
189 new_stamp
= p_atomic_read(&stfb
->iface
->stamp
);
190 if (stfb
->iface_stamp
== new_stamp
)
193 memset(textures
, 0, stfb
->num_statts
* sizeof(textures
[0]));
195 /* validate the fb */
197 if (!stfb
->iface
->validate(&st
->iface
, stfb
->iface
, stfb
->statts
,
198 stfb
->num_statts
, textures
))
201 stfb
->iface_stamp
= new_stamp
;
202 new_stamp
= p_atomic_read(&stfb
->iface
->stamp
);
203 } while(stfb
->iface_stamp
!= new_stamp
);
205 width
= stfb
->Base
.Width
;
206 height
= stfb
->Base
.Height
;
208 for (i
= 0; i
< stfb
->num_statts
; i
++) {
209 struct st_renderbuffer
*strb
;
210 struct pipe_surface
*ps
, surf_tmpl
;
216 idx
= attachment_to_buffer_index(stfb
->statts
[i
]);
217 if (idx
>= BUFFER_COUNT
) {
218 pipe_resource_reference(&textures
[i
], NULL
);
222 strb
= st_renderbuffer(stfb
->Base
.Attachment
[idx
].Renderbuffer
);
224 if (strb
->texture
== textures
[i
]) {
225 pipe_resource_reference(&textures
[i
], NULL
);
229 u_surface_default_template(&surf_tmpl
, textures
[i
]);
230 ps
= st
->pipe
->create_surface(st
->pipe
, textures
[i
], &surf_tmpl
);
232 struct pipe_surface
**psurf
=
233 util_format_is_srgb(ps
->format
) ? &strb
->surface_srgb
:
234 &strb
->surface_linear
;
236 pipe_surface_reference(psurf
, ps
);
237 strb
->surface
= *psurf
;
238 pipe_resource_reference(&strb
->texture
, ps
->texture
);
239 /* ownership transfered */
240 pipe_surface_reference(&ps
, NULL
);
244 strb
->Base
.Width
= strb
->surface
->width
;
245 strb
->Base
.Height
= strb
->surface
->height
;
247 width
= strb
->Base
.Width
;
248 height
= strb
->Base
.Height
;
251 pipe_resource_reference(&textures
[i
], NULL
);
256 _mesa_resize_framebuffer(st
->ctx
, &stfb
->Base
, width
, height
);
261 * Update the attachments to validate by looping the existing renderbuffers.
264 st_framebuffer_update_attachments(struct st_framebuffer
*stfb
)
268 stfb
->num_statts
= 0;
269 for (idx
= 0; idx
< BUFFER_COUNT
; idx
++) {
270 struct st_renderbuffer
*strb
;
271 enum st_attachment_type statt
;
273 strb
= st_renderbuffer(stfb
->Base
.Attachment
[idx
].Renderbuffer
);
274 if (!strb
|| strb
->software
)
277 statt
= buffer_index_to_attachment(idx
);
278 if (statt
!= ST_ATTACHMENT_INVALID
&&
279 st_visual_have_buffers(stfb
->iface
->visual
, 1 << statt
))
280 stfb
->statts
[stfb
->num_statts
++] = statt
;
286 * Add a renderbuffer to the framebuffer. The framebuffer is one that
287 * corresponds to a window and is not a user-created FBO.
290 st_framebuffer_add_renderbuffer(struct st_framebuffer
*stfb
,
293 struct gl_renderbuffer
*rb
;
294 enum pipe_format format
;
297 assert(_mesa_is_winsys_fbo(&stfb
->Base
));
299 /* do not distinguish depth/stencil buffers */
300 if (idx
== BUFFER_STENCIL
)
305 format
= stfb
->iface
->visual
->depth_stencil_format
;
309 format
= stfb
->iface
->visual
->accum_format
;
313 format
= stfb
->iface
->visual
->color_format
;
314 if (stfb
->Base
.Visual
.sRGBCapable
)
315 format
= util_format_srgb(format
);
320 if (format
== PIPE_FORMAT_NONE
)
323 rb
= st_new_renderbuffer_fb(format
, stfb
->iface
->visual
->samples
, sw
);
327 if (idx
!= BUFFER_DEPTH
) {
328 _mesa_attach_and_own_rb(&stfb
->Base
, idx
, rb
);
332 bool rb_ownership_taken
= false;
333 if (util_format_get_component_bits(format
, UTIL_FORMAT_COLORSPACE_ZS
, 0)) {
334 _mesa_attach_and_own_rb(&stfb
->Base
, BUFFER_DEPTH
, rb
);
335 rb_ownership_taken
= true;
338 if (util_format_get_component_bits(format
, UTIL_FORMAT_COLORSPACE_ZS
, 1)) {
339 if (rb_ownership_taken
)
340 _mesa_attach_and_reference_rb(&stfb
->Base
, BUFFER_STENCIL
, rb
);
342 _mesa_attach_and_own_rb(&stfb
->Base
, BUFFER_STENCIL
, rb
);
349 * Intialize a struct gl_config from a visual.
352 st_visual_to_context_mode(const struct st_visual
*visual
,
353 struct gl_config
*mode
)
355 memset(mode
, 0, sizeof(*mode
));
357 if (st_visual_have_buffers(visual
, ST_ATTACHMENT_BACK_LEFT_MASK
))
358 mode
->doubleBufferMode
= GL_TRUE
;
359 if (st_visual_have_buffers(visual
,
360 ST_ATTACHMENT_FRONT_RIGHT_MASK
| ST_ATTACHMENT_BACK_RIGHT_MASK
))
361 mode
->stereoMode
= GL_TRUE
;
363 if (visual
->color_format
!= PIPE_FORMAT_NONE
) {
364 mode
->rgbMode
= GL_TRUE
;
367 util_format_get_component_bits(visual
->color_format
,
368 UTIL_FORMAT_COLORSPACE_RGB
, 0);
370 util_format_get_component_bits(visual
->color_format
,
371 UTIL_FORMAT_COLORSPACE_RGB
, 1);
373 util_format_get_component_bits(visual
->color_format
,
374 UTIL_FORMAT_COLORSPACE_RGB
, 2);
376 util_format_get_component_bits(visual
->color_format
,
377 UTIL_FORMAT_COLORSPACE_RGB
, 3);
379 mode
->rgbBits
= mode
->redBits
+
380 mode
->greenBits
+ mode
->blueBits
+ mode
->alphaBits
;
381 mode
->sRGBCapable
= util_format_is_srgb(visual
->color_format
);
384 if (visual
->depth_stencil_format
!= PIPE_FORMAT_NONE
) {
386 util_format_get_component_bits(visual
->depth_stencil_format
,
387 UTIL_FORMAT_COLORSPACE_ZS
, 0);
389 util_format_get_component_bits(visual
->depth_stencil_format
,
390 UTIL_FORMAT_COLORSPACE_ZS
, 1);
392 mode
->haveDepthBuffer
= mode
->depthBits
> 0;
393 mode
->haveStencilBuffer
= mode
->stencilBits
> 0;
396 if (visual
->accum_format
!= PIPE_FORMAT_NONE
) {
397 mode
->haveAccumBuffer
= GL_TRUE
;
400 util_format_get_component_bits(visual
->accum_format
,
401 UTIL_FORMAT_COLORSPACE_RGB
, 0);
402 mode
->accumGreenBits
=
403 util_format_get_component_bits(visual
->accum_format
,
404 UTIL_FORMAT_COLORSPACE_RGB
, 1);
405 mode
->accumBlueBits
=
406 util_format_get_component_bits(visual
->accum_format
,
407 UTIL_FORMAT_COLORSPACE_RGB
, 2);
408 mode
->accumAlphaBits
=
409 util_format_get_component_bits(visual
->accum_format
,
410 UTIL_FORMAT_COLORSPACE_RGB
, 3);
413 if (visual
->samples
> 1) {
414 mode
->sampleBuffers
= 1;
415 mode
->samples
= visual
->samples
;
420 * Create a framebuffer from a manager interface.
422 static struct st_framebuffer
*
423 st_framebuffer_create(struct st_context
*st
,
424 struct st_framebuffer_iface
*stfbi
)
426 struct st_framebuffer
*stfb
;
427 struct gl_config mode
;
433 stfb
= CALLOC_STRUCT(st_framebuffer
);
437 st_visual_to_context_mode(stfbi
->visual
, &mode
);
440 * For desktop GL, sRGB framebuffer write is controlled by both the
441 * capability of the framebuffer and GL_FRAMEBUFFER_SRGB. We should
442 * advertise the capability when the pipe driver (and core Mesa) supports
443 * it so that applications can enable sRGB write when they want to.
445 * This is not to be confused with GLX_FRAMEBUFFER_SRGB_CAPABLE_ARB. When
446 * the attribute is GLX_TRUE, it tells the st manager to pick a color
447 * format such that util_format_srgb(visual->color_format) can be supported
448 * by the pipe driver. We still need to advertise the capability here.
450 * For GLES, however, sRGB framebuffer write is controlled only by the
451 * capability of the framebuffer. There is GL_EXT_sRGB_write_control to
452 * give applications the control back, but sRGB write is still enabled by
453 * default. To avoid unexpected results, we should not advertise the
454 * capability. This could change when we add support for
455 * EGL_KHR_gl_colorspace.
457 if (_mesa_is_desktop_gl(st
->ctx
)) {
458 struct pipe_screen
*screen
= st
->pipe
->screen
;
459 const enum pipe_format srgb_format
=
460 util_format_srgb(stfbi
->visual
->color_format
);
462 if (srgb_format
!= PIPE_FORMAT_NONE
&&
463 st_pipe_format_to_mesa_format(srgb_format
) != MESA_FORMAT_NONE
&&
464 screen
->is_format_supported(screen
, srgb_format
,
465 PIPE_TEXTURE_2D
, stfbi
->visual
->samples
,
466 (PIPE_BIND_DISPLAY_TARGET
|
467 PIPE_BIND_RENDER_TARGET
)))
468 mode
.sRGBCapable
= GL_TRUE
;
471 _mesa_initialize_window_framebuffer(&stfb
->Base
, &mode
);
474 stfb
->iface_ID
= stfbi
->ID
;
475 stfb
->iface_stamp
= p_atomic_read(&stfbi
->stamp
) - 1;
477 /* add the color buffer */
478 idx
= stfb
->Base
._ColorDrawBufferIndexes
[0];
479 if (!st_framebuffer_add_renderbuffer(stfb
, idx
)) {
484 st_framebuffer_add_renderbuffer(stfb
, BUFFER_DEPTH
);
485 st_framebuffer_add_renderbuffer(stfb
, BUFFER_ACCUM
);
488 st_framebuffer_update_attachments(stfb
);
494 * Reference a framebuffer.
497 st_framebuffer_reference(struct st_framebuffer
**ptr
,
498 struct st_framebuffer
*stfb
)
500 struct gl_framebuffer
*fb
= &stfb
->Base
;
501 _mesa_reference_framebuffer((struct gl_framebuffer
**) ptr
, fb
);
506 st_framebuffer_iface_hash(const void *key
)
508 return (uintptr_t)key
;
513 st_framebuffer_iface_equal(const void *a
, const void *b
)
515 return (struct st_framebuffer_iface
*)a
== (struct st_framebuffer_iface
*)b
;
520 st_framebuffer_iface_lookup(struct st_manager
*smapi
,
521 const struct st_framebuffer_iface
*stfbi
)
523 struct st_manager_private
*smPriv
=
524 (struct st_manager_private
*)smapi
->st_manager_private
;
525 struct hash_entry
*entry
;
528 assert(smPriv
->stfbi_ht
);
530 mtx_lock(&smPriv
->st_mutex
);
531 entry
= _mesa_hash_table_search(smPriv
->stfbi_ht
, stfbi
);
532 mtx_unlock(&smPriv
->st_mutex
);
534 return entry
!= NULL
;
539 st_framebuffer_iface_insert(struct st_manager
*smapi
,
540 struct st_framebuffer_iface
*stfbi
)
542 struct st_manager_private
*smPriv
=
543 (struct st_manager_private
*)smapi
->st_manager_private
;
544 struct hash_entry
*entry
;
547 assert(smPriv
->stfbi_ht
);
549 mtx_lock(&smPriv
->st_mutex
);
550 entry
= _mesa_hash_table_insert(smPriv
->stfbi_ht
, stfbi
, stfbi
);
551 mtx_unlock(&smPriv
->st_mutex
);
553 return entry
!= NULL
;
558 st_framebuffer_iface_remove(struct st_manager
*smapi
,
559 struct st_framebuffer_iface
*stfbi
)
561 struct st_manager_private
*smPriv
=
562 (struct st_manager_private
*)smapi
->st_manager_private
;
563 struct hash_entry
*entry
;
565 if (!smPriv
|| !smPriv
->stfbi_ht
)
568 mtx_lock(&smPriv
->st_mutex
);
569 entry
= _mesa_hash_table_search(smPriv
->stfbi_ht
, stfbi
);
573 _mesa_hash_table_remove(smPriv
->stfbi_ht
, entry
);
576 mtx_unlock(&smPriv
->st_mutex
);
581 * The framebuffer interface object is no longer valid.
582 * Remove the object from the framebuffer interface hash table.
585 st_api_destroy_drawable(struct st_api
*stapi
,
586 struct st_framebuffer_iface
*stfbi
)
591 st_framebuffer_iface_remove(stfbi
->state_manager
, stfbi
);
596 * Purge the winsys buffers list to remove any references to
597 * non-existing framebuffer interface objects.
600 st_framebuffers_purge(struct st_context
*st
)
602 struct st_context_iface
*st_iface
= &st
->iface
;
603 struct st_manager
*smapi
= st_iface
->state_manager
;
604 struct st_framebuffer
*stfb
, *next
;
608 LIST_FOR_EACH_ENTRY_SAFE_REV(stfb
, next
, &st
->winsys_buffers
, head
) {
609 struct st_framebuffer_iface
*stfbi
= stfb
->iface
;
614 * If the corresponding framebuffer interface object no longer exists,
615 * remove the framebuffer object from the context's winsys buffers list,
616 * and unreference the framebuffer object, so its resources can be
619 if (!st_framebuffer_iface_lookup(smapi
, stfbi
)) {
620 LIST_DEL(&stfb
->head
);
621 st_framebuffer_reference(&stfb
, NULL
);
627 st_context_flush(struct st_context_iface
*stctxi
, unsigned flags
,
628 struct pipe_fence_handle
**fence
)
630 struct st_context
*st
= (struct st_context
*) stctxi
;
631 unsigned pipe_flags
= 0;
633 if (flags
& ST_FLUSH_END_OF_FRAME
) {
634 pipe_flags
|= PIPE_FLUSH_END_OF_FRAME
;
637 st_flush(st
, fence
, pipe_flags
);
639 if ((flags
& ST_FLUSH_WAIT
) && fence
&& *fence
) {
640 st
->pipe
->screen
->fence_finish(st
->pipe
->screen
, NULL
, *fence
,
641 PIPE_TIMEOUT_INFINITE
);
642 st
->pipe
->screen
->fence_reference(st
->pipe
->screen
, fence
, NULL
);
645 if (flags
& ST_FLUSH_FRONT
)
646 st_manager_flush_frontbuffer(st
);
648 /* DRI3 changes the framebuffer after SwapBuffers, but we need to invoke
649 * st_manager_validate_framebuffers to notice that.
651 * Set gfx_shaders_may_be_dirty to invoke st_validate_state in the next
652 * draw call, which will invoke st_manager_validate_framebuffers, but it
653 * won't dirty states if there is no change.
655 if (flags
& ST_FLUSH_END_OF_FRAME
)
656 st
->gfx_shaders_may_be_dirty
= true;
660 st_context_teximage(struct st_context_iface
*stctxi
,
661 enum st_texture_type tex_type
,
662 int level
, enum pipe_format pipe_format
,
663 struct pipe_resource
*tex
, boolean mipmap
)
665 struct st_context
*st
= (struct st_context
*) stctxi
;
666 struct gl_context
*ctx
= st
->ctx
;
667 struct gl_texture_object
*texObj
;
668 struct gl_texture_image
*texImage
;
669 struct st_texture_object
*stObj
;
670 struct st_texture_image
*stImage
;
671 GLenum internalFormat
;
672 GLuint width
, height
, depth
;
677 target
= GL_TEXTURE_1D
;
680 target
= GL_TEXTURE_2D
;
683 target
= GL_TEXTURE_3D
;
685 case ST_TEXTURE_RECT
:
686 target
= GL_TEXTURE_RECTANGLE_ARB
;
692 texObj
= _mesa_get_current_tex_object(ctx
, target
);
694 _mesa_lock_texture(ctx
, texObj
);
696 stObj
= st_texture_object(texObj
);
697 /* switch to surface based */
698 if (!stObj
->surface_based
) {
699 _mesa_clear_texture_object(ctx
, texObj
, NULL
);
700 stObj
->surface_based
= GL_TRUE
;
703 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, level
);
704 stImage
= st_texture_image(texImage
);
706 mesa_format texFormat
= st_pipe_format_to_mesa_format(pipe_format
);
708 if (util_format_has_alpha(tex
->format
))
709 internalFormat
= GL_RGBA
;
711 internalFormat
= GL_RGB
;
713 _mesa_init_teximage_fields(ctx
, texImage
,
714 tex
->width0
, tex
->height0
, 1, 0,
715 internalFormat
, texFormat
);
718 height
= tex
->height0
;
721 /* grow the image size until we hit level = 0 */
733 _mesa_clear_texture_image(ctx
, texImage
);
734 width
= height
= depth
= 0;
737 pipe_resource_reference(&stImage
->pt
, tex
);
738 stObj
->surface_format
= pipe_format
;
740 stObj
->needs_validation
= true;
742 _mesa_dirty_texobj(ctx
, texObj
);
743 _mesa_unlock_texture(ctx
, texObj
);
749 st_context_copy(struct st_context_iface
*stctxi
,
750 struct st_context_iface
*stsrci
, unsigned mask
)
752 struct st_context
*st
= (struct st_context
*) stctxi
;
753 struct st_context
*src
= (struct st_context
*) stsrci
;
755 _mesa_copy_context(src
->ctx
, st
->ctx
, mask
);
759 st_context_share(struct st_context_iface
*stctxi
,
760 struct st_context_iface
*stsrci
)
762 struct st_context
*st
= (struct st_context
*) stctxi
;
763 struct st_context
*src
= (struct st_context
*) stsrci
;
765 return _mesa_share_state(st
->ctx
, src
->ctx
);
769 st_context_destroy(struct st_context_iface
*stctxi
)
771 struct st_context
*st
= (struct st_context
*) stctxi
;
772 st_destroy_context(st
);
776 st_start_thread(struct st_context_iface
*stctxi
)
778 struct st_context
*st
= (struct st_context
*) stctxi
;
780 _mesa_glthread_init(st
->ctx
);
784 st_thread_finish(struct st_context_iface
*stctxi
)
786 struct st_context
*st
= (struct st_context
*) stctxi
;
788 _mesa_glthread_finish(st
->ctx
);
791 static struct st_context_iface
*
792 st_api_create_context(struct st_api
*stapi
, struct st_manager
*smapi
,
793 const struct st_context_attribs
*attribs
,
794 enum st_context_error
*error
,
795 struct st_context_iface
*shared_stctxi
)
797 struct st_context
*shared_ctx
= (struct st_context
*) shared_stctxi
;
798 struct st_context
*st
;
799 struct pipe_context
*pipe
;
800 struct gl_config mode
;
802 bool no_error
= false;
803 unsigned ctx_flags
= PIPE_CONTEXT_PREFER_THREADED
;
805 if (!(stapi
->profile_mask
& (1 << attribs
->profile
)))
808 switch (attribs
->profile
) {
809 case ST_PROFILE_DEFAULT
:
810 api
= API_OPENGL_COMPAT
;
812 case ST_PROFILE_OPENGL_ES1
:
815 case ST_PROFILE_OPENGL_ES2
:
818 case ST_PROFILE_OPENGL_CORE
:
819 api
= API_OPENGL_CORE
;
822 *error
= ST_CONTEXT_ERROR_BAD_API
;
826 /* Create a hash table for the framebuffer interface objects
827 * if it has not been created for this st manager.
829 if (smapi
->st_manager_private
== NULL
) {
830 struct st_manager_private
*smPriv
;
832 smPriv
= CALLOC_STRUCT(st_manager_private
);
833 mtx_init(&smPriv
->st_mutex
, mtx_plain
);
834 smPriv
->stfbi_ht
= _mesa_hash_table_create(NULL
,
835 st_framebuffer_iface_hash
,
836 st_framebuffer_iface_equal
);
837 smapi
->st_manager_private
= smPriv
;
838 smapi
->destroy
= st_manager_destroy
;
841 if (attribs
->flags
& ST_CONTEXT_FLAG_ROBUST_ACCESS
)
842 ctx_flags
|= PIPE_CONTEXT_ROBUST_BUFFER_ACCESS
;
844 if (attribs
->flags
& ST_CONTEXT_FLAG_NO_ERROR
)
847 pipe
= smapi
->screen
->context_create(smapi
->screen
, NULL
, ctx_flags
);
849 *error
= ST_CONTEXT_ERROR_NO_MEMORY
;
853 st_visual_to_context_mode(&attribs
->visual
, &mode
);
854 st
= st_create_context(api
, pipe
, &mode
, shared_ctx
, &attribs
->options
, no_error
);
856 *error
= ST_CONTEXT_ERROR_NO_MEMORY
;
861 if (attribs
->flags
& ST_CONTEXT_FLAG_DEBUG
) {
862 if (!_mesa_set_debug_state_int(st
->ctx
, GL_DEBUG_OUTPUT
, GL_TRUE
)) {
863 *error
= ST_CONTEXT_ERROR_NO_MEMORY
;
867 st
->ctx
->Const
.ContextFlags
|= GL_CONTEXT_FLAG_DEBUG_BIT
;
870 if (st
->ctx
->Const
.ContextFlags
& GL_CONTEXT_FLAG_DEBUG_BIT
) {
871 st_update_debug_callback(st
);
874 if (attribs
->flags
& ST_CONTEXT_FLAG_FORWARD_COMPATIBLE
)
875 st
->ctx
->Const
.ContextFlags
|= GL_CONTEXT_FLAG_FORWARD_COMPATIBLE_BIT
;
876 if (attribs
->flags
& ST_CONTEXT_FLAG_ROBUST_ACCESS
) {
877 st
->ctx
->Const
.ContextFlags
|= GL_CONTEXT_FLAG_ROBUST_ACCESS_BIT_ARB
;
878 st
->ctx
->Const
.RobustAccess
= GL_TRUE
;
880 if (attribs
->flags
& ST_CONTEXT_FLAG_RESET_NOTIFICATION_ENABLED
) {
881 st
->ctx
->Const
.ResetStrategy
= GL_LOSE_CONTEXT_ON_RESET_ARB
;
882 st_install_device_reset_callback(st
);
885 /* need to perform version check */
886 if (attribs
->major
> 1 || attribs
->minor
> 0) {
887 /* Is the actual version less than the requested version?
889 if (st
->ctx
->Version
< attribs
->major
* 10U + attribs
->minor
) {
890 *error
= ST_CONTEXT_ERROR_BAD_VERSION
;
891 st_destroy_context(st
);
896 st
->invalidate_on_gl_viewport
=
897 smapi
->get_param(smapi
, ST_MANAGER_BROKEN_INVALIDATE
);
899 st
->iface
.destroy
= st_context_destroy
;
900 st
->iface
.flush
= st_context_flush
;
901 st
->iface
.teximage
= st_context_teximage
;
902 st
->iface
.copy
= st_context_copy
;
903 st
->iface
.share
= st_context_share
;
904 st
->iface
.start_thread
= st_start_thread
;
905 st
->iface
.thread_finish
= st_thread_finish
;
906 st
->iface
.st_context_private
= (void *) smapi
;
907 st
->iface
.cso_context
= st
->cso_context
;
908 st
->iface
.pipe
= st
->pipe
;
909 st
->iface
.state_manager
= smapi
;
911 *error
= ST_CONTEXT_SUCCESS
;
915 static struct st_context_iface
*
916 st_api_get_current(struct st_api
*stapi
)
918 GET_CURRENT_CONTEXT(ctx
);
919 struct st_context
*st
= (ctx
) ? ctx
->st
: NULL
;
921 return (st
) ? &st
->iface
: NULL
;
924 static struct st_framebuffer
*
925 st_framebuffer_reuse_or_create(struct st_context
*st
,
926 struct gl_framebuffer
*fb
,
927 struct st_framebuffer_iface
*stfbi
)
929 struct st_framebuffer
*cur
= NULL
, *stfb
= NULL
;
934 /* Check if there is already a framebuffer object for the specified
935 * framebuffer interface in this context. If there is one, use it.
937 LIST_FOR_EACH_ENTRY(cur
, &st
->winsys_buffers
, head
) {
938 if (cur
->iface_ID
== stfbi
->ID
) {
939 st_framebuffer_reference(&stfb
, cur
);
944 /* If there is not already a framebuffer object, create one */
946 cur
= st_framebuffer_create(st
, stfbi
);
949 /* add the referenced framebuffer interface object to
950 * the framebuffer interface object hash table.
952 if (!st_framebuffer_iface_insert(stfbi
->state_manager
, stfbi
)) {
953 st_framebuffer_reference(&cur
, NULL
);
957 /* add to the context's winsys buffers list */
958 LIST_ADD(&cur
->head
, &st
->winsys_buffers
);
960 st_framebuffer_reference(&stfb
, cur
);
968 st_api_make_current(struct st_api
*stapi
, struct st_context_iface
*stctxi
,
969 struct st_framebuffer_iface
*stdrawi
,
970 struct st_framebuffer_iface
*streadi
)
972 struct st_context
*st
= (struct st_context
*) stctxi
;
973 struct st_framebuffer
*stdraw
, *stread
;
976 _glapi_check_multithread();
979 /* reuse or create the draw fb */
980 stdraw
= st_framebuffer_reuse_or_create(st
,
981 st
->ctx
->WinSysDrawBuffer
, stdrawi
);
982 if (streadi
!= stdrawi
) {
983 /* do the same for the read fb */
984 stread
= st_framebuffer_reuse_or_create(st
,
985 st
->ctx
->WinSysReadBuffer
, streadi
);
989 /* reuse the draw fb for the read fb */
991 st_framebuffer_reference(&stread
, stdraw
);
994 if (stdraw
&& stread
) {
995 st_framebuffer_validate(stdraw
, st
);
996 if (stread
!= stdraw
)
997 st_framebuffer_validate(stread
, st
);
999 ret
= _mesa_make_current(st
->ctx
, &stdraw
->Base
, &stread
->Base
);
1001 st
->draw_stamp
= stdraw
->stamp
- 1;
1002 st
->read_stamp
= stread
->stamp
- 1;
1003 st_context_validate(st
, stdraw
, stread
);
1006 struct gl_framebuffer
*incomplete
= _mesa_get_incomplete_framebuffer();
1007 ret
= _mesa_make_current(st
->ctx
, incomplete
, incomplete
);
1010 st_framebuffer_reference(&stdraw
, NULL
);
1011 st_framebuffer_reference(&stread
, NULL
);
1013 /* Purge the context's winsys_buffers list in case any
1014 * of the referenced drawables no longer exist.
1016 st_framebuffers_purge(st
);
1019 ret
= _mesa_make_current(NULL
, NULL
, NULL
);
1026 st_api_destroy(struct st_api
*stapi
)
1031 * Flush the front buffer if the current context renders to the front buffer.
1034 st_manager_flush_frontbuffer(struct st_context
*st
)
1036 struct st_framebuffer
*stfb
= st_ws_framebuffer(st
->ctx
->DrawBuffer
);
1037 struct st_renderbuffer
*strb
= NULL
;
1040 strb
= st_renderbuffer(stfb
->Base
.Attachment
[BUFFER_FRONT_LEFT
].Renderbuffer
);
1042 /* Do we have a front color buffer and has it been drawn to since last
1043 * frontbuffer flush?
1045 if (strb
&& strb
->defined
) {
1046 stfb
->iface
->flush_front(&st
->iface
, stfb
->iface
,
1047 ST_ATTACHMENT_FRONT_LEFT
);
1048 strb
->defined
= GL_FALSE
;
1050 /* Trigger an update of strb->defined on next draw */
1051 st
->dirty
|= ST_NEW_FB_STATE
;
1056 * Re-validate the framebuffers.
1059 st_manager_validate_framebuffers(struct st_context
*st
)
1061 struct st_framebuffer
*stdraw
= st_ws_framebuffer(st
->ctx
->DrawBuffer
);
1062 struct st_framebuffer
*stread
= st_ws_framebuffer(st
->ctx
->ReadBuffer
);
1065 st_framebuffer_validate(stdraw
, st
);
1066 if (stread
&& stread
!= stdraw
)
1067 st_framebuffer_validate(stread
, st
);
1069 st_context_validate(st
, stdraw
, stread
);
1074 * Flush any outstanding swapbuffers on the current draw framebuffer.
1077 st_manager_flush_swapbuffers(void)
1079 GET_CURRENT_CONTEXT(ctx
);
1080 struct st_context
*st
= (ctx
) ? ctx
->st
: NULL
;
1081 struct st_framebuffer
*stfb
;
1086 stfb
= st_ws_framebuffer(ctx
->DrawBuffer
);
1087 if (!stfb
|| !stfb
->iface
->flush_swapbuffers
)
1090 stfb
->iface
->flush_swapbuffers(&st
->iface
, stfb
->iface
);
1095 * Add a color renderbuffer on demand. The FBO must correspond to a window,
1096 * not a user-created FBO.
1099 st_manager_add_color_renderbuffer(struct st_context
*st
,
1100 struct gl_framebuffer
*fb
,
1101 gl_buffer_index idx
)
1103 struct st_framebuffer
*stfb
= st_ws_framebuffer(fb
);
1109 assert(_mesa_is_winsys_fbo(fb
));
1111 if (stfb
->Base
.Attachment
[idx
].Renderbuffer
)
1115 case BUFFER_FRONT_LEFT
:
1116 case BUFFER_BACK_LEFT
:
1117 case BUFFER_FRONT_RIGHT
:
1118 case BUFFER_BACK_RIGHT
:
1124 if (!st_framebuffer_add_renderbuffer(stfb
, idx
))
1127 st_framebuffer_update_attachments(stfb
);
1130 * Force a call to the state tracker manager to validate the
1131 * new renderbuffer. It might be that there is a window system
1132 * renderbuffer available.
1135 stfb
->iface_stamp
= p_atomic_read(&stfb
->iface
->stamp
) - 1;
1137 st_invalidate_buffers(st
);
1143 st_manager_destroy(struct st_manager
*smapi
)
1145 struct st_manager_private
*smPriv
= smapi
->st_manager_private
;
1147 if (smPriv
&& smPriv
->stfbi_ht
) {
1148 _mesa_hash_table_destroy(smPriv
->stfbi_ht
, NULL
);
1149 mtx_destroy(&smPriv
->st_mutex
);
1151 smapi
->st_manager_private
= NULL
;
1156 get_version(struct pipe_screen
*screen
,
1157 struct st_config_options
*options
, gl_api api
)
1159 struct gl_constants consts
= {0};
1160 struct gl_extensions extensions
= {0};
1163 if (_mesa_override_gl_version_contextless(&consts
, &api
, &version
)) {
1167 _mesa_init_constants(&consts
, api
);
1168 _mesa_init_extensions(&extensions
);
1170 st_init_limits(screen
, &consts
, &extensions
);
1171 st_init_extensions(screen
, &consts
, &extensions
, options
);
1173 return _mesa_get_version(&extensions
, &consts
, api
);
1177 st_api_query_versions(struct st_api
*stapi
, struct st_manager
*sm
,
1178 struct st_config_options
*options
,
1179 int *gl_core_version
,
1180 int *gl_compat_version
,
1181 int *gl_es1_version
,
1182 int *gl_es2_version
)
1184 *gl_core_version
= get_version(sm
->screen
, options
, API_OPENGL_CORE
);
1185 *gl_compat_version
= get_version(sm
->screen
, options
, API_OPENGL_COMPAT
);
1186 *gl_es1_version
= get_version(sm
->screen
, options
, API_OPENGLES
);
1187 *gl_es2_version
= get_version(sm
->screen
, options
, API_OPENGLES2
);
1190 static const struct st_api st_gl_api
= {
1191 .name
= "Mesa " PACKAGE_VERSION
,
1192 .api
= ST_API_OPENGL
,
1193 .profile_mask
= ST_PROFILE_DEFAULT_MASK
|
1194 ST_PROFILE_OPENGL_CORE_MASK
|
1195 ST_PROFILE_OPENGL_ES1_MASK
|
1196 ST_PROFILE_OPENGL_ES2_MASK
|
1198 .feature_mask
= ST_API_FEATURE_MS_VISUALS_MASK
,
1199 .destroy
= st_api_destroy
,
1200 .query_versions
= st_api_query_versions
,
1201 .create_context
= st_api_create_context
,
1202 .make_current
= st_api_make_current
,
1203 .get_current
= st_api_get_current
,
1204 .destroy_drawable
= st_api_destroy_drawable
,
1208 st_gl_api_create(void)
1210 return (struct st_api
*) &st_gl_api
;