gallium: Unify reference counting.
[mesa.git] / src / gallium / auxiliary / util / u_rect.c
1 /**************************************************************************
2 *
3 * Copyright 2008 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 /**
29 * Rectangle-related helper functions.
30 */
31
32
33 #include "pipe/p_defines.h"
34 #include "pipe/p_format.h"
35 #include "pipe/p_context.h"
36 #include "pipe/p_screen.h"
37 #include "util/u_rect.h"
38
39
40 /**
41 * Copy 2D rect from one place to another.
42 * Position and sizes are in pixels.
43 * src_pitch may be negative to do vertical flip of pixels from source.
44 */
45 void
46 pipe_copy_rect(ubyte * dst,
47 const struct pipe_format_block *block,
48 unsigned dst_stride,
49 unsigned dst_x,
50 unsigned dst_y,
51 unsigned width,
52 unsigned height,
53 const ubyte * src,
54 int src_stride,
55 unsigned src_x,
56 int src_y)
57 {
58 unsigned i;
59 int src_stride_pos = src_stride < 0 ? -src_stride : src_stride;
60
61 assert(block->size > 0);
62 assert(block->width > 0);
63 assert(block->height > 0);
64 assert(src_x >= 0);
65 assert(src_y >= 0);
66 assert(dst_x >= 0);
67 assert(dst_y >= 0);
68
69 dst_x /= block->width;
70 dst_y /= block->height;
71 width = (width + block->width - 1)/block->width;
72 height = (height + block->height - 1)/block->height;
73 src_x /= block->width;
74 src_y /= block->height;
75
76 dst += dst_x * block->size;
77 src += src_x * block->size;
78 dst += dst_y * dst_stride;
79 src += src_y * src_stride_pos;
80 width *= block->size;
81
82 if (width == dst_stride && width == src_stride)
83 memcpy(dst, src, height * width);
84 else {
85 for (i = 0; i < height; i++) {
86 memcpy(dst, src, width);
87 dst += dst_stride;
88 src += src_stride;
89 }
90 }
91 }
92
93 void
94 pipe_fill_rect(ubyte * dst,
95 const struct pipe_format_block *block,
96 unsigned dst_stride,
97 unsigned dst_x,
98 unsigned dst_y,
99 unsigned width,
100 unsigned height,
101 uint32_t value)
102 {
103 unsigned i, j;
104 unsigned width_size;
105
106 assert(block->size > 0);
107 assert(block->width > 0);
108 assert(block->height > 0);
109 assert(dst_x >= 0);
110 assert(dst_y >= 0);
111
112 dst_x /= block->width;
113 dst_y /= block->height;
114 width = (width + block->width - 1)/block->width;
115 height = (height + block->height - 1)/block->height;
116
117 dst += dst_x * block->size;
118 dst += dst_y * dst_stride;
119 width_size = width * block->size;
120
121 switch (block->size) {
122 case 1:
123 if(dst_stride == width_size)
124 memset(dst, (ubyte) value, height * width_size);
125 else {
126 for (i = 0; i < height; i++) {
127 memset(dst, (ubyte) value, width_size);
128 dst += dst_stride;
129 }
130 }
131 break;
132 case 2:
133 for (i = 0; i < height; i++) {
134 uint16_t *row = (uint16_t *)dst;
135 for (j = 0; j < width; j++)
136 *row++ = (uint16_t) value;
137 dst += dst_stride;
138 }
139 break;
140 case 4:
141 for (i = 0; i < height; i++) {
142 uint32_t *row = (uint32_t *)dst;
143 for (j = 0; j < width; j++)
144 *row++ = value;
145 dst += dst_stride;
146 }
147 break;
148 default:
149 assert(0);
150 break;
151 }
152 }
153
154
155
156 /**
157 * Fallback function for pipe->surface_copy().
158 * Note: (X,Y)=(0,0) is always the upper-left corner.
159 * if do_flip, flip the image vertically on its way from src rect to dst rect.
160 * XXX should probably put this in new u_surface.c file...
161 */
162 void
163 util_surface_copy(struct pipe_context *pipe,
164 boolean do_flip,
165 struct pipe_surface *dst,
166 unsigned dst_x, unsigned dst_y,
167 struct pipe_surface *src,
168 unsigned src_x, unsigned src_y,
169 unsigned w, unsigned h)
170 {
171 struct pipe_screen *screen = pipe->screen;
172 struct pipe_transfer *src_trans, *dst_trans;
173 void *dst_map;
174 const void *src_map;
175
176 assert(src->texture && dst->texture);
177 if (!src->texture || !dst->texture)
178 return;
179 src_trans = screen->get_tex_transfer(screen,
180 src->texture,
181 src->face,
182 src->level,
183 src->zslice,
184 PIPE_TRANSFER_READ,
185 src_x, src_y, w, h);
186
187 dst_trans = screen->get_tex_transfer(screen,
188 dst->texture,
189 dst->face,
190 dst->level,
191 dst->zslice,
192 PIPE_TRANSFER_WRITE,
193 dst_x, dst_y, w, h);
194
195 assert(dst_trans->block.size == src_trans->block.size);
196 assert(dst_trans->block.width == src_trans->block.width);
197 assert(dst_trans->block.height == src_trans->block.height);
198
199 src_map = pipe->screen->transfer_map(screen, src_trans);
200 dst_map = pipe->screen->transfer_map(screen, dst_trans);
201
202 assert(src_map);
203 assert(dst_map);
204
205 if (src_map && dst_map) {
206 /* If do_flip, invert src_y position and pass negative src stride */
207 pipe_copy_rect(dst_map,
208 &dst_trans->block,
209 dst_trans->stride,
210 0, 0,
211 w, h,
212 src_map,
213 do_flip ? -(int) src_trans->stride : src_trans->stride,
214 0,
215 do_flip ? h - 1 : 0);
216 }
217
218 pipe->screen->transfer_unmap(pipe->screen, src_trans);
219 pipe->screen->transfer_unmap(pipe->screen, dst_trans);
220
221 screen->tex_transfer_destroy(src_trans);
222 screen->tex_transfer_destroy(dst_trans);
223 }
224
225
226
227 #define UBYTE_TO_USHORT(B) ((B) | ((B) << 8))
228
229
230 /**
231 * Fallback for pipe->surface_fill() function.
232 * XXX should probably put this in new u_surface.c file...
233 */
234 void
235 util_surface_fill(struct pipe_context *pipe,
236 struct pipe_surface *dst,
237 unsigned dstx, unsigned dsty,
238 unsigned width, unsigned height, unsigned value)
239 {
240 struct pipe_screen *screen = pipe->screen;
241 struct pipe_transfer *dst_trans;
242 void *dst_map;
243
244 assert(dst->texture);
245 if (!dst->texture)
246 return;
247 dst_trans = screen->get_tex_transfer(screen,
248 dst->texture,
249 dst->face,
250 dst->level,
251 dst->zslice,
252 PIPE_TRANSFER_WRITE,
253 dstx, dsty, width, height);
254
255 dst_map = pipe->screen->transfer_map(screen, dst_trans);
256
257 assert(dst_map);
258
259 if (dst_map) {
260 assert(dst_trans->stride > 0);
261
262 switch (dst_trans->block.size) {
263 case 1:
264 case 2:
265 case 4:
266 pipe_fill_rect(dst_map, &dst_trans->block, dst_trans->stride,
267 0, 0, width, height, value);
268 break;
269 case 8:
270 {
271 /* expand the 4-byte clear value to an 8-byte value */
272 ushort *row = (ushort *) dst_map;
273 ushort val0 = UBYTE_TO_USHORT((value >> 0) & 0xff);
274 ushort val1 = UBYTE_TO_USHORT((value >> 8) & 0xff);
275 ushort val2 = UBYTE_TO_USHORT((value >> 16) & 0xff);
276 ushort val3 = UBYTE_TO_USHORT((value >> 24) & 0xff);
277 unsigned i, j;
278 val0 = (val0 << 8) | val0;
279 val1 = (val1 << 8) | val1;
280 val2 = (val2 << 8) | val2;
281 val3 = (val3 << 8) | val3;
282 for (i = 0; i < height; i++) {
283 for (j = 0; j < width; j++) {
284 row[j*4+0] = val0;
285 row[j*4+1] = val1;
286 row[j*4+2] = val2;
287 row[j*4+3] = val3;
288 }
289 row += dst_trans->stride/2;
290 }
291 }
292 break;
293 default:
294 assert(0);
295 break;
296 }
297 }
298
299 pipe->screen->transfer_unmap(pipe->screen, dst_trans);
300 screen->tex_transfer_destroy(dst_trans);
301 }