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 /* validate the fb */
195 if (!stfb
->iface
->validate(&st
->iface
, stfb
->iface
, stfb
->statts
,
196 stfb
->num_statts
, textures
))
199 stfb
->iface_stamp
= new_stamp
;
200 new_stamp
= p_atomic_read(&stfb
->iface
->stamp
);
201 } while(stfb
->iface_stamp
!= new_stamp
);
203 width
= stfb
->Base
.Width
;
204 height
= stfb
->Base
.Height
;
206 for (i
= 0; i
< stfb
->num_statts
; i
++) {
207 struct st_renderbuffer
*strb
;
208 struct pipe_surface
*ps
, surf_tmpl
;
214 idx
= attachment_to_buffer_index(stfb
->statts
[i
]);
215 if (idx
>= BUFFER_COUNT
) {
216 pipe_resource_reference(&textures
[i
], NULL
);
220 strb
= st_renderbuffer(stfb
->Base
.Attachment
[idx
].Renderbuffer
);
222 if (strb
->texture
== textures
[i
]) {
223 pipe_resource_reference(&textures
[i
], NULL
);
227 u_surface_default_template(&surf_tmpl
, textures
[i
]);
228 ps
= st
->pipe
->create_surface(st
->pipe
, textures
[i
], &surf_tmpl
);
230 struct pipe_surface
**psurf
=
231 util_format_is_srgb(ps
->format
) ? &strb
->surface_srgb
:
232 &strb
->surface_linear
;
234 pipe_surface_reference(psurf
, ps
);
235 strb
->surface
= *psurf
;
236 pipe_resource_reference(&strb
->texture
, ps
->texture
);
237 /* ownership transfered */
238 pipe_surface_reference(&ps
, NULL
);
242 strb
->Base
.Width
= strb
->surface
->width
;
243 strb
->Base
.Height
= strb
->surface
->height
;
245 width
= strb
->Base
.Width
;
246 height
= strb
->Base
.Height
;
249 pipe_resource_reference(&textures
[i
], NULL
);
254 _mesa_resize_framebuffer(st
->ctx
, &stfb
->Base
, width
, height
);
259 * Update the attachments to validate by looping the existing renderbuffers.
262 st_framebuffer_update_attachments(struct st_framebuffer
*stfb
)
266 stfb
->num_statts
= 0;
267 for (idx
= 0; idx
< BUFFER_COUNT
; idx
++) {
268 struct st_renderbuffer
*strb
;
269 enum st_attachment_type statt
;
271 strb
= st_renderbuffer(stfb
->Base
.Attachment
[idx
].Renderbuffer
);
272 if (!strb
|| strb
->software
)
275 statt
= buffer_index_to_attachment(idx
);
276 if (statt
!= ST_ATTACHMENT_INVALID
&&
277 st_visual_have_buffers(stfb
->iface
->visual
, 1 << statt
))
278 stfb
->statts
[stfb
->num_statts
++] = statt
;
284 * Add a renderbuffer to the framebuffer. The framebuffer is one that
285 * corresponds to a window and is not a user-created FBO.
288 st_framebuffer_add_renderbuffer(struct st_framebuffer
*stfb
,
291 struct gl_renderbuffer
*rb
;
292 enum pipe_format format
;
295 assert(_mesa_is_winsys_fbo(&stfb
->Base
));
297 /* do not distinguish depth/stencil buffers */
298 if (idx
== BUFFER_STENCIL
)
303 format
= stfb
->iface
->visual
->depth_stencil_format
;
307 format
= stfb
->iface
->visual
->accum_format
;
311 format
= stfb
->iface
->visual
->color_format
;
312 if (stfb
->Base
.Visual
.sRGBCapable
)
313 format
= util_format_srgb(format
);
318 if (format
== PIPE_FORMAT_NONE
)
321 rb
= st_new_renderbuffer_fb(format
, stfb
->iface
->visual
->samples
, sw
);
325 if (idx
!= BUFFER_DEPTH
) {
326 _mesa_attach_and_own_rb(&stfb
->Base
, idx
, rb
);
330 bool rb_ownership_taken
= false;
331 if (util_format_get_component_bits(format
, UTIL_FORMAT_COLORSPACE_ZS
, 0)) {
332 _mesa_attach_and_own_rb(&stfb
->Base
, BUFFER_DEPTH
, rb
);
333 rb_ownership_taken
= true;
336 if (util_format_get_component_bits(format
, UTIL_FORMAT_COLORSPACE_ZS
, 1)) {
337 if (rb_ownership_taken
)
338 _mesa_attach_and_reference_rb(&stfb
->Base
, BUFFER_STENCIL
, rb
);
340 _mesa_attach_and_own_rb(&stfb
->Base
, BUFFER_STENCIL
, rb
);
347 * Intialize a struct gl_config from a visual.
350 st_visual_to_context_mode(const struct st_visual
*visual
,
351 struct gl_config
*mode
)
353 memset(mode
, 0, sizeof(*mode
));
355 if (st_visual_have_buffers(visual
, ST_ATTACHMENT_BACK_LEFT_MASK
))
356 mode
->doubleBufferMode
= GL_TRUE
;
357 if (st_visual_have_buffers(visual
,
358 ST_ATTACHMENT_FRONT_RIGHT_MASK
| ST_ATTACHMENT_BACK_RIGHT_MASK
))
359 mode
->stereoMode
= GL_TRUE
;
361 if (visual
->color_format
!= PIPE_FORMAT_NONE
) {
362 mode
->rgbMode
= GL_TRUE
;
365 util_format_get_component_bits(visual
->color_format
,
366 UTIL_FORMAT_COLORSPACE_RGB
, 0);
368 util_format_get_component_bits(visual
->color_format
,
369 UTIL_FORMAT_COLORSPACE_RGB
, 1);
371 util_format_get_component_bits(visual
->color_format
,
372 UTIL_FORMAT_COLORSPACE_RGB
, 2);
374 util_format_get_component_bits(visual
->color_format
,
375 UTIL_FORMAT_COLORSPACE_RGB
, 3);
377 mode
->rgbBits
= mode
->redBits
+
378 mode
->greenBits
+ mode
->blueBits
+ mode
->alphaBits
;
379 mode
->sRGBCapable
= util_format_is_srgb(visual
->color_format
);
382 if (visual
->depth_stencil_format
!= PIPE_FORMAT_NONE
) {
384 util_format_get_component_bits(visual
->depth_stencil_format
,
385 UTIL_FORMAT_COLORSPACE_ZS
, 0);
387 util_format_get_component_bits(visual
->depth_stencil_format
,
388 UTIL_FORMAT_COLORSPACE_ZS
, 1);
390 mode
->haveDepthBuffer
= mode
->depthBits
> 0;
391 mode
->haveStencilBuffer
= mode
->stencilBits
> 0;
394 if (visual
->accum_format
!= PIPE_FORMAT_NONE
) {
395 mode
->haveAccumBuffer
= GL_TRUE
;
398 util_format_get_component_bits(visual
->accum_format
,
399 UTIL_FORMAT_COLORSPACE_RGB
, 0);
400 mode
->accumGreenBits
=
401 util_format_get_component_bits(visual
->accum_format
,
402 UTIL_FORMAT_COLORSPACE_RGB
, 1);
403 mode
->accumBlueBits
=
404 util_format_get_component_bits(visual
->accum_format
,
405 UTIL_FORMAT_COLORSPACE_RGB
, 2);
406 mode
->accumAlphaBits
=
407 util_format_get_component_bits(visual
->accum_format
,
408 UTIL_FORMAT_COLORSPACE_RGB
, 3);
411 if (visual
->samples
> 1) {
412 mode
->sampleBuffers
= 1;
413 mode
->samples
= visual
->samples
;
418 * Create a framebuffer from a manager interface.
420 static struct st_framebuffer
*
421 st_framebuffer_create(struct st_context
*st
,
422 struct st_framebuffer_iface
*stfbi
)
424 struct st_framebuffer
*stfb
;
425 struct gl_config mode
;
431 stfb
= CALLOC_STRUCT(st_framebuffer
);
435 st_visual_to_context_mode(stfbi
->visual
, &mode
);
438 * For desktop GL, sRGB framebuffer write is controlled by both the
439 * capability of the framebuffer and GL_FRAMEBUFFER_SRGB. We should
440 * advertise the capability when the pipe driver (and core Mesa) supports
441 * it so that applications can enable sRGB write when they want to.
443 * This is not to be confused with GLX_FRAMEBUFFER_SRGB_CAPABLE_ARB. When
444 * the attribute is GLX_TRUE, it tells the st manager to pick a color
445 * format such that util_format_srgb(visual->color_format) can be supported
446 * by the pipe driver. We still need to advertise the capability here.
448 * For GLES, however, sRGB framebuffer write is controlled only by the
449 * capability of the framebuffer. There is GL_EXT_sRGB_write_control to
450 * give applications the control back, but sRGB write is still enabled by
451 * default. To avoid unexpected results, we should not advertise the
452 * capability. This could change when we add support for
453 * EGL_KHR_gl_colorspace.
455 if (_mesa_is_desktop_gl(st
->ctx
)) {
456 struct pipe_screen
*screen
= st
->pipe
->screen
;
457 const enum pipe_format srgb_format
=
458 util_format_srgb(stfbi
->visual
->color_format
);
460 if (srgb_format
!= PIPE_FORMAT_NONE
&&
461 st_pipe_format_to_mesa_format(srgb_format
) != MESA_FORMAT_NONE
&&
462 screen
->is_format_supported(screen
, srgb_format
,
463 PIPE_TEXTURE_2D
, stfbi
->visual
->samples
,
464 (PIPE_BIND_DISPLAY_TARGET
|
465 PIPE_BIND_RENDER_TARGET
)))
466 mode
.sRGBCapable
= GL_TRUE
;
469 _mesa_initialize_window_framebuffer(&stfb
->Base
, &mode
);
472 stfb
->iface_ID
= stfbi
->ID
;
473 stfb
->iface_stamp
= p_atomic_read(&stfbi
->stamp
) - 1;
475 /* add the color buffer */
476 idx
= stfb
->Base
._ColorDrawBufferIndexes
[0];
477 if (!st_framebuffer_add_renderbuffer(stfb
, idx
)) {
482 st_framebuffer_add_renderbuffer(stfb
, BUFFER_DEPTH
);
483 st_framebuffer_add_renderbuffer(stfb
, BUFFER_ACCUM
);
486 st_framebuffer_update_attachments(stfb
);
492 * Reference a framebuffer.
495 st_framebuffer_reference(struct st_framebuffer
**ptr
,
496 struct st_framebuffer
*stfb
)
498 struct gl_framebuffer
*fb
= &stfb
->Base
;
499 _mesa_reference_framebuffer((struct gl_framebuffer
**) ptr
, fb
);
504 st_framebuffer_iface_hash(const void *key
)
506 return (uintptr_t)key
;
511 st_framebuffer_iface_equal(const void *a
, const void *b
)
513 return (struct st_framebuffer_iface
*)a
== (struct st_framebuffer_iface
*)b
;
518 st_framebuffer_iface_lookup(struct st_manager
*smapi
,
519 const struct st_framebuffer_iface
*stfbi
)
521 struct st_manager_private
*smPriv
=
522 (struct st_manager_private
*)smapi
->st_manager_private
;
523 struct hash_entry
*entry
;
526 assert(smPriv
->stfbi_ht
);
528 mtx_lock(&smPriv
->st_mutex
);
529 entry
= _mesa_hash_table_search(smPriv
->stfbi_ht
, stfbi
);
530 mtx_unlock(&smPriv
->st_mutex
);
532 return entry
!= NULL
;
537 st_framebuffer_iface_insert(struct st_manager
*smapi
,
538 struct st_framebuffer_iface
*stfbi
)
540 struct st_manager_private
*smPriv
=
541 (struct st_manager_private
*)smapi
->st_manager_private
;
542 struct hash_entry
*entry
;
545 assert(smPriv
->stfbi_ht
);
547 mtx_lock(&smPriv
->st_mutex
);
548 entry
= _mesa_hash_table_insert(smPriv
->stfbi_ht
, stfbi
, stfbi
);
549 mtx_unlock(&smPriv
->st_mutex
);
551 return entry
!= NULL
;
556 st_framebuffer_iface_remove(struct st_manager
*smapi
,
557 struct st_framebuffer_iface
*stfbi
)
559 struct st_manager_private
*smPriv
=
560 (struct st_manager_private
*)smapi
->st_manager_private
;
561 struct hash_entry
*entry
;
563 if (!smPriv
|| !smPriv
->stfbi_ht
)
566 mtx_lock(&smPriv
->st_mutex
);
567 entry
= _mesa_hash_table_search(smPriv
->stfbi_ht
, stfbi
);
571 _mesa_hash_table_remove(smPriv
->stfbi_ht
, entry
);
574 mtx_unlock(&smPriv
->st_mutex
);
579 * The framebuffer interface object is no longer valid.
580 * Remove the object from the framebuffer interface hash table.
583 st_api_destroy_drawable(struct st_api
*stapi
,
584 struct st_framebuffer_iface
*stfbi
)
589 st_framebuffer_iface_remove(stfbi
->state_manager
, stfbi
);
594 * Purge the winsys buffers list to remove any references to
595 * non-existing framebuffer interface objects.
598 st_framebuffers_purge(struct st_context
*st
)
600 struct st_context_iface
*st_iface
= &st
->iface
;
601 struct st_manager
*smapi
= st_iface
->state_manager
;
602 struct st_framebuffer
*stfb
, *next
;
606 LIST_FOR_EACH_ENTRY_SAFE_REV(stfb
, next
, &st
->winsys_buffers
, head
) {
607 struct st_framebuffer_iface
*stfbi
= stfb
->iface
;
612 * If the corresponding framebuffer interface object no longer exists,
613 * remove the framebuffer object from the context's winsys buffers list,
614 * and unreference the framebuffer object, so its resources can be
617 if (!st_framebuffer_iface_lookup(smapi
, stfbi
)) {
618 LIST_DEL(&stfb
->head
);
619 st_framebuffer_reference(&stfb
, NULL
);
625 st_context_flush(struct st_context_iface
*stctxi
, unsigned flags
,
626 struct pipe_fence_handle
**fence
)
628 struct st_context
*st
= (struct st_context
*) stctxi
;
629 unsigned pipe_flags
= 0;
631 if (flags
& ST_FLUSH_END_OF_FRAME
) {
632 pipe_flags
|= PIPE_FLUSH_END_OF_FRAME
;
635 st_flush(st
, fence
, pipe_flags
);
637 if ((flags
& ST_FLUSH_WAIT
) && fence
&& *fence
) {
638 st
->pipe
->screen
->fence_finish(st
->pipe
->screen
, NULL
, *fence
,
639 PIPE_TIMEOUT_INFINITE
);
640 st
->pipe
->screen
->fence_reference(st
->pipe
->screen
, fence
, NULL
);
643 if (flags
& ST_FLUSH_FRONT
)
644 st_manager_flush_frontbuffer(st
);
646 /* DRI3 changes the framebuffer after SwapBuffers, but we need to invoke
647 * st_manager_validate_framebuffers to notice that.
649 * Set gfx_shaders_may_be_dirty to invoke st_validate_state in the next
650 * draw call, which will invoke st_manager_validate_framebuffers, but it
651 * won't dirty states if there is no change.
653 if (flags
& ST_FLUSH_END_OF_FRAME
)
654 st
->gfx_shaders_may_be_dirty
= true;
658 st_context_teximage(struct st_context_iface
*stctxi
,
659 enum st_texture_type tex_type
,
660 int level
, enum pipe_format pipe_format
,
661 struct pipe_resource
*tex
, boolean mipmap
)
663 struct st_context
*st
= (struct st_context
*) stctxi
;
664 struct gl_context
*ctx
= st
->ctx
;
665 struct gl_texture_object
*texObj
;
666 struct gl_texture_image
*texImage
;
667 struct st_texture_object
*stObj
;
668 struct st_texture_image
*stImage
;
669 GLenum internalFormat
;
670 GLuint width
, height
, depth
;
675 target
= GL_TEXTURE_1D
;
678 target
= GL_TEXTURE_2D
;
681 target
= GL_TEXTURE_3D
;
683 case ST_TEXTURE_RECT
:
684 target
= GL_TEXTURE_RECTANGLE_ARB
;
690 texObj
= _mesa_get_current_tex_object(ctx
, target
);
692 _mesa_lock_texture(ctx
, texObj
);
694 stObj
= st_texture_object(texObj
);
695 /* switch to surface based */
696 if (!stObj
->surface_based
) {
697 _mesa_clear_texture_object(ctx
, texObj
, NULL
);
698 stObj
->surface_based
= GL_TRUE
;
701 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, level
);
702 stImage
= st_texture_image(texImage
);
704 mesa_format texFormat
= st_pipe_format_to_mesa_format(pipe_format
);
706 if (util_format_has_alpha(tex
->format
))
707 internalFormat
= GL_RGBA
;
709 internalFormat
= GL_RGB
;
711 _mesa_init_teximage_fields(ctx
, texImage
,
712 tex
->width0
, tex
->height0
, 1, 0,
713 internalFormat
, texFormat
);
716 height
= tex
->height0
;
719 /* grow the image size until we hit level = 0 */
731 _mesa_clear_texture_image(ctx
, texImage
);
732 width
= height
= depth
= 0;
735 pipe_resource_reference(&stImage
->pt
, tex
);
736 stObj
->surface_format
= pipe_format
;
738 stObj
->needs_validation
= true;
740 _mesa_dirty_texobj(ctx
, texObj
);
741 _mesa_unlock_texture(ctx
, texObj
);
747 st_context_copy(struct st_context_iface
*stctxi
,
748 struct st_context_iface
*stsrci
, unsigned mask
)
750 struct st_context
*st
= (struct st_context
*) stctxi
;
751 struct st_context
*src
= (struct st_context
*) stsrci
;
753 _mesa_copy_context(src
->ctx
, st
->ctx
, mask
);
757 st_context_share(struct st_context_iface
*stctxi
,
758 struct st_context_iface
*stsrci
)
760 struct st_context
*st
= (struct st_context
*) stctxi
;
761 struct st_context
*src
= (struct st_context
*) stsrci
;
763 return _mesa_share_state(st
->ctx
, src
->ctx
);
767 st_context_destroy(struct st_context_iface
*stctxi
)
769 struct st_context
*st
= (struct st_context
*) stctxi
;
770 st_destroy_context(st
);
774 st_start_thread(struct st_context_iface
*stctxi
)
776 struct st_context
*st
= (struct st_context
*) stctxi
;
778 _mesa_glthread_init(st
->ctx
);
782 st_thread_finish(struct st_context_iface
*stctxi
)
784 struct st_context
*st
= (struct st_context
*) stctxi
;
786 _mesa_glthread_finish(st
->ctx
);
789 static struct st_context_iface
*
790 st_api_create_context(struct st_api
*stapi
, struct st_manager
*smapi
,
791 const struct st_context_attribs
*attribs
,
792 enum st_context_error
*error
,
793 struct st_context_iface
*shared_stctxi
)
795 struct st_context
*shared_ctx
= (struct st_context
*) shared_stctxi
;
796 struct st_context
*st
;
797 struct pipe_context
*pipe
;
798 struct gl_config mode
;
800 bool no_error
= false;
801 unsigned ctx_flags
= PIPE_CONTEXT_PREFER_THREADED
;
803 if (!(stapi
->profile_mask
& (1 << attribs
->profile
)))
806 switch (attribs
->profile
) {
807 case ST_PROFILE_DEFAULT
:
808 api
= API_OPENGL_COMPAT
;
810 case ST_PROFILE_OPENGL_ES1
:
813 case ST_PROFILE_OPENGL_ES2
:
816 case ST_PROFILE_OPENGL_CORE
:
817 api
= API_OPENGL_CORE
;
820 *error
= ST_CONTEXT_ERROR_BAD_API
;
824 /* Create a hash table for the framebuffer interface objects
825 * if it has not been created for this st manager.
827 if (smapi
->st_manager_private
== NULL
) {
828 struct st_manager_private
*smPriv
;
830 smPriv
= CALLOC_STRUCT(st_manager_private
);
831 mtx_init(&smPriv
->st_mutex
, mtx_plain
);
832 smPriv
->stfbi_ht
= _mesa_hash_table_create(NULL
,
833 st_framebuffer_iface_hash
,
834 st_framebuffer_iface_equal
);
835 smapi
->st_manager_private
= smPriv
;
836 smapi
->destroy
= st_manager_destroy
;
839 if (attribs
->flags
& ST_CONTEXT_FLAG_ROBUST_ACCESS
)
840 ctx_flags
|= PIPE_CONTEXT_ROBUST_BUFFER_ACCESS
;
842 if (attribs
->flags
& ST_CONTEXT_FLAG_NO_ERROR
)
845 pipe
= smapi
->screen
->context_create(smapi
->screen
, NULL
, ctx_flags
);
847 *error
= ST_CONTEXT_ERROR_NO_MEMORY
;
851 st_visual_to_context_mode(&attribs
->visual
, &mode
);
852 st
= st_create_context(api
, pipe
, &mode
, shared_ctx
, &attribs
->options
, no_error
);
854 *error
= ST_CONTEXT_ERROR_NO_MEMORY
;
859 if (attribs
->flags
& ST_CONTEXT_FLAG_DEBUG
) {
860 if (!_mesa_set_debug_state_int(st
->ctx
, GL_DEBUG_OUTPUT
, GL_TRUE
)) {
861 *error
= ST_CONTEXT_ERROR_NO_MEMORY
;
865 st
->ctx
->Const
.ContextFlags
|= GL_CONTEXT_FLAG_DEBUG_BIT
;
868 if (st
->ctx
->Const
.ContextFlags
& GL_CONTEXT_FLAG_DEBUG_BIT
) {
869 st_update_debug_callback(st
);
872 if (attribs
->flags
& ST_CONTEXT_FLAG_FORWARD_COMPATIBLE
)
873 st
->ctx
->Const
.ContextFlags
|= GL_CONTEXT_FLAG_FORWARD_COMPATIBLE_BIT
;
874 if (attribs
->flags
& ST_CONTEXT_FLAG_ROBUST_ACCESS
) {
875 st
->ctx
->Const
.ContextFlags
|= GL_CONTEXT_FLAG_ROBUST_ACCESS_BIT_ARB
;
876 st
->ctx
->Const
.RobustAccess
= GL_TRUE
;
878 if (attribs
->flags
& ST_CONTEXT_FLAG_RESET_NOTIFICATION_ENABLED
) {
879 st
->ctx
->Const
.ResetStrategy
= GL_LOSE_CONTEXT_ON_RESET_ARB
;
880 st_install_device_reset_callback(st
);
883 /* need to perform version check */
884 if (attribs
->major
> 1 || attribs
->minor
> 0) {
885 /* Is the actual version less than the requested version?
887 if (st
->ctx
->Version
< attribs
->major
* 10U + attribs
->minor
) {
888 *error
= ST_CONTEXT_ERROR_BAD_VERSION
;
889 st_destroy_context(st
);
894 st
->invalidate_on_gl_viewport
=
895 smapi
->get_param(smapi
, ST_MANAGER_BROKEN_INVALIDATE
);
897 st
->iface
.destroy
= st_context_destroy
;
898 st
->iface
.flush
= st_context_flush
;
899 st
->iface
.teximage
= st_context_teximage
;
900 st
->iface
.copy
= st_context_copy
;
901 st
->iface
.share
= st_context_share
;
902 st
->iface
.start_thread
= st_start_thread
;
903 st
->iface
.thread_finish
= st_thread_finish
;
904 st
->iface
.st_context_private
= (void *) smapi
;
905 st
->iface
.cso_context
= st
->cso_context
;
906 st
->iface
.pipe
= st
->pipe
;
907 st
->iface
.state_manager
= smapi
;
909 *error
= ST_CONTEXT_SUCCESS
;
913 static struct st_context_iface
*
914 st_api_get_current(struct st_api
*stapi
)
916 GET_CURRENT_CONTEXT(ctx
);
917 struct st_context
*st
= (ctx
) ? ctx
->st
: NULL
;
919 return (st
) ? &st
->iface
: NULL
;
922 static struct st_framebuffer
*
923 st_framebuffer_reuse_or_create(struct st_context
*st
,
924 struct gl_framebuffer
*fb
,
925 struct st_framebuffer_iface
*stfbi
)
927 struct st_framebuffer
*cur
= NULL
, *stfb
= NULL
;
932 /* Check if there is already a framebuffer object for the specified
933 * framebuffer interface in this context. If there is one, use it.
935 LIST_FOR_EACH_ENTRY(cur
, &st
->winsys_buffers
, head
) {
936 if (cur
->iface_ID
== stfbi
->ID
) {
937 st_framebuffer_reference(&stfb
, cur
);
942 /* If there is not already a framebuffer object, create one */
944 cur
= st_framebuffer_create(st
, stfbi
);
947 /* add the referenced framebuffer interface object to
948 * the framebuffer interface object hash table.
950 if (!st_framebuffer_iface_insert(stfbi
->state_manager
, stfbi
)) {
951 st_framebuffer_reference(&cur
, NULL
);
955 /* add to the context's winsys buffers list */
956 LIST_ADD(&cur
->head
, &st
->winsys_buffers
);
958 st_framebuffer_reference(&stfb
, cur
);
966 st_api_make_current(struct st_api
*stapi
, struct st_context_iface
*stctxi
,
967 struct st_framebuffer_iface
*stdrawi
,
968 struct st_framebuffer_iface
*streadi
)
970 struct st_context
*st
= (struct st_context
*) stctxi
;
971 struct st_framebuffer
*stdraw
, *stread
;
974 _glapi_check_multithread();
977 /* reuse or create the draw fb */
978 stdraw
= st_framebuffer_reuse_or_create(st
,
979 st
->ctx
->WinSysDrawBuffer
, stdrawi
);
980 if (streadi
!= stdrawi
) {
981 /* do the same for the read fb */
982 stread
= st_framebuffer_reuse_or_create(st
,
983 st
->ctx
->WinSysReadBuffer
, streadi
);
987 /* reuse the draw fb for the read fb */
989 st_framebuffer_reference(&stread
, stdraw
);
992 if (stdraw
&& stread
) {
993 st_framebuffer_validate(stdraw
, st
);
994 if (stread
!= stdraw
)
995 st_framebuffer_validate(stread
, st
);
997 ret
= _mesa_make_current(st
->ctx
, &stdraw
->Base
, &stread
->Base
);
999 st
->draw_stamp
= stdraw
->stamp
- 1;
1000 st
->read_stamp
= stread
->stamp
- 1;
1001 st_context_validate(st
, stdraw
, stread
);
1004 struct gl_framebuffer
*incomplete
= _mesa_get_incomplete_framebuffer();
1005 ret
= _mesa_make_current(st
->ctx
, incomplete
, incomplete
);
1008 st_framebuffer_reference(&stdraw
, NULL
);
1009 st_framebuffer_reference(&stread
, NULL
);
1011 /* Purge the context's winsys_buffers list in case any
1012 * of the referenced drawables no longer exist.
1014 st_framebuffers_purge(st
);
1017 ret
= _mesa_make_current(NULL
, NULL
, NULL
);
1024 st_api_destroy(struct st_api
*stapi
)
1029 * Flush the front buffer if the current context renders to the front buffer.
1032 st_manager_flush_frontbuffer(struct st_context
*st
)
1034 struct st_framebuffer
*stfb
= st_ws_framebuffer(st
->ctx
->DrawBuffer
);
1035 struct st_renderbuffer
*strb
= NULL
;
1038 strb
= st_renderbuffer(stfb
->Base
.Attachment
[BUFFER_FRONT_LEFT
].Renderbuffer
);
1040 /* Do we have a front color buffer and has it been drawn to since last
1041 * frontbuffer flush?
1043 if (strb
&& strb
->defined
) {
1044 stfb
->iface
->flush_front(&st
->iface
, stfb
->iface
,
1045 ST_ATTACHMENT_FRONT_LEFT
);
1046 strb
->defined
= GL_FALSE
;
1048 /* Trigger an update of strb->defined on next draw */
1049 st
->dirty
|= ST_NEW_FB_STATE
;
1054 * Re-validate the framebuffers.
1057 st_manager_validate_framebuffers(struct st_context
*st
)
1059 struct st_framebuffer
*stdraw
= st_ws_framebuffer(st
->ctx
->DrawBuffer
);
1060 struct st_framebuffer
*stread
= st_ws_framebuffer(st
->ctx
->ReadBuffer
);
1063 st_framebuffer_validate(stdraw
, st
);
1064 if (stread
&& stread
!= stdraw
)
1065 st_framebuffer_validate(stread
, st
);
1067 st_context_validate(st
, stdraw
, stread
);
1072 * Flush any outstanding swapbuffers on the current draw framebuffer.
1075 st_manager_flush_swapbuffers(void)
1077 GET_CURRENT_CONTEXT(ctx
);
1078 struct st_context
*st
= (ctx
) ? ctx
->st
: NULL
;
1079 struct st_framebuffer
*stfb
;
1084 stfb
= st_ws_framebuffer(ctx
->DrawBuffer
);
1085 if (!stfb
|| !stfb
->iface
->flush_swapbuffers
)
1088 stfb
->iface
->flush_swapbuffers(&st
->iface
, stfb
->iface
);
1093 * Add a color renderbuffer on demand. The FBO must correspond to a window,
1094 * not a user-created FBO.
1097 st_manager_add_color_renderbuffer(struct st_context
*st
,
1098 struct gl_framebuffer
*fb
,
1099 gl_buffer_index idx
)
1101 struct st_framebuffer
*stfb
= st_ws_framebuffer(fb
);
1107 assert(_mesa_is_winsys_fbo(fb
));
1109 if (stfb
->Base
.Attachment
[idx
].Renderbuffer
)
1113 case BUFFER_FRONT_LEFT
:
1114 case BUFFER_BACK_LEFT
:
1115 case BUFFER_FRONT_RIGHT
:
1116 case BUFFER_BACK_RIGHT
:
1122 if (!st_framebuffer_add_renderbuffer(stfb
, idx
))
1125 st_framebuffer_update_attachments(stfb
);
1128 * Force a call to the state tracker manager to validate the
1129 * new renderbuffer. It might be that there is a window system
1130 * renderbuffer available.
1133 stfb
->iface_stamp
= p_atomic_read(&stfb
->iface
->stamp
) - 1;
1135 st_invalidate_buffers(st
);
1141 st_manager_destroy(struct st_manager
*smapi
)
1143 struct st_manager_private
*smPriv
= smapi
->st_manager_private
;
1145 if (smPriv
&& smPriv
->stfbi_ht
) {
1146 _mesa_hash_table_destroy(smPriv
->stfbi_ht
, NULL
);
1147 mtx_destroy(&smPriv
->st_mutex
);
1149 smapi
->st_manager_private
= NULL
;
1154 get_version(struct pipe_screen
*screen
,
1155 struct st_config_options
*options
, gl_api api
)
1157 struct gl_constants consts
= {0};
1158 struct gl_extensions extensions
= {0};
1161 if (_mesa_override_gl_version_contextless(&consts
, &api
, &version
)) {
1165 _mesa_init_constants(&consts
, api
);
1166 _mesa_init_extensions(&extensions
);
1168 st_init_limits(screen
, &consts
, &extensions
);
1169 st_init_extensions(screen
, &consts
, &extensions
, options
);
1171 return _mesa_get_version(&extensions
, &consts
, api
);
1175 st_api_query_versions(struct st_api
*stapi
, struct st_manager
*sm
,
1176 struct st_config_options
*options
,
1177 int *gl_core_version
,
1178 int *gl_compat_version
,
1179 int *gl_es1_version
,
1180 int *gl_es2_version
)
1182 *gl_core_version
= get_version(sm
->screen
, options
, API_OPENGL_CORE
);
1183 *gl_compat_version
= get_version(sm
->screen
, options
, API_OPENGL_COMPAT
);
1184 *gl_es1_version
= get_version(sm
->screen
, options
, API_OPENGLES
);
1185 *gl_es2_version
= get_version(sm
->screen
, options
, API_OPENGLES2
);
1188 static const struct st_api st_gl_api
= {
1189 .name
= "Mesa " PACKAGE_VERSION
,
1190 .api
= ST_API_OPENGL
,
1191 .profile_mask
= ST_PROFILE_DEFAULT_MASK
|
1192 ST_PROFILE_OPENGL_CORE_MASK
|
1193 ST_PROFILE_OPENGL_ES1_MASK
|
1194 ST_PROFILE_OPENGL_ES2_MASK
|
1196 .feature_mask
= ST_API_FEATURE_MS_VISUALS_MASK
,
1197 .destroy
= st_api_destroy
,
1198 .query_versions
= st_api_query_versions
,
1199 .create_context
= st_api_create_context
,
1200 .make_current
= st_api_make_current
,
1201 .get_current
= st_api_get_current
,
1202 .destroy_drawable
= st_api_destroy_drawable
,
1206 st_gl_api_create(void)
1208 return (struct st_api
*) &st_gl_api
;