i965: formatting, comments, whitespace clean-ups
[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 /* XXX: Thread safety?
56 */
57 GLubyte *
58 intel_region_map(struct intel_context *intel, struct intel_region *region)
59 {
60 DBG("%s\n", __FUNCTION__);
61 if (!region->map_refcount++) {
62 if (region->pbo)
63 intel_region_cow(intel, region);
64
65 dri_bo_map(region->buffer, GL_TRUE);
66 region->map = region->buffer->virtual;
67 }
68
69 return region->map;
70 }
71
72 void
73 intel_region_unmap(struct intel_context *intel, struct intel_region *region)
74 {
75 DBG("%s\n", __FUNCTION__);
76 if (!--region->map_refcount) {
77 dri_bo_unmap(region->buffer);
78 region->map = NULL;
79 }
80 }
81
82 static struct intel_region *
83 intel_region_alloc_internal(struct intel_context *intel,
84 GLuint cpp,
85 GLuint width, GLuint height, GLuint pitch,
86 dri_bo *buffer)
87 {
88 struct intel_region *region;
89
90 DBG("%s\n", __FUNCTION__);
91
92 if (buffer == NULL)
93 return NULL;
94
95 region = calloc(sizeof(*region), 1);
96 region->cpp = cpp;
97 region->width = width;
98 region->height = height;
99 region->pitch = pitch;
100 region->refcount = 1;
101 region->buffer = buffer;
102
103 /* Default to no tiling */
104 region->tiling = I915_TILING_NONE;
105 region->bit_6_swizzle = I915_BIT_6_SWIZZLE_NONE;
106
107 return region;
108 }
109
110 struct intel_region *
111 intel_region_alloc(struct intel_context *intel,
112 GLuint cpp, GLuint width, GLuint height, GLuint pitch)
113 {
114 dri_bo *buffer;
115
116 buffer = dri_bo_alloc(intel->bufmgr, "region",
117 pitch * cpp * height, 64);
118
119 return intel_region_alloc_internal(intel, cpp, width, height, pitch, buffer);
120 }
121
122 struct intel_region *
123 intel_region_alloc_for_handle(struct intel_context *intel,
124 GLuint cpp,
125 GLuint width, GLuint height, GLuint pitch,
126 GLuint handle, const char *name)
127 {
128 struct intel_region *region;
129 dri_bo *buffer;
130 int ret;
131
132 buffer = intel_bo_gem_create_from_name(intel->bufmgr, name, handle);
133
134 region = intel_region_alloc_internal(intel, cpp,
135 width, height, pitch, buffer);
136 if (region == NULL)
137 return region;
138
139 ret = dri_bo_get_tiling(region->buffer, &region->tiling,
140 &region->bit_6_swizzle);
141 if (ret != 0) {
142 fprintf(stderr, "Couldn't get tiling of buffer %d (%s): %s\n",
143 handle, name, strerror(-ret));
144 intel_region_release(&region);
145 return NULL;
146 }
147
148 return region;
149 }
150
151 void
152 intel_region_reference(struct intel_region **dst, struct intel_region *src)
153 {
154 if (src)
155 DBG("%s %d\n", __FUNCTION__, src->refcount);
156
157 assert(*dst == NULL);
158 if (src) {
159 src->refcount++;
160 *dst = src;
161 }
162 }
163
164 void
165 intel_region_release(struct intel_region **region_handle)
166 {
167 struct intel_region *region = *region_handle;
168
169 if (region == NULL)
170 return;
171
172 DBG("%s %d\n", __FUNCTION__, region->refcount - 1);
173
174 ASSERT(region->refcount > 0);
175 region->refcount--;
176
177 if (region->refcount == 0) {
178 assert(region->map_refcount == 0);
179
180 if (region->pbo)
181 region->pbo->region = NULL;
182 region->pbo = NULL;
183 dri_bo_unreference(region->buffer);
184
185 if (region->classic_map != NULL) {
186 drmUnmap(region->classic_map,
187 region->pitch * region->cpp * region->height);
188 }
189
190 free(region);
191 }
192 *region_handle = NULL;
193 }
194
195 /*
196 * XXX Move this into core Mesa?
197 */
198 void
199 _mesa_copy_rect(GLubyte * dst,
200 GLuint cpp,
201 GLuint dst_pitch,
202 GLuint dst_x,
203 GLuint dst_y,
204 GLuint width,
205 GLuint height,
206 const GLubyte * src,
207 GLuint src_pitch, GLuint src_x, GLuint src_y)
208 {
209 GLuint i;
210
211 dst_pitch *= cpp;
212 src_pitch *= cpp;
213 dst += dst_x * cpp;
214 src += src_x * cpp;
215 dst += dst_y * dst_pitch;
216 src += src_y * dst_pitch;
217 width *= cpp;
218
219 if (width == dst_pitch && width == src_pitch)
220 memcpy(dst, src, height * width);
221 else {
222 for (i = 0; i < height; i++) {
223 memcpy(dst, src, width);
224 dst += dst_pitch;
225 src += src_pitch;
226 }
227 }
228 }
229
230
231 /* Upload data to a rectangular sub-region. Lots of choices how to do this:
232 *
233 * - memcpy by span to current destination
234 * - upload data as new buffer and blit
235 *
236 * Currently always memcpy.
237 */
238 void
239 intel_region_data(struct intel_context *intel,
240 struct intel_region *dst,
241 GLuint dst_offset,
242 GLuint dstx, GLuint dsty,
243 const void *src, GLuint src_pitch,
244 GLuint srcx, GLuint srcy, GLuint width, GLuint height)
245 {
246 GLboolean locked = GL_FALSE;
247
248 DBG("%s\n", __FUNCTION__);
249
250 if (intel == NULL)
251 return;
252
253 if (dst->pbo) {
254 if (dstx == 0 &&
255 dsty == 0 && width == dst->pitch && height == dst->height)
256 intel_region_release_pbo(intel, dst);
257 else
258 intel_region_cow(intel, dst);
259 }
260
261 if (!intel->locked) {
262 LOCK_HARDWARE(intel);
263 locked = GL_TRUE;
264 }
265
266 _mesa_copy_rect(intel_region_map(intel, dst) + dst_offset,
267 dst->cpp,
268 dst->pitch,
269 dstx, dsty, width, height, src, src_pitch, srcx, srcy);
270
271 intel_region_unmap(intel, dst);
272
273 if (locked)
274 UNLOCK_HARDWARE(intel);
275
276 }
277
278 /* Copy rectangular sub-regions. Need better logic about when to
279 * push buffers into AGP - will currently do so whenever possible.
280 */
281 void
282 intel_region_copy(struct intel_context *intel,
283 struct intel_region *dst,
284 GLuint dst_offset,
285 GLuint dstx, GLuint dsty,
286 struct intel_region *src,
287 GLuint src_offset,
288 GLuint srcx, GLuint srcy, GLuint width, GLuint height)
289 {
290 DBG("%s\n", __FUNCTION__);
291
292 if (intel == NULL)
293 return;
294
295 if (dst->pbo) {
296 if (dstx == 0 &&
297 dsty == 0 && width == dst->pitch && height == dst->height)
298 intel_region_release_pbo(intel, dst);
299 else
300 intel_region_cow(intel, dst);
301 }
302
303 assert(src->cpp == dst->cpp);
304
305 intelEmitCopyBlit(intel,
306 dst->cpp,
307 src->pitch, src->buffer, src_offset, src->tiling,
308 dst->pitch, dst->buffer, dst_offset, dst->tiling,
309 srcx, srcy, dstx, dsty, width, height,
310 GL_COPY);
311 }
312
313 /* Fill a rectangular sub-region. Need better logic about when to
314 * push buffers into AGP - will currently do so whenever possible.
315 */
316 void
317 intel_region_fill(struct intel_context *intel,
318 struct intel_region *dst,
319 GLuint dst_offset,
320 GLuint dstx, GLuint dsty,
321 GLuint width, GLuint height, GLuint color)
322 {
323 DBG("%s\n", __FUNCTION__);
324
325 if (intel == NULL)
326 return;
327
328 if (dst->pbo) {
329 if (dstx == 0 &&
330 dsty == 0 && width == dst->pitch && height == dst->height)
331 intel_region_release_pbo(intel, dst);
332 else
333 intel_region_cow(intel, dst);
334 }
335
336 intelEmitFillBlit(intel,
337 dst->cpp,
338 dst->pitch, dst->buffer, dst_offset, dst->tiling,
339 dstx, dsty, width, height, color);
340 }
341
342 /* Attach to a pbo, discarding our data. Effectively zero-copy upload
343 * the pbo's data.
344 */
345 void
346 intel_region_attach_pbo(struct intel_context *intel,
347 struct intel_region *region,
348 struct intel_buffer_object *pbo)
349 {
350 if (region->pbo == pbo)
351 return;
352
353 /* If there is already a pbo attached, break the cow tie now.
354 * Don't call intel_region_release_pbo() as that would
355 * unnecessarily allocate a new buffer we would have to immediately
356 * discard.
357 */
358 if (region->pbo) {
359 region->pbo->region = NULL;
360 region->pbo = NULL;
361 }
362
363 if (region->buffer) {
364 dri_bo_unreference(region->buffer);
365 region->buffer = NULL;
366 }
367
368 region->pbo = pbo;
369 region->pbo->region = region;
370 dri_bo_reference(pbo->buffer);
371 region->buffer = pbo->buffer;
372 }
373
374
375 /* Break the COW tie to the pbo and allocate a new buffer.
376 * The pbo gets to keep the data.
377 */
378 void
379 intel_region_release_pbo(struct intel_context *intel,
380 struct intel_region *region)
381 {
382 assert(region->buffer == region->pbo->buffer);
383 region->pbo->region = NULL;
384 region->pbo = NULL;
385 dri_bo_unreference(region->buffer);
386 region->buffer = NULL;
387
388 region->buffer = dri_bo_alloc(intel->bufmgr, "region",
389 region->pitch * region->cpp * region->height,
390 64);
391 }
392
393 /* Break the COW tie to the pbo. Both the pbo and the region end up
394 * with a copy of the data.
395 */
396 void
397 intel_region_cow(struct intel_context *intel, struct intel_region *region)
398 {
399 struct intel_buffer_object *pbo = region->pbo;
400 GLboolean was_locked = intel->locked;
401
402 if (intel == NULL)
403 return;
404
405 intel_region_release_pbo(intel, region);
406
407 assert(region->cpp * region->pitch * region->height == pbo->Base.Size);
408
409 DBG("%s (%d bytes)\n", __FUNCTION__, pbo->Base.Size);
410
411 /* Now blit from the texture buffer to the new buffer:
412 */
413
414 was_locked = intel->locked;
415 if (!was_locked)
416 LOCK_HARDWARE(intel);
417
418 intelEmitCopyBlit(intel,
419 region->cpp,
420 region->pitch, region->buffer, 0, region->tiling,
421 region->pitch, pbo->buffer, 0, region->tiling,
422 0, 0, 0, 0,
423 region->pitch, region->height,
424 GL_COPY);
425
426 if (!was_locked)
427 UNLOCK_HARDWARE(intel);
428 }
429
430 dri_bo *
431 intel_region_buffer(struct intel_context *intel,
432 struct intel_region *region, GLuint flag)
433 {
434 if (region->pbo) {
435 if (flag == INTEL_WRITE_PART)
436 intel_region_cow(intel, region);
437 else if (flag == INTEL_WRITE_FULL)
438 intel_region_release_pbo(intel, region);
439 }
440
441 return region->buffer;
442 }
443
444 static struct intel_region *
445 intel_recreate_static(struct intel_context *intel,
446 const char *name,
447 struct intel_region *region,
448 intelRegion *region_desc)
449 {
450 intelScreenPrivate *intelScreen = intel->intelScreen;
451 int ret;
452
453 if (region == NULL) {
454 region = calloc(sizeof(*region), 1);
455 region->refcount = 1;
456 }
457
458 if (intel->ctx.Visual.rgbBits == 24)
459 region->cpp = 4;
460 else
461 region->cpp = intel->ctx.Visual.rgbBits / 8;
462 region->pitch = intelScreen->pitch;
463 region->height = intelScreen->height; /* needed? */
464
465 if (region->buffer != NULL) {
466 dri_bo_unreference(region->buffer);
467 region->buffer = NULL;
468 }
469
470 if (intel->ttm) {
471 assert(region_desc->bo_handle != -1);
472 region->buffer = intel_bo_gem_create_from_name(intel->bufmgr,
473 name,
474 region_desc->bo_handle);
475
476 ret = dri_bo_get_tiling(region->buffer, &region->tiling,
477 &region->bit_6_swizzle);
478 if (ret != 0) {
479 fprintf(stderr, "Couldn't get tiling of buffer %d (%s): %s\n",
480 region_desc->bo_handle, name, strerror(-ret));
481 intel_region_release(&region);
482 return NULL;
483 }
484 } else {
485 if (region->classic_map != NULL) {
486 drmUnmap(region->classic_map,
487 region->pitch * region->cpp * region->height);
488 region->classic_map = NULL;
489 }
490 ret = drmMap(intel->driFd, region_desc->handle,
491 region->pitch * region->cpp * region->height,
492 &region->classic_map);
493 if (ret != 0) {
494 fprintf(stderr, "Failed to drmMap %s buffer\n", name);
495 free(region);
496 return NULL;
497 }
498
499 region->buffer = intel_bo_fake_alloc_static(intel->bufmgr,
500 name,
501 region_desc->offset,
502 region->pitch * region->cpp *
503 region->height,
504 region->classic_map);
505
506 /* The sarea just gives us a boolean for whether it's tiled or not,
507 * instead of which tiling mode it is. Guess.
508 */
509 if (region_desc->tiled) {
510 if (IS_965(intel->intelScreen->deviceID) &&
511 region_desc == &intelScreen->depth)
512 region->tiling = I915_TILING_Y;
513 else
514 region->tiling = I915_TILING_X;
515 } else {
516 region->tiling = I915_TILING_NONE;
517 }
518
519 region->bit_6_swizzle = I915_BIT_6_SWIZZLE_NONE;
520 }
521
522 assert(region->buffer != NULL);
523
524 return region;
525 }
526
527 /**
528 * Create intel_region structs to describe the static front, back, and depth
529 * buffers created by the xserver.
530 *
531 * Although FBO's mean we now no longer use these as render targets in
532 * all circumstances, they won't go away until the back and depth
533 * buffers become private, and the front buffer will remain even then.
534 *
535 * Note that these don't allocate video memory, just describe
536 * allocations alread made by the X server.
537 */
538 void
539 intel_recreate_static_regions(struct intel_context *intel)
540 {
541 intelScreenPrivate *intelScreen = intel->intelScreen;
542
543 intel->front_region =
544 intel_recreate_static(intel, "front",
545 intel->front_region,
546 &intelScreen->front);
547
548 intel->back_region =
549 intel_recreate_static(intel, "back",
550 intel->back_region,
551 &intelScreen->back);
552
553 /* Still assumes front.cpp == depth.cpp. We can kill this when we move to
554 * private buffers.
555 */
556 intel->depth_region =
557 intel_recreate_static(intel, "depth",
558 intel->depth_region,
559 &intelScreen->depth);
560 }