Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

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