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