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