From 89b87158dbf100e4177f4a010190ba5f80252eb2 Mon Sep 17 00:00:00 2001 From: Vaxry <43317083+vaxerski@users.noreply.github.com> Date: Wed, 19 Jul 2023 20:09:49 +0200 Subject: internal: Wrap regions (#2750) --- src/helpers/Monitor.cpp | 6 ++- src/helpers/Monitor.hpp | 4 +- src/helpers/Region.cpp | 93 +++++++++++++++++++++++++++++++++++++++++++++++ src/helpers/Region.hpp | 53 +++++++++++++++++++++++++++ src/helpers/WLClasses.hpp | 3 +- 5 files changed, 155 insertions(+), 4 deletions(-) create mode 100644 src/helpers/Region.cpp create mode 100644 src/helpers/Region.hpp (limited to 'src/helpers') diff --git a/src/helpers/Monitor.cpp b/src/helpers/Monitor.cpp index 390bd3c4..a860585d 100644 --- a/src/helpers/Monitor.cpp +++ b/src/helpers/Monitor.cpp @@ -10,12 +10,10 @@ int ratHandler(void* data) { CMonitor::CMonitor() { wlr_damage_ring_init(&damage); - pixman_region32_init(&lastFrameDamage); } CMonitor::~CMonitor() { wlr_damage_ring_finish(&damage); - pixman_region32_fini(&lastFrameDamage); hyprListener_monitorDestroy.removeCallback(); hyprListener_monitorFrame.removeCallback(); @@ -331,6 +329,10 @@ void CMonitor::addDamage(const pixman_region32_t* rg) { g_pCompositor->scheduleFrameForMonitor(this); } +void CMonitor::addDamage(const CRegion* rg) { + addDamage(const_cast(rg)->pixman()); +} + void CMonitor::addDamage(const wlr_box* box) { static auto* const PZOOMFACTOR = &g_pConfigManager->getConfigValuePtr("misc:cursor_zoom_factor")->floatValue; if (*PZOOMFACTOR != 1.f && g_pCompositor->getMonitorFromCursor() == this) { diff --git a/src/helpers/Monitor.hpp b/src/helpers/Monitor.hpp index 08fe5f2b..b453bc8c 100644 --- a/src/helpers/Monitor.hpp +++ b/src/helpers/Monitor.hpp @@ -8,6 +8,7 @@ #include #include #include "Timer.hpp" +#include "Region.hpp" struct SMonitorRule; @@ -61,7 +62,7 @@ class CMonitor { CMonitor* pMirrorOf = nullptr; std::vector mirrors; - pixman_region32_t lastFrameDamage; // stores last frame damage + CRegion lastFrameDamage; // stores last frame damage // for the special workspace. 0 means not open. int specialWorkspaceID = 0; @@ -84,6 +85,7 @@ class CMonitor { void onConnect(bool noRule); void onDisconnect(); void addDamage(const pixman_region32_t* rg); + void addDamage(const CRegion* rg); void addDamage(const wlr_box* box); void setMirror(const std::string&); bool isMirror(); diff --git a/src/helpers/Region.cpp b/src/helpers/Region.cpp new file mode 100644 index 00000000..3a210d06 --- /dev/null +++ b/src/helpers/Region.cpp @@ -0,0 +1,93 @@ +#include "Region.hpp" +#include + +CRegion::CRegion() { + pixman_region32_init(&m_rRegion); +} + +CRegion::CRegion(pixman_region32_t* ref) { + pixman_region32_init(&m_rRegion); + pixman_region32_copy(&m_rRegion, ref); +} + +CRegion::CRegion(double x, double y, double w, double h) { + pixman_region32_init_rect(&m_rRegion, x, y, w, h); +} + +CRegion::CRegion(wlr_box* box) { + pixman_region32_init_rect(&m_rRegion, box->x, box->y, box->width, box->height); +} + +CRegion::CRegion(const CRegion& other) { + pixman_region32_init(&m_rRegion); + pixman_region32_copy(&m_rRegion, const_cast(&other)->pixman()); +} + +CRegion::CRegion(CRegion&& other) { + pixman_region32_init(&m_rRegion); + pixman_region32_copy(&m_rRegion, other.pixman()); +} + +CRegion::~CRegion() { + pixman_region32_fini(&m_rRegion); +} + +CRegion& CRegion::clear() { + pixman_region32_clear(&m_rRegion); + return *this; +} + +CRegion& CRegion::set(const CRegion& other) { + pixman_region32_copy(&m_rRegion, const_cast(&other)->pixman()); + return *this; +} + +CRegion& CRegion::add(const CRegion& other) { + pixman_region32_union(&m_rRegion, &m_rRegion, const_cast(&other)->pixman()); + return *this; +} + +CRegion& CRegion::add(double x, double y, double w, double h) { + pixman_region32_union_rect(&m_rRegion, &m_rRegion, x, y, w, h); + return *this; +} + +CRegion& CRegion::subtract(const CRegion& other) { + pixman_region32_subtract(&m_rRegion, &m_rRegion, const_cast(&other)->pixman()); + return *this; +} + +CRegion& CRegion::intersect(const CRegion& other) { + pixman_region32_intersect(&m_rRegion, &m_rRegion, const_cast(&other)->pixman()); + return *this; +} + +CRegion& CRegion::intersect(double x, double y, double w, double h) { + pixman_region32_intersect_rect(&m_rRegion, &m_rRegion, x, y, w, h); + return *this; +} + +CRegion& CRegion::invert(pixman_box32_t* box) { + pixman_region32_inverse(&m_rRegion, &m_rRegion, box); + return *this; +} + +CRegion& CRegion::translate(const Vector2D& vec) { + pixman_region32_translate(&m_rRegion, vec.x, vec.y); + return *this; +} + +std::vector CRegion::getRects() const { + std::vector result; + + int rectsNum = 0; + const auto RECTSARR = pixman_region32_rectangles(&m_rRegion, &rectsNum); + + result.assign(RECTSARR, RECTSARR + rectsNum); + + return result; +} + +bool CRegion::empty() { + return !pixman_region32_not_empty(&m_rRegion); +} diff --git a/src/helpers/Region.hpp b/src/helpers/Region.hpp new file mode 100644 index 00000000..5c700fa5 --- /dev/null +++ b/src/helpers/Region.hpp @@ -0,0 +1,53 @@ +#pragma once +#include +#include +#include "Vector2D.hpp" + +struct wlr_box; + +class CRegion { + public: + /* Create an empty region */ + CRegion(); + /* Create from a reference. Copies, does not own. */ + CRegion(pixman_region32_t* ref); + /* Create from a box */ + CRegion(double x, double y, double w, double h); + /* Create from a wlr_box */ + CRegion(wlr_box* box); + + CRegion(const CRegion&); + CRegion(CRegion&&); + + ~CRegion(); + + CRegion& operator=(CRegion&& other) { + pixman_region32_copy(&m_rRegion, other.pixman()); + return *this; + } + + CRegion& operator=(CRegion& other) { + pixman_region32_copy(&m_rRegion, other.pixman()); + return *this; + } + + CRegion& clear(); + CRegion& set(const CRegion& other); + CRegion& add(const CRegion& other); + CRegion& add(double x, double y, double w, double h); + CRegion& subtract(const CRegion& other); + CRegion& intersect(const CRegion& other); + CRegion& intersect(double x, double y, double w, double h); + CRegion& translate(const Vector2D& vec); + CRegion& invert(pixman_box32_t* box); + bool empty(); + + std::vector getRects() const; + + pixman_region32_t* pixman() { + return &m_rRegion; + } + + private: + pixman_region32_t m_rRegion; +}; \ No newline at end of file diff --git a/src/helpers/WLClasses.hpp b/src/helpers/WLClasses.hpp index b7cd6e34..18e223b6 100644 --- a/src/helpers/WLClasses.hpp +++ b/src/helpers/WLClasses.hpp @@ -7,6 +7,7 @@ #include "SubsurfaceTree.hpp" #include "AnimatedVariable.hpp" #include "WLSurface.hpp" +#include "Region.hpp" struct SLayerRule { std::string targetNamespace = ""; @@ -144,7 +145,7 @@ struct SMouse { wlr_pointer_constraint_v1* currentConstraint = nullptr; bool constraintActive = false; - pixman_region32_t confinedTo; + CRegion confinedTo; std::string name = ""; -- cgit v1.2.3