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