From d1f6dbaa79f31722ef52305ae257f3efe6c246a7 Mon Sep 17 00:00:00 2001 From: Emmanuele Bassi Date: Thu, 30 Oct 2008 16:50:07 +0000 Subject: [PATCH] 2008-10-30 Emmanuele Bassi Bug 1210 - Add CoglColor API * clutter/cogl/cogl-color.h: * clutter/cogl/cogl.h.in: * clutter/cogl/common/Makefile.am: * clutter/cogl/common/cogl-color.c: * clutter/cogl/gl/Makefile.am: * clutter/cogl/gl/cogl.c: * clutter/cogl/gles/Makefile.am: * clutter/cogl/gles/cogl-texture.c: * clutter/cogl/gles/cogl.c: Add a new color-type, to be used by COGL. CoglColor is optimized to allow the minimum amount of conversions possible for both GL and GLES implementations. * clutter/clutter-actor.c: * clutter/clutter-clone-texture.c: * clutter/clutter-entry.c: * clutter/clutter-main.c: * clutter/clutter-rectangle.c: * clutter/clutter-stage.c: * clutter/clutter-texture.c: Use CoglColor when needed. * clutter/pango/pangoclutter-render.c: Use CoglColor when needed. * doc/reference/cogl/cogl-docs.sgml: * doc/reference/cogl/cogl-sections.txt: Update the documentation. * tests/test-cogl-offscreen.c: * tests/test-cogl-primitives.c: * tests/test-cogl-tex-convert.c: * tests/test-cogl-tex-foreign.c: * tests/test-cogl-tex-getset.c: * tests/test-cogl-tex-polygon.c: * tests/test-cogl-tex-tile.c: * tests/test-paint-wrapper.c: Update the tests. * README: Update release notes. --- cogl-color.h | 223 +++++++++++++++++++++++++++ cogl.h.in | 13 +- common/Makefile.am | 3 +- common/cogl-color.c | 107 +++++++++++++ doc/reference/cogl/cogl-docs.sgml | 1 + doc/reference/cogl/cogl-sections.txt | 26 ++++ gl/Makefile.am | 2 + gl/cogl.c | 32 ++-- gles/Makefile.am | 2 + gles/cogl-texture.c | 26 ++-- gles/cogl.c | 32 ++-- 11 files changed, 415 insertions(+), 52 deletions(-) create mode 100644 cogl-color.h create mode 100644 common/cogl-color.c diff --git a/cogl-color.h b/cogl-color.h new file mode 100644 index 000000000..14c5d8413 --- /dev/null +++ b/cogl-color.h @@ -0,0 +1,223 @@ +#ifndef __COGL_COLOR_H__ +#define __COGL_COLOR_H__ + +#include +#include + +G_BEGIN_DECLS + +typedef struct _CoglColor CoglColor; + +/** + * CoglColor: + * + * A structure for holding a color definition. The contents of + * the CoglColor structure are private and should never by accessed + * directly. + * + * Since: 1.0 + */ +struct _CoglColor +{ + /*< private >*/ + CoglFixed red; + CoglFixed green; + CoglFixed blue; + + CoglFixed alpha; +}; + +/** + * cogl_color_set_from_4ub: + * @dest: return location for a #CoglColor + * @red: value of the red channel, between 0 and 255 + * @green: value of the green channel, between 0 and 255 + * @blue: value of the blue channel, between 0 and 255 + * @alpha: value of the alpha channel, between 0 and 255 + * + * Sets the values of the passed channel into a #CoglColor. + * + * Since: 1.0 + */ +void cogl_color_set_from_4ub (CoglColor *dest, + guint8 red, + guint8 green, + guint8 blue, + guint8 alpha); +/** + * cogl_color_set_from_4ub: + * @dest: return location for a #CoglColor + * @red: value of the red channel, between 0 and 1 + * @green: value of the green channel, between 0 and 1 + * @blue: value of the blue channel, between 0 and 1 + * @alpha: value of the alpha channel, between 0 and 1 + * + * Sets the values of the passed channel into a #CoglColor. + * + * Since: 1.0 + */ +void cogl_color_set_from_4d (CoglColor *dest, + gdouble red, + gdouble green, + gdouble blue, + gdouble alpha); + +/** + * cogl_color_get_red_byte: + * @color: a #CoglColor + * + * Retrieves the red channel of @color as a byte value + * between 0 and 255 + * + * Return value: the red channel of the passed color + * + * Since: 1.0 + */ +unsigned char cogl_color_get_red_byte (const CoglColor *color); + +/** + * cogl_color_get_green_byte: + * @color: a #CoglColor + * + * Retrieves the green channel of @color as a byte value + * between 0 and 255 + * + * Return value: the green channel of the passed color + * + * Since: 1.0 + */ +unsigned char cogl_color_get_green_byte (const CoglColor *color); + +/** + * cogl_color_get_blue_byte: + * @color: a #CoglColor + * + * Retrieves the blue channel of @color as a byte value + * between 0 and 255 + * + * Return value: the blue channel of the passed color + * + * Since: 1.0 + */ +unsigned char cogl_color_get_blue_byte (const CoglColor *color); + +/** + * cogl_color_get_alpha_byte: + * @color: a #CoglColor + * + * Retrieves the alpha channel of @color as a byte value + * between 0 and 255 + * + * Return value: the alpha channel of the passed color + * + * Since: 1.0 + */ +unsigned char cogl_color_get_alpha_byte (const CoglColor *color); + +/** + * cogl_color_get_red_float: + * @color: a #CoglColor + * + * Retrieves the red channel of @color as a floating point + * value between 0.0 and 1.0 + * + * Return value: the red channel of the passed color + * + * Since: 1.0 + */ +float cogl_color_get_red_float (const CoglColor *color); + +/** + * cogl_color_get_green_float: + * @color: a #CoglColor + * + * Retrieves the green channel of @color as a floating point + * value between 0.0 and 1.0 + * + * Return value: the green channel of the passed color + * + * Since: 1.0 + */ +float cogl_color_get_green_float (const CoglColor *color); + +/** + * cogl_color_get_blue_float: + * @color: a #CoglColor + * + * Retrieves the blue channel of @color as a floating point + * value between 0.0 and 1.0 + * + * Return value: the blue channel of the passed color + * + * Since: 1.0 + */ +float cogl_color_get_blue_float (const CoglColor *color); + +/** + * cogl_color_get_alpha_float: + * @color: a #CoglColor + * + * Retrieves the alpha channel of @color as a floating point + * value between 0.0 and 1.0 + * + * Return value: the alpha channel of the passed color + * + * Since: 1.0 + */ +float cogl_color_get_alpha_float (const CoglColor *color); + +/** + * cogl_color_get_red: + * @color: a #CoglColor + * + * Retrieves the red channel of @color as a fixed point + * value between 0 and %COGL_FIXED_1. + * + * Return value: the red channel of the passed color + * + * Since: 1.0 + */ +CoglFixed cogl_color_get_red (const CoglColor *color); + +/** + * cogl_color_get_green: + * @color: a #CoglColor + * + * Retrieves the green channel of @color as a fixed point + * value between 0 and %COGL_FIXED_1. + * + * Return value: the green channel of the passed color + * + * Since: 1.0 + */ +CoglFixed cogl_color_get_green (const CoglColor *color); + +/** + * cogl_color_get_blue: + * @color: a #CoglColor + * + * Retrieves the blue channel of @color as a fixed point + * value between 0 and %COGL_FIXED_1. + * + * Return value: the blue channel of the passed color + * + * Since: 1.0 + */ +CoglFixed cogl_color_get_blue (const CoglColor *color); + +/** + * cogl_color_get_alpha: + * @color: a #CoglColor + * + * Retrieves the alpha channel of @color as a fixed point + * value between 0 and %COGL_FIXED_1. + * + * Return value: the alpha channel of the passed color + * + * Since: 1.0 + */ +CoglFixed cogl_color_get_alpha (const CoglColor *color); + +G_END_DECLS + +#endif /* __COGL_COLOR_H__ */ diff --git a/cogl.h.in b/cogl.h.in index 6f20ab9fd..02d54dce7 100644 --- a/cogl.h.in +++ b/cogl.h.in @@ -43,12 +43,11 @@ #define __COGL_H__ #include -#include #include -#include #include #include +#include G_BEGIN_DECLS @@ -212,7 +211,7 @@ struct _CoglTextureVertex { CoglFixed x, y, z; CoglFixed tx, ty; - ClutterColor color; + CoglColor color; }; typedef struct _CoglTextureVertex CoglTextureVertex; @@ -618,7 +617,7 @@ void cogl_alpha_func (COGLenum func, * with @fog_color. Fogging will remain enabled until the next call to * cogl_paint_init(). */ -void cogl_fog_set (const ClutterColor *fog_color, +void cogl_fog_set (const CoglColor *fog_color, CoglFixed density, CoglFixed z_near, CoglFixed z_far); @@ -630,7 +629,7 @@ void cogl_fog_set (const ClutterColor *fog_color, * Clears the color buffer to @color. The depth buffer and stencil * buffers are also cleared and fogging and lighting are disabled. */ -void cogl_paint_init (const ClutterColor *color); +void cogl_paint_init (const CoglColor *color); /** * SECTION:cogl-texture @@ -1019,12 +1018,12 @@ void cogl_texture_polygon (CoglHandle handle, /** * cogl_color: - * @color: new current @ClutterColor. + * @color: new current @CoglColor. * * Changes the color of cogl's current paint, which is used for filling and stroking * primitives. */ -void cogl_color (const ClutterColor *color); +void cogl_color (const CoglColor *color); /** diff --git a/common/Makefile.am b/common/Makefile.am index d439f8e25..5a52418be 100644 --- a/common/Makefile.am +++ b/common/Makefile.am @@ -26,4 +26,5 @@ libclutter_cogl_common_la_SOURCES = \ cogl-bitmap-pixbuf.c \ cogl-clip-stack.h \ cogl-clip-stack.c \ - cogl-fixed.c + cogl-fixed.c \ + cogl-color.c diff --git a/common/cogl-color.c b/common/cogl-color.c new file mode 100644 index 000000000..52f86f641 --- /dev/null +++ b/common/cogl-color.c @@ -0,0 +1,107 @@ +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "cogl-color.h" + +void +cogl_color_set_from_4ub (CoglColor *dest, + guint8 red, + guint8 green, + guint8 blue, + guint8 alpha) +{ + g_return_if_fail (dest != NULL); + + dest->red = COGL_FIXED_FROM_FLOAT ((float) red / 0xff * 1.0); + dest->green = COGL_FIXED_FROM_FLOAT ((float) green / 0xff * 1.0); + dest->blue = COGL_FIXED_FROM_FLOAT ((float) blue / 0xff * 1.0); + dest->alpha = COGL_FIXED_FROM_FLOAT ((float) alpha / 0xff * 1.0); +} + +void +cogl_color_set_from_4d (CoglColor *dest, + gdouble red, + gdouble green, + gdouble blue, + gdouble alpha) +{ + g_return_if_fail (dest != NULL); + + dest->red = COGL_FIXED_FROM_FLOAT (CLAMP (red, 0.0, 1.0)); + dest->green = COGL_FIXED_FROM_FLOAT (CLAMP (green, 0.0, 1.0)); + dest->blue = COGL_FIXED_FROM_FLOAT (CLAMP (blue, 0.0, 1.0)); + dest->alpha = COGL_FIXED_FROM_FLOAT (CLAMP (alpha, 0.0, 1.0)); +} + +unsigned char +cogl_color_get_red_byte (const CoglColor *color) +{ + return COGL_FIXED_TO_INT (color->red * 255); +} + +float +cogl_color_get_red_float (const CoglColor *color) +{ + return COGL_FIXED_TO_FLOAT (color->red); +} + +CoglFixed +cogl_color_get_red (const CoglColor *color) +{ + return color->red; +} + +unsigned char +cogl_color_get_green_byte (const CoglColor *color) +{ + return COGL_FIXED_TO_INT (color->green * 255); +} + +float +cogl_color_get_green_float (const CoglColor *color) +{ + return COGL_FIXED_TO_FLOAT (color->green); +} + +CoglFixed +cogl_color_get_green (const CoglColor *color) +{ + return color->green; +} + +unsigned char +cogl_color_get_blue_byte (const CoglColor *color) +{ + return COGL_FIXED_TO_INT (color->blue * 255); +} + +float +cogl_color_get_blue_float (const CoglColor *color) +{ + return COGL_FIXED_TO_FLOAT (color->blue); +} + +CoglFixed +cogl_color_get_blue (const CoglColor *color) +{ + return color->blue; +} + +unsigned char +cogl_color_get_alpha_byte (const CoglColor *color) +{ + return COGL_FIXED_TO_INT (color->alpha * 255); +} + +float +cogl_color_get_alpha_float (const CoglColor *color) +{ + return COGL_FIXED_TO_FLOAT (color->alpha); +} + +CoglFixed +cogl_color_get_alpha (const CoglColor *color) +{ + return color->alpha; +} diff --git a/doc/reference/cogl/cogl-docs.sgml b/doc/reference/cogl/cogl-docs.sgml index 23a341d25..2a906f1de 100644 --- a/doc/reference/cogl/cogl-docs.sgml +++ b/doc/reference/cogl/cogl-docs.sgml @@ -59,6 +59,7 @@ + diff --git a/doc/reference/cogl/cogl-sections.txt b/doc/reference/cogl/cogl-sections.txt index 7c6a14ba3..d045538d1 100644 --- a/doc/reference/cogl/cogl-sections.txt +++ b/doc/reference/cogl/cogl-sections.txt @@ -231,3 +231,29 @@ cogl_double_to_fixed cogl_double_to_int cogl_double_to_unit + +
+cogl-color +Color Type +CoglColor +cogl_color_set_from_4ub +cogl_color_set_from_4d + + +cogl_color_get_red +cogl_color_get_green +cogl_color_get_blue +cogl_color_get_alpha + + +cogl_color_get_red_byte +cogl_color_get_green_byte +cogl_color_get_blue_byte +cogl_color_get_alpha_byte + + +cogl_color_get_red_float +cogl_color_get_green_float +cogl_color_get_blue_float +cogl_color_get_alpha_float +
diff --git a/gl/Makefile.am b/gl/Makefile.am index 083f78ae4..2b2273078 100644 --- a/gl/Makefile.am +++ b/gl/Makefile.am @@ -2,6 +2,7 @@ libclutterincludedir = $(includedir)/clutter-@CLUTTER_API_VERSION@/cogl libclutterinclude_HEADERS = \ $(top_builddir)/clutter/cogl/cogl.h \ $(top_builddir)/clutter/cogl/cogl-defines-gl.h \ + $(top_builddir)/clutter/cogl/cogl-color.h \ $(top_builddir)/clutter/cogl/cogl-fixed.h INCLUDES = \ @@ -23,6 +24,7 @@ noinst_LTLIBRARIES = libclutter-cogl.la libclutter_cogl_la_SOURCES = \ $(top_builddir)/clutter/cogl/cogl.h \ $(top_builddir)/clutter/cogl/cogl-defines-gl.h \ + $(top_builddir)/clutter/cogl/cogl-color.h \ $(top_builddir)/clutter/cogl/cogl-fixed.h \ cogl-internal.h \ cogl-texture.h \ diff --git a/gl/cogl.c b/gl/cogl.c index b78035bbb..2b9446b9d 100644 --- a/gl/cogl.c +++ b/gl/cogl.c @@ -169,12 +169,12 @@ cogl_check_extension (const gchar *name, const gchar *ext) } void -cogl_paint_init (const ClutterColor *color) +cogl_paint_init (const CoglColor *color) { - GE( glClearColor (((float) color->red / 0xff * 1.0), - ((float) color->green / 0xff * 1.0), - ((float) color->blue / 0xff * 1.0), - 0.0) ); + GE( glClearColor (cogl_color_get_red_float (color), + cogl_color_get_green_float (color), + cogl_color_get_blue_float (color), + 0.0) ); glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); glDisable (GL_LIGHTING); @@ -380,17 +380,17 @@ cogl_enable_backface_culling (gboolean setting) } void -cogl_color (const ClutterColor *color) +cogl_color (const CoglColor *color) { _COGL_GET_CONTEXT (ctx, NO_RETVAL); - glColor4ub (color->red, - color->green, - color->blue, - color->alpha); + glColor4f (cogl_color_get_red_float (color), + cogl_color_get_green_float (color), + cogl_color_get_blue_float (color), + cogl_color_get_alpha_float (color)); /* Store alpha for proper blending enables */ - ctx->color_alpha = color->alpha; + ctx->color_alpha = cogl_color_get_alpha_byte (color); } static void @@ -1242,17 +1242,17 @@ cogl_get_bitmasks (gint *red, gint *green, gint *blue, gint *alpha) } void -cogl_fog_set (const ClutterColor *fog_color, +cogl_fog_set (const CoglColor *fog_color, CoglFixed density, CoglFixed start, CoglFixed stop) { GLfloat fogColor[4]; - fogColor[0] = ((float) fog_color->red / 0xff * 1.0); - fogColor[1] = ((float) fog_color->green / 0xff * 1.0); - fogColor[2] = ((float) fog_color->blue / 0xff * 1.0); - fogColor[3] = ((float) fog_color->alpha / 0xff * 1.0); + fogColor[0] = cogl_color_get_red_float (fog_color); + fogColor[1] = cogl_color_get_green_float (fog_color); + fogColor[2] = cogl_color_get_blue_float (fog_color); + fogColor[3] = cogl_color_get_alpha_float (fog_color); glEnable (GL_FOG); diff --git a/gles/Makefile.am b/gles/Makefile.am index fe4bb77d8..1ac969ec3 100644 --- a/gles/Makefile.am +++ b/gles/Makefile.am @@ -2,6 +2,7 @@ libclutterincludedir = $(includedir)/clutter-@CLUTTER_API_VERSION@/cogl libclutterinclude_HEADERS = \ $(top_builddir)/clutter/cogl/cogl.h \ $(top_builddir)/clutter/cogl/cogl-defines-gles.h \ + $(top_builddir)/clutter/cogl/cogl-color.h \ $(top_builddir)/clutter/cogl/cogl-fixed.h INCLUDES = \ @@ -23,6 +24,7 @@ noinst_LTLIBRARIES = libclutter-cogl.la libclutter_cogl_la_SOURCES = \ $(top_builddir)/clutter/cogl/cogl.h \ $(top_builddir)/clutter/cogl/cogl-defines-gles.h \ + $(top_builddir)/clutter/cogl/cogl-color.h \ $(top_builddir)/clutter/cogl/cogl-fixed.h \ cogl-internal.h \ cogl-texture.h \ diff --git a/gles/cogl-texture.c b/gles/cogl-texture.c index 1ad09abd9..3beb80889 100644 --- a/gles/cogl-texture.c +++ b/gles/cogl-texture.c @@ -336,10 +336,10 @@ _cogl_texture_upload_to_gl (CoglTexture *tex) } static void -_cogl_texture_draw_and_read (CoglTexture *tex, - CoglBitmap *target_bmp, - ClutterColor *back_color, - GLint *viewport) +_cogl_texture_draw_and_read (CoglTexture *tex, + CoglBitmap *target_bmp, + CoglColor *back_color, + GLint *viewport) { gint bpp; CoglFixed rx1, ry1; @@ -460,15 +460,17 @@ _cogl_texture_download_from_gl (CoglTexture *tex, GLuint target_gl_format, GLuint target_gl_type) { - gint bpp; - GLint viewport[4]; - ClutterColor cwhite = {0xFF, 0xFF, 0xFF, 0xFF}; - CoglBitmap alpha_bmp; - COGLenum old_src_factor; - COGLenum old_dst_factor; - + gint bpp; + GLint viewport[4]; + CoglColor cwhite; + CoglBitmap alpha_bmp; + COGLenum old_src_factor; + COGLenum old_dst_factor; + _COGL_GET_CONTEXT (ctx, FALSE); - + + cogl_color_set_from_4ub (&cwhite, 0xff, 0xff, 0xff, 0xff); + bpp = _cogl_get_format_bpp (COGL_PIXEL_FORMAT_RGBA_8888); /* Viewport needs to have some size and be inside the window for this */ diff --git a/gles/cogl.c b/gles/cogl.c index 531b7479b..490c97447 100644 --- a/gles/cogl.c +++ b/gles/cogl.c @@ -86,16 +86,16 @@ cogl_check_extension (const gchar *name, const gchar *ext) } void -cogl_paint_init (const ClutterColor *color) +cogl_paint_init (const CoglColor *color) { #if COGL_DEBUG fprintf(stderr, "\n ============== Paint Start ================ \n"); #endif - cogl_wrap_glClearColorx ((color->red << 16) / 0xff, - (color->green << 16) / 0xff, - (color->blue << 16) / 0xff, - 0xff); + cogl_wrap_glClearColorx (cogl_color_get_red (color), + cogl_color_get_green (color), + cogl_color_get_blue (color), + 0); glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); cogl_wrap_glDisable (GL_LIGHTING); @@ -291,7 +291,7 @@ cogl_enable_backface_culling (gboolean setting) } void -cogl_color (const ClutterColor *color) +cogl_color (const CoglColor *color) { _COGL_GET_CONTEXT (ctx, NO_RETVAL); @@ -315,14 +315,14 @@ cogl_color (const ClutterColor *color) #else /* conversion can cause issues with picking on some gles implementations */ - GE( cogl_wrap_glColor4x ((color->red << 16) / 0xff, - (color->green << 16) / 0xff, - (color->blue << 16) / 0xff, - (color->alpha << 16) / 0xff)); + GE( cogl_wrap_glColor4x (cogl_color_get_red (color), + cogl_color_get_green (color), + cogl_color_get_blue (color), + cogl_color_get_alpha (color)) ); #endif /* Store alpha for proper blending enables */ - ctx->color_alpha = color->alpha; + ctx->color_alpha = cogl_color_get_alpha_byte (color); } static void @@ -898,17 +898,17 @@ cogl_get_bitmasks (gint *red, gint *green, gint *blue, gint *alpha) } void -cogl_fog_set (const ClutterColor *fog_color, +cogl_fog_set (const CoglColor *fog_color, CoglFixed density, CoglFixed z_near, CoglFixed z_far) { GLfixed fogColor[4]; - fogColor[0] = (fog_color->red << 16) / 0xff; - fogColor[1] = (fog_color->green << 16) / 0xff; - fogColor[2] = (fog_color->blue << 16) / 0xff; - fogColor[3] = (fog_color->alpha << 16) / 0xff; + fogColor[0] = cogl_color_get_red (fog_color); + fogColor[1] = cogl_color_get_green (fog_color); + fogColor[2] = cogl_color_get_blue (fog_color); + fogColor[3] = cogl_color_get_alpha (fog_color); cogl_wrap_glEnable (GL_FOG);