Merge branch 'master' into i915-superioctl
[mesa.git] / src / mesa / drivers / dri / i915 / intel_screen.c
1 /**************************************************************************
2 *
3 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 #include "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"
49 #include "dri_bufmgr.h"
50 #include "intel_regions.h"
51 #include "intel_batchbuffer.h"
52
53 #include "intel_bufmgr_ttm.h"
54
55 PUBLIC 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[];
70
71 /**
72 * Map all the memory regions described by the screen.
73 * \return GL_TRUE if success, GL_FALSE if error.
74 */
75 GLboolean
76 intelMapScreenRegions(__DRIscreenPrivate * sPriv)
77 {
78 intelScreenPrivate *intelScreen = (intelScreenPrivate *) sPriv->private;
79
80 if (intelScreen->front.handle) {
81 if (drmMap(sPriv->fd,
82 intelScreen->front.handle,
83 intelScreen->front.size,
84 (drmAddress *) & intelScreen->front.map) != 0) {
85 _mesa_problem(NULL, "drmMap(frontbuffer) failed!");
86 return GL_FALSE;
87 }
88 }
89 else {
90 _mesa_warning(NULL, "no front buffer handle in intelMapScreenRegions!");
91 }
92
93 if (0)
94 _mesa_printf("Back 0x%08x ", intelScreen->back.handle);
95 if (drmMap(sPriv->fd,
96 intelScreen->back.handle,
97 intelScreen->back.size,
98 (drmAddress *) & intelScreen->back.map) != 0) {
99 intelUnmapScreenRegions(intelScreen);
100 return GL_FALSE;
101 }
102
103 if (intelScreen->third.handle) {
104 if (0)
105 _mesa_printf("Third 0x%08x ", intelScreen->third.handle);
106 if (drmMap(sPriv->fd,
107 intelScreen->third.handle,
108 intelScreen->third.size,
109 (drmAddress *) & intelScreen->third.map) != 0) {
110 intelUnmapScreenRegions(intelScreen);
111 return GL_FALSE;
112 }
113 }
114
115 if (0)
116 _mesa_printf("Depth 0x%08x ", intelScreen->depth.handle);
117 if (drmMap(sPriv->fd,
118 intelScreen->depth.handle,
119 intelScreen->depth.size,
120 (drmAddress *) & intelScreen->depth.map) != 0) {
121 intelUnmapScreenRegions(intelScreen);
122 return GL_FALSE;
123 }
124
125 if (0)
126 _mesa_printf("TEX 0x%08x ", intelScreen->tex.handle);
127 if (intelScreen->tex.size != 0) {
128 if (drmMap(sPriv->fd,
129 intelScreen->tex.handle,
130 intelScreen->tex.size,
131 (drmAddress *) & intelScreen->tex.map) != 0) {
132 intelUnmapScreenRegions(intelScreen);
133 return GL_FALSE;
134 }
135 }
136
137 if (0)
138 printf("Mappings: front: %p back: %p third: %p depth: %p tex: %p\n",
139 intelScreen->front.map,
140 intelScreen->back.map, intelScreen->third.map,
141 intelScreen->depth.map, intelScreen->tex.map);
142 return GL_TRUE;
143 }
144
145 /** Driver-specific fence emit implementation for the fake memory manager. */
146 static unsigned int
147 intel_fence_emit(void *private)
148 {
149 intelScreenPrivate *intelScreen = (intelScreenPrivate *)private;
150 unsigned int fence;
151
152 /* XXX: Need to emit a flush, if we haven't already (at least with the
153 * current batchbuffer implementation, we have).
154 */
155
156 fence = intelEmitIrqLocked(intelScreen);
157
158 return fence;
159 }
160
161 /** Driver-specific fence wait implementation for the fake memory manager. */
162 static int
163 intel_fence_wait(void *private, unsigned int cookie)
164 {
165 intelScreenPrivate *intelScreen = (intelScreenPrivate *)private;
166
167 intelWaitIrq(intelScreen, cookie);
168
169 return 0;
170 }
171
172 static struct intel_region *
173 intel_recreate_static(intelScreenPrivate *intelScreen,
174 struct intel_region *region,
175 intelRegion *region_desc,
176 GLuint mem_type)
177 {
178 if (region) {
179 intel_region_update_static(intelScreen, region, mem_type,
180 region_desc->bo_handle, region_desc->offset,
181 region_desc->map, intelScreen->cpp,
182 region_desc->pitch / intelScreen->cpp,
183 intelScreen->height);
184 } else {
185 region = intel_region_create_static(intelScreen, mem_type,
186 region_desc->bo_handle,
187 region_desc->offset,
188 region_desc->map, intelScreen->cpp,
189 region_desc->pitch / intelScreen->cpp,
190 intelScreen->height);
191 }
192
193 assert(region->buffer != NULL);
194
195 return region;
196 }
197
198
199 /* Create intel_region structs to describe the static front,back,depth
200 * buffers created by the xserver.
201 *
202 * Although FBO's mean we now no longer use these as render targets in
203 * all circumstances, they won't go away until the back and depth
204 * buffers become private, and the front and rotated buffers will
205 * remain even then.
206 *
207 * Note that these don't allocate video memory, just describe
208 * allocations alread made by the X server.
209 */
210 static void
211 intel_recreate_static_regions(intelScreenPrivate *intelScreen)
212 {
213 intelScreen->front_region =
214 intel_recreate_static(intelScreen,
215 intelScreen->front_region,
216 &intelScreen->front,
217 DRM_BO_FLAG_MEM_TT);
218
219 /* The rotated region is only used for old DDXes that didn't handle rotation
220 \ * on their own.
221 */
222 if (intelScreen->driScrnPriv->ddxMinor < 8) {
223 intelScreen->rotated_region =
224 intel_recreate_static(intelScreen,
225 intelScreen->rotated_region,
226 &intelScreen->rotated,
227 DRM_BO_FLAG_MEM_TT);
228 }
229
230 intelScreen->back_region =
231 intel_recreate_static(intelScreen,
232 intelScreen->back_region,
233 &intelScreen->back,
234 DRM_BO_FLAG_MEM_TT);
235
236 if (intelScreen->third.handle) {
237 intelScreen->third_region =
238 intel_recreate_static(intelScreen,
239 intelScreen->third_region,
240 &intelScreen->third,
241 DRM_BO_FLAG_MEM_TT);
242 }
243
244 /* Still assumes front.cpp == depth.cpp. We can kill this when we move to
245 * private buffers.
246 */
247 intelScreen->depth_region =
248 intel_recreate_static(intelScreen,
249 intelScreen->depth_region,
250 &intelScreen->depth,
251 DRM_BO_FLAG_MEM_TT);
252 }
253
254 /**
255 * Use the information in the sarea to update the screen parameters
256 * related to screen rotation. Needs to be called locked.
257 */
258 void
259 intelUpdateScreenRotation(__DRIscreenPrivate * sPriv, drmI830Sarea * sarea)
260 {
261 intelScreenPrivate *intelScreen = (intelScreenPrivate *) sPriv->private;
262
263 intelUnmapScreenRegions(intelScreen);
264 intelUpdateScreenFromSAREA(intelScreen, sarea);
265 if (!intelMapScreenRegions(sPriv)) {
266 fprintf(stderr, "ERROR Remapping screen regions!!!\n");
267 }
268 intel_recreate_static_regions(intelScreen);
269 }
270
271
272 void
273 intelUnmapScreenRegions(intelScreenPrivate * intelScreen)
274 {
275 #define REALLY_UNMAP 1
276 if (intelScreen->front.map) {
277 #if REALLY_UNMAP
278 if (drmUnmap(intelScreen->front.map, intelScreen->front.size) != 0)
279 printf("drmUnmap front failed!\n");
280 #endif
281 intelScreen->front.map = NULL;
282 }
283 if (intelScreen->back.map) {
284 #if REALLY_UNMAP
285 if (drmUnmap(intelScreen->back.map, intelScreen->back.size) != 0)
286 printf("drmUnmap back failed!\n");
287 #endif
288 intelScreen->back.map = NULL;
289 }
290 if (intelScreen->third.map) {
291 #if REALLY_UNMAP
292 if (drmUnmap(intelScreen->third.map, intelScreen->third.size) != 0)
293 printf("drmUnmap third failed!\n");
294 #endif
295 intelScreen->third.map = NULL;
296 }
297 if (intelScreen->depth.map) {
298 #if REALLY_UNMAP
299 drmUnmap(intelScreen->depth.map, intelScreen->depth.size);
300 intelScreen->depth.map = NULL;
301 #endif
302 }
303 if (intelScreen->tex.map) {
304 #if REALLY_UNMAP
305 drmUnmap(intelScreen->tex.map, intelScreen->tex.size);
306 intelScreen->tex.map = NULL;
307 #endif
308 }
309 }
310
311
312 static void
313 intelPrintDRIInfo(intelScreenPrivate * intelScreen,
314 __DRIscreenPrivate * sPriv, I830DRIPtr gDRIPriv)
315 {
316 fprintf(stderr, "*** Front size: 0x%x offset: 0x%x pitch: %d\n",
317 intelScreen->front.size, intelScreen->front.offset,
318 intelScreen->front.pitch);
319 fprintf(stderr, "*** Back size: 0x%x offset: 0x%x pitch: %d\n",
320 intelScreen->back.size, intelScreen->back.offset,
321 intelScreen->back.pitch);
322 fprintf(stderr, "*** Depth size: 0x%x offset: 0x%x pitch: %d\n",
323 intelScreen->depth.size, intelScreen->depth.offset,
324 intelScreen->depth.pitch);
325 fprintf(stderr, "*** Rotated size: 0x%x offset: 0x%x pitch: %d\n",
326 intelScreen->rotated.size, intelScreen->rotated.offset,
327 intelScreen->rotated.pitch);
328 fprintf(stderr, "*** Texture size: 0x%x offset: 0x%x\n",
329 intelScreen->tex.size, intelScreen->tex.offset);
330 fprintf(stderr, "*** Memory : 0x%x\n", gDRIPriv->mem);
331 }
332
333
334 static void
335 intelPrintSAREA(const drmI830Sarea * sarea)
336 {
337 fprintf(stderr, "SAREA: sarea width %d height %d\n", sarea->width,
338 sarea->height);
339 fprintf(stderr, "SAREA: pitch: %d\n", sarea->pitch);
340 fprintf(stderr,
341 "SAREA: front offset: 0x%08x size: 0x%x handle: 0x%x\n",
342 sarea->front_offset, sarea->front_size,
343 (unsigned) sarea->front_handle);
344 fprintf(stderr,
345 "SAREA: back offset: 0x%08x size: 0x%x handle: 0x%x\n",
346 sarea->back_offset, sarea->back_size,
347 (unsigned) sarea->back_handle);
348 fprintf(stderr, "SAREA: depth offset: 0x%08x size: 0x%x handle: 0x%x\n",
349 sarea->depth_offset, sarea->depth_size,
350 (unsigned) sarea->depth_handle);
351 fprintf(stderr, "SAREA: tex offset: 0x%08x size: 0x%x handle: 0x%x\n",
352 sarea->tex_offset, sarea->tex_size, (unsigned) sarea->tex_handle);
353 fprintf(stderr, "SAREA: rotation: %d\n", sarea->rotation);
354 fprintf(stderr,
355 "SAREA: rotated offset: 0x%08x size: 0x%x\n",
356 sarea->rotated_offset, sarea->rotated_size);
357 fprintf(stderr, "SAREA: rotated pitch: %d\n", sarea->rotated_pitch);
358 }
359
360
361 /**
362 * A number of the screen parameters are obtained/computed from
363 * information in the SAREA. This function updates those parameters.
364 */
365 void
366 intelUpdateScreenFromSAREA(intelScreenPrivate * intelScreen,
367 drmI830Sarea * sarea)
368 {
369 intelScreen->width = sarea->width;
370 intelScreen->height = sarea->height;
371
372 intelScreen->front.offset = sarea->front_offset;
373 intelScreen->front.pitch = sarea->pitch * intelScreen->cpp;
374 intelScreen->front.handle = sarea->front_handle;
375 intelScreen->front.size = sarea->front_size;
376
377 intelScreen->back.offset = sarea->back_offset;
378 intelScreen->back.pitch = sarea->pitch * intelScreen->cpp;
379 intelScreen->back.handle = sarea->back_handle;
380 intelScreen->back.size = sarea->back_size;
381
382 if (intelScreen->driScrnPriv->ddxMinor >= 8) {
383 intelScreen->third.offset = sarea->third_offset;
384 intelScreen->third.pitch = sarea->pitch * intelScreen->cpp;
385 intelScreen->third.handle = sarea->third_handle;
386 intelScreen->third.size = sarea->third_size;
387 }
388
389 intelScreen->depth.offset = sarea->depth_offset;
390 intelScreen->depth.pitch = sarea->pitch * intelScreen->cpp;
391 intelScreen->depth.handle = sarea->depth_handle;
392 intelScreen->depth.size = sarea->depth_size;
393
394 if (intelScreen->driScrnPriv->ddxMinor >= 9) {
395 intelScreen->front.bo_handle = sarea->front_bo_handle;
396 intelScreen->back.bo_handle = sarea->back_bo_handle;
397 intelScreen->third.bo_handle = sarea->third_bo_handle;
398 intelScreen->depth.bo_handle = sarea->depth_bo_handle;
399 } else {
400 intelScreen->front.bo_handle = -1;
401 intelScreen->back.bo_handle = -1;
402 intelScreen->third.bo_handle = -1;
403 intelScreen->depth.bo_handle = -1;
404 }
405
406 intelScreen->tex.offset = sarea->tex_offset;
407 intelScreen->logTextureGranularity = sarea->log_tex_granularity;
408 intelScreen->tex.handle = sarea->tex_handle;
409 intelScreen->tex.size = sarea->tex_size;
410
411 intelScreen->rotated.offset = sarea->rotated_offset;
412 intelScreen->rotated.pitch = sarea->rotated_pitch * intelScreen->cpp;
413 intelScreen->rotated.size = sarea->rotated_size;
414 intelScreen->current_rotation = sarea->rotation;
415 matrix23Rotate(&intelScreen->rotMatrix,
416 sarea->width, sarea->height, sarea->rotation);
417 intelScreen->rotatedWidth = sarea->virtualX;
418 intelScreen->rotatedHeight = sarea->virtualY;
419
420 if (0)
421 intelPrintSAREA(sarea);
422 }
423
424
425 static GLboolean
426 intelInitDriver(__DRIscreenPrivate * sPriv)
427 {
428 intelScreenPrivate *intelScreen;
429 I830DRIPtr gDRIPriv = (I830DRIPtr) sPriv->pDevPriv;
430 drmI830Sarea *sarea;
431
432 PFNGLXSCRENABLEEXTENSIONPROC glx_enable_extension =
433 (PFNGLXSCRENABLEEXTENSIONPROC) (*dri_interface->
434 getProcAddress("glxEnableExtension"));
435 void *const psc = sPriv->psc->screenConfigs;
436
437 if (sPriv->devPrivSize != sizeof(I830DRIRec)) {
438 fprintf(stderr,
439 "\nERROR! sizeof(I830DRIRec) does not match passed size from device driver\n");
440 return GL_FALSE;
441 }
442
443 /* Allocate the private area */
444 intelScreen = (intelScreenPrivate *) CALLOC(sizeof(intelScreenPrivate));
445 if (!intelScreen) {
446 fprintf(stderr, "\nERROR! Allocating private area failed\n");
447 return GL_FALSE;
448 }
449 /* parse information in __driConfigOptions */
450 driParseOptionInfo(&intelScreen->optionCache,
451 __driConfigOptions, __driNConfigOptions);
452
453 intelScreen->driScrnPriv = sPriv;
454 sPriv->private = (void *) intelScreen;
455 intelScreen->sarea_priv_offset = gDRIPriv->sarea_priv_offset;
456 sarea = (drmI830Sarea *)
457 (((GLubyte *) sPriv->pSAREA) + intelScreen->sarea_priv_offset);
458
459 intelScreen->deviceID = gDRIPriv->deviceID;
460 if (intelScreen->deviceID == PCI_CHIP_I865_G)
461 intelScreen->maxBatchSize = 4096;
462 else
463 intelScreen->maxBatchSize = BATCH_SZ;
464
465 intelScreen->mem = gDRIPriv->mem;
466 intelScreen->cpp = gDRIPriv->cpp;
467
468 switch (gDRIPriv->bitsPerPixel) {
469 case 16:
470 intelScreen->fbFormat = DV_PF_565;
471 break;
472 case 32:
473 intelScreen->fbFormat = DV_PF_8888;
474 break;
475 default:
476 exit(1);
477 break;
478 }
479
480 intelUpdateScreenFromSAREA(intelScreen, sarea);
481
482 if (!intelMapScreenRegions(sPriv)) {
483 fprintf(stderr, "\nERROR! mapping regions\n");
484 _mesa_free(intelScreen);
485 sPriv->private = NULL;
486 return GL_FALSE;
487 }
488
489 intelScreen->sarea_priv_offset = gDRIPriv->sarea_priv_offset;
490
491 if (0)
492 intelPrintDRIInfo(intelScreen, sPriv, gDRIPriv);
493
494 intelScreen->drmMinor = sPriv->drmMinor;
495
496 /* Determine if IRQs are active? */
497 {
498 int ret;
499 drmI830GetParam gp;
500
501 gp.param = I830_PARAM_IRQ_ACTIVE;
502 gp.value = &intelScreen->irq_active;
503
504 ret = drmCommandWriteRead(sPriv->fd, DRM_I830_GETPARAM,
505 &gp, sizeof(gp));
506 if (ret) {
507 fprintf(stderr, "drmI830GetParam: %d\n", ret);
508 return GL_FALSE;
509 }
510 }
511
512 /* Determine if batchbuffers are allowed */
513 {
514 int ret;
515 drmI830GetParam gp;
516
517 gp.param = I830_PARAM_ALLOW_BATCHBUFFER;
518 gp.value = &intelScreen->allow_batchbuffer;
519
520 ret = drmCommandWriteRead(sPriv->fd, DRM_I830_GETPARAM,
521 &gp, sizeof(gp));
522 if (ret) {
523 fprintf(stderr, "drmI830GetParam: (%d) %d\n", gp.param, ret);
524 return GL_FALSE;
525 }
526 }
527
528 if (glx_enable_extension != NULL) {
529 (*glx_enable_extension) (psc, "GLX_SGI_swap_control");
530 (*glx_enable_extension) (psc, "GLX_SGI_video_sync");
531 (*glx_enable_extension) (psc, "GLX_MESA_swap_control");
532 (*glx_enable_extension) (psc, "GLX_MESA_swap_frame_usage");
533 (*glx_enable_extension) (psc, "GLX_SGI_make_current_read");
534 }
535
536 /* If we've got a new enough DDX that's initializing TTM and giving us
537 * object handles for the shared buffers, use that.
538 */
539 intelScreen->ttm = GL_FALSE;
540 if (getenv("INTEL_NO_TTM") == NULL &&
541 intelScreen->driScrnPriv->ddxMinor >= 9 &&
542 intelScreen->drmMinor >= 11 &&
543 intelScreen->front.bo_handle != -1) {
544 intelScreen->bufmgr = intel_bufmgr_ttm_init(sPriv->fd,
545 DRM_FENCE_TYPE_EXE,
546 DRM_FENCE_TYPE_EXE |
547 DRM_I915_FENCE_TYPE_RW,
548 BATCH_SZ);
549 if (intelScreen->bufmgr != NULL)
550 intelScreen->ttm = GL_TRUE;
551 }
552 /* Otherwise, use the classic buffer manager. */
553 if (intelScreen->bufmgr == NULL) {
554 if (intelScreen->tex.size == 0) {
555 fprintf(stderr, "[%s:%u] Error initializing buffer manager.\n",
556 __func__, __LINE__);
557 return GL_FALSE;
558 }
559 fprintf(stderr, "[%s:%u] Failed to init TTM buffer manager, falling back"
560 " to classic.\n", __func__, __LINE__);
561 intelScreen->bufmgr = dri_bufmgr_fake_init(intelScreen->tex.offset,
562 intelScreen->tex.map,
563 intelScreen->tex.size,
564 intel_fence_emit,
565 intel_fence_wait,
566 intelScreen);
567 }
568
569 intel_recreate_static_regions(intelScreen);
570
571 return GL_TRUE;
572 }
573
574
575 static void
576 intelDestroyScreen(__DRIscreenPrivate * sPriv)
577 {
578 intelScreenPrivate *intelScreen = (intelScreenPrivate *) sPriv->private;
579
580 intelUnmapScreenRegions(intelScreen);
581
582 dri_bufmgr_destroy(intelScreen->bufmgr);
583 FREE(intelScreen);
584 sPriv->private = NULL;
585 }
586
587
588 /**
589 * This is called when we need to set up GL rendering to a new X window.
590 */
591 static GLboolean
592 intelCreateBuffer(__DRIscreenPrivate * driScrnPriv,
593 __DRIdrawablePrivate * driDrawPriv,
594 const __GLcontextModes * mesaVis, GLboolean isPixmap)
595 {
596 intelScreenPrivate *screen = (intelScreenPrivate *) driScrnPriv->private;
597
598 if (isPixmap) {
599 return GL_FALSE; /* not implemented */
600 }
601 else {
602 GLboolean swStencil = (mesaVis->stencilBits > 0 &&
603 mesaVis->depthBits != 24);
604 GLenum rgbFormat = (mesaVis->redBits == 5 ? GL_RGB5 : GL_RGBA8);
605
606 struct intel_framebuffer *intel_fb = CALLOC_STRUCT(intel_framebuffer);
607
608 if (!intel_fb)
609 return GL_FALSE;
610
611 _mesa_initialize_framebuffer(&intel_fb->Base, mesaVis);
612
613 /* setup the hardware-based renderbuffers */
614 {
615 intel_fb->color_rb[0]
616 = intel_create_renderbuffer(rgbFormat,
617 screen->width, screen->height,
618 screen->front.offset,
619 screen->front.pitch,
620 screen->cpp,
621 screen->front.map);
622 intel_set_span_functions(&intel_fb->color_rb[0]->Base);
623 _mesa_add_renderbuffer(&intel_fb->Base, BUFFER_FRONT_LEFT,
624 &intel_fb->color_rb[0]->Base);
625 }
626
627 if (mesaVis->doubleBufferMode) {
628 intel_fb->color_rb[1]
629 = intel_create_renderbuffer(rgbFormat,
630 screen->width, screen->height,
631 screen->back.offset,
632 screen->back.pitch,
633 screen->cpp,
634 screen->back.map);
635 intel_set_span_functions(&intel_fb->color_rb[1]->Base);
636 _mesa_add_renderbuffer(&intel_fb->Base, BUFFER_BACK_LEFT,
637 &intel_fb->color_rb[1]->Base);
638
639 if (screen->third.handle) {
640 struct gl_renderbuffer *tmp_rb = NULL;
641
642 intel_fb->color_rb[2]
643 = intel_create_renderbuffer(rgbFormat,
644 screen->width, screen->height,
645 screen->third.offset,
646 screen->third.pitch,
647 screen->cpp,
648 screen->third.map);
649 intel_set_span_functions(&intel_fb->color_rb[2]->Base);
650 _mesa_reference_renderbuffer(&tmp_rb, &intel_fb->color_rb[2]->Base);
651 }
652 }
653
654 if (mesaVis->depthBits == 24 && mesaVis->stencilBits == 8) {
655 /* combined depth/stencil buffer */
656 struct intel_renderbuffer *depthStencilRb
657 = intel_create_renderbuffer(GL_DEPTH24_STENCIL8_EXT,
658 screen->width, screen->height,
659 screen->depth.offset,
660 screen->depth.pitch,
661 screen->cpp, /* 4! */
662 screen->depth.map);
663 intel_set_span_functions(&depthStencilRb->Base);
664 /* note: bind RB to two attachment points */
665 _mesa_add_renderbuffer(&intel_fb->Base, BUFFER_DEPTH,
666 &depthStencilRb->Base);
667 _mesa_add_renderbuffer(&intel_fb->Base, BUFFER_STENCIL,
668 &depthStencilRb->Base);
669 }
670 else if (mesaVis->depthBits == 16) {
671 /* just 16-bit depth buffer, no hw stencil */
672 struct intel_renderbuffer *depthRb
673 = intel_create_renderbuffer(GL_DEPTH_COMPONENT16,
674 screen->width, screen->height,
675 screen->depth.offset,
676 screen->depth.pitch,
677 screen->cpp, /* 2! */
678 screen->depth.map);
679 intel_set_span_functions(&depthRb->Base);
680 _mesa_add_renderbuffer(&intel_fb->Base, BUFFER_DEPTH, &depthRb->Base);
681 }
682
683 /* now add any/all software-based renderbuffers we may need */
684 _mesa_add_soft_renderbuffers(&intel_fb->Base,
685 GL_FALSE, /* never sw color */
686 GL_FALSE, /* never sw depth */
687 swStencil, mesaVis->accumRedBits > 0,
688 GL_FALSE, /* never sw alpha */
689 GL_FALSE /* never sw aux */ );
690 driDrawPriv->driverPrivate = (void *) intel_fb;
691
692 return GL_TRUE;
693 }
694 }
695
696 static void
697 intelDestroyBuffer(__DRIdrawablePrivate * driDrawPriv)
698 {
699 _mesa_unreference_framebuffer((GLframebuffer **)(&(driDrawPriv->driverPrivate)));
700 }
701
702
703 /**
704 * Get information about previous buffer swaps.
705 */
706 static int
707 intelGetSwapInfo(__DRIdrawablePrivate * dPriv, __DRIswapInfo * sInfo)
708 {
709 struct intel_framebuffer *intel_fb;
710
711 if ((dPriv == NULL) || (dPriv->driverPrivate == NULL)
712 || (sInfo == NULL)) {
713 return -1;
714 }
715
716 intel_fb = dPriv->driverPrivate;
717 sInfo->swap_count = intel_fb->swap_count;
718 sInfo->swap_ust = intel_fb->swap_ust;
719 sInfo->swap_missed_count = intel_fb->swap_missed_count;
720
721 sInfo->swap_missed_usage = (sInfo->swap_missed_count != 0)
722 ? driCalculateSwapUsage(dPriv, 0, intel_fb->swap_missed_ust)
723 : 0.0;
724
725 return 0;
726 }
727
728
729 /* There are probably better ways to do this, such as an
730 * init-designated function to register chipids and createcontext
731 * functions.
732 */
733 extern GLboolean i830CreateContext(const __GLcontextModes * mesaVis,
734 __DRIcontextPrivate * driContextPriv,
735 void *sharedContextPrivate);
736
737 extern GLboolean i915CreateContext(const __GLcontextModes * mesaVis,
738 __DRIcontextPrivate * driContextPriv,
739 void *sharedContextPrivate);
740
741
742
743
744 static GLboolean
745 intelCreateContext(const __GLcontextModes * mesaVis,
746 __DRIcontextPrivate * driContextPriv,
747 void *sharedContextPrivate)
748 {
749 __DRIscreenPrivate *sPriv = driContextPriv->driScreenPriv;
750 intelScreenPrivate *intelScreen = (intelScreenPrivate *) sPriv->private;
751
752 switch (intelScreen->deviceID) {
753 /* Don't deal with i830 until texture work complete:
754 */
755 case PCI_CHIP_845_G:
756 case PCI_CHIP_I830_M:
757 case PCI_CHIP_I855_GM:
758 case PCI_CHIP_I865_G:
759 return i830CreateContext(mesaVis, driContextPriv, sharedContextPrivate);
760
761 case PCI_CHIP_I915_G:
762 case PCI_CHIP_I915_GM:
763 case PCI_CHIP_I945_G:
764 case PCI_CHIP_I945_GM:
765 case PCI_CHIP_I945_GME:
766 case PCI_CHIP_G33_G:
767 case PCI_CHIP_Q35_G:
768 case PCI_CHIP_Q33_G:
769 return i915CreateContext(mesaVis, driContextPriv, sharedContextPrivate);
770
771 default:
772 fprintf(stderr, "Unrecognized deviceID %x\n", intelScreen->deviceID);
773 return GL_FALSE;
774 }
775 }
776
777
778 static const struct __DriverAPIRec intelAPI = {
779 .InitDriver = intelInitDriver,
780 .DestroyScreen = intelDestroyScreen,
781 .CreateContext = intelCreateContext,
782 .DestroyContext = intelDestroyContext,
783 .CreateBuffer = intelCreateBuffer,
784 .DestroyBuffer = intelDestroyBuffer,
785 .SwapBuffers = intelSwapBuffers,
786 .MakeCurrent = intelMakeCurrent,
787 .UnbindContext = intelUnbindContext,
788 .GetSwapInfo = intelGetSwapInfo,
789 .GetMSC = driGetMSC32,
790 .WaitForMSC = driWaitForMSC32,
791 .WaitForSBC = NULL,
792 .SwapBuffersMSC = NULL,
793 .CopySubBuffer = intelCopySubBuffer,
794 .setTexOffset = intelSetTexOffset,
795 };
796
797
798 static __GLcontextModes *
799 intelFillInModes(unsigned pixel_bits, unsigned depth_bits,
800 unsigned stencil_bits, GLboolean have_back_buffer)
801 {
802 __GLcontextModes *modes;
803 __GLcontextModes *m;
804 unsigned num_modes;
805 unsigned depth_buffer_factor;
806 unsigned back_buffer_factor;
807 GLenum fb_format;
808 GLenum fb_type;
809
810 /* GLX_SWAP_COPY_OML is only supported because the Intel driver doesn't
811 * support pageflipping at all.
812 */
813 static const GLenum back_buffer_modes[] = {
814 GLX_NONE, GLX_SWAP_UNDEFINED_OML, GLX_SWAP_COPY_OML
815 };
816
817 u_int8_t depth_bits_array[3];
818 u_int8_t stencil_bits_array[3];
819
820
821 depth_bits_array[0] = 0;
822 depth_bits_array[1] = depth_bits;
823 depth_bits_array[2] = depth_bits;
824
825 /* Just like with the accumulation buffer, always provide some modes
826 * with a stencil buffer. It will be a sw fallback, but some apps won't
827 * care about that.
828 */
829 stencil_bits_array[0] = 0;
830 stencil_bits_array[1] = 0;
831 if (depth_bits == 24)
832 stencil_bits_array[1] = (stencil_bits == 0) ? 8 : stencil_bits;
833
834 stencil_bits_array[2] = (stencil_bits == 0) ? 8 : stencil_bits;
835
836 depth_buffer_factor = ((depth_bits != 0) || (stencil_bits != 0)) ? 3 : 1;
837 back_buffer_factor = (have_back_buffer) ? 3 : 1;
838
839 num_modes = depth_buffer_factor * back_buffer_factor * 4;
840
841 if (pixel_bits == 16) {
842 fb_format = GL_RGB;
843 fb_type = GL_UNSIGNED_SHORT_5_6_5;
844 }
845 else {
846 fb_format = GL_BGRA;
847 fb_type = GL_UNSIGNED_INT_8_8_8_8_REV;
848 }
849
850 modes =
851 (*dri_interface->createContextModes) (num_modes,
852 sizeof(__GLcontextModes));
853 m = modes;
854 if (!driFillInModes(&m, fb_format, fb_type,
855 depth_bits_array, stencil_bits_array,
856 depth_buffer_factor, back_buffer_modes,
857 back_buffer_factor, GLX_TRUE_COLOR)) {
858 fprintf(stderr, "[%s:%u] Error creating FBConfig!\n", __func__,
859 __LINE__);
860 return NULL;
861 }
862 if (!driFillInModes(&m, fb_format, fb_type,
863 depth_bits_array, stencil_bits_array,
864 depth_buffer_factor, back_buffer_modes,
865 back_buffer_factor, GLX_DIRECT_COLOR)) {
866 fprintf(stderr, "[%s:%u] Error creating FBConfig!\n", __func__,
867 __LINE__);
868 return NULL;
869 }
870
871 /* Mark the visual as slow if there are "fake" stencil bits.
872 */
873 for (m = modes; m != NULL; m = m->next) {
874 if ((m->stencilBits != 0) && (m->stencilBits != stencil_bits)) {
875 m->visualRating = GLX_SLOW_CONFIG;
876 }
877 }
878
879 return modes;
880 }
881
882
883 /**
884 * This is the bootstrap function for the driver. libGL supplies all of the
885 * requisite information about the system, and the driver initializes itself.
886 * This routine also fills in the linked list pointed to by \c driver_modes
887 * with the \c __GLcontextModes that the driver can support for windows or
888 * pbuffers.
889 *
890 * \return A pointer to a \c __DRIscreenPrivate on success, or \c NULL on
891 * failure.
892 */
893 PUBLIC void *
894 __driCreateNewScreen_20050727(__DRInativeDisplay * dpy, int scrn,
895 __DRIscreen * psc,
896 const __GLcontextModes * modes,
897 const __DRIversion * ddx_version,
898 const __DRIversion * dri_version,
899 const __DRIversion * drm_version,
900 const __DRIframebuffer * frame_buffer,
901 drmAddress pSAREA, int fd,
902 int internal_api_version,
903 const __DRIinterfaceMethods * interface,
904 __GLcontextModes ** driver_modes)
905 {
906 __DRIscreenPrivate *psp;
907 static const __DRIversion ddx_expected = { 1, 5, 0 };
908 static const __DRIversion dri_expected = { 4, 0, 0 };
909 static const __DRIversion drm_expected = { 1, 5, 0 };
910
911 dri_interface = interface;
912
913 if (!driCheckDriDdxDrmVersions2("i915",
914 dri_version, &dri_expected,
915 ddx_version, &ddx_expected,
916 drm_version, &drm_expected)) {
917 return NULL;
918 }
919
920 psp = __driUtilCreateNewScreen(dpy, scrn, psc, NULL,
921 ddx_version, dri_version, drm_version,
922 frame_buffer, pSAREA, fd,
923 internal_api_version, &intelAPI);
924
925 if (psp != NULL) {
926 I830DRIPtr dri_priv = (I830DRIPtr) psp->pDevPriv;
927 *driver_modes = intelFillInModes(dri_priv->cpp * 8,
928 (dri_priv->cpp == 2) ? 16 : 24,
929 (dri_priv->cpp == 2) ? 0 : 8, 1);
930
931 /* Calling driInitExtensions here, with a NULL context pointer, does not actually
932 * enable the extensions. It just makes sure that all the dispatch offsets for all
933 * the extensions that *might* be enables are known. This is needed because the
934 * dispatch offsets need to be known when _mesa_context_create is called, but we can't
935 * enable the extensions until we have a context pointer.
936 *
937 * Hello chicken. Hello egg. How are you two today?
938 */
939 driInitExtensions(NULL, card_extensions, GL_FALSE);
940 }
941
942 return (void *) psp;
943 }
944
945 struct intel_context *intelScreenContext(intelScreenPrivate *intelScreen)
946 {
947 /*
948 * This should probably change to have the screen allocate a dummy
949 * context at screen creation. For now just use the current context.
950 */
951
952 GET_CURRENT_CONTEXT(ctx);
953 if (ctx == NULL) {
954 _mesa_problem(NULL, "No current context in intelScreenContext\n");
955 return NULL;
956 }
957 return intel_context(ctx);
958 }
959