eebde6218371b64a564b1a359d0c91d7612a21a2
[mesa.git] / src / mesa / state_tracker / st_manager.c
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 2010 LunarG Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 * Chia-I Wu <olv@lunarg.com>
26 */
27
28 #include "main/mtypes.h"
29 #include "main/extensions.h"
30 #include "main/context.h"
31 #include "main/debug_output.h"
32 #include "main/glthread.h"
33 #include "main/texobj.h"
34 #include "main/teximage.h"
35 #include "main/texstate.h"
36 #include "main/errors.h"
37 #include "main/framebuffer.h"
38 #include "main/fbobject.h"
39 #include "main/renderbuffer.h"
40 #include "main/version.h"
41 #include "util/hash_table.h"
42 #include "st_texture.h"
43
44 #include "st_context.h"
45 #include "st_debug.h"
46 #include "st_extensions.h"
47 #include "st_format.h"
48 #include "st_cb_fbo.h"
49 #include "st_cb_flush.h"
50 #include "st_manager.h"
51
52 #include "state_tracker/st_gl_api.h"
53
54 #include "pipe/p_context.h"
55 #include "pipe/p_screen.h"
56 #include "util/u_format.h"
57 #include "util/u_pointer.h"
58 #include "util/u_inlines.h"
59 #include "util/u_atomic.h"
60 #include "util/u_surface.h"
61 #include "util/list.h"
62
63 struct hash_table;
64 struct st_manager_private
65 {
66 struct hash_table *stfbi_ht; /* framebuffer iface objects hash table */
67 mtx_t st_mutex;
68 };
69
70 static void st_manager_destroy(struct st_manager *);
71
72 /**
73 * Map an attachment to a buffer index.
74 */
75 static inline gl_buffer_index
76 attachment_to_buffer_index(enum st_attachment_type statt)
77 {
78 gl_buffer_index index;
79
80 switch (statt) {
81 case ST_ATTACHMENT_FRONT_LEFT:
82 index = BUFFER_FRONT_LEFT;
83 break;
84 case ST_ATTACHMENT_BACK_LEFT:
85 index = BUFFER_BACK_LEFT;
86 break;
87 case ST_ATTACHMENT_FRONT_RIGHT:
88 index = BUFFER_FRONT_RIGHT;
89 break;
90 case ST_ATTACHMENT_BACK_RIGHT:
91 index = BUFFER_BACK_RIGHT;
92 break;
93 case ST_ATTACHMENT_DEPTH_STENCIL:
94 index = BUFFER_DEPTH;
95 break;
96 case ST_ATTACHMENT_ACCUM:
97 index = BUFFER_ACCUM;
98 break;
99 case ST_ATTACHMENT_SAMPLE:
100 default:
101 index = BUFFER_COUNT;
102 break;
103 }
104
105 return index;
106 }
107
108 /**
109 * Map a buffer index to an attachment.
110 */
111 static inline enum st_attachment_type
112 buffer_index_to_attachment(gl_buffer_index index)
113 {
114 enum st_attachment_type statt;
115
116 switch (index) {
117 case BUFFER_FRONT_LEFT:
118 statt = ST_ATTACHMENT_FRONT_LEFT;
119 break;
120 case BUFFER_BACK_LEFT:
121 statt = ST_ATTACHMENT_BACK_LEFT;
122 break;
123 case BUFFER_FRONT_RIGHT:
124 statt = ST_ATTACHMENT_FRONT_RIGHT;
125 break;
126 case BUFFER_BACK_RIGHT:
127 statt = ST_ATTACHMENT_BACK_RIGHT;
128 break;
129 case BUFFER_DEPTH:
130 statt = ST_ATTACHMENT_DEPTH_STENCIL;
131 break;
132 case BUFFER_ACCUM:
133 statt = ST_ATTACHMENT_ACCUM;
134 break;
135 default:
136 statt = ST_ATTACHMENT_INVALID;
137 break;
138 }
139
140 return statt;
141 }
142
143 /**
144 * Make sure a context picks up the latest cached state of the
145 * drawables it binds to.
146 */
147 static void
148 st_context_validate(struct st_context *st,
149 struct st_framebuffer *stdraw,
150 struct st_framebuffer *stread)
151 {
152 if (stdraw && stdraw->stamp != st->draw_stamp) {
153 st->dirty |= ST_NEW_FRAMEBUFFER;
154 _mesa_resize_framebuffer(st->ctx, &stdraw->Base,
155 stdraw->Base.Width,
156 stdraw->Base.Height);
157 st->draw_stamp = stdraw->stamp;
158 }
159
160 if (stread && stread->stamp != st->read_stamp) {
161 if (stread != stdraw) {
162 st->dirty |= ST_NEW_FRAMEBUFFER;
163 _mesa_resize_framebuffer(st->ctx, &stread->Base,
164 stread->Base.Width,
165 stread->Base.Height);
166 }
167 st->read_stamp = stread->stamp;
168 }
169 }
170
171 /**
172 * Validate a framebuffer to make sure up-to-date pipe_textures are used.
173 * The context is only used for creating pipe surfaces and for calling
174 * _mesa_resize_framebuffer().
175 * (That should probably be rethought, since those surfaces become
176 * drawable state, not context state, and can be freed by another pipe
177 * context).
178 */
179 static void
180 st_framebuffer_validate(struct st_framebuffer *stfb,
181 struct st_context *st)
182 {
183 struct pipe_resource *textures[ST_ATTACHMENT_COUNT];
184 uint width, height;
185 unsigned i;
186 boolean changed = FALSE;
187 int32_t new_stamp;
188
189 new_stamp = p_atomic_read(&stfb->iface->stamp);
190 if (stfb->iface_stamp == new_stamp)
191 return;
192
193 memset(textures, 0, stfb->num_statts * sizeof(textures[0]));
194
195 /* validate the fb */
196 do {
197 if (!stfb->iface->validate(&st->iface, stfb->iface, stfb->statts,
198 stfb->num_statts, textures))
199 return;
200
201 stfb->iface_stamp = new_stamp;
202 new_stamp = p_atomic_read(&stfb->iface->stamp);
203 } while(stfb->iface_stamp != new_stamp);
204
205 width = stfb->Base.Width;
206 height = stfb->Base.Height;
207
208 for (i = 0; i < stfb->num_statts; i++) {
209 struct st_renderbuffer *strb;
210 struct pipe_surface *ps, surf_tmpl;
211 gl_buffer_index idx;
212
213 if (!textures[i])
214 continue;
215
216 idx = attachment_to_buffer_index(stfb->statts[i]);
217 if (idx >= BUFFER_COUNT) {
218 pipe_resource_reference(&textures[i], NULL);
219 continue;
220 }
221
222 strb = st_renderbuffer(stfb->Base.Attachment[idx].Renderbuffer);
223 assert(strb);
224 if (strb->texture == textures[i]) {
225 pipe_resource_reference(&textures[i], NULL);
226 continue;
227 }
228
229 u_surface_default_template(&surf_tmpl, textures[i]);
230 ps = st->pipe->create_surface(st->pipe, textures[i], &surf_tmpl);
231 if (ps) {
232 struct pipe_surface **psurf =
233 util_format_is_srgb(ps->format) ? &strb->surface_srgb :
234 &strb->surface_linear;
235
236 pipe_surface_reference(psurf, ps);
237 strb->surface = *psurf;
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. The framebuffer is one that
287 * corresponds to a window and is not a user-created FBO.
288 */
289 static boolean
290 st_framebuffer_add_renderbuffer(struct st_framebuffer *stfb,
291 gl_buffer_index idx)
292 {
293 struct gl_renderbuffer *rb;
294 enum pipe_format format;
295 boolean sw;
296
297 assert(_mesa_is_winsys_fbo(&stfb->Base));
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_attach_and_own_rb(&stfb->Base, idx, rb);
329 return TRUE;
330 }
331
332 bool rb_ownership_taken = false;
333 if (util_format_get_component_bits(format, UTIL_FORMAT_COLORSPACE_ZS, 0)) {
334 _mesa_attach_and_own_rb(&stfb->Base, BUFFER_DEPTH, rb);
335 rb_ownership_taken = true;
336 }
337
338 if (util_format_get_component_bits(format, UTIL_FORMAT_COLORSPACE_ZS, 1)) {
339 if (rb_ownership_taken)
340 _mesa_attach_and_reference_rb(&stfb->Base, BUFFER_STENCIL, rb);
341 else
342 _mesa_attach_and_own_rb(&stfb->Base, BUFFER_STENCIL, rb);
343 }
344
345 return TRUE;
346 }
347
348 /**
349 * Intialize a struct gl_config from a visual.
350 */
351 static void
352 st_visual_to_context_mode(const struct st_visual *visual,
353 struct gl_config *mode)
354 {
355 memset(mode, 0, sizeof(*mode));
356
357 if (st_visual_have_buffers(visual, ST_ATTACHMENT_BACK_LEFT_MASK))
358 mode->doubleBufferMode = GL_TRUE;
359 if (st_visual_have_buffers(visual,
360 ST_ATTACHMENT_FRONT_RIGHT_MASK | ST_ATTACHMENT_BACK_RIGHT_MASK))
361 mode->stereoMode = GL_TRUE;
362
363 if (visual->color_format != PIPE_FORMAT_NONE) {
364 mode->rgbMode = GL_TRUE;
365
366 mode->redBits =
367 util_format_get_component_bits(visual->color_format,
368 UTIL_FORMAT_COLORSPACE_RGB, 0);
369 mode->greenBits =
370 util_format_get_component_bits(visual->color_format,
371 UTIL_FORMAT_COLORSPACE_RGB, 1);
372 mode->blueBits =
373 util_format_get_component_bits(visual->color_format,
374 UTIL_FORMAT_COLORSPACE_RGB, 2);
375 mode->alphaBits =
376 util_format_get_component_bits(visual->color_format,
377 UTIL_FORMAT_COLORSPACE_RGB, 3);
378
379 mode->rgbBits = mode->redBits +
380 mode->greenBits + mode->blueBits + mode->alphaBits;
381 mode->sRGBCapable = util_format_is_srgb(visual->color_format);
382 }
383
384 if (visual->depth_stencil_format != PIPE_FORMAT_NONE) {
385 mode->depthBits =
386 util_format_get_component_bits(visual->depth_stencil_format,
387 UTIL_FORMAT_COLORSPACE_ZS, 0);
388 mode->stencilBits =
389 util_format_get_component_bits(visual->depth_stencil_format,
390 UTIL_FORMAT_COLORSPACE_ZS, 1);
391
392 mode->haveDepthBuffer = mode->depthBits > 0;
393 mode->haveStencilBuffer = mode->stencilBits > 0;
394 }
395
396 if (visual->accum_format != PIPE_FORMAT_NONE) {
397 mode->haveAccumBuffer = GL_TRUE;
398
399 mode->accumRedBits =
400 util_format_get_component_bits(visual->accum_format,
401 UTIL_FORMAT_COLORSPACE_RGB, 0);
402 mode->accumGreenBits =
403 util_format_get_component_bits(visual->accum_format,
404 UTIL_FORMAT_COLORSPACE_RGB, 1);
405 mode->accumBlueBits =
406 util_format_get_component_bits(visual->accum_format,
407 UTIL_FORMAT_COLORSPACE_RGB, 2);
408 mode->accumAlphaBits =
409 util_format_get_component_bits(visual->accum_format,
410 UTIL_FORMAT_COLORSPACE_RGB, 3);
411 }
412
413 if (visual->samples > 1) {
414 mode->sampleBuffers = 1;
415 mode->samples = visual->samples;
416 }
417 }
418
419 /**
420 * Create a framebuffer from a manager interface.
421 */
422 static struct st_framebuffer *
423 st_framebuffer_create(struct st_context *st,
424 struct st_framebuffer_iface *stfbi)
425 {
426 struct st_framebuffer *stfb;
427 struct gl_config mode;
428 gl_buffer_index idx;
429
430 if (!stfbi)
431 return NULL;
432
433 stfb = CALLOC_STRUCT(st_framebuffer);
434 if (!stfb)
435 return NULL;
436
437 st_visual_to_context_mode(stfbi->visual, &mode);
438
439 /*
440 * For desktop GL, sRGB framebuffer write is controlled by both the
441 * capability of the framebuffer and GL_FRAMEBUFFER_SRGB. We should
442 * advertise the capability when the pipe driver (and core Mesa) supports
443 * it so that applications can enable sRGB write when they want to.
444 *
445 * This is not to be confused with GLX_FRAMEBUFFER_SRGB_CAPABLE_ARB. When
446 * the attribute is GLX_TRUE, it tells the st manager to pick a color
447 * format such that util_format_srgb(visual->color_format) can be supported
448 * by the pipe driver. We still need to advertise the capability here.
449 *
450 * For GLES, however, sRGB framebuffer write is controlled only by the
451 * capability of the framebuffer. There is GL_EXT_sRGB_write_control to
452 * give applications the control back, but sRGB write is still enabled by
453 * default. To avoid unexpected results, we should not advertise the
454 * capability. This could change when we add support for
455 * EGL_KHR_gl_colorspace.
456 */
457 if (_mesa_is_desktop_gl(st->ctx)) {
458 struct pipe_screen *screen = st->pipe->screen;
459 const enum pipe_format srgb_format =
460 util_format_srgb(stfbi->visual->color_format);
461
462 if (srgb_format != PIPE_FORMAT_NONE &&
463 st_pipe_format_to_mesa_format(srgb_format) != MESA_FORMAT_NONE &&
464 screen->is_format_supported(screen, srgb_format,
465 PIPE_TEXTURE_2D, stfbi->visual->samples,
466 (PIPE_BIND_DISPLAY_TARGET |
467 PIPE_BIND_RENDER_TARGET)))
468 mode.sRGBCapable = GL_TRUE;
469 }
470
471 _mesa_initialize_window_framebuffer(&stfb->Base, &mode);
472
473 stfb->iface = stfbi;
474 stfb->iface_ID = stfbi->ID;
475 stfb->iface_stamp = p_atomic_read(&stfbi->stamp) - 1;
476
477 /* add the color buffer */
478 idx = stfb->Base._ColorDrawBufferIndexes[0];
479 if (!st_framebuffer_add_renderbuffer(stfb, idx)) {
480 free(stfb);
481 return NULL;
482 }
483
484 st_framebuffer_add_renderbuffer(stfb, BUFFER_DEPTH);
485 st_framebuffer_add_renderbuffer(stfb, BUFFER_ACCUM);
486
487 stfb->stamp = 0;
488 st_framebuffer_update_attachments(stfb);
489
490 return stfb;
491 }
492
493 /**
494 * Reference a framebuffer.
495 */
496 void
497 st_framebuffer_reference(struct st_framebuffer **ptr,
498 struct st_framebuffer *stfb)
499 {
500 struct gl_framebuffer *fb = &stfb->Base;
501 _mesa_reference_framebuffer((struct gl_framebuffer **) ptr, fb);
502 }
503
504
505 static uint32_t
506 st_framebuffer_iface_hash(const void *key)
507 {
508 return (uintptr_t)key;
509 }
510
511
512 static bool
513 st_framebuffer_iface_equal(const void *a, const void *b)
514 {
515 return (struct st_framebuffer_iface *)a == (struct st_framebuffer_iface *)b;
516 }
517
518
519 static boolean
520 st_framebuffer_iface_lookup(struct st_manager *smapi,
521 const struct st_framebuffer_iface *stfbi)
522 {
523 struct st_manager_private *smPriv =
524 (struct st_manager_private *)smapi->st_manager_private;
525 struct hash_entry *entry;
526
527 assert(smPriv);
528 assert(smPriv->stfbi_ht);
529
530 mtx_lock(&smPriv->st_mutex);
531 entry = _mesa_hash_table_search(smPriv->stfbi_ht, stfbi);
532 mtx_unlock(&smPriv->st_mutex);
533
534 return entry != NULL;
535 }
536
537
538 static boolean
539 st_framebuffer_iface_insert(struct st_manager *smapi,
540 struct st_framebuffer_iface *stfbi)
541 {
542 struct st_manager_private *smPriv =
543 (struct st_manager_private *)smapi->st_manager_private;
544 struct hash_entry *entry;
545
546 assert(smPriv);
547 assert(smPriv->stfbi_ht);
548
549 mtx_lock(&smPriv->st_mutex);
550 entry = _mesa_hash_table_insert(smPriv->stfbi_ht, stfbi, stfbi);
551 mtx_unlock(&smPriv->st_mutex);
552
553 return entry != NULL;
554 }
555
556
557 static void
558 st_framebuffer_iface_remove(struct st_manager *smapi,
559 struct st_framebuffer_iface *stfbi)
560 {
561 struct st_manager_private *smPriv =
562 (struct st_manager_private *)smapi->st_manager_private;
563 struct hash_entry *entry;
564
565 if (!smPriv || !smPriv->stfbi_ht)
566 return;
567
568 mtx_lock(&smPriv->st_mutex);
569 entry = _mesa_hash_table_search(smPriv->stfbi_ht, stfbi);
570 if (!entry)
571 goto unlock;
572
573 _mesa_hash_table_remove(smPriv->stfbi_ht, entry);
574
575 unlock:
576 mtx_unlock(&smPriv->st_mutex);
577 }
578
579
580 /**
581 * The framebuffer interface object is no longer valid.
582 * Remove the object from the framebuffer interface hash table.
583 */
584 static void
585 st_api_destroy_drawable(struct st_api *stapi,
586 struct st_framebuffer_iface *stfbi)
587 {
588 if (!stfbi)
589 return;
590
591 st_framebuffer_iface_remove(stfbi->state_manager, stfbi);
592 }
593
594
595 /**
596 * Purge the winsys buffers list to remove any references to
597 * non-existing framebuffer interface objects.
598 */
599 static void
600 st_framebuffers_purge(struct st_context *st)
601 {
602 struct st_context_iface *st_iface = &st->iface;
603 struct st_manager *smapi = st_iface->state_manager;
604 struct st_framebuffer *stfb, *next;
605
606 assert(smapi);
607
608 LIST_FOR_EACH_ENTRY_SAFE_REV(stfb, next, &st->winsys_buffers, head) {
609 struct st_framebuffer_iface *stfbi = stfb->iface;
610
611 assert(stfbi);
612
613 /**
614 * If the corresponding framebuffer interface object no longer exists,
615 * remove the framebuffer object from the context's winsys buffers list,
616 * and unreference the framebuffer object, so its resources can be
617 * deleted.
618 */
619 if (!st_framebuffer_iface_lookup(smapi, stfbi)) {
620 LIST_DEL(&stfb->head);
621 st_framebuffer_reference(&stfb, NULL);
622 }
623 }
624 }
625
626 static void
627 st_context_flush(struct st_context_iface *stctxi, unsigned flags,
628 struct pipe_fence_handle **fence)
629 {
630 struct st_context *st = (struct st_context *) stctxi;
631 unsigned pipe_flags = 0;
632
633 if (flags & ST_FLUSH_END_OF_FRAME) {
634 pipe_flags |= PIPE_FLUSH_END_OF_FRAME;
635 }
636
637 st_flush(st, fence, pipe_flags);
638
639 if ((flags & ST_FLUSH_WAIT) && fence && *fence) {
640 st->pipe->screen->fence_finish(st->pipe->screen, NULL, *fence,
641 PIPE_TIMEOUT_INFINITE);
642 st->pipe->screen->fence_reference(st->pipe->screen, fence, NULL);
643 }
644
645 if (flags & ST_FLUSH_FRONT)
646 st_manager_flush_frontbuffer(st);
647
648 /* DRI3 changes the framebuffer after SwapBuffers, but we need to invoke
649 * st_manager_validate_framebuffers to notice that.
650 *
651 * Set gfx_shaders_may_be_dirty to invoke st_validate_state in the next
652 * draw call, which will invoke st_manager_validate_framebuffers, but it
653 * won't dirty states if there is no change.
654 */
655 if (flags & ST_FLUSH_END_OF_FRAME)
656 st->gfx_shaders_may_be_dirty = true;
657 }
658
659 static boolean
660 st_context_teximage(struct st_context_iface *stctxi,
661 enum st_texture_type tex_type,
662 int level, enum pipe_format pipe_format,
663 struct pipe_resource *tex, boolean mipmap)
664 {
665 struct st_context *st = (struct st_context *) stctxi;
666 struct gl_context *ctx = st->ctx;
667 struct gl_texture_object *texObj;
668 struct gl_texture_image *texImage;
669 struct st_texture_object *stObj;
670 struct st_texture_image *stImage;
671 GLenum internalFormat;
672 GLuint width, height, depth;
673 GLenum target;
674
675 switch (tex_type) {
676 case ST_TEXTURE_1D:
677 target = GL_TEXTURE_1D;
678 break;
679 case ST_TEXTURE_2D:
680 target = GL_TEXTURE_2D;
681 break;
682 case ST_TEXTURE_3D:
683 target = GL_TEXTURE_3D;
684 break;
685 case ST_TEXTURE_RECT:
686 target = GL_TEXTURE_RECTANGLE_ARB;
687 break;
688 default:
689 return FALSE;
690 }
691
692 texObj = _mesa_get_current_tex_object(ctx, target);
693
694 _mesa_lock_texture(ctx, texObj);
695
696 stObj = st_texture_object(texObj);
697 /* switch to surface based */
698 if (!stObj->surface_based) {
699 _mesa_clear_texture_object(ctx, texObj, NULL);
700 stObj->surface_based = GL_TRUE;
701 }
702
703 texImage = _mesa_get_tex_image(ctx, texObj, target, level);
704 stImage = st_texture_image(texImage);
705 if (tex) {
706 mesa_format texFormat = st_pipe_format_to_mesa_format(pipe_format);
707
708 if (util_format_has_alpha(tex->format))
709 internalFormat = GL_RGBA;
710 else
711 internalFormat = GL_RGB;
712
713 _mesa_init_teximage_fields(ctx, texImage,
714 tex->width0, tex->height0, 1, 0,
715 internalFormat, texFormat);
716
717 width = tex->width0;
718 height = tex->height0;
719 depth = tex->depth0;
720
721 /* grow the image size until we hit level = 0 */
722 while (level > 0) {
723 if (width != 1)
724 width <<= 1;
725 if (height != 1)
726 height <<= 1;
727 if (depth != 1)
728 depth <<= 1;
729 level--;
730 }
731 }
732 else {
733 _mesa_clear_texture_image(ctx, texImage);
734 width = height = depth = 0;
735 }
736
737 pipe_resource_reference(&stImage->pt, tex);
738 stObj->surface_format = pipe_format;
739
740 stObj->needs_validation = true;
741
742 _mesa_dirty_texobj(ctx, texObj);
743 _mesa_unlock_texture(ctx, texObj);
744
745 return TRUE;
746 }
747
748 static void
749 st_context_copy(struct st_context_iface *stctxi,
750 struct st_context_iface *stsrci, unsigned mask)
751 {
752 struct st_context *st = (struct st_context *) stctxi;
753 struct st_context *src = (struct st_context *) stsrci;
754
755 _mesa_copy_context(src->ctx, st->ctx, mask);
756 }
757
758 static boolean
759 st_context_share(struct st_context_iface *stctxi,
760 struct st_context_iface *stsrci)
761 {
762 struct st_context *st = (struct st_context *) stctxi;
763 struct st_context *src = (struct st_context *) stsrci;
764
765 return _mesa_share_state(st->ctx, src->ctx);
766 }
767
768 static void
769 st_context_destroy(struct st_context_iface *stctxi)
770 {
771 struct st_context *st = (struct st_context *) stctxi;
772 st_destroy_context(st);
773 }
774
775 static void
776 st_start_thread(struct st_context_iface *stctxi)
777 {
778 struct st_context *st = (struct st_context *) stctxi;
779
780 _mesa_glthread_init(st->ctx);
781 }
782
783 static void
784 st_thread_finish(struct st_context_iface *stctxi)
785 {
786 struct st_context *st = (struct st_context *) stctxi;
787
788 _mesa_glthread_finish(st->ctx);
789 }
790
791 static struct st_context_iface *
792 st_api_create_context(struct st_api *stapi, struct st_manager *smapi,
793 const struct st_context_attribs *attribs,
794 enum st_context_error *error,
795 struct st_context_iface *shared_stctxi)
796 {
797 struct st_context *shared_ctx = (struct st_context *) shared_stctxi;
798 struct st_context *st;
799 struct pipe_context *pipe;
800 struct gl_config mode;
801 gl_api api;
802 bool no_error = false;
803 unsigned ctx_flags = PIPE_CONTEXT_PREFER_THREADED;
804
805 if (!(stapi->profile_mask & (1 << attribs->profile)))
806 return NULL;
807
808 switch (attribs->profile) {
809 case ST_PROFILE_DEFAULT:
810 api = API_OPENGL_COMPAT;
811 break;
812 case ST_PROFILE_OPENGL_ES1:
813 api = API_OPENGLES;
814 break;
815 case ST_PROFILE_OPENGL_ES2:
816 api = API_OPENGLES2;
817 break;
818 case ST_PROFILE_OPENGL_CORE:
819 api = API_OPENGL_CORE;
820 break;
821 default:
822 *error = ST_CONTEXT_ERROR_BAD_API;
823 return NULL;
824 }
825
826 /* Create a hash table for the framebuffer interface objects
827 * if it has not been created for this st manager.
828 */
829 if (smapi->st_manager_private == NULL) {
830 struct st_manager_private *smPriv;
831
832 smPriv = CALLOC_STRUCT(st_manager_private);
833 mtx_init(&smPriv->st_mutex, mtx_plain);
834 smPriv->stfbi_ht = _mesa_hash_table_create(NULL,
835 st_framebuffer_iface_hash,
836 st_framebuffer_iface_equal);
837 smapi->st_manager_private = smPriv;
838 smapi->destroy = st_manager_destroy;
839 }
840
841 if (attribs->flags & ST_CONTEXT_FLAG_ROBUST_ACCESS)
842 ctx_flags |= PIPE_CONTEXT_ROBUST_BUFFER_ACCESS;
843
844 if (attribs->flags & ST_CONTEXT_FLAG_NO_ERROR)
845 no_error = true;
846
847 pipe = smapi->screen->context_create(smapi->screen, NULL, ctx_flags);
848 if (!pipe) {
849 *error = ST_CONTEXT_ERROR_NO_MEMORY;
850 return NULL;
851 }
852
853 st_visual_to_context_mode(&attribs->visual, &mode);
854 st = st_create_context(api, pipe, &mode, shared_ctx, &attribs->options, no_error);
855 if (!st) {
856 *error = ST_CONTEXT_ERROR_NO_MEMORY;
857 pipe->destroy(pipe);
858 return NULL;
859 }
860
861 if (attribs->flags & ST_CONTEXT_FLAG_DEBUG) {
862 if (!_mesa_set_debug_state_int(st->ctx, GL_DEBUG_OUTPUT, GL_TRUE)) {
863 *error = ST_CONTEXT_ERROR_NO_MEMORY;
864 return NULL;
865 }
866
867 st->ctx->Const.ContextFlags |= GL_CONTEXT_FLAG_DEBUG_BIT;
868 }
869
870 if (st->ctx->Const.ContextFlags & GL_CONTEXT_FLAG_DEBUG_BIT) {
871 st_update_debug_callback(st);
872 }
873
874 if (attribs->flags & ST_CONTEXT_FLAG_FORWARD_COMPATIBLE)
875 st->ctx->Const.ContextFlags |= GL_CONTEXT_FLAG_FORWARD_COMPATIBLE_BIT;
876 if (attribs->flags & ST_CONTEXT_FLAG_ROBUST_ACCESS) {
877 st->ctx->Const.ContextFlags |= GL_CONTEXT_FLAG_ROBUST_ACCESS_BIT_ARB;
878 st->ctx->Const.RobustAccess = GL_TRUE;
879 }
880 if (attribs->flags & ST_CONTEXT_FLAG_RESET_NOTIFICATION_ENABLED) {
881 st->ctx->Const.ResetStrategy = GL_LOSE_CONTEXT_ON_RESET_ARB;
882 st_install_device_reset_callback(st);
883 }
884
885 /* need to perform version check */
886 if (attribs->major > 1 || attribs->minor > 0) {
887 /* Is the actual version less than the requested version?
888 */
889 if (st->ctx->Version < attribs->major * 10U + attribs->minor) {
890 *error = ST_CONTEXT_ERROR_BAD_VERSION;
891 st_destroy_context(st);
892 return NULL;
893 }
894 }
895
896 st->invalidate_on_gl_viewport =
897 smapi->get_param(smapi, ST_MANAGER_BROKEN_INVALIDATE);
898
899 st->iface.destroy = st_context_destroy;
900 st->iface.flush = st_context_flush;
901 st->iface.teximage = st_context_teximage;
902 st->iface.copy = st_context_copy;
903 st->iface.share = st_context_share;
904 st->iface.start_thread = st_start_thread;
905 st->iface.thread_finish = st_thread_finish;
906 st->iface.st_context_private = (void *) smapi;
907 st->iface.cso_context = st->cso_context;
908 st->iface.pipe = st->pipe;
909 st->iface.state_manager = smapi;
910
911 *error = ST_CONTEXT_SUCCESS;
912 return &st->iface;
913 }
914
915 static struct st_context_iface *
916 st_api_get_current(struct st_api *stapi)
917 {
918 GET_CURRENT_CONTEXT(ctx);
919 struct st_context *st = (ctx) ? ctx->st : NULL;
920
921 return (st) ? &st->iface : NULL;
922 }
923
924 static struct st_framebuffer *
925 st_framebuffer_reuse_or_create(struct st_context *st,
926 struct gl_framebuffer *fb,
927 struct st_framebuffer_iface *stfbi)
928 {
929 struct st_framebuffer *cur = NULL, *stfb = NULL;
930
931 if (!stfbi)
932 return NULL;
933
934 /* Check if there is already a framebuffer object for the specified
935 * framebuffer interface in this context. If there is one, use it.
936 */
937 LIST_FOR_EACH_ENTRY(cur, &st->winsys_buffers, head) {
938 if (cur->iface_ID == stfbi->ID) {
939 st_framebuffer_reference(&stfb, cur);
940 break;
941 }
942 }
943
944 /* If there is not already a framebuffer object, create one */
945 if (stfb == NULL) {
946 cur = st_framebuffer_create(st, stfbi);
947
948 if (cur) {
949 /* add the referenced framebuffer interface object to
950 * the framebuffer interface object hash table.
951 */
952 if (!st_framebuffer_iface_insert(stfbi->state_manager, stfbi)) {
953 st_framebuffer_reference(&cur, NULL);
954 return NULL;
955 }
956
957 /* add to the context's winsys buffers list */
958 LIST_ADD(&cur->head, &st->winsys_buffers);
959
960 st_framebuffer_reference(&stfb, cur);
961 }
962 }
963
964 return stfb;
965 }
966
967 static boolean
968 st_api_make_current(struct st_api *stapi, struct st_context_iface *stctxi,
969 struct st_framebuffer_iface *stdrawi,
970 struct st_framebuffer_iface *streadi)
971 {
972 struct st_context *st = (struct st_context *) stctxi;
973 struct st_framebuffer *stdraw, *stread;
974 boolean ret;
975
976 _glapi_check_multithread();
977
978 if (st) {
979 /* reuse or create the draw fb */
980 stdraw = st_framebuffer_reuse_or_create(st,
981 st->ctx->WinSysDrawBuffer, stdrawi);
982 if (streadi != stdrawi) {
983 /* do the same for the read fb */
984 stread = st_framebuffer_reuse_or_create(st,
985 st->ctx->WinSysReadBuffer, streadi);
986 }
987 else {
988 stread = NULL;
989 /* reuse the draw fb for the read fb */
990 if (stdraw)
991 st_framebuffer_reference(&stread, stdraw);
992 }
993
994 if (stdraw && stread) {
995 st_framebuffer_validate(stdraw, st);
996 if (stread != stdraw)
997 st_framebuffer_validate(stread, st);
998
999 ret = _mesa_make_current(st->ctx, &stdraw->Base, &stread->Base);
1000
1001 st->draw_stamp = stdraw->stamp - 1;
1002 st->read_stamp = stread->stamp - 1;
1003 st_context_validate(st, stdraw, stread);
1004 }
1005 else {
1006 struct gl_framebuffer *incomplete = _mesa_get_incomplete_framebuffer();
1007 ret = _mesa_make_current(st->ctx, incomplete, incomplete);
1008 }
1009
1010 st_framebuffer_reference(&stdraw, NULL);
1011 st_framebuffer_reference(&stread, NULL);
1012
1013 /* Purge the context's winsys_buffers list in case any
1014 * of the referenced drawables no longer exist.
1015 */
1016 st_framebuffers_purge(st);
1017 }
1018 else {
1019 ret = _mesa_make_current(NULL, NULL, NULL);
1020 }
1021
1022 return ret;
1023 }
1024
1025 static void
1026 st_api_destroy(struct st_api *stapi)
1027 {
1028 }
1029
1030 /**
1031 * Flush the front buffer if the current context renders to the front buffer.
1032 */
1033 void
1034 st_manager_flush_frontbuffer(struct st_context *st)
1035 {
1036 struct st_framebuffer *stfb = st_ws_framebuffer(st->ctx->DrawBuffer);
1037 struct st_renderbuffer *strb = NULL;
1038
1039 if (stfb)
1040 strb = st_renderbuffer(stfb->Base.Attachment[BUFFER_FRONT_LEFT].Renderbuffer);
1041
1042 /* Do we have a front color buffer and has it been drawn to since last
1043 * frontbuffer flush?
1044 */
1045 if (strb && strb->defined) {
1046 stfb->iface->flush_front(&st->iface, stfb->iface,
1047 ST_ATTACHMENT_FRONT_LEFT);
1048 strb->defined = GL_FALSE;
1049
1050 /* Trigger an update of strb->defined on next draw */
1051 st->dirty |= ST_NEW_FB_STATE;
1052 }
1053 }
1054
1055 /**
1056 * Re-validate the framebuffers.
1057 */
1058 void
1059 st_manager_validate_framebuffers(struct st_context *st)
1060 {
1061 struct st_framebuffer *stdraw = st_ws_framebuffer(st->ctx->DrawBuffer);
1062 struct st_framebuffer *stread = st_ws_framebuffer(st->ctx->ReadBuffer);
1063
1064 if (stdraw)
1065 st_framebuffer_validate(stdraw, st);
1066 if (stread && stread != stdraw)
1067 st_framebuffer_validate(stread, st);
1068
1069 st_context_validate(st, stdraw, stread);
1070 }
1071
1072
1073 /**
1074 * Flush any outstanding swapbuffers on the current draw framebuffer.
1075 */
1076 void
1077 st_manager_flush_swapbuffers(void)
1078 {
1079 GET_CURRENT_CONTEXT(ctx);
1080 struct st_context *st = (ctx) ? ctx->st : NULL;
1081 struct st_framebuffer *stfb;
1082
1083 if (!st)
1084 return;
1085
1086 stfb = st_ws_framebuffer(ctx->DrawBuffer);
1087 if (!stfb || !stfb->iface->flush_swapbuffers)
1088 return;
1089
1090 stfb->iface->flush_swapbuffers(&st->iface, stfb->iface);
1091 }
1092
1093
1094 /**
1095 * Add a color renderbuffer on demand. The FBO must correspond to a window,
1096 * not a user-created FBO.
1097 */
1098 boolean
1099 st_manager_add_color_renderbuffer(struct st_context *st,
1100 struct gl_framebuffer *fb,
1101 gl_buffer_index idx)
1102 {
1103 struct st_framebuffer *stfb = st_ws_framebuffer(fb);
1104
1105 /* FBO */
1106 if (!stfb)
1107 return FALSE;
1108
1109 assert(_mesa_is_winsys_fbo(fb));
1110
1111 if (stfb->Base.Attachment[idx].Renderbuffer)
1112 return TRUE;
1113
1114 switch (idx) {
1115 case BUFFER_FRONT_LEFT:
1116 case BUFFER_BACK_LEFT:
1117 case BUFFER_FRONT_RIGHT:
1118 case BUFFER_BACK_RIGHT:
1119 break;
1120 default:
1121 return FALSE;
1122 }
1123
1124 if (!st_framebuffer_add_renderbuffer(stfb, idx))
1125 return FALSE;
1126
1127 st_framebuffer_update_attachments(stfb);
1128
1129 /*
1130 * Force a call to the state tracker manager to validate the
1131 * new renderbuffer. It might be that there is a window system
1132 * renderbuffer available.
1133 */
1134 if (stfb->iface)
1135 stfb->iface_stamp = p_atomic_read(&stfb->iface->stamp) - 1;
1136
1137 st_invalidate_buffers(st);
1138
1139 return TRUE;
1140 }
1141
1142 static void
1143 st_manager_destroy(struct st_manager *smapi)
1144 {
1145 struct st_manager_private *smPriv = smapi->st_manager_private;
1146
1147 if (smPriv && smPriv->stfbi_ht) {
1148 _mesa_hash_table_destroy(smPriv->stfbi_ht, NULL);
1149 mtx_destroy(&smPriv->st_mutex);
1150 free(smPriv);
1151 smapi->st_manager_private = NULL;
1152 }
1153 }
1154
1155 static unsigned
1156 get_version(struct pipe_screen *screen,
1157 struct st_config_options *options, gl_api api)
1158 {
1159 struct gl_constants consts = {0};
1160 struct gl_extensions extensions = {0};
1161 GLuint version;
1162
1163 if (_mesa_override_gl_version_contextless(&consts, &api, &version)) {
1164 return version;
1165 }
1166
1167 _mesa_init_constants(&consts, api);
1168 _mesa_init_extensions(&extensions);
1169
1170 st_init_limits(screen, &consts, &extensions);
1171 st_init_extensions(screen, &consts, &extensions, options);
1172
1173 return _mesa_get_version(&extensions, &consts, api);
1174 }
1175
1176 static void
1177 st_api_query_versions(struct st_api *stapi, struct st_manager *sm,
1178 struct st_config_options *options,
1179 int *gl_core_version,
1180 int *gl_compat_version,
1181 int *gl_es1_version,
1182 int *gl_es2_version)
1183 {
1184 *gl_core_version = get_version(sm->screen, options, API_OPENGL_CORE);
1185 *gl_compat_version = get_version(sm->screen, options, API_OPENGL_COMPAT);
1186 *gl_es1_version = get_version(sm->screen, options, API_OPENGLES);
1187 *gl_es2_version = get_version(sm->screen, options, API_OPENGLES2);
1188 }
1189
1190 static const struct st_api st_gl_api = {
1191 .name = "Mesa " PACKAGE_VERSION,
1192 .api = ST_API_OPENGL,
1193 .profile_mask = ST_PROFILE_DEFAULT_MASK |
1194 ST_PROFILE_OPENGL_CORE_MASK |
1195 ST_PROFILE_OPENGL_ES1_MASK |
1196 ST_PROFILE_OPENGL_ES2_MASK |
1197 0,
1198 .feature_mask = ST_API_FEATURE_MS_VISUALS_MASK,
1199 .destroy = st_api_destroy,
1200 .query_versions = st_api_query_versions,
1201 .create_context = st_api_create_context,
1202 .make_current = st_api_make_current,
1203 .get_current = st_api_get_current,
1204 .destroy_drawable = st_api_destroy_drawable,
1205 };
1206
1207 struct st_api *
1208 st_gl_api_create(void)
1209 {
1210 return (struct st_api *) &st_gl_api;
1211 }