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
62int INTEL_DEBUG = (0);
63
64const char *const i915_vendor_string = "Intel Open Source Technology Center";
65
66const char *
67i915_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
85static const GLubyte *
86intelGetString(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
105static void
106intel_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
127static void
128intel_update_image_buffers(struct intel_context *intel, __DRIdrawable *drawable);
129
130static unsigned
131intel_bits_per_pixel(const struct intel_renderbuffer *rb)
132{
133   return _mesa_get_format_bytes(intel_rb_format(rb)) * 8;
134}
135
136static void
137intel_query_dri2_buffers(struct intel_context *intel,
138			 __DRIdrawable *drawable,
139			 __DRIbuffer **buffers,
140			 int *count);
141
142static void
143intel_process_dri2_buffer(struct intel_context *intel,
144			  __DRIdrawable *drawable,
145			  __DRIbuffer *buffer,
146			  struct intel_renderbuffer *rb,
147			  const char *buffer_name);
148
149static void
150intel_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
196void
197intel_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 */
222void
223intel_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
274static void
275intel_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
288static void
289intel_viewport(struct gl_context *ctx)
290{
291    intelCalcViewport(ctx);
292}
293
294static 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
316static void
317intelInvalidateState(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
334void
335intel_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
345void
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
356static void
357intel_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
367void
368intelFinish(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
379void
380intelInitDriverFunctions(struct dd_function_table *functions)
381{
382   _mesa_init_driver_functions(functions);
383   _tnl_init_driver_draw_function(functions);
384
385   functions->Flush = intel_glFlush;
386   functions->Finish = intelFinish;
387   functions->GetString = intelGetString;
388   functions->UpdateState = intelInvalidateState;
389
390   intelInitTextureFuncs(functions);
391   intelInitTextureImageFuncs(functions);
392   intelInitTextureSubImageFuncs(functions);
393   intelInitTextureCopyImageFuncs(functions);
394   intelInitClearFuncs(functions);
395   intelInitBufferFuncs(functions);
396   intelInitPixelFuncs(functions);
397   intelInitBufferObjectFuncs(functions);
398   intel_init_syncobj_functions(functions);
399}
400
401bool
402intelInitContext(struct intel_context *intel,
403                 int api,
404                 unsigned major_version,
405                 unsigned minor_version,
406                 uint32_t flags,
407                 const struct gl_config * mesaVis,
408                 __DRIcontext * driContextPriv,
409                 void *sharedContextPrivate,
410                 struct dd_function_table *functions,
411                 unsigned *dri_ctx_error)
412{
413   struct gl_context *ctx = &intel->ctx;
414   struct gl_context *shareCtx = (struct gl_context *) sharedContextPrivate;
415   __DRIscreen *sPriv = driContextPriv->driScreenPriv;
416   struct intel_screen *intelScreen = sPriv->driverPrivate;
417   int bo_reuse_mode;
418
419   /* Can't rely on invalidate events, fall back to glViewport hack */
420   if (!driContextPriv->driScreenPriv->dri2.useInvalidate)
421      functions->Viewport = intel_noninvalidate_viewport;
422   else
423      functions->Viewport = intel_viewport;
424
425   intel->intelScreen = intelScreen;
426
427   if (!_mesa_initialize_context(&intel->ctx, api, mesaVis, shareCtx,
428                                 functions)) {
429      *dri_ctx_error = __DRI_CTX_ERROR_NO_MEMORY;
430      printf("%s: failed to init mesa context\n", __func__);
431      return false;
432   }
433
434   driContextSetFlags(&intel->ctx, flags);
435
436   driContextPriv->driverPrivate = intel;
437   intel->driContext = driContextPriv;
438
439   intel->gen = intelScreen->gen;
440
441   const int devID = intelScreen->deviceID;
442
443   intel->is_945 = IS_945(devID);
444
445   memset(&ctx->TextureFormatSupported,
446	  0, sizeof(ctx->TextureFormatSupported));
447
448   driParseConfigFiles(&intel->optionCache, &intelScreen->optionCache,
449                       sPriv->myNum, "i915", NULL);
450   intel->maxBatchSize = 4096;
451
452   /* Estimate the size of the mappable aperture into the GTT.  There's an
453    * ioctl to get the whole GTT size, but not one to get the mappable subset.
454    * It turns out it's basically always 256MB, though some ancient hardware
455    * was smaller.
456    */
457   uint32_t gtt_size = 256 * 1024 * 1024;
458   if (intel->gen == 2)
459      gtt_size = 128 * 1024 * 1024;
460
461   /* We don't want to map two objects such that a memcpy between them would
462    * just fault one mapping in and then the other over and over forever.  So
463    * we would need to divide the GTT size by 2.  Additionally, some GTT is
464    * taken up by things like the framebuffer and the ringbuffer and such, so
465    * be more conservative.
466    */
467   intel->max_gtt_map_object_size = gtt_size / 4;
468
469   intel->bufmgr = intelScreen->bufmgr;
470
471   bo_reuse_mode = driQueryOptioni(&intel->optionCache, "bo_reuse");
472   switch (bo_reuse_mode) {
473   case DRI_CONF_BO_REUSE_DISABLED:
474      break;
475   case DRI_CONF_BO_REUSE_ALL:
476      intel_bufmgr_gem_enable_reuse(intel->bufmgr);
477      break;
478   }
479
480   ctx->Const.MinLineWidth = 1.0;
481   ctx->Const.MinLineWidthAA = 1.0;
482   ctx->Const.MaxLineWidth = 7.0;
483   ctx->Const.MaxLineWidthAA = 7.0;
484   ctx->Const.LineWidthGranularity = 0.5;
485
486   ctx->Const.MinPointSize = 1.0;
487   ctx->Const.MinPointSizeAA = 1.0;
488   ctx->Const.MaxPointSize = 255.0;
489   ctx->Const.MaxPointSizeAA = 3.0;
490   ctx->Const.PointSizeGranularity = 1.0;
491
492   ctx->Const.StripTextureBorder = GL_TRUE;
493
494   /* reinitialize the context point state.
495    * It depend on constants in __struct gl_contextRec::Const
496    */
497   _mesa_init_point(ctx);
498
499   ctx->Const.MaxRenderbufferSize = 2048;
500
501   _swrast_CreateContext(ctx);
502   _vbo_CreateContext(ctx);
503   if (ctx->swrast_context) {
504      _tnl_CreateContext(ctx);
505      _swsetup_CreateContext(ctx);
506
507      /* Configure swrast to match hardware characteristics: */
508      _swrast_allow_pixel_fog(ctx, false);
509      _swrast_allow_vertex_fog(ctx, true);
510   }
511
512   _mesa_meta_init(ctx);
513
514   intel->hw_stipple = 1;
515
516   intel->RenderIndex = ~0;
517
518   intelInitExtensions(ctx);
519
520   INTEL_DEBUG = parse_debug_string(getenv("INTEL_DEBUG"), debug_control);
521   if (INTEL_DEBUG & DEBUG_BUFMGR)
522      dri_bufmgr_set_debug(intel->bufmgr, true);
523   if (INTEL_DEBUG & DEBUG_PERF)
524      intel->perf_debug = true;
525
526   if (INTEL_DEBUG & DEBUG_AUB)
527      drm_intel_bufmgr_gem_set_aub_dump(intel->bufmgr, true);
528
529   intel_batchbuffer_init(intel);
530
531   intel_fbo_init(intel);
532
533   intel->prim.primitive = ~0;
534
535   /* Force all software fallbacks */
536   if (getenv("INTEL_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
559void
560intelDestroyContext(__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, true);
603
604      _math_matrix_dtr(&intel->ViewportMatrix);
605
606      ralloc_free(intel);
607      driContextPriv->driverPrivate = NULL;
608   }
609}
610
611GLboolean
612intelUnbindContext(__DRIcontext * driContextPriv)
613{
614   /* Unset current context and dispath table */
615   _mesa_make_current(NULL, NULL, NULL);
616
617   return true;
618}
619
620GLboolean
621intelMakeCurrent(__DRIcontext * driContextPriv,
622                 __DRIdrawable * driDrawPriv,
623                 __DRIdrawable * driReadPriv)
624{
625   struct intel_context *intel;
626
627   if (driContextPriv)
628      intel = (struct intel_context *) driContextPriv->driverPrivate;
629   else
630      intel = NULL;
631
632   if (driContextPriv) {
633      struct gl_context *ctx = &intel->ctx;
634      struct gl_framebuffer *fb, *readFb;
635
636      if (driDrawPriv == NULL && driReadPriv == NULL) {
637	 fb = _mesa_get_incomplete_framebuffer();
638	 readFb = _mesa_get_incomplete_framebuffer();
639      } else {
640	 fb = driDrawPriv->driverPrivate;
641	 readFb = driReadPriv->driverPrivate;
642	 driContextPriv->dri2.draw_stamp = driDrawPriv->dri2.stamp - 1;
643	 driContextPriv->dri2.read_stamp = driReadPriv->dri2.stamp - 1;
644      }
645
646      intel_prepare_render(intel);
647      _mesa_make_current(ctx, fb, readFb);
648
649      /* We do this in intel_prepare_render() too, but intel->ctx.DrawBuffer
650       * is NULL at that point.  We can't call _mesa_makecurrent()
651       * first, since we need the buffer size for the initial
652       * viewport.  So just call intel_draw_buffer() again here. */
653      intel_draw_buffer(ctx);
654   }
655   else {
656      _mesa_make_current(NULL, NULL, NULL);
657   }
658
659   return true;
660}
661
662/**
663 * \brief Query DRI2 to obtain a DRIdrawable's buffers.
664 *
665 * To determine which DRI buffers to request, examine the renderbuffers
666 * attached to the drawable's framebuffer. Then request the buffers with
667 * DRI2GetBuffers() or DRI2GetBuffersWithFormat().
668 *
669 * This is called from intel_update_renderbuffers().
670 *
671 * \param drawable      Drawable whose buffers are queried.
672 * \param buffers       [out] List of buffers returned by DRI2 query.
673 * \param buffer_count  [out] Number of buffers returned.
674 *
675 * \see intel_update_renderbuffers()
676 * \see DRI2GetBuffers()
677 * \see DRI2GetBuffersWithFormat()
678 */
679static void
680intel_query_dri2_buffers(struct intel_context *intel,
681			 __DRIdrawable *drawable,
682			 __DRIbuffer **buffers,
683			 int *buffer_count)
684{
685   __DRIscreen *screen = intel->intelScreen->driScrnPriv;
686   struct gl_framebuffer *fb = drawable->driverPrivate;
687   int i = 0;
688   unsigned attachments[8];
689
690   struct intel_renderbuffer *front_rb;
691   struct intel_renderbuffer *back_rb;
692
693   front_rb = intel_get_renderbuffer(fb, BUFFER_FRONT_LEFT);
694   back_rb = intel_get_renderbuffer(fb, BUFFER_BACK_LEFT);
695
696   memset(attachments, 0, sizeof(attachments));
697   if ((_mesa_is_front_buffer_drawing(fb) ||
698        _mesa_is_front_buffer_reading(fb) ||
699	!back_rb) && front_rb) {
700      /* If a fake front buffer is in use, then querying for
701       * __DRI_BUFFER_FRONT_LEFT will cause the server to copy the image from
702       * the real front buffer to the fake front buffer.  So before doing the
703       * query, we need to make sure all the pending drawing has landed in the
704       * real front buffer.
705       */
706      intel_flush(&intel->ctx);
707      intel_flush_front(&intel->ctx);
708
709      attachments[i++] = __DRI_BUFFER_FRONT_LEFT;
710      attachments[i++] = intel_bits_per_pixel(front_rb);
711   } else if (front_rb && intel->front_buffer_dirty) {
712      /* We have pending front buffer rendering, but we aren't querying for a
713       * front buffer.  If the front buffer we have is a fake front buffer,
714       * the X server is going to throw it away when it processes the query.
715       * So before doing the query, make sure all the pending drawing has
716       * landed in the real front buffer.
717       */
718      intel_flush(&intel->ctx);
719      intel_flush_front(&intel->ctx);
720   }
721
722   if (back_rb) {
723      attachments[i++] = __DRI_BUFFER_BACK_LEFT;
724      attachments[i++] = intel_bits_per_pixel(back_rb);
725   }
726
727   assert(i <= ARRAY_SIZE(attachments));
728
729   *buffers = screen->dri2.loader->getBuffersWithFormat(drawable,
730							&drawable->w,
731							&drawable->h,
732							attachments, i / 2,
733							buffer_count,
734							drawable->loaderPrivate);
735}
736
737/**
738 * \brief Assign a DRI buffer's DRM region to a renderbuffer.
739 *
740 * This is called from intel_update_renderbuffers().
741 *
742 * \par Note:
743 *    DRI buffers whose attachment point is DRI2BufferStencil or
744 *    DRI2BufferDepthStencil are handled as special cases.
745 *
746 * \param buffer_name is a human readable name, such as "dri2 front buffer",
747 *        that is passed to intel_region_alloc_for_handle().
748 *
749 * \see intel_update_renderbuffers()
750 * \see intel_region_alloc_for_handle()
751 */
752static void
753intel_process_dri2_buffer(struct intel_context *intel,
754			  __DRIdrawable *drawable,
755			  __DRIbuffer *buffer,
756			  struct intel_renderbuffer *rb,
757			  const char *buffer_name)
758{
759   struct intel_region *region = NULL;
760
761   if (!rb)
762      return;
763
764   /* We try to avoid closing and reopening the same BO name, because the first
765    * use of a mapping of the buffer involves a bunch of page faulting which is
766    * moderately expensive.
767    */
768   if (rb->mt &&
769       rb->mt->region &&
770       rb->mt->region->name == buffer->name)
771      return;
772
773   if (unlikely(INTEL_DEBUG & DEBUG_DRI)) {
774      fprintf(stderr,
775	      "attaching buffer %d, at %d, cpp %d, pitch %d\n",
776	      buffer->name, buffer->attachment,
777	      buffer->cpp, buffer->pitch);
778   }
779
780   intel_miptree_release(&rb->mt);
781   region = intel_region_alloc_for_handle(intel->intelScreen,
782                                          buffer->cpp,
783                                          drawable->w,
784                                          drawable->h,
785                                          buffer->pitch,
786                                          buffer->name,
787                                          buffer_name);
788   if (!region)
789      return;
790
791   rb->mt = intel_miptree_create_for_dri2_buffer(intel,
792                                                 buffer->attachment,
793                                                 intel_rb_format(rb),
794                                                 region);
795   intel_region_release(&region);
796}
797
798/**
799 * \brief Query DRI Image loader to obtain a DRIdrawable's buffers.
800 *
801 * To determine which DRI buffers to request, examine the renderbuffers
802 * attached to the drawable's framebuffer. Then request the buffers with
803 * dri3
804 *
805 * This is called from intel_update_renderbuffers().
806 *
807 * \param drawable      Drawable whose buffers are queried.
808 * \param buffers       [out] List of buffers returned by DRI2 query.
809 * \param buffer_count  [out] Number of buffers returned.
810 *
811 * \see intel_update_renderbuffers()
812 */
813
814static void
815intel_update_image_buffer(struct intel_context *intel,
816                          __DRIdrawable *drawable,
817                          struct intel_renderbuffer *rb,
818                          __DRIimage *buffer,
819                          enum __DRIimageBufferMask buffer_type)
820{
821   struct intel_region *region = buffer->region;
822
823   if (!rb || !region)
824      return;
825
826   unsigned num_samples = rb->Base.Base.NumSamples;
827
828   if (rb->mt &&
829       rb->mt->region &&
830       rb->mt->region == region)
831      return;
832
833   intel_miptree_release(&rb->mt);
834   rb->mt = intel_miptree_create_for_image_buffer(intel,
835                                                  buffer_type,
836                                                  intel_rb_format(rb),
837                                                  num_samples,
838                                                  region);
839}
840
841
842static void
843intel_update_image_buffers(struct intel_context *intel, __DRIdrawable *drawable)
844{
845   struct gl_framebuffer *fb = drawable->driverPrivate;
846   __DRIscreen *screen = intel->intelScreen->driScrnPriv;
847   struct intel_renderbuffer *front_rb;
848   struct intel_renderbuffer *back_rb;
849   struct __DRIimageList images;
850   unsigned int format;
851   uint32_t buffer_mask = 0;
852   int ret;
853
854   front_rb = intel_get_renderbuffer(fb, BUFFER_FRONT_LEFT);
855   back_rb = intel_get_renderbuffer(fb, BUFFER_BACK_LEFT);
856
857   if (back_rb)
858      format = intel_rb_format(back_rb);
859   else if (front_rb)
860      format = intel_rb_format(front_rb);
861   else
862      return;
863
864   if (front_rb && (_mesa_is_front_buffer_drawing(fb) ||
865                    _mesa_is_front_buffer_reading(fb) || !back_rb)) {
866      buffer_mask |= __DRI_IMAGE_BUFFER_FRONT;
867   }
868
869   if (back_rb)
870      buffer_mask |= __DRI_IMAGE_BUFFER_BACK;
871
872   ret = screen->image.loader->getBuffers(drawable,
873                                          driGLFormatToImageFormat(format),
874                                          &drawable->dri2.stamp,
875                                          drawable->loaderPrivate,
876                                          buffer_mask,
877                                          &images);
878   if (!ret)
879      return;
880
881   if (images.image_mask & __DRI_IMAGE_BUFFER_FRONT) {
882      drawable->w = images.front->width;
883      drawable->h = images.front->height;
884      intel_update_image_buffer(intel,
885                                drawable,
886                                front_rb,
887                                images.front,
888                                __DRI_IMAGE_BUFFER_FRONT);
889   }
890   if (images.image_mask & __DRI_IMAGE_BUFFER_BACK) {
891      drawable->w = images.back->width;
892      drawable->h = images.back->height;
893      intel_update_image_buffer(intel,
894                                drawable,
895                                back_rb,
896                                images.back,
897                                __DRI_IMAGE_BUFFER_BACK);
898   }
899}
900