2008-05-28 14:09:22 +00:00
|
|
|
/*
|
|
|
|
* Clutter.
|
|
|
|
*
|
|
|
|
* An OpenGL based 'interactive canvas' library.
|
|
|
|
*
|
|
|
|
* Authored By Matthew Allum <mallum@openedhand.com>
|
|
|
|
*
|
|
|
|
* Copyright (C) 2008 OpenedHand
|
|
|
|
*
|
|
|
|
* 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
|
2008-10-30 17:11:29 +00:00
|
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses>.
|
2008-05-28 14:09:22 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <glib.h>
|
|
|
|
|
2008-10-30 17:11:29 +00:00
|
|
|
#include "cogl-pango-glyph-cache.h"
|
|
|
|
#include "cogl-pango-private.h"
|
2008-05-28 14:09:22 +00:00
|
|
|
|
|
|
|
/* Minimum width/height for each texture */
|
|
|
|
#define MIN_TEXTURE_SIZE 256
|
|
|
|
/* All glyph with heights within this margin from each other can be
|
|
|
|
put in the same band */
|
|
|
|
#define BAND_HEIGHT_ROUND 4
|
|
|
|
|
2008-10-30 17:11:29 +00:00
|
|
|
typedef struct _CoglPangoGlyphCacheKey CoglPangoGlyphCacheKey;
|
|
|
|
typedef struct _CoglPangoGlyphCacheTexture CoglPangoGlyphCacheTexture;
|
|
|
|
typedef struct _CoglPangoGlyphCacheBand CoglPangoGlyphCacheBand;
|
2008-05-28 14:09:22 +00:00
|
|
|
|
2008-10-30 17:11:29 +00:00
|
|
|
struct _CoglPangoGlyphCache
|
2008-05-28 14:09:22 +00:00
|
|
|
{
|
|
|
|
/* Hash table to quickly check whether a particular glyph in a
|
|
|
|
particular font is already cached */
|
|
|
|
GHashTable *hash_table;
|
|
|
|
|
|
|
|
/* List of textures */
|
2008-10-30 17:11:29 +00:00
|
|
|
CoglPangoGlyphCacheTexture *textures;
|
2008-05-28 14:09:22 +00:00
|
|
|
|
|
|
|
/* List of horizontal bands of glyphs */
|
2008-10-30 17:11:29 +00:00
|
|
|
CoglPangoGlyphCacheBand *bands;
|
2008-05-28 14:09:22 +00:00
|
|
|
|
|
|
|
/* If TRUE all of the textures will be created with automatic mipmap
|
|
|
|
generation enabled */
|
|
|
|
gboolean use_mipmapping;
|
|
|
|
};
|
|
|
|
|
2008-10-30 17:11:29 +00:00
|
|
|
struct _CoglPangoGlyphCacheKey
|
2008-05-28 14:09:22 +00:00
|
|
|
{
|
|
|
|
PangoFont *font;
|
|
|
|
PangoGlyph glyph;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Represents one texture that will be used to store glyphs. The
|
|
|
|
texture is divided into horizontal bands which all contain glyphs
|
|
|
|
of approximatly the same height */
|
2008-10-30 17:11:29 +00:00
|
|
|
struct _CoglPangoGlyphCacheTexture
|
2008-05-28 14:09:22 +00:00
|
|
|
{
|
|
|
|
/* The width and height of the texture which should always be a
|
|
|
|
power of two. This can vary so that glyphs larger than
|
|
|
|
MIN_TEXTURE_SIZE can use a bigger texture */
|
|
|
|
int texture_size;
|
|
|
|
|
|
|
|
/* The remaining vertical space not taken up by any bands */
|
|
|
|
int space_remaining;
|
|
|
|
|
|
|
|
/* The actual texture */
|
|
|
|
CoglHandle texture;
|
|
|
|
|
2008-10-30 17:11:29 +00:00
|
|
|
CoglPangoGlyphCacheTexture *next;
|
2008-05-28 14:09:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Represents one horizontal band of a texture. Each band contains
|
|
|
|
glyphs of a similar height */
|
2008-10-30 17:11:29 +00:00
|
|
|
struct _CoglPangoGlyphCacheBand
|
2008-05-28 14:09:22 +00:00
|
|
|
{
|
|
|
|
/* The y position of the top of the band */
|
|
|
|
int top;
|
|
|
|
|
|
|
|
/* The height of the band */
|
|
|
|
int height;
|
|
|
|
|
|
|
|
/* The remaining horizontal space not taken up by any glyphs */
|
|
|
|
int space_remaining;
|
|
|
|
|
|
|
|
/* The size of the texture. Needed to calculate texture
|
|
|
|
coordinates */
|
|
|
|
int texture_size;
|
|
|
|
|
|
|
|
/* The texture containing this band */
|
|
|
|
CoglHandle texture;
|
|
|
|
|
2008-10-30 17:11:29 +00:00
|
|
|
CoglPangoGlyphCacheBand *next;
|
2008-05-28 14:09:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
2008-10-30 17:11:29 +00:00
|
|
|
cogl_pango_glyph_cache_value_free (CoglPangoGlyphCacheValue *value)
|
2008-05-28 14:09:22 +00:00
|
|
|
{
|
2009-04-01 16:16:44 +00:00
|
|
|
cogl_handle_unref (value->texture);
|
2008-10-30 17:11:29 +00:00
|
|
|
g_slice_free (CoglPangoGlyphCacheValue, value);
|
2008-05-28 14:09:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2008-10-30 17:11:29 +00:00
|
|
|
cogl_pango_glyph_cache_key_free (CoglPangoGlyphCacheKey *key)
|
2008-05-28 14:09:22 +00:00
|
|
|
{
|
|
|
|
g_object_unref (key->font);
|
2008-10-30 17:11:29 +00:00
|
|
|
g_slice_free (CoglPangoGlyphCacheKey, key);
|
2008-05-28 14:09:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static guint
|
2008-10-30 17:11:29 +00:00
|
|
|
cogl_pango_glyph_cache_hash_func (gconstpointer key)
|
2008-05-28 14:09:22 +00:00
|
|
|
{
|
2008-10-30 17:11:29 +00:00
|
|
|
const CoglPangoGlyphCacheKey *cache_key
|
|
|
|
= (const CoglPangoGlyphCacheKey *) key;
|
2008-05-28 14:09:22 +00:00
|
|
|
|
|
|
|
/* Generate a number affected by both the font and the glyph
|
|
|
|
number. We can safely directly compare the pointers because the
|
|
|
|
key holds a reference to the font so it is not possible that a
|
|
|
|
different font will have the same memory address */
|
2009-02-26 10:56:42 +00:00
|
|
|
return GPOINTER_TO_UINT (cache_key->font) ^ cache_key->glyph;
|
2008-05-28 14:09:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2008-10-30 17:11:29 +00:00
|
|
|
cogl_pango_glyph_cache_equal_func (gconstpointer a,
|
2008-05-28 14:09:22 +00:00
|
|
|
gconstpointer b)
|
|
|
|
{
|
2008-10-30 17:11:29 +00:00
|
|
|
const CoglPangoGlyphCacheKey *key_a
|
|
|
|
= (const CoglPangoGlyphCacheKey *) a;
|
|
|
|
const CoglPangoGlyphCacheKey *key_b
|
|
|
|
= (const CoglPangoGlyphCacheKey *) b;
|
2008-05-28 14:09:22 +00:00
|
|
|
|
|
|
|
/* We can safely directly compare the pointers for the fonts because
|
|
|
|
the key holds a reference to the font so it is not possible that
|
|
|
|
a different font will have the same memory address */
|
|
|
|
return key_a->font == key_b->font
|
|
|
|
&& key_a->glyph == key_b->glyph;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2008-10-30 17:11:29 +00:00
|
|
|
cogl_pango_glyph_cache_free_textures (CoglPangoGlyphCacheTexture *node)
|
2008-05-28 14:09:22 +00:00
|
|
|
{
|
2008-10-30 17:11:29 +00:00
|
|
|
CoglPangoGlyphCacheTexture *next;
|
2008-05-28 14:09:22 +00:00
|
|
|
|
|
|
|
while (node)
|
|
|
|
{
|
|
|
|
next = node->next;
|
2009-04-01 16:16:44 +00:00
|
|
|
cogl_handle_unref (node->texture);
|
2008-10-30 17:11:29 +00:00
|
|
|
g_slice_free (CoglPangoGlyphCacheTexture, node);
|
2008-05-28 14:09:22 +00:00
|
|
|
node = next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2008-10-30 17:11:29 +00:00
|
|
|
cogl_pango_glyph_cache_free_bands (CoglPangoGlyphCacheBand *node)
|
2008-05-28 14:09:22 +00:00
|
|
|
{
|
2008-10-30 17:11:29 +00:00
|
|
|
CoglPangoGlyphCacheBand *next;
|
2008-05-28 14:09:22 +00:00
|
|
|
|
|
|
|
while (node)
|
|
|
|
{
|
|
|
|
next = node->next;
|
2009-04-01 16:16:44 +00:00
|
|
|
cogl_handle_unref (node->texture);
|
2008-10-30 17:11:29 +00:00
|
|
|
g_slice_free (CoglPangoGlyphCacheBand, node);
|
2008-05-28 14:09:22 +00:00
|
|
|
node = next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-30 17:11:29 +00:00
|
|
|
CoglPangoGlyphCache *
|
|
|
|
cogl_pango_glyph_cache_new (gboolean use_mipmapping)
|
2008-05-28 14:09:22 +00:00
|
|
|
{
|
2008-10-30 17:11:29 +00:00
|
|
|
CoglPangoGlyphCache *cache;
|
2008-05-28 14:09:22 +00:00
|
|
|
|
2008-10-30 17:11:29 +00:00
|
|
|
cache = g_malloc (sizeof (CoglPangoGlyphCache));
|
2008-05-28 14:09:22 +00:00
|
|
|
|
|
|
|
cache->hash_table = g_hash_table_new_full
|
2008-10-30 17:11:29 +00:00
|
|
|
(cogl_pango_glyph_cache_hash_func,
|
|
|
|
cogl_pango_glyph_cache_equal_func,
|
|
|
|
(GDestroyNotify) cogl_pango_glyph_cache_key_free,
|
|
|
|
(GDestroyNotify) cogl_pango_glyph_cache_value_free);
|
2008-05-28 14:09:22 +00:00
|
|
|
|
|
|
|
cache->textures = NULL;
|
|
|
|
cache->bands = NULL;
|
|
|
|
cache->use_mipmapping = use_mipmapping;
|
|
|
|
|
|
|
|
return cache;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2008-10-30 17:11:29 +00:00
|
|
|
cogl_pango_glyph_cache_clear (CoglPangoGlyphCache *cache)
|
2008-05-28 14:09:22 +00:00
|
|
|
{
|
2008-10-30 17:11:29 +00:00
|
|
|
cogl_pango_glyph_cache_free_textures (cache->textures);
|
2008-05-28 14:09:22 +00:00
|
|
|
cache->textures = NULL;
|
2008-10-30 17:11:29 +00:00
|
|
|
cogl_pango_glyph_cache_free_bands (cache->bands);
|
2008-05-28 14:09:22 +00:00
|
|
|
cache->bands = NULL;
|
|
|
|
|
|
|
|
g_hash_table_remove_all (cache->hash_table);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2008-10-30 17:11:29 +00:00
|
|
|
cogl_pango_glyph_cache_free (CoglPangoGlyphCache *cache)
|
2008-05-28 14:09:22 +00:00
|
|
|
{
|
2008-10-30 17:11:29 +00:00
|
|
|
cogl_pango_glyph_cache_clear (cache);
|
2008-05-28 14:09:22 +00:00
|
|
|
|
|
|
|
g_hash_table_unref (cache->hash_table);
|
|
|
|
|
|
|
|
g_free (cache);
|
|
|
|
}
|
|
|
|
|
2008-10-30 17:11:29 +00:00
|
|
|
CoglPangoGlyphCacheValue *
|
|
|
|
cogl_pango_glyph_cache_lookup (CoglPangoGlyphCache *cache,
|
2008-05-28 14:09:22 +00:00
|
|
|
PangoFont *font,
|
|
|
|
PangoGlyph glyph)
|
|
|
|
{
|
2008-10-30 17:11:29 +00:00
|
|
|
CoglPangoGlyphCacheKey key;
|
2008-05-28 14:09:22 +00:00
|
|
|
|
|
|
|
key.font = font;
|
|
|
|
key.glyph = glyph;
|
|
|
|
|
2008-10-30 17:11:29 +00:00
|
|
|
return (CoglPangoGlyphCacheValue *)
|
2008-05-28 14:09:22 +00:00
|
|
|
g_hash_table_lookup (cache->hash_table, &key);
|
|
|
|
}
|
|
|
|
|
2008-10-30 17:11:29 +00:00
|
|
|
CoglPangoGlyphCacheValue *
|
|
|
|
cogl_pango_glyph_cache_set (CoglPangoGlyphCache *cache,
|
|
|
|
PangoFont *font,
|
|
|
|
PangoGlyph glyph,
|
|
|
|
gconstpointer pixels,
|
|
|
|
int width,
|
|
|
|
int height,
|
|
|
|
int stride,
|
|
|
|
int draw_x,
|
|
|
|
int draw_y)
|
2008-05-28 14:09:22 +00:00
|
|
|
{
|
2008-10-30 17:11:29 +00:00
|
|
|
int band_height;
|
|
|
|
CoglPangoGlyphCacheBand *band;
|
|
|
|
CoglPangoGlyphCacheKey *key;
|
|
|
|
CoglPangoGlyphCacheValue *value;
|
2008-05-28 14:09:22 +00:00
|
|
|
|
|
|
|
/* Reserve an extra pixel gap around the glyph so that it can pull
|
|
|
|
in blank pixels when linear filtering is enabled */
|
|
|
|
width++;
|
|
|
|
height++;
|
|
|
|
|
|
|
|
/* Round the height up to the nearest multiple of
|
|
|
|
BAND_HEIGHT_ROUND */
|
|
|
|
band_height = (height + BAND_HEIGHT_ROUND - 1) & ~(BAND_HEIGHT_ROUND - 1);
|
|
|
|
|
|
|
|
/* Look for a band with the same height and enough width available */
|
|
|
|
for (band = cache->bands;
|
|
|
|
band && (band->height != band_height || band->space_remaining < width);
|
|
|
|
band = band->next);
|
|
|
|
if (band == NULL)
|
|
|
|
{
|
2008-10-30 17:11:29 +00:00
|
|
|
CoglPangoGlyphCacheTexture *texture;
|
2008-05-28 14:09:22 +00:00
|
|
|
|
|
|
|
/* Look for a texture with enough vertical space left for a band
|
|
|
|
with this height */
|
|
|
|
for (texture = cache->textures;
|
2008-06-02 16:02:31 +00:00
|
|
|
texture && (texture->space_remaining < band_height
|
|
|
|
|| texture->texture_size < width);
|
2008-05-28 14:09:22 +00:00
|
|
|
texture = texture->next);
|
|
|
|
if (texture == NULL)
|
|
|
|
{
|
2009-01-18 14:51:19 +00:00
|
|
|
CoglTextureFlags flags = COGL_TEXTURE_NONE;
|
2008-05-28 14:09:22 +00:00
|
|
|
guchar *clear_data;
|
|
|
|
|
|
|
|
/* Allocate a new texture that is the nearest power of two
|
|
|
|
greater than the band height or the minimum size,
|
|
|
|
whichever is lower */
|
2008-10-30 17:11:29 +00:00
|
|
|
texture = g_slice_new (CoglPangoGlyphCacheTexture);
|
2008-05-28 14:09:22 +00:00
|
|
|
|
|
|
|
texture->texture_size = MIN_TEXTURE_SIZE;
|
2009-01-18 14:51:19 +00:00
|
|
|
while (texture->texture_size < band_height ||
|
|
|
|
texture->texture_size < width)
|
|
|
|
{
|
|
|
|
texture->texture_size *= 2;
|
|
|
|
}
|
2008-05-28 14:09:22 +00:00
|
|
|
|
|
|
|
/* Allocate an empty buffer to clear the texture */
|
2009-01-18 14:51:19 +00:00
|
|
|
clear_data =
|
|
|
|
g_malloc0 (texture->texture_size * texture->texture_size);
|
|
|
|
|
|
|
|
if (cache->use_mipmapping)
|
|
|
|
flags |= COGL_TEXTURE_AUTO_MIPMAP;
|
|
|
|
|
|
|
|
texture->texture =
|
|
|
|
cogl_texture_new_from_data (texture->texture_size,
|
|
|
|
texture->texture_size,
|
[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 18:18:18 +00:00
|
|
|
flags,
|
2009-01-18 14:51:19 +00:00
|
|
|
COGL_PIXEL_FORMAT_A_8,
|
|
|
|
COGL_PIXEL_FORMAT_A_8,
|
|
|
|
texture->texture_size,
|
|
|
|
clear_data);
|
2008-05-28 14:09:22 +00:00
|
|
|
|
|
|
|
g_free (clear_data);
|
|
|
|
|
|
|
|
texture->space_remaining = texture->texture_size;
|
|
|
|
texture->next = cache->textures;
|
|
|
|
cache->textures = texture;
|
|
|
|
|
|
|
|
if (cache->use_mipmapping)
|
|
|
|
cogl_texture_set_filters (texture->texture,
|
2009-05-12 13:15:18 +00:00
|
|
|
COGL_TEXTURE_FILTER_LINEAR_MIPMAP_LINEAR,
|
|
|
|
COGL_TEXTURE_FILTER_LINEAR);
|
2008-05-28 14:09:22 +00:00
|
|
|
else
|
|
|
|
cogl_texture_set_filters (texture->texture,
|
2009-05-12 13:15:18 +00:00
|
|
|
COGL_TEXTURE_FILTER_LINEAR,
|
|
|
|
COGL_TEXTURE_FILTER_LINEAR);
|
2008-05-28 14:09:22 +00:00
|
|
|
}
|
|
|
|
|
2008-10-30 17:11:29 +00:00
|
|
|
band = g_slice_new (CoglPangoGlyphCacheBand);
|
2008-05-28 14:09:22 +00:00
|
|
|
band->top = texture->texture_size - texture->space_remaining;
|
|
|
|
band->height = band_height;
|
|
|
|
band->space_remaining = texture->texture_size;
|
2009-04-01 16:16:44 +00:00
|
|
|
band->texture = cogl_handle_ref (texture->texture);
|
2008-05-28 14:09:22 +00:00
|
|
|
band->texture_size = texture->texture_size;
|
|
|
|
band->next = cache->bands;
|
|
|
|
cache->bands = band;
|
|
|
|
texture->space_remaining -= band_height;
|
|
|
|
}
|
|
|
|
|
|
|
|
band->space_remaining -= width;
|
|
|
|
|
|
|
|
width--;
|
|
|
|
height--;
|
|
|
|
|
|
|
|
cogl_texture_set_region (band->texture,
|
|
|
|
0, 0,
|
|
|
|
band->space_remaining,
|
|
|
|
band->top,
|
|
|
|
width, height,
|
|
|
|
width, height,
|
|
|
|
COGL_PIXEL_FORMAT_A_8,
|
|
|
|
stride,
|
|
|
|
pixels);
|
|
|
|
|
2008-10-30 17:11:29 +00:00
|
|
|
key = g_slice_new (CoglPangoGlyphCacheKey);
|
2008-05-28 14:09:22 +00:00
|
|
|
key->font = g_object_ref (font);
|
|
|
|
key->glyph = glyph;
|
|
|
|
|
2008-10-30 17:11:29 +00:00
|
|
|
value = g_slice_new (CoglPangoGlyphCacheValue);
|
2009-04-01 16:16:44 +00:00
|
|
|
value->texture = cogl_handle_ref (band->texture);
|
2009-01-20 16:20:54 +00:00
|
|
|
value->tx1 = (float)(band->space_remaining)
|
2008-10-30 16:37:55 +00:00
|
|
|
/ band->texture_size;
|
2009-01-20 16:20:54 +00:00
|
|
|
value->tx2 = (float)(band->space_remaining + width)
|
2008-10-30 16:37:55 +00:00
|
|
|
/ band->texture_size;
|
2009-01-20 16:20:54 +00:00
|
|
|
value->ty1 = (float)(band->top)
|
2008-10-30 16:37:55 +00:00
|
|
|
/ band->texture_size;
|
2009-01-20 16:20:54 +00:00
|
|
|
value->ty2 = (float)(band->top + height)
|
2008-10-30 16:37:55 +00:00
|
|
|
/ band->texture_size;
|
2008-05-28 14:09:22 +00:00
|
|
|
value->draw_x = draw_x;
|
|
|
|
value->draw_y = draw_y;
|
|
|
|
value->draw_width = width;
|
|
|
|
value->draw_height = height;
|
|
|
|
|
|
|
|
g_hash_table_insert (cache->hash_table, key, value);
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|