s/Tungsten Graphics/VMware/
[mesa.git] / src / mesa / drivers / dri / i915 / intel_screen.c
1 /**************************************************************************
2 *
3 * Copyright 2003 VMware, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 #include <errno.h>
29 #include <time.h>
30 #include <unistd.h>
31 #include "main/glheader.h"
32 #include "main/context.h"
33 #include "main/framebuffer.h"
34 #include "main/renderbuffer.h"
35 #include "main/texobj.h"
36 #include "main/hash.h"
37 #include "main/fbobject.h"
38 #include "main/version.h"
39 #include "swrast/s_renderbuffer.h"
40
41 #include "utils.h"
42 #include "xmlpool.h"
43
44 static const __DRIconfigOptionsExtension i915_config_options = {
45 .base = { __DRI_CONFIG_OPTIONS, 1 },
46 .xml =
47
48 DRI_CONF_BEGIN
49 DRI_CONF_SECTION_PERFORMANCE
50 DRI_CONF_VBLANK_MODE(DRI_CONF_VBLANK_ALWAYS_SYNC)
51 /* Options correspond to DRI_CONF_BO_REUSE_DISABLED,
52 * DRI_CONF_BO_REUSE_ALL
53 */
54 DRI_CONF_OPT_BEGIN_V(bo_reuse, enum, 1, "0:1")
55 DRI_CONF_DESC_BEGIN(en, "Buffer object reuse")
56 DRI_CONF_ENUM(0, "Disable buffer object reuse")
57 DRI_CONF_ENUM(1, "Enable reuse of all sizes of buffer objects")
58 DRI_CONF_DESC_END
59 DRI_CONF_OPT_END
60
61 DRI_CONF_OPT_BEGIN_B(early_z, "false")
62 DRI_CONF_DESC(en, "Enable early Z in classic mode (unstable, 945-only).")
63 DRI_CONF_OPT_END
64
65 DRI_CONF_SECTION_END
66 DRI_CONF_SECTION_QUALITY
67 DRI_CONF_FORCE_S3TC_ENABLE("false")
68 DRI_CONF_SECTION_END
69 DRI_CONF_SECTION_DEBUG
70 DRI_CONF_NO_RAST("false")
71 DRI_CONF_ALWAYS_FLUSH_BATCH("false")
72 DRI_CONF_ALWAYS_FLUSH_CACHE("false")
73 DRI_CONF_DISABLE_THROTTLING("false")
74 DRI_CONF_FORCE_GLSL_EXTENSIONS_WARN("false")
75 DRI_CONF_DISABLE_GLSL_LINE_CONTINUATIONS("false")
76 DRI_CONF_DISABLE_BLEND_FUNC_EXTENDED("false")
77
78 DRI_CONF_OPT_BEGIN_B(shader_precompile, "true")
79 DRI_CONF_DESC(en, "Perform code generation at shader link time.")
80 DRI_CONF_OPT_END
81 DRI_CONF_SECTION_END
82 DRI_CONF_END
83 };
84
85 #include "intel_batchbuffer.h"
86 #include "intel_buffers.h"
87 #include "intel_bufmgr.h"
88 #include "intel_chipset.h"
89 #include "intel_fbo.h"
90 #include "intel_mipmap_tree.h"
91 #include "intel_screen.h"
92 #include "intel_tex.h"
93 #include "intel_regions.h"
94
95 #include "i915_drm.h"
96
97 #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, unsigned 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 &intelRendererQueryExtension.base,
794 &dri2ConfigQueryExtension.base,
795 NULL
796 };
797
798 static bool
799 intel_get_param(__DRIscreen *psp, int param, int *value)
800 {
801 int ret;
802 struct drm_i915_getparam gp;
803
804 memset(&gp, 0, sizeof(gp));
805 gp.param = param;
806 gp.value = value;
807
808 ret = drmCommandWriteRead(psp->fd, DRM_I915_GETPARAM, &gp, sizeof(gp));
809 if (ret) {
810 if (ret != -EINVAL)
811 _mesa_warning(NULL, "drm_i915_getparam: %d", ret);
812 return false;
813 }
814
815 return true;
816 }
817
818 static bool
819 intel_get_boolean(__DRIscreen *psp, int param)
820 {
821 int value = 0;
822 return intel_get_param(psp, param, &value) && value;
823 }
824
825 static void
826 intelDestroyScreen(__DRIscreen * sPriv)
827 {
828 struct intel_screen *intelScreen = sPriv->driverPrivate;
829
830 dri_bufmgr_destroy(intelScreen->bufmgr);
831 driDestroyOptionInfo(&intelScreen->optionCache);
832
833 free(intelScreen);
834 sPriv->driverPrivate = NULL;
835 }
836
837
838 /**
839 * This is called when we need to set up GL rendering to a new X window.
840 */
841 static GLboolean
842 intelCreateBuffer(__DRIscreen * driScrnPriv,
843 __DRIdrawable * driDrawPriv,
844 const struct gl_config * mesaVis, GLboolean isPixmap)
845 {
846 struct intel_renderbuffer *rb;
847 gl_format rgbFormat;
848 struct gl_framebuffer *fb;
849
850 if (isPixmap)
851 return false;
852
853 fb = CALLOC_STRUCT(gl_framebuffer);
854 if (!fb)
855 return false;
856
857 _mesa_initialize_window_framebuffer(fb, mesaVis);
858
859 if (mesaVis->redBits == 5)
860 rgbFormat = MESA_FORMAT_RGB565;
861 else if (mesaVis->sRGBCapable)
862 rgbFormat = MESA_FORMAT_SARGB8;
863 else if (mesaVis->alphaBits == 0)
864 rgbFormat = MESA_FORMAT_XRGB8888;
865 else
866 rgbFormat = MESA_FORMAT_ARGB8888;
867
868 /* setup the hardware-based renderbuffers */
869 rb = intel_create_renderbuffer(rgbFormat);
870 _mesa_add_renderbuffer(fb, BUFFER_FRONT_LEFT, &rb->Base.Base);
871
872 if (mesaVis->doubleBufferMode) {
873 rb = intel_create_renderbuffer(rgbFormat);
874 _mesa_add_renderbuffer(fb, BUFFER_BACK_LEFT, &rb->Base.Base);
875 }
876
877 /*
878 * Assert here that the gl_config has an expected depth/stencil bit
879 * combination: one of d24/s8, d16/s0, d0/s0. (See intelInitScreen2(),
880 * which constructs the advertised configs.)
881 */
882 if (mesaVis->depthBits == 24) {
883 assert(mesaVis->stencilBits == 8);
884
885 /*
886 * Use combined depth/stencil. Note that the renderbuffer is
887 * attached to two attachment points.
888 */
889 rb = intel_create_private_renderbuffer(MESA_FORMAT_S8_Z24);
890 _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &rb->Base.Base);
891 _mesa_add_renderbuffer(fb, BUFFER_STENCIL, &rb->Base.Base);
892 }
893 else if (mesaVis->depthBits == 16) {
894 assert(mesaVis->stencilBits == 0);
895 rb = intel_create_private_renderbuffer(MESA_FORMAT_Z16);
896 _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &rb->Base.Base);
897 }
898 else {
899 assert(mesaVis->depthBits == 0);
900 assert(mesaVis->stencilBits == 0);
901 }
902
903 /* now add any/all software-based renderbuffers we may need */
904 _swrast_add_soft_renderbuffers(fb,
905 false, /* never sw color */
906 false, /* never sw depth */
907 false, /* never sw stencil */
908 mesaVis->accumRedBits > 0,
909 false, /* never sw alpha */
910 false /* never sw aux */ );
911 driDrawPriv->driverPrivate = fb;
912
913 return true;
914 }
915
916 static void
917 intelDestroyBuffer(__DRIdrawable * driDrawPriv)
918 {
919 struct gl_framebuffer *fb = driDrawPriv->driverPrivate;
920
921 _mesa_reference_framebuffer(&fb, NULL);
922 }
923
924 /* There are probably better ways to do this, such as an
925 * init-designated function to register chipids and createcontext
926 * functions.
927 */
928 extern bool
929 i830CreateContext(int api,
930 const struct gl_config *mesaVis,
931 __DRIcontext *driContextPriv,
932 unsigned major_version,
933 unsigned minor_version,
934 uint32_t flags,
935 unsigned *error,
936 void *sharedContextPrivate);
937
938 extern bool
939 i915CreateContext(int api,
940 const struct gl_config *mesaVis,
941 __DRIcontext *driContextPriv,
942 unsigned major_version,
943 unsigned minor_version,
944 uint32_t flags,
945 unsigned *error,
946 void *sharedContextPrivate);
947
948 static GLboolean
949 intelCreateContext(gl_api api,
950 const struct gl_config * mesaVis,
951 __DRIcontext * driContextPriv,
952 unsigned major_version,
953 unsigned minor_version,
954 uint32_t flags,
955 bool notify_reset,
956 unsigned *error,
957 void *sharedContextPrivate)
958 {
959 bool success = false;
960
961 __DRIscreen *sPriv = driContextPriv->driScreenPriv;
962 struct intel_screen *intelScreen = sPriv->driverPrivate;
963
964 if (flags & ~__DRI_CTX_FLAG_DEBUG) {
965 *error = __DRI_CTX_ERROR_UNKNOWN_FLAG;
966 return false;
967 }
968
969 if (notify_reset) {
970 *error = __DRI_CTX_ERROR_UNKNOWN_ATTRIBUTE;
971 return false;
972 }
973
974 if (IS_9XX(intelScreen->deviceID)) {
975 success = i915CreateContext(api, mesaVis, driContextPriv,
976 major_version, minor_version, flags,
977 error, sharedContextPrivate);
978 } else {
979 intelScreen->no_vbo = true;
980 success = i830CreateContext(api, mesaVis, driContextPriv,
981 major_version, minor_version, flags,
982 error, sharedContextPrivate);
983 }
984
985 if (success)
986 return true;
987
988 if (driContextPriv->driverPrivate != NULL)
989 intelDestroyContext(driContextPriv);
990
991 return false;
992 }
993
994 static bool
995 intel_init_bufmgr(struct intel_screen *intelScreen)
996 {
997 __DRIscreen *spriv = intelScreen->driScrnPriv;
998
999 intelScreen->no_hw = getenv("INTEL_NO_HW") != NULL;
1000
1001 intelScreen->bufmgr = intel_bufmgr_gem_init(spriv->fd, BATCH_SZ);
1002 if (intelScreen->bufmgr == NULL) {
1003 fprintf(stderr, "[%s:%u] Error initializing buffer manager.\n",
1004 __func__, __LINE__);
1005 return false;
1006 }
1007
1008 drm_intel_bufmgr_gem_enable_fenced_relocs(intelScreen->bufmgr);
1009
1010 if (!intel_get_boolean(spriv, I915_PARAM_HAS_RELAXED_DELTA)) {
1011 fprintf(stderr, "[%s: %u] Kernel 2.6.39 required.\n", __func__, __LINE__);
1012 return false;
1013 }
1014
1015 return true;
1016 }
1017
1018 static bool
1019 intel_detect_swizzling(struct intel_screen *screen)
1020 {
1021 drm_intel_bo *buffer;
1022 unsigned long flags = 0;
1023 unsigned long aligned_pitch;
1024 uint32_t tiling = I915_TILING_X;
1025 uint32_t swizzle_mode = 0;
1026
1027 buffer = drm_intel_bo_alloc_tiled(screen->bufmgr, "swizzle test",
1028 64, 64, 4,
1029 &tiling, &aligned_pitch, flags);
1030 if (buffer == NULL)
1031 return false;
1032
1033 drm_intel_bo_get_tiling(buffer, &tiling, &swizzle_mode);
1034 drm_intel_bo_unreference(buffer);
1035
1036 if (swizzle_mode == I915_BIT_6_SWIZZLE_NONE)
1037 return false;
1038 else
1039 return true;
1040 }
1041
1042 static __DRIconfig**
1043 intel_screen_make_configs(__DRIscreen *dri_screen)
1044 {
1045 static const gl_format formats[] = {
1046 MESA_FORMAT_RGB565,
1047 MESA_FORMAT_ARGB8888
1048 };
1049
1050 /* GLX_SWAP_COPY_OML is not supported due to page flipping. */
1051 static const GLenum back_buffer_modes[] = {
1052 GLX_SWAP_UNDEFINED_OML, GLX_NONE,
1053 };
1054
1055 static const uint8_t singlesample_samples[1] = {0};
1056
1057 uint8_t depth_bits[4], stencil_bits[4];
1058 __DRIconfig **configs = NULL;
1059
1060 /* Generate singlesample configs without accumulation buffer. */
1061 for (int i = 0; i < ARRAY_SIZE(formats); i++) {
1062 __DRIconfig **new_configs;
1063 int num_depth_stencil_bits = 2;
1064
1065 /* Starting with DRI2 protocol version 1.1 we can request a depth/stencil
1066 * buffer that has a different number of bits per pixel than the color
1067 * buffer.
1068 */
1069 depth_bits[0] = 0;
1070 stencil_bits[0] = 0;
1071
1072 if (formats[i] == MESA_FORMAT_RGB565) {
1073 depth_bits[1] = 16;
1074 stencil_bits[1] = 0;
1075 } else {
1076 depth_bits[1] = 24;
1077 stencil_bits[1] = 8;
1078 }
1079
1080 new_configs = driCreateConfigs(formats[i],
1081 depth_bits,
1082 stencil_bits,
1083 num_depth_stencil_bits,
1084 back_buffer_modes, 2,
1085 singlesample_samples, 1,
1086 false);
1087 configs = driConcatConfigs(configs, new_configs);
1088 }
1089
1090 /* Generate the minimum possible set of configs that include an
1091 * accumulation buffer.
1092 */
1093 for (int i = 0; i < ARRAY_SIZE(formats); i++) {
1094 __DRIconfig **new_configs;
1095
1096 if (formats[i] == MESA_FORMAT_RGB565) {
1097 depth_bits[0] = 16;
1098 stencil_bits[0] = 0;
1099 } else {
1100 depth_bits[0] = 24;
1101 stencil_bits[0] = 8;
1102 }
1103
1104 new_configs = driCreateConfigs(formats[i],
1105 depth_bits, stencil_bits, 1,
1106 back_buffer_modes, 1,
1107 singlesample_samples, 1,
1108 true);
1109 configs = driConcatConfigs(configs, new_configs);
1110 }
1111
1112 if (configs == NULL) {
1113 fprintf(stderr, "[%s:%u] Error creating FBConfig!\n", __func__,
1114 __LINE__);
1115 return NULL;
1116 }
1117
1118 return configs;
1119 }
1120
1121 static void
1122 set_max_gl_versions(struct intel_screen *screen)
1123 {
1124 __DRIscreen *psp = screen->driScrnPriv;
1125
1126 switch (screen->gen) {
1127 case 3:
1128 psp->max_gl_core_version = 0;
1129 psp->max_gl_es1_version = 11;
1130 psp->max_gl_compat_version = 21;
1131 psp->max_gl_es2_version = 20;
1132 break;
1133 case 2:
1134 psp->max_gl_core_version = 0;
1135 psp->max_gl_compat_version = 13;
1136 psp->max_gl_es1_version = 11;
1137 psp->max_gl_es2_version = 0;
1138 break;
1139 default:
1140 assert(!"unrecognized intel_screen::gen");
1141 break;
1142 }
1143 }
1144
1145 /**
1146 * This is the driver specific part of the createNewScreen entry point.
1147 * Called when using DRI2.
1148 *
1149 * \return the struct gl_config supported by this driver
1150 */
1151 static const
1152 __DRIconfig **intelInitScreen2(__DRIscreen *psp)
1153 {
1154 struct intel_screen *intelScreen;
1155
1156 if (psp->dri2.loader->base.version <= 2 ||
1157 psp->dri2.loader->getBuffersWithFormat == NULL) {
1158 fprintf(stderr,
1159 "\nERROR! DRI2 loader with getBuffersWithFormat() "
1160 "support required\n");
1161 return false;
1162 }
1163
1164 /* Allocate the private area */
1165 intelScreen = calloc(1, sizeof *intelScreen);
1166 if (!intelScreen) {
1167 fprintf(stderr, "\nERROR! Allocating private area failed\n");
1168 return false;
1169 }
1170 /* parse information in __driConfigOptions */
1171 driParseOptionInfo(&intelScreen->optionCache, i915_config_options.xml);
1172
1173 intelScreen->driScrnPriv = psp;
1174 psp->driverPrivate = (void *) intelScreen;
1175
1176 if (!intel_init_bufmgr(intelScreen))
1177 return false;
1178
1179 intelScreen->deviceID = drm_intel_bufmgr_gem_get_devid(intelScreen->bufmgr);
1180
1181 if (IS_9XX(intelScreen->deviceID)) {
1182 intelScreen->gen = 3;
1183 } else {
1184 intelScreen->gen = 2;
1185 }
1186
1187 intelScreen->hw_has_swizzling = intel_detect_swizzling(intelScreen);
1188
1189 set_max_gl_versions(intelScreen);
1190
1191 psp->extensions = intelScreenExtensions;
1192
1193 return (const __DRIconfig**) intel_screen_make_configs(psp);
1194 }
1195
1196 struct intel_buffer {
1197 __DRIbuffer base;
1198 struct intel_region *region;
1199 };
1200
1201 static __DRIbuffer *
1202 intelAllocateBuffer(__DRIscreen *screen,
1203 unsigned attachment, unsigned format,
1204 int width, int height)
1205 {
1206 struct intel_buffer *intelBuffer;
1207 struct intel_screen *intelScreen = screen->driverPrivate;
1208
1209 assert(attachment == __DRI_BUFFER_FRONT_LEFT ||
1210 attachment == __DRI_BUFFER_BACK_LEFT);
1211
1212 intelBuffer = calloc(1, sizeof *intelBuffer);
1213 if (intelBuffer == NULL)
1214 return NULL;
1215
1216 /* The front and back buffers are color buffers, which are X tiled. */
1217 intelBuffer->region = intel_region_alloc(intelScreen,
1218 I915_TILING_X,
1219 format / 8,
1220 width,
1221 height,
1222 true);
1223
1224 if (intelBuffer->region == NULL) {
1225 free(intelBuffer);
1226 return NULL;
1227 }
1228
1229 intel_region_flink(intelBuffer->region, &intelBuffer->base.name);
1230
1231 intelBuffer->base.attachment = attachment;
1232 intelBuffer->base.cpp = intelBuffer->region->cpp;
1233 intelBuffer->base.pitch = intelBuffer->region->pitch;
1234
1235 return &intelBuffer->base;
1236 }
1237
1238 static void
1239 intelReleaseBuffer(__DRIscreen *screen, __DRIbuffer *buffer)
1240 {
1241 struct intel_buffer *intelBuffer = (struct intel_buffer *) buffer;
1242
1243 intel_region_release(&intelBuffer->region);
1244 free(intelBuffer);
1245 }
1246
1247
1248 static const struct __DriverAPIRec i915_driver_api = {
1249 .InitScreen = intelInitScreen2,
1250 .DestroyScreen = intelDestroyScreen,
1251 .CreateContext = intelCreateContext,
1252 .DestroyContext = intelDestroyContext,
1253 .CreateBuffer = intelCreateBuffer,
1254 .DestroyBuffer = intelDestroyBuffer,
1255 .MakeCurrent = intelMakeCurrent,
1256 .UnbindContext = intelUnbindContext,
1257 .AllocateBuffer = intelAllocateBuffer,
1258 .ReleaseBuffer = intelReleaseBuffer
1259 };
1260
1261 static const struct __DRIDriverVtableExtensionRec i915_vtable = {
1262 .base = { __DRI_DRIVER_VTABLE, 1 },
1263 .vtable = &i915_driver_api,
1264 };
1265
1266 /* This is the table of extensions that the loader will dlsym() for. */
1267 static const __DRIextension *i915_driver_extensions[] = {
1268 &driCoreExtension.base,
1269 &driImageDriverExtension.base,
1270 &driDRI2Extension.base,
1271 &i915_vtable.base,
1272 &i915_config_options.base,
1273 NULL
1274 };
1275
1276 PUBLIC const __DRIextension **__driDriverGetExtensions_i915(void)
1277 {
1278 globalDriverAPI = &i915_driver_api;
1279
1280 return i915_driver_extensions;
1281 }