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