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