Merge remote branch 'origin/master' into nv50-compiler
[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 "main/hash.h"
46 #include "intel_context.h"
47 #include "intel_regions.h"
48 #include "intel_blit.h"
49 #include "intel_buffer_objects.h"
50 #include "intel_bufmgr.h"
51 #include "intel_batchbuffer.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 intel_flush(&intel->ctx);
115
116 _DBG("%s %p\n", __FUNCTION__, region);
117 if (!region->map_refcount++) {
118 if (region->pbo)
119 intel_region_cow(intel, region);
120
121 if (region->tiling != I915_TILING_NONE)
122 drm_intel_gem_bo_map_gtt(region->buffer);
123 else
124 drm_intel_bo_map(region->buffer, GL_TRUE);
125 region->map = region->buffer->virtual;
126 }
127
128 return region->map;
129 }
130
131 void
132 intel_region_unmap(struct intel_context *intel, struct intel_region *region)
133 {
134 _DBG("%s %p\n", __FUNCTION__, region);
135 if (!--region->map_refcount) {
136 if (region->tiling != I915_TILING_NONE)
137 drm_intel_gem_bo_unmap_gtt(region->buffer);
138 else
139 drm_intel_bo_unmap(region->buffer);
140 region->map = NULL;
141 }
142 }
143
144 static struct intel_region *
145 intel_region_alloc_internal(struct intel_screen *screen,
146 GLuint cpp,
147 GLuint width, GLuint height, GLuint pitch,
148 uint32_t tiling, drm_intel_bo *buffer)
149 {
150 struct intel_region *region;
151
152 if (buffer == NULL) {
153 _DBG("%s <-- NULL\n", __FUNCTION__);
154 return NULL;
155 }
156
157 region = calloc(sizeof(*region), 1);
158 if (region == NULL)
159 return region;
160
161 region->cpp = cpp;
162 region->width = width;
163 region->height = height;
164 region->pitch = pitch;
165 region->refcount = 1;
166 region->buffer = buffer;
167 region->tiling = tiling;
168 region->screen = screen;
169
170 _DBG("%s <-- %p\n", __FUNCTION__, region);
171 return region;
172 }
173
174 struct intel_region *
175 intel_region_alloc(struct intel_screen *screen,
176 uint32_t tiling,
177 GLuint cpp, GLuint width, GLuint height,
178 GLboolean expect_accelerated_upload)
179 {
180 drm_intel_bo *buffer;
181 unsigned long flags = 0;
182 unsigned long aligned_pitch;
183
184 if (expect_accelerated_upload)
185 flags |= BO_ALLOC_FOR_RENDER;
186
187 buffer = drm_intel_bo_alloc_tiled(screen->bufmgr, "region",
188 width, height, cpp,
189 &tiling, &aligned_pitch, flags);
190
191 return intel_region_alloc_internal(screen, cpp, width, height,
192 aligned_pitch / cpp, tiling, buffer);
193 }
194
195 GLboolean
196 intel_region_flink(struct intel_region *region, uint32_t *name)
197 {
198 if (region->name == 0) {
199 if (drm_intel_bo_flink(region->buffer, &region->name))
200 return GL_FALSE;
201
202 _mesa_HashInsert(region->screen->named_regions,
203 region->name, region);
204 }
205
206 *name = region->name;
207
208 return GL_TRUE;
209 }
210
211 struct intel_region *
212 intel_region_alloc_for_handle(struct intel_screen *screen,
213 GLuint cpp,
214 GLuint width, GLuint height, GLuint pitch,
215 GLuint handle, const char *name)
216 {
217 struct intel_region *region, *dummy;
218 drm_intel_bo *buffer;
219 int ret;
220 uint32_t bit_6_swizzle, tiling;
221
222 region = _mesa_HashLookup(screen->named_regions, handle);
223 if (region != NULL) {
224 dummy = NULL;
225 if (region->width != width || region->height != height ||
226 region->cpp != cpp || region->pitch != pitch) {
227 fprintf(stderr,
228 "Region for name %d already exists but is not compatible\n",
229 handle);
230 return NULL;
231 }
232 intel_region_reference(&dummy, region);
233 return dummy;
234 }
235
236 buffer = intel_bo_gem_create_from_name(screen->bufmgr, name, handle);
237 if (buffer == NULL)
238 return NULL;
239 ret = drm_intel_bo_get_tiling(buffer, &tiling, &bit_6_swizzle);
240 if (ret != 0) {
241 fprintf(stderr, "Couldn't get tiling of buffer %d (%s): %s\n",
242 handle, name, strerror(-ret));
243 drm_intel_bo_unreference(buffer);
244 return NULL;
245 }
246
247 region = intel_region_alloc_internal(screen, cpp,
248 width, height, pitch, tiling, buffer);
249 if (region == NULL) {
250 drm_intel_bo_unreference(buffer);
251 return NULL;
252 }
253
254 region->name = handle;
255 _mesa_HashInsert(screen->named_regions, handle, region);
256
257 return region;
258 }
259
260 void
261 intel_region_reference(struct intel_region **dst, struct intel_region *src)
262 {
263 if (src)
264 _DBG("%s %p %d\n", __FUNCTION__, src, src->refcount);
265
266 assert(*dst == NULL);
267 if (src) {
268 src->refcount++;
269 *dst = src;
270 }
271 }
272
273 void
274 intel_region_release(struct intel_region **region_handle)
275 {
276 struct intel_region *region = *region_handle;
277
278 if (region == NULL) {
279 _DBG("%s NULL\n", __FUNCTION__);
280 return;
281 }
282
283 _DBG("%s %p %d\n", __FUNCTION__, region, region->refcount - 1);
284
285 ASSERT(region->refcount > 0);
286 region->refcount--;
287
288 if (region->refcount == 0) {
289 assert(region->map_refcount == 0);
290
291 if (region->pbo)
292 region->pbo->region = NULL;
293 region->pbo = NULL;
294 drm_intel_bo_unreference(region->buffer);
295
296 if (region->name > 0)
297 _mesa_HashRemove(region->screen->named_regions, region->name);
298
299 free(region);
300 }
301 *region_handle = NULL;
302 }
303
304 /*
305 * XXX Move this into core Mesa?
306 */
307 void
308 _mesa_copy_rect(GLubyte * dst,
309 GLuint cpp,
310 GLuint dst_pitch,
311 GLuint dst_x,
312 GLuint dst_y,
313 GLuint width,
314 GLuint height,
315 const GLubyte * src,
316 GLuint src_pitch, GLuint src_x, GLuint src_y)
317 {
318 GLuint i;
319
320 dst_pitch *= cpp;
321 src_pitch *= cpp;
322 dst += dst_x * cpp;
323 src += src_x * cpp;
324 dst += dst_y * dst_pitch;
325 src += src_y * src_pitch;
326 width *= cpp;
327
328 if (width == dst_pitch && width == src_pitch)
329 memcpy(dst, src, height * width);
330 else {
331 for (i = 0; i < height; i++) {
332 memcpy(dst, src, width);
333 dst += dst_pitch;
334 src += src_pitch;
335 }
336 }
337 }
338
339
340 /* Upload data to a rectangular sub-region. Lots of choices how to do this:
341 *
342 * - memcpy by span to current destination
343 * - upload data as new buffer and blit
344 *
345 * Currently always memcpy.
346 */
347 void
348 intel_region_data(struct intel_context *intel,
349 struct intel_region *dst,
350 GLuint dst_offset,
351 GLuint dstx, GLuint dsty,
352 const void *src, GLuint src_pitch,
353 GLuint srcx, GLuint srcy, GLuint width, GLuint height)
354 {
355 _DBG("%s\n", __FUNCTION__);
356
357 if (intel == NULL)
358 return;
359
360 if (dst->pbo) {
361 if (dstx == 0 &&
362 dsty == 0 && width == dst->pitch && height == dst->height)
363 intel_region_release_pbo(intel, dst);
364 else
365 intel_region_cow(intel, dst);
366 }
367
368 intel_prepare_render(intel);
369
370 _mesa_copy_rect(intel_region_map(intel, dst) + dst_offset,
371 dst->cpp,
372 dst->pitch,
373 dstx, dsty, width, height, src, src_pitch, srcx, srcy);
374
375 intel_region_unmap(intel, dst);
376 }
377
378 /* Copy rectangular sub-regions. Need better logic about when to
379 * push buffers into AGP - will currently do so whenever possible.
380 */
381 GLboolean
382 intel_region_copy(struct intel_context *intel,
383 struct intel_region *dst,
384 GLuint dst_offset,
385 GLuint dstx, GLuint dsty,
386 struct intel_region *src,
387 GLuint src_offset,
388 GLuint srcx, GLuint srcy, GLuint width, GLuint height,
389 GLboolean flip,
390 GLenum logicop)
391 {
392 uint32_t src_pitch = src->pitch;
393
394 _DBG("%s\n", __FUNCTION__);
395
396 if (intel == NULL)
397 return GL_FALSE;
398
399 if (dst->pbo) {
400 if (dstx == 0 &&
401 dsty == 0 && width == dst->pitch && height == dst->height)
402 intel_region_release_pbo(intel, dst);
403 else
404 intel_region_cow(intel, dst);
405 }
406
407 assert(src->cpp == dst->cpp);
408
409 if (flip)
410 src_pitch = -src_pitch;
411
412 return intelEmitCopyBlit(intel,
413 dst->cpp,
414 src_pitch, src->buffer, src_offset, src->tiling,
415 dst->pitch, dst->buffer, dst_offset, dst->tiling,
416 srcx, srcy, dstx, dsty, width, height,
417 logicop);
418 }
419
420 /* Attach to a pbo, discarding our data. Effectively zero-copy upload
421 * the pbo's data.
422 */
423 void
424 intel_region_attach_pbo(struct intel_context *intel,
425 struct intel_region *region,
426 struct intel_buffer_object *pbo)
427 {
428 drm_intel_bo *buffer;
429
430 if (region->pbo == pbo)
431 return;
432
433 _DBG("%s %p %p\n", __FUNCTION__, region, pbo);
434
435 /* If there is already a pbo attached, break the cow tie now.
436 * Don't call intel_region_release_pbo() as that would
437 * unnecessarily allocate a new buffer we would have to immediately
438 * discard.
439 */
440 if (region->pbo) {
441 region->pbo->region = NULL;
442 region->pbo = NULL;
443 }
444
445 if (region->buffer) {
446 drm_intel_bo_unreference(region->buffer);
447 region->buffer = NULL;
448 }
449
450 /* make sure pbo has a buffer of its own */
451 buffer = intel_bufferobj_buffer(intel, pbo, INTEL_WRITE_FULL);
452
453 region->pbo = pbo;
454 region->pbo->region = region;
455 drm_intel_bo_reference(buffer);
456 region->buffer = buffer;
457 region->tiling = I915_TILING_NONE;
458 }
459
460
461 /* Break the COW tie to the pbo and allocate a new buffer.
462 * The pbo gets to keep the data.
463 */
464 void
465 intel_region_release_pbo(struct intel_context *intel,
466 struct intel_region *region)
467 {
468 _DBG("%s %p\n", __FUNCTION__, region);
469 assert(region->buffer == region->pbo->buffer);
470 region->pbo->region = NULL;
471 region->pbo = NULL;
472 drm_intel_bo_unreference(region->buffer);
473 region->buffer = NULL;
474
475 region->buffer = drm_intel_bo_alloc(intel->bufmgr, "region",
476 region->pitch * region->cpp *
477 region->height,
478 64);
479 }
480
481 /* Break the COW tie to the pbo. Both the pbo and the region end up
482 * with a copy of the data.
483 */
484 void
485 intel_region_cow(struct intel_context *intel, struct intel_region *region)
486 {
487 struct intel_buffer_object *pbo = region->pbo;
488 GLboolean ok;
489
490 intel_region_release_pbo(intel, region);
491
492 assert(region->cpp * region->pitch * region->height == pbo->Base.Size);
493
494 _DBG("%s %p (%d bytes)\n", __FUNCTION__, region, pbo->Base.Size);
495
496 /* Now blit from the texture buffer to the new buffer:
497 */
498
499 intel_prepare_render(intel);
500 ok = intelEmitCopyBlit(intel,
501 region->cpp,
502 region->pitch, pbo->buffer, 0, region->tiling,
503 region->pitch, region->buffer, 0, region->tiling,
504 0, 0, 0, 0,
505 region->pitch, region->height,
506 GL_COPY);
507 assert(ok);
508 }
509
510 drm_intel_bo *
511 intel_region_buffer(struct intel_context *intel,
512 struct intel_region *region, GLuint flag)
513 {
514 if (region->pbo) {
515 if (flag == INTEL_WRITE_PART)
516 intel_region_cow(intel, region);
517 else if (flag == INTEL_WRITE_FULL)
518 intel_region_release_pbo(intel, region);
519 }
520
521 return region->buffer;
522 }