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