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