st/mesa: set debug callback for debug contexts
[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/texobj.h"
32 #include "main/teximage.h"
33 #include "main/texstate.h"
34 #include "main/errors.h"
35 #include "main/framebuffer.h"
36 #include "main/fbobject.h"
37 #include "main/renderbuffer.h"
38 #include "main/version.h"
39 #include "st_texture.h"
40
41 #include "st_context.h"
42 #include "st_extensions.h"
43 #include "st_format.h"
44 #include "st_cb_fbo.h"
45 #include "st_cb_flush.h"
46 #include "st_manager.h"
47
48 #include "state_tracker/st_gl_api.h"
49
50 #include "pipe/p_context.h"
51 #include "pipe/p_screen.h"
52 #include "util/u_format.h"
53 #include "util/u_pointer.h"
54 #include "util/u_inlines.h"
55 #include "util/u_atomic.h"
56 #include "util/u_surface.h"
57
58 /**
59 * Cast wrapper to convert a struct gl_framebuffer to an st_framebuffer.
60 * Return NULL if the struct gl_framebuffer is a user-created framebuffer.
61 * We'll only return non-null for window system framebuffers.
62 * Note that this function may fail.
63 */
64 static inline struct st_framebuffer *
65 st_ws_framebuffer(struct gl_framebuffer *fb)
66 {
67 /* FBO cannot be casted. See st_new_framebuffer */
68 if (fb && _mesa_is_winsys_fbo(fb))
69 return (struct st_framebuffer *) fb;
70 return NULL;
71 }
72
73 /**
74 * Map an attachment to a buffer index.
75 */
76 static inline gl_buffer_index
77 attachment_to_buffer_index(enum st_attachment_type statt)
78 {
79 gl_buffer_index index;
80
81 switch (statt) {
82 case ST_ATTACHMENT_FRONT_LEFT:
83 index = BUFFER_FRONT_LEFT;
84 break;
85 case ST_ATTACHMENT_BACK_LEFT:
86 index = BUFFER_BACK_LEFT;
87 break;
88 case ST_ATTACHMENT_FRONT_RIGHT:
89 index = BUFFER_FRONT_RIGHT;
90 break;
91 case ST_ATTACHMENT_BACK_RIGHT:
92 index = BUFFER_BACK_RIGHT;
93 break;
94 case ST_ATTACHMENT_DEPTH_STENCIL:
95 index = BUFFER_DEPTH;
96 break;
97 case ST_ATTACHMENT_ACCUM:
98 index = BUFFER_ACCUM;
99 break;
100 case ST_ATTACHMENT_SAMPLE:
101 default:
102 index = BUFFER_COUNT;
103 break;
104 }
105
106 return index;
107 }
108
109 /**
110 * Map a buffer index to an attachment.
111 */
112 static inline enum st_attachment_type
113 buffer_index_to_attachment(gl_buffer_index index)
114 {
115 enum st_attachment_type statt;
116
117 switch (index) {
118 case BUFFER_FRONT_LEFT:
119 statt = ST_ATTACHMENT_FRONT_LEFT;
120 break;
121 case BUFFER_BACK_LEFT:
122 statt = ST_ATTACHMENT_BACK_LEFT;
123 break;
124 case BUFFER_FRONT_RIGHT:
125 statt = ST_ATTACHMENT_FRONT_RIGHT;
126 break;
127 case BUFFER_BACK_RIGHT:
128 statt = ST_ATTACHMENT_BACK_RIGHT;
129 break;
130 case BUFFER_DEPTH:
131 statt = ST_ATTACHMENT_DEPTH_STENCIL;
132 break;
133 case BUFFER_ACCUM:
134 statt = ST_ATTACHMENT_ACCUM;
135 break;
136 default:
137 statt = ST_ATTACHMENT_INVALID;
138 break;
139 }
140
141 return statt;
142 }
143
144 /**
145 * Make sure a context picks up the latest cached state of the
146 * drawables it binds to.
147 */
148 static void
149 st_context_validate(struct st_context *st,
150 struct st_framebuffer *stdraw,
151 struct st_framebuffer *stread)
152 {
153 if (stdraw && stdraw->stamp != st->draw_stamp) {
154 st->dirty.st |= ST_NEW_FRAMEBUFFER;
155 _mesa_resize_framebuffer(st->ctx, &stdraw->Base,
156 stdraw->Base.Width,
157 stdraw->Base.Height);
158 st->draw_stamp = stdraw->stamp;
159 }
160
161 if (stread && stread->stamp != st->read_stamp) {
162 if (stread != stdraw) {
163 st->dirty.st |= ST_NEW_FRAMEBUFFER;
164 _mesa_resize_framebuffer(st->ctx, &stread->Base,
165 stread->Base.Width,
166 stread->Base.Height);
167 }
168 st->read_stamp = stread->stamp;
169 }
170 }
171
172 /**
173 * Validate a framebuffer to make sure up-to-date pipe_textures are used.
174 * The context is only used for creating pipe surfaces and for calling
175 * _mesa_resize_framebuffer().
176 * (That should probably be rethought, since those surfaces become
177 * drawable state, not context state, and can be freed by another pipe
178 * context).
179 */
180 static void
181 st_framebuffer_validate(struct st_framebuffer *stfb,
182 struct st_context *st)
183 {
184 struct pipe_resource *textures[ST_ATTACHMENT_COUNT];
185 uint width, height;
186 unsigned i;
187 boolean changed = FALSE;
188 int32_t new_stamp;
189
190 /* Check for incomplete framebuffers (e.g. EGL_KHR_surfaceless_context) */
191 if (!stfb->iface)
192 return;
193
194 new_stamp = p_atomic_read(&stfb->iface->stamp);
195 if (stfb->iface_stamp == new_stamp)
196 return;
197
198 /* validate the fb */
199 do {
200 if (!stfb->iface->validate(&st->iface, stfb->iface, stfb->statts,
201 stfb->num_statts, textures))
202 return;
203
204 stfb->iface_stamp = new_stamp;
205 new_stamp = p_atomic_read(&stfb->iface->stamp);
206 } while(stfb->iface_stamp != new_stamp);
207
208 width = stfb->Base.Width;
209 height = stfb->Base.Height;
210
211 for (i = 0; i < stfb->num_statts; i++) {
212 struct st_renderbuffer *strb;
213 struct pipe_surface *ps, surf_tmpl;
214 gl_buffer_index idx;
215
216 if (!textures[i])
217 continue;
218
219 idx = attachment_to_buffer_index(stfb->statts[i]);
220 if (idx >= BUFFER_COUNT) {
221 pipe_resource_reference(&textures[i], NULL);
222 continue;
223 }
224
225 strb = st_renderbuffer(stfb->Base.Attachment[idx].Renderbuffer);
226 assert(strb);
227 if (strb->texture == textures[i]) {
228 pipe_resource_reference(&textures[i], NULL);
229 continue;
230 }
231
232 u_surface_default_template(&surf_tmpl, textures[i]);
233 ps = st->pipe->create_surface(st->pipe, textures[i], &surf_tmpl);
234 if (ps) {
235 pipe_surface_reference(&strb->surface, ps);
236 pipe_resource_reference(&strb->texture, ps->texture);
237 /* ownership transfered */
238 pipe_surface_reference(&ps, NULL);
239
240 changed = TRUE;
241
242 strb->Base.Width = strb->surface->width;
243 strb->Base.Height = strb->surface->height;
244
245 width = strb->Base.Width;
246 height = strb->Base.Height;
247 }
248
249 pipe_resource_reference(&textures[i], NULL);
250 }
251
252 if (changed) {
253 ++stfb->stamp;
254 _mesa_resize_framebuffer(st->ctx, &stfb->Base, width, height);
255 }
256 }
257
258 /**
259 * Update the attachments to validate by looping the existing renderbuffers.
260 */
261 static void
262 st_framebuffer_update_attachments(struct st_framebuffer *stfb)
263 {
264 gl_buffer_index idx;
265
266 stfb->num_statts = 0;
267 for (idx = 0; idx < BUFFER_COUNT; idx++) {
268 struct st_renderbuffer *strb;
269 enum st_attachment_type statt;
270
271 strb = st_renderbuffer(stfb->Base.Attachment[idx].Renderbuffer);
272 if (!strb || strb->software)
273 continue;
274
275 statt = buffer_index_to_attachment(idx);
276 if (statt != ST_ATTACHMENT_INVALID &&
277 st_visual_have_buffers(stfb->iface->visual, 1 << statt))
278 stfb->statts[stfb->num_statts++] = statt;
279 }
280 stfb->stamp++;
281 }
282
283 /**
284 * Add a renderbuffer to the framebuffer.
285 */
286 static boolean
287 st_framebuffer_add_renderbuffer(struct st_framebuffer *stfb,
288 gl_buffer_index idx)
289 {
290 struct gl_renderbuffer *rb;
291 enum pipe_format format;
292 boolean sw;
293
294 if (!stfb->iface)
295 return FALSE;
296
297 /* do not distinguish depth/stencil buffers */
298 if (idx == BUFFER_STENCIL)
299 idx = BUFFER_DEPTH;
300
301 switch (idx) {
302 case BUFFER_DEPTH:
303 format = stfb->iface->visual->depth_stencil_format;
304 sw = FALSE;
305 break;
306 case BUFFER_ACCUM:
307 format = stfb->iface->visual->accum_format;
308 sw = TRUE;
309 break;
310 default:
311 format = stfb->iface->visual->color_format;
312 if (stfb->Base.Visual.sRGBCapable)
313 format = util_format_srgb(format);
314 sw = FALSE;
315 break;
316 }
317
318 if (format == PIPE_FORMAT_NONE)
319 return FALSE;
320
321 rb = st_new_renderbuffer_fb(format, stfb->iface->visual->samples, sw);
322 if (!rb)
323 return FALSE;
324
325 if (idx != BUFFER_DEPTH) {
326 _mesa_add_renderbuffer(&stfb->Base, idx, rb);
327 }
328 else {
329 if (util_format_get_component_bits(format, UTIL_FORMAT_COLORSPACE_ZS, 0))
330 _mesa_add_renderbuffer(&stfb->Base, BUFFER_DEPTH, rb);
331 if (util_format_get_component_bits(format, UTIL_FORMAT_COLORSPACE_ZS, 1))
332 _mesa_add_renderbuffer(&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 if (flags & ST_FLUSH_FRONT)
506 st_manager_flush_frontbuffer(st);
507 }
508
509 static boolean
510 st_context_teximage(struct st_context_iface *stctxi,
511 enum st_texture_type tex_type,
512 int level, enum pipe_format pipe_format,
513 struct pipe_resource *tex, boolean mipmap)
514 {
515 struct st_context *st = (struct st_context *) stctxi;
516 struct gl_context *ctx = st->ctx;
517 struct gl_texture_object *texObj;
518 struct gl_texture_image *texImage;
519 struct st_texture_object *stObj;
520 struct st_texture_image *stImage;
521 GLenum internalFormat;
522 GLuint width, height, depth;
523 GLenum target;
524
525 switch (tex_type) {
526 case ST_TEXTURE_1D:
527 target = GL_TEXTURE_1D;
528 break;
529 case ST_TEXTURE_2D:
530 target = GL_TEXTURE_2D;
531 break;
532 case ST_TEXTURE_3D:
533 target = GL_TEXTURE_3D;
534 break;
535 case ST_TEXTURE_RECT:
536 target = GL_TEXTURE_RECTANGLE_ARB;
537 break;
538 default:
539 return FALSE;
540 }
541
542 texObj = _mesa_get_current_tex_object(ctx, target);
543
544 _mesa_lock_texture(ctx, texObj);
545
546 stObj = st_texture_object(texObj);
547 /* switch to surface based */
548 if (!stObj->surface_based) {
549 _mesa_clear_texture_object(ctx, texObj);
550 stObj->surface_based = GL_TRUE;
551 }
552
553 texImage = _mesa_get_tex_image(ctx, texObj, target, level);
554 stImage = st_texture_image(texImage);
555 if (tex) {
556 mesa_format texFormat = st_pipe_format_to_mesa_format(pipe_format);
557
558 if (util_format_has_alpha(tex->format))
559 internalFormat = GL_RGBA;
560 else
561 internalFormat = GL_RGB;
562
563 _mesa_init_teximage_fields(ctx, texImage,
564 tex->width0, tex->height0, 1, 0,
565 internalFormat, texFormat);
566
567 width = tex->width0;
568 height = tex->height0;
569 depth = tex->depth0;
570
571 /* grow the image size until we hit level = 0 */
572 while (level > 0) {
573 if (width != 1)
574 width <<= 1;
575 if (height != 1)
576 height <<= 1;
577 if (depth != 1)
578 depth <<= 1;
579 level--;
580 }
581 }
582 else {
583 _mesa_clear_texture_image(ctx, texImage);
584 width = height = depth = 0;
585 }
586
587 pipe_resource_reference(&stImage->pt, tex);
588 stObj->width0 = width;
589 stObj->height0 = height;
590 stObj->depth0 = depth;
591 stObj->surface_format = pipe_format;
592
593 _mesa_dirty_texobj(ctx, texObj);
594 _mesa_unlock_texture(ctx, texObj);
595
596 return TRUE;
597 }
598
599 static void
600 st_context_copy(struct st_context_iface *stctxi,
601 struct st_context_iface *stsrci, unsigned mask)
602 {
603 struct st_context *st = (struct st_context *) stctxi;
604 struct st_context *src = (struct st_context *) stsrci;
605
606 _mesa_copy_context(src->ctx, st->ctx, mask);
607 }
608
609 static boolean
610 st_context_share(struct st_context_iface *stctxi,
611 struct st_context_iface *stsrci)
612 {
613 struct st_context *st = (struct st_context *) stctxi;
614 struct st_context *src = (struct st_context *) stsrci;
615
616 return _mesa_share_state(st->ctx, src->ctx);
617 }
618
619 static void
620 st_context_destroy(struct st_context_iface *stctxi)
621 {
622 struct st_context *st = (struct st_context *) stctxi;
623 st_destroy_context(st);
624 }
625
626 static void
627 st_debug_message(void *data,
628 unsigned *id,
629 enum pipe_debug_type ptype,
630 const char *fmt,
631 va_list args)
632 {
633 struct st_context *st = data;
634 enum mesa_debug_source source;
635 enum mesa_debug_type type;
636 enum mesa_debug_severity severity;
637
638 switch (ptype) {
639 case PIPE_DEBUG_TYPE_OUT_OF_MEMORY:
640 source = MESA_DEBUG_SOURCE_API;
641 type = MESA_DEBUG_TYPE_ERROR;
642 severity = MESA_DEBUG_SEVERITY_MEDIUM;
643 break;
644 case PIPE_DEBUG_TYPE_ERROR:
645 source = MESA_DEBUG_SOURCE_API;
646 type = MESA_DEBUG_TYPE_ERROR;
647 severity = MESA_DEBUG_SEVERITY_MEDIUM;
648 break;
649 case PIPE_DEBUG_TYPE_SHADER_INFO:
650 source = MESA_DEBUG_SOURCE_SHADER_COMPILER;
651 type = MESA_DEBUG_TYPE_OTHER;
652 severity = MESA_DEBUG_SEVERITY_NOTIFICATION;
653 break;
654 case PIPE_DEBUG_TYPE_PERF_INFO:
655 source = MESA_DEBUG_SOURCE_API;
656 type = MESA_DEBUG_TYPE_PERFORMANCE;
657 severity = MESA_DEBUG_SEVERITY_NOTIFICATION;
658 break;
659 case PIPE_DEBUG_TYPE_INFO:
660 source = MESA_DEBUG_SOURCE_API;
661 type = MESA_DEBUG_TYPE_OTHER;
662 severity = MESA_DEBUG_SEVERITY_NOTIFICATION;
663 break;
664 case PIPE_DEBUG_TYPE_FALLBACK:
665 source = MESA_DEBUG_SOURCE_API;
666 type = MESA_DEBUG_TYPE_PERFORMANCE;
667 severity = MESA_DEBUG_SEVERITY_NOTIFICATION;
668 break;
669 case PIPE_DEBUG_TYPE_CONFORMANCE:
670 source = MESA_DEBUG_SOURCE_API;
671 type = MESA_DEBUG_TYPE_OTHER;
672 severity = MESA_DEBUG_SEVERITY_NOTIFICATION;
673 break;
674 }
675 _mesa_gl_vdebug(st->ctx, id, source, type, severity, fmt, args);
676 }
677
678 static struct st_context_iface *
679 st_api_create_context(struct st_api *stapi, struct st_manager *smapi,
680 const struct st_context_attribs *attribs,
681 enum st_context_error *error,
682 struct st_context_iface *shared_stctxi)
683 {
684 struct st_context *shared_ctx = (struct st_context *) shared_stctxi;
685 struct st_context *st;
686 struct pipe_context *pipe;
687 struct gl_config mode;
688 gl_api api;
689
690 if (!(stapi->profile_mask & (1 << attribs->profile)))
691 return NULL;
692
693 switch (attribs->profile) {
694 case ST_PROFILE_DEFAULT:
695 api = API_OPENGL_COMPAT;
696 break;
697 case ST_PROFILE_OPENGL_ES1:
698 api = API_OPENGLES;
699 break;
700 case ST_PROFILE_OPENGL_ES2:
701 api = API_OPENGLES2;
702 break;
703 case ST_PROFILE_OPENGL_CORE:
704 api = API_OPENGL_CORE;
705 break;
706 default:
707 *error = ST_CONTEXT_ERROR_BAD_API;
708 return NULL;
709 break;
710 }
711
712 pipe = smapi->screen->context_create(smapi->screen, NULL, 0);
713 if (!pipe) {
714 *error = ST_CONTEXT_ERROR_NO_MEMORY;
715 return NULL;
716 }
717
718 st_visual_to_context_mode(&attribs->visual, &mode);
719 st = st_create_context(api, pipe, &mode, shared_ctx, &attribs->options);
720 if (!st) {
721 *error = ST_CONTEXT_ERROR_NO_MEMORY;
722 pipe->destroy(pipe);
723 return NULL;
724 }
725
726 if (attribs->flags & ST_CONTEXT_FLAG_DEBUG){
727 if (!_mesa_set_debug_state_int(st->ctx, GL_DEBUG_OUTPUT, GL_TRUE)) {
728 *error = ST_CONTEXT_ERROR_NO_MEMORY;
729 return NULL;
730 }
731 st->ctx->Const.ContextFlags |= GL_CONTEXT_FLAG_DEBUG_BIT;
732
733 if (pipe->set_debug_callback) {
734 struct pipe_debug_callback cb = { st_debug_message, st };
735 pipe->set_debug_callback(pipe, &cb);
736 }
737 }
738
739 if (attribs->flags & ST_CONTEXT_FLAG_FORWARD_COMPATIBLE)
740 st->ctx->Const.ContextFlags |= GL_CONTEXT_FLAG_FORWARD_COMPATIBLE_BIT;
741 if (attribs->flags & ST_CONTEXT_FLAG_ROBUST_ACCESS)
742 st->ctx->Const.ContextFlags |= GL_CONTEXT_FLAG_ROBUST_ACCESS_BIT_ARB;
743 if (attribs->flags & ST_CONTEXT_FLAG_RESET_NOTIFICATION_ENABLED)
744 st->ctx->Const.ResetStrategy = GL_LOSE_CONTEXT_ON_RESET_ARB;
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.st_context_private = (void *) smapi;
766 st->iface.cso_context = st->cso_context;
767 st->iface.pipe = st->pipe;
768
769 *error = ST_CONTEXT_SUCCESS;
770 return &st->iface;
771 }
772
773 static struct st_context_iface *
774 st_api_get_current(struct st_api *stapi)
775 {
776 GET_CURRENT_CONTEXT(ctx);
777 struct st_context *st = (ctx) ? ctx->st : NULL;
778
779 return (st) ? &st->iface : NULL;
780 }
781
782 static struct st_framebuffer *
783 st_framebuffer_reuse_or_create(struct st_context *st,
784 struct gl_framebuffer *fb,
785 struct st_framebuffer_iface *stfbi)
786 {
787 struct st_framebuffer *cur = st_ws_framebuffer(fb), *stfb = NULL;
788
789 /* dummy framebuffers cant be used as st_framebuffer */
790 if (cur && &cur->Base != _mesa_get_incomplete_framebuffer() &&
791 cur->iface == stfbi) {
792 /* reuse the current stfb */
793 st_framebuffer_reference(&stfb, cur);
794 }
795 else {
796 /* create a new one */
797 stfb = st_framebuffer_create(st, stfbi);
798 }
799
800 return stfb;
801 }
802
803 static boolean
804 st_api_make_current(struct st_api *stapi, struct st_context_iface *stctxi,
805 struct st_framebuffer_iface *stdrawi,
806 struct st_framebuffer_iface *streadi)
807 {
808 struct st_context *st = (struct st_context *) stctxi;
809 struct st_framebuffer *stdraw, *stread;
810 boolean ret;
811
812 _glapi_check_multithread();
813
814 if (st) {
815 /* reuse or create the draw fb */
816 stdraw = st_framebuffer_reuse_or_create(st,
817 st->ctx->WinSysDrawBuffer, stdrawi);
818 if (streadi != stdrawi) {
819 /* do the same for the read fb */
820 stread = st_framebuffer_reuse_or_create(st,
821 st->ctx->WinSysReadBuffer, streadi);
822 }
823 else {
824 stread = NULL;
825 /* reuse the draw fb for the read fb */
826 if (stdraw)
827 st_framebuffer_reference(&stread, stdraw);
828 }
829
830 if (stdraw && stread) {
831 st_framebuffer_validate(stdraw, st);
832 if (stread != stdraw)
833 st_framebuffer_validate(stread, st);
834
835 ret = _mesa_make_current(st->ctx, &stdraw->Base, &stread->Base);
836
837 st->draw_stamp = stdraw->stamp - 1;
838 st->read_stamp = stread->stamp - 1;
839 st_context_validate(st, stdraw, stread);
840 }
841 else {
842 struct gl_framebuffer *incomplete = _mesa_get_incomplete_framebuffer();
843 ret = _mesa_make_current(st->ctx, incomplete, incomplete);
844 }
845
846 st_framebuffer_reference(&stdraw, NULL);
847 st_framebuffer_reference(&stread, NULL);
848 }
849 else {
850 ret = _mesa_make_current(NULL, NULL, NULL);
851 }
852
853 return ret;
854 }
855
856 static st_proc_t
857 st_api_get_proc_address(struct st_api *stapi, const char *procname)
858 {
859 return (st_proc_t) _glapi_get_proc_address(procname);
860 }
861
862 static void
863 st_api_destroy(struct st_api *stapi)
864 {
865 }
866
867 /**
868 * Flush the front buffer if the current context renders to the front buffer.
869 */
870 void
871 st_manager_flush_frontbuffer(struct st_context *st)
872 {
873 struct st_framebuffer *stfb = st_ws_framebuffer(st->ctx->DrawBuffer);
874 struct st_renderbuffer *strb = NULL;
875
876 if (stfb)
877 strb = st_renderbuffer(stfb->Base.Attachment[BUFFER_FRONT_LEFT].Renderbuffer);
878 if (!strb)
879 return;
880
881 /* never a dummy fb */
882 assert(&stfb->Base != _mesa_get_incomplete_framebuffer());
883 stfb->iface->flush_front(&st->iface, stfb->iface, ST_ATTACHMENT_FRONT_LEFT);
884 }
885
886 /**
887 * Return the surface of an EGLImage.
888 * FIXME: I think this should operate on resources, not surfaces
889 */
890 struct pipe_surface *
891 st_manager_get_egl_image_surface(struct st_context *st, void *eglimg)
892 {
893 struct st_manager *smapi =
894 (struct st_manager *) st->iface.st_context_private;
895 struct st_egl_image stimg;
896 struct pipe_surface *ps, surf_tmpl;
897
898 if (!smapi || !smapi->get_egl_image)
899 return NULL;
900
901 memset(&stimg, 0, sizeof(stimg));
902 if (!smapi->get_egl_image(smapi, eglimg, &stimg))
903 return NULL;
904
905 u_surface_default_template(&surf_tmpl, stimg.texture);
906 surf_tmpl.u.tex.level = stimg.level;
907 surf_tmpl.u.tex.first_layer = stimg.layer;
908 surf_tmpl.u.tex.last_layer = stimg.layer;
909 ps = st->pipe->create_surface(st->pipe, stimg.texture, &surf_tmpl);
910 pipe_resource_reference(&stimg.texture, NULL);
911
912 return ps;
913 }
914
915 /**
916 * Re-validate the framebuffers.
917 */
918 void
919 st_manager_validate_framebuffers(struct st_context *st)
920 {
921 struct st_framebuffer *stdraw = st_ws_framebuffer(st->ctx->DrawBuffer);
922 struct st_framebuffer *stread = st_ws_framebuffer(st->ctx->ReadBuffer);
923
924 if (stdraw)
925 st_framebuffer_validate(stdraw, st);
926 if (stread && stread != stdraw)
927 st_framebuffer_validate(stread, st);
928
929 st_context_validate(st, stdraw, stread);
930 }
931
932 /**
933 * Add a color renderbuffer on demand.
934 */
935 boolean
936 st_manager_add_color_renderbuffer(struct st_context *st,
937 struct gl_framebuffer *fb,
938 gl_buffer_index idx)
939 {
940 struct st_framebuffer *stfb = st_ws_framebuffer(fb);
941
942 /* FBO */
943 if (!stfb)
944 return FALSE;
945
946 if (stfb->Base.Attachment[idx].Renderbuffer)
947 return TRUE;
948
949 switch (idx) {
950 case BUFFER_FRONT_LEFT:
951 case BUFFER_BACK_LEFT:
952 case BUFFER_FRONT_RIGHT:
953 case BUFFER_BACK_RIGHT:
954 break;
955 default:
956 return FALSE;
957 break;
958 }
959
960 if (!st_framebuffer_add_renderbuffer(stfb, idx))
961 return FALSE;
962
963 st_framebuffer_update_attachments(stfb);
964
965 /*
966 * Force a call to the state tracker manager to validate the
967 * new renderbuffer. It might be that there is a window system
968 * renderbuffer available.
969 */
970 if(stfb->iface)
971 stfb->iface_stamp = p_atomic_read(&stfb->iface->stamp) - 1;
972
973 st_invalidate_state(st->ctx, _NEW_BUFFERS);
974
975 return TRUE;
976 }
977
978 static unsigned get_version(struct pipe_screen *screen,
979 struct st_config_options *options, gl_api api)
980 {
981 struct gl_constants consts = {0};
982 struct gl_extensions extensions = {0};
983 GLuint version;
984
985 if (_mesa_override_gl_version_contextless(&consts, &api, &version)) {
986 return version;
987 }
988
989 _mesa_init_constants(&consts, api);
990 _mesa_init_extensions(&extensions);
991
992 st_init_limits(screen, &consts, &extensions);
993 st_init_extensions(screen, &consts, &extensions, options, GL_TRUE);
994
995 return _mesa_get_version(&extensions, &consts, api);
996 }
997
998 static void
999 st_api_query_versions(struct st_api *stapi, struct st_manager *sm,
1000 struct st_config_options *options,
1001 int *gl_core_version,
1002 int *gl_compat_version,
1003 int *gl_es1_version,
1004 int *gl_es2_version)
1005 {
1006 *gl_core_version = get_version(sm->screen, options, API_OPENGL_CORE);
1007 *gl_compat_version = get_version(sm->screen, options, API_OPENGL_COMPAT);
1008 *gl_es1_version = get_version(sm->screen, options, API_OPENGLES);
1009 *gl_es2_version = get_version(sm->screen, options, API_OPENGLES2);
1010 }
1011
1012 static const struct st_api st_gl_api = {
1013 "Mesa " PACKAGE_VERSION,
1014 ST_API_OPENGL,
1015 ST_PROFILE_DEFAULT_MASK |
1016 ST_PROFILE_OPENGL_CORE_MASK |
1017 ST_PROFILE_OPENGL_ES1_MASK |
1018 ST_PROFILE_OPENGL_ES2_MASK |
1019 0,
1020 ST_API_FEATURE_MS_VISUALS_MASK,
1021 st_api_destroy,
1022 st_api_query_versions,
1023 st_api_get_proc_address,
1024 st_api_create_context,
1025 st_api_make_current,
1026 st_api_get_current,
1027 };
1028
1029 struct st_api *
1030 st_gl_api_create(void)
1031 {
1032 return (struct st_api *) &st_gl_api;
1033 }