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