dri/i915,dri/i965: Use driGLFormatToImageFormat and driImageFormatToGLFormat
[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 (image->format == 0) {
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 if (drm_intel_bo_gem_export_to_prime(image->region->bo, value) == 0)
493 return true;
494 return false;
495 default:
496 return false;
497 }
498 }
499
500 static __DRIimage *
501 intel_dup_image(__DRIimage *orig_image, void *loaderPrivate)
502 {
503 __DRIimage *image;
504
505 image = calloc(1, sizeof *image);
506 if (image == NULL)
507 return NULL;
508
509 intel_region_reference(&image->region, orig_image->region);
510 if (image->region == NULL) {
511 free(image);
512 return NULL;
513 }
514
515 image->internal_format = orig_image->internal_format;
516 image->planar_format = orig_image->planar_format;
517 image->dri_format = orig_image->dri_format;
518 image->format = orig_image->format;
519 image->offset = orig_image->offset;
520 image->width = orig_image->width;
521 image->height = orig_image->height;
522 image->tile_x = orig_image->tile_x;
523 image->tile_y = orig_image->tile_y;
524 image->data = loaderPrivate;
525
526 memcpy(image->strides, orig_image->strides, sizeof(image->strides));
527 memcpy(image->offsets, orig_image->offsets, sizeof(image->offsets));
528
529 return image;
530 }
531
532 static GLboolean
533 intel_validate_usage(__DRIimage *image, unsigned int use)
534 {
535 if (use & __DRI_IMAGE_USE_CURSOR) {
536 if (image->region->width != 64 || image->region->height != 64)
537 return GL_FALSE;
538 }
539
540 return GL_TRUE;
541 }
542
543 static __DRIimage *
544 intel_create_image_from_names(__DRIscreen *screen,
545 int width, int height, int fourcc,
546 int *names, int num_names,
547 int *strides, int *offsets,
548 void *loaderPrivate)
549 {
550 struct intel_image_format *f = NULL;
551 __DRIimage *image;
552 int i, index;
553
554 if (screen == NULL || names == NULL || num_names != 1)
555 return NULL;
556
557 for (i = 0; i < ARRAY_SIZE(intel_image_formats); i++) {
558 if (intel_image_formats[i].fourcc == fourcc) {
559 f = &intel_image_formats[i];
560 }
561 }
562
563 if (f == NULL)
564 return NULL;
565
566 image = intel_create_image_from_name(screen, width, height,
567 __DRI_IMAGE_FORMAT_NONE,
568 names[0], strides[0],
569 loaderPrivate);
570
571 if (image == NULL)
572 return NULL;
573
574 image->planar_format = f;
575 for (i = 0; i < f->nplanes; i++) {
576 index = f->planes[i].buffer_index;
577 image->offsets[index] = offsets[index];
578 image->strides[index] = strides[index];
579 }
580
581 return image;
582 }
583
584 static __DRIimage *
585 intel_create_image_from_fds(__DRIscreen *screen,
586 int width, int height, int fourcc,
587 int *fds, int num_fds, int *strides, int *offsets,
588 void *loaderPrivate)
589 {
590 struct intel_screen *intelScreen = screen->driverPrivate;
591 struct intel_image_format *f = NULL;
592 __DRIimage *image;
593 int i, index;
594
595 if (fds == NULL || num_fds != 1)
596 return NULL;
597
598 for (i = 0; i < ARRAY_SIZE(intel_image_formats); i++) {
599 if (intel_image_formats[i].fourcc == fourcc) {
600 f = &intel_image_formats[i];
601 }
602 }
603
604 if (f == NULL)
605 return NULL;
606
607 image = intel_allocate_image(__DRI_IMAGE_FORMAT_NONE, loaderPrivate);
608 if (image == NULL)
609 return NULL;
610
611 image->region = intel_region_alloc_for_fd(intelScreen,
612 f->planes[0].cpp, width, height, strides[0],
613 height * strides[0], fds[0], "image");
614 if (image->region == NULL) {
615 free(image);
616 return NULL;
617 }
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 struct __DRIimageExtensionRec 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, 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 const unsigned long agp_bytes = drmAgpSize(psp->fd);
726 const unsigned gpu_mappable_megabytes =
727 (agp_bytes / (1024 * 1024)) * 3 / 4;
728
729 const long system_memory_pages = sysconf(_SC_PHYS_PAGES);
730 const long system_page_size = sysconf(_SC_PAGE_SIZE);
731
732 if (system_memory_pages <= 0 || system_page_size <= 0)
733 return -1;
734
735 const uint64_t system_memory_bytes = (uint64_t) system_memory_pages
736 * (uint64_t) system_page_size;
737
738 const unsigned system_memory_megabytes =
739 (unsigned) (system_memory_bytes / 1024);
740
741 value[0] = MIN2(system_memory_megabytes, gpu_mappable_megabytes);
742 return 0;
743 }
744 case __DRI2_RENDERER_UNIFIED_MEMORY_ARCHITECTURE:
745 value[0] = 1;
746 return 0;
747 case __DRI2_RENDERER_PREFERRED_PROFILE:
748 value[0] = (1U << __DRI_API_OPENGL);
749 return 0;
750 default:
751 return driQueryRendererIntegerCommon(psp, param, value);
752 }
753
754 return -1;
755 }
756
757 static int
758 i915_query_renderer_string(__DRIscreen *psp, int param, const char **value)
759 {
760 const struct intel_screen *intelScreen =
761 (struct intel_screen *) psp->driverPrivate;
762
763 switch (param) {
764 case __DRI2_RENDERER_VENDOR_ID:
765 value[0] = i915_vendor_string;
766 return 0;
767 case __DRI2_RENDERER_DEVICE_ID:
768 value[0] = i915_get_renderer_string(intelScreen->deviceID);
769 return 0;
770 default:
771 break;
772 }
773
774 return -1;
775 }
776
777 static struct __DRI2rendererQueryExtensionRec intelRendererQueryExtension = {
778 .base = { __DRI2_RENDERER_QUERY, 1 },
779
780 .queryInteger = i915_query_renderer_integer,
781 .queryString = i915_query_renderer_string
782 };
783
784 static const __DRIextension *intelScreenExtensions[] = {
785 &intelTexBufferExtension.base,
786 &intelFlushExtension.base,
787 &intelImageExtension.base,
788 &dri2ConfigQueryExtension.base,
789 NULL
790 };
791
792 static bool
793 intel_get_param(__DRIscreen *psp, int param, int *value)
794 {
795 int ret;
796 struct drm_i915_getparam gp;
797
798 memset(&gp, 0, sizeof(gp));
799 gp.param = param;
800 gp.value = value;
801
802 ret = drmCommandWriteRead(psp->fd, DRM_I915_GETPARAM, &gp, sizeof(gp));
803 if (ret) {
804 if (ret != -EINVAL)
805 _mesa_warning(NULL, "drm_i915_getparam: %d", ret);
806 return false;
807 }
808
809 return true;
810 }
811
812 static bool
813 intel_get_boolean(__DRIscreen *psp, int param)
814 {
815 int value = 0;
816 return intel_get_param(psp, param, &value) && value;
817 }
818
819 static void
820 intelDestroyScreen(__DRIscreen * sPriv)
821 {
822 struct intel_screen *intelScreen = sPriv->driverPrivate;
823
824 dri_bufmgr_destroy(intelScreen->bufmgr);
825 driDestroyOptionInfo(&intelScreen->optionCache);
826
827 free(intelScreen);
828 sPriv->driverPrivate = NULL;
829 }
830
831
832 /**
833 * This is called when we need to set up GL rendering to a new X window.
834 */
835 static GLboolean
836 intelCreateBuffer(__DRIscreen * driScrnPriv,
837 __DRIdrawable * driDrawPriv,
838 const struct gl_config * mesaVis, GLboolean isPixmap)
839 {
840 struct intel_renderbuffer *rb;
841 gl_format rgbFormat;
842 struct gl_framebuffer *fb;
843
844 if (isPixmap)
845 return false;
846
847 fb = CALLOC_STRUCT(gl_framebuffer);
848 if (!fb)
849 return false;
850
851 _mesa_initialize_window_framebuffer(fb, mesaVis);
852
853 if (mesaVis->redBits == 5)
854 rgbFormat = MESA_FORMAT_RGB565;
855 else if (mesaVis->sRGBCapable)
856 rgbFormat = MESA_FORMAT_SARGB8;
857 else if (mesaVis->alphaBits == 0)
858 rgbFormat = MESA_FORMAT_XRGB8888;
859 else
860 rgbFormat = MESA_FORMAT_ARGB8888;
861
862 /* setup the hardware-based renderbuffers */
863 rb = intel_create_renderbuffer(rgbFormat);
864 _mesa_add_renderbuffer(fb, BUFFER_FRONT_LEFT, &rb->Base.Base);
865
866 if (mesaVis->doubleBufferMode) {
867 rb = intel_create_renderbuffer(rgbFormat);
868 _mesa_add_renderbuffer(fb, BUFFER_BACK_LEFT, &rb->Base.Base);
869 }
870
871 /*
872 * Assert here that the gl_config has an expected depth/stencil bit
873 * combination: one of d24/s8, d16/s0, d0/s0. (See intelInitScreen2(),
874 * which constructs the advertised configs.)
875 */
876 if (mesaVis->depthBits == 24) {
877 assert(mesaVis->stencilBits == 8);
878
879 /*
880 * Use combined depth/stencil. Note that the renderbuffer is
881 * attached to two attachment points.
882 */
883 rb = intel_create_private_renderbuffer(MESA_FORMAT_S8_Z24);
884 _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &rb->Base.Base);
885 _mesa_add_renderbuffer(fb, BUFFER_STENCIL, &rb->Base.Base);
886 }
887 else if (mesaVis->depthBits == 16) {
888 assert(mesaVis->stencilBits == 0);
889 rb = intel_create_private_renderbuffer(MESA_FORMAT_Z16);
890 _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &rb->Base.Base);
891 }
892 else {
893 assert(mesaVis->depthBits == 0);
894 assert(mesaVis->stencilBits == 0);
895 }
896
897 /* now add any/all software-based renderbuffers we may need */
898 _swrast_add_soft_renderbuffers(fb,
899 false, /* never sw color */
900 false, /* never sw depth */
901 false, /* never sw stencil */
902 mesaVis->accumRedBits > 0,
903 false, /* never sw alpha */
904 false /* never sw aux */ );
905 driDrawPriv->driverPrivate = fb;
906
907 return true;
908 }
909
910 static void
911 intelDestroyBuffer(__DRIdrawable * driDrawPriv)
912 {
913 struct gl_framebuffer *fb = driDrawPriv->driverPrivate;
914
915 _mesa_reference_framebuffer(&fb, NULL);
916 }
917
918 /* There are probably better ways to do this, such as an
919 * init-designated function to register chipids and createcontext
920 * functions.
921 */
922 extern bool
923 i830CreateContext(int api,
924 const struct gl_config *mesaVis,
925 __DRIcontext *driContextPriv,
926 unsigned major_version,
927 unsigned minor_version,
928 unsigned *error,
929 void *sharedContextPrivate);
930
931 extern bool
932 i915CreateContext(int api,
933 const struct gl_config *mesaVis,
934 __DRIcontext *driContextPriv,
935 unsigned major_version,
936 unsigned minor_version,
937 unsigned *error,
938 void *sharedContextPrivate);
939
940 static GLboolean
941 intelCreateContext(gl_api api,
942 const struct gl_config * mesaVis,
943 __DRIcontext * driContextPriv,
944 unsigned major_version,
945 unsigned minor_version,
946 uint32_t flags,
947 bool notify_reset,
948 unsigned *error,
949 void *sharedContextPrivate)
950 {
951 bool success = false;
952
953 __DRIscreen *sPriv = driContextPriv->driScreenPriv;
954 struct intel_screen *intelScreen = sPriv->driverPrivate;
955
956 if (flags & ~__DRI_CTX_FLAG_DEBUG) {
957 *error = __DRI_CTX_ERROR_UNKNOWN_FLAG;
958 return false;
959 }
960
961 if (notify_reset) {
962 *error = __DRI_CTX_ERROR_UNKNOWN_ATTRIBUTE;
963 return false;
964 }
965
966 if (IS_9XX(intelScreen->deviceID)) {
967 success = i915CreateContext(api, mesaVis, driContextPriv,
968 major_version, minor_version, error,
969 sharedContextPrivate);
970 } else {
971 intelScreen->no_vbo = true;
972 success = i830CreateContext(api, mesaVis, driContextPriv,
973 major_version, minor_version, error,
974 sharedContextPrivate);
975 }
976
977 if (success)
978 return true;
979
980 if (driContextPriv->driverPrivate != NULL)
981 intelDestroyContext(driContextPriv);
982
983 return false;
984 }
985
986 static bool
987 intel_init_bufmgr(struct intel_screen *intelScreen)
988 {
989 __DRIscreen *spriv = intelScreen->driScrnPriv;
990
991 intelScreen->no_hw = getenv("INTEL_NO_HW") != NULL;
992
993 intelScreen->bufmgr = intel_bufmgr_gem_init(spriv->fd, BATCH_SZ);
994 if (intelScreen->bufmgr == NULL) {
995 fprintf(stderr, "[%s:%u] Error initializing buffer manager.\n",
996 __func__, __LINE__);
997 return false;
998 }
999
1000 drm_intel_bufmgr_gem_enable_fenced_relocs(intelScreen->bufmgr);
1001
1002 if (!intel_get_boolean(spriv, I915_PARAM_HAS_RELAXED_DELTA)) {
1003 fprintf(stderr, "[%s: %u] Kernel 2.6.39 required.\n", __func__, __LINE__);
1004 return false;
1005 }
1006
1007 return true;
1008 }
1009
1010 static bool
1011 intel_detect_swizzling(struct intel_screen *screen)
1012 {
1013 drm_intel_bo *buffer;
1014 unsigned long flags = 0;
1015 unsigned long aligned_pitch;
1016 uint32_t tiling = I915_TILING_X;
1017 uint32_t swizzle_mode = 0;
1018
1019 buffer = drm_intel_bo_alloc_tiled(screen->bufmgr, "swizzle test",
1020 64, 64, 4,
1021 &tiling, &aligned_pitch, flags);
1022 if (buffer == NULL)
1023 return false;
1024
1025 drm_intel_bo_get_tiling(buffer, &tiling, &swizzle_mode);
1026 drm_intel_bo_unreference(buffer);
1027
1028 if (swizzle_mode == I915_BIT_6_SWIZZLE_NONE)
1029 return false;
1030 else
1031 return true;
1032 }
1033
1034 static __DRIconfig**
1035 intel_screen_make_configs(__DRIscreen *dri_screen)
1036 {
1037 static const gl_format formats[] = {
1038 MESA_FORMAT_RGB565,
1039 MESA_FORMAT_ARGB8888
1040 };
1041
1042 /* GLX_SWAP_COPY_OML is not supported due to page flipping. */
1043 static const GLenum back_buffer_modes[] = {
1044 GLX_SWAP_UNDEFINED_OML, GLX_NONE,
1045 };
1046
1047 static const uint8_t singlesample_samples[1] = {0};
1048
1049 uint8_t depth_bits[4], stencil_bits[4];
1050 __DRIconfig **configs = NULL;
1051
1052 /* Generate singlesample configs without accumulation buffer. */
1053 for (int i = 0; i < ARRAY_SIZE(formats); i++) {
1054 __DRIconfig **new_configs;
1055 int num_depth_stencil_bits = 2;
1056
1057 /* Starting with DRI2 protocol version 1.1 we can request a depth/stencil
1058 * buffer that has a different number of bits per pixel than the color
1059 * buffer.
1060 */
1061 depth_bits[0] = 0;
1062 stencil_bits[0] = 0;
1063
1064 if (formats[i] == MESA_FORMAT_RGB565) {
1065 depth_bits[1] = 16;
1066 stencil_bits[1] = 0;
1067 } else {
1068 depth_bits[1] = 24;
1069 stencil_bits[1] = 8;
1070 }
1071
1072 new_configs = driCreateConfigs(formats[i],
1073 depth_bits,
1074 stencil_bits,
1075 num_depth_stencil_bits,
1076 back_buffer_modes, 2,
1077 singlesample_samples, 1,
1078 false);
1079 configs = driConcatConfigs(configs, new_configs);
1080 }
1081
1082 /* Generate the minimum possible set of configs that include an
1083 * accumulation buffer.
1084 */
1085 for (int i = 0; i < ARRAY_SIZE(formats); i++) {
1086 __DRIconfig **new_configs;
1087
1088 if (formats[i] == MESA_FORMAT_RGB565) {
1089 depth_bits[0] = 16;
1090 stencil_bits[0] = 0;
1091 } else {
1092 depth_bits[0] = 24;
1093 stencil_bits[0] = 8;
1094 }
1095
1096 new_configs = driCreateConfigs(formats[i],
1097 depth_bits, stencil_bits, 1,
1098 back_buffer_modes, 1,
1099 singlesample_samples, 1,
1100 true);
1101 configs = driConcatConfigs(configs, new_configs);
1102 }
1103
1104 if (configs == NULL) {
1105 fprintf(stderr, "[%s:%u] Error creating FBConfig!\n", __func__,
1106 __LINE__);
1107 return NULL;
1108 }
1109
1110 return configs;
1111 }
1112
1113 static void
1114 set_max_gl_versions(struct intel_screen *screen)
1115 {
1116 __DRIscreen *psp = screen->driScrnPriv;
1117
1118 switch (screen->gen) {
1119 case 3:
1120 psp->max_gl_core_version = 0;
1121 psp->max_gl_es1_version = 11;
1122 psp->max_gl_compat_version = 21;
1123 psp->max_gl_es2_version = 20;
1124 break;
1125 case 2:
1126 psp->max_gl_core_version = 0;
1127 psp->max_gl_compat_version = 13;
1128 psp->max_gl_es1_version = 11;
1129 psp->max_gl_es2_version = 0;
1130 break;
1131 default:
1132 assert(!"unrecognized intel_screen::gen");
1133 break;
1134 }
1135 }
1136
1137 /**
1138 * This is the driver specific part of the createNewScreen entry point.
1139 * Called when using DRI2.
1140 *
1141 * \return the struct gl_config supported by this driver
1142 */
1143 static const
1144 __DRIconfig **intelInitScreen2(__DRIscreen *psp)
1145 {
1146 struct intel_screen *intelScreen;
1147
1148 if (psp->dri2.loader->base.version <= 2 ||
1149 psp->dri2.loader->getBuffersWithFormat == NULL) {
1150 fprintf(stderr,
1151 "\nERROR! DRI2 loader with getBuffersWithFormat() "
1152 "support required\n");
1153 return false;
1154 }
1155
1156 /* Allocate the private area */
1157 intelScreen = calloc(1, sizeof *intelScreen);
1158 if (!intelScreen) {
1159 fprintf(stderr, "\nERROR! Allocating private area failed\n");
1160 return false;
1161 }
1162 /* parse information in __driConfigOptions */
1163 driParseOptionInfo(&intelScreen->optionCache, i915_config_options.xml);
1164
1165 intelScreen->driScrnPriv = psp;
1166 psp->driverPrivate = (void *) intelScreen;
1167
1168 if (!intel_init_bufmgr(intelScreen))
1169 return false;
1170
1171 intelScreen->deviceID = drm_intel_bufmgr_gem_get_devid(intelScreen->bufmgr);
1172
1173 if (IS_9XX(intelScreen->deviceID)) {
1174 intelScreen->gen = 3;
1175 } else {
1176 intelScreen->gen = 2;
1177 }
1178
1179 intelScreen->hw_has_swizzling = intel_detect_swizzling(intelScreen);
1180
1181 set_max_gl_versions(intelScreen);
1182
1183 psp->extensions = intelScreenExtensions;
1184
1185 return (const __DRIconfig**) intel_screen_make_configs(psp);
1186 }
1187
1188 struct intel_buffer {
1189 __DRIbuffer base;
1190 struct intel_region *region;
1191 };
1192
1193 static __DRIbuffer *
1194 intelAllocateBuffer(__DRIscreen *screen,
1195 unsigned attachment, unsigned format,
1196 int width, int height)
1197 {
1198 struct intel_buffer *intelBuffer;
1199 struct intel_screen *intelScreen = screen->driverPrivate;
1200
1201 assert(attachment == __DRI_BUFFER_FRONT_LEFT ||
1202 attachment == __DRI_BUFFER_BACK_LEFT);
1203
1204 intelBuffer = calloc(1, sizeof *intelBuffer);
1205 if (intelBuffer == NULL)
1206 return NULL;
1207
1208 /* The front and back buffers are color buffers, which are X tiled. */
1209 intelBuffer->region = intel_region_alloc(intelScreen,
1210 I915_TILING_X,
1211 format / 8,
1212 width,
1213 height,
1214 true);
1215
1216 if (intelBuffer->region == NULL) {
1217 free(intelBuffer);
1218 return NULL;
1219 }
1220
1221 intel_region_flink(intelBuffer->region, &intelBuffer->base.name);
1222
1223 intelBuffer->base.attachment = attachment;
1224 intelBuffer->base.cpp = intelBuffer->region->cpp;
1225 intelBuffer->base.pitch = intelBuffer->region->pitch;
1226
1227 return &intelBuffer->base;
1228 }
1229
1230 static void
1231 intelReleaseBuffer(__DRIscreen *screen, __DRIbuffer *buffer)
1232 {
1233 struct intel_buffer *intelBuffer = (struct intel_buffer *) buffer;
1234
1235 intel_region_release(&intelBuffer->region);
1236 free(intelBuffer);
1237 }
1238
1239
1240 static const struct __DriverAPIRec i915_driver_api = {
1241 .InitScreen = intelInitScreen2,
1242 .DestroyScreen = intelDestroyScreen,
1243 .CreateContext = intelCreateContext,
1244 .DestroyContext = intelDestroyContext,
1245 .CreateBuffer = intelCreateBuffer,
1246 .DestroyBuffer = intelDestroyBuffer,
1247 .MakeCurrent = intelMakeCurrent,
1248 .UnbindContext = intelUnbindContext,
1249 .AllocateBuffer = intelAllocateBuffer,
1250 .ReleaseBuffer = intelReleaseBuffer
1251 };
1252
1253 static const struct __DRIDriverVtableExtensionRec i915_vtable = {
1254 .base = { __DRI_DRIVER_VTABLE, 1 },
1255 .vtable = &i915_driver_api,
1256 };
1257
1258 /* This is the table of extensions that the loader will dlsym() for. */
1259 static const __DRIextension *i915_driver_extensions[] = {
1260 &driCoreExtension.base,
1261 &driDRI2Extension.base,
1262 &i915_vtable.base,
1263 &i915_config_options.base,
1264 NULL
1265 };
1266
1267 PUBLIC const __DRIextension **__driDriverGetExtensions_i915(void)
1268 {
1269 globalDriverAPI = &i915_driver_api;
1270
1271 return i915_driver_extensions;
1272 }