From 7eadc948aef835baf3100b5a7d48fe14f9977168 Mon Sep 17 00:00:00 2001 From: Bilal Elmoussaoui Date: Fri, 26 Jul 2024 22:35:39 +0200 Subject: [PATCH] cogl: Inline various utilities Part-of: --- cogl/cogl/cogl-clip-stack.c | 53 ++++++- cogl/cogl/cogl-texture.c | 6 + cogl/cogl/cogl-util.c | 167 --------------------- cogl/cogl/cogl-util.h | 46 +----- cogl/cogl/cogl.c | 41 ----- cogl/cogl/meson.build | 1 - cogl/cogl/winsys/cogl-texture-pixmap-x11.c | 129 ++++++++++++++++ 7 files changed, 188 insertions(+), 255 deletions(-) delete mode 100644 cogl/cogl/cogl-util.c diff --git a/cogl/cogl/cogl-clip-stack.c b/cogl/cogl/cogl-clip-stack.c index 96f7514de..7be177ee9 100644 --- a/cogl/cogl/cogl-clip-stack.c +++ b/cogl/cogl/cogl-clip-stack.c @@ -38,6 +38,7 @@ #include "cogl/cogl-clip-stack.h" #include "cogl/cogl-context-private.h" #include "cogl/cogl-framebuffer-private.h" +#include "cogl/cogl-graphene.h" #include "cogl/cogl-journal-private.h" #include "cogl/cogl-util.h" #include "cogl/cogl-primitives-private.h" @@ -97,6 +98,46 @@ _cogl_clip_stack_entry_set_bounds (CoglClipStack *entry, entry->bounds_y1 = (int) ceilf (max_y); } +/* Scale from OpenGL normalized device coordinates (ranging from -1 to 1) + * to Cogl window/framebuffer coordinates (ranging from 0 to buffer-size) with + * (0,0) being top left. */ +#define VIEWPORT_TRANSFORM_X(x, vp_origin_x, vp_width) \ + ( ( ((x) + 1.0f) * ((vp_width) / 2.0f) ) + (vp_origin_x) ) +/* Note: for Y we first flip all coordinates around the X axis while in + * normalized device coordinates */ +#define VIEWPORT_TRANSFORM_Y(y, vp_origin_y, vp_height) \ + ( ( ((-(y)) + 1.0f) * ((vp_height) / 2.0f) ) + (vp_origin_y) ) + +/* Transform a homogeneous vertex position from model space to Cogl + * window coordinates (with 0,0 being top left) */ +void +_cogl_transform_point (const graphene_matrix_t *matrix_mv, + const graphene_matrix_t *matrix_p, + const float *viewport, + float *x, + float *y) +{ + float z = 0; + float w = 1; + + /* Apply the modelview matrix transform */ + cogl_graphene_matrix_project_point (matrix_mv, x, y, &z, &w); + + /* Apply the projection matrix transform */ + cogl_graphene_matrix_project_point (matrix_p, x, y, &z, &w); + + /* Perform perspective division */ + *x /= w; + *y /= w; + + /* Apply viewport transform */ + *x = VIEWPORT_TRANSFORM_X (*x, viewport[0], viewport[2]); + *y = VIEWPORT_TRANSFORM_Y (*y, viewport[1], viewport[3]); +} + +#undef VIEWPORT_TRANSFORM_X +#undef VIEWPORT_TRANSFORM_Y + CoglClipStack * _cogl_clip_stack_push_rectangle (CoglClipStack *stack, float x_1, @@ -304,14 +345,10 @@ _cogl_clip_stack_get_bounds (CoglClipStack *stack, { /* Get the intersection of the current scissor and the bounding box of this clip */ - _cogl_util_scissor_intersect (entry->bounds_x0, - entry->bounds_y0, - entry->bounds_x1, - entry->bounds_y1, - scissor_x0, - scissor_y0, - scissor_x1, - scissor_y1); + *scissor_x0 = MAX (*scissor_x0, entry->bounds_x0); + *scissor_y0 = MAX (*scissor_y0, entry->bounds_y0); + *scissor_x1 = MIN (*scissor_x1, entry->bounds_x1); + *scissor_y1 = MIN (*scissor_y1, entry->bounds_y1); } } diff --git a/cogl/cogl/cogl-texture.c b/cogl/cogl/cogl-texture.c index b03f90cd6..773789dd0 100644 --- a/cogl/cogl/cogl-texture.c +++ b/cogl/cogl/cogl-texture.c @@ -264,6 +264,12 @@ cogl_texture_get_format (CoglTexture *texture) return COGL_TEXTURE_GET_CLASS (texture)->get_format (texture); } +static inline unsigned int +_cogl_util_fls (unsigned int n) +{ + return n == 0 ? 0 : sizeof (unsigned int) * 8 - __builtin_clz (n); +} + int _cogl_texture_get_n_levels (CoglTexture *texture) { diff --git a/cogl/cogl/cogl-util.c b/cogl/cogl/cogl-util.c deleted file mode 100644 index 4dffa822d..000000000 --- a/cogl/cogl/cogl-util.c +++ /dev/null @@ -1,167 +0,0 @@ -/* - * Cogl - * - * A Low Level GPU Graphics and Utilities API - * - * Copyright (C) 2007,2008,2009,2010 Intel Corporation. - * - * 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. - * - * - */ - -#include "config.h" - -#include - -#include "cogl/cogl-util.h" -#include "cogl/cogl-private.h" - -unsigned int -_cogl_util_one_at_a_time_mix (unsigned int hash) -{ - hash += ( hash << 3 ); - hash ^= ( hash >> 11 ); - hash += ( hash << 15 ); - - return hash; -} - -/* 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, - gboolean check_bgr, - gboolean check_afirst, - 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; - } - else if ((depth == 30 || depth == 32) && - r_mask == 0x3ff00000 && g_mask == 0xffc00 && b_mask == 0x3ff) - { - return COGL_PIXEL_FORMAT_ARGB_2101010_PRE; - } - 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, - gboolean byte_order_is_lsb_first) -{ - 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; -} diff --git a/cogl/cogl/cogl-util.h b/cogl/cogl/cogl-util.h index a32ce311a..04cd23ae4 100644 --- a/cogl/cogl/cogl-util.h +++ b/cogl/cogl/cogl-util.h @@ -71,46 +71,16 @@ _cogl_util_one_at_a_time_hash (unsigned int hash, return hash; } -unsigned int -_cogl_util_one_at_a_time_mix (unsigned int hash); +static inline unsigned int +_cogl_util_one_at_a_time_mix (unsigned int hash) +{ + hash += ( hash << 3 ); + hash ^= ( hash >> 11 ); + hash += ( hash << 15 ); + return hash; +} #define _cogl_util_ffsl __builtin_ffsl -static inline unsigned int -_cogl_util_fls (unsigned int n) -{ - return n == 0 ? 0 : sizeof (unsigned int) * 8 - __builtin_clz (n); -} - #define _cogl_util_popcountl __builtin_popcountl - -/* Match a CoglPixelFormat according to channel masks, color depth, - * bits per pixel and byte order. These information are provided by - * the Visual and XImage structures. - * - * If no specific pixel format could be found, COGL_PIXEL_FORMAT_ANY - * is returned. - */ -CoglPixelFormat -_cogl_util_pixel_format_from_masks (unsigned long r_mask, - unsigned long g_mask, - unsigned long b_mask, - int depth, int bpp, - int byte_order); - -static inline void -_cogl_util_scissor_intersect (int rect_x0, - int rect_y0, - int rect_x1, - int rect_y1, - int *scissor_x0, - int *scissor_y0, - int *scissor_x1, - int *scissor_y1) -{ - *scissor_x0 = MAX (*scissor_x0, rect_x0); - *scissor_y0 = MAX (*scissor_y0, rect_y0); - *scissor_x1 = MIN (*scissor_x1, rect_x1); - *scissor_y1 = MIN (*scissor_y1, rect_y1); -} diff --git a/cogl/cogl/cogl.c b/cogl/cogl/cogl.c index ffe0251a2..b6cfe0ec5 100644 --- a/cogl/cogl/cogl.c +++ b/cogl/cogl/cogl.c @@ -36,7 +36,6 @@ #include "cogl/cogl-cpu-caps.h" #include "cogl/cogl-debug.h" -#include "cogl/cogl-graphene.h" #include "cogl/cogl-util.h" #include "cogl/cogl-context-private.h" #include "cogl/cogl-pipeline-private.h" @@ -71,46 +70,6 @@ _cogl_driver_error_quark (void) return g_quark_from_static_string ("cogl-driver-error-quark"); } -/* Scale from OpenGL normalized device coordinates (ranging from -1 to 1) - * to Cogl window/framebuffer coordinates (ranging from 0 to buffer-size) with - * (0,0) being top left. */ -#define VIEWPORT_TRANSFORM_X(x, vp_origin_x, vp_width) \ - ( ( ((x) + 1.0f) * ((vp_width) / 2.0f) ) + (vp_origin_x) ) -/* Note: for Y we first flip all coordinates around the X axis while in - * normalized device coordinates */ -#define VIEWPORT_TRANSFORM_Y(y, vp_origin_y, vp_height) \ - ( ( ((-(y)) + 1.0f) * ((vp_height) / 2.0f) ) + (vp_origin_y) ) - -/* Transform a homogeneous vertex position from model space to Cogl - * window coordinates (with 0,0 being top left) */ -void -_cogl_transform_point (const graphene_matrix_t *matrix_mv, - const graphene_matrix_t *matrix_p, - const float *viewport, - float *x, - float *y) -{ - float z = 0; - float w = 1; - - /* Apply the modelview matrix transform */ - cogl_graphene_matrix_project_point (matrix_mv, x, y, &z, &w); - - /* Apply the projection matrix transform */ - cogl_graphene_matrix_project_point (matrix_p, x, y, &z, &w); - - /* Perform perspective division */ - *x /= w; - *y /= w; - - /* Apply viewport transform */ - *x = VIEWPORT_TRANSFORM_X (*x, viewport[0], viewport[2]); - *y = VIEWPORT_TRANSFORM_Y (*y, viewport[1], viewport[3]); -} - -#undef VIEWPORT_TRANSFORM_X -#undef VIEWPORT_TRANSFORM_Y - uint32_t _cogl_system_error_quark (void) { diff --git a/cogl/cogl/meson.build b/cogl/cogl/meson.build index 3d80d4f18..e52a7fb8b 100644 --- a/cogl/cogl/meson.build +++ b/cogl/cogl/meson.build @@ -273,7 +273,6 @@ cogl_sources = [ 'cogl-texture-private.h', 'cogl-texture.c', 'cogl-trace.c', - 'cogl-util.c', 'cogl-util.h', 'cogl.c', 'deprecated/cogl-program-private.h', diff --git a/cogl/cogl/winsys/cogl-texture-pixmap-x11.c b/cogl/cogl/winsys/cogl-texture-pixmap-x11.c index 9be2778e4..75c809bf0 100644 --- a/cogl/cogl/winsys/cogl-texture-pixmap-x11.c +++ b/cogl/cogl/winsys/cogl-texture-pixmap-x11.c @@ -413,6 +413,135 @@ try_alloc_shm (CoglTexturePixmapX11 *tex_pixmap) tex_pixmap->shm_info.shmid = -1; } +/* 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, + gboolean check_bgr, + gboolean check_afirst, + 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; + } + else if ((depth == 30 || depth == 32) && + r_mask == 0x3ff00000 && g_mask == 0xffc00 && b_mask == 0x3ff) + { + return COGL_PIXEL_FORMAT_ARGB_2101010_PRE; + } + 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; +} + + +/* Match a CoglPixelFormat according to channel masks, color depth, + * bits per pixel and byte order. These information are provided by + * the Visual and XImage structures. + * + * If no specific pixel format could be found, COGL_PIXEL_FORMAT_ANY + * is returned. + */ +static CoglPixelFormat +_cogl_util_pixel_format_from_masks (unsigned long r_mask, + unsigned long g_mask, + unsigned long b_mask, + int depth, int bpp, + gboolean byte_order_is_lsb_first) +{ + 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; +} + static void _cogl_texture_pixmap_x11_update_image_texture (CoglTexturePixmapX11 *tex_pixmap) {