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