1 /**************************************************************************
3 * Copyright 2006 Tungsten Graphics, Inc., Cedar Park, Texas.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
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
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
42 #include <sys/ioctl.h>
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"
53 #define FILE_DEBUG_FLAG DEBUG_REGION
55 /* XXX: Thread safety?
58 intel_region_map(struct intel_context
*intel
, struct intel_region
*region
)
60 DBG("%s\n", __FUNCTION__
);
61 if (!region
->map_refcount
++) {
63 intel_region_cow(intel
, region
);
65 dri_bo_map(region
->buffer
, GL_TRUE
);
66 region
->map
= region
->buffer
->virtual;
73 intel_region_unmap(struct intel_context
*intel
, struct intel_region
*region
)
75 DBG("%s\n", __FUNCTION__
);
76 if (!--region
->map_refcount
) {
77 dri_bo_unmap(region
->buffer
);
83 intel_set_region_tiling_gem(struct intel_context
*intel
,
84 struct intel_region
*region
,
87 struct drm_i915_gem_get_tiling get_tiling
;
90 memset(&get_tiling
, 0, sizeof(get_tiling
));
92 get_tiling
.handle
= bo_handle
;
93 ret
= ioctl(intel
->driFd
, DRM_IOCTL_I915_GEM_GET_TILING
, &get_tiling
);
95 fprintf(stderr
, "Failed to get tiling state for region: %s\n",
100 region
->tiling
= get_tiling
.tiling_mode
;
101 region
->bit_6_swizzle
= get_tiling
.swizzle_mode
;
106 static struct intel_region
*
107 intel_region_alloc_internal(struct intel_context
*intel
,
109 GLuint width
, GLuint height
, GLuint pitch
,
112 struct intel_region
*region
;
114 DBG("%s\n", __FUNCTION__
);
119 region
= calloc(sizeof(*region
), 1);
121 region
->width
= width
;
122 region
->height
= height
;
123 region
->pitch
= pitch
;
124 region
->refcount
= 1;
125 region
->buffer
= buffer
;
127 /* Default to no tiling */
128 region
->tiling
= I915_TILING_NONE
;
129 region
->bit_6_swizzle
= I915_BIT_6_SWIZZLE_NONE
;
134 struct intel_region
*
135 intel_region_alloc(struct intel_context
*intel
,
136 GLuint cpp
, GLuint width
, GLuint height
, GLuint pitch
)
140 buffer
= dri_bo_alloc(intel
->bufmgr
, "region",
141 pitch
* cpp
* height
, 64);
143 return intel_region_alloc_internal(intel
, cpp
, width
, height
, pitch
, buffer
);
146 struct intel_region
*
147 intel_region_alloc_for_handle(struct intel_context
*intel
,
149 GLuint width
, GLuint height
, GLuint pitch
,
150 GLuint handle
, const char *name
)
152 struct intel_region
*region
;
155 buffer
= intel_bo_gem_create_from_name(intel
->bufmgr
, name
, handle
);
157 region
= intel_region_alloc_internal(intel
, cpp
,
158 width
, height
, pitch
, buffer
);
162 intel_set_region_tiling_gem(intel
, region
, handle
);
168 intel_region_reference(struct intel_region
**dst
, struct intel_region
*src
)
171 DBG("%s %d\n", __FUNCTION__
, src
->refcount
);
173 assert(*dst
== NULL
);
181 intel_region_release(struct intel_region
**region_handle
)
183 struct intel_region
*region
= *region_handle
;
188 DBG("%s %d\n", __FUNCTION__
, region
->refcount
- 1);
190 ASSERT(region
->refcount
> 0);
193 if (region
->refcount
== 0) {
194 assert(region
->map_refcount
== 0);
197 region
->pbo
->region
= NULL
;
199 dri_bo_unreference(region
->buffer
);
201 if (region
->classic_map
!= NULL
) {
202 drmUnmap(region
->classic_map
,
203 region
->pitch
* region
->cpp
* region
->height
);
208 *region_handle
= NULL
;
212 * XXX Move this into core Mesa?
215 _mesa_copy_rect(GLubyte
* dst
,
223 GLuint src_pitch
, GLuint src_x
, GLuint src_y
)
231 dst
+= dst_y
* dst_pitch
;
232 src
+= src_y
* dst_pitch
;
235 if (width
== dst_pitch
&& width
== src_pitch
)
236 memcpy(dst
, src
, height
* width
);
238 for (i
= 0; i
< height
; i
++) {
239 memcpy(dst
, src
, width
);
247 /* Upload data to a rectangular sub-region. Lots of choices how to do this:
249 * - memcpy by span to current destination
250 * - upload data as new buffer and blit
252 * Currently always memcpy.
255 intel_region_data(struct intel_context
*intel
,
256 struct intel_region
*dst
,
258 GLuint dstx
, GLuint dsty
,
259 const void *src
, GLuint src_pitch
,
260 GLuint srcx
, GLuint srcy
, GLuint width
, GLuint height
)
262 GLboolean locked
= GL_FALSE
;
264 DBG("%s\n", __FUNCTION__
);
271 dsty
== 0 && width
== dst
->pitch
&& height
== dst
->height
)
272 intel_region_release_pbo(intel
, dst
);
274 intel_region_cow(intel
, dst
);
277 if (!intel
->locked
) {
278 LOCK_HARDWARE(intel
);
282 _mesa_copy_rect(intel_region_map(intel
, dst
) + dst_offset
,
285 dstx
, dsty
, width
, height
, src
, src_pitch
, srcx
, srcy
);
287 intel_region_unmap(intel
, dst
);
290 UNLOCK_HARDWARE(intel
);
294 /* Copy rectangular sub-regions. Need better logic about when to
295 * push buffers into AGP - will currently do so whenever possible.
298 intel_region_copy(struct intel_context
*intel
,
299 struct intel_region
*dst
,
301 GLuint dstx
, GLuint dsty
,
302 struct intel_region
*src
,
304 GLuint srcx
, GLuint srcy
, GLuint width
, GLuint height
)
306 DBG("%s\n", __FUNCTION__
);
313 dsty
== 0 && width
== dst
->pitch
&& height
== dst
->height
)
314 intel_region_release_pbo(intel
, dst
);
316 intel_region_cow(intel
, dst
);
319 assert(src
->cpp
== dst
->cpp
);
321 intelEmitCopyBlit(intel
,
323 src
->pitch
, src
->buffer
, src_offset
, src
->tiling
,
324 dst
->pitch
, dst
->buffer
, dst_offset
, dst
->tiling
,
325 srcx
, srcy
, dstx
, dsty
, width
, height
,
329 /* Fill a rectangular sub-region. Need better logic about when to
330 * push buffers into AGP - will currently do so whenever possible.
333 intel_region_fill(struct intel_context
*intel
,
334 struct intel_region
*dst
,
336 GLuint dstx
, GLuint dsty
,
337 GLuint width
, GLuint height
, GLuint color
)
339 DBG("%s\n", __FUNCTION__
);
346 dsty
== 0 && width
== dst
->pitch
&& height
== dst
->height
)
347 intel_region_release_pbo(intel
, dst
);
349 intel_region_cow(intel
, dst
);
352 intelEmitFillBlit(intel
,
354 dst
->pitch
, dst
->buffer
, dst_offset
, dst
->tiling
,
355 dstx
, dsty
, width
, height
, color
);
358 /* Attach to a pbo, discarding our data. Effectively zero-copy upload
362 intel_region_attach_pbo(struct intel_context
*intel
,
363 struct intel_region
*region
,
364 struct intel_buffer_object
*pbo
)
366 if (region
->pbo
== pbo
)
369 /* If there is already a pbo attached, break the cow tie now.
370 * Don't call intel_region_release_pbo() as that would
371 * unnecessarily allocate a new buffer we would have to immediately
375 region
->pbo
->region
= NULL
;
379 if (region
->buffer
) {
380 dri_bo_unreference(region
->buffer
);
381 region
->buffer
= NULL
;
385 region
->pbo
->region
= region
;
386 dri_bo_reference(pbo
->buffer
);
387 region
->buffer
= pbo
->buffer
;
391 /* Break the COW tie to the pbo and allocate a new buffer.
392 * The pbo gets to keep the data.
395 intel_region_release_pbo(struct intel_context
*intel
,
396 struct intel_region
*region
)
398 assert(region
->buffer
== region
->pbo
->buffer
);
399 region
->pbo
->region
= NULL
;
401 dri_bo_unreference(region
->buffer
);
402 region
->buffer
= NULL
;
404 region
->buffer
= dri_bo_alloc(intel
->bufmgr
, "region",
405 region
->pitch
* region
->cpp
* region
->height
,
409 /* Break the COW tie to the pbo. Both the pbo and the region end up
410 * with a copy of the data.
413 intel_region_cow(struct intel_context
*intel
, struct intel_region
*region
)
415 struct intel_buffer_object
*pbo
= region
->pbo
;
416 GLboolean was_locked
= intel
->locked
;
421 intel_region_release_pbo(intel
, region
);
423 assert(region
->cpp
* region
->pitch
* region
->height
== pbo
->Base
.Size
);
425 DBG("%s (%d bytes)\n", __FUNCTION__
, pbo
->Base
.Size
);
427 /* Now blit from the texture buffer to the new buffer:
430 was_locked
= intel
->locked
;
432 LOCK_HARDWARE(intel
);
434 intelEmitCopyBlit(intel
,
436 region
->pitch
, region
->buffer
, 0, region
->tiling
,
437 region
->pitch
, pbo
->buffer
, 0, region
->tiling
,
439 region
->pitch
, region
->height
,
443 UNLOCK_HARDWARE(intel
);
447 intel_region_buffer(struct intel_context
*intel
,
448 struct intel_region
*region
, GLuint flag
)
451 if (flag
== INTEL_WRITE_PART
)
452 intel_region_cow(intel
, region
);
453 else if (flag
== INTEL_WRITE_FULL
)
454 intel_region_release_pbo(intel
, region
);
457 return region
->buffer
;
460 static struct intel_region
*
461 intel_recreate_static(struct intel_context
*intel
,
463 struct intel_region
*region
,
464 intelRegion
*region_desc
)
466 intelScreenPrivate
*intelScreen
= intel
->intelScreen
;
469 if (region
== NULL
) {
470 region
= calloc(sizeof(*region
), 1);
471 region
->refcount
= 1;
474 if (intel
->ctx
.Visual
.rgbBits
== 24)
477 region
->cpp
= intel
->ctx
.Visual
.rgbBits
/ 8;
478 region
->pitch
= intelScreen
->pitch
;
479 region
->height
= intelScreen
->height
; /* needed? */
482 assert(region_desc
->bo_handle
!= -1);
483 region
->buffer
= intel_bo_gem_create_from_name(intel
->bufmgr
,
485 region_desc
->bo_handle
);
487 intel_set_region_tiling_gem(intel
, region
, region_desc
->bo_handle
);
489 ret
= drmMap(intel
->driFd
, region_desc
->handle
,
490 region
->pitch
* region
->cpp
* region
->height
,
491 ®ion
->classic_map
);
493 fprintf(stderr
, "Failed to drmMap %s buffer\n", name
);
498 region
->buffer
= intel_bo_fake_alloc_static(intel
->bufmgr
,
501 region
->pitch
* region
->cpp
*
503 region
->classic_map
);
505 /* The sarea just gives us a boolean for whether it's tiled or not,
506 * instead of which tiling mode it is. Guess.
508 if (region_desc
->tiled
) {
509 if (IS_965(intel
->intelScreen
->deviceID
) &&
510 region_desc
== &intelScreen
->depth
)
511 region
->tiling
= I915_TILING_Y
;
513 region
->tiling
= I915_TILING_X
;
515 region
->tiling
= I915_TILING_NONE
;
518 region
->bit_6_swizzle
= I915_BIT_6_SWIZZLE_NONE
;
521 assert(region
->buffer
!= NULL
);
527 * Create intel_region structs to describe the static front, back, and depth
528 * buffers created by the xserver.
530 * Although FBO's mean we now no longer use these as render targets in
531 * all circumstances, they won't go away until the back and depth
532 * buffers become private, and the front buffer will remain even then.
534 * Note that these don't allocate video memory, just describe
535 * allocations alread made by the X server.
538 intel_recreate_static_regions(struct intel_context
*intel
)
540 intelScreenPrivate
*intelScreen
= intel
->intelScreen
;
542 intel
->front_region
=
543 intel_recreate_static(intel
, "front",
545 &intelScreen
->front
);
548 intel_recreate_static(intel
, "back",
553 if (intelScreen
->third
.handle
) {
554 intel
->third_region
=
555 intel_recreate_static(intel
, "third",
557 &intelScreen
->third
);
561 /* Still assumes front.cpp == depth.cpp. We can kill this when we move to
564 intel
->depth_region
=
565 intel_recreate_static(intel
, "depth",
567 &intelScreen
->depth
);