gallium: move pipe_copy_rect(), pipe_fill_rect() protos into new u_rect.h header
[mesa.git] / src / gallium / drivers / softpipe / sp_surface.c
1 /**************************************************************************
2 *
3 * Copyright 2007 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 #include "pipe/p_defines.h"
29 #include "pipe/p_util.h"
30 #include "pipe/p_inlines.h"
31 #include "pipe/p_winsys.h"
32 #include "util/p_tile.h"
33 #include "util/u_rect.h"
34 #include "sp_context.h"
35 #include "sp_surface.h"
36
37
38
39 /**
40 * Copy a rectangular region from one surface to another.
41 * Surfaces must have same bpp.
42 *
43 * Note that it's always the case that Y=0=top of the raster.
44 * If do_flip is non-zero, the region being copied will be flipped vertically.
45 *
46 * Assumes all values are within bounds -- no checking at this level -
47 * do it higher up if required.
48 */
49 static void
50 sp_surface_copy(struct pipe_context *pipe,
51 boolean do_flip,
52 struct pipe_surface *dst,
53 unsigned dstx, unsigned dsty,
54 struct pipe_surface *src,
55 unsigned srcx, unsigned srcy, unsigned width, unsigned height)
56 {
57 void *dst_map = pipe->screen->surface_map( pipe->screen,
58 dst,
59 PIPE_BUFFER_USAGE_CPU_WRITE );
60
61 const void *src_map = pipe->screen->surface_map( pipe->screen,
62 src,
63 PIPE_BUFFER_USAGE_CPU_READ );
64
65 assert(dst->block.size == src->block.size);
66 assert(dst->block.width == src->block.width);
67 assert(dst->block.height == src->block.height);
68 assert(src_map);
69 assert(dst_map);
70
71 /* If do_flip, invert src_y position and pass negative src stride */
72 pipe_copy_rect(dst_map,
73 &dst->block,
74 dst->stride,
75 dstx, dsty,
76 width, height,
77 src_map,
78 do_flip ? -(int) src->stride : src->stride,
79 srcx, srcy);
80
81 pipe->screen->surface_unmap(pipe->screen, src);
82 pipe->screen->surface_unmap(pipe->screen, dst);
83 }
84
85
86 static void *
87 get_pointer(struct pipe_surface *dst, void *dst_map, unsigned x, unsigned y)
88 {
89 return (char *)dst_map + y / dst->block.height * dst->stride + x / dst->block.width * dst->block.size;
90 }
91
92
93 #define UBYTE_TO_USHORT(B) ((B) | ((B) << 8))
94
95
96 /**
97 * Fill a rectangular sub-region. Need better logic about when to
98 * push buffers into AGP - will currently do so whenever possible.
99 */
100 static void
101 sp_surface_fill(struct pipe_context *pipe,
102 struct pipe_surface *dst,
103 unsigned dstx, unsigned dsty,
104 unsigned width, unsigned height, unsigned value)
105 {
106 unsigned i, j;
107 void *dst_map = pipe->screen->surface_map( pipe->screen,
108 dst,
109 PIPE_BUFFER_USAGE_CPU_WRITE );
110
111 assert(dst->stride > 0);
112
113
114 switch (dst->block.size) {
115 case 1:
116 case 2:
117 case 4:
118 pipe_fill_rect(dst_map, &dst->block, dst->stride, dstx, dsty, width, height, value);
119 break;
120 case 8:
121 {
122 /* expand the 4-byte clear value to an 8-byte value */
123 ushort *row = (ushort *) get_pointer(dst, dst_map, dstx, dsty);
124 ushort val0 = UBYTE_TO_USHORT((value >> 0) & 0xff);
125 ushort val1 = UBYTE_TO_USHORT((value >> 8) & 0xff);
126 ushort val2 = UBYTE_TO_USHORT((value >> 16) & 0xff);
127 ushort val3 = UBYTE_TO_USHORT((value >> 24) & 0xff);
128 val0 = (val0 << 8) | val0;
129 val1 = (val1 << 8) | val1;
130 val2 = (val2 << 8) | val2;
131 val3 = (val3 << 8) | val3;
132 for (i = 0; i < height; i++) {
133 for (j = 0; j < width; j++) {
134 row[j*4+0] = val0;
135 row[j*4+1] = val1;
136 row[j*4+2] = val2;
137 row[j*4+3] = val3;
138 }
139 row += dst->stride/2;
140 }
141 }
142 break;
143 default:
144 assert(0);
145 break;
146 }
147
148 pipe->screen->surface_unmap(pipe->screen, dst);
149 }
150
151
152 void
153 sp_init_surface_functions(struct softpipe_context *sp)
154 {
155 sp->pipe.surface_copy = sp_surface_copy;
156 sp->pipe.surface_fill = sp_surface_fill;
157 }