4 * An object oriented GL/GLES Abstraction/Utility Layer
6 * Copyright (C) 2011 Intel Corporation.
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library. If not, see
20 * <http://www.gnu.org/licenses/>.
25 * Neil Roberts <neil@linux.intel.com>
33 #include <cogl/cogl.h>
34 #include "cogl-pango-pipeline-cache.h"
36 #include "cogl/cogl-context-private.h"
38 typedef struct _CoglPangoPipelineCacheEntry CoglPangoPipelineCacheEntry;
40 struct _CoglPangoPipelineCache
42 GHashTable *hash_table;
44 CoglPipeline *base_texture_alpha_pipeline;
45 CoglPipeline *base_texture_rgba_pipeline;
47 gboolean use_mipmapping;
50 struct _CoglPangoPipelineCacheEntry
52 /* This will take a reference or it can be NULL to represent the
53 pipeline used to render colors */
56 /* This will only take a weak reference */
61 _cogl_pango_pipeline_cache_key_destroy (gpointer data)
64 cogl_object_unref (data);
68 _cogl_pango_pipeline_cache_value_destroy (gpointer data)
70 CoglPangoPipelineCacheEntry *cache_entry = data;
72 if (cache_entry->texture)
73 cogl_object_unref (cache_entry->texture);
75 /* We don't need to unref the pipeline because it only takes a weak
78 g_slice_free (CoglPangoPipelineCacheEntry, cache_entry);
81 CoglPangoPipelineCache *
82 _cogl_pango_pipeline_cache_new (gboolean use_mipmapping)
84 CoglPangoPipelineCache *cache = g_new (CoglPangoPipelineCache, 1);
86 /* The key is the pipeline pointer. A reference is taken when the
87 pipeline is used as a key so we should unref it again in the
90 g_hash_table_new_full (g_direct_hash,
92 _cogl_pango_pipeline_cache_key_destroy,
93 _cogl_pango_pipeline_cache_value_destroy);
95 cache->base_texture_rgba_pipeline = NULL;
96 cache->base_texture_alpha_pipeline = NULL;
98 cache->use_mipmapping = use_mipmapping;
103 static CoglPipeline *
104 get_base_texture_rgba_pipeline (CoglPangoPipelineCache *cache)
106 if (cache->base_texture_rgba_pipeline == NULL)
108 CoglPipeline *pipeline;
110 _COGL_GET_CONTEXT (ctx, NULL);
112 pipeline = cache->base_texture_rgba_pipeline = cogl_pipeline_new (ctx);
114 cogl_pipeline_set_layer_wrap_mode (pipeline, 0,
115 COGL_PIPELINE_WRAP_MODE_CLAMP_TO_EDGE);
117 if (cache->use_mipmapping)
118 cogl_pipeline_set_layer_filters
120 COGL_PIPELINE_FILTER_LINEAR_MIPMAP_LINEAR,
121 COGL_PIPELINE_FILTER_LINEAR);
124 return cache->base_texture_rgba_pipeline;
127 static CoglPipeline *
128 get_base_texture_alpha_pipeline (CoglPangoPipelineCache *cache)
130 if (cache->base_texture_alpha_pipeline == NULL)
132 CoglPipeline *pipeline;
134 pipeline = cogl_pipeline_copy (get_base_texture_rgba_pipeline (cache));
135 cache->base_texture_alpha_pipeline = pipeline;
137 /* The default combine mode of materials is to modulate (A x B)
138 * the texture RGBA channels with the RGBA channels of the
139 * previous layer (which in our case is just the font color)
141 * Since the RGB for an alpha texture is defined as 0, this gives us:
143 * result.rgb = color.rgb * 0
144 * result.a = color.a * texture.a
146 * What we want is premultiplied rgba values:
148 * result.rgba = color.rgb * texture.a
149 * result.a = color.a * texture.a
151 cogl_pipeline_set_layer_combine (pipeline, 0, /* layer */
152 "RGBA = MODULATE (PREVIOUS, TEXTURE[A])",
156 return cache->base_texture_alpha_pipeline;
161 CoglPangoPipelineCache *cache;
162 CoglTexture *texture;
163 } PipelineDestroyNotifyData;
166 pipeline_destroy_notify_cb (void *user_data)
168 PipelineDestroyNotifyData *data = user_data;
170 g_hash_table_remove (data->cache->hash_table, data->texture);
171 g_slice_free (PipelineDestroyNotifyData, data);
175 _cogl_pango_pipeline_cache_get (CoglPangoPipelineCache *cache,
178 CoglPangoPipelineCacheEntry *entry;
179 PipelineDestroyNotifyData *destroy_data;
180 static CoglUserDataKey pipeline_destroy_notify_key;
182 /* Look for an existing entry */
183 entry = g_hash_table_lookup (cache->hash_table, texture);
186 return cogl_object_ref (entry->pipeline);
188 /* No existing pipeline was found so let's create another */
189 entry = g_slice_new (CoglPangoPipelineCacheEntry);
195 entry->texture = cogl_object_ref (texture);
197 if (cogl_texture_get_format (entry->texture) == COGL_PIXEL_FORMAT_A_8)
198 base = get_base_texture_alpha_pipeline (cache);
200 base = get_base_texture_rgba_pipeline (cache);
202 entry->pipeline = cogl_pipeline_copy (base);
204 cogl_pipeline_set_layer_texture (entry->pipeline, 0 /* layer */, texture);
208 _COGL_GET_CONTEXT (ctx, NULL);
210 entry->texture = NULL;
211 entry->pipeline = cogl_pipeline_new (ctx);
214 /* Add a weak reference to the pipeline so we can remove it from the
215 hash table when it is destroyed */
216 destroy_data = g_slice_new (PipelineDestroyNotifyData);
217 destroy_data->cache = cache;
218 destroy_data->texture = texture;
219 cogl_object_set_user_data (entry->pipeline,
220 &pipeline_destroy_notify_key,
222 pipeline_destroy_notify_cb);
224 g_hash_table_insert (cache->hash_table,
225 texture ? cogl_object_ref (texture) : NULL,
228 /* This doesn't take a reference on the pipeline so that it will use
229 the newly created reference */
230 return entry->pipeline;
234 _cogl_pango_pipeline_cache_free (CoglPangoPipelineCache *cache)
236 if (cache->base_texture_rgba_pipeline)
237 cogl_object_unref (cache->base_texture_rgba_pipeline);
238 if (cache->base_texture_alpha_pipeline)
239 cogl_object_unref (cache->base_texture_alpha_pipeline);
241 g_hash_table_destroy (cache->hash_table);