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