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