1 /**************************************************************************
3 * Copyright 2008 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 **************************************************************************/
29 * Rectangle-related helper functions.
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"
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.
46 util_copy_rect(ubyte
* dst
,
47 enum pipe_format format
,
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
);
64 assert(blocksize
> 0);
65 assert(blockwidth
> 0);
66 assert(blockheight
> 0);
74 width
= (width
+ blockwidth
- 1)/blockwidth
;
75 height
= (height
+ blockheight
- 1)/blockheight
;
79 dst
+= dst_x
* blocksize
;
80 src
+= src_x
* blocksize
;
81 dst
+= dst_y
* dst_stride
;
82 src
+= src_y
* src_stride_pos
;
85 if (width
== dst_stride
&& width
== src_stride
)
86 memcpy(dst
, src
, height
* width
);
88 for (i
= 0; i
< height
; i
++) {
89 memcpy(dst
, src
, width
);
97 util_fill_rect(ubyte
* dst
,
98 enum pipe_format format
,
108 int blocksize
= pf_get_blocksize(format
);
109 int blockwidth
= pf_get_blockwidth(format
);
110 int blockheight
= pf_get_blockheight(format
);
112 assert(blocksize
> 0);
113 assert(blockwidth
> 0);
114 assert(blockheight
> 0);
119 dst_y
/= blockheight
;
120 width
= (width
+ blockwidth
- 1)/blockwidth
;
121 height
= (height
+ blockheight
- 1)/blockheight
;
123 dst
+= dst_x
* blocksize
;
124 dst
+= dst_y
* dst_stride
;
125 width_size
= width
* blocksize
;
129 if(dst_stride
== width_size
)
130 memset(dst
, (ubyte
) value
, height
* width_size
);
132 for (i
= 0; i
< height
; i
++) {
133 memset(dst
, (ubyte
) value
, width_size
);
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
;
147 for (i
= 0; i
< height
; i
++) {
148 uint32_t *row
= (uint32_t *)dst
;
149 for (j
= 0; j
< width
; j
++)
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...
169 util_surface_copy(struct pipe_context
*pipe
,
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
)
177 struct pipe_screen
*screen
= pipe
->screen
;
178 struct pipe_transfer
*src_trans
, *dst_trans
;
181 enum pipe_format src_format
, dst_format
;
183 assert(src
->texture
&& dst
->texture
);
184 if (!src
->texture
|| !dst
->texture
)
187 src_format
= src
->texture
->format
;
188 dst_format
= dst
->texture
->format
;
190 src_trans
= screen
->get_tex_transfer(screen
,
198 dst_trans
= screen
->get_tex_transfer(screen
,
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
));
210 src_map
= pipe
->screen
->transfer_map(screen
, src_trans
);
211 dst_map
= pipe
->screen
->transfer_map(screen
, dst_trans
);
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
,
224 do_flip
? -(int) src_trans
->stride
: src_trans
->stride
,
226 do_flip
? h
- 1 : 0);
229 pipe
->screen
->transfer_unmap(pipe
->screen
, src_trans
);
230 pipe
->screen
->transfer_unmap(pipe
->screen
, dst_trans
);
232 screen
->tex_transfer_destroy(src_trans
);
233 screen
->tex_transfer_destroy(dst_trans
);
238 #define UBYTE_TO_USHORT(B) ((B) | ((B) << 8))
242 * Fallback for pipe->surface_fill() function.
243 * XXX should probably put this in new u_surface.c file...
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
)
251 struct pipe_screen
*screen
= pipe
->screen
;
252 struct pipe_transfer
*dst_trans
;
255 assert(dst
->texture
);
258 dst_trans
= screen
->get_tex_transfer(screen
,
264 dstx
, dsty
, width
, height
);
266 dst_map
= pipe
->screen
->transfer_map(screen
, dst_trans
);
271 assert(dst_trans
->stride
> 0);
273 switch (pf_get_blocksize(dst_trans
->texture
->format
)) {
277 util_fill_rect(dst_map
, dst_trans
->texture
->format
, dst_trans
->stride
,
278 0, 0, width
, height
, value
);
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);
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
++) {
300 row
+= dst_trans
->stride
/2;
310 pipe
->screen
->transfer_unmap(pipe
->screen
, dst_trans
);
311 screen
->tex_transfer_destroy(dst_trans
);