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"
48 #define FILE_DEBUG_FLAG DEBUG_REGION
50 /* XXX: Thread safety?
52 GLubyte
*intel_region_map(struct intel_context
*intel
, struct intel_region
*region
)
54 DBG("%s\n", __FUNCTION__
);
55 if (!region
->map_refcount
++) {
56 region
->map
= bmMapBuffer(intel
, region
->buffer
, 0);
58 region
->map_refcount
--;
64 void intel_region_unmap(struct intel_context
*intel
,
65 struct intel_region
*region
)
67 DBG("%s\n", __FUNCTION__
);
68 if (!--region
->map_refcount
) {
69 bmUnmapBuffer(intel
, region
->buffer
);
74 struct intel_region
*intel_region_alloc( struct intel_context
*intel
,
79 struct intel_region
*region
= calloc(sizeof(*region
), 1);
81 DBG("%s %dx%dx%d == 0x%x bytes\n", __FUNCTION__
,
82 cpp
, pitch
, height
, cpp
*pitch
*height
);
85 region
->pitch
= pitch
;
86 region
->height
= height
; /* needed? */
89 bmGenBuffers(intel
, "tex", 1, ®ion
->buffer
, 6);
90 bmBufferData(intel
, region
->buffer
, pitch
* cpp
* height
, NULL
, 0);
95 void intel_region_reference( struct intel_region
**dst
,
96 struct intel_region
*src
)
103 void intel_region_release( struct intel_context
*intel
,
104 struct intel_region
**region
)
109 DBG("%s %d\n", __FUNCTION__
, (*region
)->refcount
-1);
111 if (--(*region
)->refcount
== 0) {
112 assert((*region
)->map_refcount
== 0);
113 bmDeleteBuffers(intel
, 1, &(*region
)->buffer
);
120 struct intel_region
*intel_region_create_static( struct intel_context
*intel
,
130 struct intel_region
*region
= calloc(sizeof(*region
), 1);
133 DBG("%s\n", __FUNCTION__
);
136 region
->pitch
= pitch
;
137 region
->height
= height
; /* needed? */
138 region
->refcount
= 1;
139 region
->tiled
= tiled
;
141 /* Recipe for creating a static buffer - create a static pool with
142 * the right offset and size, generate a buffer and use a special
143 * call to bind it to all of the memory in that pool.
145 pool
= bmInitPool(intel
, offset
, virtual, size
,
151 _mesa_printf("bmInitPool failed for static region\n");
155 region
->buffer
= bmGenBufferStatic(intel
, pool
);
163 void _mesa_copy_rect( GLubyte
*dst
,
181 dst
+= dst_y
* dst_pitch
;
182 src
+= src_y
* dst_pitch
;
185 if (width
== dst_pitch
&&
187 do_memcpy(dst
, src
, height
* width
);
189 for (i
= 0; i
< height
; i
++) {
190 do_memcpy(dst
, src
, width
);
198 /* Upload data to a rectangular sub-region. Lots of choices how to do this:
200 * - memcpy by span to current destination
201 * - upload data as new buffer and blit
203 * Currently always memcpy.
205 GLboolean
intel_region_data(struct intel_context
*intel
,
206 struct intel_region
*dst
,
208 GLuint dstx
, GLuint dsty
,
209 const void *src
, GLuint src_pitch
,
210 GLuint srcx
, GLuint srcy
,
211 GLuint width
, GLuint height
)
213 DBG("%s\n", __FUNCTION__
);
215 if (width
== dst
->pitch
&&
216 width
== src_pitch
&&
218 height
== dst
->height
&&
222 return (bmBufferData(intel
,
224 dst
->cpp
* width
* dst
->height
,
228 GLubyte
*map
= intel_region_map(intel
, dst
);
231 assert (dst_offset
+ dstx
+ width
+
232 (dsty
+ height
- 1) * dst
->pitch
* dst
->cpp
<=
233 dst
->pitch
* dst
->cpp
* dst
->height
);
235 _mesa_copy_rect(map
+ dst_offset
,
244 intel_region_unmap(intel
, dst
);
252 /* Copy rectangular sub-regions. Need better logic about when to
253 * push buffers into AGP - will currently do so whenever possible.
255 void intel_region_copy( struct intel_context
*intel
,
256 struct intel_region
*dst
,
258 GLuint dstx
, GLuint dsty
,
259 struct intel_region
*src
,
261 GLuint srcx
, GLuint srcy
,
262 GLuint width
, GLuint height
)
264 DBG("%s\n", __FUNCTION__
);
266 assert(src
->cpp
== dst
->cpp
);
268 intelEmitCopyBlit(intel
,
270 src
->pitch
, src
->buffer
, src_offset
, src
->tiled
,
271 dst
->pitch
, dst
->buffer
, dst_offset
, dst
->tiled
,
278 /* Fill a rectangular sub-region. Need better logic about when to
279 * push buffers into AGP - will currently do so whenever possible.
281 void intel_region_fill( struct intel_context
*intel
,
282 struct intel_region
*dst
,
284 GLuint dstx
, GLuint dsty
,
285 GLuint width
, GLuint height
,
288 DBG("%s\n", __FUNCTION__
);
290 intelEmitFillBlit(intel
,
292 dst
->pitch
, dst
->buffer
, dst_offset
, dst
->tiled
,