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