mesa/st: Reduce the number of frontbuffer flush calls
[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 /* validate the fb */
194 do {
195 if (!stfb->iface->validate(&st->iface, stfb->iface, stfb->statts,
196 stfb->num_statts, textures))
197 return;
198
199 stfb->iface_stamp = new_stamp;
200 new_stamp = p_atomic_read(&stfb->iface->stamp);
201 } while(stfb->iface_stamp != new_stamp);
202
203 width = stfb->Base.Width;
204 height = stfb->Base.Height;
205
206 for (i = 0; i < stfb->num_statts; i++) {
207 struct st_renderbuffer *strb;
208 struct pipe_surface *ps, surf_tmpl;
209 gl_buffer_index idx;
210
211 if (!textures[i])
212 continue;
213
214 idx = attachment_to_buffer_index(stfb->statts[i]);
215 if (idx >= BUFFER_COUNT) {
216 pipe_resource_reference(&textures[i], NULL);
217 continue;
218 }
219
220 strb = st_renderbuffer(stfb->Base.Attachment[idx].Renderbuffer);
221 assert(strb);
222 if (strb->texture == textures[i]) {
223 pipe_resource_reference(&textures[i], NULL);
224 continue;
225 }
226
227 u_surface_default_template(&surf_tmpl, textures[i]);
228 ps = st->pipe->create_surface(st->pipe, textures[i], &surf_tmpl);
229 if (ps) {
230 struct pipe_surface **psurf =
231 util_format_is_srgb(ps->format) ? &strb->surface_srgb :
232 &strb->surface_linear;
233
234 pipe_surface_reference(psurf, ps);
235 strb->surface = *psurf;
236 pipe_resource_reference(&strb->texture, ps->texture);
237 /* ownership transfered */
238 pipe_surface_reference(&ps, NULL);
239
240 changed = TRUE;
241
242 strb->Base.Width = strb->surface->width;
243 strb->Base.Height = strb->surface->height;
244
245 width = strb->Base.Width;
246 height = strb->Base.Height;
247 }
248
249 pipe_resource_reference(&textures[i], NULL);
250 }
251
252 if (changed) {
253 ++stfb->stamp;
254 _mesa_resize_framebuffer(st->ctx, &stfb->Base, width, height);
255 }
256 }
257
258 /**
259 * Update the attachments to validate by looping the existing renderbuffers.
260 */
261 static void
262 st_framebuffer_update_attachments(struct st_framebuffer *stfb)
263 {
264 gl_buffer_index idx;
265
266 stfb->num_statts = 0;
267 for (idx = 0; idx < BUFFER_COUNT; idx++) {
268 struct st_renderbuffer *strb;
269 enum st_attachment_type statt;
270
271 strb = st_renderbuffer(stfb->Base.Attachment[idx].Renderbuffer);
272 if (!strb || strb->software)
273 continue;
274
275 statt = buffer_index_to_attachment(idx);
276 if (statt != ST_ATTACHMENT_INVALID &&
277 st_visual_have_buffers(stfb->iface->visual, 1 << statt))
278 stfb->statts[stfb->num_statts++] = statt;
279 }
280 stfb->stamp++;
281 }
282
283 /**
284 * Add a renderbuffer to the framebuffer. The framebuffer is one that
285 * corresponds to a window and is not a user-created FBO.
286 */
287 static boolean
288 st_framebuffer_add_renderbuffer(struct st_framebuffer *stfb,
289 gl_buffer_index idx)
290 {
291 struct gl_renderbuffer *rb;
292 enum pipe_format format;
293 boolean sw;
294
295 assert(_mesa_is_winsys_fbo(&stfb->Base));
296
297 /* do not distinguish depth/stencil buffers */
298 if (idx == BUFFER_STENCIL)
299 idx = BUFFER_DEPTH;
300
301 switch (idx) {
302 case BUFFER_DEPTH:
303 format = stfb->iface->visual->depth_stencil_format;
304 sw = FALSE;
305 break;
306 case BUFFER_ACCUM:
307 format = stfb->iface->visual->accum_format;
308 sw = TRUE;
309 break;
310 default:
311 format = stfb->iface->visual->color_format;
312 if (stfb->Base.Visual.sRGBCapable)
313 format = util_format_srgb(format);
314 sw = FALSE;
315 break;
316 }
317
318 if (format == PIPE_FORMAT_NONE)
319 return FALSE;
320
321 rb = st_new_renderbuffer_fb(format, stfb->iface->visual->samples, sw);
322 if (!rb)
323 return FALSE;
324
325 if (idx != BUFFER_DEPTH) {
326 _mesa_attach_and_own_rb(&stfb->Base, idx, rb);
327 return TRUE;
328 }
329
330 bool rb_ownership_taken = false;
331 if (util_format_get_component_bits(format, UTIL_FORMAT_COLORSPACE_ZS, 0)) {
332 _mesa_attach_and_own_rb(&stfb->Base, BUFFER_DEPTH, rb);
333 rb_ownership_taken = true;
334 }
335
336 if (util_format_get_component_bits(format, UTIL_FORMAT_COLORSPACE_ZS, 1)) {
337 if (rb_ownership_taken)
338 _mesa_attach_and_reference_rb(&stfb->Base, BUFFER_STENCIL, rb);
339 else
340 _mesa_attach_and_own_rb(&stfb->Base, BUFFER_STENCIL, rb);
341 }
342
343 return TRUE;
344 }
345
346 /**
347 * Intialize a struct gl_config from a visual.
348 */
349 static void
350 st_visual_to_context_mode(const struct st_visual *visual,
351 struct gl_config *mode)
352 {
353 memset(mode, 0, sizeof(*mode));
354
355 if (st_visual_have_buffers(visual, ST_ATTACHMENT_BACK_LEFT_MASK))
356 mode->doubleBufferMode = GL_TRUE;
357 if (st_visual_have_buffers(visual,
358 ST_ATTACHMENT_FRONT_RIGHT_MASK | ST_ATTACHMENT_BACK_RIGHT_MASK))
359 mode->stereoMode = GL_TRUE;
360
361 if (visual->color_format != PIPE_FORMAT_NONE) {
362 mode->rgbMode = GL_TRUE;
363
364 mode->redBits =
365 util_format_get_component_bits(visual->color_format,
366 UTIL_FORMAT_COLORSPACE_RGB, 0);
367 mode->greenBits =
368 util_format_get_component_bits(visual->color_format,
369 UTIL_FORMAT_COLORSPACE_RGB, 1);
370 mode->blueBits =
371 util_format_get_component_bits(visual->color_format,
372 UTIL_FORMAT_COLORSPACE_RGB, 2);
373 mode->alphaBits =
374 util_format_get_component_bits(visual->color_format,
375 UTIL_FORMAT_COLORSPACE_RGB, 3);
376
377 mode->rgbBits = mode->redBits +
378 mode->greenBits + mode->blueBits + mode->alphaBits;
379 mode->sRGBCapable = util_format_is_srgb(visual->color_format);
380 }
381
382 if (visual->depth_stencil_format != PIPE_FORMAT_NONE) {
383 mode->depthBits =
384 util_format_get_component_bits(visual->depth_stencil_format,
385 UTIL_FORMAT_COLORSPACE_ZS, 0);
386 mode->stencilBits =
387 util_format_get_component_bits(visual->depth_stencil_format,
388 UTIL_FORMAT_COLORSPACE_ZS, 1);
389
390 mode->haveDepthBuffer = mode->depthBits > 0;
391 mode->haveStencilBuffer = mode->stencilBits > 0;
392 }
393
394 if (visual->accum_format != PIPE_FORMAT_NONE) {
395 mode->haveAccumBuffer = GL_TRUE;
396
397 mode->accumRedBits =
398 util_format_get_component_bits(visual->accum_format,
399 UTIL_FORMAT_COLORSPACE_RGB, 0);
400 mode->accumGreenBits =
401 util_format_get_component_bits(visual->accum_format,
402 UTIL_FORMAT_COLORSPACE_RGB, 1);
403 mode->accumBlueBits =
404 util_format_get_component_bits(visual->accum_format,
405 UTIL_FORMAT_COLORSPACE_RGB, 2);
406 mode->accumAlphaBits =
407 util_format_get_component_bits(visual->accum_format,
408 UTIL_FORMAT_COLORSPACE_RGB, 3);
409 }
410
411 if (visual->samples > 1) {
412 mode->sampleBuffers = 1;
413 mode->samples = visual->samples;
414 }
415 }
416
417 /**
418 * Create a framebuffer from a manager interface.
419 */
420 static struct st_framebuffer *
421 st_framebuffer_create(struct st_context *st,
422 struct st_framebuffer_iface *stfbi)
423 {
424 struct st_framebuffer *stfb;
425 struct gl_config mode;
426 gl_buffer_index idx;
427
428 if (!stfbi)
429 return NULL;
430
431 stfb = CALLOC_STRUCT(st_framebuffer);
432 if (!stfb)
433 return NULL;
434
435 st_visual_to_context_mode(stfbi->visual, &mode);
436
437 /*
438 * For desktop GL, sRGB framebuffer write is controlled by both the
439 * capability of the framebuffer and GL_FRAMEBUFFER_SRGB. We should
440 * advertise the capability when the pipe driver (and core Mesa) supports
441 * it so that applications can enable sRGB write when they want to.
442 *
443 * This is not to be confused with GLX_FRAMEBUFFER_SRGB_CAPABLE_ARB. When
444 * the attribute is GLX_TRUE, it tells the st manager to pick a color
445 * format such that util_format_srgb(visual->color_format) can be supported
446 * by the pipe driver. We still need to advertise the capability here.
447 *
448 * For GLES, however, sRGB framebuffer write is controlled only by the
449 * capability of the framebuffer. There is GL_EXT_sRGB_write_control to
450 * give applications the control back, but sRGB write is still enabled by
451 * default. To avoid unexpected results, we should not advertise the
452 * capability. This could change when we add support for
453 * EGL_KHR_gl_colorspace.
454 */
455 if (_mesa_is_desktop_gl(st->ctx)) {
456 struct pipe_screen *screen = st->pipe->screen;
457 const enum pipe_format srgb_format =
458 util_format_srgb(stfbi->visual->color_format);
459
460 if (srgb_format != PIPE_FORMAT_NONE &&
461 st_pipe_format_to_mesa_format(srgb_format) != MESA_FORMAT_NONE &&
462 screen->is_format_supported(screen, srgb_format,
463 PIPE_TEXTURE_2D, stfbi->visual->samples,
464 (PIPE_BIND_DISPLAY_TARGET |
465 PIPE_BIND_RENDER_TARGET)))
466 mode.sRGBCapable = GL_TRUE;
467 }
468
469 _mesa_initialize_window_framebuffer(&stfb->Base, &mode);
470
471 stfb->iface = stfbi;
472 stfb->iface_ID = stfbi->ID;
473 stfb->iface_stamp = p_atomic_read(&stfbi->stamp) - 1;
474
475 /* add the color buffer */
476 idx = stfb->Base._ColorDrawBufferIndexes[0];
477 if (!st_framebuffer_add_renderbuffer(stfb, idx)) {
478 free(stfb);
479 return NULL;
480 }
481
482 st_framebuffer_add_renderbuffer(stfb, BUFFER_DEPTH);
483 st_framebuffer_add_renderbuffer(stfb, BUFFER_ACCUM);
484
485 stfb->stamp = 0;
486 st_framebuffer_update_attachments(stfb);
487
488 return stfb;
489 }
490
491 /**
492 * Reference a framebuffer.
493 */
494 void
495 st_framebuffer_reference(struct st_framebuffer **ptr,
496 struct st_framebuffer *stfb)
497 {
498 struct gl_framebuffer *fb = &stfb->Base;
499 _mesa_reference_framebuffer((struct gl_framebuffer **) ptr, fb);
500 }
501
502
503 static uint32_t
504 st_framebuffer_iface_hash(const void *key)
505 {
506 return (uintptr_t)key;
507 }
508
509
510 static bool
511 st_framebuffer_iface_equal(const void *a, const void *b)
512 {
513 return (struct st_framebuffer_iface *)a == (struct st_framebuffer_iface *)b;
514 }
515
516
517 static boolean
518 st_framebuffer_iface_lookup(struct st_manager *smapi,
519 const struct st_framebuffer_iface *stfbi)
520 {
521 struct st_manager_private *smPriv =
522 (struct st_manager_private *)smapi->st_manager_private;
523 struct hash_entry *entry;
524
525 assert(smPriv);
526 assert(smPriv->stfbi_ht);
527
528 mtx_lock(&smPriv->st_mutex);
529 entry = _mesa_hash_table_search(smPriv->stfbi_ht, stfbi);
530 mtx_unlock(&smPriv->st_mutex);
531
532 return entry != NULL;
533 }
534
535
536 static boolean
537 st_framebuffer_iface_insert(struct st_manager *smapi,
538 struct st_framebuffer_iface *stfbi)
539 {
540 struct st_manager_private *smPriv =
541 (struct st_manager_private *)smapi->st_manager_private;
542 struct hash_entry *entry;
543
544 assert(smPriv);
545 assert(smPriv->stfbi_ht);
546
547 mtx_lock(&smPriv->st_mutex);
548 entry = _mesa_hash_table_insert(smPriv->stfbi_ht, stfbi, stfbi);
549 mtx_unlock(&smPriv->st_mutex);
550
551 return entry != NULL;
552 }
553
554
555 static void
556 st_framebuffer_iface_remove(struct st_manager *smapi,
557 struct st_framebuffer_iface *stfbi)
558 {
559 struct st_manager_private *smPriv =
560 (struct st_manager_private *)smapi->st_manager_private;
561 struct hash_entry *entry;
562
563 if (!smPriv || !smPriv->stfbi_ht)
564 return;
565
566 mtx_lock(&smPriv->st_mutex);
567 entry = _mesa_hash_table_search(smPriv->stfbi_ht, stfbi);
568 if (!entry)
569 goto unlock;
570
571 _mesa_hash_table_remove(smPriv->stfbi_ht, entry);
572
573 unlock:
574 mtx_unlock(&smPriv->st_mutex);
575 }
576
577
578 /**
579 * The framebuffer interface object is no longer valid.
580 * Remove the object from the framebuffer interface hash table.
581 */
582 static void
583 st_api_destroy_drawable(struct st_api *stapi,
584 struct st_framebuffer_iface *stfbi)
585 {
586 if (!stfbi)
587 return;
588
589 st_framebuffer_iface_remove(stfbi->state_manager, stfbi);
590 }
591
592
593 /**
594 * Purge the winsys buffers list to remove any references to
595 * non-existing framebuffer interface objects.
596 */
597 static void
598 st_framebuffers_purge(struct st_context *st)
599 {
600 struct st_context_iface *st_iface = &st->iface;
601 struct st_manager *smapi = st_iface->state_manager;
602 struct st_framebuffer *stfb, *next;
603
604 assert(smapi);
605
606 LIST_FOR_EACH_ENTRY_SAFE_REV(stfb, next, &st->winsys_buffers, head) {
607 struct st_framebuffer_iface *stfbi = stfb->iface;
608
609 assert(stfbi);
610
611 /**
612 * If the corresponding framebuffer interface object no longer exists,
613 * remove the framebuffer object from the context's winsys buffers list,
614 * and unreference the framebuffer object, so its resources can be
615 * deleted.
616 */
617 if (!st_framebuffer_iface_lookup(smapi, stfbi)) {
618 LIST_DEL(&stfb->head);
619 st_framebuffer_reference(&stfb, NULL);
620 }
621 }
622 }
623
624 static void
625 st_context_flush(struct st_context_iface *stctxi, unsigned flags,
626 struct pipe_fence_handle **fence)
627 {
628 struct st_context *st = (struct st_context *) stctxi;
629 unsigned pipe_flags = 0;
630
631 if (flags & ST_FLUSH_END_OF_FRAME) {
632 pipe_flags |= PIPE_FLUSH_END_OF_FRAME;
633 }
634
635 st_flush(st, fence, pipe_flags);
636
637 if ((flags & ST_FLUSH_WAIT) && fence) {
638 st->pipe->screen->fence_finish(st->pipe->screen, NULL, *fence,
639 PIPE_TIMEOUT_INFINITE);
640 st->pipe->screen->fence_reference(st->pipe->screen, fence, NULL);
641 }
642
643 if (flags & ST_FLUSH_FRONT)
644 st_manager_flush_frontbuffer(st);
645
646 /* DRI3 changes the framebuffer after SwapBuffers, but we need to invoke
647 * st_manager_validate_framebuffers to notice that.
648 *
649 * Set gfx_shaders_may_be_dirty to invoke st_validate_state in the next
650 * draw call, which will invoke st_manager_validate_framebuffers, but it
651 * won't dirty states if there is no change.
652 */
653 if (flags & ST_FLUSH_END_OF_FRAME)
654 st->gfx_shaders_may_be_dirty = true;
655 }
656
657 static boolean
658 st_context_teximage(struct st_context_iface *stctxi,
659 enum st_texture_type tex_type,
660 int level, enum pipe_format pipe_format,
661 struct pipe_resource *tex, boolean mipmap)
662 {
663 struct st_context *st = (struct st_context *) stctxi;
664 struct gl_context *ctx = st->ctx;
665 struct gl_texture_object *texObj;
666 struct gl_texture_image *texImage;
667 struct st_texture_object *stObj;
668 struct st_texture_image *stImage;
669 GLenum internalFormat;
670 GLuint width, height, depth;
671 GLenum target;
672
673 switch (tex_type) {
674 case ST_TEXTURE_1D:
675 target = GL_TEXTURE_1D;
676 break;
677 case ST_TEXTURE_2D:
678 target = GL_TEXTURE_2D;
679 break;
680 case ST_TEXTURE_3D:
681 target = GL_TEXTURE_3D;
682 break;
683 case ST_TEXTURE_RECT:
684 target = GL_TEXTURE_RECTANGLE_ARB;
685 break;
686 default:
687 return FALSE;
688 }
689
690 texObj = _mesa_get_current_tex_object(ctx, target);
691
692 _mesa_lock_texture(ctx, texObj);
693
694 stObj = st_texture_object(texObj);
695 /* switch to surface based */
696 if (!stObj->surface_based) {
697 _mesa_clear_texture_object(ctx, texObj);
698 stObj->surface_based = GL_TRUE;
699 }
700
701 texImage = _mesa_get_tex_image(ctx, texObj, target, level);
702 stImage = st_texture_image(texImage);
703 if (tex) {
704 mesa_format texFormat = st_pipe_format_to_mesa_format(pipe_format);
705
706 if (util_format_has_alpha(tex->format))
707 internalFormat = GL_RGBA;
708 else
709 internalFormat = GL_RGB;
710
711 _mesa_init_teximage_fields(ctx, texImage,
712 tex->width0, tex->height0, 1, 0,
713 internalFormat, texFormat);
714
715 width = tex->width0;
716 height = tex->height0;
717 depth = tex->depth0;
718
719 /* grow the image size until we hit level = 0 */
720 while (level > 0) {
721 if (width != 1)
722 width <<= 1;
723 if (height != 1)
724 height <<= 1;
725 if (depth != 1)
726 depth <<= 1;
727 level--;
728 }
729 }
730 else {
731 _mesa_clear_texture_image(ctx, texImage);
732 width = height = depth = 0;
733 }
734
735 pipe_resource_reference(&stImage->pt, tex);
736 stObj->surface_format = pipe_format;
737
738 stObj->needs_validation = true;
739
740 _mesa_dirty_texobj(ctx, texObj);
741 _mesa_unlock_texture(ctx, texObj);
742
743 return TRUE;
744 }
745
746 static void
747 st_context_copy(struct st_context_iface *stctxi,
748 struct st_context_iface *stsrci, unsigned mask)
749 {
750 struct st_context *st = (struct st_context *) stctxi;
751 struct st_context *src = (struct st_context *) stsrci;
752
753 _mesa_copy_context(src->ctx, st->ctx, mask);
754 }
755
756 static boolean
757 st_context_share(struct st_context_iface *stctxi,
758 struct st_context_iface *stsrci)
759 {
760 struct st_context *st = (struct st_context *) stctxi;
761 struct st_context *src = (struct st_context *) stsrci;
762
763 return _mesa_share_state(st->ctx, src->ctx);
764 }
765
766 static void
767 st_context_destroy(struct st_context_iface *stctxi)
768 {
769 struct st_context *st = (struct st_context *) stctxi;
770 st_destroy_context(st);
771 }
772
773 static void
774 st_start_thread(struct st_context_iface *stctxi)
775 {
776 struct st_context *st = (struct st_context *) stctxi;
777
778 _mesa_glthread_init(st->ctx);
779 }
780
781 static void
782 st_thread_finish(struct st_context_iface *stctxi)
783 {
784 struct st_context *st = (struct st_context *) stctxi;
785
786 _mesa_glthread_finish(st->ctx);
787 }
788
789 static struct st_context_iface *
790 st_api_create_context(struct st_api *stapi, struct st_manager *smapi,
791 const struct st_context_attribs *attribs,
792 enum st_context_error *error,
793 struct st_context_iface *shared_stctxi)
794 {
795 struct st_context *shared_ctx = (struct st_context *) shared_stctxi;
796 struct st_context *st;
797 struct pipe_context *pipe;
798 struct gl_config mode;
799 gl_api api;
800 bool no_error = false;
801 unsigned ctx_flags = PIPE_CONTEXT_PREFER_THREADED;
802
803 if (!(stapi->profile_mask & (1 << attribs->profile)))
804 return NULL;
805
806 switch (attribs->profile) {
807 case ST_PROFILE_DEFAULT:
808 api = API_OPENGL_COMPAT;
809 break;
810 case ST_PROFILE_OPENGL_ES1:
811 api = API_OPENGLES;
812 break;
813 case ST_PROFILE_OPENGL_ES2:
814 api = API_OPENGLES2;
815 break;
816 case ST_PROFILE_OPENGL_CORE:
817 api = API_OPENGL_CORE;
818 break;
819 default:
820 *error = ST_CONTEXT_ERROR_BAD_API;
821 return NULL;
822 }
823
824 /* Create a hash table for the framebuffer interface objects
825 * if it has not been created for this st manager.
826 */
827 if (smapi->st_manager_private == NULL) {
828 struct st_manager_private *smPriv;
829
830 smPriv = CALLOC_STRUCT(st_manager_private);
831 mtx_init(&smPriv->st_mutex, mtx_plain);
832 smPriv->stfbi_ht = _mesa_hash_table_create(NULL,
833 st_framebuffer_iface_hash,
834 st_framebuffer_iface_equal);
835 smapi->st_manager_private = smPriv;
836 smapi->destroy = st_manager_destroy;
837 }
838
839 if (attribs->flags & ST_CONTEXT_FLAG_ROBUST_ACCESS)
840 ctx_flags |= PIPE_CONTEXT_ROBUST_BUFFER_ACCESS;
841
842 if (attribs->flags & ST_CONTEXT_FLAG_NO_ERROR)
843 no_error = true;
844
845 pipe = smapi->screen->context_create(smapi->screen, NULL, ctx_flags);
846 if (!pipe) {
847 *error = ST_CONTEXT_ERROR_NO_MEMORY;
848 return NULL;
849 }
850
851 st_visual_to_context_mode(&attribs->visual, &mode);
852 st = st_create_context(api, pipe, &mode, shared_ctx, &attribs->options, no_error);
853 if (!st) {
854 *error = ST_CONTEXT_ERROR_NO_MEMORY;
855 pipe->destroy(pipe);
856 return NULL;
857 }
858
859 if (attribs->flags & ST_CONTEXT_FLAG_DEBUG) {
860 if (!_mesa_set_debug_state_int(st->ctx, GL_DEBUG_OUTPUT, GL_TRUE)) {
861 *error = ST_CONTEXT_ERROR_NO_MEMORY;
862 return NULL;
863 }
864
865 st->ctx->Const.ContextFlags |= GL_CONTEXT_FLAG_DEBUG_BIT;
866 }
867
868 if (st->ctx->Const.ContextFlags & GL_CONTEXT_FLAG_DEBUG_BIT) {
869 st_update_debug_callback(st);
870 }
871
872 if (attribs->flags & ST_CONTEXT_FLAG_FORWARD_COMPATIBLE)
873 st->ctx->Const.ContextFlags |= GL_CONTEXT_FLAG_FORWARD_COMPATIBLE_BIT;
874 if (attribs->flags & ST_CONTEXT_FLAG_ROBUST_ACCESS) {
875 st->ctx->Const.ContextFlags |= GL_CONTEXT_FLAG_ROBUST_ACCESS_BIT_ARB;
876 st->ctx->Const.RobustAccess = GL_TRUE;
877 }
878 if (attribs->flags & ST_CONTEXT_FLAG_RESET_NOTIFICATION_ENABLED) {
879 st->ctx->Const.ResetStrategy = GL_LOSE_CONTEXT_ON_RESET_ARB;
880 st_install_device_reset_callback(st);
881 }
882
883 /* need to perform version check */
884 if (attribs->major > 1 || attribs->minor > 0) {
885 /* Is the actual version less than the requested version?
886 */
887 if (st->ctx->Version < attribs->major * 10U + attribs->minor) {
888 *error = ST_CONTEXT_ERROR_BAD_VERSION;
889 st_destroy_context(st);
890 return NULL;
891 }
892 }
893
894 st->invalidate_on_gl_viewport =
895 smapi->get_param(smapi, ST_MANAGER_BROKEN_INVALIDATE);
896
897 st->iface.destroy = st_context_destroy;
898 st->iface.flush = st_context_flush;
899 st->iface.teximage = st_context_teximage;
900 st->iface.copy = st_context_copy;
901 st->iface.share = st_context_share;
902 st->iface.start_thread = st_start_thread;
903 st->iface.thread_finish = st_thread_finish;
904 st->iface.st_context_private = (void *) smapi;
905 st->iface.cso_context = st->cso_context;
906 st->iface.pipe = st->pipe;
907 st->iface.state_manager = smapi;
908
909 *error = ST_CONTEXT_SUCCESS;
910 return &st->iface;
911 }
912
913 static struct st_context_iface *
914 st_api_get_current(struct st_api *stapi)
915 {
916 GET_CURRENT_CONTEXT(ctx);
917 struct st_context *st = (ctx) ? ctx->st : NULL;
918
919 return (st) ? &st->iface : NULL;
920 }
921
922 static struct st_framebuffer *
923 st_framebuffer_reuse_or_create(struct st_context *st,
924 struct gl_framebuffer *fb,
925 struct st_framebuffer_iface *stfbi)
926 {
927 struct st_framebuffer *cur = NULL, *stfb = NULL;
928
929 if (!stfbi)
930 return NULL;
931
932 /* Check if there is already a framebuffer object for the specified
933 * framebuffer interface in this context. If there is one, use it.
934 */
935 LIST_FOR_EACH_ENTRY(cur, &st->winsys_buffers, head) {
936 if (cur->iface_ID == stfbi->ID) {
937 st_framebuffer_reference(&stfb, cur);
938 break;
939 }
940 }
941
942 /* If there is not already a framebuffer object, create one */
943 if (stfb == NULL) {
944 cur = st_framebuffer_create(st, stfbi);
945
946 if (cur) {
947 /* add the referenced framebuffer interface object to
948 * the framebuffer interface object hash table.
949 */
950 if (!st_framebuffer_iface_insert(stfbi->state_manager, stfbi)) {
951 st_framebuffer_reference(&cur, NULL);
952 return NULL;
953 }
954
955 /* add to the context's winsys buffers list */
956 LIST_ADD(&cur->head, &st->winsys_buffers);
957
958 st_framebuffer_reference(&stfb, cur);
959 }
960 }
961
962 return stfb;
963 }
964
965 static boolean
966 st_api_make_current(struct st_api *stapi, struct st_context_iface *stctxi,
967 struct st_framebuffer_iface *stdrawi,
968 struct st_framebuffer_iface *streadi)
969 {
970 struct st_context *st = (struct st_context *) stctxi;
971 struct st_framebuffer *stdraw, *stread;
972 boolean ret;
973
974 _glapi_check_multithread();
975
976 if (st) {
977 /* reuse or create the draw fb */
978 stdraw = st_framebuffer_reuse_or_create(st,
979 st->ctx->WinSysDrawBuffer, stdrawi);
980 if (streadi != stdrawi) {
981 /* do the same for the read fb */
982 stread = st_framebuffer_reuse_or_create(st,
983 st->ctx->WinSysReadBuffer, streadi);
984 }
985 else {
986 stread = NULL;
987 /* reuse the draw fb for the read fb */
988 if (stdraw)
989 st_framebuffer_reference(&stread, stdraw);
990 }
991
992 if (stdraw && stread) {
993 st_framebuffer_validate(stdraw, st);
994 if (stread != stdraw)
995 st_framebuffer_validate(stread, st);
996
997 ret = _mesa_make_current(st->ctx, &stdraw->Base, &stread->Base);
998
999 st->draw_stamp = stdraw->stamp - 1;
1000 st->read_stamp = stread->stamp - 1;
1001 st_context_validate(st, stdraw, stread);
1002 }
1003 else {
1004 struct gl_framebuffer *incomplete = _mesa_get_incomplete_framebuffer();
1005 ret = _mesa_make_current(st->ctx, incomplete, incomplete);
1006 }
1007
1008 st_framebuffer_reference(&stdraw, NULL);
1009 st_framebuffer_reference(&stread, NULL);
1010
1011 /* Purge the context's winsys_buffers list in case any
1012 * of the referenced drawables no longer exist.
1013 */
1014 st_framebuffers_purge(st);
1015 }
1016 else {
1017 ret = _mesa_make_current(NULL, NULL, NULL);
1018 }
1019
1020 return ret;
1021 }
1022
1023 static void
1024 st_api_destroy(struct st_api *stapi)
1025 {
1026 }
1027
1028 /**
1029 * Flush the front buffer if the current context renders to the front buffer.
1030 */
1031 void
1032 st_manager_flush_frontbuffer(struct st_context *st)
1033 {
1034 struct st_framebuffer *stfb = st_ws_framebuffer(st->ctx->DrawBuffer);
1035 struct st_renderbuffer *strb = NULL;
1036
1037 if (stfb)
1038 strb = st_renderbuffer(stfb->Base.Attachment[BUFFER_FRONT_LEFT].Renderbuffer);
1039
1040 /* Do we have a front color buffer and has it been drawn to since last
1041 * frontbuffer flush?
1042 */
1043 if (strb && strb->defined) {
1044 stfb->iface->flush_front(&st->iface, stfb->iface,
1045 ST_ATTACHMENT_FRONT_LEFT);
1046 strb->defined = GL_FALSE;
1047 }
1048 }
1049
1050 /**
1051 * Re-validate the framebuffers.
1052 */
1053 void
1054 st_manager_validate_framebuffers(struct st_context *st)
1055 {
1056 struct st_framebuffer *stdraw = st_ws_framebuffer(st->ctx->DrawBuffer);
1057 struct st_framebuffer *stread = st_ws_framebuffer(st->ctx->ReadBuffer);
1058
1059 if (stdraw)
1060 st_framebuffer_validate(stdraw, st);
1061 if (stread && stread != stdraw)
1062 st_framebuffer_validate(stread, st);
1063
1064 st_context_validate(st, stdraw, stread);
1065 }
1066
1067 /**
1068 * Add a color renderbuffer on demand. The FBO must correspond to a window,
1069 * not a user-created FBO.
1070 */
1071 boolean
1072 st_manager_add_color_renderbuffer(struct st_context *st,
1073 struct gl_framebuffer *fb,
1074 gl_buffer_index idx)
1075 {
1076 struct st_framebuffer *stfb = st_ws_framebuffer(fb);
1077
1078 /* FBO */
1079 if (!stfb)
1080 return FALSE;
1081
1082 assert(_mesa_is_winsys_fbo(fb));
1083
1084 if (stfb->Base.Attachment[idx].Renderbuffer)
1085 return TRUE;
1086
1087 switch (idx) {
1088 case BUFFER_FRONT_LEFT:
1089 case BUFFER_BACK_LEFT:
1090 case BUFFER_FRONT_RIGHT:
1091 case BUFFER_BACK_RIGHT:
1092 break;
1093 default:
1094 return FALSE;
1095 }
1096
1097 if (!st_framebuffer_add_renderbuffer(stfb, idx))
1098 return FALSE;
1099
1100 st_framebuffer_update_attachments(stfb);
1101
1102 /*
1103 * Force a call to the state tracker manager to validate the
1104 * new renderbuffer. It might be that there is a window system
1105 * renderbuffer available.
1106 */
1107 if (stfb->iface)
1108 stfb->iface_stamp = p_atomic_read(&stfb->iface->stamp) - 1;
1109
1110 st_invalidate_buffers(st);
1111
1112 return TRUE;
1113 }
1114
1115 static void
1116 st_manager_destroy(struct st_manager *smapi)
1117 {
1118 struct st_manager_private *smPriv = smapi->st_manager_private;
1119
1120 if (smPriv && smPriv->stfbi_ht) {
1121 _mesa_hash_table_destroy(smPriv->stfbi_ht, NULL);
1122 mtx_destroy(&smPriv->st_mutex);
1123 free(smPriv);
1124 smapi->st_manager_private = NULL;
1125 }
1126 }
1127
1128 static unsigned
1129 get_version(struct pipe_screen *screen,
1130 struct st_config_options *options, gl_api api)
1131 {
1132 struct gl_constants consts = {0};
1133 struct gl_extensions extensions = {0};
1134 GLuint version;
1135
1136 if (_mesa_override_gl_version_contextless(&consts, &api, &version)) {
1137 return version;
1138 }
1139
1140 _mesa_init_constants(&consts, api);
1141 _mesa_init_extensions(&extensions);
1142
1143 st_init_limits(screen, &consts, &extensions);
1144 st_init_extensions(screen, &consts, &extensions, options, GL_TRUE);
1145
1146 return _mesa_get_version(&extensions, &consts, api);
1147 }
1148
1149 static void
1150 st_api_query_versions(struct st_api *stapi, struct st_manager *sm,
1151 struct st_config_options *options,
1152 int *gl_core_version,
1153 int *gl_compat_version,
1154 int *gl_es1_version,
1155 int *gl_es2_version)
1156 {
1157 *gl_core_version = get_version(sm->screen, options, API_OPENGL_CORE);
1158 *gl_compat_version = get_version(sm->screen, options, API_OPENGL_COMPAT);
1159 *gl_es1_version = get_version(sm->screen, options, API_OPENGLES);
1160 *gl_es2_version = get_version(sm->screen, options, API_OPENGLES2);
1161 }
1162
1163 static const struct st_api st_gl_api = {
1164 .name = "Mesa " PACKAGE_VERSION,
1165 .api = ST_API_OPENGL,
1166 .profile_mask = ST_PROFILE_DEFAULT_MASK |
1167 ST_PROFILE_OPENGL_CORE_MASK |
1168 ST_PROFILE_OPENGL_ES1_MASK |
1169 ST_PROFILE_OPENGL_ES2_MASK |
1170 0,
1171 .feature_mask = ST_API_FEATURE_MS_VISUALS_MASK,
1172 .destroy = st_api_destroy,
1173 .query_versions = st_api_query_versions,
1174 .create_context = st_api_create_context,
1175 .make_current = st_api_make_current,
1176 .get_current = st_api_get_current,
1177 .destroy_drawable = st_api_destroy_drawable,
1178 };
1179
1180 struct st_api *
1181 st_gl_api_create(void)
1182 {
1183 return (struct st_api *) &st_gl_api;
1184 }