From 841a6c77d61edddbf9db56eb532c3109daa718ea Mon Sep 17 00:00:00 2001 From: Taekyun Kim Date: Tue, 30 Jun 2015 13:31:16 +0900 Subject: [PATCH] pepper: utils: GLSL-like geometric primitives. Geometric primitives are renamed similar with that of GLSL and some more functions are added. Change-Id: If107b46b60fee528031a4fe55e7b337ab42ec95c --- pepper/src/pepper-internal.h | 6 +-- pepper/src/pepper-utils.h | 123 ++++++++++++++++++++++++++++++++++--------- pepper/src/pepper.h | 4 +- pepper/src/view.c | 18 +++---- 4 files changed, 112 insertions(+), 39 deletions(-) diff --git a/pepper/src/pepper-internal.h b/pepper/src/pepper-internal.h index 86e6c25..b1245de 100644 --- a/pepper/src/pepper-internal.h +++ b/pepper/src/pepper-internal.h @@ -316,9 +316,9 @@ struct pepper_view /* Geometry. */ double x, y; int w, h; - pepper_matrix_t transform; - pepper_matrix_t matrix_to_parent; - pepper_matrix_t matrix_to_global; + pepper_mat4_t transform; + pepper_mat4_t matrix_to_parent; + pepper_mat4_t matrix_to_global; pixman_region32_t bounding_region; pepper_bool_t geometry_dirty; diff --git a/pepper/src/pepper-utils.h b/pepper/src/pepper-utils.h index d2a29c2..63c074b 100644 --- a/pepper/src/pepper-utils.h +++ b/pepper/src/pepper-utils.h @@ -18,6 +18,9 @@ extern "C" { # define PEPPER_API #endif +#define PEPPER_MAX(a, b) ((a) > (b) ? (a) : (b)) +#define PEPPER_MIN(a, b) ((a) < (b) ? (a) : (b)) + #define pepper_container_of(ptr, type, member) ({ \ const __typeof__( ((type *)0)->member ) *__mptr = (ptr); \ (type *)( (char *)__mptr - offsetof(type,member) );}) @@ -99,7 +102,6 @@ get_pixman_format(pepper_format_t format) } typedef struct pepper_list pepper_list_t; -typedef struct pepper_matrix pepper_matrix_t; #define PEPPER_LIST_FOR_EACH(head, pos) \ for (pos = (head)->next; \ @@ -258,12 +260,32 @@ pepper_create_anonymous_file(off_t size); PEPPER_API int pepper_log(const char* domain, int level, const char *format, ...); -struct pepper_matrix +typedef struct pepper_mat4 pepper_mat4_t; +typedef struct pepper_vec4 pepper_vec4_t; +typedef struct pepper_vec3 pepper_vec3_t; +typedef struct pepper_vec2 pepper_vec2_t; + +struct pepper_mat4 { double m[16]; uint32_t flags; }; +struct pepper_vec2 +{ + double x, y; +}; + +struct pepper_vec3 +{ + double x, y, z; +}; + +struct pepper_vec4 +{ + double x, y, z, w; +}; + enum { PEPPER_MATRIX_TRANSLATE = (1 << 0), PEPPER_MATRIX_SCALE = (1 << 1), @@ -271,6 +293,12 @@ enum { PEPPER_MATRIX_COMPLEX = (1 << 3), }; +static inline pepper_bool_t +pepper_mat4_is_translation(const pepper_mat4_t *matrix) +{ + return matrix->flags == PEPPER_MATRIX_TRANSLATE || matrix->flags == 0; +} + static inline double pepper_reciprocal_sqrt(double x) { @@ -285,22 +313,22 @@ pepper_reciprocal_sqrt(double x) } static inline void -pepper_matrix_multiply(pepper_matrix_t *dst, const pepper_matrix_t *ma, const pepper_matrix_t *mb) +pepper_mat4_multiply(pepper_mat4_t *dst, const pepper_mat4_t *ma, const pepper_mat4_t *mb) { - pepper_matrix_t tmp; + pepper_mat4_t tmp; double *d = tmp.m; const double *a = ma->m; const double *b = mb->m; if (!ma->flags) { - memcpy(dst, mb, sizeof(pepper_matrix_t)); + memcpy(dst, mb, sizeof(pepper_mat4_t)); return; } if (!mb->flags) { - memcpy(dst, ma, sizeof(pepper_matrix_t)); + memcpy(dst, ma, sizeof(pepper_mat4_t)); return; } @@ -325,11 +353,11 @@ pepper_matrix_multiply(pepper_matrix_t *dst, const pepper_matrix_t *ma, const pe d[15] = a[ 3] * b[12] + a[ 7] * b[13] + a[11] * b[14] + a[15] * b[15]; tmp.flags = ma->flags | mb->flags; - memcpy(dst, &tmp, sizeof(pepper_matrix_t)); + memcpy(dst, &tmp, sizeof(pepper_mat4_t)); } static inline void -pepper_matrix_init_identity(pepper_matrix_t *matrix) +pepper_mat4_init_identity(pepper_mat4_t *matrix) { matrix->m[ 0] = 1.0f; matrix->m[ 1] = 0.0f; @@ -355,9 +383,9 @@ pepper_matrix_init_identity(pepper_matrix_t *matrix) } static inline void -pepper_matrix_init_translate(pepper_matrix_t *matrix, double x, double y, double z) +pepper_mat4_init_translate(pepper_mat4_t *matrix, double x, double y, double z) { - pepper_matrix_init_identity(matrix); + pepper_mat4_init_identity(matrix); matrix->m[ 3] = x; matrix->m[ 7] = y; @@ -367,7 +395,7 @@ pepper_matrix_init_translate(pepper_matrix_t *matrix, double x, double y, double } static inline void -pepper_matrix_translate(pepper_matrix_t *matrix, double x, double y, double z) +pepper_mat4_translate(pepper_mat4_t *matrix, double x, double y, double z) { matrix->m[ 3] = matrix->m[0] * x + matrix->m[1] * y + matrix->m[ 2] * z; matrix->m[ 7] = matrix->m[4] * x + matrix->m[5] * y + matrix->m[ 6] * z; @@ -377,9 +405,9 @@ pepper_matrix_translate(pepper_matrix_t *matrix, double x, double y, double z) } static inline void -pepper_matrix_init_scale(pepper_matrix_t *matrix, double x, double y, double z) +pepper_mat4_init_scale(pepper_mat4_t *matrix, double x, double y, double z) { - pepper_matrix_init_identity(matrix); + pepper_mat4_init_identity(matrix); matrix->m[ 0] = x; matrix->m[ 5] = y; @@ -389,7 +417,7 @@ pepper_matrix_init_scale(pepper_matrix_t *matrix, double x, double y, double z) } static inline void -pepper_matrix_scale(pepper_matrix_t *matrix, double x, double y, double z) +pepper_mat4_scale(pepper_mat4_t *matrix, double x, double y, double z) { matrix->m[ 0] *= x; matrix->m[ 1] *= y; @@ -407,7 +435,7 @@ pepper_matrix_scale(pepper_matrix_t *matrix, double x, double y, double z) } static inline void -pepper_matrix_init_rotate(pepper_matrix_t *matrix, double x, double y, double z, double angle) +pepper_mat4_init_rotate(pepper_mat4_t *matrix, double x, double y, double z, double angle) { double c; double s; @@ -422,7 +450,7 @@ pepper_matrix_init_rotate(pepper_matrix_t *matrix, double x, double y, double z, if (angle == 0.0f || (z == 0.0f && y == 0.0f && z == 0.0f)) { - pepper_matrix_init_identity(matrix); + pepper_mat4_init_identity(matrix); return; } @@ -433,7 +461,7 @@ pepper_matrix_init_rotate(pepper_matrix_t *matrix, double x, double y, double z, if (x == 0.0f && y == 0.0f) { - pepper_matrix_init_identity(matrix); + pepper_mat4_init_identity(matrix); matrix->m[ 0] = c; matrix->m[ 1] = -s; @@ -442,7 +470,7 @@ pepper_matrix_init_rotate(pepper_matrix_t *matrix, double x, double y, double z, } else if (y == 0.0f && z == 0.0f) { - pepper_matrix_init_identity(matrix); + pepper_mat4_init_identity(matrix); matrix->m[ 5] = c; matrix->m[ 6] = -s; @@ -451,7 +479,7 @@ pepper_matrix_init_rotate(pepper_matrix_t *matrix, double x, double y, double z, } else if (x == 0.0f && z == 0.0f) { - pepper_matrix_init_identity(matrix); + pepper_mat4_init_identity(matrix); matrix->m[ 2] = c; matrix->m[ 0] = -s; @@ -497,18 +525,63 @@ pepper_matrix_init_rotate(pepper_matrix_t *matrix, double x, double y, double z, } static inline void -pepper_matrix_rotate(pepper_matrix_t *matrix, double x, double y, double z, double angle) +pepper_mat4_rotate(pepper_mat4_t *matrix, double x, double y, double z, double angle) { - pepper_matrix_t rotate; + pepper_mat4_t rotate; + + pepper_mat4_init_rotate(&rotate, x, y, z, angle); + pepper_mat4_multiply(matrix, matrix, &rotate); +} + +static inline void +pepper_mat4_copy(pepper_mat4_t *dst, const pepper_mat4_t *src) +{ + memcpy(dst, src, sizeof(pepper_mat4_t)); +} + +static inline void +pepper_mat4_transform_vec2(const pepper_mat4_t *matrix, pepper_vec2_t *v) +{ + double x, y; + const double *m = &matrix->m[0]; + + x = m[0] * v->x + m[1] * v->y + m[3]; + y = m[4] * v->x + m[5] * v->y + m[7]; + + v->x = x; + v->y = y; +} + +static inline void +pepper_mat4_transform_vec3(const pepper_mat4_t *matrix, pepper_vec3_t *v) +{ + double x, y, z; + const double *m = &matrix->m[0]; + + x = m[0] * v->x + m[1] * v->y + m[ 2] * v->z + m[ 3]; + y = m[4] * v->x + m[5] * v->y + m[ 6] * v->z + m[ 7]; + y = m[8] * v->x + m[9] * v->y + m[10] * v->z + m[11]; - pepper_matrix_init_rotate(&rotate, x, y, z, angle); - pepper_matrix_multiply(matrix, matrix, &rotate); + v->x = x; + v->y = y; + v->z = z; } static inline void -pepper_matrix_copy(pepper_matrix_t *dst, const pepper_matrix_t *src) +pepper_mat4_transform_vec4(const pepper_mat4_t *matrix, pepper_vec4_t *v) { - memcpy(dst, src, sizeof(pepper_matrix_t)); + double x, y, z, w; + const double *m = &matrix->m[0]; + + x = m[ 0] * v->x + m[ 1] * v->y + m[ 2] * v->z + m[ 3] * v->w; + y = m[ 4] * v->x + m[ 5] * v->y + m[ 6] * v->z + m[ 7] * v->w; + y = m[ 8] * v->x + m[ 9] * v->y + m[10] * v->z + m[11] * v->w; + y = m[12] * v->x + m[13] * v->y + m[14] * v->z + m[15] * v->w; + + v->x = x; + v->y = y; + v->z = z; + v->w = w; } /* Virtual terminal */ diff --git a/pepper/src/pepper.h b/pepper/src/pepper.h index b271bca..e3c37c2 100644 --- a/pepper/src/pepper.h +++ b/pepper/src/pepper.h @@ -279,9 +279,9 @@ PEPPER_API void pepper_view_get_position(pepper_object_t *view, double *x, double *y); PEPPER_API void -pepper_view_set_transform(pepper_object_t *view, const pepper_matrix_t *matrix); +pepper_view_set_transform(pepper_object_t *view, const pepper_mat4_t *matrix); -PEPPER_API const pepper_matrix_t * +PEPPER_API const pepper_mat4_t * pepper_view_get_transform(pepper_object_t *view); PEPPER_API void diff --git a/pepper/src/view.c b/pepper/src/view.c index 2d37774..0f082ad 100644 --- a/pepper/src/view.c +++ b/pepper/src/view.c @@ -92,9 +92,9 @@ pepper_compositor_add_surface_view(pepper_object_t *comp, pepper_object_t *sfc) view->w = 0; view->h = 0; - pepper_matrix_init_identity(&view->transform); - pepper_matrix_init_identity(&view->matrix_to_parent); - pepper_matrix_init_identity(&view->matrix_to_global); + pepper_mat4_init_identity(&view->transform); + pepper_mat4_init_identity(&view->matrix_to_parent); + pepper_mat4_init_identity(&view->matrix_to_global); view->parent_link.item = (void *)view; view->z_link.item = (void *)view; @@ -355,16 +355,16 @@ pepper_view_get_position(pepper_object_t *v, double *x, double *y) } PEPPER_API void -pepper_view_set_transform(pepper_object_t *v, const pepper_matrix_t *matrix) +pepper_view_set_transform(pepper_object_t *v, const pepper_mat4_t *matrix) { pepper_view_t *view = (pepper_view_t *)v; CHECK_MAGIC_AND_NON_NULL(v, PEPPER_VIEW); - pepper_matrix_copy(&view->transform, matrix); + pepper_mat4_copy(&view->transform, matrix); view_geometry_dirty(view); } -PEPPER_API const pepper_matrix_t * +PEPPER_API const pepper_mat4_t * pepper_view_get_transform(pepper_object_t *v) { pepper_view_t *view = (pepper_view_t *)v; @@ -425,7 +425,7 @@ view_update_opaque_region(pepper_view_t *view) } static void -damage_region_transform(pixman_region32_t *region, const pepper_matrix_t *matrix) +damage_region_transform(pixman_region32_t *region, const pepper_mat4_t *matrix) { /* TODO: */ } @@ -460,12 +460,12 @@ view_update_geometry(pepper_view_t *view) if (view->parent) { - pepper_matrix_multiply(&view->matrix_to_global, + pepper_mat4_multiply(&view->matrix_to_global, &view->parent->matrix_to_global, &view->matrix_to_parent); } else { - pepper_matrix_copy(&view->matrix_to_global, &view->matrix_to_parent); + pepper_mat4_copy(&view->matrix_to_global, &view->matrix_to_parent); } view_update_bounding_region(view); -- 2.7.4