dri: allow 16bit R/GR images to be exported via drm buffers
[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 <errno.h>
27 #include <time.h>
28 #include <unistd.h>
29 #include "main/context.h"
30 #include "main/framebuffer.h"
31 #include "main/renderbuffer.h"
32 #include "main/texobj.h"
33 #include "main/hash.h"
34 #include "main/fbobject.h"
35 #include "main/version.h"
36 #include "swrast/s_renderbuffer.h"
37 #include "util/ralloc.h"
38 #include "brw_shader.h"
39 #include "compiler/nir/nir.h"
40
41 #include "utils.h"
42 #include "xmlpool.h"
43
44 static const __DRIconfigOptionsExtension brw_config_options = {
45 .base = { __DRI_CONFIG_OPTIONS, 1 },
46 .xml =
47 DRI_CONF_BEGIN
48 DRI_CONF_SECTION_PERFORMANCE
49 DRI_CONF_VBLANK_MODE(DRI_CONF_VBLANK_ALWAYS_SYNC)
50 /* Options correspond to DRI_CONF_BO_REUSE_DISABLED,
51 * DRI_CONF_BO_REUSE_ALL
52 */
53 DRI_CONF_OPT_BEGIN_V(bo_reuse, enum, 1, "0:1")
54 DRI_CONF_DESC_BEGIN(en, "Buffer object reuse")
55 DRI_CONF_ENUM(0, "Disable buffer object reuse")
56 DRI_CONF_ENUM(1, "Enable reuse of all sizes of buffer objects")
57 DRI_CONF_DESC_END
58 DRI_CONF_OPT_END
59
60 DRI_CONF_OPT_BEGIN_B(hiz, "true")
61 DRI_CONF_DESC(en, "Enable Hierarchical Z on gen6+")
62 DRI_CONF_OPT_END
63 DRI_CONF_SECTION_END
64
65 DRI_CONF_SECTION_QUALITY
66 DRI_CONF_FORCE_S3TC_ENABLE("false")
67
68 DRI_CONF_PRECISE_TRIG("false")
69
70 DRI_CONF_OPT_BEGIN(clamp_max_samples, int, -1)
71 DRI_CONF_DESC(en, "Clamp the value of GL_MAX_SAMPLES to the "
72 "given integer. If negative, then do not clamp.")
73 DRI_CONF_OPT_END
74 DRI_CONF_SECTION_END
75
76 DRI_CONF_SECTION_DEBUG
77 DRI_CONF_NO_RAST("false")
78 DRI_CONF_ALWAYS_FLUSH_BATCH("false")
79 DRI_CONF_ALWAYS_FLUSH_CACHE("false")
80 DRI_CONF_DISABLE_THROTTLING("false")
81 DRI_CONF_FORCE_GLSL_EXTENSIONS_WARN("false")
82 DRI_CONF_DISABLE_GLSL_LINE_CONTINUATIONS("false")
83 DRI_CONF_DISABLE_BLEND_FUNC_EXTENDED("false")
84 DRI_CONF_DUAL_COLOR_BLEND_BY_LOCATION("false")
85 DRI_CONF_ALLOW_GLSL_EXTENSION_DIRECTIVE_MIDSHADER("false")
86
87 DRI_CONF_OPT_BEGIN_B(shader_precompile, "true")
88 DRI_CONF_DESC(en, "Perform code generation at shader link time.")
89 DRI_CONF_OPT_END
90 DRI_CONF_SECTION_END
91
92 DRI_CONF_SECTION_MISCELLANEOUS
93 DRI_CONF_GLSL_ZERO_INIT("false")
94 DRI_CONF_SECTION_END
95 DRI_CONF_END
96 };
97
98 #include "intel_batchbuffer.h"
99 #include "intel_buffers.h"
100 #include "intel_bufmgr.h"
101 #include "intel_fbo.h"
102 #include "intel_mipmap_tree.h"
103 #include "intel_screen.h"
104 #include "intel_tex.h"
105 #include "intel_image.h"
106
107 #include "brw_context.h"
108
109 #include "i915_drm.h"
110
111 /**
112 * For debugging purposes, this returns a time in seconds.
113 */
114 double
115 get_time(void)
116 {
117 struct timespec tp;
118
119 clock_gettime(CLOCK_MONOTONIC, &tp);
120
121 return tp.tv_sec + tp.tv_nsec / 1000000000.0;
122 }
123
124 void
125 aub_dump_bmp(struct gl_context *ctx)
126 {
127 struct gl_framebuffer *fb = ctx->DrawBuffer;
128
129 for (unsigned i = 0; i < fb->_NumColorDrawBuffers; i++) {
130 struct intel_renderbuffer *irb =
131 intel_renderbuffer(fb->_ColorDrawBuffers[i]);
132
133 if (irb && irb->mt) {
134 enum aub_dump_bmp_format format;
135
136 switch (irb->Base.Base.Format) {
137 case MESA_FORMAT_B8G8R8A8_UNORM:
138 case MESA_FORMAT_B8G8R8X8_UNORM:
139 format = AUB_DUMP_BMP_FORMAT_ARGB_8888;
140 break;
141 default:
142 continue;
143 }
144
145 drm_intel_gem_bo_aub_dump_bmp(irb->mt->bo,
146 irb->draw_x,
147 irb->draw_y,
148 irb->Base.Base.Width,
149 irb->Base.Base.Height,
150 format,
151 irb->mt->pitch,
152 0);
153 }
154 }
155 }
156
157 static const __DRItexBufferExtension intelTexBufferExtension = {
158 .base = { __DRI_TEX_BUFFER, 3 },
159
160 .setTexBuffer = intelSetTexBuffer,
161 .setTexBuffer2 = intelSetTexBuffer2,
162 .releaseTexBuffer = NULL,
163 };
164
165 static void
166 intel_dri2_flush_with_flags(__DRIcontext *cPriv,
167 __DRIdrawable *dPriv,
168 unsigned flags,
169 enum __DRI2throttleReason reason)
170 {
171 struct brw_context *brw = cPriv->driverPrivate;
172
173 if (!brw)
174 return;
175
176 struct gl_context *ctx = &brw->ctx;
177
178 FLUSH_VERTICES(ctx, 0);
179
180 if (flags & __DRI2_FLUSH_DRAWABLE)
181 intel_resolve_for_dri2_flush(brw, dPriv);
182
183 if (reason == __DRI2_THROTTLE_SWAPBUFFER)
184 brw->need_swap_throttle = true;
185 if (reason == __DRI2_THROTTLE_FLUSHFRONT)
186 brw->need_flush_throttle = true;
187
188 intel_batchbuffer_flush(brw);
189
190 if (INTEL_DEBUG & DEBUG_AUB) {
191 aub_dump_bmp(ctx);
192 }
193 }
194
195 /**
196 * Provides compatibility with loaders that only support the older (version
197 * 1-3) flush interface.
198 *
199 * That includes libGL up to Mesa 9.0, and the X Server at least up to 1.13.
200 */
201 static void
202 intel_dri2_flush(__DRIdrawable *drawable)
203 {
204 intel_dri2_flush_with_flags(drawable->driContextPriv, drawable,
205 __DRI2_FLUSH_DRAWABLE,
206 __DRI2_THROTTLE_SWAPBUFFER);
207 }
208
209 static const struct __DRI2flushExtensionRec intelFlushExtension = {
210 .base = { __DRI2_FLUSH, 4 },
211
212 .flush = intel_dri2_flush,
213 .invalidate = dri2InvalidateDrawable,
214 .flush_with_flags = intel_dri2_flush_with_flags,
215 };
216
217 static struct intel_image_format intel_image_formats[] = {
218 { __DRI_IMAGE_FOURCC_ARGB8888, __DRI_IMAGE_COMPONENTS_RGBA, 1,
219 { { 0, 0, 0, __DRI_IMAGE_FORMAT_ARGB8888, 4 } } },
220
221 { __DRI_IMAGE_FOURCC_ABGR8888, __DRI_IMAGE_COMPONENTS_RGBA, 1,
222 { { 0, 0, 0, __DRI_IMAGE_FORMAT_ABGR8888, 4 } } },
223
224 { __DRI_IMAGE_FOURCC_SARGB8888, __DRI_IMAGE_COMPONENTS_RGBA, 1,
225 { { 0, 0, 0, __DRI_IMAGE_FORMAT_SARGB8, 4 } } },
226
227 { __DRI_IMAGE_FOURCC_XRGB8888, __DRI_IMAGE_COMPONENTS_RGB, 1,
228 { { 0, 0, 0, __DRI_IMAGE_FORMAT_XRGB8888, 4 }, } },
229
230 { __DRI_IMAGE_FOURCC_XBGR8888, __DRI_IMAGE_COMPONENTS_RGB, 1,
231 { { 0, 0, 0, __DRI_IMAGE_FORMAT_XBGR8888, 4 }, } },
232
233 { __DRI_IMAGE_FOURCC_ARGB1555, __DRI_IMAGE_COMPONENTS_RGBA, 1,
234 { { 0, 0, 0, __DRI_IMAGE_FORMAT_ARGB1555, 2 } } },
235
236 { __DRI_IMAGE_FOURCC_RGB565, __DRI_IMAGE_COMPONENTS_RGB, 1,
237 { { 0, 0, 0, __DRI_IMAGE_FORMAT_RGB565, 2 } } },
238
239 { __DRI_IMAGE_FOURCC_R8, __DRI_IMAGE_COMPONENTS_R, 1,
240 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 }, } },
241
242 { __DRI_IMAGE_FOURCC_R16, __DRI_IMAGE_COMPONENTS_R, 1,
243 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R16, 1 }, } },
244
245 { __DRI_IMAGE_FOURCC_GR88, __DRI_IMAGE_COMPONENTS_RG, 1,
246 { { 0, 0, 0, __DRI_IMAGE_FORMAT_GR88, 2 }, } },
247
248 { __DRI_IMAGE_FOURCC_GR1616, __DRI_IMAGE_COMPONENTS_RG, 1,
249 { { 0, 0, 0, __DRI_IMAGE_FORMAT_GR1616, 2 }, } },
250
251 { __DRI_IMAGE_FOURCC_YUV410, __DRI_IMAGE_COMPONENTS_Y_U_V, 3,
252 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
253 { 1, 2, 2, __DRI_IMAGE_FORMAT_R8, 1 },
254 { 2, 2, 2, __DRI_IMAGE_FORMAT_R8, 1 } } },
255
256 { __DRI_IMAGE_FOURCC_YUV411, __DRI_IMAGE_COMPONENTS_Y_U_V, 3,
257 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
258 { 1, 2, 0, __DRI_IMAGE_FORMAT_R8, 1 },
259 { 2, 2, 0, __DRI_IMAGE_FORMAT_R8, 1 } } },
260
261 { __DRI_IMAGE_FOURCC_YUV420, __DRI_IMAGE_COMPONENTS_Y_U_V, 3,
262 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
263 { 1, 1, 1, __DRI_IMAGE_FORMAT_R8, 1 },
264 { 2, 1, 1, __DRI_IMAGE_FORMAT_R8, 1 } } },
265
266 { __DRI_IMAGE_FOURCC_YUV422, __DRI_IMAGE_COMPONENTS_Y_U_V, 3,
267 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
268 { 1, 1, 0, __DRI_IMAGE_FORMAT_R8, 1 },
269 { 2, 1, 0, __DRI_IMAGE_FORMAT_R8, 1 } } },
270
271 { __DRI_IMAGE_FOURCC_YUV444, __DRI_IMAGE_COMPONENTS_Y_U_V, 3,
272 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
273 { 1, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
274 { 2, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 } } },
275
276 { __DRI_IMAGE_FOURCC_YVU410, __DRI_IMAGE_COMPONENTS_Y_U_V, 3,
277 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
278 { 2, 2, 2, __DRI_IMAGE_FORMAT_R8, 1 },
279 { 1, 2, 2, __DRI_IMAGE_FORMAT_R8, 1 } } },
280
281 { __DRI_IMAGE_FOURCC_YVU411, __DRI_IMAGE_COMPONENTS_Y_U_V, 3,
282 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
283 { 2, 2, 0, __DRI_IMAGE_FORMAT_R8, 1 },
284 { 1, 2, 0, __DRI_IMAGE_FORMAT_R8, 1 } } },
285
286 { __DRI_IMAGE_FOURCC_YVU420, __DRI_IMAGE_COMPONENTS_Y_U_V, 3,
287 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
288 { 2, 1, 1, __DRI_IMAGE_FORMAT_R8, 1 },
289 { 1, 1, 1, __DRI_IMAGE_FORMAT_R8, 1 } } },
290
291 { __DRI_IMAGE_FOURCC_YVU422, __DRI_IMAGE_COMPONENTS_Y_U_V, 3,
292 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
293 { 2, 1, 0, __DRI_IMAGE_FORMAT_R8, 1 },
294 { 1, 1, 0, __DRI_IMAGE_FORMAT_R8, 1 } } },
295
296 { __DRI_IMAGE_FOURCC_YVU444, __DRI_IMAGE_COMPONENTS_Y_U_V, 3,
297 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
298 { 2, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
299 { 1, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 } } },
300
301 { __DRI_IMAGE_FOURCC_NV12, __DRI_IMAGE_COMPONENTS_Y_UV, 2,
302 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
303 { 1, 1, 1, __DRI_IMAGE_FORMAT_GR88, 2 } } },
304
305 { __DRI_IMAGE_FOURCC_NV16, __DRI_IMAGE_COMPONENTS_Y_UV, 2,
306 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
307 { 1, 1, 0, __DRI_IMAGE_FORMAT_GR88, 2 } } },
308
309 /* For YUYV buffers, we set up two overlapping DRI images and treat
310 * them as planar buffers in the compositors. Plane 0 is GR88 and
311 * samples YU or YV pairs and places Y into the R component, while
312 * plane 1 is ARGB and samples YUYV clusters and places pairs and
313 * places U into the G component and V into A. This lets the
314 * texture sampler interpolate the Y components correctly when
315 * sampling from plane 0, and interpolate U and V correctly when
316 * sampling from plane 1. */
317 { __DRI_IMAGE_FOURCC_YUYV, __DRI_IMAGE_COMPONENTS_Y_XUXV, 2,
318 { { 0, 0, 0, __DRI_IMAGE_FORMAT_GR88, 2 },
319 { 0, 1, 0, __DRI_IMAGE_FORMAT_ARGB8888, 4 } } }
320 };
321
322 static void
323 intel_image_warn_if_unaligned(__DRIimage *image, const char *func)
324 {
325 uint32_t tiling, swizzle;
326 drm_intel_bo_get_tiling(image->bo, &tiling, &swizzle);
327
328 if (tiling != I915_TILING_NONE && (image->offset & 0xfff)) {
329 _mesa_warning(NULL, "%s: offset 0x%08x not on tile boundary",
330 func, image->offset);
331 }
332 }
333
334 static struct intel_image_format *
335 intel_image_format_lookup(int fourcc)
336 {
337 struct intel_image_format *f = NULL;
338
339 for (unsigned i = 0; i < ARRAY_SIZE(intel_image_formats); i++) {
340 if (intel_image_formats[i].fourcc == fourcc) {
341 f = &intel_image_formats[i];
342 break;
343 }
344 }
345
346 return f;
347 }
348
349 static boolean intel_lookup_fourcc(int dri_format, int *fourcc)
350 {
351 for (unsigned i = 0; i < ARRAY_SIZE(intel_image_formats); i++) {
352 if (intel_image_formats[i].planes[0].dri_format == dri_format) {
353 *fourcc = intel_image_formats[i].fourcc;
354 return true;
355 }
356 }
357 return false;
358 }
359
360 static __DRIimage *
361 intel_allocate_image(int dri_format, void *loaderPrivate)
362 {
363 __DRIimage *image;
364
365 image = calloc(1, sizeof *image);
366 if (image == NULL)
367 return NULL;
368
369 image->dri_format = dri_format;
370 image->offset = 0;
371
372 image->format = driImageFormatToGLFormat(dri_format);
373 if (dri_format != __DRI_IMAGE_FORMAT_NONE &&
374 image->format == MESA_FORMAT_NONE) {
375 free(image);
376 return NULL;
377 }
378
379 image->internal_format = _mesa_get_format_base_format(image->format);
380 image->data = loaderPrivate;
381
382 return image;
383 }
384
385 /**
386 * Sets up a DRIImage structure to point to a slice out of a miptree.
387 */
388 static void
389 intel_setup_image_from_mipmap_tree(struct brw_context *brw, __DRIimage *image,
390 struct intel_mipmap_tree *mt, GLuint level,
391 GLuint zoffset)
392 {
393 intel_miptree_make_shareable(brw, mt);
394
395 intel_miptree_check_level_layer(mt, level, zoffset);
396
397 image->width = minify(mt->physical_width0, level - mt->first_level);
398 image->height = minify(mt->physical_height0, level - mt->first_level);
399 image->pitch = mt->pitch;
400
401 image->offset = intel_miptree_get_tile_offsets(mt, level, zoffset,
402 &image->tile_x,
403 &image->tile_y);
404
405 drm_intel_bo_unreference(image->bo);
406 image->bo = mt->bo;
407 drm_intel_bo_reference(mt->bo);
408 }
409
410 static __DRIimage *
411 intel_create_image_from_name(__DRIscreen *dri_screen,
412 int width, int height, int format,
413 int name, int pitch, void *loaderPrivate)
414 {
415 struct intel_screen *screen = dri_screen->driverPrivate;
416 __DRIimage *image;
417 int cpp;
418
419 image = intel_allocate_image(format, loaderPrivate);
420 if (image == NULL)
421 return NULL;
422
423 if (image->format == MESA_FORMAT_NONE)
424 cpp = 1;
425 else
426 cpp = _mesa_get_format_bytes(image->format);
427
428 image->width = width;
429 image->height = height;
430 image->pitch = pitch * cpp;
431 image->bo = drm_intel_bo_gem_create_from_name(screen->bufmgr, "image",
432 name);
433 if (!image->bo) {
434 free(image);
435 return NULL;
436 }
437
438 return image;
439 }
440
441 static __DRIimage *
442 intel_create_image_from_renderbuffer(__DRIcontext *context,
443 int renderbuffer, void *loaderPrivate)
444 {
445 __DRIimage *image;
446 struct brw_context *brw = context->driverPrivate;
447 struct gl_context *ctx = &brw->ctx;
448 struct gl_renderbuffer *rb;
449 struct intel_renderbuffer *irb;
450
451 rb = _mesa_lookup_renderbuffer(ctx, renderbuffer);
452 if (!rb) {
453 _mesa_error(ctx, GL_INVALID_OPERATION, "glRenderbufferExternalMESA");
454 return NULL;
455 }
456
457 irb = intel_renderbuffer(rb);
458 intel_miptree_make_shareable(brw, irb->mt);
459 image = calloc(1, sizeof *image);
460 if (image == NULL)
461 return NULL;
462
463 image->internal_format = rb->InternalFormat;
464 image->format = rb->Format;
465 image->offset = 0;
466 image->data = loaderPrivate;
467 drm_intel_bo_unreference(image->bo);
468 image->bo = irb->mt->bo;
469 drm_intel_bo_reference(irb->mt->bo);
470 image->width = rb->Width;
471 image->height = rb->Height;
472 image->pitch = irb->mt->pitch;
473 image->dri_format = driGLFormatToImageFormat(image->format);
474 image->has_depthstencil = irb->mt->stencil_mt? true : false;
475
476 rb->NeedsFinishRenderTexture = true;
477 return image;
478 }
479
480 static __DRIimage *
481 intel_create_image_from_texture(__DRIcontext *context, int target,
482 unsigned texture, int zoffset,
483 int level,
484 unsigned *error,
485 void *loaderPrivate)
486 {
487 __DRIimage *image;
488 struct brw_context *brw = context->driverPrivate;
489 struct gl_texture_object *obj;
490 struct intel_texture_object *iobj;
491 GLuint face = 0;
492
493 obj = _mesa_lookup_texture(&brw->ctx, texture);
494 if (!obj || obj->Target != target) {
495 *error = __DRI_IMAGE_ERROR_BAD_PARAMETER;
496 return NULL;
497 }
498
499 if (target == GL_TEXTURE_CUBE_MAP)
500 face = zoffset;
501
502 _mesa_test_texobj_completeness(&brw->ctx, obj);
503 iobj = intel_texture_object(obj);
504 if (!obj->_BaseComplete || (level > 0 && !obj->_MipmapComplete)) {
505 *error = __DRI_IMAGE_ERROR_BAD_PARAMETER;
506 return NULL;
507 }
508
509 if (level < obj->BaseLevel || level > obj->_MaxLevel) {
510 *error = __DRI_IMAGE_ERROR_BAD_MATCH;
511 return NULL;
512 }
513
514 if (target == GL_TEXTURE_3D && obj->Image[face][level]->Depth < zoffset) {
515 *error = __DRI_IMAGE_ERROR_BAD_MATCH;
516 return NULL;
517 }
518 image = calloc(1, sizeof *image);
519 if (image == NULL) {
520 *error = __DRI_IMAGE_ERROR_BAD_ALLOC;
521 return NULL;
522 }
523
524 image->internal_format = obj->Image[face][level]->InternalFormat;
525 image->format = obj->Image[face][level]->TexFormat;
526 image->data = loaderPrivate;
527 intel_setup_image_from_mipmap_tree(brw, image, iobj->mt, level, zoffset);
528 image->dri_format = driGLFormatToImageFormat(image->format);
529 image->has_depthstencil = iobj->mt->stencil_mt? true : false;
530 if (image->dri_format == MESA_FORMAT_NONE) {
531 *error = __DRI_IMAGE_ERROR_BAD_PARAMETER;
532 free(image);
533 return NULL;
534 }
535
536 *error = __DRI_IMAGE_ERROR_SUCCESS;
537 return image;
538 }
539
540 static void
541 intel_destroy_image(__DRIimage *image)
542 {
543 drm_intel_bo_unreference(image->bo);
544 free(image);
545 }
546
547 static __DRIimage *
548 intel_create_image(__DRIscreen *dri_screen,
549 int width, int height, int format,
550 unsigned int use,
551 void *loaderPrivate)
552 {
553 __DRIimage *image;
554 struct intel_screen *screen = dri_screen->driverPrivate;
555 uint32_t tiling;
556 int cpp;
557 unsigned long pitch;
558
559 tiling = I915_TILING_X;
560 if (use & __DRI_IMAGE_USE_CURSOR) {
561 if (width != 64 || height != 64)
562 return NULL;
563 tiling = I915_TILING_NONE;
564 }
565
566 if (use & __DRI_IMAGE_USE_LINEAR)
567 tiling = I915_TILING_NONE;
568
569 image = intel_allocate_image(format, loaderPrivate);
570 if (image == NULL)
571 return NULL;
572
573 cpp = _mesa_get_format_bytes(image->format);
574 image->bo = drm_intel_bo_alloc_tiled(screen->bufmgr, "image",
575 width, height, cpp, &tiling,
576 &pitch, 0);
577 if (image->bo == NULL) {
578 free(image);
579 return NULL;
580 }
581 image->width = width;
582 image->height = height;
583 image->pitch = pitch;
584
585 return image;
586 }
587
588 static GLboolean
589 intel_query_image(__DRIimage *image, int attrib, int *value)
590 {
591 switch (attrib) {
592 case __DRI_IMAGE_ATTRIB_STRIDE:
593 *value = image->pitch;
594 return true;
595 case __DRI_IMAGE_ATTRIB_HANDLE:
596 *value = image->bo->handle;
597 return true;
598 case __DRI_IMAGE_ATTRIB_NAME:
599 return !drm_intel_bo_flink(image->bo, (uint32_t *) value);
600 case __DRI_IMAGE_ATTRIB_FORMAT:
601 *value = image->dri_format;
602 return true;
603 case __DRI_IMAGE_ATTRIB_WIDTH:
604 *value = image->width;
605 return true;
606 case __DRI_IMAGE_ATTRIB_HEIGHT:
607 *value = image->height;
608 return true;
609 case __DRI_IMAGE_ATTRIB_COMPONENTS:
610 if (image->planar_format == NULL)
611 return false;
612 *value = image->planar_format->components;
613 return true;
614 case __DRI_IMAGE_ATTRIB_FD:
615 return !drm_intel_bo_gem_export_to_prime(image->bo, value);
616 case __DRI_IMAGE_ATTRIB_FOURCC:
617 return intel_lookup_fourcc(image->dri_format, value);
618 case __DRI_IMAGE_ATTRIB_NUM_PLANES:
619 *value = 1;
620 return true;
621 case __DRI_IMAGE_ATTRIB_OFFSET:
622 *value = image->offset;
623 return true;
624
625 default:
626 return false;
627 }
628 }
629
630 static __DRIimage *
631 intel_dup_image(__DRIimage *orig_image, void *loaderPrivate)
632 {
633 __DRIimage *image;
634
635 image = calloc(1, sizeof *image);
636 if (image == NULL)
637 return NULL;
638
639 drm_intel_bo_reference(orig_image->bo);
640 image->bo = orig_image->bo;
641 image->internal_format = orig_image->internal_format;
642 image->planar_format = orig_image->planar_format;
643 image->dri_format = orig_image->dri_format;
644 image->format = orig_image->format;
645 image->offset = orig_image->offset;
646 image->width = orig_image->width;
647 image->height = orig_image->height;
648 image->pitch = orig_image->pitch;
649 image->tile_x = orig_image->tile_x;
650 image->tile_y = orig_image->tile_y;
651 image->has_depthstencil = orig_image->has_depthstencil;
652 image->data = loaderPrivate;
653
654 memcpy(image->strides, orig_image->strides, sizeof(image->strides));
655 memcpy(image->offsets, orig_image->offsets, sizeof(image->offsets));
656
657 return image;
658 }
659
660 static GLboolean
661 intel_validate_usage(__DRIimage *image, unsigned int use)
662 {
663 if (use & __DRI_IMAGE_USE_CURSOR) {
664 if (image->width != 64 || image->height != 64)
665 return GL_FALSE;
666 }
667
668 return GL_TRUE;
669 }
670
671 static __DRIimage *
672 intel_create_image_from_names(__DRIscreen *dri_screen,
673 int width, int height, int fourcc,
674 int *names, int num_names,
675 int *strides, int *offsets,
676 void *loaderPrivate)
677 {
678 struct intel_image_format *f = NULL;
679 __DRIimage *image;
680 int i, index;
681
682 if (dri_screen == NULL || names == NULL || num_names != 1)
683 return NULL;
684
685 f = intel_image_format_lookup(fourcc);
686 if (f == NULL)
687 return NULL;
688
689 image = intel_create_image_from_name(dri_screen, width, height,
690 __DRI_IMAGE_FORMAT_NONE,
691 names[0], strides[0],
692 loaderPrivate);
693
694 if (image == NULL)
695 return NULL;
696
697 image->planar_format = f;
698 for (i = 0; i < f->nplanes; i++) {
699 index = f->planes[i].buffer_index;
700 image->offsets[index] = offsets[index];
701 image->strides[index] = strides[index];
702 }
703
704 return image;
705 }
706
707 static __DRIimage *
708 intel_create_image_from_fds(__DRIscreen *dri_screen,
709 int width, int height, int fourcc,
710 int *fds, int num_fds, int *strides, int *offsets,
711 void *loaderPrivate)
712 {
713 struct intel_screen *screen = dri_screen->driverPrivate;
714 struct intel_image_format *f;
715 __DRIimage *image;
716 int i, index;
717
718 if (fds == NULL || num_fds < 1)
719 return NULL;
720
721 /* We only support all planes from the same bo */
722 for (i = 0; i < num_fds; i++)
723 if (fds[0] != fds[i])
724 return NULL;
725
726 f = intel_image_format_lookup(fourcc);
727 if (f == NULL)
728 return NULL;
729
730 if (f->nplanes == 1)
731 image = intel_allocate_image(f->planes[0].dri_format, loaderPrivate);
732 else
733 image = intel_allocate_image(__DRI_IMAGE_FORMAT_NONE, loaderPrivate);
734
735 if (image == NULL)
736 return NULL;
737
738 image->width = width;
739 image->height = height;
740 image->pitch = strides[0];
741
742 image->planar_format = f;
743 int size = 0;
744 for (i = 0; i < f->nplanes; i++) {
745 index = f->planes[i].buffer_index;
746 image->offsets[index] = offsets[index];
747 image->strides[index] = strides[index];
748
749 const int plane_height = height >> f->planes[i].height_shift;
750 const int end = offsets[index] + plane_height * strides[index];
751 if (size < end)
752 size = end;
753 }
754
755 image->bo = drm_intel_bo_gem_create_from_prime(screen->bufmgr,
756 fds[0], size);
757 if (image->bo == NULL) {
758 free(image);
759 return NULL;
760 }
761
762 if (f->nplanes == 1) {
763 image->offset = image->offsets[0];
764 intel_image_warn_if_unaligned(image, __func__);
765 }
766
767 return image;
768 }
769
770 static __DRIimage *
771 intel_create_image_from_dma_bufs(__DRIscreen *dri_screen,
772 int width, int height, int fourcc,
773 int *fds, int num_fds,
774 int *strides, int *offsets,
775 enum __DRIYUVColorSpace yuv_color_space,
776 enum __DRISampleRange sample_range,
777 enum __DRIChromaSiting horizontal_siting,
778 enum __DRIChromaSiting vertical_siting,
779 unsigned *error,
780 void *loaderPrivate)
781 {
782 __DRIimage *image;
783 struct intel_image_format *f = intel_image_format_lookup(fourcc);
784
785 if (!f) {
786 *error = __DRI_IMAGE_ERROR_BAD_MATCH;
787 return NULL;
788 }
789
790 image = intel_create_image_from_fds(dri_screen, width, height, fourcc, fds,
791 num_fds, strides, offsets,
792 loaderPrivate);
793
794 /*
795 * Invalid parameters and any inconsistencies between are assumed to be
796 * checked by the caller. Therefore besides unsupported formats one can fail
797 * only in allocation.
798 */
799 if (!image) {
800 *error = __DRI_IMAGE_ERROR_BAD_ALLOC;
801 return NULL;
802 }
803
804 image->dma_buf_imported = true;
805 image->yuv_color_space = yuv_color_space;
806 image->sample_range = sample_range;
807 image->horizontal_siting = horizontal_siting;
808 image->vertical_siting = vertical_siting;
809
810 *error = __DRI_IMAGE_ERROR_SUCCESS;
811 return image;
812 }
813
814 static __DRIimage *
815 intel_from_planar(__DRIimage *parent, int plane, void *loaderPrivate)
816 {
817 int width, height, offset, stride, dri_format, index;
818 struct intel_image_format *f;
819 __DRIimage *image;
820
821 if (parent == NULL || parent->planar_format == NULL)
822 return NULL;
823
824 f = parent->planar_format;
825
826 if (plane >= f->nplanes)
827 return NULL;
828
829 width = parent->width >> f->planes[plane].width_shift;
830 height = parent->height >> f->planes[plane].height_shift;
831 dri_format = f->planes[plane].dri_format;
832 index = f->planes[plane].buffer_index;
833 offset = parent->offsets[index];
834 stride = parent->strides[index];
835
836 image = intel_allocate_image(dri_format, loaderPrivate);
837 if (image == NULL)
838 return NULL;
839
840 if (offset + height * stride > parent->bo->size) {
841 _mesa_warning(NULL, "intel_create_sub_image: subimage out of bounds");
842 free(image);
843 return NULL;
844 }
845
846 image->bo = parent->bo;
847 drm_intel_bo_reference(parent->bo);
848
849 image->width = width;
850 image->height = height;
851 image->pitch = stride;
852 image->offset = offset;
853
854 intel_image_warn_if_unaligned(image, __func__);
855
856 return image;
857 }
858
859 static const __DRIimageExtension intelImageExtension = {
860 .base = { __DRI_IMAGE, 13 },
861
862 .createImageFromName = intel_create_image_from_name,
863 .createImageFromRenderbuffer = intel_create_image_from_renderbuffer,
864 .destroyImage = intel_destroy_image,
865 .createImage = intel_create_image,
866 .queryImage = intel_query_image,
867 .dupImage = intel_dup_image,
868 .validateUsage = intel_validate_usage,
869 .createImageFromNames = intel_create_image_from_names,
870 .fromPlanar = intel_from_planar,
871 .createImageFromTexture = intel_create_image_from_texture,
872 .createImageFromFds = intel_create_image_from_fds,
873 .createImageFromDmaBufs = intel_create_image_from_dma_bufs,
874 .blitImage = NULL,
875 .getCapabilities = NULL,
876 .mapImage = NULL,
877 .unmapImage = NULL,
878 };
879
880 static int
881 brw_query_renderer_integer(__DRIscreen *dri_screen,
882 int param, unsigned int *value)
883 {
884 const struct intel_screen *const screen =
885 (struct intel_screen *) dri_screen->driverPrivate;
886
887 switch (param) {
888 case __DRI2_RENDERER_VENDOR_ID:
889 value[0] = 0x8086;
890 return 0;
891 case __DRI2_RENDERER_DEVICE_ID:
892 value[0] = screen->deviceID;
893 return 0;
894 case __DRI2_RENDERER_ACCELERATED:
895 value[0] = 1;
896 return 0;
897 case __DRI2_RENDERER_VIDEO_MEMORY: {
898 /* Once a batch uses more than 75% of the maximum mappable size, we
899 * assume that there's some fragmentation, and we start doing extra
900 * flushing, etc. That's the big cliff apps will care about.
901 */
902 size_t aper_size;
903 size_t mappable_size;
904
905 drm_intel_get_aperture_sizes(dri_screen->fd, &mappable_size, &aper_size);
906
907 const unsigned gpu_mappable_megabytes =
908 (aper_size / (1024 * 1024)) * 3 / 4;
909
910 const long system_memory_pages = sysconf(_SC_PHYS_PAGES);
911 const long system_page_size = sysconf(_SC_PAGE_SIZE);
912
913 if (system_memory_pages <= 0 || system_page_size <= 0)
914 return -1;
915
916 const uint64_t system_memory_bytes = (uint64_t) system_memory_pages
917 * (uint64_t) system_page_size;
918
919 const unsigned system_memory_megabytes =
920 (unsigned) (system_memory_bytes / (1024 * 1024));
921
922 value[0] = MIN2(system_memory_megabytes, gpu_mappable_megabytes);
923 return 0;
924 }
925 case __DRI2_RENDERER_UNIFIED_MEMORY_ARCHITECTURE:
926 value[0] = 1;
927 return 0;
928 case __DRI2_RENDERER_HAS_TEXTURE_3D:
929 value[0] = 1;
930 return 0;
931 default:
932 return driQueryRendererIntegerCommon(dri_screen, param, value);
933 }
934
935 return -1;
936 }
937
938 static int
939 brw_query_renderer_string(__DRIscreen *dri_screen,
940 int param, const char **value)
941 {
942 const struct intel_screen *screen =
943 (struct intel_screen *) dri_screen->driverPrivate;
944
945 switch (param) {
946 case __DRI2_RENDERER_VENDOR_ID:
947 value[0] = brw_vendor_string;
948 return 0;
949 case __DRI2_RENDERER_DEVICE_ID:
950 value[0] = brw_get_renderer_string(screen);
951 return 0;
952 default:
953 break;
954 }
955
956 return -1;
957 }
958
959 static const __DRI2rendererQueryExtension intelRendererQueryExtension = {
960 .base = { __DRI2_RENDERER_QUERY, 1 },
961
962 .queryInteger = brw_query_renderer_integer,
963 .queryString = brw_query_renderer_string
964 };
965
966 static const __DRIrobustnessExtension dri2Robustness = {
967 .base = { __DRI2_ROBUSTNESS, 1 }
968 };
969
970 static const __DRIextension *screenExtensions[] = {
971 &intelTexBufferExtension.base,
972 &intelFenceExtension.base,
973 &intelFlushExtension.base,
974 &intelImageExtension.base,
975 &intelRendererQueryExtension.base,
976 &dri2ConfigQueryExtension.base,
977 NULL
978 };
979
980 static const __DRIextension *intelRobustScreenExtensions[] = {
981 &intelTexBufferExtension.base,
982 &intelFenceExtension.base,
983 &intelFlushExtension.base,
984 &intelImageExtension.base,
985 &intelRendererQueryExtension.base,
986 &dri2ConfigQueryExtension.base,
987 &dri2Robustness.base,
988 NULL
989 };
990
991 static int
992 intel_get_param(struct intel_screen *screen, int param, int *value)
993 {
994 int ret = 0;
995 struct drm_i915_getparam gp;
996
997 memset(&gp, 0, sizeof(gp));
998 gp.param = param;
999 gp.value = value;
1000
1001 if (drmIoctl(screen->driScrnPriv->fd, DRM_IOCTL_I915_GETPARAM, &gp) == -1) {
1002 ret = -errno;
1003 if (ret != -EINVAL)
1004 _mesa_warning(NULL, "drm_i915_getparam: %d", ret);
1005 }
1006
1007 return ret;
1008 }
1009
1010 static bool
1011 intel_get_boolean(struct intel_screen *screen, int param)
1012 {
1013 int value = 0;
1014 return (intel_get_param(screen, param, &value) == 0) && value;
1015 }
1016
1017 static int
1018 intel_get_integer(struct intel_screen *screen, int param)
1019 {
1020 int value = -1;
1021
1022 if (intel_get_param(screen, param, &value) == 0)
1023 return value;
1024
1025 return -1;
1026 }
1027
1028 static void
1029 intelDestroyScreen(__DRIscreen * sPriv)
1030 {
1031 struct intel_screen *screen = sPriv->driverPrivate;
1032
1033 dri_bufmgr_destroy(screen->bufmgr);
1034 driDestroyOptionInfo(&screen->optionCache);
1035
1036 ralloc_free(screen);
1037 sPriv->driverPrivate = NULL;
1038 }
1039
1040
1041 /**
1042 * This is called when we need to set up GL rendering to a new X window.
1043 */
1044 static GLboolean
1045 intelCreateBuffer(__DRIscreen *dri_screen,
1046 __DRIdrawable * driDrawPriv,
1047 const struct gl_config * mesaVis, GLboolean isPixmap)
1048 {
1049 struct intel_renderbuffer *rb;
1050 struct intel_screen *screen = (struct intel_screen *)
1051 dri_screen->driverPrivate;
1052 mesa_format rgbFormat;
1053 unsigned num_samples =
1054 intel_quantize_num_samples(screen, mesaVis->samples);
1055 struct gl_framebuffer *fb;
1056
1057 if (isPixmap)
1058 return false;
1059
1060 fb = CALLOC_STRUCT(gl_framebuffer);
1061 if (!fb)
1062 return false;
1063
1064 _mesa_initialize_window_framebuffer(fb, mesaVis);
1065
1066 if (screen->winsys_msaa_samples_override != -1) {
1067 num_samples = screen->winsys_msaa_samples_override;
1068 fb->Visual.samples = num_samples;
1069 }
1070
1071 if (mesaVis->redBits == 5) {
1072 rgbFormat = mesaVis->redMask == 0x1f ? MESA_FORMAT_R5G6B5_UNORM
1073 : MESA_FORMAT_B5G6R5_UNORM;
1074 } else if (mesaVis->sRGBCapable) {
1075 rgbFormat = mesaVis->redMask == 0xff ? MESA_FORMAT_R8G8B8A8_SRGB
1076 : MESA_FORMAT_B8G8R8A8_SRGB;
1077 } else if (mesaVis->alphaBits == 0) {
1078 rgbFormat = mesaVis->redMask == 0xff ? MESA_FORMAT_R8G8B8X8_UNORM
1079 : MESA_FORMAT_B8G8R8X8_UNORM;
1080 } else {
1081 rgbFormat = mesaVis->redMask == 0xff ? MESA_FORMAT_R8G8B8A8_SRGB
1082 : MESA_FORMAT_B8G8R8A8_SRGB;
1083 fb->Visual.sRGBCapable = true;
1084 }
1085
1086 /* setup the hardware-based renderbuffers */
1087 rb = intel_create_renderbuffer(rgbFormat, num_samples);
1088 _mesa_add_renderbuffer(fb, BUFFER_FRONT_LEFT, &rb->Base.Base);
1089
1090 if (mesaVis->doubleBufferMode) {
1091 rb = intel_create_renderbuffer(rgbFormat, num_samples);
1092 _mesa_add_renderbuffer(fb, BUFFER_BACK_LEFT, &rb->Base.Base);
1093 }
1094
1095 /*
1096 * Assert here that the gl_config has an expected depth/stencil bit
1097 * combination: one of d24/s8, d16/s0, d0/s0. (See intelInitScreen2(),
1098 * which constructs the advertised configs.)
1099 */
1100 if (mesaVis->depthBits == 24) {
1101 assert(mesaVis->stencilBits == 8);
1102
1103 if (screen->devinfo.has_hiz_and_separate_stencil) {
1104 rb = intel_create_private_renderbuffer(MESA_FORMAT_Z24_UNORM_X8_UINT,
1105 num_samples);
1106 _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &rb->Base.Base);
1107 rb = intel_create_private_renderbuffer(MESA_FORMAT_S_UINT8,
1108 num_samples);
1109 _mesa_add_renderbuffer(fb, BUFFER_STENCIL, &rb->Base.Base);
1110 } else {
1111 /*
1112 * Use combined depth/stencil. Note that the renderbuffer is
1113 * attached to two attachment points.
1114 */
1115 rb = intel_create_private_renderbuffer(MESA_FORMAT_Z24_UNORM_S8_UINT,
1116 num_samples);
1117 _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &rb->Base.Base);
1118 _mesa_add_renderbuffer(fb, BUFFER_STENCIL, &rb->Base.Base);
1119 }
1120 }
1121 else if (mesaVis->depthBits == 16) {
1122 assert(mesaVis->stencilBits == 0);
1123 rb = intel_create_private_renderbuffer(MESA_FORMAT_Z_UNORM16,
1124 num_samples);
1125 _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &rb->Base.Base);
1126 }
1127 else {
1128 assert(mesaVis->depthBits == 0);
1129 assert(mesaVis->stencilBits == 0);
1130 }
1131
1132 /* now add any/all software-based renderbuffers we may need */
1133 _swrast_add_soft_renderbuffers(fb,
1134 false, /* never sw color */
1135 false, /* never sw depth */
1136 false, /* never sw stencil */
1137 mesaVis->accumRedBits > 0,
1138 false, /* never sw alpha */
1139 false /* never sw aux */ );
1140 driDrawPriv->driverPrivate = fb;
1141
1142 return true;
1143 }
1144
1145 static void
1146 intelDestroyBuffer(__DRIdrawable * driDrawPriv)
1147 {
1148 struct gl_framebuffer *fb = driDrawPriv->driverPrivate;
1149
1150 _mesa_reference_framebuffer(&fb, NULL);
1151 }
1152
1153 static void
1154 intel_detect_sseu(struct intel_screen *screen)
1155 {
1156 assert(screen->devinfo.gen >= 8);
1157 int ret;
1158
1159 screen->subslice_total = -1;
1160 screen->eu_total = -1;
1161
1162 ret = intel_get_param(screen, I915_PARAM_SUBSLICE_TOTAL,
1163 &screen->subslice_total);
1164 if (ret < 0 && ret != -EINVAL)
1165 goto err_out;
1166
1167 ret = intel_get_param(screen,
1168 I915_PARAM_EU_TOTAL, &screen->eu_total);
1169 if (ret < 0 && ret != -EINVAL)
1170 goto err_out;
1171
1172 /* Without this information, we cannot get the right Braswell brandstrings,
1173 * and we have to use conservative numbers for GPGPU on many platforms, but
1174 * otherwise, things will just work.
1175 */
1176 if (screen->subslice_total < 1 || screen->eu_total < 1)
1177 _mesa_warning(NULL,
1178 "Kernel 4.1 required to properly query GPU properties.\n");
1179
1180 return;
1181
1182 err_out:
1183 screen->subslice_total = -1;
1184 screen->eu_total = -1;
1185 _mesa_warning(NULL, "Failed to query GPU properties (%s).\n", strerror(-ret));
1186 }
1187
1188 static bool
1189 intel_init_bufmgr(struct intel_screen *screen)
1190 {
1191 __DRIscreen *dri_screen = screen->driScrnPriv;
1192
1193 screen->no_hw = getenv("INTEL_NO_HW") != NULL;
1194
1195 screen->bufmgr = intel_bufmgr_gem_init(dri_screen->fd, BATCH_SZ);
1196 if (screen->bufmgr == NULL) {
1197 fprintf(stderr, "[%s:%u] Error initializing buffer manager.\n",
1198 __func__, __LINE__);
1199 return false;
1200 }
1201
1202 drm_intel_bufmgr_gem_enable_fenced_relocs(screen->bufmgr);
1203
1204 if (!intel_get_boolean(screen, I915_PARAM_HAS_RELAXED_DELTA)) {
1205 fprintf(stderr, "[%s: %u] Kernel 2.6.39 required.\n", __func__, __LINE__);
1206 return false;
1207 }
1208
1209 return true;
1210 }
1211
1212 static bool
1213 intel_detect_swizzling(struct intel_screen *screen)
1214 {
1215 drm_intel_bo *buffer;
1216 unsigned long flags = 0;
1217 unsigned long aligned_pitch;
1218 uint32_t tiling = I915_TILING_X;
1219 uint32_t swizzle_mode = 0;
1220
1221 buffer = drm_intel_bo_alloc_tiled(screen->bufmgr, "swizzle test",
1222 64, 64, 4,
1223 &tiling, &aligned_pitch, flags);
1224 if (buffer == NULL)
1225 return false;
1226
1227 drm_intel_bo_get_tiling(buffer, &tiling, &swizzle_mode);
1228 drm_intel_bo_unreference(buffer);
1229
1230 if (swizzle_mode == I915_BIT_6_SWIZZLE_NONE)
1231 return false;
1232 else
1233 return true;
1234 }
1235
1236 static int
1237 intel_detect_timestamp(struct intel_screen *screen)
1238 {
1239 uint64_t dummy = 0, last = 0;
1240 int upper, lower, loops;
1241
1242 /* On 64bit systems, some old kernels trigger a hw bug resulting in the
1243 * TIMESTAMP register being shifted and the low 32bits always zero.
1244 *
1245 * More recent kernels offer an interface to read the full 36bits
1246 * everywhere.
1247 */
1248 if (drm_intel_reg_read(screen->bufmgr, TIMESTAMP | 1, &dummy) == 0)
1249 return 3;
1250
1251 /* Determine if we have a 32bit or 64bit kernel by inspecting the
1252 * upper 32bits for a rapidly changing timestamp.
1253 */
1254 if (drm_intel_reg_read(screen->bufmgr, TIMESTAMP, &last))
1255 return 0;
1256
1257 upper = lower = 0;
1258 for (loops = 0; loops < 10; loops++) {
1259 /* The TIMESTAMP should change every 80ns, so several round trips
1260 * through the kernel should be enough to advance it.
1261 */
1262 if (drm_intel_reg_read(screen->bufmgr, TIMESTAMP, &dummy))
1263 return 0;
1264
1265 upper += (dummy >> 32) != (last >> 32);
1266 if (upper > 1) /* beware 32bit counter overflow */
1267 return 2; /* upper dword holds the low 32bits of the timestamp */
1268
1269 lower += (dummy & 0xffffffff) != (last & 0xffffffff);
1270 if (lower > 1)
1271 return 1; /* timestamp is unshifted */
1272
1273 last = dummy;
1274 }
1275
1276 /* No advancement? No timestamp! */
1277 return 0;
1278 }
1279
1280 /**
1281 * Test if we can use MI_LOAD_REGISTER_MEM from an untrusted batchbuffer.
1282 *
1283 * Some combinations of hardware and kernel versions allow this feature,
1284 * while others don't. Instead of trying to enumerate every case, just
1285 * try and write a register and see if works.
1286 */
1287 static bool
1288 intel_detect_pipelined_register(struct intel_screen *screen,
1289 int reg, uint32_t expected_value, bool reset)
1290 {
1291 drm_intel_bo *results, *bo;
1292 uint32_t *batch;
1293 uint32_t offset = 0;
1294 bool success = false;
1295
1296 /* Create a zero'ed temporary buffer for reading our results */
1297 results = drm_intel_bo_alloc(screen->bufmgr, "registers", 4096, 0);
1298 if (results == NULL)
1299 goto err;
1300
1301 bo = drm_intel_bo_alloc(screen->bufmgr, "batchbuffer", 4096, 0);
1302 if (bo == NULL)
1303 goto err_results;
1304
1305 if (drm_intel_bo_map(bo, 1))
1306 goto err_batch;
1307
1308 batch = bo->virtual;
1309
1310 /* Write the register. */
1311 *batch++ = MI_LOAD_REGISTER_IMM | (3 - 2);
1312 *batch++ = reg;
1313 *batch++ = expected_value;
1314
1315 /* Save the register's value back to the buffer. */
1316 *batch++ = MI_STORE_REGISTER_MEM | (3 - 2);
1317 *batch++ = reg;
1318 drm_intel_bo_emit_reloc(bo, (char *)batch -(char *)bo->virtual,
1319 results, offset*sizeof(uint32_t),
1320 I915_GEM_DOMAIN_INSTRUCTION,
1321 I915_GEM_DOMAIN_INSTRUCTION);
1322 *batch++ = results->offset + offset*sizeof(uint32_t);
1323
1324 /* And afterwards clear the register */
1325 if (reset) {
1326 *batch++ = MI_LOAD_REGISTER_IMM | (3 - 2);
1327 *batch++ = reg;
1328 *batch++ = 0;
1329 }
1330
1331 *batch++ = MI_BATCH_BUFFER_END;
1332
1333 drm_intel_bo_mrb_exec(bo, ALIGN((char *)batch - (char *)bo->virtual, 8),
1334 NULL, 0, 0,
1335 I915_EXEC_RENDER);
1336
1337 /* Check whether the value got written. */
1338 if (drm_intel_bo_map(results, false) == 0) {
1339 success = *((uint32_t *)results->virtual + offset) == expected_value;
1340 drm_intel_bo_unmap(results);
1341 }
1342
1343 err_batch:
1344 drm_intel_bo_unreference(bo);
1345 err_results:
1346 drm_intel_bo_unreference(results);
1347 err:
1348 return success;
1349 }
1350
1351 static bool
1352 intel_detect_pipelined_so(struct intel_screen *screen)
1353 {
1354 /* Supposedly, Broadwell just works. */
1355 if (screen->devinfo.gen >= 8)
1356 return true;
1357
1358 if (screen->devinfo.gen <= 6)
1359 return false;
1360
1361 /* We use SO_WRITE_OFFSET0 since you're supposed to write it (unlike the
1362 * statistics registers), and we already reset it to zero before using it.
1363 */
1364 return intel_detect_pipelined_register(screen,
1365 GEN7_SO_WRITE_OFFSET(0),
1366 0x1337d0d0,
1367 false);
1368 }
1369
1370 /**
1371 * Return array of MSAA modes supported by the hardware. The array is
1372 * zero-terminated and sorted in decreasing order.
1373 */
1374 const int*
1375 intel_supported_msaa_modes(const struct intel_screen *screen)
1376 {
1377 static const int gen9_modes[] = {16, 8, 4, 2, 0, -1};
1378 static const int gen8_modes[] = {8, 4, 2, 0, -1};
1379 static const int gen7_modes[] = {8, 4, 0, -1};
1380 static const int gen6_modes[] = {4, 0, -1};
1381 static const int gen4_modes[] = {0, -1};
1382
1383 if (screen->devinfo.gen >= 9) {
1384 return gen9_modes;
1385 } else if (screen->devinfo.gen >= 8) {
1386 return gen8_modes;
1387 } else if (screen->devinfo.gen >= 7) {
1388 return gen7_modes;
1389 } else if (screen->devinfo.gen == 6) {
1390 return gen6_modes;
1391 } else {
1392 return gen4_modes;
1393 }
1394 }
1395
1396 static __DRIconfig**
1397 intel_screen_make_configs(__DRIscreen *dri_screen)
1398 {
1399 static const mesa_format formats[] = {
1400 MESA_FORMAT_B5G6R5_UNORM,
1401 MESA_FORMAT_B8G8R8A8_UNORM,
1402 MESA_FORMAT_B8G8R8X8_UNORM
1403 };
1404
1405 /* GLX_SWAP_COPY_OML is not supported due to page flipping. */
1406 static const GLenum back_buffer_modes[] = {
1407 GLX_SWAP_UNDEFINED_OML, GLX_NONE,
1408 };
1409
1410 static const uint8_t singlesample_samples[1] = {0};
1411 static const uint8_t multisample_samples[2] = {4, 8};
1412
1413 struct intel_screen *screen = dri_screen->driverPrivate;
1414 const struct gen_device_info *devinfo = &screen->devinfo;
1415 uint8_t depth_bits[4], stencil_bits[4];
1416 __DRIconfig **configs = NULL;
1417
1418 /* Generate singlesample configs without accumulation buffer. */
1419 for (unsigned i = 0; i < ARRAY_SIZE(formats); i++) {
1420 __DRIconfig **new_configs;
1421 int num_depth_stencil_bits = 2;
1422
1423 /* Starting with DRI2 protocol version 1.1 we can request a depth/stencil
1424 * buffer that has a different number of bits per pixel than the color
1425 * buffer, gen >= 6 supports this.
1426 */
1427 depth_bits[0] = 0;
1428 stencil_bits[0] = 0;
1429
1430 if (formats[i] == MESA_FORMAT_B5G6R5_UNORM) {
1431 depth_bits[1] = 16;
1432 stencil_bits[1] = 0;
1433 if (devinfo->gen >= 6) {
1434 depth_bits[2] = 24;
1435 stencil_bits[2] = 8;
1436 num_depth_stencil_bits = 3;
1437 }
1438 } else {
1439 depth_bits[1] = 24;
1440 stencil_bits[1] = 8;
1441 }
1442
1443 new_configs = driCreateConfigs(formats[i],
1444 depth_bits,
1445 stencil_bits,
1446 num_depth_stencil_bits,
1447 back_buffer_modes, 2,
1448 singlesample_samples, 1,
1449 false, false);
1450 configs = driConcatConfigs(configs, new_configs);
1451 }
1452
1453 /* Generate the minimum possible set of configs that include an
1454 * accumulation buffer.
1455 */
1456 for (unsigned i = 0; i < ARRAY_SIZE(formats); i++) {
1457 __DRIconfig **new_configs;
1458
1459 if (formats[i] == MESA_FORMAT_B5G6R5_UNORM) {
1460 depth_bits[0] = 16;
1461 stencil_bits[0] = 0;
1462 } else {
1463 depth_bits[0] = 24;
1464 stencil_bits[0] = 8;
1465 }
1466
1467 new_configs = driCreateConfigs(formats[i],
1468 depth_bits, stencil_bits, 1,
1469 back_buffer_modes, 1,
1470 singlesample_samples, 1,
1471 true, false);
1472 configs = driConcatConfigs(configs, new_configs);
1473 }
1474
1475 /* Generate multisample configs.
1476 *
1477 * This loop breaks early, and hence is a no-op, on gen < 6.
1478 *
1479 * Multisample configs must follow the singlesample configs in order to
1480 * work around an X server bug present in 1.12. The X server chooses to
1481 * associate the first listed RGBA888-Z24S8 config, regardless of its
1482 * sample count, with the 32-bit depth visual used for compositing.
1483 *
1484 * Only doublebuffer configs with GLX_SWAP_UNDEFINED_OML behavior are
1485 * supported. Singlebuffer configs are not supported because no one wants
1486 * them.
1487 */
1488 for (unsigned i = 0; i < ARRAY_SIZE(formats); i++) {
1489 if (devinfo->gen < 6)
1490 break;
1491
1492 __DRIconfig **new_configs;
1493 const int num_depth_stencil_bits = 2;
1494 int num_msaa_modes = 0;
1495
1496 depth_bits[0] = 0;
1497 stencil_bits[0] = 0;
1498
1499 if (formats[i] == MESA_FORMAT_B5G6R5_UNORM) {
1500 depth_bits[1] = 16;
1501 stencil_bits[1] = 0;
1502 } else {
1503 depth_bits[1] = 24;
1504 stencil_bits[1] = 8;
1505 }
1506
1507 if (devinfo->gen >= 7)
1508 num_msaa_modes = 2;
1509 else if (devinfo->gen == 6)
1510 num_msaa_modes = 1;
1511
1512 new_configs = driCreateConfigs(formats[i],
1513 depth_bits,
1514 stencil_bits,
1515 num_depth_stencil_bits,
1516 back_buffer_modes, 1,
1517 multisample_samples,
1518 num_msaa_modes,
1519 false, false);
1520 configs = driConcatConfigs(configs, new_configs);
1521 }
1522
1523 if (configs == NULL) {
1524 fprintf(stderr, "[%s:%u] Error creating FBConfig!\n", __func__,
1525 __LINE__);
1526 return NULL;
1527 }
1528
1529 return configs;
1530 }
1531
1532 static void
1533 set_max_gl_versions(struct intel_screen *screen)
1534 {
1535 __DRIscreen *dri_screen = screen->driScrnPriv;
1536 const bool has_astc = screen->devinfo.gen >= 9;
1537
1538 switch (screen->devinfo.gen) {
1539 case 9:
1540 case 8:
1541 dri_screen->max_gl_core_version = 45;
1542 dri_screen->max_gl_compat_version = 30;
1543 dri_screen->max_gl_es1_version = 11;
1544 dri_screen->max_gl_es2_version = has_astc ? 32 : 31;
1545 break;
1546 case 7:
1547 dri_screen->max_gl_core_version = screen->devinfo.is_haswell &&
1548 can_do_pipelined_register_writes(screen) ? 45 : 33;
1549 dri_screen->max_gl_compat_version = 30;
1550 dri_screen->max_gl_es1_version = 11;
1551 dri_screen->max_gl_es2_version = screen->devinfo.is_haswell ? 31 : 30;
1552 break;
1553 case 6:
1554 dri_screen->max_gl_core_version = 33;
1555 dri_screen->max_gl_compat_version = 30;
1556 dri_screen->max_gl_es1_version = 11;
1557 dri_screen->max_gl_es2_version = 30;
1558 break;
1559 case 5:
1560 case 4:
1561 dri_screen->max_gl_core_version = 0;
1562 dri_screen->max_gl_compat_version = 21;
1563 dri_screen->max_gl_es1_version = 11;
1564 dri_screen->max_gl_es2_version = 20;
1565 break;
1566 default:
1567 unreachable("unrecognized intel_screen::gen");
1568 }
1569 }
1570
1571 /**
1572 * Return the revision (generally the revid field of the PCI header) of the
1573 * graphics device.
1574 *
1575 * XXX: This function is useful to keep around even if it is not currently in
1576 * use. It is necessary for new platforms and revision specific workarounds or
1577 * features. Please don't remove it so that we know it at least continues to
1578 * build.
1579 */
1580 static __attribute__((__unused__)) int
1581 brw_get_revision(int fd)
1582 {
1583 struct drm_i915_getparam gp;
1584 int revision;
1585 int ret;
1586
1587 memset(&gp, 0, sizeof(gp));
1588 gp.param = I915_PARAM_REVISION;
1589 gp.value = &revision;
1590
1591 ret = drmCommandWriteRead(fd, DRM_I915_GETPARAM, &gp, sizeof(gp));
1592 if (ret)
1593 revision = -1;
1594
1595 return revision;
1596 }
1597
1598 /* Drop when RS headers get pulled to libdrm */
1599 #ifndef I915_PARAM_HAS_RESOURCE_STREAMER
1600 #define I915_PARAM_HAS_RESOURCE_STREAMER 36
1601 #endif
1602
1603 static void
1604 shader_debug_log_mesa(void *data, const char *fmt, ...)
1605 {
1606 struct brw_context *brw = (struct brw_context *)data;
1607 va_list args;
1608
1609 va_start(args, fmt);
1610 GLuint msg_id = 0;
1611 _mesa_gl_vdebug(&brw->ctx, &msg_id,
1612 MESA_DEBUG_SOURCE_SHADER_COMPILER,
1613 MESA_DEBUG_TYPE_OTHER,
1614 MESA_DEBUG_SEVERITY_NOTIFICATION, fmt, args);
1615 va_end(args);
1616 }
1617
1618 static void
1619 shader_perf_log_mesa(void *data, const char *fmt, ...)
1620 {
1621 struct brw_context *brw = (struct brw_context *)data;
1622
1623 va_list args;
1624 va_start(args, fmt);
1625
1626 if (unlikely(INTEL_DEBUG & DEBUG_PERF)) {
1627 va_list args_copy;
1628 va_copy(args_copy, args);
1629 vfprintf(stderr, fmt, args_copy);
1630 va_end(args_copy);
1631 }
1632
1633 if (brw->perf_debug) {
1634 GLuint msg_id = 0;
1635 _mesa_gl_vdebug(&brw->ctx, &msg_id,
1636 MESA_DEBUG_SOURCE_SHADER_COMPILER,
1637 MESA_DEBUG_TYPE_PERFORMANCE,
1638 MESA_DEBUG_SEVERITY_MEDIUM, fmt, args);
1639 }
1640 va_end(args);
1641 }
1642
1643 /**
1644 * This is the driver specific part of the createNewScreen entry point.
1645 * Called when using DRI2.
1646 *
1647 * \return the struct gl_config supported by this driver
1648 */
1649 static const
1650 __DRIconfig **intelInitScreen2(__DRIscreen *dri_screen)
1651 {
1652 struct intel_screen *screen;
1653
1654 if (dri_screen->image.loader) {
1655 } else if (dri_screen->dri2.loader->base.version <= 2 ||
1656 dri_screen->dri2.loader->getBuffersWithFormat == NULL) {
1657 fprintf(stderr,
1658 "\nERROR! DRI2 loader with getBuffersWithFormat() "
1659 "support required\n");
1660 return false;
1661 }
1662
1663 /* Allocate the private area */
1664 screen = rzalloc(NULL, struct intel_screen);
1665 if (!screen) {
1666 fprintf(stderr, "\nERROR! Allocating private area failed\n");
1667 return false;
1668 }
1669 /* parse information in __driConfigOptions */
1670 driParseOptionInfo(&screen->optionCache, brw_config_options.xml);
1671
1672 screen->driScrnPriv = dri_screen;
1673 dri_screen->driverPrivate = (void *) screen;
1674
1675 if (!intel_init_bufmgr(screen))
1676 return false;
1677
1678 screen->deviceID = drm_intel_bufmgr_gem_get_devid(screen->bufmgr);
1679 if (!gen_get_device_info(screen->deviceID, &screen->devinfo))
1680 return false;
1681
1682 brw_process_intel_debug_variable();
1683
1684 if (INTEL_DEBUG & DEBUG_BUFMGR)
1685 dri_bufmgr_set_debug(screen->bufmgr, true);
1686
1687 if ((INTEL_DEBUG & DEBUG_SHADER_TIME) && screen->devinfo.gen < 7) {
1688 fprintf(stderr,
1689 "shader_time debugging requires gen7 (Ivybridge) or better.\n");
1690 INTEL_DEBUG &= ~DEBUG_SHADER_TIME;
1691 }
1692
1693 if (INTEL_DEBUG & DEBUG_AUB)
1694 drm_intel_bufmgr_gem_set_aub_dump(screen->bufmgr, true);
1695
1696 #ifndef I915_PARAM_MMAP_GTT_VERSION
1697 #define I915_PARAM_MMAP_GTT_VERSION 40 /* XXX delete me with new libdrm */
1698 #endif
1699 if (intel_get_integer(screen, I915_PARAM_MMAP_GTT_VERSION) >= 1) {
1700 /* Theorectically unlimited! At least for individual objects...
1701 *
1702 * Currently the entire (global) address space for all GTT maps is
1703 * limited to 64bits. That is all objects on the system that are
1704 * setup for GTT mmapping must fit within 64bits. An attempt to use
1705 * one that exceeds the limit with fail in drm_intel_bo_map_gtt().
1706 *
1707 * Long before we hit that limit, we will be practically limited by
1708 * that any single object must fit in physical memory (RAM). The upper
1709 * limit on the CPU's address space is currently 48bits (Skylake), of
1710 * which only 39bits can be physical memory. (The GPU itself also has
1711 * a 48bit addressable virtual space.) We can fit over 32 million
1712 * objects of the current maximum allocable size before running out
1713 * of mmap space.
1714 */
1715 screen->max_gtt_map_object_size = UINT64_MAX;
1716 } else {
1717 /* Estimate the size of the mappable aperture into the GTT. There's an
1718 * ioctl to get the whole GTT size, but not one to get the mappable subset.
1719 * It turns out it's basically always 256MB, though some ancient hardware
1720 * was smaller.
1721 */
1722 uint32_t gtt_size = 256 * 1024 * 1024;
1723
1724 /* We don't want to map two objects such that a memcpy between them would
1725 * just fault one mapping in and then the other over and over forever. So
1726 * we would need to divide the GTT size by 2. Additionally, some GTT is
1727 * taken up by things like the framebuffer and the ringbuffer and such, so
1728 * be more conservative.
1729 */
1730 screen->max_gtt_map_object_size = gtt_size / 4;
1731 }
1732
1733 screen->hw_has_swizzling = intel_detect_swizzling(screen);
1734 screen->hw_has_timestamp = intel_detect_timestamp(screen);
1735
1736 /* GENs prior to 8 do not support EU/Subslice info */
1737 if (screen->devinfo.gen >= 8) {
1738 intel_detect_sseu(screen);
1739 } else if (screen->devinfo.gen == 7) {
1740 screen->subslice_total = 1 << (screen->devinfo.gt - 1);
1741 }
1742
1743 if (intel_detect_pipelined_so(screen))
1744 screen->kernel_features |= KERNEL_ALLOWS_SOL_OFFSET_WRITES;
1745
1746 const char *force_msaa = getenv("INTEL_FORCE_MSAA");
1747 if (force_msaa) {
1748 screen->winsys_msaa_samples_override =
1749 intel_quantize_num_samples(screen, atoi(force_msaa));
1750 printf("Forcing winsys sample count to %d\n",
1751 screen->winsys_msaa_samples_override);
1752 } else {
1753 screen->winsys_msaa_samples_override = -1;
1754 }
1755
1756 set_max_gl_versions(screen);
1757
1758 /* Notification of GPU resets requires hardware contexts and a kernel new
1759 * enough to support DRM_IOCTL_I915_GET_RESET_STATS. If the ioctl is
1760 * supported, calling it with a context of 0 will either generate EPERM or
1761 * no error. If the ioctl is not supported, it always generate EINVAL.
1762 * Use this to determine whether to advertise the __DRI2_ROBUSTNESS
1763 * extension to the loader.
1764 *
1765 * Don't even try on pre-Gen6, since we don't attempt to use contexts there.
1766 */
1767 if (screen->devinfo.gen >= 6) {
1768 struct drm_i915_reset_stats stats;
1769 memset(&stats, 0, sizeof(stats));
1770
1771 const int ret = drmIoctl(dri_screen->fd, DRM_IOCTL_I915_GET_RESET_STATS, &stats);
1772
1773 screen->has_context_reset_notification =
1774 (ret != -1 || errno != EINVAL);
1775 }
1776
1777 if (intel_get_param(screen, I915_PARAM_CMD_PARSER_VERSION,
1778 &screen->cmd_parser_version) < 0) {
1779 screen->cmd_parser_version = 0;
1780 }
1781
1782 if (screen->devinfo.gen >= 8 || screen->cmd_parser_version >= 2)
1783 screen->kernel_features |= KERNEL_ALLOWS_PREDICATE_WRITES;
1784
1785 /* Haswell requires command parser version 4 in order to have L3
1786 * atomic scratch1 and chicken3 bits
1787 */
1788 if (screen->devinfo.is_haswell && screen->cmd_parser_version >= 4) {
1789 screen->kernel_features |=
1790 KERNEL_ALLOWS_HSW_SCRATCH1_AND_ROW_CHICKEN3;
1791 }
1792
1793 /* Haswell requires command parser version 6 in order to write to the
1794 * MI_MATH GPR registers, and version 7 in order to use
1795 * MI_LOAD_REGISTER_REG (which all users of MI_MATH use).
1796 */
1797 if (screen->devinfo.gen >= 8 ||
1798 (screen->devinfo.is_haswell && screen->cmd_parser_version >= 7)) {
1799 screen->kernel_features |= KERNEL_ALLOWS_MI_MATH_AND_LRR;
1800 }
1801
1802 /* Gen7 needs at least command parser version 5 to support compute */
1803 if (screen->devinfo.gen >= 8 || screen->cmd_parser_version >= 5)
1804 screen->kernel_features |= KERNEL_ALLOWS_COMPUTE_DISPATCH;
1805
1806 dri_screen->extensions = !screen->has_context_reset_notification
1807 ? screenExtensions : intelRobustScreenExtensions;
1808
1809 screen->compiler = brw_compiler_create(screen,
1810 &screen->devinfo);
1811 screen->compiler->shader_debug_log = shader_debug_log_mesa;
1812 screen->compiler->shader_perf_log = shader_perf_log_mesa;
1813 screen->program_id = 1;
1814
1815 if (screen->devinfo.has_resource_streamer) {
1816 screen->has_resource_streamer =
1817 intel_get_boolean(screen, I915_PARAM_HAS_RESOURCE_STREAMER);
1818 }
1819
1820 return (const __DRIconfig**) intel_screen_make_configs(dri_screen);
1821 }
1822
1823 struct intel_buffer {
1824 __DRIbuffer base;
1825 drm_intel_bo *bo;
1826 };
1827
1828 static __DRIbuffer *
1829 intelAllocateBuffer(__DRIscreen *dri_screen,
1830 unsigned attachment, unsigned format,
1831 int width, int height)
1832 {
1833 struct intel_buffer *intelBuffer;
1834 struct intel_screen *screen = dri_screen->driverPrivate;
1835
1836 assert(attachment == __DRI_BUFFER_FRONT_LEFT ||
1837 attachment == __DRI_BUFFER_BACK_LEFT);
1838
1839 intelBuffer = calloc(1, sizeof *intelBuffer);
1840 if (intelBuffer == NULL)
1841 return NULL;
1842
1843 /* The front and back buffers are color buffers, which are X tiled. */
1844 uint32_t tiling = I915_TILING_X;
1845 unsigned long pitch;
1846 int cpp = format / 8;
1847 intelBuffer->bo = drm_intel_bo_alloc_tiled(screen->bufmgr,
1848 "intelAllocateBuffer",
1849 width,
1850 height,
1851 cpp,
1852 &tiling, &pitch,
1853 BO_ALLOC_FOR_RENDER);
1854
1855 if (intelBuffer->bo == NULL) {
1856 free(intelBuffer);
1857 return NULL;
1858 }
1859
1860 drm_intel_bo_flink(intelBuffer->bo, &intelBuffer->base.name);
1861
1862 intelBuffer->base.attachment = attachment;
1863 intelBuffer->base.cpp = cpp;
1864 intelBuffer->base.pitch = pitch;
1865
1866 return &intelBuffer->base;
1867 }
1868
1869 static void
1870 intelReleaseBuffer(__DRIscreen *dri_screen, __DRIbuffer *buffer)
1871 {
1872 struct intel_buffer *intelBuffer = (struct intel_buffer *) buffer;
1873
1874 drm_intel_bo_unreference(intelBuffer->bo);
1875 free(intelBuffer);
1876 }
1877
1878 static const struct __DriverAPIRec brw_driver_api = {
1879 .InitScreen = intelInitScreen2,
1880 .DestroyScreen = intelDestroyScreen,
1881 .CreateContext = brwCreateContext,
1882 .DestroyContext = intelDestroyContext,
1883 .CreateBuffer = intelCreateBuffer,
1884 .DestroyBuffer = intelDestroyBuffer,
1885 .MakeCurrent = intelMakeCurrent,
1886 .UnbindContext = intelUnbindContext,
1887 .AllocateBuffer = intelAllocateBuffer,
1888 .ReleaseBuffer = intelReleaseBuffer
1889 };
1890
1891 static const struct __DRIDriverVtableExtensionRec brw_vtable = {
1892 .base = { __DRI_DRIVER_VTABLE, 1 },
1893 .vtable = &brw_driver_api,
1894 };
1895
1896 static const __DRIextension *brw_driver_extensions[] = {
1897 &driCoreExtension.base,
1898 &driImageDriverExtension.base,
1899 &driDRI2Extension.base,
1900 &brw_vtable.base,
1901 &brw_config_options.base,
1902 NULL
1903 };
1904
1905 PUBLIC const __DRIextension **__driDriverGetExtensions_i965(void)
1906 {
1907 globalDriverAPI = &brw_driver_api;
1908
1909 return brw_driver_extensions;
1910 }