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