mutter/cogl-pango/cogl-pango-render.c

850 lines
27 KiB
C
Raw Normal View History

/*
* 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
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#ifndef PANGO_ENABLE_BACKEND
#define PANGO_ENABLE_BACKEND 1
#endif
#include <pango/pango-fontmap.h>
#include <pango/pangocairo.h>
#include <pango/pango-renderer.h>
#include <cairo.h>
#include "cogl/cogl-debug.h"
#include "cogl/cogl-context-private.h"
#include "cogl/cogl-texture-private.h"
#include "cogl-pango-private.h"
#include "cogl-pango-glyph-cache.h"
#include "cogl-pango-display-list.h"
typedef struct
{
CoglPangoGlyphCache *glyph_cache;
CoglPangoPipelineCache *pipeline_cache;
} CoglPangoRendererCaches;
struct _CoglPangoRenderer
{
PangoRenderer parent_instance;
/* Two caches of glyphs as textures and their corresponding pipeline
caches, one with mipmapped textures and one without */
CoglPangoRendererCaches no_mipmap_caches;
CoglPangoRendererCaches mipmap_caches;
gboolean use_mipmapping;
/* The current display list that is being built */
CoglPangoDisplayList *display_list;
};
struct _CoglPangoRendererClass
{
PangoRendererClass class_instance;
};
typedef struct _CoglPangoRendererQdata CoglPangoRendererQdata;
/* An instance of this struct gets attached to each PangoLayout to
cache the VBO and to detect changes to the layout */
struct _CoglPangoRendererQdata
{
cogl-pango: Use a CoglAtlas to maintain the glyph cache The glyph cache is now stored in a CoglAtlas structure instead of the custom atlasing code. This has the advantage that it can share code with the main texture atlas and that it supports reorganizing the atlas when it becomes full. Unlike the texture atlas, the glyph cache can use multiple atlases which would be neccessary if the maximum texture size is reached and we need to create a second texture. Whenever a display list is created it now has to register a callback with the glyph cache so that the display list can be recreated whenever any of the atlases are reorganized. This is needed because the display list directly stores texture coordinates within the atlas texture and they would become invalid when the texture is moved. The ensure_glyphs_for_layout now works in two steps. First it reserves space in the atlas for all of the glyphs. The atlas is created with the DISABLE_MIGRATION flag so that it won't actually copy any textures if any rearranging is needed. Whenever the position is updated for a glyph then it is marked as dirty. After space for all of the glyphs has been reserved it will iterate over all dirty glyphs and redraw them using Cairo. The rendered glyph is then stored in the texture with a sub texture update. The glyphs need to all be set at the right location before starting to create the display list because the display list stores the texture coordinates of the glyph. If any of the glyphs were moved around then the parts of the display list that was created already would become invalid. To make this work, ensure_glyphs_for_layout is now always called before rendering a layout or a layout line.
2010-08-04 17:05:21 +00:00
CoglPangoRenderer *renderer;
/* The cache of the geometry for the layout */
CoglPangoDisplayList *display_list;
/* A reference to the first line of the layout. This is just used to
detect changes */
PangoLayoutLine *first_line;
/* Whether mipmapping was previously used to render this layout. We
need to regenerate the display list if the mipmapping value is
changed because it will be using a different set of textures */
gboolean mipmapping_used;
};
cogl-pango: Use a CoglAtlas to maintain the glyph cache The glyph cache is now stored in a CoglAtlas structure instead of the custom atlasing code. This has the advantage that it can share code with the main texture atlas and that it supports reorganizing the atlas when it becomes full. Unlike the texture atlas, the glyph cache can use multiple atlases which would be neccessary if the maximum texture size is reached and we need to create a second texture. Whenever a display list is created it now has to register a callback with the glyph cache so that the display list can be recreated whenever any of the atlases are reorganized. This is needed because the display list directly stores texture coordinates within the atlas texture and they would become invalid when the texture is moved. The ensure_glyphs_for_layout now works in two steps. First it reserves space in the atlas for all of the glyphs. The atlas is created with the DISABLE_MIGRATION flag so that it won't actually copy any textures if any rearranging is needed. Whenever the position is updated for a glyph then it is marked as dirty. After space for all of the glyphs has been reserved it will iterate over all dirty glyphs and redraw them using Cairo. The rendered glyph is then stored in the texture with a sub texture update. The glyphs need to all be set at the right location before starting to create the display list because the display list stores the texture coordinates of the glyph. If any of the glyphs were moved around then the parts of the display list that was created already would become invalid. To make this work, ensure_glyphs_for_layout is now always called before rendering a layout or a layout line.
2010-08-04 17:05:21 +00:00
static void
_cogl_pango_ensure_glyph_cache_for_layout_line (PangoLayoutLine *line);
typedef struct
{
CoglPangoDisplayList *display_list;
float x1, y1, x2, y2;
} CoglPangoRendererSliceCbData;
Add -Wmissing-declarations to maintainer flags and fix problems This option to GCC makes it give a warning whenever a global function is defined without a declaration. This should catch cases were we've defined a function but forgot to put it in a header. In that case it is either only used within one file so we should make it static or we should declare it in a header. The following changes where made to fix problems: • Some functions were made static • cogl-path.h (the one containing the 1.0 API) was split into two files, one defining the functions and one defining the enums so that cogl-path.c can include the enum and function declarations from the 2.0 API as well as the function declarations from the 1.0 API. • cogl2-clip-state has been removed. This only had one experimental function called cogl_clip_push_from_path but as this is unstable we might as well remove it favour of the equivalent cogl_framebuffer_* API. • The GLX, SDL and WGL winsys's now have a private header to define their get_vtable function instead of directly declaring in the C file where it is called. • All places that were calling COGL_OBJECT_DEFINE need to have the cogl_is_whatever function declared so these have been added either as a public function or in a private header. • Some files that were not including the header containing their function declarations have been fixed to do so. • Any unused error quark functions have been removed. If we later want them we should add them back one by one and add a declaration for them in a header. • _cogl_is_framebuffer has been renamed to cogl_is_framebuffer and made a public function with a declaration in cogl-framebuffer.h • Similarly for CoglOnscreen. • cogl_vdraw_indexed_attributes is called cogl_framebuffer_vdraw_indexed_attributes in the header. The definition has been changed to match the header. • cogl_index_buffer_allocate has been removed. This had no declaration and I'm not sure what it's supposed to do. • CoglJournal has been changed to use the internal CoglObject macro so that it won't define an exported cogl_is_journal symbol. • The _cogl_blah_pointer_from_handle functions have been removed. CoglHandle isn't used much anymore anyway and in the few places where it is used I think it's safe to just use the implicit cast from void* to the right type. • The test-utils.h header for the conformance tests explicitly disables the -Wmissing-declaration option using a pragma because all of the tests declare their main function without a header. Any mistakes relating to missing declarations aren't really important for the tests. • cogl_quaternion_init_from_quaternion and init_from_matrix have been given declarations in cogl-quaternion.h Reviewed-by: Robert Bragg <robert@linux.intel.com>
2012-03-06 18:21:28 +00:00
static void
Add a strong CoglTexture type to replace CoglHandle As part of the on going, incremental effort to purge the non type safe CoglHandle type from the Cogl API this patch tackles most of the CoglHandle uses relating to textures. We'd postponed making this change for quite a while because we wanted to have a clearer understanding of how we wanted to evolve the texture APIs towards Cogl 2.0 before exposing type safety here which would be difficult to change later since it would imply breaking APIs. The basic idea that we are steering towards now is that CoglTexture can be considered to be the most primitive interface we have for any object representing a texture. The texture interface would provide roughly these methods: cogl_texture_get_width cogl_texture_get_height cogl_texture_can_repeat cogl_texture_can_mipmap cogl_texture_generate_mipmap; cogl_texture_get_format cogl_texture_set_region cogl_texture_get_region Besides the texture interface we will then start to expose types corresponding to specific texture types: CoglTexture2D, CoglTexture3D, CoglTexture2DSliced, CoglSubTexture, CoglAtlasTexture and CoglTexturePixmapX11. We will then also expose an interface for the high-level texture types we have (such as CoglTexture2DSlice, CoglSubTexture and CoglAtlasTexture) called CoglMetaTexture. CoglMetaTexture is an additional interface that lets you iterate a virtual region of a meta texture and get mappings of primitive textures to sub-regions of that virtual region. Internally we already have this kind of abstraction for dealing with sliced texture, sub-textures and atlas textures in a consistent way, so this will just make that abstraction public. The aim here is to clarify that there is a difference between primitive textures (CoglTexture2D/3D) and some of the other high-level textures, and also enable developers to implement primitives that can support meta textures since they can only be used with the cogl_rectangle API currently. The thing that's not so clean-cut with this are the texture constructors we have currently; such as cogl_texture_new_from_file which no longer make sense when CoglTexture is considered to be an interface. These will basically just become convenient factory functions and it's just a bit unusual that they are within the cogl_texture namespace. It's worth noting here that all the texture type APIs will also have their own type specific constructors so these functions will only be used for the convenience of being able to create a texture without really wanting to know the details of what type of texture you need. Longer term for 2.0 we may come up with replacement names for these factory functions or the other thing we are considering is designing some asynchronous factory functions instead since it's so often detrimental to application performance to be blocked waiting for a texture to be uploaded to the GPU. Reviewed-by: Neil Roberts <neil@linux.intel.com>
2011-08-24 20:30:34 +00:00
cogl_pango_renderer_slice_cb (CoglTexture *texture,
const float *slice_coords,
const float *virtual_coords,
void *user_data)
{
CoglPangoRendererSliceCbData *data = user_data;
/* Note: this assumes that there is only one slice containing the
whole texture and it doesn't attempt to split up the vertex
coordinates based on the virtual_coords */
_cogl_pango_display_list_add_texture (data->display_list,
Add a strong CoglTexture type to replace CoglHandle As part of the on going, incremental effort to purge the non type safe CoglHandle type from the Cogl API this patch tackles most of the CoglHandle uses relating to textures. We'd postponed making this change for quite a while because we wanted to have a clearer understanding of how we wanted to evolve the texture APIs towards Cogl 2.0 before exposing type safety here which would be difficult to change later since it would imply breaking APIs. The basic idea that we are steering towards now is that CoglTexture can be considered to be the most primitive interface we have for any object representing a texture. The texture interface would provide roughly these methods: cogl_texture_get_width cogl_texture_get_height cogl_texture_can_repeat cogl_texture_can_mipmap cogl_texture_generate_mipmap; cogl_texture_get_format cogl_texture_set_region cogl_texture_get_region Besides the texture interface we will then start to expose types corresponding to specific texture types: CoglTexture2D, CoglTexture3D, CoglTexture2DSliced, CoglSubTexture, CoglAtlasTexture and CoglTexturePixmapX11. We will then also expose an interface for the high-level texture types we have (such as CoglTexture2DSlice, CoglSubTexture and CoglAtlasTexture) called CoglMetaTexture. CoglMetaTexture is an additional interface that lets you iterate a virtual region of a meta texture and get mappings of primitive textures to sub-regions of that virtual region. Internally we already have this kind of abstraction for dealing with sliced texture, sub-textures and atlas textures in a consistent way, so this will just make that abstraction public. The aim here is to clarify that there is a difference between primitive textures (CoglTexture2D/3D) and some of the other high-level textures, and also enable developers to implement primitives that can support meta textures since they can only be used with the cogl_rectangle API currently. The thing that's not so clean-cut with this are the texture constructors we have currently; such as cogl_texture_new_from_file which no longer make sense when CoglTexture is considered to be an interface. These will basically just become convenient factory functions and it's just a bit unusual that they are within the cogl_texture namespace. It's worth noting here that all the texture type APIs will also have their own type specific constructors so these functions will only be used for the convenience of being able to create a texture without really wanting to know the details of what type of texture you need. Longer term for 2.0 we may come up with replacement names for these factory functions or the other thing we are considering is designing some asynchronous factory functions instead since it's so often detrimental to application performance to be blocked waiting for a texture to be uploaded to the GPU. Reviewed-by: Neil Roberts <neil@linux.intel.com>
2011-08-24 20:30:34 +00:00
texture,
data->x1,
data->y1,
data->x2,
data->y2,
slice_coords[0],
slice_coords[1],
slice_coords[2],
slice_coords[3]);
}
static void
cogl_pango_renderer_draw_glyph (CoglPangoRenderer *priv,
CoglPangoGlyphCacheValue *cache_value,
float x1,
float y1)
{
CoglPangoRendererSliceCbData data;
g_return_if_fail (priv->display_list != NULL);
data.display_list = priv->display_list;
data.x1 = x1;
data.y1 = y1;
data.x2 = x1 + (float) cache_value->draw_width;
data.y2 = y1 + (float) cache_value->draw_height;
/* We iterate the internal sub textures of the texture so that we
can get a pointer to the base texture even if the texture is in
the global atlas. That way the display list can recognise that
the neighbouring glyphs are coming from the same atlas and bundle
them together into a single VBO */
cogl_meta_texture_foreach_in_region (COGL_META_TEXTURE (cache_value->texture),
cache_value->tx1,
cache_value->ty1,
cache_value->tx2,
cache_value->ty2,
COGL_PIPELINE_WRAP_MODE_REPEAT,
COGL_PIPELINE_WRAP_MODE_REPEAT,
cogl_pango_renderer_slice_cb,
&data);
}
static void cogl_pango_renderer_finalize (GObject *object);
static void cogl_pango_renderer_draw_glyphs (PangoRenderer *renderer,
PangoFont *font,
PangoGlyphString *glyphs,
int x,
int y);
static void cogl_pango_renderer_draw_rectangle (PangoRenderer *renderer,
PangoRenderPart part,
int x,
int y,
int width,
int height);
static void cogl_pango_renderer_draw_trapezoid (PangoRenderer *renderer,
PangoRenderPart part,
double y1,
double x11,
double x21,
double y2,
double x12,
double x22);
G_DEFINE_TYPE (CoglPangoRenderer, cogl_pango_renderer, PANGO_TYPE_RENDERER);
static void
cogl_pango_renderer_init (CoglPangoRenderer *priv)
{
priv->no_mipmap_caches.pipeline_cache =
_cogl_pango_pipeline_cache_new (FALSE);
priv->mipmap_caches.pipeline_cache =
_cogl_pango_pipeline_cache_new (TRUE);
priv->no_mipmap_caches.glyph_cache = cogl_pango_glyph_cache_new (FALSE);
priv->mipmap_caches.glyph_cache = cogl_pango_glyph_cache_new (TRUE);
_cogl_pango_renderer_set_use_mipmapping (priv, FALSE);
}
static void
cogl_pango_renderer_class_init (CoglPangoRendererClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
PangoRendererClass *renderer_class = PANGO_RENDERER_CLASS (klass);
object_class->finalize = cogl_pango_renderer_finalize;
renderer_class->draw_glyphs = cogl_pango_renderer_draw_glyphs;
renderer_class->draw_rectangle = cogl_pango_renderer_draw_rectangle;
renderer_class->draw_trapezoid = cogl_pango_renderer_draw_trapezoid;
}
static void
cogl_pango_renderer_finalize (GObject *object)
{
CoglPangoRenderer *priv = COGL_PANGO_RENDERER (object);
cogl_pango_glyph_cache_free (priv->no_mipmap_caches.glyph_cache);
cogl_pango_glyph_cache_free (priv->mipmap_caches.glyph_cache);
_cogl_pango_pipeline_cache_free (priv->no_mipmap_caches.pipeline_cache);
_cogl_pango_pipeline_cache_free (priv->mipmap_caches.pipeline_cache);
G_OBJECT_CLASS (cogl_pango_renderer_parent_class)->finalize (object);
}
static CoglPangoRenderer *
cogl_pango_get_renderer_from_context (PangoContext *context)
{
PangoFontMap *font_map;
PangoRenderer *renderer;
CoglPangoFontMap *font_map_priv;
font_map = pango_context_get_font_map (context);
g_return_val_if_fail (COGL_PANGO_IS_FONT_MAP (font_map), NULL);
font_map_priv = COGL_PANGO_FONT_MAP (font_map);
renderer = cogl_pango_font_map_get_renderer (font_map_priv);
g_return_val_if_fail (COGL_PANGO_IS_RENDERER (renderer), NULL);
return COGL_PANGO_RENDERER (renderer);
}
static GQuark
cogl_pango_render_get_qdata_key (void)
{
static GQuark key = 0;
if (G_UNLIKELY (key == 0))
key = g_quark_from_static_string ("CoglPangoDisplayList");
return key;
}
static void
cogl-pango: Use a CoglAtlas to maintain the glyph cache The glyph cache is now stored in a CoglAtlas structure instead of the custom atlasing code. This has the advantage that it can share code with the main texture atlas and that it supports reorganizing the atlas when it becomes full. Unlike the texture atlas, the glyph cache can use multiple atlases which would be neccessary if the maximum texture size is reached and we need to create a second texture. Whenever a display list is created it now has to register a callback with the glyph cache so that the display list can be recreated whenever any of the atlases are reorganized. This is needed because the display list directly stores texture coordinates within the atlas texture and they would become invalid when the texture is moved. The ensure_glyphs_for_layout now works in two steps. First it reserves space in the atlas for all of the glyphs. The atlas is created with the DISABLE_MIGRATION flag so that it won't actually copy any textures if any rearranging is needed. Whenever the position is updated for a glyph then it is marked as dirty. After space for all of the glyphs has been reserved it will iterate over all dirty glyphs and redraw them using Cairo. The rendered glyph is then stored in the texture with a sub texture update. The glyphs need to all be set at the right location before starting to create the display list because the display list stores the texture coordinates of the glyph. If any of the glyphs were moved around then the parts of the display list that was created already would become invalid. To make this work, ensure_glyphs_for_layout is now always called before rendering a layout or a layout line.
2010-08-04 17:05:21 +00:00
cogl_pango_render_qdata_forget_display_list (CoglPangoRendererQdata *qdata)
{
if (qdata->display_list)
cogl-pango: Use a CoglAtlas to maintain the glyph cache The glyph cache is now stored in a CoglAtlas structure instead of the custom atlasing code. This has the advantage that it can share code with the main texture atlas and that it supports reorganizing the atlas when it becomes full. Unlike the texture atlas, the glyph cache can use multiple atlases which would be neccessary if the maximum texture size is reached and we need to create a second texture. Whenever a display list is created it now has to register a callback with the glyph cache so that the display list can be recreated whenever any of the atlases are reorganized. This is needed because the display list directly stores texture coordinates within the atlas texture and they would become invalid when the texture is moved. The ensure_glyphs_for_layout now works in two steps. First it reserves space in the atlas for all of the glyphs. The atlas is created with the DISABLE_MIGRATION flag so that it won't actually copy any textures if any rearranging is needed. Whenever the position is updated for a glyph then it is marked as dirty. After space for all of the glyphs has been reserved it will iterate over all dirty glyphs and redraw them using Cairo. The rendered glyph is then stored in the texture with a sub texture update. The glyphs need to all be set at the right location before starting to create the display list because the display list stores the texture coordinates of the glyph. If any of the glyphs were moved around then the parts of the display list that was created already would become invalid. To make this work, ensure_glyphs_for_layout is now always called before rendering a layout or a layout line.
2010-08-04 17:05:21 +00:00
{
CoglPangoRendererCaches *caches = qdata->mipmapping_used ?
&qdata->renderer->mipmap_caches :
&qdata->renderer->no_mipmap_caches;
cogl-pango: Use a CoglAtlas to maintain the glyph cache The glyph cache is now stored in a CoglAtlas structure instead of the custom atlasing code. This has the advantage that it can share code with the main texture atlas and that it supports reorganizing the atlas when it becomes full. Unlike the texture atlas, the glyph cache can use multiple atlases which would be neccessary if the maximum texture size is reached and we need to create a second texture. Whenever a display list is created it now has to register a callback with the glyph cache so that the display list can be recreated whenever any of the atlases are reorganized. This is needed because the display list directly stores texture coordinates within the atlas texture and they would become invalid when the texture is moved. The ensure_glyphs_for_layout now works in two steps. First it reserves space in the atlas for all of the glyphs. The atlas is created with the DISABLE_MIGRATION flag so that it won't actually copy any textures if any rearranging is needed. Whenever the position is updated for a glyph then it is marked as dirty. After space for all of the glyphs has been reserved it will iterate over all dirty glyphs and redraw them using Cairo. The rendered glyph is then stored in the texture with a sub texture update. The glyphs need to all be set at the right location before starting to create the display list because the display list stores the texture coordinates of the glyph. If any of the glyphs were moved around then the parts of the display list that was created already would become invalid. To make this work, ensure_glyphs_for_layout is now always called before rendering a layout or a layout line.
2010-08-04 17:05:21 +00:00
_cogl_pango_glyph_cache_remove_reorganize_callback
(caches->glyph_cache,
(GHookFunc) cogl_pango_render_qdata_forget_display_list,
cogl-pango: Use a CoglAtlas to maintain the glyph cache The glyph cache is now stored in a CoglAtlas structure instead of the custom atlasing code. This has the advantage that it can share code with the main texture atlas and that it supports reorganizing the atlas when it becomes full. Unlike the texture atlas, the glyph cache can use multiple atlases which would be neccessary if the maximum texture size is reached and we need to create a second texture. Whenever a display list is created it now has to register a callback with the glyph cache so that the display list can be recreated whenever any of the atlases are reorganized. This is needed because the display list directly stores texture coordinates within the atlas texture and they would become invalid when the texture is moved. The ensure_glyphs_for_layout now works in two steps. First it reserves space in the atlas for all of the glyphs. The atlas is created with the DISABLE_MIGRATION flag so that it won't actually copy any textures if any rearranging is needed. Whenever the position is updated for a glyph then it is marked as dirty. After space for all of the glyphs has been reserved it will iterate over all dirty glyphs and redraw them using Cairo. The rendered glyph is then stored in the texture with a sub texture update. The glyphs need to all be set at the right location before starting to create the display list because the display list stores the texture coordinates of the glyph. If any of the glyphs were moved around then the parts of the display list that was created already would become invalid. To make this work, ensure_glyphs_for_layout is now always called before rendering a layout or a layout line.
2010-08-04 17:05:21 +00:00
qdata);
_cogl_pango_display_list_free (qdata->display_list);
qdata->display_list = NULL;
}
}
static void
cogl_pango_render_qdata_destroy (CoglPangoRendererQdata *qdata)
{
cogl_pango_render_qdata_forget_display_list (qdata);
if (qdata->first_line)
pango_layout_line_unref (qdata->first_line);
g_slice_free (CoglPangoRendererQdata, qdata);
}
/**
* cogl_pango_render_layout_subpixel:
* @layout: a #PangoLayout
* @x: FIXME
* @y: FIXME
* @color: color to use when rendering the layout
* @flags: flags to pass to the renderer
*
* FIXME
*
* Since: 1.0
*/
void
cogl_pango_render_layout_subpixel (PangoLayout *layout,
int x,
int y,
const CoglColor *color,
int flags)
{
PangoContext *context;
CoglPangoRenderer *priv;
CoglPangoRendererQdata *qdata;
context = pango_layout_get_context (layout);
priv = cogl_pango_get_renderer_from_context (context);
if (G_UNLIKELY (!priv))
return;
qdata = g_object_get_qdata (G_OBJECT (layout),
cogl_pango_render_get_qdata_key ());
if (qdata == NULL)
{
qdata = g_slice_new0 (CoglPangoRendererQdata);
cogl-pango: Use a CoglAtlas to maintain the glyph cache The glyph cache is now stored in a CoglAtlas structure instead of the custom atlasing code. This has the advantage that it can share code with the main texture atlas and that it supports reorganizing the atlas when it becomes full. Unlike the texture atlas, the glyph cache can use multiple atlases which would be neccessary if the maximum texture size is reached and we need to create a second texture. Whenever a display list is created it now has to register a callback with the glyph cache so that the display list can be recreated whenever any of the atlases are reorganized. This is needed because the display list directly stores texture coordinates within the atlas texture and they would become invalid when the texture is moved. The ensure_glyphs_for_layout now works in two steps. First it reserves space in the atlas for all of the glyphs. The atlas is created with the DISABLE_MIGRATION flag so that it won't actually copy any textures if any rearranging is needed. Whenever the position is updated for a glyph then it is marked as dirty. After space for all of the glyphs has been reserved it will iterate over all dirty glyphs and redraw them using Cairo. The rendered glyph is then stored in the texture with a sub texture update. The glyphs need to all be set at the right location before starting to create the display list because the display list stores the texture coordinates of the glyph. If any of the glyphs were moved around then the parts of the display list that was created already would become invalid. To make this work, ensure_glyphs_for_layout is now always called before rendering a layout or a layout line.
2010-08-04 17:05:21 +00:00
qdata->renderer = priv;
g_object_set_qdata_full (G_OBJECT (layout),
cogl_pango_render_get_qdata_key (),
qdata,
(GDestroyNotify)
cogl_pango_render_qdata_destroy);
}
/* Check if the layout has changed since the last build of the
display list. This trick was suggested by Behdad Esfahbod here:
http://mail.gnome.org/archives/gtk-i18n-list/2009-May/msg00019.html */
if (qdata->display_list &&
((qdata->first_line &&
qdata->first_line->layout != layout) ||
qdata->mipmapping_used != priv->use_mipmapping))
cogl-pango: Use a CoglAtlas to maintain the glyph cache The glyph cache is now stored in a CoglAtlas structure instead of the custom atlasing code. This has the advantage that it can share code with the main texture atlas and that it supports reorganizing the atlas when it becomes full. Unlike the texture atlas, the glyph cache can use multiple atlases which would be neccessary if the maximum texture size is reached and we need to create a second texture. Whenever a display list is created it now has to register a callback with the glyph cache so that the display list can be recreated whenever any of the atlases are reorganized. This is needed because the display list directly stores texture coordinates within the atlas texture and they would become invalid when the texture is moved. The ensure_glyphs_for_layout now works in two steps. First it reserves space in the atlas for all of the glyphs. The atlas is created with the DISABLE_MIGRATION flag so that it won't actually copy any textures if any rearranging is needed. Whenever the position is updated for a glyph then it is marked as dirty. After space for all of the glyphs has been reserved it will iterate over all dirty glyphs and redraw them using Cairo. The rendered glyph is then stored in the texture with a sub texture update. The glyphs need to all be set at the right location before starting to create the display list because the display list stores the texture coordinates of the glyph. If any of the glyphs were moved around then the parts of the display list that was created already would become invalid. To make this work, ensure_glyphs_for_layout is now always called before rendering a layout or a layout line.
2010-08-04 17:05:21 +00:00
cogl_pango_render_qdata_forget_display_list (qdata);
if (qdata->display_list == NULL)
{
CoglPangoRendererCaches *caches = priv->use_mipmapping ?
&priv->mipmap_caches :
&priv->no_mipmap_caches;
cogl-pango: Use a CoglAtlas to maintain the glyph cache The glyph cache is now stored in a CoglAtlas structure instead of the custom atlasing code. This has the advantage that it can share code with the main texture atlas and that it supports reorganizing the atlas when it becomes full. Unlike the texture atlas, the glyph cache can use multiple atlases which would be neccessary if the maximum texture size is reached and we need to create a second texture. Whenever a display list is created it now has to register a callback with the glyph cache so that the display list can be recreated whenever any of the atlases are reorganized. This is needed because the display list directly stores texture coordinates within the atlas texture and they would become invalid when the texture is moved. The ensure_glyphs_for_layout now works in two steps. First it reserves space in the atlas for all of the glyphs. The atlas is created with the DISABLE_MIGRATION flag so that it won't actually copy any textures if any rearranging is needed. Whenever the position is updated for a glyph then it is marked as dirty. After space for all of the glyphs has been reserved it will iterate over all dirty glyphs and redraw them using Cairo. The rendered glyph is then stored in the texture with a sub texture update. The glyphs need to all be set at the right location before starting to create the display list because the display list stores the texture coordinates of the glyph. If any of the glyphs were moved around then the parts of the display list that was created already would become invalid. To make this work, ensure_glyphs_for_layout is now always called before rendering a layout or a layout line.
2010-08-04 17:05:21 +00:00
cogl_pango_ensure_glyph_cache_for_layout (layout);
qdata->display_list =
_cogl_pango_display_list_new (caches->pipeline_cache);
cogl-pango: Use a CoglAtlas to maintain the glyph cache The glyph cache is now stored in a CoglAtlas structure instead of the custom atlasing code. This has the advantage that it can share code with the main texture atlas and that it supports reorganizing the atlas when it becomes full. Unlike the texture atlas, the glyph cache can use multiple atlases which would be neccessary if the maximum texture size is reached and we need to create a second texture. Whenever a display list is created it now has to register a callback with the glyph cache so that the display list can be recreated whenever any of the atlases are reorganized. This is needed because the display list directly stores texture coordinates within the atlas texture and they would become invalid when the texture is moved. The ensure_glyphs_for_layout now works in two steps. First it reserves space in the atlas for all of the glyphs. The atlas is created with the DISABLE_MIGRATION flag so that it won't actually copy any textures if any rearranging is needed. Whenever the position is updated for a glyph then it is marked as dirty. After space for all of the glyphs has been reserved it will iterate over all dirty glyphs and redraw them using Cairo. The rendered glyph is then stored in the texture with a sub texture update. The glyphs need to all be set at the right location before starting to create the display list because the display list stores the texture coordinates of the glyph. If any of the glyphs were moved around then the parts of the display list that was created already would become invalid. To make this work, ensure_glyphs_for_layout is now always called before rendering a layout or a layout line.
2010-08-04 17:05:21 +00:00
/* Register for notification of when the glyph cache changes so
we can rebuild the display list */
_cogl_pango_glyph_cache_add_reorganize_callback
(caches->glyph_cache,
(GHookFunc) cogl_pango_render_qdata_forget_display_list,
cogl-pango: Use a CoglAtlas to maintain the glyph cache The glyph cache is now stored in a CoglAtlas structure instead of the custom atlasing code. This has the advantage that it can share code with the main texture atlas and that it supports reorganizing the atlas when it becomes full. Unlike the texture atlas, the glyph cache can use multiple atlases which would be neccessary if the maximum texture size is reached and we need to create a second texture. Whenever a display list is created it now has to register a callback with the glyph cache so that the display list can be recreated whenever any of the atlases are reorganized. This is needed because the display list directly stores texture coordinates within the atlas texture and they would become invalid when the texture is moved. The ensure_glyphs_for_layout now works in two steps. First it reserves space in the atlas for all of the glyphs. The atlas is created with the DISABLE_MIGRATION flag so that it won't actually copy any textures if any rearranging is needed. Whenever the position is updated for a glyph then it is marked as dirty. After space for all of the glyphs has been reserved it will iterate over all dirty glyphs and redraw them using Cairo. The rendered glyph is then stored in the texture with a sub texture update. The glyphs need to all be set at the right location before starting to create the display list because the display list stores the texture coordinates of the glyph. If any of the glyphs were moved around then the parts of the display list that was created already would become invalid. To make this work, ensure_glyphs_for_layout is now always called before rendering a layout or a layout line.
2010-08-04 17:05:21 +00:00
qdata);
priv->display_list = qdata->display_list;
pango_renderer_draw_layout (PANGO_RENDERER (priv), layout, 0, 0);
priv->display_list = NULL;
qdata->mipmapping_used = priv->use_mipmapping;
}
cogl_push_matrix ();
cogl_translate (x / (gfloat) PANGO_SCALE, y / (gfloat) PANGO_SCALE, 0);
_cogl_pango_display_list_render (qdata->display_list,
color);
cogl_pop_matrix ();
/* Keep a reference to the first line of the layout so we can detect
changes */
if (qdata->first_line)
{
pango_layout_line_unref (qdata->first_line);
qdata->first_line = NULL;
}
if (pango_layout_get_line_count (layout) > 0)
{
qdata->first_line = pango_layout_get_line (layout, 0);
pango_layout_line_ref (qdata->first_line);
}
}
/**
* cogl_pango_render_layout:
* @layout: a #PangoLayout
* @x: X coordinate to render the layout at
* @y: Y coordinate to render the layout at
* @color: color to use when rendering the layout
* @flags: flags to pass to the renderer
*
* Renders @layout.
*
* Since: 1.0
*/
void
cogl_pango_render_layout (PangoLayout *layout,
int x,
int y,
const CoglColor *color,
int flags)
{
cogl_pango_render_layout_subpixel (layout,
x * PANGO_SCALE,
y * PANGO_SCALE,
color,
flags);
}
/**
* cogl_pango_render_layout_line:
* @line: a #PangoLayoutLine
* @x: X coordinate to render the line at
* @y: Y coordinate to render the line at
* @color: color to use when rendering the line
*
* Renders @line at the given coordinates using the given color.
*
* Since: 1.0
*/
void
cogl_pango_render_layout_line (PangoLayoutLine *line,
int x,
int y,
const CoglColor *color)
{
PangoContext *context;
CoglPangoRenderer *priv;
CoglPangoRendererCaches *caches;
context = pango_layout_get_context (line->layout);
priv = cogl_pango_get_renderer_from_context (context);
if (G_UNLIKELY (!priv))
return;
caches = (priv->use_mipmapping ?
&priv->mipmap_caches :
&priv->no_mipmap_caches);
priv->display_list = _cogl_pango_display_list_new (caches->pipeline_cache);
cogl-pango: Use a CoglAtlas to maintain the glyph cache The glyph cache is now stored in a CoglAtlas structure instead of the custom atlasing code. This has the advantage that it can share code with the main texture atlas and that it supports reorganizing the atlas when it becomes full. Unlike the texture atlas, the glyph cache can use multiple atlases which would be neccessary if the maximum texture size is reached and we need to create a second texture. Whenever a display list is created it now has to register a callback with the glyph cache so that the display list can be recreated whenever any of the atlases are reorganized. This is needed because the display list directly stores texture coordinates within the atlas texture and they would become invalid when the texture is moved. The ensure_glyphs_for_layout now works in two steps. First it reserves space in the atlas for all of the glyphs. The atlas is created with the DISABLE_MIGRATION flag so that it won't actually copy any textures if any rearranging is needed. Whenever the position is updated for a glyph then it is marked as dirty. After space for all of the glyphs has been reserved it will iterate over all dirty glyphs and redraw them using Cairo. The rendered glyph is then stored in the texture with a sub texture update. The glyphs need to all be set at the right location before starting to create the display list because the display list stores the texture coordinates of the glyph. If any of the glyphs were moved around then the parts of the display list that was created already would become invalid. To make this work, ensure_glyphs_for_layout is now always called before rendering a layout or a layout line.
2010-08-04 17:05:21 +00:00
_cogl_pango_ensure_glyph_cache_for_layout_line (line);
pango_renderer_draw_layout_line (PANGO_RENDERER (priv), line, x, y);
_cogl_pango_display_list_render (priv->display_list,
color);
_cogl_pango_display_list_free (priv->display_list);
priv->display_list = NULL;
}
void
_cogl_pango_renderer_clear_glyph_cache (CoglPangoRenderer *renderer)
{
cogl_pango_glyph_cache_clear (renderer->mipmap_caches.glyph_cache);
cogl_pango_glyph_cache_clear (renderer->no_mipmap_caches.glyph_cache);
}
void
_cogl_pango_renderer_set_use_mipmapping (CoglPangoRenderer *renderer,
gboolean value)
{
renderer->use_mipmapping = value;
}
gboolean
_cogl_pango_renderer_get_use_mipmapping (CoglPangoRenderer *renderer)
{
return renderer->use_mipmapping;
}
static CoglPangoGlyphCacheValue *
cogl_pango_renderer_get_cached_glyph (PangoRenderer *renderer,
cogl-pango: Use a CoglAtlas to maintain the glyph cache The glyph cache is now stored in a CoglAtlas structure instead of the custom atlasing code. This has the advantage that it can share code with the main texture atlas and that it supports reorganizing the atlas when it becomes full. Unlike the texture atlas, the glyph cache can use multiple atlases which would be neccessary if the maximum texture size is reached and we need to create a second texture. Whenever a display list is created it now has to register a callback with the glyph cache so that the display list can be recreated whenever any of the atlases are reorganized. This is needed because the display list directly stores texture coordinates within the atlas texture and they would become invalid when the texture is moved. The ensure_glyphs_for_layout now works in two steps. First it reserves space in the atlas for all of the glyphs. The atlas is created with the DISABLE_MIGRATION flag so that it won't actually copy any textures if any rearranging is needed. Whenever the position is updated for a glyph then it is marked as dirty. After space for all of the glyphs has been reserved it will iterate over all dirty glyphs and redraw them using Cairo. The rendered glyph is then stored in the texture with a sub texture update. The glyphs need to all be set at the right location before starting to create the display list because the display list stores the texture coordinates of the glyph. If any of the glyphs were moved around then the parts of the display list that was created already would become invalid. To make this work, ensure_glyphs_for_layout is now always called before rendering a layout or a layout line.
2010-08-04 17:05:21 +00:00
gboolean create,
PangoFont *font,
PangoGlyph glyph)
{
CoglPangoRenderer *priv = COGL_PANGO_RENDERER (renderer);
CoglPangoRendererCaches *caches = (priv->use_mipmapping ?
&priv->mipmap_caches :
&priv->no_mipmap_caches);
return cogl_pango_glyph_cache_lookup (caches->glyph_cache,
create, font, glyph);
cogl-pango: Use a CoglAtlas to maintain the glyph cache The glyph cache is now stored in a CoglAtlas structure instead of the custom atlasing code. This has the advantage that it can share code with the main texture atlas and that it supports reorganizing the atlas when it becomes full. Unlike the texture atlas, the glyph cache can use multiple atlases which would be neccessary if the maximum texture size is reached and we need to create a second texture. Whenever a display list is created it now has to register a callback with the glyph cache so that the display list can be recreated whenever any of the atlases are reorganized. This is needed because the display list directly stores texture coordinates within the atlas texture and they would become invalid when the texture is moved. The ensure_glyphs_for_layout now works in two steps. First it reserves space in the atlas for all of the glyphs. The atlas is created with the DISABLE_MIGRATION flag so that it won't actually copy any textures if any rearranging is needed. Whenever the position is updated for a glyph then it is marked as dirty. After space for all of the glyphs has been reserved it will iterate over all dirty glyphs and redraw them using Cairo. The rendered glyph is then stored in the texture with a sub texture update. The glyphs need to all be set at the right location before starting to create the display list because the display list stores the texture coordinates of the glyph. If any of the glyphs were moved around then the parts of the display list that was created already would become invalid. To make this work, ensure_glyphs_for_layout is now always called before rendering a layout or a layout line.
2010-08-04 17:05:21 +00:00
}
static void
cogl_pango_renderer_set_dirty_glyph (PangoFont *font,
PangoGlyph glyph,
CoglPangoGlyphCacheValue *value)
{
cairo_surface_t *surface;
cairo_t *cr;
cairo_scaled_font_t *scaled_font;
cairo_glyph_t cairo_glyph;
cairo_format_t format_cairo;
CoglPixelFormat format_cogl;
cogl-pango: Use a CoglAtlas to maintain the glyph cache The glyph cache is now stored in a CoglAtlas structure instead of the custom atlasing code. This has the advantage that it can share code with the main texture atlas and that it supports reorganizing the atlas when it becomes full. Unlike the texture atlas, the glyph cache can use multiple atlases which would be neccessary if the maximum texture size is reached and we need to create a second texture. Whenever a display list is created it now has to register a callback with the glyph cache so that the display list can be recreated whenever any of the atlases are reorganized. This is needed because the display list directly stores texture coordinates within the atlas texture and they would become invalid when the texture is moved. The ensure_glyphs_for_layout now works in two steps. First it reserves space in the atlas for all of the glyphs. The atlas is created with the DISABLE_MIGRATION flag so that it won't actually copy any textures if any rearranging is needed. Whenever the position is updated for a glyph then it is marked as dirty. After space for all of the glyphs has been reserved it will iterate over all dirty glyphs and redraw them using Cairo. The rendered glyph is then stored in the texture with a sub texture update. The glyphs need to all be set at the right location before starting to create the display list because the display list stores the texture coordinates of the glyph. If any of the glyphs were moved around then the parts of the display list that was created already would become invalid. To make this work, ensure_glyphs_for_layout is now always called before rendering a layout or a layout line.
2010-08-04 17:05:21 +00:00
COGL_NOTE (PANGO, "redrawing glyph %i", glyph);
/* Glyphs that don't take up any space will end up without a
texture. These should never become dirty so they shouldn't end up
here */
Add a strong CoglTexture type to replace CoglHandle As part of the on going, incremental effort to purge the non type safe CoglHandle type from the Cogl API this patch tackles most of the CoglHandle uses relating to textures. We'd postponed making this change for quite a while because we wanted to have a clearer understanding of how we wanted to evolve the texture APIs towards Cogl 2.0 before exposing type safety here which would be difficult to change later since it would imply breaking APIs. The basic idea that we are steering towards now is that CoglTexture can be considered to be the most primitive interface we have for any object representing a texture. The texture interface would provide roughly these methods: cogl_texture_get_width cogl_texture_get_height cogl_texture_can_repeat cogl_texture_can_mipmap cogl_texture_generate_mipmap; cogl_texture_get_format cogl_texture_set_region cogl_texture_get_region Besides the texture interface we will then start to expose types corresponding to specific texture types: CoglTexture2D, CoglTexture3D, CoglTexture2DSliced, CoglSubTexture, CoglAtlasTexture and CoglTexturePixmapX11. We will then also expose an interface for the high-level texture types we have (such as CoglTexture2DSlice, CoglSubTexture and CoglAtlasTexture) called CoglMetaTexture. CoglMetaTexture is an additional interface that lets you iterate a virtual region of a meta texture and get mappings of primitive textures to sub-regions of that virtual region. Internally we already have this kind of abstraction for dealing with sliced texture, sub-textures and atlas textures in a consistent way, so this will just make that abstraction public. The aim here is to clarify that there is a difference between primitive textures (CoglTexture2D/3D) and some of the other high-level textures, and also enable developers to implement primitives that can support meta textures since they can only be used with the cogl_rectangle API currently. The thing that's not so clean-cut with this are the texture constructors we have currently; such as cogl_texture_new_from_file which no longer make sense when CoglTexture is considered to be an interface. These will basically just become convenient factory functions and it's just a bit unusual that they are within the cogl_texture namespace. It's worth noting here that all the texture type APIs will also have their own type specific constructors so these functions will only be used for the convenience of being able to create a texture without really wanting to know the details of what type of texture you need. Longer term for 2.0 we may come up with replacement names for these factory functions or the other thing we are considering is designing some asynchronous factory functions instead since it's so often detrimental to application performance to be blocked waiting for a texture to be uploaded to the GPU. Reviewed-by: Neil Roberts <neil@linux.intel.com>
2011-08-24 20:30:34 +00:00
g_return_if_fail (value->texture != NULL);
if (cogl_texture_get_format (value->texture) == COGL_PIXEL_FORMAT_A_8)
{
format_cairo = CAIRO_FORMAT_A8;
format_cogl = COGL_PIXEL_FORMAT_A_8;
}
else
{
format_cairo = CAIRO_FORMAT_ARGB32;
/* Cairo stores the data in native byte order as ARGB but Cogl's
pixel formats specify the actual byte order. Therefore we
need to use a different format depending on the
architecture */
#if G_BYTE_ORDER == G_LITTLE_ENDIAN
format_cogl = COGL_PIXEL_FORMAT_BGRA_8888_PRE;
#else
format_cogl = COGL_PIXEL_FORMAT_ARGB_8888_PRE;
#endif
}
surface = cairo_image_surface_create (format_cairo,
cogl-pango: Use a CoglAtlas to maintain the glyph cache The glyph cache is now stored in a CoglAtlas structure instead of the custom atlasing code. This has the advantage that it can share code with the main texture atlas and that it supports reorganizing the atlas when it becomes full. Unlike the texture atlas, the glyph cache can use multiple atlases which would be neccessary if the maximum texture size is reached and we need to create a second texture. Whenever a display list is created it now has to register a callback with the glyph cache so that the display list can be recreated whenever any of the atlases are reorganized. This is needed because the display list directly stores texture coordinates within the atlas texture and they would become invalid when the texture is moved. The ensure_glyphs_for_layout now works in two steps. First it reserves space in the atlas for all of the glyphs. The atlas is created with the DISABLE_MIGRATION flag so that it won't actually copy any textures if any rearranging is needed. Whenever the position is updated for a glyph then it is marked as dirty. After space for all of the glyphs has been reserved it will iterate over all dirty glyphs and redraw them using Cairo. The rendered glyph is then stored in the texture with a sub texture update. The glyphs need to all be set at the right location before starting to create the display list because the display list stores the texture coordinates of the glyph. If any of the glyphs were moved around then the parts of the display list that was created already would become invalid. To make this work, ensure_glyphs_for_layout is now always called before rendering a layout or a layout line.
2010-08-04 17:05:21 +00:00
value->draw_width,
value->draw_height);
cr = cairo_create (surface);
scaled_font = pango_cairo_font_get_scaled_font (PANGO_CAIRO_FONT (font));
cairo_set_scaled_font (cr, scaled_font);
cairo_set_source_rgba (cr, 1.0, 1.0, 1.0, 1.0);
cogl-pango: Use a CoglAtlas to maintain the glyph cache The glyph cache is now stored in a CoglAtlas structure instead of the custom atlasing code. This has the advantage that it can share code with the main texture atlas and that it supports reorganizing the atlas when it becomes full. Unlike the texture atlas, the glyph cache can use multiple atlases which would be neccessary if the maximum texture size is reached and we need to create a second texture. Whenever a display list is created it now has to register a callback with the glyph cache so that the display list can be recreated whenever any of the atlases are reorganized. This is needed because the display list directly stores texture coordinates within the atlas texture and they would become invalid when the texture is moved. The ensure_glyphs_for_layout now works in two steps. First it reserves space in the atlas for all of the glyphs. The atlas is created with the DISABLE_MIGRATION flag so that it won't actually copy any textures if any rearranging is needed. Whenever the position is updated for a glyph then it is marked as dirty. After space for all of the glyphs has been reserved it will iterate over all dirty glyphs and redraw them using Cairo. The rendered glyph is then stored in the texture with a sub texture update. The glyphs need to all be set at the right location before starting to create the display list because the display list stores the texture coordinates of the glyph. If any of the glyphs were moved around then the parts of the display list that was created already would become invalid. To make this work, ensure_glyphs_for_layout is now always called before rendering a layout or a layout line.
2010-08-04 17:05:21 +00:00
cairo_glyph.x = -value->draw_x;
cairo_glyph.y = -value->draw_y;
/* The PangoCairo glyph numbers directly map to Cairo glyph
numbers */
cairo_glyph.index = glyph;
cairo_show_glyphs (cr, &cairo_glyph, 1);
cairo_destroy (cr);
cairo_surface_flush (surface);
/* Copy the glyph to the texture */
cogl_texture_set_region (value->texture,
0, /* src_x */
0, /* src_y */
value->tx_pixel, /* dst_x */
value->ty_pixel, /* dst_y */
value->draw_width, /* dst_width */
value->draw_height, /* dst_height */
value->draw_width, /* width */
value->draw_height, /* height */
format_cogl,
cogl-pango: Use a CoglAtlas to maintain the glyph cache The glyph cache is now stored in a CoglAtlas structure instead of the custom atlasing code. This has the advantage that it can share code with the main texture atlas and that it supports reorganizing the atlas when it becomes full. Unlike the texture atlas, the glyph cache can use multiple atlases which would be neccessary if the maximum texture size is reached and we need to create a second texture. Whenever a display list is created it now has to register a callback with the glyph cache so that the display list can be recreated whenever any of the atlases are reorganized. This is needed because the display list directly stores texture coordinates within the atlas texture and they would become invalid when the texture is moved. The ensure_glyphs_for_layout now works in two steps. First it reserves space in the atlas for all of the glyphs. The atlas is created with the DISABLE_MIGRATION flag so that it won't actually copy any textures if any rearranging is needed. Whenever the position is updated for a glyph then it is marked as dirty. After space for all of the glyphs has been reserved it will iterate over all dirty glyphs and redraw them using Cairo. The rendered glyph is then stored in the texture with a sub texture update. The glyphs need to all be set at the right location before starting to create the display list because the display list stores the texture coordinates of the glyph. If any of the glyphs were moved around then the parts of the display list that was created already would become invalid. To make this work, ensure_glyphs_for_layout is now always called before rendering a layout or a layout line.
2010-08-04 17:05:21 +00:00
cairo_image_surface_get_stride (surface),
cairo_image_surface_get_data (surface));
cairo_surface_destroy (surface);
}
static void
_cogl_pango_ensure_glyph_cache_for_layout_line_internal (PangoLayoutLine *line)
{
PangoContext *context;
PangoRenderer *renderer;
GSList *l;
context = pango_layout_get_context (line->layout);
renderer =
PANGO_RENDERER (cogl_pango_get_renderer_from_context (context));
for (l = line->runs; l; l = l->next)
{
cogl-pango: Use a CoglAtlas to maintain the glyph cache The glyph cache is now stored in a CoglAtlas structure instead of the custom atlasing code. This has the advantage that it can share code with the main texture atlas and that it supports reorganizing the atlas when it becomes full. Unlike the texture atlas, the glyph cache can use multiple atlases which would be neccessary if the maximum texture size is reached and we need to create a second texture. Whenever a display list is created it now has to register a callback with the glyph cache so that the display list can be recreated whenever any of the atlases are reorganized. This is needed because the display list directly stores texture coordinates within the atlas texture and they would become invalid when the texture is moved. The ensure_glyphs_for_layout now works in two steps. First it reserves space in the atlas for all of the glyphs. The atlas is created with the DISABLE_MIGRATION flag so that it won't actually copy any textures if any rearranging is needed. Whenever the position is updated for a glyph then it is marked as dirty. After space for all of the glyphs has been reserved it will iterate over all dirty glyphs and redraw them using Cairo. The rendered glyph is then stored in the texture with a sub texture update. The glyphs need to all be set at the right location before starting to create the display list because the display list stores the texture coordinates of the glyph. If any of the glyphs were moved around then the parts of the display list that was created already would become invalid. To make this work, ensure_glyphs_for_layout is now always called before rendering a layout or a layout line.
2010-08-04 17:05:21 +00:00
PangoLayoutRun *run = l->data;
PangoGlyphString *glyphs = run->glyphs;
int i;
for (i = 0; i < glyphs->num_glyphs; i++)
{
PangoGlyphInfo *gi = &glyphs->glyphs[i];
/* If the glyph isn't cached then this will reserve
space for it now. We won't actually draw the glyph
yet because reserving space could cause all of the
other glyphs to be moved so we might as well redraw
them all later once we know that the position is
settled */
cogl_pango_renderer_get_cached_glyph (renderer, TRUE,
run->item->analysis.font,
gi->glyph);
}
}
cogl-pango: Use a CoglAtlas to maintain the glyph cache The glyph cache is now stored in a CoglAtlas structure instead of the custom atlasing code. This has the advantage that it can share code with the main texture atlas and that it supports reorganizing the atlas when it becomes full. Unlike the texture atlas, the glyph cache can use multiple atlases which would be neccessary if the maximum texture size is reached and we need to create a second texture. Whenever a display list is created it now has to register a callback with the glyph cache so that the display list can be recreated whenever any of the atlases are reorganized. This is needed because the display list directly stores texture coordinates within the atlas texture and they would become invalid when the texture is moved. The ensure_glyphs_for_layout now works in two steps. First it reserves space in the atlas for all of the glyphs. The atlas is created with the DISABLE_MIGRATION flag so that it won't actually copy any textures if any rearranging is needed. Whenever the position is updated for a glyph then it is marked as dirty. After space for all of the glyphs has been reserved it will iterate over all dirty glyphs and redraw them using Cairo. The rendered glyph is then stored in the texture with a sub texture update. The glyphs need to all be set at the right location before starting to create the display list because the display list stores the texture coordinates of the glyph. If any of the glyphs were moved around then the parts of the display list that was created already would become invalid. To make this work, ensure_glyphs_for_layout is now always called before rendering a layout or a layout line.
2010-08-04 17:05:21 +00:00
}
static void
_cogl_pango_set_dirty_glyphs (CoglPangoRenderer *priv)
{
_cogl_pango_glyph_cache_set_dirty_glyphs
(priv->mipmap_caches.glyph_cache, cogl_pango_renderer_set_dirty_glyph);
_cogl_pango_glyph_cache_set_dirty_glyphs
(priv->no_mipmap_caches.glyph_cache, cogl_pango_renderer_set_dirty_glyph);
}
cogl-pango: Use a CoglAtlas to maintain the glyph cache The glyph cache is now stored in a CoglAtlas structure instead of the custom atlasing code. This has the advantage that it can share code with the main texture atlas and that it supports reorganizing the atlas when it becomes full. Unlike the texture atlas, the glyph cache can use multiple atlases which would be neccessary if the maximum texture size is reached and we need to create a second texture. Whenever a display list is created it now has to register a callback with the glyph cache so that the display list can be recreated whenever any of the atlases are reorganized. This is needed because the display list directly stores texture coordinates within the atlas texture and they would become invalid when the texture is moved. The ensure_glyphs_for_layout now works in two steps. First it reserves space in the atlas for all of the glyphs. The atlas is created with the DISABLE_MIGRATION flag so that it won't actually copy any textures if any rearranging is needed. Whenever the position is updated for a glyph then it is marked as dirty. After space for all of the glyphs has been reserved it will iterate over all dirty glyphs and redraw them using Cairo. The rendered glyph is then stored in the texture with a sub texture update. The glyphs need to all be set at the right location before starting to create the display list because the display list stores the texture coordinates of the glyph. If any of the glyphs were moved around then the parts of the display list that was created already would become invalid. To make this work, ensure_glyphs_for_layout is now always called before rendering a layout or a layout line.
2010-08-04 17:05:21 +00:00
static void
_cogl_pango_ensure_glyph_cache_for_layout_line (PangoLayoutLine *line)
{
PangoContext *context;
CoglPangoRenderer *priv;
context = pango_layout_get_context (line->layout);
priv = cogl_pango_get_renderer_from_context (context);
_cogl_pango_ensure_glyph_cache_for_layout_line_internal (line);
cogl-pango: Use a CoglAtlas to maintain the glyph cache The glyph cache is now stored in a CoglAtlas structure instead of the custom atlasing code. This has the advantage that it can share code with the main texture atlas and that it supports reorganizing the atlas when it becomes full. Unlike the texture atlas, the glyph cache can use multiple atlases which would be neccessary if the maximum texture size is reached and we need to create a second texture. Whenever a display list is created it now has to register a callback with the glyph cache so that the display list can be recreated whenever any of the atlases are reorganized. This is needed because the display list directly stores texture coordinates within the atlas texture and they would become invalid when the texture is moved. The ensure_glyphs_for_layout now works in two steps. First it reserves space in the atlas for all of the glyphs. The atlas is created with the DISABLE_MIGRATION flag so that it won't actually copy any textures if any rearranging is needed. Whenever the position is updated for a glyph then it is marked as dirty. After space for all of the glyphs has been reserved it will iterate over all dirty glyphs and redraw them using Cairo. The rendered glyph is then stored in the texture with a sub texture update. The glyphs need to all be set at the right location before starting to create the display list because the display list stores the texture coordinates of the glyph. If any of the glyphs were moved around then the parts of the display list that was created already would become invalid. To make this work, ensure_glyphs_for_layout is now always called before rendering a layout or a layout line.
2010-08-04 17:05:21 +00:00
/* Now that we know all of the positions are settled we'll fill in
any dirty glyphs */
_cogl_pango_set_dirty_glyphs (priv);
}
void
cogl_pango_ensure_glyph_cache_for_layout (PangoLayout *layout)
{
cogl-pango: Use a CoglAtlas to maintain the glyph cache The glyph cache is now stored in a CoglAtlas structure instead of the custom atlasing code. This has the advantage that it can share code with the main texture atlas and that it supports reorganizing the atlas when it becomes full. Unlike the texture atlas, the glyph cache can use multiple atlases which would be neccessary if the maximum texture size is reached and we need to create a second texture. Whenever a display list is created it now has to register a callback with the glyph cache so that the display list can be recreated whenever any of the atlases are reorganized. This is needed because the display list directly stores texture coordinates within the atlas texture and they would become invalid when the texture is moved. The ensure_glyphs_for_layout now works in two steps. First it reserves space in the atlas for all of the glyphs. The atlas is created with the DISABLE_MIGRATION flag so that it won't actually copy any textures if any rearranging is needed. Whenever the position is updated for a glyph then it is marked as dirty. After space for all of the glyphs has been reserved it will iterate over all dirty glyphs and redraw them using Cairo. The rendered glyph is then stored in the texture with a sub texture update. The glyphs need to all be set at the right location before starting to create the display list because the display list stores the texture coordinates of the glyph. If any of the glyphs were moved around then the parts of the display list that was created already would become invalid. To make this work, ensure_glyphs_for_layout is now always called before rendering a layout or a layout line.
2010-08-04 17:05:21 +00:00
PangoContext *context;
CoglPangoRenderer *priv;
PangoLayoutIter *iter;
cogl-pango: Use a CoglAtlas to maintain the glyph cache The glyph cache is now stored in a CoglAtlas structure instead of the custom atlasing code. This has the advantage that it can share code with the main texture atlas and that it supports reorganizing the atlas when it becomes full. Unlike the texture atlas, the glyph cache can use multiple atlases which would be neccessary if the maximum texture size is reached and we need to create a second texture. Whenever a display list is created it now has to register a callback with the glyph cache so that the display list can be recreated whenever any of the atlases are reorganized. This is needed because the display list directly stores texture coordinates within the atlas texture and they would become invalid when the texture is moved. The ensure_glyphs_for_layout now works in two steps. First it reserves space in the atlas for all of the glyphs. The atlas is created with the DISABLE_MIGRATION flag so that it won't actually copy any textures if any rearranging is needed. Whenever the position is updated for a glyph then it is marked as dirty. After space for all of the glyphs has been reserved it will iterate over all dirty glyphs and redraw them using Cairo. The rendered glyph is then stored in the texture with a sub texture update. The glyphs need to all be set at the right location before starting to create the display list because the display list stores the texture coordinates of the glyph. If any of the glyphs were moved around then the parts of the display list that was created already would become invalid. To make this work, ensure_glyphs_for_layout is now always called before rendering a layout or a layout line.
2010-08-04 17:05:21 +00:00
context = pango_layout_get_context (layout);
priv = cogl_pango_get_renderer_from_context (context);
g_return_if_fail (PANGO_IS_LAYOUT (layout));
if ((iter = pango_layout_get_iter (layout)) == NULL)
return;
do
{
PangoLayoutLine *line;
line = pango_layout_iter_get_line_readonly (iter);
cogl-pango: Use a CoglAtlas to maintain the glyph cache The glyph cache is now stored in a CoglAtlas structure instead of the custom atlasing code. This has the advantage that it can share code with the main texture atlas and that it supports reorganizing the atlas when it becomes full. Unlike the texture atlas, the glyph cache can use multiple atlases which would be neccessary if the maximum texture size is reached and we need to create a second texture. Whenever a display list is created it now has to register a callback with the glyph cache so that the display list can be recreated whenever any of the atlases are reorganized. This is needed because the display list directly stores texture coordinates within the atlas texture and they would become invalid when the texture is moved. The ensure_glyphs_for_layout now works in two steps. First it reserves space in the atlas for all of the glyphs. The atlas is created with the DISABLE_MIGRATION flag so that it won't actually copy any textures if any rearranging is needed. Whenever the position is updated for a glyph then it is marked as dirty. After space for all of the glyphs has been reserved it will iterate over all dirty glyphs and redraw them using Cairo. The rendered glyph is then stored in the texture with a sub texture update. The glyphs need to all be set at the right location before starting to create the display list because the display list stores the texture coordinates of the glyph. If any of the glyphs were moved around then the parts of the display list that was created already would become invalid. To make this work, ensure_glyphs_for_layout is now always called before rendering a layout or a layout line.
2010-08-04 17:05:21 +00:00
_cogl_pango_ensure_glyph_cache_for_layout_line_internal (line);
}
while (pango_layout_iter_next_line (iter));
pango_layout_iter_free (iter);
cogl-pango: Use a CoglAtlas to maintain the glyph cache The glyph cache is now stored in a CoglAtlas structure instead of the custom atlasing code. This has the advantage that it can share code with the main texture atlas and that it supports reorganizing the atlas when it becomes full. Unlike the texture atlas, the glyph cache can use multiple atlases which would be neccessary if the maximum texture size is reached and we need to create a second texture. Whenever a display list is created it now has to register a callback with the glyph cache so that the display list can be recreated whenever any of the atlases are reorganized. This is needed because the display list directly stores texture coordinates within the atlas texture and they would become invalid when the texture is moved. The ensure_glyphs_for_layout now works in two steps. First it reserves space in the atlas for all of the glyphs. The atlas is created with the DISABLE_MIGRATION flag so that it won't actually copy any textures if any rearranging is needed. Whenever the position is updated for a glyph then it is marked as dirty. After space for all of the glyphs has been reserved it will iterate over all dirty glyphs and redraw them using Cairo. The rendered glyph is then stored in the texture with a sub texture update. The glyphs need to all be set at the right location before starting to create the display list because the display list stores the texture coordinates of the glyph. If any of the glyphs were moved around then the parts of the display list that was created already would become invalid. To make this work, ensure_glyphs_for_layout is now always called before rendering a layout or a layout line.
2010-08-04 17:05:21 +00:00
/* Now that we know all of the positions are settled we'll fill in
any dirty glyphs */
_cogl_pango_set_dirty_glyphs (priv);
}
static void
cogl_pango_renderer_set_color_for_part (PangoRenderer *renderer,
PangoRenderPart part)
{
PangoColor *pango_color = pango_renderer_get_color (renderer, part);
CoglPangoRenderer *priv = COGL_PANGO_RENDERER (renderer);
if (pango_color)
{
CoglColor color;
cogl_color_init_from_4ub (&color,
pango_color->red >> 8,
pango_color->green >> 8,
pango_color->blue >> 8,
0xff);
_cogl_pango_display_list_set_color_override (priv->display_list, &color);
}
else
_cogl_pango_display_list_remove_color_override (priv->display_list);
}
static void
cogl_pango_renderer_draw_box (PangoRenderer *renderer,
int x,
int y,
int width,
int height)
{
CoglPangoRenderer *priv = COGL_PANGO_RENDERER (renderer);
g_return_if_fail (priv->display_list != NULL);
_cogl_pango_display_list_add_rectangle (priv->display_list,
x,
y - height,
x + width,
y);
}
static void
cogl_pango_renderer_get_device_units (PangoRenderer *renderer,
int xin,
int yin,
float *xout,
float *yout)
{
const PangoMatrix *matrix;
if ((matrix = pango_renderer_get_matrix (renderer)))
{
/* Convert user-space coords to device coords */
*xout = ((xin * matrix->xx + yin * matrix->xy)
/ PANGO_SCALE + matrix->x0);
*yout = ((yin * matrix->yy + xin * matrix->yx)
/ PANGO_SCALE + matrix->y0);
}
else
{
*xout = PANGO_PIXELS (xin);
*yout = PANGO_PIXELS (yin);
}
}
static void
cogl_pango_renderer_draw_rectangle (PangoRenderer *renderer,
PangoRenderPart part,
int x,
int y,
int width,
int height)
{
CoglPangoRenderer *priv = COGL_PANGO_RENDERER (renderer);
float x1, x2, y1, y2;
g_return_if_fail (priv->display_list != NULL);
cogl_pango_renderer_set_color_for_part (renderer, part);
cogl_pango_renderer_get_device_units (renderer,
x, y,
&x1, &y1);
cogl_pango_renderer_get_device_units (renderer,
x + width, y + height,
&x2, &y2);
_cogl_pango_display_list_add_rectangle (priv->display_list,
x1, y1, x2, y2);
}
static void
cogl_pango_renderer_draw_trapezoid (PangoRenderer *renderer,
PangoRenderPart part,
double y1,
double x11,
double x21,
double y2,
double x12,
double x22)
{
CoglPangoRenderer *priv = COGL_PANGO_RENDERER (renderer);
float points[8];
g_return_if_fail (priv->display_list != NULL);
points[0] = (x11);
points[1] = (y1);
points[2] = (x12);
points[3] = (y2);
points[4] = (x22);
points[5] = points[3];
points[6] = (x21);
points[7] = points[1];
cogl_pango_renderer_set_color_for_part (renderer, part);
_cogl_pango_display_list_add_trapezoid (priv->display_list,
y1,
x11,
x21,
y2,
x12,
x22);
}
static void
cogl_pango_renderer_draw_glyphs (PangoRenderer *renderer,
PangoFont *font,
PangoGlyphString *glyphs,
int xi,
int yi)
{
CoglPangoRenderer *priv = (CoglPangoRenderer *) renderer;
CoglPangoGlyphCacheValue *cache_value;
int i;
cogl_pango_renderer_set_color_for_part (renderer,
PANGO_RENDER_PART_FOREGROUND);
for (i = 0; i < glyphs->num_glyphs; i++)
{
PangoGlyphInfo *gi = glyphs->glyphs + i;
float x, y;
cogl_pango_renderer_get_device_units (renderer,
xi + gi->geometry.x_offset,
yi + gi->geometry.y_offset,
&x, &y);
if ((gi->glyph & PANGO_GLYPH_UNKNOWN_FLAG))
{
if (font == NULL)
{
cogl_pango_renderer_draw_box (renderer,
x,
y,
PANGO_UNKNOWN_GLYPH_WIDTH,
PANGO_UNKNOWN_GLYPH_HEIGHT);
}
else
{
PangoRectangle ink_rect;
pango_font_get_glyph_extents (font, gi->glyph, &ink_rect, NULL);
pango_extents_to_pixels (&ink_rect, NULL);
cogl_pango_renderer_draw_box (renderer,
x + ink_rect.x,
y + ink_rect.y + ink_rect.height,
ink_rect.width,
ink_rect.height);
}
}
else
{
cogl-pango: Use a CoglAtlas to maintain the glyph cache The glyph cache is now stored in a CoglAtlas structure instead of the custom atlasing code. This has the advantage that it can share code with the main texture atlas and that it supports reorganizing the atlas when it becomes full. Unlike the texture atlas, the glyph cache can use multiple atlases which would be neccessary if the maximum texture size is reached and we need to create a second texture. Whenever a display list is created it now has to register a callback with the glyph cache so that the display list can be recreated whenever any of the atlases are reorganized. This is needed because the display list directly stores texture coordinates within the atlas texture and they would become invalid when the texture is moved. The ensure_glyphs_for_layout now works in two steps. First it reserves space in the atlas for all of the glyphs. The atlas is created with the DISABLE_MIGRATION flag so that it won't actually copy any textures if any rearranging is needed. Whenever the position is updated for a glyph then it is marked as dirty. After space for all of the glyphs has been reserved it will iterate over all dirty glyphs and redraw them using Cairo. The rendered glyph is then stored in the texture with a sub texture update. The glyphs need to all be set at the right location before starting to create the display list because the display list stores the texture coordinates of the glyph. If any of the glyphs were moved around then the parts of the display list that was created already would become invalid. To make this work, ensure_glyphs_for_layout is now always called before rendering a layout or a layout line.
2010-08-04 17:05:21 +00:00
/* Get the texture containing the glyph */
cache_value =
cogl_pango_renderer_get_cached_glyph (renderer,
cogl-pango: Use a CoglAtlas to maintain the glyph cache The glyph cache is now stored in a CoglAtlas structure instead of the custom atlasing code. This has the advantage that it can share code with the main texture atlas and that it supports reorganizing the atlas when it becomes full. Unlike the texture atlas, the glyph cache can use multiple atlases which would be neccessary if the maximum texture size is reached and we need to create a second texture. Whenever a display list is created it now has to register a callback with the glyph cache so that the display list can be recreated whenever any of the atlases are reorganized. This is needed because the display list directly stores texture coordinates within the atlas texture and they would become invalid when the texture is moved. The ensure_glyphs_for_layout now works in two steps. First it reserves space in the atlas for all of the glyphs. The atlas is created with the DISABLE_MIGRATION flag so that it won't actually copy any textures if any rearranging is needed. Whenever the position is updated for a glyph then it is marked as dirty. After space for all of the glyphs has been reserved it will iterate over all dirty glyphs and redraw them using Cairo. The rendered glyph is then stored in the texture with a sub texture update. The glyphs need to all be set at the right location before starting to create the display list because the display list stores the texture coordinates of the glyph. If any of the glyphs were moved around then the parts of the display list that was created already would become invalid. To make this work, ensure_glyphs_for_layout is now always called before rendering a layout or a layout line.
2010-08-04 17:05:21 +00:00
FALSE,
font,
gi->glyph);
cogl-pango: Use a CoglAtlas to maintain the glyph cache The glyph cache is now stored in a CoglAtlas structure instead of the custom atlasing code. This has the advantage that it can share code with the main texture atlas and that it supports reorganizing the atlas when it becomes full. Unlike the texture atlas, the glyph cache can use multiple atlases which would be neccessary if the maximum texture size is reached and we need to create a second texture. Whenever a display list is created it now has to register a callback with the glyph cache so that the display list can be recreated whenever any of the atlases are reorganized. This is needed because the display list directly stores texture coordinates within the atlas texture and they would become invalid when the texture is moved. The ensure_glyphs_for_layout now works in two steps. First it reserves space in the atlas for all of the glyphs. The atlas is created with the DISABLE_MIGRATION flag so that it won't actually copy any textures if any rearranging is needed. Whenever the position is updated for a glyph then it is marked as dirty. After space for all of the glyphs has been reserved it will iterate over all dirty glyphs and redraw them using Cairo. The rendered glyph is then stored in the texture with a sub texture update. The glyphs need to all be set at the right location before starting to create the display list because the display list stores the texture coordinates of the glyph. If any of the glyphs were moved around then the parts of the display list that was created already would become invalid. To make this work, ensure_glyphs_for_layout is now always called before rendering a layout or a layout line.
2010-08-04 17:05:21 +00:00
/* cogl_pango_ensure_glyph_cache_for_layout should always be
called before rendering a layout so we should never have
a dirty glyph here */
g_assert (cache_value == NULL || !cache_value->dirty);
if (cache_value == NULL)
{
cogl_pango_renderer_draw_box (renderer,
x,
y,
PANGO_UNKNOWN_GLYPH_WIDTH,
PANGO_UNKNOWN_GLYPH_HEIGHT);
}
else if (cache_value->texture)
{
x += (float)(cache_value->draw_x);
y += (float)(cache_value->draw_y);
cogl_pango_renderer_draw_glyph (priv, cache_value, x, y);
}
}
xi += gi->geometry.width;
}
}