From 2ae0fcfc5c2edc9ad8c50e39d24c45f6d9240961 Mon Sep 17 00:00:00 2001 From: Robert Mader Date: Thu, 25 Jul 2024 00:30:32 +0200 Subject: [PATCH] mtk: Add mtk_compute_viewport_matrix helper It'll be used to calculate a CoglPipeline layer matrix given viewport data. Part-of: --- mtk/mtk/meson.build | 2 ++ mtk/mtk/mtk-utils.c | 62 +++++++++++++++++++++++++++++++++++++++++++++ mtk/mtk/mtk-utils.h | 31 +++++++++++++++++++++++ mtk/mtk/mtk.h | 1 + 4 files changed, 96 insertions(+) create mode 100644 mtk/mtk/mtk-utils.c create mode 100644 mtk/mtk/mtk-utils.h diff --git a/mtk/mtk/meson.build b/mtk/mtk/meson.build index 6959c3a14..005560c6f 100644 --- a/mtk/mtk/meson.build +++ b/mtk/mtk/meson.build @@ -5,6 +5,7 @@ mtk_headers = [ 'mtk-monitor-transform.h', 'mtk-rectangle.h', 'mtk-region.h', + 'mtk-utils.h', 'mtk.h', ] @@ -12,6 +13,7 @@ mtk_sources = [ 'mtk-monitor-transform.c', 'mtk-rectangle.c', 'mtk-region.c', + 'mtk-utils.c', ] if have_x11_client diff --git a/mtk/mtk/mtk-utils.c b/mtk/mtk/mtk-utils.c new file mode 100644 index 000000000..e641d199e --- /dev/null +++ b/mtk/mtk/mtk-utils.c @@ -0,0 +1,62 @@ +/* + * Copyright (C) 2021-2024 Robert Mader + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, see . + */ + +#include "config.h" + +#include "mtk/mtk-utils.h" + +void +mtk_compute_viewport_matrix (graphene_matrix_t *matrix, + int width, + int height, + float scale, + MtkMonitorTransform transform, + const graphene_rect_t *src_rect) +{ + if (src_rect) + { + float scaled_width; + float scaled_height; + graphene_point3d_t p; + + scaled_width = width / scale; + scaled_height = height / scale; + + graphene_point3d_init (&p, + src_rect->origin.x / src_rect->size.width, + src_rect->origin.y / src_rect->size.height, + 0); + graphene_matrix_translate (matrix, &p); + + if (mtk_monitor_transform_is_rotated (transform)) + { + graphene_matrix_scale (matrix, + src_rect->size.width / scaled_height, + src_rect->size.height / scaled_width, + 1); + } + else + { + graphene_matrix_scale (matrix, + src_rect->size.width / scaled_width, + src_rect->size.height / scaled_height, + 1); + } + } + + mtk_monitor_transform_transform_matrix (transform, matrix); +} diff --git a/mtk/mtk/mtk-utils.h b/mtk/mtk/mtk-utils.h new file mode 100644 index 000000000..b17806904 --- /dev/null +++ b/mtk/mtk/mtk-utils.h @@ -0,0 +1,31 @@ +/* + * Copyright (C) 2021-2024 Robert Mader + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, see . + */ + +#pragma once + +#include + +#include "mtk/mtk-macros.h" +#include "mtk/mtk-monitor-transform.h" + +MTK_EXPORT +void mtk_compute_viewport_matrix (graphene_matrix_t *matrix, + int width, + int height, + float scale, + MtkMonitorTransform transform, + const graphene_rect_t *src_rect); diff --git a/mtk/mtk/mtk.h b/mtk/mtk/mtk.h index 926a7fd00..8ff15b7a0 100644 --- a/mtk/mtk/mtk.h +++ b/mtk/mtk/mtk.h @@ -27,5 +27,6 @@ #include "mtk/mtk-region.h" #include "mtk/mtk-macros.h" #include "mtk/mtk-monitor-transform.h" +#include "mtk/mtk-utils.h" #undef __MTK_H_INSIDE__