gbm: Add gbm_bo_write entry point
[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 <errno.h>
29 #include "main/glheader.h"
30 #include "main/context.h"
31 #include "main/framebuffer.h"
32 #include "main/renderbuffer.h"
33 #include "main/hash.h"
34 #include "main/fbobject.h"
35 #include "main/mfeatures.h"
36 #include "main/version.h"
37 #include "swrast/s_renderbuffer.h"
38
39 #include "utils.h"
40 #include "xmlpool.h"
41
42 PUBLIC const char __driConfigOptions[] =
43 DRI_CONF_BEGIN
44 DRI_CONF_SECTION_PERFORMANCE
45 DRI_CONF_VBLANK_MODE(DRI_CONF_VBLANK_ALWAYS_SYNC)
46 /* Options correspond to DRI_CONF_BO_REUSE_DISABLED,
47 * DRI_CONF_BO_REUSE_ALL
48 */
49 DRI_CONF_OPT_BEGIN_V(bo_reuse, enum, 1, "0:1")
50 DRI_CONF_DESC_BEGIN(en, "Buffer object reuse")
51 DRI_CONF_ENUM(0, "Disable buffer object reuse")
52 DRI_CONF_ENUM(1, "Enable reuse of all sizes of buffer objects")
53 DRI_CONF_DESC_END
54 DRI_CONF_OPT_END
55
56 DRI_CONF_OPT_BEGIN(texture_tiling, bool, true)
57 DRI_CONF_DESC(en, "Enable texture tiling")
58 DRI_CONF_OPT_END
59
60 DRI_CONF_OPT_BEGIN(hiz, bool, true)
61 DRI_CONF_DESC(en, "Enable Hierarchical Z on gen6+")
62 DRI_CONF_OPT_END
63
64 DRI_CONF_OPT_BEGIN(early_z, bool, false)
65 DRI_CONF_DESC(en, "Enable early Z in classic mode (unstable, 945-only).")
66 DRI_CONF_OPT_END
67
68 DRI_CONF_OPT_BEGIN(fragment_shader, bool, true)
69 DRI_CONF_DESC(en, "Enable limited ARB_fragment_shader support on 915/945.")
70 DRI_CONF_OPT_END
71
72 DRI_CONF_SECTION_END
73 DRI_CONF_SECTION_QUALITY
74 DRI_CONF_FORCE_S3TC_ENABLE(false)
75 DRI_CONF_ALLOW_LARGE_TEXTURES(2)
76 DRI_CONF_SECTION_END
77 DRI_CONF_SECTION_DEBUG
78 DRI_CONF_NO_RAST(false)
79 DRI_CONF_ALWAYS_FLUSH_BATCH(false)
80 DRI_CONF_ALWAYS_FLUSH_CACHE(false)
81 DRI_CONF_FORCE_GLSL_EXTENSIONS_WARN(false)
82
83 DRI_CONF_OPT_BEGIN(stub_occlusion_query, bool, false)
84 DRI_CONF_DESC(en, "Enable stub ARB_occlusion_query support on 915/945.")
85 DRI_CONF_OPT_END
86
87 DRI_CONF_OPT_BEGIN(shader_precompile, bool, false)
88 DRI_CONF_DESC(en, "Perform code generation at shader link time.")
89 DRI_CONF_OPT_END
90 DRI_CONF_SECTION_END
91 DRI_CONF_END;
92
93 const GLuint __driNConfigOptions = 14;
94
95 #include "intel_batchbuffer.h"
96 #include "intel_buffers.h"
97 #include "intel_bufmgr.h"
98 #include "intel_chipset.h"
99 #include "intel_fbo.h"
100 #include "intel_mipmap_tree.h"
101 #include "intel_screen.h"
102 #include "intel_tex.h"
103 #include "intel_regions.h"
104
105 #include "i915_drm.h"
106
107 #ifdef USE_NEW_INTERFACE
108 static PFNGLXCREATECONTEXTMODES create_context_modes = NULL;
109 #endif /*USE_NEW_INTERFACE */
110
111 void
112 aub_dump_bmp(struct gl_context *ctx)
113 {
114 struct gl_framebuffer *fb = ctx->DrawBuffer;
115
116 for (int i = 0; i < fb->_NumColorDrawBuffers; i++) {
117 struct intel_renderbuffer *irb =
118 intel_renderbuffer(fb->_ColorDrawBuffers[i]);
119
120 if (irb && irb->mt) {
121 enum aub_dump_bmp_format format;
122
123 switch (irb->Base.Base.Format) {
124 case MESA_FORMAT_ARGB8888:
125 case MESA_FORMAT_XRGB8888:
126 format = AUB_DUMP_BMP_FORMAT_ARGB_8888;
127 break;
128 default:
129 continue;
130 }
131
132 drm_intel_gem_bo_aub_dump_bmp(irb->mt->region->bo,
133 irb->draw_x,
134 irb->draw_y,
135 irb->Base.Base.Width,
136 irb->Base.Base.Height,
137 format,
138 irb->mt->region->pitch *
139 irb->mt->region->cpp,
140 0);
141 }
142 }
143 }
144
145 static const __DRItexBufferExtension intelTexBufferExtension = {
146 { __DRI_TEX_BUFFER, __DRI_TEX_BUFFER_VERSION },
147 intelSetTexBuffer,
148 intelSetTexBuffer2,
149 };
150
151 static void
152 intelDRI2Flush(__DRIdrawable *drawable)
153 {
154 GET_CURRENT_CONTEXT(ctx);
155 struct intel_context *intel = intel_context(ctx);
156 if (intel == NULL)
157 return;
158
159 if (intel->gen < 4)
160 INTEL_FIREVERTICES(intel);
161
162 intel->need_throttle = true;
163
164 if (intel->batch.used)
165 intel_batchbuffer_flush(intel);
166
167 if (INTEL_DEBUG & DEBUG_AUB) {
168 aub_dump_bmp(ctx);
169 }
170 }
171
172 static const struct __DRI2flushExtensionRec intelFlushExtension = {
173 { __DRI2_FLUSH, __DRI2_FLUSH_VERSION },
174 intelDRI2Flush,
175 dri2InvalidateDrawable,
176 };
177
178 static __DRIimage *
179 intel_create_image_from_name(__DRIscreen *screen,
180 int width, int height, int format,
181 int name, int pitch, void *loaderPrivate)
182 {
183 struct intel_screen *intelScreen = screen->driverPrivate;
184 __DRIimage *image;
185 int cpp;
186
187 image = CALLOC(sizeof *image);
188 if (image == NULL)
189 return NULL;
190
191 image->dri_format = format;
192
193 switch (format) {
194 case __DRI_IMAGE_FORMAT_RGB565:
195 image->format = MESA_FORMAT_RGB565;
196 image->internal_format = GL_RGB;
197 image->data_type = GL_UNSIGNED_BYTE;
198 break;
199 case __DRI_IMAGE_FORMAT_XRGB8888:
200 image->format = MESA_FORMAT_XRGB8888;
201 image->internal_format = GL_RGB;
202 image->data_type = GL_UNSIGNED_BYTE;
203 break;
204 case __DRI_IMAGE_FORMAT_ARGB8888:
205 image->format = MESA_FORMAT_ARGB8888;
206 image->internal_format = GL_RGBA;
207 image->data_type = GL_UNSIGNED_BYTE;
208 break;
209 case __DRI_IMAGE_FORMAT_ABGR8888:
210 image->format = MESA_FORMAT_RGBA8888_REV;
211 image->internal_format = GL_RGBA;
212 image->data_type = GL_UNSIGNED_BYTE;
213 break;
214 case __DRI_IMAGE_FORMAT_XBGR8888:
215 image->format = MESA_FORMAT_RGBX8888_REV;
216 image->internal_format = GL_RGB;
217 image->data_type = GL_UNSIGNED_BYTE;
218 break;
219 default:
220 free(image);
221 return NULL;
222 }
223
224 image->data = loaderPrivate;
225 cpp = _mesa_get_format_bytes(image->format);
226
227 image->region = intel_region_alloc_for_handle(intelScreen,
228 cpp, width, height,
229 pitch, name, "image");
230 if (image->region == NULL) {
231 FREE(image);
232 return NULL;
233 }
234
235 return image;
236 }
237
238 static __DRIimage *
239 intel_create_image_from_renderbuffer(__DRIcontext *context,
240 int renderbuffer, void *loaderPrivate)
241 {
242 __DRIimage *image;
243 struct intel_context *intel = context->driverPrivate;
244 struct gl_renderbuffer *rb;
245 struct intel_renderbuffer *irb;
246
247 rb = _mesa_lookup_renderbuffer(&intel->ctx, renderbuffer);
248 if (!rb) {
249 _mesa_error(&intel->ctx,
250 GL_INVALID_OPERATION, "glRenderbufferExternalMESA");
251 return NULL;
252 }
253
254 irb = intel_renderbuffer(rb);
255 image = CALLOC(sizeof *image);
256 if (image == NULL)
257 return NULL;
258
259 image->internal_format = rb->InternalFormat;
260 image->format = rb->Format;
261 image->data_type = GL_UNSIGNED_BYTE;
262 image->data = loaderPrivate;
263 intel_region_reference(&image->region, irb->mt->region);
264
265 switch (image->format) {
266 case MESA_FORMAT_RGB565:
267 image->dri_format = __DRI_IMAGE_FORMAT_RGB565;
268 break;
269 case MESA_FORMAT_XRGB8888:
270 image->dri_format = __DRI_IMAGE_FORMAT_XRGB8888;
271 break;
272 case MESA_FORMAT_ARGB8888:
273 image->dri_format = __DRI_IMAGE_FORMAT_ARGB8888;
274 break;
275 case MESA_FORMAT_RGBA8888_REV:
276 image->dri_format = __DRI_IMAGE_FORMAT_ABGR8888;
277 break;
278 }
279
280 return image;
281 }
282
283 static void
284 intel_destroy_image(__DRIimage *image)
285 {
286 intel_region_release(&image->region);
287 FREE(image);
288 }
289
290 static __DRIimage *
291 intel_create_image(__DRIscreen *screen,
292 int width, int height, int format,
293 unsigned int use,
294 void *loaderPrivate)
295 {
296 __DRIimage *image;
297 struct intel_screen *intelScreen = screen->driverPrivate;
298 uint32_t tiling;
299 int cpp;
300
301 tiling = I915_TILING_X;
302 if (use & __DRI_IMAGE_USE_CURSOR) {
303 if (width != 64 || height != 64)
304 return NULL;
305 tiling = I915_TILING_NONE;
306 }
307
308 /* We only support write for cursor drm images */
309 if ((use & __DRI_IMAGE_USE_WRITE) &&
310 use != (__DRI_IMAGE_USE_WRITE | __DRI_IMAGE_USE_CURSOR))
311 return NULL;
312
313 image = CALLOC(sizeof *image);
314 if (image == NULL)
315 return NULL;
316
317 image->usage = use;
318 image->dri_format = format;
319
320 switch (format) {
321 case __DRI_IMAGE_FORMAT_RGB565:
322 image->format = MESA_FORMAT_RGB565;
323 image->internal_format = GL_RGB;
324 image->data_type = GL_UNSIGNED_BYTE;
325 break;
326 case __DRI_IMAGE_FORMAT_XRGB8888:
327 image->format = MESA_FORMAT_XRGB8888;
328 image->internal_format = GL_RGB;
329 image->data_type = GL_UNSIGNED_BYTE;
330 break;
331 case __DRI_IMAGE_FORMAT_ARGB8888:
332 image->format = MESA_FORMAT_ARGB8888;
333 image->internal_format = GL_RGBA;
334 image->data_type = GL_UNSIGNED_BYTE;
335 break;
336 case __DRI_IMAGE_FORMAT_ABGR8888:
337 image->format = MESA_FORMAT_RGBA8888_REV;
338 image->internal_format = GL_RGBA;
339 image->data_type = GL_UNSIGNED_BYTE;
340 break;
341 case __DRI_IMAGE_FORMAT_XBGR8888:
342 image->format = MESA_FORMAT_RGBX8888_REV;
343 image->internal_format = GL_RGB;
344 image->data_type = GL_UNSIGNED_BYTE;
345 break;
346 default:
347 free(image);
348 return NULL;
349 }
350
351 image->data = loaderPrivate;
352 cpp = _mesa_get_format_bytes(image->format);
353
354 image->region =
355 intel_region_alloc(intelScreen, tiling,
356 cpp, width, height, true);
357 if (image->region == NULL) {
358 FREE(image);
359 return NULL;
360 }
361
362 return image;
363 }
364
365 static GLboolean
366 intel_query_image(__DRIimage *image, int attrib, int *value)
367 {
368 switch (attrib) {
369 case __DRI_IMAGE_ATTRIB_STRIDE:
370 *value = image->region->pitch * image->region->cpp;
371 return true;
372 case __DRI_IMAGE_ATTRIB_HANDLE:
373 *value = image->region->bo->handle;
374 return true;
375 case __DRI_IMAGE_ATTRIB_NAME:
376 return intel_region_flink(image->region, (uint32_t *) value);
377 case __DRI_IMAGE_ATTRIB_FORMAT:
378 *value = image->dri_format;
379 return true;
380 default:
381 return false;
382 }
383 }
384
385 static __DRIimage *
386 intel_dup_image(__DRIimage *orig_image, void *loaderPrivate)
387 {
388 __DRIimage *image;
389
390 image = CALLOC(sizeof *image);
391 if (image == NULL)
392 return NULL;
393
394 intel_region_reference(&image->region, orig_image->region);
395 if (image->region == NULL) {
396 FREE(image);
397 return NULL;
398 }
399
400 image->internal_format = orig_image->internal_format;
401 image->usage = orig_image->usage;
402 image->dri_format = orig_image->dri_format;
403 image->format = orig_image->format;
404 image->data_type = orig_image->data_type;
405 image->data = loaderPrivate;
406
407 return image;
408 }
409
410 static GLboolean
411 intel_validate_usage(__DRIimage *image, unsigned int use)
412 {
413 if (use & __DRI_IMAGE_USE_CURSOR) {
414 if (image->region->width != 64 || image->region->height != 64)
415 return GL_FALSE;
416 }
417
418 /* We only support write for cursor drm images */
419 if ((use & __DRI_IMAGE_USE_WRITE) &&
420 use != (__DRI_IMAGE_USE_WRITE | __DRI_IMAGE_USE_CURSOR))
421 return GL_FALSE;
422
423 return GL_TRUE;
424 }
425
426 static int
427 intel_image_write(__DRIimage *image, const void *buf, size_t count)
428 {
429 if (image->region->map_refcount)
430 return -1;
431 if (!(image->usage & __DRI_IMAGE_USE_WRITE))
432 return -1;
433
434 drm_intel_bo_map(image->region->bo, true);
435 memcpy(image->region->bo->virtual, buf, count);
436 drm_intel_bo_unmap(image->region->bo);
437
438 return 0;
439 }
440
441 static struct __DRIimageExtensionRec intelImageExtension = {
442 { __DRI_IMAGE, 4 },
443 intel_create_image_from_name,
444 intel_create_image_from_renderbuffer,
445 intel_destroy_image,
446 intel_create_image,
447 intel_query_image,
448 intel_dup_image,
449 intel_validate_usage,
450 intel_image_write
451 };
452
453 static const __DRIextension *intelScreenExtensions[] = {
454 &intelTexBufferExtension.base,
455 &intelFlushExtension.base,
456 &intelImageExtension.base,
457 &dri2ConfigQueryExtension.base,
458 NULL
459 };
460
461 static bool
462 intel_get_param(__DRIscreen *psp, int param, int *value)
463 {
464 int ret;
465 struct drm_i915_getparam gp;
466
467 memset(&gp, 0, sizeof(gp));
468 gp.param = param;
469 gp.value = value;
470
471 ret = drmCommandWriteRead(psp->fd, DRM_I915_GETPARAM, &gp, sizeof(gp));
472 if (ret) {
473 if (ret != -EINVAL)
474 _mesa_warning(NULL, "drm_i915_getparam: %d", ret);
475 return false;
476 }
477
478 return true;
479 }
480
481 static bool
482 intel_get_boolean(__DRIscreen *psp, int param)
483 {
484 int value = 0;
485 return intel_get_param(psp, param, &value) && value;
486 }
487
488 static void
489 nop_callback(GLuint key, void *data, void *userData)
490 {
491 }
492
493 static void
494 intelDestroyScreen(__DRIscreen * sPriv)
495 {
496 struct intel_screen *intelScreen = sPriv->driverPrivate;
497
498 dri_bufmgr_destroy(intelScreen->bufmgr);
499 driDestroyOptionInfo(&intelScreen->optionCache);
500
501 /* Some regions may still have references to them at this point, so
502 * flush the hash table to prevent _mesa_DeleteHashTable() from
503 * complaining about the hash not being empty; */
504 _mesa_HashDeleteAll(intelScreen->named_regions, nop_callback, NULL);
505 _mesa_DeleteHashTable(intelScreen->named_regions);
506
507 FREE(intelScreen);
508 sPriv->driverPrivate = 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(__DRIscreen * driScrnPriv,
517 __DRIdrawable * driDrawPriv,
518 const struct gl_config * mesaVis, GLboolean isPixmap)
519 {
520 struct intel_renderbuffer *rb;
521 struct intel_screen *screen = (struct intel_screen*) driScrnPriv->driverPrivate;
522
523 if (isPixmap) {
524 return false; /* not implemented */
525 }
526 else {
527 gl_format rgbFormat;
528
529 struct gl_framebuffer *fb = CALLOC_STRUCT(gl_framebuffer);
530
531 if (!fb)
532 return false;
533
534 _mesa_initialize_window_framebuffer(fb, mesaVis);
535
536 if (mesaVis->redBits == 5)
537 rgbFormat = MESA_FORMAT_RGB565;
538 else if (mesaVis->alphaBits == 0)
539 rgbFormat = MESA_FORMAT_XRGB8888;
540 else
541 rgbFormat = MESA_FORMAT_ARGB8888;
542
543 /* setup the hardware-based renderbuffers */
544 rb = intel_create_renderbuffer(rgbFormat);
545 _mesa_add_renderbuffer(fb, BUFFER_FRONT_LEFT, &rb->Base.Base);
546
547 if (mesaVis->doubleBufferMode) {
548 rb = intel_create_renderbuffer(rgbFormat);
549 _mesa_add_renderbuffer(fb, BUFFER_BACK_LEFT, &rb->Base.Base);
550 }
551
552 /*
553 * Assert here that the gl_config has an expected depth/stencil bit
554 * combination: one of d24/s8, d16/s0, d0/s0. (See intelInitScreen2(),
555 * which constructs the advertised configs.)
556 */
557 if (mesaVis->depthBits == 24) {
558 assert(mesaVis->stencilBits == 8);
559
560 if (screen->hw_has_separate_stencil
561 && screen->dri2_has_hiz != INTEL_DRI2_HAS_HIZ_FALSE) {
562 /*
563 * Request a separate stencil buffer even if we do not yet know if
564 * the screen supports it. (See comments for
565 * enum intel_dri2_has_hiz).
566 */
567 rb = intel_create_renderbuffer(MESA_FORMAT_X8_Z24);
568 _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &rb->Base.Base);
569 rb = intel_create_renderbuffer(MESA_FORMAT_S8);
570 _mesa_add_renderbuffer(fb, BUFFER_STENCIL, &rb->Base.Base);
571 } else {
572 /*
573 * Use combined depth/stencil. Note that the renderbuffer is
574 * attached to two attachment points.
575 */
576 rb = intel_create_renderbuffer(MESA_FORMAT_S8_Z24);
577 _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &rb->Base.Base);
578 _mesa_add_renderbuffer(fb, BUFFER_STENCIL, &rb->Base.Base);
579 }
580 }
581 else if (mesaVis->depthBits == 16) {
582 assert(mesaVis->stencilBits == 0);
583 /* just 16-bit depth buffer, no hw stencil */
584 struct intel_renderbuffer *depthRb
585 = intel_create_renderbuffer(MESA_FORMAT_Z16);
586 _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &depthRb->Base.Base);
587 }
588 else {
589 assert(mesaVis->depthBits == 0);
590 assert(mesaVis->stencilBits == 0);
591 }
592
593 /* now add any/all software-based renderbuffers we may need */
594 _swrast_add_soft_renderbuffers(fb,
595 false, /* never sw color */
596 false, /* never sw depth */
597 false, /* never sw stencil */
598 mesaVis->accumRedBits > 0,
599 false, /* never sw alpha */
600 false /* never sw aux */ );
601 driDrawPriv->driverPrivate = fb;
602
603 return true;
604 }
605 }
606
607 static void
608 intelDestroyBuffer(__DRIdrawable * driDrawPriv)
609 {
610 struct gl_framebuffer *fb = driDrawPriv->driverPrivate;
611
612 _mesa_reference_framebuffer(&fb, NULL);
613 }
614
615 /* There are probably better ways to do this, such as an
616 * init-designated function to register chipids and createcontext
617 * functions.
618 */
619 extern bool
620 i830CreateContext(const struct gl_config *mesaVis,
621 __DRIcontext *driContextPriv,
622 void *sharedContextPrivate);
623
624 extern bool
625 i915CreateContext(int api,
626 const struct gl_config *mesaVis,
627 __DRIcontext *driContextPriv,
628 void *sharedContextPrivate);
629 extern bool
630 brwCreateContext(int api,
631 const struct gl_config *mesaVis,
632 __DRIcontext *driContextPriv,
633 void *sharedContextPrivate);
634
635 static GLboolean
636 intelCreateContext(gl_api api,
637 const struct gl_config * mesaVis,
638 __DRIcontext * driContextPriv,
639 unsigned major_version,
640 unsigned minor_version,
641 uint32_t flags,
642 unsigned *error,
643 void *sharedContextPrivate)
644 {
645 __DRIscreen *sPriv = driContextPriv->driScreenPriv;
646 struct intel_screen *intelScreen = sPriv->driverPrivate;
647 bool success = false;
648
649 #ifdef I915
650 if (IS_9XX(intelScreen->deviceID)) {
651 if (!IS_965(intelScreen->deviceID)) {
652 success = i915CreateContext(api, mesaVis, driContextPriv,
653 sharedContextPrivate);
654 }
655 } else {
656 intelScreen->no_vbo = true;
657 success = i830CreateContext(mesaVis, driContextPriv,
658 sharedContextPrivate);
659 }
660 #else
661 if (IS_965(intelScreen->deviceID))
662 success = brwCreateContext(api, mesaVis,
663 driContextPriv,
664 sharedContextPrivate);
665 #endif
666
667 if (success) {
668 struct gl_context *ctx =
669 (struct gl_context *) driContextPriv->driverPrivate;
670
671 _mesa_compute_version(ctx);
672 if (ctx->VersionMajor > major_version
673 || (ctx->VersionMajor == major_version
674 && ctx->VersionMinor >= minor_version)) {
675 *error = __DRI_CTX_ERROR_BAD_VERSION;
676 return true;
677 }
678
679 intelDestroyContext(driContextPriv);
680 } else {
681 *error = __DRI_CTX_ERROR_NO_MEMORY;
682 fprintf(stderr, "Unrecognized deviceID 0x%x\n", intelScreen->deviceID);
683 }
684
685 return false;
686 }
687
688 static bool
689 intel_init_bufmgr(struct intel_screen *intelScreen)
690 {
691 __DRIscreen *spriv = intelScreen->driScrnPriv;
692 int num_fences = 0;
693
694 intelScreen->no_hw = getenv("INTEL_NO_HW") != NULL;
695
696 intelScreen->bufmgr = intel_bufmgr_gem_init(spriv->fd, BATCH_SZ);
697 if (intelScreen->bufmgr == NULL) {
698 fprintf(stderr, "[%s:%u] Error initializing buffer manager.\n",
699 __func__, __LINE__);
700 return false;
701 }
702
703 if (!intel_get_param(spriv, I915_PARAM_NUM_FENCES_AVAIL, &num_fences) ||
704 num_fences == 0) {
705 fprintf(stderr, "[%s: %u] Kernel 2.6.29 required.\n", __func__, __LINE__);
706 return false;
707 }
708
709 drm_intel_bufmgr_gem_enable_fenced_relocs(intelScreen->bufmgr);
710
711 intelScreen->named_regions = _mesa_NewHashTable();
712
713 intelScreen->relaxed_relocations = 0;
714 intelScreen->relaxed_relocations |=
715 intel_get_boolean(spriv, I915_PARAM_HAS_RELAXED_DELTA) << 0;
716
717 return true;
718 }
719
720 /**
721 * Override intel_screen.hw_has_separate_stencil with environment variable
722 * INTEL_SEPARATE_STENCIL.
723 *
724 * Valid values for INTEL_SEPARATE_STENCIL are "0" and "1". If an invalid
725 * valid value is encountered, a warning is emitted and INTEL_SEPARATE_STENCIL
726 * is ignored.
727 */
728 static void
729 intel_override_separate_stencil(struct intel_screen *screen)
730 {
731 const char *s = getenv("INTEL_SEPARATE_STENCIL");
732 if (!s) {
733 return;
734 } else if (!strncmp("0", s, 2)) {
735 screen->hw_has_separate_stencil = false;
736 } else if (!strncmp("1", s, 2)) {
737 screen->hw_has_separate_stencil = true;
738 } else {
739 fprintf(stderr,
740 "warning: env variable INTEL_SEPARATE_STENCIL=\"%s\" has "
741 "invalid value and is ignored", s);
742 }
743 }
744
745 static bool
746 intel_detect_swizzling(struct intel_screen *screen)
747 {
748 drm_intel_bo *buffer;
749 unsigned long flags = 0;
750 unsigned long aligned_pitch;
751 uint32_t tiling = I915_TILING_X;
752 uint32_t swizzle_mode = 0;
753
754 buffer = drm_intel_bo_alloc_tiled(screen->bufmgr, "swizzle test",
755 64, 64, 4,
756 &tiling, &aligned_pitch, flags);
757 if (buffer == NULL)
758 return false;
759
760 drm_intel_bo_get_tiling(buffer, &tiling, &swizzle_mode);
761 drm_intel_bo_unreference(buffer);
762
763 if (swizzle_mode == I915_BIT_6_SWIZZLE_NONE)
764 return false;
765 else
766 return true;
767 }
768
769 /**
770 * This is the driver specific part of the createNewScreen entry point.
771 * Called when using DRI2.
772 *
773 * \return the struct gl_config supported by this driver
774 */
775 static const
776 __DRIconfig **intelInitScreen2(__DRIscreen *psp)
777 {
778 struct intel_screen *intelScreen;
779 GLenum fb_format[3];
780 GLenum fb_type[3];
781 unsigned int api_mask;
782
783 static const GLenum back_buffer_modes[] = {
784 GLX_NONE, GLX_SWAP_UNDEFINED_OML, GLX_SWAP_COPY_OML
785 };
786 uint8_t depth_bits[4], stencil_bits[4], msaa_samples_array[1];
787 int color;
788 __DRIconfig **configs = NULL;
789
790 /* Allocate the private area */
791 intelScreen = CALLOC(sizeof *intelScreen);
792 if (!intelScreen) {
793 fprintf(stderr, "\nERROR! Allocating private area failed\n");
794 return false;
795 }
796 /* parse information in __driConfigOptions */
797 driParseOptionInfo(&intelScreen->optionCache,
798 __driConfigOptions, __driNConfigOptions);
799
800 intelScreen->driScrnPriv = psp;
801 psp->driverPrivate = (void *) intelScreen;
802
803 if (!intel_init_bufmgr(intelScreen))
804 return false;
805
806 intelScreen->deviceID = drm_intel_bufmgr_gem_get_devid(intelScreen->bufmgr);
807
808 intelScreen->kernel_has_gen7_sol_reset =
809 intel_get_boolean(intelScreen->driScrnPriv,
810 I915_PARAM_HAS_GEN7_SOL_RESET);
811
812 if (IS_GEN7(intelScreen->deviceID)) {
813 intelScreen->gen = 7;
814 } else if (IS_GEN6(intelScreen->deviceID)) {
815 intelScreen->gen = 6;
816 } else if (IS_GEN5(intelScreen->deviceID)) {
817 intelScreen->gen = 5;
818 } else if (IS_965(intelScreen->deviceID)) {
819 intelScreen->gen = 4;
820 } else if (IS_9XX(intelScreen->deviceID)) {
821 intelScreen->gen = 3;
822 } else {
823 intelScreen->gen = 2;
824 }
825
826 intelScreen->hw_has_separate_stencil = intelScreen->gen >= 6;
827 intelScreen->hw_must_use_separate_stencil = intelScreen->gen >= 7;
828 intelScreen->dri2_has_hiz = INTEL_DRI2_HAS_HIZ_UNKNOWN;
829
830 int has_llc = 0;
831 bool success = intel_get_param(intelScreen->driScrnPriv, I915_PARAM_HAS_LLC,
832 &has_llc);
833 if (success && has_llc)
834 intelScreen->hw_has_llc = true;
835 else if (!success && intelScreen->gen >= 6)
836 intelScreen->hw_has_llc = true;
837
838 intel_override_separate_stencil(intelScreen);
839
840 api_mask = (1 << __DRI_API_OPENGL);
841 #if FEATURE_ES1
842 api_mask |= (1 << __DRI_API_GLES);
843 #endif
844 #if FEATURE_ES2
845 api_mask |= (1 << __DRI_API_GLES2);
846 #endif
847
848 if (IS_9XX(intelScreen->deviceID) || IS_965(intelScreen->deviceID))
849 psp->api_mask = api_mask;
850
851 intelScreen->hw_has_swizzling = intel_detect_swizzling(intelScreen);
852
853 psp->extensions = intelScreenExtensions;
854
855 msaa_samples_array[0] = 0;
856
857 fb_format[0] = GL_RGB;
858 fb_type[0] = GL_UNSIGNED_SHORT_5_6_5;
859
860 fb_format[1] = GL_BGR;
861 fb_type[1] = GL_UNSIGNED_INT_8_8_8_8_REV;
862
863 fb_format[2] = GL_BGRA;
864 fb_type[2] = GL_UNSIGNED_INT_8_8_8_8_REV;
865
866 depth_bits[0] = 0;
867 stencil_bits[0] = 0;
868
869 /* Generate a rich set of useful configs that do not include an
870 * accumulation buffer.
871 */
872 for (color = 0; color < ARRAY_SIZE(fb_format); color++) {
873 __DRIconfig **new_configs;
874 int depth_factor;
875
876 /* Starting with DRI2 protocol version 1.1 we can request a depth/stencil
877 * buffer that has a diffferent number of bits per pixel than the color
878 * buffer. This isn't yet supported here.
879 */
880 if (fb_type[color] == GL_UNSIGNED_SHORT_5_6_5) {
881 depth_bits[1] = 16;
882 stencil_bits[1] = 0;
883 } else {
884 depth_bits[1] = 24;
885 stencil_bits[1] = 8;
886 }
887
888 depth_factor = 2;
889
890 new_configs = driCreateConfigs(fb_format[color], fb_type[color],
891 depth_bits,
892 stencil_bits,
893 depth_factor,
894 back_buffer_modes,
895 ARRAY_SIZE(back_buffer_modes),
896 msaa_samples_array,
897 ARRAY_SIZE(msaa_samples_array),
898 false);
899 if (configs == NULL)
900 configs = new_configs;
901 else
902 configs = driConcatConfigs(configs, new_configs);
903 }
904
905 /* Generate the minimum possible set of configs that include an
906 * accumulation buffer.
907 */
908 for (color = 0; color < ARRAY_SIZE(fb_format); color++) {
909 __DRIconfig **new_configs;
910
911 if (fb_type[color] == GL_UNSIGNED_SHORT_5_6_5) {
912 depth_bits[0] = 16;
913 stencil_bits[0] = 0;
914 } else {
915 depth_bits[0] = 24;
916 stencil_bits[0] = 8;
917 }
918
919 new_configs = driCreateConfigs(fb_format[color], fb_type[color],
920 depth_bits, stencil_bits, 1,
921 back_buffer_modes + 1, 1,
922 msaa_samples_array, 1,
923 true);
924 if (configs == NULL)
925 configs = new_configs;
926 else
927 configs = driConcatConfigs(configs, new_configs);
928 }
929
930 if (configs == NULL) {
931 fprintf(stderr, "[%s:%u] Error creating FBConfig!\n", __func__,
932 __LINE__);
933 return NULL;
934 }
935
936 return (const __DRIconfig **)configs;
937 }
938
939 struct intel_buffer {
940 __DRIbuffer base;
941 struct intel_region *region;
942 };
943
944 /**
945 * \brief Get tiling format for a DRI buffer.
946 *
947 * \param attachment is the buffer's attachmet point, such as
948 * __DRI_BUFFER_DEPTH.
949 * \param out_tiling is the returned tiling format for buffer.
950 * \return false if attachment is unrecognized or is incompatible with screen.
951 */
952 static bool
953 intel_get_dri_buffer_tiling(struct intel_screen *screen,
954 uint32_t attachment,
955 uint32_t *out_tiling)
956 {
957 if (screen->gen < 4) {
958 *out_tiling = I915_TILING_X;
959 return true;
960 }
961
962 switch (attachment) {
963 case __DRI_BUFFER_DEPTH:
964 case __DRI_BUFFER_DEPTH_STENCIL:
965 case __DRI_BUFFER_HIZ:
966 *out_tiling = I915_TILING_Y;
967 return true;
968 case __DRI_BUFFER_ACCUM:
969 case __DRI_BUFFER_FRONT_LEFT:
970 case __DRI_BUFFER_FRONT_RIGHT:
971 case __DRI_BUFFER_BACK_LEFT:
972 case __DRI_BUFFER_BACK_RIGHT:
973 case __DRI_BUFFER_FAKE_FRONT_LEFT:
974 case __DRI_BUFFER_FAKE_FRONT_RIGHT:
975 *out_tiling = I915_TILING_X;
976 return true;
977 case __DRI_BUFFER_STENCIL:
978 /* The stencil buffer is W tiled. However, we request from the kernel
979 * a non-tiled buffer because the GTT is incapable of W fencing.
980 */
981 *out_tiling = I915_TILING_NONE;
982 return true;
983 default:
984 if(unlikely(INTEL_DEBUG & DEBUG_DRI)) {
985 fprintf(stderr, "error: %s: unrecognized DRI buffer attachment 0x%x\n",
986 __FUNCTION__, attachment);
987 }
988 return false;
989 }
990 }
991
992 static __DRIbuffer *
993 intelAllocateBuffer(__DRIscreen *screen,
994 unsigned attachment, unsigned format,
995 int width, int height)
996 {
997 struct intel_buffer *intelBuffer;
998 struct intel_screen *intelScreen = screen->driverPrivate;
999
1000 uint32_t tiling;
1001 uint32_t region_width;
1002 uint32_t region_height;
1003 uint32_t region_cpp;
1004
1005 bool ok = true;
1006
1007 ok = intel_get_dri_buffer_tiling(intelScreen, attachment, &tiling);
1008 if (!ok)
1009 return NULL;
1010
1011 intelBuffer = CALLOC(sizeof *intelBuffer);
1012 if (intelBuffer == NULL)
1013 return NULL;
1014
1015 if (attachment == __DRI_BUFFER_STENCIL) {
1016 /* Stencil buffers use W tiling, a tiling format that the DRM functions
1017 * don't properly account for. Therefore, when we allocate a stencil
1018 * buffer that is private to Mesa (see intel_miptree_create), we round
1019 * the height and width up to the next multiple of the tile size (64x64)
1020 * and then ask DRM to allocate an untiled buffer. Consequently, the
1021 * height and the width stored in the stencil buffer's region structure
1022 * are always multiples of 64, even if the stencil buffer itself is
1023 * smaller.
1024 *
1025 * To avoid inconsistencies between how we represent private buffers and
1026 * buffers shared with the window system, round up the height and width
1027 * for window system buffers too.
1028 */
1029 region_width = ALIGN(width, 64);
1030 region_height = ALIGN(height, 64);
1031 } else {
1032 region_width = width;
1033 region_height = height;
1034 }
1035
1036 region_cpp = format / 8;
1037
1038 intelBuffer->region = intel_region_alloc(intelScreen,
1039 tiling,
1040 region_cpp,
1041 region_width,
1042 region_height,
1043 true);
1044
1045 if (intelBuffer->region == NULL) {
1046 FREE(intelBuffer);
1047 return NULL;
1048 }
1049
1050 intel_region_flink(intelBuffer->region, &intelBuffer->base.name);
1051
1052 intelBuffer->base.attachment = attachment;
1053 intelBuffer->base.cpp = intelBuffer->region->cpp;
1054 intelBuffer->base.pitch =
1055 intelBuffer->region->pitch * intelBuffer->region->cpp;
1056
1057 return &intelBuffer->base;
1058 }
1059
1060 static void
1061 intelReleaseBuffer(__DRIscreen *screen, __DRIbuffer *buffer)
1062 {
1063 struct intel_buffer *intelBuffer = (struct intel_buffer *) buffer;
1064
1065 intel_region_release(&intelBuffer->region);
1066 free(intelBuffer);
1067 }
1068
1069
1070 const struct __DriverAPIRec driDriverAPI = {
1071 .InitScreen = intelInitScreen2,
1072 .DestroyScreen = intelDestroyScreen,
1073 .CreateContext = intelCreateContext,
1074 .DestroyContext = intelDestroyContext,
1075 .CreateBuffer = intelCreateBuffer,
1076 .DestroyBuffer = intelDestroyBuffer,
1077 .MakeCurrent = intelMakeCurrent,
1078 .UnbindContext = intelUnbindContext,
1079 .AllocateBuffer = intelAllocateBuffer,
1080 .ReleaseBuffer = intelReleaseBuffer
1081 };
1082
1083 /* This is the table of extensions that the loader will dlsym() for. */
1084 PUBLIC const __DRIextension *__driDriverExtensions[] = {
1085 &driCoreExtension.base,
1086 &driDRI2Extension.base,
1087 NULL
1088 };