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