9866b6fc8a0e626eb3ff1cfc17777ea94b2962f0
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 const struct pipe_format_block
*block
,
59 int src_stride_pos
= src_stride
< 0 ? -src_stride
: src_stride
;
61 assert(block
->size
> 0);
62 assert(block
->width
> 0);
63 assert(block
->height
> 0);
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
;
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
;
82 if (width
== dst_stride
&& width
== src_stride
)
83 memcpy(dst
, src
, height
* width
);
85 for (i
= 0; i
< height
; i
++) {
86 memcpy(dst
, src
, width
);
94 util_fill_rect(ubyte
* dst
,
95 const struct pipe_format_block
*block
,
106 assert(block
->size
> 0);
107 assert(block
->width
> 0);
108 assert(block
->height
> 0);
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
;
117 dst
+= dst_x
* block
->size
;
118 dst
+= dst_y
* dst_stride
;
119 width_size
= width
* block
->size
;
121 switch (block
->size
) {
123 if(dst_stride
== width_size
)
124 memset(dst
, (ubyte
) value
, height
* width_size
);
126 for (i
= 0; i
< height
; i
++) {
127 memset(dst
, (ubyte
) value
, width_size
);
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
;
141 for (i
= 0; i
< height
; i
++) {
142 uint32_t *row
= (uint32_t *)dst
;
143 for (j
= 0; j
< width
; j
++)
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...
163 util_surface_copy(struct pipe_context
*pipe
,
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
)
171 struct pipe_screen
*screen
= pipe
->screen
;
172 struct pipe_transfer
*src_trans
, *dst_trans
;
176 assert(src
->texture
&& dst
->texture
);
177 if (!src
->texture
|| !dst
->texture
)
179 src_trans
= screen
->get_tex_transfer(screen
,
187 dst_trans
= screen
->get_tex_transfer(screen
,
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
);
199 src_map
= pipe
->screen
->transfer_map(screen
, src_trans
);
200 dst_map
= pipe
->screen
->transfer_map(screen
, dst_trans
);
205 if (src_map
&& dst_map
) {
206 /* If do_flip, invert src_y position and pass negative src stride */
207 util_copy_rect(dst_map
,
213 do_flip
? -(int) src_trans
->stride
: src_trans
->stride
,
215 do_flip
? h
- 1 : 0);
218 pipe
->screen
->transfer_unmap(pipe
->screen
, src_trans
);
219 pipe
->screen
->transfer_unmap(pipe
->screen
, dst_trans
);
221 screen
->tex_transfer_destroy(src_trans
);
222 screen
->tex_transfer_destroy(dst_trans
);
227 #define UBYTE_TO_USHORT(B) ((B) | ((B) << 8))
231 * Fallback for pipe->surface_fill() function.
232 * XXX should probably put this in new u_surface.c file...
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
)
240 struct pipe_screen
*screen
= pipe
->screen
;
241 struct pipe_transfer
*dst_trans
;
244 assert(dst
->texture
);
247 dst_trans
= screen
->get_tex_transfer(screen
,
253 dstx
, dsty
, width
, height
);
255 dst_map
= pipe
->screen
->transfer_map(screen
, dst_trans
);
260 assert(dst_trans
->stride
> 0);
262 switch (dst_trans
->block
.size
) {
266 util_fill_rect(dst_map
, &dst_trans
->block
, dst_trans
->stride
,
267 0, 0, width
, height
, value
);
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);
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
++) {
289 row
+= dst_trans
->stride
/2;
299 pipe
->screen
->transfer_unmap(pipe
->screen
, dst_trans
);
300 screen
->tex_transfer_destroy(dst_trans
);