4eac859a13356fd8fd4c82a9f19ee1bb41a5a736
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 "intel_context.h"
43 #include "intel_regions.h"
44 #include "intel_blit.h"
45 #include "intel_buffer_objects.h"
46 #include "dri_bufmgr.h"
47 #include "intel_batchbuffer.h"
49 #define FILE_DEBUG_FLAG DEBUG_REGION
52 intel_region_idle(intelScreenPrivate
*intelScreen
, struct intel_region
*region
)
54 DBG("%s\n", __FUNCTION__
);
55 /* XXX: Using this function is likely bogus -- it ought to only have been
56 * used before a map, anyway, but leave this cheap implementation of it
59 if (region
&& region
->buffer
) {
60 /* Mapping it for read will ensure that any acceleration to the region
61 * would have landed already.
63 dri_bo_map(region
->buffer
, GL_TRUE
);
64 dri_bo_unmap(region
->buffer
);
68 /* XXX: Thread safety?
71 intel_region_map(intelScreenPrivate
*intelScreen
, struct intel_region
*region
)
73 DBG("%s\n", __FUNCTION__
);
74 if (!region
->map_refcount
++) {
76 intel_region_cow(intelScreen
, region
);
78 dri_bo_map(region
->buffer
, GL_TRUE
);
79 region
->map
= region
->buffer
->virtual;
86 intel_region_unmap(intelScreenPrivate
*intelScreen
, struct intel_region
*region
)
88 DBG("%s\n", __FUNCTION__
);
89 if (!--region
->map_refcount
) {
90 dri_bo_unmap(region
->buffer
);
96 intel_region_alloc(intelScreenPrivate
*intelScreen
,
97 GLuint cpp
, GLuint pitch
, GLuint height
)
99 struct intel_region
*region
= calloc(sizeof(*region
), 1);
101 DBG("%s\n", __FUNCTION__
);
104 region
->pitch
= pitch
;
105 region
->height
= height
; /* needed? */
106 region
->refcount
= 1;
108 region
->buffer
= dri_bo_alloc(intelScreen
->bufmgr
, "region",
109 pitch
* cpp
* height
, 64, DRM_BO_FLAG_MEM_TT
);
114 intel_region_reference(struct intel_region
**dst
, struct intel_region
*src
)
116 assert(*dst
== NULL
);
124 intel_region_release(struct intel_region
**region
)
129 DBG("%s %d\n", __FUNCTION__
, (*region
)->refcount
- 1);
131 ASSERT((*region
)->refcount
> 0);
132 (*region
)->refcount
--;
134 if ((*region
)->refcount
== 0) {
135 assert((*region
)->map_refcount
== 0);
138 (*region
)->pbo
->region
= NULL
;
139 (*region
)->pbo
= NULL
;
140 dri_bo_unreference((*region
)->buffer
);
147 struct intel_region
*
148 intel_region_create_static(intelScreenPrivate
*intelScreen
,
150 unsigned int bo_handle
,
153 GLuint cpp
, GLuint pitch
, GLuint height
)
155 struct intel_region
*region
= calloc(sizeof(*region
), 1);
156 DBG("%s\n", __FUNCTION__
);
159 region
->pitch
= pitch
;
160 region
->height
= height
; /* needed? */
161 region
->refcount
= 1;
163 if (intelScreen
->ttm
) {
164 assert(bo_handle
!= -1);
165 region
->buffer
= dri_ttm_bo_create_from_handle(intelScreen
->bufmgr
,
169 region
->buffer
= dri_bo_alloc_static(intelScreen
->bufmgr
,
171 offset
, pitch
* cpp
* height
,
182 intel_region_update_static(intelScreenPrivate
*intelScreen
,
183 struct intel_region
*region
,
185 unsigned int bo_handle
,
188 GLuint cpp
, GLuint pitch
, GLuint height
)
190 DBG("%s\n", __FUNCTION__
);
193 region
->pitch
= pitch
;
194 region
->height
= height
; /* needed? */
197 * We use a "shared" buffer type to indicate buffers created and
201 dri_bo_unreference(region
->buffer
);
202 if (intelScreen
->ttm
) {
203 assert(bo_handle
!= -1);
204 region
->buffer
= dri_ttm_bo_create_from_handle(intelScreen
->bufmgr
,
208 region
->buffer
= dri_bo_alloc_static(intelScreen
->bufmgr
,
210 offset
, pitch
* cpp
* height
,
219 * XXX Move this into core Mesa?
222 _mesa_copy_rect(GLubyte
* dst
,
230 GLuint src_pitch
, GLuint src_x
, GLuint src_y
)
238 dst
+= dst_y
* dst_pitch
;
239 src
+= src_y
* dst_pitch
;
242 if (width
== dst_pitch
&& width
== src_pitch
)
243 memcpy(dst
, src
, height
* width
);
245 for (i
= 0; i
< height
; i
++) {
246 memcpy(dst
, src
, width
);
254 /* Upload data to a rectangular sub-region. Lots of choices how to do this:
256 * - memcpy by span to current destination
257 * - upload data as new buffer and blit
259 * Currently always memcpy.
262 intel_region_data(intelScreenPrivate
*intelScreen
,
263 struct intel_region
*dst
,
265 GLuint dstx
, GLuint dsty
,
266 const void *src
, GLuint src_pitch
,
267 GLuint srcx
, GLuint srcy
, GLuint width
, GLuint height
)
269 struct intel_context
*intel
= intelScreenContext(intelScreen
);
271 DBG("%s\n", __FUNCTION__
);
278 dsty
== 0 && width
== dst
->pitch
&& height
== dst
->height
)
279 intel_region_release_pbo(intelScreen
, dst
);
281 intel_region_cow(intelScreen
, dst
);
285 LOCK_HARDWARE(intel
);
287 _mesa_copy_rect(intel_region_map(intelScreen
, dst
) + dst_offset
,
290 dstx
, dsty
, width
, height
, src
, src_pitch
, srcx
, srcy
);
292 intel_region_unmap(intelScreen
, dst
);
294 UNLOCK_HARDWARE(intel
);
298 /* Copy rectangular sub-regions. Need better logic about when to
299 * push buffers into AGP - will currently do so whenever possible.
302 intel_region_copy(intelScreenPrivate
*intelScreen
,
303 struct intel_region
*dst
,
305 GLuint dstx
, GLuint dsty
,
306 struct intel_region
*src
,
308 GLuint srcx
, GLuint srcy
, GLuint width
, GLuint height
)
310 struct intel_context
*intel
= intelScreenContext(intelScreen
);
312 DBG("%s\n", __FUNCTION__
);
319 dsty
== 0 && width
== dst
->pitch
&& height
== dst
->height
)
320 intel_region_release_pbo(intelScreen
, dst
);
322 intel_region_cow(intelScreen
, dst
);
325 assert(src
->cpp
== dst
->cpp
);
327 intelEmitCopyBlit(intel
,
329 src
->pitch
, src
->buffer
, src_offset
,
330 dst
->pitch
, dst
->buffer
, dst_offset
,
331 srcx
, srcy
, dstx
, dsty
, width
, height
,
335 /* Fill a rectangular sub-region. Need better logic about when to
336 * push buffers into AGP - will currently do so whenever possible.
339 intel_region_fill(intelScreenPrivate
*intelScreen
,
340 struct intel_region
*dst
,
342 GLuint dstx
, GLuint dsty
,
343 GLuint width
, GLuint height
, GLuint color
)
345 struct intel_context
*intel
= intelScreenContext(intelScreen
);
347 DBG("%s\n", __FUNCTION__
);
354 dsty
== 0 && width
== dst
->pitch
&& height
== dst
->height
)
355 intel_region_release_pbo(intelScreen
, dst
);
357 intel_region_cow(intelScreen
, dst
);
360 intelEmitFillBlit(intel
,
362 dst
->pitch
, dst
->buffer
, dst_offset
,
363 dstx
, dsty
, width
, height
, color
);
366 /* Attach to a pbo, discarding our data. Effectively zero-copy upload
370 intel_region_attach_pbo(intelScreenPrivate
*intelScreen
,
371 struct intel_region
*region
,
372 struct intel_buffer_object
*pbo
)
374 if (region
->pbo
== pbo
)
377 /* If there is already a pbo attached, break the cow tie now.
378 * Don't call intel_region_release_pbo() as that would
379 * unnecessarily allocate a new buffer we would have to immediately
383 region
->pbo
->region
= NULL
;
387 if (region
->buffer
) {
388 dri_bo_unreference(region
->buffer
);
389 region
->buffer
= NULL
;
393 region
->pbo
->region
= region
;
394 dri_bo_reference(pbo
->buffer
);
395 region
->buffer
= pbo
->buffer
;
399 /* Break the COW tie to the pbo and allocate a new buffer.
400 * The pbo gets to keep the data.
403 intel_region_release_pbo(intelScreenPrivate
*intelScreen
,
404 struct intel_region
*region
)
406 assert(region
->buffer
== region
->pbo
->buffer
);
407 region
->pbo
->region
= NULL
;
409 dri_bo_unreference(region
->buffer
);
410 region
->buffer
= NULL
;
412 region
->buffer
= dri_bo_alloc(intelScreen
->bufmgr
, "region",
413 region
->pitch
* region
->cpp
* region
->height
,
414 64, DRM_BO_FLAG_MEM_TT
);
417 /* Break the COW tie to the pbo. Both the pbo and the region end up
418 * with a copy of the data.
421 intel_region_cow(intelScreenPrivate
*intelScreen
, struct intel_region
*region
)
423 struct intel_context
*intel
= intelScreenContext(intelScreen
);
424 struct intel_buffer_object
*pbo
= region
->pbo
;
429 intel_region_release_pbo(intelScreen
, region
);
431 assert(region
->cpp
* region
->pitch
* region
->height
== pbo
->Base
.Size
);
433 DBG("%s (%d bytes)\n", __FUNCTION__
, pbo
->Base
.Size
);
435 /* Now blit from the texture buffer to the new buffer:
438 intel_batchbuffer_flush(intel
->batch
);
440 if (!intel
->locked
) {
441 LOCK_HARDWARE(intel
);
442 intelEmitCopyBlit(intel
,
449 region
->pitch
, region
->height
,
452 intel_batchbuffer_flush(intel
->batch
);
453 UNLOCK_HARDWARE(intel
);
456 intelEmitCopyBlit(intel
,
463 region
->pitch
, region
->height
,
466 intel_batchbuffer_flush(intel
->batch
);
471 intel_region_buffer(intelScreenPrivate
*intelScreen
,
472 struct intel_region
*region
, GLuint flag
)
475 if (flag
== INTEL_WRITE_PART
)
476 intel_region_cow(intelScreen
, region
);
477 else if (flag
== INTEL_WRITE_FULL
)
478 intel_region_release_pbo(intelScreen
, region
);
481 return region
->buffer
;