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