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 *
st_ws_framebuffer(struct gl_framebuffer * fb)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
attachment_to_buffer_index(enum st_attachment_type statt)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
buffer_index_to_attachment(gl_buffer_index index)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
st_context_validate(struct st_context * st,struct st_framebuffer * stdraw,struct st_framebuffer * stread)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_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_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
st_framebuffer_validate(struct st_framebuffer * stfb,struct st_context * st)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
st_framebuffer_update_attachments(struct st_framebuffer * stfb)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
st_framebuffer_add_renderbuffer(struct st_framebuffer * stfb,gl_buffer_index idx)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
st_visual_to_context_mode(const struct st_visual * visual,struct gl_config * mode)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 *
st_framebuffer_create(struct st_context * st,struct st_framebuffer_iface * stfbi)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
st_framebuffer_reference(struct st_framebuffer ** ptr,struct st_framebuffer * stfb)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
st_context_flush(struct st_context_iface * stctxi,unsigned flags,struct pipe_fence_handle ** fence)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
st_context_teximage(struct st_context_iface * stctxi,enum st_texture_type tex_type,int level,enum pipe_format pipe_format,struct pipe_resource * tex,boolean mipmap)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
st_context_copy(struct st_context_iface * stctxi,struct st_context_iface * stsrci,unsigned mask)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
st_context_share(struct st_context_iface * stctxi,struct st_context_iface * stsrci)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
st_context_destroy(struct st_context_iface * stctxi)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 *
st_api_create_context(struct st_api * stapi,struct st_manager * smapi,const struct st_context_attribs * attribs,enum st_context_error * error,struct st_context_iface * shared_stctxi)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 }
658
659 if (attribs->flags & ST_CONTEXT_FLAG_ROBUST_ACCESS)
660 ctx_flags |= PIPE_CONTEXT_ROBUST_BUFFER_ACCESS;
661
662 pipe = smapi->screen->context_create(smapi->screen, NULL, ctx_flags);
663 if (!pipe) {
664 *error = ST_CONTEXT_ERROR_NO_MEMORY;
665 return NULL;
666 }
667
668 st_visual_to_context_mode(&attribs->visual, &mode);
669 st = st_create_context(api, pipe, &mode, shared_ctx, &attribs->options);
670 if (!st) {
671 *error = ST_CONTEXT_ERROR_NO_MEMORY;
672 pipe->destroy(pipe);
673 return NULL;
674 }
675
676 if (attribs->flags & ST_CONTEXT_FLAG_DEBUG) {
677 if (!_mesa_set_debug_state_int(st->ctx, GL_DEBUG_OUTPUT, GL_TRUE)) {
678 *error = ST_CONTEXT_ERROR_NO_MEMORY;
679 return NULL;
680 }
681
682 st->ctx->Const.ContextFlags |= GL_CONTEXT_FLAG_DEBUG_BIT;
683 }
684
685 if (st->ctx->Const.ContextFlags & GL_CONTEXT_FLAG_DEBUG_BIT) {
686 st_update_debug_callback(st);
687 }
688
689 if (attribs->flags & ST_CONTEXT_FLAG_FORWARD_COMPATIBLE)
690 st->ctx->Const.ContextFlags |= GL_CONTEXT_FLAG_FORWARD_COMPATIBLE_BIT;
691 if (attribs->flags & ST_CONTEXT_FLAG_ROBUST_ACCESS) {
692 st->ctx->Const.ContextFlags |= GL_CONTEXT_FLAG_ROBUST_ACCESS_BIT_ARB;
693 st->ctx->Const.RobustAccess = GL_TRUE;
694 }
695 if (attribs->flags & ST_CONTEXT_FLAG_RESET_NOTIFICATION_ENABLED) {
696 st->ctx->Const.ResetStrategy = GL_LOSE_CONTEXT_ON_RESET_ARB;
697 st_install_device_reset_callback(st);
698 }
699
700 /* need to perform version check */
701 if (attribs->major > 1 || attribs->minor > 0) {
702 /* Is the actual version less than the requested version?
703 */
704 if (st->ctx->Version < attribs->major * 10U + attribs->minor) {
705 *error = ST_CONTEXT_ERROR_BAD_VERSION;
706 st_destroy_context(st);
707 return NULL;
708 }
709 }
710
711 st->invalidate_on_gl_viewport =
712 smapi->get_param(smapi, ST_MANAGER_BROKEN_INVALIDATE);
713
714 st->iface.destroy = st_context_destroy;
715 st->iface.flush = st_context_flush;
716 st->iface.teximage = st_context_teximage;
717 st->iface.copy = st_context_copy;
718 st->iface.share = st_context_share;
719 st->iface.st_context_private = (void *) smapi;
720 st->iface.cso_context = st->cso_context;
721 st->iface.pipe = st->pipe;
722
723 *error = ST_CONTEXT_SUCCESS;
724 return &st->iface;
725 }
726
727 static struct st_context_iface *
st_api_get_current(struct st_api * stapi)728 st_api_get_current(struct st_api *stapi)
729 {
730 GET_CURRENT_CONTEXT(ctx);
731 struct st_context *st = (ctx) ? ctx->st : NULL;
732
733 return (st) ? &st->iface : NULL;
734 }
735
736 static struct st_framebuffer *
st_framebuffer_reuse_or_create(struct st_context * st,struct gl_framebuffer * fb,struct st_framebuffer_iface * stfbi)737 st_framebuffer_reuse_or_create(struct st_context *st,
738 struct gl_framebuffer *fb,
739 struct st_framebuffer_iface *stfbi)
740 {
741 struct st_framebuffer *cur = st_ws_framebuffer(fb), *stfb = NULL;
742
743 /* dummy framebuffers cant be used as st_framebuffer */
744 if (cur && &cur->Base != _mesa_get_incomplete_framebuffer() &&
745 cur->iface == stfbi) {
746 /* reuse the current stfb */
747 st_framebuffer_reference(&stfb, cur);
748 }
749 else {
750 /* create a new one */
751 stfb = st_framebuffer_create(st, stfbi);
752 }
753
754 return stfb;
755 }
756
757 static boolean
st_api_make_current(struct st_api * stapi,struct st_context_iface * stctxi,struct st_framebuffer_iface * stdrawi,struct st_framebuffer_iface * streadi)758 st_api_make_current(struct st_api *stapi, struct st_context_iface *stctxi,
759 struct st_framebuffer_iface *stdrawi,
760 struct st_framebuffer_iface *streadi)
761 {
762 struct st_context *st = (struct st_context *) stctxi;
763 struct st_framebuffer *stdraw, *stread;
764 boolean ret;
765
766 _glapi_check_multithread();
767
768 if (st) {
769 /* reuse or create the draw fb */
770 stdraw = st_framebuffer_reuse_or_create(st,
771 st->ctx->WinSysDrawBuffer, stdrawi);
772 if (streadi != stdrawi) {
773 /* do the same for the read fb */
774 stread = st_framebuffer_reuse_or_create(st,
775 st->ctx->WinSysReadBuffer, streadi);
776 }
777 else {
778 stread = NULL;
779 /* reuse the draw fb for the read fb */
780 if (stdraw)
781 st_framebuffer_reference(&stread, stdraw);
782 }
783
784 if (stdraw && stread) {
785 st_framebuffer_validate(stdraw, st);
786 if (stread != stdraw)
787 st_framebuffer_validate(stread, st);
788
789 ret = _mesa_make_current(st->ctx, &stdraw->Base, &stread->Base);
790
791 st->draw_stamp = stdraw->stamp - 1;
792 st->read_stamp = stread->stamp - 1;
793 st_context_validate(st, stdraw, stread);
794 }
795 else {
796 struct gl_framebuffer *incomplete = _mesa_get_incomplete_framebuffer();
797 ret = _mesa_make_current(st->ctx, incomplete, incomplete);
798 }
799
800 st_framebuffer_reference(&stdraw, NULL);
801 st_framebuffer_reference(&stread, NULL);
802 }
803 else {
804 ret = _mesa_make_current(NULL, NULL, NULL);
805 }
806
807 return ret;
808 }
809
810 static void
st_api_destroy(struct st_api * stapi)811 st_api_destroy(struct st_api *stapi)
812 {
813 }
814
815 /**
816 * Flush the front buffer if the current context renders to the front buffer.
817 */
818 void
st_manager_flush_frontbuffer(struct st_context * st)819 st_manager_flush_frontbuffer(struct st_context *st)
820 {
821 struct st_framebuffer *stfb = st_ws_framebuffer(st->ctx->DrawBuffer);
822 struct st_renderbuffer *strb = NULL;
823
824 if (stfb)
825 strb = st_renderbuffer(stfb->Base.Attachment[BUFFER_FRONT_LEFT].Renderbuffer);
826 if (!strb)
827 return;
828
829 /* never a dummy fb */
830 assert(&stfb->Base != _mesa_get_incomplete_framebuffer());
831 stfb->iface->flush_front(&st->iface, stfb->iface, ST_ATTACHMENT_FRONT_LEFT);
832 }
833
834 /**
835 * Return the surface of an EGLImage.
836 * FIXME: I think this should operate on resources, not surfaces
837 */
838 struct pipe_surface *
st_manager_get_egl_image_surface(struct st_context * st,void * eglimg)839 st_manager_get_egl_image_surface(struct st_context *st, void *eglimg)
840 {
841 struct st_manager *smapi =
842 (struct st_manager *) st->iface.st_context_private;
843 struct st_egl_image stimg;
844 struct pipe_surface *ps, surf_tmpl;
845
846 if (!smapi || !smapi->get_egl_image)
847 return NULL;
848
849 memset(&stimg, 0, sizeof(stimg));
850 if (!smapi->get_egl_image(smapi, eglimg, &stimg))
851 return NULL;
852
853 u_surface_default_template(&surf_tmpl, stimg.texture);
854 surf_tmpl.format = stimg.format;
855 surf_tmpl.u.tex.level = stimg.level;
856 surf_tmpl.u.tex.first_layer = stimg.layer;
857 surf_tmpl.u.tex.last_layer = stimg.layer;
858 ps = st->pipe->create_surface(st->pipe, stimg.texture, &surf_tmpl);
859 pipe_resource_reference(&stimg.texture, NULL);
860
861 return ps;
862 }
863
864 /**
865 * Re-validate the framebuffers.
866 */
867 void
st_manager_validate_framebuffers(struct st_context * st)868 st_manager_validate_framebuffers(struct st_context *st)
869 {
870 struct st_framebuffer *stdraw = st_ws_framebuffer(st->ctx->DrawBuffer);
871 struct st_framebuffer *stread = st_ws_framebuffer(st->ctx->ReadBuffer);
872
873 if (stdraw)
874 st_framebuffer_validate(stdraw, st);
875 if (stread && stread != stdraw)
876 st_framebuffer_validate(stread, st);
877
878 st_context_validate(st, stdraw, stread);
879 }
880
881 /**
882 * Add a color renderbuffer on demand.
883 */
884 boolean
st_manager_add_color_renderbuffer(struct st_context * st,struct gl_framebuffer * fb,gl_buffer_index idx)885 st_manager_add_color_renderbuffer(struct st_context *st,
886 struct gl_framebuffer *fb,
887 gl_buffer_index idx)
888 {
889 struct st_framebuffer *stfb = st_ws_framebuffer(fb);
890
891 /* FBO */
892 if (!stfb)
893 return FALSE;
894
895 if (stfb->Base.Attachment[idx].Renderbuffer)
896 return TRUE;
897
898 switch (idx) {
899 case BUFFER_FRONT_LEFT:
900 case BUFFER_BACK_LEFT:
901 case BUFFER_FRONT_RIGHT:
902 case BUFFER_BACK_RIGHT:
903 break;
904 default:
905 return FALSE;
906 }
907
908 if (!st_framebuffer_add_renderbuffer(stfb, idx))
909 return FALSE;
910
911 st_framebuffer_update_attachments(stfb);
912
913 /*
914 * Force a call to the state tracker manager to validate the
915 * new renderbuffer. It might be that there is a window system
916 * renderbuffer available.
917 */
918 if(stfb->iface)
919 stfb->iface_stamp = p_atomic_read(&stfb->iface->stamp) - 1;
920
921 st_invalidate_state(st->ctx, _NEW_BUFFERS);
922
923 return TRUE;
924 }
925
get_version(struct pipe_screen * screen,struct st_config_options * options,gl_api api)926 static unsigned get_version(struct pipe_screen *screen,
927 struct st_config_options *options, gl_api api)
928 {
929 struct gl_constants consts = {0};
930 struct gl_extensions extensions = {0};
931 GLuint version;
932
933 if (_mesa_override_gl_version_contextless(&consts, &api, &version)) {
934 return version;
935 }
936
937 _mesa_init_constants(&consts, api);
938 _mesa_init_extensions(&extensions);
939
940 st_init_limits(screen, &consts, &extensions);
941 st_init_extensions(screen, &consts, &extensions, options, GL_TRUE);
942
943 return _mesa_get_version(&extensions, &consts, api);
944 }
945
946 static void
st_api_query_versions(struct st_api * stapi,struct st_manager * sm,struct st_config_options * options,int * gl_core_version,int * gl_compat_version,int * gl_es1_version,int * gl_es2_version)947 st_api_query_versions(struct st_api *stapi, struct st_manager *sm,
948 struct st_config_options *options,
949 int *gl_core_version,
950 int *gl_compat_version,
951 int *gl_es1_version,
952 int *gl_es2_version)
953 {
954 *gl_core_version = get_version(sm->screen, options, API_OPENGL_CORE);
955 *gl_compat_version = get_version(sm->screen, options, API_OPENGL_COMPAT);
956 *gl_es1_version = get_version(sm->screen, options, API_OPENGLES);
957 *gl_es2_version = get_version(sm->screen, options, API_OPENGLES2);
958 }
959
960 static const struct st_api st_gl_api = {
961 .name = "Mesa " PACKAGE_VERSION,
962 .api = ST_API_OPENGL,
963 .profile_mask = ST_PROFILE_DEFAULT_MASK |
964 ST_PROFILE_OPENGL_CORE_MASK |
965 ST_PROFILE_OPENGL_ES1_MASK |
966 ST_PROFILE_OPENGL_ES2_MASK |
967 0,
968 .feature_mask = ST_API_FEATURE_MS_VISUALS_MASK,
969 .destroy = st_api_destroy,
970 .query_versions = st_api_query_versions,
971 .create_context = st_api_create_context,
972 .make_current = st_api_make_current,
973 .get_current = st_api_get_current,
974 };
975
976 struct st_api *
st_gl_api_create(void)977 st_gl_api_create(void)
978 {
979 return (struct st_api *) &st_gl_api;
980 }
981