blob: 041ff6bef087113d0f019a5b6d28fbbf2df99551 [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
53PUBLIC const char __driConfigOptions[] =
54 DRI_CONF_BEGIN DRI_CONF_SECTION_PERFORMANCE
55 DRI_CONF_FTHROTTLE_MODE(DRI_CONF_FTHROTTLE_IRQS)
56 DRI_CONF_VBLANK_MODE(DRI_CONF_VBLANK_DEF_INTERVAL_0)
57 DRI_CONF_SECTION_END DRI_CONF_SECTION_QUALITY
58 DRI_CONF_FORCE_S3TC_ENABLE(false)
59 DRI_CONF_ALLOW_LARGE_TEXTURES(1)
60 DRI_CONF_SECTION_END DRI_CONF_END;
61 const GLuint __driNConfigOptions = 4;
62
63#ifdef USE_NEW_INTERFACE
64 static PFNGLXCREATECONTEXTMODES create_context_modes = NULL;
65#endif /*USE_NEW_INTERFACE */
66
67 extern const struct dri_extension card_extensions[];
68
69/**
70 * Map all the memory regions described by the screen.
71 * \return GL_TRUE if success, GL_FALSE if error.
72 */
73GLboolean
74intelMapScreenRegions(__DRIscreenPrivate * sPriv)
75{
76 intelScreenPrivate *intelScreen = (intelScreenPrivate *) sPriv->private;
77
78 if (intelScreen->front.handle) {
79 if (drmMap(sPriv->fd,
80 intelScreen->front.handle,
81 intelScreen->front.size,
82 (drmAddress *) & intelScreen->front.map) != 0) {
83 _mesa_problem(NULL, "drmMap(frontbuffer) failed!");
84 return GL_FALSE;
85 }
86 }
87 else {
88 _mesa_warning(NULL, "no front buffer handle in intelMapScreenRegions!");
89 }
90
Brian Paul4db0c892006-11-01 18:48:28 +000091 if (0)
92 _mesa_printf("Back 0x%08x ", intelScreen->back.handle);
Keith Whitwell6b9e31f2006-11-01 12:03:11 +000093 if (drmMap(sPriv->fd,
94 intelScreen->back.handle,
95 intelScreen->back.size,
96 (drmAddress *) & intelScreen->back.map) != 0) {
97 intelUnmapScreenRegions(intelScreen);
98 return GL_FALSE;
99 }
100
Michel Dänzere33a9d62007-02-20 19:14:23 +0100101 if (intelScreen->third.handle) {
102 if (0)
103 _mesa_printf("Third 0x%08x ", intelScreen->third.handle);
104 if (drmMap(sPriv->fd,
105 intelScreen->third.handle,
106 intelScreen->third.size,
107 (drmAddress *) & intelScreen->third.map) != 0) {
108 intelUnmapScreenRegions(intelScreen);
109 return GL_FALSE;
110 }
111 }
112
Brian Paul4db0c892006-11-01 18:48:28 +0000113 if (0)
114 _mesa_printf("Depth 0x%08x ", intelScreen->depth.handle);
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000115 if (drmMap(sPriv->fd,
116 intelScreen->depth.handle,
117 intelScreen->depth.size,
118 (drmAddress *) & intelScreen->depth.map) != 0) {
119 intelUnmapScreenRegions(intelScreen);
120 return GL_FALSE;
121 }
122
Eric Anholtcfc21192007-05-17 15:28:01 -0700123 if (0)
124 _mesa_printf("TEX 0x%08x ", intelScreen->tex.handle);
125 if (intelScreen->tex.size != 0) {
Eric Anholtcfc21192007-05-17 15:28:01 -0700126 if (drmMap(sPriv->fd,
127 intelScreen->tex.handle,
128 intelScreen->tex.size,
129 (drmAddress *) & intelScreen->tex.map) != 0) {
130 intelUnmapScreenRegions(intelScreen);
131 return GL_FALSE;
132 }
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000133 }
Eric Anholtcfc21192007-05-17 15:28:01 -0700134
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000135 if (0)
Michel Dänzere33a9d62007-02-20 19:14:23 +0100136 printf("Mappings: front: %p back: %p third: %p depth: %p tex: %p\n",
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000137 intelScreen->front.map,
Michel Dänzere33a9d62007-02-20 19:14:23 +0100138 intelScreen->back.map, intelScreen->third.map,
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000139 intelScreen->depth.map, intelScreen->tex.map);
140 return GL_TRUE;
141}
142
Eric Anholtcfc21192007-05-17 15:28:01 -0700143/** Driver-specific fence emit implementation for the fake memory manager. */
144static unsigned int
145intel_fence_emit(void *private)
146{
147 intelScreenPrivate *intelScreen = (intelScreenPrivate *)private;
148 unsigned int fence;
149
150 /* XXX: Need to emit a flush, if we haven't already (at least with the
151 * current batchbuffer implementation, we have).
152 */
153
154 fence = intelEmitIrqLocked(intelScreen);
155
156 return fence;
157}
158
159/** Driver-specific fence wait implementation for the fake memory manager. */
160static int
161intel_fence_wait(void *private, unsigned int cookie)
162{
163 intelScreenPrivate *intelScreen = (intelScreenPrivate *)private;
164
165 intelWaitIrq(intelScreen, cookie);
166
167 return 0;
168}
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000169
170static struct intel_region *
171intel_recreate_static(intelScreenPrivate *intelScreen,
172 struct intel_region *region,
Eric Anholt3e168a02007-08-16 14:32:53 -0700173 intelRegion *region_desc,
174 GLuint mem_type)
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000175{
176 if (region) {
Eric Anholt3e168a02007-08-16 14:32:53 -0700177 intel_region_update_static(intelScreen, region, mem_type,
178 region_desc->bo_handle, region_desc->offset,
179 region_desc->map, intelScreen->cpp,
180 region_desc->pitch / intelScreen->cpp,
181 intelScreen->height);
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000182 } else {
Eric Anholt3e168a02007-08-16 14:32:53 -0700183 region = intel_region_create_static(intelScreen, mem_type,
184 region_desc->bo_handle,
185 region_desc->offset,
186 region_desc->map, intelScreen->cpp,
187 region_desc->pitch / intelScreen->cpp,
188 intelScreen->height);
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000189 }
Eric Anholt3e168a02007-08-16 14:32:53 -0700190
191 assert(region->buffer != NULL);
192
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000193 return region;
194}
195
196
197/* Create intel_region structs to describe the static front,back,depth
198 * buffers created by the xserver.
199 *
200 * Although FBO's mean we now no longer use these as render targets in
201 * all circumstances, they won't go away until the back and depth
202 * buffers become private, and the front and rotated buffers will
203 * remain even then.
204 *
205 * Note that these don't allocate video memory, just describe
206 * allocations alread made by the X server.
207 */
208static void
209intel_recreate_static_regions(intelScreenPrivate *intelScreen)
210{
211 intelScreen->front_region =
212 intel_recreate_static(intelScreen,
213 intelScreen->front_region,
Eric Anholt3e168a02007-08-16 14:32:53 -0700214 &intelScreen->front,
215 DRM_BO_FLAG_MEM_TT);
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000216
Eric Anholt3e168a02007-08-16 14:32:53 -0700217 /* The rotated region is only used for old DDXes that didn't handle rotation
218\ * on their own.
219 */
Kristian Høgsbergefd03a22007-05-14 16:37:19 -0400220 if (intelScreen->driScrnPriv->ddx_version.minor < 8) {
Eric Anholt3e168a02007-08-16 14:32:53 -0700221 intelScreen->rotated_region =
222 intel_recreate_static(intelScreen,
223 intelScreen->rotated_region,
224 &intelScreen->rotated,
225 DRM_BO_FLAG_MEM_TT);
226 }
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000227
228 intelScreen->back_region =
229 intel_recreate_static(intelScreen,
230 intelScreen->back_region,
Eric Anholt3e168a02007-08-16 14:32:53 -0700231 &intelScreen->back,
232 DRM_BO_FLAG_MEM_TT);
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000233
Michel Dänzere33a9d62007-02-20 19:14:23 +0100234 if (intelScreen->third.handle) {
235 intelScreen->third_region =
236 intel_recreate_static(intelScreen,
237 intelScreen->third_region,
Eric Anholt3e168a02007-08-16 14:32:53 -0700238 &intelScreen->third,
239 DRM_BO_FLAG_MEM_TT);
Michel Dänzere33a9d62007-02-20 19:14:23 +0100240 }
241
Eric Anholt3e168a02007-08-16 14:32:53 -0700242 /* Still assumes front.cpp == depth.cpp. We can kill this when we move to
243 * private buffers.
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000244 */
245 intelScreen->depth_region =
246 intel_recreate_static(intelScreen,
247 intelScreen->depth_region,
Eric Anholt3e168a02007-08-16 14:32:53 -0700248 &intelScreen->depth,
249 DRM_BO_FLAG_MEM_TT);
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000250}
251
252/**
253 * Use the information in the sarea to update the screen parameters
254 * related to screen rotation. Needs to be called locked.
255 */
256void
257intelUpdateScreenRotation(__DRIscreenPrivate * sPriv, drmI830Sarea * sarea)
258{
259 intelScreenPrivate *intelScreen = (intelScreenPrivate *) sPriv->private;
260
261 intelUnmapScreenRegions(intelScreen);
262 intelUpdateScreenFromSAREA(intelScreen, sarea);
263 if (!intelMapScreenRegions(sPriv)) {
264 fprintf(stderr, "ERROR Remapping screen regions!!!\n");
265 }
266 intel_recreate_static_regions(intelScreen);
267}
268
269
270void
271intelUnmapScreenRegions(intelScreenPrivate * intelScreen)
272{
273#define REALLY_UNMAP 1
274 if (intelScreen->front.map) {
275#if REALLY_UNMAP
276 if (drmUnmap(intelScreen->front.map, intelScreen->front.size) != 0)
277 printf("drmUnmap front failed!\n");
278#endif
279 intelScreen->front.map = NULL;
280 }
281 if (intelScreen->back.map) {
282#if REALLY_UNMAP
283 if (drmUnmap(intelScreen->back.map, intelScreen->back.size) != 0)
284 printf("drmUnmap back failed!\n");
285#endif
286 intelScreen->back.map = NULL;
287 }
Michel Dänzere33a9d62007-02-20 19:14:23 +0100288 if (intelScreen->third.map) {
289#if REALLY_UNMAP
290 if (drmUnmap(intelScreen->third.map, intelScreen->third.size) != 0)
291 printf("drmUnmap third failed!\n");
292#endif
293 intelScreen->third.map = NULL;
294 }
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000295 if (intelScreen->depth.map) {
296#if REALLY_UNMAP
297 drmUnmap(intelScreen->depth.map, intelScreen->depth.size);
298 intelScreen->depth.map = NULL;
299#endif
300 }
301 if (intelScreen->tex.map) {
302#if REALLY_UNMAP
303 drmUnmap(intelScreen->tex.map, intelScreen->tex.size);
304 intelScreen->tex.map = NULL;
305#endif
306 }
307}
308
309
310static void
311intelPrintDRIInfo(intelScreenPrivate * intelScreen,
312 __DRIscreenPrivate * sPriv, I830DRIPtr gDRIPriv)
313{
314 fprintf(stderr, "*** Front size: 0x%x offset: 0x%x pitch: %d\n",
315 intelScreen->front.size, intelScreen->front.offset,
316 intelScreen->front.pitch);
317 fprintf(stderr, "*** Back size: 0x%x offset: 0x%x pitch: %d\n",
318 intelScreen->back.size, intelScreen->back.offset,
319 intelScreen->back.pitch);
320 fprintf(stderr, "*** Depth size: 0x%x offset: 0x%x pitch: %d\n",
321 intelScreen->depth.size, intelScreen->depth.offset,
322 intelScreen->depth.pitch);
323 fprintf(stderr, "*** Rotated size: 0x%x offset: 0x%x pitch: %d\n",
324 intelScreen->rotated.size, intelScreen->rotated.offset,
325 intelScreen->rotated.pitch);
326 fprintf(stderr, "*** Texture size: 0x%x offset: 0x%x\n",
327 intelScreen->tex.size, intelScreen->tex.offset);
328 fprintf(stderr, "*** Memory : 0x%x\n", gDRIPriv->mem);
329}
330
331
332static void
333intelPrintSAREA(const drmI830Sarea * sarea)
334{
335 fprintf(stderr, "SAREA: sarea width %d height %d\n", sarea->width,
336 sarea->height);
337 fprintf(stderr, "SAREA: pitch: %d\n", sarea->pitch);
338 fprintf(stderr,
339 "SAREA: front offset: 0x%08x size: 0x%x handle: 0x%x\n",
340 sarea->front_offset, sarea->front_size,
341 (unsigned) sarea->front_handle);
342 fprintf(stderr,
343 "SAREA: back offset: 0x%08x size: 0x%x handle: 0x%x\n",
344 sarea->back_offset, sarea->back_size,
345 (unsigned) sarea->back_handle);
346 fprintf(stderr, "SAREA: depth offset: 0x%08x size: 0x%x handle: 0x%x\n",
347 sarea->depth_offset, sarea->depth_size,
348 (unsigned) sarea->depth_handle);
349 fprintf(stderr, "SAREA: tex offset: 0x%08x size: 0x%x handle: 0x%x\n",
350 sarea->tex_offset, sarea->tex_size, (unsigned) sarea->tex_handle);
351 fprintf(stderr, "SAREA: rotation: %d\n", sarea->rotation);
352 fprintf(stderr,
353 "SAREA: rotated offset: 0x%08x size: 0x%x\n",
354 sarea->rotated_offset, sarea->rotated_size);
355 fprintf(stderr, "SAREA: rotated pitch: %d\n", sarea->rotated_pitch);
356}
357
358
359/**
360 * A number of the screen parameters are obtained/computed from
361 * information in the SAREA. This function updates those parameters.
362 */
363void
364intelUpdateScreenFromSAREA(intelScreenPrivate * intelScreen,
365 drmI830Sarea * sarea)
366{
367 intelScreen->width = sarea->width;
368 intelScreen->height = sarea->height;
369
370 intelScreen->front.offset = sarea->front_offset;
371 intelScreen->front.pitch = sarea->pitch * intelScreen->cpp;
372 intelScreen->front.handle = sarea->front_handle;
373 intelScreen->front.size = sarea->front_size;
374
375 intelScreen->back.offset = sarea->back_offset;
376 intelScreen->back.pitch = sarea->pitch * intelScreen->cpp;
377 intelScreen->back.handle = sarea->back_handle;
378 intelScreen->back.size = sarea->back_size;
379
Kristian Høgsbergefd03a22007-05-14 16:37:19 -0400380 if (intelScreen->driScrnPriv->ddx_version.minor >= 8) {
Michel Dänzere33a9d62007-02-20 19:14:23 +0100381 intelScreen->third.offset = sarea->third_offset;
382 intelScreen->third.pitch = sarea->pitch * intelScreen->cpp;
383 intelScreen->third.handle = sarea->third_handle;
384 intelScreen->third.size = sarea->third_size;
385 }
386
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000387 intelScreen->depth.offset = sarea->depth_offset;
388 intelScreen->depth.pitch = sarea->pitch * intelScreen->cpp;
389 intelScreen->depth.handle = sarea->depth_handle;
390 intelScreen->depth.size = sarea->depth_size;
391
Kristian Høgsbergefd03a22007-05-14 16:37:19 -0400392 if (intelScreen->driScrnPriv->ddx_version.minor >= 9) {
Eric Anholt3e168a02007-08-16 14:32:53 -0700393 intelScreen->front.bo_handle = sarea->front_bo_handle;
394 intelScreen->back.bo_handle = sarea->back_bo_handle;
395 intelScreen->third.bo_handle = sarea->third_bo_handle;
396 intelScreen->depth.bo_handle = sarea->depth_bo_handle;
397 } else {
398 intelScreen->front.bo_handle = -1;
399 intelScreen->back.bo_handle = -1;
400 intelScreen->third.bo_handle = -1;
401 intelScreen->depth.bo_handle = -1;
402 }
403
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000404 intelScreen->tex.offset = sarea->tex_offset;
405 intelScreen->logTextureGranularity = sarea->log_tex_granularity;
406 intelScreen->tex.handle = sarea->tex_handle;
407 intelScreen->tex.size = sarea->tex_size;
408
409 intelScreen->rotated.offset = sarea->rotated_offset;
410 intelScreen->rotated.pitch = sarea->rotated_pitch * intelScreen->cpp;
411 intelScreen->rotated.size = sarea->rotated_size;
412 intelScreen->current_rotation = sarea->rotation;
413 matrix23Rotate(&intelScreen->rotMatrix,
414 sarea->width, sarea->height, sarea->rotation);
415 intelScreen->rotatedWidth = sarea->virtualX;
416 intelScreen->rotatedHeight = sarea->virtualY;
417
418 if (0)
419 intelPrintSAREA(sarea);
420}
421
Kristian Høgsberg78a6aa52007-05-16 14:10:29 -0400422static const __DRIallocateExtension intelAllocateExtension = {
423 { __DRI_ALLOCATE },
424 intelAllocateMemoryMESA,
425 intelFreeMemoryMESA,
426 intelGetMemoryOffsetMESA
427};
428
Kristian Høgsbergac3e8382007-05-15 15:17:30 -0400429static const __DRIextension *intelExtensions[] = {
430 &driCopySubBufferExtension.base,
Kristian Høgsbergefaf90b2007-05-15 16:09:44 -0400431 &driSwapControlExtension.base,
Kristian Høgsberg78a6aa52007-05-16 14:10:29 -0400432 &intelAllocateExtension.base,
Kristian Høgsberga7a0a2b2007-05-16 15:50:40 -0400433 &driFrameTrackingExtension.base,
Kristian Høgsbergac3e8382007-05-15 15:17:30 -0400434 NULL
435};
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000436
Kristian Høgsbergac3e8382007-05-15 15:17:30 -0400437
438static GLboolean intelInitDriver(__DRIscreenPrivate *sPriv)
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000439{
440 intelScreenPrivate *intelScreen;
441 I830DRIPtr gDRIPriv = (I830DRIPtr) sPriv->pDevPriv;
442 drmI830Sarea *sarea;
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000443
444 PFNGLXSCRENABLEEXTENSIONPROC glx_enable_extension =
445 (PFNGLXSCRENABLEEXTENSIONPROC) (*dri_interface->
446 getProcAddress("glxEnableExtension"));
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000447
448 if (sPriv->devPrivSize != sizeof(I830DRIRec)) {
449 fprintf(stderr,
450 "\nERROR! sizeof(I830DRIRec) does not match passed size from device driver\n");
451 return GL_FALSE;
452 }
453
454 /* Allocate the private area */
455 intelScreen = (intelScreenPrivate *) CALLOC(sizeof(intelScreenPrivate));
456 if (!intelScreen) {
457 fprintf(stderr, "\nERROR! Allocating private area failed\n");
458 return GL_FALSE;
459 }
460 /* parse information in __driConfigOptions */
461 driParseOptionInfo(&intelScreen->optionCache,
462 __driConfigOptions, __driNConfigOptions);
463
464 intelScreen->driScrnPriv = sPriv;
465 sPriv->private = (void *) intelScreen;
466 intelScreen->sarea_priv_offset = gDRIPriv->sarea_priv_offset;
467 sarea = (drmI830Sarea *)
468 (((GLubyte *) sPriv->pSAREA) + intelScreen->sarea_priv_offset);
469
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000470 intelScreen->deviceID = gDRIPriv->deviceID;
471 if (intelScreen->deviceID == PCI_CHIP_I865_G)
472 intelScreen->maxBatchSize = 4096;
Eric Anholtcfc21192007-05-17 15:28:01 -0700473 else
474 intelScreen->maxBatchSize = BATCH_SZ;
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000475
476 intelScreen->mem = gDRIPriv->mem;
477 intelScreen->cpp = gDRIPriv->cpp;
478
479 switch (gDRIPriv->bitsPerPixel) {
480 case 16:
481 intelScreen->fbFormat = DV_PF_565;
482 break;
483 case 32:
484 intelScreen->fbFormat = DV_PF_8888;
485 break;
486 default:
487 exit(1);
488 break;
489 }
490
491 intelUpdateScreenFromSAREA(intelScreen, sarea);
492
493 if (!intelMapScreenRegions(sPriv)) {
494 fprintf(stderr, "\nERROR! mapping regions\n");
495 _mesa_free(intelScreen);
496 sPriv->private = NULL;
497 return GL_FALSE;
498 }
499
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000500 intelScreen->sarea_priv_offset = gDRIPriv->sarea_priv_offset;
501
Brian Paul4db0c892006-11-01 18:48:28 +0000502 if (0)
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000503 intelPrintDRIInfo(intelScreen, sPriv, gDRIPriv);
504
Kristian Høgsbergefd03a22007-05-14 16:37:19 -0400505 intelScreen->drmMinor = sPriv->drm_version.minor;
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000506
507 /* Determine if IRQs are active? */
508 {
509 int ret;
510 drmI830GetParam gp;
511
512 gp.param = I830_PARAM_IRQ_ACTIVE;
513 gp.value = &intelScreen->irq_active;
514
515 ret = drmCommandWriteRead(sPriv->fd, DRM_I830_GETPARAM,
516 &gp, sizeof(gp));
517 if (ret) {
518 fprintf(stderr, "drmI830GetParam: %d\n", ret);
519 return GL_FALSE;
520 }
521 }
522
523 /* Determine if batchbuffers are allowed */
524 {
525 int ret;
526 drmI830GetParam gp;
527
528 gp.param = I830_PARAM_ALLOW_BATCHBUFFER;
529 gp.value = &intelScreen->allow_batchbuffer;
530
531 ret = drmCommandWriteRead(sPriv->fd, DRM_I830_GETPARAM,
532 &gp, sizeof(gp));
533 if (ret) {
534 fprintf(stderr, "drmI830GetParam: (%d) %d\n", gp.param, ret);
535 return GL_FALSE;
536 }
537 }
538
Kristian Høgsbergac3e8382007-05-15 15:17:30 -0400539 sPriv->extensions = intelExtensions;
540
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000541 if (glx_enable_extension != NULL) {
Kristian Høgsbergaac367f2007-05-11 16:11:23 -0400542 (*glx_enable_extension) (sPriv->psc, "GLX_SGI_video_sync");
Kristian Høgsbergaac367f2007-05-11 16:11:23 -0400543 (*glx_enable_extension) (sPriv->psc, "GLX_SGI_make_current_read");
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000544 }
545
Eric Anholt3e168a02007-08-16 14:32:53 -0700546 /* If we've got a new enough DDX that's initializing TTM and giving us
547 * object handles for the shared buffers, use that.
548 */
549 intelScreen->ttm = GL_FALSE;
550 if (getenv("INTEL_NO_TTM") == NULL &&
Kristian Høgsbergefd03a22007-05-14 16:37:19 -0400551 intelScreen->driScrnPriv->ddx_version.minor >= 9 &&
Eric Anholt3e168a02007-08-16 14:32:53 -0700552 intelScreen->front.bo_handle != -1) {
553 intelScreen->bufmgr = dri_bufmgr_ttm_init(sPriv->fd,
554 DRM_FENCE_TYPE_EXE,
555 DRM_FENCE_TYPE_EXE |
556 DRM_I915_FENCE_TYPE_RW);
557 if (intelScreen->bufmgr != NULL)
558 intelScreen->ttm = GL_TRUE;
559 }
560 /* Otherwise, use the classic buffer manager. */
Eric Anholt1ddbfd72007-06-18 12:19:42 -0700561 if (intelScreen->bufmgr == NULL) {
562 if (intelScreen->tex.size == 0) {
563 fprintf(stderr, "[%s:%u] Error initializing buffer manager.\n",
564 __func__, __LINE__);
565 return GL_FALSE;
566 }
Eric Anholt9a4cc2e2007-07-25 10:12:23 -0700567 fprintf(stderr, "[%s:%u] Failed to init TTM buffer manager, falling back"
568 " to classic.\n", __func__, __LINE__);
Eric Anholtcfc21192007-05-17 15:28:01 -0700569 intelScreen->bufmgr = dri_bufmgr_fake_init(intelScreen->tex.offset,
570 intelScreen->tex.map,
571 intelScreen->tex.size,
572 intel_fence_emit,
573 intel_fence_wait,
574 intelScreen);
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000575 }
576
577 intel_recreate_static_regions(intelScreen);
578
579 return GL_TRUE;
580}
581
582
583static void
584intelDestroyScreen(__DRIscreenPrivate * sPriv)
585{
586 intelScreenPrivate *intelScreen = (intelScreenPrivate *) sPriv->private;
587
588 intelUnmapScreenRegions(intelScreen);
589
Eric Anholt2ac17c62007-05-31 09:45:45 -0700590 dri_bufmgr_destroy(intelScreen->bufmgr);
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000591 FREE(intelScreen);
592 sPriv->private = NULL;
593}
594
595
596/**
597 * This is called when we need to set up GL rendering to a new X window.
598 */
599static GLboolean
600intelCreateBuffer(__DRIscreenPrivate * driScrnPriv,
601 __DRIdrawablePrivate * driDrawPriv,
602 const __GLcontextModes * mesaVis, GLboolean isPixmap)
603{
604 intelScreenPrivate *screen = (intelScreenPrivate *) driScrnPriv->private;
605
606 if (isPixmap) {
607 return GL_FALSE; /* not implemented */
608 }
609 else {
610 GLboolean swStencil = (mesaVis->stencilBits > 0 &&
611 mesaVis->depthBits != 24);
612 GLenum rgbFormat = (mesaVis->redBits == 5 ? GL_RGB5 : GL_RGBA8);
613
Michel Dänzer6b99caf2007-02-15 16:30:40 +0100614 struct intel_framebuffer *intel_fb = CALLOC_STRUCT(intel_framebuffer);
615
616 if (!intel_fb)
617 return GL_FALSE;
618
619 _mesa_initialize_framebuffer(&intel_fb->Base, mesaVis);
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000620
621 /* setup the hardware-based renderbuffers */
622 {
Michel Dänzere33a9d62007-02-20 19:14:23 +0100623 intel_fb->color_rb[0]
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000624 = intel_create_renderbuffer(rgbFormat,
625 screen->width, screen->height,
626 screen->front.offset,
627 screen->front.pitch,
628 screen->cpp,
629 screen->front.map);
Michel Dänzere33a9d62007-02-20 19:14:23 +0100630 intel_set_span_functions(&intel_fb->color_rb[0]->Base);
Michel Dänzer6b99caf2007-02-15 16:30:40 +0100631 _mesa_add_renderbuffer(&intel_fb->Base, BUFFER_FRONT_LEFT,
Michel Dänzere33a9d62007-02-20 19:14:23 +0100632 &intel_fb->color_rb[0]->Base);
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000633 }
634
635 if (mesaVis->doubleBufferMode) {
Michel Dänzere33a9d62007-02-20 19:14:23 +0100636 intel_fb->color_rb[1]
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000637 = intel_create_renderbuffer(rgbFormat,
638 screen->width, screen->height,
639 screen->back.offset,
640 screen->back.pitch,
641 screen->cpp,
642 screen->back.map);
Michel Dänzere33a9d62007-02-20 19:14:23 +0100643 intel_set_span_functions(&intel_fb->color_rb[1]->Base);
Michel Dänzer6b99caf2007-02-15 16:30:40 +0100644 _mesa_add_renderbuffer(&intel_fb->Base, BUFFER_BACK_LEFT,
Michel Dänzere33a9d62007-02-20 19:14:23 +0100645 &intel_fb->color_rb[1]->Base);
646
647 if (screen->third.handle) {
Michel Dänzer76f3b662007-03-26 17:38:58 +0200648 struct gl_renderbuffer *tmp_rb = NULL;
649
Michel Dänzere33a9d62007-02-20 19:14:23 +0100650 intel_fb->color_rb[2]
651 = intel_create_renderbuffer(rgbFormat,
652 screen->width, screen->height,
653 screen->third.offset,
654 screen->third.pitch,
655 screen->cpp,
656 screen->third.map);
657 intel_set_span_functions(&intel_fb->color_rb[2]->Base);
Michel Dänzer76f3b662007-03-26 17:38:58 +0200658 _mesa_reference_renderbuffer(&tmp_rb, &intel_fb->color_rb[2]->Base);
Michel Dänzere33a9d62007-02-20 19:14:23 +0100659 }
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000660 }
661
662 if (mesaVis->depthBits == 24 && mesaVis->stencilBits == 8) {
663 /* combined depth/stencil buffer */
664 struct intel_renderbuffer *depthStencilRb
665 = intel_create_renderbuffer(GL_DEPTH24_STENCIL8_EXT,
666 screen->width, screen->height,
667 screen->depth.offset,
668 screen->depth.pitch,
669 screen->cpp, /* 4! */
670 screen->depth.map);
671 intel_set_span_functions(&depthStencilRb->Base);
672 /* note: bind RB to two attachment points */
Michel Dänzer6b99caf2007-02-15 16:30:40 +0100673 _mesa_add_renderbuffer(&intel_fb->Base, BUFFER_DEPTH,
674 &depthStencilRb->Base);
675 _mesa_add_renderbuffer(&intel_fb->Base, BUFFER_STENCIL,
676 &depthStencilRb->Base);
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000677 }
678 else if (mesaVis->depthBits == 16) {
679 /* just 16-bit depth buffer, no hw stencil */
680 struct intel_renderbuffer *depthRb
681 = intel_create_renderbuffer(GL_DEPTH_COMPONENT16,
682 screen->width, screen->height,
683 screen->depth.offset,
684 screen->depth.pitch,
685 screen->cpp, /* 2! */
686 screen->depth.map);
687 intel_set_span_functions(&depthRb->Base);
Michel Dänzer6b99caf2007-02-15 16:30:40 +0100688 _mesa_add_renderbuffer(&intel_fb->Base, BUFFER_DEPTH, &depthRb->Base);
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000689 }
690
691 /* now add any/all software-based renderbuffers we may need */
Michel Dänzer6b99caf2007-02-15 16:30:40 +0100692 _mesa_add_soft_renderbuffers(&intel_fb->Base,
693 GL_FALSE, /* never sw color */
694 GL_FALSE, /* never sw depth */
695 swStencil, mesaVis->accumRedBits > 0,
696 GL_FALSE, /* never sw alpha */
697 GL_FALSE /* never sw aux */ );
698 driDrawPriv->driverPrivate = (void *) intel_fb;
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000699
Michel Dänzer6b99caf2007-02-15 16:30:40 +0100700 return GL_TRUE;
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000701 }
702}
703
704static void
705intelDestroyBuffer(__DRIdrawablePrivate * driDrawPriv)
706{
Briana510bc32007-03-06 10:07:59 -0700707 _mesa_unreference_framebuffer((GLframebuffer **)(&(driDrawPriv->driverPrivate)));
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000708}
709
710
711/**
712 * Get information about previous buffer swaps.
713 */
714static int
715intelGetSwapInfo(__DRIdrawablePrivate * dPriv, __DRIswapInfo * sInfo)
716{
Michel Dänzer641c9662007-02-22 17:24:09 +0100717 struct intel_framebuffer *intel_fb;
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000718
Michel Dänzer641c9662007-02-22 17:24:09 +0100719 if ((dPriv == NULL) || (dPriv->driverPrivate == NULL)
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000720 || (sInfo == NULL)) {
721 return -1;
722 }
723
Michel Dänzer641c9662007-02-22 17:24:09 +0100724 intel_fb = dPriv->driverPrivate;
725 sInfo->swap_count = intel_fb->swap_count;
726 sInfo->swap_ust = intel_fb->swap_ust;
727 sInfo->swap_missed_count = intel_fb->swap_missed_count;
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000728
729 sInfo->swap_missed_usage = (sInfo->swap_missed_count != 0)
Michel Dänzer641c9662007-02-22 17:24:09 +0100730 ? driCalculateSwapUsage(dPriv, 0, intel_fb->swap_missed_ust)
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000731 : 0.0;
732
733 return 0;
734}
735
736
737/* There are probably better ways to do this, such as an
738 * init-designated function to register chipids and createcontext
739 * functions.
740 */
741extern GLboolean i830CreateContext(const __GLcontextModes * mesaVis,
742 __DRIcontextPrivate * driContextPriv,
743 void *sharedContextPrivate);
744
745extern GLboolean i915CreateContext(const __GLcontextModes * mesaVis,
746 __DRIcontextPrivate * driContextPriv,
747 void *sharedContextPrivate);
748
749
750
751
752static GLboolean
753intelCreateContext(const __GLcontextModes * mesaVis,
754 __DRIcontextPrivate * driContextPriv,
755 void *sharedContextPrivate)
756{
757 __DRIscreenPrivate *sPriv = driContextPriv->driScreenPriv;
758 intelScreenPrivate *intelScreen = (intelScreenPrivate *) sPriv->private;
759
760 switch (intelScreen->deviceID) {
761 /* Don't deal with i830 until texture work complete:
762 */
763 case PCI_CHIP_845_G:
764 case PCI_CHIP_I830_M:
765 case PCI_CHIP_I855_GM:
766 case PCI_CHIP_I865_G:
767 return i830CreateContext(mesaVis, driContextPriv, sharedContextPrivate);
768
769 case PCI_CHIP_I915_G:
770 case PCI_CHIP_I915_GM:
771 case PCI_CHIP_I945_G:
772 case PCI_CHIP_I945_GM:
Wang Zhenyuad6351a2007-05-30 16:18:26 +0800773 case PCI_CHIP_I945_GME:
Wang Zhenyu8331d9d2007-06-05 11:42:43 -0700774 case PCI_CHIP_G33_G:
775 case PCI_CHIP_Q35_G:
776 case PCI_CHIP_Q33_G:
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000777 return i915CreateContext(mesaVis, driContextPriv, sharedContextPrivate);
778
779 default:
780 fprintf(stderr, "Unrecognized deviceID %x\n", intelScreen->deviceID);
781 return GL_FALSE;
782 }
783}
784
785
786static const struct __DriverAPIRec intelAPI = {
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000787 .DestroyScreen = intelDestroyScreen,
788 .CreateContext = intelCreateContext,
789 .DestroyContext = intelDestroyContext,
790 .CreateBuffer = intelCreateBuffer,
791 .DestroyBuffer = intelDestroyBuffer,
792 .SwapBuffers = intelSwapBuffers,
793 .MakeCurrent = intelMakeCurrent,
794 .UnbindContext = intelUnbindContext,
795 .GetSwapInfo = intelGetSwapInfo,
796 .GetMSC = driGetMSC32,
797 .WaitForMSC = driWaitForMSC32,
798 .WaitForSBC = NULL,
799 .SwapBuffersMSC = NULL,
Michel Dänzere0885b82007-05-22 14:08:11 +0200800 .CopySubBuffer = intelCopySubBuffer,
801 .setTexOffset = intelSetTexOffset,
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000802};
803
804
805static __GLcontextModes *
806intelFillInModes(unsigned pixel_bits, unsigned depth_bits,
807 unsigned stencil_bits, GLboolean have_back_buffer)
808{
809 __GLcontextModes *modes;
810 __GLcontextModes *m;
811 unsigned num_modes;
812 unsigned depth_buffer_factor;
813 unsigned back_buffer_factor;
814 GLenum fb_format;
815 GLenum fb_type;
816
817 /* GLX_SWAP_COPY_OML is only supported because the Intel driver doesn't
818 * support pageflipping at all.
819 */
820 static const GLenum back_buffer_modes[] = {
821 GLX_NONE, GLX_SWAP_UNDEFINED_OML, GLX_SWAP_COPY_OML
822 };
823
824 u_int8_t depth_bits_array[3];
825 u_int8_t stencil_bits_array[3];
826
827
828 depth_bits_array[0] = 0;
829 depth_bits_array[1] = depth_bits;
830 depth_bits_array[2] = depth_bits;
831
832 /* Just like with the accumulation buffer, always provide some modes
833 * with a stencil buffer. It will be a sw fallback, but some apps won't
834 * care about that.
835 */
836 stencil_bits_array[0] = 0;
837 stencil_bits_array[1] = 0;
Dave Airlieb6becfa2006-12-31 10:01:17 +1100838 if (depth_bits == 24)
839 stencil_bits_array[1] = (stencil_bits == 0) ? 8 : stencil_bits;
840
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000841 stencil_bits_array[2] = (stencil_bits == 0) ? 8 : stencil_bits;
842
843 depth_buffer_factor = ((depth_bits != 0) || (stencil_bits != 0)) ? 3 : 1;
844 back_buffer_factor = (have_back_buffer) ? 3 : 1;
845
846 num_modes = depth_buffer_factor * back_buffer_factor * 4;
847
848 if (pixel_bits == 16) {
849 fb_format = GL_RGB;
850 fb_type = GL_UNSIGNED_SHORT_5_6_5;
851 }
852 else {
853 fb_format = GL_BGRA;
854 fb_type = GL_UNSIGNED_INT_8_8_8_8_REV;
855 }
856
857 modes =
858 (*dri_interface->createContextModes) (num_modes,
859 sizeof(__GLcontextModes));
860 m = modes;
861 if (!driFillInModes(&m, fb_format, fb_type,
862 depth_bits_array, stencil_bits_array,
863 depth_buffer_factor, back_buffer_modes,
864 back_buffer_factor, GLX_TRUE_COLOR)) {
865 fprintf(stderr, "[%s:%u] Error creating FBConfig!\n", __func__,
866 __LINE__);
867 return NULL;
868 }
869 if (!driFillInModes(&m, fb_format, fb_type,
870 depth_bits_array, stencil_bits_array,
871 depth_buffer_factor, back_buffer_modes,
872 back_buffer_factor, GLX_DIRECT_COLOR)) {
873 fprintf(stderr, "[%s:%u] Error creating FBConfig!\n", __func__,
874 __LINE__);
875 return NULL;
876 }
877
878 /* Mark the visual as slow if there are "fake" stencil bits.
879 */
880 for (m = modes; m != NULL; m = m->next) {
881 if ((m->stencilBits != 0) && (m->stencilBits != stencil_bits)) {
882 m->visualRating = GLX_SLOW_CONFIG;
883 }
884 }
885
886 return modes;
887}
888
889
890/**
Kristian Høgsberg64106d02007-05-14 16:58:37 -0400891 * This is the driver specific part of the createNewScreen entry point.
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000892 *
Kristian Høgsberg64106d02007-05-14 16:58:37 -0400893 * \todo maybe fold this into intelInitDriver
894 *
895 * \return the __GLcontextModes supported by this driver
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000896 */
Kristian Høgsberg64106d02007-05-14 16:58:37 -0400897PUBLIC __GLcontextModes *__driDriverInitScreen(__DRIscreenPrivate *psp)
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000898{
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000899 static const __DRIversion ddx_expected = { 1, 5, 0 };
900 static const __DRIversion dri_expected = { 4, 0, 0 };
Eric Anholt4cf2cc02007-05-18 11:29:55 -0700901 static const __DRIversion drm_expected = { 1, 5, 0 };
Kristian Høgsberg64106d02007-05-14 16:58:37 -0400902 I830DRIPtr dri_priv = (I830DRIPtr) psp->pDevPriv;
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000903
Kristian Høgsberg64106d02007-05-14 16:58:37 -0400904 psp->DriverAPI = intelAPI;
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000905
906 if (!driCheckDriDdxDrmVersions2("i915",
Kristian Høgsberg64106d02007-05-14 16:58:37 -0400907 &psp->dri_version, &dri_expected,
908 &psp->ddx_version, &ddx_expected,
909 &psp->drm_version, &drm_expected)) {
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000910 return NULL;
911 }
912
Kristian Høgsberg64106d02007-05-14 16:58:37 -0400913 /* Calling driInitExtensions here, with a NULL context pointer,
914 * does not actually enable the extensions. It just makes sure
915 * that all the dispatch offsets for all the extensions that
916 * *might* be enables are known. This is needed because the
917 * dispatch offsets need to be known when _mesa_context_create is
918 * called, but we can't enable the extensions until we have a
919 * context pointer.
920 *
921 * Hello chicken. Hello egg. How are you two today?
922 */
923 driInitExtensions(NULL, card_extensions, GL_FALSE);
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000924
Kristian Høgsberg64106d02007-05-14 16:58:37 -0400925 if (!intelInitDriver(psp))
926 return NULL;
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000927
Kristian Høgsberg64106d02007-05-14 16:58:37 -0400928 return intelFillInModes(dri_priv->cpp * 8,
929 (dri_priv->cpp == 2) ? 16 : 24,
930 (dri_priv->cpp == 2) ? 0 : 8, 1);
Keith Whitwell6b9e31f2006-11-01 12:03:11 +0000931}
932
933struct intel_context *intelScreenContext(intelScreenPrivate *intelScreen)
934{
935 /*
936 * This should probably change to have the screen allocate a dummy
937 * context at screen creation. For now just use the current context.
938 */
939
940 GET_CURRENT_CONTEXT(ctx);
941 if (ctx == NULL) {
942 _mesa_problem(NULL, "No current context in intelScreenContext\n");
943 return NULL;
944 }
945 return intel_context(ctx);
946}
947