st/mesa: release sampler views when redefining a texture in st_context_teximage
[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 #include "st_sampler_view.h"
52
53 #include "state_tracker/st_gl_api.h"
54
55 #include "pipe/p_context.h"
56 #include "pipe/p_screen.h"
57 #include "util/u_format.h"
58 #include "util/u_pointer.h"
59 #include "util/u_inlines.h"
60 #include "util/u_atomic.h"
61 #include "util/u_surface.h"
62 #include "util/list.h"
63
64 struct hash_table;
65 struct st_manager_private
66 {
67 struct hash_table *stfbi_ht; /* framebuffer iface objects hash table */
68 mtx_t st_mutex;
69 };
70
71 static void st_manager_destroy(struct st_manager *);
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_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_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 new_stamp = p_atomic_read(&stfb->iface->stamp);
191 if (stfb->iface_stamp == new_stamp)
192 return;
193
194 /* validate the fb */
195 do {
196 if (!stfb->iface->validate(&st->iface, stfb->iface, stfb->statts,
197 stfb->num_statts, textures))
198 return;
199
200 stfb->iface_stamp = new_stamp;
201 new_stamp = p_atomic_read(&stfb->iface->stamp);
202 } while(stfb->iface_stamp != new_stamp);
203
204 width = stfb->Base.Width;
205 height = stfb->Base.Height;
206
207 for (i = 0; i < stfb->num_statts; i++) {
208 struct st_renderbuffer *strb;
209 struct pipe_surface *ps, surf_tmpl;
210 gl_buffer_index idx;
211
212 if (!textures[i])
213 continue;
214
215 idx = attachment_to_buffer_index(stfb->statts[i]);
216 if (idx >= BUFFER_COUNT) {
217 pipe_resource_reference(&textures[i], NULL);
218 continue;
219 }
220
221 strb = st_renderbuffer(stfb->Base.Attachment[idx].Renderbuffer);
222 assert(strb);
223 if (strb->texture == textures[i]) {
224 pipe_resource_reference(&textures[i], NULL);
225 continue;
226 }
227
228 u_surface_default_template(&surf_tmpl, textures[i]);
229 ps = st->pipe->create_surface(st->pipe, textures[i], &surf_tmpl);
230 if (ps) {
231 struct pipe_surface **psurf =
232 util_format_is_srgb(ps->format) ? &strb->surface_srgb :
233 &strb->surface_linear;
234
235 pipe_surface_reference(psurf, ps);
236 strb->surface = *psurf;
237 pipe_resource_reference(&strb->texture, ps->texture);
238 /* ownership transfered */
239 pipe_surface_reference(&ps, NULL);
240
241 changed = TRUE;
242
243 strb->Base.Width = strb->surface->width;
244 strb->Base.Height = strb->surface->height;
245
246 width = strb->Base.Width;
247 height = strb->Base.Height;
248 }
249
250 pipe_resource_reference(&textures[i], NULL);
251 }
252
253 if (changed) {
254 ++stfb->stamp;
255 _mesa_resize_framebuffer(st->ctx, &stfb->Base, width, height);
256 }
257 }
258
259 /**
260 * Update the attachments to validate by looping the existing renderbuffers.
261 */
262 static void
263 st_framebuffer_update_attachments(struct st_framebuffer *stfb)
264 {
265 gl_buffer_index idx;
266
267 stfb->num_statts = 0;
268 for (idx = 0; idx < BUFFER_COUNT; idx++) {
269 struct st_renderbuffer *strb;
270 enum st_attachment_type statt;
271
272 strb = st_renderbuffer(stfb->Base.Attachment[idx].Renderbuffer);
273 if (!strb || strb->software)
274 continue;
275
276 statt = buffer_index_to_attachment(idx);
277 if (statt != ST_ATTACHMENT_INVALID &&
278 st_visual_have_buffers(stfb->iface->visual, 1 << statt))
279 stfb->statts[stfb->num_statts++] = statt;
280 }
281 stfb->stamp++;
282 }
283
284 /**
285 * Add a renderbuffer to the framebuffer. The framebuffer is one that
286 * corresponds to a window and is not a user-created FBO.
287 */
288 static boolean
289 st_framebuffer_add_renderbuffer(struct st_framebuffer *stfb,
290 gl_buffer_index idx)
291 {
292 struct gl_renderbuffer *rb;
293 enum pipe_format format;
294 boolean sw;
295
296 assert(_mesa_is_winsys_fbo(&stfb->Base));
297
298 /* do not distinguish depth/stencil buffers */
299 if (idx == BUFFER_STENCIL)
300 idx = BUFFER_DEPTH;
301
302 switch (idx) {
303 case BUFFER_DEPTH:
304 format = stfb->iface->visual->depth_stencil_format;
305 sw = FALSE;
306 break;
307 case BUFFER_ACCUM:
308 format = stfb->iface->visual->accum_format;
309 sw = TRUE;
310 break;
311 default:
312 format = stfb->iface->visual->color_format;
313 if (stfb->Base.Visual.sRGBCapable)
314 format = util_format_srgb(format);
315 sw = FALSE;
316 break;
317 }
318
319 if (format == PIPE_FORMAT_NONE)
320 return FALSE;
321
322 rb = st_new_renderbuffer_fb(format, stfb->iface->visual->samples, sw);
323 if (!rb)
324 return FALSE;
325
326 if (idx != BUFFER_DEPTH) {
327 _mesa_attach_and_own_rb(&stfb->Base, idx, rb);
328 return TRUE;
329 }
330
331 bool rb_ownership_taken = false;
332 if (util_format_get_component_bits(format, UTIL_FORMAT_COLORSPACE_ZS, 0)) {
333 _mesa_attach_and_own_rb(&stfb->Base, BUFFER_DEPTH, rb);
334 rb_ownership_taken = true;
335 }
336
337 if (util_format_get_component_bits(format, UTIL_FORMAT_COLORSPACE_ZS, 1)) {
338 if (rb_ownership_taken)
339 _mesa_attach_and_reference_rb(&stfb->Base, BUFFER_STENCIL, rb);
340 else
341 _mesa_attach_and_own_rb(&stfb->Base, BUFFER_STENCIL, rb);
342 }
343
344 return TRUE;
345 }
346
347 /**
348 * Intialize a struct gl_config from a visual.
349 */
350 static void
351 st_visual_to_context_mode(const struct st_visual *visual,
352 struct gl_config *mode)
353 {
354 memset(mode, 0, sizeof(*mode));
355
356 if (st_visual_have_buffers(visual, ST_ATTACHMENT_BACK_LEFT_MASK))
357 mode->doubleBufferMode = GL_TRUE;
358 if (st_visual_have_buffers(visual,
359 ST_ATTACHMENT_FRONT_RIGHT_MASK | ST_ATTACHMENT_BACK_RIGHT_MASK))
360 mode->stereoMode = GL_TRUE;
361
362 if (visual->color_format != PIPE_FORMAT_NONE) {
363 mode->rgbMode = GL_TRUE;
364
365 mode->redBits =
366 util_format_get_component_bits(visual->color_format,
367 UTIL_FORMAT_COLORSPACE_RGB, 0);
368 mode->greenBits =
369 util_format_get_component_bits(visual->color_format,
370 UTIL_FORMAT_COLORSPACE_RGB, 1);
371 mode->blueBits =
372 util_format_get_component_bits(visual->color_format,
373 UTIL_FORMAT_COLORSPACE_RGB, 2);
374 mode->alphaBits =
375 util_format_get_component_bits(visual->color_format,
376 UTIL_FORMAT_COLORSPACE_RGB, 3);
377
378 mode->rgbBits = mode->redBits +
379 mode->greenBits + mode->blueBits + mode->alphaBits;
380 mode->sRGBCapable = util_format_is_srgb(visual->color_format);
381 }
382
383 if (visual->depth_stencil_format != PIPE_FORMAT_NONE) {
384 mode->depthBits =
385 util_format_get_component_bits(visual->depth_stencil_format,
386 UTIL_FORMAT_COLORSPACE_ZS, 0);
387 mode->stencilBits =
388 util_format_get_component_bits(visual->depth_stencil_format,
389 UTIL_FORMAT_COLORSPACE_ZS, 1);
390
391 mode->haveDepthBuffer = mode->depthBits > 0;
392 mode->haveStencilBuffer = mode->stencilBits > 0;
393 }
394
395 if (visual->accum_format != PIPE_FORMAT_NONE) {
396 mode->haveAccumBuffer = GL_TRUE;
397
398 mode->accumRedBits =
399 util_format_get_component_bits(visual->accum_format,
400 UTIL_FORMAT_COLORSPACE_RGB, 0);
401 mode->accumGreenBits =
402 util_format_get_component_bits(visual->accum_format,
403 UTIL_FORMAT_COLORSPACE_RGB, 1);
404 mode->accumBlueBits =
405 util_format_get_component_bits(visual->accum_format,
406 UTIL_FORMAT_COLORSPACE_RGB, 2);
407 mode->accumAlphaBits =
408 util_format_get_component_bits(visual->accum_format,
409 UTIL_FORMAT_COLORSPACE_RGB, 3);
410 }
411
412 if (visual->samples > 1) {
413 mode->sampleBuffers = 1;
414 mode->samples = visual->samples;
415 }
416 }
417
418 /**
419 * Create a framebuffer from a manager interface.
420 */
421 static struct st_framebuffer *
422 st_framebuffer_create(struct st_context *st,
423 struct st_framebuffer_iface *stfbi)
424 {
425 struct st_framebuffer *stfb;
426 struct gl_config mode;
427 gl_buffer_index idx;
428
429 if (!stfbi)
430 return NULL;
431
432 stfb = CALLOC_STRUCT(st_framebuffer);
433 if (!stfb)
434 return NULL;
435
436 st_visual_to_context_mode(stfbi->visual, &mode);
437
438 /*
439 * For desktop GL, sRGB framebuffer write is controlled by both the
440 * capability of the framebuffer and GL_FRAMEBUFFER_SRGB. We should
441 * advertise the capability when the pipe driver (and core Mesa) supports
442 * it so that applications can enable sRGB write when they want to.
443 *
444 * This is not to be confused with GLX_FRAMEBUFFER_SRGB_CAPABLE_ARB. When
445 * the attribute is GLX_TRUE, it tells the st manager to pick a color
446 * format such that util_format_srgb(visual->color_format) can be supported
447 * by the pipe driver. We still need to advertise the capability here.
448 *
449 * For GLES, however, sRGB framebuffer write is controlled only by the
450 * capability of the framebuffer. There is GL_EXT_sRGB_write_control to
451 * give applications the control back, but sRGB write is still enabled by
452 * default. To avoid unexpected results, we should not advertise the
453 * capability. This could change when we add support for
454 * EGL_KHR_gl_colorspace.
455 */
456 if (_mesa_is_desktop_gl(st->ctx)) {
457 struct pipe_screen *screen = st->pipe->screen;
458 const enum pipe_format srgb_format =
459 util_format_srgb(stfbi->visual->color_format);
460
461 if (srgb_format != PIPE_FORMAT_NONE &&
462 st_pipe_format_to_mesa_format(srgb_format) != MESA_FORMAT_NONE &&
463 screen->is_format_supported(screen, srgb_format,
464 PIPE_TEXTURE_2D, stfbi->visual->samples,
465 (PIPE_BIND_DISPLAY_TARGET |
466 PIPE_BIND_RENDER_TARGET)))
467 mode.sRGBCapable = GL_TRUE;
468 }
469
470 _mesa_initialize_window_framebuffer(&stfb->Base, &mode);
471
472 stfb->iface = stfbi;
473 stfb->iface_ID = stfbi->ID;
474 stfb->iface_stamp = p_atomic_read(&stfbi->stamp) - 1;
475
476 /* add the color buffer */
477 idx = stfb->Base._ColorDrawBufferIndexes[0];
478 if (!st_framebuffer_add_renderbuffer(stfb, idx)) {
479 free(stfb);
480 return NULL;
481 }
482
483 st_framebuffer_add_renderbuffer(stfb, BUFFER_DEPTH);
484 st_framebuffer_add_renderbuffer(stfb, BUFFER_ACCUM);
485
486 stfb->stamp = 0;
487 st_framebuffer_update_attachments(stfb);
488
489 return stfb;
490 }
491
492 /**
493 * Reference a framebuffer.
494 */
495 void
496 st_framebuffer_reference(struct st_framebuffer **ptr,
497 struct st_framebuffer *stfb)
498 {
499 struct gl_framebuffer *fb = &stfb->Base;
500 _mesa_reference_framebuffer((struct gl_framebuffer **) ptr, fb);
501 }
502
503
504 static uint32_t
505 st_framebuffer_iface_hash(const void *key)
506 {
507 return (uintptr_t)key;
508 }
509
510
511 static bool
512 st_framebuffer_iface_equal(const void *a, const void *b)
513 {
514 return (struct st_framebuffer_iface *)a == (struct st_framebuffer_iface *)b;
515 }
516
517
518 static boolean
519 st_framebuffer_iface_lookup(struct st_manager *smapi,
520 const struct st_framebuffer_iface *stfbi)
521 {
522 struct st_manager_private *smPriv =
523 (struct st_manager_private *)smapi->st_manager_private;
524 struct hash_entry *entry;
525
526 assert(smPriv);
527 assert(smPriv->stfbi_ht);
528
529 mtx_lock(&smPriv->st_mutex);
530 entry = _mesa_hash_table_search(smPriv->stfbi_ht, stfbi);
531 mtx_unlock(&smPriv->st_mutex);
532
533 return entry != NULL;
534 }
535
536
537 static boolean
538 st_framebuffer_iface_insert(struct st_manager *smapi,
539 struct st_framebuffer_iface *stfbi)
540 {
541 struct st_manager_private *smPriv =
542 (struct st_manager_private *)smapi->st_manager_private;
543 struct hash_entry *entry;
544
545 assert(smPriv);
546 assert(smPriv->stfbi_ht);
547
548 mtx_lock(&smPriv->st_mutex);
549 entry = _mesa_hash_table_insert(smPriv->stfbi_ht, stfbi, stfbi);
550 mtx_unlock(&smPriv->st_mutex);
551
552 return entry != NULL;
553 }
554
555
556 static void
557 st_framebuffer_iface_remove(struct st_manager *smapi,
558 struct st_framebuffer_iface *stfbi)
559 {
560 struct st_manager_private *smPriv =
561 (struct st_manager_private *)smapi->st_manager_private;
562 struct hash_entry *entry;
563
564 if (!smPriv || !smPriv->stfbi_ht)
565 return;
566
567 mtx_lock(&smPriv->st_mutex);
568 entry = _mesa_hash_table_search(smPriv->stfbi_ht, stfbi);
569 if (!entry)
570 goto unlock;
571
572 _mesa_hash_table_remove(smPriv->stfbi_ht, entry);
573
574 unlock:
575 mtx_unlock(&smPriv->st_mutex);
576 }
577
578
579 /**
580 * The framebuffer interface object is no longer valid.
581 * Remove the object from the framebuffer interface hash table.
582 */
583 static void
584 st_api_destroy_drawable(struct st_api *stapi,
585 struct st_framebuffer_iface *stfbi)
586 {
587 if (!stfbi)
588 return;
589
590 st_framebuffer_iface_remove(stfbi->state_manager, stfbi);
591 }
592
593
594 /**
595 * Purge the winsys buffers list to remove any references to
596 * non-existing framebuffer interface objects.
597 */
598 static void
599 st_framebuffers_purge(struct st_context *st)
600 {
601 struct st_context_iface *st_iface = &st->iface;
602 struct st_manager *smapi = st_iface->state_manager;
603 struct st_framebuffer *stfb, *next;
604
605 assert(smapi);
606
607 LIST_FOR_EACH_ENTRY_SAFE_REV(stfb, next, &st->winsys_buffers, head) {
608 struct st_framebuffer_iface *stfbi = stfb->iface;
609
610 assert(stfbi);
611
612 /**
613 * If the corresponding framebuffer interface object no longer exists,
614 * remove the framebuffer object from the context's winsys buffers list,
615 * and unreference the framebuffer object, so its resources can be
616 * deleted.
617 */
618 if (!st_framebuffer_iface_lookup(smapi, stfbi)) {
619 LIST_DEL(&stfb->head);
620 st_framebuffer_reference(&stfb, NULL);
621 }
622 }
623 }
624
625 static void
626 st_context_flush(struct st_context_iface *stctxi, unsigned flags,
627 struct pipe_fence_handle **fence)
628 {
629 struct st_context *st = (struct st_context *) stctxi;
630 unsigned pipe_flags = 0;
631
632 if (flags & ST_FLUSH_END_OF_FRAME) {
633 pipe_flags |= PIPE_FLUSH_END_OF_FRAME;
634 }
635
636 st_flush(st, fence, pipe_flags);
637
638 if ((flags & ST_FLUSH_WAIT) && fence) {
639 st->pipe->screen->fence_finish(st->pipe->screen, NULL, *fence,
640 PIPE_TIMEOUT_INFINITE);
641 st->pipe->screen->fence_reference(st->pipe->screen, fence, NULL);
642 }
643
644 if (flags & ST_FLUSH_FRONT)
645 st_manager_flush_frontbuffer(st);
646
647 /* DRI3 changes the framebuffer after SwapBuffers, but we need to invoke
648 * st_manager_validate_framebuffers to notice that.
649 *
650 * Set gfx_shaders_may_be_dirty to invoke st_validate_state in the next
651 * draw call, which will invoke st_manager_validate_framebuffers, but it
652 * won't dirty states if there is no change.
653 */
654 if (flags & ST_FLUSH_END_OF_FRAME)
655 st->gfx_shaders_may_be_dirty = true;
656 }
657
658 static boolean
659 st_context_teximage(struct st_context_iface *stctxi,
660 enum st_texture_type tex_type,
661 int level, enum pipe_format pipe_format,
662 struct pipe_resource *tex, boolean mipmap)
663 {
664 struct st_context *st = (struct st_context *) stctxi;
665 struct gl_context *ctx = st->ctx;
666 struct gl_texture_object *texObj;
667 struct gl_texture_image *texImage;
668 struct st_texture_object *stObj;
669 struct st_texture_image *stImage;
670 GLenum internalFormat;
671 GLuint width, height, depth;
672 GLenum target;
673
674 switch (tex_type) {
675 case ST_TEXTURE_1D:
676 target = GL_TEXTURE_1D;
677 break;
678 case ST_TEXTURE_2D:
679 target = GL_TEXTURE_2D;
680 break;
681 case ST_TEXTURE_3D:
682 target = GL_TEXTURE_3D;
683 break;
684 case ST_TEXTURE_RECT:
685 target = GL_TEXTURE_RECTANGLE_ARB;
686 break;
687 default:
688 return FALSE;
689 }
690
691 texObj = _mesa_get_current_tex_object(ctx, target);
692
693 _mesa_lock_texture(ctx, texObj);
694
695 stObj = st_texture_object(texObj);
696 /* switch to surface based */
697 if (!stObj->surface_based) {
698 _mesa_clear_texture_object(ctx, texObj);
699 stObj->surface_based = GL_TRUE;
700 }
701
702 texImage = _mesa_get_tex_image(ctx, texObj, target, level);
703 stImage = st_texture_image(texImage);
704 if (tex) {
705 mesa_format texFormat = st_pipe_format_to_mesa_format(pipe_format);
706
707 if (util_format_has_alpha(tex->format))
708 internalFormat = GL_RGBA;
709 else
710 internalFormat = GL_RGB;
711
712 _mesa_init_teximage_fields(ctx, texImage,
713 tex->width0, tex->height0, 1, 0,
714 internalFormat, texFormat);
715
716 width = tex->width0;
717 height = tex->height0;
718 depth = tex->depth0;
719
720 /* grow the image size until we hit level = 0 */
721 while (level > 0) {
722 if (width != 1)
723 width <<= 1;
724 if (height != 1)
725 height <<= 1;
726 if (depth != 1)
727 depth <<= 1;
728 level--;
729 }
730 }
731 else {
732 _mesa_clear_texture_image(ctx, texImage);
733 width = height = depth = 0;
734 }
735
736 pipe_resource_reference(&stImage->pt, tex);
737 stObj->surface_format = pipe_format;
738
739 st_texture_release_all_sampler_views(st, stObj);
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 if (!strb)
1042 return;
1043
1044 /* never a dummy fb */
1045 stfb->iface->flush_front(&st->iface, stfb->iface, ST_ATTACHMENT_FRONT_LEFT);
1046 }
1047
1048 /**
1049 * Re-validate the framebuffers.
1050 */
1051 void
1052 st_manager_validate_framebuffers(struct st_context *st)
1053 {
1054 struct st_framebuffer *stdraw = st_ws_framebuffer(st->ctx->DrawBuffer);
1055 struct st_framebuffer *stread = st_ws_framebuffer(st->ctx->ReadBuffer);
1056
1057 if (stdraw)
1058 st_framebuffer_validate(stdraw, st);
1059 if (stread && stread != stdraw)
1060 st_framebuffer_validate(stread, st);
1061
1062 st_context_validate(st, stdraw, stread);
1063 }
1064
1065 /**
1066 * Add a color renderbuffer on demand. The FBO must correspond to a window,
1067 * not a user-created FBO.
1068 */
1069 boolean
1070 st_manager_add_color_renderbuffer(struct st_context *st,
1071 struct gl_framebuffer *fb,
1072 gl_buffer_index idx)
1073 {
1074 struct st_framebuffer *stfb = st_ws_framebuffer(fb);
1075
1076 /* FBO */
1077 if (!stfb)
1078 return FALSE;
1079
1080 assert(_mesa_is_winsys_fbo(fb));
1081
1082 if (stfb->Base.Attachment[idx].Renderbuffer)
1083 return TRUE;
1084
1085 switch (idx) {
1086 case BUFFER_FRONT_LEFT:
1087 case BUFFER_BACK_LEFT:
1088 case BUFFER_FRONT_RIGHT:
1089 case BUFFER_BACK_RIGHT:
1090 break;
1091 default:
1092 return FALSE;
1093 }
1094
1095 if (!st_framebuffer_add_renderbuffer(stfb, idx))
1096 return FALSE;
1097
1098 st_framebuffer_update_attachments(stfb);
1099
1100 /*
1101 * Force a call to the state tracker manager to validate the
1102 * new renderbuffer. It might be that there is a window system
1103 * renderbuffer available.
1104 */
1105 if (stfb->iface)
1106 stfb->iface_stamp = p_atomic_read(&stfb->iface->stamp) - 1;
1107
1108 st_invalidate_buffers(st);
1109
1110 return TRUE;
1111 }
1112
1113 static void
1114 st_manager_destroy(struct st_manager *smapi)
1115 {
1116 struct st_manager_private *smPriv = smapi->st_manager_private;
1117
1118 if (smPriv && smPriv->stfbi_ht) {
1119 _mesa_hash_table_destroy(smPriv->stfbi_ht, NULL);
1120 mtx_destroy(&smPriv->st_mutex);
1121 free(smPriv);
1122 smapi->st_manager_private = NULL;
1123 }
1124 }
1125
1126 static unsigned
1127 get_version(struct pipe_screen *screen,
1128 struct st_config_options *options, gl_api api)
1129 {
1130 struct gl_constants consts = {0};
1131 struct gl_extensions extensions = {0};
1132 GLuint version;
1133
1134 if (_mesa_override_gl_version_contextless(&consts, &api, &version)) {
1135 return version;
1136 }
1137
1138 _mesa_init_constants(&consts, api);
1139 _mesa_init_extensions(&extensions);
1140
1141 st_init_limits(screen, &consts, &extensions);
1142 st_init_extensions(screen, &consts, &extensions, options, GL_TRUE);
1143
1144 return _mesa_get_version(&extensions, &consts, api);
1145 }
1146
1147 static void
1148 st_api_query_versions(struct st_api *stapi, struct st_manager *sm,
1149 struct st_config_options *options,
1150 int *gl_core_version,
1151 int *gl_compat_version,
1152 int *gl_es1_version,
1153 int *gl_es2_version)
1154 {
1155 *gl_core_version = get_version(sm->screen, options, API_OPENGL_CORE);
1156 *gl_compat_version = get_version(sm->screen, options, API_OPENGL_COMPAT);
1157 *gl_es1_version = get_version(sm->screen, options, API_OPENGLES);
1158 *gl_es2_version = get_version(sm->screen, options, API_OPENGLES2);
1159 }
1160
1161 static const struct st_api st_gl_api = {
1162 .name = "Mesa " PACKAGE_VERSION,
1163 .api = ST_API_OPENGL,
1164 .profile_mask = ST_PROFILE_DEFAULT_MASK |
1165 ST_PROFILE_OPENGL_CORE_MASK |
1166 ST_PROFILE_OPENGL_ES1_MASK |
1167 ST_PROFILE_OPENGL_ES2_MASK |
1168 0,
1169 .feature_mask = ST_API_FEATURE_MS_VISUALS_MASK,
1170 .destroy = st_api_destroy,
1171 .query_versions = st_api_query_versions,
1172 .create_context = st_api_create_context,
1173 .make_current = st_api_make_current,
1174 .get_current = st_api_get_current,
1175 .destroy_drawable = st_api_destroy_drawable,
1176 };
1177
1178 struct st_api *
1179 st_gl_api_create(void)
1180 {
1181 return (struct st_api *) &st_gl_api;
1182 }