python: More efficient blits from surfaces.
[mesa.git] / src / gallium / state_trackers / python / p_texture.i
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 * @file
30 * SWIG interface definion for Gallium types.
31 *
32 * @author Jose Fonseca <jrfonseca@tungstengraphics.com>
33 */
34
35
36 %nodefaultctor pipe_texture;
37 %nodefaultctor pipe_surface;
38 %nodefaultctor st_buffer;
39
40 %nodefaultdtor pipe_texture;
41 %nodefaultdtor pipe_surface;
42 %nodefaultdtor st_buffer;
43
44 %ignore pipe_texture::screen;
45
46 %ignore pipe_surface::winsys;
47 %immutable pipe_surface::texture;
48 %immutable pipe_surface::buffer;
49
50 %newobject pipe_texture::get_surface;
51
52
53 %extend pipe_texture {
54
55 ~pipe_texture() {
56 struct pipe_texture *ptr = $self;
57 pipe_texture_reference(&ptr, NULL);
58 }
59
60 unsigned get_width(unsigned level=0) {
61 return $self->width[level];
62 }
63
64 unsigned get_height(unsigned level=0) {
65 return $self->height[level];
66 }
67
68 unsigned get_depth(unsigned level=0) {
69 return $self->depth[level];
70 }
71
72 unsigned get_nblocksx(unsigned level=0) {
73 return $self->nblocksx[level];
74 }
75
76 unsigned get_nblocksy(unsigned level=0) {
77 return $self->nblocksy[level];
78 }
79
80 /** Get a surface which is a "view" into a texture */
81 struct pipe_surface *
82 get_surface(unsigned face=0, unsigned level=0, unsigned zslice=0, unsigned usage=0 )
83 {
84 struct pipe_screen *screen = $self->screen;
85 return screen->get_tex_surface(screen, $self, face, level, zslice, usage);
86 }
87
88 };
89
90
91 %extend pipe_surface {
92
93 ~pipe_surface() {
94 struct pipe_surface *ptr = $self;
95 pipe_surface_reference(&ptr, NULL);
96 }
97
98 // gets mapped to pipe_surface_map automatically
99 void * map( unsigned flags );
100
101 // gets mapped to pipe_surface_unmap automatically
102 void unmap( void );
103
104 void
105 get_tile_raw(unsigned x, unsigned y, unsigned w, unsigned h, char *raw, unsigned stride) {
106 pipe_get_tile_raw($self, x, y, w, h, raw, stride);
107 }
108
109 void
110 put_tile_raw(unsigned x, unsigned y, unsigned w, unsigned h, const char *raw, unsigned stride) {
111 pipe_put_tile_raw($self, x, y, w, h, raw, stride);
112 }
113
114 void
115 get_tile_rgba(unsigned x, unsigned y, unsigned w, unsigned h, float *rgba) {
116 pipe_get_tile_rgba($self, x, y, w, h, rgba);
117 }
118
119 void
120 put_tile_rgba(unsigned x, unsigned y, unsigned w, unsigned h, const float *rgba) {
121 pipe_put_tile_rgba($self, x, y, w, h, rgba);
122 }
123
124 %cstring_output_allocate_size(char **STRING, int *LENGTH, free(*$1));
125 void
126 get_tile_rgba8(unsigned x, unsigned y, unsigned w, unsigned h, char **STRING, int *LENGTH)
127 {
128 unsigned surface_usage;
129 float *rgba;
130 unsigned char *rgba8;
131 unsigned i, j, k;
132
133 *LENGTH = 0;
134 *STRING = NULL;
135
136 if (!$self)
137 return;
138
139 *LENGTH = h*w*4;
140 *STRING = (char *) malloc(*LENGTH);
141 if(!*STRING)
142 return;
143
144 rgba = malloc(w*4*sizeof(float));
145 if(!rgba)
146 return;
147
148 rgba8 = (unsigned char *) *STRING;
149
150 /* XXX: force mappable surface */
151 surface_usage = $self->usage;
152 $self->usage |= PIPE_BUFFER_USAGE_CPU_READ;
153
154 for(j = 0; j < h; ++j) {
155 pipe_get_tile_rgba($self,
156 x, y + j, w, 1,
157 rgba);
158 for(i = 0; i < w; ++i)
159 for(k = 0; k <4; ++k)
160 rgba8[j*w*4 + i*4 + k] = float_to_ubyte(rgba[i*4 + k]);
161 }
162
163 $self->usage = surface_usage;
164
165 free(rgba);
166 }
167
168 void
169 get_tile_z(unsigned x, unsigned y, unsigned w, unsigned h, unsigned *z) {
170 pipe_get_tile_z($self, x, y, w, h, z);
171 }
172
173 void
174 put_tile_z(unsigned x, unsigned y, unsigned w, unsigned h, const unsigned *z) {
175 pipe_put_tile_z($self, x, y, w, h, z);
176 }
177
178 void
179 sample_rgba(float *rgba) {
180 st_sample_surface($self, rgba);
181 }
182
183 unsigned
184 compare_tile_rgba(unsigned x, unsigned y, unsigned w, unsigned h, const float *rgba, float tol = 0.0)
185 {
186 float *rgba2;
187 const float *p1;
188 const float *p2;
189 unsigned i, j, n;
190
191 rgba2 = MALLOC(h*w*4*sizeof(float));
192 if(!rgba2)
193 return ~0;
194
195 pipe_get_tile_rgba($self, x, y, w, h, rgba2);
196
197 p1 = rgba;
198 p2 = rgba2;
199 n = 0;
200 for(i = h*w; i; --i) {
201 unsigned differs = 0;
202 for(j = 4; j; --j) {
203 float delta = *p2++ - *p1++;
204 if (delta < -tol || delta > tol)
205 differs = 1;
206 }
207 n += differs;
208 }
209
210 FREE(rgba2);
211
212 return n;
213 }
214
215 };
216
217 struct st_buffer {
218 };
219
220 %extend st_buffer {
221
222 ~st_buffer() {
223 st_buffer_destroy($self);
224 }
225
226 unsigned __len__(void)
227 {
228 assert($self->buffer->refcount);
229 return $self->buffer->size;
230 }
231
232 %cstring_output_allocate_size(char **STRING, int *LENGTH, free(*$1));
233 void read(char **STRING, int *LENGTH)
234 {
235 struct pipe_screen *screen = $self->st_dev->screen;
236 const char *map;
237
238 assert($self->buffer->refcount);
239
240 *LENGTH = $self->buffer->size;
241 *STRING = (char *) malloc($self->buffer->size);
242 if(!*STRING)
243 return;
244
245 map = pipe_buffer_map(screen, $self->buffer, PIPE_BUFFER_USAGE_CPU_READ);
246 if(map) {
247 memcpy(*STRING, map, $self->buffer->size);
248 pipe_buffer_unmap(screen, $self->buffer);
249 }
250 }
251
252 %cstring_input_binary(const char *STRING, unsigned LENGTH);
253 void write(const char *STRING, unsigned LENGTH, unsigned offset = 0)
254 {
255 struct pipe_screen *screen = $self->st_dev->screen;
256 char *map;
257
258 assert($self->buffer->refcount);
259
260 if(offset > $self->buffer->size) {
261 PyErr_SetString(PyExc_ValueError, "offset must be smaller than buffer size");
262 return;
263 }
264
265 if(offset + LENGTH > $self->buffer->size) {
266 PyErr_SetString(PyExc_ValueError, "data length must fit inside the buffer");
267 return;
268 }
269
270 map = pipe_buffer_map(screen, $self->buffer, PIPE_BUFFER_USAGE_CPU_WRITE);
271 if(map) {
272 memcpy(map + offset, STRING, LENGTH);
273 pipe_buffer_unmap(screen, $self->buffer);
274 }
275 }
276 };