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