intel: Add a batch flush between front-buffer downsample and X protocol.
[mesa.git] / src / mesa / drivers / dri / i965 / intel_context.c
1 /**************************************************************************
2 *
3 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28
29 #include "main/glheader.h"
30 #include "main/context.h"
31 #include "main/extensions.h"
32 #include "main/fbobject.h"
33 #include "main/framebuffer.h"
34 #include "main/imports.h"
35 #include "main/renderbuffer.h"
36
37 #include "swrast/swrast.h"
38 #include "swrast_setup/swrast_setup.h"
39 #include "tnl/tnl.h"
40 #include "drivers/common/driverfuncs.h"
41 #include "drivers/common/meta.h"
42
43 #include "intel_chipset.h"
44 #include "intel_buffers.h"
45 #include "intel_tex.h"
46 #include "intel_batchbuffer.h"
47 #include "intel_pixel.h"
48 #include "intel_regions.h"
49 #include "intel_buffer_objects.h"
50 #include "intel_fbo.h"
51 #include "intel_bufmgr.h"
52 #include "intel_screen.h"
53 #include "intel_mipmap_tree.h"
54
55 #include "utils.h"
56 #include "../glsl/ralloc.h"
57
58 #ifndef INTEL_DEBUG
59 int INTEL_DEBUG = (0);
60 #endif
61
62
63 static const GLubyte *
64 intelGetString(struct gl_context * ctx, GLenum name)
65 {
66 const struct brw_context *const brw = brw_context(ctx);
67 const char *chipset;
68 static char buffer[128];
69
70 switch (name) {
71 case GL_VENDOR:
72 return (GLubyte *) "Intel Open Source Technology Center";
73 break;
74
75 case GL_RENDERER:
76 switch (brw->intelScreen->deviceID) {
77 #undef CHIPSET
78 #define CHIPSET(id, symbol, str) case id: chipset = str; break;
79 #include "pci_ids/i965_pci_ids.h"
80 default:
81 chipset = "Unknown Intel Chipset";
82 break;
83 }
84
85 (void) driGetRendererString(buffer, chipset, 0);
86 return (GLubyte *) buffer;
87
88 default:
89 return NULL;
90 }
91 }
92
93 void
94 intel_resolve_for_dri2_flush(struct brw_context *brw,
95 __DRIdrawable *drawable)
96 {
97 if (brw->gen < 6) {
98 /* MSAA and fast color clear are not supported, so don't waste time
99 * checking whether a resolve is needed.
100 */
101 return;
102 }
103
104 struct gl_framebuffer *fb = drawable->driverPrivate;
105 struct intel_renderbuffer *rb;
106
107 /* Usually, only the back buffer will need to be downsampled. However,
108 * the front buffer will also need it if the user has rendered into it.
109 */
110 static const gl_buffer_index buffers[2] = {
111 BUFFER_BACK_LEFT,
112 BUFFER_FRONT_LEFT,
113 };
114
115 for (int i = 0; i < 2; ++i) {
116 rb = intel_get_renderbuffer(fb, buffers[i]);
117 if (rb == NULL || rb->mt == NULL)
118 continue;
119 if (rb->mt->num_samples <= 1)
120 intel_miptree_resolve_color(brw, rb->mt);
121 else
122 intel_miptree_downsample(brw, rb->mt);
123 }
124 }
125
126 static void
127 intel_flush_front(struct gl_context *ctx)
128 {
129 struct brw_context *brw = brw_context(ctx);
130 __DRIcontext *driContext = brw->driContext;
131 __DRIdrawable *driDrawable = driContext->driDrawablePriv;
132 __DRIscreen *const screen = brw->intelScreen->driScrnPriv;
133
134 if (brw->front_buffer_dirty && _mesa_is_winsys_fbo(ctx->DrawBuffer)) {
135 if (screen->dri2.loader->flushFrontBuffer != NULL &&
136 driDrawable &&
137 driDrawable->loaderPrivate) {
138
139 /* Resolve before flushing FAKE_FRONT_LEFT to FRONT_LEFT.
140 *
141 * This potentially resolves both front and back buffer. It
142 * is unnecessary to resolve the back, but harms nothing except
143 * performance. And no one cares about front-buffer render
144 * performance.
145 */
146 intel_resolve_for_dri2_flush(brw, driDrawable);
147 intel_batchbuffer_flush(brw);
148
149 screen->dri2.loader->flushFrontBuffer(driDrawable,
150 driDrawable->loaderPrivate);
151
152 /* We set the dirty bit in intel_prepare_render() if we're
153 * front buffer rendering once we get there.
154 */
155 brw->front_buffer_dirty = false;
156 }
157 }
158 }
159
160 static unsigned
161 intel_bits_per_pixel(const struct intel_renderbuffer *rb)
162 {
163 return _mesa_get_format_bytes(intel_rb_format(rb)) * 8;
164 }
165
166 static void
167 intel_query_dri2_buffers(struct brw_context *brw,
168 __DRIdrawable *drawable,
169 __DRIbuffer **buffers,
170 int *count);
171
172 static void
173 intel_process_dri2_buffer(struct brw_context *brw,
174 __DRIdrawable *drawable,
175 __DRIbuffer *buffer,
176 struct intel_renderbuffer *rb,
177 const char *buffer_name);
178
179 void
180 intel_update_renderbuffers(__DRIcontext *context, __DRIdrawable *drawable)
181 {
182 struct gl_framebuffer *fb = drawable->driverPrivate;
183 struct intel_renderbuffer *rb;
184 struct brw_context *brw = context->driverPrivate;
185 __DRIbuffer *buffers = NULL;
186 int i, count;
187 const char *region_name;
188
189 /* Set this up front, so that in case our buffers get invalidated
190 * while we're getting new buffers, we don't clobber the stamp and
191 * thus ignore the invalidate. */
192 drawable->lastStamp = drawable->dri2.stamp;
193
194 if (unlikely(INTEL_DEBUG & DEBUG_DRI))
195 fprintf(stderr, "enter %s, drawable %p\n", __func__, drawable);
196
197 intel_query_dri2_buffers(brw, drawable, &buffers, &count);
198
199 if (buffers == NULL)
200 return;
201
202 for (i = 0; i < count; i++) {
203 switch (buffers[i].attachment) {
204 case __DRI_BUFFER_FRONT_LEFT:
205 rb = intel_get_renderbuffer(fb, BUFFER_FRONT_LEFT);
206 region_name = "dri2 front buffer";
207 break;
208
209 case __DRI_BUFFER_FAKE_FRONT_LEFT:
210 rb = intel_get_renderbuffer(fb, BUFFER_FRONT_LEFT);
211 region_name = "dri2 fake front buffer";
212 break;
213
214 case __DRI_BUFFER_BACK_LEFT:
215 rb = intel_get_renderbuffer(fb, BUFFER_BACK_LEFT);
216 region_name = "dri2 back buffer";
217 break;
218
219 case __DRI_BUFFER_DEPTH:
220 case __DRI_BUFFER_HIZ:
221 case __DRI_BUFFER_DEPTH_STENCIL:
222 case __DRI_BUFFER_STENCIL:
223 case __DRI_BUFFER_ACCUM:
224 default:
225 fprintf(stderr,
226 "unhandled buffer attach event, attachment type %d\n",
227 buffers[i].attachment);
228 return;
229 }
230
231 intel_process_dri2_buffer(brw, drawable, &buffers[i], rb, region_name);
232 }
233
234 driUpdateFramebufferSize(&brw->ctx, drawable);
235 }
236
237 /**
238 * intel_prepare_render should be called anywhere that curent read/drawbuffer
239 * state is required.
240 */
241 void
242 intel_prepare_render(struct brw_context *brw)
243 {
244 __DRIcontext *driContext = brw->driContext;
245 __DRIdrawable *drawable;
246
247 drawable = driContext->driDrawablePriv;
248 if (drawable && drawable->dri2.stamp != driContext->dri2.draw_stamp) {
249 if (drawable->lastStamp != drawable->dri2.stamp)
250 intel_update_renderbuffers(driContext, drawable);
251 driContext->dri2.draw_stamp = drawable->dri2.stamp;
252 }
253
254 drawable = driContext->driReadablePriv;
255 if (drawable && drawable->dri2.stamp != driContext->dri2.read_stamp) {
256 if (drawable->lastStamp != drawable->dri2.stamp)
257 intel_update_renderbuffers(driContext, drawable);
258 driContext->dri2.read_stamp = drawable->dri2.stamp;
259 }
260
261 /* If we're currently rendering to the front buffer, the rendering
262 * that will happen next will probably dirty the front buffer. So
263 * mark it as dirty here.
264 */
265 if (brw->is_front_buffer_rendering)
266 brw->front_buffer_dirty = true;
267
268 /* Wait for the swapbuffers before the one we just emitted, so we
269 * don't get too many swaps outstanding for apps that are GPU-heavy
270 * but not CPU-heavy.
271 *
272 * We're using intelDRI2Flush (called from the loader before
273 * swapbuffer) and glFlush (for front buffer rendering) as the
274 * indicator that a frame is done and then throttle when we get
275 * here as we prepare to render the next frame. At this point for
276 * round trips for swap/copy and getting new buffers are done and
277 * we'll spend less time waiting on the GPU.
278 *
279 * Unfortunately, we don't have a handle to the batch containing
280 * the swap, and getting our hands on that doesn't seem worth it,
281 * so we just us the first batch we emitted after the last swap.
282 */
283 if (brw->need_throttle && brw->first_post_swapbuffers_batch) {
284 if (!brw->disable_throttling)
285 drm_intel_bo_wait_rendering(brw->first_post_swapbuffers_batch);
286 drm_intel_bo_unreference(brw->first_post_swapbuffers_batch);
287 brw->first_post_swapbuffers_batch = NULL;
288 brw->need_throttle = false;
289 }
290 }
291
292 static void
293 intel_viewport(struct gl_context *ctx, GLint x, GLint y, GLsizei w, GLsizei h)
294 {
295 struct brw_context *brw = brw_context(ctx);
296 __DRIcontext *driContext = brw->driContext;
297
298 if (brw->saved_viewport)
299 brw->saved_viewport(ctx, x, y, w, h);
300
301 if (_mesa_is_winsys_fbo(ctx->DrawBuffer)) {
302 dri2InvalidateDrawable(driContext->driDrawablePriv);
303 dri2InvalidateDrawable(driContext->driReadablePriv);
304 }
305 }
306
307 static const struct dri_debug_control debug_control[] = {
308 { "tex", DEBUG_TEXTURE},
309 { "state", DEBUG_STATE},
310 { "ioctl", DEBUG_IOCTL},
311 { "blit", DEBUG_BLIT},
312 { "mip", DEBUG_MIPTREE},
313 { "fall", DEBUG_PERF},
314 { "perf", DEBUG_PERF},
315 { "bat", DEBUG_BATCH},
316 { "pix", DEBUG_PIXEL},
317 { "buf", DEBUG_BUFMGR},
318 { "reg", DEBUG_REGION},
319 { "fbo", DEBUG_FBO},
320 { "fs", DEBUG_WM },
321 { "gs", DEBUG_GS},
322 { "sync", DEBUG_SYNC},
323 { "prim", DEBUG_PRIMS },
324 { "vert", DEBUG_VERTS },
325 { "dri", DEBUG_DRI },
326 { "sf", DEBUG_SF },
327 { "stats", DEBUG_STATS },
328 { "wm", DEBUG_WM },
329 { "urb", DEBUG_URB },
330 { "vs", DEBUG_VS },
331 { "clip", DEBUG_CLIP },
332 { "aub", DEBUG_AUB },
333 { "shader_time", DEBUG_SHADER_TIME },
334 { "no16", DEBUG_NO16 },
335 { "blorp", DEBUG_BLORP },
336 { "vue", DEBUG_VUE },
337 { NULL, 0 }
338 };
339
340
341 static void
342 intelInvalidateState(struct gl_context * ctx, GLuint new_state)
343 {
344 struct brw_context *brw = brw_context(ctx);
345
346 if (ctx->swrast_context)
347 _swrast_InvalidateState(ctx, new_state);
348 _vbo_InvalidateState(ctx, new_state);
349
350 brw->NewGLState |= new_state;
351 }
352
353 static void
354 intel_glFlush(struct gl_context *ctx)
355 {
356 struct brw_context *brw = brw_context(ctx);
357
358 intel_batchbuffer_flush(brw);
359 intel_flush_front(ctx);
360 if (brw->is_front_buffer_rendering)
361 brw->need_throttle = true;
362 }
363
364 void
365 intelFinish(struct gl_context * ctx)
366 {
367 struct brw_context *brw = brw_context(ctx);
368
369 intel_batchbuffer_flush(brw);
370 intel_flush_front(ctx);
371
372 if (brw->batch.last_bo)
373 drm_intel_bo_wait_rendering(brw->batch.last_bo);
374 }
375
376 void
377 intelInitDriverFunctions(struct dd_function_table *functions)
378 {
379 _mesa_init_driver_functions(functions);
380
381 functions->Flush = intel_glFlush;
382 functions->Finish = intelFinish;
383 functions->GetString = intelGetString;
384 functions->UpdateState = intelInvalidateState;
385
386 intelInitTextureFuncs(functions);
387 intelInitTextureImageFuncs(functions);
388 intelInitTextureSubImageFuncs(functions);
389 intelInitTextureCopyImageFuncs(functions);
390 intelInitClearFuncs(functions);
391 intelInitBufferFuncs(functions);
392 intelInitPixelFuncs(functions);
393 intelInitBufferObjectFuncs(functions);
394 intel_init_syncobj_functions(functions);
395 brw_init_object_purgeable_functions(functions);
396 }
397
398 static bool
399 validate_context_version(struct intel_screen *screen,
400 int mesa_api,
401 unsigned major_version,
402 unsigned minor_version,
403 unsigned *dri_ctx_error)
404 {
405 unsigned req_version = 10 * major_version + minor_version;
406 unsigned max_version = 0;
407
408 switch (mesa_api) {
409 case API_OPENGL_COMPAT:
410 max_version = screen->max_gl_compat_version;
411 break;
412 case API_OPENGL_CORE:
413 max_version = screen->max_gl_core_version;
414 break;
415 case API_OPENGLES:
416 max_version = screen->max_gl_es1_version;
417 break;
418 case API_OPENGLES2:
419 max_version = screen->max_gl_es2_version;
420 break;
421 default:
422 max_version = 0;
423 break;
424 }
425
426 if (max_version == 0) {
427 *dri_ctx_error = __DRI_CTX_ERROR_BAD_API;
428 return false;
429 } else if (req_version > max_version) {
430 *dri_ctx_error = __DRI_CTX_ERROR_BAD_VERSION;
431 return false;
432 }
433
434 return true;
435 }
436
437 bool
438 intelInitContext(struct brw_context *brw,
439 int api,
440 unsigned major_version,
441 unsigned minor_version,
442 const struct gl_config * mesaVis,
443 __DRIcontext * driContextPriv,
444 void *sharedContextPrivate,
445 struct dd_function_table *functions,
446 unsigned *dri_ctx_error)
447 {
448 struct gl_context *ctx = &brw->ctx;
449 struct gl_context *shareCtx = (struct gl_context *) sharedContextPrivate;
450 __DRIscreen *sPriv = driContextPriv->driScreenPriv;
451 struct intel_screen *intelScreen = sPriv->driverPrivate;
452 int bo_reuse_mode;
453 struct gl_config visual;
454
455 /* we can't do anything without a connection to the device */
456 if (intelScreen->bufmgr == NULL) {
457 *dri_ctx_error = __DRI_CTX_ERROR_NO_MEMORY;
458 return false;
459 }
460
461 if (!validate_context_version(intelScreen,
462 api, major_version, minor_version,
463 dri_ctx_error))
464 return false;
465
466 /* Can't rely on invalidate events, fall back to glViewport hack */
467 if (!driContextPriv->driScreenPriv->dri2.useInvalidate) {
468 brw->saved_viewport = functions->Viewport;
469 functions->Viewport = intel_viewport;
470 }
471
472 if (mesaVis == NULL) {
473 memset(&visual, 0, sizeof visual);
474 mesaVis = &visual;
475 }
476
477 brw->intelScreen = intelScreen;
478 brw->bufmgr = intelScreen->bufmgr;
479
480 if (!_mesa_initialize_context(&brw->ctx, api, mesaVis, shareCtx,
481 functions)) {
482 *dri_ctx_error = __DRI_CTX_ERROR_NO_MEMORY;
483 printf("%s: failed to init mesa context\n", __FUNCTION__);
484 return false;
485 }
486
487 driContextPriv->driverPrivate = brw;
488 brw->driContext = driContextPriv;
489
490 brw->gen = intelScreen->gen;
491
492 const int devID = intelScreen->deviceID;
493 if (IS_SNB_GT1(devID) || IS_IVB_GT1(devID) || IS_HSW_GT1(devID))
494 brw->gt = 1;
495 else if (IS_SNB_GT2(devID) || IS_IVB_GT2(devID) || IS_HSW_GT2(devID))
496 brw->gt = 2;
497 else if (IS_HSW_GT3(devID))
498 brw->gt = 3;
499 else
500 brw->gt = 0;
501
502 if (IS_HASWELL(devID)) {
503 brw->is_haswell = true;
504 } else if (IS_BAYTRAIL(devID)) {
505 brw->is_baytrail = true;
506 brw->gt = 1;
507 } else if (IS_G4X(devID)) {
508 brw->is_g4x = true;
509 }
510
511 brw->has_separate_stencil = brw->intelScreen->hw_has_separate_stencil;
512 brw->must_use_separate_stencil = brw->intelScreen->hw_must_use_separate_stencil;
513 brw->has_hiz = brw->gen >= 6;
514 brw->has_llc = brw->intelScreen->hw_has_llc;
515 brw->has_swizzling = brw->intelScreen->hw_has_swizzling;
516
517 memset(&ctx->TextureFormatSupported,
518 0, sizeof(ctx->TextureFormatSupported));
519
520 driParseConfigFiles(&brw->optionCache, &intelScreen->optionCache,
521 sPriv->myNum, "i965");
522
523 /* Estimate the size of the mappable aperture into the GTT. There's an
524 * ioctl to get the whole GTT size, but not one to get the mappable subset.
525 * It turns out it's basically always 256MB, though some ancient hardware
526 * was smaller.
527 */
528 uint32_t gtt_size = 256 * 1024 * 1024;
529
530 /* We don't want to map two objects such that a memcpy between them would
531 * just fault one mapping in and then the other over and over forever. So
532 * we would need to divide the GTT size by 2. Additionally, some GTT is
533 * taken up by things like the framebuffer and the ringbuffer and such, so
534 * be more conservative.
535 */
536 brw->max_gtt_map_object_size = gtt_size / 4;
537
538 bo_reuse_mode = driQueryOptioni(&brw->optionCache, "bo_reuse");
539 switch (bo_reuse_mode) {
540 case DRI_CONF_BO_REUSE_DISABLED:
541 break;
542 case DRI_CONF_BO_REUSE_ALL:
543 intel_bufmgr_gem_enable_reuse(brw->bufmgr);
544 break;
545 }
546
547 /* Initialize the software rasterizer and helper modules.
548 *
549 * As of GL 3.1 core, the gen4+ driver doesn't need the swrast context for
550 * software fallbacks (which we have to support on legacy GL to do weird
551 * glDrawPixels(), glBitmap(), and other functions).
552 */
553 if (api != API_OPENGL_CORE && api != API_OPENGLES2) {
554 _swrast_CreateContext(ctx);
555 }
556
557 _vbo_CreateContext(ctx);
558 if (ctx->swrast_context) {
559 _tnl_CreateContext(ctx);
560 _swsetup_CreateContext(ctx);
561
562 /* Configure swrast to match hardware characteristics: */
563 _swrast_allow_pixel_fog(ctx, false);
564 _swrast_allow_vertex_fog(ctx, true);
565 }
566
567 _mesa_meta_init(ctx);
568
569 intelInitExtensions(ctx);
570
571 INTEL_DEBUG = driParseDebugString(getenv("INTEL_DEBUG"), debug_control);
572 if (INTEL_DEBUG & DEBUG_BUFMGR)
573 dri_bufmgr_set_debug(brw->bufmgr, true);
574 if ((INTEL_DEBUG & DEBUG_SHADER_TIME) && brw->gen < 7) {
575 fprintf(stderr,
576 "shader_time debugging requires gen7 (Ivybridge) or better.\n");
577 INTEL_DEBUG &= ~DEBUG_SHADER_TIME;
578 }
579 if (INTEL_DEBUG & DEBUG_PERF)
580 brw->perf_debug = true;
581
582 if (INTEL_DEBUG & DEBUG_AUB)
583 drm_intel_bufmgr_gem_set_aub_dump(brw->bufmgr, true);
584
585 intel_batchbuffer_init(brw);
586
587 intel_fbo_init(brw);
588
589 if (!driQueryOptionb(&brw->optionCache, "hiz")) {
590 brw->has_hiz = false;
591 /* On gen6, you can only do separate stencil with HIZ. */
592 if (brw->gen == 6)
593 brw->has_separate_stencil = false;
594 }
595
596 if (driQueryOptionb(&brw->optionCache, "always_flush_batch")) {
597 fprintf(stderr, "flushing batchbuffer before/after each draw call\n");
598 brw->always_flush_batch = 1;
599 }
600
601 if (driQueryOptionb(&brw->optionCache, "always_flush_cache")) {
602 fprintf(stderr, "flushing GPU caches before/after each draw call\n");
603 brw->always_flush_cache = 1;
604 }
605
606 if (driQueryOptionb(&brw->optionCache, "disable_throttling")) {
607 fprintf(stderr, "disabling flush throttling\n");
608 brw->disable_throttling = 1;
609 }
610
611 return true;
612 }
613
614 void
615 intelDestroyContext(__DRIcontext * driContextPriv)
616 {
617 struct brw_context *brw =
618 (struct brw_context *) driContextPriv->driverPrivate;
619 struct gl_context *ctx = &brw->ctx;
620
621 assert(brw); /* should never be null */
622 if (brw) {
623 /* Dump a final BMP in case the application doesn't call SwapBuffers */
624 if (INTEL_DEBUG & DEBUG_AUB) {
625 intel_batchbuffer_flush(brw);
626 aub_dump_bmp(&brw->ctx);
627 }
628
629 _mesa_meta_free(&brw->ctx);
630
631 brw->vtbl.destroy(brw);
632
633 if (ctx->swrast_context) {
634 _swsetup_DestroyContext(&brw->ctx);
635 _tnl_DestroyContext(&brw->ctx);
636 }
637 _vbo_DestroyContext(&brw->ctx);
638
639 if (ctx->swrast_context)
640 _swrast_DestroyContext(&brw->ctx);
641
642 intel_batchbuffer_free(brw);
643
644 drm_intel_bo_unreference(brw->first_post_swapbuffers_batch);
645 brw->first_post_swapbuffers_batch = NULL;
646
647 driDestroyOptionCache(&brw->optionCache);
648
649 /* free the Mesa context */
650 _mesa_free_context_data(&brw->ctx);
651
652 ralloc_free(brw);
653 driContextPriv->driverPrivate = NULL;
654 }
655 }
656
657 GLboolean
658 intelUnbindContext(__DRIcontext * driContextPriv)
659 {
660 /* Unset current context and dispath table */
661 _mesa_make_current(NULL, NULL, NULL);
662
663 return true;
664 }
665
666 /**
667 * Fixes up the context for GLES23 with our default-to-sRGB-capable behavior
668 * on window system framebuffers.
669 *
670 * Desktop GL is fairly reasonable in its handling of sRGB: You can ask if
671 * your renderbuffer can do sRGB encode, and you can flip a switch that does
672 * sRGB encode if the renderbuffer can handle it. You can ask specifically
673 * for a visual where you're guaranteed to be capable, but it turns out that
674 * everyone just makes all their ARGB8888 visuals capable and doesn't offer
675 * incapable ones, becuase there's no difference between the two in resources
676 * used. Applications thus get built that accidentally rely on the default
677 * visual choice being sRGB, so we make ours sRGB capable. Everything sounds
678 * great...
679 *
680 * But for GLES2/3, they decided that it was silly to not turn on sRGB encode
681 * for sRGB renderbuffers you made with the GL_EXT_texture_sRGB equivalent.
682 * So they removed the enable knob and made it "if the renderbuffer is sRGB
683 * capable, do sRGB encode". Then, for your window system renderbuffers, you
684 * can ask for sRGB visuals and get sRGB encode, or not ask for sRGB visuals
685 * and get no sRGB encode (assuming that both kinds of visual are available).
686 * Thus our choice to support sRGB by default on our visuals for desktop would
687 * result in broken rendering of GLES apps that aren't expecting sRGB encode.
688 *
689 * Unfortunately, renderbuffer setup happens before a context is created. So
690 * in intel_screen.c we always set up sRGB, and here, if you're a GLES2/3
691 * context (without an sRGB visual, though we don't have sRGB visuals exposed
692 * yet), we go turn that back off before anyone finds out.
693 */
694 static void
695 intel_gles3_srgb_workaround(struct brw_context *brw,
696 struct gl_framebuffer *fb)
697 {
698 struct gl_context *ctx = &brw->ctx;
699
700 if (_mesa_is_desktop_gl(ctx) || !fb->Visual.sRGBCapable)
701 return;
702
703 /* Some day when we support the sRGB capable bit on visuals available for
704 * GLES, we'll need to respect that and not disable things here.
705 */
706 fb->Visual.sRGBCapable = false;
707 for (int i = 0; i < BUFFER_COUNT; i++) {
708 if (fb->Attachment[i].Renderbuffer &&
709 fb->Attachment[i].Renderbuffer->Format == MESA_FORMAT_SARGB8) {
710 fb->Attachment[i].Renderbuffer->Format = MESA_FORMAT_ARGB8888;
711 }
712 }
713 }
714
715 GLboolean
716 intelMakeCurrent(__DRIcontext * driContextPriv,
717 __DRIdrawable * driDrawPriv,
718 __DRIdrawable * driReadPriv)
719 {
720 struct brw_context *brw;
721 GET_CURRENT_CONTEXT(curCtx);
722
723 if (driContextPriv)
724 brw = (struct brw_context *) driContextPriv->driverPrivate;
725 else
726 brw = NULL;
727
728 /* According to the glXMakeCurrent() man page: "Pending commands to
729 * the previous context, if any, are flushed before it is released."
730 * But only flush if we're actually changing contexts.
731 */
732 if (brw_context(curCtx) && brw_context(curCtx) != brw) {
733 _mesa_flush(curCtx);
734 }
735
736 if (driContextPriv) {
737 struct gl_context *ctx = &brw->ctx;
738 struct gl_framebuffer *fb, *readFb;
739
740 if (driDrawPriv == NULL && driReadPriv == NULL) {
741 fb = _mesa_get_incomplete_framebuffer();
742 readFb = _mesa_get_incomplete_framebuffer();
743 } else {
744 fb = driDrawPriv->driverPrivate;
745 readFb = driReadPriv->driverPrivate;
746 driContextPriv->dri2.draw_stamp = driDrawPriv->dri2.stamp - 1;
747 driContextPriv->dri2.read_stamp = driReadPriv->dri2.stamp - 1;
748 }
749
750 /* The sRGB workaround changes the renderbuffer's format. We must change
751 * the format before the renderbuffer's miptree get's allocated, otherwise
752 * the formats of the renderbuffer and its miptree will differ.
753 */
754 intel_gles3_srgb_workaround(brw, fb);
755 intel_gles3_srgb_workaround(brw, readFb);
756
757 intel_prepare_render(brw);
758 _mesa_make_current(ctx, fb, readFb);
759 }
760 else {
761 _mesa_make_current(NULL, NULL, NULL);
762 }
763
764 return true;
765 }
766
767 /**
768 * \brief Query DRI2 to obtain a DRIdrawable's buffers.
769 *
770 * To determine which DRI buffers to request, examine the renderbuffers
771 * attached to the drawable's framebuffer. Then request the buffers with
772 * DRI2GetBuffers() or DRI2GetBuffersWithFormat().
773 *
774 * This is called from intel_update_renderbuffers().
775 *
776 * \param drawable Drawable whose buffers are queried.
777 * \param buffers [out] List of buffers returned by DRI2 query.
778 * \param buffer_count [out] Number of buffers returned.
779 *
780 * \see intel_update_renderbuffers()
781 * \see DRI2GetBuffers()
782 * \see DRI2GetBuffersWithFormat()
783 */
784 static void
785 intel_query_dri2_buffers(struct brw_context *brw,
786 __DRIdrawable *drawable,
787 __DRIbuffer **buffers,
788 int *buffer_count)
789 {
790 __DRIscreen *screen = brw->intelScreen->driScrnPriv;
791 struct gl_framebuffer *fb = drawable->driverPrivate;
792 int i = 0;
793 unsigned attachments[8];
794
795 struct intel_renderbuffer *front_rb;
796 struct intel_renderbuffer *back_rb;
797
798 front_rb = intel_get_renderbuffer(fb, BUFFER_FRONT_LEFT);
799 back_rb = intel_get_renderbuffer(fb, BUFFER_BACK_LEFT);
800
801 memset(attachments, 0, sizeof(attachments));
802 if ((brw->is_front_buffer_rendering ||
803 brw->is_front_buffer_reading ||
804 !back_rb) && front_rb) {
805 /* If a fake front buffer is in use, then querying for
806 * __DRI_BUFFER_FRONT_LEFT will cause the server to copy the image from
807 * the real front buffer to the fake front buffer. So before doing the
808 * query, we need to make sure all the pending drawing has landed in the
809 * real front buffer.
810 */
811 intel_batchbuffer_flush(brw);
812 intel_flush_front(&brw->ctx);
813
814 attachments[i++] = __DRI_BUFFER_FRONT_LEFT;
815 attachments[i++] = intel_bits_per_pixel(front_rb);
816 } else if (front_rb && brw->front_buffer_dirty) {
817 /* We have pending front buffer rendering, but we aren't querying for a
818 * front buffer. If the front buffer we have is a fake front buffer,
819 * the X server is going to throw it away when it processes the query.
820 * So before doing the query, make sure all the pending drawing has
821 * landed in the real front buffer.
822 */
823 intel_batchbuffer_flush(brw);
824 intel_flush_front(&brw->ctx);
825 }
826
827 if (back_rb) {
828 attachments[i++] = __DRI_BUFFER_BACK_LEFT;
829 attachments[i++] = intel_bits_per_pixel(back_rb);
830 }
831
832 assert(i <= ARRAY_SIZE(attachments));
833
834 *buffers = screen->dri2.loader->getBuffersWithFormat(drawable,
835 &drawable->w,
836 &drawable->h,
837 attachments, i / 2,
838 buffer_count,
839 drawable->loaderPrivate);
840 }
841
842 /**
843 * \brief Assign a DRI buffer's DRM region to a renderbuffer.
844 *
845 * This is called from intel_update_renderbuffers().
846 *
847 * \par Note:
848 * DRI buffers whose attachment point is DRI2BufferStencil or
849 * DRI2BufferDepthStencil are handled as special cases.
850 *
851 * \param buffer_name is a human readable name, such as "dri2 front buffer",
852 * that is passed to intel_region_alloc_for_handle().
853 *
854 * \see intel_update_renderbuffers()
855 * \see intel_region_alloc_for_handle()
856 */
857 static void
858 intel_process_dri2_buffer(struct brw_context *brw,
859 __DRIdrawable *drawable,
860 __DRIbuffer *buffer,
861 struct intel_renderbuffer *rb,
862 const char *buffer_name)
863 {
864 struct intel_region *region = NULL;
865
866 if (!rb)
867 return;
868
869 unsigned num_samples = rb->Base.Base.NumSamples;
870
871 /* We try to avoid closing and reopening the same BO name, because the first
872 * use of a mapping of the buffer involves a bunch of page faulting which is
873 * moderately expensive.
874 */
875 if (num_samples == 0) {
876 if (rb->mt &&
877 rb->mt->region &&
878 rb->mt->region->name == buffer->name)
879 return;
880 } else {
881 if (rb->mt &&
882 rb->mt->singlesample_mt &&
883 rb->mt->singlesample_mt->region &&
884 rb->mt->singlesample_mt->region->name == buffer->name)
885 return;
886 }
887
888 if (unlikely(INTEL_DEBUG & DEBUG_DRI)) {
889 fprintf(stderr,
890 "attaching buffer %d, at %d, cpp %d, pitch %d\n",
891 buffer->name, buffer->attachment,
892 buffer->cpp, buffer->pitch);
893 }
894
895 intel_miptree_release(&rb->mt);
896 region = intel_region_alloc_for_handle(brw->intelScreen,
897 buffer->cpp,
898 drawable->w,
899 drawable->h,
900 buffer->pitch,
901 buffer->name,
902 buffer_name);
903 if (!region)
904 return;
905
906 rb->mt = intel_miptree_create_for_dri2_buffer(brw,
907 buffer->attachment,
908 intel_rb_format(rb),
909 num_samples,
910 region);
911 intel_region_release(&region);
912 }