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