i965: improve debug logging
[mesa.git] / src / mesa / drivers / dri / intel / intel_regions.c
1 /**************************************************************************
2 *
3 * Copyright 2006 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 /* Provide additional functionality on top of bufmgr buffers:
29 * - 2d semantics and blit operations
30 * - refcounting of buffers for multiple images in a buffer.
31 * - refcounting of buffer mappings.
32 * - some logic for moving the buffers to the best memory pools for
33 * given operations.
34 *
35 * Most of this is to make it easier to implement the fixed-layout
36 * mipmap tree required by intel hardware in the face of GL's
37 * programming interface where each image can be specifed in random
38 * order and it isn't clear what layout the tree should have until the
39 * last moment.
40 */
41
42 #include <sys/ioctl.h>
43 #include <errno.h>
44
45 #include "intel_context.h"
46 #include "intel_regions.h"
47 #include "intel_blit.h"
48 #include "intel_buffer_objects.h"
49 #include "intel_bufmgr.h"
50 #include "intel_batchbuffer.h"
51 #include "intel_chipset.h"
52
53 #define FILE_DEBUG_FLAG DEBUG_REGION
54
55 /* This should be set to the maximum backtrace size desired.
56 * Set it to 0 to disable backtrace debugging.
57 */
58 #define DEBUG_BACKTRACE_SIZE 0
59
60 #if DEBUG_BACKTRACE_SIZE == 0
61 /* Use the standard debug output */
62 #define _DBG(...) DBG(__VA_ARGS__)
63 #else
64 /* Use backtracing debug output */
65 #define _DBG(...) {debug_backtrace(); DBG(__VA_ARGS__);}
66
67 /* Backtracing debug support */
68 #include <execinfo.h>
69
70 static void
71 debug_backtrace(void)
72 {
73 void *trace[DEBUG_BACKTRACE_SIZE];
74 char **strings = NULL;
75 int traceSize;
76 register int i;
77
78 traceSize = backtrace(trace, DEBUG_BACKTRACE_SIZE);
79 strings = backtrace_symbols(trace, traceSize);
80 if (strings == NULL) {
81 DBG("no backtrace:");
82 return;
83 }
84
85 /* Spit out all the strings with a colon separator. Ignore
86 * the first, since we don't really care about the call
87 * to debug_backtrace() itself. Skip until the final "/" in
88 * the trace to avoid really long lines.
89 */
90 for (i = 1; i < traceSize; i++) {
91 char *p = strings[i], *slash = strings[i];
92 while (*p) {
93 if (*p++ == '/') {
94 slash = p;
95 }
96 }
97
98 DBG("%s:", slash);
99 }
100
101 /* Free up the memory, and we're done */
102 free(strings);
103 }
104
105 #endif
106
107
108
109 /* XXX: Thread safety?
110 */
111 GLubyte *
112 intel_region_map(struct intel_context *intel, struct intel_region *region)
113 {
114 _DBG("%s %p\n", __FUNCTION__, region);
115 if (!region->map_refcount++) {
116 if (region->pbo)
117 intel_region_cow(intel, region);
118
119 dri_bo_map(region->buffer, GL_TRUE);
120 region->map = region->buffer->virtual;
121 }
122
123 return region->map;
124 }
125
126 void
127 intel_region_unmap(struct intel_context *intel, struct intel_region *region)
128 {
129 _DBG("%s %p\n", __FUNCTION__, region);
130 if (!--region->map_refcount) {
131 dri_bo_unmap(region->buffer);
132 region->map = NULL;
133 }
134 }
135
136 static struct intel_region *
137 intel_region_alloc_internal(struct intel_context *intel,
138 GLuint cpp,
139 GLuint width, GLuint height, GLuint pitch,
140 dri_bo *buffer)
141 {
142 struct intel_region *region;
143
144 if (buffer == NULL) {
145 _DBG("%s <-- NULL\n", __FUNCTION__);
146 return NULL;
147 }
148
149 region = calloc(sizeof(*region), 1);
150 region->cpp = cpp;
151 region->width = width;
152 region->height = height;
153 region->pitch = pitch;
154 region->refcount = 1;
155 region->buffer = buffer;
156
157 /* Default to no tiling */
158 region->tiling = I915_TILING_NONE;
159 region->bit_6_swizzle = I915_BIT_6_SWIZZLE_NONE;
160
161 _DBG("%s <-- %p\n", __FUNCTION__, region);
162 return region;
163 }
164
165 struct intel_region *
166 intel_region_alloc(struct intel_context *intel,
167 GLuint cpp, GLuint width, GLuint height, GLuint pitch,
168 GLboolean expect_accelerated_upload)
169 {
170 dri_bo *buffer;
171
172 if (expect_accelerated_upload) {
173 buffer = drm_intel_bo_alloc_for_render(intel->bufmgr, "region",
174 pitch * cpp * height, 64);
175 } else {
176 buffer = drm_intel_bo_alloc(intel->bufmgr, "region",
177 pitch * cpp * height, 64);
178 }
179
180 return intel_region_alloc_internal(intel, cpp, width, height, pitch, buffer);
181 }
182
183 struct intel_region *
184 intel_region_alloc_for_handle(struct intel_context *intel,
185 GLuint cpp,
186 GLuint width, GLuint height, GLuint pitch,
187 GLuint handle, const char *name)
188 {
189 struct intel_region *region;
190 dri_bo *buffer;
191 int ret;
192
193 buffer = intel_bo_gem_create_from_name(intel->bufmgr, name, handle);
194
195 region = intel_region_alloc_internal(intel, cpp,
196 width, height, pitch, buffer);
197 if (region == NULL)
198 return region;
199
200 ret = dri_bo_get_tiling(region->buffer, &region->tiling,
201 &region->bit_6_swizzle);
202 if (ret != 0) {
203 fprintf(stderr, "Couldn't get tiling of buffer %d (%s): %s\n",
204 handle, name, strerror(-ret));
205 intel_region_release(&region);
206 return NULL;
207 }
208
209 return region;
210 }
211
212 void
213 intel_region_reference(struct intel_region **dst, struct intel_region *src)
214 {
215 if (src)
216 _DBG("%s %p %d\n", __FUNCTION__, src, src->refcount);
217
218 assert(*dst == NULL);
219 if (src) {
220 src->refcount++;
221 *dst = src;
222 }
223 }
224
225 void
226 intel_region_release(struct intel_region **region_handle)
227 {
228 struct intel_region *region = *region_handle;
229
230 if (region == NULL) {
231 _DBG("%s NULL\n", __FUNCTION__);
232 return;
233 }
234
235 _DBG("%s %p %d\n", __FUNCTION__, region, region->refcount - 1);
236
237 ASSERT(region->refcount > 0);
238 region->refcount--;
239
240 if (region->refcount == 0) {
241 assert(region->map_refcount == 0);
242
243 if (region->pbo)
244 region->pbo->region = NULL;
245 region->pbo = NULL;
246 dri_bo_unreference(region->buffer);
247
248 if (region->classic_map != NULL) {
249 drmUnmap(region->classic_map,
250 region->pitch * region->cpp * region->height);
251 }
252
253 free(region);
254 }
255 *region_handle = NULL;
256 }
257
258 /*
259 * XXX Move this into core Mesa?
260 */
261 void
262 _mesa_copy_rect(GLubyte * dst,
263 GLuint cpp,
264 GLuint dst_pitch,
265 GLuint dst_x,
266 GLuint dst_y,
267 GLuint width,
268 GLuint height,
269 const GLubyte * src,
270 GLuint src_pitch, GLuint src_x, GLuint src_y)
271 {
272 GLuint i;
273
274 dst_pitch *= cpp;
275 src_pitch *= cpp;
276 dst += dst_x * cpp;
277 src += src_x * cpp;
278 dst += dst_y * dst_pitch;
279 src += src_y * dst_pitch;
280 width *= cpp;
281
282 if (width == dst_pitch && width == src_pitch)
283 memcpy(dst, src, height * width);
284 else {
285 for (i = 0; i < height; i++) {
286 memcpy(dst, src, width);
287 dst += dst_pitch;
288 src += src_pitch;
289 }
290 }
291 }
292
293
294 /* Upload data to a rectangular sub-region. Lots of choices how to do this:
295 *
296 * - memcpy by span to current destination
297 * - upload data as new buffer and blit
298 *
299 * Currently always memcpy.
300 */
301 void
302 intel_region_data(struct intel_context *intel,
303 struct intel_region *dst,
304 GLuint dst_offset,
305 GLuint dstx, GLuint dsty,
306 const void *src, GLuint src_pitch,
307 GLuint srcx, GLuint srcy, GLuint width, GLuint height)
308 {
309 GLboolean locked = GL_FALSE;
310
311 _DBG("%s\n", __FUNCTION__);
312
313 if (intel == NULL)
314 return;
315
316 if (dst->pbo) {
317 if (dstx == 0 &&
318 dsty == 0 && width == dst->pitch && height == dst->height)
319 intel_region_release_pbo(intel, dst);
320 else
321 intel_region_cow(intel, dst);
322 }
323
324 if (!intel->locked) {
325 LOCK_HARDWARE(intel);
326 locked = GL_TRUE;
327 }
328
329 _mesa_copy_rect(intel_region_map(intel, dst) + dst_offset,
330 dst->cpp,
331 dst->pitch,
332 dstx, dsty, width, height, src, src_pitch, srcx, srcy);
333
334 intel_region_unmap(intel, dst);
335
336 if (locked)
337 UNLOCK_HARDWARE(intel);
338
339 }
340
341 /* Copy rectangular sub-regions. Need better logic about when to
342 * push buffers into AGP - will currently do so whenever possible.
343 */
344 void
345 intel_region_copy(struct intel_context *intel,
346 struct intel_region *dst,
347 GLuint dst_offset,
348 GLuint dstx, GLuint dsty,
349 struct intel_region *src,
350 GLuint src_offset,
351 GLuint srcx, GLuint srcy, GLuint width, GLuint height)
352 {
353 _DBG("%s\n", __FUNCTION__);
354
355 if (intel == NULL)
356 return;
357
358 if (dst->pbo) {
359 if (dstx == 0 &&
360 dsty == 0 && width == dst->pitch && height == dst->height)
361 intel_region_release_pbo(intel, dst);
362 else
363 intel_region_cow(intel, dst);
364 }
365
366 assert(src->cpp == dst->cpp);
367
368 intelEmitCopyBlit(intel,
369 dst->cpp,
370 src->pitch, src->buffer, src_offset, src->tiling,
371 dst->pitch, dst->buffer, dst_offset, dst->tiling,
372 srcx, srcy, dstx, dsty, width, height,
373 GL_COPY);
374 }
375
376 /* Fill a rectangular sub-region. Need better logic about when to
377 * push buffers into AGP - will currently do so whenever possible.
378 */
379 void
380 intel_region_fill(struct intel_context *intel,
381 struct intel_region *dst,
382 GLuint dst_offset,
383 GLuint dstx, GLuint dsty,
384 GLuint width, GLuint height, GLuint color)
385 {
386 _DBG("%s\n", __FUNCTION__);
387
388 if (intel == NULL)
389 return;
390
391 if (dst->pbo) {
392 if (dstx == 0 &&
393 dsty == 0 && width == dst->pitch && height == dst->height)
394 intel_region_release_pbo(intel, dst);
395 else
396 intel_region_cow(intel, dst);
397 }
398
399 intelEmitFillBlit(intel,
400 dst->cpp,
401 dst->pitch, dst->buffer, dst_offset, dst->tiling,
402 dstx, dsty, width, height, color);
403 }
404
405 /* Attach to a pbo, discarding our data. Effectively zero-copy upload
406 * the pbo's data.
407 */
408 void
409 intel_region_attach_pbo(struct intel_context *intel,
410 struct intel_region *region,
411 struct intel_buffer_object *pbo)
412 {
413 if (region->pbo == pbo)
414 return;
415
416 _DBG("%s %p %p\n", __FUNCTION__, region, pbo);
417
418 /* If there is already a pbo attached, break the cow tie now.
419 * Don't call intel_region_release_pbo() as that would
420 * unnecessarily allocate a new buffer we would have to immediately
421 * discard.
422 */
423 if (region->pbo) {
424 region->pbo->region = NULL;
425 region->pbo = NULL;
426 }
427
428 if (region->buffer) {
429 dri_bo_unreference(region->buffer);
430 region->buffer = NULL;
431 }
432
433 region->pbo = pbo;
434 region->pbo->region = region;
435 dri_bo_reference(pbo->buffer);
436 region->buffer = pbo->buffer;
437 }
438
439
440 /* Break the COW tie to the pbo and allocate a new buffer.
441 * The pbo gets to keep the data.
442 */
443 void
444 intel_region_release_pbo(struct intel_context *intel,
445 struct intel_region *region)
446 {
447 _DBG("%s %p\n", __FUNCTION__, region);
448 assert(region->buffer == region->pbo->buffer);
449 region->pbo->region = NULL;
450 region->pbo = NULL;
451 dri_bo_unreference(region->buffer);
452 region->buffer = NULL;
453
454 region->buffer = dri_bo_alloc(intel->bufmgr, "region",
455 region->pitch * region->cpp * region->height,
456 64);
457 }
458
459 /* Break the COW tie to the pbo. Both the pbo and the region end up
460 * with a copy of the data.
461 */
462 void
463 intel_region_cow(struct intel_context *intel, struct intel_region *region)
464 {
465 struct intel_buffer_object *pbo = region->pbo;
466 GLboolean was_locked = intel->locked;
467
468 if (intel == NULL)
469 return;
470
471 intel_region_release_pbo(intel, region);
472
473 assert(region->cpp * region->pitch * region->height == pbo->Base.Size);
474
475 _DBG("%s %p (%d bytes)\n", __FUNCTION__, region, pbo->Base.Size);
476
477 /* Now blit from the texture buffer to the new buffer:
478 */
479
480 was_locked = intel->locked;
481 if (!was_locked)
482 LOCK_HARDWARE(intel);
483
484 intelEmitCopyBlit(intel,
485 region->cpp,
486 region->pitch, region->buffer, 0, region->tiling,
487 region->pitch, pbo->buffer, 0, region->tiling,
488 0, 0, 0, 0,
489 region->pitch, region->height,
490 GL_COPY);
491
492 if (!was_locked)
493 UNLOCK_HARDWARE(intel);
494 }
495
496 dri_bo *
497 intel_region_buffer(struct intel_context *intel,
498 struct intel_region *region, GLuint flag)
499 {
500 if (region->pbo) {
501 if (flag == INTEL_WRITE_PART)
502 intel_region_cow(intel, region);
503 else if (flag == INTEL_WRITE_FULL)
504 intel_region_release_pbo(intel, region);
505 }
506
507 return region->buffer;
508 }
509
510 static struct intel_region *
511 intel_recreate_static(struct intel_context *intel,
512 const char *name,
513 struct intel_region *region,
514 intelRegion *region_desc)
515 {
516 intelScreenPrivate *intelScreen = intel->intelScreen;
517 int ret;
518
519 if (region == NULL) {
520 region = calloc(sizeof(*region), 1);
521 region->refcount = 1;
522 _DBG("%s creating new region %p\n", __FUNCTION__, region);
523 }
524 else {
525 _DBG("%s %p\n", __FUNCTION__, region);
526 }
527
528 if (intel->ctx.Visual.rgbBits == 24)
529 region->cpp = 4;
530 else
531 region->cpp = intel->ctx.Visual.rgbBits / 8;
532 region->pitch = intelScreen->pitch;
533 region->width = intelScreen->width;
534 region->height = intelScreen->height;
535
536 if (region->buffer != NULL) {
537 dri_bo_unreference(region->buffer);
538 region->buffer = NULL;
539 }
540
541 if (intel->ttm) {
542 assert(region_desc->bo_handle != -1);
543 region->buffer = intel_bo_gem_create_from_name(intel->bufmgr,
544 name,
545 region_desc->bo_handle);
546
547 ret = dri_bo_get_tiling(region->buffer, &region->tiling,
548 &region->bit_6_swizzle);
549 if (ret != 0) {
550 fprintf(stderr, "Couldn't get tiling of buffer %d (%s): %s\n",
551 region_desc->bo_handle, name, strerror(-ret));
552 intel_region_release(&region);
553 return NULL;
554 }
555 } else {
556 if (region->classic_map != NULL) {
557 drmUnmap(region->classic_map,
558 region->pitch * region->cpp * region->height);
559 region->classic_map = NULL;
560 }
561 ret = drmMap(intel->driFd, region_desc->handle,
562 region->pitch * region->cpp * region->height,
563 &region->classic_map);
564 if (ret != 0) {
565 fprintf(stderr, "Failed to drmMap %s buffer\n", name);
566 free(region);
567 return NULL;
568 }
569
570 region->buffer = intel_bo_fake_alloc_static(intel->bufmgr,
571 name,
572 region_desc->offset,
573 region->pitch * region->cpp *
574 region->height,
575 region->classic_map);
576
577 /* The sarea just gives us a boolean for whether it's tiled or not,
578 * instead of which tiling mode it is. Guess.
579 */
580 if (region_desc->tiled) {
581 if (IS_965(intel->intelScreen->deviceID) &&
582 region_desc == &intelScreen->depth)
583 region->tiling = I915_TILING_Y;
584 else
585 region->tiling = I915_TILING_X;
586 } else {
587 region->tiling = I915_TILING_NONE;
588 }
589
590 region->bit_6_swizzle = I915_BIT_6_SWIZZLE_NONE;
591 }
592
593 assert(region->buffer != NULL);
594
595 return region;
596 }
597
598 /**
599 * Create intel_region structs to describe the static front, back, and depth
600 * buffers created by the xserver.
601 *
602 * Although FBO's mean we now no longer use these as render targets in
603 * all circumstances, they won't go away until the back and depth
604 * buffers become private, and the front buffer will remain even then.
605 *
606 * Note that these don't allocate video memory, just describe
607 * allocations alread made by the X server.
608 */
609 void
610 intel_recreate_static_regions(struct intel_context *intel)
611 {
612 intelScreenPrivate *intelScreen = intel->intelScreen;
613
614 intel->front_region =
615 intel_recreate_static(intel, "front",
616 intel->front_region,
617 &intelScreen->front);
618
619 intel->back_region =
620 intel_recreate_static(intel, "back",
621 intel->back_region,
622 &intelScreen->back);
623
624 /* Still assumes front.cpp == depth.cpp. We can kill this when we move to
625 * private buffers.
626 */
627 intel->depth_region =
628 intel_recreate_static(intel, "depth",
629 intel->depth_region,
630 &intelScreen->depth);
631 }