mirror of
https://github.com/virtcode/hypr-dynamic-cursors
synced 2025-09-19 16:13:21 +02:00
feat: inefficient support for hardware cursors
This commit is contained in:
parent
facfa7f356
commit
581a094b7b
6 changed files with 339 additions and 2 deletions
148
src/cursor.cpp
148
src/cursor.cpp
|
@ -1,18 +1,29 @@
|
|||
#include "globals.hpp"
|
||||
|
||||
#include <chrono>
|
||||
#include <cmath>
|
||||
#include <cstdlib>
|
||||
|
||||
#define private public
|
||||
#include <hyprland/src/managers/PointerManager.hpp>
|
||||
#include <hyprland/src/render/OpenGL.hpp>
|
||||
#include <hyprland/src/Compositor.hpp>
|
||||
#undef private
|
||||
|
||||
#include <hyprland/src/Compositor.hpp>
|
||||
#include <hyprland/src/config/ConfigValue.hpp>
|
||||
#include "hyprland/cursor.hpp"
|
||||
|
||||
#include <hyprland/wlr/interfaces/wlr_output.h>
|
||||
#include <hyprland/wlr/render/interface.h>
|
||||
#include <hyprland/wlr/render/wlr_renderer.h>
|
||||
|
||||
#include "cursor.hpp"
|
||||
#include "renderer.hpp"
|
||||
|
||||
/*
|
||||
Reimplements rendering of the software cursor.
|
||||
Is also largely identical to hyprlands impl, but uses our custom rendering to rotate the cursor.
|
||||
*/
|
||||
void CDynamicCursors::renderSoftware(CPointerManager* pointers, SP<CMonitor> pMonitor, timespec* now, CRegion& damage, std::optional<Vector2D> overridePos) {
|
||||
|
||||
if (!pointers->hasCursor())
|
||||
|
@ -46,6 +57,10 @@ void CDynamicCursors::renderSoftware(CPointerManager* pointers, SP<CMonitor> pMo
|
|||
renderCursorTextureInternalWithDamage(texture, &box, &damage, 1.F, pointers->currentCursorImage.hotspot);
|
||||
}
|
||||
|
||||
/*
|
||||
This function implements damaging the screen such that the software cursor is drawn.
|
||||
It is largely identical to hyprlands implementation, but expands the damage reagion, to accomodate various rotations.
|
||||
*/
|
||||
void CDynamicCursors::damageSoftware(CPointerManager* pointers) {
|
||||
|
||||
// we damage a 3x3 area around the cursor, to accomodate for all possible hotspots and rotations
|
||||
|
@ -65,6 +80,137 @@ void CDynamicCursors::damageSoftware(CPointerManager* pointers) {
|
|||
}
|
||||
}
|
||||
|
||||
/*
|
||||
This function reimplements the hardware cursor buffer drawing.
|
||||
It is largely copied from hyprland, but adjusted to allow the cursor to be rotated.
|
||||
*/
|
||||
wlr_buffer* CDynamicCursors::renderHardware(CPointerManager* pointers, SP<CPointerManager::SMonitorPointerState> state, SP<CTexture> texture) {
|
||||
static auto* const* PHW_DEBUG= (Hyprlang::INT* const*)HyprlandAPI::getConfigValue(PHANDLE, CONFIG_HW_DEBUG)->getDataStaticPtr();
|
||||
|
||||
auto output = state->monitor->output;
|
||||
|
||||
auto size = pointers->currentCursorImage.size;
|
||||
// we try to allocate a buffer that is thrice as big, see software rendering
|
||||
auto target = size * 3;
|
||||
|
||||
if (output->impl->get_cursor_size) {
|
||||
int w, h;
|
||||
output->impl->get_cursor_size(output, &w, &h);
|
||||
|
||||
if (w < target.x || h < target.y) {
|
||||
Debug::log(TRACE, "hardware cursor too big! {} > {}x{}", pointers->currentCursorImage.size, w, h);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
target.x = w;
|
||||
target.y = h;
|
||||
}
|
||||
|
||||
if (target.x <= 0 || target.y <= 0) {
|
||||
Debug::log(TRACE, "hw cursor for output {} failed the size checks ({}x{} is invalid)", state->monitor->szName, target.x, target.y);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (!output->cursor_swapchain || target != Vector2D{output->cursor_swapchain->width, output->cursor_swapchain->height}) {
|
||||
wlr_drm_format fmt = {0};
|
||||
if (!output_pick_cursor_format(output, &fmt)) {
|
||||
Debug::log(TRACE, "Failed to pick cursor format");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
wlr_swapchain_destroy(output->cursor_swapchain);
|
||||
output->cursor_swapchain = wlr_swapchain_create(output->allocator, target.x, target.y, &fmt);
|
||||
wlr_drm_format_finish(&fmt);
|
||||
|
||||
if (!output->cursor_swapchain) {
|
||||
Debug::log(TRACE, "Failed to create cursor swapchain");
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
wlr_buffer* buf = wlr_swapchain_acquire(output->cursor_swapchain, nullptr);
|
||||
if (!buf) {
|
||||
Debug::log(TRACE, "Failed to acquire a buffer from the cursor swapchain");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
CRegion damage = {0, 0, INT16_MAX, INT16_MAX};
|
||||
|
||||
g_pHyprRenderer->makeEGLCurrent();
|
||||
g_pHyprOpenGL->m_RenderData.pMonitor = state->monitor.get(); // has to be set cuz allocs
|
||||
|
||||
const auto RBO = g_pHyprRenderer->getOrCreateRenderbuffer(buf, DRM_FORMAT_ARGB8888);
|
||||
RBO->bind();
|
||||
|
||||
g_pHyprOpenGL->beginSimple(state->monitor.get(), damage, RBO);
|
||||
|
||||
if (**PHW_DEBUG)
|
||||
g_pHyprOpenGL->clear(CColor{rand() / float(RAND_MAX), rand() / float(RAND_MAX), rand() / float(RAND_MAX), 1.F});
|
||||
else
|
||||
g_pHyprOpenGL->clear(CColor{0.F, 0.F, 0.F, 0.F});
|
||||
|
||||
// the box should start in the middle portion, rotate by our calculated amount
|
||||
CBox xbox = {size, Vector2D{pointers->currentCursorImage.size / pointers->currentCursorImage.scale * state->monitor->scale}.round()};
|
||||
xbox.rot = this->calculate(&pointers->pointerPos);
|
||||
|
||||
// use our custom draw function
|
||||
renderCursorTextureInternalWithDamage(texture, &xbox, &damage, 1.F, pointers->currentCursorImage.hotspot);
|
||||
|
||||
g_pHyprOpenGL->end();
|
||||
glFlush();
|
||||
g_pHyprOpenGL->m_RenderData.pMonitor = nullptr;
|
||||
|
||||
wlr_buffer_unlock(buf);
|
||||
|
||||
return buf;
|
||||
}
|
||||
|
||||
/*
|
||||
Implements the hardware cursor setting.
|
||||
It is also mostly the same as stock hyprland, but with the hotspot translated more into the middle.
|
||||
*/
|
||||
bool CDynamicCursors::setHardware(CPointerManager* pointers, SP<CPointerManager::SMonitorPointerState> state, wlr_buffer* buf) {
|
||||
if (!state->monitor->output->impl->set_cursor)
|
||||
return false;
|
||||
|
||||
const auto HOTSPOT = pointers->transformedHotspot(state->monitor.lock());
|
||||
|
||||
Debug::log(TRACE, "[pointer] hw transformed hotspot for {}: {}", state->monitor->szName, HOTSPOT);
|
||||
|
||||
if (!state->monitor->output->impl->set_cursor(state->monitor->output, buf, HOTSPOT.x + pointers->currentCursorImage.size.x, HOTSPOT.y + pointers->currentCursorImage.size.y))
|
||||
return false;
|
||||
|
||||
wlr_buffer_unlock(state->cursorFrontBuffer);
|
||||
state->cursorFrontBuffer = buf;
|
||||
|
||||
g_pCompositor->scheduleFrameForMonitor(state->monitor.get());
|
||||
|
||||
if (buf)
|
||||
wlr_buffer_lock(buf);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/*
|
||||
Handles cursor move events.
|
||||
*/
|
||||
void CDynamicCursors::onCursorMoved(CPointerManager* pointers) {
|
||||
if (!pointers->hasCursor())
|
||||
return;
|
||||
|
||||
for (auto& m : g_pCompositor->m_vMonitors) {
|
||||
auto state = pointers->stateFor(m);
|
||||
|
||||
state->box = pointers->getCursorBoxLogicalForMonitor(state->monitor.lock());
|
||||
|
||||
if (state->hardwareFailed || !state->entered)
|
||||
continue;
|
||||
|
||||
// we set a new hardware cursor on every move
|
||||
pointers->attemptHardwareCursor(state);
|
||||
}
|
||||
}
|
||||
|
||||
double CDynamicCursors::calculate(Vector2D* pos) {
|
||||
static auto* const* PLENGTH = (Hyprlang::INT* const*)HyprlandAPI::getConfigValue(PHANDLE, CONFIG_LENGTH)->getDataStaticPtr();
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue