Squashed commit of the following:
[profile/ivi/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_resource;
37 %nodefaultctor st_surface;
38
39 %nodefaultdtor pipe_resource;
40 %nodefaultdtor st_surface;
41
42 %ignore pipe_resource::screen;
43
44 %immutable st_surface::texture;
45 %immutable st_surface::face;
46 %immutable st_surface::level;
47 %immutable st_surface::zslice;
48
49 %newobject pipe_resource::get_surface;
50
51 /* Avoid naming conflict with p_inlines.h's pipe_buffer_read/write */ 
52 %rename(read) read_; 
53 %rename(write) write_; 
54
55 %extend pipe_resource {
56
57    ~pipe_resource() {
58       struct pipe_resource *ptr = $self;
59       pipe_resource_reference(&ptr, NULL);
60    }
61
62    unsigned get_width(unsigned level=0) {
63       return u_minify($self->width0, level);
64    }
65
66    unsigned get_height(unsigned level=0) {
67       return u_minify($self->height0, level);
68    }
69
70    unsigned get_depth(unsigned level=0) {
71       return u_minify($self->depth0, level);
72    }
73
74    /** Get a surface which is a "view" into a texture */
75    struct st_surface *
76    get_surface(unsigned face=0, unsigned level=0, unsigned zslice=0)
77    {
78       struct st_surface *surface;
79
80       if(face >= ($self->target == PIPE_TEXTURE_CUBE ? 6U : 1U))
81          SWIG_exception(SWIG_ValueError, "face out of bounds");
82       if(level > $self->last_level)
83          SWIG_exception(SWIG_ValueError, "level out of bounds");
84       if(zslice >= u_minify($self->depth0, level))
85          SWIG_exception(SWIG_ValueError, "zslice out of bounds");
86
87       surface = CALLOC_STRUCT(st_surface);
88       if(!surface)
89          return NULL;
90
91       pipe_resource_reference(&surface->texture, $self);
92       surface->face = face;
93       surface->level = level;
94       surface->zslice = zslice;
95
96       return surface;
97
98    fail:
99       return NULL;
100    }
101
102    unsigned __len__(void) 
103    {
104       assert($self->target == PIPE_BUFFER);
105       assert(p_atomic_read(&$self->reference.count) > 0);
106       return $self->width0;
107    }
108
109    %cstring_output_allocate_size(char **STRING, int *LENGTH, free(*$1));
110    void read_(char **STRING, int *LENGTH)
111    {
112       struct pipe_screen *screen = $self->screen;
113       /* XXX need context here not screen */
114
115       assert($self->target == PIPE_BUFFER);
116       assert(p_atomic_read(&$self->reference.count) > 0);
117
118       *LENGTH = $self->width0;
119       *STRING = (char *) malloc($self->width0);
120       if(!*STRING)
121          return;
122
123       pipe_buffer_read(screen, $self, 0, $self->width0, *STRING);
124    }
125
126    %cstring_input_binary(const char *STRING, unsigned LENGTH);
127    void write_(const char *STRING, unsigned LENGTH, unsigned offset = 0) 
128    {
129       struct pipe_screen *screen = $self->screen;
130       /* XXX need context here not screen */
131
132       assert($self->target == PIPE_BUFFER);
133       assert(p_atomic_read(&$self->reference.count) > 0);
134
135       if(offset > $self->width0)
136          SWIG_exception(SWIG_ValueError, "offset must be smaller than buffer size");
137
138       if(offset + LENGTH > $self->width0)
139          SWIG_exception(SWIG_ValueError, "data length must fit inside the buffer");
140
141       pipe_buffer_write(screen, $self, offset, LENGTH, STRING);
142
143 fail:
144       return;
145    }
146
147
148 };
149
150 struct st_surface
151 {
152    %immutable;
153
154    struct pipe_resource *texture;
155    unsigned face;
156    unsigned level;
157    unsigned zslice;
158
159 };
160
161 %extend st_surface {
162
163    %immutable;
164
165    unsigned format;
166    unsigned width;
167    unsigned height;
168
169    ~st_surface() {
170       pipe_resource_reference(&$self->texture, NULL);
171       FREE($self);
172    }
173
174
175 };
176
177 %{
178    static enum pipe_format
179    st_surface_format_get(struct st_surface *surface)
180    {
181       return surface->texture->format;
182    }
183    
184    static unsigned
185    st_surface_width_get(struct st_surface *surface)
186    {
187       return u_minify(surface->texture->width0, surface->level);
188    }
189    
190    static unsigned
191    st_surface_height_get(struct st_surface *surface)
192    {
193       return u_minify(surface->texture->height0, surface->level);
194    }
195 %}