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