blob: 8be5d910a0b322fd7cdd793c5562e113c483cc35 [file] [log] [blame]
Keith Whitwell6b9e31f2006-11-01 12:03:11 +00001/**************************************************************************
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 "glheader.h"
29#include "context.h"
30#include "framebuffer.h"
31#include "matrix.h"
32#include "renderbuffer.h"
33#include "simple_list.h"
34#include "utils.h"
35#include "vblank.h"
36#include "xmlpool.h"
37
38
39#include "intel_screen.h"
40
41#include "intel_buffers.h"
42#include "intel_tex.h"
43#include "intel_span.h"
44#include "intel_tris.h"
45#include "intel_ioctl.h"
46#include "intel_fbo.h"
47
48#include "i830_dri.h"
Eric Anholtcfc21192007-05-17 15:28:01 -070049#include "dri_bufmgr.h"
Keith Whitwell6b9e31f2006-11-01 12:03:11 +000050#include "intel_regions.h"
51#include "intel_batchbuffer.h"
52
Dave Airliec4a9a702007-10-04 15:31:47 +100053#include "intel_bufmgr_ttm.h"
54
Keith Whitwell6b9e31f2006-11-01 12:03:11 +000055PUBLIC const char __driConfigOptions[] =
56 DRI_CONF_BEGIN DRI_CONF_SECTION_PERFORMANCE
57 DRI_CONF_FTHROTTLE_MODE(DRI_CONF_FTHROTTLE_IRQS)
58 DRI_CONF_VBLANK_MODE(DRI_CONF_VBLANK_DEF_INTERVAL_0)
59 DRI_CONF_SECTION_END DRI_CONF_SECTION_QUALITY
60 DRI_CONF_FORCE_S3TC_ENABLE(false)
61 DRI_CONF_ALLOW_LARGE_TEXTURES(1)
62 DRI_CONF_SECTION_END DRI_CONF_END;
63 const GLuint __driNConfigOptions = 4;
64
65#ifdef USE_NEW_INTERFACE
66 static PFNGLXCREATECONTEXTMODES create_context_modes = NULL;
67#endif /*USE_NEW_INTERFACE */
68
69 extern const struct dri_extension card_extensions[];
Michel Dänzer3feefee2007-10-16 15:48:46 +020070 extern const struct dri_extension ttm_extensions[];
Keith Whitwell6b9e31f2006-11-01 12:03:11 +000071
72/**
73 * Map all the memory regions described by the screen.
74 * \return GL_TRUE if success, GL_FALSE if error.
75 */
76GLboolean
77intelMapScreenRegions(__DRIscreenPrivate * sPriv)
78{
79 intelScreenPrivate *intelScreen = (intelScreenPrivate *) sPriv->private;
80
81 if (intelScreen->front.handle) {
82 if (drmMap(sPriv->fd,
83 intelScreen->front.handle,
84 intelScreen->front.size,
85 (drmAddress *) & intelScreen->front.map) != 0) {
86 _mesa_problem(NULL, "drmMap(frontbuffer) failed!");
87 return GL_FALSE;
88 }
89 }
90 else {
91 _mesa_warning(NULL, "no front buffer handle in intelMapScreenRegions!");
92 }
93
Brian Paul4db0c892006-11-01 18:48:28 +000094 if (0)
95 _mesa_printf("Back 0x%08x ", intelScreen->back.handle);
Keith Whitwell6b9e31f2006-11-01 12:03:11 +000096 if (drmMap(sPriv->fd,
97 intelScreen->back.handle,
98 intelScreen->back.size,
99 (drmAddress *) & intelScreen->back.map) != 0) {
100 intelUnmapScreenRegions(intelScreen);
101 return GL_FALSE;
102 }
103
Michel Dänzere33a9d62007-02-20 19:14:23 +0100104 if (intelScreen->third.handle) {
105 if (0)
106 _mesa_printf("Third 0x%08x ", intelScreen->third.handle);
107 if (drmMap(sPriv->fd,
108 intelScreen->third.handle,
109 intelScreen->third.size,
110 (drmAddress *) & intelScreen->third.map) != 0) {
111 intelUnmapScreenRegions(intelScreen);
112 return GL_FALSE;
113 }
114 }
115
Brian Paul4db0c892006-11-01 18:48:28 +0000116 if (0)
117 _mesa_printf("Depth 0x%08x ", intelScreen->depth.handle);
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000118 if (drmMap(sPriv->fd,
119 intelScreen->depth.handle,
120 intelScreen->depth.size,
121 (drmAddress *) & intelScreen->depth.map) != 0) {
122 intelUnmapScreenRegions(intelScreen);
123 return GL_FALSE;
124 }
125
Eric Anholtcfc21192007-05-17 15:28:01 -0700126 if (0)
127 _mesa_printf("TEX 0x%08x ", intelScreen->tex.handle);
128 if (intelScreen->tex.size != 0) {
Eric Anholtcfc21192007-05-17 15:28:01 -0700129 if (drmMap(sPriv->fd,
130 intelScreen->tex.handle,
131 intelScreen->tex.size,
132 (drmAddress *) & intelScreen->tex.map) != 0) {
133 intelUnmapScreenRegions(intelScreen);
134 return GL_FALSE;
135 }
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000136 }
Eric Anholtcfc21192007-05-17 15:28:01 -0700137
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000138 if (0)
Michel Dänzere33a9d62007-02-20 19:14:23 +0100139 printf("Mappings: front: %p back: %p third: %p depth: %p tex: %p\n",
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000140 intelScreen->front.map,
Michel Dänzere33a9d62007-02-20 19:14:23 +0100141 intelScreen->back.map, intelScreen->third.map,
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000142 intelScreen->depth.map, intelScreen->tex.map);
143 return GL_TRUE;
144}
145
Eric Anholtcfc21192007-05-17 15:28:01 -0700146/** Driver-specific fence emit implementation for the fake memory manager. */
147static unsigned int
148intel_fence_emit(void *private)
149{
150 intelScreenPrivate *intelScreen = (intelScreenPrivate *)private;
151 unsigned int fence;
152
153 /* XXX: Need to emit a flush, if we haven't already (at least with the
154 * current batchbuffer implementation, we have).
155 */
156
157 fence = intelEmitIrqLocked(intelScreen);
158
159 return fence;
160}
161
162/** Driver-specific fence wait implementation for the fake memory manager. */
163static int
164intel_fence_wait(void *private, unsigned int cookie)
165{
166 intelScreenPrivate *intelScreen = (intelScreenPrivate *)private;
167
168 intelWaitIrq(intelScreen, cookie);
169
170 return 0;
171}
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000172
173static struct intel_region *
174intel_recreate_static(intelScreenPrivate *intelScreen,
175 struct intel_region *region,
Eric Anholt3e168a02007-08-16 14:32:53 -0700176 intelRegion *region_desc,
177 GLuint mem_type)
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000178{
179 if (region) {
Eric Anholt3e168a02007-08-16 14:32:53 -0700180 intel_region_update_static(intelScreen, region, mem_type,
181 region_desc->bo_handle, region_desc->offset,
182 region_desc->map, intelScreen->cpp,
183 region_desc->pitch / intelScreen->cpp,
184 intelScreen->height);
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000185 } else {
Eric Anholt3e168a02007-08-16 14:32:53 -0700186 region = intel_region_create_static(intelScreen, mem_type,
187 region_desc->bo_handle,
188 region_desc->offset,
189 region_desc->map, intelScreen->cpp,
190 region_desc->pitch / intelScreen->cpp,
191 intelScreen->height);
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000192 }
Eric Anholt3e168a02007-08-16 14:32:53 -0700193
194 assert(region->buffer != NULL);
195
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000196 return region;
197}
198
199
200/* Create intel_region structs to describe the static front,back,depth
201 * buffers created by the xserver.
202 *
203 * Although FBO's mean we now no longer use these as render targets in
204 * all circumstances, they won't go away until the back and depth
205 * buffers become private, and the front and rotated buffers will
206 * remain even then.
207 *
208 * Note that these don't allocate video memory, just describe
209 * allocations alread made by the X server.
210 */
211static void
212intel_recreate_static_regions(intelScreenPrivate *intelScreen)
213{
214 intelScreen->front_region =
215 intel_recreate_static(intelScreen,
216 intelScreen->front_region,
Eric Anholt3e168a02007-08-16 14:32:53 -0700217 &intelScreen->front,
218 DRM_BO_FLAG_MEM_TT);
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000219
Eric Anholt3e168a02007-08-16 14:32:53 -0700220 /* The rotated region is only used for old DDXes that didn't handle rotation
221\ * on their own.
222 */
Kristian Høgsbergefd03a22007-05-14 16:37:19 -0400223 if (intelScreen->driScrnPriv->ddx_version.minor < 8) {
Eric Anholt3e168a02007-08-16 14:32:53 -0700224 intelScreen->rotated_region =
225 intel_recreate_static(intelScreen,
226 intelScreen->rotated_region,
227 &intelScreen->rotated,
228 DRM_BO_FLAG_MEM_TT);
229 }
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000230
231 intelScreen->back_region =
232 intel_recreate_static(intelScreen,
233 intelScreen->back_region,
Eric Anholt3e168a02007-08-16 14:32:53 -0700234 &intelScreen->back,
235 DRM_BO_FLAG_MEM_TT);
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000236
Michel Dänzere33a9d62007-02-20 19:14:23 +0100237 if (intelScreen->third.handle) {
238 intelScreen->third_region =
239 intel_recreate_static(intelScreen,
240 intelScreen->third_region,
Eric Anholt3e168a02007-08-16 14:32:53 -0700241 &intelScreen->third,
242 DRM_BO_FLAG_MEM_TT);
Michel Dänzere33a9d62007-02-20 19:14:23 +0100243 }
244
Eric Anholt3e168a02007-08-16 14:32:53 -0700245 /* Still assumes front.cpp == depth.cpp. We can kill this when we move to
246 * private buffers.
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000247 */
248 intelScreen->depth_region =
249 intel_recreate_static(intelScreen,
250 intelScreen->depth_region,
Eric Anholt3e168a02007-08-16 14:32:53 -0700251 &intelScreen->depth,
252 DRM_BO_FLAG_MEM_TT);
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000253}
254
255/**
256 * Use the information in the sarea to update the screen parameters
257 * related to screen rotation. Needs to be called locked.
258 */
259void
260intelUpdateScreenRotation(__DRIscreenPrivate * sPriv, drmI830Sarea * sarea)
261{
262 intelScreenPrivate *intelScreen = (intelScreenPrivate *) sPriv->private;
263
264 intelUnmapScreenRegions(intelScreen);
265 intelUpdateScreenFromSAREA(intelScreen, sarea);
266 if (!intelMapScreenRegions(sPriv)) {
267 fprintf(stderr, "ERROR Remapping screen regions!!!\n");
268 }
269 intel_recreate_static_regions(intelScreen);
270}
271
272
273void
274intelUnmapScreenRegions(intelScreenPrivate * intelScreen)
275{
276#define REALLY_UNMAP 1
277 if (intelScreen->front.map) {
278#if REALLY_UNMAP
279 if (drmUnmap(intelScreen->front.map, intelScreen->front.size) != 0)
280 printf("drmUnmap front failed!\n");
281#endif
282 intelScreen->front.map = NULL;
283 }
284 if (intelScreen->back.map) {
285#if REALLY_UNMAP
286 if (drmUnmap(intelScreen->back.map, intelScreen->back.size) != 0)
287 printf("drmUnmap back failed!\n");
288#endif
289 intelScreen->back.map = NULL;
290 }
Michel Dänzere33a9d62007-02-20 19:14:23 +0100291 if (intelScreen->third.map) {
292#if REALLY_UNMAP
293 if (drmUnmap(intelScreen->third.map, intelScreen->third.size) != 0)
294 printf("drmUnmap third failed!\n");
295#endif
296 intelScreen->third.map = NULL;
297 }
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000298 if (intelScreen->depth.map) {
299#if REALLY_UNMAP
300 drmUnmap(intelScreen->depth.map, intelScreen->depth.size);
301 intelScreen->depth.map = NULL;
302#endif
303 }
304 if (intelScreen->tex.map) {
305#if REALLY_UNMAP
306 drmUnmap(intelScreen->tex.map, intelScreen->tex.size);
307 intelScreen->tex.map = NULL;
308#endif
309 }
310}
311
312
313static void
314intelPrintDRIInfo(intelScreenPrivate * intelScreen,
315 __DRIscreenPrivate * sPriv, I830DRIPtr gDRIPriv)
316{
317 fprintf(stderr, "*** Front size: 0x%x offset: 0x%x pitch: %d\n",
318 intelScreen->front.size, intelScreen->front.offset,
319 intelScreen->front.pitch);
320 fprintf(stderr, "*** Back size: 0x%x offset: 0x%x pitch: %d\n",
321 intelScreen->back.size, intelScreen->back.offset,
322 intelScreen->back.pitch);
323 fprintf(stderr, "*** Depth size: 0x%x offset: 0x%x pitch: %d\n",
324 intelScreen->depth.size, intelScreen->depth.offset,
325 intelScreen->depth.pitch);
326 fprintf(stderr, "*** Rotated size: 0x%x offset: 0x%x pitch: %d\n",
327 intelScreen->rotated.size, intelScreen->rotated.offset,
328 intelScreen->rotated.pitch);
329 fprintf(stderr, "*** Texture size: 0x%x offset: 0x%x\n",
330 intelScreen->tex.size, intelScreen->tex.offset);
331 fprintf(stderr, "*** Memory : 0x%x\n", gDRIPriv->mem);
332}
333
334
335static void
336intelPrintSAREA(const drmI830Sarea * sarea)
337{
338 fprintf(stderr, "SAREA: sarea width %d height %d\n", sarea->width,
339 sarea->height);
340 fprintf(stderr, "SAREA: pitch: %d\n", sarea->pitch);
341 fprintf(stderr,
342 "SAREA: front offset: 0x%08x size: 0x%x handle: 0x%x\n",
343 sarea->front_offset, sarea->front_size,
344 (unsigned) sarea->front_handle);
345 fprintf(stderr,
346 "SAREA: back offset: 0x%08x size: 0x%x handle: 0x%x\n",
347 sarea->back_offset, sarea->back_size,
348 (unsigned) sarea->back_handle);
349 fprintf(stderr, "SAREA: depth offset: 0x%08x size: 0x%x handle: 0x%x\n",
350 sarea->depth_offset, sarea->depth_size,
351 (unsigned) sarea->depth_handle);
352 fprintf(stderr, "SAREA: tex offset: 0x%08x size: 0x%x handle: 0x%x\n",
353 sarea->tex_offset, sarea->tex_size, (unsigned) sarea->tex_handle);
354 fprintf(stderr, "SAREA: rotation: %d\n", sarea->rotation);
355 fprintf(stderr,
356 "SAREA: rotated offset: 0x%08x size: 0x%x\n",
357 sarea->rotated_offset, sarea->rotated_size);
358 fprintf(stderr, "SAREA: rotated pitch: %d\n", sarea->rotated_pitch);
359}
360
361
362/**
363 * A number of the screen parameters are obtained/computed from
364 * information in the SAREA. This function updates those parameters.
365 */
366void
367intelUpdateScreenFromSAREA(intelScreenPrivate * intelScreen,
368 drmI830Sarea * sarea)
369{
370 intelScreen->width = sarea->width;
371 intelScreen->height = sarea->height;
372
373 intelScreen->front.offset = sarea->front_offset;
374 intelScreen->front.pitch = sarea->pitch * intelScreen->cpp;
375 intelScreen->front.handle = sarea->front_handle;
376 intelScreen->front.size = sarea->front_size;
377
378 intelScreen->back.offset = sarea->back_offset;
379 intelScreen->back.pitch = sarea->pitch * intelScreen->cpp;
380 intelScreen->back.handle = sarea->back_handle;
381 intelScreen->back.size = sarea->back_size;
382
Kristian Høgsbergefd03a22007-05-14 16:37:19 -0400383 if (intelScreen->driScrnPriv->ddx_version.minor >= 8) {
Michel Dänzere33a9d62007-02-20 19:14:23 +0100384 intelScreen->third.offset = sarea->third_offset;
385 intelScreen->third.pitch = sarea->pitch * intelScreen->cpp;
386 intelScreen->third.handle = sarea->third_handle;
387 intelScreen->third.size = sarea->third_size;
388 }
389
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000390 intelScreen->depth.offset = sarea->depth_offset;
391 intelScreen->depth.pitch = sarea->pitch * intelScreen->cpp;
392 intelScreen->depth.handle = sarea->depth_handle;
393 intelScreen->depth.size = sarea->depth_size;
394
Kristian Høgsbergefd03a22007-05-14 16:37:19 -0400395 if (intelScreen->driScrnPriv->ddx_version.minor >= 9) {
Eric Anholt3e168a02007-08-16 14:32:53 -0700396 intelScreen->front.bo_handle = sarea->front_bo_handle;
397 intelScreen->back.bo_handle = sarea->back_bo_handle;
398 intelScreen->third.bo_handle = sarea->third_bo_handle;
399 intelScreen->depth.bo_handle = sarea->depth_bo_handle;
400 } else {
401 intelScreen->front.bo_handle = -1;
402 intelScreen->back.bo_handle = -1;
403 intelScreen->third.bo_handle = -1;
404 intelScreen->depth.bo_handle = -1;
405 }
406
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000407 intelScreen->tex.offset = sarea->tex_offset;
408 intelScreen->logTextureGranularity = sarea->log_tex_granularity;
409 intelScreen->tex.handle = sarea->tex_handle;
410 intelScreen->tex.size = sarea->tex_size;
411
412 intelScreen->rotated.offset = sarea->rotated_offset;
413 intelScreen->rotated.pitch = sarea->rotated_pitch * intelScreen->cpp;
414 intelScreen->rotated.size = sarea->rotated_size;
415 intelScreen->current_rotation = sarea->rotation;
416 matrix23Rotate(&intelScreen->rotMatrix,
417 sarea->width, sarea->height, sarea->rotation);
418 intelScreen->rotatedWidth = sarea->virtualX;
419 intelScreen->rotatedHeight = sarea->virtualY;
420
421 if (0)
422 intelPrintSAREA(sarea);
423}
424
Michel Dänzer9c4d1042007-10-16 13:01:44 +0200425static const __DRItexOffsetExtension intelTexOffsetExtension = {
426 { __DRI_TEX_OFFSET },
427 intelSetTexOffset,
428};
429
Kristian Høgsbergac3e8382007-05-15 15:17:30 -0400430static const __DRIextension *intelExtensions[] = {
Kristian Høgsbergf968f672007-05-17 14:39:06 -0400431 &driReadDrawableExtension,
Kristian Høgsbergac3e8382007-05-15 15:17:30 -0400432 &driCopySubBufferExtension.base,
Kristian Høgsbergefaf90b2007-05-15 16:09:44 -0400433 &driSwapControlExtension.base,
Kristian Høgsberga7a0a2b2007-05-16 15:50:40 -0400434 &driFrameTrackingExtension.base,
Kristian Høgsberg106a6f22007-05-16 18:13:41 -0400435 &driMediaStreamCounterExtension.base,
Michel Dänzer9c4d1042007-10-16 13:01:44 +0200436 &intelTexOffsetExtension.base,
Kristian Høgsbergac3e8382007-05-15 15:17:30 -0400437 NULL
438};
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000439
Kristian Høgsbergac3e8382007-05-15 15:17:30 -0400440
441static GLboolean intelInitDriver(__DRIscreenPrivate *sPriv)
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000442{
443 intelScreenPrivate *intelScreen;
444 I830DRIPtr gDRIPriv = (I830DRIPtr) sPriv->pDevPriv;
445 drmI830Sarea *sarea;
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000446
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000447 if (sPriv->devPrivSize != sizeof(I830DRIRec)) {
448 fprintf(stderr,
449 "\nERROR! sizeof(I830DRIRec) does not match passed size from device driver\n");
450 return GL_FALSE;
451 }
452
453 /* Allocate the private area */
454 intelScreen = (intelScreenPrivate *) CALLOC(sizeof(intelScreenPrivate));
455 if (!intelScreen) {
456 fprintf(stderr, "\nERROR! Allocating private area failed\n");
457 return GL_FALSE;
458 }
459 /* parse information in __driConfigOptions */
460 driParseOptionInfo(&intelScreen->optionCache,
461 __driConfigOptions, __driNConfigOptions);
462
463 intelScreen->driScrnPriv = sPriv;
464 sPriv->private = (void *) intelScreen;
465 intelScreen->sarea_priv_offset = gDRIPriv->sarea_priv_offset;
466 sarea = (drmI830Sarea *)
467 (((GLubyte *) sPriv->pSAREA) + intelScreen->sarea_priv_offset);
468
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000469 intelScreen->deviceID = gDRIPriv->deviceID;
470 if (intelScreen->deviceID == PCI_CHIP_I865_G)
471 intelScreen->maxBatchSize = 4096;
Eric Anholtcfc21192007-05-17 15:28:01 -0700472 else
473 intelScreen->maxBatchSize = BATCH_SZ;
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000474
475 intelScreen->mem = gDRIPriv->mem;
476 intelScreen->cpp = gDRIPriv->cpp;
477
478 switch (gDRIPriv->bitsPerPixel) {
479 case 16:
480 intelScreen->fbFormat = DV_PF_565;
481 break;
482 case 32:
483 intelScreen->fbFormat = DV_PF_8888;
484 break;
485 default:
486 exit(1);
487 break;
488 }
489
490 intelUpdateScreenFromSAREA(intelScreen, sarea);
491
492 if (!intelMapScreenRegions(sPriv)) {
493 fprintf(stderr, "\nERROR! mapping regions\n");
494 _mesa_free(intelScreen);
495 sPriv->private = NULL;
496 return GL_FALSE;
497 }
498
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000499 intelScreen->sarea_priv_offset = gDRIPriv->sarea_priv_offset;
500
Brian Paul4db0c892006-11-01 18:48:28 +0000501 if (0)
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000502 intelPrintDRIInfo(intelScreen, sPriv, gDRIPriv);
503
Kristian Høgsbergefd03a22007-05-14 16:37:19 -0400504 intelScreen->drmMinor = sPriv->drm_version.minor;
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000505
506 /* Determine if IRQs are active? */
507 {
508 int ret;
509 drmI830GetParam gp;
510
511 gp.param = I830_PARAM_IRQ_ACTIVE;
512 gp.value = &intelScreen->irq_active;
513
514 ret = drmCommandWriteRead(sPriv->fd, DRM_I830_GETPARAM,
515 &gp, sizeof(gp));
516 if (ret) {
517 fprintf(stderr, "drmI830GetParam: %d\n", ret);
518 return GL_FALSE;
519 }
520 }
521
522 /* Determine if batchbuffers are allowed */
523 {
524 int ret;
525 drmI830GetParam gp;
526
527 gp.param = I830_PARAM_ALLOW_BATCHBUFFER;
528 gp.value = &intelScreen->allow_batchbuffer;
529
530 ret = drmCommandWriteRead(sPriv->fd, DRM_I830_GETPARAM,
531 &gp, sizeof(gp));
532 if (ret) {
533 fprintf(stderr, "drmI830GetParam: (%d) %d\n", gp.param, ret);
534 return GL_FALSE;
535 }
536 }
537
Kristian Høgsbergac3e8382007-05-15 15:17:30 -0400538 sPriv->extensions = intelExtensions;
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000539
Eric Anholt3e168a02007-08-16 14:32:53 -0700540 /* If we've got a new enough DDX that's initializing TTM and giving us
541 * object handles for the shared buffers, use that.
542 */
543 intelScreen->ttm = GL_FALSE;
544 if (getenv("INTEL_NO_TTM") == NULL &&
Kristian Høgsbergefd03a22007-05-14 16:37:19 -0400545 intelScreen->driScrnPriv->ddx_version.minor >= 9 &&
Dave Airlie9e06cf02007-10-12 11:01:17 +1000546 intelScreen->drmMinor >= 11 &&
Eric Anholt3e168a02007-08-16 14:32:53 -0700547 intelScreen->front.bo_handle != -1) {
Dave Airliefa031c82007-10-03 16:54:59 +1000548 intelScreen->bufmgr = intel_bufmgr_ttm_init(sPriv->fd,
Dave Airlie8909f112007-10-09 14:36:04 +1000549 DRM_FENCE_TYPE_EXE,
550 DRM_FENCE_TYPE_EXE |
551 DRM_I915_FENCE_TYPE_RW,
552 BATCH_SZ);
Eric Anholt3e168a02007-08-16 14:32:53 -0700553 if (intelScreen->bufmgr != NULL)
554 intelScreen->ttm = GL_TRUE;
555 }
556 /* Otherwise, use the classic buffer manager. */
Eric Anholt1ddbfd72007-06-18 12:19:42 -0700557 if (intelScreen->bufmgr == NULL) {
558 if (intelScreen->tex.size == 0) {
559 fprintf(stderr, "[%s:%u] Error initializing buffer manager.\n",
560 __func__, __LINE__);
561 return GL_FALSE;
562 }
Eric Anholt9a4cc2e2007-07-25 10:12:23 -0700563 fprintf(stderr, "[%s:%u] Failed to init TTM buffer manager, falling back"
564 " to classic.\n", __func__, __LINE__);
Eric Anholtcfc21192007-05-17 15:28:01 -0700565 intelScreen->bufmgr = dri_bufmgr_fake_init(intelScreen->tex.offset,
566 intelScreen->tex.map,
567 intelScreen->tex.size,
568 intel_fence_emit,
569 intel_fence_wait,
570 intelScreen);
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000571 }
572
573 intel_recreate_static_regions(intelScreen);
574
575 return GL_TRUE;
576}
577
578
579static void
580intelDestroyScreen(__DRIscreenPrivate * sPriv)
581{
582 intelScreenPrivate *intelScreen = (intelScreenPrivate *) sPriv->private;
583
584 intelUnmapScreenRegions(intelScreen);
585
Eric Anholt2ac17c62007-05-31 09:45:45 -0700586 dri_bufmgr_destroy(intelScreen->bufmgr);
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000587 FREE(intelScreen);
588 sPriv->private = NULL;
589}
590
591
592/**
593 * This is called when we need to set up GL rendering to a new X window.
594 */
595static GLboolean
596intelCreateBuffer(__DRIscreenPrivate * driScrnPriv,
597 __DRIdrawablePrivate * driDrawPriv,
598 const __GLcontextModes * mesaVis, GLboolean isPixmap)
599{
600 intelScreenPrivate *screen = (intelScreenPrivate *) driScrnPriv->private;
601
602 if (isPixmap) {
603 return GL_FALSE; /* not implemented */
604 }
605 else {
606 GLboolean swStencil = (mesaVis->stencilBits > 0 &&
607 mesaVis->depthBits != 24);
608 GLenum rgbFormat = (mesaVis->redBits == 5 ? GL_RGB5 : GL_RGBA8);
609
Michel Dänzer6b99caf2007-02-15 16:30:40 +0100610 struct intel_framebuffer *intel_fb = CALLOC_STRUCT(intel_framebuffer);
611
612 if (!intel_fb)
613 return GL_FALSE;
614
615 _mesa_initialize_framebuffer(&intel_fb->Base, mesaVis);
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000616
617 /* setup the hardware-based renderbuffers */
618 {
Michel Dänzere33a9d62007-02-20 19:14:23 +0100619 intel_fb->color_rb[0]
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000620 = intel_create_renderbuffer(rgbFormat,
621 screen->width, screen->height,
622 screen->front.offset,
623 screen->front.pitch,
624 screen->cpp,
625 screen->front.map);
Michel Dänzere33a9d62007-02-20 19:14:23 +0100626 intel_set_span_functions(&intel_fb->color_rb[0]->Base);
Michel Dänzer6b99caf2007-02-15 16:30:40 +0100627 _mesa_add_renderbuffer(&intel_fb->Base, BUFFER_FRONT_LEFT,
Michel Dänzere33a9d62007-02-20 19:14:23 +0100628 &intel_fb->color_rb[0]->Base);
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000629 }
630
631 if (mesaVis->doubleBufferMode) {
Michel Dänzere33a9d62007-02-20 19:14:23 +0100632 intel_fb->color_rb[1]
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000633 = intel_create_renderbuffer(rgbFormat,
634 screen->width, screen->height,
635 screen->back.offset,
636 screen->back.pitch,
637 screen->cpp,
638 screen->back.map);
Michel Dänzere33a9d62007-02-20 19:14:23 +0100639 intel_set_span_functions(&intel_fb->color_rb[1]->Base);
Michel Dänzer6b99caf2007-02-15 16:30:40 +0100640 _mesa_add_renderbuffer(&intel_fb->Base, BUFFER_BACK_LEFT,
Michel Dänzere33a9d62007-02-20 19:14:23 +0100641 &intel_fb->color_rb[1]->Base);
642
643 if (screen->third.handle) {
Michel Dänzer76f3b662007-03-26 17:38:58 +0200644 struct gl_renderbuffer *tmp_rb = NULL;
645
Michel Dänzere33a9d62007-02-20 19:14:23 +0100646 intel_fb->color_rb[2]
647 = intel_create_renderbuffer(rgbFormat,
648 screen->width, screen->height,
649 screen->third.offset,
650 screen->third.pitch,
651 screen->cpp,
652 screen->third.map);
653 intel_set_span_functions(&intel_fb->color_rb[2]->Base);
Michel Dänzer76f3b662007-03-26 17:38:58 +0200654 _mesa_reference_renderbuffer(&tmp_rb, &intel_fb->color_rb[2]->Base);
Michel Dänzere33a9d62007-02-20 19:14:23 +0100655 }
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000656 }
657
658 if (mesaVis->depthBits == 24 && mesaVis->stencilBits == 8) {
659 /* combined depth/stencil buffer */
660 struct intel_renderbuffer *depthStencilRb
661 = intel_create_renderbuffer(GL_DEPTH24_STENCIL8_EXT,
662 screen->width, screen->height,
663 screen->depth.offset,
664 screen->depth.pitch,
665 screen->cpp, /* 4! */
666 screen->depth.map);
667 intel_set_span_functions(&depthStencilRb->Base);
668 /* note: bind RB to two attachment points */
Michel Dänzer6b99caf2007-02-15 16:30:40 +0100669 _mesa_add_renderbuffer(&intel_fb->Base, BUFFER_DEPTH,
670 &depthStencilRb->Base);
671 _mesa_add_renderbuffer(&intel_fb->Base, BUFFER_STENCIL,
672 &depthStencilRb->Base);
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000673 }
674 else if (mesaVis->depthBits == 16) {
675 /* just 16-bit depth buffer, no hw stencil */
676 struct intel_renderbuffer *depthRb
677 = intel_create_renderbuffer(GL_DEPTH_COMPONENT16,
678 screen->width, screen->height,
679 screen->depth.offset,
680 screen->depth.pitch,
681 screen->cpp, /* 2! */
682 screen->depth.map);
683 intel_set_span_functions(&depthRb->Base);
Michel Dänzer6b99caf2007-02-15 16:30:40 +0100684 _mesa_add_renderbuffer(&intel_fb->Base, BUFFER_DEPTH, &depthRb->Base);
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000685 }
686
687 /* now add any/all software-based renderbuffers we may need */
Michel Dänzer6b99caf2007-02-15 16:30:40 +0100688 _mesa_add_soft_renderbuffers(&intel_fb->Base,
689 GL_FALSE, /* never sw color */
690 GL_FALSE, /* never sw depth */
691 swStencil, mesaVis->accumRedBits > 0,
692 GL_FALSE, /* never sw alpha */
693 GL_FALSE /* never sw aux */ );
694 driDrawPriv->driverPrivate = (void *) intel_fb;
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000695
Michel Dänzer6b99caf2007-02-15 16:30:40 +0100696 return GL_TRUE;
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000697 }
698}
699
700static void
701intelDestroyBuffer(__DRIdrawablePrivate * driDrawPriv)
702{
Briana510bc32007-03-06 10:07:59 -0700703 _mesa_unreference_framebuffer((GLframebuffer **)(&(driDrawPriv->driverPrivate)));
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000704}
705
706
707/**
708 * Get information about previous buffer swaps.
709 */
710static int
711intelGetSwapInfo(__DRIdrawablePrivate * dPriv, __DRIswapInfo * sInfo)
712{
Michel Dänzer641c9662007-02-22 17:24:09 +0100713 struct intel_framebuffer *intel_fb;
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000714
Michel Dänzer641c9662007-02-22 17:24:09 +0100715 if ((dPriv == NULL) || (dPriv->driverPrivate == NULL)
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000716 || (sInfo == NULL)) {
717 return -1;
718 }
719
Michel Dänzer641c9662007-02-22 17:24:09 +0100720 intel_fb = dPriv->driverPrivate;
721 sInfo->swap_count = intel_fb->swap_count;
722 sInfo->swap_ust = intel_fb->swap_ust;
723 sInfo->swap_missed_count = intel_fb->swap_missed_count;
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000724
725 sInfo->swap_missed_usage = (sInfo->swap_missed_count != 0)
Michel Dänzer641c9662007-02-22 17:24:09 +0100726 ? driCalculateSwapUsage(dPriv, 0, intel_fb->swap_missed_ust)
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000727 : 0.0;
728
729 return 0;
730}
731
732
733/* There are probably better ways to do this, such as an
734 * init-designated function to register chipids and createcontext
735 * functions.
736 */
737extern GLboolean i830CreateContext(const __GLcontextModes * mesaVis,
738 __DRIcontextPrivate * driContextPriv,
739 void *sharedContextPrivate);
740
741extern GLboolean i915CreateContext(const __GLcontextModes * mesaVis,
742 __DRIcontextPrivate * driContextPriv,
743 void *sharedContextPrivate);
744
745
746
747
748static GLboolean
749intelCreateContext(const __GLcontextModes * mesaVis,
750 __DRIcontextPrivate * driContextPriv,
751 void *sharedContextPrivate)
752{
753 __DRIscreenPrivate *sPriv = driContextPriv->driScreenPriv;
754 intelScreenPrivate *intelScreen = (intelScreenPrivate *) sPriv->private;
755
756 switch (intelScreen->deviceID) {
757 /* Don't deal with i830 until texture work complete:
758 */
759 case PCI_CHIP_845_G:
760 case PCI_CHIP_I830_M:
761 case PCI_CHIP_I855_GM:
762 case PCI_CHIP_I865_G:
763 return i830CreateContext(mesaVis, driContextPriv, sharedContextPrivate);
764
765 case PCI_CHIP_I915_G:
766 case PCI_CHIP_I915_GM:
767 case PCI_CHIP_I945_G:
768 case PCI_CHIP_I945_GM:
Wang Zhenyuad6351a2007-05-30 16:18:26 +0800769 case PCI_CHIP_I945_GME:
Wang Zhenyu8331d9d2007-06-05 11:42:43 -0700770 case PCI_CHIP_G33_G:
771 case PCI_CHIP_Q35_G:
772 case PCI_CHIP_Q33_G:
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000773 return i915CreateContext(mesaVis, driContextPriv, sharedContextPrivate);
774
775 default:
776 fprintf(stderr, "Unrecognized deviceID %x\n", intelScreen->deviceID);
777 return GL_FALSE;
778 }
779}
780
781
782static const struct __DriverAPIRec intelAPI = {
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000783 .DestroyScreen = intelDestroyScreen,
784 .CreateContext = intelCreateContext,
785 .DestroyContext = intelDestroyContext,
786 .CreateBuffer = intelCreateBuffer,
787 .DestroyBuffer = intelDestroyBuffer,
788 .SwapBuffers = intelSwapBuffers,
789 .MakeCurrent = intelMakeCurrent,
790 .UnbindContext = intelUnbindContext,
791 .GetSwapInfo = intelGetSwapInfo,
792 .GetMSC = driGetMSC32,
793 .WaitForMSC = driWaitForMSC32,
794 .WaitForSBC = NULL,
795 .SwapBuffersMSC = NULL,
Michel Dänzere0885b82007-05-22 14:08:11 +0200796 .CopySubBuffer = intelCopySubBuffer,
797 .setTexOffset = intelSetTexOffset,
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000798};
799
800
801static __GLcontextModes *
802intelFillInModes(unsigned pixel_bits, unsigned depth_bits,
803 unsigned stencil_bits, GLboolean have_back_buffer)
804{
805 __GLcontextModes *modes;
806 __GLcontextModes *m;
807 unsigned num_modes;
808 unsigned depth_buffer_factor;
809 unsigned back_buffer_factor;
810 GLenum fb_format;
811 GLenum fb_type;
812
813 /* GLX_SWAP_COPY_OML is only supported because the Intel driver doesn't
814 * support pageflipping at all.
815 */
816 static const GLenum back_buffer_modes[] = {
817 GLX_NONE, GLX_SWAP_UNDEFINED_OML, GLX_SWAP_COPY_OML
818 };
819
820 u_int8_t depth_bits_array[3];
821 u_int8_t stencil_bits_array[3];
822
823
824 depth_bits_array[0] = 0;
825 depth_bits_array[1] = depth_bits;
826 depth_bits_array[2] = depth_bits;
827
828 /* Just like with the accumulation buffer, always provide some modes
829 * with a stencil buffer. It will be a sw fallback, but some apps won't
830 * care about that.
831 */
832 stencil_bits_array[0] = 0;
833 stencil_bits_array[1] = 0;
Dave Airlieb6becfa2006-12-31 10:01:17 +1100834 if (depth_bits == 24)
835 stencil_bits_array[1] = (stencil_bits == 0) ? 8 : stencil_bits;
836
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000837 stencil_bits_array[2] = (stencil_bits == 0) ? 8 : stencil_bits;
838
839 depth_buffer_factor = ((depth_bits != 0) || (stencil_bits != 0)) ? 3 : 1;
840 back_buffer_factor = (have_back_buffer) ? 3 : 1;
841
842 num_modes = depth_buffer_factor * back_buffer_factor * 4;
843
844 if (pixel_bits == 16) {
845 fb_format = GL_RGB;
846 fb_type = GL_UNSIGNED_SHORT_5_6_5;
847 }
848 else {
849 fb_format = GL_BGRA;
850 fb_type = GL_UNSIGNED_INT_8_8_8_8_REV;
851 }
852
853 modes =
854 (*dri_interface->createContextModes) (num_modes,
855 sizeof(__GLcontextModes));
856 m = modes;
857 if (!driFillInModes(&m, fb_format, fb_type,
858 depth_bits_array, stencil_bits_array,
859 depth_buffer_factor, back_buffer_modes,
860 back_buffer_factor, GLX_TRUE_COLOR)) {
861 fprintf(stderr, "[%s:%u] Error creating FBConfig!\n", __func__,
862 __LINE__);
863 return NULL;
864 }
865 if (!driFillInModes(&m, fb_format, fb_type,
866 depth_bits_array, stencil_bits_array,
867 depth_buffer_factor, back_buffer_modes,
868 back_buffer_factor, GLX_DIRECT_COLOR)) {
869 fprintf(stderr, "[%s:%u] Error creating FBConfig!\n", __func__,
870 __LINE__);
871 return NULL;
872 }
873
874 /* Mark the visual as slow if there are "fake" stencil bits.
875 */
876 for (m = modes; m != NULL; m = m->next) {
877 if ((m->stencilBits != 0) && (m->stencilBits != stencil_bits)) {
878 m->visualRating = GLX_SLOW_CONFIG;
879 }
880 }
881
882 return modes;
883}
884
885
886/**
Kristian Høgsberg64106d02007-05-14 16:58:37 -0400887 * This is the driver specific part of the createNewScreen entry point.
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000888 *
Kristian Høgsberg64106d02007-05-14 16:58:37 -0400889 * \todo maybe fold this into intelInitDriver
890 *
891 * \return the __GLcontextModes supported by this driver
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000892 */
Kristian Høgsberg64106d02007-05-14 16:58:37 -0400893PUBLIC __GLcontextModes *__driDriverInitScreen(__DRIscreenPrivate *psp)
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000894{
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000895 static const __DRIversion ddx_expected = { 1, 5, 0 };
896 static const __DRIversion dri_expected = { 4, 0, 0 };
Eric Anholt4cf2cc02007-05-18 11:29:55 -0700897 static const __DRIversion drm_expected = { 1, 5, 0 };
Kristian Høgsberg64106d02007-05-14 16:58:37 -0400898 I830DRIPtr dri_priv = (I830DRIPtr) psp->pDevPriv;
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000899
Kristian Høgsberg64106d02007-05-14 16:58:37 -0400900 psp->DriverAPI = intelAPI;
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000901
902 if (!driCheckDriDdxDrmVersions2("i915",
Kristian Høgsberg64106d02007-05-14 16:58:37 -0400903 &psp->dri_version, &dri_expected,
904 &psp->ddx_version, &ddx_expected,
905 &psp->drm_version, &drm_expected)) {
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000906 return NULL;
907 }
908
Kristian Høgsberg64106d02007-05-14 16:58:37 -0400909 /* Calling driInitExtensions here, with a NULL context pointer,
910 * does not actually enable the extensions. It just makes sure
911 * that all the dispatch offsets for all the extensions that
912 * *might* be enables are known. This is needed because the
913 * dispatch offsets need to be known when _mesa_context_create is
914 * called, but we can't enable the extensions until we have a
915 * context pointer.
916 *
917 * Hello chicken. Hello egg. How are you two today?
918 */
919 driInitExtensions(NULL, card_extensions, GL_FALSE);
Michel Dänzer3feefee2007-10-16 15:48:46 +0200920 driInitExtensions(NULL, ttm_extensions, GL_FALSE);
Thomas Hellstromda56df92007-04-16 16:04:12 +0200921
Kristian Høgsberg64106d02007-05-14 16:58:37 -0400922 if (!intelInitDriver(psp))
923 return NULL;
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000924
Kristian Høgsberg64106d02007-05-14 16:58:37 -0400925 return intelFillInModes(dri_priv->cpp * 8,
926 (dri_priv->cpp == 2) ? 16 : 24,
927 (dri_priv->cpp == 2) ? 0 : 8, 1);
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000928}
929
930struct intel_context *intelScreenContext(intelScreenPrivate *intelScreen)
931{
932 /*
933 * This should probably change to have the screen allocate a dummy
934 * context at screen creation. For now just use the current context.
935 */
936
937 GET_CURRENT_CONTEXT(ctx);
938 if (ctx == NULL) {
939 _mesa_problem(NULL, "No current context in intelScreenContext\n");
940 return NULL;
941 }
942 return intel_context(ctx);
943}
944