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