2010-08-02 11:29:10 -04:00
|
|
|
/*
|
|
|
|
* Cogl
|
|
|
|
*
|
|
|
|
* An object oriented GL/GLES Abstraction/Utility Layer
|
|
|
|
*
|
2011-02-17 08:11:34 -05:00
|
|
|
* Copyright (C) 2010,2011 Intel Corporation.
|
2010-08-02 11:29:10 -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.
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Neil Roberts <neil@linux.intel.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "cogl-atlas.h"
|
|
|
|
#include "cogl-rectangle-map.h"
|
2010-11-04 18:25:52 -04:00
|
|
|
#include "cogl-context-private.h"
|
2010-08-02 11:29:10 -04:00
|
|
|
#include "cogl-texture-private.h"
|
|
|
|
#include "cogl-texture-2d-private.h"
|
2010-08-12 04:08:30 -04:00
|
|
|
#include "cogl-texture-driver.h"
|
2010-10-27 13:54:57 -04:00
|
|
|
#include "cogl-pipeline-opengl-private.h"
|
2010-08-02 11:29:10 -04:00
|
|
|
#include "cogl-debug.h"
|
2011-01-20 12:45:47 -05:00
|
|
|
#include "cogl-framebuffer-private.h"
|
|
|
|
#include "cogl-blit.h"
|
2012-02-13 18:02:04 -05:00
|
|
|
#include "cogl-private.h"
|
2010-08-02 11:29:10 -04:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2010-11-27 08:06:38 -05:00
|
|
|
static void _cogl_atlas_free (CoglAtlas *atlas);
|
|
|
|
|
|
|
|
COGL_OBJECT_INTERNAL_DEFINE (Atlas, atlas);
|
|
|
|
|
2010-08-02 11:29:10 -04:00
|
|
|
CoglAtlas *
|
2010-08-05 08:24:02 -04:00
|
|
|
_cogl_atlas_new (CoglPixelFormat texture_format,
|
|
|
|
CoglAtlasFlags flags,
|
|
|
|
CoglAtlasUpdatePositionCallback update_position_cb)
|
2010-08-02 11:29:10 -04:00
|
|
|
{
|
|
|
|
CoglAtlas *atlas = g_new (CoglAtlas, 1);
|
|
|
|
|
|
|
|
atlas->update_position_cb = update_position_cb;
|
|
|
|
atlas->map = NULL;
|
|
|
|
atlas->texture = NULL;
|
2010-08-05 08:24:02 -04:00
|
|
|
atlas->flags = flags;
|
|
|
|
atlas->texture_format = texture_format;
|
2011-03-14 13:58:31 -04:00
|
|
|
g_hook_list_init (&atlas->pre_reorganize_callbacks, sizeof (GHook));
|
|
|
|
g_hook_list_init (&atlas->post_reorganize_callbacks, sizeof (GHook));
|
2010-08-02 11:29:10 -04:00
|
|
|
|
2010-11-27 08:06:38 -05:00
|
|
|
return _cogl_atlas_object_new (atlas);
|
2010-08-02 11:29:10 -04:00
|
|
|
}
|
|
|
|
|
2010-11-27 08:06:38 -05:00
|
|
|
static void
|
2010-08-02 11:29:10 -04:00
|
|
|
_cogl_atlas_free (CoglAtlas *atlas)
|
|
|
|
{
|
2010-11-27 08:06:38 -05:00
|
|
|
COGL_NOTE (ATLAS, "%p: Atlas destroyed", atlas);
|
|
|
|
|
2010-08-02 11:29:10 -04:00
|
|
|
if (atlas->texture)
|
2012-04-16 09:14:10 -04:00
|
|
|
cogl_object_unref (atlas->texture);
|
2010-08-02 11:29:10 -04:00
|
|
|
if (atlas->map)
|
|
|
|
_cogl_rectangle_map_free (atlas->map);
|
|
|
|
|
2011-03-14 13:58:31 -04:00
|
|
|
g_hook_list_clear (&atlas->pre_reorganize_callbacks);
|
|
|
|
g_hook_list_clear (&atlas->post_reorganize_callbacks);
|
2010-08-02 11:29:10 -04:00
|
|
|
|
|
|
|
g_free (atlas);
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct _CoglAtlasRepositionData
|
|
|
|
{
|
|
|
|
/* The current user data for this texture */
|
|
|
|
void *user_data;
|
|
|
|
/* The old and new positions of the texture */
|
|
|
|
CoglRectangleMapEntry old_position;
|
|
|
|
CoglRectangleMapEntry new_position;
|
|
|
|
} CoglAtlasRepositionData;
|
|
|
|
|
|
|
|
static void
|
|
|
|
_cogl_atlas_migrate (CoglAtlas *atlas,
|
|
|
|
unsigned int n_textures,
|
|
|
|
CoglAtlasRepositionData *textures,
|
2012-04-16 09:14:10 -04:00
|
|
|
CoglTexture *old_texture,
|
|
|
|
CoglTexture *new_texture,
|
2010-08-02 11:29:10 -04:00
|
|
|
void *skip_user_data)
|
|
|
|
{
|
|
|
|
unsigned int i;
|
2011-01-20 12:45:47 -05:00
|
|
|
CoglBlitData blit_data;
|
2010-08-02 11:29:10 -04:00
|
|
|
|
2010-08-05 08:24:02 -04:00
|
|
|
/* If the 'disable migrate' flag is set then we won't actually copy
|
|
|
|
the textures to their new location. Instead we'll just invoke the
|
|
|
|
callback to update the position */
|
|
|
|
if ((atlas->flags & COGL_ATLAS_DISABLE_MIGRATION))
|
|
|
|
for (i = 0; i < n_textures; i++)
|
2010-08-02 11:29:10 -04:00
|
|
|
/* Update the texture position */
|
|
|
|
atlas->update_position_cb (textures[i].user_data,
|
|
|
|
new_texture,
|
|
|
|
&textures[i].new_position);
|
2010-08-05 08:24:02 -04:00
|
|
|
else
|
|
|
|
{
|
2011-01-20 12:45:47 -05:00
|
|
|
_cogl_blit_begin (&blit_data, new_texture, old_texture);
|
2010-08-05 08:24:02 -04:00
|
|
|
|
|
|
|
for (i = 0; i < n_textures; i++)
|
|
|
|
{
|
|
|
|
/* Skip the texture that is being added because it doesn't contain
|
|
|
|
any data yet */
|
|
|
|
if (textures[i].user_data != skip_user_data)
|
2011-01-20 12:45:47 -05:00
|
|
|
_cogl_blit (&blit_data,
|
|
|
|
textures[i].old_position.x,
|
|
|
|
textures[i].old_position.y,
|
|
|
|
textures[i].new_position.x,
|
|
|
|
textures[i].new_position.y,
|
|
|
|
textures[i].new_position.width,
|
|
|
|
textures[i].new_position.height);
|
2010-08-05 08:24:02 -04:00
|
|
|
|
|
|
|
/* Update the texture position */
|
|
|
|
atlas->update_position_cb (textures[i].user_data,
|
|
|
|
new_texture,
|
|
|
|
&textures[i].new_position);
|
|
|
|
}
|
2010-08-02 11:29:10 -04:00
|
|
|
|
2011-01-20 12:45:47 -05:00
|
|
|
_cogl_blit_end (&blit_data);
|
2010-08-05 08:24:02 -04:00
|
|
|
}
|
2010-08-02 11:29:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct _CoglAtlasGetRectanglesData
|
|
|
|
{
|
|
|
|
CoglAtlasRepositionData *textures;
|
|
|
|
/* Number of textures found so far */
|
|
|
|
unsigned int n_textures;
|
|
|
|
} CoglAtlasGetRectanglesData;
|
|
|
|
|
|
|
|
static void
|
|
|
|
_cogl_atlas_get_rectangles_cb (const CoglRectangleMapEntry *rectangle,
|
|
|
|
void *rect_data,
|
|
|
|
void *user_data)
|
|
|
|
{
|
|
|
|
CoglAtlasGetRectanglesData *data = user_data;
|
|
|
|
|
|
|
|
data->textures[data->n_textures].old_position = *rectangle;
|
|
|
|
data->textures[data->n_textures++].user_data = rect_data;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
_cogl_atlas_get_next_size (unsigned int *map_width,
|
|
|
|
unsigned int *map_height)
|
|
|
|
{
|
|
|
|
/* Double the size of the texture by increasing whichever dimension
|
|
|
|
is smaller */
|
|
|
|
if (*map_width < *map_height)
|
|
|
|
*map_width <<= 1;
|
|
|
|
else
|
|
|
|
*map_height <<= 1;
|
|
|
|
}
|
|
|
|
|
2010-08-12 04:59:58 -04:00
|
|
|
static void
|
|
|
|
_cogl_atlas_get_initial_size (CoglPixelFormat format,
|
|
|
|
unsigned int *map_width,
|
|
|
|
unsigned int *map_height)
|
|
|
|
{
|
|
|
|
unsigned int size;
|
|
|
|
GLenum gl_intformat;
|
|
|
|
GLenum gl_type;
|
|
|
|
|
2011-07-07 07:48:24 -04:00
|
|
|
_COGL_GET_CONTEXT (ctx, NO_RETVAL);
|
|
|
|
|
2012-03-22 12:32:56 -04:00
|
|
|
ctx->driver_vtable->pixel_format_to_gl (ctx,
|
|
|
|
format,
|
|
|
|
&gl_intformat,
|
|
|
|
NULL, /* gl_format */
|
|
|
|
&gl_type);
|
2010-08-12 04:59:58 -04:00
|
|
|
|
|
|
|
/* At least on Intel hardware, the texture size will be rounded up
|
|
|
|
to at least 1MB so we might as well try to aim for that as an
|
|
|
|
initial minimum size. If the format is only 1 byte per pixel we
|
|
|
|
can use 1024x1024, otherwise we'll assume it will take 4 bytes
|
|
|
|
per pixel and use 512x512. */
|
2012-02-13 18:02:04 -05:00
|
|
|
if (_cogl_pixel_format_get_bytes_per_pixel (format) == 1)
|
2010-08-12 04:59:58 -04:00
|
|
|
size = 1024;
|
|
|
|
else
|
|
|
|
size = 512;
|
|
|
|
|
|
|
|
/* Some platforms might not support this large size so we'll
|
|
|
|
decrease the size until it can */
|
|
|
|
while (size > 1 &&
|
2012-03-22 13:04:47 -04:00
|
|
|
!ctx->texture_driver->size_supported (ctx,
|
|
|
|
GL_TEXTURE_2D,
|
2010-08-12 04:59:58 -04:00
|
|
|
gl_intformat,
|
|
|
|
gl_type,
|
|
|
|
size, size))
|
|
|
|
size >>= 1;
|
|
|
|
|
|
|
|
*map_width = size;
|
|
|
|
*map_height = size;
|
|
|
|
}
|
|
|
|
|
2010-08-02 11:29:10 -04:00
|
|
|
static CoglRectangleMap *
|
2010-08-12 04:08:30 -04:00
|
|
|
_cogl_atlas_create_map (CoglPixelFormat format,
|
|
|
|
unsigned int map_width,
|
2010-08-02 11:29:10 -04:00
|
|
|
unsigned int map_height,
|
|
|
|
unsigned int n_textures,
|
|
|
|
CoglAtlasRepositionData *textures)
|
|
|
|
{
|
2010-08-12 04:08:30 -04:00
|
|
|
GLenum gl_intformat;
|
|
|
|
GLenum gl_type;
|
2010-08-02 11:29:10 -04:00
|
|
|
|
2011-07-07 07:48:24 -04:00
|
|
|
_COGL_GET_CONTEXT (ctx, NULL);
|
|
|
|
|
2012-03-22 12:32:56 -04:00
|
|
|
ctx->driver_vtable->pixel_format_to_gl (ctx,
|
|
|
|
format,
|
|
|
|
&gl_intformat,
|
|
|
|
NULL, /* gl_format */
|
|
|
|
&gl_type);
|
2010-08-02 11:29:10 -04:00
|
|
|
|
|
|
|
/* Keep trying increasingly larger atlases until we can fit all of
|
|
|
|
the textures */
|
2012-03-22 13:04:47 -04:00
|
|
|
while (ctx->texture_driver->size_supported (ctx,
|
|
|
|
GL_TEXTURE_2D,
|
2010-08-12 04:08:30 -04:00
|
|
|
gl_intformat,
|
|
|
|
gl_type,
|
|
|
|
map_width, map_height))
|
2010-08-02 11:29:10 -04:00
|
|
|
{
|
|
|
|
CoglRectangleMap *new_atlas = _cogl_rectangle_map_new (map_width,
|
|
|
|
map_height,
|
|
|
|
NULL);
|
|
|
|
unsigned int i;
|
|
|
|
|
2010-08-06 13:42:01 -04:00
|
|
|
COGL_NOTE (ATLAS, "Trying to resize the atlas to %ux%u",
|
|
|
|
map_width, map_height);
|
|
|
|
|
2010-08-02 11:29:10 -04:00
|
|
|
/* Add all of the textures and keep track of the new position */
|
|
|
|
for (i = 0; i < n_textures; i++)
|
|
|
|
if (!_cogl_rectangle_map_add (new_atlas,
|
|
|
|
textures[i].old_position.width,
|
|
|
|
textures[i].old_position.height,
|
|
|
|
textures[i].user_data,
|
|
|
|
&textures[i].new_position))
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* If the atlas can contain all of the textures then we have a
|
|
|
|
winner */
|
|
|
|
if (i >= n_textures)
|
|
|
|
return new_atlas;
|
2010-08-06 13:42:01 -04:00
|
|
|
else
|
|
|
|
COGL_NOTE (ATLAS, "Atlas size abandoned after trying "
|
|
|
|
"%u out of %u textures",
|
|
|
|
i, n_textures);
|
2010-08-02 11:29:10 -04:00
|
|
|
|
|
|
|
_cogl_rectangle_map_free (new_atlas);
|
|
|
|
_cogl_atlas_get_next_size (&map_width, &map_height);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If we get here then there's no atlas that can accommodate all of
|
|
|
|
the rectangles */
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-04-16 09:14:10 -04:00
|
|
|
static CoglTexture2D *
|
2010-08-05 08:24:02 -04:00
|
|
|
_cogl_atlas_create_texture (CoglAtlas *atlas,
|
|
|
|
int width,
|
|
|
|
int height)
|
|
|
|
{
|
2012-04-16 09:14:10 -04:00
|
|
|
CoglTexture2D *tex;
|
2010-08-05 08:24:02 -04:00
|
|
|
|
2012-04-16 09:14:10 -04:00
|
|
|
_COGL_GET_CONTEXT (ctx, NULL);
|
2012-03-13 10:46:18 -04:00
|
|
|
|
2010-08-05 08:24:02 -04:00
|
|
|
if ((atlas->flags & COGL_ATLAS_CLEAR_TEXTURE))
|
|
|
|
{
|
Switch use of primitive glib types to c99 equivalents
The coding style has for a long time said to avoid using redundant glib
data types such as gint or gchar etc because we feel that they make the
code look unnecessarily foreign to developers coming from outside of the
Gnome developer community.
Note: When we tried to find the historical rationale for the types we
just found that they were apparently only added for consistent syntax
highlighting which didn't seem that compelling.
Up until now we have been continuing to use some of the platform
specific type such as gint{8,16,32,64} and gsize but this patch switches
us over to using the standard c99 equivalents instead so we can further
ensure that our code looks familiar to the widest range of C developers
who might potentially contribute to Cogl.
So instead of using the gint{8,16,32,64} and guint{8,16,32,64} types this
switches all Cogl code to instead use the int{8,16,32,64}_t and
uint{8,16,32,64}_t c99 types instead.
Instead of gsize we now use size_t
For now we are not going to use the c99 _Bool type and instead we have
introduced a new CoglBool type to use instead of gboolean.
Reviewed-by: Neil Roberts <neil@linux.intel.com>
(cherry picked from commit 5967dad2400d32ca6319cef6cb572e81bf2c15f0)
2012-04-16 16:56:40 -04:00
|
|
|
uint8_t *clear_data;
|
2010-08-05 08:24:02 -04:00
|
|
|
CoglBitmap *clear_bmp;
|
2012-02-13 18:02:04 -05:00
|
|
|
int bpp = _cogl_pixel_format_get_bytes_per_pixel (atlas->texture_format);
|
2010-08-05 08:24:02 -04:00
|
|
|
|
|
|
|
/* Create a buffer of zeroes to initially clear the texture */
|
|
|
|
clear_data = g_malloc0 (width * height * bpp);
|
2012-03-13 10:46:18 -04:00
|
|
|
clear_bmp = cogl_bitmap_new_for_data (ctx,
|
|
|
|
width,
|
|
|
|
height,
|
|
|
|
atlas->texture_format,
|
|
|
|
width * bpp,
|
|
|
|
clear_data);
|
2010-08-05 08:24:02 -04:00
|
|
|
|
2012-04-04 10:10:04 -04:00
|
|
|
tex = cogl_texture_2d_new_from_bitmap (clear_bmp,
|
|
|
|
atlas->texture_format,
|
|
|
|
NULL);
|
2010-08-05 08:24:02 -04:00
|
|
|
cogl_object_unref (clear_bmp);
|
2012-03-13 10:46:18 -04:00
|
|
|
|
|
|
|
g_free (clear_data);
|
2010-08-05 08:24:02 -04:00
|
|
|
}
|
|
|
|
else
|
2011-05-16 19:05:54 -04:00
|
|
|
{
|
|
|
|
tex = cogl_texture_2d_new_with_size (ctx,
|
|
|
|
width, height,
|
|
|
|
atlas->texture_format,
|
|
|
|
NULL);
|
|
|
|
}
|
2010-08-05 08:24:02 -04:00
|
|
|
|
|
|
|
return tex;
|
|
|
|
}
|
|
|
|
|
2010-08-02 11:29:10 -04:00
|
|
|
static int
|
|
|
|
_cogl_atlas_compare_size_cb (const void *a,
|
|
|
|
const void *b)
|
|
|
|
{
|
|
|
|
const CoglAtlasRepositionData *ta = a;
|
|
|
|
const CoglAtlasRepositionData *tb = b;
|
|
|
|
unsigned int a_size, b_size;
|
|
|
|
|
|
|
|
a_size = ta->old_position.width * ta->old_position.height;
|
|
|
|
b_size = tb->old_position.width * tb->old_position.height;
|
|
|
|
|
|
|
|
return a_size < b_size ? 1 : a_size > b_size ? -1 : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-02-17 08:11:34 -05:00
|
|
|
_cogl_atlas_notify_pre_reorganize (CoglAtlas *atlas)
|
2010-08-02 11:29:10 -04:00
|
|
|
{
|
2011-03-14 13:58:31 -04:00
|
|
|
g_hook_list_invoke (&atlas->pre_reorganize_callbacks, FALSE);
|
2011-02-17 08:11:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
_cogl_atlas_notify_post_reorganize (CoglAtlas *atlas)
|
|
|
|
{
|
2011-03-14 13:58:31 -04:00
|
|
|
g_hook_list_invoke (&atlas->post_reorganize_callbacks, FALSE);
|
2010-08-02 11:29:10 -04:00
|
|
|
}
|
|
|
|
|
Switch use of primitive glib types to c99 equivalents
The coding style has for a long time said to avoid using redundant glib
data types such as gint or gchar etc because we feel that they make the
code look unnecessarily foreign to developers coming from outside of the
Gnome developer community.
Note: When we tried to find the historical rationale for the types we
just found that they were apparently only added for consistent syntax
highlighting which didn't seem that compelling.
Up until now we have been continuing to use some of the platform
specific type such as gint{8,16,32,64} and gsize but this patch switches
us over to using the standard c99 equivalents instead so we can further
ensure that our code looks familiar to the widest range of C developers
who might potentially contribute to Cogl.
So instead of using the gint{8,16,32,64} and guint{8,16,32,64} types this
switches all Cogl code to instead use the int{8,16,32,64}_t and
uint{8,16,32,64}_t c99 types instead.
Instead of gsize we now use size_t
For now we are not going to use the c99 _Bool type and instead we have
introduced a new CoglBool type to use instead of gboolean.
Reviewed-by: Neil Roberts <neil@linux.intel.com>
(cherry picked from commit 5967dad2400d32ca6319cef6cb572e81bf2c15f0)
2012-04-16 16:56:40 -04:00
|
|
|
CoglBool
|
2010-08-02 11:29:10 -04:00
|
|
|
_cogl_atlas_reserve_space (CoglAtlas *atlas,
|
|
|
|
unsigned int width,
|
|
|
|
unsigned int height,
|
|
|
|
void *user_data)
|
|
|
|
{
|
|
|
|
CoglAtlasGetRectanglesData data;
|
|
|
|
CoglRectangleMap *new_map;
|
2012-04-16 09:14:10 -04:00
|
|
|
CoglTexture2D *new_tex;
|
2010-08-02 11:29:10 -04:00
|
|
|
unsigned int map_width, map_height;
|
Switch use of primitive glib types to c99 equivalents
The coding style has for a long time said to avoid using redundant glib
data types such as gint or gchar etc because we feel that they make the
code look unnecessarily foreign to developers coming from outside of the
Gnome developer community.
Note: When we tried to find the historical rationale for the types we
just found that they were apparently only added for consistent syntax
highlighting which didn't seem that compelling.
Up until now we have been continuing to use some of the platform
specific type such as gint{8,16,32,64} and gsize but this patch switches
us over to using the standard c99 equivalents instead so we can further
ensure that our code looks familiar to the widest range of C developers
who might potentially contribute to Cogl.
So instead of using the gint{8,16,32,64} and guint{8,16,32,64} types this
switches all Cogl code to instead use the int{8,16,32,64}_t and
uint{8,16,32,64}_t c99 types instead.
Instead of gsize we now use size_t
For now we are not going to use the c99 _Bool type and instead we have
introduced a new CoglBool type to use instead of gboolean.
Reviewed-by: Neil Roberts <neil@linux.intel.com>
(cherry picked from commit 5967dad2400d32ca6319cef6cb572e81bf2c15f0)
2012-04-16 16:56:40 -04:00
|
|
|
CoglBool ret;
|
2010-08-02 11:29:10 -04:00
|
|
|
CoglRectangleMapEntry new_position;
|
|
|
|
|
|
|
|
/* Check if we can fit the rectangle into the existing map */
|
|
|
|
if (atlas->map &&
|
|
|
|
_cogl_rectangle_map_add (atlas->map, width, height,
|
|
|
|
user_data,
|
|
|
|
&new_position))
|
|
|
|
{
|
2010-11-27 08:15:02 -05:00
|
|
|
COGL_NOTE (ATLAS, "%p: Atlas is %ix%i, has %i textures and is %i%% waste",
|
|
|
|
atlas,
|
2010-08-02 11:29:10 -04:00
|
|
|
_cogl_rectangle_map_get_width (atlas->map),
|
|
|
|
_cogl_rectangle_map_get_height (atlas->map),
|
|
|
|
_cogl_rectangle_map_get_n_rectangles (atlas->map),
|
2011-02-15 07:50:15 -05:00
|
|
|
/* waste as a percentage */
|
|
|
|
_cogl_rectangle_map_get_remaining_space (atlas->map) *
|
|
|
|
100 / (_cogl_rectangle_map_get_width (atlas->map) *
|
|
|
|
_cogl_rectangle_map_get_height (atlas->map)));
|
2010-08-02 11:29:10 -04:00
|
|
|
|
|
|
|
atlas->update_position_cb (user_data,
|
|
|
|
atlas->texture,
|
|
|
|
&new_position);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2011-02-17 08:11:34 -05:00
|
|
|
/* If we make it here then we need to reorganize the atlas. First
|
|
|
|
we'll notify any users of the atlas that this is going to happen
|
|
|
|
so that for example in CoglAtlasTexture it can notify that the
|
|
|
|
storage has changed and cause a flush */
|
|
|
|
_cogl_atlas_notify_pre_reorganize (atlas);
|
|
|
|
|
|
|
|
/* Get an array of all the textures currently in the atlas. */
|
2010-08-02 11:29:10 -04:00
|
|
|
data.n_textures = 0;
|
|
|
|
if (atlas->map == NULL)
|
|
|
|
data.textures = g_malloc (sizeof (CoglAtlasRepositionData));
|
|
|
|
else
|
|
|
|
{
|
|
|
|
unsigned int n_rectangles =
|
|
|
|
_cogl_rectangle_map_get_n_rectangles (atlas->map);
|
|
|
|
data.textures = g_malloc (sizeof (CoglAtlasRepositionData) *
|
|
|
|
(n_rectangles + 1));
|
|
|
|
_cogl_rectangle_map_foreach (atlas->map,
|
|
|
|
_cogl_atlas_get_rectangles_cb,
|
|
|
|
&data);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add the new rectangle as a dummy texture so that it can be
|
|
|
|
positioned with the rest */
|
|
|
|
data.textures[data.n_textures].old_position.x = 0;
|
|
|
|
data.textures[data.n_textures].old_position.y = 0;
|
|
|
|
data.textures[data.n_textures].old_position.width = width;
|
|
|
|
data.textures[data.n_textures].old_position.height = height;
|
|
|
|
data.textures[data.n_textures++].user_data = user_data;
|
|
|
|
|
|
|
|
/* The atlasing algorithm works a lot better if the rectangles are
|
|
|
|
added in decreasing order of size so we'll first sort the
|
|
|
|
array */
|
|
|
|
qsort (data.textures, data.n_textures,
|
|
|
|
sizeof (CoglAtlasRepositionData),
|
|
|
|
_cogl_atlas_compare_size_cb);
|
|
|
|
|
|
|
|
/* Try to create a new atlas that can contain all of the textures */
|
|
|
|
if (atlas->map)
|
|
|
|
{
|
|
|
|
map_width = _cogl_rectangle_map_get_width (atlas->map);
|
|
|
|
map_height = _cogl_rectangle_map_get_height (atlas->map);
|
|
|
|
|
2010-08-06 13:42:43 -04:00
|
|
|
/* If there is enough space in for the new rectangle in the
|
|
|
|
existing atlas with at least 6% waste we'll start with the
|
|
|
|
same size, otherwise we'll immediately double it */
|
|
|
|
if ((map_width * map_height -
|
|
|
|
_cogl_rectangle_map_get_remaining_space (atlas->map) +
|
|
|
|
width * height) * 53 / 50 >
|
|
|
|
map_width * map_height)
|
2010-08-02 11:29:10 -04:00
|
|
|
_cogl_atlas_get_next_size (&map_width, &map_height);
|
|
|
|
}
|
|
|
|
else
|
2010-08-12 04:59:58 -04:00
|
|
|
_cogl_atlas_get_initial_size (atlas->texture_format,
|
|
|
|
&map_width, &map_height);
|
2010-08-02 11:29:10 -04:00
|
|
|
|
2010-08-12 04:08:30 -04:00
|
|
|
new_map = _cogl_atlas_create_map (atlas->texture_format,
|
|
|
|
map_width, map_height,
|
2010-08-02 11:29:10 -04:00
|
|
|
data.n_textures, data.textures);
|
|
|
|
|
|
|
|
/* If we can't create a map with the texture then give up */
|
|
|
|
if (new_map == NULL)
|
|
|
|
{
|
2010-11-27 08:15:02 -05:00
|
|
|
COGL_NOTE (ATLAS, "%p: Could not fit texture in the atlas", atlas);
|
2010-08-02 11:29:10 -04:00
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
/* We need to migrate the existing textures into a new texture */
|
2010-08-05 08:24:02 -04:00
|
|
|
else if ((new_tex = _cogl_atlas_create_texture
|
|
|
|
(atlas,
|
|
|
|
_cogl_rectangle_map_get_width (new_map),
|
2012-04-16 09:14:10 -04:00
|
|
|
_cogl_rectangle_map_get_height (new_map))) == NULL)
|
2010-08-02 11:29:10 -04:00
|
|
|
{
|
2010-11-27 08:15:02 -05:00
|
|
|
COGL_NOTE (ATLAS, "%p: Could not create a CoglTexture2D", atlas);
|
2010-08-02 11:29:10 -04:00
|
|
|
_cogl_rectangle_map_free (new_map);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int waste;
|
|
|
|
|
|
|
|
COGL_NOTE (ATLAS,
|
2010-11-27 08:15:02 -05:00
|
|
|
"%p: Atlas %s with size %ix%i",
|
|
|
|
atlas,
|
2010-08-02 11:29:10 -04:00
|
|
|
atlas->map == NULL ||
|
|
|
|
_cogl_rectangle_map_get_width (atlas->map) !=
|
|
|
|
_cogl_rectangle_map_get_width (new_map) ||
|
|
|
|
_cogl_rectangle_map_get_height (atlas->map) !=
|
|
|
|
_cogl_rectangle_map_get_height (new_map) ?
|
|
|
|
"resized" : "reorganized",
|
|
|
|
_cogl_rectangle_map_get_width (new_map),
|
|
|
|
_cogl_rectangle_map_get_height (new_map));
|
|
|
|
|
|
|
|
if (atlas->map)
|
|
|
|
{
|
|
|
|
/* Move all the textures to the right position in the new
|
|
|
|
texture. This will also update the texture's rectangle */
|
|
|
|
_cogl_atlas_migrate (atlas,
|
|
|
|
data.n_textures,
|
|
|
|
data.textures,
|
|
|
|
atlas->texture,
|
2012-04-16 09:14:10 -04:00
|
|
|
COGL_TEXTURE (new_tex),
|
2010-08-02 11:29:10 -04:00
|
|
|
user_data);
|
|
|
|
_cogl_rectangle_map_free (atlas->map);
|
2012-04-16 09:14:10 -04:00
|
|
|
cogl_object_unref (atlas->texture);
|
2010-08-02 11:29:10 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
/* We know there's only one texture so we can just directly
|
|
|
|
update the rectangle from its new position */
|
|
|
|
atlas->update_position_cb (data.textures[0].user_data,
|
2012-04-16 09:14:10 -04:00
|
|
|
COGL_TEXTURE (new_tex),
|
2010-08-02 11:29:10 -04:00
|
|
|
&data.textures[0].new_position);
|
|
|
|
|
|
|
|
atlas->map = new_map;
|
2012-04-16 09:14:10 -04:00
|
|
|
atlas->texture = COGL_TEXTURE (new_tex);
|
2010-08-02 11:29:10 -04:00
|
|
|
|
|
|
|
waste = (_cogl_rectangle_map_get_remaining_space (atlas->map) *
|
|
|
|
100 / (_cogl_rectangle_map_get_width (atlas->map) *
|
|
|
|
_cogl_rectangle_map_get_height (atlas->map)));
|
|
|
|
|
2010-11-27 08:15:02 -05:00
|
|
|
COGL_NOTE (ATLAS, "%p: Atlas is %ix%i, has %i textures and is %i%% waste",
|
|
|
|
atlas,
|
2010-08-02 11:29:10 -04:00
|
|
|
_cogl_rectangle_map_get_width (atlas->map),
|
|
|
|
_cogl_rectangle_map_get_height (atlas->map),
|
|
|
|
_cogl_rectangle_map_get_n_rectangles (atlas->map),
|
|
|
|
waste);
|
|
|
|
|
|
|
|
ret = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_free (data.textures);
|
|
|
|
|
2011-02-17 08:11:34 -05:00
|
|
|
_cogl_atlas_notify_post_reorganize (atlas);
|
|
|
|
|
2010-08-02 11:29:10 -04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
_cogl_atlas_remove (CoglAtlas *atlas,
|
|
|
|
const CoglRectangleMapEntry *rectangle)
|
|
|
|
{
|
|
|
|
_cogl_rectangle_map_remove (atlas->map, rectangle);
|
|
|
|
|
2010-11-27 08:15:02 -05:00
|
|
|
COGL_NOTE (ATLAS, "%p: Removed rectangle sized %ix%i",
|
|
|
|
atlas,
|
2010-08-02 11:29:10 -04:00
|
|
|
rectangle->width,
|
|
|
|
rectangle->height);
|
2010-11-27 08:15:02 -05:00
|
|
|
COGL_NOTE (ATLAS, "%p: Atlas is %ix%i, has %i textures and is %i%% waste",
|
|
|
|
atlas,
|
2010-08-02 11:29:10 -04:00
|
|
|
_cogl_rectangle_map_get_width (atlas->map),
|
|
|
|
_cogl_rectangle_map_get_height (atlas->map),
|
|
|
|
_cogl_rectangle_map_get_n_rectangles (atlas->map),
|
|
|
|
_cogl_rectangle_map_get_remaining_space (atlas->map) *
|
|
|
|
100 / (_cogl_rectangle_map_get_width (atlas->map) *
|
|
|
|
_cogl_rectangle_map_get_height (atlas->map)));
|
|
|
|
};
|
|
|
|
|
2012-04-16 09:14:10 -04:00
|
|
|
CoglTexture *
|
2010-08-02 11:29:10 -04:00
|
|
|
_cogl_atlas_copy_rectangle (CoglAtlas *atlas,
|
|
|
|
unsigned int x,
|
|
|
|
unsigned int y,
|
|
|
|
unsigned int width,
|
|
|
|
unsigned int height,
|
|
|
|
CoglTextureFlags flags,
|
|
|
|
CoglPixelFormat format)
|
|
|
|
{
|
2012-04-16 09:14:10 -04:00
|
|
|
CoglTexture *tex;
|
2011-01-20 12:45:47 -05:00
|
|
|
CoglBlitData blit_data;
|
2010-08-02 11:29:10 -04:00
|
|
|
|
|
|
|
/* Create a new texture at the right size */
|
|
|
|
tex = cogl_texture_new_with_size (width, height, flags, format);
|
|
|
|
|
|
|
|
/* Blit the data out of the atlas to the new texture. If FBOs
|
|
|
|
aren't available this will end up having to copy the entire
|
|
|
|
atlas texture */
|
2011-01-20 12:45:47 -05:00
|
|
|
_cogl_blit_begin (&blit_data, tex, atlas->texture);
|
|
|
|
_cogl_blit (&blit_data,
|
2010-08-02 11:29:10 -04:00
|
|
|
x, y,
|
|
|
|
0, 0,
|
|
|
|
width, height);
|
2011-01-20 12:45:47 -05:00
|
|
|
_cogl_blit_end (&blit_data);
|
2010-08-02 11:29:10 -04:00
|
|
|
|
|
|
|
return tex;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
_cogl_atlas_add_reorganize_callback (CoglAtlas *atlas,
|
2011-03-14 13:58:31 -04:00
|
|
|
GHookFunc pre_callback,
|
|
|
|
GHookFunc post_callback,
|
2010-08-02 11:29:10 -04:00
|
|
|
void *user_data)
|
|
|
|
{
|
2011-02-17 08:11:34 -05:00
|
|
|
if (pre_callback)
|
2011-03-14 13:58:31 -04:00
|
|
|
{
|
|
|
|
GHook *hook = g_hook_alloc (&atlas->post_reorganize_callbacks);
|
|
|
|
hook->func = pre_callback;
|
|
|
|
hook->data = user_data;
|
|
|
|
g_hook_prepend (&atlas->pre_reorganize_callbacks, hook);
|
|
|
|
}
|
2011-02-17 08:11:34 -05:00
|
|
|
if (post_callback)
|
2011-03-14 13:58:31 -04:00
|
|
|
{
|
|
|
|
GHook *hook = g_hook_alloc (&atlas->pre_reorganize_callbacks);
|
|
|
|
hook->func = post_callback;
|
|
|
|
hook->data = user_data;
|
|
|
|
g_hook_prepend (&atlas->post_reorganize_callbacks, hook);
|
|
|
|
}
|
2010-08-02 11:29:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
_cogl_atlas_remove_reorganize_callback (CoglAtlas *atlas,
|
2011-03-14 13:58:31 -04:00
|
|
|
GHookFunc pre_callback,
|
|
|
|
GHookFunc post_callback,
|
2010-08-02 11:29:10 -04:00
|
|
|
void *user_data)
|
|
|
|
{
|
2011-02-17 08:11:34 -05:00
|
|
|
if (pre_callback)
|
2011-03-14 13:58:31 -04:00
|
|
|
{
|
|
|
|
GHook *hook = g_hook_find_func_data (&atlas->pre_reorganize_callbacks,
|
|
|
|
FALSE,
|
|
|
|
pre_callback,
|
|
|
|
user_data);
|
|
|
|
if (hook)
|
|
|
|
g_hook_destroy_link (&atlas->pre_reorganize_callbacks, hook);
|
|
|
|
}
|
2011-02-17 08:11:34 -05:00
|
|
|
if (post_callback)
|
2011-03-14 13:58:31 -04:00
|
|
|
{
|
|
|
|
GHook *hook = g_hook_find_func_data (&atlas->post_reorganize_callbacks,
|
|
|
|
FALSE,
|
|
|
|
post_callback,
|
|
|
|
user_data);
|
|
|
|
if (hook)
|
|
|
|
g_hook_destroy_link (&atlas->post_reorganize_callbacks, hook);
|
|
|
|
}
|
2010-08-02 11:29:10 -04:00
|
|
|
}
|