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