Merge commit 'origin/master' into gallium-0.2
[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_surface *new_src = NULL, *new_dst = NULL;
173 void *dst_map;
174 const void *src_map;
175
176 assert(dst->block.size == src->block.size);
177 assert(dst->block.width == src->block.width);
178 assert(dst->block.height == src->block.height);
179
180 if ((src->usage & PIPE_BUFFER_USAGE_CPU_READ) == 0) {
181 /* Need to create new src surface which is CPU readable */
182 assert(src->texture);
183 if (!src->texture)
184 return;
185 new_src = screen->get_tex_surface(screen,
186 src->texture,
187 src->face,
188 src->level,
189 src->zslice,
190 PIPE_BUFFER_USAGE_CPU_READ);
191 src = new_src;
192 }
193
194 if ((dst->usage & PIPE_BUFFER_USAGE_CPU_WRITE) == 0) {
195 /* Need to create new dst surface which is CPU writable */
196 assert(dst->texture);
197 if (!dst->texture)
198 return;
199 new_dst = screen->get_tex_surface(screen,
200 dst->texture,
201 dst->face,
202 dst->level,
203 dst->zslice,
204 PIPE_BUFFER_USAGE_CPU_WRITE);
205 dst = new_dst;
206 }
207
208 src_map = pipe->screen->surface_map(screen,
209 src, PIPE_BUFFER_USAGE_CPU_READ);
210 dst_map = pipe->screen->surface_map(screen,
211 dst, PIPE_BUFFER_USAGE_CPU_WRITE);
212
213 assert(src_map);
214 assert(dst_map);
215
216 if (src_map && dst_map) {
217 /* If do_flip, invert src_y position and pass negative src stride */
218 pipe_copy_rect(dst_map,
219 &dst->block,
220 dst->stride,
221 dst_x, dst_y,
222 w, h,
223 src_map,
224 do_flip ? -(int) src->stride : src->stride,
225 src_x,
226 do_flip ? w - src_y : src_y);
227 }
228
229 pipe->screen->surface_unmap(pipe->screen, src);
230 pipe->screen->surface_unmap(pipe->screen, dst);
231
232 if (new_src)
233 screen->tex_surface_release(screen, &new_src);
234 if (new_dst)
235 screen->tex_surface_release(screen, &new_dst);
236 }
237
238
239
240 static void *
241 get_pointer(struct pipe_surface *dst, void *dst_map, unsigned x, unsigned y)
242 {
243 return (char *)dst_map
244 + y / dst->block.height * dst->stride
245 + x / dst->block.width * dst->block.size;
246 }
247
248
249 #define UBYTE_TO_USHORT(B) ((B) | ((B) << 8))
250
251
252 /**
253 * Fallback for pipe->surface_fill() function.
254 * XXX should probably put this in new u_surface.c file...
255 */
256 void
257 util_surface_fill(struct pipe_context *pipe,
258 struct pipe_surface *dst,
259 unsigned dstx, unsigned dsty,
260 unsigned width, unsigned height, unsigned value)
261 {
262 struct pipe_screen *screen = pipe->screen;
263 struct pipe_surface *new_dst = NULL;
264 void *dst_map;
265
266 if ((dst->usage & PIPE_BUFFER_USAGE_CPU_WRITE) == 0) {
267 /* Need to create new dst surface which is CPU writable */
268 assert(dst->texture);
269 if (!dst->texture)
270 return;
271 new_dst = screen->get_tex_surface(screen,
272 dst->texture,
273 dst->face,
274 dst->level,
275 dst->zslice,
276 PIPE_BUFFER_USAGE_CPU_WRITE);
277 dst = new_dst;
278 }
279
280 dst_map = pipe->screen->surface_map(screen,
281 dst, PIPE_BUFFER_USAGE_CPU_WRITE);
282
283 assert(dst_map);
284
285 if (dst_map) {
286 assert(dst->stride > 0);
287
288 switch (dst->block.size) {
289 case 1:
290 case 2:
291 case 4:
292 pipe_fill_rect(dst_map, &dst->block, dst->stride,
293 dstx, dsty, width, height, value);
294 break;
295 case 8:
296 {
297 /* expand the 4-byte clear value to an 8-byte value */
298 ushort *row = (ushort *) get_pointer(dst, dst_map, dstx, dsty);
299 ushort val0 = UBYTE_TO_USHORT((value >> 0) & 0xff);
300 ushort val1 = UBYTE_TO_USHORT((value >> 8) & 0xff);
301 ushort val2 = UBYTE_TO_USHORT((value >> 16) & 0xff);
302 ushort val3 = UBYTE_TO_USHORT((value >> 24) & 0xff);
303 unsigned i, j;
304 val0 = (val0 << 8) | val0;
305 val1 = (val1 << 8) | val1;
306 val2 = (val2 << 8) | val2;
307 val3 = (val3 << 8) | val3;
308 for (i = 0; i < height; i++) {
309 for (j = 0; j < width; j++) {
310 row[j*4+0] = val0;
311 row[j*4+1] = val1;
312 row[j*4+2] = val2;
313 row[j*4+3] = val3;
314 }
315 row += dst->stride/2;
316 }
317 }
318 break;
319 default:
320 assert(0);
321 break;
322 }
323 }
324
325 pipe->screen->surface_unmap(pipe->screen, dst);
326
327 if (new_dst)
328 screen->tex_surface_release(screen, &new_dst);
329 }