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