[intel] Add a driconf option to cache freed buffer objects for reuse.
[mesa.git] / src / mesa / drivers / dri / intel / intel_screen.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 #include "glheader.h"
29 #include "context.h"
30 #include "framebuffer.h"
31 #include "matrix.h"
32 #include "renderbuffer.h"
33 #include "simple_list.h"
34 #include "utils.h"
35 #include "vblank.h"
36 #include "xmlpool.h"
37
38
39 #include "intel_screen.h"
40
41 #include "intel_buffers.h"
42 #include "intel_tex.h"
43 #include "intel_span.h"
44 #include "intel_ioctl.h"
45 #include "intel_fbo.h"
46 #include "intel_chipset.h"
47
48 #include "i915_drm.h"
49 #include "i830_dri.h"
50 #include "intel_regions.h"
51 #include "intel_batchbuffer.h"
52 #include "intel_bufmgr_ttm.h"
53
54 PUBLIC const char __driConfigOptions[] =
55 DRI_CONF_BEGIN
56 DRI_CONF_SECTION_PERFORMANCE
57 DRI_CONF_FTHROTTLE_MODE(DRI_CONF_FTHROTTLE_IRQS)
58 DRI_CONF_VBLANK_MODE(DRI_CONF_VBLANK_DEF_INTERVAL_0)
59 /* Options correspond to DRI_CONF_BO_REUSE_DISABLED,
60 * DRI_CONF_BO_REUSE_ALL
61 */
62 DRI_CONF_OPT_BEGIN_V(bo_reuse, enum, 0, "0:1")
63 DRI_CONF_DESC_BEGIN(en, "Buffer object reuse")
64 DRI_CONF_ENUM(0, "Disable buffer object reuse")
65 DRI_CONF_ENUM(1, "Enable reuse of all sizes of buffer objects")
66 DRI_CONF_DESC_END
67 DRI_CONF_OPT_END
68 DRI_CONF_SECTION_END
69 DRI_CONF_SECTION_QUALITY
70 DRI_CONF_FORCE_S3TC_ENABLE(false)
71 DRI_CONF_ALLOW_LARGE_TEXTURES(1)
72 DRI_CONF_SECTION_END
73 DRI_CONF_SECTION_DEBUG
74 DRI_CONF_NO_RAST(false)
75 DRI_CONF_SECTION_END
76 DRI_CONF_END;
77
78 const GLuint __driNConfigOptions = 6;
79
80 #ifdef USE_NEW_INTERFACE
81 static PFNGLXCREATECONTEXTMODES create_context_modes = NULL;
82 #endif /*USE_NEW_INTERFACE */
83
84 /**
85 * Map all the memory regions described by the screen.
86 * \return GL_TRUE if success, GL_FALSE if error.
87 */
88 GLboolean
89 intelMapScreenRegions(__DRIscreenPrivate * sPriv)
90 {
91 intelScreenPrivate *intelScreen = (intelScreenPrivate *) sPriv->private;
92
93 if (intelScreen->front.handle) {
94 if (drmMap(sPriv->fd,
95 intelScreen->front.handle,
96 intelScreen->front.size,
97 (drmAddress *) & intelScreen->front.map) != 0) {
98 _mesa_problem(NULL, "drmMap(frontbuffer) failed!");
99 return GL_FALSE;
100 }
101 }
102 else {
103 _mesa_warning(NULL, "no front buffer handle in intelMapScreenRegions!");
104 }
105
106 if (0)
107 _mesa_printf("Back 0x%08x ", intelScreen->back.handle);
108 if (drmMap(sPriv->fd,
109 intelScreen->back.handle,
110 intelScreen->back.size,
111 (drmAddress *) & intelScreen->back.map) != 0) {
112 intelUnmapScreenRegions(intelScreen);
113 return GL_FALSE;
114 }
115
116 if (intelScreen->third.handle) {
117 if (0)
118 _mesa_printf("Third 0x%08x ", intelScreen->third.handle);
119 if (drmMap(sPriv->fd,
120 intelScreen->third.handle,
121 intelScreen->third.size,
122 (drmAddress *) & intelScreen->third.map) != 0) {
123 intelUnmapScreenRegions(intelScreen);
124 return GL_FALSE;
125 }
126 }
127
128 if (0)
129 _mesa_printf("Depth 0x%08x ", intelScreen->depth.handle);
130 if (drmMap(sPriv->fd,
131 intelScreen->depth.handle,
132 intelScreen->depth.size,
133 (drmAddress *) & intelScreen->depth.map) != 0) {
134 intelUnmapScreenRegions(intelScreen);
135 return GL_FALSE;
136 }
137
138 if (0)
139 _mesa_printf("TEX 0x%08x ", intelScreen->tex.handle);
140 if (intelScreen->tex.size != 0) {
141 if (drmMap(sPriv->fd,
142 intelScreen->tex.handle,
143 intelScreen->tex.size,
144 (drmAddress *) & intelScreen->tex.map) != 0) {
145 intelUnmapScreenRegions(intelScreen);
146 return GL_FALSE;
147 }
148 }
149
150 if (0)
151 printf("Mappings: front: %p back: %p third: %p depth: %p tex: %p\n",
152 intelScreen->front.map,
153 intelScreen->back.map, intelScreen->third.map,
154 intelScreen->depth.map, intelScreen->tex.map);
155 return GL_TRUE;
156 }
157
158 void
159 intelUnmapScreenRegions(intelScreenPrivate * intelScreen)
160 {
161 #define REALLY_UNMAP 1
162 if (intelScreen->front.map) {
163 #if REALLY_UNMAP
164 if (drmUnmap(intelScreen->front.map, intelScreen->front.size) != 0)
165 printf("drmUnmap front failed!\n");
166 #endif
167 intelScreen->front.map = NULL;
168 }
169 if (intelScreen->back.map) {
170 #if REALLY_UNMAP
171 if (drmUnmap(intelScreen->back.map, intelScreen->back.size) != 0)
172 printf("drmUnmap back failed!\n");
173 #endif
174 intelScreen->back.map = NULL;
175 }
176 if (intelScreen->third.map) {
177 #if REALLY_UNMAP
178 if (drmUnmap(intelScreen->third.map, intelScreen->third.size) != 0)
179 printf("drmUnmap third failed!\n");
180 #endif
181 intelScreen->third.map = NULL;
182 }
183 if (intelScreen->depth.map) {
184 #if REALLY_UNMAP
185 drmUnmap(intelScreen->depth.map, intelScreen->depth.size);
186 intelScreen->depth.map = NULL;
187 #endif
188 }
189 if (intelScreen->tex.map) {
190 #if REALLY_UNMAP
191 drmUnmap(intelScreen->tex.map, intelScreen->tex.size);
192 intelScreen->tex.map = NULL;
193 #endif
194 }
195 }
196
197
198 static void
199 intelPrintDRIInfo(intelScreenPrivate * intelScreen,
200 __DRIscreenPrivate * sPriv, I830DRIPtr gDRIPriv)
201 {
202 fprintf(stderr, "*** Front size: 0x%x offset: 0x%x pitch: %d\n",
203 intelScreen->front.size, intelScreen->front.offset,
204 intelScreen->pitch);
205 fprintf(stderr, "*** Back size: 0x%x offset: 0x%x pitch: %d\n",
206 intelScreen->back.size, intelScreen->back.offset,
207 intelScreen->pitch);
208 fprintf(stderr, "*** Depth size: 0x%x offset: 0x%x pitch: %d\n",
209 intelScreen->depth.size, intelScreen->depth.offset,
210 intelScreen->pitch);
211 fprintf(stderr, "*** Texture size: 0x%x offset: 0x%x\n",
212 intelScreen->tex.size, intelScreen->tex.offset);
213 fprintf(stderr, "*** Memory : 0x%x\n", gDRIPriv->mem);
214 }
215
216
217 static void
218 intelPrintSAREA(const struct drm_i915_sarea * sarea)
219 {
220 fprintf(stderr, "SAREA: sarea width %d height %d\n", sarea->width,
221 sarea->height);
222 fprintf(stderr, "SAREA: pitch: %d\n", sarea->pitch);
223 fprintf(stderr,
224 "SAREA: front offset: 0x%08x size: 0x%x handle: 0x%x\n",
225 sarea->front_offset, sarea->front_size,
226 (unsigned) sarea->front_handle);
227 fprintf(stderr,
228 "SAREA: back offset: 0x%08x size: 0x%x handle: 0x%x\n",
229 sarea->back_offset, sarea->back_size,
230 (unsigned) sarea->back_handle);
231 fprintf(stderr, "SAREA: depth offset: 0x%08x size: 0x%x handle: 0x%x\n",
232 sarea->depth_offset, sarea->depth_size,
233 (unsigned) sarea->depth_handle);
234 fprintf(stderr, "SAREA: tex offset: 0x%08x size: 0x%x handle: 0x%x\n",
235 sarea->tex_offset, sarea->tex_size, (unsigned) sarea->tex_handle);
236 }
237
238
239 /**
240 * A number of the screen parameters are obtained/computed from
241 * information in the SAREA. This function updates those parameters.
242 */
243 void
244 intelUpdateScreenFromSAREA(intelScreenPrivate * intelScreen,
245 struct drm_i915_sarea * sarea)
246 {
247 intelScreen->width = sarea->width;
248 intelScreen->height = sarea->height;
249 intelScreen->pitch = sarea->pitch;
250
251 intelScreen->front.offset = sarea->front_offset;
252 intelScreen->front.handle = sarea->front_handle;
253 intelScreen->front.size = sarea->front_size;
254 intelScreen->front.tiled = sarea->front_tiled;
255
256 intelScreen->back.offset = sarea->back_offset;
257 intelScreen->back.handle = sarea->back_handle;
258 intelScreen->back.size = sarea->back_size;
259 intelScreen->back.tiled = sarea->back_tiled;
260
261 if (intelScreen->driScrnPriv->ddx_version.minor >= 8) {
262 intelScreen->third.offset = sarea->third_offset;
263 intelScreen->third.handle = sarea->third_handle;
264 intelScreen->third.size = sarea->third_size;
265 intelScreen->third.tiled = sarea->third_tiled;
266 }
267
268 intelScreen->depth.offset = sarea->depth_offset;
269 intelScreen->depth.handle = sarea->depth_handle;
270 intelScreen->depth.size = sarea->depth_size;
271 intelScreen->depth.tiled = sarea->depth_tiled;
272
273 if (intelScreen->driScrnPriv->ddx_version.minor >= 9) {
274 intelScreen->front.bo_handle = sarea->front_bo_handle;
275 intelScreen->back.bo_handle = sarea->back_bo_handle;
276 intelScreen->third.bo_handle = sarea->third_bo_handle;
277 intelScreen->depth.bo_handle = sarea->depth_bo_handle;
278 } else {
279 intelScreen->front.bo_handle = -1;
280 intelScreen->back.bo_handle = -1;
281 intelScreen->third.bo_handle = -1;
282 intelScreen->depth.bo_handle = -1;
283 }
284
285 intelScreen->tex.offset = sarea->tex_offset;
286 intelScreen->logTextureGranularity = sarea->log_tex_granularity;
287 intelScreen->tex.handle = sarea->tex_handle;
288 intelScreen->tex.size = sarea->tex_size;
289
290 if (0)
291 intelPrintSAREA(sarea);
292 }
293
294 static void
295 intelHandleDrawableConfig(__DRIdrawablePrivate *dPriv,
296 __DRIDrawableConfigEvent *event)
297 {
298 struct intel_framebuffer *intel_fb = dPriv->driverPrivate;
299 struct intel_region *region = NULL;
300 struct intel_renderbuffer *rb, *depth_rb, *stencil_rb;
301 struct intel_context *intel = dPriv->driContextPriv->driverPrivate;
302 int cpp = intel->ctx.Visual.rgbBits / 8;
303 GLuint pitch = ((cpp * dPriv->w + 63) & ~63) / cpp;
304
305 rb = intel_fb->color_rb[1];
306 if (rb) {
307 region = intel_region_alloc(intel, cpp, pitch, dPriv->h);
308 intel_renderbuffer_set_region(rb, region);
309 }
310
311 rb = intel_fb->color_rb[2];
312 if (rb) {
313 region = intel_region_alloc(intel, cpp, pitch, dPriv->h);
314 intel_renderbuffer_set_region(rb, region);
315 }
316
317 depth_rb = intel_get_renderbuffer(&intel_fb->Base, BUFFER_DEPTH);
318 stencil_rb = intel_get_renderbuffer(&intel_fb->Base, BUFFER_STENCIL);
319 if (depth_rb || stencil_rb)
320 region = intel_region_alloc(intel, cpp, pitch, dPriv->h);
321 if (depth_rb)
322 intel_renderbuffer_set_region(depth_rb, region);
323 if (stencil_rb)
324 intel_renderbuffer_set_region(stencil_rb, region);
325
326 /* FIXME: Tell the X server about the regions we just allocated and
327 * attached. */
328 }
329
330 #define BUFFER_FLAG_TILED 0x0100
331
332 static void
333 intelHandleBufferAttach(__DRIdrawablePrivate *dPriv,
334 __DRIBufferAttachEvent *ba)
335 {
336 struct intel_framebuffer *intel_fb = dPriv->driverPrivate;
337 struct intel_renderbuffer *rb;
338 struct intel_region *region;
339 struct intel_context *intel = dPriv->driContextPriv->driverPrivate;
340 GLuint tiled;
341
342 switch (ba->buffer.attachment) {
343 case DRI_DRAWABLE_BUFFER_FRONT_LEFT:
344 rb = intel_fb->color_rb[0];
345 break;
346
347 case DRI_DRAWABLE_BUFFER_BACK_LEFT:
348 rb = intel_fb->color_rb[0];
349 break;
350
351 case DRI_DRAWABLE_BUFFER_DEPTH:
352 rb = intel_get_renderbuffer(&intel_fb->Base, BUFFER_DEPTH);
353 break;
354
355 case DRI_DRAWABLE_BUFFER_STENCIL:
356 rb = intel_get_renderbuffer(&intel_fb->Base, BUFFER_STENCIL);
357 break;
358
359 case DRI_DRAWABLE_BUFFER_ACCUM:
360 default:
361 fprintf(stderr, "unhandled buffer attach event, attacment type %d\n",
362 ba->buffer.attachment);
363 return;
364 }
365
366 #if 0
367 /* FIXME: Add this so we can filter out when the X server sends us
368 * attachment events for the buffers we just allocated. Need to
369 * get the BO handle for a render buffer. */
370 if (intel_renderbuffer_get_region_handle(rb) == ba->buffer.handle)
371 return;
372 #endif
373
374 tiled = (ba->buffer.flags & BUFFER_FLAG_TILED) > 0;
375 region = intel_region_alloc_for_handle(intel, ba->buffer.cpp,
376 ba->buffer.pitch / ba->buffer.cpp,
377 dPriv->h, tiled,
378 ba->buffer.handle);
379
380 intel_renderbuffer_set_region(rb, region);
381 }
382
383 static void
384 intelUpdateBuffer(__DRIdrawablePrivate *dPriv, unsigned int *event)
385 {
386 switch (DRI2_EVENT_TYPE(*event)) {
387 case DRI2_EVENT_DRAWABLE_CONFIG:
388 /* flush all current regions, allocate new ones, except front buffer */
389 intelHandleDrawableConfig(dPriv, (__DRIDrawableConfigEvent *) event);
390 break;
391
392 case DRI2_EVENT_BUFFER_ATTACH:
393 /* attach buffer if different from what we have */
394 intelHandleBufferAttach(dPriv, (__DRIBufferAttachEvent *) event);
395 break;
396 }
397 }
398
399 static const __DRItexOffsetExtension intelTexOffsetExtension = {
400 { __DRI_TEX_OFFSET },
401 intelSetTexOffset,
402 };
403
404 static const __DRItexBufferExtension intelTexBufferExtension = {
405 { __DRI_TEX_BUFFER, __DRI_TEX_BUFFER_VERSION },
406 intelSetTexBuffer,
407 };
408
409 static const __DRIextension *intelExtensions[] = {
410 &driReadDrawableExtension,
411 &driCopySubBufferExtension.base,
412 &driSwapControlExtension.base,
413 &driFrameTrackingExtension.base,
414 &driMediaStreamCounterExtension.base,
415 &intelTexOffsetExtension.base,
416 &intelTexBufferExtension.base,
417 NULL
418 };
419
420 static GLboolean
421 intel_get_param(__DRIscreenPrivate *psp, int param, int *value)
422 {
423 int ret;
424 struct drm_i915_getparam gp;
425
426 gp.param = param;
427 gp.value = value;
428
429 ret = drmCommandWriteRead(psp->fd, DRM_I915_GETPARAM, &gp, sizeof(gp));
430 if (ret) {
431 fprintf(stderr, "drm_i915_getparam: %d\n", ret);
432 return GL_FALSE;
433 }
434
435 return GL_TRUE;
436 }
437
438 static GLboolean intelInitDriver(__DRIscreenPrivate *sPriv)
439 {
440 intelScreenPrivate *intelScreen;
441 I830DRIPtr gDRIPriv = (I830DRIPtr) sPriv->pDevPriv;
442 struct drm_i915_sarea *sarea;
443
444 if (sPriv->devPrivSize != sizeof(I830DRIRec)) {
445 fprintf(stderr,
446 "\nERROR! sizeof(I830DRIRec) does not match passed size from device driver\n");
447 return GL_FALSE;
448 }
449
450 /* Allocate the private area */
451 intelScreen = (intelScreenPrivate *) CALLOC(sizeof(intelScreenPrivate));
452 if (!intelScreen) {
453 fprintf(stderr, "\nERROR! Allocating private area failed\n");
454 return GL_FALSE;
455 }
456 /* parse information in __driConfigOptions */
457 driParseOptionInfo(&intelScreen->optionCache,
458 __driConfigOptions, __driNConfigOptions);
459
460 intelScreen->driScrnPriv = sPriv;
461 sPriv->private = (void *) intelScreen;
462 intelScreen->sarea_priv_offset = gDRIPriv->sarea_priv_offset;
463 sarea = (struct drm_i915_sarea *)
464 (((GLubyte *) sPriv->pSAREA) + intelScreen->sarea_priv_offset);
465
466 intelScreen->deviceID = gDRIPriv->deviceID;
467
468 intelUpdateScreenFromSAREA(intelScreen, sarea);
469
470 if (!intelMapScreenRegions(sPriv)) {
471 fprintf(stderr, "\nERROR! mapping regions\n");
472 _mesa_free(intelScreen);
473 sPriv->private = NULL;
474 return GL_FALSE;
475 }
476
477 intelScreen->sarea_priv_offset = gDRIPriv->sarea_priv_offset;
478
479 if (0)
480 intelPrintDRIInfo(intelScreen, sPriv, gDRIPriv);
481
482 intelScreen->drmMinor = sPriv->drm_version.minor;
483
484 /* Determine if IRQs are active? */
485 if (!intel_get_param(sPriv, I915_PARAM_IRQ_ACTIVE,
486 &intelScreen->irq_active))
487 return GL_FALSE;
488
489 /* Determine if batchbuffers are allowed */
490 if (!intel_get_param(sPriv, I915_PARAM_ALLOW_BATCHBUFFER,
491 &intelScreen->allow_batchbuffer))
492 return GL_FALSE;
493
494 sPriv->extensions = intelExtensions;
495
496 return GL_TRUE;
497 }
498
499
500 static void
501 intelDestroyScreen(__DRIscreenPrivate * sPriv)
502 {
503 intelScreenPrivate *intelScreen = (intelScreenPrivate *) sPriv->private;
504
505 intelUnmapScreenRegions(intelScreen);
506
507 FREE(intelScreen);
508 sPriv->private = NULL;
509 }
510
511
512 /**
513 * This is called when we need to set up GL rendering to a new X window.
514 */
515 static GLboolean
516 intelCreateBuffer(__DRIscreenPrivate * driScrnPriv,
517 __DRIdrawablePrivate * driDrawPriv,
518 const __GLcontextModes * mesaVis, GLboolean isPixmap)
519 {
520 intelScreenPrivate *screen = (intelScreenPrivate *) driScrnPriv->private;
521
522 if (isPixmap) {
523 return GL_FALSE; /* not implemented */
524 }
525 else {
526 GLboolean swStencil = (mesaVis->stencilBits > 0 &&
527 mesaVis->depthBits != 24);
528 GLenum rgbFormat = (mesaVis->redBits == 5 ? GL_RGB5 : GL_RGBA8);
529
530 struct intel_framebuffer *intel_fb = CALLOC_STRUCT(intel_framebuffer);
531
532 if (!intel_fb)
533 return GL_FALSE;
534
535 _mesa_initialize_framebuffer(&intel_fb->Base, mesaVis);
536
537 /* setup the hardware-based renderbuffers */
538 {
539 intel_fb->color_rb[0] = intel_create_renderbuffer(rgbFormat);
540 _mesa_add_renderbuffer(&intel_fb->Base, BUFFER_FRONT_LEFT,
541 &intel_fb->color_rb[0]->Base);
542 }
543
544 if (mesaVis->doubleBufferMode) {
545 intel_fb->color_rb[1] = intel_create_renderbuffer(rgbFormat);
546 _mesa_add_renderbuffer(&intel_fb->Base, BUFFER_BACK_LEFT,
547 &intel_fb->color_rb[1]->Base);
548
549 if (screen->third.handle) {
550 struct gl_renderbuffer *tmp_rb = NULL;
551
552 intel_fb->color_rb[2] = intel_create_renderbuffer(rgbFormat);
553 _mesa_reference_renderbuffer(&tmp_rb, &intel_fb->color_rb[2]->Base);
554 }
555 }
556
557 if (mesaVis->depthBits == 24) {
558 if (mesaVis->stencilBits == 8) {
559 /* combined depth/stencil buffer */
560 struct intel_renderbuffer *depthStencilRb
561 = intel_create_renderbuffer(GL_DEPTH24_STENCIL8_EXT);
562 /* note: bind RB to two attachment points */
563 _mesa_add_renderbuffer(&intel_fb->Base, BUFFER_DEPTH,
564 &depthStencilRb->Base);
565 _mesa_add_renderbuffer(&intel_fb->Base, BUFFER_STENCIL,
566 &depthStencilRb->Base);
567 } else {
568 struct intel_renderbuffer *depthRb
569 = intel_create_renderbuffer(GL_DEPTH_COMPONENT24);
570 _mesa_add_renderbuffer(&intel_fb->Base, BUFFER_DEPTH,
571 &depthRb->Base);
572 }
573 }
574 else if (mesaVis->depthBits == 16) {
575 /* just 16-bit depth buffer, no hw stencil */
576 struct intel_renderbuffer *depthRb
577 = intel_create_renderbuffer(GL_DEPTH_COMPONENT16);
578 _mesa_add_renderbuffer(&intel_fb->Base, BUFFER_DEPTH, &depthRb->Base);
579 }
580
581 /* now add any/all software-based renderbuffers we may need */
582 _mesa_add_soft_renderbuffers(&intel_fb->Base,
583 GL_FALSE, /* never sw color */
584 GL_FALSE, /* never sw depth */
585 swStencil, mesaVis->accumRedBits > 0,
586 GL_FALSE, /* never sw alpha */
587 GL_FALSE /* never sw aux */ );
588 driDrawPriv->driverPrivate = (void *) intel_fb;
589
590 return GL_TRUE;
591 }
592 }
593
594 static void
595 intelDestroyBuffer(__DRIdrawablePrivate * driDrawPriv)
596 {
597 _mesa_unreference_framebuffer((GLframebuffer **)(&(driDrawPriv->driverPrivate)));
598 }
599
600
601 /**
602 * Get information about previous buffer swaps.
603 */
604 static int
605 intelGetSwapInfo(__DRIdrawablePrivate * dPriv, __DRIswapInfo * sInfo)
606 {
607 struct intel_framebuffer *intel_fb;
608
609 if ((dPriv == NULL) || (dPriv->driverPrivate == NULL)
610 || (sInfo == NULL)) {
611 return -1;
612 }
613
614 intel_fb = dPriv->driverPrivate;
615 sInfo->swap_count = intel_fb->swap_count;
616 sInfo->swap_ust = intel_fb->swap_ust;
617 sInfo->swap_missed_count = intel_fb->swap_missed_count;
618
619 sInfo->swap_missed_usage = (sInfo->swap_missed_count != 0)
620 ? driCalculateSwapUsage(dPriv, 0, intel_fb->swap_missed_ust)
621 : 0.0;
622
623 return 0;
624 }
625
626
627 /* There are probably better ways to do this, such as an
628 * init-designated function to register chipids and createcontext
629 * functions.
630 */
631 extern GLboolean i830CreateContext(const __GLcontextModes * mesaVis,
632 __DRIcontextPrivate * driContextPriv,
633 void *sharedContextPrivate);
634
635 extern GLboolean i915CreateContext(const __GLcontextModes * mesaVis,
636 __DRIcontextPrivate * driContextPriv,
637 void *sharedContextPrivate);
638 extern GLboolean brwCreateContext(const __GLcontextModes * mesaVis,
639 __DRIcontextPrivate * driContextPriv,
640 void *sharedContextPrivate);
641
642 static GLboolean
643 intelCreateContext(const __GLcontextModes * mesaVis,
644 __DRIcontextPrivate * driContextPriv,
645 void *sharedContextPrivate)
646 {
647 __DRIscreenPrivate *sPriv = driContextPriv->driScreenPriv;
648 intelScreenPrivate *intelScreen = (intelScreenPrivate *) sPriv->private;
649
650 #ifdef I915
651 if (IS_9XX(intelScreen->deviceID)) {
652 if (!IS_965(intelScreen->deviceID)) {
653 return i915CreateContext(mesaVis, driContextPriv,
654 sharedContextPrivate);
655 }
656 } else {
657 return i830CreateContext(mesaVis, driContextPriv, sharedContextPrivate);
658 }
659 #else
660 if (IS_965(intelScreen->deviceID))
661 return brwCreateContext(mesaVis, driContextPriv, sharedContextPrivate);
662 #endif
663 fprintf(stderr, "Unrecognized deviceID %x\n", intelScreen->deviceID);
664 return GL_FALSE;
665 }
666
667
668 static const struct __DriverAPIRec intelAPI = {
669 .DestroyScreen = intelDestroyScreen,
670 .CreateContext = intelCreateContext,
671 .DestroyContext = intelDestroyContext,
672 .CreateBuffer = intelCreateBuffer,
673 .DestroyBuffer = intelDestroyBuffer,
674 .SwapBuffers = intelSwapBuffers,
675 .MakeCurrent = intelMakeCurrent,
676 .UnbindContext = intelUnbindContext,
677 .GetSwapInfo = intelGetSwapInfo,
678 .GetDrawableMSC = driDrawableGetMSC32,
679 .WaitForMSC = driWaitForMSC32,
680 .WaitForSBC = NULL,
681 .SwapBuffersMSC = NULL,
682 .CopySubBuffer = intelCopySubBuffer,
683 .UpdateBuffer = intelUpdateBuffer,
684 };
685
686
687 static __GLcontextModes *
688 intelFillInModes(__DRIscreenPrivate *psp,
689 unsigned pixel_bits, unsigned depth_bits,
690 unsigned stencil_bits, GLboolean have_back_buffer)
691 {
692 __GLcontextModes *modes;
693 __GLcontextModes *m;
694 unsigned num_modes;
695 unsigned depth_buffer_factor;
696 unsigned back_buffer_factor;
697 GLenum fb_format;
698 GLenum fb_type;
699
700 /* GLX_SWAP_COPY_OML is only supported because the Intel driver doesn't
701 * support pageflipping at all.
702 */
703 static const GLenum back_buffer_modes[] = {
704 GLX_NONE, GLX_SWAP_UNDEFINED_OML, GLX_SWAP_COPY_OML
705 };
706
707 u_int8_t depth_bits_array[3];
708 u_int8_t stencil_bits_array[3];
709
710
711 depth_bits_array[0] = 0;
712 depth_bits_array[1] = depth_bits;
713 depth_bits_array[2] = depth_bits;
714
715 /* Just like with the accumulation buffer, always provide some modes
716 * with a stencil buffer. It will be a sw fallback, but some apps won't
717 * care about that.
718 */
719 stencil_bits_array[0] = 0;
720 stencil_bits_array[1] = 0;
721 if (depth_bits == 24)
722 stencil_bits_array[1] = (stencil_bits == 0) ? 8 : stencil_bits;
723
724 stencil_bits_array[2] = (stencil_bits == 0) ? 8 : stencil_bits;
725
726 depth_buffer_factor = ((depth_bits != 0) || (stencil_bits != 0)) ? 3 : 1;
727 back_buffer_factor = (have_back_buffer) ? 3 : 1;
728
729 num_modes = depth_buffer_factor * back_buffer_factor * 4;
730
731 if (pixel_bits == 16) {
732 fb_format = GL_RGB;
733 fb_type = GL_UNSIGNED_SHORT_5_6_5;
734 }
735 else {
736 fb_format = GL_BGRA;
737 fb_type = GL_UNSIGNED_INT_8_8_8_8_REV;
738 }
739
740 modes =
741 (*psp->contextModes->createContextModes) (num_modes,
742 sizeof(__GLcontextModes));
743 m = modes;
744 if (!driFillInModes(&m, fb_format, fb_type,
745 depth_bits_array, stencil_bits_array,
746 depth_buffer_factor, back_buffer_modes,
747 back_buffer_factor, GLX_TRUE_COLOR)) {
748 fprintf(stderr, "[%s:%u] Error creating FBConfig!\n", __func__,
749 __LINE__);
750 return NULL;
751 }
752 if (!driFillInModes(&m, fb_format, fb_type,
753 depth_bits_array, stencil_bits_array,
754 depth_buffer_factor, back_buffer_modes,
755 back_buffer_factor, GLX_DIRECT_COLOR)) {
756 fprintf(stderr, "[%s:%u] Error creating FBConfig!\n", __func__,
757 __LINE__);
758 return NULL;
759 }
760
761 /* Mark the visual as slow if there are "fake" stencil bits.
762 */
763 for (m = modes; m != NULL; m = m->next) {
764 if ((m->stencilBits != 0) && (m->stencilBits != stencil_bits)) {
765 m->visualRating = GLX_SLOW_CONFIG;
766 }
767 }
768
769 return modes;
770 }
771
772
773 /**
774 * This is the driver specific part of the createNewScreen entry point.
775 *
776 * \todo maybe fold this into intelInitDriver
777 *
778 * \return the __GLcontextModes supported by this driver
779 */
780 PUBLIC __GLcontextModes *__driDriverInitScreen(__DRIscreenPrivate *psp)
781 {
782 #ifdef I915
783 static const __DRIversion ddx_expected = { 1, 5, 0 };
784 #else
785 static const __DRIversion ddx_expected = { 1, 6, 0 };
786 #endif
787 static const __DRIversion dri_expected = { 4, 0, 0 };
788 static const __DRIversion drm_expected = { 1, 5, 0 };
789 I830DRIPtr dri_priv = (I830DRIPtr) psp->pDevPriv;
790
791 psp->DriverAPI = intelAPI;
792
793 if (!driCheckDriDdxDrmVersions2("i915",
794 &psp->dri_version, &dri_expected,
795 &psp->ddx_version, &ddx_expected,
796 &psp->drm_version, &drm_expected)) {
797 return NULL;
798 }
799
800 /* Calling driInitExtensions here, with a NULL context pointer,
801 * does not actually enable the extensions. It just makes sure
802 * that all the dispatch offsets for all the extensions that
803 * *might* be enables are known. This is needed because the
804 * dispatch offsets need to be known when _mesa_context_create is
805 * called, but we can't enable the extensions until we have a
806 * context pointer.
807 *
808 * Hello chicken. Hello egg. How are you two today?
809 */
810 intelInitExtensions(NULL, GL_TRUE);
811
812 if (!intelInitDriver(psp))
813 return NULL;
814
815 return intelFillInModes(psp, dri_priv->cpp * 8,
816 (dri_priv->cpp == 2) ? 16 : 24,
817 (dri_priv->cpp == 2) ? 0 : 8, 1);
818 }
819
820 struct intel_context *intelScreenContext(intelScreenPrivate *intelScreen)
821 {
822 /*
823 * This should probably change to have the screen allocate a dummy
824 * context at screen creation. For now just use the current context.
825 */
826
827 GET_CURRENT_CONTEXT(ctx);
828 if (ctx == NULL) {
829 _mesa_problem(NULL, "No current context in intelScreenContext\n");
830 return NULL;
831 }
832 return intel_context(ctx);
833 }
834
835 /**
836 * This is the driver specific part of the createNewScreen entry point.
837 *
838 * \return the __GLcontextModes supported by this driver
839 */
840 PUBLIC __GLcontextModes *__dri2DriverInitScreen(__DRIscreenPrivate *psp)
841 {
842 intelScreenPrivate *intelScreen;
843 __GLcontextModes *modes, *m;
844
845 psp->DriverAPI = intelAPI;
846
847 /* Calling driInitExtensions here, with a NULL context pointer,
848 * does not actually enable the extensions. It just makes sure
849 * that all the dispatch offsets for all the extensions that
850 * *might* be enables are known. This is needed because the
851 * dispatch offsets need to be known when _mesa_context_create is
852 * called, but we can't enable the extensions until we have a
853 * context pointer.
854 *
855 * Hello chicken. Hello egg. How are you two today?
856 */
857 intelInitExtensions(NULL, GL_TRUE);
858
859 /* Allocate the private area */
860 intelScreen = (intelScreenPrivate *) CALLOC(sizeof(intelScreenPrivate));
861 if (!intelScreen) {
862 fprintf(stderr, "\nERROR! Allocating private area failed\n");
863 return GL_FALSE;
864 }
865 /* parse information in __driConfigOptions */
866 driParseOptionInfo(&intelScreen->optionCache,
867 __driConfigOptions, __driNConfigOptions);
868
869 intelScreen->driScrnPriv = psp;
870 psp->private = (void *) intelScreen;
871
872 intelScreen->drmMinor = psp->drm_version.minor;
873
874 /* Determine chipset ID? */
875 if (!intel_get_param(psp, I915_PARAM_CHIPSET_ID,
876 &intelScreen->deviceID))
877 return GL_FALSE;
878
879 /* Determine if IRQs are active? */
880 if (!intel_get_param(psp, I915_PARAM_IRQ_ACTIVE,
881 &intelScreen->irq_active))
882 return GL_FALSE;
883
884 /* Determine if batchbuffers are allowed */
885 if (!intel_get_param(psp, I915_PARAM_ALLOW_BATCHBUFFER,
886 &intelScreen->allow_batchbuffer))
887 return GL_FALSE;
888
889 if (!intelScreen->allow_batchbuffer) {
890 fprintf(stderr, "batch buffer not allowed\n");
891 return GL_FALSE;
892 }
893
894 psp->extensions = intelExtensions;
895
896 modes = intelFillInModes(psp, 16, 16, 0, 1);
897 for (m = modes; m->next != NULL; m = m->next)
898 ;
899 m->next = intelFillInModes(psp, 32, 24, 8, 1);
900
901 return modes;
902 }