2010-02-26 07:38:22 -05:00
|
|
|
/*
|
|
|
|
* Cogl
|
|
|
|
*
|
2014-02-21 20:28:54 -05:00
|
|
|
* A Low Level GPU Graphics and Utilities API
|
2010-02-26 07:38:22 -05:00
|
|
|
*
|
|
|
|
* Copyright (C) 2011 Intel Corporation.
|
|
|
|
*
|
2014-02-21 20:28:54 -05:00
|
|
|
* Permission is hereby granted, free of charge, to any person
|
|
|
|
* obtaining a copy of this software and associated documentation
|
|
|
|
* files (the "Software"), to deal in the Software without
|
|
|
|
* restriction, including without limitation the rights to use, copy,
|
|
|
|
* modify, merge, publish, distribute, sublicense, and/or sell copies
|
|
|
|
* of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
2010-02-26 07:38:22 -05:00
|
|
|
*
|
2014-02-21 20:28:54 -05:00
|
|
|
* The above copyright notice and this permission notice shall be
|
|
|
|
* included in all copies or substantial portions of the Software.
|
2010-02-26 07:38:22 -05:00
|
|
|
*
|
2014-02-21 20:28:54 -05:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
|
|
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
* SOFTWARE.
|
2010-02-26 07:38:22 -05:00
|
|
|
*
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Neil Roberts <neil@linux.intel.com>
|
|
|
|
*/
|
|
|
|
|
2016-05-05 10:21:51 -04:00
|
|
|
#include "cogl-config.h"
|
2010-02-26 07:38:22 -05:00
|
|
|
|
|
|
|
#include <glib.h>
|
|
|
|
|
2023-08-07 09:38:12 -04:00
|
|
|
#include "cogl-pango/cogl-pango-pipeline-cache.h"
|
2012-02-18 11:03:10 -05:00
|
|
|
#include "cogl/cogl-context-private.h"
|
2014-02-27 06:18:27 -05:00
|
|
|
#include "cogl/cogl-texture-private.h"
|
2012-02-18 11:03:10 -05:00
|
|
|
|
2010-02-26 07:38:22 -05:00
|
|
|
typedef struct _CoglPangoPipelineCacheEntry CoglPangoPipelineCacheEntry;
|
|
|
|
|
|
|
|
struct _CoglPangoPipelineCacheEntry
|
|
|
|
{
|
|
|
|
/* This will take a reference or it can be NULL to represent the
|
|
|
|
pipeline used to render colors */
|
2011-08-24 16:30:34 -04:00
|
|
|
CoglTexture *texture;
|
2010-02-26 07:38:22 -05:00
|
|
|
|
|
|
|
/* This will only take a weak reference */
|
2012-04-16 09:14:10 -04:00
|
|
|
CoglPipeline *pipeline;
|
2010-02-26 07:38:22 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
Switch use of primitive glib types to c99 equivalents
The coding style has for a long time said to avoid using redundant glib
data types such as gint or gchar etc because we feel that they make the
code look unnecessarily foreign to developers coming from outside of the
Gnome developer community.
Note: When we tried to find the historical rationale for the types we
just found that they were apparently only added for consistent syntax
highlighting which didn't seem that compelling.
Up until now we have been continuing to use some of the platform
specific type such as gint{8,16,32,64} and gsize but this patch switches
us over to using the standard c99 equivalents instead so we can further
ensure that our code looks familiar to the widest range of C developers
who might potentially contribute to Cogl.
So instead of using the gint{8,16,32,64} and guint{8,16,32,64} types this
switches all Cogl code to instead use the int{8,16,32,64}_t and
uint{8,16,32,64}_t c99 types instead.
Instead of gsize we now use size_t
For now we are not going to use the c99 _Bool type and instead we have
introduced a new CoglBool type to use instead of gboolean.
Reviewed-by: Neil Roberts <neil@linux.intel.com>
(cherry picked from commit 5967dad2400d32ca6319cef6cb572e81bf2c15f0)
2012-04-16 16:56:40 -04:00
|
|
|
_cogl_pango_pipeline_cache_key_destroy (void *data)
|
2010-02-26 07:38:22 -05:00
|
|
|
{
|
|
|
|
if (data)
|
|
|
|
cogl_object_unref (data);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
Switch use of primitive glib types to c99 equivalents
The coding style has for a long time said to avoid using redundant glib
data types such as gint or gchar etc because we feel that they make the
code look unnecessarily foreign to developers coming from outside of the
Gnome developer community.
Note: When we tried to find the historical rationale for the types we
just found that they were apparently only added for consistent syntax
highlighting which didn't seem that compelling.
Up until now we have been continuing to use some of the platform
specific type such as gint{8,16,32,64} and gsize but this patch switches
us over to using the standard c99 equivalents instead so we can further
ensure that our code looks familiar to the widest range of C developers
who might potentially contribute to Cogl.
So instead of using the gint{8,16,32,64} and guint{8,16,32,64} types this
switches all Cogl code to instead use the int{8,16,32,64}_t and
uint{8,16,32,64}_t c99 types instead.
Instead of gsize we now use size_t
For now we are not going to use the c99 _Bool type and instead we have
introduced a new CoglBool type to use instead of gboolean.
Reviewed-by: Neil Roberts <neil@linux.intel.com>
(cherry picked from commit 5967dad2400d32ca6319cef6cb572e81bf2c15f0)
2012-04-16 16:56:40 -04:00
|
|
|
_cogl_pango_pipeline_cache_value_destroy (void *data)
|
2010-02-26 07:38:22 -05:00
|
|
|
{
|
|
|
|
CoglPangoPipelineCacheEntry *cache_entry = data;
|
|
|
|
|
|
|
|
if (cache_entry->texture)
|
2023-09-18 10:58:36 -04:00
|
|
|
g_object_unref (cache_entry->texture);
|
2010-02-26 07:38:22 -05:00
|
|
|
|
|
|
|
/* We don't need to unref the pipeline because it only takes a weak
|
|
|
|
reference */
|
|
|
|
|
2020-10-18 07:46:08 -04:00
|
|
|
g_free (cache_entry);
|
2010-02-26 07:38:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
CoglPangoPipelineCache *
|
2012-05-10 07:16:03 -04:00
|
|
|
_cogl_pango_pipeline_cache_new (CoglContext *ctx,
|
2018-11-24 07:04:47 -05:00
|
|
|
gboolean use_mipmapping)
|
2010-02-26 07:38:22 -05:00
|
|
|
{
|
|
|
|
CoglPangoPipelineCache *cache = g_new (CoglPangoPipelineCache, 1);
|
|
|
|
|
2023-08-17 06:08:32 -04:00
|
|
|
cache->ctx = g_object_ref (ctx);
|
2012-05-10 07:16:03 -04:00
|
|
|
|
2010-02-26 07:38:22 -05:00
|
|
|
/* The key is the pipeline pointer. A reference is taken when the
|
|
|
|
pipeline is used as a key so we should unref it again in the
|
|
|
|
destroy function */
|
|
|
|
cache->hash_table =
|
|
|
|
g_hash_table_new_full (g_direct_hash,
|
|
|
|
g_direct_equal,
|
|
|
|
_cogl_pango_pipeline_cache_key_destroy,
|
|
|
|
_cogl_pango_pipeline_cache_value_destroy);
|
|
|
|
|
2010-02-26 08:01:54 -05:00
|
|
|
cache->base_texture_rgba_pipeline = NULL;
|
|
|
|
cache->base_texture_alpha_pipeline = NULL;
|
2010-02-26 07:38:22 -05:00
|
|
|
|
|
|
|
cache->use_mipmapping = use_mipmapping;
|
|
|
|
|
|
|
|
return cache;
|
|
|
|
}
|
|
|
|
|
|
|
|
static CoglPipeline *
|
2010-02-26 08:01:54 -05:00
|
|
|
get_base_texture_rgba_pipeline (CoglPangoPipelineCache *cache)
|
2010-02-26 07:38:22 -05:00
|
|
|
{
|
2010-02-26 08:01:54 -05:00
|
|
|
if (cache->base_texture_rgba_pipeline == NULL)
|
2010-02-26 07:38:22 -05:00
|
|
|
{
|
|
|
|
CoglPipeline *pipeline;
|
|
|
|
|
2012-05-10 07:16:03 -04:00
|
|
|
pipeline = cache->base_texture_rgba_pipeline =
|
|
|
|
cogl_pipeline_new (cache->ctx);
|
2010-02-26 08:01:54 -05:00
|
|
|
|
|
|
|
cogl_pipeline_set_layer_wrap_mode (pipeline, 0,
|
2012-02-18 09:43:01 -05:00
|
|
|
COGL_PIPELINE_WRAP_MODE_CLAMP_TO_EDGE);
|
2010-02-26 08:01:54 -05:00
|
|
|
|
|
|
|
if (cache->use_mipmapping)
|
|
|
|
cogl_pipeline_set_layer_filters
|
|
|
|
(pipeline, 0,
|
|
|
|
COGL_PIPELINE_FILTER_LINEAR_MIPMAP_LINEAR,
|
|
|
|
COGL_PIPELINE_FILTER_LINEAR);
|
|
|
|
}
|
|
|
|
|
|
|
|
return cache->base_texture_rgba_pipeline;
|
|
|
|
}
|
|
|
|
|
|
|
|
static CoglPipeline *
|
|
|
|
get_base_texture_alpha_pipeline (CoglPangoPipelineCache *cache)
|
|
|
|
{
|
|
|
|
if (cache->base_texture_alpha_pipeline == NULL)
|
|
|
|
{
|
|
|
|
CoglPipeline *pipeline;
|
|
|
|
|
|
|
|
pipeline = cogl_pipeline_copy (get_base_texture_rgba_pipeline (cache));
|
|
|
|
cache->base_texture_alpha_pipeline = pipeline;
|
2010-02-26 07:38:22 -05:00
|
|
|
|
|
|
|
/* The default combine mode of materials is to modulate (A x B)
|
|
|
|
* the texture RGBA channels with the RGBA channels of the
|
|
|
|
* previous layer (which in our case is just the font color)
|
|
|
|
*
|
|
|
|
* Since the RGB for an alpha texture is defined as 0, this gives us:
|
|
|
|
*
|
|
|
|
* result.rgb = color.rgb * 0
|
|
|
|
* result.a = color.a * texture.a
|
|
|
|
*
|
|
|
|
* What we want is premultiplied rgba values:
|
|
|
|
*
|
|
|
|
* result.rgba = color.rgb * texture.a
|
|
|
|
* result.a = color.a * texture.a
|
|
|
|
*/
|
|
|
|
cogl_pipeline_set_layer_combine (pipeline, 0, /* layer */
|
|
|
|
"RGBA = MODULATE (PREVIOUS, TEXTURE[A])",
|
|
|
|
NULL);
|
|
|
|
}
|
|
|
|
|
2010-02-26 08:01:54 -05:00
|
|
|
return cache->base_texture_alpha_pipeline;
|
2010-02-26 07:38:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
CoglPangoPipelineCache *cache;
|
2011-08-24 16:30:34 -04:00
|
|
|
CoglTexture *texture;
|
2010-02-26 07:38:22 -05:00
|
|
|
} PipelineDestroyNotifyData;
|
|
|
|
|
|
|
|
static void
|
|
|
|
pipeline_destroy_notify_cb (void *user_data)
|
|
|
|
{
|
|
|
|
PipelineDestroyNotifyData *data = user_data;
|
|
|
|
|
|
|
|
g_hash_table_remove (data->cache->hash_table, data->texture);
|
2020-10-18 07:46:08 -04:00
|
|
|
g_free (data);
|
2010-02-26 07:38:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
CoglPipeline *
|
|
|
|
_cogl_pango_pipeline_cache_get (CoglPangoPipelineCache *cache,
|
2023-09-18 10:58:36 -04:00
|
|
|
CoglTexture *texture)
|
2010-02-26 07:38:22 -05:00
|
|
|
{
|
|
|
|
CoglPangoPipelineCacheEntry *entry;
|
|
|
|
PipelineDestroyNotifyData *destroy_data;
|
|
|
|
static CoglUserDataKey pipeline_destroy_notify_key;
|
|
|
|
|
|
|
|
/* Look for an existing entry */
|
|
|
|
entry = g_hash_table_lookup (cache->hash_table, texture);
|
|
|
|
|
|
|
|
if (entry)
|
|
|
|
return cogl_object_ref (entry->pipeline);
|
|
|
|
|
|
|
|
/* No existing pipeline was found so let's create another */
|
2020-10-18 07:46:08 -04:00
|
|
|
entry = g_new0 (CoglPangoPipelineCacheEntry, 1);
|
2010-02-26 07:38:22 -05:00
|
|
|
|
|
|
|
if (texture)
|
|
|
|
{
|
2010-02-26 08:01:54 -05:00
|
|
|
CoglPipeline *base;
|
|
|
|
|
2023-09-18 10:58:36 -04:00
|
|
|
entry->texture = g_object_ref (texture);
|
2010-02-26 08:01:54 -05:00
|
|
|
|
2013-06-27 13:33:04 -04:00
|
|
|
if (_cogl_texture_get_format (entry->texture) == COGL_PIXEL_FORMAT_A_8)
|
2010-02-26 08:01:54 -05:00
|
|
|
base = get_base_texture_alpha_pipeline (cache);
|
|
|
|
else
|
|
|
|
base = get_base_texture_rgba_pipeline (cache);
|
|
|
|
|
|
|
|
entry->pipeline = cogl_pipeline_copy (base);
|
2010-02-26 07:38:22 -05:00
|
|
|
|
|
|
|
cogl_pipeline_set_layer_texture (entry->pipeline, 0 /* layer */, texture);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
entry->texture = NULL;
|
2012-05-10 07:16:03 -04:00
|
|
|
entry->pipeline = cogl_pipeline_new (cache->ctx);
|
2010-02-26 07:38:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Add a weak reference to the pipeline so we can remove it from the
|
|
|
|
hash table when it is destroyed */
|
2020-10-18 07:46:08 -04:00
|
|
|
destroy_data = g_new0 (PipelineDestroyNotifyData, 1);
|
2010-02-26 07:38:22 -05:00
|
|
|
destroy_data->cache = cache;
|
|
|
|
destroy_data->texture = texture;
|
2012-04-16 09:14:10 -04:00
|
|
|
cogl_object_set_user_data (COGL_OBJECT (entry->pipeline),
|
2010-02-26 07:38:22 -05:00
|
|
|
&pipeline_destroy_notify_key,
|
|
|
|
destroy_data,
|
|
|
|
pipeline_destroy_notify_cb);
|
|
|
|
|
|
|
|
g_hash_table_insert (cache->hash_table,
|
2023-09-18 10:58:36 -04:00
|
|
|
texture ? g_object_ref (texture) : NULL,
|
2010-02-26 07:38:22 -05:00
|
|
|
entry);
|
|
|
|
|
|
|
|
/* This doesn't take a reference on the pipeline so that it will use
|
|
|
|
the newly created reference */
|
|
|
|
return entry->pipeline;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
_cogl_pango_pipeline_cache_free (CoglPangoPipelineCache *cache)
|
|
|
|
{
|
2010-02-26 08:01:54 -05:00
|
|
|
if (cache->base_texture_rgba_pipeline)
|
|
|
|
cogl_object_unref (cache->base_texture_rgba_pipeline);
|
|
|
|
if (cache->base_texture_alpha_pipeline)
|
|
|
|
cogl_object_unref (cache->base_texture_alpha_pipeline);
|
2010-02-26 07:38:22 -05:00
|
|
|
|
|
|
|
g_hash_table_destroy (cache->hash_table);
|
|
|
|
|
2023-08-17 06:08:32 -04:00
|
|
|
g_object_unref (cache->ctx);
|
2012-05-10 07:16:03 -04:00
|
|
|
|
2010-02-26 07:38:22 -05:00
|
|
|
g_free (cache);
|
|
|
|
}
|