579554f4ba98411b01f4d12824c68fb952fdb46d
[mesa.git] / src / mesa / drivers / dri / i965 / intel_screen.c
1 /*
2 * Copyright 2003 VMware, Inc.
3 * All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial portions
15 * of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
21 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26 #include <drm_fourcc.h>
27 #include <errno.h>
28 #include <time.h>
29 #include <unistd.h>
30 #include "main/context.h"
31 #include "main/framebuffer.h"
32 #include "main/renderbuffer.h"
33 #include "main/texobj.h"
34 #include "main/hash.h"
35 #include "main/fbobject.h"
36 #include "main/version.h"
37 #include "swrast/s_renderbuffer.h"
38 #include "util/ralloc.h"
39 #include "brw_defines.h"
40 #include "brw_state.h"
41 #include "compiler/nir/nir.h"
42
43 #include "utils.h"
44 #include "util/xmlpool.h"
45
46 static const __DRIconfigOptionsExtension brw_config_options = {
47 .base = { __DRI_CONFIG_OPTIONS, 1 },
48 .xml =
49 DRI_CONF_BEGIN
50 DRI_CONF_SECTION_PERFORMANCE
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 DRI_CONF_MESA_NO_ERROR("false")
61 DRI_CONF_SECTION_END
62
63 DRI_CONF_SECTION_QUALITY
64 DRI_CONF_FORCE_S3TC_ENABLE("false")
65
66 DRI_CONF_PRECISE_TRIG("false")
67
68 DRI_CONF_OPT_BEGIN(clamp_max_samples, int, -1)
69 DRI_CONF_DESC(en, "Clamp the value of GL_MAX_SAMPLES to the "
70 "given integer. If negative, then do not clamp.")
71 DRI_CONF_OPT_END
72 DRI_CONF_SECTION_END
73
74 DRI_CONF_SECTION_DEBUG
75 DRI_CONF_NO_RAST("false")
76 DRI_CONF_ALWAYS_FLUSH_BATCH("false")
77 DRI_CONF_ALWAYS_FLUSH_CACHE("false")
78 DRI_CONF_DISABLE_THROTTLING("false")
79 DRI_CONF_FORCE_GLSL_EXTENSIONS_WARN("false")
80 DRI_CONF_FORCE_GLSL_VERSION(0)
81 DRI_CONF_DISABLE_GLSL_LINE_CONTINUATIONS("false")
82 DRI_CONF_DISABLE_BLEND_FUNC_EXTENDED("false")
83 DRI_CONF_DUAL_COLOR_BLEND_BY_LOCATION("false")
84 DRI_CONF_ALLOW_GLSL_EXTENSION_DIRECTIVE_MIDSHADER("false")
85 DRI_CONF_ALLOW_GLSL_BUILTIN_VARIABLE_REDECLARATION("false")
86 DRI_CONF_ALLOW_HIGHER_COMPAT_VERSION("false")
87 DRI_CONF_FORCE_GLSL_ABS_SQRT("false")
88
89 DRI_CONF_OPT_BEGIN_B(shader_precompile, "true")
90 DRI_CONF_DESC(en, "Perform code generation at shader link time.")
91 DRI_CONF_OPT_END
92 DRI_CONF_SECTION_END
93
94 DRI_CONF_SECTION_MISCELLANEOUS
95 DRI_CONF_GLSL_ZERO_INIT("false")
96 DRI_CONF_SECTION_END
97 DRI_CONF_END
98 };
99
100 #include "intel_batchbuffer.h"
101 #include "intel_buffers.h"
102 #include "brw_bufmgr.h"
103 #include "intel_fbo.h"
104 #include "intel_mipmap_tree.h"
105 #include "intel_screen.h"
106 #include "intel_tex.h"
107 #include "intel_image.h"
108
109 #include "brw_context.h"
110
111 #include "i915_drm.h"
112
113 /**
114 * For debugging purposes, this returns a time in seconds.
115 */
116 double
117 get_time(void)
118 {
119 struct timespec tp;
120
121 clock_gettime(CLOCK_MONOTONIC, &tp);
122
123 return tp.tv_sec + tp.tv_nsec / 1000000000.0;
124 }
125
126 static const __DRItexBufferExtension intelTexBufferExtension = {
127 .base = { __DRI_TEX_BUFFER, 3 },
128
129 .setTexBuffer = intelSetTexBuffer,
130 .setTexBuffer2 = intelSetTexBuffer2,
131 .releaseTexBuffer = NULL,
132 };
133
134 static void
135 intel_dri2_flush_with_flags(__DRIcontext *cPriv,
136 __DRIdrawable *dPriv,
137 unsigned flags,
138 enum __DRI2throttleReason reason)
139 {
140 struct brw_context *brw = cPriv->driverPrivate;
141
142 if (!brw)
143 return;
144
145 struct gl_context *ctx = &brw->ctx;
146
147 FLUSH_VERTICES(ctx, 0);
148
149 if (flags & __DRI2_FLUSH_DRAWABLE)
150 intel_resolve_for_dri2_flush(brw, dPriv);
151
152 if (reason == __DRI2_THROTTLE_SWAPBUFFER)
153 brw->need_swap_throttle = true;
154 if (reason == __DRI2_THROTTLE_FLUSHFRONT)
155 brw->need_flush_throttle = true;
156
157 intel_batchbuffer_flush(brw);
158 }
159
160 /**
161 * Provides compatibility with loaders that only support the older (version
162 * 1-3) flush interface.
163 *
164 * That includes libGL up to Mesa 9.0, and the X Server at least up to 1.13.
165 */
166 static void
167 intel_dri2_flush(__DRIdrawable *drawable)
168 {
169 intel_dri2_flush_with_flags(drawable->driContextPriv, drawable,
170 __DRI2_FLUSH_DRAWABLE,
171 __DRI2_THROTTLE_SWAPBUFFER);
172 }
173
174 static const struct __DRI2flushExtensionRec intelFlushExtension = {
175 .base = { __DRI2_FLUSH, 4 },
176
177 .flush = intel_dri2_flush,
178 .invalidate = dri2InvalidateDrawable,
179 .flush_with_flags = intel_dri2_flush_with_flags,
180 };
181
182 static const struct intel_image_format intel_image_formats[] = {
183 { __DRI_IMAGE_FOURCC_ARGB8888, __DRI_IMAGE_COMPONENTS_RGBA, 1,
184 { { 0, 0, 0, __DRI_IMAGE_FORMAT_ARGB8888, 4 } } },
185
186 { __DRI_IMAGE_FOURCC_ABGR8888, __DRI_IMAGE_COMPONENTS_RGBA, 1,
187 { { 0, 0, 0, __DRI_IMAGE_FORMAT_ABGR8888, 4 } } },
188
189 { __DRI_IMAGE_FOURCC_SARGB8888, __DRI_IMAGE_COMPONENTS_RGBA, 1,
190 { { 0, 0, 0, __DRI_IMAGE_FORMAT_SARGB8, 4 } } },
191
192 { __DRI_IMAGE_FOURCC_XRGB8888, __DRI_IMAGE_COMPONENTS_RGB, 1,
193 { { 0, 0, 0, __DRI_IMAGE_FORMAT_XRGB8888, 4 }, } },
194
195 { __DRI_IMAGE_FOURCC_XBGR8888, __DRI_IMAGE_COMPONENTS_RGB, 1,
196 { { 0, 0, 0, __DRI_IMAGE_FORMAT_XBGR8888, 4 }, } },
197
198 { __DRI_IMAGE_FOURCC_ARGB1555, __DRI_IMAGE_COMPONENTS_RGBA, 1,
199 { { 0, 0, 0, __DRI_IMAGE_FORMAT_ARGB1555, 2 } } },
200
201 { __DRI_IMAGE_FOURCC_RGB565, __DRI_IMAGE_COMPONENTS_RGB, 1,
202 { { 0, 0, 0, __DRI_IMAGE_FORMAT_RGB565, 2 } } },
203
204 { __DRI_IMAGE_FOURCC_R8, __DRI_IMAGE_COMPONENTS_R, 1,
205 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 }, } },
206
207 { __DRI_IMAGE_FOURCC_R16, __DRI_IMAGE_COMPONENTS_R, 1,
208 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R16, 1 }, } },
209
210 { __DRI_IMAGE_FOURCC_GR88, __DRI_IMAGE_COMPONENTS_RG, 1,
211 { { 0, 0, 0, __DRI_IMAGE_FORMAT_GR88, 2 }, } },
212
213 { __DRI_IMAGE_FOURCC_GR1616, __DRI_IMAGE_COMPONENTS_RG, 1,
214 { { 0, 0, 0, __DRI_IMAGE_FORMAT_GR1616, 2 }, } },
215
216 { __DRI_IMAGE_FOURCC_YUV410, __DRI_IMAGE_COMPONENTS_Y_U_V, 3,
217 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
218 { 1, 2, 2, __DRI_IMAGE_FORMAT_R8, 1 },
219 { 2, 2, 2, __DRI_IMAGE_FORMAT_R8, 1 } } },
220
221 { __DRI_IMAGE_FOURCC_YUV411, __DRI_IMAGE_COMPONENTS_Y_U_V, 3,
222 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
223 { 1, 2, 0, __DRI_IMAGE_FORMAT_R8, 1 },
224 { 2, 2, 0, __DRI_IMAGE_FORMAT_R8, 1 } } },
225
226 { __DRI_IMAGE_FOURCC_YUV420, __DRI_IMAGE_COMPONENTS_Y_U_V, 3,
227 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
228 { 1, 1, 1, __DRI_IMAGE_FORMAT_R8, 1 },
229 { 2, 1, 1, __DRI_IMAGE_FORMAT_R8, 1 } } },
230
231 { __DRI_IMAGE_FOURCC_YUV422, __DRI_IMAGE_COMPONENTS_Y_U_V, 3,
232 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
233 { 1, 1, 0, __DRI_IMAGE_FORMAT_R8, 1 },
234 { 2, 1, 0, __DRI_IMAGE_FORMAT_R8, 1 } } },
235
236 { __DRI_IMAGE_FOURCC_YUV444, __DRI_IMAGE_COMPONENTS_Y_U_V, 3,
237 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
238 { 1, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
239 { 2, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 } } },
240
241 { __DRI_IMAGE_FOURCC_YVU410, __DRI_IMAGE_COMPONENTS_Y_U_V, 3,
242 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
243 { 2, 2, 2, __DRI_IMAGE_FORMAT_R8, 1 },
244 { 1, 2, 2, __DRI_IMAGE_FORMAT_R8, 1 } } },
245
246 { __DRI_IMAGE_FOURCC_YVU411, __DRI_IMAGE_COMPONENTS_Y_U_V, 3,
247 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
248 { 2, 2, 0, __DRI_IMAGE_FORMAT_R8, 1 },
249 { 1, 2, 0, __DRI_IMAGE_FORMAT_R8, 1 } } },
250
251 { __DRI_IMAGE_FOURCC_YVU420, __DRI_IMAGE_COMPONENTS_Y_U_V, 3,
252 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
253 { 2, 1, 1, __DRI_IMAGE_FORMAT_R8, 1 },
254 { 1, 1, 1, __DRI_IMAGE_FORMAT_R8, 1 } } },
255
256 { __DRI_IMAGE_FOURCC_YVU422, __DRI_IMAGE_COMPONENTS_Y_U_V, 3,
257 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
258 { 2, 1, 0, __DRI_IMAGE_FORMAT_R8, 1 },
259 { 1, 1, 0, __DRI_IMAGE_FORMAT_R8, 1 } } },
260
261 { __DRI_IMAGE_FOURCC_YVU444, __DRI_IMAGE_COMPONENTS_Y_U_V, 3,
262 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
263 { 2, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
264 { 1, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 } } },
265
266 { __DRI_IMAGE_FOURCC_NV12, __DRI_IMAGE_COMPONENTS_Y_UV, 2,
267 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
268 { 1, 1, 1, __DRI_IMAGE_FORMAT_GR88, 2 } } },
269
270 { __DRI_IMAGE_FOURCC_NV16, __DRI_IMAGE_COMPONENTS_Y_UV, 2,
271 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
272 { 1, 1, 0, __DRI_IMAGE_FORMAT_GR88, 2 } } },
273
274 /* For YUYV and UYVY buffers, we set up two overlapping DRI images
275 * and treat them as planar buffers in the compositors.
276 * Plane 0 is GR88 and samples YU or YV pairs and places Y into
277 * the R component, while plane 1 is ARGB/ABGR and samples YUYV/UYVY
278 * clusters and places pairs and places U into the G component and
279 * V into A. This lets the texture sampler interpolate the Y
280 * components correctly when sampling from plane 0, and interpolate
281 * U and V correctly when sampling from plane 1. */
282 { __DRI_IMAGE_FOURCC_YUYV, __DRI_IMAGE_COMPONENTS_Y_XUXV, 2,
283 { { 0, 0, 0, __DRI_IMAGE_FORMAT_GR88, 2 },
284 { 0, 1, 0, __DRI_IMAGE_FORMAT_ARGB8888, 4 } } },
285 { __DRI_IMAGE_FOURCC_UYVY, __DRI_IMAGE_COMPONENTS_Y_UXVX, 2,
286 { { 0, 0, 0, __DRI_IMAGE_FORMAT_GR88, 2 },
287 { 0, 1, 0, __DRI_IMAGE_FORMAT_ABGR8888, 4 } } }
288 };
289
290 static const struct {
291 uint64_t modifier;
292 unsigned since_gen;
293 } supported_modifiers[] = {
294 { .modifier = DRM_FORMAT_MOD_LINEAR , .since_gen = 1 },
295 { .modifier = I915_FORMAT_MOD_X_TILED , .since_gen = 1 },
296 { .modifier = I915_FORMAT_MOD_Y_TILED , .since_gen = 6 },
297 { .modifier = I915_FORMAT_MOD_Y_TILED_CCS , .since_gen = 9 },
298 };
299
300 static bool
301 modifier_is_supported(const struct gen_device_info *devinfo,
302 struct intel_image_format *fmt, int dri_format,
303 uint64_t modifier)
304 {
305 const struct isl_drm_modifier_info *modinfo =
306 isl_drm_modifier_get_info(modifier);
307 int i;
308
309 /* ISL had better know about the modifier */
310 if (!modinfo)
311 return false;
312
313 if (modinfo->aux_usage == ISL_AUX_USAGE_CCS_E) {
314 /* If INTEL_DEBUG=norbc is set, don't support any CCS_E modifiers */
315 if (unlikely(INTEL_DEBUG & DEBUG_NO_RBC))
316 return false;
317
318 /* CCS_E is not supported for planar images */
319 if (fmt && fmt->nplanes > 1)
320 return false;
321
322 if (fmt) {
323 assert(dri_format == 0);
324 dri_format = fmt->planes[0].dri_format;
325 }
326
327 mesa_format format = driImageFormatToGLFormat(dri_format);
328 format = _mesa_get_srgb_format_linear(format);
329 if (!isl_format_supports_ccs_e(devinfo,
330 brw_isl_format_for_mesa_format(format)))
331 return false;
332 }
333
334 for (i = 0; i < ARRAY_SIZE(supported_modifiers); i++) {
335 if (supported_modifiers[i].modifier != modifier)
336 continue;
337
338 return supported_modifiers[i].since_gen <= devinfo->gen;
339 }
340
341 return false;
342 }
343
344 static uint64_t
345 tiling_to_modifier(uint32_t tiling)
346 {
347 static const uint64_t map[] = {
348 [I915_TILING_NONE] = DRM_FORMAT_MOD_LINEAR,
349 [I915_TILING_X] = I915_FORMAT_MOD_X_TILED,
350 [I915_TILING_Y] = I915_FORMAT_MOD_Y_TILED,
351 };
352
353 assert(tiling < ARRAY_SIZE(map));
354
355 return map[tiling];
356 }
357
358 static void
359 intel_image_warn_if_unaligned(__DRIimage *image, const char *func)
360 {
361 uint32_t tiling, swizzle;
362 brw_bo_get_tiling(image->bo, &tiling, &swizzle);
363
364 if (tiling != I915_TILING_NONE && (image->offset & 0xfff)) {
365 _mesa_warning(NULL, "%s: offset 0x%08x not on tile boundary",
366 func, image->offset);
367 }
368 }
369
370 static const struct intel_image_format *
371 intel_image_format_lookup(int fourcc)
372 {
373 for (unsigned i = 0; i < ARRAY_SIZE(intel_image_formats); i++) {
374 if (intel_image_formats[i].fourcc == fourcc)
375 return &intel_image_formats[i];
376 }
377
378 return NULL;
379 }
380
381 static boolean intel_lookup_fourcc(int dri_format, int *fourcc)
382 {
383 for (unsigned i = 0; i < ARRAY_SIZE(intel_image_formats); i++) {
384 if (intel_image_formats[i].planes[0].dri_format == dri_format) {
385 *fourcc = intel_image_formats[i].fourcc;
386 return true;
387 }
388 }
389 return false;
390 }
391
392 static __DRIimage *
393 intel_allocate_image(struct intel_screen *screen, int dri_format,
394 void *loaderPrivate)
395 {
396 __DRIimage *image;
397
398 image = calloc(1, sizeof *image);
399 if (image == NULL)
400 return NULL;
401
402 image->screen = screen;
403 image->dri_format = dri_format;
404 image->offset = 0;
405
406 image->format = driImageFormatToGLFormat(dri_format);
407 if (dri_format != __DRI_IMAGE_FORMAT_NONE &&
408 image->format == MESA_FORMAT_NONE) {
409 free(image);
410 return NULL;
411 }
412
413 image->internal_format = _mesa_get_format_base_format(image->format);
414 image->data = loaderPrivate;
415
416 return image;
417 }
418
419 /**
420 * Sets up a DRIImage structure to point to a slice out of a miptree.
421 */
422 static void
423 intel_setup_image_from_mipmap_tree(struct brw_context *brw, __DRIimage *image,
424 struct intel_mipmap_tree *mt, GLuint level,
425 GLuint zoffset)
426 {
427 intel_miptree_make_shareable(brw, mt);
428
429 intel_miptree_check_level_layer(mt, level, zoffset);
430
431 image->width = minify(mt->surf.phys_level0_sa.width,
432 level - mt->first_level);
433 image->height = minify(mt->surf.phys_level0_sa.height,
434 level - mt->first_level);
435 image->pitch = mt->surf.row_pitch;
436
437 image->offset = intel_miptree_get_tile_offsets(mt, level, zoffset,
438 &image->tile_x,
439 &image->tile_y);
440
441 brw_bo_unreference(image->bo);
442 image->bo = mt->bo;
443 brw_bo_reference(mt->bo);
444 }
445
446 static __DRIimage *
447 intel_create_image_from_name(__DRIscreen *dri_screen,
448 int width, int height, int format,
449 int name, int pitch, void *loaderPrivate)
450 {
451 struct intel_screen *screen = dri_screen->driverPrivate;
452 __DRIimage *image;
453 int cpp;
454
455 image = intel_allocate_image(screen, format, loaderPrivate);
456 if (image == NULL)
457 return NULL;
458
459 if (image->format == MESA_FORMAT_NONE)
460 cpp = 1;
461 else
462 cpp = _mesa_get_format_bytes(image->format);
463
464 image->width = width;
465 image->height = height;
466 image->pitch = pitch * cpp;
467 image->bo = brw_bo_gem_create_from_name(screen->bufmgr, "image",
468 name);
469 if (!image->bo) {
470 free(image);
471 return NULL;
472 }
473 image->modifier = tiling_to_modifier(image->bo->tiling_mode);
474
475 return image;
476 }
477
478 static __DRIimage *
479 intel_create_image_from_renderbuffer(__DRIcontext *context,
480 int renderbuffer, void *loaderPrivate)
481 {
482 __DRIimage *image;
483 struct brw_context *brw = context->driverPrivate;
484 struct gl_context *ctx = &brw->ctx;
485 struct gl_renderbuffer *rb;
486 struct intel_renderbuffer *irb;
487
488 rb = _mesa_lookup_renderbuffer(ctx, renderbuffer);
489 if (!rb) {
490 _mesa_error(ctx, GL_INVALID_OPERATION, "glRenderbufferExternalMESA");
491 return NULL;
492 }
493
494 irb = intel_renderbuffer(rb);
495 intel_miptree_make_shareable(brw, irb->mt);
496 image = calloc(1, sizeof *image);
497 if (image == NULL)
498 return NULL;
499
500 image->internal_format = rb->InternalFormat;
501 image->format = rb->Format;
502 image->modifier = tiling_to_modifier(
503 isl_tiling_to_i915_tiling(irb->mt->surf.tiling));
504 image->offset = 0;
505 image->data = loaderPrivate;
506 brw_bo_unreference(image->bo);
507 image->bo = irb->mt->bo;
508 brw_bo_reference(irb->mt->bo);
509 image->width = rb->Width;
510 image->height = rb->Height;
511 image->pitch = irb->mt->surf.row_pitch;
512 image->dri_format = driGLFormatToImageFormat(image->format);
513 image->has_depthstencil = irb->mt->stencil_mt? true : false;
514
515 rb->NeedsFinishRenderTexture = true;
516 return image;
517 }
518
519 static __DRIimage *
520 intel_create_image_from_texture(__DRIcontext *context, int target,
521 unsigned texture, int zoffset,
522 int level,
523 unsigned *error,
524 void *loaderPrivate)
525 {
526 __DRIimage *image;
527 struct brw_context *brw = context->driverPrivate;
528 struct gl_texture_object *obj;
529 struct intel_texture_object *iobj;
530 GLuint face = 0;
531
532 obj = _mesa_lookup_texture(&brw->ctx, texture);
533 if (!obj || obj->Target != target) {
534 *error = __DRI_IMAGE_ERROR_BAD_PARAMETER;
535 return NULL;
536 }
537
538 if (target == GL_TEXTURE_CUBE_MAP)
539 face = zoffset;
540
541 _mesa_test_texobj_completeness(&brw->ctx, obj);
542 iobj = intel_texture_object(obj);
543 if (!obj->_BaseComplete || (level > 0 && !obj->_MipmapComplete)) {
544 *error = __DRI_IMAGE_ERROR_BAD_PARAMETER;
545 return NULL;
546 }
547
548 if (level < obj->BaseLevel || level > obj->_MaxLevel) {
549 *error = __DRI_IMAGE_ERROR_BAD_MATCH;
550 return NULL;
551 }
552
553 if (target == GL_TEXTURE_3D && obj->Image[face][level]->Depth < zoffset) {
554 *error = __DRI_IMAGE_ERROR_BAD_MATCH;
555 return NULL;
556 }
557 image = calloc(1, sizeof *image);
558 if (image == NULL) {
559 *error = __DRI_IMAGE_ERROR_BAD_ALLOC;
560 return NULL;
561 }
562
563 image->internal_format = obj->Image[face][level]->InternalFormat;
564 image->format = obj->Image[face][level]->TexFormat;
565 image->modifier = tiling_to_modifier(
566 isl_tiling_to_i915_tiling(iobj->mt->surf.tiling));
567 image->data = loaderPrivate;
568 intel_setup_image_from_mipmap_tree(brw, image, iobj->mt, level, zoffset);
569 image->dri_format = driGLFormatToImageFormat(image->format);
570 image->has_depthstencil = iobj->mt->stencil_mt? true : false;
571 if (image->dri_format == MESA_FORMAT_NONE) {
572 *error = __DRI_IMAGE_ERROR_BAD_PARAMETER;
573 free(image);
574 return NULL;
575 }
576
577 *error = __DRI_IMAGE_ERROR_SUCCESS;
578 return image;
579 }
580
581 static void
582 intel_destroy_image(__DRIimage *image)
583 {
584 brw_bo_unreference(image->bo);
585 free(image);
586 }
587
588 enum modifier_priority {
589 MODIFIER_PRIORITY_INVALID = 0,
590 MODIFIER_PRIORITY_LINEAR,
591 MODIFIER_PRIORITY_X,
592 MODIFIER_PRIORITY_Y,
593 MODIFIER_PRIORITY_Y_CCS,
594 };
595
596 const uint64_t priority_to_modifier[] = {
597 [MODIFIER_PRIORITY_INVALID] = DRM_FORMAT_MOD_INVALID,
598 [MODIFIER_PRIORITY_LINEAR] = DRM_FORMAT_MOD_LINEAR,
599 [MODIFIER_PRIORITY_X] = I915_FORMAT_MOD_X_TILED,
600 [MODIFIER_PRIORITY_Y] = I915_FORMAT_MOD_Y_TILED,
601 [MODIFIER_PRIORITY_Y_CCS] = I915_FORMAT_MOD_Y_TILED_CCS,
602 };
603
604 static uint64_t
605 select_best_modifier(struct gen_device_info *devinfo,
606 int dri_format,
607 const uint64_t *modifiers,
608 const unsigned count)
609 {
610 enum modifier_priority prio = MODIFIER_PRIORITY_INVALID;
611
612 for (int i = 0; i < count; i++) {
613 if (!modifier_is_supported(devinfo, NULL, dri_format, modifiers[i]))
614 continue;
615
616 switch (modifiers[i]) {
617 case I915_FORMAT_MOD_Y_TILED_CCS:
618 prio = MAX2(prio, MODIFIER_PRIORITY_Y_CCS);
619 break;
620 case I915_FORMAT_MOD_Y_TILED:
621 prio = MAX2(prio, MODIFIER_PRIORITY_Y);
622 break;
623 case I915_FORMAT_MOD_X_TILED:
624 prio = MAX2(prio, MODIFIER_PRIORITY_X);
625 break;
626 case DRM_FORMAT_MOD_LINEAR:
627 prio = MAX2(prio, MODIFIER_PRIORITY_LINEAR);
628 break;
629 case DRM_FORMAT_MOD_INVALID:
630 default:
631 break;
632 }
633 }
634
635 return priority_to_modifier[prio];
636 }
637
638 static __DRIimage *
639 intel_create_image_common(__DRIscreen *dri_screen,
640 int width, int height, int format,
641 unsigned int use,
642 const uint64_t *modifiers,
643 unsigned count,
644 void *loaderPrivate)
645 {
646 __DRIimage *image;
647 struct intel_screen *screen = dri_screen->driverPrivate;
648 uint64_t modifier = DRM_FORMAT_MOD_INVALID;
649 bool ok;
650
651 /* Callers of this may specify a modifier, or a dri usage, but not both. The
652 * newer modifier interface deprecates the older usage flags newer modifier
653 * interface deprecates the older usage flags.
654 */
655 assert(!(use && count));
656
657 if (use & __DRI_IMAGE_USE_CURSOR) {
658 if (width != 64 || height != 64)
659 return NULL;
660 modifier = DRM_FORMAT_MOD_LINEAR;
661 }
662
663 if (use & __DRI_IMAGE_USE_LINEAR)
664 modifier = DRM_FORMAT_MOD_LINEAR;
665
666 if (modifier == DRM_FORMAT_MOD_INVALID) {
667 if (modifiers) {
668 /* User requested specific modifiers */
669 modifier = select_best_modifier(&screen->devinfo, format,
670 modifiers, count);
671 if (modifier == DRM_FORMAT_MOD_INVALID)
672 return NULL;
673 } else {
674 /* Historically, X-tiled was the default, and so lack of modifier means
675 * X-tiled.
676 */
677 modifier = I915_FORMAT_MOD_X_TILED;
678 }
679 }
680
681 image = intel_allocate_image(screen, format, loaderPrivate);
682 if (image == NULL)
683 return NULL;
684
685 const struct isl_drm_modifier_info *mod_info =
686 isl_drm_modifier_get_info(modifier);
687
688 struct isl_surf surf;
689 ok = isl_surf_init(&screen->isl_dev, &surf,
690 .dim = ISL_SURF_DIM_2D,
691 .format = brw_isl_format_for_mesa_format(image->format),
692 .width = width,
693 .height = height,
694 .depth = 1,
695 .levels = 1,
696 .array_len = 1,
697 .samples = 1,
698 .usage = ISL_SURF_USAGE_RENDER_TARGET_BIT |
699 ISL_SURF_USAGE_TEXTURE_BIT |
700 ISL_SURF_USAGE_STORAGE_BIT,
701 .tiling_flags = (1 << mod_info->tiling));
702 assert(ok);
703 if (!ok) {
704 free(image);
705 return NULL;
706 }
707
708 struct isl_surf aux_surf;
709 if (mod_info->aux_usage == ISL_AUX_USAGE_CCS_E) {
710 ok = isl_surf_get_ccs_surf(&screen->isl_dev, &surf, &aux_surf, 0);
711 if (!ok) {
712 free(image);
713 return NULL;
714 }
715 } else {
716 assert(mod_info->aux_usage == ISL_AUX_USAGE_NONE);
717 aux_surf.size = 0;
718 }
719
720 /* We request that the bufmgr zero the buffer for us for two reasons:
721 *
722 * 1) If a buffer gets re-used from the pool, we don't want to leak random
723 * garbage from our process to some other.
724 *
725 * 2) For images with CCS_E, we want to ensure that the CCS starts off in
726 * a valid state. A CCS value of 0 indicates that the given block is
727 * in the pass-through state which is what we want.
728 */
729 image->bo = brw_bo_alloc_tiled(screen->bufmgr, "image",
730 surf.size + aux_surf.size,
731 isl_tiling_to_i915_tiling(mod_info->tiling),
732 surf.row_pitch, BO_ALLOC_ZEROED);
733 if (image->bo == NULL) {
734 free(image);
735 return NULL;
736 }
737 image->width = width;
738 image->height = height;
739 image->pitch = surf.row_pitch;
740 image->modifier = modifier;
741
742 if (aux_surf.size) {
743 image->aux_offset = surf.size;
744 image->aux_pitch = aux_surf.row_pitch;
745 }
746
747 return image;
748 }
749
750 static __DRIimage *
751 intel_create_image(__DRIscreen *dri_screen,
752 int width, int height, int format,
753 unsigned int use,
754 void *loaderPrivate)
755 {
756 return intel_create_image_common(dri_screen, width, height, format, use, NULL, 0,
757 loaderPrivate);
758 }
759
760 static __DRIimage *
761 intel_create_image_with_modifiers(__DRIscreen *dri_screen,
762 int width, int height, int format,
763 const uint64_t *modifiers,
764 const unsigned count,
765 void *loaderPrivate)
766 {
767 return intel_create_image_common(dri_screen, width, height, format, 0,
768 modifiers, count, loaderPrivate);
769 }
770
771 static GLboolean
772 intel_query_image(__DRIimage *image, int attrib, int *value)
773 {
774 switch (attrib) {
775 case __DRI_IMAGE_ATTRIB_STRIDE:
776 *value = image->pitch;
777 return true;
778 case __DRI_IMAGE_ATTRIB_HANDLE:
779 *value = image->bo->gem_handle;
780 return true;
781 case __DRI_IMAGE_ATTRIB_NAME:
782 return !brw_bo_flink(image->bo, (uint32_t *) value);
783 case __DRI_IMAGE_ATTRIB_FORMAT:
784 *value = image->dri_format;
785 return true;
786 case __DRI_IMAGE_ATTRIB_WIDTH:
787 *value = image->width;
788 return true;
789 case __DRI_IMAGE_ATTRIB_HEIGHT:
790 *value = image->height;
791 return true;
792 case __DRI_IMAGE_ATTRIB_COMPONENTS:
793 if (image->planar_format == NULL)
794 return false;
795 *value = image->planar_format->components;
796 return true;
797 case __DRI_IMAGE_ATTRIB_FD:
798 return !brw_bo_gem_export_to_prime(image->bo, value);
799 case __DRI_IMAGE_ATTRIB_FOURCC:
800 return intel_lookup_fourcc(image->dri_format, value);
801 case __DRI_IMAGE_ATTRIB_NUM_PLANES:
802 *value = isl_drm_modifier_has_aux(image->modifier) ? 2 : 1;
803 return true;
804 case __DRI_IMAGE_ATTRIB_OFFSET:
805 *value = image->offset;
806 return true;
807 case __DRI_IMAGE_ATTRIB_MODIFIER_LOWER:
808 *value = (image->modifier & 0xffffffff);
809 return true;
810 case __DRI_IMAGE_ATTRIB_MODIFIER_UPPER:
811 *value = ((image->modifier >> 32) & 0xffffffff);
812 return true;
813
814 default:
815 return false;
816 }
817 }
818
819 static __DRIimage *
820 intel_dup_image(__DRIimage *orig_image, void *loaderPrivate)
821 {
822 __DRIimage *image;
823
824 image = calloc(1, sizeof *image);
825 if (image == NULL)
826 return NULL;
827
828 brw_bo_reference(orig_image->bo);
829 image->bo = orig_image->bo;
830 image->internal_format = orig_image->internal_format;
831 image->planar_format = orig_image->planar_format;
832 image->dri_format = orig_image->dri_format;
833 image->format = orig_image->format;
834 image->modifier = orig_image->modifier;
835 image->offset = orig_image->offset;
836 image->width = orig_image->width;
837 image->height = orig_image->height;
838 image->pitch = orig_image->pitch;
839 image->tile_x = orig_image->tile_x;
840 image->tile_y = orig_image->tile_y;
841 image->has_depthstencil = orig_image->has_depthstencil;
842 image->data = loaderPrivate;
843
844 memcpy(image->strides, orig_image->strides, sizeof(image->strides));
845 memcpy(image->offsets, orig_image->offsets, sizeof(image->offsets));
846
847 return image;
848 }
849
850 static GLboolean
851 intel_validate_usage(__DRIimage *image, unsigned int use)
852 {
853 if (use & __DRI_IMAGE_USE_CURSOR) {
854 if (image->width != 64 || image->height != 64)
855 return GL_FALSE;
856 }
857
858 return GL_TRUE;
859 }
860
861 static __DRIimage *
862 intel_create_image_from_names(__DRIscreen *dri_screen,
863 int width, int height, int fourcc,
864 int *names, int num_names,
865 int *strides, int *offsets,
866 void *loaderPrivate)
867 {
868 const struct intel_image_format *f = NULL;
869 __DRIimage *image;
870 int i, index;
871
872 if (dri_screen == NULL || names == NULL || num_names != 1)
873 return NULL;
874
875 f = intel_image_format_lookup(fourcc);
876 if (f == NULL)
877 return NULL;
878
879 image = intel_create_image_from_name(dri_screen, width, height,
880 __DRI_IMAGE_FORMAT_NONE,
881 names[0], strides[0],
882 loaderPrivate);
883
884 if (image == NULL)
885 return NULL;
886
887 image->planar_format = f;
888 for (i = 0; i < f->nplanes; i++) {
889 index = f->planes[i].buffer_index;
890 image->offsets[index] = offsets[index];
891 image->strides[index] = strides[index];
892 }
893
894 return image;
895 }
896
897 static __DRIimage *
898 intel_create_image_from_fds_common(__DRIscreen *dri_screen,
899 int width, int height, int fourcc,
900 uint64_t modifier, int *fds, int num_fds,
901 int *strides, int *offsets,
902 void *loaderPrivate)
903 {
904 struct intel_screen *screen = dri_screen->driverPrivate;
905 const struct intel_image_format *f;
906 __DRIimage *image;
907 int i, index;
908 bool ok;
909
910 if (fds == NULL || num_fds < 1)
911 return NULL;
912
913 f = intel_image_format_lookup(fourcc);
914 if (f == NULL)
915 return NULL;
916
917 if (modifier != DRM_FORMAT_MOD_INVALID &&
918 !modifier_is_supported(&screen->devinfo, f, 0, modifier))
919 return NULL;
920
921 if (f->nplanes == 1)
922 image = intel_allocate_image(screen, f->planes[0].dri_format,
923 loaderPrivate);
924 else
925 image = intel_allocate_image(screen, __DRI_IMAGE_FORMAT_NONE,
926 loaderPrivate);
927
928 if (image == NULL)
929 return NULL;
930
931 image->width = width;
932 image->height = height;
933 image->pitch = strides[0];
934
935 image->planar_format = f;
936
937 image->bo = brw_bo_gem_create_from_prime(screen->bufmgr, fds[0]);
938 if (image->bo == NULL) {
939 free(image);
940 return NULL;
941 }
942
943 /* We only support all planes from the same bo.
944 * brw_bo_gem_create_from_prime() should return the same pointer for all
945 * fds received here */
946 for (i = 1; i < num_fds; i++) {
947 struct brw_bo *aux = brw_bo_gem_create_from_prime(screen->bufmgr, fds[i]);
948 brw_bo_unreference(aux);
949 if (aux != image->bo) {
950 brw_bo_unreference(image->bo);
951 free(image);
952 return NULL;
953 }
954 }
955
956 if (modifier != DRM_FORMAT_MOD_INVALID)
957 image->modifier = modifier;
958 else
959 image->modifier = tiling_to_modifier(image->bo->tiling_mode);
960
961 const struct isl_drm_modifier_info *mod_info =
962 isl_drm_modifier_get_info(image->modifier);
963
964 int size = 0;
965 struct isl_surf surf;
966 for (i = 0; i < f->nplanes; i++) {
967 index = f->planes[i].buffer_index;
968 image->offsets[index] = offsets[index];
969 image->strides[index] = strides[index];
970
971 mesa_format format = driImageFormatToGLFormat(f->planes[i].dri_format);
972
973 ok = isl_surf_init(&screen->isl_dev, &surf,
974 .dim = ISL_SURF_DIM_2D,
975 .format = brw_isl_format_for_mesa_format(format),
976 .width = image->width >> f->planes[i].width_shift,
977 .height = image->height >> f->planes[i].height_shift,
978 .depth = 1,
979 .levels = 1,
980 .array_len = 1,
981 .samples = 1,
982 .row_pitch = strides[index],
983 .usage = ISL_SURF_USAGE_RENDER_TARGET_BIT |
984 ISL_SURF_USAGE_TEXTURE_BIT |
985 ISL_SURF_USAGE_STORAGE_BIT,
986 .tiling_flags = (1 << mod_info->tiling));
987 if (!ok) {
988 brw_bo_unreference(image->bo);
989 free(image);
990 return NULL;
991 }
992
993 const int end = offsets[index] + surf.size;
994 if (size < end)
995 size = end;
996 }
997
998 if (mod_info->aux_usage == ISL_AUX_USAGE_CCS_E) {
999 /* Even though we initialize surf in the loop above, we know that
1000 * anything with CCS_E will have exactly one plane so surf is properly
1001 * initialized when we get here.
1002 */
1003 assert(f->nplanes == 1);
1004
1005 image->aux_offset = offsets[1];
1006 image->aux_pitch = strides[1];
1007
1008 /* Scanout hardware requires that the CCS be placed after the main
1009 * surface in memory. We consider any CCS that is placed any earlier in
1010 * memory to be invalid and reject it.
1011 *
1012 * At some point in the future, this restriction may be relaxed if the
1013 * hardware becomes less strict but we may need a new modifier for that.
1014 */
1015 assert(size > 0);
1016 if (image->aux_offset < size) {
1017 brw_bo_unreference(image->bo);
1018 free(image);
1019 return NULL;
1020 }
1021
1022 struct isl_surf aux_surf;
1023 ok = isl_surf_get_ccs_surf(&screen->isl_dev, &surf, &aux_surf,
1024 image->aux_pitch);
1025 if (!ok) {
1026 brw_bo_unreference(image->bo);
1027 free(image);
1028 return NULL;
1029 }
1030
1031 const int end = image->aux_offset + aux_surf.size;
1032 if (size < end)
1033 size = end;
1034 } else {
1035 assert(mod_info->aux_usage == ISL_AUX_USAGE_NONE);
1036 }
1037
1038 /* Check that the requested image actually fits within the BO. 'size'
1039 * is already relative to the offsets, so we don't need to add that. */
1040 if (image->bo->size == 0) {
1041 image->bo->size = size;
1042 } else if (size > image->bo->size) {
1043 brw_bo_unreference(image->bo);
1044 free(image);
1045 return NULL;
1046 }
1047
1048 if (f->nplanes == 1) {
1049 image->offset = image->offsets[0];
1050 intel_image_warn_if_unaligned(image, __func__);
1051 }
1052
1053 return image;
1054 }
1055
1056 static __DRIimage *
1057 intel_create_image_from_fds(__DRIscreen *dri_screen,
1058 int width, int height, int fourcc,
1059 int *fds, int num_fds, int *strides, int *offsets,
1060 void *loaderPrivate)
1061 {
1062 return intel_create_image_from_fds_common(dri_screen, width, height, fourcc,
1063 DRM_FORMAT_MOD_INVALID,
1064 fds, num_fds, strides, offsets,
1065 loaderPrivate);
1066 }
1067
1068 static __DRIimage *
1069 intel_create_image_from_dma_bufs2(__DRIscreen *dri_screen,
1070 int width, int height,
1071 int fourcc, uint64_t modifier,
1072 int *fds, int num_fds,
1073 int *strides, int *offsets,
1074 enum __DRIYUVColorSpace yuv_color_space,
1075 enum __DRISampleRange sample_range,
1076 enum __DRIChromaSiting horizontal_siting,
1077 enum __DRIChromaSiting vertical_siting,
1078 unsigned *error,
1079 void *loaderPrivate)
1080 {
1081 __DRIimage *image;
1082 const struct intel_image_format *f = intel_image_format_lookup(fourcc);
1083
1084 if (!f) {
1085 *error = __DRI_IMAGE_ERROR_BAD_MATCH;
1086 return NULL;
1087 }
1088
1089 image = intel_create_image_from_fds_common(dri_screen, width, height,
1090 fourcc, modifier,
1091 fds, num_fds, strides, offsets,
1092 loaderPrivate);
1093
1094 /*
1095 * Invalid parameters and any inconsistencies between are assumed to be
1096 * checked by the caller. Therefore besides unsupported formats one can fail
1097 * only in allocation.
1098 */
1099 if (!image) {
1100 *error = __DRI_IMAGE_ERROR_BAD_ALLOC;
1101 return NULL;
1102 }
1103
1104 image->dma_buf_imported = true;
1105 image->yuv_color_space = yuv_color_space;
1106 image->sample_range = sample_range;
1107 image->horizontal_siting = horizontal_siting;
1108 image->vertical_siting = vertical_siting;
1109
1110 *error = __DRI_IMAGE_ERROR_SUCCESS;
1111 return image;
1112 }
1113
1114 static __DRIimage *
1115 intel_create_image_from_dma_bufs(__DRIscreen *dri_screen,
1116 int width, int height, int fourcc,
1117 int *fds, int num_fds,
1118 int *strides, int *offsets,
1119 enum __DRIYUVColorSpace yuv_color_space,
1120 enum __DRISampleRange sample_range,
1121 enum __DRIChromaSiting horizontal_siting,
1122 enum __DRIChromaSiting vertical_siting,
1123 unsigned *error,
1124 void *loaderPrivate)
1125 {
1126 return intel_create_image_from_dma_bufs2(dri_screen, width, height,
1127 fourcc, DRM_FORMAT_MOD_INVALID,
1128 fds, num_fds, strides, offsets,
1129 yuv_color_space,
1130 sample_range,
1131 horizontal_siting,
1132 vertical_siting,
1133 error,
1134 loaderPrivate);
1135 }
1136
1137 static GLboolean
1138 intel_query_dma_buf_formats(__DRIscreen *screen, int max,
1139 int *formats, int *count)
1140 {
1141 int i, j = 0;
1142
1143 if (max == 0) {
1144 *count = ARRAY_SIZE(intel_image_formats) - 1; /* not SARGB */
1145 return true;
1146 }
1147
1148 for (i = 0; i < (ARRAY_SIZE(intel_image_formats)) && j < max; i++) {
1149 if (intel_image_formats[i].fourcc == __DRI_IMAGE_FOURCC_SARGB8888)
1150 continue;
1151 formats[j++] = intel_image_formats[i].fourcc;
1152 }
1153
1154 *count = j;
1155 return true;
1156 }
1157
1158 static GLboolean
1159 intel_query_dma_buf_modifiers(__DRIscreen *_screen, int fourcc, int max,
1160 uint64_t *modifiers,
1161 unsigned int *external_only,
1162 int *count)
1163 {
1164 struct intel_screen *screen = _screen->driverPrivate;
1165 const struct intel_image_format *f;
1166 int num_mods = 0, i;
1167
1168 f = intel_image_format_lookup(fourcc);
1169 if (f == NULL)
1170 return false;
1171
1172 for (i = 0; i < ARRAY_SIZE(supported_modifiers); i++) {
1173 uint64_t modifier = supported_modifiers[i].modifier;
1174 if (!modifier_is_supported(&screen->devinfo, f, 0, modifier))
1175 continue;
1176
1177 num_mods++;
1178 if (max == 0)
1179 continue;
1180
1181 modifiers[num_mods - 1] = modifier;
1182 if (num_mods >= max)
1183 break;
1184 }
1185
1186 if (external_only != NULL) {
1187 for (i = 0; i < num_mods && i < max; i++) {
1188 if (f->components == __DRI_IMAGE_COMPONENTS_Y_U_V ||
1189 f->components == __DRI_IMAGE_COMPONENTS_Y_UV ||
1190 f->components == __DRI_IMAGE_COMPONENTS_Y_XUXV) {
1191 external_only[i] = GL_TRUE;
1192 }
1193 else {
1194 external_only[i] = GL_FALSE;
1195 }
1196 }
1197 }
1198
1199 *count = num_mods;
1200 return true;
1201 }
1202
1203 static __DRIimage *
1204 intel_from_planar(__DRIimage *parent, int plane, void *loaderPrivate)
1205 {
1206 int width, height, offset, stride, dri_format, index;
1207 const struct intel_image_format *f;
1208 __DRIimage *image;
1209
1210 if (parent == NULL) {
1211 return NULL;
1212 } else if (parent->planar_format == NULL) {
1213 const bool is_aux =
1214 isl_drm_modifier_has_aux(parent->modifier) && plane == 1;
1215 if (!is_aux)
1216 return NULL;
1217
1218 width = parent->width;
1219 height = parent->height;
1220 dri_format = parent->dri_format;
1221 offset = parent->aux_offset;
1222 stride = parent->aux_pitch;
1223 } else {
1224 /* Planar formats don't support aux buffers/images */
1225 assert(!isl_drm_modifier_has_aux(parent->modifier));
1226 f = parent->planar_format;
1227
1228 if (plane >= f->nplanes)
1229 return NULL;
1230
1231 width = parent->width >> f->planes[plane].width_shift;
1232 height = parent->height >> f->planes[plane].height_shift;
1233 dri_format = f->planes[plane].dri_format;
1234 index = f->planes[plane].buffer_index;
1235 offset = parent->offsets[index];
1236 stride = parent->strides[index];
1237
1238 if (offset + height * stride > parent->bo->size) {
1239 _mesa_warning(NULL, "intel_create_sub_image: subimage out of bounds");
1240 return NULL;
1241 }
1242 }
1243
1244 image = intel_allocate_image(parent->screen, dri_format, loaderPrivate);
1245 if (image == NULL)
1246 return NULL;
1247
1248 image->bo = parent->bo;
1249 brw_bo_reference(parent->bo);
1250 image->modifier = parent->modifier;
1251
1252 image->width = width;
1253 image->height = height;
1254 image->pitch = stride;
1255 image->offset = offset;
1256
1257 intel_image_warn_if_unaligned(image, __func__);
1258
1259 return image;
1260 }
1261
1262 static const __DRIimageExtension intelImageExtension = {
1263 .base = { __DRI_IMAGE, 15 },
1264
1265 .createImageFromName = intel_create_image_from_name,
1266 .createImageFromRenderbuffer = intel_create_image_from_renderbuffer,
1267 .destroyImage = intel_destroy_image,
1268 .createImage = intel_create_image,
1269 .queryImage = intel_query_image,
1270 .dupImage = intel_dup_image,
1271 .validateUsage = intel_validate_usage,
1272 .createImageFromNames = intel_create_image_from_names,
1273 .fromPlanar = intel_from_planar,
1274 .createImageFromTexture = intel_create_image_from_texture,
1275 .createImageFromFds = intel_create_image_from_fds,
1276 .createImageFromDmaBufs = intel_create_image_from_dma_bufs,
1277 .blitImage = NULL,
1278 .getCapabilities = NULL,
1279 .mapImage = NULL,
1280 .unmapImage = NULL,
1281 .createImageWithModifiers = intel_create_image_with_modifiers,
1282 .createImageFromDmaBufs2 = intel_create_image_from_dma_bufs2,
1283 .queryDmaBufFormats = intel_query_dma_buf_formats,
1284 .queryDmaBufModifiers = intel_query_dma_buf_modifiers,
1285 };
1286
1287 static uint64_t
1288 get_aperture_size(int fd)
1289 {
1290 struct drm_i915_gem_get_aperture aperture;
1291
1292 if (drmIoctl(fd, DRM_IOCTL_I915_GEM_GET_APERTURE, &aperture) != 0)
1293 return 0;
1294
1295 return aperture.aper_size;
1296 }
1297
1298 static int
1299 brw_query_renderer_integer(__DRIscreen *dri_screen,
1300 int param, unsigned int *value)
1301 {
1302 const struct intel_screen *const screen =
1303 (struct intel_screen *) dri_screen->driverPrivate;
1304
1305 switch (param) {
1306 case __DRI2_RENDERER_VENDOR_ID:
1307 value[0] = 0x8086;
1308 return 0;
1309 case __DRI2_RENDERER_DEVICE_ID:
1310 value[0] = screen->deviceID;
1311 return 0;
1312 case __DRI2_RENDERER_ACCELERATED:
1313 value[0] = 1;
1314 return 0;
1315 case __DRI2_RENDERER_VIDEO_MEMORY: {
1316 /* Once a batch uses more than 75% of the maximum mappable size, we
1317 * assume that there's some fragmentation, and we start doing extra
1318 * flushing, etc. That's the big cliff apps will care about.
1319 */
1320 const unsigned gpu_mappable_megabytes =
1321 screen->aperture_threshold / (1024 * 1024);
1322
1323 const long system_memory_pages = sysconf(_SC_PHYS_PAGES);
1324 const long system_page_size = sysconf(_SC_PAGE_SIZE);
1325
1326 if (system_memory_pages <= 0 || system_page_size <= 0)
1327 return -1;
1328
1329 const uint64_t system_memory_bytes = (uint64_t) system_memory_pages
1330 * (uint64_t) system_page_size;
1331
1332 const unsigned system_memory_megabytes =
1333 (unsigned) (system_memory_bytes / (1024 * 1024));
1334
1335 value[0] = MIN2(system_memory_megabytes, gpu_mappable_megabytes);
1336 return 0;
1337 }
1338 case __DRI2_RENDERER_UNIFIED_MEMORY_ARCHITECTURE:
1339 value[0] = 1;
1340 return 0;
1341 case __DRI2_RENDERER_HAS_TEXTURE_3D:
1342 value[0] = 1;
1343 return 0;
1344 default:
1345 return driQueryRendererIntegerCommon(dri_screen, param, value);
1346 }
1347
1348 return -1;
1349 }
1350
1351 static int
1352 brw_query_renderer_string(__DRIscreen *dri_screen,
1353 int param, const char **value)
1354 {
1355 const struct intel_screen *screen =
1356 (struct intel_screen *) dri_screen->driverPrivate;
1357
1358 switch (param) {
1359 case __DRI2_RENDERER_VENDOR_ID:
1360 value[0] = brw_vendor_string;
1361 return 0;
1362 case __DRI2_RENDERER_DEVICE_ID:
1363 value[0] = brw_get_renderer_string(screen);
1364 return 0;
1365 default:
1366 break;
1367 }
1368
1369 return -1;
1370 }
1371
1372 static const __DRI2rendererQueryExtension intelRendererQueryExtension = {
1373 .base = { __DRI2_RENDERER_QUERY, 1 },
1374
1375 .queryInteger = brw_query_renderer_integer,
1376 .queryString = brw_query_renderer_string
1377 };
1378
1379 static const __DRIrobustnessExtension dri2Robustness = {
1380 .base = { __DRI2_ROBUSTNESS, 1 }
1381 };
1382
1383 static const __DRIextension *screenExtensions[] = {
1384 &intelTexBufferExtension.base,
1385 &intelFenceExtension.base,
1386 &intelFlushExtension.base,
1387 &intelImageExtension.base,
1388 &intelRendererQueryExtension.base,
1389 &dri2ConfigQueryExtension.base,
1390 &dri2NoErrorExtension.base,
1391 NULL
1392 };
1393
1394 static const __DRIextension *intelRobustScreenExtensions[] = {
1395 &intelTexBufferExtension.base,
1396 &intelFenceExtension.base,
1397 &intelFlushExtension.base,
1398 &intelImageExtension.base,
1399 &intelRendererQueryExtension.base,
1400 &dri2ConfigQueryExtension.base,
1401 &dri2Robustness.base,
1402 &dri2NoErrorExtension.base,
1403 NULL
1404 };
1405
1406 static int
1407 intel_get_param(struct intel_screen *screen, int param, int *value)
1408 {
1409 int ret = 0;
1410 struct drm_i915_getparam gp;
1411
1412 memset(&gp, 0, sizeof(gp));
1413 gp.param = param;
1414 gp.value = value;
1415
1416 if (drmIoctl(screen->driScrnPriv->fd, DRM_IOCTL_I915_GETPARAM, &gp) == -1) {
1417 ret = -errno;
1418 if (ret != -EINVAL)
1419 _mesa_warning(NULL, "drm_i915_getparam: %d", ret);
1420 }
1421
1422 return ret;
1423 }
1424
1425 static bool
1426 intel_get_boolean(struct intel_screen *screen, int param)
1427 {
1428 int value = 0;
1429 return (intel_get_param(screen, param, &value) == 0) && value;
1430 }
1431
1432 static int
1433 intel_get_integer(struct intel_screen *screen, int param)
1434 {
1435 int value = -1;
1436
1437 if (intel_get_param(screen, param, &value) == 0)
1438 return value;
1439
1440 return -1;
1441 }
1442
1443 static void
1444 intelDestroyScreen(__DRIscreen * sPriv)
1445 {
1446 struct intel_screen *screen = sPriv->driverPrivate;
1447
1448 brw_bufmgr_destroy(screen->bufmgr);
1449 driDestroyOptionInfo(&screen->optionCache);
1450
1451 ralloc_free(screen);
1452 sPriv->driverPrivate = NULL;
1453 }
1454
1455
1456 /**
1457 * Create a gl_framebuffer and attach it to __DRIdrawable::driverPrivate.
1458 *
1459 *_This implements driDriverAPI::createNewDrawable, which the DRI layer calls
1460 * when creating a EGLSurface, GLXDrawable, or GLXPixmap. Despite the name,
1461 * this does not allocate GPU memory.
1462 */
1463 static GLboolean
1464 intelCreateBuffer(__DRIscreen *dri_screen,
1465 __DRIdrawable * driDrawPriv,
1466 const struct gl_config * mesaVis, GLboolean isPixmap)
1467 {
1468 struct intel_renderbuffer *rb;
1469 struct intel_screen *screen = (struct intel_screen *)
1470 dri_screen->driverPrivate;
1471 mesa_format rgbFormat;
1472 unsigned num_samples =
1473 intel_quantize_num_samples(screen, mesaVis->samples);
1474
1475 if (isPixmap)
1476 return false;
1477
1478 struct gl_framebuffer *fb = CALLOC_STRUCT(gl_framebuffer);
1479 if (!fb)
1480 return false;
1481
1482 _mesa_initialize_window_framebuffer(fb, mesaVis);
1483
1484 if (screen->winsys_msaa_samples_override != -1) {
1485 num_samples = screen->winsys_msaa_samples_override;
1486 fb->Visual.samples = num_samples;
1487 }
1488
1489 if (mesaVis->redBits == 5) {
1490 rgbFormat = mesaVis->redMask == 0x1f ? MESA_FORMAT_R5G6B5_UNORM
1491 : MESA_FORMAT_B5G6R5_UNORM;
1492 } else if (mesaVis->sRGBCapable) {
1493 rgbFormat = mesaVis->redMask == 0xff ? MESA_FORMAT_R8G8B8A8_SRGB
1494 : MESA_FORMAT_B8G8R8A8_SRGB;
1495 } else if (mesaVis->alphaBits == 0) {
1496 rgbFormat = mesaVis->redMask == 0xff ? MESA_FORMAT_R8G8B8X8_UNORM
1497 : MESA_FORMAT_B8G8R8X8_UNORM;
1498 } else {
1499 rgbFormat = mesaVis->redMask == 0xff ? MESA_FORMAT_R8G8B8A8_SRGB
1500 : MESA_FORMAT_B8G8R8A8_SRGB;
1501 fb->Visual.sRGBCapable = true;
1502 }
1503
1504 /* setup the hardware-based renderbuffers */
1505 rb = intel_create_winsys_renderbuffer(screen, rgbFormat, num_samples);
1506 _mesa_attach_and_own_rb(fb, BUFFER_FRONT_LEFT, &rb->Base.Base);
1507
1508 if (mesaVis->doubleBufferMode) {
1509 rb = intel_create_winsys_renderbuffer(screen, rgbFormat, num_samples);
1510 _mesa_attach_and_own_rb(fb, BUFFER_BACK_LEFT, &rb->Base.Base);
1511 }
1512
1513 /*
1514 * Assert here that the gl_config has an expected depth/stencil bit
1515 * combination: one of d24/s8, d16/s0, d0/s0. (See intelInitScreen2(),
1516 * which constructs the advertised configs.)
1517 */
1518 if (mesaVis->depthBits == 24) {
1519 assert(mesaVis->stencilBits == 8);
1520
1521 if (screen->devinfo.has_hiz_and_separate_stencil) {
1522 rb = intel_create_private_renderbuffer(screen,
1523 MESA_FORMAT_Z24_UNORM_X8_UINT,
1524 num_samples);
1525 _mesa_attach_and_own_rb(fb, BUFFER_DEPTH, &rb->Base.Base);
1526 rb = intel_create_private_renderbuffer(screen, MESA_FORMAT_S_UINT8,
1527 num_samples);
1528 _mesa_attach_and_own_rb(fb, BUFFER_STENCIL, &rb->Base.Base);
1529 } else {
1530 /*
1531 * Use combined depth/stencil. Note that the renderbuffer is
1532 * attached to two attachment points.
1533 */
1534 rb = intel_create_private_renderbuffer(screen,
1535 MESA_FORMAT_Z24_UNORM_S8_UINT,
1536 num_samples);
1537 _mesa_attach_and_own_rb(fb, BUFFER_DEPTH, &rb->Base.Base);
1538 _mesa_attach_and_reference_rb(fb, BUFFER_STENCIL, &rb->Base.Base);
1539 }
1540 }
1541 else if (mesaVis->depthBits == 16) {
1542 assert(mesaVis->stencilBits == 0);
1543 rb = intel_create_private_renderbuffer(screen, MESA_FORMAT_Z_UNORM16,
1544 num_samples);
1545 _mesa_attach_and_own_rb(fb, BUFFER_DEPTH, &rb->Base.Base);
1546 }
1547 else {
1548 assert(mesaVis->depthBits == 0);
1549 assert(mesaVis->stencilBits == 0);
1550 }
1551
1552 /* now add any/all software-based renderbuffers we may need */
1553 _swrast_add_soft_renderbuffers(fb,
1554 false, /* never sw color */
1555 false, /* never sw depth */
1556 false, /* never sw stencil */
1557 mesaVis->accumRedBits > 0,
1558 false, /* never sw alpha */
1559 false /* never sw aux */ );
1560 driDrawPriv->driverPrivate = fb;
1561
1562 return true;
1563 }
1564
1565 static void
1566 intelDestroyBuffer(__DRIdrawable * driDrawPriv)
1567 {
1568 struct gl_framebuffer *fb = driDrawPriv->driverPrivate;
1569
1570 _mesa_reference_framebuffer(&fb, NULL);
1571 }
1572
1573 static void
1574 intel_detect_sseu(struct intel_screen *screen)
1575 {
1576 assert(screen->devinfo.gen >= 8);
1577 int ret;
1578
1579 screen->subslice_total = -1;
1580 screen->eu_total = -1;
1581
1582 ret = intel_get_param(screen, I915_PARAM_SUBSLICE_TOTAL,
1583 &screen->subslice_total);
1584 if (ret < 0 && ret != -EINVAL)
1585 goto err_out;
1586
1587 ret = intel_get_param(screen,
1588 I915_PARAM_EU_TOTAL, &screen->eu_total);
1589 if (ret < 0 && ret != -EINVAL)
1590 goto err_out;
1591
1592 /* Without this information, we cannot get the right Braswell brandstrings,
1593 * and we have to use conservative numbers for GPGPU on many platforms, but
1594 * otherwise, things will just work.
1595 */
1596 if (screen->subslice_total < 1 || screen->eu_total < 1)
1597 _mesa_warning(NULL,
1598 "Kernel 4.1 required to properly query GPU properties.\n");
1599
1600 return;
1601
1602 err_out:
1603 screen->subslice_total = -1;
1604 screen->eu_total = -1;
1605 _mesa_warning(NULL, "Failed to query GPU properties (%s).\n", strerror(-ret));
1606 }
1607
1608 static bool
1609 intel_init_bufmgr(struct intel_screen *screen)
1610 {
1611 __DRIscreen *dri_screen = screen->driScrnPriv;
1612
1613 if (getenv("INTEL_NO_HW") != NULL)
1614 screen->no_hw = true;
1615
1616 screen->bufmgr = brw_bufmgr_init(&screen->devinfo, dri_screen->fd, BATCH_SZ);
1617 if (screen->bufmgr == NULL) {
1618 fprintf(stderr, "[%s:%u] Error initializing buffer manager.\n",
1619 __func__, __LINE__);
1620 return false;
1621 }
1622
1623 if (!intel_get_boolean(screen, I915_PARAM_HAS_WAIT_TIMEOUT)) {
1624 fprintf(stderr, "[%s: %u] Kernel 3.6 required.\n", __func__, __LINE__);
1625 return false;
1626 }
1627
1628 return true;
1629 }
1630
1631 static bool
1632 intel_detect_swizzling(struct intel_screen *screen)
1633 {
1634 struct brw_bo *buffer;
1635 unsigned flags = 0;
1636 uint32_t aligned_pitch;
1637 uint32_t tiling = I915_TILING_X;
1638 uint32_t swizzle_mode = 0;
1639
1640 buffer = brw_bo_alloc_tiled_2d(screen->bufmgr, "swizzle test",
1641 64, 64, 4, tiling, &aligned_pitch, flags);
1642 if (buffer == NULL)
1643 return false;
1644
1645 brw_bo_get_tiling(buffer, &tiling, &swizzle_mode);
1646 brw_bo_unreference(buffer);
1647
1648 if (swizzle_mode == I915_BIT_6_SWIZZLE_NONE)
1649 return false;
1650 else
1651 return true;
1652 }
1653
1654 static int
1655 intel_detect_timestamp(struct intel_screen *screen)
1656 {
1657 uint64_t dummy = 0, last = 0;
1658 int upper, lower, loops;
1659
1660 /* On 64bit systems, some old kernels trigger a hw bug resulting in the
1661 * TIMESTAMP register being shifted and the low 32bits always zero.
1662 *
1663 * More recent kernels offer an interface to read the full 36bits
1664 * everywhere.
1665 */
1666 if (brw_reg_read(screen->bufmgr, TIMESTAMP | 1, &dummy) == 0)
1667 return 3;
1668
1669 /* Determine if we have a 32bit or 64bit kernel by inspecting the
1670 * upper 32bits for a rapidly changing timestamp.
1671 */
1672 if (brw_reg_read(screen->bufmgr, TIMESTAMP, &last))
1673 return 0;
1674
1675 upper = lower = 0;
1676 for (loops = 0; loops < 10; loops++) {
1677 /* The TIMESTAMP should change every 80ns, so several round trips
1678 * through the kernel should be enough to advance it.
1679 */
1680 if (brw_reg_read(screen->bufmgr, TIMESTAMP, &dummy))
1681 return 0;
1682
1683 upper += (dummy >> 32) != (last >> 32);
1684 if (upper > 1) /* beware 32bit counter overflow */
1685 return 2; /* upper dword holds the low 32bits of the timestamp */
1686
1687 lower += (dummy & 0xffffffff) != (last & 0xffffffff);
1688 if (lower > 1)
1689 return 1; /* timestamp is unshifted */
1690
1691 last = dummy;
1692 }
1693
1694 /* No advancement? No timestamp! */
1695 return 0;
1696 }
1697
1698 /**
1699 * Test if we can use MI_LOAD_REGISTER_MEM from an untrusted batchbuffer.
1700 *
1701 * Some combinations of hardware and kernel versions allow this feature,
1702 * while others don't. Instead of trying to enumerate every case, just
1703 * try and write a register and see if works.
1704 */
1705 static bool
1706 intel_detect_pipelined_register(struct intel_screen *screen,
1707 int reg, uint32_t expected_value, bool reset)
1708 {
1709 if (screen->no_hw)
1710 return false;
1711
1712 struct brw_bo *results, *bo;
1713 uint32_t *batch;
1714 uint32_t offset = 0;
1715 void *map;
1716 bool success = false;
1717
1718 /* Create a zero'ed temporary buffer for reading our results */
1719 results = brw_bo_alloc(screen->bufmgr, "registers", 4096, 0);
1720 if (results == NULL)
1721 goto err;
1722
1723 bo = brw_bo_alloc(screen->bufmgr, "batchbuffer", 4096, 0);
1724 if (bo == NULL)
1725 goto err_results;
1726
1727 map = brw_bo_map(NULL, bo, MAP_WRITE);
1728 if (!map)
1729 goto err_batch;
1730
1731 batch = map;
1732
1733 /* Write the register. */
1734 *batch++ = MI_LOAD_REGISTER_IMM | (3 - 2);
1735 *batch++ = reg;
1736 *batch++ = expected_value;
1737
1738 /* Save the register's value back to the buffer. */
1739 *batch++ = MI_STORE_REGISTER_MEM | (3 - 2);
1740 *batch++ = reg;
1741 struct drm_i915_gem_relocation_entry reloc = {
1742 .offset = (char *) batch - (char *) map,
1743 .delta = offset * sizeof(uint32_t),
1744 .target_handle = results->gem_handle,
1745 .read_domains = I915_GEM_DOMAIN_INSTRUCTION,
1746 .write_domain = I915_GEM_DOMAIN_INSTRUCTION,
1747 };
1748 *batch++ = reloc.presumed_offset + reloc.delta;
1749
1750 /* And afterwards clear the register */
1751 if (reset) {
1752 *batch++ = MI_LOAD_REGISTER_IMM | (3 - 2);
1753 *batch++ = reg;
1754 *batch++ = 0;
1755 }
1756
1757 *batch++ = MI_BATCH_BUFFER_END;
1758
1759 struct drm_i915_gem_exec_object2 exec_objects[2] = {
1760 {
1761 .handle = results->gem_handle,
1762 },
1763 {
1764 .handle = bo->gem_handle,
1765 .relocation_count = 1,
1766 .relocs_ptr = (uintptr_t) &reloc,
1767 }
1768 };
1769
1770 struct drm_i915_gem_execbuffer2 execbuf = {
1771 .buffers_ptr = (uintptr_t) exec_objects,
1772 .buffer_count = 2,
1773 .batch_len = ALIGN((char *) batch - (char *) map, 8),
1774 .flags = I915_EXEC_RENDER,
1775 };
1776
1777 /* Don't bother with error checking - if the execbuf fails, the
1778 * value won't be written and we'll just report that there's no access.
1779 */
1780 __DRIscreen *dri_screen = screen->driScrnPriv;
1781 drmIoctl(dri_screen->fd, DRM_IOCTL_I915_GEM_EXECBUFFER2, &execbuf);
1782
1783 /* Check whether the value got written. */
1784 void *results_map = brw_bo_map(NULL, results, MAP_READ);
1785 if (results_map) {
1786 success = *((uint32_t *)results_map + offset) == expected_value;
1787 brw_bo_unmap(results);
1788 }
1789
1790 err_batch:
1791 brw_bo_unreference(bo);
1792 err_results:
1793 brw_bo_unreference(results);
1794 err:
1795 return success;
1796 }
1797
1798 static bool
1799 intel_detect_pipelined_so(struct intel_screen *screen)
1800 {
1801 const struct gen_device_info *devinfo = &screen->devinfo;
1802
1803 /* Supposedly, Broadwell just works. */
1804 if (devinfo->gen >= 8)
1805 return true;
1806
1807 if (devinfo->gen <= 6)
1808 return false;
1809
1810 /* See the big explanation about command parser versions below */
1811 if (screen->cmd_parser_version >= (devinfo->is_haswell ? 7 : 2))
1812 return true;
1813
1814 /* We use SO_WRITE_OFFSET0 since you're supposed to write it (unlike the
1815 * statistics registers), and we already reset it to zero before using it.
1816 */
1817 return intel_detect_pipelined_register(screen,
1818 GEN7_SO_WRITE_OFFSET(0),
1819 0x1337d0d0,
1820 false);
1821 }
1822
1823 /**
1824 * Return array of MSAA modes supported by the hardware. The array is
1825 * zero-terminated and sorted in decreasing order.
1826 */
1827 const int*
1828 intel_supported_msaa_modes(const struct intel_screen *screen)
1829 {
1830 static const int gen9_modes[] = {16, 8, 4, 2, 0, -1};
1831 static const int gen8_modes[] = {8, 4, 2, 0, -1};
1832 static const int gen7_modes[] = {8, 4, 0, -1};
1833 static const int gen6_modes[] = {4, 0, -1};
1834 static const int gen4_modes[] = {0, -1};
1835
1836 if (screen->devinfo.gen >= 9) {
1837 return gen9_modes;
1838 } else if (screen->devinfo.gen >= 8) {
1839 return gen8_modes;
1840 } else if (screen->devinfo.gen >= 7) {
1841 return gen7_modes;
1842 } else if (screen->devinfo.gen == 6) {
1843 return gen6_modes;
1844 } else {
1845 return gen4_modes;
1846 }
1847 }
1848
1849 static __DRIconfig**
1850 intel_screen_make_configs(__DRIscreen *dri_screen)
1851 {
1852 static const mesa_format formats[] = {
1853 MESA_FORMAT_B5G6R5_UNORM,
1854 MESA_FORMAT_B8G8R8A8_UNORM,
1855 MESA_FORMAT_B8G8R8X8_UNORM,
1856
1857 /* The 32-bit RGBA format must not precede the 32-bit BGRA format.
1858 * Likewise for RGBX and BGRX. Otherwise, the GLX client and the GLX
1859 * server may disagree on which format the GLXFBConfig represents,
1860 * resulting in swapped color channels.
1861 *
1862 * The problem, as of 2017-05-30:
1863 * When matching a GLXFBConfig to a __DRIconfig, GLX ignores the channel
1864 * order and chooses the first __DRIconfig with the expected channel
1865 * sizes. Specifically, GLX compares the GLXFBConfig's and __DRIconfig's
1866 * __DRI_ATTRIB_{CHANNEL}_SIZE but ignores __DRI_ATTRIB_{CHANNEL}_MASK.
1867 *
1868 * EGL does not suffer from this problem. It correctly compares the
1869 * channel masks when matching EGLConfig to __DRIconfig.
1870 */
1871
1872 /* Required by Android, for HAL_PIXEL_FORMAT_RGBA_8888. */
1873 MESA_FORMAT_R8G8B8A8_UNORM,
1874
1875 /* Required by Android, for HAL_PIXEL_FORMAT_RGBX_8888. */
1876 MESA_FORMAT_R8G8B8X8_UNORM,
1877 };
1878
1879 /* GLX_SWAP_COPY_OML is not supported due to page flipping. */
1880 static const GLenum back_buffer_modes[] = {
1881 __DRI_ATTRIB_SWAP_UNDEFINED, __DRI_ATTRIB_SWAP_NONE
1882 };
1883
1884 static const uint8_t singlesample_samples[1] = {0};
1885 static const uint8_t multisample_samples[2] = {4, 8};
1886
1887 struct intel_screen *screen = dri_screen->driverPrivate;
1888 const struct gen_device_info *devinfo = &screen->devinfo;
1889 uint8_t depth_bits[4], stencil_bits[4];
1890 __DRIconfig **configs = NULL;
1891
1892 /* Generate singlesample configs without accumulation buffer. */
1893 for (unsigned i = 0; i < ARRAY_SIZE(formats); i++) {
1894 __DRIconfig **new_configs;
1895 int num_depth_stencil_bits = 2;
1896
1897 /* Starting with DRI2 protocol version 1.1 we can request a depth/stencil
1898 * buffer that has a different number of bits per pixel than the color
1899 * buffer, gen >= 6 supports this.
1900 */
1901 depth_bits[0] = 0;
1902 stencil_bits[0] = 0;
1903
1904 if (formats[i] == MESA_FORMAT_B5G6R5_UNORM) {
1905 depth_bits[1] = 16;
1906 stencil_bits[1] = 0;
1907 if (devinfo->gen >= 6) {
1908 depth_bits[2] = 24;
1909 stencil_bits[2] = 8;
1910 num_depth_stencil_bits = 3;
1911 }
1912 } else {
1913 depth_bits[1] = 24;
1914 stencil_bits[1] = 8;
1915 }
1916
1917 new_configs = driCreateConfigs(formats[i],
1918 depth_bits,
1919 stencil_bits,
1920 num_depth_stencil_bits,
1921 back_buffer_modes, 2,
1922 singlesample_samples, 1,
1923 false, false);
1924 configs = driConcatConfigs(configs, new_configs);
1925 }
1926
1927 /* Generate the minimum possible set of configs that include an
1928 * accumulation buffer.
1929 */
1930 for (unsigned i = 0; i < ARRAY_SIZE(formats); i++) {
1931 __DRIconfig **new_configs;
1932
1933 if (formats[i] == MESA_FORMAT_B5G6R5_UNORM) {
1934 depth_bits[0] = 16;
1935 stencil_bits[0] = 0;
1936 } else {
1937 depth_bits[0] = 24;
1938 stencil_bits[0] = 8;
1939 }
1940
1941 new_configs = driCreateConfigs(formats[i],
1942 depth_bits, stencil_bits, 1,
1943 back_buffer_modes, 1,
1944 singlesample_samples, 1,
1945 true, false);
1946 configs = driConcatConfigs(configs, new_configs);
1947 }
1948
1949 /* Generate multisample configs.
1950 *
1951 * This loop breaks early, and hence is a no-op, on gen < 6.
1952 *
1953 * Multisample configs must follow the singlesample configs in order to
1954 * work around an X server bug present in 1.12. The X server chooses to
1955 * associate the first listed RGBA888-Z24S8 config, regardless of its
1956 * sample count, with the 32-bit depth visual used for compositing.
1957 *
1958 * Only doublebuffer configs with GLX_SWAP_UNDEFINED_OML behavior are
1959 * supported. Singlebuffer configs are not supported because no one wants
1960 * them.
1961 */
1962 for (unsigned i = 0; i < ARRAY_SIZE(formats); i++) {
1963 if (devinfo->gen < 6)
1964 break;
1965
1966 __DRIconfig **new_configs;
1967 const int num_depth_stencil_bits = 2;
1968 int num_msaa_modes = 0;
1969
1970 depth_bits[0] = 0;
1971 stencil_bits[0] = 0;
1972
1973 if (formats[i] == MESA_FORMAT_B5G6R5_UNORM) {
1974 depth_bits[1] = 16;
1975 stencil_bits[1] = 0;
1976 } else {
1977 depth_bits[1] = 24;
1978 stencil_bits[1] = 8;
1979 }
1980
1981 if (devinfo->gen >= 7)
1982 num_msaa_modes = 2;
1983 else if (devinfo->gen == 6)
1984 num_msaa_modes = 1;
1985
1986 new_configs = driCreateConfigs(formats[i],
1987 depth_bits,
1988 stencil_bits,
1989 num_depth_stencil_bits,
1990 back_buffer_modes, 1,
1991 multisample_samples,
1992 num_msaa_modes,
1993 false, false);
1994 configs = driConcatConfigs(configs, new_configs);
1995 }
1996
1997 if (configs == NULL) {
1998 fprintf(stderr, "[%s:%u] Error creating FBConfig!\n", __func__,
1999 __LINE__);
2000 return NULL;
2001 }
2002
2003 return configs;
2004 }
2005
2006 static void
2007 set_max_gl_versions(struct intel_screen *screen)
2008 {
2009 __DRIscreen *dri_screen = screen->driScrnPriv;
2010 const bool has_astc = screen->devinfo.gen >= 9;
2011
2012 switch (screen->devinfo.gen) {
2013 case 10:
2014 case 9:
2015 case 8:
2016 dri_screen->max_gl_core_version = 45;
2017 dri_screen->max_gl_compat_version = 30;
2018 dri_screen->max_gl_es1_version = 11;
2019 dri_screen->max_gl_es2_version = has_astc ? 32 : 31;
2020 break;
2021 case 7:
2022 dri_screen->max_gl_core_version = 33;
2023 if (can_do_pipelined_register_writes(screen)) {
2024 dri_screen->max_gl_core_version = 42;
2025 if (screen->devinfo.is_haswell && can_do_compute_dispatch(screen))
2026 dri_screen->max_gl_core_version = 43;
2027 if (screen->devinfo.is_haswell && can_do_mi_math_and_lrr(screen))
2028 dri_screen->max_gl_core_version = 45;
2029 }
2030 dri_screen->max_gl_compat_version = 30;
2031 dri_screen->max_gl_es1_version = 11;
2032 dri_screen->max_gl_es2_version = screen->devinfo.is_haswell ? 31 : 30;
2033 break;
2034 case 6:
2035 dri_screen->max_gl_core_version = 33;
2036 dri_screen->max_gl_compat_version = 30;
2037 dri_screen->max_gl_es1_version = 11;
2038 dri_screen->max_gl_es2_version = 30;
2039 break;
2040 case 5:
2041 case 4:
2042 dri_screen->max_gl_core_version = 0;
2043 dri_screen->max_gl_compat_version = 21;
2044 dri_screen->max_gl_es1_version = 11;
2045 dri_screen->max_gl_es2_version = 20;
2046 break;
2047 default:
2048 unreachable("unrecognized intel_screen::gen");
2049 }
2050 }
2051
2052 /**
2053 * Return the revision (generally the revid field of the PCI header) of the
2054 * graphics device.
2055 *
2056 * XXX: This function is useful to keep around even if it is not currently in
2057 * use. It is necessary for new platforms and revision specific workarounds or
2058 * features. Please don't remove it so that we know it at least continues to
2059 * build.
2060 */
2061 static __attribute__((__unused__)) int
2062 brw_get_revision(int fd)
2063 {
2064 struct drm_i915_getparam gp;
2065 int revision;
2066 int ret;
2067
2068 memset(&gp, 0, sizeof(gp));
2069 gp.param = I915_PARAM_REVISION;
2070 gp.value = &revision;
2071
2072 ret = drmCommandWriteRead(fd, DRM_I915_GETPARAM, &gp, sizeof(gp));
2073 if (ret)
2074 revision = -1;
2075
2076 return revision;
2077 }
2078
2079 static void
2080 shader_debug_log_mesa(void *data, const char *fmt, ...)
2081 {
2082 struct brw_context *brw = (struct brw_context *)data;
2083 va_list args;
2084
2085 va_start(args, fmt);
2086 GLuint msg_id = 0;
2087 _mesa_gl_vdebug(&brw->ctx, &msg_id,
2088 MESA_DEBUG_SOURCE_SHADER_COMPILER,
2089 MESA_DEBUG_TYPE_OTHER,
2090 MESA_DEBUG_SEVERITY_NOTIFICATION, fmt, args);
2091 va_end(args);
2092 }
2093
2094 static void
2095 shader_perf_log_mesa(void *data, const char *fmt, ...)
2096 {
2097 struct brw_context *brw = (struct brw_context *)data;
2098
2099 va_list args;
2100 va_start(args, fmt);
2101
2102 if (unlikely(INTEL_DEBUG & DEBUG_PERF)) {
2103 va_list args_copy;
2104 va_copy(args_copy, args);
2105 vfprintf(stderr, fmt, args_copy);
2106 va_end(args_copy);
2107 }
2108
2109 if (brw->perf_debug) {
2110 GLuint msg_id = 0;
2111 _mesa_gl_vdebug(&brw->ctx, &msg_id,
2112 MESA_DEBUG_SOURCE_SHADER_COMPILER,
2113 MESA_DEBUG_TYPE_PERFORMANCE,
2114 MESA_DEBUG_SEVERITY_MEDIUM, fmt, args);
2115 }
2116 va_end(args);
2117 }
2118
2119 static int
2120 parse_devid_override(const char *devid_override)
2121 {
2122 static const struct {
2123 const char *name;
2124 int pci_id;
2125 } name_map[] = {
2126 { "brw", 0x2a02 },
2127 { "g4x", 0x2a42 },
2128 { "ilk", 0x0042 },
2129 { "snb", 0x0126 },
2130 { "ivb", 0x016a },
2131 { "hsw", 0x0d2e },
2132 { "byt", 0x0f33 },
2133 { "bdw", 0x162e },
2134 { "skl", 0x1912 },
2135 { "kbl", 0x5912 },
2136 { "cnl", 0x5a52 },
2137 };
2138
2139 for (unsigned i = 0; i < ARRAY_SIZE(name_map); i++) {
2140 if (!strcmp(name_map[i].name, devid_override))
2141 return name_map[i].pci_id;
2142 }
2143
2144 return strtol(devid_override, NULL, 0);
2145 }
2146
2147 /**
2148 * Get the PCI ID for the device. This can be overridden by setting the
2149 * INTEL_DEVID_OVERRIDE environment variable to the desired ID.
2150 *
2151 * Returns -1 on ioctl failure.
2152 */
2153 static int
2154 get_pci_device_id(struct intel_screen *screen)
2155 {
2156 if (geteuid() == getuid()) {
2157 char *devid_override = getenv("INTEL_DEVID_OVERRIDE");
2158 if (devid_override) {
2159 screen->no_hw = true;
2160 return parse_devid_override(devid_override);
2161 }
2162 }
2163
2164 return intel_get_integer(screen, I915_PARAM_CHIPSET_ID);
2165 }
2166
2167 /**
2168 * This is the driver specific part of the createNewScreen entry point.
2169 * Called when using DRI2.
2170 *
2171 * \return the struct gl_config supported by this driver
2172 */
2173 static const
2174 __DRIconfig **intelInitScreen2(__DRIscreen *dri_screen)
2175 {
2176 struct intel_screen *screen;
2177
2178 if (dri_screen->image.loader) {
2179 } else if (dri_screen->dri2.loader->base.version <= 2 ||
2180 dri_screen->dri2.loader->getBuffersWithFormat == NULL) {
2181 fprintf(stderr,
2182 "\nERROR! DRI2 loader with getBuffersWithFormat() "
2183 "support required\n");
2184 return NULL;
2185 }
2186
2187 /* Allocate the private area */
2188 screen = rzalloc(NULL, struct intel_screen);
2189 if (!screen) {
2190 fprintf(stderr, "\nERROR! Allocating private area failed\n");
2191 return NULL;
2192 }
2193 /* parse information in __driConfigOptions */
2194 driParseOptionInfo(&screen->optionCache, brw_config_options.xml);
2195
2196 screen->driScrnPriv = dri_screen;
2197 dri_screen->driverPrivate = (void *) screen;
2198
2199 screen->deviceID = get_pci_device_id(screen);
2200
2201 if (!gen_get_device_info(screen->deviceID, &screen->devinfo))
2202 return NULL;
2203
2204 if (!intel_init_bufmgr(screen))
2205 return NULL;
2206
2207 const struct gen_device_info *devinfo = &screen->devinfo;
2208
2209 brw_process_intel_debug_variable();
2210
2211 if ((INTEL_DEBUG & DEBUG_SHADER_TIME) && devinfo->gen < 7) {
2212 fprintf(stderr,
2213 "shader_time debugging requires gen7 (Ivybridge) or better.\n");
2214 INTEL_DEBUG &= ~DEBUG_SHADER_TIME;
2215 }
2216
2217 if (intel_get_integer(screen, I915_PARAM_MMAP_GTT_VERSION) >= 1) {
2218 /* Theorectically unlimited! At least for individual objects...
2219 *
2220 * Currently the entire (global) address space for all GTT maps is
2221 * limited to 64bits. That is all objects on the system that are
2222 * setup for GTT mmapping must fit within 64bits. An attempt to use
2223 * one that exceeds the limit with fail in brw_bo_map_gtt().
2224 *
2225 * Long before we hit that limit, we will be practically limited by
2226 * that any single object must fit in physical memory (RAM). The upper
2227 * limit on the CPU's address space is currently 48bits (Skylake), of
2228 * which only 39bits can be physical memory. (The GPU itself also has
2229 * a 48bit addressable virtual space.) We can fit over 32 million
2230 * objects of the current maximum allocable size before running out
2231 * of mmap space.
2232 */
2233 screen->max_gtt_map_object_size = UINT64_MAX;
2234 } else {
2235 /* Estimate the size of the mappable aperture into the GTT. There's an
2236 * ioctl to get the whole GTT size, but not one to get the mappable subset.
2237 * It turns out it's basically always 256MB, though some ancient hardware
2238 * was smaller.
2239 */
2240 uint32_t gtt_size = 256 * 1024 * 1024;
2241
2242 /* We don't want to map two objects such that a memcpy between them would
2243 * just fault one mapping in and then the other over and over forever. So
2244 * we would need to divide the GTT size by 2. Additionally, some GTT is
2245 * taken up by things like the framebuffer and the ringbuffer and such, so
2246 * be more conservative.
2247 */
2248 screen->max_gtt_map_object_size = gtt_size / 4;
2249 }
2250
2251 screen->aperture_threshold = get_aperture_size(dri_screen->fd) * 3 / 4;
2252
2253 screen->hw_has_swizzling = intel_detect_swizzling(screen);
2254 screen->hw_has_timestamp = intel_detect_timestamp(screen);
2255
2256 isl_device_init(&screen->isl_dev, &screen->devinfo,
2257 screen->hw_has_swizzling);
2258
2259 /* GENs prior to 8 do not support EU/Subslice info */
2260 if (devinfo->gen >= 8) {
2261 intel_detect_sseu(screen);
2262 } else if (devinfo->gen == 7) {
2263 screen->subslice_total = 1 << (devinfo->gt - 1);
2264 }
2265
2266 /* Gen7-7.5 kernel requirements / command parser saga:
2267 *
2268 * - pre-v3.16:
2269 * Haswell and Baytrail cannot use any privileged batchbuffer features.
2270 *
2271 * Ivybridge has aliasing PPGTT on by default, which accidentally marks
2272 * all batches secure, allowing them to use any feature with no checking.
2273 * This is effectively equivalent to a command parser version of
2274 * \infinity - everything is possible.
2275 *
2276 * The command parser does not exist, and querying the version will
2277 * return -EINVAL.
2278 *
2279 * - v3.16:
2280 * The kernel enables the command parser by default, for systems with
2281 * aliasing PPGTT enabled (Ivybridge and Haswell). However, the
2282 * hardware checker is still enabled, so Haswell and Baytrail cannot
2283 * do anything.
2284 *
2285 * Ivybridge goes from "everything is possible" to "only what the
2286 * command parser allows" (if the user boots with i915.cmd_parser=0,
2287 * then everything is possible again). We can only safely use features
2288 * allowed by the supported command parser version.
2289 *
2290 * Annoyingly, I915_PARAM_CMD_PARSER_VERSION reports the static version
2291 * implemented by the kernel, even if it's turned off. So, checking
2292 * for version > 0 does not mean that you can write registers. We have
2293 * to try it and see. The version does, however, indicate the age of
2294 * the kernel.
2295 *
2296 * Instead of matching the hardware checker's behavior of converting
2297 * privileged commands to MI_NOOP, it makes execbuf2 start returning
2298 * -EINVAL, making it dangerous to try and use privileged features.
2299 *
2300 * Effective command parser versions:
2301 * - Haswell: 0 (reporting 1, writes don't work)
2302 * - Baytrail: 0 (reporting 1, writes don't work)
2303 * - Ivybridge: 1 (enabled) or infinite (disabled)
2304 *
2305 * - v3.17:
2306 * Baytrail aliasing PPGTT is enabled, making it like Ivybridge:
2307 * effectively version 1 (enabled) or infinite (disabled).
2308 *
2309 * - v3.19: f1f55cc0556031c8ee3fe99dae7251e78b9b653b
2310 * Command parser v2 supports predicate writes.
2311 *
2312 * - Haswell: 0 (reporting 1, writes don't work)
2313 * - Baytrail: 2 (enabled) or infinite (disabled)
2314 * - Ivybridge: 2 (enabled) or infinite (disabled)
2315 *
2316 * So version >= 2 is enough to know that Ivybridge and Baytrail
2317 * will work. Haswell still can't do anything.
2318 *
2319 * - v4.0: Version 3 happened. Largely not relevant.
2320 *
2321 * - v4.1: 6702cf16e0ba8b0129f5aa1b6609d4e9c70bc13b
2322 * L3 config registers are properly saved and restored as part
2323 * of the hardware context. We can approximately detect this point
2324 * in time by checking if I915_PARAM_REVISION is recognized - it
2325 * landed in a later commit, but in the same release cycle.
2326 *
2327 * - v4.2: 245054a1fe33c06ad233e0d58a27ec7b64db9284
2328 * Command parser finally gains secure batch promotion. On Haswell,
2329 * the hardware checker gets disabled, which finally allows it to do
2330 * privileged commands.
2331 *
2332 * I915_PARAM_CMD_PARSER_VERSION reports 3. Effective versions:
2333 * - Haswell: 3 (enabled) or 0 (disabled)
2334 * - Baytrail: 3 (enabled) or infinite (disabled)
2335 * - Ivybridge: 3 (enabled) or infinite (disabled)
2336 *
2337 * Unfortunately, detecting this point in time is tricky, because
2338 * no version bump happened when this important change occurred.
2339 * On Haswell, if we can write any register, then the kernel is at
2340 * least this new, and we can start trusting the version number.
2341 *
2342 * - v4.4: 2bbe6bbb0dc94fd4ce287bdac9e1bd184e23057b and
2343 * Command parser reaches version 4, allowing access to Haswell
2344 * atomic scratch and chicken3 registers. If version >= 4, we know
2345 * the kernel is new enough to support privileged features on all
2346 * hardware. However, the user might have disabled it...and the
2347 * kernel will still report version 4. So we still have to guess
2348 * and check.
2349 *
2350 * - v4.4: 7b9748cb513a6bef4af87b79f0da3ff7e8b56cd8
2351 * Command parser v5 whitelists indirect compute shader dispatch
2352 * registers, needed for OpenGL 4.3 and later.
2353 *
2354 * - v4.8:
2355 * Command parser v7 lets us use MI_MATH on Haswell.
2356 *
2357 * Additionally, the kernel begins reporting version 0 when
2358 * the command parser is disabled, allowing us to skip the
2359 * guess-and-check step on Haswell. Unfortunately, this also
2360 * means that we can no longer use it as an indicator of the
2361 * age of the kernel.
2362 */
2363 if (intel_get_param(screen, I915_PARAM_CMD_PARSER_VERSION,
2364 &screen->cmd_parser_version) < 0) {
2365 /* Command parser does not exist - getparam is unrecognized */
2366 screen->cmd_parser_version = 0;
2367 }
2368
2369 /* Kernel 4.13 retuired for exec object capture */
2370 if (intel_get_boolean(screen, I915_PARAM_HAS_EXEC_CAPTURE)) {
2371 screen->kernel_features |= KERNEL_ALLOWS_EXEC_CAPTURE;
2372 }
2373
2374 if (intel_get_boolean(screen, I915_PARAM_HAS_EXEC_BATCH_FIRST)) {
2375 screen->kernel_features |= KERNEL_ALLOWS_EXEC_BATCH_FIRST;
2376 }
2377
2378 if (!intel_detect_pipelined_so(screen)) {
2379 /* We can't do anything, so the effective version is 0. */
2380 screen->cmd_parser_version = 0;
2381 } else {
2382 screen->kernel_features |= KERNEL_ALLOWS_SOL_OFFSET_WRITES;
2383 }
2384
2385 if (devinfo->gen >= 8 || screen->cmd_parser_version >= 2)
2386 screen->kernel_features |= KERNEL_ALLOWS_PREDICATE_WRITES;
2387
2388 /* Haswell requires command parser version 4 in order to have L3
2389 * atomic scratch1 and chicken3 bits
2390 */
2391 if (devinfo->is_haswell && screen->cmd_parser_version >= 4) {
2392 screen->kernel_features |=
2393 KERNEL_ALLOWS_HSW_SCRATCH1_AND_ROW_CHICKEN3;
2394 }
2395
2396 /* Haswell requires command parser version 6 in order to write to the
2397 * MI_MATH GPR registers, and version 7 in order to use
2398 * MI_LOAD_REGISTER_REG (which all users of MI_MATH use).
2399 */
2400 if (devinfo->gen >= 8 ||
2401 (devinfo->is_haswell && screen->cmd_parser_version >= 7)) {
2402 screen->kernel_features |= KERNEL_ALLOWS_MI_MATH_AND_LRR;
2403 }
2404
2405 /* Gen7 needs at least command parser version 5 to support compute */
2406 if (devinfo->gen >= 8 || screen->cmd_parser_version >= 5)
2407 screen->kernel_features |= KERNEL_ALLOWS_COMPUTE_DISPATCH;
2408
2409 const char *force_msaa = getenv("INTEL_FORCE_MSAA");
2410 if (force_msaa) {
2411 screen->winsys_msaa_samples_override =
2412 intel_quantize_num_samples(screen, atoi(force_msaa));
2413 printf("Forcing winsys sample count to %d\n",
2414 screen->winsys_msaa_samples_override);
2415 } else {
2416 screen->winsys_msaa_samples_override = -1;
2417 }
2418
2419 set_max_gl_versions(screen);
2420
2421 /* Notification of GPU resets requires hardware contexts and a kernel new
2422 * enough to support DRM_IOCTL_I915_GET_RESET_STATS. If the ioctl is
2423 * supported, calling it with a context of 0 will either generate EPERM or
2424 * no error. If the ioctl is not supported, it always generate EINVAL.
2425 * Use this to determine whether to advertise the __DRI2_ROBUSTNESS
2426 * extension to the loader.
2427 *
2428 * Don't even try on pre-Gen6, since we don't attempt to use contexts there.
2429 */
2430 if (devinfo->gen >= 6) {
2431 struct drm_i915_reset_stats stats;
2432 memset(&stats, 0, sizeof(stats));
2433
2434 const int ret = drmIoctl(dri_screen->fd, DRM_IOCTL_I915_GET_RESET_STATS, &stats);
2435
2436 screen->has_context_reset_notification =
2437 (ret != -1 || errno != EINVAL);
2438 }
2439
2440 dri_screen->extensions = !screen->has_context_reset_notification
2441 ? screenExtensions : intelRobustScreenExtensions;
2442
2443 screen->compiler = brw_compiler_create(screen, devinfo);
2444 screen->compiler->shader_debug_log = shader_debug_log_mesa;
2445 screen->compiler->shader_perf_log = shader_perf_log_mesa;
2446 screen->compiler->constant_buffer_0_is_relative = devinfo->gen < 8;
2447 screen->program_id = 1;
2448
2449 screen->has_exec_fence =
2450 intel_get_boolean(screen, I915_PARAM_HAS_EXEC_FENCE);
2451
2452 intel_screen_init_surface_formats(screen);
2453
2454 return (const __DRIconfig**) intel_screen_make_configs(dri_screen);
2455 }
2456
2457 struct intel_buffer {
2458 __DRIbuffer base;
2459 struct brw_bo *bo;
2460 };
2461
2462 static __DRIbuffer *
2463 intelAllocateBuffer(__DRIscreen *dri_screen,
2464 unsigned attachment, unsigned format,
2465 int width, int height)
2466 {
2467 struct intel_buffer *intelBuffer;
2468 struct intel_screen *screen = dri_screen->driverPrivate;
2469
2470 assert(attachment == __DRI_BUFFER_FRONT_LEFT ||
2471 attachment == __DRI_BUFFER_BACK_LEFT);
2472
2473 intelBuffer = calloc(1, sizeof *intelBuffer);
2474 if (intelBuffer == NULL)
2475 return NULL;
2476
2477 /* The front and back buffers are color buffers, which are X tiled. GEN9+
2478 * supports Y tiled and compressed buffers, but there is no way to plumb that
2479 * through to here. */
2480 uint32_t pitch;
2481 int cpp = format / 8;
2482 intelBuffer->bo = brw_bo_alloc_tiled_2d(screen->bufmgr,
2483 "intelAllocateBuffer",
2484 width,
2485 height,
2486 cpp,
2487 I915_TILING_X, &pitch,
2488 BO_ALLOC_BUSY);
2489
2490 if (intelBuffer->bo == NULL) {
2491 free(intelBuffer);
2492 return NULL;
2493 }
2494
2495 brw_bo_flink(intelBuffer->bo, &intelBuffer->base.name);
2496
2497 intelBuffer->base.attachment = attachment;
2498 intelBuffer->base.cpp = cpp;
2499 intelBuffer->base.pitch = pitch;
2500
2501 return &intelBuffer->base;
2502 }
2503
2504 static void
2505 intelReleaseBuffer(__DRIscreen *dri_screen, __DRIbuffer *buffer)
2506 {
2507 struct intel_buffer *intelBuffer = (struct intel_buffer *) buffer;
2508
2509 brw_bo_unreference(intelBuffer->bo);
2510 free(intelBuffer);
2511 }
2512
2513 static const struct __DriverAPIRec brw_driver_api = {
2514 .InitScreen = intelInitScreen2,
2515 .DestroyScreen = intelDestroyScreen,
2516 .CreateContext = brwCreateContext,
2517 .DestroyContext = intelDestroyContext,
2518 .CreateBuffer = intelCreateBuffer,
2519 .DestroyBuffer = intelDestroyBuffer,
2520 .MakeCurrent = intelMakeCurrent,
2521 .UnbindContext = intelUnbindContext,
2522 .AllocateBuffer = intelAllocateBuffer,
2523 .ReleaseBuffer = intelReleaseBuffer
2524 };
2525
2526 static const struct __DRIDriverVtableExtensionRec brw_vtable = {
2527 .base = { __DRI_DRIVER_VTABLE, 1 },
2528 .vtable = &brw_driver_api,
2529 };
2530
2531 static const __DRIextension *brw_driver_extensions[] = {
2532 &driCoreExtension.base,
2533 &driImageDriverExtension.base,
2534 &driDRI2Extension.base,
2535 &brw_vtable.base,
2536 &brw_config_options.base,
2537 NULL
2538 };
2539
2540 PUBLIC const __DRIextension **__driDriverGetExtensions_i965(void)
2541 {
2542 globalDriverAPI = &brw_driver_api;
2543
2544 return brw_driver_extensions;
2545 }