2008-04-25 09:37:36 -04:00
|
|
|
/*
|
2009-04-27 10:48:12 -04:00
|
|
|
* Cogl
|
2008-04-25 09:37:36 -04:00
|
|
|
*
|
2009-04-27 10:48:12 -04:00
|
|
|
* An object oriented GL/GLES Abstraction/Utility Layer
|
2008-04-25 09:37:36 -04:00
|
|
|
*
|
2009-04-27 10:48:12 -04:00
|
|
|
* Copyright (C) 2007,2008,2009 Intel Corporation.
|
2008-04-25 09:37:36 -04:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the
|
|
|
|
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 02111-1307, USA.
|
2009-04-27 10:48:12 -04:00
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Matthew Allum <mallum@openedhand.com>
|
|
|
|
* Neil Roberts <neil@linux.intel.com>
|
|
|
|
* Robert Bragg <robert@linux.intel.com>
|
2008-04-25 09:37:36 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "cogl.h"
|
|
|
|
#include "cogl-internal.h"
|
|
|
|
#include "cogl-util.h"
|
|
|
|
#include "cogl-bitmap.h"
|
2009-04-30 13:00:22 -04:00
|
|
|
#include "cogl-bitmap-private.h"
|
2008-10-30 13:25:00 -04:00
|
|
|
#include "cogl-texture-private.h"
|
Adds a CoglMaterial abstraction, which includes support for multi-texturing
My previous work to provide muti-texturing support has been extended into
a CoglMaterial abstraction that adds control over the texture combine
functions (controlling how multiple texture layers are blended together),
the gl blend function (used for blending the final primitive with the
framebuffer), the alpha function (used to discard fragments based on
their alpha channel), describing attributes such as a diffuse, ambient and
specular color (for use with the standard OpenGL lighting model), and
per layer rotations. (utilizing the new CoglMatrix utility API)
For now the only way this abstraction is exposed is via a new
cogl_material_rectangle function, that is similar to cogl_texture_rectangle
but doesn't take a texture handle (the source material is pulled from
the context), and the array of texture coordinates is extended to be able
to supply coordinates for each layer.
Note: this function doesn't support sliced textures; supporting sliced
textures is a non trivial problem, considering the ability to rotate layers.
Note: cogl_material_rectangle, has quite a few workarounds, for a number of
other limitations within Cogl a.t.m.
Note: The GLES1/2 multi-texturing support has yet to be updated to use
the material abstraction.
2008-12-11 15:11:30 -05:00
|
|
|
#include "cogl-material.h"
|
2008-04-25 09:37:36 -04:00
|
|
|
#include "cogl-context.h"
|
2008-04-29 12:10:37 -04:00
|
|
|
#include "cogl-handle.h"
|
[cogl] Improving Cogl journal to minimize driver overheads + GPU state changes
Previously the journal was always flushed at the end of
_cogl_rectangles_with_multitexture_coords, (i.e. the end of any
cogl_rectangle* calls) but now we have broadened the potential for batching
geometry. In ideal circumstances we will only flush once per scene.
In summary the journal works like this:
When you use any of the cogl_rectangle* APIs then nothing is emitted to the
GPU at this point, we just log one or more quads into the journal. A
journal entry consists of the quad coordinates, an associated material
reference, and a modelview matrix. Ideally the journal only gets flushed
once at the end of a scene, but in fact there are things to consider that
may cause unwanted flushing, including:
- modifying materials mid-scene
This is because each quad in the journal has an associated material
reference (i.e. not copy), so if you try and modify a material that is
already referenced in the journal we force a flush first)
NOTE: For now this means you should avoid using cogl_set_source_color()
since that currently uses a single shared material. Later we
should change it to use a pool of materials that is recycled
when the journal is flushed.
- modifying any state that isn't currently logged, such as depth, fog and
backface culling enables.
The first thing that happens when flushing, is to upload all the vertex data
associated with the journal into a single VBO.
We then go through a process of splitting up the journal into batches that
have compatible state so they can be emitted to the GPU together. This is
currently broken up into 3 levels so we can stagger the state changes:
1) we break the journal up according to changes in the number of material layers
associated with logged quads. The number of layers in a material determines
the stride of the associated vertices, so we have to update our vertex
array offsets at this level. (i.e. calling gl{Vertex,Color},Pointer etc)
2) we further split batches up according to material compatability. (e.g.
materials with different textures) We flush material state at this level.
3) Finally we split batches up according to modelview changes. At this level
we update the modelview matrix and actually emit the actual draw command.
This commit is largely about putting the initial design in-place; this will be
followed by other changes that take advantage of the extended batching.
2009-06-17 13:46:42 -04:00
|
|
|
#include "cogl-primitives.h"
|
2008-04-25 09:37:36 -04:00
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
2009-01-20 11:20:54 -05:00
|
|
|
#include <math.h>
|
2008-04-25 09:37:36 -04:00
|
|
|
|
2009-01-26 06:07:35 -05:00
|
|
|
#ifdef HAVE_COGL_GL
|
2009-01-28 09:09:51 -05:00
|
|
|
|
2009-01-26 06:07:35 -05:00
|
|
|
#define glDrawRangeElements ctx->pf_glDrawRangeElements
|
2009-02-16 07:42:08 -05:00
|
|
|
#define glActiveTexture ctx->pf_glActiveTexture
|
|
|
|
#define glClientActiveTexture ctx->pf_glClientActiveTexture
|
2009-06-04 11:04:57 -04:00
|
|
|
#define glGenerateMipmap ctx->pf_glGenerateMipmapEXT
|
2009-01-28 09:09:51 -05:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
/* GLES doesn't have glDrawRangeElements, so we simply pretend it does
|
|
|
|
* but that it makes no use of the start, end constraints: */
|
|
|
|
#define glDrawRangeElements(mode, start, end, count, type, indices) \
|
|
|
|
glDrawElements (mode, count, type, indices)
|
|
|
|
|
2009-01-26 06:07:35 -05:00
|
|
|
#endif
|
|
|
|
|
2008-12-11 10:33:38 -05:00
|
|
|
static void _cogl_texture_free (CoglTexture *tex);
|
|
|
|
|
2009-04-01 12:16:44 -04:00
|
|
|
COGL_HANDLE_DEFINE (Texture, texture);
|
2008-12-11 10:33:38 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
static void
|
|
|
|
_cogl_texture_bitmap_free (CoglTexture *tex)
|
|
|
|
{
|
|
|
|
if (tex->bitmap.data != NULL && tex->bitmap_owner)
|
|
|
|
g_free (tex->bitmap.data);
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
tex->bitmap.data = NULL;
|
|
|
|
tex->bitmap_owner = FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
_cogl_texture_bitmap_swap (CoglTexture *tex,
|
|
|
|
CoglBitmap *new_bitmap)
|
|
|
|
{
|
|
|
|
if (tex->bitmap.data != NULL && tex->bitmap_owner)
|
|
|
|
g_free (tex->bitmap.data);
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
tex->bitmap = *new_bitmap;
|
|
|
|
tex->bitmap_owner = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
_cogl_span_iter_update (CoglSpanIter *iter)
|
|
|
|
{
|
|
|
|
/* Pick current span */
|
|
|
|
iter->span = &g_array_index (iter->array,
|
|
|
|
CoglTexSliceSpan,
|
|
|
|
iter->index);
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
/* Offset next position by span size */
|
2008-11-18 11:24:09 -05:00
|
|
|
iter->next_pos = iter->pos +
|
2009-01-20 11:20:54 -05:00
|
|
|
(float)(iter->span->size - iter->span->waste);
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
/* Check if span intersects the area to cover */
|
|
|
|
if (iter->next_pos <= iter->cover_start ||
|
|
|
|
iter->pos >= iter->cover_end)
|
|
|
|
{
|
|
|
|
/* Intersection undefined */
|
|
|
|
iter->intersects = FALSE;
|
|
|
|
return;
|
|
|
|
}
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
iter->intersects = TRUE;
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
/* Clip start position to coverage area */
|
|
|
|
if (iter->pos < iter->cover_start)
|
|
|
|
iter->intersect_start = iter->cover_start;
|
|
|
|
else
|
|
|
|
iter->intersect_start = iter->pos;
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
/* Clip end position to coverage area */
|
|
|
|
if (iter->next_pos > iter->cover_end)
|
|
|
|
iter->intersect_end = iter->cover_end;
|
|
|
|
else
|
|
|
|
iter->intersect_end = iter->next_pos;
|
|
|
|
}
|
|
|
|
|
2009-03-23 08:29:15 -04:00
|
|
|
void
|
2008-04-25 09:37:36 -04:00
|
|
|
_cogl_span_iter_begin (CoglSpanIter *iter,
|
|
|
|
GArray *array,
|
2009-01-20 11:20:54 -05:00
|
|
|
float origin,
|
|
|
|
float cover_start,
|
|
|
|
float cover_end)
|
2008-04-25 09:37:36 -04:00
|
|
|
{
|
|
|
|
/* Copy info */
|
|
|
|
iter->index = 0;
|
|
|
|
iter->array = array;
|
|
|
|
iter->span = NULL;
|
|
|
|
iter->origin = origin;
|
|
|
|
iter->cover_start = cover_start;
|
|
|
|
iter->cover_end = cover_end;
|
|
|
|
iter->pos = iter->origin;
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
/* Update intersection */
|
|
|
|
_cogl_span_iter_update (iter);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
_cogl_span_iter_next (CoglSpanIter *iter)
|
|
|
|
{
|
|
|
|
/* Move current position */
|
|
|
|
iter->pos = iter->next_pos;
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
/* Pick next slice (wrap when last reached) */
|
|
|
|
iter->index = (iter->index + 1) % iter->array->len;
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
/* Update intersection */
|
|
|
|
_cogl_span_iter_update (iter);
|
|
|
|
}
|
|
|
|
|
2009-03-23 08:29:15 -04:00
|
|
|
gboolean
|
2008-04-25 09:37:36 -04:00
|
|
|
_cogl_span_iter_end (CoglSpanIter *iter)
|
|
|
|
{
|
|
|
|
/* End reached when whole area covered */
|
|
|
|
return iter->pos >= iter->cover_end;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2008-11-18 11:24:09 -05:00
|
|
|
prep_for_gl_pixels_upload (gint pixels_rowstride,
|
|
|
|
gint pixels_src_x,
|
|
|
|
gint pixels_src_y,
|
|
|
|
gint pixels_bpp)
|
2008-04-25 09:37:36 -04:00
|
|
|
{
|
2008-11-18 11:24:09 -05:00
|
|
|
GE( glPixelStorei (GL_UNPACK_ROW_LENGTH, pixels_rowstride / pixels_bpp) );
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-11-18 11:24:09 -05:00
|
|
|
GE( glPixelStorei (GL_UNPACK_SKIP_PIXELS, pixels_src_x) );
|
|
|
|
GE( glPixelStorei (GL_UNPACK_SKIP_ROWS, pixels_src_y) );
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-11-18 11:24:09 -05:00
|
|
|
if (!(pixels_rowstride & 0x7))
|
|
|
|
GE( glPixelStorei (GL_UNPACK_ALIGNMENT, 8) );
|
|
|
|
else if (!(pixels_rowstride & 0x3))
|
|
|
|
GE( glPixelStorei (GL_UNPACK_ALIGNMENT, 4) );
|
|
|
|
else if (!(pixels_rowstride & 0x1))
|
|
|
|
GE( glPixelStorei (GL_UNPACK_ALIGNMENT, 2) );
|
|
|
|
else
|
|
|
|
GE( glPixelStorei (GL_UNPACK_ALIGNMENT, 1) );
|
|
|
|
}
|
2008-04-25 09:37:36 -04:00
|
|
|
|
2008-11-18 11:24:09 -05:00
|
|
|
static void
|
2009-08-24 07:50:57 -04:00
|
|
|
prep_for_gl_pixels_download (gint pixels_rowstride,
|
|
|
|
gint pixels_src_x,
|
|
|
|
gint pixels_src_y,
|
|
|
|
gint pixels_bpp)
|
2008-11-18 11:24:09 -05:00
|
|
|
{
|
2009-08-24 07:50:57 -04:00
|
|
|
GE( glPixelStorei (GL_PACK_ROW_LENGTH, pixels_rowstride / pixels_bpp) );
|
|
|
|
|
|
|
|
GE( glPixelStorei (GL_PACK_SKIP_PIXELS, pixels_src_x) );
|
|
|
|
GE( glPixelStorei (GL_PACK_SKIP_ROWS, pixels_src_y) );
|
|
|
|
|
2008-11-18 11:24:09 -05:00
|
|
|
if (!(pixels_rowstride & 0x7))
|
|
|
|
GE( glPixelStorei (GL_PACK_ALIGNMENT, 8) );
|
|
|
|
else if (!(pixels_rowstride & 0x3))
|
|
|
|
GE( glPixelStorei (GL_PACK_ALIGNMENT, 4) );
|
|
|
|
else if (!(pixels_rowstride & 0x1))
|
|
|
|
GE( glPixelStorei (GL_PACK_ALIGNMENT, 2) );
|
2008-04-25 09:37:36 -04:00
|
|
|
else
|
2008-11-18 11:24:09 -05:00
|
|
|
GE( glPixelStorei (GL_PACK_ALIGNMENT, 1) );
|
2008-04-25 09:37:36 -04:00
|
|
|
}
|
|
|
|
|
2008-10-27 08:39:22 -04:00
|
|
|
static guchar *
|
|
|
|
_cogl_texture_allocate_waste_buffer (CoglTexture *tex)
|
|
|
|
{
|
|
|
|
CoglTexSliceSpan *last_x_span;
|
|
|
|
CoglTexSliceSpan *last_y_span;
|
|
|
|
guchar *waste_buf = NULL;
|
|
|
|
|
|
|
|
/* If the texture has any waste then allocate a buffer big enough to
|
|
|
|
fill the gaps */
|
|
|
|
last_x_span = &g_array_index (tex->slice_x_spans, CoglTexSliceSpan,
|
|
|
|
tex->slice_x_spans->len - 1);
|
|
|
|
last_y_span = &g_array_index (tex->slice_y_spans, CoglTexSliceSpan,
|
|
|
|
tex->slice_y_spans->len - 1);
|
|
|
|
if (last_x_span->waste > 0 || last_y_span->waste > 0)
|
|
|
|
{
|
|
|
|
gint bpp = _cogl_get_format_bpp (tex->bitmap.format);
|
|
|
|
CoglTexSliceSpan *first_x_span
|
|
|
|
= &g_array_index (tex->slice_x_spans, CoglTexSliceSpan, 0);
|
|
|
|
CoglTexSliceSpan *first_y_span
|
|
|
|
= &g_array_index (tex->slice_y_spans, CoglTexSliceSpan, 0);
|
|
|
|
guint right_size = first_y_span->size * last_x_span->waste;
|
|
|
|
guint bottom_size = first_x_span->size * last_y_span->waste;
|
|
|
|
|
|
|
|
waste_buf = g_malloc (MAX (right_size, bottom_size) * bpp);
|
|
|
|
}
|
|
|
|
|
|
|
|
return waste_buf;
|
|
|
|
}
|
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
static gboolean
|
|
|
|
_cogl_texture_upload_to_gl (CoglTexture *tex)
|
|
|
|
{
|
|
|
|
CoglTexSliceSpan *x_span;
|
|
|
|
CoglTexSliceSpan *y_span;
|
|
|
|
GLuint gl_handle;
|
|
|
|
gint bpp;
|
|
|
|
gint x,y;
|
2008-10-27 08:39:22 -04:00
|
|
|
guchar *waste_buf;
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
bpp = _cogl_get_format_bpp (tex->bitmap.format);
|
2008-10-27 08:39:22 -04:00
|
|
|
|
|
|
|
waste_buf = _cogl_texture_allocate_waste_buffer (tex);
|
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
/* Iterate vertical slices */
|
|
|
|
for (y = 0; y < tex->slice_y_spans->len; ++y)
|
|
|
|
{
|
|
|
|
y_span = &g_array_index (tex->slice_y_spans, CoglTexSliceSpan, y);
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
/* Iterate horizontal slices */
|
|
|
|
for (x = 0; x < tex->slice_x_spans->len; ++x)
|
|
|
|
{
|
2009-06-04 11:04:57 -04:00
|
|
|
gint slice_num = y * tex->slice_x_spans->len + x;
|
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
x_span = &g_array_index (tex->slice_x_spans, CoglTexSliceSpan, x);
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
/* Pick the gl texture object handle */
|
2009-06-04 11:04:57 -04:00
|
|
|
gl_handle = g_array_index (tex->slice_gl_handles, GLuint, slice_num);
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
/* Setup gl alignment to match rowstride and top-left corner */
|
2008-11-18 11:24:09 -05:00
|
|
|
prep_for_gl_pixels_upload (tex->bitmap.rowstride,
|
|
|
|
x_span->start,
|
|
|
|
y_span->start,
|
|
|
|
bpp);
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2009-06-04 11:04:57 -04:00
|
|
|
/* Keep a copy of the first pixel if needed */
|
|
|
|
if (tex->first_pixels)
|
|
|
|
{
|
|
|
|
memcpy (tex->first_pixels[slice_num].data,
|
|
|
|
tex->bitmap.data + x_span->start * bpp
|
|
|
|
+ y_span->start * tex->bitmap.rowstride,
|
|
|
|
bpp);
|
|
|
|
tex->first_pixels[slice_num].gl_format = tex->gl_format;
|
|
|
|
tex->first_pixels[slice_num].gl_type = tex->gl_type;
|
|
|
|
}
|
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
/* Upload new image data */
|
|
|
|
GE( glBindTexture (tex->gl_target, gl_handle) );
|
2008-11-18 11:24:09 -05:00
|
|
|
|
|
|
|
GE( glTexSubImage2D (tex->gl_target, 0,
|
|
|
|
0,
|
|
|
|
0,
|
2008-04-25 09:37:36 -04:00
|
|
|
x_span->size - x_span->waste,
|
|
|
|
y_span->size - y_span->waste,
|
|
|
|
tex->gl_format, tex->gl_type,
|
|
|
|
tex->bitmap.data) );
|
2008-10-27 08:39:22 -04:00
|
|
|
|
|
|
|
/* Fill the waste with a copies of the rightmost pixels */
|
|
|
|
if (x_span->waste > 0)
|
|
|
|
{
|
|
|
|
const guchar *src = tex->bitmap.data
|
|
|
|
+ y_span->start * tex->bitmap.rowstride
|
|
|
|
+ (x_span->start + x_span->size - x_span->waste - 1) * bpp;
|
|
|
|
guchar *dst = waste_buf;
|
|
|
|
guint wx, wy;
|
|
|
|
|
|
|
|
for (wy = 0; wy < y_span->size - y_span->waste; wy++)
|
|
|
|
{
|
|
|
|
for (wx = 0; wx < x_span->waste; wx++)
|
|
|
|
{
|
|
|
|
memcpy (dst, src, bpp);
|
|
|
|
dst += bpp;
|
|
|
|
}
|
|
|
|
src += tex->bitmap.rowstride;
|
|
|
|
}
|
|
|
|
|
2008-11-18 11:24:09 -05:00
|
|
|
prep_for_gl_pixels_upload (x_span->waste * bpp,
|
|
|
|
0, /* src x */
|
|
|
|
0, /* src y */
|
|
|
|
bpp);
|
2008-10-27 08:39:22 -04:00
|
|
|
|
|
|
|
GE( glTexSubImage2D (tex->gl_target, 0,
|
2008-11-18 11:24:09 -05:00
|
|
|
x_span->size - x_span->waste,
|
|
|
|
0,
|
2008-10-27 08:39:22 -04:00
|
|
|
x_span->waste,
|
|
|
|
y_span->size - y_span->waste,
|
|
|
|
tex->gl_format, tex->gl_type,
|
|
|
|
waste_buf) );
|
|
|
|
}
|
|
|
|
|
|
|
|
if (y_span->waste > 0)
|
|
|
|
{
|
|
|
|
const guchar *src = tex->bitmap.data
|
|
|
|
+ ((y_span->start + y_span->size - y_span->waste - 1)
|
|
|
|
* tex->bitmap.rowstride)
|
|
|
|
+ x_span->start * bpp;
|
|
|
|
guchar *dst = waste_buf;
|
|
|
|
guint wy, wx;
|
|
|
|
|
|
|
|
for (wy = 0; wy < y_span->waste; wy++)
|
|
|
|
{
|
|
|
|
memcpy (dst, src, (x_span->size - x_span->waste) * bpp);
|
|
|
|
dst += (x_span->size - x_span->waste) * bpp;
|
|
|
|
|
|
|
|
for (wx = 0; wx < x_span->waste; wx++)
|
|
|
|
{
|
|
|
|
memcpy (dst, dst - bpp, bpp);
|
|
|
|
dst += bpp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-18 11:24:09 -05:00
|
|
|
prep_for_gl_pixels_upload (x_span->size * bpp,
|
|
|
|
0, /* src x */
|
|
|
|
0, /* src y */
|
|
|
|
bpp);
|
2008-10-27 08:39:22 -04:00
|
|
|
|
|
|
|
GE( glTexSubImage2D (tex->gl_target, 0,
|
2008-11-18 11:24:09 -05:00
|
|
|
0,
|
|
|
|
y_span->size - y_span->waste,
|
2008-10-27 08:39:22 -04:00
|
|
|
x_span->size,
|
|
|
|
y_span->waste,
|
|
|
|
tex->gl_format, tex->gl_type,
|
|
|
|
waste_buf) );
|
|
|
|
}
|
2008-04-25 09:37:36 -04:00
|
|
|
}
|
|
|
|
}
|
2008-10-27 08:39:22 -04:00
|
|
|
|
|
|
|
if (waste_buf)
|
|
|
|
g_free (waste_buf);
|
|
|
|
|
2009-06-04 11:04:57 -04:00
|
|
|
tex->mipmaps_dirty = TRUE;
|
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
_cogl_texture_download_from_gl (CoglTexture *tex,
|
|
|
|
CoglBitmap *target_bmp,
|
|
|
|
GLuint target_gl_format,
|
|
|
|
GLuint target_gl_type)
|
|
|
|
{
|
|
|
|
CoglTexSliceSpan *x_span;
|
|
|
|
CoglTexSliceSpan *y_span;
|
|
|
|
GLuint gl_handle;
|
|
|
|
gint bpp;
|
|
|
|
gint x,y;
|
|
|
|
CoglBitmap slice_bmp;
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
bpp = _cogl_get_format_bpp (target_bmp->format);
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
/* Iterate vertical slices */
|
|
|
|
for (y = 0; y < tex->slice_y_spans->len; ++y)
|
|
|
|
{
|
|
|
|
y_span = &g_array_index (tex->slice_y_spans, CoglTexSliceSpan, y);
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
/* Iterate horizontal slices */
|
|
|
|
for (x = 0; x < tex->slice_x_spans->len; ++x)
|
|
|
|
{
|
|
|
|
/*if (x != 0 || y != 1) continue;*/
|
|
|
|
x_span = &g_array_index (tex->slice_x_spans, CoglTexSliceSpan, x);
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
/* Pick the gl texture object handle */
|
|
|
|
gl_handle = g_array_index (tex->slice_gl_handles, GLuint,
|
|
|
|
y * tex->slice_x_spans->len + x);
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
/* If there's any waste we need to copy manually
|
|
|
|
(no glGetTexSubImage) */
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
if (y_span->waste != 0 || x_span->waste != 0)
|
|
|
|
{
|
|
|
|
/* Setup temp bitmap for slice subregion */
|
2009-06-19 11:39:37 -04:00
|
|
|
slice_bmp.format = target_bmp->format;
|
2008-04-25 09:37:36 -04:00
|
|
|
slice_bmp.width = x_span->size;
|
|
|
|
slice_bmp.height = y_span->size;
|
|
|
|
slice_bmp.rowstride = bpp * slice_bmp.width;
|
|
|
|
slice_bmp.data = (guchar*) g_malloc (slice_bmp.rowstride *
|
2008-11-18 11:24:09 -05:00
|
|
|
slice_bmp.height);
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
/* Setup gl alignment to 0,0 top-left corner */
|
2009-08-24 07:50:57 -04:00
|
|
|
prep_for_gl_pixels_download (slice_bmp.rowstride, 0, 0, bpp);
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
/* Download slice image data into temp bmp */
|
|
|
|
GE( glBindTexture (tex->gl_target, gl_handle) );
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-11-18 11:24:09 -05:00
|
|
|
GE (glGetTexImage (tex->gl_target,
|
|
|
|
0, /* level */
|
2008-04-25 09:37:36 -04:00
|
|
|
target_gl_format,
|
|
|
|
target_gl_type,
|
|
|
|
slice_bmp.data) );
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
/* Copy portion of slice from temp to target bmp */
|
|
|
|
_cogl_bitmap_copy_subregion (&slice_bmp,
|
|
|
|
target_bmp,
|
|
|
|
0, 0,
|
|
|
|
x_span->start,
|
|
|
|
y_span->start,
|
|
|
|
x_span->size - x_span->waste,
|
|
|
|
y_span->size - y_span->waste);
|
|
|
|
/* Free temp bitmap */
|
|
|
|
g_free (slice_bmp.data);
|
|
|
|
}
|
|
|
|
else
|
2008-11-18 11:24:09 -05:00
|
|
|
{
|
|
|
|
GLvoid *dst = target_bmp->data
|
|
|
|
+ x_span->start * bpp
|
|
|
|
+ y_span->start * target_bmp->rowstride;
|
|
|
|
|
2009-08-24 07:50:57 -04:00
|
|
|
prep_for_gl_pixels_download (target_bmp->rowstride, 0, 0, bpp);
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
/* Download slice image data */
|
|
|
|
GE( glBindTexture (tex->gl_target, gl_handle) );
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-11-18 11:24:09 -05:00
|
|
|
GE( glGetTexImage (tex->gl_target,
|
|
|
|
0, /* level */
|
2008-04-25 09:37:36 -04:00
|
|
|
target_gl_format,
|
|
|
|
target_gl_type,
|
2008-11-18 11:24:09 -05:00
|
|
|
dst) );
|
2008-04-25 09:37:36 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
_cogl_texture_upload_subregion_to_gl (CoglTexture *tex,
|
|
|
|
gint src_x,
|
|
|
|
gint src_y,
|
|
|
|
gint dst_x,
|
|
|
|
gint dst_y,
|
|
|
|
gint width,
|
|
|
|
gint height,
|
|
|
|
CoglBitmap *source_bmp,
|
|
|
|
GLuint source_gl_format,
|
|
|
|
GLuint source_gl_type)
|
|
|
|
{
|
2008-10-27 08:39:22 -04:00
|
|
|
CoglTexSliceSpan *x_span;
|
|
|
|
CoglTexSliceSpan *y_span;
|
|
|
|
gint bpp;
|
|
|
|
CoglSpanIter x_iter;
|
|
|
|
CoglSpanIter y_iter;
|
|
|
|
GLuint gl_handle;
|
|
|
|
gint source_x = 0, source_y = 0;
|
|
|
|
gint inter_w = 0, inter_h = 0;
|
|
|
|
gint local_x = 0, local_y = 0;
|
|
|
|
guchar *waste_buf;
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
bpp = _cogl_get_format_bpp (source_bmp->format);
|
2008-11-18 11:24:09 -05:00
|
|
|
|
2008-10-27 08:39:22 -04:00
|
|
|
waste_buf = _cogl_texture_allocate_waste_buffer (tex);
|
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
/* Iterate vertical spans */
|
|
|
|
for (source_y = src_y,
|
|
|
|
_cogl_span_iter_begin (&y_iter, tex->slice_y_spans,
|
2009-01-20 11:20:54 -05:00
|
|
|
0, (float)(dst_y),
|
|
|
|
(float)(dst_y + height));
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
!_cogl_span_iter_end (&y_iter);
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
_cogl_span_iter_next (&y_iter),
|
|
|
|
source_y += inter_h )
|
|
|
|
{
|
2008-10-20 11:54:17 -04:00
|
|
|
/* Discard slices out of the subregion early */
|
|
|
|
if (!y_iter.intersects)
|
|
|
|
{
|
|
|
|
inter_h = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2008-10-27 08:39:22 -04:00
|
|
|
y_span = &g_array_index (tex->slice_y_spans, CoglTexSliceSpan,
|
|
|
|
y_iter.index);
|
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
/* Iterate horizontal spans */
|
|
|
|
for (source_x = src_x,
|
|
|
|
_cogl_span_iter_begin (&x_iter, tex->slice_x_spans,
|
2009-01-20 11:20:54 -05:00
|
|
|
0, (float)(dst_x),
|
|
|
|
(float)(dst_x + width));
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
!_cogl_span_iter_end (&x_iter);
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
_cogl_span_iter_next (&x_iter),
|
|
|
|
source_x += inter_w )
|
|
|
|
{
|
2009-06-04 11:04:57 -04:00
|
|
|
gint slice_num;
|
|
|
|
|
2008-10-20 11:54:17 -04:00
|
|
|
/* Discard slices out of the subregion early */
|
|
|
|
if (!x_iter.intersects)
|
|
|
|
{
|
|
|
|
inter_w = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2008-10-27 08:39:22 -04:00
|
|
|
x_span = &g_array_index (tex->slice_x_spans, CoglTexSliceSpan,
|
|
|
|
x_iter.index);
|
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
/* Pick intersection width and height */
|
2009-01-20 11:20:54 -05:00
|
|
|
inter_w = (x_iter.intersect_end -
|
2008-10-30 12:37:55 -04:00
|
|
|
x_iter.intersect_start);
|
2009-01-20 11:20:54 -05:00
|
|
|
inter_h = (y_iter.intersect_end -
|
2008-10-30 12:37:55 -04:00
|
|
|
y_iter.intersect_start);
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
/* Localize intersection top-left corner to slice*/
|
2009-01-20 11:20:54 -05:00
|
|
|
local_x = (x_iter.intersect_start -
|
2008-10-30 12:37:55 -04:00
|
|
|
x_iter.pos);
|
2009-01-20 11:20:54 -05:00
|
|
|
local_y = (y_iter.intersect_start -
|
2008-10-30 12:37:55 -04:00
|
|
|
y_iter.pos);
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2009-06-04 11:04:57 -04:00
|
|
|
slice_num = y_iter.index * tex->slice_x_spans->len + x_iter.index;
|
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
/* Pick slice GL handle */
|
2009-06-04 11:04:57 -04:00
|
|
|
gl_handle = g_array_index (tex->slice_gl_handles, GLuint, slice_num);
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
/* Setup gl alignment to match rowstride and top-left corner */
|
2008-11-18 11:24:09 -05:00
|
|
|
prep_for_gl_pixels_upload (source_bmp->rowstride,
|
|
|
|
source_x,
|
|
|
|
source_y,
|
|
|
|
bpp);
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2009-06-04 11:04:57 -04:00
|
|
|
/* Keep a copy of the first pixel if needed */
|
|
|
|
if (tex->first_pixels && local_x == 0 && local_y == 0)
|
|
|
|
{
|
|
|
|
memcpy (tex->first_pixels[slice_num].data,
|
|
|
|
source_bmp->data + source_x * bpp
|
|
|
|
+ source_y * source_bmp->rowstride,
|
|
|
|
bpp);
|
|
|
|
tex->first_pixels[slice_num].gl_format = source_gl_format;
|
|
|
|
tex->first_pixels[slice_num].gl_type = source_gl_type;
|
|
|
|
}
|
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
/* Upload new image data */
|
|
|
|
GE( glBindTexture (tex->gl_target, gl_handle) );
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
GE( glTexSubImage2D (tex->gl_target, 0,
|
|
|
|
local_x, local_y,
|
|
|
|
inter_w, inter_h,
|
|
|
|
source_gl_format,
|
|
|
|
source_gl_type,
|
|
|
|
source_bmp->data) );
|
2008-10-27 08:39:22 -04:00
|
|
|
|
|
|
|
/* If the x_span is sliced and the upload touches the
|
|
|
|
rightmost pixels then fill the waste with copies of the
|
|
|
|
pixels */
|
|
|
|
if (x_span->waste > 0
|
|
|
|
&& local_x < x_span->size - x_span->waste
|
|
|
|
&& local_x + inter_w >= x_span->size - x_span->waste)
|
|
|
|
{
|
2008-10-30 12:37:55 -04:00
|
|
|
const guchar *src;
|
|
|
|
guchar *dst;
|
2008-10-27 08:39:22 -04:00
|
|
|
guint wx, wy;
|
|
|
|
|
2008-10-30 12:37:55 -04:00
|
|
|
src = source_bmp->data
|
2009-01-20 11:20:54 -05:00
|
|
|
+ (src_y + ((int)y_iter.intersect_start)
|
2008-10-30 12:37:55 -04:00
|
|
|
- dst_y)
|
|
|
|
* source_bmp->rowstride
|
|
|
|
+ (src_x + x_span->start + x_span->size - x_span->waste
|
|
|
|
- dst_x - 1)
|
|
|
|
* bpp;
|
|
|
|
|
|
|
|
dst = waste_buf;
|
|
|
|
|
2008-10-27 08:39:22 -04:00
|
|
|
for (wy = 0; wy < inter_h; wy++)
|
|
|
|
{
|
|
|
|
for (wx = 0; wx < x_span->waste; wx++)
|
|
|
|
{
|
|
|
|
memcpy (dst, src, bpp);
|
|
|
|
dst += bpp;
|
|
|
|
}
|
|
|
|
src += source_bmp->rowstride;
|
|
|
|
}
|
|
|
|
|
2008-11-18 11:24:09 -05:00
|
|
|
prep_for_gl_pixels_upload (x_span->waste * bpp,
|
|
|
|
0, /* src x */
|
|
|
|
0, /* src y */
|
|
|
|
bpp);
|
2008-10-27 08:39:22 -04:00
|
|
|
|
|
|
|
GE( glTexSubImage2D (tex->gl_target, 0,
|
2008-11-18 11:24:09 -05:00
|
|
|
x_span->size - x_span->waste,
|
|
|
|
local_y,
|
2008-10-27 08:39:22 -04:00
|
|
|
x_span->waste,
|
|
|
|
inter_h,
|
|
|
|
source_gl_format,
|
|
|
|
source_gl_type,
|
|
|
|
waste_buf) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* same for the bottom-most pixels */
|
|
|
|
if (y_span->waste > 0
|
|
|
|
&& local_y < y_span->size - y_span->waste
|
|
|
|
&& local_y + inter_h >= y_span->size - y_span->waste)
|
|
|
|
{
|
2008-10-30 12:37:55 -04:00
|
|
|
const guchar *src;
|
|
|
|
guchar *dst;
|
2008-10-27 08:39:22 -04:00
|
|
|
guint wy, wx;
|
|
|
|
guint copy_width;
|
|
|
|
|
2008-10-30 12:37:55 -04:00
|
|
|
src = source_bmp->data
|
2009-01-20 11:20:54 -05:00
|
|
|
+ (src_x + ((int)x_iter.intersect_start)
|
2008-10-30 12:37:55 -04:00
|
|
|
- dst_x)
|
|
|
|
* bpp
|
|
|
|
+ (src_y + y_span->start + y_span->size - y_span->waste
|
|
|
|
- dst_y - 1)
|
|
|
|
* source_bmp->rowstride;
|
|
|
|
|
|
|
|
dst = waste_buf;
|
|
|
|
|
2008-10-27 08:39:22 -04:00
|
|
|
if (local_x + inter_w >= x_span->size - x_span->waste)
|
|
|
|
copy_width = x_span->size - local_x;
|
|
|
|
else
|
|
|
|
copy_width = inter_w;
|
|
|
|
|
|
|
|
for (wy = 0; wy < y_span->waste; wy++)
|
|
|
|
{
|
|
|
|
memcpy (dst, src, inter_w * bpp);
|
|
|
|
dst += inter_w * bpp;
|
|
|
|
|
|
|
|
for (wx = inter_w; wx < copy_width; wx++)
|
|
|
|
{
|
|
|
|
memcpy (dst, dst - bpp, bpp);
|
|
|
|
dst += bpp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-18 11:24:09 -05:00
|
|
|
prep_for_gl_pixels_upload (copy_width * bpp,
|
|
|
|
0, /* src x */
|
|
|
|
0, /* src y */
|
|
|
|
bpp);
|
2008-10-27 08:39:22 -04:00
|
|
|
|
|
|
|
GE( glTexSubImage2D (tex->gl_target, 0,
|
2008-11-18 11:24:09 -05:00
|
|
|
local_x,
|
|
|
|
y_span->size - y_span->waste,
|
2008-10-27 08:39:22 -04:00
|
|
|
copy_width,
|
|
|
|
y_span->waste,
|
|
|
|
source_gl_format,
|
|
|
|
source_gl_type,
|
|
|
|
waste_buf) );
|
|
|
|
}
|
2008-04-25 09:37:36 -04:00
|
|
|
}
|
|
|
|
}
|
2008-10-27 08:39:22 -04:00
|
|
|
|
|
|
|
if (waste_buf)
|
|
|
|
g_free (waste_buf);
|
|
|
|
|
2009-06-04 11:04:57 -04:00
|
|
|
tex->mipmaps_dirty = TRUE;
|
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gint
|
[cogl] Remove max_waste argument from Texture ctors
The CoglTexture constructors expose the "max-waste" argument for
controlling the maximum amount of wasted areas for slicing or,
if set to -1, disables slicing.
Slicing is really relevant only for large images that are never
repeated, so it's a useful feature only in controlled use cases.
Specifying the amount of wasted area is, on the other hand, just
a way to mess up this feature; 99% the times, you either pull this
number out of thin air, hoping it's right, or you try to do the
right thing and you choose the wrong number anyway.
Instead, we can use the CoglTextureFlags to control whether the
texture should not be sliced (useful for Clutter-GST and for the
texture-from-pixmap actors) and provide a reasonable value for
enabling the slicing ourself. At some point, we might even
provide a way to change the default at compile time or at run time,
for particular platforms.
Since max_waste is gone, the :tile-waste property of ClutterTexture
becomes read-only, and it proxies the cogl_texture_get_max_waste()
function.
Inside Clutter, the only cases where the max_waste argument was
not set to -1 are in the Pango glyph cache (which is a POT texture
anyway) and inside the test cases where we want to force slicing;
for the latter we can create larger textures that will be bigger than
the threshold we set.
Signed-off-by: Emmanuele Bassi <ebassi@linux.intel.com>
Signed-off-by: Robert Bragg <robert@linux.intel.com>
Signed-off-by: Neil Roberts <neil@linux.intel.com>
2009-05-23 14:18:18 -04:00
|
|
|
_cogl_rect_slices_for_size (gint size_to_fill,
|
|
|
|
gint max_span_size,
|
|
|
|
gint max_waste,
|
|
|
|
GArray *out_spans)
|
2008-04-25 09:37:36 -04:00
|
|
|
{
|
|
|
|
gint n_spans = 0;
|
|
|
|
CoglTexSliceSpan span;
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
/* Init first slice span */
|
|
|
|
span.start = 0;
|
|
|
|
span.size = max_span_size;
|
|
|
|
span.waste = 0;
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
/* Repeat until whole area covered */
|
|
|
|
while (size_to_fill >= span.size)
|
|
|
|
{
|
|
|
|
/* Add another slice span of same size */
|
|
|
|
if (out_spans) g_array_append_val (out_spans, span);
|
|
|
|
span.start += span.size;
|
|
|
|
size_to_fill -= span.size;
|
|
|
|
n_spans++;
|
|
|
|
}
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
/* Add one last smaller slice span */
|
|
|
|
if (size_to_fill > 0)
|
|
|
|
{
|
|
|
|
span.size = size_to_fill;
|
|
|
|
if (out_spans) g_array_append_val (out_spans, span);
|
|
|
|
n_spans++;
|
|
|
|
}
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
return n_spans;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gint
|
[cogl] Remove max_waste argument from Texture ctors
The CoglTexture constructors expose the "max-waste" argument for
controlling the maximum amount of wasted areas for slicing or,
if set to -1, disables slicing.
Slicing is really relevant only for large images that are never
repeated, so it's a useful feature only in controlled use cases.
Specifying the amount of wasted area is, on the other hand, just
a way to mess up this feature; 99% the times, you either pull this
number out of thin air, hoping it's right, or you try to do the
right thing and you choose the wrong number anyway.
Instead, we can use the CoglTextureFlags to control whether the
texture should not be sliced (useful for Clutter-GST and for the
texture-from-pixmap actors) and provide a reasonable value for
enabling the slicing ourself. At some point, we might even
provide a way to change the default at compile time or at run time,
for particular platforms.
Since max_waste is gone, the :tile-waste property of ClutterTexture
becomes read-only, and it proxies the cogl_texture_get_max_waste()
function.
Inside Clutter, the only cases where the max_waste argument was
not set to -1 are in the Pango glyph cache (which is a POT texture
anyway) and inside the test cases where we want to force slicing;
for the latter we can create larger textures that will be bigger than
the threshold we set.
Signed-off-by: Emmanuele Bassi <ebassi@linux.intel.com>
Signed-off-by: Robert Bragg <robert@linux.intel.com>
Signed-off-by: Neil Roberts <neil@linux.intel.com>
2009-05-23 14:18:18 -04:00
|
|
|
_cogl_pot_slices_for_size (gint size_to_fill,
|
|
|
|
gint max_span_size,
|
|
|
|
gint max_waste,
|
|
|
|
GArray *out_spans)
|
2008-04-25 09:37:36 -04:00
|
|
|
{
|
|
|
|
gint n_spans = 0;
|
|
|
|
CoglTexSliceSpan span;
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
/* Init first slice span */
|
|
|
|
span.start = 0;
|
|
|
|
span.size = max_span_size;
|
|
|
|
span.waste = 0;
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
/* Fix invalid max_waste */
|
[cogl] Remove max_waste argument from Texture ctors
The CoglTexture constructors expose the "max-waste" argument for
controlling the maximum amount of wasted areas for slicing or,
if set to -1, disables slicing.
Slicing is really relevant only for large images that are never
repeated, so it's a useful feature only in controlled use cases.
Specifying the amount of wasted area is, on the other hand, just
a way to mess up this feature; 99% the times, you either pull this
number out of thin air, hoping it's right, or you try to do the
right thing and you choose the wrong number anyway.
Instead, we can use the CoglTextureFlags to control whether the
texture should not be sliced (useful for Clutter-GST and for the
texture-from-pixmap actors) and provide a reasonable value for
enabling the slicing ourself. At some point, we might even
provide a way to change the default at compile time or at run time,
for particular platforms.
Since max_waste is gone, the :tile-waste property of ClutterTexture
becomes read-only, and it proxies the cogl_texture_get_max_waste()
function.
Inside Clutter, the only cases where the max_waste argument was
not set to -1 are in the Pango glyph cache (which is a POT texture
anyway) and inside the test cases where we want to force slicing;
for the latter we can create larger textures that will be bigger than
the threshold we set.
Signed-off-by: Emmanuele Bassi <ebassi@linux.intel.com>
Signed-off-by: Robert Bragg <robert@linux.intel.com>
Signed-off-by: Neil Roberts <neil@linux.intel.com>
2009-05-23 14:18:18 -04:00
|
|
|
if (max_waste < 0)
|
|
|
|
max_waste = 0;
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
while (TRUE)
|
|
|
|
{
|
|
|
|
/* Is the whole area covered? */
|
|
|
|
if (size_to_fill > span.size)
|
|
|
|
{
|
|
|
|
/* Not yet - add a span of this size */
|
|
|
|
if (out_spans) g_array_append_val (out_spans, span);
|
|
|
|
span.start += span.size;
|
|
|
|
size_to_fill -= span.size;
|
|
|
|
n_spans++;
|
|
|
|
}
|
|
|
|
else if (span.size - size_to_fill <= max_waste)
|
|
|
|
{
|
|
|
|
/* Yes and waste is small enough */
|
|
|
|
span.waste = span.size - size_to_fill;
|
|
|
|
if (out_spans) g_array_append_val (out_spans, span);
|
|
|
|
return ++n_spans;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Yes but waste is too large */
|
|
|
|
while (span.size - size_to_fill > max_waste)
|
|
|
|
{
|
|
|
|
span.size /= 2;
|
|
|
|
g_assert (span.size > 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
/* Can't get here */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
_cogl_texture_size_supported (GLenum gl_target,
|
|
|
|
GLenum gl_format,
|
|
|
|
GLenum gl_type,
|
|
|
|
int width,
|
|
|
|
int height)
|
|
|
|
{
|
2008-11-18 11:24:09 -05:00
|
|
|
if (gl_target == GL_TEXTURE_2D)
|
2008-04-25 09:37:36 -04:00
|
|
|
{
|
|
|
|
/* Proxy texture allows for a quick check for supported size */
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
GLint new_width = 0;
|
|
|
|
|
|
|
|
GE( glTexImage2D (GL_PROXY_TEXTURE_2D, 0, GL_RGBA,
|
|
|
|
width, height, 0 /* border */,
|
|
|
|
gl_format, gl_type, NULL) );
|
|
|
|
|
|
|
|
GE( glGetTexLevelParameteriv (GL_PROXY_TEXTURE_2D, 0,
|
|
|
|
GL_TEXTURE_WIDTH, &new_width) );
|
|
|
|
|
|
|
|
return new_width != 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* not used */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-23 08:29:15 -04:00
|
|
|
void
|
2008-12-01 11:27:54 -05:00
|
|
|
_cogl_texture_set_wrap_mode_parameter (CoglTexture *tex,
|
|
|
|
GLenum wrap_mode)
|
|
|
|
{
|
|
|
|
/* Only set the wrap mode if it's different from the current
|
|
|
|
value to avoid too many GL calls */
|
|
|
|
if (tex->wrap_mode != wrap_mode)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
Fully integrates CoglMaterial throughout the rest of Cogl
This glues CoglMaterial in as the fundamental way that Cogl describes how to
fill in geometry.
It adds cogl_set_source (), which is used to set the material which will be
used by all subsequent drawing functions
It adds cogl_set_source_texture as a convenience for setting up a default
material with a single texture layer, and cogl_set_source_color is now also
a convenience for setting up a material with a solid fill.
"drawing functions" include, cogl_rectangle, cogl_texture_rectangle,
cogl_texture_multiple_rectangles, cogl_texture_polygon (though the
cogl_texture_* funcs have been renamed; see below for details),
cogl_path_fill/stroke and cogl_vertex_buffer_draw*.
cogl_texture_rectangle, cogl_texture_multiple_rectangles and
cogl_texture_polygon no longer take a texture handle; instead the current
source material is referenced. The functions have also been renamed to:
cogl_rectangle_with_texture_coords, cogl_rectangles_with_texture_coords
and cogl_polygon respectivly.
Most code that previously did:
cogl_texture_rectangle (tex_handle, x, y,...);
needs to be changed to now do:
cogl_set_source_texture (tex_handle);
cogl_rectangle_with_texture_coords (x, y,....);
In the less likely case where you were blending your source texture with a color
like:
cogl_set_source_color4ub (r,g,b,a); /* where r,g,b,a isn't just white */
cogl_texture_rectangle (tex_handle, x, y,...);
you will need your own material to do that:
mat = cogl_material_new ();
cogl_material_set_color4ub (r,g,b,a);
cogl_material_set_layer (mat, 0, tex_handle));
cogl_set_source_material (mat);
Code that uses the texture coordinates, 0, 0, 1, 1 don't need to use
cog_rectangle_with_texure_coords since these are the coordinates that
cogl_rectangle will use.
For cogl_texture_polygon; as well as dropping the texture handle, the
n_vertices and vertices arguments were transposed for consistency. So
code previously written as:
cogl_texture_polygon (tex_handle, 3, verts, TRUE);
need to be written as:
cogl_set_source_texture (tex_handle);
cogl_polygon (verts, 3, TRUE);
All of the unit tests have been updated to now use the material API and
test-cogl-material has been renamed to test-cogl-multitexture since any
textured quad is now technically a test of CoglMaterial but this test
specifically creates a material with multiple texture layers.
Note: The GLES backend has not been updated yet; that will be done in a
following commit.
2009-01-23 11:15:40 -05:00
|
|
|
/* Any queued texture rectangles may be depending on the previous
|
|
|
|
* wrap mode... */
|
|
|
|
_cogl_journal_flush ();
|
|
|
|
|
2008-12-01 11:27:54 -05:00
|
|
|
for (i = 0; i < tex->slice_gl_handles->len; i++)
|
|
|
|
{
|
|
|
|
GLuint texnum = g_array_index (tex->slice_gl_handles, GLuint, i);
|
|
|
|
|
|
|
|
GE( glBindTexture (tex->gl_target, texnum) );
|
|
|
|
GE( glTexParameteri (tex->gl_target, GL_TEXTURE_WRAP_S, wrap_mode) );
|
|
|
|
GE( glTexParameteri (tex->gl_target, GL_TEXTURE_WRAP_T, wrap_mode) );
|
|
|
|
}
|
|
|
|
|
|
|
|
tex->wrap_mode = wrap_mode;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
static gboolean
|
|
|
|
_cogl_texture_slices_create (CoglTexture *tex)
|
2008-12-23 11:29:29 -05:00
|
|
|
{
|
2008-04-25 09:37:36 -04:00
|
|
|
gint bpp;
|
|
|
|
gint max_width;
|
|
|
|
gint max_height;
|
|
|
|
GLuint *gl_handles;
|
|
|
|
gint n_x_slices;
|
|
|
|
gint n_y_slices;
|
|
|
|
gint n_slices;
|
|
|
|
gint x, y;
|
|
|
|
CoglTexSliceSpan *x_span;
|
|
|
|
CoglTexSliceSpan *y_span;
|
|
|
|
const GLfloat transparent_color[4] = { 0x00, 0x00, 0x00, 0x00 };
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
gint (*slices_for_size) (gint, gint, gint, GArray*);
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
bpp = _cogl_get_format_bpp (tex->bitmap.format);
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-11-18 11:24:09 -05:00
|
|
|
/* Initialize size of largest slice according to supported features */
|
2008-04-25 09:37:36 -04:00
|
|
|
if (cogl_features_available (COGL_FEATURE_TEXTURE_NPOT))
|
|
|
|
{
|
|
|
|
max_width = tex->bitmap.width;
|
|
|
|
max_height = tex->bitmap.height;
|
|
|
|
tex->gl_target = GL_TEXTURE_2D;
|
|
|
|
slices_for_size = _cogl_rect_slices_for_size;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
max_width = cogl_util_next_p2 (tex->bitmap.width);
|
|
|
|
max_height = cogl_util_next_p2 (tex->bitmap.height);
|
|
|
|
tex->gl_target = GL_TEXTURE_2D;
|
|
|
|
slices_for_size = _cogl_pot_slices_for_size;
|
|
|
|
}
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
/* Negative number means no slicing forced by the user */
|
|
|
|
if (tex->max_waste <= -1)
|
|
|
|
{
|
|
|
|
CoglTexSliceSpan span;
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
/* Check if size supported else bail out */
|
|
|
|
if (!_cogl_texture_size_supported (tex->gl_target,
|
[cogl] Remove max_waste argument from Texture ctors
The CoglTexture constructors expose the "max-waste" argument for
controlling the maximum amount of wasted areas for slicing or,
if set to -1, disables slicing.
Slicing is really relevant only for large images that are never
repeated, so it's a useful feature only in controlled use cases.
Specifying the amount of wasted area is, on the other hand, just
a way to mess up this feature; 99% the times, you either pull this
number out of thin air, hoping it's right, or you try to do the
right thing and you choose the wrong number anyway.
Instead, we can use the CoglTextureFlags to control whether the
texture should not be sliced (useful for Clutter-GST and for the
texture-from-pixmap actors) and provide a reasonable value for
enabling the slicing ourself. At some point, we might even
provide a way to change the default at compile time or at run time,
for particular platforms.
Since max_waste is gone, the :tile-waste property of ClutterTexture
becomes read-only, and it proxies the cogl_texture_get_max_waste()
function.
Inside Clutter, the only cases where the max_waste argument was
not set to -1 are in the Pango glyph cache (which is a POT texture
anyway) and inside the test cases where we want to force slicing;
for the latter we can create larger textures that will be bigger than
the threshold we set.
Signed-off-by: Emmanuele Bassi <ebassi@linux.intel.com>
Signed-off-by: Robert Bragg <robert@linux.intel.com>
Signed-off-by: Neil Roberts <neil@linux.intel.com>
2009-05-23 14:18:18 -04:00
|
|
|
tex->gl_format,
|
|
|
|
tex->gl_type,
|
|
|
|
max_width,
|
|
|
|
max_height))
|
2008-04-25 09:37:36 -04:00
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
n_x_slices = 1;
|
|
|
|
n_y_slices = 1;
|
2008-12-23 11:29:29 -05:00
|
|
|
|
|
|
|
/* Init span arrays */
|
2008-04-25 09:37:36 -04:00
|
|
|
tex->slice_x_spans = g_array_sized_new (FALSE, FALSE,
|
|
|
|
sizeof (CoglTexSliceSpan),
|
|
|
|
1);
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
tex->slice_y_spans = g_array_sized_new (FALSE, FALSE,
|
|
|
|
sizeof (CoglTexSliceSpan),
|
|
|
|
1);
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
/* Add a single span for width and height */
|
|
|
|
span.start = 0;
|
|
|
|
span.size = max_width;
|
|
|
|
span.waste = max_width - tex->bitmap.width;
|
|
|
|
g_array_append_val (tex->slice_x_spans, span);
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
span.size = max_height;
|
|
|
|
span.waste = max_height - tex->bitmap.height;
|
|
|
|
g_array_append_val (tex->slice_y_spans, span);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Decrease the size of largest slice until supported by GL */
|
|
|
|
while (!_cogl_texture_size_supported (tex->gl_target,
|
|
|
|
tex->gl_format,
|
|
|
|
tex->gl_type,
|
|
|
|
max_width,
|
|
|
|
max_height))
|
|
|
|
{
|
|
|
|
/* Alternate between width and height */
|
|
|
|
if (max_width > max_height)
|
|
|
|
max_width /= 2;
|
|
|
|
else
|
|
|
|
max_height /= 2;
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
if (max_width == 0 || max_height == 0)
|
|
|
|
return FALSE;
|
|
|
|
}
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
/* Determine the slices required to cover the bitmap area */
|
|
|
|
n_x_slices = slices_for_size (tex->bitmap.width,
|
|
|
|
max_width, tex->max_waste,
|
|
|
|
NULL);
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
n_y_slices = slices_for_size (tex->bitmap.height,
|
|
|
|
max_height, tex->max_waste,
|
|
|
|
NULL);
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
/* Init span arrays with reserved size */
|
|
|
|
tex->slice_x_spans = g_array_sized_new (FALSE, FALSE,
|
|
|
|
sizeof (CoglTexSliceSpan),
|
|
|
|
n_x_slices);
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
tex->slice_y_spans = g_array_sized_new (FALSE, FALSE,
|
|
|
|
sizeof (CoglTexSliceSpan),
|
|
|
|
n_y_slices);
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
/* Fill span arrays with info */
|
|
|
|
slices_for_size (tex->bitmap.width,
|
|
|
|
max_width, tex->max_waste,
|
|
|
|
tex->slice_x_spans);
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
slices_for_size (tex->bitmap.height,
|
|
|
|
max_height, tex->max_waste,
|
|
|
|
tex->slice_y_spans);
|
|
|
|
}
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
/* Init and resize GL handle array */
|
|
|
|
n_slices = n_x_slices * n_y_slices;
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
tex->slice_gl_handles = g_array_sized_new (FALSE, FALSE,
|
|
|
|
sizeof (GLuint),
|
|
|
|
n_slices);
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
g_array_set_size (tex->slice_gl_handles, n_slices);
|
2008-12-01 11:27:54 -05:00
|
|
|
|
2009-06-04 11:04:57 -04:00
|
|
|
/* Allocate some space to store a copy of the first pixel of each
|
|
|
|
slice. This is only needed to glGenerateMipmap (which is part of
|
|
|
|
the FBO extension) is not available */
|
|
|
|
if (cogl_features_available (COGL_FEATURE_OFFSCREEN))
|
|
|
|
tex->first_pixels = NULL;
|
|
|
|
else
|
|
|
|
tex->first_pixels = g_new (CoglTexturePixel, n_slices);
|
|
|
|
|
2008-12-01 11:27:54 -05:00
|
|
|
/* Wrap mode not yet set */
|
|
|
|
tex->wrap_mode = GL_FALSE;
|
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
/* Generate a "working set" of GL texture objects
|
|
|
|
* (some implementations might supported faster
|
|
|
|
* re-binding between textures inside a set) */
|
|
|
|
gl_handles = (GLuint*) tex->slice_gl_handles->data;
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
GE( glGenTextures (n_slices, gl_handles) );
|
2008-12-23 11:29:29 -05:00
|
|
|
|
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
/* Init each GL texture object */
|
|
|
|
for (y = 0; y < n_y_slices; ++y)
|
|
|
|
{
|
|
|
|
y_span = &g_array_index (tex->slice_y_spans, CoglTexSliceSpan, y);
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
for (x = 0; x < n_x_slices; ++x)
|
|
|
|
{
|
|
|
|
x_span = &g_array_index (tex->slice_x_spans, CoglTexSliceSpan, x);
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2009-05-19 09:44:29 -04:00
|
|
|
COGL_NOTE (TEXTURE, "CREATE SLICE (%d,%d)\tsize (%d,%d)",
|
|
|
|
x, y,
|
|
|
|
x_span->size - x_span->waste,
|
|
|
|
y_span->size - y_span->waste);
|
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
/* Setup texture parameters */
|
2008-11-18 11:24:09 -05:00
|
|
|
GE( glBindTexture (tex->gl_target,
|
|
|
|
gl_handles[y * n_x_slices + x]) );
|
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
/* Use a transparent border color so that we can leave the
|
|
|
|
color buffer alone when using texture co-ordinates
|
|
|
|
outside of the texture */
|
|
|
|
GE( glTexParameterfv (tex->gl_target, GL_TEXTURE_BORDER_COLOR,
|
|
|
|
transparent_color) );
|
|
|
|
|
|
|
|
/* Pass NULL data to init size and internal format */
|
|
|
|
GE( glTexImage2D (tex->gl_target, 0, tex->gl_intformat,
|
|
|
|
x_span->size, y_span->size, 0,
|
|
|
|
tex->gl_format, tex->gl_type, 0) );
|
|
|
|
}
|
|
|
|
}
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
_cogl_texture_slices_free (CoglTexture *tex)
|
2008-12-23 11:29:29 -05:00
|
|
|
{
|
2008-04-25 09:37:36 -04:00
|
|
|
if (tex->slice_x_spans != NULL)
|
|
|
|
g_array_free (tex->slice_x_spans, TRUE);
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
if (tex->slice_y_spans != NULL)
|
|
|
|
g_array_free (tex->slice_y_spans, TRUE);
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
if (tex->slice_gl_handles != NULL)
|
|
|
|
{
|
|
|
|
if (tex->is_foreign == FALSE)
|
|
|
|
{
|
|
|
|
GE( glDeleteTextures (tex->slice_gl_handles->len,
|
|
|
|
(GLuint*) tex->slice_gl_handles->data) );
|
|
|
|
}
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
g_array_free (tex->slice_gl_handles, TRUE);
|
|
|
|
}
|
2009-06-04 11:04:57 -04:00
|
|
|
|
|
|
|
if (tex->first_pixels != NULL)
|
|
|
|
g_free (tex->first_pixels);
|
2008-04-25 09:37:36 -04:00
|
|
|
}
|
|
|
|
|
Fully integrates CoglMaterial throughout the rest of Cogl
This glues CoglMaterial in as the fundamental way that Cogl describes how to
fill in geometry.
It adds cogl_set_source (), which is used to set the material which will be
used by all subsequent drawing functions
It adds cogl_set_source_texture as a convenience for setting up a default
material with a single texture layer, and cogl_set_source_color is now also
a convenience for setting up a material with a solid fill.
"drawing functions" include, cogl_rectangle, cogl_texture_rectangle,
cogl_texture_multiple_rectangles, cogl_texture_polygon (though the
cogl_texture_* funcs have been renamed; see below for details),
cogl_path_fill/stroke and cogl_vertex_buffer_draw*.
cogl_texture_rectangle, cogl_texture_multiple_rectangles and
cogl_texture_polygon no longer take a texture handle; instead the current
source material is referenced. The functions have also been renamed to:
cogl_rectangle_with_texture_coords, cogl_rectangles_with_texture_coords
and cogl_polygon respectivly.
Most code that previously did:
cogl_texture_rectangle (tex_handle, x, y,...);
needs to be changed to now do:
cogl_set_source_texture (tex_handle);
cogl_rectangle_with_texture_coords (x, y,....);
In the less likely case where you were blending your source texture with a color
like:
cogl_set_source_color4ub (r,g,b,a); /* where r,g,b,a isn't just white */
cogl_texture_rectangle (tex_handle, x, y,...);
you will need your own material to do that:
mat = cogl_material_new ();
cogl_material_set_color4ub (r,g,b,a);
cogl_material_set_layer (mat, 0, tex_handle));
cogl_set_source_material (mat);
Code that uses the texture coordinates, 0, 0, 1, 1 don't need to use
cog_rectangle_with_texure_coords since these are the coordinates that
cogl_rectangle will use.
For cogl_texture_polygon; as well as dropping the texture handle, the
n_vertices and vertices arguments were transposed for consistency. So
code previously written as:
cogl_texture_polygon (tex_handle, 3, verts, TRUE);
need to be written as:
cogl_set_source_texture (tex_handle);
cogl_polygon (verts, 3, TRUE);
All of the unit tests have been updated to now use the material API and
test-cogl-material has been renamed to test-cogl-multitexture since any
textured quad is now technically a test of CoglMaterial but this test
specifically creates a material with multiple texture layers.
Note: The GLES backend has not been updated yet; that will be done in a
following commit.
2009-01-23 11:15:40 -05:00
|
|
|
gboolean
|
|
|
|
_cogl_texture_span_has_waste (CoglTexture *tex,
|
|
|
|
gint x_span_index,
|
|
|
|
gint y_span_index)
|
|
|
|
{
|
|
|
|
CoglTexSliceSpan *x_span;
|
|
|
|
CoglTexSliceSpan *y_span;
|
|
|
|
|
|
|
|
x_span = &g_array_index (tex->slice_x_spans, CoglTexSliceSpan, x_span_index);
|
|
|
|
y_span = &g_array_index (tex->slice_y_spans, CoglTexSliceSpan, y_span_index);
|
|
|
|
|
|
|
|
return (x_span->waste || y_span->waste) ? TRUE : FALSE;
|
|
|
|
}
|
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
static gboolean
|
|
|
|
_cogl_pixel_format_from_gl_internal (GLenum gl_int_format,
|
|
|
|
CoglPixelFormat *out_format)
|
|
|
|
{
|
|
|
|
/* It doesn't really matter we convert to exact same
|
|
|
|
format (some have no cogl match anyway) since format
|
|
|
|
is re-matched against cogl when getting or setting
|
|
|
|
texture image data.
|
|
|
|
*/
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
switch (gl_int_format)
|
|
|
|
{
|
|
|
|
case GL_ALPHA: case GL_ALPHA4: case GL_ALPHA8:
|
|
|
|
case GL_ALPHA12: case GL_ALPHA16:
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
*out_format = COGL_PIXEL_FORMAT_A_8;
|
|
|
|
return TRUE;
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
case GL_LUMINANCE: case GL_LUMINANCE4: case GL_LUMINANCE8:
|
|
|
|
case GL_LUMINANCE12: case GL_LUMINANCE16:
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
*out_format = COGL_PIXEL_FORMAT_G_8;
|
|
|
|
return TRUE;
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
case GL_RGB: case GL_RGB4: case GL_RGB5: case GL_RGB8:
|
|
|
|
case GL_RGB10: case GL_RGB12: case GL_RGB16: case GL_R3_G3_B2:
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
*out_format = COGL_PIXEL_FORMAT_RGB_888;
|
|
|
|
return TRUE;
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
case GL_RGBA: case GL_RGBA2: case GL_RGBA4: case GL_RGB5_A1:
|
|
|
|
case GL_RGBA8: case GL_RGB10_A2: case GL_RGBA12: case GL_RGBA16:
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
*out_format = COGL_PIXEL_FORMAT_RGBA_8888;
|
|
|
|
return TRUE;
|
|
|
|
}
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static CoglPixelFormat
|
|
|
|
_cogl_pixel_format_to_gl (CoglPixelFormat format,
|
|
|
|
GLenum *out_glintformat,
|
|
|
|
GLenum *out_glformat,
|
|
|
|
GLenum *out_gltype)
|
|
|
|
{
|
|
|
|
CoglPixelFormat required_format;
|
|
|
|
GLenum glintformat = 0;
|
|
|
|
GLenum glformat = 0;
|
|
|
|
GLenum gltype = 0;
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2009-06-06 16:45:05 -04:00
|
|
|
/* FIXME: check YUV support */
|
|
|
|
|
|
|
|
required_format = format;
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
/* Find GL equivalents */
|
2009-05-09 14:39:01 -04:00
|
|
|
switch (format & COGL_UNPREMULT_MASK)
|
2008-04-25 09:37:36 -04:00
|
|
|
{
|
|
|
|
case COGL_PIXEL_FORMAT_A_8:
|
|
|
|
glintformat = GL_ALPHA;
|
|
|
|
glformat = GL_ALPHA;
|
|
|
|
gltype = GL_UNSIGNED_BYTE;
|
|
|
|
break;
|
|
|
|
case COGL_PIXEL_FORMAT_G_8:
|
|
|
|
glintformat = GL_LUMINANCE;
|
|
|
|
glformat = GL_LUMINANCE;
|
|
|
|
gltype = GL_UNSIGNED_BYTE;
|
|
|
|
break;
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
case COGL_PIXEL_FORMAT_RGB_888:
|
|
|
|
glintformat = GL_RGB;
|
|
|
|
glformat = GL_RGB;
|
|
|
|
gltype = GL_UNSIGNED_BYTE;
|
|
|
|
break;
|
|
|
|
case COGL_PIXEL_FORMAT_BGR_888:
|
|
|
|
glintformat = GL_RGB;
|
|
|
|
glformat = GL_BGR;
|
|
|
|
gltype = GL_UNSIGNED_BYTE;
|
|
|
|
break;
|
|
|
|
case COGL_PIXEL_FORMAT_RGBA_8888:
|
|
|
|
glintformat = GL_RGBA;
|
|
|
|
glformat = GL_RGBA;
|
|
|
|
gltype = GL_UNSIGNED_BYTE;
|
|
|
|
break;
|
|
|
|
case COGL_PIXEL_FORMAT_BGRA_8888:
|
|
|
|
glintformat = GL_RGBA;
|
|
|
|
glformat = GL_BGRA;
|
|
|
|
gltype = GL_UNSIGNED_BYTE;
|
|
|
|
break;
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
/* The following two types of channel ordering
|
|
|
|
* have no GL equivalent unless defined using
|
|
|
|
* system word byte ordering */
|
|
|
|
case COGL_PIXEL_FORMAT_ARGB_8888:
|
|
|
|
glintformat = GL_RGBA;
|
|
|
|
glformat = GL_BGRA;
|
|
|
|
#if G_BYTE_ORDER == G_LITTLE_ENDIAN
|
|
|
|
gltype = GL_UNSIGNED_INT_8_8_8_8;
|
|
|
|
#else
|
|
|
|
gltype = GL_UNSIGNED_INT_8_8_8_8_REV;
|
|
|
|
#endif
|
|
|
|
break;
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
case COGL_PIXEL_FORMAT_ABGR_8888:
|
|
|
|
glintformat = GL_RGBA;
|
|
|
|
glformat = GL_RGBA;
|
|
|
|
#if G_BYTE_ORDER == G_LITTLE_ENDIAN
|
|
|
|
gltype = GL_UNSIGNED_INT_8_8_8_8;
|
|
|
|
#else
|
|
|
|
gltype = GL_UNSIGNED_INT_8_8_8_8_REV;
|
|
|
|
#endif
|
|
|
|
break;
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
/* The following three types of channel ordering
|
|
|
|
* are always defined using system word byte
|
|
|
|
* ordering (even according to GLES spec) */
|
|
|
|
case COGL_PIXEL_FORMAT_RGB_565:
|
|
|
|
glintformat = GL_RGB;
|
|
|
|
glformat = GL_RGB;
|
|
|
|
gltype = GL_UNSIGNED_SHORT_5_6_5;
|
|
|
|
break;
|
|
|
|
case COGL_PIXEL_FORMAT_RGBA_4444:
|
|
|
|
glintformat = GL_RGBA;
|
|
|
|
glformat = GL_RGBA;
|
|
|
|
gltype = GL_UNSIGNED_SHORT_4_4_4_4;
|
|
|
|
break;
|
|
|
|
case COGL_PIXEL_FORMAT_RGBA_5551:
|
|
|
|
glintformat = GL_RGBA;
|
|
|
|
glformat = GL_RGBA;
|
|
|
|
gltype = GL_UNSIGNED_SHORT_5_5_5_1;
|
|
|
|
break;
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
/* FIXME: check extensions for YUV support */
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
if (out_glintformat != NULL)
|
|
|
|
*out_glintformat = glintformat;
|
|
|
|
if (out_glformat != NULL)
|
|
|
|
*out_glformat = glformat;
|
|
|
|
if (out_gltype != NULL)
|
|
|
|
*out_gltype = gltype;
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
return required_format;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
_cogl_texture_bitmap_prepare (CoglTexture *tex,
|
|
|
|
CoglPixelFormat internal_format)
|
|
|
|
{
|
|
|
|
CoglBitmap new_bitmap;
|
|
|
|
CoglPixelFormat new_data_format;
|
|
|
|
gboolean success;
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2009-06-06 16:45:05 -04:00
|
|
|
/* Was there any internal conversion requested?
|
|
|
|
* By default Cogl will use a premultiplied internal format. Later we will
|
|
|
|
* add control over this. */
|
2008-04-25 09:37:36 -04:00
|
|
|
if (internal_format == COGL_PIXEL_FORMAT_ANY)
|
2009-06-06 16:45:05 -04:00
|
|
|
{
|
|
|
|
if ((tex->bitmap.format & COGL_A_BIT) &&
|
|
|
|
tex->bitmap.format != COGL_PIXEL_FORMAT_A_8)
|
|
|
|
internal_format = tex->bitmap.format | COGL_PREMULT_BIT;
|
2009-06-08 12:43:34 -04:00
|
|
|
else
|
|
|
|
internal_format = tex->bitmap.format;
|
2009-06-06 16:45:05 -04:00
|
|
|
}
|
2008-04-25 09:37:36 -04:00
|
|
|
|
|
|
|
/* Find closest format accepted by GL */
|
|
|
|
new_data_format = _cogl_pixel_format_to_gl (internal_format,
|
|
|
|
&tex->gl_intformat,
|
|
|
|
&tex->gl_format,
|
|
|
|
&tex->gl_type);
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
/* Convert to internal format */
|
|
|
|
if (new_data_format != tex->bitmap.format)
|
|
|
|
{
|
|
|
|
success = _cogl_bitmap_convert_and_premult (&tex->bitmap,
|
|
|
|
&new_bitmap,
|
|
|
|
new_data_format);
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
if (!success)
|
|
|
|
return FALSE;
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
/* Update texture with new data */
|
|
|
|
_cogl_texture_bitmap_swap (tex, &new_bitmap);
|
|
|
|
}
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
_cogl_texture_free (CoglTexture *tex)
|
|
|
|
{
|
|
|
|
/* Frees texture resources but its handle is not
|
|
|
|
released! Do that separately before this! */
|
|
|
|
_cogl_texture_bitmap_free (tex);
|
|
|
|
_cogl_texture_slices_free (tex);
|
|
|
|
g_free (tex);
|
|
|
|
}
|
|
|
|
|
|
|
|
CoglHandle
|
[cogl] Remove max_waste argument from Texture ctors
The CoglTexture constructors expose the "max-waste" argument for
controlling the maximum amount of wasted areas for slicing or,
if set to -1, disables slicing.
Slicing is really relevant only for large images that are never
repeated, so it's a useful feature only in controlled use cases.
Specifying the amount of wasted area is, on the other hand, just
a way to mess up this feature; 99% the times, you either pull this
number out of thin air, hoping it's right, or you try to do the
right thing and you choose the wrong number anyway.
Instead, we can use the CoglTextureFlags to control whether the
texture should not be sliced (useful for Clutter-GST and for the
texture-from-pixmap actors) and provide a reasonable value for
enabling the slicing ourself. At some point, we might even
provide a way to change the default at compile time or at run time,
for particular platforms.
Since max_waste is gone, the :tile-waste property of ClutterTexture
becomes read-only, and it proxies the cogl_texture_get_max_waste()
function.
Inside Clutter, the only cases where the max_waste argument was
not set to -1 are in the Pango glyph cache (which is a POT texture
anyway) and inside the test cases where we want to force slicing;
for the latter we can create larger textures that will be bigger than
the threshold we set.
Signed-off-by: Emmanuele Bassi <ebassi@linux.intel.com>
Signed-off-by: Robert Bragg <robert@linux.intel.com>
Signed-off-by: Neil Roberts <neil@linux.intel.com>
2009-05-23 14:18:18 -04:00
|
|
|
cogl_texture_new_with_size (guint width,
|
|
|
|
guint height,
|
|
|
|
CoglTextureFlags flags,
|
|
|
|
CoglPixelFormat internal_format)
|
2008-04-25 09:37:36 -04:00
|
|
|
{
|
|
|
|
CoglTexture *tex;
|
|
|
|
gint bpp;
|
|
|
|
gint rowstride;
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
/* Since no data, we need some internal format */
|
|
|
|
if (internal_format == COGL_PIXEL_FORMAT_ANY)
|
|
|
|
return COGL_INVALID_HANDLE;
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
/* Rowstride from width */
|
|
|
|
bpp = _cogl_get_format_bpp (internal_format);
|
|
|
|
rowstride = width * bpp;
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
/* Init texture with empty bitmap */
|
|
|
|
tex = (CoglTexture*) g_malloc (sizeof (CoglTexture));
|
|
|
|
|
|
|
|
tex->is_foreign = FALSE;
|
2009-06-04 11:04:57 -04:00
|
|
|
tex->auto_mipmap = (flags & COGL_TEXTURE_NO_AUTO_MIPMAP) == 0;
|
|
|
|
tex->mipmaps_dirty = TRUE;
|
2009-06-09 06:13:11 -04:00
|
|
|
tex->first_pixels = NULL;
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
tex->bitmap.width = width;
|
|
|
|
tex->bitmap.height = height;
|
|
|
|
tex->bitmap.format = internal_format;
|
|
|
|
tex->bitmap.rowstride = rowstride;
|
|
|
|
tex->bitmap.data = NULL;
|
|
|
|
tex->bitmap_owner = FALSE;
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
tex->slice_x_spans = NULL;
|
|
|
|
tex->slice_y_spans = NULL;
|
|
|
|
tex->slice_gl_handles = NULL;
|
2008-12-23 11:29:29 -05:00
|
|
|
|
[cogl] Remove max_waste argument from Texture ctors
The CoglTexture constructors expose the "max-waste" argument for
controlling the maximum amount of wasted areas for slicing or,
if set to -1, disables slicing.
Slicing is really relevant only for large images that are never
repeated, so it's a useful feature only in controlled use cases.
Specifying the amount of wasted area is, on the other hand, just
a way to mess up this feature; 99% the times, you either pull this
number out of thin air, hoping it's right, or you try to do the
right thing and you choose the wrong number anyway.
Instead, we can use the CoglTextureFlags to control whether the
texture should not be sliced (useful for Clutter-GST and for the
texture-from-pixmap actors) and provide a reasonable value for
enabling the slicing ourself. At some point, we might even
provide a way to change the default at compile time or at run time,
for particular platforms.
Since max_waste is gone, the :tile-waste property of ClutterTexture
becomes read-only, and it proxies the cogl_texture_get_max_waste()
function.
Inside Clutter, the only cases where the max_waste argument was
not set to -1 are in the Pango glyph cache (which is a POT texture
anyway) and inside the test cases where we want to force slicing;
for the latter we can create larger textures that will be bigger than
the threshold we set.
Signed-off-by: Emmanuele Bassi <ebassi@linux.intel.com>
Signed-off-by: Robert Bragg <robert@linux.intel.com>
Signed-off-by: Neil Roberts <neil@linux.intel.com>
2009-05-23 14:18:18 -04:00
|
|
|
if (flags & COGL_TEXTURE_NO_SLICING)
|
|
|
|
tex->max_waste = -1;
|
|
|
|
else
|
|
|
|
tex->max_waste = COGL_TEXTURE_MAX_WASTE;
|
|
|
|
|
2009-06-04 11:04:57 -04:00
|
|
|
/* Unknown filter */
|
|
|
|
tex->min_filter = GL_FALSE;
|
|
|
|
tex->mag_filter = GL_FALSE;
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
/* Find closest GL format match */
|
|
|
|
tex->bitmap.format =
|
|
|
|
_cogl_pixel_format_to_gl (internal_format,
|
|
|
|
&tex->gl_intformat,
|
|
|
|
&tex->gl_format,
|
|
|
|
&tex->gl_type);
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
/* Create slices for the given format and size */
|
|
|
|
if (!_cogl_texture_slices_create (tex))
|
|
|
|
{
|
|
|
|
_cogl_texture_free (tex);
|
|
|
|
return COGL_INVALID_HANDLE;
|
|
|
|
}
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
return _cogl_texture_handle_new (tex);
|
|
|
|
}
|
|
|
|
|
|
|
|
CoglHandle
|
2009-01-18 09:51:19 -05:00
|
|
|
cogl_texture_new_from_data (guint width,
|
|
|
|
guint height,
|
|
|
|
CoglTextureFlags flags,
|
|
|
|
CoglPixelFormat format,
|
|
|
|
CoglPixelFormat internal_format,
|
|
|
|
guint rowstride,
|
|
|
|
const guchar *data)
|
2008-04-25 09:37:36 -04:00
|
|
|
{
|
|
|
|
CoglTexture *tex;
|
|
|
|
gint bpp;
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
if (format == COGL_PIXEL_FORMAT_ANY)
|
|
|
|
return COGL_INVALID_HANDLE;
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
if (data == NULL)
|
|
|
|
return COGL_INVALID_HANDLE;
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
/* Rowstride from width if not given */
|
|
|
|
bpp = _cogl_get_format_bpp (format);
|
|
|
|
if (rowstride == 0) rowstride = width * bpp;
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
/* Create new texture and fill with given data */
|
|
|
|
tex = (CoglTexture*) g_malloc (sizeof (CoglTexture));
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
tex->is_foreign = FALSE;
|
2009-06-04 11:04:57 -04:00
|
|
|
tex->auto_mipmap = (flags & COGL_TEXTURE_NO_AUTO_MIPMAP) == 0;
|
|
|
|
tex->mipmaps_dirty = TRUE;
|
2009-06-09 06:13:11 -04:00
|
|
|
tex->first_pixels = NULL;
|
2008-11-18 11:24:09 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
tex->bitmap.width = width;
|
|
|
|
tex->bitmap.height = height;
|
|
|
|
tex->bitmap.data = (guchar*)data;
|
|
|
|
tex->bitmap.format = format;
|
|
|
|
tex->bitmap.rowstride = rowstride;
|
|
|
|
tex->bitmap_owner = FALSE;
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
tex->slice_x_spans = NULL;
|
|
|
|
tex->slice_y_spans = NULL;
|
|
|
|
tex->slice_gl_handles = NULL;
|
2008-12-23 11:29:29 -05:00
|
|
|
|
[cogl] Remove max_waste argument from Texture ctors
The CoglTexture constructors expose the "max-waste" argument for
controlling the maximum amount of wasted areas for slicing or,
if set to -1, disables slicing.
Slicing is really relevant only for large images that are never
repeated, so it's a useful feature only in controlled use cases.
Specifying the amount of wasted area is, on the other hand, just
a way to mess up this feature; 99% the times, you either pull this
number out of thin air, hoping it's right, or you try to do the
right thing and you choose the wrong number anyway.
Instead, we can use the CoglTextureFlags to control whether the
texture should not be sliced (useful for Clutter-GST and for the
texture-from-pixmap actors) and provide a reasonable value for
enabling the slicing ourself. At some point, we might even
provide a way to change the default at compile time or at run time,
for particular platforms.
Since max_waste is gone, the :tile-waste property of ClutterTexture
becomes read-only, and it proxies the cogl_texture_get_max_waste()
function.
Inside Clutter, the only cases where the max_waste argument was
not set to -1 are in the Pango glyph cache (which is a POT texture
anyway) and inside the test cases where we want to force slicing;
for the latter we can create larger textures that will be bigger than
the threshold we set.
Signed-off-by: Emmanuele Bassi <ebassi@linux.intel.com>
Signed-off-by: Robert Bragg <robert@linux.intel.com>
Signed-off-by: Neil Roberts <neil@linux.intel.com>
2009-05-23 14:18:18 -04:00
|
|
|
if (flags & COGL_TEXTURE_NO_SLICING)
|
|
|
|
tex->max_waste = -1;
|
|
|
|
else
|
|
|
|
tex->max_waste = COGL_TEXTURE_MAX_WASTE;
|
|
|
|
|
2009-06-04 11:04:57 -04:00
|
|
|
/* Unknown filter */
|
|
|
|
tex->min_filter = GL_FALSE;
|
|
|
|
tex->mag_filter = GL_FALSE;
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
/* FIXME: If upload fails we should set some kind of
|
|
|
|
* error flag but still return texture handle (this
|
|
|
|
* is to keep the behavior equal to _new_from_file;
|
|
|
|
* see below) */
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
if (!_cogl_texture_bitmap_prepare (tex, internal_format))
|
|
|
|
{
|
|
|
|
_cogl_texture_free (tex);
|
|
|
|
return COGL_INVALID_HANDLE;
|
|
|
|
}
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
if (!_cogl_texture_slices_create (tex))
|
|
|
|
{
|
|
|
|
_cogl_texture_free (tex);
|
|
|
|
return COGL_INVALID_HANDLE;
|
|
|
|
}
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
if (!_cogl_texture_upload_to_gl (tex))
|
|
|
|
{
|
|
|
|
_cogl_texture_free (tex);
|
|
|
|
return COGL_INVALID_HANDLE;
|
|
|
|
}
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
_cogl_texture_bitmap_free (tex);
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
return _cogl_texture_handle_new (tex);
|
|
|
|
}
|
|
|
|
|
|
|
|
CoglHandle
|
[cogl] Remove max_waste argument from Texture ctors
The CoglTexture constructors expose the "max-waste" argument for
controlling the maximum amount of wasted areas for slicing or,
if set to -1, disables slicing.
Slicing is really relevant only for large images that are never
repeated, so it's a useful feature only in controlled use cases.
Specifying the amount of wasted area is, on the other hand, just
a way to mess up this feature; 99% the times, you either pull this
number out of thin air, hoping it's right, or you try to do the
right thing and you choose the wrong number anyway.
Instead, we can use the CoglTextureFlags to control whether the
texture should not be sliced (useful for Clutter-GST and for the
texture-from-pixmap actors) and provide a reasonable value for
enabling the slicing ourself. At some point, we might even
provide a way to change the default at compile time or at run time,
for particular platforms.
Since max_waste is gone, the :tile-waste property of ClutterTexture
becomes read-only, and it proxies the cogl_texture_get_max_waste()
function.
Inside Clutter, the only cases where the max_waste argument was
not set to -1 are in the Pango glyph cache (which is a POT texture
anyway) and inside the test cases where we want to force slicing;
for the latter we can create larger textures that will be bigger than
the threshold we set.
Signed-off-by: Emmanuele Bassi <ebassi@linux.intel.com>
Signed-off-by: Robert Bragg <robert@linux.intel.com>
Signed-off-by: Neil Roberts <neil@linux.intel.com>
2009-05-23 14:18:18 -04:00
|
|
|
cogl_texture_new_from_bitmap (CoglHandle bmp_handle,
|
|
|
|
CoglTextureFlags flags,
|
|
|
|
CoglPixelFormat internal_format)
|
2008-04-25 09:37:36 -04:00
|
|
|
{
|
|
|
|
CoglTexture *tex;
|
2009-04-30 13:00:22 -04:00
|
|
|
CoglBitmap *bmp = (CoglBitmap *)bmp_handle;
|
|
|
|
|
|
|
|
g_return_val_if_fail (bmp_handle != COGL_INVALID_HANDLE, COGL_INVALID_HANDLE);
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
/* Create new texture and fill with loaded data */
|
|
|
|
tex = (CoglTexture*) g_malloc ( sizeof (CoglTexture));
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
tex->is_foreign = FALSE;
|
2009-06-04 11:04:57 -04:00
|
|
|
tex->auto_mipmap = (flags & COGL_TEXTURE_NO_AUTO_MIPMAP) == 0;
|
|
|
|
tex->mipmaps_dirty = TRUE;
|
2009-06-09 06:13:11 -04:00
|
|
|
tex->first_pixels = NULL;
|
2008-11-18 11:24:09 -05:00
|
|
|
|
2009-01-07 12:02:43 -05:00
|
|
|
tex->bitmap = *bmp;
|
2009-06-02 13:01:18 -04:00
|
|
|
tex->bitmap_owner = FALSE;
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
tex->slice_x_spans = NULL;
|
|
|
|
tex->slice_y_spans = NULL;
|
|
|
|
tex->slice_gl_handles = NULL;
|
2008-12-23 11:29:29 -05:00
|
|
|
|
[cogl] Remove max_waste argument from Texture ctors
The CoglTexture constructors expose the "max-waste" argument for
controlling the maximum amount of wasted areas for slicing or,
if set to -1, disables slicing.
Slicing is really relevant only for large images that are never
repeated, so it's a useful feature only in controlled use cases.
Specifying the amount of wasted area is, on the other hand, just
a way to mess up this feature; 99% the times, you either pull this
number out of thin air, hoping it's right, or you try to do the
right thing and you choose the wrong number anyway.
Instead, we can use the CoglTextureFlags to control whether the
texture should not be sliced (useful for Clutter-GST and for the
texture-from-pixmap actors) and provide a reasonable value for
enabling the slicing ourself. At some point, we might even
provide a way to change the default at compile time or at run time,
for particular platforms.
Since max_waste is gone, the :tile-waste property of ClutterTexture
becomes read-only, and it proxies the cogl_texture_get_max_waste()
function.
Inside Clutter, the only cases where the max_waste argument was
not set to -1 are in the Pango glyph cache (which is a POT texture
anyway) and inside the test cases where we want to force slicing;
for the latter we can create larger textures that will be bigger than
the threshold we set.
Signed-off-by: Emmanuele Bassi <ebassi@linux.intel.com>
Signed-off-by: Robert Bragg <robert@linux.intel.com>
Signed-off-by: Neil Roberts <neil@linux.intel.com>
2009-05-23 14:18:18 -04:00
|
|
|
if (flags & COGL_TEXTURE_NO_SLICING)
|
|
|
|
tex->max_waste = -1;
|
|
|
|
else
|
|
|
|
tex->max_waste = COGL_TEXTURE_MAX_WASTE;
|
|
|
|
|
2009-06-04 11:04:57 -04:00
|
|
|
/* Unknown filter */
|
|
|
|
tex->min_filter = GL_FALSE;
|
|
|
|
tex->mag_filter = GL_FALSE;
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
/* FIXME: If upload fails we should set some kind of
|
|
|
|
* error flag but still return texture handle if the
|
|
|
|
* user decides to destroy another texture and upload
|
|
|
|
* this one instead (reloading from file is not needed
|
|
|
|
* in that case). As a rule then, everytime a valid
|
|
|
|
* CoglHandle is returned, it should also be destroyed
|
2009-04-01 12:16:44 -04:00
|
|
|
* with cogl_handle_unref at some point! */
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
if (!_cogl_texture_bitmap_prepare (tex, internal_format))
|
|
|
|
{
|
|
|
|
_cogl_texture_free (tex);
|
|
|
|
return COGL_INVALID_HANDLE;
|
|
|
|
}
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
if (!_cogl_texture_slices_create (tex))
|
|
|
|
{
|
|
|
|
_cogl_texture_free (tex);
|
|
|
|
return COGL_INVALID_HANDLE;
|
|
|
|
}
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
if (!_cogl_texture_upload_to_gl (tex))
|
|
|
|
{
|
|
|
|
_cogl_texture_free (tex);
|
|
|
|
return COGL_INVALID_HANDLE;
|
|
|
|
}
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
_cogl_texture_bitmap_free (tex);
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
return _cogl_texture_handle_new (tex);
|
|
|
|
}
|
|
|
|
|
2009-01-07 12:02:43 -05:00
|
|
|
CoglHandle
|
2009-01-18 09:51:19 -05:00
|
|
|
cogl_texture_new_from_file (const gchar *filename,
|
|
|
|
CoglTextureFlags flags,
|
|
|
|
CoglPixelFormat internal_format,
|
|
|
|
GError **error)
|
2009-01-07 12:02:43 -05:00
|
|
|
{
|
[cogl] Remove max_waste argument from Texture ctors
The CoglTexture constructors expose the "max-waste" argument for
controlling the maximum amount of wasted areas for slicing or,
if set to -1, disables slicing.
Slicing is really relevant only for large images that are never
repeated, so it's a useful feature only in controlled use cases.
Specifying the amount of wasted area is, on the other hand, just
a way to mess up this feature; 99% the times, you either pull this
number out of thin air, hoping it's right, or you try to do the
right thing and you choose the wrong number anyway.
Instead, we can use the CoglTextureFlags to control whether the
texture should not be sliced (useful for Clutter-GST and for the
texture-from-pixmap actors) and provide a reasonable value for
enabling the slicing ourself. At some point, we might even
provide a way to change the default at compile time or at run time,
for particular platforms.
Since max_waste is gone, the :tile-waste property of ClutterTexture
becomes read-only, and it proxies the cogl_texture_get_max_waste()
function.
Inside Clutter, the only cases where the max_waste argument was
not set to -1 are in the Pango glyph cache (which is a POT texture
anyway) and inside the test cases where we want to force slicing;
for the latter we can create larger textures that will be bigger than
the threshold we set.
Signed-off-by: Emmanuele Bassi <ebassi@linux.intel.com>
Signed-off-by: Robert Bragg <robert@linux.intel.com>
Signed-off-by: Neil Roberts <neil@linux.intel.com>
2009-05-23 14:18:18 -04:00
|
|
|
CoglHandle bmp;
|
|
|
|
CoglHandle handle;
|
Fully integrates CoglMaterial throughout the rest of Cogl
This glues CoglMaterial in as the fundamental way that Cogl describes how to
fill in geometry.
It adds cogl_set_source (), which is used to set the material which will be
used by all subsequent drawing functions
It adds cogl_set_source_texture as a convenience for setting up a default
material with a single texture layer, and cogl_set_source_color is now also
a convenience for setting up a material with a solid fill.
"drawing functions" include, cogl_rectangle, cogl_texture_rectangle,
cogl_texture_multiple_rectangles, cogl_texture_polygon (though the
cogl_texture_* funcs have been renamed; see below for details),
cogl_path_fill/stroke and cogl_vertex_buffer_draw*.
cogl_texture_rectangle, cogl_texture_multiple_rectangles and
cogl_texture_polygon no longer take a texture handle; instead the current
source material is referenced. The functions have also been renamed to:
cogl_rectangle_with_texture_coords, cogl_rectangles_with_texture_coords
and cogl_polygon respectivly.
Most code that previously did:
cogl_texture_rectangle (tex_handle, x, y,...);
needs to be changed to now do:
cogl_set_source_texture (tex_handle);
cogl_rectangle_with_texture_coords (x, y,....);
In the less likely case where you were blending your source texture with a color
like:
cogl_set_source_color4ub (r,g,b,a); /* where r,g,b,a isn't just white */
cogl_texture_rectangle (tex_handle, x, y,...);
you will need your own material to do that:
mat = cogl_material_new ();
cogl_material_set_color4ub (r,g,b,a);
cogl_material_set_layer (mat, 0, tex_handle));
cogl_set_source_material (mat);
Code that uses the texture coordinates, 0, 0, 1, 1 don't need to use
cog_rectangle_with_texure_coords since these are the coordinates that
cogl_rectangle will use.
For cogl_texture_polygon; as well as dropping the texture handle, the
n_vertices and vertices arguments were transposed for consistency. So
code previously written as:
cogl_texture_polygon (tex_handle, 3, verts, TRUE);
need to be written as:
cogl_set_source_texture (tex_handle);
cogl_polygon (verts, 3, TRUE);
All of the unit tests have been updated to now use the material API and
test-cogl-material has been renamed to test-cogl-multitexture since any
textured quad is now technically a test of CoglMaterial but this test
specifically creates a material with multiple texture layers.
Note: The GLES backend has not been updated yet; that will be done in a
following commit.
2009-01-23 11:15:40 -05:00
|
|
|
|
2009-01-07 12:02:43 -05:00
|
|
|
g_return_val_if_fail (error == NULL || *error == NULL, COGL_INVALID_HANDLE);
|
|
|
|
|
2009-04-30 13:00:22 -04:00
|
|
|
bmp = cogl_bitmap_new_from_file (filename, error);
|
|
|
|
if (bmp == COGL_INVALID_HANDLE)
|
2009-01-07 12:02:43 -05:00
|
|
|
return COGL_INVALID_HANDLE;
|
Fully integrates CoglMaterial throughout the rest of Cogl
This glues CoglMaterial in as the fundamental way that Cogl describes how to
fill in geometry.
It adds cogl_set_source (), which is used to set the material which will be
used by all subsequent drawing functions
It adds cogl_set_source_texture as a convenience for setting up a default
material with a single texture layer, and cogl_set_source_color is now also
a convenience for setting up a material with a solid fill.
"drawing functions" include, cogl_rectangle, cogl_texture_rectangle,
cogl_texture_multiple_rectangles, cogl_texture_polygon (though the
cogl_texture_* funcs have been renamed; see below for details),
cogl_path_fill/stroke and cogl_vertex_buffer_draw*.
cogl_texture_rectangle, cogl_texture_multiple_rectangles and
cogl_texture_polygon no longer take a texture handle; instead the current
source material is referenced. The functions have also been renamed to:
cogl_rectangle_with_texture_coords, cogl_rectangles_with_texture_coords
and cogl_polygon respectivly.
Most code that previously did:
cogl_texture_rectangle (tex_handle, x, y,...);
needs to be changed to now do:
cogl_set_source_texture (tex_handle);
cogl_rectangle_with_texture_coords (x, y,....);
In the less likely case where you were blending your source texture with a color
like:
cogl_set_source_color4ub (r,g,b,a); /* where r,g,b,a isn't just white */
cogl_texture_rectangle (tex_handle, x, y,...);
you will need your own material to do that:
mat = cogl_material_new ();
cogl_material_set_color4ub (r,g,b,a);
cogl_material_set_layer (mat, 0, tex_handle));
cogl_set_source_material (mat);
Code that uses the texture coordinates, 0, 0, 1, 1 don't need to use
cog_rectangle_with_texure_coords since these are the coordinates that
cogl_rectangle will use.
For cogl_texture_polygon; as well as dropping the texture handle, the
n_vertices and vertices arguments were transposed for consistency. So
code previously written as:
cogl_texture_polygon (tex_handle, 3, verts, TRUE);
need to be written as:
cogl_set_source_texture (tex_handle);
cogl_polygon (verts, 3, TRUE);
All of the unit tests have been updated to now use the material API and
test-cogl-material has been renamed to test-cogl-multitexture since any
textured quad is now technically a test of CoglMaterial but this test
specifically creates a material with multiple texture layers.
Note: The GLES backend has not been updated yet; that will be done in a
following commit.
2009-01-23 11:15:40 -05:00
|
|
|
|
[cogl] Remove max_waste argument from Texture ctors
The CoglTexture constructors expose the "max-waste" argument for
controlling the maximum amount of wasted areas for slicing or,
if set to -1, disables slicing.
Slicing is really relevant only for large images that are never
repeated, so it's a useful feature only in controlled use cases.
Specifying the amount of wasted area is, on the other hand, just
a way to mess up this feature; 99% the times, you either pull this
number out of thin air, hoping it's right, or you try to do the
right thing and you choose the wrong number anyway.
Instead, we can use the CoglTextureFlags to control whether the
texture should not be sliced (useful for Clutter-GST and for the
texture-from-pixmap actors) and provide a reasonable value for
enabling the slicing ourself. At some point, we might even
provide a way to change the default at compile time or at run time,
for particular platforms.
Since max_waste is gone, the :tile-waste property of ClutterTexture
becomes read-only, and it proxies the cogl_texture_get_max_waste()
function.
Inside Clutter, the only cases where the max_waste argument was
not set to -1 are in the Pango glyph cache (which is a POT texture
anyway) and inside the test cases where we want to force slicing;
for the latter we can create larger textures that will be bigger than
the threshold we set.
Signed-off-by: Emmanuele Bassi <ebassi@linux.intel.com>
Signed-off-by: Robert Bragg <robert@linux.intel.com>
Signed-off-by: Neil Roberts <neil@linux.intel.com>
2009-05-23 14:18:18 -04:00
|
|
|
handle = cogl_texture_new_from_bitmap (bmp, flags, internal_format);
|
2009-04-30 13:00:22 -04:00
|
|
|
cogl_handle_unref (bmp);
|
2009-01-07 12:02:43 -05:00
|
|
|
|
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
CoglHandle
|
|
|
|
cogl_texture_new_from_foreign (GLuint gl_handle,
|
|
|
|
GLenum gl_target,
|
|
|
|
GLuint width,
|
|
|
|
GLuint height,
|
|
|
|
GLuint x_pot_waste,
|
|
|
|
GLuint y_pot_waste,
|
|
|
|
CoglPixelFormat format)
|
|
|
|
{
|
|
|
|
/* NOTE: width, height and internal format are not queriable
|
2008-11-18 11:24:09 -05:00
|
|
|
in GLES, hence such a function prototype. However, for
|
|
|
|
OpenGL they are still queried from the texture for improved
|
|
|
|
robustness and for completeness in case one day GLES gains
|
|
|
|
support for them.
|
2008-04-25 09:37:36 -04:00
|
|
|
*/
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
GLenum gl_error = 0;
|
|
|
|
GLboolean gl_istexture;
|
|
|
|
GLint gl_compressed = GL_FALSE;
|
|
|
|
GLint gl_int_format = 0;
|
|
|
|
GLint gl_width = 0;
|
|
|
|
GLint gl_height = 0;
|
2008-05-07 12:12:54 -04:00
|
|
|
GLint gl_gen_mipmap;
|
2008-04-25 09:37:36 -04:00
|
|
|
guint bpp;
|
|
|
|
CoglTexture *tex;
|
|
|
|
CoglTexSliceSpan x_span;
|
|
|
|
CoglTexSliceSpan y_span;
|
2008-12-04 12:24:33 -05:00
|
|
|
|
|
|
|
/* GL_ARB_texture_rectangle textures are supported if they are
|
|
|
|
created from foreign because some chipsets have trouble with
|
|
|
|
GL_ARB_texture_non_power_of_two. There is no Cogl call to create
|
|
|
|
them directly to emphasize the fact that they don't work fully
|
|
|
|
(for example, no mipmapping and complicated shader support) */
|
|
|
|
|
|
|
|
/* Allow 2-dimensional or rectangle textures only */
|
|
|
|
if (gl_target != GL_TEXTURE_2D && gl_target != CGL_TEXTURE_RECTANGLE_ARB)
|
2008-04-25 09:37:36 -04:00
|
|
|
return COGL_INVALID_HANDLE;
|
2008-12-04 12:24:33 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
/* Make sure it is a valid GL texture object */
|
2008-05-21 09:20:33 -04:00
|
|
|
gl_istexture = glIsTexture (gl_handle);
|
2008-04-25 09:37:36 -04:00
|
|
|
if (gl_istexture == GL_FALSE)
|
|
|
|
return COGL_INVALID_HANDLE;
|
2008-12-04 12:24:33 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
/* Make sure binding succeeds */
|
|
|
|
gl_error = glGetError ();
|
|
|
|
glBindTexture (gl_target, gl_handle);
|
|
|
|
if (glGetError () != GL_NO_ERROR)
|
|
|
|
return COGL_INVALID_HANDLE;
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
/* Obtain texture parameters
|
|
|
|
(only level 0 we are interested in) */
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
GE( glGetTexLevelParameteriv (gl_target, 0,
|
|
|
|
GL_TEXTURE_COMPRESSED,
|
|
|
|
&gl_compressed) );
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
GE( glGetTexLevelParameteriv (gl_target, 0,
|
|
|
|
GL_TEXTURE_INTERNAL_FORMAT,
|
|
|
|
&gl_int_format) );
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
|
|
|
|
GE( glGetTexLevelParameteriv (gl_target, 0,
|
|
|
|
GL_TEXTURE_WIDTH,
|
|
|
|
&gl_width) );
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
GE( glGetTexLevelParameteriv (gl_target, 0,
|
|
|
|
GL_TEXTURE_HEIGHT,
|
|
|
|
&gl_height) );
|
2008-11-18 11:24:09 -05:00
|
|
|
|
2008-05-07 12:12:54 -04:00
|
|
|
GE( glGetTexParameteriv (gl_target,
|
|
|
|
GL_GENERATE_MIPMAP,
|
|
|
|
&gl_gen_mipmap) );
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
/* Validate width and height */
|
|
|
|
if (gl_width <= 0 || gl_height <= 0)
|
|
|
|
return COGL_INVALID_HANDLE;
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
/* Validate pot waste */
|
|
|
|
if (x_pot_waste < 0 || x_pot_waste >= gl_width ||
|
|
|
|
y_pot_waste < 0 || y_pot_waste >= gl_height)
|
|
|
|
return COGL_INVALID_HANDLE;
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
/* Compressed texture images not supported */
|
|
|
|
if (gl_compressed == GL_TRUE)
|
|
|
|
return COGL_INVALID_HANDLE;
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
/* Try and match to a cogl format */
|
[cogl] Remove max_waste argument from Texture ctors
The CoglTexture constructors expose the "max-waste" argument for
controlling the maximum amount of wasted areas for slicing or,
if set to -1, disables slicing.
Slicing is really relevant only for large images that are never
repeated, so it's a useful feature only in controlled use cases.
Specifying the amount of wasted area is, on the other hand, just
a way to mess up this feature; 99% the times, you either pull this
number out of thin air, hoping it's right, or you try to do the
right thing and you choose the wrong number anyway.
Instead, we can use the CoglTextureFlags to control whether the
texture should not be sliced (useful for Clutter-GST and for the
texture-from-pixmap actors) and provide a reasonable value for
enabling the slicing ourself. At some point, we might even
provide a way to change the default at compile time or at run time,
for particular platforms.
Since max_waste is gone, the :tile-waste property of ClutterTexture
becomes read-only, and it proxies the cogl_texture_get_max_waste()
function.
Inside Clutter, the only cases where the max_waste argument was
not set to -1 are in the Pango glyph cache (which is a POT texture
anyway) and inside the test cases where we want to force slicing;
for the latter we can create larger textures that will be bigger than
the threshold we set.
Signed-off-by: Emmanuele Bassi <ebassi@linux.intel.com>
Signed-off-by: Robert Bragg <robert@linux.intel.com>
Signed-off-by: Neil Roberts <neil@linux.intel.com>
2009-05-23 14:18:18 -04:00
|
|
|
if (!_cogl_pixel_format_from_gl_internal (gl_int_format, &format))
|
|
|
|
return COGL_INVALID_HANDLE;
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
/* Create new texture */
|
[cogl] Remove max_waste argument from Texture ctors
The CoglTexture constructors expose the "max-waste" argument for
controlling the maximum amount of wasted areas for slicing or,
if set to -1, disables slicing.
Slicing is really relevant only for large images that are never
repeated, so it's a useful feature only in controlled use cases.
Specifying the amount of wasted area is, on the other hand, just
a way to mess up this feature; 99% the times, you either pull this
number out of thin air, hoping it's right, or you try to do the
right thing and you choose the wrong number anyway.
Instead, we can use the CoglTextureFlags to control whether the
texture should not be sliced (useful for Clutter-GST and for the
texture-from-pixmap actors) and provide a reasonable value for
enabling the slicing ourself. At some point, we might even
provide a way to change the default at compile time or at run time,
for particular platforms.
Since max_waste is gone, the :tile-waste property of ClutterTexture
becomes read-only, and it proxies the cogl_texture_get_max_waste()
function.
Inside Clutter, the only cases where the max_waste argument was
not set to -1 are in the Pango glyph cache (which is a POT texture
anyway) and inside the test cases where we want to force slicing;
for the latter we can create larger textures that will be bigger than
the threshold we set.
Signed-off-by: Emmanuele Bassi <ebassi@linux.intel.com>
Signed-off-by: Robert Bragg <robert@linux.intel.com>
Signed-off-by: Neil Roberts <neil@linux.intel.com>
2009-05-23 14:18:18 -04:00
|
|
|
tex = (CoglTexture *) g_malloc (sizeof (CoglTexture));
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
/* Setup bitmap info */
|
|
|
|
tex->is_foreign = TRUE;
|
2008-05-07 12:12:54 -04:00
|
|
|
tex->auto_mipmap = (gl_gen_mipmap == GL_TRUE) ? TRUE : FALSE;
|
2009-06-04 11:04:57 -04:00
|
|
|
tex->mipmaps_dirty = TRUE;
|
2009-06-09 06:13:11 -04:00
|
|
|
tex->first_pixels = NULL;
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-11-18 11:24:09 -05:00
|
|
|
bpp = _cogl_get_format_bpp (format);
|
2008-04-25 09:37:36 -04:00
|
|
|
tex->bitmap.format = format;
|
|
|
|
tex->bitmap.width = gl_width - x_pot_waste;
|
|
|
|
tex->bitmap.height = gl_height - y_pot_waste;
|
|
|
|
tex->bitmap.rowstride = tex->bitmap.width * bpp;
|
|
|
|
tex->bitmap_owner = FALSE;
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
tex->gl_target = gl_target;
|
|
|
|
tex->gl_intformat = gl_int_format;
|
|
|
|
tex->gl_format = gl_int_format;
|
|
|
|
tex->gl_type = GL_UNSIGNED_BYTE;
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2009-06-04 11:04:57 -04:00
|
|
|
/* Unknown filter */
|
|
|
|
tex->min_filter = GL_FALSE;
|
|
|
|
tex->mag_filter = GL_FALSE;
|
2008-04-25 09:37:36 -04:00
|
|
|
tex->max_waste = 0;
|
2008-12-01 11:27:54 -05:00
|
|
|
|
|
|
|
/* Wrap mode not yet set */
|
|
|
|
tex->wrap_mode = GL_FALSE;
|
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
/* Create slice arrays */
|
|
|
|
tex->slice_x_spans =
|
|
|
|
g_array_sized_new (FALSE, FALSE,
|
|
|
|
sizeof (CoglTexSliceSpan), 1);
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
tex->slice_y_spans =
|
|
|
|
g_array_sized_new (FALSE, FALSE,
|
|
|
|
sizeof (CoglTexSliceSpan), 1);
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
tex->slice_gl_handles =
|
|
|
|
g_array_sized_new (FALSE, FALSE,
|
|
|
|
sizeof (GLuint), 1);
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
/* Store info for a single slice */
|
|
|
|
x_span.start = 0;
|
|
|
|
x_span.size = gl_width;
|
|
|
|
x_span.waste = x_pot_waste;
|
|
|
|
g_array_append_val (tex->slice_x_spans, x_span);
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
y_span.start = 0;
|
|
|
|
y_span.size = gl_height;
|
|
|
|
y_span.waste = y_pot_waste;
|
|
|
|
g_array_append_val (tex->slice_y_spans, y_span);
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
g_array_append_val (tex->slice_gl_handles, gl_handle);
|
2008-12-01 11:27:54 -05:00
|
|
|
|
2009-06-08 16:45:43 -04:00
|
|
|
tex->first_pixels = NULL;
|
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
return _cogl_texture_handle_new (tex);
|
|
|
|
}
|
|
|
|
|
|
|
|
guint
|
|
|
|
cogl_texture_get_width (CoglHandle handle)
|
|
|
|
{
|
|
|
|
CoglTexture *tex;
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
if (!cogl_is_texture (handle))
|
|
|
|
return 0;
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
tex = _cogl_texture_pointer_from_handle (handle);
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
return tex->bitmap.width;
|
|
|
|
}
|
|
|
|
|
|
|
|
guint
|
|
|
|
cogl_texture_get_height (CoglHandle handle)
|
|
|
|
{
|
|
|
|
CoglTexture *tex;
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
if (!cogl_is_texture (handle))
|
|
|
|
return 0;
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
tex = _cogl_texture_pointer_from_handle (handle);
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
return tex->bitmap.height;
|
|
|
|
}
|
|
|
|
|
|
|
|
CoglPixelFormat
|
|
|
|
cogl_texture_get_format (CoglHandle handle)
|
|
|
|
{
|
|
|
|
CoglTexture *tex;
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
if (!cogl_is_texture (handle))
|
|
|
|
return COGL_PIXEL_FORMAT_ANY;
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
tex = _cogl_texture_pointer_from_handle (handle);
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
return tex->bitmap.format;
|
|
|
|
}
|
|
|
|
|
|
|
|
guint
|
|
|
|
cogl_texture_get_rowstride (CoglHandle handle)
|
|
|
|
{
|
|
|
|
CoglTexture *tex;
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
if (!cogl_is_texture (handle))
|
|
|
|
return 0;
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
tex = _cogl_texture_pointer_from_handle (handle);
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
return tex->bitmap.rowstride;
|
|
|
|
}
|
|
|
|
|
|
|
|
gint
|
|
|
|
cogl_texture_get_max_waste (CoglHandle handle)
|
|
|
|
{
|
|
|
|
CoglTexture *tex;
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
if (!cogl_is_texture (handle))
|
|
|
|
return 0;
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
tex = _cogl_texture_pointer_from_handle (handle);
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
return tex->max_waste;
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
cogl_texture_is_sliced (CoglHandle handle)
|
|
|
|
{
|
|
|
|
CoglTexture *tex;
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
if (!cogl_is_texture (handle))
|
|
|
|
return FALSE;
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
tex = _cogl_texture_pointer_from_handle (handle);
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
if (tex->slice_gl_handles == NULL)
|
|
|
|
return FALSE;
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
if (tex->slice_gl_handles->len <= 1)
|
|
|
|
return FALSE;
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
cogl_texture_get_gl_texture (CoglHandle handle,
|
|
|
|
GLuint *out_gl_handle,
|
|
|
|
GLenum *out_gl_target)
|
|
|
|
{
|
|
|
|
CoglTexture *tex;
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
if (!cogl_is_texture (handle))
|
|
|
|
return FALSE;
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
tex = _cogl_texture_pointer_from_handle (handle);
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
if (tex->slice_gl_handles == NULL)
|
|
|
|
return FALSE;
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
if (tex->slice_gl_handles->len < 1)
|
|
|
|
return FALSE;
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
if (out_gl_handle != NULL)
|
|
|
|
*out_gl_handle = g_array_index (tex->slice_gl_handles, GLuint, 0);
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
if (out_gl_target != NULL)
|
|
|
|
*out_gl_target = tex->gl_target;
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2009-06-04 11:04:57 -04:00
|
|
|
void
|
|
|
|
_cogl_texture_set_filters (CoglHandle handle,
|
|
|
|
GLenum min_filter,
|
|
|
|
GLenum mag_filter)
|
2008-04-25 09:37:36 -04:00
|
|
|
{
|
|
|
|
CoglTexture *tex;
|
2009-06-04 11:04:57 -04:00
|
|
|
GLuint gl_handle;
|
|
|
|
int i;
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
if (!cogl_is_texture (handle))
|
2009-06-04 11:04:57 -04:00
|
|
|
return;
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
tex = _cogl_texture_pointer_from_handle (handle);
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2009-06-04 11:04:57 -04:00
|
|
|
/* Make sure slices were created */
|
|
|
|
if (tex->slice_gl_handles == NULL)
|
|
|
|
return;
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2009-06-04 11:04:57 -04:00
|
|
|
if (min_filter == tex->min_filter
|
|
|
|
&& mag_filter == tex->mag_filter)
|
|
|
|
return;
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2009-06-04 11:04:57 -04:00
|
|
|
/* Store new values */
|
|
|
|
tex->min_filter = min_filter;
|
|
|
|
tex->mag_filter = mag_filter;
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2009-06-04 11:04:57 -04:00
|
|
|
/* Apply new filters to every slice */
|
|
|
|
for (i=0; i<tex->slice_gl_handles->len; ++i)
|
|
|
|
{
|
|
|
|
gl_handle = g_array_index (tex->slice_gl_handles, GLuint, i);
|
|
|
|
GE( glBindTexture (tex->gl_target, gl_handle) );
|
|
|
|
GE( glTexParameteri (tex->gl_target, GL_TEXTURE_MAG_FILTER,
|
|
|
|
tex->mag_filter) );
|
|
|
|
GE( glTexParameteri (tex->gl_target, GL_TEXTURE_MIN_FILTER,
|
|
|
|
tex->min_filter) );
|
|
|
|
}
|
2008-04-25 09:37:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-06-04 11:04:57 -04:00
|
|
|
_cogl_texture_ensure_mipmaps (CoglHandle handle)
|
2008-04-25 09:37:36 -04:00
|
|
|
{
|
|
|
|
CoglTexture *tex;
|
|
|
|
int i;
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2009-06-04 11:04:57 -04:00
|
|
|
_COGL_GET_CONTEXT (ctx, NO_RETVAL);
|
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
if (!cogl_is_texture (handle))
|
|
|
|
return;
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
tex = _cogl_texture_pointer_from_handle (handle);
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2009-06-04 11:04:57 -04:00
|
|
|
/* Only update if the mipmaps are dirty */
|
|
|
|
if (!tex->auto_mipmap || !tex->mipmaps_dirty)
|
|
|
|
return;
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
/* Make sure slices were created */
|
|
|
|
if (tex->slice_gl_handles == NULL)
|
|
|
|
return;
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2009-06-04 11:04:57 -04:00
|
|
|
/* Regenerate the mipmaps on every slice */
|
|
|
|
for (i = 0; i < tex->slice_gl_handles->len; i++)
|
2008-04-25 09:37:36 -04:00
|
|
|
{
|
2009-06-04 11:04:57 -04:00
|
|
|
GLuint gl_handle = g_array_index (tex->slice_gl_handles, GLuint, i);
|
2008-11-18 11:24:09 -05:00
|
|
|
GE( glBindTexture (tex->gl_target, gl_handle) );
|
2009-06-04 11:04:57 -04:00
|
|
|
|
|
|
|
/* glGenerateMipmap is defined in the FBO extension */
|
|
|
|
if (cogl_features_available (COGL_FEATURE_OFFSCREEN))
|
|
|
|
GE( glGenerateMipmap (tex->gl_target) );
|
2009-06-09 06:13:11 -04:00
|
|
|
else if (tex->first_pixels)
|
2009-06-04 11:04:57 -04:00
|
|
|
{
|
|
|
|
CoglTexturePixel *pixel = tex->first_pixels + i;
|
|
|
|
/* Temporarily enable automatic mipmap generation and
|
|
|
|
re-upload the first pixel to cause a regeneration */
|
|
|
|
GE( glTexParameteri (tex->gl_target, GL_GENERATE_MIPMAP, GL_TRUE) );
|
|
|
|
GE( glTexSubImage2D (tex->gl_target, 0, 0, 0, 1, 1,
|
|
|
|
pixel->gl_format, pixel->gl_type,
|
|
|
|
pixel->data) );
|
|
|
|
GE( glTexParameteri (tex->gl_target, GL_GENERATE_MIPMAP, GL_FALSE) );
|
|
|
|
}
|
2008-04-25 09:37:36 -04:00
|
|
|
}
|
2009-06-04 11:04:57 -04:00
|
|
|
|
|
|
|
tex->mipmaps_dirty = FALSE;
|
2008-04-25 09:37:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
cogl_texture_set_region (CoglHandle handle,
|
|
|
|
gint src_x,
|
|
|
|
gint src_y,
|
|
|
|
gint dst_x,
|
|
|
|
gint dst_y,
|
|
|
|
guint dst_width,
|
|
|
|
guint dst_height,
|
|
|
|
gint width,
|
|
|
|
gint height,
|
|
|
|
CoglPixelFormat format,
|
|
|
|
guint rowstride,
|
|
|
|
const guchar *data)
|
|
|
|
{
|
|
|
|
CoglTexture *tex;
|
|
|
|
gint bpp;
|
|
|
|
CoglBitmap source_bmp;
|
|
|
|
CoglBitmap temp_bmp;
|
|
|
|
gboolean source_bmp_owner = FALSE;
|
|
|
|
CoglPixelFormat closest_format;
|
|
|
|
GLenum closest_gl_format;
|
|
|
|
GLenum closest_gl_type;
|
|
|
|
gboolean success;
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
/* Check if valid texture handle */
|
|
|
|
if (!cogl_is_texture (handle))
|
|
|
|
return FALSE;
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
tex = _cogl_texture_pointer_from_handle (handle);
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
/* Check for valid format */
|
|
|
|
if (format == COGL_PIXEL_FORMAT_ANY)
|
|
|
|
return FALSE;
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-09-18 09:16:15 -04:00
|
|
|
/* Shortcut out early if the image is empty */
|
|
|
|
if (width == 0 || height == 0)
|
|
|
|
return TRUE;
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
/* Init source bitmap */
|
|
|
|
source_bmp.width = width;
|
|
|
|
source_bmp.height = height;
|
|
|
|
source_bmp.format = format;
|
|
|
|
source_bmp.data = (guchar*)data;
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
/* Rowstride from width if none specified */
|
|
|
|
bpp = _cogl_get_format_bpp (format);
|
|
|
|
source_bmp.rowstride = (rowstride == 0) ? width * bpp : rowstride;
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
/* Find closest format to internal that's supported by GL */
|
|
|
|
closest_format = _cogl_pixel_format_to_gl (tex->bitmap.format,
|
|
|
|
NULL, /* don't need */
|
|
|
|
&closest_gl_format,
|
|
|
|
&closest_gl_type);
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
/* If no direct match, convert */
|
|
|
|
if (closest_format != format)
|
|
|
|
{
|
|
|
|
/* Convert to required format */
|
|
|
|
success = _cogl_bitmap_convert_and_premult (&source_bmp,
|
|
|
|
&temp_bmp,
|
|
|
|
closest_format);
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
/* Swap bitmaps if succeeded */
|
|
|
|
if (!success) return FALSE;
|
|
|
|
source_bmp = temp_bmp;
|
|
|
|
source_bmp_owner = TRUE;
|
|
|
|
}
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
/* Send data to GL */
|
|
|
|
_cogl_texture_upload_subregion_to_gl (tex,
|
|
|
|
src_x, src_y,
|
|
|
|
dst_x, dst_y,
|
|
|
|
dst_width, dst_height,
|
|
|
|
&source_bmp,
|
|
|
|
closest_gl_format,
|
|
|
|
closest_gl_type);
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
/* Free data if owner */
|
|
|
|
if (source_bmp_owner)
|
|
|
|
g_free (source_bmp.data);
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
gint
|
|
|
|
cogl_texture_get_data (CoglHandle handle,
|
|
|
|
CoglPixelFormat format,
|
|
|
|
guint rowstride,
|
|
|
|
guchar *data)
|
|
|
|
{
|
|
|
|
CoglTexture *tex;
|
|
|
|
gint bpp;
|
|
|
|
gint byte_size;
|
|
|
|
CoglPixelFormat closest_format;
|
|
|
|
gint closest_bpp;
|
|
|
|
GLenum closest_gl_format;
|
|
|
|
GLenum closest_gl_type;
|
|
|
|
CoglBitmap target_bmp;
|
|
|
|
CoglBitmap new_bmp;
|
|
|
|
gboolean success;
|
|
|
|
guchar *src;
|
|
|
|
guchar *dst;
|
|
|
|
gint y;
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
/* Check if valid texture handle */
|
|
|
|
if (!cogl_is_texture (handle))
|
|
|
|
return 0;
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
tex = _cogl_texture_pointer_from_handle (handle);
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
/* Default to internal format if none specified */
|
|
|
|
if (format == COGL_PIXEL_FORMAT_ANY)
|
|
|
|
format = tex->bitmap.format;
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
/* Rowstride from texture width if none specified */
|
|
|
|
bpp = _cogl_get_format_bpp (format);
|
|
|
|
if (rowstride == 0) rowstride = tex->bitmap.width * bpp;
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
/* Return byte size if only that requested */
|
|
|
|
byte_size = tex->bitmap.height * rowstride;
|
|
|
|
if (data == NULL) return byte_size;
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
/* Find closest format that's supported by GL */
|
|
|
|
closest_format = _cogl_pixel_format_to_gl (format,
|
|
|
|
NULL, /* don't need */
|
|
|
|
&closest_gl_format,
|
|
|
|
&closest_gl_type);
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
closest_bpp = _cogl_get_format_bpp (closest_format);
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
/* Is the requested format supported? */
|
|
|
|
if (closest_format == format)
|
|
|
|
{
|
|
|
|
/* Target user data directly */
|
|
|
|
target_bmp = tex->bitmap;
|
|
|
|
target_bmp.format = format;
|
|
|
|
target_bmp.rowstride = rowstride;
|
|
|
|
target_bmp.data = data;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Target intermediate buffer */
|
|
|
|
target_bmp = tex->bitmap;
|
|
|
|
target_bmp.format = closest_format;
|
|
|
|
target_bmp.rowstride = target_bmp.width * closest_bpp;
|
|
|
|
target_bmp.data = (guchar*) g_malloc (target_bmp.height
|
|
|
|
* target_bmp.rowstride);
|
|
|
|
}
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
/* Retrieve data from slices */
|
|
|
|
_cogl_texture_download_from_gl (tex, &target_bmp,
|
|
|
|
closest_gl_format,
|
|
|
|
closest_gl_type);
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
/* Was intermediate used? */
|
|
|
|
if (closest_format != format)
|
|
|
|
{
|
|
|
|
/* Convert to requested format */
|
|
|
|
success = _cogl_bitmap_convert_and_premult (&target_bmp,
|
|
|
|
&new_bmp,
|
|
|
|
format);
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
/* Free intermediate data and return if failed */
|
|
|
|
g_free (target_bmp.data);
|
|
|
|
if (!success) return 0;
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
/* Copy to user buffer */
|
|
|
|
for (y = 0; y < new_bmp.height; ++y)
|
|
|
|
{
|
|
|
|
src = new_bmp.data + y * new_bmp.rowstride;
|
|
|
|
dst = data + y * rowstride;
|
|
|
|
memcpy (dst, src, new_bmp.width);
|
|
|
|
}
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
/* Free converted data */
|
|
|
|
g_free (new_bmp.data);
|
|
|
|
}
|
2008-12-23 11:29:29 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
return byte_size;
|
|
|
|
}
|
|
|
|
|