cea7ddfe67ad19c25edc8110734904974f1e1ade
[mesa.git] / src / mesa / drivers / dri / i965 / intel_screen.c
1 /**************************************************************************
2 *
3 * Copyright 2003 VMware, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 #include <errno.h>
29 #include <time.h>
30 #include <unistd.h>
31 #include "main/glheader.h"
32 #include "main/context.h"
33 #include "main/framebuffer.h"
34 #include "main/renderbuffer.h"
35 #include "main/texobj.h"
36 #include "main/hash.h"
37 #include "main/fbobject.h"
38 #include "main/version.h"
39 #include "swrast/s_renderbuffer.h"
40 #include "util/ralloc.h"
41
42 #include "utils.h"
43 #include "xmlpool.h"
44
45 static const __DRIconfigOptionsExtension brw_config_options = {
46 .base = { __DRI_CONFIG_OPTIONS, 1 },
47 .xml =
48 DRI_CONF_BEGIN
49 DRI_CONF_SECTION_PERFORMANCE
50 DRI_CONF_VBLANK_MODE(DRI_CONF_VBLANK_ALWAYS_SYNC)
51 /* Options correspond to DRI_CONF_BO_REUSE_DISABLED,
52 * DRI_CONF_BO_REUSE_ALL
53 */
54 DRI_CONF_OPT_BEGIN_V(bo_reuse, enum, 1, "0:1")
55 DRI_CONF_DESC_BEGIN(en, "Buffer object reuse")
56 DRI_CONF_ENUM(0, "Disable buffer object reuse")
57 DRI_CONF_ENUM(1, "Enable reuse of all sizes of buffer objects")
58 DRI_CONF_DESC_END
59 DRI_CONF_OPT_END
60
61 DRI_CONF_OPT_BEGIN_B(hiz, "true")
62 DRI_CONF_DESC(en, "Enable Hierarchical Z on gen6+")
63 DRI_CONF_OPT_END
64 DRI_CONF_SECTION_END
65
66 DRI_CONF_SECTION_QUALITY
67 DRI_CONF_FORCE_S3TC_ENABLE("false")
68
69 DRI_CONF_OPT_BEGIN(clamp_max_samples, int, -1)
70 DRI_CONF_DESC(en, "Clamp the value of GL_MAX_SAMPLES to the "
71 "given integer. If negative, then do not clamp.")
72 DRI_CONF_OPT_END
73 DRI_CONF_SECTION_END
74
75 DRI_CONF_SECTION_DEBUG
76 DRI_CONF_NO_RAST("false")
77 DRI_CONF_ALWAYS_FLUSH_BATCH("false")
78 DRI_CONF_ALWAYS_FLUSH_CACHE("false")
79 DRI_CONF_DISABLE_THROTTLING("false")
80 DRI_CONF_FORCE_GLSL_EXTENSIONS_WARN("false")
81 DRI_CONF_DISABLE_GLSL_LINE_CONTINUATIONS("false")
82 DRI_CONF_DISABLE_BLEND_FUNC_EXTENDED("false")
83 DRI_CONF_ALLOW_GLSL_EXTENSION_DIRECTIVE_MIDSHADER("false")
84
85 DRI_CONF_OPT_BEGIN_B(shader_precompile, "true")
86 DRI_CONF_DESC(en, "Perform code generation at shader link time.")
87 DRI_CONF_OPT_END
88 DRI_CONF_SECTION_END
89 DRI_CONF_END
90 };
91
92 #include "intel_batchbuffer.h"
93 #include "intel_buffers.h"
94 #include "intel_bufmgr.h"
95 #include "intel_fbo.h"
96 #include "intel_mipmap_tree.h"
97 #include "intel_screen.h"
98 #include "intel_tex.h"
99 #include "intel_image.h"
100
101 #include "brw_context.h"
102
103 #include "i915_drm.h"
104
105 /**
106 * For debugging purposes, this returns a time in seconds.
107 */
108 double
109 get_time(void)
110 {
111 struct timespec tp;
112
113 clock_gettime(CLOCK_MONOTONIC, &tp);
114
115 return tp.tv_sec + tp.tv_nsec / 1000000000.0;
116 }
117
118 void
119 aub_dump_bmp(struct gl_context *ctx)
120 {
121 struct gl_framebuffer *fb = ctx->DrawBuffer;
122
123 for (int i = 0; i < fb->_NumColorDrawBuffers; i++) {
124 struct intel_renderbuffer *irb =
125 intel_renderbuffer(fb->_ColorDrawBuffers[i]);
126
127 if (irb && irb->mt) {
128 enum aub_dump_bmp_format format;
129
130 switch (irb->Base.Base.Format) {
131 case MESA_FORMAT_B8G8R8A8_UNORM:
132 case MESA_FORMAT_B8G8R8X8_UNORM:
133 format = AUB_DUMP_BMP_FORMAT_ARGB_8888;
134 break;
135 default:
136 continue;
137 }
138
139 drm_intel_gem_bo_aub_dump_bmp(irb->mt->bo,
140 irb->draw_x,
141 irb->draw_y,
142 irb->Base.Base.Width,
143 irb->Base.Base.Height,
144 format,
145 irb->mt->pitch,
146 0);
147 }
148 }
149 }
150
151 static const __DRItexBufferExtension intelTexBufferExtension = {
152 .base = { __DRI_TEX_BUFFER, 3 },
153
154 .setTexBuffer = intelSetTexBuffer,
155 .setTexBuffer2 = intelSetTexBuffer2,
156 .releaseTexBuffer = NULL,
157 };
158
159 static void
160 intel_dri2_flush_with_flags(__DRIcontext *cPriv,
161 __DRIdrawable *dPriv,
162 unsigned flags,
163 enum __DRI2throttleReason reason)
164 {
165 struct brw_context *brw = cPriv->driverPrivate;
166
167 if (!brw)
168 return;
169
170 struct gl_context *ctx = &brw->ctx;
171
172 FLUSH_VERTICES(ctx, 0);
173
174 if (flags & __DRI2_FLUSH_DRAWABLE)
175 intel_resolve_for_dri2_flush(brw, dPriv);
176
177 if (reason == __DRI2_THROTTLE_SWAPBUFFER ||
178 reason == __DRI2_THROTTLE_FLUSHFRONT) {
179 brw->need_throttle = true;
180 }
181
182 intel_batchbuffer_flush(brw);
183
184 if (INTEL_DEBUG & DEBUG_AUB) {
185 aub_dump_bmp(ctx);
186 }
187 }
188
189 /**
190 * Provides compatibility with loaders that only support the older (version
191 * 1-3) flush interface.
192 *
193 * That includes libGL up to Mesa 9.0, and the X Server at least up to 1.13.
194 */
195 static void
196 intel_dri2_flush(__DRIdrawable *drawable)
197 {
198 intel_dri2_flush_with_flags(drawable->driContextPriv, drawable,
199 __DRI2_FLUSH_DRAWABLE,
200 __DRI2_THROTTLE_SWAPBUFFER);
201 }
202
203 static const struct __DRI2flushExtensionRec intelFlushExtension = {
204 .base = { __DRI2_FLUSH, 4 },
205
206 .flush = intel_dri2_flush,
207 .invalidate = dri2InvalidateDrawable,
208 .flush_with_flags = intel_dri2_flush_with_flags,
209 };
210
211 static struct intel_image_format intel_image_formats[] = {
212 { __DRI_IMAGE_FOURCC_ARGB8888, __DRI_IMAGE_COMPONENTS_RGBA, 1,
213 { { 0, 0, 0, __DRI_IMAGE_FORMAT_ARGB8888, 4 } } },
214
215 { __DRI_IMAGE_FOURCC_ABGR8888, __DRI_IMAGE_COMPONENTS_RGBA, 1,
216 { { 0, 0, 0, __DRI_IMAGE_FORMAT_ABGR8888, 4 } } },
217
218 { __DRI_IMAGE_FOURCC_SARGB8888, __DRI_IMAGE_COMPONENTS_RGBA, 1,
219 { { 0, 0, 0, __DRI_IMAGE_FORMAT_SARGB8, 4 } } },
220
221 { __DRI_IMAGE_FOURCC_XRGB8888, __DRI_IMAGE_COMPONENTS_RGB, 1,
222 { { 0, 0, 0, __DRI_IMAGE_FORMAT_XRGB8888, 4 }, } },
223
224 { __DRI_IMAGE_FOURCC_XBGR8888, __DRI_IMAGE_COMPONENTS_RGB, 1,
225 { { 0, 0, 0, __DRI_IMAGE_FORMAT_XBGR8888, 4 }, } },
226
227 { __DRI_IMAGE_FOURCC_RGB565, __DRI_IMAGE_COMPONENTS_RGB, 1,
228 { { 0, 0, 0, __DRI_IMAGE_FORMAT_RGB565, 2 } } },
229
230 { __DRI_IMAGE_FOURCC_YUV410, __DRI_IMAGE_COMPONENTS_Y_U_V, 3,
231 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
232 { 1, 2, 2, __DRI_IMAGE_FORMAT_R8, 1 },
233 { 2, 2, 2, __DRI_IMAGE_FORMAT_R8, 1 } } },
234
235 { __DRI_IMAGE_FOURCC_YUV411, __DRI_IMAGE_COMPONENTS_Y_U_V, 3,
236 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
237 { 1, 2, 0, __DRI_IMAGE_FORMAT_R8, 1 },
238 { 2, 2, 0, __DRI_IMAGE_FORMAT_R8, 1 } } },
239
240 { __DRI_IMAGE_FOURCC_YUV420, __DRI_IMAGE_COMPONENTS_Y_U_V, 3,
241 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
242 { 1, 1, 1, __DRI_IMAGE_FORMAT_R8, 1 },
243 { 2, 1, 1, __DRI_IMAGE_FORMAT_R8, 1 } } },
244
245 { __DRI_IMAGE_FOURCC_YUV422, __DRI_IMAGE_COMPONENTS_Y_U_V, 3,
246 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
247 { 1, 1, 0, __DRI_IMAGE_FORMAT_R8, 1 },
248 { 2, 1, 0, __DRI_IMAGE_FORMAT_R8, 1 } } },
249
250 { __DRI_IMAGE_FOURCC_YUV444, __DRI_IMAGE_COMPONENTS_Y_U_V, 3,
251 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
252 { 1, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
253 { 2, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 } } },
254
255 { __DRI_IMAGE_FOURCC_NV12, __DRI_IMAGE_COMPONENTS_Y_UV, 2,
256 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
257 { 1, 1, 1, __DRI_IMAGE_FORMAT_GR88, 2 } } },
258
259 { __DRI_IMAGE_FOURCC_NV16, __DRI_IMAGE_COMPONENTS_Y_UV, 2,
260 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
261 { 1, 1, 0, __DRI_IMAGE_FORMAT_GR88, 2 } } },
262
263 /* For YUYV buffers, we set up two overlapping DRI images and treat
264 * them as planar buffers in the compositors. Plane 0 is GR88 and
265 * samples YU or YV pairs and places Y into the R component, while
266 * plane 1 is ARGB and samples YUYV clusters and places pairs and
267 * places U into the G component and V into A. This lets the
268 * texture sampler interpolate the Y components correctly when
269 * sampling from plane 0, and interpolate U and V correctly when
270 * sampling from plane 1. */
271 { __DRI_IMAGE_FOURCC_YUYV, __DRI_IMAGE_COMPONENTS_Y_XUXV, 2,
272 { { 0, 0, 0, __DRI_IMAGE_FORMAT_GR88, 2 },
273 { 0, 1, 0, __DRI_IMAGE_FORMAT_ARGB8888, 4 } } }
274 };
275
276 static void
277 intel_image_warn_if_unaligned(__DRIimage *image, const char *func)
278 {
279 uint32_t tiling, swizzle;
280 drm_intel_bo_get_tiling(image->bo, &tiling, &swizzle);
281
282 if (tiling != I915_TILING_NONE && (image->offset & 0xfff)) {
283 _mesa_warning(NULL, "%s: offset 0x%08x not on tile boundary",
284 func, image->offset);
285 }
286 }
287
288 static struct intel_image_format *
289 intel_image_format_lookup(int fourcc)
290 {
291 struct intel_image_format *f = NULL;
292
293 for (unsigned i = 0; i < ARRAY_SIZE(intel_image_formats); i++) {
294 if (intel_image_formats[i].fourcc == fourcc) {
295 f = &intel_image_formats[i];
296 break;
297 }
298 }
299
300 return f;
301 }
302
303 static __DRIimage *
304 intel_allocate_image(int dri_format, void *loaderPrivate)
305 {
306 __DRIimage *image;
307
308 image = calloc(1, sizeof *image);
309 if (image == NULL)
310 return NULL;
311
312 image->dri_format = dri_format;
313 image->offset = 0;
314
315 image->format = driImageFormatToGLFormat(dri_format);
316 if (dri_format != __DRI_IMAGE_FORMAT_NONE &&
317 image->format == MESA_FORMAT_NONE) {
318 free(image);
319 return NULL;
320 }
321
322 image->internal_format = _mesa_get_format_base_format(image->format);
323 image->data = loaderPrivate;
324
325 return image;
326 }
327
328 /**
329 * Sets up a DRIImage structure to point to a slice out of a miptree.
330 */
331 static void
332 intel_setup_image_from_mipmap_tree(struct brw_context *brw, __DRIimage *image,
333 struct intel_mipmap_tree *mt, GLuint level,
334 GLuint zoffset)
335 {
336 intel_miptree_make_shareable(brw, mt);
337
338 intel_miptree_check_level_layer(mt, level, zoffset);
339
340 image->width = minify(mt->physical_width0, level - mt->first_level);
341 image->height = minify(mt->physical_height0, level - mt->first_level);
342 image->pitch = mt->pitch;
343
344 image->offset = intel_miptree_get_tile_offsets(mt, level, zoffset,
345 &image->tile_x,
346 &image->tile_y);
347
348 drm_intel_bo_unreference(image->bo);
349 image->bo = mt->bo;
350 drm_intel_bo_reference(mt->bo);
351 }
352
353 static __DRIimage *
354 intel_create_image_from_name(__DRIscreen *screen,
355 int width, int height, int format,
356 int name, int pitch, void *loaderPrivate)
357 {
358 struct intel_screen *intelScreen = screen->driverPrivate;
359 __DRIimage *image;
360 int cpp;
361
362 image = intel_allocate_image(format, loaderPrivate);
363 if (image == NULL)
364 return NULL;
365
366 if (image->format == MESA_FORMAT_NONE)
367 cpp = 1;
368 else
369 cpp = _mesa_get_format_bytes(image->format);
370
371 image->width = width;
372 image->height = height;
373 image->pitch = pitch * cpp;
374 image->bo = drm_intel_bo_gem_create_from_name(intelScreen->bufmgr, "image",
375 name);
376 if (!image->bo) {
377 free(image);
378 return NULL;
379 }
380
381 return image;
382 }
383
384 static __DRIimage *
385 intel_create_image_from_renderbuffer(__DRIcontext *context,
386 int renderbuffer, void *loaderPrivate)
387 {
388 __DRIimage *image;
389 struct brw_context *brw = context->driverPrivate;
390 struct gl_context *ctx = &brw->ctx;
391 struct gl_renderbuffer *rb;
392 struct intel_renderbuffer *irb;
393
394 rb = _mesa_lookup_renderbuffer(ctx, renderbuffer);
395 if (!rb) {
396 _mesa_error(ctx, GL_INVALID_OPERATION, "glRenderbufferExternalMESA");
397 return NULL;
398 }
399
400 irb = intel_renderbuffer(rb);
401 intel_miptree_make_shareable(brw, irb->mt);
402 image = calloc(1, sizeof *image);
403 if (image == NULL)
404 return NULL;
405
406 image->internal_format = rb->InternalFormat;
407 image->format = rb->Format;
408 image->offset = 0;
409 image->data = loaderPrivate;
410 drm_intel_bo_unreference(image->bo);
411 image->bo = irb->mt->bo;
412 drm_intel_bo_reference(irb->mt->bo);
413 image->width = rb->Width;
414 image->height = rb->Height;
415 image->pitch = irb->mt->pitch;
416 image->dri_format = driGLFormatToImageFormat(image->format);
417 image->has_depthstencil = irb->mt->stencil_mt? true : false;
418
419 rb->NeedsFinishRenderTexture = true;
420 return image;
421 }
422
423 static __DRIimage *
424 intel_create_image_from_texture(__DRIcontext *context, int target,
425 unsigned texture, int zoffset,
426 int level,
427 unsigned *error,
428 void *loaderPrivate)
429 {
430 __DRIimage *image;
431 struct brw_context *brw = context->driverPrivate;
432 struct gl_texture_object *obj;
433 struct intel_texture_object *iobj;
434 GLuint face = 0;
435
436 obj = _mesa_lookup_texture(&brw->ctx, texture);
437 if (!obj || obj->Target != target) {
438 *error = __DRI_IMAGE_ERROR_BAD_PARAMETER;
439 return NULL;
440 }
441
442 if (target == GL_TEXTURE_CUBE_MAP)
443 face = zoffset;
444
445 _mesa_test_texobj_completeness(&brw->ctx, obj);
446 iobj = intel_texture_object(obj);
447 if (!obj->_BaseComplete || (level > 0 && !obj->_MipmapComplete)) {
448 *error = __DRI_IMAGE_ERROR_BAD_PARAMETER;
449 return NULL;
450 }
451
452 if (level < obj->BaseLevel || level > obj->_MaxLevel) {
453 *error = __DRI_IMAGE_ERROR_BAD_MATCH;
454 return NULL;
455 }
456
457 if (target == GL_TEXTURE_3D && obj->Image[face][level]->Depth < zoffset) {
458 *error = __DRI_IMAGE_ERROR_BAD_MATCH;
459 return NULL;
460 }
461 image = calloc(1, sizeof *image);
462 if (image == NULL) {
463 *error = __DRI_IMAGE_ERROR_BAD_ALLOC;
464 return NULL;
465 }
466
467 image->internal_format = obj->Image[face][level]->InternalFormat;
468 image->format = obj->Image[face][level]->TexFormat;
469 image->data = loaderPrivate;
470 intel_setup_image_from_mipmap_tree(brw, image, iobj->mt, level, zoffset);
471 image->dri_format = driGLFormatToImageFormat(image->format);
472 image->has_depthstencil = iobj->mt->stencil_mt? true : false;
473 if (image->dri_format == MESA_FORMAT_NONE) {
474 *error = __DRI_IMAGE_ERROR_BAD_PARAMETER;
475 free(image);
476 return NULL;
477 }
478
479 *error = __DRI_IMAGE_ERROR_SUCCESS;
480 return image;
481 }
482
483 static void
484 intel_destroy_image(__DRIimage *image)
485 {
486 drm_intel_bo_unreference(image->bo);
487 free(image);
488 }
489
490 static __DRIimage *
491 intel_create_image(__DRIscreen *screen,
492 int width, int height, int format,
493 unsigned int use,
494 void *loaderPrivate)
495 {
496 __DRIimage *image;
497 struct intel_screen *intelScreen = screen->driverPrivate;
498 uint32_t tiling;
499 int cpp;
500 unsigned long pitch;
501
502 tiling = I915_TILING_X;
503 if (use & __DRI_IMAGE_USE_CURSOR) {
504 if (width != 64 || height != 64)
505 return NULL;
506 tiling = I915_TILING_NONE;
507 }
508
509 if (use & __DRI_IMAGE_USE_LINEAR)
510 tiling = I915_TILING_NONE;
511
512 image = intel_allocate_image(format, loaderPrivate);
513 if (image == NULL)
514 return NULL;
515
516
517 cpp = _mesa_get_format_bytes(image->format);
518 image->bo = drm_intel_bo_alloc_tiled(intelScreen->bufmgr, "image",
519 width, height, cpp, &tiling,
520 &pitch, 0);
521 if (image->bo == NULL) {
522 free(image);
523 return NULL;
524 }
525 image->width = width;
526 image->height = height;
527 image->pitch = pitch;
528
529 return image;
530 }
531
532 static GLboolean
533 intel_query_image(__DRIimage *image, int attrib, int *value)
534 {
535 switch (attrib) {
536 case __DRI_IMAGE_ATTRIB_STRIDE:
537 *value = image->pitch;
538 return true;
539 case __DRI_IMAGE_ATTRIB_HANDLE:
540 *value = image->bo->handle;
541 return true;
542 case __DRI_IMAGE_ATTRIB_NAME:
543 return !drm_intel_bo_flink(image->bo, (uint32_t *) value);
544 case __DRI_IMAGE_ATTRIB_FORMAT:
545 *value = image->dri_format;
546 return true;
547 case __DRI_IMAGE_ATTRIB_WIDTH:
548 *value = image->width;
549 return true;
550 case __DRI_IMAGE_ATTRIB_HEIGHT:
551 *value = image->height;
552 return true;
553 case __DRI_IMAGE_ATTRIB_COMPONENTS:
554 if (image->planar_format == NULL)
555 return false;
556 *value = image->planar_format->components;
557 return true;
558 case __DRI_IMAGE_ATTRIB_FD:
559 if (drm_intel_bo_gem_export_to_prime(image->bo, value) == 0)
560 return true;
561 return false;
562 default:
563 return false;
564 }
565 }
566
567 static __DRIimage *
568 intel_dup_image(__DRIimage *orig_image, void *loaderPrivate)
569 {
570 __DRIimage *image;
571
572 image = calloc(1, sizeof *image);
573 if (image == NULL)
574 return NULL;
575
576 drm_intel_bo_reference(orig_image->bo);
577 image->bo = orig_image->bo;
578 image->internal_format = orig_image->internal_format;
579 image->planar_format = orig_image->planar_format;
580 image->dri_format = orig_image->dri_format;
581 image->format = orig_image->format;
582 image->offset = orig_image->offset;
583 image->width = orig_image->width;
584 image->height = orig_image->height;
585 image->pitch = orig_image->pitch;
586 image->tile_x = orig_image->tile_x;
587 image->tile_y = orig_image->tile_y;
588 image->has_depthstencil = orig_image->has_depthstencil;
589 image->data = loaderPrivate;
590
591 memcpy(image->strides, orig_image->strides, sizeof(image->strides));
592 memcpy(image->offsets, orig_image->offsets, sizeof(image->offsets));
593
594 return image;
595 }
596
597 static GLboolean
598 intel_validate_usage(__DRIimage *image, unsigned int use)
599 {
600 if (use & __DRI_IMAGE_USE_CURSOR) {
601 if (image->width != 64 || image->height != 64)
602 return GL_FALSE;
603 }
604
605 return GL_TRUE;
606 }
607
608 static __DRIimage *
609 intel_create_image_from_names(__DRIscreen *screen,
610 int width, int height, int fourcc,
611 int *names, int num_names,
612 int *strides, int *offsets,
613 void *loaderPrivate)
614 {
615 struct intel_image_format *f = NULL;
616 __DRIimage *image;
617 int i, index;
618
619 if (screen == NULL || names == NULL || num_names != 1)
620 return NULL;
621
622 f = intel_image_format_lookup(fourcc);
623 if (f == NULL)
624 return NULL;
625
626 image = intel_create_image_from_name(screen, width, height,
627 __DRI_IMAGE_FORMAT_NONE,
628 names[0], strides[0],
629 loaderPrivate);
630
631 if (image == NULL)
632 return NULL;
633
634 image->planar_format = f;
635 for (i = 0; i < f->nplanes; i++) {
636 index = f->planes[i].buffer_index;
637 image->offsets[index] = offsets[index];
638 image->strides[index] = strides[index];
639 }
640
641 return image;
642 }
643
644 static __DRIimage *
645 intel_create_image_from_fds(__DRIscreen *screen,
646 int width, int height, int fourcc,
647 int *fds, int num_fds, int *strides, int *offsets,
648 void *loaderPrivate)
649 {
650 struct intel_screen *intelScreen = screen->driverPrivate;
651 struct intel_image_format *f;
652 __DRIimage *image;
653 int i, index;
654
655 if (fds == NULL || num_fds != 1)
656 return NULL;
657
658 f = intel_image_format_lookup(fourcc);
659 if (f == NULL)
660 return NULL;
661
662 if (f->nplanes == 1)
663 image = intel_allocate_image(f->planes[0].dri_format, loaderPrivate);
664 else
665 image = intel_allocate_image(__DRI_IMAGE_FORMAT_NONE, loaderPrivate);
666
667 if (image == NULL)
668 return NULL;
669
670 image->bo = drm_intel_bo_gem_create_from_prime(intelScreen->bufmgr,
671 fds[0],
672 height * strides[0]);
673 if (image->bo == NULL) {
674 free(image);
675 return NULL;
676 }
677 image->width = width;
678 image->height = height;
679 image->pitch = strides[0];
680
681 image->planar_format = f;
682 for (i = 0; i < f->nplanes; i++) {
683 index = f->planes[i].buffer_index;
684 image->offsets[index] = offsets[index];
685 image->strides[index] = strides[index];
686 }
687
688 if (f->nplanes == 1) {
689 image->offset = image->offsets[0];
690 intel_image_warn_if_unaligned(image, __FUNCTION__);
691 }
692
693 return image;
694 }
695
696 static __DRIimage *
697 intel_create_image_from_dma_bufs(__DRIscreen *screen,
698 int width, int height, int fourcc,
699 int *fds, int num_fds,
700 int *strides, int *offsets,
701 enum __DRIYUVColorSpace yuv_color_space,
702 enum __DRISampleRange sample_range,
703 enum __DRIChromaSiting horizontal_siting,
704 enum __DRIChromaSiting vertical_siting,
705 unsigned *error,
706 void *loaderPrivate)
707 {
708 __DRIimage *image;
709 struct intel_image_format *f = intel_image_format_lookup(fourcc);
710
711 /* For now only packed formats that have native sampling are supported. */
712 if (!f || f->nplanes != 1) {
713 *error = __DRI_IMAGE_ERROR_BAD_MATCH;
714 return NULL;
715 }
716
717 image = intel_create_image_from_fds(screen, width, height, fourcc, fds,
718 num_fds, strides, offsets,
719 loaderPrivate);
720
721 /*
722 * Invalid parameters and any inconsistencies between are assumed to be
723 * checked by the caller. Therefore besides unsupported formats one can fail
724 * only in allocation.
725 */
726 if (!image) {
727 *error = __DRI_IMAGE_ERROR_BAD_ALLOC;
728 return NULL;
729 }
730
731 image->dma_buf_imported = true;
732 image->yuv_color_space = yuv_color_space;
733 image->sample_range = sample_range;
734 image->horizontal_siting = horizontal_siting;
735 image->vertical_siting = vertical_siting;
736
737 *error = __DRI_IMAGE_ERROR_SUCCESS;
738 return image;
739 }
740
741 static __DRIimage *
742 intel_from_planar(__DRIimage *parent, int plane, void *loaderPrivate)
743 {
744 int width, height, offset, stride, dri_format, index;
745 struct intel_image_format *f;
746 __DRIimage *image;
747
748 if (parent == NULL || parent->planar_format == NULL)
749 return NULL;
750
751 f = parent->planar_format;
752
753 if (plane >= f->nplanes)
754 return NULL;
755
756 width = parent->width >> f->planes[plane].width_shift;
757 height = parent->height >> f->planes[plane].height_shift;
758 dri_format = f->planes[plane].dri_format;
759 index = f->planes[plane].buffer_index;
760 offset = parent->offsets[index];
761 stride = parent->strides[index];
762
763 image = intel_allocate_image(dri_format, loaderPrivate);
764 if (image == NULL)
765 return NULL;
766
767 if (offset + height * stride > parent->bo->size) {
768 _mesa_warning(NULL, "intel_create_sub_image: subimage out of bounds");
769 free(image);
770 return NULL;
771 }
772
773 image->bo = parent->bo;
774 drm_intel_bo_reference(parent->bo);
775
776 image->width = width;
777 image->height = height;
778 image->pitch = stride;
779 image->offset = offset;
780
781 intel_image_warn_if_unaligned(image, __FUNCTION__);
782
783 return image;
784 }
785
786 static const __DRIimageExtension intelImageExtension = {
787 .base = { __DRI_IMAGE, 8 },
788
789 .createImageFromName = intel_create_image_from_name,
790 .createImageFromRenderbuffer = intel_create_image_from_renderbuffer,
791 .destroyImage = intel_destroy_image,
792 .createImage = intel_create_image,
793 .queryImage = intel_query_image,
794 .dupImage = intel_dup_image,
795 .validateUsage = intel_validate_usage,
796 .createImageFromNames = intel_create_image_from_names,
797 .fromPlanar = intel_from_planar,
798 .createImageFromTexture = intel_create_image_from_texture,
799 .createImageFromFds = intel_create_image_from_fds,
800 .createImageFromDmaBufs = intel_create_image_from_dma_bufs
801 };
802
803 static int
804 brw_query_renderer_integer(__DRIscreen *psp, int param, unsigned int *value)
805 {
806 const struct intel_screen *const intelScreen =
807 (struct intel_screen *) psp->driverPrivate;
808
809 switch (param) {
810 case __DRI2_RENDERER_VENDOR_ID:
811 value[0] = 0x8086;
812 return 0;
813 case __DRI2_RENDERER_DEVICE_ID:
814 value[0] = intelScreen->deviceID;
815 return 0;
816 case __DRI2_RENDERER_ACCELERATED:
817 value[0] = 1;
818 return 0;
819 case __DRI2_RENDERER_VIDEO_MEMORY: {
820 /* Once a batch uses more than 75% of the maximum mappable size, we
821 * assume that there's some fragmentation, and we start doing extra
822 * flushing, etc. That's the big cliff apps will care about.
823 */
824 size_t aper_size;
825 size_t mappable_size;
826
827 drm_intel_get_aperture_sizes(psp->fd, &mappable_size, &aper_size);
828
829 const unsigned gpu_mappable_megabytes =
830 (aper_size / (1024 * 1024)) * 3 / 4;
831
832 const long system_memory_pages = sysconf(_SC_PHYS_PAGES);
833 const long system_page_size = sysconf(_SC_PAGE_SIZE);
834
835 if (system_memory_pages <= 0 || system_page_size <= 0)
836 return -1;
837
838 const uint64_t system_memory_bytes = (uint64_t) system_memory_pages
839 * (uint64_t) system_page_size;
840
841 const unsigned system_memory_megabytes =
842 (unsigned) (system_memory_bytes / (1024 * 1024));
843
844 value[0] = MIN2(system_memory_megabytes, gpu_mappable_megabytes);
845 return 0;
846 }
847 case __DRI2_RENDERER_UNIFIED_MEMORY_ARCHITECTURE:
848 value[0] = 1;
849 return 0;
850 default:
851 return driQueryRendererIntegerCommon(psp, param, value);
852 }
853
854 return -1;
855 }
856
857 static int
858 brw_query_renderer_string(__DRIscreen *psp, int param, const char **value)
859 {
860 const struct intel_screen *intelScreen =
861 (struct intel_screen *) psp->driverPrivate;
862
863 switch (param) {
864 case __DRI2_RENDERER_VENDOR_ID:
865 value[0] = brw_vendor_string;
866 return 0;
867 case __DRI2_RENDERER_DEVICE_ID:
868 value[0] = brw_get_renderer_string(intelScreen->deviceID);
869 return 0;
870 default:
871 break;
872 }
873
874 return -1;
875 }
876
877 static const __DRI2rendererQueryExtension intelRendererQueryExtension = {
878 .base = { __DRI2_RENDERER_QUERY, 1 },
879
880 .queryInteger = brw_query_renderer_integer,
881 .queryString = brw_query_renderer_string
882 };
883
884 static const __DRIrobustnessExtension dri2Robustness = {
885 .base = { __DRI2_ROBUSTNESS, 1 }
886 };
887
888 static const __DRIextension *intelScreenExtensions[] = {
889 &intelTexBufferExtension.base,
890 &intelFlushExtension.base,
891 &intelImageExtension.base,
892 &intelRendererQueryExtension.base,
893 &dri2ConfigQueryExtension.base,
894 NULL
895 };
896
897 static const __DRIextension *intelRobustScreenExtensions[] = {
898 &intelTexBufferExtension.base,
899 &intelFlushExtension.base,
900 &intelImageExtension.base,
901 &intelRendererQueryExtension.base,
902 &dri2ConfigQueryExtension.base,
903 &dri2Robustness.base,
904 NULL
905 };
906
907 static bool
908 intel_get_param(__DRIscreen *psp, int param, int *value)
909 {
910 int ret;
911 struct drm_i915_getparam gp;
912
913 memset(&gp, 0, sizeof(gp));
914 gp.param = param;
915 gp.value = value;
916
917 ret = drmCommandWriteRead(psp->fd, DRM_I915_GETPARAM, &gp, sizeof(gp));
918 if (ret) {
919 if (ret != -EINVAL)
920 _mesa_warning(NULL, "drm_i915_getparam: %d", ret);
921 return false;
922 }
923
924 return true;
925 }
926
927 static bool
928 intel_get_boolean(__DRIscreen *psp, int param)
929 {
930 int value = 0;
931 return intel_get_param(psp, param, &value) && value;
932 }
933
934 static void
935 intelDestroyScreen(__DRIscreen * sPriv)
936 {
937 struct intel_screen *intelScreen = sPriv->driverPrivate;
938
939 dri_bufmgr_destroy(intelScreen->bufmgr);
940 driDestroyOptionInfo(&intelScreen->optionCache);
941
942 ralloc_free(intelScreen);
943 sPriv->driverPrivate = NULL;
944 }
945
946
947 /**
948 * This is called when we need to set up GL rendering to a new X window.
949 */
950 static GLboolean
951 intelCreateBuffer(__DRIscreen * driScrnPriv,
952 __DRIdrawable * driDrawPriv,
953 const struct gl_config * mesaVis, GLboolean isPixmap)
954 {
955 struct intel_renderbuffer *rb;
956 struct intel_screen *screen = (struct intel_screen*) driScrnPriv->driverPrivate;
957 mesa_format rgbFormat;
958 unsigned num_samples = intel_quantize_num_samples(screen, mesaVis->samples);
959 struct gl_framebuffer *fb;
960
961 if (isPixmap)
962 return false;
963
964 fb = CALLOC_STRUCT(gl_framebuffer);
965 if (!fb)
966 return false;
967
968 _mesa_initialize_window_framebuffer(fb, mesaVis);
969
970 if (screen->winsys_msaa_samples_override != -1) {
971 num_samples = screen->winsys_msaa_samples_override;
972 fb->Visual.samples = num_samples;
973 }
974
975 if (mesaVis->redBits == 5)
976 rgbFormat = MESA_FORMAT_B5G6R5_UNORM;
977 else if (mesaVis->sRGBCapable)
978 rgbFormat = MESA_FORMAT_B8G8R8A8_SRGB;
979 else if (mesaVis->alphaBits == 0)
980 rgbFormat = MESA_FORMAT_B8G8R8X8_UNORM;
981 else {
982 rgbFormat = MESA_FORMAT_B8G8R8A8_SRGB;
983 fb->Visual.sRGBCapable = true;
984 }
985
986 /* setup the hardware-based renderbuffers */
987 rb = intel_create_renderbuffer(rgbFormat, num_samples);
988 _mesa_add_renderbuffer(fb, BUFFER_FRONT_LEFT, &rb->Base.Base);
989
990 if (mesaVis->doubleBufferMode) {
991 rb = intel_create_renderbuffer(rgbFormat, num_samples);
992 _mesa_add_renderbuffer(fb, BUFFER_BACK_LEFT, &rb->Base.Base);
993 }
994
995 /*
996 * Assert here that the gl_config has an expected depth/stencil bit
997 * combination: one of d24/s8, d16/s0, d0/s0. (See intelInitScreen2(),
998 * which constructs the advertised configs.)
999 */
1000 if (mesaVis->depthBits == 24) {
1001 assert(mesaVis->stencilBits == 8);
1002
1003 if (screen->devinfo->has_hiz_and_separate_stencil) {
1004 rb = intel_create_private_renderbuffer(MESA_FORMAT_Z24_UNORM_X8_UINT,
1005 num_samples);
1006 _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &rb->Base.Base);
1007 rb = intel_create_private_renderbuffer(MESA_FORMAT_S_UINT8,
1008 num_samples);
1009 _mesa_add_renderbuffer(fb, BUFFER_STENCIL, &rb->Base.Base);
1010 } else {
1011 /*
1012 * Use combined depth/stencil. Note that the renderbuffer is
1013 * attached to two attachment points.
1014 */
1015 rb = intel_create_private_renderbuffer(MESA_FORMAT_Z24_UNORM_S8_UINT,
1016 num_samples);
1017 _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &rb->Base.Base);
1018 _mesa_add_renderbuffer(fb, BUFFER_STENCIL, &rb->Base.Base);
1019 }
1020 }
1021 else if (mesaVis->depthBits == 16) {
1022 assert(mesaVis->stencilBits == 0);
1023 rb = intel_create_private_renderbuffer(MESA_FORMAT_Z_UNORM16,
1024 num_samples);
1025 _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &rb->Base.Base);
1026 }
1027 else {
1028 assert(mesaVis->depthBits == 0);
1029 assert(mesaVis->stencilBits == 0);
1030 }
1031
1032 /* now add any/all software-based renderbuffers we may need */
1033 _swrast_add_soft_renderbuffers(fb,
1034 false, /* never sw color */
1035 false, /* never sw depth */
1036 false, /* never sw stencil */
1037 mesaVis->accumRedBits > 0,
1038 false, /* never sw alpha */
1039 false /* never sw aux */ );
1040 driDrawPriv->driverPrivate = fb;
1041
1042 return true;
1043 }
1044
1045 static void
1046 intelDestroyBuffer(__DRIdrawable * driDrawPriv)
1047 {
1048 struct gl_framebuffer *fb = driDrawPriv->driverPrivate;
1049
1050 _mesa_reference_framebuffer(&fb, NULL);
1051 }
1052
1053 static bool
1054 intel_init_bufmgr(struct intel_screen *intelScreen)
1055 {
1056 __DRIscreen *spriv = intelScreen->driScrnPriv;
1057
1058 intelScreen->no_hw = getenv("INTEL_NO_HW") != NULL;
1059
1060 intelScreen->bufmgr = intel_bufmgr_gem_init(spriv->fd, BATCH_SZ);
1061 if (intelScreen->bufmgr == NULL) {
1062 fprintf(stderr, "[%s:%u] Error initializing buffer manager.\n",
1063 __func__, __LINE__);
1064 return false;
1065 }
1066
1067 drm_intel_bufmgr_gem_enable_fenced_relocs(intelScreen->bufmgr);
1068
1069 if (!intel_get_boolean(spriv, I915_PARAM_HAS_RELAXED_DELTA)) {
1070 fprintf(stderr, "[%s: %u] Kernel 2.6.39 required.\n", __func__, __LINE__);
1071 return false;
1072 }
1073
1074 return true;
1075 }
1076
1077 static bool
1078 intel_detect_swizzling(struct intel_screen *screen)
1079 {
1080 drm_intel_bo *buffer;
1081 unsigned long flags = 0;
1082 unsigned long aligned_pitch;
1083 uint32_t tiling = I915_TILING_X;
1084 uint32_t swizzle_mode = 0;
1085
1086 buffer = drm_intel_bo_alloc_tiled(screen->bufmgr, "swizzle test",
1087 64, 64, 4,
1088 &tiling, &aligned_pitch, flags);
1089 if (buffer == NULL)
1090 return false;
1091
1092 drm_intel_bo_get_tiling(buffer, &tiling, &swizzle_mode);
1093 drm_intel_bo_unreference(buffer);
1094
1095 if (swizzle_mode == I915_BIT_6_SWIZZLE_NONE)
1096 return false;
1097 else
1098 return true;
1099 }
1100
1101 /**
1102 * Return array of MSAA modes supported by the hardware. The array is
1103 * zero-terminated and sorted in decreasing order.
1104 */
1105 const int*
1106 intel_supported_msaa_modes(const struct intel_screen *screen)
1107 {
1108 static const int gen8_modes[] = {8, 4, 2, 0, -1};
1109 static const int gen7_modes[] = {8, 4, 0, -1};
1110 static const int gen6_modes[] = {4, 0, -1};
1111 static const int gen4_modes[] = {0, -1};
1112
1113 if (screen->devinfo->gen >= 8) {
1114 return gen8_modes;
1115 } else if (screen->devinfo->gen >= 7) {
1116 return gen7_modes;
1117 } else if (screen->devinfo->gen == 6) {
1118 return gen6_modes;
1119 } else {
1120 return gen4_modes;
1121 }
1122 }
1123
1124 static __DRIconfig**
1125 intel_screen_make_configs(__DRIscreen *dri_screen)
1126 {
1127 static const mesa_format formats[] = {
1128 MESA_FORMAT_B5G6R5_UNORM,
1129 MESA_FORMAT_B8G8R8A8_UNORM
1130 };
1131
1132 /* GLX_SWAP_COPY_OML is not supported due to page flipping. */
1133 static const GLenum back_buffer_modes[] = {
1134 GLX_SWAP_UNDEFINED_OML, GLX_NONE,
1135 };
1136
1137 static const uint8_t singlesample_samples[1] = {0};
1138 static const uint8_t multisample_samples[2] = {4, 8};
1139
1140 struct intel_screen *screen = dri_screen->driverPrivate;
1141 const struct brw_device_info *devinfo = screen->devinfo;
1142 uint8_t depth_bits[4], stencil_bits[4];
1143 __DRIconfig **configs = NULL;
1144
1145 /* Generate singlesample configs without accumulation buffer. */
1146 for (int i = 0; i < ARRAY_SIZE(formats); i++) {
1147 __DRIconfig **new_configs;
1148 int num_depth_stencil_bits = 2;
1149
1150 /* Starting with DRI2 protocol version 1.1 we can request a depth/stencil
1151 * buffer that has a different number of bits per pixel than the color
1152 * buffer, gen >= 6 supports this.
1153 */
1154 depth_bits[0] = 0;
1155 stencil_bits[0] = 0;
1156
1157 if (formats[i] == MESA_FORMAT_B5G6R5_UNORM) {
1158 depth_bits[1] = 16;
1159 stencil_bits[1] = 0;
1160 if (devinfo->gen >= 6) {
1161 depth_bits[2] = 24;
1162 stencil_bits[2] = 8;
1163 num_depth_stencil_bits = 3;
1164 }
1165 } else {
1166 depth_bits[1] = 24;
1167 stencil_bits[1] = 8;
1168 }
1169
1170 new_configs = driCreateConfigs(formats[i],
1171 depth_bits,
1172 stencil_bits,
1173 num_depth_stencil_bits,
1174 back_buffer_modes, 2,
1175 singlesample_samples, 1,
1176 false);
1177 configs = driConcatConfigs(configs, new_configs);
1178 }
1179
1180 /* Generate the minimum possible set of configs that include an
1181 * accumulation buffer.
1182 */
1183 for (int i = 0; i < ARRAY_SIZE(formats); i++) {
1184 __DRIconfig **new_configs;
1185
1186 if (formats[i] == MESA_FORMAT_B5G6R5_UNORM) {
1187 depth_bits[0] = 16;
1188 stencil_bits[0] = 0;
1189 } else {
1190 depth_bits[0] = 24;
1191 stencil_bits[0] = 8;
1192 }
1193
1194 new_configs = driCreateConfigs(formats[i],
1195 depth_bits, stencil_bits, 1,
1196 back_buffer_modes, 1,
1197 singlesample_samples, 1,
1198 true);
1199 configs = driConcatConfigs(configs, new_configs);
1200 }
1201
1202 /* Generate multisample configs.
1203 *
1204 * This loop breaks early, and hence is a no-op, on gen < 6.
1205 *
1206 * Multisample configs must follow the singlesample configs in order to
1207 * work around an X server bug present in 1.12. The X server chooses to
1208 * associate the first listed RGBA888-Z24S8 config, regardless of its
1209 * sample count, with the 32-bit depth visual used for compositing.
1210 *
1211 * Only doublebuffer configs with GLX_SWAP_UNDEFINED_OML behavior are
1212 * supported. Singlebuffer configs are not supported because no one wants
1213 * them.
1214 */
1215 for (int i = 0; i < ARRAY_SIZE(formats); i++) {
1216 if (devinfo->gen < 6)
1217 break;
1218
1219 __DRIconfig **new_configs;
1220 const int num_depth_stencil_bits = 2;
1221 int num_msaa_modes = 0;
1222
1223 depth_bits[0] = 0;
1224 stencil_bits[0] = 0;
1225
1226 if (formats[i] == MESA_FORMAT_B5G6R5_UNORM) {
1227 depth_bits[1] = 16;
1228 stencil_bits[1] = 0;
1229 } else {
1230 depth_bits[1] = 24;
1231 stencil_bits[1] = 8;
1232 }
1233
1234 if (devinfo->gen >= 7)
1235 num_msaa_modes = 2;
1236 else if (devinfo->gen == 6)
1237 num_msaa_modes = 1;
1238
1239 new_configs = driCreateConfigs(formats[i],
1240 depth_bits,
1241 stencil_bits,
1242 num_depth_stencil_bits,
1243 back_buffer_modes, 1,
1244 multisample_samples,
1245 num_msaa_modes,
1246 false);
1247 configs = driConcatConfigs(configs, new_configs);
1248 }
1249
1250 if (configs == NULL) {
1251 fprintf(stderr, "[%s:%u] Error creating FBConfig!\n", __func__,
1252 __LINE__);
1253 return NULL;
1254 }
1255
1256 return configs;
1257 }
1258
1259 static void
1260 set_max_gl_versions(struct intel_screen *screen)
1261 {
1262 __DRIscreen *psp = screen->driScrnPriv;
1263
1264 switch (screen->devinfo->gen) {
1265 case 9:
1266 case 8:
1267 case 7:
1268 case 6:
1269 psp->max_gl_core_version = 33;
1270 psp->max_gl_compat_version = 30;
1271 psp->max_gl_es1_version = 11;
1272 psp->max_gl_es2_version = 30;
1273 break;
1274 case 5:
1275 case 4:
1276 psp->max_gl_core_version = 0;
1277 psp->max_gl_compat_version = 21;
1278 psp->max_gl_es1_version = 11;
1279 psp->max_gl_es2_version = 20;
1280 break;
1281 default:
1282 unreachable("unrecognized intel_screen::gen");
1283 }
1284 }
1285
1286 /**
1287 * This is the driver specific part of the createNewScreen entry point.
1288 * Called when using DRI2.
1289 *
1290 * \return the struct gl_config supported by this driver
1291 */
1292 static const
1293 __DRIconfig **intelInitScreen2(__DRIscreen *psp)
1294 {
1295 struct intel_screen *intelScreen;
1296
1297 if (psp->image.loader) {
1298 } else if (psp->dri2.loader->base.version <= 2 ||
1299 psp->dri2.loader->getBuffersWithFormat == NULL) {
1300 fprintf(stderr,
1301 "\nERROR! DRI2 loader with getBuffersWithFormat() "
1302 "support required\n");
1303 return false;
1304 }
1305
1306 /* Allocate the private area */
1307 intelScreen = rzalloc(NULL, struct intel_screen);
1308 if (!intelScreen) {
1309 fprintf(stderr, "\nERROR! Allocating private area failed\n");
1310 return false;
1311 }
1312 /* parse information in __driConfigOptions */
1313 driParseOptionInfo(&intelScreen->optionCache, brw_config_options.xml);
1314
1315 intelScreen->driScrnPriv = psp;
1316 psp->driverPrivate = (void *) intelScreen;
1317
1318 if (!intel_init_bufmgr(intelScreen))
1319 return false;
1320
1321 intelScreen->deviceID = drm_intel_bufmgr_gem_get_devid(intelScreen->bufmgr);
1322 intelScreen->devinfo = brw_get_device_info(intelScreen->deviceID);
1323 if (!intelScreen->devinfo)
1324 return false;
1325
1326 intelScreen->hw_must_use_separate_stencil = intelScreen->devinfo->gen >= 7;
1327
1328 intelScreen->hw_has_swizzling = intel_detect_swizzling(intelScreen);
1329
1330 const char *force_msaa = getenv("INTEL_FORCE_MSAA");
1331 if (force_msaa) {
1332 intelScreen->winsys_msaa_samples_override =
1333 intel_quantize_num_samples(intelScreen, atoi(force_msaa));
1334 printf("Forcing winsys sample count to %d\n",
1335 intelScreen->winsys_msaa_samples_override);
1336 } else {
1337 intelScreen->winsys_msaa_samples_override = -1;
1338 }
1339
1340 set_max_gl_versions(intelScreen);
1341
1342 /* Notification of GPU resets requires hardware contexts and a kernel new
1343 * enough to support DRM_IOCTL_I915_GET_RESET_STATS. If the ioctl is
1344 * supported, calling it with a context of 0 will either generate EPERM or
1345 * no error. If the ioctl is not supported, it always generate EINVAL.
1346 * Use this to determine whether to advertise the __DRI2_ROBUSTNESS
1347 * extension to the loader.
1348 *
1349 * Don't even try on pre-Gen6, since we don't attempt to use contexts there.
1350 */
1351 if (intelScreen->devinfo->gen >= 6) {
1352 struct drm_i915_reset_stats stats;
1353 memset(&stats, 0, sizeof(stats));
1354
1355 const int ret = drmIoctl(psp->fd, DRM_IOCTL_I915_GET_RESET_STATS, &stats);
1356
1357 intelScreen->has_context_reset_notification =
1358 (ret != -1 || errno != EINVAL);
1359 }
1360
1361 psp->extensions = !intelScreen->has_context_reset_notification
1362 ? intelScreenExtensions : intelRobustScreenExtensions;
1363
1364 brw_fs_alloc_reg_sets(intelScreen);
1365 brw_vec4_alloc_reg_set(intelScreen);
1366
1367 return (const __DRIconfig**) intel_screen_make_configs(psp);
1368 }
1369
1370 struct intel_buffer {
1371 __DRIbuffer base;
1372 drm_intel_bo *bo;
1373 };
1374
1375 static __DRIbuffer *
1376 intelAllocateBuffer(__DRIscreen *screen,
1377 unsigned attachment, unsigned format,
1378 int width, int height)
1379 {
1380 struct intel_buffer *intelBuffer;
1381 struct intel_screen *intelScreen = screen->driverPrivate;
1382
1383 assert(attachment == __DRI_BUFFER_FRONT_LEFT ||
1384 attachment == __DRI_BUFFER_BACK_LEFT);
1385
1386 intelBuffer = calloc(1, sizeof *intelBuffer);
1387 if (intelBuffer == NULL)
1388 return NULL;
1389
1390 /* The front and back buffers are color buffers, which are X tiled. */
1391 uint32_t tiling = I915_TILING_X;
1392 unsigned long pitch;
1393 int cpp = format / 8;
1394 intelBuffer->bo = drm_intel_bo_alloc_tiled(intelScreen->bufmgr,
1395 "intelAllocateBuffer",
1396 width,
1397 height,
1398 cpp,
1399 &tiling, &pitch,
1400 BO_ALLOC_FOR_RENDER);
1401
1402 if (intelBuffer->bo == NULL) {
1403 free(intelBuffer);
1404 return NULL;
1405 }
1406
1407 drm_intel_bo_flink(intelBuffer->bo, &intelBuffer->base.name);
1408
1409 intelBuffer->base.attachment = attachment;
1410 intelBuffer->base.cpp = cpp;
1411 intelBuffer->base.pitch = pitch;
1412
1413 return &intelBuffer->base;
1414 }
1415
1416 static void
1417 intelReleaseBuffer(__DRIscreen *screen, __DRIbuffer *buffer)
1418 {
1419 struct intel_buffer *intelBuffer = (struct intel_buffer *) buffer;
1420
1421 drm_intel_bo_unreference(intelBuffer->bo);
1422 free(intelBuffer);
1423 }
1424
1425 static const struct __DriverAPIRec brw_driver_api = {
1426 .InitScreen = intelInitScreen2,
1427 .DestroyScreen = intelDestroyScreen,
1428 .CreateContext = brwCreateContext,
1429 .DestroyContext = intelDestroyContext,
1430 .CreateBuffer = intelCreateBuffer,
1431 .DestroyBuffer = intelDestroyBuffer,
1432 .MakeCurrent = intelMakeCurrent,
1433 .UnbindContext = intelUnbindContext,
1434 .AllocateBuffer = intelAllocateBuffer,
1435 .ReleaseBuffer = intelReleaseBuffer
1436 };
1437
1438 static const struct __DRIDriverVtableExtensionRec brw_vtable = {
1439 .base = { __DRI_DRIVER_VTABLE, 1 },
1440 .vtable = &brw_driver_api,
1441 };
1442
1443 static const __DRIextension *brw_driver_extensions[] = {
1444 &driCoreExtension.base,
1445 &driImageDriverExtension.base,
1446 &driDRI2Extension.base,
1447 &brw_vtable.base,
1448 &brw_config_options.base,
1449 NULL
1450 };
1451
1452 PUBLIC const __DRIextension **__driDriverGetExtensions_i965(void)
1453 {
1454 globalDriverAPI = &brw_driver_api;
1455
1456 return brw_driver_extensions;
1457 }