blob: 0a118c742b73712d3a41362cf8cecf1c2634987f [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,
Eric Anholt9b461d42007-11-16 16:14:42 -0800175 const char *name,
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000176 struct intel_region *region,
Eric Anholt3e168a02007-08-16 14:32:53 -0700177 intelRegion *region_desc,
178 GLuint mem_type)
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000179{
180 if (region) {
Eric Anholt9b461d42007-11-16 16:14:42 -0800181 intel_region_update_static(intelScreen, region, name, mem_type,
Eric Anholt3e168a02007-08-16 14:32:53 -0700182 region_desc->bo_handle, region_desc->offset,
183 region_desc->map, intelScreen->cpp,
184 region_desc->pitch / intelScreen->cpp,
185 intelScreen->height);
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000186 } else {
Eric Anholt9b461d42007-11-16 16:14:42 -0800187 region = intel_region_create_static(intelScreen, name, mem_type,
Eric Anholt3e168a02007-08-16 14:32:53 -0700188 region_desc->bo_handle,
189 region_desc->offset,
190 region_desc->map, intelScreen->cpp,
191 region_desc->pitch / intelScreen->cpp,
192 intelScreen->height);
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000193 }
Eric Anholt3e168a02007-08-16 14:32:53 -0700194
195 assert(region->buffer != NULL);
196
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000197 return region;
198}
199
200
201/* Create intel_region structs to describe the static front,back,depth
202 * buffers created by the xserver.
203 *
204 * Although FBO's mean we now no longer use these as render targets in
205 * all circumstances, they won't go away until the back and depth
Eric Anholt9724dc12007-11-09 15:05:56 -0800206 * buffers become private, and the front buffer will remain even then.
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000207 *
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 =
Eric Anholt9b461d42007-11-16 16:14:42 -0800215 intel_recreate_static(intelScreen, "front",
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000216 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
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000220 intelScreen->back_region =
Eric Anholt9b461d42007-11-16 16:14:42 -0800221 intel_recreate_static(intelScreen, "back",
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000222 intelScreen->back_region,
Eric Anholt3e168a02007-08-16 14:32:53 -0700223 &intelScreen->back,
224 DRM_BO_FLAG_MEM_TT);
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000225
Michel Dänzere33a9d62007-02-20 19:14:23 +0100226 if (intelScreen->third.handle) {
227 intelScreen->third_region =
Eric Anholt9b461d42007-11-16 16:14:42 -0800228 intel_recreate_static(intelScreen, "third",
Michel Dänzere33a9d62007-02-20 19:14:23 +0100229 intelScreen->third_region,
Eric Anholt3e168a02007-08-16 14:32:53 -0700230 &intelScreen->third,
231 DRM_BO_FLAG_MEM_TT);
Michel Dänzere33a9d62007-02-20 19:14:23 +0100232 }
233
Eric Anholt3e168a02007-08-16 14:32:53 -0700234 /* Still assumes front.cpp == depth.cpp. We can kill this when we move to
235 * private buffers.
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000236 */
237 intelScreen->depth_region =
Eric Anholt9b461d42007-11-16 16:14:42 -0800238 intel_recreate_static(intelScreen, "depth",
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000239 intelScreen->depth_region,
Eric Anholt3e168a02007-08-16 14:32:53 -0700240 &intelScreen->depth,
241 DRM_BO_FLAG_MEM_TT);
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000242}
243
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000244void
245intelUnmapScreenRegions(intelScreenPrivate * intelScreen)
246{
247#define REALLY_UNMAP 1
248 if (intelScreen->front.map) {
249#if REALLY_UNMAP
250 if (drmUnmap(intelScreen->front.map, intelScreen->front.size) != 0)
251 printf("drmUnmap front failed!\n");
252#endif
253 intelScreen->front.map = NULL;
254 }
255 if (intelScreen->back.map) {
256#if REALLY_UNMAP
257 if (drmUnmap(intelScreen->back.map, intelScreen->back.size) != 0)
258 printf("drmUnmap back failed!\n");
259#endif
260 intelScreen->back.map = NULL;
261 }
Michel Dänzere33a9d62007-02-20 19:14:23 +0100262 if (intelScreen->third.map) {
263#if REALLY_UNMAP
264 if (drmUnmap(intelScreen->third.map, intelScreen->third.size) != 0)
265 printf("drmUnmap third failed!\n");
266#endif
267 intelScreen->third.map = NULL;
268 }
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000269 if (intelScreen->depth.map) {
270#if REALLY_UNMAP
271 drmUnmap(intelScreen->depth.map, intelScreen->depth.size);
272 intelScreen->depth.map = NULL;
273#endif
274 }
275 if (intelScreen->tex.map) {
276#if REALLY_UNMAP
277 drmUnmap(intelScreen->tex.map, intelScreen->tex.size);
278 intelScreen->tex.map = NULL;
279#endif
280 }
281}
282
283
284static void
285intelPrintDRIInfo(intelScreenPrivate * intelScreen,
286 __DRIscreenPrivate * sPriv, I830DRIPtr gDRIPriv)
287{
288 fprintf(stderr, "*** Front size: 0x%x offset: 0x%x pitch: %d\n",
289 intelScreen->front.size, intelScreen->front.offset,
290 intelScreen->front.pitch);
291 fprintf(stderr, "*** Back size: 0x%x offset: 0x%x pitch: %d\n",
292 intelScreen->back.size, intelScreen->back.offset,
293 intelScreen->back.pitch);
294 fprintf(stderr, "*** Depth size: 0x%x offset: 0x%x pitch: %d\n",
295 intelScreen->depth.size, intelScreen->depth.offset,
296 intelScreen->depth.pitch);
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000297 fprintf(stderr, "*** Texture size: 0x%x offset: 0x%x\n",
298 intelScreen->tex.size, intelScreen->tex.offset);
299 fprintf(stderr, "*** Memory : 0x%x\n", gDRIPriv->mem);
300}
301
302
303static void
304intelPrintSAREA(const drmI830Sarea * sarea)
305{
306 fprintf(stderr, "SAREA: sarea width %d height %d\n", sarea->width,
307 sarea->height);
308 fprintf(stderr, "SAREA: pitch: %d\n", sarea->pitch);
309 fprintf(stderr,
310 "SAREA: front offset: 0x%08x size: 0x%x handle: 0x%x\n",
311 sarea->front_offset, sarea->front_size,
312 (unsigned) sarea->front_handle);
313 fprintf(stderr,
314 "SAREA: back offset: 0x%08x size: 0x%x handle: 0x%x\n",
315 sarea->back_offset, sarea->back_size,
316 (unsigned) sarea->back_handle);
317 fprintf(stderr, "SAREA: depth offset: 0x%08x size: 0x%x handle: 0x%x\n",
318 sarea->depth_offset, sarea->depth_size,
319 (unsigned) sarea->depth_handle);
320 fprintf(stderr, "SAREA: tex offset: 0x%08x size: 0x%x handle: 0x%x\n",
321 sarea->tex_offset, sarea->tex_size, (unsigned) sarea->tex_handle);
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000322}
323
324
325/**
326 * A number of the screen parameters are obtained/computed from
327 * information in the SAREA. This function updates those parameters.
328 */
329void
330intelUpdateScreenFromSAREA(intelScreenPrivate * intelScreen,
331 drmI830Sarea * sarea)
332{
333 intelScreen->width = sarea->width;
334 intelScreen->height = sarea->height;
335
336 intelScreen->front.offset = sarea->front_offset;
337 intelScreen->front.pitch = sarea->pitch * intelScreen->cpp;
338 intelScreen->front.handle = sarea->front_handle;
339 intelScreen->front.size = sarea->front_size;
340
341 intelScreen->back.offset = sarea->back_offset;
342 intelScreen->back.pitch = sarea->pitch * intelScreen->cpp;
343 intelScreen->back.handle = sarea->back_handle;
344 intelScreen->back.size = sarea->back_size;
345
Kristian Høgsbergefd03a22007-05-14 16:37:19 -0400346 if (intelScreen->driScrnPriv->ddx_version.minor >= 8) {
Michel Dänzere33a9d62007-02-20 19:14:23 +0100347 intelScreen->third.offset = sarea->third_offset;
348 intelScreen->third.pitch = sarea->pitch * intelScreen->cpp;
349 intelScreen->third.handle = sarea->third_handle;
350 intelScreen->third.size = sarea->third_size;
351 }
352
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000353 intelScreen->depth.offset = sarea->depth_offset;
354 intelScreen->depth.pitch = sarea->pitch * intelScreen->cpp;
355 intelScreen->depth.handle = sarea->depth_handle;
356 intelScreen->depth.size = sarea->depth_size;
357
Kristian Høgsbergefd03a22007-05-14 16:37:19 -0400358 if (intelScreen->driScrnPriv->ddx_version.minor >= 9) {
Eric Anholt3e168a02007-08-16 14:32:53 -0700359 intelScreen->front.bo_handle = sarea->front_bo_handle;
360 intelScreen->back.bo_handle = sarea->back_bo_handle;
361 intelScreen->third.bo_handle = sarea->third_bo_handle;
362 intelScreen->depth.bo_handle = sarea->depth_bo_handle;
363 } else {
364 intelScreen->front.bo_handle = -1;
365 intelScreen->back.bo_handle = -1;
366 intelScreen->third.bo_handle = -1;
367 intelScreen->depth.bo_handle = -1;
368 }
369
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000370 intelScreen->tex.offset = sarea->tex_offset;
371 intelScreen->logTextureGranularity = sarea->log_tex_granularity;
372 intelScreen->tex.handle = sarea->tex_handle;
373 intelScreen->tex.size = sarea->tex_size;
374
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000375 if (0)
376 intelPrintSAREA(sarea);
377}
378
Michel Dänzer9c4d1042007-10-16 13:01:44 +0200379static const __DRItexOffsetExtension intelTexOffsetExtension = {
380 { __DRI_TEX_OFFSET },
381 intelSetTexOffset,
382};
383
Kristian Høgsbergac3e8382007-05-15 15:17:30 -0400384static const __DRIextension *intelExtensions[] = {
Kristian Høgsbergf968f672007-05-17 14:39:06 -0400385 &driReadDrawableExtension,
Kristian Høgsbergac3e8382007-05-15 15:17:30 -0400386 &driCopySubBufferExtension.base,
Kristian Høgsbergefaf90b2007-05-15 16:09:44 -0400387 &driSwapControlExtension.base,
Kristian Høgsberga7a0a2b2007-05-16 15:50:40 -0400388 &driFrameTrackingExtension.base,
Kristian Høgsberg106a6f22007-05-16 18:13:41 -0400389 &driMediaStreamCounterExtension.base,
Michel Dänzer9c4d1042007-10-16 13:01:44 +0200390 &intelTexOffsetExtension.base,
Kristian Høgsbergac3e8382007-05-15 15:17:30 -0400391 NULL
392};
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000393
Kristian Høgsbergac3e8382007-05-15 15:17:30 -0400394
395static GLboolean intelInitDriver(__DRIscreenPrivate *sPriv)
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000396{
397 intelScreenPrivate *intelScreen;
398 I830DRIPtr gDRIPriv = (I830DRIPtr) sPriv->pDevPriv;
399 drmI830Sarea *sarea;
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000400
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000401 if (sPriv->devPrivSize != sizeof(I830DRIRec)) {
402 fprintf(stderr,
403 "\nERROR! sizeof(I830DRIRec) does not match passed size from device driver\n");
404 return GL_FALSE;
405 }
406
407 /* Allocate the private area */
408 intelScreen = (intelScreenPrivate *) CALLOC(sizeof(intelScreenPrivate));
409 if (!intelScreen) {
410 fprintf(stderr, "\nERROR! Allocating private area failed\n");
411 return GL_FALSE;
412 }
413 /* parse information in __driConfigOptions */
414 driParseOptionInfo(&intelScreen->optionCache,
415 __driConfigOptions, __driNConfigOptions);
416
417 intelScreen->driScrnPriv = sPriv;
418 sPriv->private = (void *) intelScreen;
419 intelScreen->sarea_priv_offset = gDRIPriv->sarea_priv_offset;
420 sarea = (drmI830Sarea *)
421 (((GLubyte *) sPriv->pSAREA) + intelScreen->sarea_priv_offset);
422
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000423 intelScreen->deviceID = gDRIPriv->deviceID;
424 if (intelScreen->deviceID == PCI_CHIP_I865_G)
425 intelScreen->maxBatchSize = 4096;
Eric Anholtcfc21192007-05-17 15:28:01 -0700426 else
427 intelScreen->maxBatchSize = BATCH_SZ;
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000428
429 intelScreen->mem = gDRIPriv->mem;
430 intelScreen->cpp = gDRIPriv->cpp;
431
432 switch (gDRIPriv->bitsPerPixel) {
433 case 16:
434 intelScreen->fbFormat = DV_PF_565;
435 break;
436 case 32:
437 intelScreen->fbFormat = DV_PF_8888;
438 break;
439 default:
440 exit(1);
441 break;
442 }
443
444 intelUpdateScreenFromSAREA(intelScreen, sarea);
445
446 if (!intelMapScreenRegions(sPriv)) {
447 fprintf(stderr, "\nERROR! mapping regions\n");
448 _mesa_free(intelScreen);
449 sPriv->private = NULL;
450 return GL_FALSE;
451 }
452
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000453 intelScreen->sarea_priv_offset = gDRIPriv->sarea_priv_offset;
454
Brian Paul4db0c892006-11-01 18:48:28 +0000455 if (0)
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000456 intelPrintDRIInfo(intelScreen, sPriv, gDRIPriv);
457
Kristian Høgsbergefd03a22007-05-14 16:37:19 -0400458 intelScreen->drmMinor = sPriv->drm_version.minor;
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000459
460 /* Determine if IRQs are active? */
461 {
462 int ret;
463 drmI830GetParam gp;
464
465 gp.param = I830_PARAM_IRQ_ACTIVE;
466 gp.value = &intelScreen->irq_active;
467
468 ret = drmCommandWriteRead(sPriv->fd, DRM_I830_GETPARAM,
469 &gp, sizeof(gp));
470 if (ret) {
471 fprintf(stderr, "drmI830GetParam: %d\n", ret);
472 return GL_FALSE;
473 }
474 }
475
476 /* Determine if batchbuffers are allowed */
477 {
478 int ret;
479 drmI830GetParam gp;
480
481 gp.param = I830_PARAM_ALLOW_BATCHBUFFER;
482 gp.value = &intelScreen->allow_batchbuffer;
483
484 ret = drmCommandWriteRead(sPriv->fd, DRM_I830_GETPARAM,
485 &gp, sizeof(gp));
486 if (ret) {
487 fprintf(stderr, "drmI830GetParam: (%d) %d\n", gp.param, ret);
488 return GL_FALSE;
489 }
490 }
491
Kristian Høgsbergac3e8382007-05-15 15:17:30 -0400492 sPriv->extensions = intelExtensions;
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000493
Eric Anholt3e168a02007-08-16 14:32:53 -0700494 /* If we've got a new enough DDX that's initializing TTM and giving us
495 * object handles for the shared buffers, use that.
496 */
497 intelScreen->ttm = GL_FALSE;
498 if (getenv("INTEL_NO_TTM") == NULL &&
Kristian Høgsbergefd03a22007-05-14 16:37:19 -0400499 intelScreen->driScrnPriv->ddx_version.minor >= 9 &&
Dave Airlie9e06cf02007-10-12 11:01:17 +1000500 intelScreen->drmMinor >= 11 &&
Eric Anholt3e168a02007-08-16 14:32:53 -0700501 intelScreen->front.bo_handle != -1) {
Dave Airliefa031c82007-10-03 16:54:59 +1000502 intelScreen->bufmgr = intel_bufmgr_ttm_init(sPriv->fd,
Dave Airlie8909f112007-10-09 14:36:04 +1000503 DRM_FENCE_TYPE_EXE,
504 DRM_FENCE_TYPE_EXE |
505 DRM_I915_FENCE_TYPE_RW,
506 BATCH_SZ);
Eric Anholt3e168a02007-08-16 14:32:53 -0700507 if (intelScreen->bufmgr != NULL)
508 intelScreen->ttm = GL_TRUE;
509 }
510 /* Otherwise, use the classic buffer manager. */
Eric Anholt1ddbfd72007-06-18 12:19:42 -0700511 if (intelScreen->bufmgr == NULL) {
512 if (intelScreen->tex.size == 0) {
513 fprintf(stderr, "[%s:%u] Error initializing buffer manager.\n",
514 __func__, __LINE__);
515 return GL_FALSE;
516 }
Eric Anholt9a4cc2e2007-07-25 10:12:23 -0700517 fprintf(stderr, "[%s:%u] Failed to init TTM buffer manager, falling back"
518 " to classic.\n", __func__, __LINE__);
Eric Anholtcfc21192007-05-17 15:28:01 -0700519 intelScreen->bufmgr = dri_bufmgr_fake_init(intelScreen->tex.offset,
520 intelScreen->tex.map,
521 intelScreen->tex.size,
522 intel_fence_emit,
523 intel_fence_wait,
524 intelScreen);
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000525 }
526
527 intel_recreate_static_regions(intelScreen);
528
529 return GL_TRUE;
530}
531
532
533static void
534intelDestroyScreen(__DRIscreenPrivate * sPriv)
535{
536 intelScreenPrivate *intelScreen = (intelScreenPrivate *) sPriv->private;
537
538 intelUnmapScreenRegions(intelScreen);
539
Eric Anholt2ac17c62007-05-31 09:45:45 -0700540 dri_bufmgr_destroy(intelScreen->bufmgr);
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000541 FREE(intelScreen);
542 sPriv->private = NULL;
543}
544
545
546/**
547 * This is called when we need to set up GL rendering to a new X window.
548 */
549static GLboolean
550intelCreateBuffer(__DRIscreenPrivate * driScrnPriv,
551 __DRIdrawablePrivate * driDrawPriv,
552 const __GLcontextModes * mesaVis, GLboolean isPixmap)
553{
554 intelScreenPrivate *screen = (intelScreenPrivate *) driScrnPriv->private;
555
556 if (isPixmap) {
557 return GL_FALSE; /* not implemented */
558 }
559 else {
560 GLboolean swStencil = (mesaVis->stencilBits > 0 &&
561 mesaVis->depthBits != 24);
562 GLenum rgbFormat = (mesaVis->redBits == 5 ? GL_RGB5 : GL_RGBA8);
563
Michel Dänzer6b99caf2007-02-15 16:30:40 +0100564 struct intel_framebuffer *intel_fb = CALLOC_STRUCT(intel_framebuffer);
565
566 if (!intel_fb)
567 return GL_FALSE;
568
569 _mesa_initialize_framebuffer(&intel_fb->Base, mesaVis);
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000570
571 /* setup the hardware-based renderbuffers */
572 {
Michel Dänzere33a9d62007-02-20 19:14:23 +0100573 intel_fb->color_rb[0]
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000574 = intel_create_renderbuffer(rgbFormat,
575 screen->width, screen->height,
576 screen->front.offset,
577 screen->front.pitch,
578 screen->cpp,
579 screen->front.map);
Michel Dänzere33a9d62007-02-20 19:14:23 +0100580 intel_set_span_functions(&intel_fb->color_rb[0]->Base);
Michel Dänzer6b99caf2007-02-15 16:30:40 +0100581 _mesa_add_renderbuffer(&intel_fb->Base, BUFFER_FRONT_LEFT,
Michel Dänzere33a9d62007-02-20 19:14:23 +0100582 &intel_fb->color_rb[0]->Base);
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000583 }
584
585 if (mesaVis->doubleBufferMode) {
Michel Dänzere33a9d62007-02-20 19:14:23 +0100586 intel_fb->color_rb[1]
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000587 = intel_create_renderbuffer(rgbFormat,
588 screen->width, screen->height,
589 screen->back.offset,
590 screen->back.pitch,
591 screen->cpp,
592 screen->back.map);
Michel Dänzere33a9d62007-02-20 19:14:23 +0100593 intel_set_span_functions(&intel_fb->color_rb[1]->Base);
Michel Dänzer6b99caf2007-02-15 16:30:40 +0100594 _mesa_add_renderbuffer(&intel_fb->Base, BUFFER_BACK_LEFT,
Michel Dänzere33a9d62007-02-20 19:14:23 +0100595 &intel_fb->color_rb[1]->Base);
596
597 if (screen->third.handle) {
Michel Dänzer76f3b662007-03-26 17:38:58 +0200598 struct gl_renderbuffer *tmp_rb = NULL;
599
Michel Dänzere33a9d62007-02-20 19:14:23 +0100600 intel_fb->color_rb[2]
601 = intel_create_renderbuffer(rgbFormat,
602 screen->width, screen->height,
603 screen->third.offset,
604 screen->third.pitch,
605 screen->cpp,
606 screen->third.map);
607 intel_set_span_functions(&intel_fb->color_rb[2]->Base);
Michel Dänzer76f3b662007-03-26 17:38:58 +0200608 _mesa_reference_renderbuffer(&tmp_rb, &intel_fb->color_rb[2]->Base);
Michel Dänzere33a9d62007-02-20 19:14:23 +0100609 }
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000610 }
611
Eric Anholt38c61622007-11-08 14:49:37 -0800612 if (mesaVis->depthBits == 24) {
613 if (mesaVis->stencilBits == 8) {
614 /* combined depth/stencil buffer */
615 struct intel_renderbuffer *depthStencilRb
616 = intel_create_renderbuffer(GL_DEPTH24_STENCIL8_EXT,
617 screen->width, screen->height,
618 screen->depth.offset,
619 screen->depth.pitch,
620 screen->cpp, /* 4! */
621 screen->depth.map);
622 intel_set_span_functions(&depthStencilRb->Base);
623 /* note: bind RB to two attachment points */
624 _mesa_add_renderbuffer(&intel_fb->Base, BUFFER_DEPTH,
625 &depthStencilRb->Base);
626 _mesa_add_renderbuffer(&intel_fb->Base, BUFFER_STENCIL,
627 &depthStencilRb->Base);
628 } else {
629 struct intel_renderbuffer *depthRb
630 = intel_create_renderbuffer(GL_DEPTH_COMPONENT24,
631 screen->width, screen->height,
632 screen->depth.offset,
633 screen->depth.pitch,
634 screen->cpp, /* 4! */
635 screen->depth.map);
636 intel_set_span_functions(&depthRb->Base);
637 _mesa_add_renderbuffer(&intel_fb->Base, BUFFER_DEPTH,
638 &depthRb->Base);
639 }
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000640 }
641 else if (mesaVis->depthBits == 16) {
642 /* just 16-bit depth buffer, no hw stencil */
643 struct intel_renderbuffer *depthRb
644 = intel_create_renderbuffer(GL_DEPTH_COMPONENT16,
645 screen->width, screen->height,
646 screen->depth.offset,
647 screen->depth.pitch,
648 screen->cpp, /* 2! */
649 screen->depth.map);
650 intel_set_span_functions(&depthRb->Base);
Michel Dänzer6b99caf2007-02-15 16:30:40 +0100651 _mesa_add_renderbuffer(&intel_fb->Base, BUFFER_DEPTH, &depthRb->Base);
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000652 }
653
654 /* now add any/all software-based renderbuffers we may need */
Michel Dänzer6b99caf2007-02-15 16:30:40 +0100655 _mesa_add_soft_renderbuffers(&intel_fb->Base,
656 GL_FALSE, /* never sw color */
657 GL_FALSE, /* never sw depth */
658 swStencil, mesaVis->accumRedBits > 0,
659 GL_FALSE, /* never sw alpha */
660 GL_FALSE /* never sw aux */ );
661 driDrawPriv->driverPrivate = (void *) intel_fb;
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000662
Michel Dänzer6b99caf2007-02-15 16:30:40 +0100663 return GL_TRUE;
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000664 }
665}
666
667static void
668intelDestroyBuffer(__DRIdrawablePrivate * driDrawPriv)
669{
Briana510bc32007-03-06 10:07:59 -0700670 _mesa_unreference_framebuffer((GLframebuffer **)(&(driDrawPriv->driverPrivate)));
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000671}
672
673
674/**
675 * Get information about previous buffer swaps.
676 */
677static int
678intelGetSwapInfo(__DRIdrawablePrivate * dPriv, __DRIswapInfo * sInfo)
679{
Michel Dänzer641c9662007-02-22 17:24:09 +0100680 struct intel_framebuffer *intel_fb;
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000681
Michel Dänzer641c9662007-02-22 17:24:09 +0100682 if ((dPriv == NULL) || (dPriv->driverPrivate == NULL)
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000683 || (sInfo == NULL)) {
684 return -1;
685 }
686
Michel Dänzer641c9662007-02-22 17:24:09 +0100687 intel_fb = dPriv->driverPrivate;
688 sInfo->swap_count = intel_fb->swap_count;
689 sInfo->swap_ust = intel_fb->swap_ust;
690 sInfo->swap_missed_count = intel_fb->swap_missed_count;
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000691
692 sInfo->swap_missed_usage = (sInfo->swap_missed_count != 0)
Michel Dänzer641c9662007-02-22 17:24:09 +0100693 ? driCalculateSwapUsage(dPriv, 0, intel_fb->swap_missed_ust)
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000694 : 0.0;
695
696 return 0;
697}
698
699
700/* There are probably better ways to do this, such as an
701 * init-designated function to register chipids and createcontext
702 * functions.
703 */
704extern GLboolean i830CreateContext(const __GLcontextModes * mesaVis,
705 __DRIcontextPrivate * driContextPriv,
706 void *sharedContextPrivate);
707
708extern GLboolean i915CreateContext(const __GLcontextModes * mesaVis,
709 __DRIcontextPrivate * driContextPriv,
710 void *sharedContextPrivate);
711
712
713
714
715static GLboolean
716intelCreateContext(const __GLcontextModes * mesaVis,
717 __DRIcontextPrivate * driContextPriv,
718 void *sharedContextPrivate)
719{
720 __DRIscreenPrivate *sPriv = driContextPriv->driScreenPriv;
721 intelScreenPrivate *intelScreen = (intelScreenPrivate *) sPriv->private;
722
723 switch (intelScreen->deviceID) {
724 /* Don't deal with i830 until texture work complete:
725 */
726 case PCI_CHIP_845_G:
727 case PCI_CHIP_I830_M:
728 case PCI_CHIP_I855_GM:
729 case PCI_CHIP_I865_G:
730 return i830CreateContext(mesaVis, driContextPriv, sharedContextPrivate);
731
732 case PCI_CHIP_I915_G:
733 case PCI_CHIP_I915_GM:
734 case PCI_CHIP_I945_G:
735 case PCI_CHIP_I945_GM:
Wang Zhenyuad6351a2007-05-30 16:18:26 +0800736 case PCI_CHIP_I945_GME:
Wang Zhenyu8331d9d2007-06-05 11:42:43 -0700737 case PCI_CHIP_G33_G:
738 case PCI_CHIP_Q35_G:
739 case PCI_CHIP_Q33_G:
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000740 return i915CreateContext(mesaVis, driContextPriv, sharedContextPrivate);
741
742 default:
743 fprintf(stderr, "Unrecognized deviceID %x\n", intelScreen->deviceID);
744 return GL_FALSE;
745 }
746}
747
748
749static const struct __DriverAPIRec intelAPI = {
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000750 .DestroyScreen = intelDestroyScreen,
751 .CreateContext = intelCreateContext,
752 .DestroyContext = intelDestroyContext,
753 .CreateBuffer = intelCreateBuffer,
754 .DestroyBuffer = intelDestroyBuffer,
755 .SwapBuffers = intelSwapBuffers,
756 .MakeCurrent = intelMakeCurrent,
757 .UnbindContext = intelUnbindContext,
758 .GetSwapInfo = intelGetSwapInfo,
759 .GetMSC = driGetMSC32,
Jesse Barnes38fdb472007-10-29 11:56:31 -0700760 .GetDrawableMSC = driDrawableGetMSC32,
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000761 .WaitForMSC = driWaitForMSC32,
762 .WaitForSBC = NULL,
763 .SwapBuffersMSC = NULL,
Michel Dänzere0885b82007-05-22 14:08:11 +0200764 .CopySubBuffer = intelCopySubBuffer,
765 .setTexOffset = intelSetTexOffset,
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000766};
767
768
769static __GLcontextModes *
770intelFillInModes(unsigned pixel_bits, unsigned depth_bits,
771 unsigned stencil_bits, GLboolean have_back_buffer)
772{
773 __GLcontextModes *modes;
774 __GLcontextModes *m;
775 unsigned num_modes;
776 unsigned depth_buffer_factor;
777 unsigned back_buffer_factor;
778 GLenum fb_format;
779 GLenum fb_type;
780
781 /* GLX_SWAP_COPY_OML is only supported because the Intel driver doesn't
782 * support pageflipping at all.
783 */
784 static const GLenum back_buffer_modes[] = {
785 GLX_NONE, GLX_SWAP_UNDEFINED_OML, GLX_SWAP_COPY_OML
786 };
787
788 u_int8_t depth_bits_array[3];
789 u_int8_t stencil_bits_array[3];
790
791
792 depth_bits_array[0] = 0;
793 depth_bits_array[1] = depth_bits;
794 depth_bits_array[2] = depth_bits;
795
796 /* Just like with the accumulation buffer, always provide some modes
797 * with a stencil buffer. It will be a sw fallback, but some apps won't
798 * care about that.
799 */
800 stencil_bits_array[0] = 0;
801 stencil_bits_array[1] = 0;
Dave Airlieb6becfa2006-12-31 10:01:17 +1100802 if (depth_bits == 24)
803 stencil_bits_array[1] = (stencil_bits == 0) ? 8 : stencil_bits;
804
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000805 stencil_bits_array[2] = (stencil_bits == 0) ? 8 : stencil_bits;
806
807 depth_buffer_factor = ((depth_bits != 0) || (stencil_bits != 0)) ? 3 : 1;
808 back_buffer_factor = (have_back_buffer) ? 3 : 1;
809
810 num_modes = depth_buffer_factor * back_buffer_factor * 4;
811
812 if (pixel_bits == 16) {
813 fb_format = GL_RGB;
814 fb_type = GL_UNSIGNED_SHORT_5_6_5;
815 }
816 else {
817 fb_format = GL_BGRA;
818 fb_type = GL_UNSIGNED_INT_8_8_8_8_REV;
819 }
820
821 modes =
822 (*dri_interface->createContextModes) (num_modes,
823 sizeof(__GLcontextModes));
824 m = modes;
825 if (!driFillInModes(&m, fb_format, fb_type,
826 depth_bits_array, stencil_bits_array,
827 depth_buffer_factor, back_buffer_modes,
828 back_buffer_factor, GLX_TRUE_COLOR)) {
829 fprintf(stderr, "[%s:%u] Error creating FBConfig!\n", __func__,
830 __LINE__);
831 return NULL;
832 }
833 if (!driFillInModes(&m, fb_format, fb_type,
834 depth_bits_array, stencil_bits_array,
835 depth_buffer_factor, back_buffer_modes,
836 back_buffer_factor, GLX_DIRECT_COLOR)) {
837 fprintf(stderr, "[%s:%u] Error creating FBConfig!\n", __func__,
838 __LINE__);
839 return NULL;
840 }
841
842 /* Mark the visual as slow if there are "fake" stencil bits.
843 */
844 for (m = modes; m != NULL; m = m->next) {
845 if ((m->stencilBits != 0) && (m->stencilBits != stencil_bits)) {
846 m->visualRating = GLX_SLOW_CONFIG;
847 }
848 }
849
850 return modes;
851}
852
853
854/**
Kristian Høgsberg64106d02007-05-14 16:58:37 -0400855 * This is the driver specific part of the createNewScreen entry point.
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000856 *
Kristian Høgsberg64106d02007-05-14 16:58:37 -0400857 * \todo maybe fold this into intelInitDriver
858 *
859 * \return the __GLcontextModes supported by this driver
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000860 */
Kristian Høgsberg64106d02007-05-14 16:58:37 -0400861PUBLIC __GLcontextModes *__driDriverInitScreen(__DRIscreenPrivate *psp)
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000862{
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000863 static const __DRIversion ddx_expected = { 1, 5, 0 };
864 static const __DRIversion dri_expected = { 4, 0, 0 };
Eric Anholt4cf2cc02007-05-18 11:29:55 -0700865 static const __DRIversion drm_expected = { 1, 5, 0 };
Kristian Høgsberg64106d02007-05-14 16:58:37 -0400866 I830DRIPtr dri_priv = (I830DRIPtr) psp->pDevPriv;
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000867
Kristian Høgsberg64106d02007-05-14 16:58:37 -0400868 psp->DriverAPI = intelAPI;
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000869
870 if (!driCheckDriDdxDrmVersions2("i915",
Kristian Høgsberg64106d02007-05-14 16:58:37 -0400871 &psp->dri_version, &dri_expected,
872 &psp->ddx_version, &ddx_expected,
873 &psp->drm_version, &drm_expected)) {
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000874 return NULL;
875 }
876
Kristian Høgsberg64106d02007-05-14 16:58:37 -0400877 /* Calling driInitExtensions here, with a NULL context pointer,
878 * does not actually enable the extensions. It just makes sure
879 * that all the dispatch offsets for all the extensions that
880 * *might* be enables are known. This is needed because the
881 * dispatch offsets need to be known when _mesa_context_create is
882 * called, but we can't enable the extensions until we have a
883 * context pointer.
884 *
885 * Hello chicken. Hello egg. How are you two today?
886 */
887 driInitExtensions(NULL, card_extensions, GL_FALSE);
Michel Dänzer3feefee2007-10-16 15:48:46 +0200888 driInitExtensions(NULL, ttm_extensions, GL_FALSE);
Thomas Hellstromda56df92007-04-16 16:04:12 +0200889
Kristian Høgsberg64106d02007-05-14 16:58:37 -0400890 if (!intelInitDriver(psp))
891 return NULL;
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000892
Kristian Høgsberg64106d02007-05-14 16:58:37 -0400893 return intelFillInModes(dri_priv->cpp * 8,
894 (dri_priv->cpp == 2) ? 16 : 24,
895 (dri_priv->cpp == 2) ? 0 : 8, 1);
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000896}
897
898struct intel_context *intelScreenContext(intelScreenPrivate *intelScreen)
899{
900 /*
901 * This should probably change to have the screen allocate a dummy
902 * context at screen creation. For now just use the current context.
903 */
904
905 GET_CURRENT_CONTEXT(ctx);
906 if (ctx == NULL) {
907 _mesa_problem(NULL, "No current context in intelScreenContext\n");
908 return NULL;
909 }
910 return intel_context(ctx);
911}
912