dri: Use DRM_FORMAT_* instead of defining our own copy.
[mesa.git] / src / mesa / drivers / dri / i915 / intel_screen.c
1 /**************************************************************************
2 *
3 * Copyright 2003 VMware, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 #include <errno.h>
29 #include <time.h>
30 #include <unistd.h>
31 #include "drm-uapi/drm_fourcc.h"
32 #include "main/glheader.h"
33 #include "main/context.h"
34 #include "main/framebuffer.h"
35 #include "main/renderbuffer.h"
36 #include "main/texobj.h"
37 #include "main/hash.h"
38 #include "main/fbobject.h"
39 #include "main/version.h"
40 #include "swrast/s_renderbuffer.h"
41
42 #include "utils.h"
43 #include "util/xmlpool.h"
44
45 static const __DRIconfigOptionsExtension i915_config_options = {
46 .base = { __DRI_CONFIG_OPTIONS, 1 },
47 .xml =
48
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
61 DRI_CONF_OPT_BEGIN_B(fragment_shader, "true")
62 DRI_CONF_DESC(en, "Enable limited ARB_fragment_shader support on 915/945.")
63 DRI_CONF_OPT_END
64
65 DRI_CONF_SECTION_END
66 DRI_CONF_SECTION_QUALITY
67 DRI_CONF_SECTION_END
68 DRI_CONF_SECTION_DEBUG
69 DRI_CONF_ALWAYS_FLUSH_BATCH("false")
70 DRI_CONF_ALWAYS_FLUSH_CACHE("false")
71 DRI_CONF_DISABLE_THROTTLING("false")
72 DRI_CONF_FORCE_GLSL_EXTENSIONS_WARN("false")
73 DRI_CONF_DISABLE_GLSL_LINE_CONTINUATIONS("false")
74 DRI_CONF_DISABLE_BLEND_FUNC_EXTENDED("false")
75
76 DRI_CONF_OPT_BEGIN_B(stub_occlusion_query, "false")
77 DRI_CONF_DESC(en, "Enable stub ARB_occlusion_query support on 915/945.")
78 DRI_CONF_OPT_END
79
80 DRI_CONF_OPT_BEGIN_B(shader_precompile, "true")
81 DRI_CONF_DESC(en, "Perform code generation at shader link time.")
82 DRI_CONF_OPT_END
83 DRI_CONF_SECTION_END
84 DRI_CONF_END
85 };
86
87 #include "intel_batchbuffer.h"
88 #include "intel_buffers.h"
89 #include "intel_bufmgr.h"
90 #include "intel_chipset.h"
91 #include "intel_fbo.h"
92 #include "intel_mipmap_tree.h"
93 #include "intel_screen.h"
94 #include "intel_tex.h"
95 #include "intel_regions.h"
96
97 #include "drm-uapi/i915_drm.h"
98
99 /**
100 * For debugging purposes, this returns a time in seconds.
101 */
102 double
103 get_time(void)
104 {
105 struct timespec tp;
106
107 clock_gettime(CLOCK_MONOTONIC, &tp);
108
109 return tp.tv_sec + tp.tv_nsec / 1000000000.0;
110 }
111
112 void
113 aub_dump_bmp(struct gl_context *ctx)
114 {
115 struct gl_framebuffer *fb = ctx->DrawBuffer;
116
117 for (int i = 0; i < fb->_NumColorDrawBuffers; i++) {
118 struct intel_renderbuffer *irb =
119 intel_renderbuffer(fb->_ColorDrawBuffers[i]);
120
121 if (irb && irb->mt) {
122 enum aub_dump_bmp_format format;
123
124 switch (irb->Base.Base.Format) {
125 case MESA_FORMAT_B8G8R8A8_UNORM:
126 case MESA_FORMAT_B8G8R8X8_UNORM:
127 format = AUB_DUMP_BMP_FORMAT_ARGB_8888;
128 break;
129 default:
130 continue;
131 }
132
133 assert(irb->mt->region->pitch % irb->mt->region->cpp == 0);
134 drm_intel_gem_bo_aub_dump_bmp(irb->mt->region->bo,
135 irb->draw_x,
136 irb->draw_y,
137 irb->Base.Base.Width,
138 irb->Base.Base.Height,
139 format,
140 irb->mt->region->pitch,
141 0);
142 }
143 }
144 }
145
146 static const __DRItexBufferExtension intelTexBufferExtension = {
147 .base = { __DRI_TEX_BUFFER, 3 },
148
149 .setTexBuffer = intelSetTexBuffer,
150 .setTexBuffer2 = intelSetTexBuffer2,
151 .releaseTexBuffer = NULL,
152 };
153
154 static void
155 intelDRI2Flush(__DRIdrawable *drawable)
156 {
157 GET_CURRENT_CONTEXT(ctx);
158 struct intel_context *intel = intel_context(ctx);
159 if (intel == NULL)
160 return;
161
162 INTEL_FIREVERTICES(intel);
163
164 intel->need_throttle = true;
165
166 if (intel->batch.used)
167 intel_batchbuffer_flush(intel);
168
169 if (INTEL_DEBUG & DEBUG_AUB) {
170 aub_dump_bmp(ctx);
171 }
172 }
173
174 static const struct __DRI2flushExtensionRec intelFlushExtension = {
175 .base = { __DRI2_FLUSH, 3 },
176
177 .flush = intelDRI2Flush,
178 .invalidate = dri2InvalidateDrawable,
179 };
180
181 static struct intel_image_format intel_image_formats[] = {
182 { DRM_FORMAT_ARGB8888, __DRI_IMAGE_COMPONENTS_RGBA, 1,
183 { { 0, 0, 0, __DRI_IMAGE_FORMAT_ARGB8888, 4 } } },
184
185 { __DRI_IMAGE_FOURCC_SARGB8888, __DRI_IMAGE_COMPONENTS_RGBA, 1,
186 { { 0, 0, 0, __DRI_IMAGE_FORMAT_SARGB8, 4 } } },
187
188 { DRM_FORMAT_XRGB8888, __DRI_IMAGE_COMPONENTS_RGB, 1,
189 { { 0, 0, 0, __DRI_IMAGE_FORMAT_XRGB8888, 4 }, } },
190
191 { DRM_FORMAT_YUV410, __DRI_IMAGE_COMPONENTS_Y_U_V, 3,
192 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
193 { 1, 2, 2, __DRI_IMAGE_FORMAT_R8, 1 },
194 { 2, 2, 2, __DRI_IMAGE_FORMAT_R8, 1 } } },
195
196 { DRM_FORMAT_YUV411, __DRI_IMAGE_COMPONENTS_Y_U_V, 3,
197 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
198 { 1, 2, 0, __DRI_IMAGE_FORMAT_R8, 1 },
199 { 2, 2, 0, __DRI_IMAGE_FORMAT_R8, 1 } } },
200
201 { DRM_FORMAT_YUV420, __DRI_IMAGE_COMPONENTS_Y_U_V, 3,
202 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
203 { 1, 1, 1, __DRI_IMAGE_FORMAT_R8, 1 },
204 { 2, 1, 1, __DRI_IMAGE_FORMAT_R8, 1 } } },
205
206 { DRM_FORMAT_YUV422, __DRI_IMAGE_COMPONENTS_Y_U_V, 3,
207 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
208 { 1, 1, 0, __DRI_IMAGE_FORMAT_R8, 1 },
209 { 2, 1, 0, __DRI_IMAGE_FORMAT_R8, 1 } } },
210
211 { DRM_FORMAT_YUV444, __DRI_IMAGE_COMPONENTS_Y_U_V, 3,
212 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
213 { 1, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
214 { 2, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 } } },
215
216 { DRM_FORMAT_NV12, __DRI_IMAGE_COMPONENTS_Y_UV, 2,
217 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
218 { 1, 1, 1, __DRI_IMAGE_FORMAT_GR88, 2 } } },
219
220 { DRM_FORMAT_NV16, __DRI_IMAGE_COMPONENTS_Y_UV, 2,
221 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
222 { 1, 1, 0, __DRI_IMAGE_FORMAT_GR88, 2 } } },
223
224 /* For YUYV and UYVY buffers, we set up two overlapping DRI images
225 * and treat them as planar buffers in the compositors.
226 * Plane 0 is GR88 and samples YU or YV pairs and places Y into
227 * the R component, while plane 1 is ARGB/ABGR and samples YUYV/UYVY
228 * clusters and places pairs and places U into the G component and
229 * V into A. This lets the texture sampler interpolate the Y
230 * components correctly when sampling from plane 0, and interpolate
231 * U and V correctly when sampling from plane 1. */
232 { DRM_FORMAT_YUYV, __DRI_IMAGE_COMPONENTS_Y_XUXV, 2,
233 { { 0, 0, 0, __DRI_IMAGE_FORMAT_GR88, 2 },
234 { 0, 1, 0, __DRI_IMAGE_FORMAT_ARGB8888, 4 } } },
235 { DRM_FORMAT_UYVY, __DRI_IMAGE_COMPONENTS_Y_UXVX, 2,
236 { { 0, 0, 0, __DRI_IMAGE_FORMAT_GR88, 2 },
237 { 0, 1, 0, __DRI_IMAGE_FORMAT_ABGR8888, 4 } } }
238 };
239
240 static __DRIimage *
241 intel_allocate_image(int dri_format, void *loaderPrivate)
242 {
243 __DRIimage *image;
244
245 image = calloc(1, sizeof *image);
246 if (image == NULL)
247 return NULL;
248
249 image->dri_format = dri_format;
250 image->offset = 0;
251
252 image->format = driImageFormatToGLFormat(dri_format);
253 if (dri_format != __DRI_IMAGE_FORMAT_NONE &&
254 image->format == MESA_FORMAT_NONE) {
255 free(image);
256 return NULL;
257 }
258
259 image->internal_format = _mesa_get_format_base_format(image->format);
260 image->data = loaderPrivate;
261
262 return image;
263 }
264
265 /**
266 * Sets up a DRIImage structure to point to our shared image in a region
267 */
268 static void
269 intel_setup_image_from_mipmap_tree(struct intel_context *intel, __DRIimage *image,
270 struct intel_mipmap_tree *mt, GLuint level,
271 GLuint zoffset)
272 {
273 unsigned int draw_x, draw_y;
274 uint32_t mask_x, mask_y;
275
276 intel_miptree_check_level_layer(mt, level, zoffset);
277
278 intel_region_get_tile_masks(mt->region, &mask_x, &mask_y);
279 intel_miptree_get_image_offset(mt, level, zoffset, &draw_x, &draw_y);
280
281 image->width = mt->level[level].width;
282 image->height = mt->level[level].height;
283 image->tile_x = draw_x & mask_x;
284 image->tile_y = draw_y & mask_y;
285
286 image->offset = intel_region_get_aligned_offset(mt->region,
287 draw_x & ~mask_x,
288 draw_y & ~mask_y);
289
290 intel_region_reference(&image->region, mt->region);
291 }
292
293 static void
294 intel_setup_image_from_dimensions(__DRIimage *image)
295 {
296 image->width = image->region->width;
297 image->height = image->region->height;
298 image->tile_x = 0;
299 image->tile_y = 0;
300 }
301
302 static __DRIimage *
303 intel_create_image_from_name(__DRIscreen *screen,
304 int width, int height, int format,
305 int name, int pitch, void *loaderPrivate)
306 {
307 struct intel_screen *intelScreen = screen->driverPrivate;
308 __DRIimage *image;
309 int cpp;
310
311 image = intel_allocate_image(format, loaderPrivate);
312 if (image == NULL)
313 return NULL;
314
315 if (image->format == MESA_FORMAT_NONE)
316 cpp = 1;
317 else
318 cpp = _mesa_get_format_bytes(image->format);
319 image->region = intel_region_alloc_for_handle(intelScreen,
320 cpp, width, height,
321 pitch * cpp, name, "image");
322 if (image->region == NULL) {
323 free(image);
324 return NULL;
325 }
326
327 intel_setup_image_from_dimensions(image);
328
329 return image;
330 }
331
332 static __DRIimage *
333 intel_create_image_from_renderbuffer(__DRIcontext *context,
334 int renderbuffer, void *loaderPrivate)
335 {
336 __DRIimage *image;
337 struct intel_context *intel = context->driverPrivate;
338 struct gl_renderbuffer *rb;
339 struct intel_renderbuffer *irb;
340
341 rb = _mesa_lookup_renderbuffer(&intel->ctx, renderbuffer);
342 if (!rb) {
343 _mesa_error(&intel->ctx,
344 GL_INVALID_OPERATION, "glRenderbufferExternalMESA");
345 return NULL;
346 }
347
348 irb = intel_renderbuffer(rb);
349 image = calloc(1, sizeof *image);
350 if (image == NULL)
351 return NULL;
352
353 image->internal_format = rb->InternalFormat;
354 image->format = rb->Format;
355 image->offset = 0;
356 image->data = loaderPrivate;
357 intel_region_reference(&image->region, irb->mt->region);
358 intel_setup_image_from_dimensions(image);
359 image->dri_format = driGLFormatToImageFormat(image->format);
360
361 rb->NeedsFinishRenderTexture = true;
362 return image;
363 }
364
365 static __DRIimage *
366 intel_create_image_from_texture(__DRIcontext *context, int target,
367 unsigned texture, int zoffset,
368 int level,
369 unsigned *error,
370 void *loaderPrivate)
371 {
372 __DRIimage *image;
373 struct intel_context *intel = context->driverPrivate;
374 struct gl_texture_object *obj;
375 struct intel_texture_object *iobj;
376 GLuint face = 0;
377
378 obj = _mesa_lookup_texture(&intel->ctx, texture);
379 if (!obj || obj->Target != target) {
380 *error = __DRI_IMAGE_ERROR_BAD_PARAMETER;
381 return NULL;
382 }
383
384 if (target == GL_TEXTURE_CUBE_MAP)
385 face = zoffset;
386
387 _mesa_test_texobj_completeness(&intel->ctx, obj);
388 iobj = intel_texture_object(obj);
389 if (!obj->_BaseComplete || (level > 0 && !obj->_MipmapComplete)) {
390 *error = __DRI_IMAGE_ERROR_BAD_PARAMETER;
391 return NULL;
392 }
393
394 if (level < obj->BaseLevel || level > obj->_MaxLevel) {
395 *error = __DRI_IMAGE_ERROR_BAD_MATCH;
396 return NULL;
397 }
398
399 if (target == GL_TEXTURE_3D && obj->Image[face][level]->Depth < zoffset) {
400 *error = __DRI_IMAGE_ERROR_BAD_MATCH;
401 return NULL;
402 }
403 image = calloc(1, sizeof *image);
404 if (image == NULL) {
405 *error = __DRI_IMAGE_ERROR_BAD_ALLOC;
406 return NULL;
407 }
408
409 image->internal_format = obj->Image[face][level]->InternalFormat;
410 image->format = obj->Image[face][level]->TexFormat;
411 image->data = loaderPrivate;
412 intel_setup_image_from_mipmap_tree(intel, image, iobj->mt, level, zoffset);
413 image->dri_format = driGLFormatToImageFormat(image->format);
414 if (image->dri_format == __DRI_IMAGE_FORMAT_NONE) {
415 *error = __DRI_IMAGE_ERROR_BAD_PARAMETER;
416 free(image);
417 return NULL;
418 }
419
420 *error = __DRI_IMAGE_ERROR_SUCCESS;
421 return image;
422 }
423
424 static void
425 intel_destroy_image(__DRIimage *image)
426 {
427 intel_region_release(&image->region);
428 free(image);
429 }
430
431 static __DRIimage *
432 intel_create_image(__DRIscreen *screen,
433 int width, int height, int format,
434 unsigned int use,
435 void *loaderPrivate)
436 {
437 __DRIimage *image;
438 struct intel_screen *intelScreen = screen->driverPrivate;
439 uint32_t tiling;
440 int cpp;
441
442 tiling = I915_TILING_X;
443 if (use & __DRI_IMAGE_USE_CURSOR) {
444 if (width != 64 || height != 64)
445 return NULL;
446 tiling = I915_TILING_NONE;
447 }
448
449 if (use & __DRI_IMAGE_USE_LINEAR)
450 tiling = I915_TILING_NONE;
451
452 image = intel_allocate_image(format, loaderPrivate);
453 if (image == NULL)
454 return NULL;
455
456 cpp = _mesa_get_format_bytes(image->format);
457 image->region =
458 intel_region_alloc(intelScreen, tiling, cpp, width, height, true);
459 if (image->region == NULL) {
460 free(image);
461 return NULL;
462 }
463
464 intel_setup_image_from_dimensions(image);
465
466 return image;
467 }
468
469 static GLboolean
470 intel_query_image(__DRIimage *image, int attrib, int *value)
471 {
472 switch (attrib) {
473 case __DRI_IMAGE_ATTRIB_STRIDE:
474 *value = image->region->pitch;
475 return true;
476 case __DRI_IMAGE_ATTRIB_HANDLE:
477 *value = image->region->bo->handle;
478 return true;
479 case __DRI_IMAGE_ATTRIB_NAME:
480 return intel_region_flink(image->region, (uint32_t *) value);
481 case __DRI_IMAGE_ATTRIB_FORMAT:
482 *value = image->dri_format;
483 return true;
484 case __DRI_IMAGE_ATTRIB_WIDTH:
485 *value = image->region->width;
486 return true;
487 case __DRI_IMAGE_ATTRIB_HEIGHT:
488 *value = image->region->height;
489 return true;
490 case __DRI_IMAGE_ATTRIB_COMPONENTS:
491 if (image->planar_format == NULL)
492 return false;
493 *value = image->planar_format->components;
494 return true;
495 case __DRI_IMAGE_ATTRIB_FD:
496 return !drm_intel_bo_gem_export_to_prime(image->region->bo, value);
497 default:
498 return false;
499 }
500 }
501
502 static __DRIimage *
503 intel_dup_image(__DRIimage *orig_image, void *loaderPrivate)
504 {
505 __DRIimage *image;
506
507 image = calloc(1, sizeof *image);
508 if (image == NULL)
509 return NULL;
510
511 intel_region_reference(&image->region, orig_image->region);
512 if (image->region == NULL) {
513 free(image);
514 return NULL;
515 }
516
517 image->internal_format = orig_image->internal_format;
518 image->planar_format = orig_image->planar_format;
519 image->dri_format = orig_image->dri_format;
520 image->format = orig_image->format;
521 image->offset = orig_image->offset;
522 image->width = orig_image->width;
523 image->height = orig_image->height;
524 image->tile_x = orig_image->tile_x;
525 image->tile_y = orig_image->tile_y;
526 image->data = loaderPrivate;
527
528 memcpy(image->strides, orig_image->strides, sizeof(image->strides));
529 memcpy(image->offsets, orig_image->offsets, sizeof(image->offsets));
530
531 return image;
532 }
533
534 static GLboolean
535 intel_validate_usage(__DRIimage *image, unsigned int use)
536 {
537 if (use & __DRI_IMAGE_USE_CURSOR) {
538 if (image->region->width != 64 || image->region->height != 64)
539 return GL_FALSE;
540 }
541
542 return GL_TRUE;
543 }
544
545 static __DRIimage *
546 intel_create_image_from_names(__DRIscreen *screen,
547 int width, int height, int fourcc,
548 int *names, int num_names,
549 int *strides, int *offsets,
550 void *loaderPrivate)
551 {
552 struct intel_image_format *f = NULL;
553 __DRIimage *image;
554 int i, index;
555
556 if (screen == NULL || names == NULL || num_names != 1)
557 return NULL;
558
559 for (i = 0; i < ARRAY_SIZE(intel_image_formats); i++) {
560 if (intel_image_formats[i].fourcc == fourcc) {
561 f = &intel_image_formats[i];
562 }
563 }
564
565 if (f == NULL)
566 return NULL;
567
568 image = intel_create_image_from_name(screen, width, height,
569 __DRI_IMAGE_FORMAT_NONE,
570 names[0], strides[0],
571 loaderPrivate);
572
573 if (image == NULL)
574 return NULL;
575
576 image->planar_format = f;
577 for (i = 0; i < f->nplanes; i++) {
578 index = f->planes[i].buffer_index;
579 image->offsets[index] = offsets[index];
580 image->strides[index] = strides[index];
581 }
582
583 return image;
584 }
585
586 static __DRIimage *
587 intel_create_image_from_fds(__DRIscreen *screen,
588 int width, int height, int fourcc,
589 int *fds, int num_fds, int *strides, int *offsets,
590 void *loaderPrivate)
591 {
592 struct intel_screen *intelScreen = screen->driverPrivate;
593 struct intel_image_format *f = NULL;
594 __DRIimage *image;
595 int i, index;
596
597 if (fds == NULL || num_fds != 1)
598 return NULL;
599
600 for (i = 0; i < ARRAY_SIZE(intel_image_formats); i++) {
601 if (intel_image_formats[i].fourcc == fourcc) {
602 f = &intel_image_formats[i];
603 }
604 }
605
606 if (f == NULL)
607 return NULL;
608
609 image = intel_allocate_image(__DRI_IMAGE_FORMAT_NONE, loaderPrivate);
610 if (image == NULL)
611 return NULL;
612
613 image->region = intel_region_alloc_for_fd(intelScreen,
614 f->planes[0].cpp, width, height, strides[0],
615 height * strides[0], fds[0], "image");
616 if (image->region == NULL) {
617 free(image);
618 return NULL;
619 }
620
621 intel_setup_image_from_dimensions(image);
622
623 image->planar_format = f;
624 for (i = 0; i < f->nplanes; i++) {
625 index = f->planes[i].buffer_index;
626 image->offsets[index] = offsets[index];
627 image->strides[index] = strides[index];
628 }
629
630 return image;
631 }
632
633
634 static __DRIimage *
635 intel_from_planar(__DRIimage *parent, int plane, void *loaderPrivate)
636 {
637 int width, height, offset, stride, dri_format, index;
638 struct intel_image_format *f;
639 uint32_t mask_x, mask_y;
640 __DRIimage *image;
641
642 if (parent == NULL || parent->planar_format == NULL)
643 return NULL;
644
645 f = parent->planar_format;
646
647 if (plane >= f->nplanes)
648 return NULL;
649
650 width = parent->region->width >> f->planes[plane].width_shift;
651 height = parent->region->height >> f->planes[plane].height_shift;
652 dri_format = f->planes[plane].dri_format;
653 index = f->planes[plane].buffer_index;
654 offset = parent->offsets[index];
655 stride = parent->strides[index];
656
657 image = intel_allocate_image(dri_format, loaderPrivate);
658 if (image == NULL)
659 return NULL;
660
661 if (offset + height * stride > parent->region->bo->size) {
662 _mesa_warning(NULL, "intel_create_sub_image: subimage out of bounds");
663 free(image);
664 return NULL;
665 }
666
667 image->region = calloc(sizeof(*image->region), 1);
668 if (image->region == NULL) {
669 free(image);
670 return NULL;
671 }
672
673 image->region->cpp = _mesa_get_format_bytes(image->format);
674 image->region->width = width;
675 image->region->height = height;
676 image->region->pitch = stride;
677 image->region->refcount = 1;
678 image->region->bo = parent->region->bo;
679 drm_intel_bo_reference(image->region->bo);
680 image->region->tiling = parent->region->tiling;
681 image->offset = offset;
682 intel_setup_image_from_dimensions(image);
683
684 intel_region_get_tile_masks(image->region, &mask_x, &mask_y);
685 if (offset & mask_x)
686 _mesa_warning(NULL,
687 "intel_create_sub_image: offset not on tile boundary");
688
689 return image;
690 }
691
692 static const __DRIimageExtension intelImageExtension = {
693 .base = { __DRI_IMAGE, 7 },
694
695 .createImageFromName = intel_create_image_from_name,
696 .createImageFromRenderbuffer = intel_create_image_from_renderbuffer,
697 .destroyImage = intel_destroy_image,
698 .createImage = intel_create_image,
699 .queryImage = intel_query_image,
700 .dupImage = intel_dup_image,
701 .validateUsage = intel_validate_usage,
702 .createImageFromNames = intel_create_image_from_names,
703 .fromPlanar = intel_from_planar,
704 .createImageFromTexture = intel_create_image_from_texture,
705 .createImageFromFds = intel_create_image_from_fds
706 };
707
708 static int
709 i915_query_renderer_integer(__DRIscreen *psp, int param, unsigned int *value)
710 {
711 const struct intel_screen *const intelScreen =
712 (struct intel_screen *) psp->driverPrivate;
713
714 switch (param) {
715 case __DRI2_RENDERER_VENDOR_ID:
716 value[0] = 0x8086;
717 return 0;
718 case __DRI2_RENDERER_DEVICE_ID:
719 value[0] = intelScreen->deviceID;
720 return 0;
721 case __DRI2_RENDERER_ACCELERATED:
722 value[0] = 1;
723 return 0;
724 case __DRI2_RENDERER_VIDEO_MEMORY: {
725 /* Once a batch uses more than 75% of the maximum mappable size, we
726 * assume that there's some fragmentation, and we start doing extra
727 * flushing, etc. That's the big cliff apps will care about.
728 */
729 size_t aper_size;
730 size_t mappable_size;
731
732 drm_intel_get_aperture_sizes(psp->fd, &mappable_size, &aper_size);
733
734 const unsigned gpu_mappable_megabytes =
735 (aper_size / (1024 * 1024)) * 3 / 4;
736
737 const long system_memory_pages = sysconf(_SC_PHYS_PAGES);
738 const long system_page_size = sysconf(_SC_PAGE_SIZE);
739
740 if (system_memory_pages <= 0 || system_page_size <= 0)
741 return -1;
742
743 const uint64_t system_memory_bytes = (uint64_t) system_memory_pages
744 * (uint64_t) system_page_size;
745
746 const unsigned system_memory_megabytes =
747 (unsigned) (system_memory_bytes / (1024 * 1024));
748
749 value[0] = MIN2(system_memory_megabytes, gpu_mappable_megabytes);
750 return 0;
751 }
752 case __DRI2_RENDERER_UNIFIED_MEMORY_ARCHITECTURE:
753 value[0] = 1;
754 return 0;
755 case __DRI2_RENDERER_HAS_TEXTURE_3D:
756 value[0] = 1;
757 return 0;
758 default:
759 return driQueryRendererIntegerCommon(psp, param, value);
760 }
761
762 return -1;
763 }
764
765 static int
766 i915_query_renderer_string(__DRIscreen *psp, int param, const char **value)
767 {
768 const struct intel_screen *intelScreen =
769 (struct intel_screen *) psp->driverPrivate;
770
771 switch (param) {
772 case __DRI2_RENDERER_VENDOR_ID:
773 value[0] = i915_vendor_string;
774 return 0;
775 case __DRI2_RENDERER_DEVICE_ID:
776 value[0] = i915_get_renderer_string(intelScreen->deviceID);
777 return 0;
778 default:
779 break;
780 }
781
782 return -1;
783 }
784
785 static const __DRI2rendererQueryExtension intelRendererQueryExtension = {
786 .base = { __DRI2_RENDERER_QUERY, 1 },
787
788 .queryInteger = i915_query_renderer_integer,
789 .queryString = i915_query_renderer_string
790 };
791
792 static const __DRIextension *intelScreenExtensions[] = {
793 &intelTexBufferExtension.base,
794 &intelFenceExtension.base,
795 &intelFlushExtension.base,
796 &intelImageExtension.base,
797 &intelRendererQueryExtension.base,
798 &dri2ConfigQueryExtension.base,
799 &dri2NoErrorExtension.base,
800 NULL
801 };
802
803 static bool
804 intel_get_param(__DRIscreen *psp, int param, int *value)
805 {
806 int ret;
807 struct drm_i915_getparam gp;
808
809 memset(&gp, 0, sizeof(gp));
810 gp.param = param;
811 gp.value = value;
812
813 ret = drmCommandWriteRead(psp->fd, DRM_I915_GETPARAM, &gp, sizeof(gp));
814 if (ret) {
815 if (ret != -EINVAL)
816 _mesa_warning(NULL, "drm_i915_getparam: %d", ret);
817 return false;
818 }
819
820 return true;
821 }
822
823 static bool
824 intel_get_boolean(__DRIscreen *psp, int param)
825 {
826 int value = 0;
827 return intel_get_param(psp, param, &value) && value;
828 }
829
830 static void
831 intelDestroyScreen(__DRIscreen * sPriv)
832 {
833 struct intel_screen *intelScreen = sPriv->driverPrivate;
834
835 dri_bufmgr_destroy(intelScreen->bufmgr);
836 driDestroyOptionInfo(&intelScreen->optionCache);
837
838 free(intelScreen);
839 sPriv->driverPrivate = NULL;
840 }
841
842
843 /**
844 * This is called when we need to set up GL rendering to a new X window.
845 */
846 static GLboolean
847 intelCreateBuffer(__DRIscreen * driScrnPriv,
848 __DRIdrawable * driDrawPriv,
849 const struct gl_config * mesaVis, GLboolean isPixmap)
850 {
851 struct intel_renderbuffer *rb;
852 mesa_format rgbFormat;
853 struct gl_framebuffer *fb;
854
855 if (isPixmap)
856 return false;
857
858 fb = CALLOC_STRUCT(gl_framebuffer);
859 if (!fb)
860 return false;
861
862 _mesa_initialize_window_framebuffer(fb, mesaVis);
863
864 if (mesaVis->redBits == 5)
865 rgbFormat = MESA_FORMAT_B5G6R5_UNORM;
866 else if (mesaVis->sRGBCapable)
867 rgbFormat = MESA_FORMAT_B8G8R8A8_SRGB;
868 else if (mesaVis->alphaBits == 0)
869 rgbFormat = MESA_FORMAT_B8G8R8X8_UNORM;
870 else
871 rgbFormat = MESA_FORMAT_B8G8R8A8_UNORM;
872
873 /* setup the hardware-based renderbuffers */
874 rb = intel_create_renderbuffer(rgbFormat);
875 _mesa_attach_and_own_rb(fb, BUFFER_FRONT_LEFT, &rb->Base.Base);
876
877 if (mesaVis->doubleBufferMode) {
878 rb = intel_create_renderbuffer(rgbFormat);
879 _mesa_attach_and_own_rb(fb, BUFFER_BACK_LEFT, &rb->Base.Base);
880 }
881
882 /*
883 * Assert here that the gl_config has an expected depth/stencil bit
884 * combination: one of d24/s8, d16/s0, d0/s0. (See intelInitScreen2(),
885 * which constructs the advertised configs.)
886 */
887 if (mesaVis->depthBits == 24) {
888 assert(mesaVis->stencilBits == 8);
889
890 /*
891 * Use combined depth/stencil. Note that the renderbuffer is
892 * attached to two attachment points.
893 */
894 rb = intel_create_private_renderbuffer(MESA_FORMAT_Z24_UNORM_S8_UINT);
895 _mesa_attach_and_own_rb(fb, BUFFER_DEPTH, &rb->Base.Base);
896 _mesa_attach_and_reference_rb(fb, BUFFER_STENCIL, &rb->Base.Base);
897 }
898 else if (mesaVis->depthBits == 16) {
899 assert(mesaVis->stencilBits == 0);
900 rb = intel_create_private_renderbuffer(MESA_FORMAT_Z_UNORM16);
901 _mesa_attach_and_own_rb(fb, BUFFER_DEPTH, &rb->Base.Base);
902 }
903 else {
904 assert(mesaVis->depthBits == 0);
905 assert(mesaVis->stencilBits == 0);
906 }
907
908 /* now add any/all software-based renderbuffers we may need */
909 _swrast_add_soft_renderbuffers(fb,
910 false, /* never sw color */
911 false, /* never sw depth */
912 false, /* never sw stencil */
913 mesaVis->accumRedBits > 0,
914 false, /* never sw alpha */
915 false /* never sw aux */ );
916 driDrawPriv->driverPrivate = fb;
917
918 return true;
919 }
920
921 static void
922 intelDestroyBuffer(__DRIdrawable * driDrawPriv)
923 {
924 struct gl_framebuffer *fb = driDrawPriv->driverPrivate;
925
926 _mesa_reference_framebuffer(&fb, NULL);
927 }
928
929 /* There are probably better ways to do this, such as an
930 * init-designated function to register chipids and createcontext
931 * functions.
932 */
933 extern bool
934 i830CreateContext(int api,
935 const struct gl_config *mesaVis,
936 __DRIcontext *driContextPriv,
937 unsigned major_version,
938 unsigned minor_version,
939 uint32_t flags,
940 unsigned *error,
941 void *sharedContextPrivate);
942
943 extern bool
944 i915CreateContext(int api,
945 const struct gl_config *mesaVis,
946 __DRIcontext *driContextPriv,
947 unsigned major_version,
948 unsigned minor_version,
949 uint32_t flags,
950 unsigned *error,
951 void *sharedContextPrivate);
952
953 static GLboolean
954 intelCreateContext(gl_api api,
955 const struct gl_config * mesaVis,
956 __DRIcontext * driContextPriv,
957 const struct __DriverContextConfig *ctx_config,
958 unsigned *error,
959 void *sharedContextPrivate)
960 {
961 bool success = false;
962
963 __DRIscreen *sPriv = driContextPriv->driScreenPriv;
964 struct intel_screen *intelScreen = sPriv->driverPrivate;
965
966 if (ctx_config->flags & ~(__DRI_CTX_FLAG_DEBUG | __DRI_CTX_FLAG_NO_ERROR)) {
967 *error = __DRI_CTX_ERROR_UNKNOWN_FLAG;
968 return false;
969 }
970
971 if (ctx_config->attribute_mask) {
972 *error = __DRI_CTX_ERROR_UNKNOWN_ATTRIBUTE;
973 return false;
974 }
975
976 if (IS_GEN3(intelScreen->deviceID)) {
977 success = i915CreateContext(api, mesaVis, driContextPriv,
978 ctx_config->major_version,
979 ctx_config->minor_version,
980 ctx_config->flags,
981 error, sharedContextPrivate);
982 } else {
983 intelScreen->no_vbo = true;
984 success = i830CreateContext(api, mesaVis, driContextPriv,
985 ctx_config->major_version,
986 ctx_config->minor_version,
987 ctx_config->flags,
988 error, sharedContextPrivate);
989 }
990
991 if (success)
992 return true;
993
994 if (driContextPriv->driverPrivate != NULL)
995 intelDestroyContext(driContextPriv);
996
997 return false;
998 }
999
1000 static bool
1001 intel_init_bufmgr(struct intel_screen *intelScreen)
1002 {
1003 __DRIscreen *spriv = intelScreen->driScrnPriv;
1004
1005 intelScreen->no_hw = getenv("INTEL_NO_HW") != NULL;
1006
1007 intelScreen->bufmgr = intel_bufmgr_gem_init(spriv->fd, BATCH_SZ);
1008 if (intelScreen->bufmgr == NULL) {
1009 fprintf(stderr, "[%s:%u] Error initializing buffer manager.\n",
1010 __func__, __LINE__);
1011 return false;
1012 }
1013
1014 drm_intel_bufmgr_gem_enable_fenced_relocs(intelScreen->bufmgr);
1015
1016 if (!intel_get_boolean(spriv, I915_PARAM_HAS_RELAXED_DELTA)) {
1017 fprintf(stderr, "[%s: %u] Kernel 2.6.39 required.\n", __func__, __LINE__);
1018 return false;
1019 }
1020
1021 return true;
1022 }
1023
1024 static __DRIconfig**
1025 intel_screen_make_configs(__DRIscreen *dri_screen)
1026 {
1027 static const mesa_format formats[] = {
1028 MESA_FORMAT_B5G6R5_UNORM,
1029 MESA_FORMAT_B8G8R8A8_UNORM,
1030 MESA_FORMAT_B8G8R8X8_UNORM
1031 };
1032
1033 /* __DRI_ATTRIB_SWAP_COPY is not supported due to page flipping. */
1034 static const GLenum back_buffer_modes[] = {
1035 __DRI_ATTRIB_SWAP_UNDEFINED, __DRI_ATTRIB_SWAP_NONE
1036 };
1037
1038 static const uint8_t singlesample_samples[1] = {0};
1039
1040 uint8_t depth_bits[4], stencil_bits[4];
1041 __DRIconfig **configs = NULL;
1042
1043 /* Generate singlesample configs without accumulation buffer. */
1044 for (int i = 0; i < ARRAY_SIZE(formats); i++) {
1045 __DRIconfig **new_configs;
1046 int num_depth_stencil_bits = 2;
1047
1048 /* Starting with DRI2 protocol version 1.1 we can request a depth/stencil
1049 * buffer that has a different number of bits per pixel than the color
1050 * buffer.
1051 */
1052 depth_bits[0] = 0;
1053 stencil_bits[0] = 0;
1054
1055 if (formats[i] == MESA_FORMAT_B5G6R5_UNORM) {
1056 depth_bits[1] = 16;
1057 stencil_bits[1] = 0;
1058 } else {
1059 depth_bits[1] = 24;
1060 stencil_bits[1] = 8;
1061 }
1062
1063 new_configs = driCreateConfigs(formats[i],
1064 depth_bits,
1065 stencil_bits,
1066 num_depth_stencil_bits,
1067 back_buffer_modes, 2,
1068 singlesample_samples, 1,
1069 false, false, false);
1070 configs = driConcatConfigs(configs, new_configs);
1071 }
1072
1073 /* Generate the minimum possible set of configs that include an
1074 * accumulation buffer.
1075 */
1076 for (int i = 0; i < ARRAY_SIZE(formats); i++) {
1077 __DRIconfig **new_configs;
1078
1079 if (formats[i] == MESA_FORMAT_B5G6R5_UNORM) {
1080 depth_bits[0] = 16;
1081 stencil_bits[0] = 0;
1082 } else {
1083 depth_bits[0] = 24;
1084 stencil_bits[0] = 8;
1085 }
1086
1087 new_configs = driCreateConfigs(formats[i],
1088 depth_bits, stencil_bits, 1,
1089 back_buffer_modes, 1,
1090 singlesample_samples, 1,
1091 true, false, false);
1092 configs = driConcatConfigs(configs, new_configs);
1093 }
1094
1095 if (configs == NULL) {
1096 fprintf(stderr, "[%s:%u] Error creating FBConfig!\n", __func__,
1097 __LINE__);
1098 return NULL;
1099 }
1100
1101 return configs;
1102 }
1103
1104 static void
1105 set_max_gl_versions(struct intel_screen *screen)
1106 {
1107 __DRIscreen *psp = screen->driScrnPriv;
1108
1109 switch (screen->gen) {
1110 case 3: {
1111 bool has_fragment_shader = driQueryOptionb(&screen->optionCache, "fragment_shader");
1112 bool has_occlusion_query = driQueryOptionb(&screen->optionCache, "stub_occlusion_query");
1113
1114 psp->max_gl_core_version = 0;
1115 psp->max_gl_es1_version = 11;
1116 psp->max_gl_es2_version = 20;
1117
1118 if (has_fragment_shader && has_occlusion_query) {
1119 psp->max_gl_compat_version = 21;
1120 } else {
1121 psp->max_gl_compat_version = 14;
1122 }
1123 break;
1124 }
1125 case 2:
1126 psp->max_gl_core_version = 0;
1127 psp->max_gl_compat_version = 13;
1128 psp->max_gl_es1_version = 11;
1129 psp->max_gl_es2_version = 0;
1130 break;
1131 default:
1132 assert(!"unrecognized intel_screen::gen");
1133 break;
1134 }
1135 }
1136
1137 /**
1138 * This is the driver specific part of the createNewScreen entry point.
1139 * Called when using DRI2.
1140 *
1141 * \return the struct gl_config supported by this driver
1142 */
1143 static const
1144 __DRIconfig **intelInitScreen2(__DRIscreen *psp)
1145 {
1146 struct intel_screen *intelScreen;
1147
1148 if (psp->image.loader) {
1149 } else if (psp->dri2.loader->base.version <= 2 ||
1150 psp->dri2.loader->getBuffersWithFormat == NULL) {
1151 fprintf(stderr,
1152 "\nERROR! DRI2 loader with getBuffersWithFormat() "
1153 "support required\n");
1154 return false;
1155 }
1156
1157 /* Allocate the private area */
1158 intelScreen = calloc(1, sizeof *intelScreen);
1159 if (!intelScreen) {
1160 fprintf(stderr, "\nERROR! Allocating private area failed\n");
1161 return false;
1162 }
1163 /* parse information in __driConfigOptions */
1164 driParseOptionInfo(&intelScreen->optionCache, i915_config_options.xml);
1165
1166 intelScreen->driScrnPriv = psp;
1167 psp->driverPrivate = (void *) intelScreen;
1168
1169 if (!intel_init_bufmgr(intelScreen))
1170 return false;
1171
1172 intelScreen->deviceID = drm_intel_bufmgr_gem_get_devid(intelScreen->bufmgr);
1173
1174 if (IS_GEN3(intelScreen->deviceID)) {
1175 intelScreen->gen = 3;
1176 } else {
1177 intelScreen->gen = 2;
1178 }
1179
1180 set_max_gl_versions(intelScreen);
1181
1182 psp->extensions = intelScreenExtensions;
1183
1184 return (const __DRIconfig**) intel_screen_make_configs(psp);
1185 }
1186
1187 struct intel_buffer {
1188 __DRIbuffer base;
1189 struct intel_region *region;
1190 };
1191
1192 static __DRIbuffer *
1193 intelAllocateBuffer(__DRIscreen *screen,
1194 unsigned attachment, unsigned format,
1195 int width, int height)
1196 {
1197 struct intel_buffer *intelBuffer;
1198 struct intel_screen *intelScreen = screen->driverPrivate;
1199
1200 assert(attachment == __DRI_BUFFER_FRONT_LEFT ||
1201 attachment == __DRI_BUFFER_BACK_LEFT);
1202
1203 intelBuffer = calloc(1, sizeof *intelBuffer);
1204 if (intelBuffer == NULL)
1205 return NULL;
1206
1207 /* The front and back buffers are color buffers, which are X tiled. */
1208 intelBuffer->region = intel_region_alloc(intelScreen,
1209 I915_TILING_X,
1210 format / 8,
1211 width,
1212 height,
1213 true);
1214
1215 if (intelBuffer->region == NULL) {
1216 free(intelBuffer);
1217 return NULL;
1218 }
1219
1220 intel_region_flink(intelBuffer->region, &intelBuffer->base.name);
1221
1222 intelBuffer->base.attachment = attachment;
1223 intelBuffer->base.cpp = intelBuffer->region->cpp;
1224 intelBuffer->base.pitch = intelBuffer->region->pitch;
1225
1226 return &intelBuffer->base;
1227 }
1228
1229 static void
1230 intelReleaseBuffer(__DRIscreen *screen, __DRIbuffer *buffer)
1231 {
1232 struct intel_buffer *intelBuffer = (struct intel_buffer *) buffer;
1233
1234 intel_region_release(&intelBuffer->region);
1235 free(intelBuffer);
1236 }
1237
1238
1239 static const struct __DriverAPIRec i915_driver_api = {
1240 .InitScreen = intelInitScreen2,
1241 .DestroyScreen = intelDestroyScreen,
1242 .CreateContext = intelCreateContext,
1243 .DestroyContext = intelDestroyContext,
1244 .CreateBuffer = intelCreateBuffer,
1245 .DestroyBuffer = intelDestroyBuffer,
1246 .MakeCurrent = intelMakeCurrent,
1247 .UnbindContext = intelUnbindContext,
1248 .AllocateBuffer = intelAllocateBuffer,
1249 .ReleaseBuffer = intelReleaseBuffer
1250 };
1251
1252 static const struct __DRIDriverVtableExtensionRec i915_vtable = {
1253 .base = { __DRI_DRIVER_VTABLE, 1 },
1254 .vtable = &i915_driver_api,
1255 };
1256
1257 /* This is the table of extensions that the loader will dlsym() for. */
1258 static const __DRIextension *i915_driver_extensions[] = {
1259 &driCoreExtension.base,
1260 &driImageDriverExtension.base,
1261 &driDRI2Extension.base,
1262 &i915_vtable.base,
1263 &i915_config_options.base,
1264 NULL
1265 };
1266
1267 PUBLIC const __DRIextension **__driDriverGetExtensions_i915(void)
1268 {
1269 globalDriverAPI = &i915_driver_api;
1270
1271 return i915_driver_extensions;
1272 }