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_bufmgr_ttm.h"
48 #include "intel_batchbuffer.h"
50 #define FILE_DEBUG_FLAG DEBUG_REGION
53 intel_region_idle(intelScreenPrivate
*intelScreen
, struct intel_region
*region
)
55 DBG("%s\n", __FUNCTION__
);
56 /* XXX: Using this function is likely bogus -- it ought to only have been
57 * used before a map, anyway, but leave this cheap implementation of it
60 if (region
&& region
->buffer
) {
61 /* Mapping it for read will ensure that any acceleration to the region
62 * would have landed already.
64 dri_bo_map(region
->buffer
, GL_TRUE
);
65 dri_bo_unmap(region
->buffer
);
69 /* XXX: Thread safety?
72 intel_region_map(intelScreenPrivate
*intelScreen
, struct intel_region
*region
)
74 DBG("%s\n", __FUNCTION__
);
75 if (!region
->map_refcount
++) {
77 intel_region_cow(intelScreen
, region
);
79 dri_bo_map(region
->buffer
, GL_TRUE
);
80 region
->map
= region
->buffer
->virtual;
87 intel_region_unmap(intelScreenPrivate
*intelScreen
, struct intel_region
*region
)
89 DBG("%s\n", __FUNCTION__
);
90 if (!--region
->map_refcount
) {
91 dri_bo_unmap(region
->buffer
);
97 intel_region_alloc(intelScreenPrivate
*intelScreen
,
98 GLuint cpp
, GLuint pitch
, GLuint height
)
100 struct intel_region
*region
= calloc(sizeof(*region
), 1);
102 DBG("%s\n", __FUNCTION__
);
105 region
->pitch
= pitch
;
106 region
->height
= height
; /* needed? */
107 region
->refcount
= 1;
109 region
->buffer
= dri_bo_alloc(intelScreen
->bufmgr
, "region",
110 pitch
* cpp
* height
, 64, DRM_BO_FLAG_MEM_TT
);
115 intel_region_reference(struct intel_region
**dst
, struct intel_region
*src
)
117 assert(*dst
== NULL
);
125 intel_region_release(struct intel_region
**region
)
130 DBG("%s %d\n", __FUNCTION__
, (*region
)->refcount
- 1);
132 ASSERT((*region
)->refcount
> 0);
133 (*region
)->refcount
--;
135 if ((*region
)->refcount
== 0) {
136 assert((*region
)->map_refcount
== 0);
139 (*region
)->pbo
->region
= NULL
;
140 (*region
)->pbo
= NULL
;
141 dri_bo_unreference((*region
)->buffer
);
148 struct intel_region
*
149 intel_region_create_static(intelScreenPrivate
*intelScreen
,
151 unsigned int bo_handle
,
154 GLuint cpp
, GLuint pitch
, GLuint height
)
156 struct intel_region
*region
= calloc(sizeof(*region
), 1);
157 DBG("%s\n", __FUNCTION__
);
160 region
->pitch
= pitch
;
161 region
->height
= height
; /* needed? */
162 region
->refcount
= 1;
164 if (intelScreen
->ttm
) {
165 assert(bo_handle
!= -1);
166 region
->buffer
= intel_ttm_bo_create_from_handle(intelScreen
->bufmgr
,
170 region
->buffer
= dri_bo_alloc_static(intelScreen
->bufmgr
,
172 offset
, pitch
* cpp
* height
,
183 intel_region_update_static(intelScreenPrivate
*intelScreen
,
184 struct intel_region
*region
,
186 unsigned int bo_handle
,
189 GLuint cpp
, GLuint pitch
, GLuint height
)
191 DBG("%s\n", __FUNCTION__
);
194 region
->pitch
= pitch
;
195 region
->height
= height
; /* needed? */
198 * We use a "shared" buffer type to indicate buffers created and
202 dri_bo_unreference(region
->buffer
);
203 if (intelScreen
->ttm
) {
204 assert(bo_handle
!= -1);
205 region
->buffer
= intel_ttm_bo_create_from_handle(intelScreen
->bufmgr
,
209 region
->buffer
= dri_bo_alloc_static(intelScreen
->bufmgr
,
211 offset
, pitch
* cpp
* height
,
220 * XXX Move this into core Mesa?
223 _mesa_copy_rect(GLubyte
* dst
,
231 GLuint src_pitch
, GLuint src_x
, GLuint src_y
)
239 dst
+= dst_y
* dst_pitch
;
240 src
+= src_y
* dst_pitch
;
243 if (width
== dst_pitch
&& width
== src_pitch
)
244 memcpy(dst
, src
, height
* width
);
246 for (i
= 0; i
< height
; i
++) {
247 memcpy(dst
, src
, width
);
255 /* Upload data to a rectangular sub-region. Lots of choices how to do this:
257 * - memcpy by span to current destination
258 * - upload data as new buffer and blit
260 * Currently always memcpy.
263 intel_region_data(intelScreenPrivate
*intelScreen
,
264 struct intel_region
*dst
,
266 GLuint dstx
, GLuint dsty
,
267 const void *src
, GLuint src_pitch
,
268 GLuint srcx
, GLuint srcy
, GLuint width
, GLuint height
)
270 struct intel_context
*intel
= intelScreenContext(intelScreen
);
272 DBG("%s\n", __FUNCTION__
);
279 dsty
== 0 && width
== dst
->pitch
&& height
== dst
->height
)
280 intel_region_release_pbo(intelScreen
, dst
);
282 intel_region_cow(intelScreen
, dst
);
286 LOCK_HARDWARE(intel
);
288 _mesa_copy_rect(intel_region_map(intelScreen
, dst
) + dst_offset
,
291 dstx
, dsty
, width
, height
, src
, src_pitch
, srcx
, srcy
);
293 intel_region_unmap(intelScreen
, dst
);
295 UNLOCK_HARDWARE(intel
);
299 /* Copy rectangular sub-regions. Need better logic about when to
300 * push buffers into AGP - will currently do so whenever possible.
303 intel_region_copy(intelScreenPrivate
*intelScreen
,
304 struct intel_region
*dst
,
306 GLuint dstx
, GLuint dsty
,
307 struct intel_region
*src
,
309 GLuint srcx
, GLuint srcy
, GLuint width
, GLuint height
)
311 struct intel_context
*intel
= intelScreenContext(intelScreen
);
313 DBG("%s\n", __FUNCTION__
);
320 dsty
== 0 && width
== dst
->pitch
&& height
== dst
->height
)
321 intel_region_release_pbo(intelScreen
, dst
);
323 intel_region_cow(intelScreen
, dst
);
326 assert(src
->cpp
== dst
->cpp
);
328 intelEmitCopyBlit(intel
,
330 src
->pitch
, src
->buffer
, src_offset
,
331 dst
->pitch
, dst
->buffer
, dst_offset
,
332 srcx
, srcy
, dstx
, dsty
, width
, height
,
336 /* Fill a rectangular sub-region. Need better logic about when to
337 * push buffers into AGP - will currently do so whenever possible.
340 intel_region_fill(intelScreenPrivate
*intelScreen
,
341 struct intel_region
*dst
,
343 GLuint dstx
, GLuint dsty
,
344 GLuint width
, GLuint height
, GLuint color
)
346 struct intel_context
*intel
= intelScreenContext(intelScreen
);
348 DBG("%s\n", __FUNCTION__
);
355 dsty
== 0 && width
== dst
->pitch
&& height
== dst
->height
)
356 intel_region_release_pbo(intelScreen
, dst
);
358 intel_region_cow(intelScreen
, dst
);
361 intelEmitFillBlit(intel
,
363 dst
->pitch
, dst
->buffer
, dst_offset
,
364 dstx
, dsty
, width
, height
, color
);
367 /* Attach to a pbo, discarding our data. Effectively zero-copy upload
371 intel_region_attach_pbo(intelScreenPrivate
*intelScreen
,
372 struct intel_region
*region
,
373 struct intel_buffer_object
*pbo
)
375 if (region
->pbo
== pbo
)
378 /* If there is already a pbo attached, break the cow tie now.
379 * Don't call intel_region_release_pbo() as that would
380 * unnecessarily allocate a new buffer we would have to immediately
384 region
->pbo
->region
= NULL
;
388 if (region
->buffer
) {
389 dri_bo_unreference(region
->buffer
);
390 region
->buffer
= NULL
;
394 region
->pbo
->region
= region
;
395 dri_bo_reference(pbo
->buffer
);
396 region
->buffer
= pbo
->buffer
;
400 /* Break the COW tie to the pbo and allocate a new buffer.
401 * The pbo gets to keep the data.
404 intel_region_release_pbo(intelScreenPrivate
*intelScreen
,
405 struct intel_region
*region
)
407 assert(region
->buffer
== region
->pbo
->buffer
);
408 region
->pbo
->region
= NULL
;
410 dri_bo_unreference(region
->buffer
);
411 region
->buffer
= NULL
;
413 region
->buffer
= dri_bo_alloc(intelScreen
->bufmgr
, "region",
414 region
->pitch
* region
->cpp
* region
->height
,
415 64, DRM_BO_FLAG_MEM_TT
);
418 /* Break the COW tie to the pbo. Both the pbo and the region end up
419 * with a copy of the data.
422 intel_region_cow(intelScreenPrivate
*intelScreen
, struct intel_region
*region
)
424 struct intel_context
*intel
= intelScreenContext(intelScreen
);
425 struct intel_buffer_object
*pbo
= region
->pbo
;
430 intel_region_release_pbo(intelScreen
, region
);
432 assert(region
->cpp
* region
->pitch
* region
->height
== pbo
->Base
.Size
);
434 DBG("%s (%d bytes)\n", __FUNCTION__
, pbo
->Base
.Size
);
436 /* Now blit from the texture buffer to the new buffer:
439 intel_batchbuffer_flush(intel
->batch
);
441 if (!intel
->locked
) {
442 LOCK_HARDWARE(intel
);
443 intelEmitCopyBlit(intel
,
450 region
->pitch
, region
->height
,
453 intel_batchbuffer_flush(intel
->batch
);
454 UNLOCK_HARDWARE(intel
);
457 intelEmitCopyBlit(intel
,
464 region
->pitch
, region
->height
,
467 intel_batchbuffer_flush(intel
->batch
);
472 intel_region_buffer(intelScreenPrivate
*intelScreen
,
473 struct intel_region
*region
, GLuint flag
)
476 if (flag
== INTEL_WRITE_PART
)
477 intel_region_cow(intelScreen
, region
);
478 else if (flag
== INTEL_WRITE_FULL
)
479 intel_region_release_pbo(intelScreen
, region
);
482 return region
->buffer
;