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