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