i915: Fix wrong sizeof argument in i915_update_tex_unit.
[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 <time.h>
30 #include "main/glheader.h"
31 #include "main/context.h"
32 #include "main/framebuffer.h"
33 #include "main/renderbuffer.h"
34 #include "main/hash.h"
35 #include "main/fbobject.h"
36 #include "main/mfeatures.h"
37 #include "main/version.h"
38 #include "swrast/s_renderbuffer.h"
39
40 #include "utils.h"
41 #include "xmlpool.h"
42
43 PUBLIC const char __driConfigOptions[] =
44 DRI_CONF_BEGIN
45 DRI_CONF_SECTION_PERFORMANCE
46 DRI_CONF_VBLANK_MODE(DRI_CONF_VBLANK_ALWAYS_SYNC)
47 /* Options correspond to DRI_CONF_BO_REUSE_DISABLED,
48 * DRI_CONF_BO_REUSE_ALL
49 */
50 DRI_CONF_OPT_BEGIN_V(bo_reuse, enum, 1, "0:1")
51 DRI_CONF_DESC_BEGIN(en, "Buffer object reuse")
52 DRI_CONF_ENUM(0, "Disable buffer object reuse")
53 DRI_CONF_ENUM(1, "Enable reuse of all sizes of buffer objects")
54 DRI_CONF_DESC_END
55 DRI_CONF_OPT_END
56
57 DRI_CONF_OPT_BEGIN(texture_tiling, bool, true)
58 DRI_CONF_DESC(en, "Enable texture tiling")
59 DRI_CONF_OPT_END
60
61 DRI_CONF_OPT_BEGIN(hiz, bool, true)
62 DRI_CONF_DESC(en, "Enable Hierarchical Z on gen6+")
63 DRI_CONF_OPT_END
64
65 DRI_CONF_OPT_BEGIN(early_z, bool, false)
66 DRI_CONF_DESC(en, "Enable early Z in classic mode (unstable, 945-only).")
67 DRI_CONF_OPT_END
68
69 DRI_CONF_OPT_BEGIN(fragment_shader, bool, true)
70 DRI_CONF_DESC(en, "Enable limited ARB_fragment_shader support on 915/945.")
71 DRI_CONF_OPT_END
72
73 DRI_CONF_SECTION_END
74 DRI_CONF_SECTION_QUALITY
75 DRI_CONF_FORCE_S3TC_ENABLE(false)
76 DRI_CONF_ALLOW_LARGE_TEXTURES(2)
77 DRI_CONF_SECTION_END
78 DRI_CONF_SECTION_DEBUG
79 DRI_CONF_NO_RAST(false)
80 DRI_CONF_ALWAYS_FLUSH_BATCH(false)
81 DRI_CONF_ALWAYS_FLUSH_CACHE(false)
82 DRI_CONF_FORCE_GLSL_EXTENSIONS_WARN(false)
83 DRI_CONF_DISABLE_BLEND_FUNC_EXTENDED(false)
84
85 DRI_CONF_OPT_BEGIN(stub_occlusion_query, bool, false)
86 DRI_CONF_DESC(en, "Enable stub ARB_occlusion_query support on 915/945.")
87 DRI_CONF_OPT_END
88
89 DRI_CONF_OPT_BEGIN(shader_precompile, bool, true)
90 DRI_CONF_DESC(en, "Perform code generation at shader link time.")
91 DRI_CONF_OPT_END
92 DRI_CONF_SECTION_END
93 DRI_CONF_END;
94
95 const GLuint __driNConfigOptions = 15;
96
97 #include "intel_batchbuffer.h"
98 #include "intel_buffers.h"
99 #include "intel_bufmgr.h"
100 #include "intel_chipset.h"
101 #include "intel_fbo.h"
102 #include "intel_mipmap_tree.h"
103 #include "intel_screen.h"
104 #include "intel_tex.h"
105 #include "intel_regions.h"
106
107 #include "i915_drm.h"
108
109 #ifdef USE_NEW_INTERFACE
110 static PFNGLXCREATECONTEXTMODES create_context_modes = NULL;
111 #endif /*USE_NEW_INTERFACE */
112
113 /**
114 * For debugging purposes, this returns a time in seconds.
115 */
116 double
117 get_time(void)
118 {
119 struct timespec tp;
120
121 clock_gettime(CLOCK_MONOTONIC, &tp);
122
123 return tp.tv_sec + tp.tv_nsec / 1000000000.0;
124 }
125
126 void
127 aub_dump_bmp(struct gl_context *ctx)
128 {
129 struct gl_framebuffer *fb = ctx->DrawBuffer;
130
131 for (int i = 0; i < fb->_NumColorDrawBuffers; i++) {
132 struct intel_renderbuffer *irb =
133 intel_renderbuffer(fb->_ColorDrawBuffers[i]);
134
135 if (irb && irb->mt) {
136 enum aub_dump_bmp_format format;
137
138 switch (irb->Base.Base.Format) {
139 case MESA_FORMAT_ARGB8888:
140 case MESA_FORMAT_XRGB8888:
141 format = AUB_DUMP_BMP_FORMAT_ARGB_8888;
142 break;
143 default:
144 continue;
145 }
146
147 drm_intel_gem_bo_aub_dump_bmp(irb->mt->region->bo,
148 irb->draw_x,
149 irb->draw_y,
150 irb->Base.Base.Width,
151 irb->Base.Base.Height,
152 format,
153 irb->mt->region->pitch *
154 irb->mt->region->cpp,
155 0);
156 }
157 }
158 }
159
160 static const __DRItexBufferExtension intelTexBufferExtension = {
161 .base = { __DRI_TEX_BUFFER, __DRI_TEX_BUFFER_VERSION },
162
163 .setTexBuffer = intelSetTexBuffer,
164 .setTexBuffer2 = intelSetTexBuffer2,
165 .releaseTexBuffer = NULL,
166 };
167
168 static void
169 intelDRI2Flush(__DRIdrawable *drawable)
170 {
171 GET_CURRENT_CONTEXT(ctx);
172 struct intel_context *intel = intel_context(ctx);
173 if (intel == NULL)
174 return;
175
176 if (intel->gen < 4)
177 INTEL_FIREVERTICES(intel);
178
179 intel_downsample_for_dri2_flush(intel, drawable);
180 intel->need_throttle = true;
181
182 if (intel->batch.used)
183 intel_batchbuffer_flush(intel);
184
185 if (INTEL_DEBUG & DEBUG_AUB) {
186 aub_dump_bmp(ctx);
187 }
188 }
189
190 static const struct __DRI2flushExtensionRec intelFlushExtension = {
191 .base = { __DRI2_FLUSH, __DRI2_FLUSH_VERSION },
192
193 .flush = intelDRI2Flush,
194 .invalidate = dri2InvalidateDrawable,
195 };
196
197 static struct intel_image_format intel_image_formats[] = {
198 { __DRI_IMAGE_FOURCC_ARGB8888, __DRI_IMAGE_COMPONENTS_RGBA, 1,
199 { { 0, 0, 0, __DRI_IMAGE_FORMAT_ARGB8888, 4 } } },
200
201 { __DRI_IMAGE_FOURCC_XRGB8888, __DRI_IMAGE_COMPONENTS_RGB, 1,
202 { { 0, 0, 0, __DRI_IMAGE_FORMAT_XRGB8888, 4 }, } },
203
204 { __DRI_IMAGE_FOURCC_YUV410, __DRI_IMAGE_COMPONENTS_Y_U_V, 3,
205 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
206 { 1, 2, 2, __DRI_IMAGE_FORMAT_R8, 1 },
207 { 2, 2, 2, __DRI_IMAGE_FORMAT_R8, 1 } } },
208
209 { __DRI_IMAGE_FOURCC_YUV411, __DRI_IMAGE_COMPONENTS_Y_U_V, 3,
210 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
211 { 1, 2, 0, __DRI_IMAGE_FORMAT_R8, 1 },
212 { 2, 2, 0, __DRI_IMAGE_FORMAT_R8, 1 } } },
213
214 { __DRI_IMAGE_FOURCC_YUV420, __DRI_IMAGE_COMPONENTS_Y_U_V, 3,
215 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
216 { 1, 1, 1, __DRI_IMAGE_FORMAT_R8, 1 },
217 { 2, 1, 1, __DRI_IMAGE_FORMAT_R8, 1 } } },
218
219 { __DRI_IMAGE_FOURCC_YUV422, __DRI_IMAGE_COMPONENTS_Y_U_V, 3,
220 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
221 { 1, 1, 0, __DRI_IMAGE_FORMAT_R8, 1 },
222 { 2, 1, 0, __DRI_IMAGE_FORMAT_R8, 1 } } },
223
224 { __DRI_IMAGE_FOURCC_YUV444, __DRI_IMAGE_COMPONENTS_Y_U_V, 3,
225 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
226 { 1, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
227 { 2, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 } } },
228
229 { __DRI_IMAGE_FOURCC_NV12, __DRI_IMAGE_COMPONENTS_Y_UV, 2,
230 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
231 { 1, 1, 1, __DRI_IMAGE_FORMAT_GR88, 2 } } },
232
233 { __DRI_IMAGE_FOURCC_NV16, __DRI_IMAGE_COMPONENTS_Y_UV, 2,
234 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
235 { 1, 1, 0, __DRI_IMAGE_FORMAT_GR88, 2 } } },
236
237 /* For YUYV buffers, we set up two overlapping DRI images and treat
238 * them as planar buffers in the compositors. Plane 0 is GR88 and
239 * samples YU or YV pairs and places Y into the R component, while
240 * plane 1 is ARGB and samples YUYV clusters and places pairs and
241 * places U into the G component and V into A. This lets the
242 * texture sampler interpolate the Y components correctly when
243 * sampling from plane 0, and interpolate U and V correctly when
244 * sampling from plane 1. */
245 { __DRI_IMAGE_FOURCC_YUYV, __DRI_IMAGE_COMPONENTS_Y_XUXV, 2,
246 { { 0, 0, 0, __DRI_IMAGE_FORMAT_GR88, 2 },
247 { 0, 1, 0, __DRI_IMAGE_FORMAT_ARGB8888, 4 } } }
248 };
249
250 static __DRIimage *
251 intel_allocate_image(int dri_format, void *loaderPrivate)
252 {
253 __DRIimage *image;
254
255 image = calloc(1, sizeof *image);
256 if (image == NULL)
257 return NULL;
258
259 image->dri_format = dri_format;
260 image->offset = 0;
261
262 switch (dri_format) {
263 case __DRI_IMAGE_FORMAT_RGB565:
264 image->format = MESA_FORMAT_RGB565;
265 break;
266 case __DRI_IMAGE_FORMAT_XRGB8888:
267 image->format = MESA_FORMAT_XRGB8888;
268 break;
269 case __DRI_IMAGE_FORMAT_ARGB8888:
270 image->format = MESA_FORMAT_ARGB8888;
271 break;
272 case __DRI_IMAGE_FORMAT_ABGR8888:
273 image->format = MESA_FORMAT_RGBA8888_REV;
274 break;
275 case __DRI_IMAGE_FORMAT_XBGR8888:
276 image->format = MESA_FORMAT_RGBX8888_REV;
277 break;
278 case __DRI_IMAGE_FORMAT_R8:
279 image->format = MESA_FORMAT_R8;
280 break;
281 case __DRI_IMAGE_FORMAT_GR88:
282 image->format = MESA_FORMAT_GR88;
283 break;
284 case __DRI_IMAGE_FORMAT_NONE:
285 image->format = MESA_FORMAT_NONE;
286 break;
287 default:
288 free(image);
289 return NULL;
290 }
291
292 image->internal_format = _mesa_get_format_base_format(image->format);
293 image->data = loaderPrivate;
294
295 return image;
296 }
297
298 static __DRIimage *
299 intel_create_image_from_name(__DRIscreen *screen,
300 int width, int height, int format,
301 int name, int pitch, void *loaderPrivate)
302 {
303 struct intel_screen *intelScreen = screen->driverPrivate;
304 __DRIimage *image;
305 int cpp;
306
307 image = intel_allocate_image(format, loaderPrivate);
308 if (image->format == MESA_FORMAT_NONE)
309 cpp = 1;
310 else
311 cpp = _mesa_get_format_bytes(image->format);
312 image->region = intel_region_alloc_for_handle(intelScreen,
313 cpp, width, height,
314 pitch, name, "image");
315 if (image->region == NULL) {
316 free(image);
317 return NULL;
318 }
319
320 return image;
321 }
322
323 static __DRIimage *
324 intel_create_image_from_renderbuffer(__DRIcontext *context,
325 int renderbuffer, void *loaderPrivate)
326 {
327 __DRIimage *image;
328 struct intel_context *intel = context->driverPrivate;
329 struct gl_renderbuffer *rb;
330 struct intel_renderbuffer *irb;
331
332 rb = _mesa_lookup_renderbuffer(&intel->ctx, renderbuffer);
333 if (!rb) {
334 _mesa_error(&intel->ctx,
335 GL_INVALID_OPERATION, "glRenderbufferExternalMESA");
336 return NULL;
337 }
338
339 irb = intel_renderbuffer(rb);
340 image = calloc(1, sizeof *image);
341 if (image == NULL)
342 return NULL;
343
344 image->internal_format = rb->InternalFormat;
345 image->format = rb->Format;
346 image->offset = 0;
347 image->data = loaderPrivate;
348 intel_region_reference(&image->region, irb->mt->region);
349
350 switch (image->format) {
351 case MESA_FORMAT_RGB565:
352 image->dri_format = __DRI_IMAGE_FORMAT_RGB565;
353 break;
354 case MESA_FORMAT_XRGB8888:
355 image->dri_format = __DRI_IMAGE_FORMAT_XRGB8888;
356 break;
357 case MESA_FORMAT_ARGB8888:
358 image->dri_format = __DRI_IMAGE_FORMAT_ARGB8888;
359 break;
360 case MESA_FORMAT_RGBA8888_REV:
361 image->dri_format = __DRI_IMAGE_FORMAT_ABGR8888;
362 break;
363 case MESA_FORMAT_R8:
364 image->dri_format = __DRI_IMAGE_FORMAT_R8;
365 break;
366 case MESA_FORMAT_RG88:
367 image->dri_format = __DRI_IMAGE_FORMAT_GR88;
368 break;
369 }
370
371 return image;
372 }
373
374 static void
375 intel_destroy_image(__DRIimage *image)
376 {
377 intel_region_release(&image->region);
378 free(image);
379 }
380
381 static __DRIimage *
382 intel_create_image(__DRIscreen *screen,
383 int width, int height, int format,
384 unsigned int use,
385 void *loaderPrivate)
386 {
387 __DRIimage *image;
388 struct intel_screen *intelScreen = screen->driverPrivate;
389 uint32_t tiling;
390 int cpp;
391
392 tiling = I915_TILING_X;
393 if (use & __DRI_IMAGE_USE_CURSOR) {
394 if (width != 64 || height != 64)
395 return NULL;
396 tiling = I915_TILING_NONE;
397 }
398
399 image = intel_allocate_image(format, loaderPrivate);
400 cpp = _mesa_get_format_bytes(image->format);
401 image->region =
402 intel_region_alloc(intelScreen, tiling, cpp, width, height, true);
403 if (image->region == NULL) {
404 free(image);
405 return NULL;
406 }
407
408 return image;
409 }
410
411 static GLboolean
412 intel_query_image(__DRIimage *image, int attrib, int *value)
413 {
414 switch (attrib) {
415 case __DRI_IMAGE_ATTRIB_STRIDE:
416 *value = image->region->pitch * image->region->cpp;
417 return true;
418 case __DRI_IMAGE_ATTRIB_HANDLE:
419 *value = image->region->bo->handle;
420 return true;
421 case __DRI_IMAGE_ATTRIB_NAME:
422 return intel_region_flink(image->region, (uint32_t *) value);
423 case __DRI_IMAGE_ATTRIB_FORMAT:
424 *value = image->dri_format;
425 return true;
426 case __DRI_IMAGE_ATTRIB_WIDTH:
427 *value = image->region->width;
428 return true;
429 case __DRI_IMAGE_ATTRIB_HEIGHT:
430 *value = image->region->height;
431 return true;
432 case __DRI_IMAGE_ATTRIB_COMPONENTS:
433 if (image->planar_format == NULL)
434 return false;
435 *value = image->planar_format->components;
436 return true;
437 default:
438 return false;
439 }
440 }
441
442 static __DRIimage *
443 intel_dup_image(__DRIimage *orig_image, void *loaderPrivate)
444 {
445 __DRIimage *image;
446
447 image = calloc(1, sizeof *image);
448 if (image == NULL)
449 return NULL;
450
451 intel_region_reference(&image->region, orig_image->region);
452 if (image->region == NULL) {
453 free(image);
454 return NULL;
455 }
456
457 image->internal_format = orig_image->internal_format;
458 image->planar_format = orig_image->planar_format;
459 image->dri_format = orig_image->dri_format;
460 image->format = orig_image->format;
461 image->offset = orig_image->offset;
462 image->data = loaderPrivate;
463
464 memcpy(image->strides, orig_image->strides, sizeof(image->strides));
465 memcpy(image->offsets, orig_image->offsets, sizeof(image->offsets));
466
467 return image;
468 }
469
470 static GLboolean
471 intel_validate_usage(__DRIimage *image, unsigned int use)
472 {
473 if (use & __DRI_IMAGE_USE_CURSOR) {
474 if (image->region->width != 64 || image->region->height != 64)
475 return GL_FALSE;
476 }
477
478 return GL_TRUE;
479 }
480
481 static __DRIimage *
482 intel_create_image_from_names(__DRIscreen *screen,
483 int width, int height, int fourcc,
484 int *names, int num_names,
485 int *strides, int *offsets,
486 void *loaderPrivate)
487 {
488 struct intel_image_format *f = NULL;
489 __DRIimage *image;
490 int i, index;
491
492 if (screen == NULL || names == NULL || num_names != 1)
493 return NULL;
494
495 for (i = 0; i < ARRAY_SIZE(intel_image_formats); i++) {
496 if (intel_image_formats[i].fourcc == fourcc) {
497 f = &intel_image_formats[i];
498 }
499 }
500
501 if (f == NULL)
502 return NULL;
503
504 image = intel_create_image_from_name(screen, width, height,
505 __DRI_IMAGE_FORMAT_NONE,
506 names[0], strides[0],
507 loaderPrivate);
508
509 if (image == NULL)
510 return NULL;
511
512 image->planar_format = f;
513 for (i = 0; i < f->nplanes; i++) {
514 index = f->planes[i].buffer_index;
515 image->offsets[index] = offsets[index];
516 image->strides[index] = strides[index];
517 }
518
519 return image;
520 }
521
522 static __DRIimage *
523 intel_from_planar(__DRIimage *parent, int plane, void *loaderPrivate)
524 {
525 int width, height, offset, stride, dri_format, cpp, index, pitch;
526 struct intel_image_format *f;
527 uint32_t mask_x, mask_y;
528 __DRIimage *image;
529
530 if (parent == NULL || parent->planar_format == NULL)
531 return NULL;
532
533 f = parent->planar_format;
534
535 if (plane >= f->nplanes)
536 return NULL;
537
538 width = parent->region->width >> f->planes[plane].width_shift;
539 height = parent->region->height >> f->planes[plane].height_shift;
540 dri_format = f->planes[plane].dri_format;
541 index = f->planes[plane].buffer_index;
542 offset = parent->offsets[index];
543 stride = parent->strides[index];
544
545 image = intel_allocate_image(dri_format, loaderPrivate);
546 cpp = _mesa_get_format_bytes(image->format); /* safe since no none format */
547 pitch = stride / cpp;
548 if (offset + height * cpp * pitch > parent->region->bo->size) {
549 _mesa_warning(NULL, "intel_create_sub_image: subimage out of bounds");
550 free(image);
551 return NULL;
552 }
553
554 image->region = calloc(sizeof(*image->region), 1);
555 if (image->region == NULL) {
556 free(image);
557 return NULL;
558 }
559
560 image->region->cpp = _mesa_get_format_bytes(image->format);
561 image->region->width = width;
562 image->region->height = height;
563 image->region->pitch = pitch;
564 image->region->refcount = 1;
565 image->region->bo = parent->region->bo;
566 drm_intel_bo_reference(image->region->bo);
567 image->region->tiling = parent->region->tiling;
568 image->region->screen = parent->region->screen;
569 image->offset = offset;
570
571 intel_region_get_tile_masks(image->region, &mask_x, &mask_y, false);
572 if (offset & mask_x)
573 _mesa_warning(NULL,
574 "intel_create_sub_image: offset not on tile boundary");
575
576 return image;
577 }
578
579 static struct __DRIimageExtensionRec intelImageExtension = {
580 .base = { __DRI_IMAGE, 5 },
581
582 .createImageFromName = intel_create_image_from_name,
583 .createImageFromRenderbuffer = intel_create_image_from_renderbuffer,
584 .destroyImage = intel_destroy_image,
585 .createImage = intel_create_image,
586 .queryImage = intel_query_image,
587 .dupImage = intel_dup_image,
588 .validateUsage = intel_validate_usage,
589 .createImageFromNames = intel_create_image_from_names,
590 .fromPlanar = intel_from_planar
591 };
592
593 static const __DRIextension *intelScreenExtensions[] = {
594 &intelTexBufferExtension.base,
595 &intelFlushExtension.base,
596 &intelImageExtension.base,
597 &dri2ConfigQueryExtension.base,
598 NULL
599 };
600
601 static bool
602 intel_get_param(__DRIscreen *psp, int param, int *value)
603 {
604 int ret;
605 struct drm_i915_getparam gp;
606
607 memset(&gp, 0, sizeof(gp));
608 gp.param = param;
609 gp.value = value;
610
611 ret = drmCommandWriteRead(psp->fd, DRM_I915_GETPARAM, &gp, sizeof(gp));
612 if (ret) {
613 if (ret != -EINVAL)
614 _mesa_warning(NULL, "drm_i915_getparam: %d", ret);
615 return false;
616 }
617
618 return true;
619 }
620
621 static bool
622 intel_get_boolean(__DRIscreen *psp, int param)
623 {
624 int value = 0;
625 return intel_get_param(psp, param, &value) && value;
626 }
627
628 static void
629 nop_callback(GLuint key, void *data, void *userData)
630 {
631 }
632
633 static void
634 intelDestroyScreen(__DRIscreen * sPriv)
635 {
636 struct intel_screen *intelScreen = sPriv->driverPrivate;
637
638 dri_bufmgr_destroy(intelScreen->bufmgr);
639 driDestroyOptionInfo(&intelScreen->optionCache);
640
641 /* Some regions may still have references to them at this point, so
642 * flush the hash table to prevent _mesa_DeleteHashTable() from
643 * complaining about the hash not being empty; */
644 _mesa_HashDeleteAll(intelScreen->named_regions, nop_callback, NULL);
645 _mesa_DeleteHashTable(intelScreen->named_regions);
646
647 free(intelScreen);
648 sPriv->driverPrivate = NULL;
649 }
650
651
652 /**
653 * This is called when we need to set up GL rendering to a new X window.
654 */
655 static GLboolean
656 intelCreateBuffer(__DRIscreen * driScrnPriv,
657 __DRIdrawable * driDrawPriv,
658 const struct gl_config * mesaVis, GLboolean isPixmap)
659 {
660 struct intel_renderbuffer *rb;
661 struct intel_screen *screen = (struct intel_screen*) driScrnPriv->driverPrivate;
662 gl_format rgbFormat;
663 unsigned num_samples = intel_quantize_num_samples(screen, mesaVis->samples);
664 struct gl_framebuffer *fb;
665
666 if (isPixmap)
667 return false;
668
669 fb = CALLOC_STRUCT(gl_framebuffer);
670 if (!fb)
671 return false;
672
673 _mesa_initialize_window_framebuffer(fb, mesaVis);
674
675 if (mesaVis->redBits == 5)
676 rgbFormat = MESA_FORMAT_RGB565;
677 else if (mesaVis->sRGBCapable)
678 rgbFormat = MESA_FORMAT_SARGB8;
679 else if (mesaVis->alphaBits == 0)
680 rgbFormat = MESA_FORMAT_XRGB8888;
681 else
682 rgbFormat = MESA_FORMAT_ARGB8888;
683
684 /* setup the hardware-based renderbuffers */
685 rb = intel_create_renderbuffer(rgbFormat, num_samples);
686 _mesa_add_renderbuffer(fb, BUFFER_FRONT_LEFT, &rb->Base.Base);
687
688 if (mesaVis->doubleBufferMode) {
689 rb = intel_create_renderbuffer(rgbFormat, num_samples);
690 _mesa_add_renderbuffer(fb, BUFFER_BACK_LEFT, &rb->Base.Base);
691 }
692
693 /*
694 * Assert here that the gl_config has an expected depth/stencil bit
695 * combination: one of d24/s8, d16/s0, d0/s0. (See intelInitScreen2(),
696 * which constructs the advertised configs.)
697 */
698 if (mesaVis->depthBits == 24) {
699 assert(mesaVis->stencilBits == 8);
700
701 if (screen->hw_has_separate_stencil) {
702 rb = intel_create_private_renderbuffer(MESA_FORMAT_X8_Z24,
703 num_samples);
704 _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &rb->Base.Base);
705 rb = intel_create_private_renderbuffer(MESA_FORMAT_S8,
706 num_samples);
707 _mesa_add_renderbuffer(fb, BUFFER_STENCIL, &rb->Base.Base);
708 } else {
709 /*
710 * Use combined depth/stencil. Note that the renderbuffer is
711 * attached to two attachment points.
712 */
713 rb = intel_create_private_renderbuffer(MESA_FORMAT_S8_Z24,
714 num_samples);
715 _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &rb->Base.Base);
716 _mesa_add_renderbuffer(fb, BUFFER_STENCIL, &rb->Base.Base);
717 }
718 }
719 else if (mesaVis->depthBits == 16) {
720 assert(mesaVis->stencilBits == 0);
721 rb = intel_create_private_renderbuffer(MESA_FORMAT_Z16,
722 num_samples);
723 _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &rb->Base.Base);
724 }
725 else {
726 assert(mesaVis->depthBits == 0);
727 assert(mesaVis->stencilBits == 0);
728 }
729
730 /* now add any/all software-based renderbuffers we may need */
731 _swrast_add_soft_renderbuffers(fb,
732 false, /* never sw color */
733 false, /* never sw depth */
734 false, /* never sw stencil */
735 mesaVis->accumRedBits > 0,
736 false, /* never sw alpha */
737 false /* never sw aux */ );
738 driDrawPriv->driverPrivate = fb;
739
740 return true;
741 }
742
743 static void
744 intelDestroyBuffer(__DRIdrawable * driDrawPriv)
745 {
746 struct gl_framebuffer *fb = driDrawPriv->driverPrivate;
747
748 _mesa_reference_framebuffer(&fb, NULL);
749 }
750
751 /* There are probably better ways to do this, such as an
752 * init-designated function to register chipids and createcontext
753 * functions.
754 */
755 extern bool
756 i830CreateContext(const struct gl_config *mesaVis,
757 __DRIcontext *driContextPriv,
758 void *sharedContextPrivate);
759
760 extern bool
761 i915CreateContext(int api,
762 const struct gl_config *mesaVis,
763 __DRIcontext *driContextPriv,
764 unsigned major_version,
765 unsigned minor_version,
766 unsigned *error,
767 void *sharedContextPrivate);
768 extern bool
769 brwCreateContext(int api,
770 const struct gl_config *mesaVis,
771 __DRIcontext *driContextPriv,
772 unsigned major_version,
773 unsigned minor_version,
774 uint32_t flags,
775 unsigned *error,
776 void *sharedContextPrivate);
777
778 static GLboolean
779 intelCreateContext(gl_api api,
780 const struct gl_config * mesaVis,
781 __DRIcontext * driContextPriv,
782 unsigned major_version,
783 unsigned minor_version,
784 uint32_t flags,
785 unsigned *error,
786 void *sharedContextPrivate)
787 {
788 bool success = false;
789
790 #ifdef I915
791 __DRIscreen *sPriv = driContextPriv->driScreenPriv;
792 struct intel_screen *intelScreen = sPriv->driverPrivate;
793
794 if (IS_9XX(intelScreen->deviceID)) {
795 success = i915CreateContext(api, mesaVis, driContextPriv,
796 major_version, minor_version, error,
797 sharedContextPrivate);
798 } else {
799 switch (api) {
800 case API_OPENGL:
801 if (major_version > 1 || minor_version > 3) {
802 *error = __DRI_CTX_ERROR_BAD_VERSION;
803 success = false;
804 }
805 break;
806 case API_OPENGLES:
807 break;
808 default:
809 *error = __DRI_CTX_ERROR_BAD_API;
810 success = false;
811 }
812
813 if (success) {
814 intelScreen->no_vbo = true;
815 success = i830CreateContext(mesaVis, driContextPriv,
816 sharedContextPrivate);
817 if (!success)
818 *error = __DRI_CTX_ERROR_NO_MEMORY;
819 }
820 }
821 #else
822 success = brwCreateContext(api, mesaVis,
823 driContextPriv,
824 major_version, minor_version, flags,
825 error, sharedContextPrivate);
826 #endif
827
828 if (success)
829 return true;
830
831 if (driContextPriv->driverPrivate != NULL)
832 intelDestroyContext(driContextPriv);
833
834 return false;
835 }
836
837 static bool
838 intel_init_bufmgr(struct intel_screen *intelScreen)
839 {
840 __DRIscreen *spriv = intelScreen->driScrnPriv;
841 int num_fences = 0;
842
843 intelScreen->no_hw = getenv("INTEL_NO_HW") != NULL;
844
845 intelScreen->bufmgr = intel_bufmgr_gem_init(spriv->fd, BATCH_SZ);
846 if (intelScreen->bufmgr == NULL) {
847 fprintf(stderr, "[%s:%u] Error initializing buffer manager.\n",
848 __func__, __LINE__);
849 return false;
850 }
851
852 if (!intel_get_param(spriv, I915_PARAM_NUM_FENCES_AVAIL, &num_fences) ||
853 num_fences == 0) {
854 fprintf(stderr, "[%s: %u] Kernel 2.6.29 required.\n", __func__, __LINE__);
855 return false;
856 }
857
858 drm_intel_bufmgr_gem_enable_fenced_relocs(intelScreen->bufmgr);
859
860 intelScreen->named_regions = _mesa_NewHashTable();
861
862 intelScreen->relaxed_relocations = 0;
863 intelScreen->relaxed_relocations |=
864 intel_get_boolean(spriv, I915_PARAM_HAS_RELAXED_DELTA) << 0;
865
866 return true;
867 }
868
869 /**
870 * Override intel_screen.hw_has_separate_stencil with environment variable
871 * INTEL_SEPARATE_STENCIL.
872 *
873 * Valid values for INTEL_SEPARATE_STENCIL are "0" and "1". If an invalid
874 * valid value is encountered, a warning is emitted and INTEL_SEPARATE_STENCIL
875 * is ignored.
876 */
877 static void
878 intel_override_separate_stencil(struct intel_screen *screen)
879 {
880 const char *s = getenv("INTEL_SEPARATE_STENCIL");
881 if (!s) {
882 return;
883 } else if (!strncmp("0", s, 2)) {
884 screen->hw_has_separate_stencil = false;
885 } else if (!strncmp("1", s, 2)) {
886 screen->hw_has_separate_stencil = true;
887 } else {
888 fprintf(stderr,
889 "warning: env variable INTEL_SEPARATE_STENCIL=\"%s\" has "
890 "invalid value and is ignored", s);
891 }
892 }
893
894 static bool
895 intel_detect_swizzling(struct intel_screen *screen)
896 {
897 drm_intel_bo *buffer;
898 unsigned long flags = 0;
899 unsigned long aligned_pitch;
900 uint32_t tiling = I915_TILING_X;
901 uint32_t swizzle_mode = 0;
902
903 buffer = drm_intel_bo_alloc_tiled(screen->bufmgr, "swizzle test",
904 64, 64, 4,
905 &tiling, &aligned_pitch, flags);
906 if (buffer == NULL)
907 return false;
908
909 drm_intel_bo_get_tiling(buffer, &tiling, &swizzle_mode);
910 drm_intel_bo_unreference(buffer);
911
912 if (swizzle_mode == I915_BIT_6_SWIZZLE_NONE)
913 return false;
914 else
915 return true;
916 }
917
918 static __DRIconfig**
919 intel_screen_make_configs(__DRIscreen *dri_screen)
920 {
921 static const gl_format formats[3] = {
922 MESA_FORMAT_RGB565,
923 MESA_FORMAT_XRGB8888,
924 MESA_FORMAT_ARGB8888
925 };
926
927 /* GLX_SWAP_COPY_OML is not supported due to page flipping. */
928 static const GLenum back_buffer_modes[] = {
929 GLX_SWAP_UNDEFINED_OML, GLX_NONE,
930 };
931
932 static const uint8_t singlesample_samples[1] = {0};
933 static const uint8_t multisample_samples[2] = {4, 8};
934
935 struct intel_screen *screen = dri_screen->driverPrivate;
936 uint8_t depth_bits[4], stencil_bits[4];
937 __DRIconfig **configs = NULL;
938
939 /* Generate singlesample configs without accumulation buffer. */
940 for (int i = 0; i < ARRAY_SIZE(formats); i++) {
941 __DRIconfig **new_configs;
942 int num_depth_stencil_bits = 2;
943
944 /* Starting with DRI2 protocol version 1.1 we can request a depth/stencil
945 * buffer that has a different number of bits per pixel than the color
946 * buffer, gen >= 6 supports this.
947 */
948 depth_bits[0] = 0;
949 stencil_bits[0] = 0;
950
951 if (formats[i] == MESA_FORMAT_RGB565) {
952 depth_bits[1] = 16;
953 stencil_bits[1] = 0;
954 if (screen->gen >= 6) {
955 depth_bits[2] = 24;
956 stencil_bits[2] = 8;
957 num_depth_stencil_bits = 3;
958 }
959 } else {
960 depth_bits[1] = 24;
961 stencil_bits[1] = 8;
962 }
963
964 new_configs = driCreateConfigs(formats[i],
965 depth_bits,
966 stencil_bits,
967 num_depth_stencil_bits,
968 back_buffer_modes, 2,
969 singlesample_samples, 1,
970 false);
971 configs = driConcatConfigs(configs, new_configs);
972 }
973
974 /* Generate the minimum possible set of configs that include an
975 * accumulation buffer.
976 */
977 for (int i = 0; i < ARRAY_SIZE(formats); i++) {
978 __DRIconfig **new_configs;
979
980 if (formats[i] == MESA_FORMAT_RGB565) {
981 depth_bits[0] = 16;
982 stencil_bits[0] = 0;
983 } else {
984 depth_bits[0] = 24;
985 stencil_bits[0] = 8;
986 }
987
988 new_configs = driCreateConfigs(formats[i],
989 depth_bits, stencil_bits, 1,
990 back_buffer_modes, 1,
991 singlesample_samples, 1,
992 true);
993 configs = driConcatConfigs(configs, new_configs);
994 }
995
996 /* Generate multisample configs.
997 *
998 * This loop breaks early, and hence is a no-op, on gen < 6.
999 *
1000 * Multisample configs must follow the singlesample configs in order to
1001 * work around an X server bug present in 1.12. The X server chooses to
1002 * associate the first listed RGBA888-Z24S8 config, regardless of its
1003 * sample count, with the 32-bit depth visual used for compositing.
1004 *
1005 * Only doublebuffer configs with GLX_SWAP_UNDEFINED_OML behavior are
1006 * supported. Singlebuffer configs are not supported because no one wants
1007 * them.
1008 */
1009 for (int i = 0; i < ARRAY_SIZE(formats); i++) {
1010 if (screen->gen < 6)
1011 break;
1012
1013 __DRIconfig **new_configs;
1014 const int num_depth_stencil_bits = 2;
1015 int num_msaa_modes = 0;
1016
1017 depth_bits[0] = 0;
1018 stencil_bits[0] = 0;
1019
1020 if (formats[i] == MESA_FORMAT_RGB565) {
1021 depth_bits[1] = 16;
1022 stencil_bits[1] = 0;
1023 } else {
1024 depth_bits[1] = 24;
1025 stencil_bits[1] = 8;
1026 }
1027
1028 if (screen->gen >= 7)
1029 num_msaa_modes = 2;
1030 else if (screen->gen == 6)
1031 num_msaa_modes = 1;
1032
1033 new_configs = driCreateConfigs(formats[i],
1034 depth_bits,
1035 stencil_bits,
1036 num_depth_stencil_bits,
1037 back_buffer_modes, 1,
1038 multisample_samples,
1039 num_msaa_modes,
1040 false);
1041 configs = driConcatConfigs(configs, new_configs);
1042 }
1043
1044 if (configs == NULL) {
1045 fprintf(stderr, "[%s:%u] Error creating FBConfig!\n", __func__,
1046 __LINE__);
1047 return NULL;
1048 }
1049
1050 return configs;
1051 }
1052
1053 /**
1054 * This is the driver specific part of the createNewScreen entry point.
1055 * Called when using DRI2.
1056 *
1057 * \return the struct gl_config supported by this driver
1058 */
1059 static const
1060 __DRIconfig **intelInitScreen2(__DRIscreen *psp)
1061 {
1062 struct intel_screen *intelScreen;
1063 unsigned int api_mask;
1064
1065 if (psp->dri2.loader->base.version <= 2 ||
1066 psp->dri2.loader->getBuffersWithFormat == NULL) {
1067 fprintf(stderr,
1068 "\nERROR! DRI2 loader with getBuffersWithFormat() "
1069 "support required\n");
1070 return false;
1071 }
1072
1073 /* Allocate the private area */
1074 intelScreen = calloc(1, sizeof *intelScreen);
1075 if (!intelScreen) {
1076 fprintf(stderr, "\nERROR! Allocating private area failed\n");
1077 return false;
1078 }
1079 /* parse information in __driConfigOptions */
1080 driParseOptionInfo(&intelScreen->optionCache,
1081 __driConfigOptions, __driNConfigOptions);
1082
1083 intelScreen->driScrnPriv = psp;
1084 psp->driverPrivate = (void *) intelScreen;
1085
1086 if (!intel_init_bufmgr(intelScreen))
1087 return false;
1088
1089 intelScreen->deviceID = drm_intel_bufmgr_gem_get_devid(intelScreen->bufmgr);
1090
1091 intelScreen->kernel_has_gen7_sol_reset =
1092 intel_get_boolean(intelScreen->driScrnPriv,
1093 I915_PARAM_HAS_GEN7_SOL_RESET);
1094
1095 if (IS_GEN7(intelScreen->deviceID)) {
1096 intelScreen->gen = 7;
1097 } else if (IS_GEN6(intelScreen->deviceID)) {
1098 intelScreen->gen = 6;
1099 } else if (IS_GEN5(intelScreen->deviceID)) {
1100 intelScreen->gen = 5;
1101 } else if (IS_965(intelScreen->deviceID)) {
1102 intelScreen->gen = 4;
1103 } else if (IS_9XX(intelScreen->deviceID)) {
1104 intelScreen->gen = 3;
1105 } else {
1106 intelScreen->gen = 2;
1107 }
1108
1109 intelScreen->hw_has_separate_stencil = intelScreen->gen >= 6;
1110 intelScreen->hw_must_use_separate_stencil = intelScreen->gen >= 7;
1111
1112 int has_llc = 0;
1113 bool success = intel_get_param(intelScreen->driScrnPriv, I915_PARAM_HAS_LLC,
1114 &has_llc);
1115 if (success && has_llc)
1116 intelScreen->hw_has_llc = true;
1117 else if (!success && intelScreen->gen >= 6)
1118 intelScreen->hw_has_llc = true;
1119
1120 intel_override_separate_stencil(intelScreen);
1121
1122 api_mask = (1 << __DRI_API_OPENGL);
1123 #if FEATURE_ES1
1124 api_mask |= (1 << __DRI_API_GLES);
1125 #endif
1126 #if FEATURE_ES2
1127 api_mask |= (1 << __DRI_API_GLES2);
1128 #endif
1129
1130 if (IS_9XX(intelScreen->deviceID) || IS_965(intelScreen->deviceID))
1131 psp->api_mask = api_mask;
1132
1133 intelScreen->hw_has_swizzling = intel_detect_swizzling(intelScreen);
1134
1135 psp->extensions = intelScreenExtensions;
1136
1137 return (const __DRIconfig**) intel_screen_make_configs(psp);
1138 }
1139
1140 struct intel_buffer {
1141 __DRIbuffer base;
1142 struct intel_region *region;
1143 };
1144
1145 static __DRIbuffer *
1146 intelAllocateBuffer(__DRIscreen *screen,
1147 unsigned attachment, unsigned format,
1148 int width, int height)
1149 {
1150 struct intel_buffer *intelBuffer;
1151 struct intel_screen *intelScreen = screen->driverPrivate;
1152
1153 assert(attachment == __DRI_BUFFER_FRONT_LEFT ||
1154 attachment == __DRI_BUFFER_BACK_LEFT);
1155
1156 intelBuffer = calloc(1, sizeof *intelBuffer);
1157 if (intelBuffer == NULL)
1158 return NULL;
1159
1160 /* The front and back buffers are color buffers, which are X tiled. */
1161 intelBuffer->region = intel_region_alloc(intelScreen,
1162 I915_TILING_X,
1163 format / 8,
1164 width,
1165 height,
1166 true);
1167
1168 if (intelBuffer->region == NULL) {
1169 free(intelBuffer);
1170 return NULL;
1171 }
1172
1173 intel_region_flink(intelBuffer->region, &intelBuffer->base.name);
1174
1175 intelBuffer->base.attachment = attachment;
1176 intelBuffer->base.cpp = intelBuffer->region->cpp;
1177 intelBuffer->base.pitch =
1178 intelBuffer->region->pitch * intelBuffer->region->cpp;
1179
1180 return &intelBuffer->base;
1181 }
1182
1183 static void
1184 intelReleaseBuffer(__DRIscreen *screen, __DRIbuffer *buffer)
1185 {
1186 struct intel_buffer *intelBuffer = (struct intel_buffer *) buffer;
1187
1188 intel_region_release(&intelBuffer->region);
1189 free(intelBuffer);
1190 }
1191
1192
1193 const struct __DriverAPIRec driDriverAPI = {
1194 .InitScreen = intelInitScreen2,
1195 .DestroyScreen = intelDestroyScreen,
1196 .CreateContext = intelCreateContext,
1197 .DestroyContext = intelDestroyContext,
1198 .CreateBuffer = intelCreateBuffer,
1199 .DestroyBuffer = intelDestroyBuffer,
1200 .MakeCurrent = intelMakeCurrent,
1201 .UnbindContext = intelUnbindContext,
1202 .AllocateBuffer = intelAllocateBuffer,
1203 .ReleaseBuffer = intelReleaseBuffer
1204 };
1205
1206 /* This is the table of extensions that the loader will dlsym() for. */
1207 PUBLIC const __DRIextension *__driDriverExtensions[] = {
1208 &driCoreExtension.base,
1209 &driDRI2Extension.base,
1210 NULL
1211 };