"Initial commit to Gerrit"
[profile/ivi/cogl.git] / cogl / cogl-texture-private.h
1 /*
2  * Cogl
3  *
4  * An object oriented GL/GLES Abstraction/Utility Layer
5  *
6  * Copyright (C) 2007,2008,2009 Intel Corporation.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
20  *
21  *
22  */
23
24 #ifndef __COGL_TEXTURE_PRIVATE_H
25 #define __COGL_TEXTURE_PRIVATE_H
26
27 #include "cogl-bitmap-private.h"
28 #include "cogl-handle.h"
29 #include "cogl-pipeline-private.h"
30 #include "cogl-spans.h"
31 #include "cogl-meta-texture.h"
32 #include "cogl-framebuffer.h"
33
34 typedef struct _CoglTextureVtable     CoglTextureVtable;
35
36 /* Encodes three possibiloities result of transforming a quad */
37 typedef enum {
38   /* quad doesn't cross the boundaries of a texture */
39   COGL_TRANSFORM_NO_REPEAT,
40   /* quad crosses boundaries, hardware wrap mode can handle */
41   COGL_TRANSFORM_HARDWARE_REPEAT,
42   /* quad crosses boundaries, needs software fallback;
43    * for a sliced texture, this might not actually involve
44    * repeating, just a quad crossing multiple slices */
45   COGL_TRANSFORM_SOFTWARE_REPEAT,
46 } CoglTransformResult;
47
48 /* Flags given to the pre_paint method */
49 typedef enum {
50   /* The texture is going to be used with filters that require
51      mipmapping. This gives the texture the opportunity to
52      automatically update the mipmap tree */
53   COGL_TEXTURE_NEEDS_MIPMAP = 1
54 } CoglTexturePrePaintFlags;
55
56 struct _CoglTextureVtable
57 {
58   /* Virtual functions that must be implemented for a texture
59      backend */
60
61   /* This should update the specified sub region of the texture with a
62      sub region of the given bitmap. The bitmap is not converted
63      before being passed so the implementation is expected to call
64      _cogl_texture_prepare_for_upload with a suitable destination
65      format before uploading */
66   gboolean (* set_region) (CoglTexture    *tex,
67                            int             src_x,
68                            int             src_y,
69                            int             dst_x,
70                            int             dst_y,
71                            unsigned int    dst_width,
72                            unsigned int    dst_height,
73                            CoglBitmap     *bitmap);
74
75   /* This should copy the image data of the texture into @data. The
76      requested format will have been first passed through
77      ctx->texture_driver->find_best_gl_get_data_format so it should
78      always be a format that is valid for GL (ie, no conversion should
79      be necessary). */
80   gboolean (* get_data) (CoglTexture     *tex,
81                          CoglPixelFormat  format,
82                          unsigned int     rowstride,
83                          guint8          *data);
84
85   void (* foreach_sub_texture_in_region) (CoglTexture *tex,
86                                           float virtual_tx_1,
87                                           float virtual_ty_1,
88                                           float virtual_tx_2,
89                                           float virtual_ty_2,
90                                           CoglMetaTextureCallback callback,
91                                           void *user_data);
92
93   int (* get_max_waste) (CoglTexture *tex);
94
95   gboolean (* is_sliced) (CoglTexture *tex);
96
97   gboolean (* can_hardware_repeat) (CoglTexture *tex);
98
99   void (* transform_coords_to_gl) (CoglTexture *tex,
100                                    float *s,
101                                    float *t);
102   CoglTransformResult (* transform_quad_coords_to_gl) (CoglTexture *tex,
103                                                        float *coords);
104
105   gboolean (* get_gl_texture) (CoglTexture *tex,
106                                GLuint *out_gl_handle,
107                                GLenum *out_gl_target);
108
109   void (* set_filters) (CoglTexture *tex,
110                         GLenum min_filter,
111                         GLenum mag_filter);
112
113   void (* pre_paint) (CoglTexture *tex, CoglTexturePrePaintFlags flags);
114   void (* ensure_non_quad_rendering) (CoglTexture *tex);
115
116   void (* set_wrap_mode_parameters) (CoglTexture *tex,
117                                      GLenum wrap_mode_s,
118                                      GLenum wrap_mode_t,
119                                      GLenum wrap_mode_p);
120
121   CoglPixelFormat (* get_format) (CoglTexture *tex);
122   GLenum (* get_gl_format) (CoglTexture *tex);
123   int (* get_width) (CoglTexture *tex);
124   int (* get_height) (CoglTexture *tex);
125
126   CoglTextureType (* get_type) (CoglTexture *tex);
127
128   gboolean (* is_foreign) (CoglTexture *tex);
129 };
130
131 struct _CoglTexture
132 {
133   CoglObject               _parent;
134   GList                   *framebuffers;
135   const CoglTextureVtable *vtable;
136 };
137
138 typedef enum _CoglTextureChangeFlags
139 {
140   /* Whenever the internals of a texture are changed such that the
141    * underlying GL textures that represent the CoglTexture change then
142    * we notify cogl-material.c via
143    * _cogl_pipeline_texture_pre_change_notify
144    */
145   COGL_TEXTURE_CHANGE_GL_TEXTURES
146
147 } CoglTextureChangeFlags;
148
149 typedef struct _CoglTexturePixel  CoglTexturePixel;
150
151 /* This is used by the texture backends to store the first pixel of
152    each GL texture. This is only used when glGenerateMipmap is not
153    available so that we can temporarily set GL_GENERATE_MIPMAP and
154    reupload a pixel */
155 struct _CoglTexturePixel
156 {
157   /* We need to store the format of the pixel because we store the
158      data in the source format which might end up being different for
159      each slice if a subregion is updated with a different format */
160   GLenum gl_format;
161   GLenum gl_type;
162   guint8 data[4];
163 };
164
165 void
166 _cogl_texture_init (CoglTexture *texture,
167                     const CoglTextureVtable *vtable);
168
169 void
170 _cogl_texture_free (CoglTexture *texture);
171
172 /* This is used to register a type to the list of handle types that
173    will be considered a texture in cogl_is_texture() */
174 void
175 _cogl_texture_register_texture_type (const CoglObjectClass *klass);
176
177 #define COGL_TEXTURE_DEFINE(TypeName, type_name)                        \
178   COGL_HANDLE_DEFINE_WITH_CODE                                          \
179   (TypeName, type_name,                                                 \
180    _cogl_texture_register_texture_type (&_cogl_##type_name##_class))
181
182 #define COGL_TEXTURE_INTERNAL_DEFINE(TypeName, type_name)               \
183   COGL_HANDLE_INTERNAL_DEFINE_WITH_CODE                                 \
184   (TypeName, type_name,                                                 \
185    _cogl_texture_register_texture_type (&_cogl_##type_name##_class))
186
187 gboolean
188 _cogl_texture_can_hardware_repeat (CoglTexture *texture);
189
190 void
191 _cogl_texture_transform_coords_to_gl (CoglTexture *texture,
192                                       float *s,
193                                       float *t);
194 CoglTransformResult
195 _cogl_texture_transform_quad_coords_to_gl (CoglTexture *texture,
196                                            float *coords);
197
198 GLenum
199 _cogl_texture_get_gl_format (CoglTexture *texture);
200
201 void
202 _cogl_texture_set_wrap_mode_parameters (CoglTexture *texture,
203                                         GLenum wrap_mode_s,
204                                         GLenum wrap_mode_t,
205                                         GLenum wrap_mode_p);
206
207
208 void
209 _cogl_texture_set_filters (CoglTexture *texture,
210                            GLenum min_filter,
211                            GLenum mag_filter);
212
213 void
214 _cogl_texture_pre_paint (CoglTexture *texture, CoglTexturePrePaintFlags flags);
215
216 void
217 _cogl_texture_ensure_non_quad_rendering (CoglTexture *texture);
218
219 /* Utility function to determine which pixel format to use when
220    dst_format is COGL_PIXEL_FORMAT_ANY. If dst_format is not ANY then
221    it will just be returned directly */
222 CoglPixelFormat
223 _cogl_texture_determine_internal_format (CoglPixelFormat src_format,
224                                          CoglPixelFormat dst_format);
225
226 /* Utility function to help uploading a bitmap. If the bitmap needs
227    premult conversion then it will be copied and *copied_bitmap will
228    be set to TRUE. Otherwise dst_bmp will be set to a shallow copy of
229    src_bmp. The GLenums needed for uploading are returned */
230
231 CoglBitmap *
232 _cogl_texture_prepare_for_upload (CoglBitmap      *src_bmp,
233                                   CoglPixelFormat  dst_format,
234                                   CoglPixelFormat *dst_format_out,
235                                   GLenum          *out_glintformat,
236                                   GLenum          *out_glformat,
237                                   GLenum          *out_gltype);
238
239 void
240 _cogl_texture_prep_gl_alignment_for_pixels_upload (int pixels_rowstride);
241
242 void
243 _cogl_texture_prep_gl_alignment_for_pixels_download (int pixels_rowstride);
244
245 /* Utility function to use as a fallback for getting the data of any
246    texture via the framebuffer */
247
248 gboolean
249 _cogl_texture_draw_and_read (CoglTexture *texture,
250                              CoglBitmap  *target_bmp,
251                              GLuint       target_gl_format,
252                              GLuint       target_gl_type);
253
254 gboolean
255 _cogl_texture_is_foreign (CoglTexture *texture);
256
257 void
258 _cogl_texture_associate_framebuffer (CoglTexture *texture,
259                                      CoglFramebuffer *framebuffer);
260
261 const GList *
262 _cogl_texture_get_associated_framebuffers (CoglTexture *texture);
263
264 void
265 _cogl_texture_flush_journal_rendering (CoglTexture *texture);
266
267 void
268 _cogl_texture_spans_foreach_in_region (CoglSpan *x_spans,
269                                        int n_x_spans,
270                                        CoglSpan *y_spans,
271                                        int n_y_spans,
272                                        CoglTexture **textures,
273                                        float *virtual_coords,
274                                        float x_normalize_factor,
275                                        float y_normalize_factor,
276                                        CoglPipelineWrapMode wrap_x,
277                                        CoglPipelineWrapMode wrap_y,
278                                        CoglMetaTextureCallback callback,
279                                        void *user_data);
280
281 /*
282  * _cogl_texture_get_type:
283  * @texture: a #CoglTexture pointer
284  *
285  * Retrieves the texture type of the underlying hardware texture that
286  * this #CoglTexture will use.
287  *
288  * Return value: The type of the hardware texture.
289  */
290 CoglTextureType
291 _cogl_texture_get_type (CoglTexture *texture);
292
293 #endif /* __COGL_TEXTURE_PRIVATE_H */