4
* An object oriented GL/GLES Abstraction/Utility Layer
6
* Copyright (C) 2007,2008,2009 Intel Corporation.
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.
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.
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/>.
24
* Matthew Allum <mallum@openedhand.com>
25
* Neil Roberts <neil@linux.intel.com>
26
* Robert Bragg <robert@linux.intel.com>
34
#include "cogl-internal.h"
35
#include "cogl-util.h"
36
#include "cogl-bitmap.h"
37
#include "cogl-bitmap-private.h"
38
#include "cogl-texture-private.h"
39
#include "cogl-material.h"
40
#include "cogl-context.h"
41
#include "cogl-handle.h"
42
#include "cogl-primitives.h"
48
#include "cogl-gles2-wrapper.h"
51
_cogl_texture_driver_gen (GLenum gl_target,
57
GE (glGenTextures (n, textures));
59
for (i = 0; i < n; i++)
61
GE (glBindTexture (gl_target, textures[i]));
66
/* GL_TEXTURE_MAG_FILTER defaults to GL_LINEAR, no need to set it */
67
GE( glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR) );
71
g_assert_not_reached();
78
_cogl_texture_driver_prep_gl_for_pixels_upload (int pixels_rowstride,
81
_cogl_texture_prep_gl_alignment_for_pixels_upload (pixels_rowstride);
85
_cogl_texture_driver_prep_gl_for_pixels_download (int pixels_rowstride,
88
_cogl_texture_prep_gl_alignment_for_pixels_download (pixels_rowstride);
92
_cogl_texture_driver_upload_subregion_to_gl (GLenum gl_target,
100
CoglBitmap *source_bmp,
101
GLuint source_gl_format,
102
GLuint source_gl_type)
104
int bpp = _cogl_get_format_bpp (source_bmp->format);
105
CoglBitmap slice_bmp;
107
/* NB: GLES doesn't support the GL_UNPACK_ROW_LENGTH, GL_UNPACK_SKIP_PIXELS
108
* or GL_UNPACK_SKIP_ROWS pixel store options so we can't directly source a
109
* sub-region from source_bmp, we need to use a transient bitmap instead. */
111
/* FIXME: optimize by not copying to intermediate slice bitmap when source
112
* rowstride = bpp * width and the texture image is not sliced */
114
/* Setup temp bitmap for slice subregion */
115
slice_bmp.format = source_bmp->format;
116
slice_bmp.width = width;
117
slice_bmp.height = height;
118
slice_bmp.rowstride = bpp * slice_bmp.width;
119
slice_bmp.data = g_malloc (slice_bmp.rowstride * slice_bmp.height);
121
/* Setup gl alignment to match rowstride and top-left corner */
122
_cogl_texture_driver_prep_gl_for_pixels_upload (slice_bmp.rowstride,
125
/* Copy subregion data */
126
_cogl_bitmap_copy_subregion (source_bmp,
134
GE( glBindTexture (gl_target, gl_handle) );
136
GE( glTexSubImage2D (gl_target, 0,
143
/* Free temp bitmap */
144
g_free (slice_bmp.data);
148
_cogl_texture_driver_upload_to_gl (GLenum gl_target,
150
CoglBitmap *source_bmp,
151
GLint internal_gl_format,
152
GLuint source_gl_format,
153
GLuint source_gl_type)
155
int bpp = _cogl_get_format_bpp (source_bmp->format);
156
CoglBitmap bmp = *source_bmp;
157
gboolean bmp_owner = FALSE;
159
/* If the rowstride can't be specified with just GL_ALIGNMENT alone
160
then we need to copy the bitmap because there is no GL_ROW_LENGTH */
161
if (source_bmp->rowstride / bpp != source_bmp->width)
163
bmp.rowstride = bpp * bmp.width;
164
bmp.data = g_malloc (bmp.rowstride * bmp.height);
167
_cogl_bitmap_copy_subregion (source_bmp,
174
/* Setup gl alignment to match rowstride and top-left corner */
175
_cogl_texture_driver_prep_gl_for_pixels_upload (bmp.rowstride,
178
GE( glBindTexture (gl_target, gl_handle) );
180
GE( glTexImage2D (gl_target, 0,
182
bmp.width, bmp.height,
192
/* NB: GLES doesn't support glGetTexImage2D, so cogl-texture will instead
193
* fallback to a generic render + readpixels approach to downloading
194
* texture data. (See _cogl_texture_draw_and_read() ) */
196
_cogl_texture_driver_gl_get_tex_image (GLenum gl_target,
197
GLenum dest_gl_format,
205
_cogl_texture_driver_size_supported (GLenum gl_target,
215
_cogl_texture_driver_try_setting_gl_border_color (
217
const GLfloat *transparent_color)
223
_cogl_pixel_format_from_gl_internal (GLenum gl_int_format,
224
CoglPixelFormat *out_format)
230
_cogl_pixel_format_to_gl (CoglPixelFormat format,
231
GLenum *out_glintformat,
232
GLenum *out_glformat,
235
CoglPixelFormat required_format;
236
GLenum glintformat = 0;
240
/* FIXME: check YUV support */
242
required_format = format;
244
/* Find GL equivalents */
245
switch (format & COGL_UNPREMULT_MASK)
247
case COGL_PIXEL_FORMAT_A_8:
248
glintformat = GL_ALPHA;
250
gltype = GL_UNSIGNED_BYTE;
252
case COGL_PIXEL_FORMAT_G_8:
253
glintformat = GL_LUMINANCE;
254
glformat = GL_LUMINANCE;
255
gltype = GL_UNSIGNED_BYTE;
258
/* Just one 24-bit ordering supported */
259
case COGL_PIXEL_FORMAT_RGB_888:
260
case COGL_PIXEL_FORMAT_BGR_888:
261
glintformat = GL_RGB;
263
gltype = GL_UNSIGNED_BYTE;
264
required_format = COGL_PIXEL_FORMAT_RGB_888;
267
/* Just one 32-bit ordering supported */
268
case COGL_PIXEL_FORMAT_RGBA_8888:
269
case COGL_PIXEL_FORMAT_BGRA_8888:
270
case COGL_PIXEL_FORMAT_ARGB_8888:
271
case COGL_PIXEL_FORMAT_ABGR_8888:
272
glintformat = GL_RGBA;
274
gltype = GL_UNSIGNED_BYTE;
275
required_format = COGL_PIXEL_FORMAT_RGBA_8888;
276
required_format |= (format & COGL_PREMULT_BIT);
279
/* The following three types of channel ordering
280
* are always defined using system word byte
281
* ordering (even according to GLES spec) */
282
case COGL_PIXEL_FORMAT_RGB_565:
283
glintformat = GL_RGB;
285
gltype = GL_UNSIGNED_SHORT_5_6_5;
287
case COGL_PIXEL_FORMAT_RGBA_4444:
288
glintformat = GL_RGBA;
290
gltype = GL_UNSIGNED_SHORT_4_4_4_4;
292
case COGL_PIXEL_FORMAT_RGBA_5551:
293
glintformat = GL_RGBA;
295
gltype = GL_UNSIGNED_SHORT_5_5_5_1;
298
/* FIXME: check extensions for YUV support */
303
if (out_glintformat != NULL)
304
*out_glintformat = glintformat;
305
if (out_glformat != NULL)
306
*out_glformat = glformat;
307
if (out_gltype != NULL)
308
*out_gltype = gltype;
310
return required_format;
314
_cogl_texture_driver_allows_foreign_gl_target (GLenum gl_target)
316
/* Allow 2-dimensional textures only */
317
if (gl_target != GL_TEXTURE_2D)
323
_cogl_texture_driver_gl_generate_mipmaps (GLenum gl_target)
325
GE( cogl_wrap_glGenerateMipmap (gl_target) );
329
_cogl_texture_driver_find_best_gl_get_data_format (
330
CoglPixelFormat format,
331
GLenum *closest_gl_format,
332
GLenum *closest_gl_type)
334
/* Find closest format that's supported by GL
335
(Can't use _cogl_pixel_format_to_gl since available formats
336
when reading pixels on GLES are severely limited) */
337
*closest_gl_format = GL_RGBA;
338
*closest_gl_type = GL_UNSIGNED_BYTE;
339
return COGL_PIXEL_FORMAT_RGBA_8888;