7a11672a8f1a6f95e7222c5184bf02b4d671d332
[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 PUBLIC const char __driConfigOptions[] =
54 DRI_CONF_BEGIN DRI_CONF_SECTION_PERFORMANCE
55 DRI_CONF_FTHROTTLE_MODE(DRI_CONF_FTHROTTLE_IRQS)
56 DRI_CONF_VBLANK_MODE(DRI_CONF_VBLANK_DEF_INTERVAL_0)
57 DRI_CONF_SECTION_END DRI_CONF_SECTION_QUALITY
58 DRI_CONF_FORCE_S3TC_ENABLE(false)
59 DRI_CONF_ALLOW_LARGE_TEXTURES(1)
60 DRI_CONF_SECTION_END DRI_CONF_END;
61 const GLuint __driNConfigOptions = 4;
62
63 #ifdef USE_NEW_INTERFACE
64 static PFNGLXCREATECONTEXTMODES create_context_modes = NULL;
65 #endif /*USE_NEW_INTERFACE */
66
67 extern const struct dri_extension card_extensions[];
68
69 /**
70 * Map all the memory regions described by the screen.
71 * \return GL_TRUE if success, GL_FALSE if error.
72 */
73 GLboolean
74 intelMapScreenRegions(__DRIscreenPrivate * sPriv)
75 {
76 intelScreenPrivate *intelScreen = (intelScreenPrivate *) sPriv->private;
77
78 if (intelScreen->front.handle) {
79 if (drmMap(sPriv->fd,
80 intelScreen->front.handle,
81 intelScreen->front.size,
82 (drmAddress *) & intelScreen->front.map) != 0) {
83 _mesa_problem(NULL, "drmMap(frontbuffer) failed!");
84 return GL_FALSE;
85 }
86 }
87 else {
88 _mesa_warning(NULL, "no front buffer handle in intelMapScreenRegions!");
89 }
90
91 if (0)
92 _mesa_printf("Back 0x%08x ", intelScreen->back.handle);
93 if (drmMap(sPriv->fd,
94 intelScreen->back.handle,
95 intelScreen->back.size,
96 (drmAddress *) & intelScreen->back.map) != 0) {
97 intelUnmapScreenRegions(intelScreen);
98 return GL_FALSE;
99 }
100
101 if (intelScreen->third.handle) {
102 if (0)
103 _mesa_printf("Third 0x%08x ", intelScreen->third.handle);
104 if (drmMap(sPriv->fd,
105 intelScreen->third.handle,
106 intelScreen->third.size,
107 (drmAddress *) & intelScreen->third.map) != 0) {
108 intelUnmapScreenRegions(intelScreen);
109 return GL_FALSE;
110 }
111 }
112
113 if (0)
114 _mesa_printf("Depth 0x%08x ", intelScreen->depth.handle);
115 if (drmMap(sPriv->fd,
116 intelScreen->depth.handle,
117 intelScreen->depth.size,
118 (drmAddress *) & intelScreen->depth.map) != 0) {
119 intelUnmapScreenRegions(intelScreen);
120 return GL_FALSE;
121 }
122
123 if (0)
124 _mesa_printf("TEX 0x%08x ", intelScreen->tex.handle);
125 if (intelScreen->tex.size != 0) {
126 if (drmMap(sPriv->fd,
127 intelScreen->tex.handle,
128 intelScreen->tex.size,
129 (drmAddress *) & intelScreen->tex.map) != 0) {
130 intelUnmapScreenRegions(intelScreen);
131 return GL_FALSE;
132 }
133 }
134
135 if (0)
136 printf("Mappings: front: %p back: %p third: %p depth: %p tex: %p\n",
137 intelScreen->front.map,
138 intelScreen->back.map, intelScreen->third.map,
139 intelScreen->depth.map, intelScreen->tex.map);
140 return GL_TRUE;
141 }
142
143 /** Driver-specific fence emit implementation for the fake memory manager. */
144 static unsigned int
145 intel_fence_emit(void *private)
146 {
147 intelScreenPrivate *intelScreen = (intelScreenPrivate *)private;
148 unsigned int fence;
149
150 /* XXX: Need to emit a flush, if we haven't already (at least with the
151 * current batchbuffer implementation, we have).
152 */
153
154 fence = intelEmitIrqLocked(intelScreen);
155
156 return fence;
157 }
158
159 /** Driver-specific fence wait implementation for the fake memory manager. */
160 static int
161 intel_fence_wait(void *private, unsigned int cookie)
162 {
163 intelScreenPrivate *intelScreen = (intelScreenPrivate *)private;
164
165 intelWaitIrq(intelScreen, cookie);
166
167 return 0;
168 }
169
170 static struct intel_region *
171 intel_recreate_static(intelScreenPrivate *intelScreen,
172 struct intel_region *region,
173 intelRegion *region_desc,
174 GLuint mem_type)
175 {
176 if (region) {
177 intel_region_update_static(intelScreen, region, mem_type,
178 region_desc->bo_handle, region_desc->offset,
179 region_desc->map, intelScreen->cpp,
180 region_desc->pitch / intelScreen->cpp,
181 intelScreen->height);
182 } else {
183 region = intel_region_create_static(intelScreen, mem_type,
184 region_desc->bo_handle,
185 region_desc->offset,
186 region_desc->map, intelScreen->cpp,
187 region_desc->pitch / intelScreen->cpp,
188 intelScreen->height);
189 }
190
191 assert(region->buffer != NULL);
192
193 return region;
194 }
195
196
197 /* Create intel_region structs to describe the static front,back,depth
198 * buffers created by the xserver.
199 *
200 * Although FBO's mean we now no longer use these as render targets in
201 * all circumstances, they won't go away until the back and depth
202 * buffers become private, and the front and rotated buffers will
203 * remain even then.
204 *
205 * Note that these don't allocate video memory, just describe
206 * allocations alread made by the X server.
207 */
208 static void
209 intel_recreate_static_regions(intelScreenPrivate *intelScreen)
210 {
211 intelScreen->front_region =
212 intel_recreate_static(intelScreen,
213 intelScreen->front_region,
214 &intelScreen->front,
215 DRM_BO_FLAG_MEM_TT);
216
217 /* The rotated region is only used for old DDXes that didn't handle rotation
218 \ * on their own.
219 */
220 if (intelScreen->driScrnPriv->ddxMinor < 8) {
221 intelScreen->rotated_region =
222 intel_recreate_static(intelScreen,
223 intelScreen->rotated_region,
224 &intelScreen->rotated,
225 DRM_BO_FLAG_MEM_TT);
226 }
227
228 intelScreen->back_region =
229 intel_recreate_static(intelScreen,
230 intelScreen->back_region,
231 &intelScreen->back,
232 DRM_BO_FLAG_MEM_TT);
233
234 if (intelScreen->third.handle) {
235 intelScreen->third_region =
236 intel_recreate_static(intelScreen,
237 intelScreen->third_region,
238 &intelScreen->third,
239 DRM_BO_FLAG_MEM_TT);
240 }
241
242 /* Still assumes front.cpp == depth.cpp. We can kill this when we move to
243 * private buffers.
244 */
245 intelScreen->depth_region =
246 intel_recreate_static(intelScreen,
247 intelScreen->depth_region,
248 &intelScreen->depth,
249 DRM_BO_FLAG_MEM_TT);
250 }
251
252 /**
253 * Use the information in the sarea to update the screen parameters
254 * related to screen rotation. Needs to be called locked.
255 */
256 void
257 intelUpdateScreenRotation(__DRIscreenPrivate * sPriv, drmI830Sarea * sarea)
258 {
259 intelScreenPrivate *intelScreen = (intelScreenPrivate *) sPriv->private;
260
261 intelUnmapScreenRegions(intelScreen);
262 intelUpdateScreenFromSAREA(intelScreen, sarea);
263 if (!intelMapScreenRegions(sPriv)) {
264 fprintf(stderr, "ERROR Remapping screen regions!!!\n");
265 }
266 intel_recreate_static_regions(intelScreen);
267 }
268
269
270 void
271 intelUnmapScreenRegions(intelScreenPrivate * intelScreen)
272 {
273 #define REALLY_UNMAP 1
274 if (intelScreen->front.map) {
275 #if REALLY_UNMAP
276 if (drmUnmap(intelScreen->front.map, intelScreen->front.size) != 0)
277 printf("drmUnmap front failed!\n");
278 #endif
279 intelScreen->front.map = NULL;
280 }
281 if (intelScreen->back.map) {
282 #if REALLY_UNMAP
283 if (drmUnmap(intelScreen->back.map, intelScreen->back.size) != 0)
284 printf("drmUnmap back failed!\n");
285 #endif
286 intelScreen->back.map = NULL;
287 }
288 if (intelScreen->third.map) {
289 #if REALLY_UNMAP
290 if (drmUnmap(intelScreen->third.map, intelScreen->third.size) != 0)
291 printf("drmUnmap third failed!\n");
292 #endif
293 intelScreen->third.map = NULL;
294 }
295 if (intelScreen->depth.map) {
296 #if REALLY_UNMAP
297 drmUnmap(intelScreen->depth.map, intelScreen->depth.size);
298 intelScreen->depth.map = NULL;
299 #endif
300 }
301 if (intelScreen->tex.map) {
302 #if REALLY_UNMAP
303 drmUnmap(intelScreen->tex.map, intelScreen->tex.size);
304 intelScreen->tex.map = NULL;
305 #endif
306 }
307 }
308
309
310 static void
311 intelPrintDRIInfo(intelScreenPrivate * intelScreen,
312 __DRIscreenPrivate * sPriv, I830DRIPtr gDRIPriv)
313 {
314 fprintf(stderr, "*** Front size: 0x%x offset: 0x%x pitch: %d\n",
315 intelScreen->front.size, intelScreen->front.offset,
316 intelScreen->front.pitch);
317 fprintf(stderr, "*** Back size: 0x%x offset: 0x%x pitch: %d\n",
318 intelScreen->back.size, intelScreen->back.offset,
319 intelScreen->back.pitch);
320 fprintf(stderr, "*** Depth size: 0x%x offset: 0x%x pitch: %d\n",
321 intelScreen->depth.size, intelScreen->depth.offset,
322 intelScreen->depth.pitch);
323 fprintf(stderr, "*** Rotated size: 0x%x offset: 0x%x pitch: %d\n",
324 intelScreen->rotated.size, intelScreen->rotated.offset,
325 intelScreen->rotated.pitch);
326 fprintf(stderr, "*** Texture size: 0x%x offset: 0x%x\n",
327 intelScreen->tex.size, intelScreen->tex.offset);
328 fprintf(stderr, "*** Memory : 0x%x\n", gDRIPriv->mem);
329 }
330
331
332 static void
333 intelPrintSAREA(const drmI830Sarea * sarea)
334 {
335 fprintf(stderr, "SAREA: sarea width %d height %d\n", sarea->width,
336 sarea->height);
337 fprintf(stderr, "SAREA: pitch: %d\n", sarea->pitch);
338 fprintf(stderr,
339 "SAREA: front offset: 0x%08x size: 0x%x handle: 0x%x\n",
340 sarea->front_offset, sarea->front_size,
341 (unsigned) sarea->front_handle);
342 fprintf(stderr,
343 "SAREA: back offset: 0x%08x size: 0x%x handle: 0x%x\n",
344 sarea->back_offset, sarea->back_size,
345 (unsigned) sarea->back_handle);
346 fprintf(stderr, "SAREA: depth offset: 0x%08x size: 0x%x handle: 0x%x\n",
347 sarea->depth_offset, sarea->depth_size,
348 (unsigned) sarea->depth_handle);
349 fprintf(stderr, "SAREA: tex offset: 0x%08x size: 0x%x handle: 0x%x\n",
350 sarea->tex_offset, sarea->tex_size, (unsigned) sarea->tex_handle);
351 fprintf(stderr, "SAREA: rotation: %d\n", sarea->rotation);
352 fprintf(stderr,
353 "SAREA: rotated offset: 0x%08x size: 0x%x\n",
354 sarea->rotated_offset, sarea->rotated_size);
355 fprintf(stderr, "SAREA: rotated pitch: %d\n", sarea->rotated_pitch);
356 }
357
358
359 /**
360 * A number of the screen parameters are obtained/computed from
361 * information in the SAREA. This function updates those parameters.
362 */
363 void
364 intelUpdateScreenFromSAREA(intelScreenPrivate * intelScreen,
365 drmI830Sarea * sarea)
366 {
367 intelScreen->width = sarea->width;
368 intelScreen->height = sarea->height;
369
370 intelScreen->front.offset = sarea->front_offset;
371 intelScreen->front.pitch = sarea->pitch * intelScreen->cpp;
372 intelScreen->front.handle = sarea->front_handle;
373 intelScreen->front.size = sarea->front_size;
374
375 intelScreen->back.offset = sarea->back_offset;
376 intelScreen->back.pitch = sarea->pitch * intelScreen->cpp;
377 intelScreen->back.handle = sarea->back_handle;
378 intelScreen->back.size = sarea->back_size;
379
380 if (intelScreen->driScrnPriv->ddxMinor >= 8) {
381 intelScreen->third.offset = sarea->third_offset;
382 intelScreen->third.pitch = sarea->pitch * intelScreen->cpp;
383 intelScreen->third.handle = sarea->third_handle;
384 intelScreen->third.size = sarea->third_size;
385 }
386
387 intelScreen->depth.offset = sarea->depth_offset;
388 intelScreen->depth.pitch = sarea->pitch * intelScreen->cpp;
389 intelScreen->depth.handle = sarea->depth_handle;
390 intelScreen->depth.size = sarea->depth_size;
391
392 if (intelScreen->driScrnPriv->ddxMinor >= 9) {
393 intelScreen->front.bo_handle = sarea->front_bo_handle;
394 intelScreen->back.bo_handle = sarea->back_bo_handle;
395 intelScreen->third.bo_handle = sarea->third_bo_handle;
396 intelScreen->depth.bo_handle = sarea->depth_bo_handle;
397 } else {
398 intelScreen->front.bo_handle = -1;
399 intelScreen->back.bo_handle = -1;
400 intelScreen->third.bo_handle = -1;
401 intelScreen->depth.bo_handle = -1;
402 }
403
404 intelScreen->tex.offset = sarea->tex_offset;
405 intelScreen->logTextureGranularity = sarea->log_tex_granularity;
406 intelScreen->tex.handle = sarea->tex_handle;
407 intelScreen->tex.size = sarea->tex_size;
408
409 intelScreen->rotated.offset = sarea->rotated_offset;
410 intelScreen->rotated.pitch = sarea->rotated_pitch * intelScreen->cpp;
411 intelScreen->rotated.size = sarea->rotated_size;
412 intelScreen->current_rotation = sarea->rotation;
413 matrix23Rotate(&intelScreen->rotMatrix,
414 sarea->width, sarea->height, sarea->rotation);
415 intelScreen->rotatedWidth = sarea->virtualX;
416 intelScreen->rotatedHeight = sarea->virtualY;
417
418 if (0)
419 intelPrintSAREA(sarea);
420 }
421
422
423 static GLboolean
424 intelInitDriver(__DRIscreenPrivate * sPriv)
425 {
426 intelScreenPrivate *intelScreen;
427 I830DRIPtr gDRIPriv = (I830DRIPtr) sPriv->pDevPriv;
428 drmI830Sarea *sarea;
429
430 PFNGLXSCRENABLEEXTENSIONPROC glx_enable_extension =
431 (PFNGLXSCRENABLEEXTENSIONPROC) (*dri_interface->
432 getProcAddress("glxEnableExtension"));
433 void *const psc = sPriv->psc->screenConfigs;
434
435 if (sPriv->devPrivSize != sizeof(I830DRIRec)) {
436 fprintf(stderr,
437 "\nERROR! sizeof(I830DRIRec) does not match passed size from device driver\n");
438 return GL_FALSE;
439 }
440
441 /* Allocate the private area */
442 intelScreen = (intelScreenPrivate *) CALLOC(sizeof(intelScreenPrivate));
443 if (!intelScreen) {
444 fprintf(stderr, "\nERROR! Allocating private area failed\n");
445 return GL_FALSE;
446 }
447 /* parse information in __driConfigOptions */
448 driParseOptionInfo(&intelScreen->optionCache,
449 __driConfigOptions, __driNConfigOptions);
450
451 intelScreen->driScrnPriv = sPriv;
452 sPriv->private = (void *) intelScreen;
453 intelScreen->sarea_priv_offset = gDRIPriv->sarea_priv_offset;
454 sarea = (drmI830Sarea *)
455 (((GLubyte *) sPriv->pSAREA) + intelScreen->sarea_priv_offset);
456
457 intelScreen->deviceID = gDRIPriv->deviceID;
458 if (intelScreen->deviceID == PCI_CHIP_I865_G)
459 intelScreen->maxBatchSize = 4096;
460 else
461 intelScreen->maxBatchSize = BATCH_SZ;
462
463 intelScreen->mem = gDRIPriv->mem;
464 intelScreen->cpp = gDRIPriv->cpp;
465
466 switch (gDRIPriv->bitsPerPixel) {
467 case 16:
468 intelScreen->fbFormat = DV_PF_565;
469 break;
470 case 32:
471 intelScreen->fbFormat = DV_PF_8888;
472 break;
473 default:
474 exit(1);
475 break;
476 }
477
478 intelUpdateScreenFromSAREA(intelScreen, sarea);
479
480 if (!intelMapScreenRegions(sPriv)) {
481 fprintf(stderr, "\nERROR! mapping regions\n");
482 _mesa_free(intelScreen);
483 sPriv->private = NULL;
484 return GL_FALSE;
485 }
486
487 intelScreen->sarea_priv_offset = gDRIPriv->sarea_priv_offset;
488
489 if (0)
490 intelPrintDRIInfo(intelScreen, sPriv, gDRIPriv);
491
492 intelScreen->drmMinor = sPriv->drmMinor;
493
494 /* Determine if IRQs are active? */
495 {
496 int ret;
497 drmI830GetParam gp;
498
499 gp.param = I830_PARAM_IRQ_ACTIVE;
500 gp.value = &intelScreen->irq_active;
501
502 ret = drmCommandWriteRead(sPriv->fd, DRM_I830_GETPARAM,
503 &gp, sizeof(gp));
504 if (ret) {
505 fprintf(stderr, "drmI830GetParam: %d\n", ret);
506 return GL_FALSE;
507 }
508 }
509
510 /* Determine if batchbuffers are allowed */
511 {
512 int ret;
513 drmI830GetParam gp;
514
515 gp.param = I830_PARAM_ALLOW_BATCHBUFFER;
516 gp.value = &intelScreen->allow_batchbuffer;
517
518 ret = drmCommandWriteRead(sPriv->fd, DRM_I830_GETPARAM,
519 &gp, sizeof(gp));
520 if (ret) {
521 fprintf(stderr, "drmI830GetParam: (%d) %d\n", gp.param, ret);
522 return GL_FALSE;
523 }
524 }
525
526 if (glx_enable_extension != NULL) {
527 (*glx_enable_extension) (psc, "GLX_SGI_swap_control");
528 (*glx_enable_extension) (psc, "GLX_SGI_video_sync");
529 (*glx_enable_extension) (psc, "GLX_MESA_swap_control");
530 (*glx_enable_extension) (psc, "GLX_MESA_swap_frame_usage");
531 (*glx_enable_extension) (psc, "GLX_SGI_make_current_read");
532 }
533
534 /* If we've got a new enough DDX that's initializing TTM and giving us
535 * object handles for the shared buffers, use that.
536 */
537 intelScreen->ttm = GL_FALSE;
538 if (getenv("INTEL_NO_TTM") == NULL &&
539 intelScreen->driScrnPriv->ddxMinor >= 9 &&
540 intelScreen->front.bo_handle != -1) {
541 intelScreen->bufmgr = dri_bufmgr_ttm_init(sPriv->fd,
542 DRM_FENCE_TYPE_EXE,
543 DRM_FENCE_TYPE_EXE |
544 DRM_I915_FENCE_TYPE_RW);
545 if (intelScreen->bufmgr != NULL)
546 intelScreen->ttm = GL_TRUE;
547 }
548 /* Otherwise, use the classic buffer manager. */
549 if (intelScreen->bufmgr == NULL) {
550 if (intelScreen->tex.size == 0) {
551 fprintf(stderr, "[%s:%u] Error initializing buffer manager.\n",
552 __func__, __LINE__);
553 return GL_FALSE;
554 }
555 fprintf(stderr, "[%s:%u] Failed to init TTM buffer manager, falling back"
556 " to classic.\n", __func__, __LINE__);
557 intelScreen->bufmgr = dri_bufmgr_fake_init(intelScreen->tex.offset,
558 intelScreen->tex.map,
559 intelScreen->tex.size,
560 intel_fence_emit,
561 intel_fence_wait,
562 intelScreen);
563 }
564
565 intel_recreate_static_regions(intelScreen);
566
567 return GL_TRUE;
568 }
569
570
571 static void
572 intelDestroyScreen(__DRIscreenPrivate * sPriv)
573 {
574 intelScreenPrivate *intelScreen = (intelScreenPrivate *) sPriv->private;
575
576 intelUnmapScreenRegions(intelScreen);
577
578 dri_bufmgr_destroy(intelScreen->bufmgr);
579 FREE(intelScreen);
580 sPriv->private = NULL;
581 }
582
583
584 /**
585 * This is called when we need to set up GL rendering to a new X window.
586 */
587 static GLboolean
588 intelCreateBuffer(__DRIscreenPrivate * driScrnPriv,
589 __DRIdrawablePrivate * driDrawPriv,
590 const __GLcontextModes * mesaVis, GLboolean isPixmap)
591 {
592 intelScreenPrivate *screen = (intelScreenPrivate *) driScrnPriv->private;
593
594 if (isPixmap) {
595 return GL_FALSE; /* not implemented */
596 }
597 else {
598 GLboolean swStencil = (mesaVis->stencilBits > 0 &&
599 mesaVis->depthBits != 24);
600 GLenum rgbFormat = (mesaVis->redBits == 5 ? GL_RGB5 : GL_RGBA8);
601
602 struct intel_framebuffer *intel_fb = CALLOC_STRUCT(intel_framebuffer);
603
604 if (!intel_fb)
605 return GL_FALSE;
606
607 _mesa_initialize_framebuffer(&intel_fb->Base, mesaVis);
608
609 /* setup the hardware-based renderbuffers */
610 {
611 intel_fb->color_rb[0]
612 = intel_create_renderbuffer(rgbFormat,
613 screen->width, screen->height,
614 screen->front.offset,
615 screen->front.pitch,
616 screen->cpp,
617 screen->front.map);
618 intel_set_span_functions(&intel_fb->color_rb[0]->Base);
619 _mesa_add_renderbuffer(&intel_fb->Base, BUFFER_FRONT_LEFT,
620 &intel_fb->color_rb[0]->Base);
621 }
622
623 if (mesaVis->doubleBufferMode) {
624 intel_fb->color_rb[1]
625 = intel_create_renderbuffer(rgbFormat,
626 screen->width, screen->height,
627 screen->back.offset,
628 screen->back.pitch,
629 screen->cpp,
630 screen->back.map);
631 intel_set_span_functions(&intel_fb->color_rb[1]->Base);
632 _mesa_add_renderbuffer(&intel_fb->Base, BUFFER_BACK_LEFT,
633 &intel_fb->color_rb[1]->Base);
634
635 if (screen->third.handle) {
636 struct gl_renderbuffer *tmp_rb = NULL;
637
638 intel_fb->color_rb[2]
639 = intel_create_renderbuffer(rgbFormat,
640 screen->width, screen->height,
641 screen->third.offset,
642 screen->third.pitch,
643 screen->cpp,
644 screen->third.map);
645 intel_set_span_functions(&intel_fb->color_rb[2]->Base);
646 _mesa_reference_renderbuffer(&tmp_rb, &intel_fb->color_rb[2]->Base);
647 }
648 }
649
650 if (mesaVis->depthBits == 24 && mesaVis->stencilBits == 8) {
651 /* combined depth/stencil buffer */
652 struct intel_renderbuffer *depthStencilRb
653 = intel_create_renderbuffer(GL_DEPTH24_STENCIL8_EXT,
654 screen->width, screen->height,
655 screen->depth.offset,
656 screen->depth.pitch,
657 screen->cpp, /* 4! */
658 screen->depth.map);
659 intel_set_span_functions(&depthStencilRb->Base);
660 /* note: bind RB to two attachment points */
661 _mesa_add_renderbuffer(&intel_fb->Base, BUFFER_DEPTH,
662 &depthStencilRb->Base);
663 _mesa_add_renderbuffer(&intel_fb->Base, BUFFER_STENCIL,
664 &depthStencilRb->Base);
665 }
666 else if (mesaVis->depthBits == 16) {
667 /* just 16-bit depth buffer, no hw stencil */
668 struct intel_renderbuffer *depthRb
669 = intel_create_renderbuffer(GL_DEPTH_COMPONENT16,
670 screen->width, screen->height,
671 screen->depth.offset,
672 screen->depth.pitch,
673 screen->cpp, /* 2! */
674 screen->depth.map);
675 intel_set_span_functions(&depthRb->Base);
676 _mesa_add_renderbuffer(&intel_fb->Base, BUFFER_DEPTH, &depthRb->Base);
677 }
678
679 /* now add any/all software-based renderbuffers we may need */
680 _mesa_add_soft_renderbuffers(&intel_fb->Base,
681 GL_FALSE, /* never sw color */
682 GL_FALSE, /* never sw depth */
683 swStencil, mesaVis->accumRedBits > 0,
684 GL_FALSE, /* never sw alpha */
685 GL_FALSE /* never sw aux */ );
686 driDrawPriv->driverPrivate = (void *) intel_fb;
687
688 return GL_TRUE;
689 }
690 }
691
692 static void
693 intelDestroyBuffer(__DRIdrawablePrivate * driDrawPriv)
694 {
695 _mesa_unreference_framebuffer((GLframebuffer **)(&(driDrawPriv->driverPrivate)));
696 }
697
698
699 /**
700 * Get information about previous buffer swaps.
701 */
702 static int
703 intelGetSwapInfo(__DRIdrawablePrivate * dPriv, __DRIswapInfo * sInfo)
704 {
705 struct intel_framebuffer *intel_fb;
706
707 if ((dPriv == NULL) || (dPriv->driverPrivate == NULL)
708 || (sInfo == NULL)) {
709 return -1;
710 }
711
712 intel_fb = dPriv->driverPrivate;
713 sInfo->swap_count = intel_fb->swap_count;
714 sInfo->swap_ust = intel_fb->swap_ust;
715 sInfo->swap_missed_count = intel_fb->swap_missed_count;
716
717 sInfo->swap_missed_usage = (sInfo->swap_missed_count != 0)
718 ? driCalculateSwapUsage(dPriv, 0, intel_fb->swap_missed_ust)
719 : 0.0;
720
721 return 0;
722 }
723
724
725 /* There are probably better ways to do this, such as an
726 * init-designated function to register chipids and createcontext
727 * functions.
728 */
729 extern GLboolean i830CreateContext(const __GLcontextModes * mesaVis,
730 __DRIcontextPrivate * driContextPriv,
731 void *sharedContextPrivate);
732
733 extern GLboolean i915CreateContext(const __GLcontextModes * mesaVis,
734 __DRIcontextPrivate * driContextPriv,
735 void *sharedContextPrivate);
736
737
738
739
740 static GLboolean
741 intelCreateContext(const __GLcontextModes * mesaVis,
742 __DRIcontextPrivate * driContextPriv,
743 void *sharedContextPrivate)
744 {
745 __DRIscreenPrivate *sPriv = driContextPriv->driScreenPriv;
746 intelScreenPrivate *intelScreen = (intelScreenPrivate *) sPriv->private;
747
748 switch (intelScreen->deviceID) {
749 /* Don't deal with i830 until texture work complete:
750 */
751 case PCI_CHIP_845_G:
752 case PCI_CHIP_I830_M:
753 case PCI_CHIP_I855_GM:
754 case PCI_CHIP_I865_G:
755 return i830CreateContext(mesaVis, driContextPriv, sharedContextPrivate);
756
757 case PCI_CHIP_I915_G:
758 case PCI_CHIP_I915_GM:
759 case PCI_CHIP_I945_G:
760 case PCI_CHIP_I945_GM:
761 case PCI_CHIP_I945_GME:
762 case PCI_CHIP_G33_G:
763 case PCI_CHIP_Q35_G:
764 case PCI_CHIP_Q33_G:
765 return i915CreateContext(mesaVis, driContextPriv, sharedContextPrivate);
766
767 default:
768 fprintf(stderr, "Unrecognized deviceID %x\n", intelScreen->deviceID);
769 return GL_FALSE;
770 }
771 }
772
773
774 static const struct __DriverAPIRec intelAPI = {
775 .InitDriver = intelInitDriver,
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 bootstrap function for the driver. libGL supplies all of the
881 * requisite information about the system, and the driver initializes itself.
882 * This routine also fills in the linked list pointed to by \c driver_modes
883 * with the \c __GLcontextModes that the driver can support for windows or
884 * pbuffers.
885 *
886 * \return A pointer to a \c __DRIscreenPrivate on success, or \c NULL on
887 * failure.
888 */
889 PUBLIC
890 void * __DRI_CREATE_NEW_SCREEN(int scrn,
891 __DRIscreen * psc,
892 const __GLcontextModes * modes,
893 const __DRIversion * ddx_version,
894 const __DRIversion * dri_version,
895 const __DRIversion * drm_version,
896 const __DRIframebuffer * frame_buffer,
897 drmAddress pSAREA, int fd,
898 int internal_api_version,
899 const __DRIinterfaceMethods * interface,
900 __GLcontextModes ** driver_modes)
901 {
902 __DRIscreenPrivate *psp;
903 static const __DRIversion ddx_expected = { 1, 5, 0 };
904 static const __DRIversion dri_expected = { 4, 0, 0 };
905 static const __DRIversion drm_expected = { 1, 5, 0 };
906
907 dri_interface = interface;
908
909 if (!driCheckDriDdxDrmVersions2("i915",
910 dri_version, &dri_expected,
911 ddx_version, &ddx_expected,
912 drm_version, &drm_expected)) {
913 return NULL;
914 }
915
916 psp = __driUtilCreateNewScreen(scrn, psc, NULL,
917 ddx_version, dri_version, drm_version,
918 frame_buffer, pSAREA, fd,
919 internal_api_version, &intelAPI);
920 if (psp != NULL) {
921 I830DRIPtr dri_priv = (I830DRIPtr) psp->pDevPriv;
922 *driver_modes = intelFillInModes(dri_priv->cpp * 8,
923 (dri_priv->cpp == 2) ? 16 : 24,
924 (dri_priv->cpp == 2) ? 0 : 8, 1);
925
926 /* Calling driInitExtensions here, with a NULL context pointer, does not actually
927 * enable the extensions. It just makes sure that all the dispatch offsets for all
928 * the extensions that *might* be enables are known. This is needed because the
929 * dispatch offsets need to be known when _mesa_context_create is called, but we can't
930 * enable the extensions until we have a context pointer.
931 *
932 * Hello chicken. Hello egg. How are you two today?
933 */
934 driInitExtensions(NULL, card_extensions, GL_FALSE);
935 }
936
937 return (void *) psp;
938 }
939
940 struct intel_context *intelScreenContext(intelScreenPrivate *intelScreen)
941 {
942 /*
943 * This should probably change to have the screen allocate a dummy
944 * context at screen creation. For now just use the current context.
945 */
946
947 GET_CURRENT_CONTEXT(ctx);
948 if (ctx == NULL) {
949 _mesa_problem(NULL, "No current context in intelScreenContext\n");
950 return NULL;
951 }
952 return intel_context(ctx);
953 }
954