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 pipe_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 pipe_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_surface
*new_src
= NULL
, *new_dst
= NULL
;
176 assert(dst
->block
.size
== src
->block
.size
);
177 assert(dst
->block
.width
== src
->block
.width
);
178 assert(dst
->block
.height
== src
->block
.height
);
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
);
185 new_src
= screen
->get_tex_surface(screen
,
190 PIPE_BUFFER_USAGE_CPU_READ
);
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
);
199 new_dst
= screen
->get_tex_surface(screen
,
204 PIPE_BUFFER_USAGE_CPU_WRITE
);
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
);
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
,
224 do_flip
? -(int) src
->stride
: src
->stride
,
228 pipe
->screen
->surface_unmap(pipe
->screen
, src
);
229 pipe
->screen
->surface_unmap(pipe
->screen
, dst
);
232 screen
->tex_surface_release(screen
, &new_src
);
234 screen
->tex_surface_release(screen
, &new_dst
);
240 get_pointer(struct pipe_surface
*dst
, void *dst_map
, unsigned x
, unsigned y
)
242 return (char *)dst_map
243 + y
/ dst
->block
.height
* dst
->stride
244 + x
/ dst
->block
.width
* dst
->block
.size
;
248 #define UBYTE_TO_USHORT(B) ((B) | ((B) << 8))
252 * Fallback for pipe->surface_fill() function.
253 * XXX should probably put this in new u_surface.c file...
256 util_surface_fill(struct pipe_context
*pipe
,
257 struct pipe_surface
*dst
,
258 unsigned dstx
, unsigned dsty
,
259 unsigned width
, unsigned height
, unsigned value
)
261 struct pipe_screen
*screen
= pipe
->screen
;
262 struct pipe_surface
*new_dst
= NULL
;
265 if ((dst
->usage
& PIPE_BUFFER_USAGE_CPU_WRITE
) == 0) {
266 /* Need to create new dst surface which is CPU writable */
267 assert(dst
->texture
);
270 new_dst
= screen
->get_tex_surface(screen
,
275 PIPE_BUFFER_USAGE_CPU_WRITE
);
279 dst_map
= pipe
->screen
->surface_map(screen
,
280 dst
, PIPE_BUFFER_USAGE_CPU_WRITE
);
285 assert(dst
->stride
> 0);
287 switch (dst
->block
.size
) {
291 pipe_fill_rect(dst_map
, &dst
->block
, dst
->stride
,
292 dstx
, dsty
, width
, height
, value
);
296 /* expand the 4-byte clear value to an 8-byte value */
297 ushort
*row
= (ushort
*) get_pointer(dst
, dst_map
, dstx
, dsty
);
298 ushort val0
= UBYTE_TO_USHORT((value
>> 0) & 0xff);
299 ushort val1
= UBYTE_TO_USHORT((value
>> 8) & 0xff);
300 ushort val2
= UBYTE_TO_USHORT((value
>> 16) & 0xff);
301 ushort val3
= UBYTE_TO_USHORT((value
>> 24) & 0xff);
303 val0
= (val0
<< 8) | val0
;
304 val1
= (val1
<< 8) | val1
;
305 val2
= (val2
<< 8) | val2
;
306 val3
= (val3
<< 8) | val3
;
307 for (i
= 0; i
< height
; i
++) {
308 for (j
= 0; j
< width
; j
++) {
314 row
+= dst
->stride
/2;
324 pipe
->screen
->surface_unmap(pipe
->screen
, dst
);
327 screen
->tex_surface_release(screen
, &new_dst
);