[mutter] monitor-transform: Add meta_monitor_transform_transform() helper



commit e6913d1471d67e4b9f28b3e8d56e3998919d236a
Author: Jonas Ã…dahl <jadahl gmail com>
Date:   Tue Feb 18 11:29:24 2020 +0100

    monitor-transform: Add meta_monitor_transform_transform() helper
    
    Intended to replace various manual monitor transform enum math here and
    there. Tests added as well, to test some hand picked transforms.
    
    https://gitlab.gnome.org/GNOME/mutter/merge_requests/1064

 src/backends/meta-monitor-transform.c |  14 +++++
 src/backends/meta-monitor-transform.h |   7 ++-
 src/tests/meson.build                 |   2 +
 src/tests/monitor-transform-tests.c   | 100 ++++++++++++++++++++++++++++++++++
 src/tests/monitor-transform-tests.h   |  23 ++++++++
 src/tests/unit-tests.c                |   2 +
 6 files changed, 147 insertions(+), 1 deletion(-)
---
diff --git a/src/backends/meta-monitor-transform.c b/src/backends/meta-monitor-transform.c
index 5287e85f8..e8672d151 100644
--- a/src/backends/meta-monitor-transform.c
+++ b/src/backends/meta-monitor-transform.c
@@ -39,3 +39,17 @@ meta_monitor_transform_invert (MetaMonitorTransform transform)
   g_assert_not_reached ();
   return 0;
 }
+
+MetaMonitorTransform
+meta_monitor_transform_transform (MetaMonitorTransform transform,
+                                  MetaMonitorTransform other)
+{
+  MetaMonitorTransform new_transform;
+
+  new_transform = (transform + other) % META_MONITOR_TRANSFORM_FLIPPED;
+  if (meta_monitor_transform_is_flipped (transform) !=
+      meta_monitor_transform_is_flipped (other))
+    new_transform += META_MONITOR_TRANSFORM_FLIPPED;
+
+  return new_transform;
+}
diff --git a/src/backends/meta-monitor-transform.h b/src/backends/meta-monitor-transform.h
index d1c4c41a6..86dc9692d 100644
--- a/src/backends/meta-monitor-transform.h
+++ b/src/backends/meta-monitor-transform.h
@@ -22,6 +22,7 @@
 #include <glib-object.h>
 
 #include "backends/meta-backend-types.h"
+#include "core/util-private.h"
 
 enum _MetaMonitorTransform
 {
@@ -48,9 +49,13 @@ meta_monitor_transform_is_rotated (MetaMonitorTransform transform)
 static inline gboolean
 meta_monitor_transform_is_flipped (MetaMonitorTransform transform)
 {
-  return (transform >= META_MONITOR_TRANSFORM_FLIPPED);
+  return (abs(transform) >= META_MONITOR_TRANSFORM_FLIPPED);
 }
 
 MetaMonitorTransform meta_monitor_transform_invert (MetaMonitorTransform transform);
 
+META_EXPORT_TEST
+MetaMonitorTransform meta_monitor_transform_transform (MetaMonitorTransform transform,
+                                                       MetaMonitorTransform other);
+
 #endif /* META_MONITOR_TRANSFORM_H */
diff --git a/src/tests/meson.build b/src/tests/meson.build
index a9a5a4ff4..8ca075f47 100644
--- a/src/tests/meson.build
+++ b/src/tests/meson.build
@@ -88,6 +88,8 @@ unit_tests = executable('mutter-test-unit-tests',
     'monitor-store-unit-tests.h',
     'monitor-test-utils.c',
     'monitor-test-utils.h',
+    'monitor-transform-tests.c',
+    'monitor-transform-tests.h',
     'monitor-unit-tests.c',
     'monitor-unit-tests.h',
     'wayland-unit-tests.c',
diff --git a/src/tests/monitor-transform-tests.c b/src/tests/monitor-transform-tests.c
new file mode 100644
index 000000000..91ce985be
--- /dev/null
+++ b/src/tests/monitor-transform-tests.c
@@ -0,0 +1,100 @@
+/*
+ * Copyright (C) 2020 Red Hat Inc.
+ *
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include "config.h"
+
+#include "tests/monitor-transform-tests.h"
+
+#include "backends/meta-monitor-transform.h"
+
+static void
+test_transform (void)
+{
+  const struct
+  {
+    MetaMonitorTransform transform;
+    MetaMonitorTransform other;
+    MetaMonitorTransform expect;
+  } tests[] = {
+    {
+      .transform = META_MONITOR_TRANSFORM_NORMAL,
+      .other = META_MONITOR_TRANSFORM_90,
+      .expect = META_MONITOR_TRANSFORM_90,
+    },
+    {
+      .transform = META_MONITOR_TRANSFORM_NORMAL,
+      .other = META_MONITOR_TRANSFORM_FLIPPED_90,
+      .expect = META_MONITOR_TRANSFORM_FLIPPED_90,
+    },
+    {
+      .transform = META_MONITOR_TRANSFORM_90,
+      .other = META_MONITOR_TRANSFORM_90,
+      .expect = META_MONITOR_TRANSFORM_180,
+    },
+    {
+      .transform = META_MONITOR_TRANSFORM_FLIPPED_90,
+      .other = META_MONITOR_TRANSFORM_90,
+      .expect = META_MONITOR_TRANSFORM_FLIPPED_180,
+    },
+    {
+      .transform = META_MONITOR_TRANSFORM_FLIPPED_90,
+      .other = META_MONITOR_TRANSFORM_180,
+      .expect = META_MONITOR_TRANSFORM_FLIPPED_270,
+    },
+    {
+      .transform = META_MONITOR_TRANSFORM_FLIPPED_180,
+      .other = META_MONITOR_TRANSFORM_FLIPPED_180,
+      .expect = META_MONITOR_TRANSFORM_NORMAL,
+    },
+    {
+      .transform = META_MONITOR_TRANSFORM_NORMAL,
+      .other = -META_MONITOR_TRANSFORM_90,
+      .expect = META_MONITOR_TRANSFORM_270,
+    },
+    {
+      .transform = META_MONITOR_TRANSFORM_FLIPPED,
+      .other = -META_MONITOR_TRANSFORM_90,
+      .expect = META_MONITOR_TRANSFORM_FLIPPED_270,
+    },
+    {
+      .transform = META_MONITOR_TRANSFORM_FLIPPED_180,
+      .other = -META_MONITOR_TRANSFORM_270,
+      .expect = META_MONITOR_TRANSFORM_FLIPPED_270,
+    },
+    {
+      .transform = META_MONITOR_TRANSFORM_FLIPPED_180,
+      .other = -META_MONITOR_TRANSFORM_FLIPPED_180,
+      .expect = META_MONITOR_TRANSFORM_NORMAL,
+    },
+  };
+  int i;
+
+  for (i = 0; i < G_N_ELEMENTS (tests); i++)
+    {
+      MetaMonitorTransform result;
+
+      result = meta_monitor_transform_transform (tests[i].transform,
+                                                 tests[i].other);
+      g_assert_cmpint (result, ==, tests[i].expect);
+    }
+}
+
+void
+init_monitor_transform_tests (void)
+{
+  g_test_add_func ("/util/monitor-transform/transform", test_transform);
+}
diff --git a/src/tests/monitor-transform-tests.h b/src/tests/monitor-transform-tests.h
new file mode 100644
index 000000000..f6d019132
--- /dev/null
+++ b/src/tests/monitor-transform-tests.h
@@ -0,0 +1,23 @@
+/*
+ * Copyright (C) 2018 Red Hat, Inc.
+ *
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef MONITOR_TRANSFORM_UNIT_TESTS_H
+#define MONITOR_TRANSFORM_UNIT_TESTS_H
+
+void init_monitor_transform_tests (void);
+
+#endif /* MONITOR_TRANSFORM_TESTS_H */
diff --git a/src/tests/unit-tests.c b/src/tests/unit-tests.c
index 53033bdca..464fd769b 100644
--- a/src/tests/unit-tests.c
+++ b/src/tests/unit-tests.c
@@ -33,6 +33,7 @@
 #include "tests/monitor-config-migration-unit-tests.h"
 #include "tests/monitor-unit-tests.h"
 #include "tests/monitor-store-unit-tests.h"
+#include "tests/monitor-transform-tests.h"
 #include "tests/test-utils.h"
 #include "tests/wayland-unit-tests.h"
 #include "wayland/meta-wayland.h"
@@ -254,6 +255,7 @@ init_tests (int argc, char **argv)
   init_monitor_tests ();
   init_boxes_tests ();
   init_wayland_tests ();
+  init_monitor_transform_tests ();
 }
 
 int


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]