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