From 4d9488033f849132446ffb7840f1309fa38be8fd Mon Sep 17 00:00:00 2001
From: bunnei <bunneidev@gmail.com>
Date: Sat, 19 Mar 2022 22:47:46 -0700
Subject: [PATCH] hle: nvflinger: Merge Rect with Common::Rectangle.

---
 src/common/math_util.h                        | 50 +++++++++++--
 src/core/CMakeLists.txt                       |  1 -
 src/core/hle/service/nvflinger/buffer_item.h  |  4 +-
 .../nvflinger/buffer_queue_producer.cpp       |  8 +-
 .../nvflinger/graphic_buffer_producer.h       |  6 +-
 src/core/hle/service/nvflinger/ui/rect.h      | 75 -------------------
 6 files changed, 54 insertions(+), 90 deletions(-)
 delete mode 100644 src/core/hle/service/nvflinger/ui/rect.h

diff --git a/src/common/math_util.h b/src/common/math_util.h
index 510c4e56d9..54485bf53f 100644
--- a/src/common/math_util.h
+++ b/src/common/math_util.h
@@ -4,6 +4,7 @@
 
 #pragma once
 
+#include <algorithm>
 #include <cstdlib>
 #include <type_traits>
 
@@ -20,10 +21,32 @@ struct Rectangle {
 
     constexpr Rectangle() = default;
 
+    constexpr Rectangle(T width, T height) : right(width), bottom(height) {}
+
     constexpr Rectangle(T left_, T top_, T right_, T bottom_)
         : left(left_), top(top_), right(right_), bottom(bottom_) {}
 
-    [[nodiscard]] T GetWidth() const {
+    [[nodiscard]] constexpr T Left() const {
+        return left;
+    }
+
+    [[nodiscard]] constexpr T Top() const {
+        return top;
+    }
+
+    [[nodiscard]] constexpr T Right() const {
+        return right;
+    }
+
+    [[nodiscard]] constexpr T Bottom() const {
+        return bottom;
+    }
+
+    [[nodiscard]] constexpr bool IsEmpty() const {
+        return (GetWidth() <= 0) || (GetHeight() <= 0);
+    }
+
+    [[nodiscard]] constexpr T GetWidth() const {
         if constexpr (std::is_floating_point_v<T>) {
             return std::abs(right - left);
         } else {
@@ -31,7 +54,7 @@ struct Rectangle {
         }
     }
 
-    [[nodiscard]] T GetHeight() const {
+    [[nodiscard]] constexpr T GetHeight() const {
         if constexpr (std::is_floating_point_v<T>) {
             return std::abs(bottom - top);
         } else {
@@ -39,18 +62,35 @@ struct Rectangle {
         }
     }
 
-    [[nodiscard]] Rectangle<T> TranslateX(const T x) const {
+    [[nodiscard]] constexpr Rectangle<T> TranslateX(const T x) const {
         return Rectangle{left + x, top, right + x, bottom};
     }
 
-    [[nodiscard]] Rectangle<T> TranslateY(const T y) const {
+    [[nodiscard]] constexpr Rectangle<T> TranslateY(const T y) const {
         return Rectangle{left, top + y, right, bottom + y};
     }
 
-    [[nodiscard]] Rectangle<T> Scale(const float s) const {
+    [[nodiscard]] constexpr Rectangle<T> Scale(const float s) const {
         return Rectangle{left, top, static_cast<T>(static_cast<float>(left + GetWidth()) * s),
                          static_cast<T>(static_cast<float>(top + GetHeight()) * s)};
     }
+
+    [[nodiscard]] constexpr bool operator==(const Rectangle<T>& rhs) const {
+        return (left == rhs.left) && (top == rhs.top) && (right == rhs.right) &&
+               (bottom == rhs.bottom);
+    }
+
+    [[nodiscard]] constexpr bool operator!=(const Rectangle<T>& rhs) const {
+        return !operator==(rhs);
+    }
+
+    [[nodiscard]] constexpr bool Intersect(const Rectangle<T>& with, Rectangle<T>* result) const {
+        result->left = std::max(left, with.left);
+        result->top = std::max(top, with.top);
+        result->right = std::min(right, with.right);
+        result->bottom = std::min(bottom, with.bottom);
+        return !result->IsEmpty();
+    }
 };
 
 template <typename T>
diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt
index ff8adfc55d..6536d05440 100644
--- a/src/core/CMakeLists.txt
+++ b/src/core/CMakeLists.txt
@@ -563,7 +563,6 @@ add_library(core STATIC
     hle/service/nvflinger/status.h
     hle/service/nvflinger/ui/fence.h
     hle/service/nvflinger/ui/graphic_buffer.h
-    hle/service/nvflinger/ui/rect.h
     hle/service/nvflinger/window.h
     hle/service/olsc/olsc.cpp
     hle/service/olsc/olsc.h
diff --git a/src/core/hle/service/nvflinger/buffer_item.h b/src/core/hle/service/nvflinger/buffer_item.h
index d607190651..64b82b8517 100644
--- a/src/core/hle/service/nvflinger/buffer_item.h
+++ b/src/core/hle/service/nvflinger/buffer_item.h
@@ -9,8 +9,8 @@
 #include <memory>
 
 #include "common/common_types.h"
+#include "common/math_util.h"
 #include "core/hle/service/nvflinger/ui/fence.h"
-#include "core/hle/service/nvflinger/ui/rect.h"
 #include "core/hle/service/nvflinger/window.h"
 
 namespace Service::android {
@@ -23,7 +23,7 @@ public:
 
     std::shared_ptr<GraphicBuffer> graphic_buffer;
     Fence fence;
-    Rect crop;
+    Common::Rectangle<s32> crop;
     NativeWindowTransform transform{};
     u32 scaling_mode{};
     s64 timestamp{};
diff --git a/src/core/hle/service/nvflinger/buffer_queue_producer.cpp b/src/core/hle/service/nvflinger/buffer_queue_producer.cpp
index 99f7ec1ac8..5ea48431f4 100644
--- a/src/core/hle/service/nvflinger/buffer_queue_producer.cpp
+++ b/src/core/hle/service/nvflinger/buffer_queue_producer.cpp
@@ -441,7 +441,7 @@ Status BufferQueueProducer::QueueBuffer(s32 slot, const QueueBufferInput& input,
                                         QueueBufferOutput* output) {
     s64 timestamp{};
     bool is_auto_timestamp{};
-    Rect crop;
+    Common::Rectangle<s32> crop;
     NativeWindowScalingMode scaling_mode{};
     NativeWindowTransform transform;
     u32 sticky_transform_{};
@@ -509,9 +509,9 @@ Status BufferQueueProducer::QueueBuffer(s32 slot, const QueueBufferInput& input,
                   crop.Bottom(), transform, scaling_mode);
 
         const std::shared_ptr<GraphicBuffer>& graphic_buffer(slots[slot].graphic_buffer);
-        Rect buffer_rect(graphic_buffer->Width(), graphic_buffer->Height());
-        Rect cropped_rect;
-        crop.Intersect(buffer_rect, &cropped_rect);
+        Common::Rectangle<s32> buffer_rect(graphic_buffer->Width(), graphic_buffer->Height());
+        Common::Rectangle<s32> cropped_rect;
+        [[maybe_unused]] const bool unused = crop.Intersect(buffer_rect, &cropped_rect);
 
         if (cropped_rect != crop) {
             LOG_ERROR(Service_NVFlinger, "crop rect is not contained within the buffer in slot {}",
diff --git a/src/core/hle/service/nvflinger/graphic_buffer_producer.h b/src/core/hle/service/nvflinger/graphic_buffer_producer.h
index 58763cf082..98d27871c9 100644
--- a/src/core/hle/service/nvflinger/graphic_buffer_producer.h
+++ b/src/core/hle/service/nvflinger/graphic_buffer_producer.h
@@ -8,8 +8,8 @@
 
 #include "common/common_funcs.h"
 #include "common/common_types.h"
+#include "common/math_util.h"
 #include "core/hle/service/nvflinger/ui/fence.h"
-#include "core/hle/service/nvflinger/ui/rect.h"
 #include "core/hle/service/nvflinger/window.h"
 
 namespace Service::android {
@@ -20,7 +20,7 @@ class Parcel;
 struct QueueBufferInput final {
     explicit QueueBufferInput(Parcel& parcel);
 
-    void Deflate(s64* timestamp_, bool* is_auto_timestamp_, Rect* crop_,
+    void Deflate(s64* timestamp_, bool* is_auto_timestamp_, Common::Rectangle<s32>* crop_,
                  NativeWindowScalingMode* scaling_mode_, NativeWindowTransform* transform_,
                  u32* sticky_transform_, bool* async_, s32* swap_interval_, Fence* fence_) const {
         *timestamp_ = timestamp;
@@ -37,7 +37,7 @@ struct QueueBufferInput final {
 private:
     s64 timestamp{};
     s32 is_auto_timestamp{};
-    Rect crop{};
+    Common::Rectangle<s32> crop{};
     NativeWindowScalingMode scaling_mode{};
     NativeWindowTransform transform{};
     u32 sticky_transform{};
diff --git a/src/core/hle/service/nvflinger/ui/rect.h b/src/core/hle/service/nvflinger/ui/rect.h
deleted file mode 100644
index c7b5f18156..0000000000
--- a/src/core/hle/service/nvflinger/ui/rect.h
+++ /dev/null
@@ -1,75 +0,0 @@
-// SPDX-License-Identifier: GPL-3.0-or-later
-// Copyright 2021 yuzu Emulator Project
-// Copyright 2006 The Android Open Source Project
-// Parts of this implementation were base on:
-// https://cs.android.com/android/platform/superproject/+/android-5.1.1_r38:frameworks/native/include/ui/Rect.h
-
-#pragma once
-
-#include <cstdint>
-#include <utility>
-
-#include "common/common_types.h"
-
-namespace Service::android {
-
-class Rect final {
-public:
-    constexpr Rect() = default;
-
-    constexpr Rect(s32 width_, s32 height_) : right{width_}, bottom{height_} {}
-
-    constexpr s32 Left() const {
-        return left;
-    }
-
-    constexpr s32 Top() const {
-        return top;
-    }
-
-    constexpr s32 Right() const {
-        return right;
-    }
-
-    constexpr s32 Bottom() const {
-        return bottom;
-    }
-
-    constexpr bool IsEmpty() const {
-        return (GetWidth() <= 0) || (GetHeight() <= 0);
-    }
-
-    constexpr s32 GetWidth() const {
-        return right - left;
-    }
-
-    constexpr s32 GetHeight() const {
-        return bottom - top;
-    }
-
-    constexpr bool operator==(const Rect& rhs) const {
-        return (left == rhs.left) && (top == rhs.top) && (right == rhs.right) &&
-               (bottom == rhs.bottom);
-    }
-
-    constexpr bool operator!=(const Rect& rhs) const {
-        return !operator==(rhs);
-    }
-
-    constexpr bool Intersect(const Rect& with, Rect* result) const {
-        result->left = std::max(left, with.left);
-        result->top = std::max(top, with.top);
-        result->right = std::min(right, with.right);
-        result->bottom = std::min(bottom, with.bottom);
-        return !result->IsEmpty();
-    }
-
-private:
-    s32 left{};
-    s32 top{};
-    s32 right{};
-    s32 bottom{};
-};
-static_assert(sizeof(Rect) == 16, "Rect has wrong size");
-
-} // namespace Service::android