Merge branch 'dri2'
[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->ddx_version.minor < 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->ddx_version.minor >= 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->ddx_version.minor >= 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 static const __DRIextension *intelExtensions[] = {
425 &driReadDrawableExtension,
426 &driCopySubBufferExtension.base,
427 &driSwapControlExtension.base,
428 &driFrameTrackingExtension.base,
429 &driMediaStreamCounterExtension.base,
430 NULL
431 };
432
433
434 static GLboolean intelInitDriver(__DRIscreenPrivate *sPriv)
435 {
436 intelScreenPrivate *intelScreen;
437 I830DRIPtr gDRIPriv = (I830DRIPtr) sPriv->pDevPriv;
438 drmI830Sarea *sarea;
439
440 if (sPriv->devPrivSize != sizeof(I830DRIRec)) {
441 fprintf(stderr,
442 "\nERROR! sizeof(I830DRIRec) does not match passed size from device driver\n");
443 return GL_FALSE;
444 }
445
446 /* Allocate the private area */
447 intelScreen = (intelScreenPrivate *) CALLOC(sizeof(intelScreenPrivate));
448 if (!intelScreen) {
449 fprintf(stderr, "\nERROR! Allocating private area failed\n");
450 return GL_FALSE;
451 }
452 /* parse information in __driConfigOptions */
453 driParseOptionInfo(&intelScreen->optionCache,
454 __driConfigOptions, __driNConfigOptions);
455
456 intelScreen->driScrnPriv = sPriv;
457 sPriv->private = (void *) intelScreen;
458 intelScreen->sarea_priv_offset = gDRIPriv->sarea_priv_offset;
459 sarea = (drmI830Sarea *)
460 (((GLubyte *) sPriv->pSAREA) + intelScreen->sarea_priv_offset);
461
462 intelScreen->deviceID = gDRIPriv->deviceID;
463 if (intelScreen->deviceID == PCI_CHIP_I865_G)
464 intelScreen->maxBatchSize = 4096;
465 else
466 intelScreen->maxBatchSize = BATCH_SZ;
467
468 intelScreen->mem = gDRIPriv->mem;
469 intelScreen->cpp = gDRIPriv->cpp;
470
471 switch (gDRIPriv->bitsPerPixel) {
472 case 16:
473 intelScreen->fbFormat = DV_PF_565;
474 break;
475 case 32:
476 intelScreen->fbFormat = DV_PF_8888;
477 break;
478 default:
479 exit(1);
480 break;
481 }
482
483 intelUpdateScreenFromSAREA(intelScreen, sarea);
484
485 if (!intelMapScreenRegions(sPriv)) {
486 fprintf(stderr, "\nERROR! mapping regions\n");
487 _mesa_free(intelScreen);
488 sPriv->private = NULL;
489 return GL_FALSE;
490 }
491
492 intelScreen->sarea_priv_offset = gDRIPriv->sarea_priv_offset;
493
494 if (0)
495 intelPrintDRIInfo(intelScreen, sPriv, gDRIPriv);
496
497 intelScreen->drmMinor = sPriv->drm_version.minor;
498
499 /* Determine if IRQs are active? */
500 {
501 int ret;
502 drmI830GetParam gp;
503
504 gp.param = I830_PARAM_IRQ_ACTIVE;
505 gp.value = &intelScreen->irq_active;
506
507 ret = drmCommandWriteRead(sPriv->fd, DRM_I830_GETPARAM,
508 &gp, sizeof(gp));
509 if (ret) {
510 fprintf(stderr, "drmI830GetParam: %d\n", ret);
511 return GL_FALSE;
512 }
513 }
514
515 /* Determine if batchbuffers are allowed */
516 {
517 int ret;
518 drmI830GetParam gp;
519
520 gp.param = I830_PARAM_ALLOW_BATCHBUFFER;
521 gp.value = &intelScreen->allow_batchbuffer;
522
523 ret = drmCommandWriteRead(sPriv->fd, DRM_I830_GETPARAM,
524 &gp, sizeof(gp));
525 if (ret) {
526 fprintf(stderr, "drmI830GetParam: (%d) %d\n", gp.param, ret);
527 return GL_FALSE;
528 }
529 }
530
531 sPriv->extensions = intelExtensions;
532
533 /* If we've got a new enough DDX that's initializing TTM and giving us
534 * object handles for the shared buffers, use that.
535 */
536 intelScreen->ttm = GL_FALSE;
537 if (getenv("INTEL_NO_TTM") == NULL &&
538 intelScreen->driScrnPriv->ddx_version.minor >= 9 &&
539 intelScreen->drmMinor >= 11 &&
540 intelScreen->front.bo_handle != -1) {
541 intelScreen->bufmgr = intel_bufmgr_ttm_init(sPriv->fd,
542 DRM_FENCE_TYPE_EXE,
543 DRM_FENCE_TYPE_EXE |
544 DRM_I915_FENCE_TYPE_RW,
545 BATCH_SZ);
546 if (intelScreen->bufmgr != NULL)
547 intelScreen->ttm = GL_TRUE;
548 }
549 /* Otherwise, use the classic buffer manager. */
550 if (intelScreen->bufmgr == NULL) {
551 if (intelScreen->tex.size == 0) {
552 fprintf(stderr, "[%s:%u] Error initializing buffer manager.\n",
553 __func__, __LINE__);
554 return GL_FALSE;
555 }
556 fprintf(stderr, "[%s:%u] Failed to init TTM buffer manager, falling back"
557 " to classic.\n", __func__, __LINE__);
558 intelScreen->bufmgr = dri_bufmgr_fake_init(intelScreen->tex.offset,
559 intelScreen->tex.map,
560 intelScreen->tex.size,
561 intel_fence_emit,
562 intel_fence_wait,
563 intelScreen);
564 }
565
566 intel_recreate_static_regions(intelScreen);
567
568 return GL_TRUE;
569 }
570
571
572 static void
573 intelDestroyScreen(__DRIscreenPrivate * sPriv)
574 {
575 intelScreenPrivate *intelScreen = (intelScreenPrivate *) sPriv->private;
576
577 intelUnmapScreenRegions(intelScreen);
578
579 dri_bufmgr_destroy(intelScreen->bufmgr);
580 FREE(intelScreen);
581 sPriv->private = NULL;
582 }
583
584
585 /**
586 * This is called when we need to set up GL rendering to a new X window.
587 */
588 static GLboolean
589 intelCreateBuffer(__DRIscreenPrivate * driScrnPriv,
590 __DRIdrawablePrivate * driDrawPriv,
591 const __GLcontextModes * mesaVis, GLboolean isPixmap)
592 {
593 intelScreenPrivate *screen = (intelScreenPrivate *) driScrnPriv->private;
594
595 if (isPixmap) {
596 return GL_FALSE; /* not implemented */
597 }
598 else {
599 GLboolean swStencil = (mesaVis->stencilBits > 0 &&
600 mesaVis->depthBits != 24);
601 GLenum rgbFormat = (mesaVis->redBits == 5 ? GL_RGB5 : GL_RGBA8);
602
603 struct intel_framebuffer *intel_fb = CALLOC_STRUCT(intel_framebuffer);
604
605 if (!intel_fb)
606 return GL_FALSE;
607
608 _mesa_initialize_framebuffer(&intel_fb->Base, mesaVis);
609
610 /* setup the hardware-based renderbuffers */
611 {
612 intel_fb->color_rb[0]
613 = intel_create_renderbuffer(rgbFormat,
614 screen->width, screen->height,
615 screen->front.offset,
616 screen->front.pitch,
617 screen->cpp,
618 screen->front.map);
619 intel_set_span_functions(&intel_fb->color_rb[0]->Base);
620 _mesa_add_renderbuffer(&intel_fb->Base, BUFFER_FRONT_LEFT,
621 &intel_fb->color_rb[0]->Base);
622 }
623
624 if (mesaVis->doubleBufferMode) {
625 intel_fb->color_rb[1]
626 = intel_create_renderbuffer(rgbFormat,
627 screen->width, screen->height,
628 screen->back.offset,
629 screen->back.pitch,
630 screen->cpp,
631 screen->back.map);
632 intel_set_span_functions(&intel_fb->color_rb[1]->Base);
633 _mesa_add_renderbuffer(&intel_fb->Base, BUFFER_BACK_LEFT,
634 &intel_fb->color_rb[1]->Base);
635
636 if (screen->third.handle) {
637 struct gl_renderbuffer *tmp_rb = NULL;
638
639 intel_fb->color_rb[2]
640 = intel_create_renderbuffer(rgbFormat,
641 screen->width, screen->height,
642 screen->third.offset,
643 screen->third.pitch,
644 screen->cpp,
645 screen->third.map);
646 intel_set_span_functions(&intel_fb->color_rb[2]->Base);
647 _mesa_reference_renderbuffer(&tmp_rb, &intel_fb->color_rb[2]->Base);
648 }
649 }
650
651 if (mesaVis->depthBits == 24 && mesaVis->stencilBits == 8) {
652 /* combined depth/stencil buffer */
653 struct intel_renderbuffer *depthStencilRb
654 = intel_create_renderbuffer(GL_DEPTH24_STENCIL8_EXT,
655 screen->width, screen->height,
656 screen->depth.offset,
657 screen->depth.pitch,
658 screen->cpp, /* 4! */
659 screen->depth.map);
660 intel_set_span_functions(&depthStencilRb->Base);
661 /* note: bind RB to two attachment points */
662 _mesa_add_renderbuffer(&intel_fb->Base, BUFFER_DEPTH,
663 &depthStencilRb->Base);
664 _mesa_add_renderbuffer(&intel_fb->Base, BUFFER_STENCIL,
665 &depthStencilRb->Base);
666 }
667 else if (mesaVis->depthBits == 16) {
668 /* just 16-bit depth buffer, no hw stencil */
669 struct intel_renderbuffer *depthRb
670 = intel_create_renderbuffer(GL_DEPTH_COMPONENT16,
671 screen->width, screen->height,
672 screen->depth.offset,
673 screen->depth.pitch,
674 screen->cpp, /* 2! */
675 screen->depth.map);
676 intel_set_span_functions(&depthRb->Base);
677 _mesa_add_renderbuffer(&intel_fb->Base, BUFFER_DEPTH, &depthRb->Base);
678 }
679
680 /* now add any/all software-based renderbuffers we may need */
681 _mesa_add_soft_renderbuffers(&intel_fb->Base,
682 GL_FALSE, /* never sw color */
683 GL_FALSE, /* never sw depth */
684 swStencil, mesaVis->accumRedBits > 0,
685 GL_FALSE, /* never sw alpha */
686 GL_FALSE /* never sw aux */ );
687 driDrawPriv->driverPrivate = (void *) intel_fb;
688
689 return GL_TRUE;
690 }
691 }
692
693 static void
694 intelDestroyBuffer(__DRIdrawablePrivate * driDrawPriv)
695 {
696 _mesa_unreference_framebuffer((GLframebuffer **)(&(driDrawPriv->driverPrivate)));
697 }
698
699
700 /**
701 * Get information about previous buffer swaps.
702 */
703 static int
704 intelGetSwapInfo(__DRIdrawablePrivate * dPriv, __DRIswapInfo * sInfo)
705 {
706 struct intel_framebuffer *intel_fb;
707
708 if ((dPriv == NULL) || (dPriv->driverPrivate == NULL)
709 || (sInfo == NULL)) {
710 return -1;
711 }
712
713 intel_fb = dPriv->driverPrivate;
714 sInfo->swap_count = intel_fb->swap_count;
715 sInfo->swap_ust = intel_fb->swap_ust;
716 sInfo->swap_missed_count = intel_fb->swap_missed_count;
717
718 sInfo->swap_missed_usage = (sInfo->swap_missed_count != 0)
719 ? driCalculateSwapUsage(dPriv, 0, intel_fb->swap_missed_ust)
720 : 0.0;
721
722 return 0;
723 }
724
725
726 /* There are probably better ways to do this, such as an
727 * init-designated function to register chipids and createcontext
728 * functions.
729 */
730 extern GLboolean i830CreateContext(const __GLcontextModes * mesaVis,
731 __DRIcontextPrivate * driContextPriv,
732 void *sharedContextPrivate);
733
734 extern GLboolean i915CreateContext(const __GLcontextModes * mesaVis,
735 __DRIcontextPrivate * driContextPriv,
736 void *sharedContextPrivate);
737
738
739
740
741 static GLboolean
742 intelCreateContext(const __GLcontextModes * mesaVis,
743 __DRIcontextPrivate * driContextPriv,
744 void *sharedContextPrivate)
745 {
746 __DRIscreenPrivate *sPriv = driContextPriv->driScreenPriv;
747 intelScreenPrivate *intelScreen = (intelScreenPrivate *) sPriv->private;
748
749 switch (intelScreen->deviceID) {
750 /* Don't deal with i830 until texture work complete:
751 */
752 case PCI_CHIP_845_G:
753 case PCI_CHIP_I830_M:
754 case PCI_CHIP_I855_GM:
755 case PCI_CHIP_I865_G:
756 return i830CreateContext(mesaVis, driContextPriv, sharedContextPrivate);
757
758 case PCI_CHIP_I915_G:
759 case PCI_CHIP_I915_GM:
760 case PCI_CHIP_I945_G:
761 case PCI_CHIP_I945_GM:
762 case PCI_CHIP_I945_GME:
763 case PCI_CHIP_G33_G:
764 case PCI_CHIP_Q35_G:
765 case PCI_CHIP_Q33_G:
766 return i915CreateContext(mesaVis, driContextPriv, sharedContextPrivate);
767
768 default:
769 fprintf(stderr, "Unrecognized deviceID %x\n", intelScreen->deviceID);
770 return GL_FALSE;
771 }
772 }
773
774
775 static const struct __DriverAPIRec intelAPI = {
776 .DestroyScreen = intelDestroyScreen,
777 .CreateContext = intelCreateContext,
778 .DestroyContext = intelDestroyContext,
779 .CreateBuffer = intelCreateBuffer,
780 .DestroyBuffer = intelDestroyBuffer,
781 .SwapBuffers = intelSwapBuffers,
782 .MakeCurrent = intelMakeCurrent,
783 .UnbindContext = intelUnbindContext,
784 .GetSwapInfo = intelGetSwapInfo,
785 .GetMSC = driGetMSC32,
786 .WaitForMSC = driWaitForMSC32,
787 .WaitForSBC = NULL,
788 .SwapBuffersMSC = NULL,
789 .CopySubBuffer = intelCopySubBuffer,
790 .setTexOffset = intelSetTexOffset,
791 };
792
793
794 static __GLcontextModes *
795 intelFillInModes(unsigned pixel_bits, unsigned depth_bits,
796 unsigned stencil_bits, GLboolean have_back_buffer)
797 {
798 __GLcontextModes *modes;
799 __GLcontextModes *m;
800 unsigned num_modes;
801 unsigned depth_buffer_factor;
802 unsigned back_buffer_factor;
803 GLenum fb_format;
804 GLenum fb_type;
805
806 /* GLX_SWAP_COPY_OML is only supported because the Intel driver doesn't
807 * support pageflipping at all.
808 */
809 static const GLenum back_buffer_modes[] = {
810 GLX_NONE, GLX_SWAP_UNDEFINED_OML, GLX_SWAP_COPY_OML
811 };
812
813 u_int8_t depth_bits_array[3];
814 u_int8_t stencil_bits_array[3];
815
816
817 depth_bits_array[0] = 0;
818 depth_bits_array[1] = depth_bits;
819 depth_bits_array[2] = depth_bits;
820
821 /* Just like with the accumulation buffer, always provide some modes
822 * with a stencil buffer. It will be a sw fallback, but some apps won't
823 * care about that.
824 */
825 stencil_bits_array[0] = 0;
826 stencil_bits_array[1] = 0;
827 if (depth_bits == 24)
828 stencil_bits_array[1] = (stencil_bits == 0) ? 8 : stencil_bits;
829
830 stencil_bits_array[2] = (stencil_bits == 0) ? 8 : stencil_bits;
831
832 depth_buffer_factor = ((depth_bits != 0) || (stencil_bits != 0)) ? 3 : 1;
833 back_buffer_factor = (have_back_buffer) ? 3 : 1;
834
835 num_modes = depth_buffer_factor * back_buffer_factor * 4;
836
837 if (pixel_bits == 16) {
838 fb_format = GL_RGB;
839 fb_type = GL_UNSIGNED_SHORT_5_6_5;
840 }
841 else {
842 fb_format = GL_BGRA;
843 fb_type = GL_UNSIGNED_INT_8_8_8_8_REV;
844 }
845
846 modes =
847 (*dri_interface->createContextModes) (num_modes,
848 sizeof(__GLcontextModes));
849 m = modes;
850 if (!driFillInModes(&m, fb_format, fb_type,
851 depth_bits_array, stencil_bits_array,
852 depth_buffer_factor, back_buffer_modes,
853 back_buffer_factor, GLX_TRUE_COLOR)) {
854 fprintf(stderr, "[%s:%u] Error creating FBConfig!\n", __func__,
855 __LINE__);
856 return NULL;
857 }
858 if (!driFillInModes(&m, fb_format, fb_type,
859 depth_bits_array, stencil_bits_array,
860 depth_buffer_factor, back_buffer_modes,
861 back_buffer_factor, GLX_DIRECT_COLOR)) {
862 fprintf(stderr, "[%s:%u] Error creating FBConfig!\n", __func__,
863 __LINE__);
864 return NULL;
865 }
866
867 /* Mark the visual as slow if there are "fake" stencil bits.
868 */
869 for (m = modes; m != NULL; m = m->next) {
870 if ((m->stencilBits != 0) && (m->stencilBits != stencil_bits)) {
871 m->visualRating = GLX_SLOW_CONFIG;
872 }
873 }
874
875 return modes;
876 }
877
878
879 /**
880 * This is the driver specific part of the createNewScreen entry point.
881 *
882 * \todo maybe fold this into intelInitDriver
883 *
884 * \return the __GLcontextModes supported by this driver
885 */
886 PUBLIC __GLcontextModes *__driDriverInitScreen(__DRIscreenPrivate *psp)
887 {
888 static const __DRIversion ddx_expected = { 1, 5, 0 };
889 static const __DRIversion dri_expected = { 4, 0, 0 };
890 static const __DRIversion drm_expected = { 1, 5, 0 };
891 I830DRIPtr dri_priv = (I830DRIPtr) psp->pDevPriv;
892
893 psp->DriverAPI = intelAPI;
894
895 if (!driCheckDriDdxDrmVersions2("i915",
896 &psp->dri_version, &dri_expected,
897 &psp->ddx_version, &ddx_expected,
898 &psp->drm_version, &drm_expected)) {
899 return NULL;
900 }
901
902 /* Calling driInitExtensions here, with a NULL context pointer,
903 * does not actually enable the extensions. It just makes sure
904 * that all the dispatch offsets for all the extensions that
905 * *might* be enables are known. This is needed because the
906 * dispatch offsets need to be known when _mesa_context_create is
907 * called, but we can't enable the extensions until we have a
908 * context pointer.
909 *
910 * Hello chicken. Hello egg. How are you two today?
911 */
912 driInitExtensions(NULL, card_extensions, GL_FALSE);
913
914 if (!intelInitDriver(psp))
915 return NULL;
916
917 return intelFillInModes(dri_priv->cpp * 8,
918 (dri_priv->cpp == 2) ? 16 : 24,
919 (dri_priv->cpp == 2) ? 0 : 8, 1);
920 }
921
922 struct intel_context *intelScreenContext(intelScreenPrivate *intelScreen)
923 {
924 /*
925 * This should probably change to have the screen allocate a dummy
926 * context at screen creation. For now just use the current context.
927 */
928
929 GET_CURRENT_CONTEXT(ctx);
930 if (ctx == NULL) {
931 _mesa_problem(NULL, "No current context in intelScreenContext\n");
932 return NULL;
933 }
934 return intel_context(ctx);
935 }
936