blob: 686cbf5ec123972f3232f2af20178800f824132c [file] [log] [blame]
Eric Anholt733d32f2013-06-20 10:00:18 -07001/**************************************************************************
2 *
José Fonseca87712852014-01-17 16:27:50 +00003 * Copyright 2003 VMware, Inc.
Eric Anholt733d32f2013-06-20 10:00:18 -07004 * 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.
José Fonseca87712852014-01-17 16:27:50 +000021 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
Eric Anholt733d32f2013-06-20 10:00:18 -070022 * 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>
Ian Romanick2fe6fbd2013-10-11 12:26:57 -070030#include <unistd.h>
Eric Anholt733d32f2013-06-20 10:00:18 -070031#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
Eric Anholt68689232013-09-27 15:25:40 -070044static const __DRIconfigOptionsExtension i915_config_options = {
45 .base = { __DRI_CONFIG_OPTIONS, 1 },
46 .xml =
47
48DRI_CONF_BEGIN
Eric Anholt733d32f2013-06-20 10:00:18 -070049 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
Eric Anholt733d32f2013-06-20 10:00:18 -070061 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
Matt Turnera1891da2017-01-29 18:20:10 -080065 DRI_CONF_OPT_BEGIN_B(fragment_shader, "true")
66 DRI_CONF_DESC(en, "Enable limited ARB_fragment_shader support on 915/945.")
67 DRI_CONF_OPT_END
68
Eric Anholt733d32f2013-06-20 10:00:18 -070069 DRI_CONF_SECTION_END
70 DRI_CONF_SECTION_QUALITY
71 DRI_CONF_FORCE_S3TC_ENABLE("false")
Eric Anholt733d32f2013-06-20 10:00:18 -070072 DRI_CONF_SECTION_END
73 DRI_CONF_SECTION_DEBUG
74 DRI_CONF_NO_RAST("false")
75 DRI_CONF_ALWAYS_FLUSH_BATCH("false")
76 DRI_CONF_ALWAYS_FLUSH_CACHE("false")
77 DRI_CONF_DISABLE_THROTTLING("false")
78 DRI_CONF_FORCE_GLSL_EXTENSIONS_WARN("false")
79 DRI_CONF_DISABLE_GLSL_LINE_CONTINUATIONS("false")
80 DRI_CONF_DISABLE_BLEND_FUNC_EXTENDED("false")
81
Matt Turnera1891da2017-01-29 18:20:10 -080082 DRI_CONF_OPT_BEGIN_B(stub_occlusion_query, "false")
83 DRI_CONF_DESC(en, "Enable stub ARB_occlusion_query support on 915/945.")
84 DRI_CONF_OPT_END
85
Eric Anholt733d32f2013-06-20 10:00:18 -070086 DRI_CONF_OPT_BEGIN_B(shader_precompile, "true")
87 DRI_CONF_DESC(en, "Perform code generation at shader link time.")
88 DRI_CONF_OPT_END
89 DRI_CONF_SECTION_END
Eric Anholt68689232013-09-27 15:25:40 -070090DRI_CONF_END
91};
Eric Anholt733d32f2013-06-20 10:00:18 -070092
Eric Anholt733d32f2013-06-20 10:00:18 -070093#include "intel_batchbuffer.h"
94#include "intel_buffers.h"
95#include "intel_bufmgr.h"
96#include "intel_chipset.h"
97#include "intel_fbo.h"
98#include "intel_mipmap_tree.h"
99#include "intel_screen.h"
100#include "intel_tex.h"
101#include "intel_regions.h"
102
Eric Anholt733d32f2013-06-20 10:00:18 -0700103#include "i915_drm.h"
104
Eric Anholt733d32f2013-06-20 10:00:18 -0700105/**
106 * For debugging purposes, this returns a time in seconds.
107 */
108double
109get_time(void)
110{
111 struct timespec tp;
112
113 clock_gettime(CLOCK_MONOTONIC, &tp);
114
115 return tp.tv_sec + tp.tv_nsec / 1000000000.0;
116}
117
118void
119aub_dump_bmp(struct gl_context *ctx)
120{
121 struct gl_framebuffer *fb = ctx->DrawBuffer;
122
123 for (int i = 0; i < fb->_NumColorDrawBuffers; i++) {
124 struct intel_renderbuffer *irb =
125 intel_renderbuffer(fb->_ColorDrawBuffers[i]);
126
127 if (irb && irb->mt) {
128 enum aub_dump_bmp_format format;
129
130 switch (irb->Base.Base.Format) {
Mark Muelleref145ba2014-01-20 14:21:43 -0800131 case MESA_FORMAT_B8G8R8A8_UNORM:
132 case MESA_FORMAT_B8G8R8X8_UNORM:
Eric Anholt733d32f2013-06-20 10:00:18 -0700133 format = AUB_DUMP_BMP_FORMAT_ARGB_8888;
134 break;
135 default:
136 continue;
137 }
138
139 assert(irb->mt->region->pitch % irb->mt->region->cpp == 0);
140 drm_intel_gem_bo_aub_dump_bmp(irb->mt->region->bo,
141 irb->draw_x,
142 irb->draw_y,
143 irb->Base.Base.Width,
144 irb->Base.Base.Height,
145 format,
146 irb->mt->region->pitch,
147 0);
148 }
149 }
150}
151
152static const __DRItexBufferExtension intelTexBufferExtension = {
Emil Velikov38f20f72014-02-12 17:47:53 +0000153 .base = { __DRI_TEX_BUFFER, 3 },
Eric Anholt733d32f2013-06-20 10:00:18 -0700154
155 .setTexBuffer = intelSetTexBuffer,
156 .setTexBuffer2 = intelSetTexBuffer2,
157 .releaseTexBuffer = NULL,
158};
159
160static void
161intelDRI2Flush(__DRIdrawable *drawable)
162{
163 GET_CURRENT_CONTEXT(ctx);
164 struct intel_context *intel = intel_context(ctx);
165 if (intel == NULL)
166 return;
167
Eric Anholt6bdc5ec2013-06-20 12:08:32 -0700168 INTEL_FIREVERTICES(intel);
Eric Anholt733d32f2013-06-20 10:00:18 -0700169
Eric Anholt733d32f2013-06-20 10:00:18 -0700170 intel->need_throttle = true;
171
172 if (intel->batch.used)
173 intel_batchbuffer_flush(intel);
174
175 if (INTEL_DEBUG & DEBUG_AUB) {
176 aub_dump_bmp(ctx);
177 }
178}
179
180static const struct __DRI2flushExtensionRec intelFlushExtension = {
181 .base = { __DRI2_FLUSH, 3 },
182
183 .flush = intelDRI2Flush,
184 .invalidate = dri2InvalidateDrawable,
185};
186
187static struct intel_image_format intel_image_formats[] = {
188 { __DRI_IMAGE_FOURCC_ARGB8888, __DRI_IMAGE_COMPONENTS_RGBA, 1,
189 { { 0, 0, 0, __DRI_IMAGE_FORMAT_ARGB8888, 4 } } },
190
Keith Packardaea47572013-11-21 20:08:35 -0800191 { __DRI_IMAGE_FOURCC_SARGB8888, __DRI_IMAGE_COMPONENTS_RGBA, 1,
192 { { 0, 0, 0, __DRI_IMAGE_FORMAT_SARGB8, 4 } } },
193
Eric Anholt733d32f2013-06-20 10:00:18 -0700194 { __DRI_IMAGE_FOURCC_XRGB8888, __DRI_IMAGE_COMPONENTS_RGB, 1,
195 { { 0, 0, 0, __DRI_IMAGE_FORMAT_XRGB8888, 4 }, } },
196
197 { __DRI_IMAGE_FOURCC_YUV410, __DRI_IMAGE_COMPONENTS_Y_U_V, 3,
198 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
199 { 1, 2, 2, __DRI_IMAGE_FORMAT_R8, 1 },
200 { 2, 2, 2, __DRI_IMAGE_FORMAT_R8, 1 } } },
201
202 { __DRI_IMAGE_FOURCC_YUV411, __DRI_IMAGE_COMPONENTS_Y_U_V, 3,
203 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
204 { 1, 2, 0, __DRI_IMAGE_FORMAT_R8, 1 },
205 { 2, 2, 0, __DRI_IMAGE_FORMAT_R8, 1 } } },
206
207 { __DRI_IMAGE_FOURCC_YUV420, __DRI_IMAGE_COMPONENTS_Y_U_V, 3,
208 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
209 { 1, 1, 1, __DRI_IMAGE_FORMAT_R8, 1 },
210 { 2, 1, 1, __DRI_IMAGE_FORMAT_R8, 1 } } },
211
212 { __DRI_IMAGE_FOURCC_YUV422, __DRI_IMAGE_COMPONENTS_Y_U_V, 3,
213 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
214 { 1, 1, 0, __DRI_IMAGE_FORMAT_R8, 1 },
215 { 2, 1, 0, __DRI_IMAGE_FORMAT_R8, 1 } } },
216
217 { __DRI_IMAGE_FOURCC_YUV444, __DRI_IMAGE_COMPONENTS_Y_U_V, 3,
218 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
219 { 1, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
220 { 2, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 } } },
221
222 { __DRI_IMAGE_FOURCC_NV12, __DRI_IMAGE_COMPONENTS_Y_UV, 2,
223 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
224 { 1, 1, 1, __DRI_IMAGE_FORMAT_GR88, 2 } } },
225
226 { __DRI_IMAGE_FOURCC_NV16, __DRI_IMAGE_COMPONENTS_Y_UV, 2,
227 { { 0, 0, 0, __DRI_IMAGE_FORMAT_R8, 1 },
228 { 1, 1, 0, __DRI_IMAGE_FORMAT_GR88, 2 } } },
229
Johnson Lin165e7042017-06-16 13:51:34 +0800230 /* For YUYV and UYVY buffers, we set up two overlapping DRI images
231 * and treat them as planar buffers in the compositors.
232 * Plane 0 is GR88 and samples YU or YV pairs and places Y into
233 * the R component, while plane 1 is ARGB/ABGR and samples YUYV/UYVY
234 * clusters and places pairs and places U into the G component and
235 * V into A. This lets the texture sampler interpolate the Y
236 * components correctly when sampling from plane 0, and interpolate
237 * U and V correctly when sampling from plane 1. */
Eric Anholt733d32f2013-06-20 10:00:18 -0700238 { __DRI_IMAGE_FOURCC_YUYV, __DRI_IMAGE_COMPONENTS_Y_XUXV, 2,
239 { { 0, 0, 0, __DRI_IMAGE_FORMAT_GR88, 2 },
Johnson Lin165e7042017-06-16 13:51:34 +0800240 { 0, 1, 0, __DRI_IMAGE_FORMAT_ARGB8888, 4 } } },
241 { __DRI_IMAGE_FOURCC_UYVY, __DRI_IMAGE_COMPONENTS_Y_UXVX, 2,
242 { { 0, 0, 0, __DRI_IMAGE_FORMAT_GR88, 2 },
243 { 0, 1, 0, __DRI_IMAGE_FORMAT_ABGR8888, 4 } } }
Eric Anholt733d32f2013-06-20 10:00:18 -0700244};
245
246static __DRIimage *
247intel_allocate_image(int dri_format, void *loaderPrivate)
248{
249 __DRIimage *image;
250
251 image = calloc(1, sizeof *image);
252 if (image == NULL)
253 return NULL;
254
255 image->dri_format = dri_format;
256 image->offset = 0;
257
Keith Packard1f085ba2013-11-04 17:33:34 -0800258 image->format = driImageFormatToGLFormat(dri_format);
Ander Conselvan de Oliveira5ba6be22013-11-12 14:47:08 +0200259 if (dri_format != __DRI_IMAGE_FORMAT_NONE &&
260 image->format == MESA_FORMAT_NONE) {
Eric Anholt733d32f2013-06-20 10:00:18 -0700261 free(image);
262 return NULL;
263 }
264
265 image->internal_format = _mesa_get_format_base_format(image->format);
266 image->data = loaderPrivate;
267
268 return image;
269}
270
271/**
272 * Sets up a DRIImage structure to point to our shared image in a region
273 */
274static void
275intel_setup_image_from_mipmap_tree(struct intel_context *intel, __DRIimage *image,
276 struct intel_mipmap_tree *mt, GLuint level,
277 GLuint zoffset)
278{
279 unsigned int draw_x, draw_y;
280 uint32_t mask_x, mask_y;
281
Eric Anholt733d32f2013-06-20 10:00:18 -0700282 intel_miptree_check_level_layer(mt, level, zoffset);
283
Ian Romanick122e6dc2017-06-02 17:29:53 -0700284 intel_region_get_tile_masks(mt->region, &mask_x, &mask_y);
Eric Anholt733d32f2013-06-20 10:00:18 -0700285 intel_miptree_get_image_offset(mt, level, zoffset, &draw_x, &draw_y);
286
287 image->width = mt->level[level].width;
288 image->height = mt->level[level].height;
289 image->tile_x = draw_x & mask_x;
290 image->tile_y = draw_y & mask_y;
291
292 image->offset = intel_region_get_aligned_offset(mt->region,
293 draw_x & ~mask_x,
Ian Romanick122e6dc2017-06-02 17:29:53 -0700294 draw_y & ~mask_y);
Eric Anholt733d32f2013-06-20 10:00:18 -0700295
296 intel_region_reference(&image->region, mt->region);
297}
298
299static void
300intel_setup_image_from_dimensions(__DRIimage *image)
301{
302 image->width = image->region->width;
303 image->height = image->region->height;
304 image->tile_x = 0;
305 image->tile_y = 0;
Eric Anholt733d32f2013-06-20 10:00:18 -0700306}
307
Eric Anholt733d32f2013-06-20 10:00:18 -0700308static __DRIimage *
309intel_create_image_from_name(__DRIscreen *screen,
310 int width, int height, int format,
311 int name, int pitch, void *loaderPrivate)
312{
313 struct intel_screen *intelScreen = screen->driverPrivate;
314 __DRIimage *image;
315 int cpp;
316
317 image = intel_allocate_image(format, loaderPrivate);
318 if (image == NULL)
319 return NULL;
320
321 if (image->format == MESA_FORMAT_NONE)
322 cpp = 1;
323 else
324 cpp = _mesa_get_format_bytes(image->format);
325 image->region = intel_region_alloc_for_handle(intelScreen,
326 cpp, width, height,
327 pitch * cpp, name, "image");
328 if (image->region == NULL) {
329 free(image);
330 return NULL;
331 }
332
333 intel_setup_image_from_dimensions(image);
334
335 return image;
336}
337
338static __DRIimage *
339intel_create_image_from_renderbuffer(__DRIcontext *context,
340 int renderbuffer, void *loaderPrivate)
341{
342 __DRIimage *image;
343 struct intel_context *intel = context->driverPrivate;
344 struct gl_renderbuffer *rb;
345 struct intel_renderbuffer *irb;
346
347 rb = _mesa_lookup_renderbuffer(&intel->ctx, renderbuffer);
348 if (!rb) {
349 _mesa_error(&intel->ctx,
350 GL_INVALID_OPERATION, "glRenderbufferExternalMESA");
351 return NULL;
352 }
353
354 irb = intel_renderbuffer(rb);
Eric Anholt733d32f2013-06-20 10:00:18 -0700355 image = calloc(1, sizeof *image);
356 if (image == NULL)
357 return NULL;
358
359 image->internal_format = rb->InternalFormat;
360 image->format = rb->Format;
361 image->offset = 0;
362 image->data = loaderPrivate;
363 intel_region_reference(&image->region, irb->mt->region);
364 intel_setup_image_from_dimensions(image);
Keith Packard1f085ba2013-11-04 17:33:34 -0800365 image->dri_format = driGLFormatToImageFormat(image->format);
Eric Anholt733d32f2013-06-20 10:00:18 -0700366
367 rb->NeedsFinishRenderTexture = true;
368 return image;
369}
370
371static __DRIimage *
372intel_create_image_from_texture(__DRIcontext *context, int target,
373 unsigned texture, int zoffset,
374 int level,
375 unsigned *error,
376 void *loaderPrivate)
377{
378 __DRIimage *image;
379 struct intel_context *intel = context->driverPrivate;
380 struct gl_texture_object *obj;
381 struct intel_texture_object *iobj;
382 GLuint face = 0;
383
384 obj = _mesa_lookup_texture(&intel->ctx, texture);
385 if (!obj || obj->Target != target) {
386 *error = __DRI_IMAGE_ERROR_BAD_PARAMETER;
387 return NULL;
388 }
389
390 if (target == GL_TEXTURE_CUBE_MAP)
391 face = zoffset;
392
393 _mesa_test_texobj_completeness(&intel->ctx, obj);
394 iobj = intel_texture_object(obj);
395 if (!obj->_BaseComplete || (level > 0 && !obj->_MipmapComplete)) {
396 *error = __DRI_IMAGE_ERROR_BAD_PARAMETER;
397 return NULL;
398 }
399
400 if (level < obj->BaseLevel || level > obj->_MaxLevel) {
401 *error = __DRI_IMAGE_ERROR_BAD_MATCH;
402 return NULL;
403 }
404
405 if (target == GL_TEXTURE_3D && obj->Image[face][level]->Depth < zoffset) {
406 *error = __DRI_IMAGE_ERROR_BAD_MATCH;
407 return NULL;
408 }
409 image = calloc(1, sizeof *image);
410 if (image == NULL) {
411 *error = __DRI_IMAGE_ERROR_BAD_ALLOC;
412 return NULL;
413 }
414
415 image->internal_format = obj->Image[face][level]->InternalFormat;
416 image->format = obj->Image[face][level]->TexFormat;
417 image->data = loaderPrivate;
418 intel_setup_image_from_mipmap_tree(intel, image, iobj->mt, level, zoffset);
Keith Packard1f085ba2013-11-04 17:33:34 -0800419 image->dri_format = driGLFormatToImageFormat(image->format);
Eric Anholt733d32f2013-06-20 10:00:18 -0700420 if (image->dri_format == MESA_FORMAT_NONE) {
421 *error = __DRI_IMAGE_ERROR_BAD_PARAMETER;
422 free(image);
423 return NULL;
424 }
425
426 *error = __DRI_IMAGE_ERROR_SUCCESS;
427 return image;
428}
429
430static void
431intel_destroy_image(__DRIimage *image)
432{
433 intel_region_release(&image->region);
434 free(image);
435}
436
437static __DRIimage *
438intel_create_image(__DRIscreen *screen,
439 int width, int height, int format,
440 unsigned int use,
441 void *loaderPrivate)
442{
443 __DRIimage *image;
444 struct intel_screen *intelScreen = screen->driverPrivate;
445 uint32_t tiling;
446 int cpp;
447
448 tiling = I915_TILING_X;
449 if (use & __DRI_IMAGE_USE_CURSOR) {
450 if (width != 64 || height != 64)
451 return NULL;
452 tiling = I915_TILING_NONE;
453 }
454
Axel Davye8f91952013-08-15 12:47:58 +0200455 if (use & __DRI_IMAGE_USE_LINEAR)
456 tiling = I915_TILING_NONE;
457
Eric Anholt733d32f2013-06-20 10:00:18 -0700458 image = intel_allocate_image(format, loaderPrivate);
459 if (image == NULL)
460 return NULL;
461
462 cpp = _mesa_get_format_bytes(image->format);
463 image->region =
464 intel_region_alloc(intelScreen, tiling, cpp, width, height, true);
465 if (image->region == NULL) {
466 free(image);
467 return NULL;
468 }
469
470 intel_setup_image_from_dimensions(image);
471
472 return image;
473}
474
475static GLboolean
476intel_query_image(__DRIimage *image, int attrib, int *value)
477{
478 switch (attrib) {
479 case __DRI_IMAGE_ATTRIB_STRIDE:
480 *value = image->region->pitch;
481 return true;
482 case __DRI_IMAGE_ATTRIB_HANDLE:
483 *value = image->region->bo->handle;
484 return true;
485 case __DRI_IMAGE_ATTRIB_NAME:
486 return intel_region_flink(image->region, (uint32_t *) value);
487 case __DRI_IMAGE_ATTRIB_FORMAT:
488 *value = image->dri_format;
489 return true;
490 case __DRI_IMAGE_ATTRIB_WIDTH:
491 *value = image->region->width;
492 return true;
493 case __DRI_IMAGE_ATTRIB_HEIGHT:
494 *value = image->region->height;
495 return true;
496 case __DRI_IMAGE_ATTRIB_COMPONENTS:
497 if (image->planar_format == NULL)
498 return false;
499 *value = image->planar_format->components;
500 return true;
501 case __DRI_IMAGE_ATTRIB_FD:
Eric Engestrom8af1b542016-08-15 15:51:21 +0100502 return !drm_intel_bo_gem_export_to_prime(image->region->bo, value);
Eric Anholt733d32f2013-06-20 10:00:18 -0700503 default:
504 return false;
505 }
506}
507
508static __DRIimage *
509intel_dup_image(__DRIimage *orig_image, void *loaderPrivate)
510{
511 __DRIimage *image;
512
513 image = calloc(1, sizeof *image);
514 if (image == NULL)
515 return NULL;
516
517 intel_region_reference(&image->region, orig_image->region);
518 if (image->region == NULL) {
519 free(image);
520 return NULL;
521 }
522
523 image->internal_format = orig_image->internal_format;
524 image->planar_format = orig_image->planar_format;
525 image->dri_format = orig_image->dri_format;
526 image->format = orig_image->format;
527 image->offset = orig_image->offset;
528 image->width = orig_image->width;
529 image->height = orig_image->height;
530 image->tile_x = orig_image->tile_x;
531 image->tile_y = orig_image->tile_y;
Eric Anholt733d32f2013-06-20 10:00:18 -0700532 image->data = loaderPrivate;
533
534 memcpy(image->strides, orig_image->strides, sizeof(image->strides));
535 memcpy(image->offsets, orig_image->offsets, sizeof(image->offsets));
536
537 return image;
538}
539
540static GLboolean
541intel_validate_usage(__DRIimage *image, unsigned int use)
542{
543 if (use & __DRI_IMAGE_USE_CURSOR) {
544 if (image->region->width != 64 || image->region->height != 64)
545 return GL_FALSE;
546 }
547
548 return GL_TRUE;
549}
550
551static __DRIimage *
552intel_create_image_from_names(__DRIscreen *screen,
553 int width, int height, int fourcc,
554 int *names, int num_names,
555 int *strides, int *offsets,
556 void *loaderPrivate)
557{
558 struct intel_image_format *f = NULL;
559 __DRIimage *image;
560 int i, index;
561
562 if (screen == NULL || names == NULL || num_names != 1)
563 return NULL;
564
565 for (i = 0; i < ARRAY_SIZE(intel_image_formats); i++) {
566 if (intel_image_formats[i].fourcc == fourcc) {
567 f = &intel_image_formats[i];
568 }
569 }
570
571 if (f == NULL)
572 return NULL;
573
574 image = intel_create_image_from_name(screen, width, height,
575 __DRI_IMAGE_FORMAT_NONE,
576 names[0], strides[0],
577 loaderPrivate);
578
579 if (image == NULL)
580 return NULL;
581
582 image->planar_format = f;
583 for (i = 0; i < f->nplanes; i++) {
584 index = f->planes[i].buffer_index;
585 image->offsets[index] = offsets[index];
586 image->strides[index] = strides[index];
587 }
588
589 return image;
590}
591
592static __DRIimage *
593intel_create_image_from_fds(__DRIscreen *screen,
594 int width, int height, int fourcc,
595 int *fds, int num_fds, int *strides, int *offsets,
596 void *loaderPrivate)
597{
598 struct intel_screen *intelScreen = screen->driverPrivate;
599 struct intel_image_format *f = NULL;
600 __DRIimage *image;
601 int i, index;
602
603 if (fds == NULL || num_fds != 1)
604 return NULL;
605
606 for (i = 0; i < ARRAY_SIZE(intel_image_formats); i++) {
607 if (intel_image_formats[i].fourcc == fourcc) {
608 f = &intel_image_formats[i];
609 }
610 }
611
612 if (f == NULL)
613 return NULL;
614
615 image = intel_allocate_image(__DRI_IMAGE_FORMAT_NONE, loaderPrivate);
616 if (image == NULL)
617 return NULL;
618
619 image->region = intel_region_alloc_for_fd(intelScreen,
Keith Packardbf6591e2013-06-03 20:59:31 -0700620 f->planes[0].cpp, width, height, strides[0],
621 height * strides[0], fds[0], "image");
Eric Anholt733d32f2013-06-20 10:00:18 -0700622 if (image->region == NULL) {
623 free(image);
624 return NULL;
625 }
626
Andreas Pokornydf341322014-08-27 09:36:16 +0200627 intel_setup_image_from_dimensions(image);
628
Eric Anholt733d32f2013-06-20 10:00:18 -0700629 image->planar_format = f;
630 for (i = 0; i < f->nplanes; i++) {
631 index = f->planes[i].buffer_index;
632 image->offsets[index] = offsets[index];
633 image->strides[index] = strides[index];
634 }
635
636 return image;
637}
638
639
640static __DRIimage *
641intel_from_planar(__DRIimage *parent, int plane, void *loaderPrivate)
642{
643 int width, height, offset, stride, dri_format, index;
644 struct intel_image_format *f;
645 uint32_t mask_x, mask_y;
646 __DRIimage *image;
647
648 if (parent == NULL || parent->planar_format == NULL)
649 return NULL;
650
651 f = parent->planar_format;
652
653 if (plane >= f->nplanes)
654 return NULL;
655
656 width = parent->region->width >> f->planes[plane].width_shift;
657 height = parent->region->height >> f->planes[plane].height_shift;
658 dri_format = f->planes[plane].dri_format;
659 index = f->planes[plane].buffer_index;
660 offset = parent->offsets[index];
661 stride = parent->strides[index];
662
663 image = intel_allocate_image(dri_format, loaderPrivate);
664 if (image == NULL)
665 return NULL;
666
667 if (offset + height * stride > parent->region->bo->size) {
668 _mesa_warning(NULL, "intel_create_sub_image: subimage out of bounds");
669 free(image);
670 return NULL;
671 }
672
673 image->region = calloc(sizeof(*image->region), 1);
674 if (image->region == NULL) {
675 free(image);
676 return NULL;
677 }
678
679 image->region->cpp = _mesa_get_format_bytes(image->format);
680 image->region->width = width;
681 image->region->height = height;
682 image->region->pitch = stride;
683 image->region->refcount = 1;
684 image->region->bo = parent->region->bo;
685 drm_intel_bo_reference(image->region->bo);
686 image->region->tiling = parent->region->tiling;
687 image->offset = offset;
688 intel_setup_image_from_dimensions(image);
689
Ian Romanick122e6dc2017-06-02 17:29:53 -0700690 intel_region_get_tile_masks(image->region, &mask_x, &mask_y);
Eric Anholt733d32f2013-06-20 10:00:18 -0700691 if (offset & mask_x)
692 _mesa_warning(NULL,
693 "intel_create_sub_image: offset not on tile boundary");
694
695 return image;
696}
697
Emil Velikov38f20f72014-02-12 17:47:53 +0000698static const __DRIimageExtension intelImageExtension = {
Eric Anholt733d32f2013-06-20 10:00:18 -0700699 .base = { __DRI_IMAGE, 7 },
700
701 .createImageFromName = intel_create_image_from_name,
702 .createImageFromRenderbuffer = intel_create_image_from_renderbuffer,
703 .destroyImage = intel_destroy_image,
704 .createImage = intel_create_image,
705 .queryImage = intel_query_image,
706 .dupImage = intel_dup_image,
707 .validateUsage = intel_validate_usage,
708 .createImageFromNames = intel_create_image_from_names,
709 .fromPlanar = intel_from_planar,
710 .createImageFromTexture = intel_create_image_from_texture,
711 .createImageFromFds = intel_create_image_from_fds
712};
713
Ian Romanick2fe6fbd2013-10-11 12:26:57 -0700714static int
Ian Romanicka15a19f2013-11-11 11:08:26 -0800715i915_query_renderer_integer(__DRIscreen *psp, int param, unsigned int *value)
Ian Romanick2fe6fbd2013-10-11 12:26:57 -0700716{
717 const struct intel_screen *const intelScreen =
718 (struct intel_screen *) psp->driverPrivate;
719
720 switch (param) {
721 case __DRI2_RENDERER_VENDOR_ID:
722 value[0] = 0x8086;
723 return 0;
724 case __DRI2_RENDERER_DEVICE_ID:
725 value[0] = intelScreen->deviceID;
726 return 0;
727 case __DRI2_RENDERER_ACCELERATED:
728 value[0] = 1;
729 return 0;
730 case __DRI2_RENDERER_VIDEO_MEMORY: {
731 /* Once a batch uses more than 75% of the maximum mappable size, we
732 * assume that there's some fragmentation, and we start doing extra
733 * flushing, etc. That's the big cliff apps will care about.
734 */
Ian Romanick9fe108d2013-11-11 10:55:34 -0800735 size_t aper_size;
736 size_t mappable_size;
737
738 drm_intel_get_aperture_sizes(psp->fd, &mappable_size, &aper_size);
739
Ian Romanick2fe6fbd2013-10-11 12:26:57 -0700740 const unsigned gpu_mappable_megabytes =
Ian Romanick9fe108d2013-11-11 10:55:34 -0800741 (aper_size / (1024 * 1024)) * 3 / 4;
Ian Romanick2fe6fbd2013-10-11 12:26:57 -0700742
743 const long system_memory_pages = sysconf(_SC_PHYS_PAGES);
744 const long system_page_size = sysconf(_SC_PAGE_SIZE);
745
746 if (system_memory_pages <= 0 || system_page_size <= 0)
747 return -1;
748
749 const uint64_t system_memory_bytes = (uint64_t) system_memory_pages
750 * (uint64_t) system_page_size;
751
752 const unsigned system_memory_megabytes =
Emil Velikovfc259562014-02-22 03:04:02 +0000753 (unsigned) (system_memory_bytes / (1024 * 1024));
Ian Romanick2fe6fbd2013-10-11 12:26:57 -0700754
755 value[0] = MIN2(system_memory_megabytes, gpu_mappable_megabytes);
756 return 0;
757 }
758 case __DRI2_RENDERER_UNIFIED_MEMORY_ARCHITECTURE:
759 value[0] = 1;
760 return 0;
Adam Jackson59813662016-09-21 09:11:26 -0400761 case __DRI2_RENDERER_HAS_TEXTURE_3D:
762 value[0] = 1;
763 return 0;
Ian Romanick2fe6fbd2013-10-11 12:26:57 -0700764 default:
765 return driQueryRendererIntegerCommon(psp, param, value);
766 }
767
768 return -1;
769}
770
771static int
772i915_query_renderer_string(__DRIscreen *psp, int param, const char **value)
773{
774 const struct intel_screen *intelScreen =
775 (struct intel_screen *) psp->driverPrivate;
776
777 switch (param) {
778 case __DRI2_RENDERER_VENDOR_ID:
779 value[0] = i915_vendor_string;
780 return 0;
781 case __DRI2_RENDERER_DEVICE_ID:
782 value[0] = i915_get_renderer_string(intelScreen->deviceID);
783 return 0;
784 default:
785 break;
786 }
787
788 return -1;
789}
790
Emil Velikov38f20f72014-02-12 17:47:53 +0000791static const __DRI2rendererQueryExtension intelRendererQueryExtension = {
Ian Romanick2fe6fbd2013-10-11 12:26:57 -0700792 .base = { __DRI2_RENDERER_QUERY, 1 },
793
794 .queryInteger = i915_query_renderer_integer,
795 .queryString = i915_query_renderer_string
796};
797
Eric Anholt733d32f2013-06-20 10:00:18 -0700798static const __DRIextension *intelScreenExtensions[] = {
799 &intelTexBufferExtension.base,
Mauro Rossid11515f2016-07-14 05:33:16 +0200800 &intelFenceExtension.base,
Eric Anholt733d32f2013-06-20 10:00:18 -0700801 &intelFlushExtension.base,
802 &intelImageExtension.base,
Ian Romanick96a35272013-11-11 11:12:08 -0800803 &intelRendererQueryExtension.base,
Eric Anholt733d32f2013-06-20 10:00:18 -0700804 &dri2ConfigQueryExtension.base,
805 NULL
806};
807
808static bool
809intel_get_param(__DRIscreen *psp, int param, int *value)
810{
811 int ret;
812 struct drm_i915_getparam gp;
813
814 memset(&gp, 0, sizeof(gp));
815 gp.param = param;
816 gp.value = value;
817
818 ret = drmCommandWriteRead(psp->fd, DRM_I915_GETPARAM, &gp, sizeof(gp));
819 if (ret) {
820 if (ret != -EINVAL)
821 _mesa_warning(NULL, "drm_i915_getparam: %d", ret);
822 return false;
823 }
824
825 return true;
826}
827
828static bool
829intel_get_boolean(__DRIscreen *psp, int param)
830{
831 int value = 0;
832 return intel_get_param(psp, param, &value) && value;
833}
834
835static void
836intelDestroyScreen(__DRIscreen * sPriv)
837{
838 struct intel_screen *intelScreen = sPriv->driverPrivate;
839
840 dri_bufmgr_destroy(intelScreen->bufmgr);
841 driDestroyOptionInfo(&intelScreen->optionCache);
842
843 free(intelScreen);
844 sPriv->driverPrivate = NULL;
845}
846
847
848/**
849 * This is called when we need to set up GL rendering to a new X window.
850 */
851static GLboolean
852intelCreateBuffer(__DRIscreen * driScrnPriv,
853 __DRIdrawable * driDrawPriv,
854 const struct gl_config * mesaVis, GLboolean isPixmap)
855{
856 struct intel_renderbuffer *rb;
Mark Mueller71fe9432014-01-04 14:11:43 -0800857 mesa_format rgbFormat;
Eric Anholt733d32f2013-06-20 10:00:18 -0700858 struct gl_framebuffer *fb;
859
860 if (isPixmap)
861 return false;
862
863 fb = CALLOC_STRUCT(gl_framebuffer);
864 if (!fb)
865 return false;
866
867 _mesa_initialize_window_framebuffer(fb, mesaVis);
868
869 if (mesaVis->redBits == 5)
Mark Muellereeed49f2014-01-26 15:12:56 -0800870 rgbFormat = MESA_FORMAT_B5G6R5_UNORM;
Eric Anholt733d32f2013-06-20 10:00:18 -0700871 else if (mesaVis->sRGBCapable)
Mark Muellereeed49f2014-01-26 15:12:56 -0800872 rgbFormat = MESA_FORMAT_B8G8R8A8_SRGB;
Eric Anholt733d32f2013-06-20 10:00:18 -0700873 else if (mesaVis->alphaBits == 0)
Mark Muelleref145ba2014-01-20 14:21:43 -0800874 rgbFormat = MESA_FORMAT_B8G8R8X8_UNORM;
Eric Anholt6bdc5ec2013-06-20 12:08:32 -0700875 else
Mark Muelleref145ba2014-01-20 14:21:43 -0800876 rgbFormat = MESA_FORMAT_B8G8R8A8_UNORM;
Eric Anholt733d32f2013-06-20 10:00:18 -0700877
878 /* setup the hardware-based renderbuffers */
Eric Anholtf26104e2013-06-20 11:25:58 -0700879 rb = intel_create_renderbuffer(rgbFormat);
Timothy Arceria63919f2017-04-14 13:33:32 +1000880 _mesa_attach_and_own_rb(fb, BUFFER_FRONT_LEFT, &rb->Base.Base);
Eric Anholt733d32f2013-06-20 10:00:18 -0700881
882 if (mesaVis->doubleBufferMode) {
Eric Anholtf26104e2013-06-20 11:25:58 -0700883 rb = intel_create_renderbuffer(rgbFormat);
Timothy Arceria63919f2017-04-14 13:33:32 +1000884 _mesa_attach_and_own_rb(fb, BUFFER_BACK_LEFT, &rb->Base.Base);
Eric Anholt733d32f2013-06-20 10:00:18 -0700885 }
886
887 /*
888 * Assert here that the gl_config has an expected depth/stencil bit
889 * combination: one of d24/s8, d16/s0, d0/s0. (See intelInitScreen2(),
890 * which constructs the advertised configs.)
891 */
892 if (mesaVis->depthBits == 24) {
893 assert(mesaVis->stencilBits == 8);
894
Eric Anholt27eedca2013-06-20 11:53:27 -0700895 /*
896 * Use combined depth/stencil. Note that the renderbuffer is
897 * attached to two attachment points.
898 */
Kenneth Graunkea487ef82014-02-07 21:53:18 -0800899 rb = intel_create_private_renderbuffer(MESA_FORMAT_Z24_UNORM_S8_UINT);
Timothy Arceria63919f2017-04-14 13:33:32 +1000900 _mesa_attach_and_own_rb(fb, BUFFER_DEPTH, &rb->Base.Base);
901 _mesa_attach_and_reference_rb(fb, BUFFER_STENCIL, &rb->Base.Base);
Eric Anholt733d32f2013-06-20 10:00:18 -0700902 }
903 else if (mesaVis->depthBits == 16) {
904 assert(mesaVis->stencilBits == 0);
Mark Mueller50a01d22014-01-20 19:08:54 -0800905 rb = intel_create_private_renderbuffer(MESA_FORMAT_Z_UNORM16);
Timothy Arceria63919f2017-04-14 13:33:32 +1000906 _mesa_attach_and_own_rb(fb, BUFFER_DEPTH, &rb->Base.Base);
Eric Anholt733d32f2013-06-20 10:00:18 -0700907 }
908 else {
909 assert(mesaVis->depthBits == 0);
910 assert(mesaVis->stencilBits == 0);
911 }
912
913 /* now add any/all software-based renderbuffers we may need */
914 _swrast_add_soft_renderbuffers(fb,
915 false, /* never sw color */
916 false, /* never sw depth */
917 false, /* never sw stencil */
918 mesaVis->accumRedBits > 0,
919 false, /* never sw alpha */
920 false /* never sw aux */ );
921 driDrawPriv->driverPrivate = fb;
922
923 return true;
924}
925
926static void
927intelDestroyBuffer(__DRIdrawable * driDrawPriv)
928{
929 struct gl_framebuffer *fb = driDrawPriv->driverPrivate;
930
931 _mesa_reference_framebuffer(&fb, NULL);
932}
933
934/* There are probably better ways to do this, such as an
935 * init-designated function to register chipids and createcontext
936 * functions.
937 */
938extern bool
939i830CreateContext(int api,
940 const struct gl_config *mesaVis,
941 __DRIcontext *driContextPriv,
942 unsigned major_version,
943 unsigned minor_version,
Kristian Høgsberg38366c02013-12-07 22:02:11 -0800944 uint32_t flags,
Eric Anholt733d32f2013-06-20 10:00:18 -0700945 unsigned *error,
946 void *sharedContextPrivate);
947
948extern bool
949i915CreateContext(int api,
950 const struct gl_config *mesaVis,
951 __DRIcontext *driContextPriv,
952 unsigned major_version,
953 unsigned minor_version,
Kristian Høgsberg38366c02013-12-07 22:02:11 -0800954 uint32_t flags,
Eric Anholt733d32f2013-06-20 10:00:18 -0700955 unsigned *error,
956 void *sharedContextPrivate);
Eric Anholt733d32f2013-06-20 10:00:18 -0700957
958static GLboolean
959intelCreateContext(gl_api api,
960 const struct gl_config * mesaVis,
961 __DRIcontext * driContextPriv,
962 unsigned major_version,
963 unsigned minor_version,
964 uint32_t flags,
Ian Romanick17c94de2012-09-10 17:11:33 +0300965 bool notify_reset,
Eric Anholt733d32f2013-06-20 10:00:18 -0700966 unsigned *error,
967 void *sharedContextPrivate)
968{
969 bool success = false;
970
Eric Anholt733d32f2013-06-20 10:00:18 -0700971 __DRIscreen *sPriv = driContextPriv->driScreenPriv;
972 struct intel_screen *intelScreen = sPriv->driverPrivate;
973
Ian Romanick7b140d12012-09-10 17:31:24 +0300974 if (flags & ~__DRI_CTX_FLAG_DEBUG) {
975 *error = __DRI_CTX_ERROR_UNKNOWN_FLAG;
976 return false;
977 }
978
Ian Romanick17c94de2012-09-10 17:11:33 +0300979 if (notify_reset) {
980 *error = __DRI_CTX_ERROR_UNKNOWN_ATTRIBUTE;
981 return false;
982 }
983
Ian Romanick19fda9f2015-03-05 10:27:04 -0800984 if (IS_GEN3(intelScreen->deviceID)) {
Eric Anholt733d32f2013-06-20 10:00:18 -0700985 success = i915CreateContext(api, mesaVis, driContextPriv,
Kristian Høgsberg38366c02013-12-07 22:02:11 -0800986 major_version, minor_version, flags,
987 error, sharedContextPrivate);
Eric Anholt733d32f2013-06-20 10:00:18 -0700988 } else {
989 intelScreen->no_vbo = true;
990 success = i830CreateContext(api, mesaVis, driContextPriv,
Kristian Høgsberg38366c02013-12-07 22:02:11 -0800991 major_version, minor_version, flags,
992 error, sharedContextPrivate);
Eric Anholt733d32f2013-06-20 10:00:18 -0700993 }
Eric Anholt733d32f2013-06-20 10:00:18 -0700994
995 if (success)
996 return true;
997
998 if (driContextPriv->driverPrivate != NULL)
999 intelDestroyContext(driContextPriv);
1000
1001 return false;
1002}
1003
1004static bool
1005intel_init_bufmgr(struct intel_screen *intelScreen)
1006{
1007 __DRIscreen *spriv = intelScreen->driScrnPriv;
1008
1009 intelScreen->no_hw = getenv("INTEL_NO_HW") != NULL;
1010
1011 intelScreen->bufmgr = intel_bufmgr_gem_init(spriv->fd, BATCH_SZ);
1012 if (intelScreen->bufmgr == NULL) {
1013 fprintf(stderr, "[%s:%u] Error initializing buffer manager.\n",
1014 __func__, __LINE__);
1015 return false;
1016 }
1017
1018 drm_intel_bufmgr_gem_enable_fenced_relocs(intelScreen->bufmgr);
1019
1020 if (!intel_get_boolean(spriv, I915_PARAM_HAS_RELAXED_DELTA)) {
1021 fprintf(stderr, "[%s: %u] Kernel 2.6.39 required.\n", __func__, __LINE__);
1022 return false;
1023 }
1024
1025 return true;
1026}
1027
Eric Anholt733d32f2013-06-20 10:00:18 -07001028static bool
1029intel_detect_swizzling(struct intel_screen *screen)
1030{
1031 drm_intel_bo *buffer;
1032 unsigned long flags = 0;
1033 unsigned long aligned_pitch;
1034 uint32_t tiling = I915_TILING_X;
1035 uint32_t swizzle_mode = 0;
1036
1037 buffer = drm_intel_bo_alloc_tiled(screen->bufmgr, "swizzle test",
1038 64, 64, 4,
1039 &tiling, &aligned_pitch, flags);
1040 if (buffer == NULL)
1041 return false;
1042
1043 drm_intel_bo_get_tiling(buffer, &tiling, &swizzle_mode);
1044 drm_intel_bo_unreference(buffer);
1045
1046 if (swizzle_mode == I915_BIT_6_SWIZZLE_NONE)
1047 return false;
1048 else
1049 return true;
1050}
1051
1052static __DRIconfig**
1053intel_screen_make_configs(__DRIscreen *dri_screen)
1054{
Mark Mueller71fe9432014-01-04 14:11:43 -08001055 static const mesa_format formats[] = {
Mark Muellereeed49f2014-01-26 15:12:56 -08001056 MESA_FORMAT_B5G6R5_UNORM,
Derek Foreman3698d712016-11-23 17:01:43 -06001057 MESA_FORMAT_B8G8R8A8_UNORM,
1058 MESA_FORMAT_B8G8R8X8_UNORM
Eric Anholt733d32f2013-06-20 10:00:18 -07001059 };
1060
1061 /* GLX_SWAP_COPY_OML is not supported due to page flipping. */
1062 static const GLenum back_buffer_modes[] = {
1063 GLX_SWAP_UNDEFINED_OML, GLX_NONE,
1064 };
1065
1066 static const uint8_t singlesample_samples[1] = {0};
Eric Anholt733d32f2013-06-20 10:00:18 -07001067
Eric Anholt733d32f2013-06-20 10:00:18 -07001068 uint8_t depth_bits[4], stencil_bits[4];
1069 __DRIconfig **configs = NULL;
1070
1071 /* Generate singlesample configs without accumulation buffer. */
1072 for (int i = 0; i < ARRAY_SIZE(formats); i++) {
1073 __DRIconfig **new_configs;
1074 int num_depth_stencil_bits = 2;
1075
1076 /* Starting with DRI2 protocol version 1.1 we can request a depth/stencil
1077 * buffer that has a different number of bits per pixel than the color
Eric Anholt6bdc5ec2013-06-20 12:08:32 -07001078 * buffer.
Eric Anholt733d32f2013-06-20 10:00:18 -07001079 */
1080 depth_bits[0] = 0;
1081 stencil_bits[0] = 0;
1082
Mark Muellereeed49f2014-01-26 15:12:56 -08001083 if (formats[i] == MESA_FORMAT_B5G6R5_UNORM) {
Eric Anholt733d32f2013-06-20 10:00:18 -07001084 depth_bits[1] = 16;
1085 stencil_bits[1] = 0;
Eric Anholt733d32f2013-06-20 10:00:18 -07001086 } else {
1087 depth_bits[1] = 24;
1088 stencil_bits[1] = 8;
1089 }
1090
1091 new_configs = driCreateConfigs(formats[i],
1092 depth_bits,
1093 stencil_bits,
1094 num_depth_stencil_bits,
1095 back_buffer_modes, 2,
1096 singlesample_samples, 1,
Ilia Mirkin52839002016-08-20 16:10:20 -04001097 false, false);
Eric Anholt733d32f2013-06-20 10:00:18 -07001098 configs = driConcatConfigs(configs, new_configs);
1099 }
1100
1101 /* Generate the minimum possible set of configs that include an
1102 * accumulation buffer.
1103 */
1104 for (int i = 0; i < ARRAY_SIZE(formats); i++) {
1105 __DRIconfig **new_configs;
1106
Mark Muellereeed49f2014-01-26 15:12:56 -08001107 if (formats[i] == MESA_FORMAT_B5G6R5_UNORM) {
Eric Anholt733d32f2013-06-20 10:00:18 -07001108 depth_bits[0] = 16;
1109 stencil_bits[0] = 0;
1110 } else {
1111 depth_bits[0] = 24;
1112 stencil_bits[0] = 8;
1113 }
1114
1115 new_configs = driCreateConfigs(formats[i],
1116 depth_bits, stencil_bits, 1,
1117 back_buffer_modes, 1,
1118 singlesample_samples, 1,
Ilia Mirkin52839002016-08-20 16:10:20 -04001119 true, false);
Eric Anholt733d32f2013-06-20 10:00:18 -07001120 configs = driConcatConfigs(configs, new_configs);
1121 }
1122
Eric Anholt733d32f2013-06-20 10:00:18 -07001123 if (configs == NULL) {
1124 fprintf(stderr, "[%s:%u] Error creating FBConfig!\n", __func__,
1125 __LINE__);
1126 return NULL;
1127 }
1128
1129 return configs;
1130}
1131
1132static void
1133set_max_gl_versions(struct intel_screen *screen)
1134{
Eric Anholt083f66f2013-09-26 12:01:56 -07001135 __DRIscreen *psp = screen->driScrnPriv;
Eric Anholt733d32f2013-06-20 10:00:18 -07001136
1137 switch (screen->gen) {
Matt Turnera1891da2017-01-29 18:20:10 -08001138 case 3: {
1139 bool has_fragment_shader = driQueryOptionb(&screen->optionCache, "fragment_shader");
1140 bool has_occlusion_query = driQueryOptionb(&screen->optionCache, "stub_occlusion_query");
1141
Eric Anholt083f66f2013-09-26 12:01:56 -07001142 psp->max_gl_core_version = 0;
1143 psp->max_gl_es1_version = 11;
Eric Anholt083f66f2013-09-26 12:01:56 -07001144 psp->max_gl_es2_version = 20;
Matt Turnera1891da2017-01-29 18:20:10 -08001145
1146 if (has_fragment_shader && has_occlusion_query) {
1147 psp->max_gl_compat_version = 21;
1148 } else {
1149 psp->max_gl_compat_version = 14;
1150 }
Eric Anholt733d32f2013-06-20 10:00:18 -07001151 break;
Matt Turnera1891da2017-01-29 18:20:10 -08001152 }
Eric Anholt733d32f2013-06-20 10:00:18 -07001153 case 2:
Eric Anholt083f66f2013-09-26 12:01:56 -07001154 psp->max_gl_core_version = 0;
1155 psp->max_gl_compat_version = 13;
1156 psp->max_gl_es1_version = 11;
1157 psp->max_gl_es2_version = 0;
Eric Anholt733d32f2013-06-20 10:00:18 -07001158 break;
1159 default:
1160 assert(!"unrecognized intel_screen::gen");
1161 break;
1162 }
Eric Anholt733d32f2013-06-20 10:00:18 -07001163}
1164
1165/**
1166 * This is the driver specific part of the createNewScreen entry point.
1167 * Called when using DRI2.
1168 *
1169 * \return the struct gl_config supported by this driver
1170 */
1171static const
1172__DRIconfig **intelInitScreen2(__DRIscreen *psp)
1173{
1174 struct intel_screen *intelScreen;
1175
Adel Gadllahb656e3c2014-07-03 22:13:53 +02001176 if (psp->image.loader) {
1177 } else if (psp->dri2.loader->base.version <= 2 ||
Eric Anholt733d32f2013-06-20 10:00:18 -07001178 psp->dri2.loader->getBuffersWithFormat == NULL) {
1179 fprintf(stderr,
1180 "\nERROR! DRI2 loader with getBuffersWithFormat() "
1181 "support required\n");
1182 return false;
1183 }
1184
1185 /* Allocate the private area */
1186 intelScreen = calloc(1, sizeof *intelScreen);
1187 if (!intelScreen) {
1188 fprintf(stderr, "\nERROR! Allocating private area failed\n");
1189 return false;
1190 }
1191 /* parse information in __driConfigOptions */
Eric Anholt68689232013-09-27 15:25:40 -07001192 driParseOptionInfo(&intelScreen->optionCache, i915_config_options.xml);
Eric Anholt733d32f2013-06-20 10:00:18 -07001193
1194 intelScreen->driScrnPriv = psp;
1195 psp->driverPrivate = (void *) intelScreen;
1196
1197 if (!intel_init_bufmgr(intelScreen))
1198 return false;
1199
1200 intelScreen->deviceID = drm_intel_bufmgr_gem_get_devid(intelScreen->bufmgr);
1201
Ian Romanick19fda9f2015-03-05 10:27:04 -08001202 if (IS_GEN3(intelScreen->deviceID)) {
Eric Anholt733d32f2013-06-20 10:00:18 -07001203 intelScreen->gen = 3;
1204 } else {
1205 intelScreen->gen = 2;
1206 }
1207
Eric Anholt733d32f2013-06-20 10:00:18 -07001208 intelScreen->hw_has_swizzling = intel_detect_swizzling(intelScreen);
1209
1210 set_max_gl_versions(intelScreen);
1211
Eric Anholt733d32f2013-06-20 10:00:18 -07001212 psp->extensions = intelScreenExtensions;
1213
1214 return (const __DRIconfig**) intel_screen_make_configs(psp);
1215}
1216
1217struct intel_buffer {
1218 __DRIbuffer base;
1219 struct intel_region *region;
1220};
1221
1222static __DRIbuffer *
1223intelAllocateBuffer(__DRIscreen *screen,
1224 unsigned attachment, unsigned format,
1225 int width, int height)
1226{
1227 struct intel_buffer *intelBuffer;
1228 struct intel_screen *intelScreen = screen->driverPrivate;
1229
1230 assert(attachment == __DRI_BUFFER_FRONT_LEFT ||
1231 attachment == __DRI_BUFFER_BACK_LEFT);
1232
1233 intelBuffer = calloc(1, sizeof *intelBuffer);
1234 if (intelBuffer == NULL)
1235 return NULL;
1236
1237 /* The front and back buffers are color buffers, which are X tiled. */
1238 intelBuffer->region = intel_region_alloc(intelScreen,
1239 I915_TILING_X,
1240 format / 8,
1241 width,
1242 height,
1243 true);
1244
1245 if (intelBuffer->region == NULL) {
1246 free(intelBuffer);
1247 return NULL;
1248 }
1249
1250 intel_region_flink(intelBuffer->region, &intelBuffer->base.name);
1251
1252 intelBuffer->base.attachment = attachment;
1253 intelBuffer->base.cpp = intelBuffer->region->cpp;
1254 intelBuffer->base.pitch = intelBuffer->region->pitch;
1255
1256 return &intelBuffer->base;
1257}
1258
1259static void
1260intelReleaseBuffer(__DRIscreen *screen, __DRIbuffer *buffer)
1261{
1262 struct intel_buffer *intelBuffer = (struct intel_buffer *) buffer;
1263
1264 intel_region_release(&intelBuffer->region);
1265 free(intelBuffer);
1266}
1267
1268
Eric Anholt6665b712013-09-27 14:22:59 -07001269static const struct __DriverAPIRec i915_driver_api = {
Eric Anholt733d32f2013-06-20 10:00:18 -07001270 .InitScreen = intelInitScreen2,
1271 .DestroyScreen = intelDestroyScreen,
1272 .CreateContext = intelCreateContext,
1273 .DestroyContext = intelDestroyContext,
1274 .CreateBuffer = intelCreateBuffer,
1275 .DestroyBuffer = intelDestroyBuffer,
1276 .MakeCurrent = intelMakeCurrent,
1277 .UnbindContext = intelUnbindContext,
1278 .AllocateBuffer = intelAllocateBuffer,
1279 .ReleaseBuffer = intelReleaseBuffer
1280};
1281
Eric Anholt6665b712013-09-27 14:22:59 -07001282static const struct __DRIDriverVtableExtensionRec i915_vtable = {
1283 .base = { __DRI_DRIVER_VTABLE, 1 },
1284 .vtable = &i915_driver_api,
1285};
1286
Eric Anholt733d32f2013-06-20 10:00:18 -07001287/* This is the table of extensions that the loader will dlsym() for. */
Eric Anholt6665b712013-09-27 14:22:59 -07001288static const __DRIextension *i915_driver_extensions[] = {
Eric Anholt733d32f2013-06-20 10:00:18 -07001289 &driCoreExtension.base,
Keith Packard44244202013-11-04 18:09:51 -08001290 &driImageDriverExtension.base,
Eric Anholt733d32f2013-06-20 10:00:18 -07001291 &driDRI2Extension.base,
Eric Anholt6665b712013-09-27 14:22:59 -07001292 &i915_vtable.base,
Eric Anholt68689232013-09-27 15:25:40 -07001293 &i915_config_options.base,
Eric Anholt733d32f2013-06-20 10:00:18 -07001294 NULL
1295};
Eric Anholt6665b712013-09-27 14:22:59 -07001296
1297PUBLIC const __DRIextension **__driDriverGetExtensions_i915(void)
1298{
1299 globalDriverAPI = &i915_driver_api;
1300
1301 return i915_driver_extensions;
1302}