From dc701b72fc579776cf10bf7d5ec91c033f9bec8e Mon Sep 17 00:00:00 2001
From: Tony Crisci <tony@dubstepdish.com>
Date: Tue, 16 Jan 2018 07:12:46 -0500
Subject: [PATCH] abstract box matrix

---
 include/wlr/types/wlr_output.h |  7 ++++++
 rootston/output.c              | 42 ++-------------------------------
 types/wlr_output.c             | 43 ++++++++++++++++++++++++++++++++++
 3 files changed, 52 insertions(+), 40 deletions(-)

diff --git a/include/wlr/types/wlr_output.h b/include/wlr/types/wlr_output.h
index 71463cb5..4c59d98b 100644
--- a/include/wlr/types/wlr_output.h
+++ b/include/wlr/types/wlr_output.h
@@ -136,4 +136,11 @@ enum wl_output_transform wlr_output_transform_invert(
 enum wl_output_transform wlr_output_transform_compose(
 	enum wl_output_transform tr_a, enum wl_output_transform tr_b);
 
+/**
+ * Get a matrix suitable for rendering a box on the output.
+ */
+void wlr_output_get_box_matrix(struct wlr_output *output, int ox, int oy,
+		int width, int height, enum wl_output_transform transform,
+		float rotation, float (*mat)[16]);
+
 #endif
diff --git a/rootston/output.c b/rootston/output.c
index c26ea36e..42dbd972 100644
--- a/rootston/output.c
+++ b/rootston/output.c
@@ -53,46 +53,8 @@ static void render_surface(struct wlr_surface *surface,
 	};
 	if (wlr_output_layout_intersects(desktop->layout, wlr_output, &render_box)) {
 		float matrix[16];
-
-		float translate_center[16];
-		wlr_matrix_translate(&translate_center,
-			(int)ox + render_width / 2, (int)oy + render_height / 2, 0);
-
-		float rotate[16];
-		wlr_matrix_rotate(&rotate, rotation);
-
-		float translate_origin[16];
-		wlr_matrix_translate(&translate_origin, -render_width / 2,
-			-render_height / 2, 0);
-
-		float scale[16];
-		wlr_matrix_scale(&scale, render_width, render_height, 1);
-
-		float transform[16];
-		wlr_matrix_mul(&translate_center, &rotate, &transform);
-		wlr_matrix_mul(&transform, &translate_origin, &transform);
-		wlr_matrix_mul(&transform, &scale, &transform);
-
-		if (surface->current->transform != WL_OUTPUT_TRANSFORM_NORMAL) {
-			float surface_translate_center[16];
-			wlr_matrix_translate(&surface_translate_center, 0.5, 0.5, 0);
-
-			float surface_transform[16];
-			wlr_matrix_transform(surface_transform,
-				wlr_output_transform_invert(surface->current->transform));
-
-			float surface_translate_origin[16];
-			wlr_matrix_translate(&surface_translate_origin, -0.5, -0.5, 0);
-
-			wlr_matrix_mul(&transform, &surface_translate_center,
-				&transform);
-			wlr_matrix_mul(&transform, &surface_transform, &transform);
-			wlr_matrix_mul(&transform, &surface_translate_origin,
-				&transform);
-		}
-
-		wlr_matrix_mul(&wlr_output->transform_matrix, &transform, &matrix);
-
+		wlr_output_get_box_matrix(wlr_output, ox, oy, render_width,
+			render_height, surface->current->transform, rotation, &matrix);
 		wlr_render_with_matrix(desktop->server->renderer, surface->texture,
 			&matrix);
 
diff --git a/types/wlr_output.c b/types/wlr_output.c
index b47fb3a0..178306a8 100644
--- a/types/wlr_output.c
+++ b/types/wlr_output.c
@@ -751,3 +751,46 @@ enum wl_output_transform wlr_output_transform_compose(
 	}
 	return flipped | rotated;
 }
+
+void wlr_output_get_box_matrix(struct wlr_output *output, int ox, int oy,
+		int width, int height, enum wl_output_transform transform,
+		float rotation, float (*mat)[16]) {
+		float translate_center[16];
+		wlr_matrix_translate(&translate_center,
+			(int)ox + width / 2, (int)oy + height / 2, 0);
+
+		float rotate[16];
+		wlr_matrix_rotate(&rotate, rotation);
+
+		float translate_origin[16];
+		wlr_matrix_translate(&translate_origin, -width / 2,
+			-height / 2, 0);
+
+		float scale[16];
+		wlr_matrix_scale(&scale, width, height, 1);
+
+		float transform_matrix[16];
+		wlr_matrix_mul(&translate_center, &rotate, &transform_matrix);
+		wlr_matrix_mul(&transform_matrix, &translate_origin, &transform_matrix);
+		wlr_matrix_mul(&transform_matrix, &scale, &transform_matrix);
+
+		if (transform != WL_OUTPUT_TRANSFORM_NORMAL) {
+			float surface_translate_center[16];
+			wlr_matrix_translate(&surface_translate_center, 0.5, 0.5, 0);
+
+			float surface_transform[16];
+			wlr_matrix_transform(surface_transform,
+				wlr_output_transform_invert(transform));
+
+			float surface_translate_origin[16];
+			wlr_matrix_translate(&surface_translate_origin, -0.5, -0.5, 0);
+
+			wlr_matrix_mul(&transform_matrix, &surface_translate_center,
+				&transform_matrix);
+			wlr_matrix_mul(&transform_matrix, &surface_transform, &transform_matrix);
+			wlr_matrix_mul(&transform_matrix, &surface_translate_origin,
+				&transform_matrix);
+		}
+
+		wlr_matrix_mul(&output->transform_matrix, &transform_matrix, mat);
+}