2008-04-25 09:37:36 -04:00
|
|
|
/*
|
2009-04-27 10:48:12 -04:00
|
|
|
* Cogl
|
2008-04-25 09:37:36 -04:00
|
|
|
*
|
2014-02-21 20:28:54 -05:00
|
|
|
* A Low Level GPU Graphics and Utilities API
|
2008-04-25 09:37:36 -04:00
|
|
|
*
|
cogl: improves header and coding style consistency
We've had complaints that our Cogl code/headers are a bit "special" so
this is a first pass at tidying things up by giving them some
consistency. These changes are all consistent with how new code in Cogl
is being written, but the style isn't consistently applied across all
code yet.
There are two parts to this patch; but since each one required a large
amount of effort to maintain tidy indenting it made sense to combine the
changes to reduce the time spent re indenting the same lines.
The first change is to use a consistent style for declaring function
prototypes in headers. Cogl headers now consistently use this style for
prototypes:
return_type
cogl_function_name (CoglType arg0,
CoglType arg1);
Not everyone likes this style, but it seems that most of the currently
active Cogl developers agree on it.
The second change is to constrain the use of redundant glib data types
in Cogl. Uses of gint, guint, gfloat, glong, gulong and gchar have all
been replaced with int, unsigned int, float, long, unsigned long and char
respectively. When talking about pixel data; use of guchar has been
replaced with guint8, otherwise unsigned char can be used.
The glib types that we continue to use for portability are gboolean,
gint{8,16,32,64}, guint{8,16,32,64} and gsize.
The general intention is that Cogl should look palatable to the widest
range of C programmers including those outside the Gnome community so
- especially for the public API - we want to minimize the number of
foreign looking typedefs.
2010-02-09 20:57:32 -05:00
|
|
|
* Copyright (C) 2007,2008,2009,2010 Intel Corporation.
|
2008-04-25 09:37:36 -04:00
|
|
|
*
|
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:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be
|
|
|
|
* included in all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* 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-03-01 07:56:10 -05:00
|
|
|
*
|
|
|
|
*
|
2008-04-25 09:37:36 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2012-08-07 06:45:29 -04:00
|
|
|
#include <string.h>
|
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
#include "cogl-util.h"
|
2012-02-17 15:44:28 -05:00
|
|
|
#include "cogl-private.h"
|
2008-04-25 09:37:36 -04:00
|
|
|
|
2010-09-15 09:39:05 -04:00
|
|
|
/*
|
2008-04-25 09:37:36 -04:00
|
|
|
* cogl_util_next_p2:
|
2010-09-15 09:39:05 -04:00
|
|
|
* @a: Value to get the next power of two
|
2008-04-25 09:37:36 -04:00
|
|
|
*
|
2010-09-15 09:39:05 -04:00
|
|
|
* Calculates the next power of two greater than or equal to @a.
|
2008-04-25 09:37:36 -04:00
|
|
|
*
|
2010-09-15 09:39:05 -04:00
|
|
|
* Return value: @a if @a is already a power of two, otherwise returns
|
|
|
|
* the next nearest power of two.
|
2008-04-25 09:37:36 -04:00
|
|
|
*/
|
|
|
|
int
|
2010-09-15 09:39:05 -04:00
|
|
|
_cogl_util_next_p2 (int a)
|
2008-04-25 09:37:36 -04:00
|
|
|
{
|
2010-01-27 16:26:26 -05:00
|
|
|
int rval = 1;
|
2008-04-25 09:37:36 -04:00
|
|
|
|
2010-01-27 16:26:26 -05:00
|
|
|
while (rval < a)
|
2008-04-25 09:37:36 -04:00
|
|
|
rval <<= 1;
|
|
|
|
|
|
|
|
return rval;
|
|
|
|
}
|
2009-02-19 07:02:42 -05:00
|
|
|
|
2010-11-04 09:57:36 -04:00
|
|
|
unsigned int
|
|
|
|
_cogl_util_one_at_a_time_mix (unsigned int hash)
|
|
|
|
{
|
|
|
|
hash += ( hash << 3 );
|
|
|
|
hash ^= ( hash >> 11 );
|
|
|
|
hash += ( hash << 15 );
|
|
|
|
|
|
|
|
return hash;
|
|
|
|
}
|
2009-03-09 13:06:22 -04:00
|
|
|
|
2010-12-17 11:06:10 -05:00
|
|
|
/* The 'ffs' function is part of C99 so it isn't always available */
|
|
|
|
#ifndef HAVE_FFS
|
|
|
|
|
|
|
|
int
|
|
|
|
_cogl_util_ffs (int num)
|
|
|
|
{
|
|
|
|
int i = 1;
|
|
|
|
|
|
|
|
if (num == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
while ((num & 1) == 0)
|
|
|
|
{
|
|
|
|
num >>= 1;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
#endif /* HAVE_FFS */
|
2011-10-28 15:09:53 -04:00
|
|
|
|
|
|
|
/* The 'ffsl' is non-standard but when building with GCC we'll use its
|
|
|
|
builtin instead */
|
|
|
|
#ifndef COGL_UTIL_HAVE_BUILTIN_FFSL
|
|
|
|
|
|
|
|
int
|
|
|
|
_cogl_util_ffsl_wrapper (long int num)
|
|
|
|
{
|
|
|
|
int i = 1;
|
|
|
|
|
|
|
|
if (num == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
while ((num & 1) == 0)
|
|
|
|
{
|
|
|
|
num >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6, 6, 7,
|
|
|
|
4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* COGL_UTIL_HAVE_BUILTIN_POPCOUNTL */
|
2012-02-17 15:44:28 -05:00
|
|
|
|
|
|
|
/* tests/conform/test-bitmask.c tests some cogl internals and includes this
|
|
|
|
* file directly but since these functions depend on other internal Cogl
|
|
|
|
* symbols we hide them from test-bitmask.c
|
|
|
|
*
|
|
|
|
* XXX: maybe there's a better way for us to handle internal testing
|
|
|
|
* to avoid needing hacks like this.
|
|
|
|
*/
|
|
|
|
#ifndef _COGL_IN_TEST_BITMASK
|
|
|
|
|
|
|
|
/* Given a set of red, green and blue component masks, a depth and
|
|
|
|
* bits per pixel this function tries to determine a corresponding
|
|
|
|
* CoglPixelFormat.
|
|
|
|
*
|
|
|
|
* The depth is measured in bits not including padding for un-used
|
|
|
|
* alpha. The bits per pixel (bpp) does include padding for un-used
|
|
|
|
* alpha.
|
|
|
|
*
|
|
|
|
* This function firstly aims to match formats with RGB ordered
|
|
|
|
* components and only considers alpha coming first, in the most
|
|
|
|
* significant bits. If the function fails to match then it recurses
|
|
|
|
* by either switching the r and b masks around to check for BGR
|
|
|
|
* ordered formats or it recurses with the masks shifted to check for
|
|
|
|
* formats where the alpha component is the least significant bits.
|
|
|
|
*/
|
|
|
|
static CoglPixelFormat
|
|
|
|
_cogl_util_pixel_format_from_masks_real (unsigned long r_mask,
|
|
|
|
unsigned long g_mask,
|
|
|
|
unsigned long b_mask,
|
|
|
|
int depth, int bpp,
|
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
|
|
|
CoglBool check_bgr,
|
|
|
|
CoglBool check_afirst,
|
2012-02-17 15:44:28 -05:00
|
|
|
int recursion_depth)
|
|
|
|
{
|
|
|
|
CoglPixelFormat image_format;
|
|
|
|
|
|
|
|
if (depth == 24 && bpp == 24 &&
|
|
|
|
r_mask == 0xff0000 && g_mask == 0xff00 && b_mask == 0xff)
|
|
|
|
{
|
|
|
|
return COGL_PIXEL_FORMAT_RGB_888;
|
|
|
|
}
|
|
|
|
else if ((depth == 24 || depth == 32) && bpp == 32 &&
|
|
|
|
r_mask == 0xff0000 && g_mask == 0xff00 && b_mask == 0xff)
|
|
|
|
{
|
|
|
|
return COGL_PIXEL_FORMAT_ARGB_8888_PRE;
|
|
|
|
}
|
2011-11-21 18:41:40 -05:00
|
|
|
else if ((depth == 30 || depth == 32) &&
|
|
|
|
r_mask == 0x3ff00000 && g_mask == 0xffc00 && b_mask == 0x3ff)
|
|
|
|
{
|
|
|
|
return COGL_PIXEL_FORMAT_ARGB_2101010_PRE;
|
|
|
|
}
|
2012-02-17 15:44:28 -05:00
|
|
|
else if (depth == 16 && bpp == 16 &&
|
|
|
|
r_mask == 0xf800 && g_mask == 0x7e0 && b_mask == 0x1f)
|
|
|
|
{
|
|
|
|
return COGL_PIXEL_FORMAT_RGB_565;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (recursion_depth == 2)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* Check for BGR ordering if we didn't find a match */
|
|
|
|
if (check_bgr)
|
|
|
|
{
|
|
|
|
image_format =
|
|
|
|
_cogl_util_pixel_format_from_masks_real (b_mask, g_mask, r_mask,
|
|
|
|
depth, bpp,
|
|
|
|
FALSE,
|
|
|
|
TRUE,
|
|
|
|
recursion_depth + 1);
|
|
|
|
if (image_format)
|
|
|
|
return image_format ^ COGL_BGR_BIT;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for alpha in the least significant bits if we still
|
|
|
|
* haven't found a match... */
|
|
|
|
if (check_afirst && depth != bpp)
|
|
|
|
{
|
|
|
|
int shift = bpp - depth;
|
|
|
|
|
|
|
|
image_format =
|
|
|
|
_cogl_util_pixel_format_from_masks_real (r_mask >> shift,
|
|
|
|
g_mask >> shift,
|
|
|
|
b_mask >> shift,
|
|
|
|
depth, bpp,
|
|
|
|
TRUE,
|
|
|
|
FALSE,
|
|
|
|
recursion_depth + 1);
|
|
|
|
if (image_format)
|
|
|
|
return image_format ^ COGL_AFIRST_BIT;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
CoglPixelFormat
|
|
|
|
_cogl_util_pixel_format_from_masks (unsigned long r_mask,
|
|
|
|
unsigned long g_mask,
|
|
|
|
unsigned long b_mask,
|
|
|
|
int depth, int bpp,
|
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
|
|
|
CoglBool byte_order_is_lsb_first)
|
2012-02-17 15:44:28 -05:00
|
|
|
{
|
|
|
|
CoglPixelFormat image_format =
|
|
|
|
_cogl_util_pixel_format_from_masks_real (r_mask, g_mask, b_mask,
|
|
|
|
depth, bpp,
|
|
|
|
TRUE,
|
|
|
|
TRUE,
|
|
|
|
0);
|
|
|
|
|
|
|
|
if (!image_format)
|
|
|
|
{
|
|
|
|
const char *byte_order[] = { "MSB first", "LSB first" };
|
|
|
|
g_warning ("Could not find a matching pixel format for red mask=0x%lx,"
|
|
|
|
"green mask=0x%lx, blue mask=0x%lx at depth=%d, bpp=%d "
|
|
|
|
"and byte order=%s\n", r_mask, g_mask, b_mask, depth, bpp,
|
|
|
|
byte_order[!!byte_order_is_lsb_first]);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If the image is in little-endian then the order in memory is
|
|
|
|
reversed */
|
|
|
|
if (byte_order_is_lsb_first &&
|
|
|
|
_cogl_pixel_format_is_endian_dependant (image_format))
|
|
|
|
{
|
|
|
|
image_format ^= COGL_BGR_BIT;
|
|
|
|
if (image_format & COGL_A_BIT)
|
|
|
|
image_format ^= COGL_AFIRST_BIT;
|
|
|
|
}
|
|
|
|
|
|
|
|
return image_format;
|
|
|
|
}
|
|
|
|
|
2012-08-07 06:45:29 -04:00
|
|
|
#ifndef HAVE_MEMMEM
|
|
|
|
|
|
|
|
char *
|
|
|
|
_cogl_util_memmem (const void *haystack,
|
|
|
|
size_t haystack_len,
|
|
|
|
const void *needle,
|
|
|
|
size_t needle_len)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
if (needle_len > haystack_len)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
for (i = 0; i <= haystack_len - needle_len; i++)
|
|
|
|
if (!memcmp ((const char *) haystack + i, needle, needle_len))
|
|
|
|
return (char *) haystack + i;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* HAVE_MEMMEM */
|
|
|
|
|
2012-02-17 15:44:28 -05:00
|
|
|
#endif /* _COGL_IN_TEST_BITMASK */
|