From ec672b1ab95e341a0dcb6679592ae4e5eea9b1cf Mon Sep 17 00:00:00 2001 From: Ikalco <73481042+ikalco@users.noreply.github.com> Date: Sat, 27 Jul 2024 10:02:02 -0500 Subject: [PATCH] protocols: move screencopy and toplevel export to hyprwayland-scanner (#7065) * move screencopy and toplevel export to hyprwayland-scanner * oops --- CMakeLists.txt | 13 +- protocols/meson.build | 4 +- src/events/Monitors.cpp | 6 +- src/events/Windows.cpp | 3 +- src/managers/ProtocolManager.cpp | 8 +- src/managers/ProtocolManager.hpp | 5 +- src/protocols/Screencopy.cpp | 671 +++++++++++++------------------ src/protocols/Screencopy.hpp | 152 +++---- src/protocols/ToplevelExport.cpp | 526 +++++++++++------------- src/protocols/ToplevelExport.hpp | 103 ++++- src/render/Renderer.hpp | 2 +- 11 files changed, 695 insertions(+), 798 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 2bd4e0d5..8a27d81b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -288,17 +288,16 @@ endfunction() target_link_libraries(Hyprland OpenGL::EGL OpenGL::GL Threads::Threads libudis86 uuid) -protocol("protocols/wlr-screencopy-unstable-v1.xml" - "wlr-screencopy-unstable-v1" true) protocol( "subprojects/hyprland-protocols/protocols/hyprland-global-shortcuts-v1.xml" "hyprland-global-shortcuts-v1" true) -protocol( - "subprojects/hyprland-protocols/protocols/hyprland-toplevel-export-v1.xml" - "hyprland-toplevel-export-v1" true) -protocol("unstable/text-input/text-input-unstable-v1.xml" - "text-input-unstable-v1" false) +protocol( + "unstable/text-input/text-input-unstable-v1.xml" + "text-input-unstable-v1" false) + +protocolnew("subprojects/hyprland-protocols/protocols" "hyprland-toplevel-export-v1" true) +protocolnew("protocols" "wlr-screencopy-unstable-v1" true) protocolnew("protocols" "wlr-gamma-control-unstable-v1" true) protocolnew("protocols" "wlr-foreign-toplevel-management-unstable-v1" true) protocolnew("protocols" "wlr-output-power-management-unstable-v1" true) diff --git a/protocols/meson.build b/protocols/meson.build index 5cdeb160..35b2b29b 100644 --- a/protocols/meson.build +++ b/protocols/meson.build @@ -25,8 +25,6 @@ hyprwayland_scanner = find_program( protocols = [ [wl_protocol_dir, 'unstable/text-input/text-input-unstable-v1.xml'], - ['wlr-screencopy-unstable-v1.xml'], - [hl_protocol_dir, 'protocols/hyprland-toplevel-export-v1.xml'], [hl_protocol_dir, 'protocols/hyprland-global-shortcuts-v1.xml'] ] @@ -42,6 +40,8 @@ new_protocols = [ ['wlr-layer-shell-unstable-v1.xml'], ['wayland-drm.xml'], ['wlr-data-control-unstable-v1.xml'], + ['wlr-screencopy-unstable-v1.xml'], + [hl_protocol_dir, 'protocols/hyprland-toplevel-export-v1.xml'], [hl_protocol_dir, 'protocols/hyprland-focus-grab-v1.xml'], [wl_protocol_dir, 'staging/tearing-control/tearing-control-v1.xml'], [wl_protocol_dir, 'staging/fractional-scale/fractional-scale-v1.xml'], diff --git a/src/events/Monitors.cpp b/src/events/Monitors.cpp index 40b6f17e..b2778062 100644 --- a/src/events/Monitors.cpp +++ b/src/events/Monitors.cpp @@ -5,6 +5,8 @@ #include "Events.hpp" #include "../debug/HyprCtl.hpp" #include "../config/ConfigValue.hpp" +#include "../protocols/Screencopy.hpp" +#include "../protocols/ToplevelExport.hpp" #include // --------------------------------------------------------- // @@ -118,8 +120,8 @@ void Events::listener_monitorCommit(void* owner, void* data) { const auto PMONITOR = (CMonitor*)owner; if (true) { // FIXME: E->state->committed & WLR_OUTPUT_STATE_BUFFER - g_pProtocolManager->m_pScreencopyProtocolManager->onOutputCommit(PMONITOR); - g_pProtocolManager->m_pToplevelExportProtocolManager->onOutputCommit(PMONITOR); + PROTO::screencopy->onOutputCommit(PMONITOR); + PROTO::toplevelExport->onOutputCommit(PMONITOR); } } diff --git a/src/events/Windows.cpp b/src/events/Windows.cpp index a5511e17..0e1037b6 100644 --- a/src/events/Windows.cpp +++ b/src/events/Windows.cpp @@ -10,6 +10,7 @@ #include "../protocols/LayerShell.hpp" #include "../protocols/XDGShell.hpp" #include "../protocols/core/Compositor.hpp" +#include "../protocols/ToplevelExport.hpp" #include "../xwayland/XSurface.hpp" #include @@ -601,7 +602,7 @@ void Events::listener_unmapWindow(void* owner, void* data) { g_pEventManager->postEvent(SHyprIPCEvent{"closewindow", std::format("{:x}", PWINDOW)}); EMIT_HOOK_EVENT("closeWindow", PWINDOW); - g_pProtocolManager->m_pToplevelExportProtocolManager->onWindowUnmap(PWINDOW); + PROTO::toplevelExport->onWindowUnmap(PWINDOW); if (PWINDOW->m_bIsFullscreen) g_pCompositor->setWindowFullscreen(PWINDOW, false, FULLSCREEN_FULL); diff --git a/src/managers/ProtocolManager.cpp b/src/managers/ProtocolManager.cpp index ef2260cc..3704befb 100644 --- a/src/managers/ProtocolManager.cpp +++ b/src/managers/ProtocolManager.cpp @@ -39,6 +39,8 @@ #include "../protocols/LinuxDMABUF.hpp" #include "../protocols/DRMLease.hpp" #include "../protocols/DRMSyncobj.hpp" +#include "../protocols/Screencopy.hpp" +#include "../protocols/ToplevelExport.hpp" #include "../protocols/core/Seat.hpp" #include "../protocols/core/DataDevice.hpp" @@ -142,6 +144,8 @@ CProtocolManager::CProtocolManager() { PROTO::dataWlr = std::make_unique(&zwlr_data_control_manager_v1_interface, 2, "DataDeviceWlr"); PROTO::primarySelection = std::make_unique(&zwp_primary_selection_device_manager_v1_interface, 1, "PrimarySelection"); PROTO::xwaylandShell = std::make_unique(&xwayland_shell_v1_interface, 1, "XWaylandShell"); + PROTO::screencopy = std::make_unique(&zwlr_screencopy_manager_v1_interface, 3, "Screencopy"); + PROTO::toplevelExport = std::make_unique(&hyprland_toplevel_export_manager_v1_interface, 2, "ToplevelExport"); for (auto& b : g_pCompositor->m_pAqBackend->getImplementations()) { if (b->type() != Aquamarine::AQ_BACKEND_DRM) @@ -161,10 +165,8 @@ CProtocolManager::CProtocolManager() { // Old protocol implementations. // TODO: rewrite them to use hyprwayland-scanner. - m_pToplevelExportProtocolManager = std::make_unique(); m_pTextInputV1ProtocolManager = std::make_unique(); m_pGlobalShortcutsProtocolManager = std::make_unique(); - m_pScreencopyProtocolManager = std::make_unique(); } CProtocolManager::~CProtocolManager() { @@ -214,6 +216,8 @@ CProtocolManager::~CProtocolManager() { PROTO::dataWlr.reset(); PROTO::primarySelection.reset(); PROTO::xwaylandShell.reset(); + PROTO::screencopy.reset(); + PROTO::toplevelExport.reset(); PROTO::lease.reset(); PROTO::sync.reset(); diff --git a/src/managers/ProtocolManager.hpp b/src/managers/ProtocolManager.hpp index 6a780bdc..62653e89 100644 --- a/src/managers/ProtocolManager.hpp +++ b/src/managers/ProtocolManager.hpp @@ -1,10 +1,9 @@ #pragma once #include "../defines.hpp" -#include "../protocols/ToplevelExport.hpp" #include "../protocols/TextInputV1.hpp" #include "../protocols/GlobalShortcuts.hpp" -#include "../protocols/Screencopy.hpp" +#include "../helpers/Monitor.hpp" #include "../helpers/memory/Memory.hpp" #include "../helpers/signal/Signal.hpp" #include @@ -15,10 +14,8 @@ class CProtocolManager { ~CProtocolManager(); // TODO: rewrite to use the new protocol framework - std::unique_ptr m_pToplevelExportProtocolManager; std::unique_ptr m_pTextInputV1ProtocolManager; std::unique_ptr m_pGlobalShortcutsProtocolManager; - std::unique_ptr m_pScreencopyProtocolManager; private: std::unordered_map m_mModeChangeListeners; diff --git a/src/protocols/Screencopy.cpp b/src/protocols/Screencopy.cpp index 98cb0be9..a8afba84 100644 --- a/src/protocols/Screencopy.cpp +++ b/src/protocols/Screencopy.cpp @@ -9,245 +9,39 @@ #include -#define SCREENCOPY_VERSION 3 +#define LOGM PROTO::screencopy->protoLog -static void bindManagerInt(wl_client* client, void* data, uint32_t version, uint32_t id) { - g_pProtocolManager->m_pScreencopyProtocolManager->bindManager(client, data, version, id); +CScreencopyFrame::~CScreencopyFrame() { + if (buffer && buffer->locked()) + buffer->unlock(); } -static void handleDisplayDestroy(struct wl_listener* listener, void* data) { - CScreencopyProtocolManager* proto = wl_container_of(listener, proto, m_liDisplayDestroy); - proto->displayDestroy(); -} +CScreencopyFrame::CScreencopyFrame(SP resource_, int32_t overlay_cursor, wl_resource* output, CBox box_) : resource(resource_) { + if (!good()) + return; -void CScreencopyProtocolManager::displayDestroy() { - wl_list_remove(&m_liDisplayDestroy.link); - wl_list_init(&m_liDisplayDestroy.link); - wl_global_destroy(m_pGlobal); -} + overlayCursor = !!overlay_cursor; + pMonitor = CWLOutputResource::fromResource(output)->monitor.get(); -static SScreencopyFrame* frameFromResource(wl_resource*); - -CScreencopyProtocolManager::~CScreencopyProtocolManager() { - displayDestroy(); -} - -CScreencopyProtocolManager::CScreencopyProtocolManager() { - - m_pGlobal = wl_global_create(g_pCompositor->m_sWLDisplay, &zwlr_screencopy_manager_v1_interface, SCREENCOPY_VERSION, this, bindManagerInt); - - if (!m_pGlobal) { - Debug::log(ERR, "ScreencopyProtocolManager could not start! Screensharing will not work!"); + if (!pMonitor) { + LOGM(ERR, "Client requested sharing of a monitor that doesnt exist"); + resource->sendFailed(); + PROTO::screencopy->destroyResource(this); return; } - m_liDisplayDestroy.notify = handleDisplayDestroy; - wl_display_add_destroy_listener(g_pCompositor->m_sWLDisplay, &m_liDisplayDestroy); - - Debug::log(LOG, "ScreencopyProtocolManager started successfully!"); - - m_pSoftwareCursorTimer = makeShared( - std::nullopt, - [this](SP self, void* data) { - // TODO: make it per-monitor - for (auto& m : g_pCompositor->m_vMonitors) { - g_pPointerManager->unlockSoftwareForMonitor(m); - } - m_bTimerArmed = false; - - Debug::log(LOG, "[screencopy] Releasing software cursor lock"); - }, - nullptr); - g_pEventLoopManager->addTimer(m_pSoftwareCursorTimer); -} - -static void handleCaptureOutput(wl_client* client, wl_resource* resource, uint32_t frame, int32_t overlay_cursor, wl_resource* output) { - g_pProtocolManager->m_pScreencopyProtocolManager->captureOutput(client, resource, frame, overlay_cursor, output); -} - -static void handleCaptureRegion(wl_client* client, wl_resource* resource, uint32_t frame, int32_t overlay_cursor, wl_resource* output, int32_t x, int32_t y, int32_t width, - int32_t height) { - g_pProtocolManager->m_pScreencopyProtocolManager->captureOutput(client, resource, frame, overlay_cursor, output, {x, y, width, height}); -} - -static void handleDestroy(wl_client* client, wl_resource* resource) { - wl_resource_destroy(resource); -} - -static void handleCopyFrame(wl_client* client, wl_resource* resource, wl_resource* buffer) { - const auto PFRAME = frameFromResource(resource); - - if (!PFRAME) - return; - - g_pProtocolManager->m_pScreencopyProtocolManager->copyFrame(client, resource, buffer); -} - -static void handleCopyWithDamage(wl_client* client, wl_resource* resource, wl_resource* buffer) { - const auto PFRAME = frameFromResource(resource); - - if (!PFRAME) - return; - - PFRAME->withDamage = true; - handleCopyFrame(client, resource, buffer); -} - -static void handleDestroyFrame(wl_client* client, wl_resource* resource) { - wl_resource_destroy(resource); -} - -static const struct zwlr_screencopy_manager_v1_interface screencopyMgrImpl = { - .capture_output = handleCaptureOutput, - .capture_output_region = handleCaptureRegion, - .destroy = handleDestroy, -}; - -static const struct zwlr_screencopy_frame_v1_interface screencopyFrameImpl = { - .copy = handleCopyFrame, - .destroy = handleDestroyFrame, - .copy_with_damage = handleCopyWithDamage, -}; - -static CScreencopyClient* clientFromResource(wl_resource* resource) { - ASSERT(wl_resource_instance_of(resource, &zwlr_screencopy_manager_v1_interface, &screencopyMgrImpl)); - return (CScreencopyClient*)wl_resource_get_user_data(resource); -} - -static SScreencopyFrame* frameFromResource(wl_resource* resource) { - ASSERT(wl_resource_instance_of(resource, &zwlr_screencopy_frame_v1_interface, &screencopyFrameImpl)); - return (SScreencopyFrame*)wl_resource_get_user_data(resource); -} - -void CScreencopyProtocolManager::removeClient(CScreencopyClient* client, bool force) { - if (!client) - return; - - if (!force) { - if (!client || client->ref <= 0) - return; - - if (--client->ref != 0) - return; - } - - m_lClients.remove(*client); // TODO: this doesn't get cleaned up after sharing app exits??? - - for (auto& f : m_lFrames) { - // avoid dangling ptrs - if (f.client == client) - f.client = nullptr; - } -} - -static void handleManagerResourceDestroy(wl_resource* resource) { - const auto PCLIENT = clientFromResource(resource); - - g_pProtocolManager->m_pScreencopyProtocolManager->removeClient(PCLIENT, true); -} - -CScreencopyClient::~CScreencopyClient() { - g_pHookSystem->unhook(tickCallback); -} - -CScreencopyClient::CScreencopyClient() { - lastMeasure.reset(); - lastFrame.reset(); - tickCallback = g_pHookSystem->hookDynamic("tick", [&](void* self, SCallbackInfo& info, std::any data) { onTick(); }); -} - -void CScreencopyClient::onTick() { - if (lastMeasure.getMillis() < 500) - return; - - framesInLastHalfSecond = frameCounter; - frameCounter = 0; - lastMeasure.reset(); - - const auto LASTFRAMEDELTA = lastFrame.getMillis() / 1000.0; - const bool FRAMEAWAITING = std::ranges::any_of(g_pProtocolManager->m_pScreencopyProtocolManager->m_lFrames, [&](const auto& frame) { return frame.client == this; }) || - std::ranges::any_of(g_pProtocolManager->m_pToplevelExportProtocolManager->m_lFrames, [&](const auto& frame) { return frame.client == this; }); - - if (framesInLastHalfSecond > 3 && !sentScreencast) { - EMIT_HOOK_EVENT("screencast", (std::vector{1, (uint64_t)framesInLastHalfSecond, (uint64_t)clientOwner})); - g_pEventManager->postEvent(SHyprIPCEvent{"screencast", "1," + std::to_string(clientOwner)}); - sentScreencast = true; - } else if (framesInLastHalfSecond < 4 && sentScreencast && LASTFRAMEDELTA > 1.0 && !FRAMEAWAITING) { - EMIT_HOOK_EVENT("screencast", (std::vector{0, (uint64_t)framesInLastHalfSecond, (uint64_t)clientOwner})); - g_pEventManager->postEvent(SHyprIPCEvent{"screencast", "0," + std::to_string(clientOwner)}); - sentScreencast = false; - } -} - -void CScreencopyProtocolManager::bindManager(wl_client* client, void* data, uint32_t version, uint32_t id) { - const auto PCLIENT = &m_lClients.emplace_back(); - - PCLIENT->resource = wl_resource_create(client, &zwlr_screencopy_manager_v1_interface, version, id); - - if (!PCLIENT->resource) { - Debug::log(ERR, "ScreencopyProtocolManager could not bind! (out of memory?)"); - m_lClients.remove(*PCLIENT); - wl_client_post_no_memory(client); - return; - } - - PCLIENT->ref = 1; - - wl_resource_set_implementation(PCLIENT->resource, &screencopyMgrImpl, PCLIENT, handleManagerResourceDestroy); - - Debug::log(LOG, "ScreencopyProtocolManager bound successfully!"); -} - -static void handleFrameResourceDestroy(wl_resource* resource) { - const auto PFRAME = frameFromResource(resource); - - g_pProtocolManager->m_pScreencopyProtocolManager->removeFrame(PFRAME); -} - -void CScreencopyProtocolManager::removeFrame(SScreencopyFrame* frame, bool force) { - if (!frame) - return; - - std::erase_if(m_vFramesAwaitingWrite, [&](const auto& other) { return other == frame; }); - - wl_resource_set_user_data(frame->resource, nullptr); - if (frame->buffer && frame->buffer->locked()) - frame->buffer->unlock(); - removeClient(frame->client, force); - m_lFrames.remove(*frame); -} - -void CScreencopyProtocolManager::captureOutput(wl_client* client, wl_resource* resource, uint32_t frame, int32_t overlay_cursor, wl_resource* output, CBox box) { - const auto PCLIENT = clientFromResource(resource); - - const auto PFRAME = &m_lFrames.emplace_back(); - PFRAME->overlayCursor = !!overlay_cursor; - PFRAME->resource = wl_resource_create(client, &zwlr_screencopy_frame_v1_interface, wl_resource_get_version(resource), frame); - PFRAME->pMonitor = CWLOutputResource::fromResource(output)->monitor.get(); - - if (!PFRAME->pMonitor) { - Debug::log(ERR, "client requested sharing of a monitor that doesnt exist"); - zwlr_screencopy_frame_v1_send_failed(PFRAME->resource); - removeFrame(PFRAME); - return; - } - - if (!PFRAME->resource) { - Debug::log(ERR, "Couldn't alloc frame for sharing! (no memory)"); - removeFrame(PFRAME); - wl_client_post_no_memory(client); - return; - } - - wl_resource_set_implementation(PFRAME->resource, &screencopyFrameImpl, PFRAME, handleFrameResourceDestroy); - - PFRAME->client = PCLIENT; - PCLIENT->ref++; + resource->setOnDestroy([this](CZwlrScreencopyFrameV1* pMgr) { PROTO::screencopy->destroyResource(this); }); + resource->setDestroy([this](CZwlrScreencopyFrameV1* pFrame) { PROTO::screencopy->destroyResource(this); }); + resource->setCopy([this](CZwlrScreencopyFrameV1* pFrame, wl_resource* res) { this->copy(pFrame, res); }); + resource->setCopyWithDamage([this](CZwlrScreencopyFrameV1* pFrame, wl_resource* res) { + withDamage = true; + this->copy(pFrame, res); + }); g_pHyprRenderer->makeEGLCurrent(); - if (g_pHyprOpenGL->m_mMonitorRenderResources.contains(PFRAME->pMonitor)) { - const auto& RDATA = g_pHyprOpenGL->m_mMonitorRenderResources.at(PFRAME->pMonitor); + if (g_pHyprOpenGL->m_mMonitorRenderResources.contains(pMonitor)) { + const auto& RDATA = g_pHyprOpenGL->m_mMonitorRenderResources.at(pMonitor); // bind the fb for its format. Suppress gl errors. #ifndef GLES2 glBindFramebuffer(GL_READ_FRAMEBUFFER, RDATA.offloadFB.m_iFb); @@ -255,246 +49,212 @@ void CScreencopyProtocolManager::captureOutput(wl_client* client, wl_resource* r glBindFramebuffer(GL_FRAMEBUFFER, RDATA.offloadFB.m_iFb); #endif } else - Debug::log(ERR, "No RDATA in screencopy???"); + LOGM(ERR, "No RDATA in screencopy???"); - PFRAME->shmFormat = g_pHyprOpenGL->getPreferredReadFormat(PFRAME->pMonitor); - if (PFRAME->shmFormat == DRM_FORMAT_INVALID) { - Debug::log(ERR, "No format supported by renderer in capture output"); - zwlr_screencopy_frame_v1_send_failed(PFRAME->resource); - removeFrame(PFRAME); + shmFormat = g_pHyprOpenGL->getPreferredReadFormat(pMonitor); + if (shmFormat == DRM_FORMAT_INVALID) { + LOGM(ERR, "No format supported by renderer in capture output"); + resource->sendFailed(); + PROTO::screencopy->destroyResource(this); return; } - const auto PSHMINFO = FormatUtils::getPixelFormatFromDRM(PFRAME->shmFormat); + const auto PSHMINFO = FormatUtils::getPixelFormatFromDRM(shmFormat); if (!PSHMINFO) { - Debug::log(ERR, "No pixel format supported by renderer in capture output"); - zwlr_screencopy_frame_v1_send_failed(PFRAME->resource); - removeFrame(PFRAME); + LOGM(ERR, "No pixel format supported by renderer in capture output"); + resource->sendFailed(); + PROTO::screencopy->destroyResource(this); return; } - PFRAME->dmabufFormat = PFRAME->pMonitor->output->state->state().drmFormat; + dmabufFormat = pMonitor->output->state->state().drmFormat; - if (box.width == 0 && box.height == 0) - PFRAME->box = {0, 0, (int)(PFRAME->pMonitor->vecSize.x), (int)(PFRAME->pMonitor->vecSize.y)}; + if (box_.width == 0 && box_.height == 0) + box = {0, 0, (int)(pMonitor->vecSize.x), (int)(pMonitor->vecSize.y)}; else { - PFRAME->box = box; + box = box_; } - PFRAME->box.transform(wlTransformToHyprutils(PFRAME->pMonitor->transform), PFRAME->pMonitor->vecTransformedSize.x, PFRAME->pMonitor->vecTransformedSize.y) - .scale(PFRAME->pMonitor->scale) - .round(); + box.transform(wlTransformToHyprutils(pMonitor->transform), pMonitor->vecTransformedSize.x, pMonitor->vecTransformedSize.y).scale(pMonitor->scale).round(); - PFRAME->shmStride = FormatUtils::minStride(PSHMINFO, PFRAME->box.w); + shmStride = FormatUtils::minStride(PSHMINFO, box.w); - zwlr_screencopy_frame_v1_send_buffer(PFRAME->resource, FormatUtils::drmToShm(PFRAME->shmFormat), PFRAME->box.width, PFRAME->box.height, PFRAME->shmStride); + resource->sendBuffer(FormatUtils::drmToShm(shmFormat), box.width, box.height, shmStride); - if (wl_resource_get_version(resource) >= 3) { - if (PFRAME->dmabufFormat != DRM_FORMAT_INVALID) { - zwlr_screencopy_frame_v1_send_linux_dmabuf(PFRAME->resource, PFRAME->dmabufFormat, PFRAME->box.width, PFRAME->box.height); + if (resource->version() >= 3) { + if (dmabufFormat != DRM_FORMAT_INVALID) { + resource->sendLinuxDmabuf(dmabufFormat, box.width, box.height); } - zwlr_screencopy_frame_v1_send_buffer_done(PFRAME->resource); + resource->sendBufferDone(); } } -void CScreencopyProtocolManager::copyFrame(wl_client* client, wl_resource* resource, wl_resource* buffer) { - const auto PFRAME = frameFromResource(resource); - - if (!PFRAME) { - Debug::log(ERR, "No frame in copyFrame??"); +void CScreencopyFrame::copy(CZwlrScreencopyFrameV1* pFrame, wl_resource* buffer_) { + if (!good()) { + LOGM(ERR, "No frame in copyFrame??"); return; } - if (!g_pCompositor->monitorExists(PFRAME->pMonitor)) { - Debug::log(ERR, "client requested sharing of a monitor that is gone"); - zwlr_screencopy_frame_v1_send_failed(PFRAME->resource); - removeFrame(PFRAME); + if (!g_pCompositor->monitorExists(pMonitor)) { + LOGM(ERR, "Client requested sharing of a monitor that is gone"); + resource->sendFailed(); + PROTO::screencopy->destroyResource(this); return; } - const auto PBUFFER = CWLBufferResource::fromResource(buffer); + const auto PBUFFER = CWLBufferResource::fromResource(buffer_); if (!PBUFFER) { - Debug::log(ERR, "[sc] invalid buffer in {:x}", (uintptr_t)PFRAME); - wl_resource_post_error(PFRAME->resource, ZWLR_SCREENCOPY_FRAME_V1_ERROR_INVALID_BUFFER, "invalid buffer"); - removeFrame(PFRAME); + LOGM(ERR, "Invalid buffer in {:x}", (uintptr_t)this); + resource->error(ZWLR_SCREENCOPY_FRAME_V1_ERROR_INVALID_BUFFER, "invalid buffer"); + PROTO::screencopy->destroyResource(this); return; } PBUFFER->buffer->lock(); - if (PBUFFER->buffer->size != PFRAME->box.size()) { - Debug::log(ERR, "[sc] invalid dimensions in {:x}", (uintptr_t)PFRAME); - wl_resource_post_error(PFRAME->resource, ZWLR_SCREENCOPY_FRAME_V1_ERROR_INVALID_BUFFER, "invalid buffer dimensions"); - removeFrame(PFRAME); + if (PBUFFER->buffer->size != box.size()) { + LOGM(ERR, "Invalid dimensions in {:x}", (uintptr_t)this); + resource->error(ZWLR_SCREENCOPY_FRAME_V1_ERROR_INVALID_BUFFER, "invalid buffer dimensions"); + PROTO::screencopy->destroyResource(this); return; } - if (PFRAME->buffer) { - Debug::log(ERR, "[sc] buffer used in {:x}", (uintptr_t)PFRAME); - wl_resource_post_error(PFRAME->resource, ZWLR_SCREENCOPY_FRAME_V1_ERROR_ALREADY_USED, "frame already used"); - removeFrame(PFRAME); + if (buffer) { + LOGM(ERR, "Buffer used in {:x}", (uintptr_t)this); + resource->error(ZWLR_SCREENCOPY_FRAME_V1_ERROR_ALREADY_USED, "frame already used"); + PROTO::screencopy->destroyResource(this); return; } if (auto attrs = PBUFFER->buffer->dmabuf(); attrs.success) { - PFRAME->bufferDMA = true; + bufferDMA = true; - if (attrs.format != PFRAME->dmabufFormat) { - Debug::log(ERR, "[sc] invalid buffer dma format in {:x}", (uintptr_t)PFRAME); - wl_resource_post_error(PFRAME->resource, ZWLR_SCREENCOPY_FRAME_V1_ERROR_INVALID_BUFFER, "invalid buffer format"); - removeFrame(PFRAME); + if (attrs.format != dmabufFormat) { + LOGM(ERR, "Invalid buffer dma format in {:x}", (uintptr_t)pFrame); + resource->error(ZWLR_SCREENCOPY_FRAME_V1_ERROR_INVALID_BUFFER, "invalid buffer format"); + PROTO::screencopy->destroyResource(this); return; } } else if (auto attrs = PBUFFER->buffer->shm(); attrs.success) { - if (attrs.format != PFRAME->shmFormat) { - Debug::log(ERR, "[sc] invalid buffer shm format in {:x}", (uintptr_t)PFRAME); - wl_resource_post_error(PFRAME->resource, ZWLR_SCREENCOPY_FRAME_V1_ERROR_INVALID_BUFFER, "invalid buffer format"); - removeFrame(PFRAME); + if (attrs.format != shmFormat) { + LOGM(ERR, "Invalid buffer shm format in {:x}", (uintptr_t)pFrame); + resource->error(ZWLR_SCREENCOPY_FRAME_V1_ERROR_INVALID_BUFFER, "invalid buffer format"); + PROTO::screencopy->destroyResource(this); return; - } else if ((int)attrs.stride != PFRAME->shmStride) { - Debug::log(ERR, "[sc] invalid buffer shm stride in {:x}", (uintptr_t)PFRAME); - wl_resource_post_error(PFRAME->resource, ZWLR_SCREENCOPY_FRAME_V1_ERROR_INVALID_BUFFER, "invalid buffer stride"); - removeFrame(PFRAME); + } else if ((int)attrs.stride != shmStride) { + LOGM(ERR, "Invalid buffer shm stride in {:x}", (uintptr_t)pFrame); + resource->error(ZWLR_SCREENCOPY_FRAME_V1_ERROR_INVALID_BUFFER, "invalid buffer stride"); + PROTO::screencopy->destroyResource(this); return; } } else { - Debug::log(ERR, "[sc] invalid buffer type in {:x}", (uintptr_t)PFRAME); - wl_resource_post_error(PFRAME->resource, ZWLR_SCREENCOPY_FRAME_V1_ERROR_INVALID_BUFFER, "invalid buffer type"); - removeFrame(PFRAME); + LOGM(ERR, "Invalid buffer type in {:x}", (uintptr_t)pFrame); + resource->error(ZWLR_SCREENCOPY_FRAME_V1_ERROR_INVALID_BUFFER, "invalid buffer type"); + PROTO::screencopy->destroyResource(this); return; } - PFRAME->buffer = PBUFFER->buffer; + buffer = PBUFFER->buffer; - m_vFramesAwaitingWrite.emplace_back(PFRAME); + PROTO::screencopy->m_vFramesAwaitingWrite.emplace_back(self); g_pHyprRenderer->m_bDirectScanoutBlocked = true; - if (PFRAME->overlayCursor && !PFRAME->lockedSWCursors) { - PFRAME->lockedSWCursors = true; + if (overlayCursor && !lockedSWCursors) { + lockedSWCursors = true; // TODO: make it per-monitor - if (!m_bTimerArmed) { + if (!PROTO::screencopy->m_bTimerArmed) { for (auto& m : g_pCompositor->m_vMonitors) { g_pPointerManager->lockSoftwareForMonitor(m); } - m_bTimerArmed = true; - Debug::log(LOG, "[screencopy] Locking sw cursors due to screensharing"); + PROTO::screencopy->m_bTimerArmed = true; + LOGM(LOG, "Locking sw cursors due to screensharing"); } - m_pSoftwareCursorTimer->updateTimeout(std::chrono::seconds(1)); + PROTO::screencopy->m_pSoftwareCursorTimer->updateTimeout(std::chrono::seconds(1)); } - if (!PFRAME->withDamage) - g_pHyprRenderer->damageMonitor(PFRAME->pMonitor); + if (!withDamage) + g_pHyprRenderer->damageMonitor(pMonitor); } -void CScreencopyProtocolManager::onOutputCommit(CMonitor* pMonitor) { - m_pLastMonitorBackBuffer = pMonitor->output->state->state().buffer; - shareAllFrames(pMonitor); - m_pLastMonitorBackBuffer.reset(); -} - -void CScreencopyProtocolManager::shareAllFrames(CMonitor* pMonitor) { - if (m_vFramesAwaitingWrite.empty()) - return; // nothing to share - - std::vector framesToRemove; - - // share frame if correct output - for (auto& f : m_vFramesAwaitingWrite) { - if (!f->pMonitor || !f->buffer) { - framesToRemove.push_back(f); - continue; - } - - if (f->pMonitor != pMonitor) - continue; - - shareFrame(f); - - f->client->lastFrame.reset(); - ++f->client->frameCounter; - - framesToRemove.push_back(f); - } - - for (auto& f : framesToRemove) { - removeFrame(f); - } - - if (m_vFramesAwaitingWrite.empty()) { - g_pHyprRenderer->m_bDirectScanoutBlocked = false; - } -} - -void CScreencopyProtocolManager::shareFrame(SScreencopyFrame* frame) { - if (!frame->buffer) +void CScreencopyFrame::share() { + if (!buffer || !pMonitor) return; timespec now; clock_gettime(CLOCK_MONOTONIC, &now); - uint32_t flags = 0; - if (frame->bufferDMA) { - if (!copyFrameDmabuf(frame)) { - Debug::log(ERR, "[sc] dmabuf copy failed in {:x}", (uintptr_t)frame); - zwlr_screencopy_frame_v1_send_failed(frame->resource); + if (bufferDMA) { + if (!copyDmabuf()) { + LOGM(ERR, "Dmabuf copy failed in {:x}", (uintptr_t)this); + resource->sendFailed(); return; } } else { - if (!copyFrameShm(frame, &now)) { - Debug::log(ERR, "[sc] shm copy failed in {:x}", (uintptr_t)frame); - zwlr_screencopy_frame_v1_send_failed(frame->resource); + if (!copyShm()) { + LOGM(ERR, "Shm copy failed in {:x}", (uintptr_t)this); + resource->sendFailed(); return; } } - zwlr_screencopy_frame_v1_send_flags(frame->resource, flags); - sendFrameDamage(frame); + resource->sendFlags((zwlrScreencopyFrameV1Flags)0); + if (withDamage) { + // TODO: add a damage ring for this. + resource->sendDamage(0, 0, buffer->size.x, buffer->size.y); + } + uint32_t tvSecHi = (sizeof(now.tv_sec) > 4) ? now.tv_sec >> 32 : 0; uint32_t tvSecLo = now.tv_sec & 0xFFFFFFFF; - zwlr_screencopy_frame_v1_send_ready(frame->resource, tvSecHi, tvSecLo, now.tv_nsec); + resource->sendReady(tvSecHi, tvSecLo, now.tv_nsec); } -void CScreencopyProtocolManager::sendFrameDamage(SScreencopyFrame* frame) { - if (!frame->withDamage) - return; +bool CScreencopyFrame::copyDmabuf() { + auto TEXTURE = makeShared(pMonitor->output->state->state().buffer); - // TODO: - // add a damage ring for this. + CRegion fakeDamage = {0, 0, INT16_MAX, INT16_MAX}; - // for (auto& RECT : frame->pMonitor->lastFrameDamage.getRects()) { + if (!g_pHyprRenderer->beginRender(pMonitor, fakeDamage, RENDER_MODE_TO_BUFFER, buffer.lock(), nullptr, true)) { + LOGM(ERR, "Can't copy: failed to begin rendering to dma frame"); + return false; + } - // if (frame->buffer->width < 1 || frame->buffer->height < 1 || frame->buffer->width - RECT.x1 < 1 || frame->buffer->height - RECT.y1 < 1) { - // Debug::log(ERR, "[sc] Failed to send damage"); - // break; - // } + CBox monbox = CBox{0, 0, pMonitor->vecPixelSize.x, pMonitor->vecPixelSize.y} + .translate({-box.x, -box.y}) // vvvv kinda ass-backwards but that's how I designed the renderer... sigh. + .transform(wlTransformToHyprutils(invertTransform(pMonitor->transform)), pMonitor->vecPixelSize.x, pMonitor->vecPixelSize.y); + g_pHyprOpenGL->setMonitorTransformEnabled(true); + g_pHyprOpenGL->setRenderModifEnabled(false); + g_pHyprOpenGL->renderTexture(TEXTURE, &monbox, 1); + g_pHyprOpenGL->setRenderModifEnabled(true); + g_pHyprOpenGL->setMonitorTransformEnabled(false); - // zwlr_screencopy_frame_v1_send_damage(frame->resource, std::clamp(RECT.x1, 0, frame->buffer->width), std::clamp(RECT.y1, 0, frame->buffer->height), - // std::clamp(RECT.x2 - RECT.x1, 0, frame->buffer->width - RECT.x1), std::clamp(RECT.y2 - RECT.y1, 0, frame->buffer->height - RECT.y1)); - // } + g_pHyprOpenGL->m_RenderData.blockScreenShader = true; + g_pHyprRenderer->endRender(); - zwlr_screencopy_frame_v1_send_damage(frame->resource, 0, 0, frame->buffer->size.x, frame->buffer->size.y); + LOGM(TRACE, "Copied frame via dma"); + + return true; } -bool CScreencopyProtocolManager::copyFrameShm(SScreencopyFrame* frame, timespec* now) { - auto TEXTURE = makeShared(m_pLastMonitorBackBuffer); +bool CScreencopyFrame::copyShm() { + auto TEXTURE = makeShared(pMonitor->output->state->state().buffer); - auto shm = frame->buffer->shm(); - auto [pixelData, fmt, bufLen] = frame->buffer->beginDataPtr(0); // no need for end, cuz it's shm + auto shm = buffer->shm(); + auto [pixelData, fmt, bufLen] = buffer->beginDataPtr(0); // no need for end, cuz it's shm CRegion fakeDamage = {0, 0, INT16_MAX, INT16_MAX}; g_pHyprRenderer->makeEGLCurrent(); CFramebuffer fb; - fb.alloc(frame->box.w, frame->box.h, g_pHyprRenderer->isNvidia() ? DRM_FORMAT_XBGR8888 : frame->pMonitor->output->state->state().drmFormat); + fb.alloc(box.w, box.h, g_pHyprRenderer->isNvidia() ? DRM_FORMAT_XBGR8888 : pMonitor->output->state->state().drmFormat); - if (!g_pHyprRenderer->beginRender(frame->pMonitor, fakeDamage, RENDER_MODE_FULL_FAKE, nullptr, &fb, true)) { - Debug::log(ERR, "Screencopy: can't copy: failed to begin rendering"); + if (!g_pHyprRenderer->beginRender(pMonitor, fakeDamage, RENDER_MODE_FULL_FAKE, nullptr, &fb, true)) { + LOGM(ERR, "Can't copy: failed to begin rendering"); return false; } - CBox monbox = CBox{0, 0, frame->pMonitor->vecTransformedSize.x, frame->pMonitor->vecTransformedSize.y}.translate({-frame->box.x, -frame->box.y}); + CBox monbox = CBox{0, 0, pMonitor->vecTransformedSize.x, pMonitor->vecTransformedSize.y}.translate({-box.x, -box.y}); g_pHyprOpenGL->setMonitorTransformEnabled(true); g_pHyprOpenGL->setRenderModifEnabled(false); g_pHyprOpenGL->renderTexture(TEXTURE, &monbox, 1); @@ -509,7 +269,7 @@ bool CScreencopyProtocolManager::copyFrameShm(SScreencopyFrame* frame, timespec* const auto PFORMAT = FormatUtils::getPixelFormatFromDRM(shm.format); if (!PFORMAT) { - Debug::log(ERR, "Screencopy: can't copy: failed to find a pixel format"); + LOGM(ERR, "Can't copy: failed to find a pixel format"); g_pHyprRenderer->endRender(); return false; } @@ -520,53 +280,164 @@ bool CScreencopyProtocolManager::copyFrameShm(SScreencopyFrame* frame, timespec* g_pHyprRenderer->endRender(); g_pHyprRenderer->makeEGLCurrent(); - g_pHyprOpenGL->m_RenderData.pMonitor = frame->pMonitor; + g_pHyprOpenGL->m_RenderData.pMonitor = pMonitor; fb.bind(); glPixelStorei(GL_PACK_ALIGNMENT, 1); const auto drmFmt = FormatUtils::getPixelFormatFromDRM(shm.format); - uint32_t packStride = FormatUtils::minStride(drmFmt, frame->box.w); + uint32_t packStride = FormatUtils::minStride(drmFmt, box.w); if (packStride == (uint32_t)shm.stride) { - glReadPixels(0, 0, frame->box.w, frame->box.h, glFormat, PFORMAT->glType, pixelData); + glReadPixels(0, 0, box.w, box.h, glFormat, PFORMAT->glType, pixelData); } else { - for (size_t i = 0; i < frame->box.h; ++i) { + for (size_t i = 0; i < box.h; ++i) { uint32_t y = i; - glReadPixels(0, y, frame->box.w, 1, glFormat, PFORMAT->glType, ((unsigned char*)pixelData) + i * shm.stride); + glReadPixels(0, y, box.w, 1, glFormat, PFORMAT->glType, ((unsigned char*)pixelData) + i * shm.stride); } } g_pHyprOpenGL->m_RenderData.pMonitor = nullptr; - Debug::log(TRACE, "Screencopy: copied frame via shm"); + LOGM(TRACE, "Copied frame via shm"); return true; } -bool CScreencopyProtocolManager::copyFrameDmabuf(SScreencopyFrame* frame) { - auto TEXTURE = makeShared(m_pLastMonitorBackBuffer); +bool CScreencopyFrame::good() { + return resource->resource(); +} - CRegion fakeDamage = {0, 0, INT16_MAX, INT16_MAX}; +CScreencopyClient::~CScreencopyClient() { + g_pHookSystem->unhook(tickCallback); +} - if (!g_pHyprRenderer->beginRender(frame->pMonitor, fakeDamage, RENDER_MODE_TO_BUFFER, frame->buffer.lock(), nullptr, true)) { - Debug::log(ERR, "Screencopy: can't copy: failed to begin rendering to dma frame"); - return false; +CScreencopyClient::CScreencopyClient(SP resource_) : resource(resource_) { + if (!good()) + return; + + resource->setDestroy([this](CZwlrScreencopyManagerV1* pMgr) { PROTO::screencopy->destroyResource(this); }); + resource->setOnDestroy([this](CZwlrScreencopyManagerV1* pMgr) { PROTO::screencopy->destroyResource(this); }); + resource->setCaptureOutput( + [this](CZwlrScreencopyManagerV1* pMgr, uint32_t frame, int32_t overlayCursor, wl_resource* output) { this->captureOutput(frame, overlayCursor, output, {}); }); + resource->setCaptureOutputRegion([this](CZwlrScreencopyManagerV1* pMgr, uint32_t frame, int32_t overlayCursor, wl_resource* output, int32_t x, int32_t y, int32_t w, + int32_t h) { this->captureOutput(frame, overlayCursor, output, {x, y, w, h}); }); + + lastMeasure.reset(); + lastFrame.reset(); + tickCallback = g_pHookSystem->hookDynamic("tick", [&](void* self, SCallbackInfo& info, std::any data) { onTick(); }); +} + +void CScreencopyClient::captureOutput(uint32_t frame, int32_t overlayCursor_, wl_resource* output, CBox box) { + const auto FRAME = PROTO::screencopy->m_vFrames.emplace_back( + makeShared(makeShared(resource->client(), resource->version(), frame), overlayCursor_, output, box)); + + if (!FRAME->good()) { + LOGM(ERR, "Couldn't alloc frame for sharing! (no memory)"); + resource->noMemory(); + PROTO::screencopy->destroyResource(FRAME.get()); + return; } - CBox monbox = CBox{0, 0, frame->pMonitor->vecPixelSize.x, frame->pMonitor->vecPixelSize.y} - .translate({-frame->box.x, -frame->box.y}) // vvvv kinda ass-backwards but that's how I designed the renderer... sigh. - .transform(wlTransformToHyprutils(invertTransform(frame->pMonitor->transform)), frame->pMonitor->vecPixelSize.x, frame->pMonitor->vecPixelSize.y); - g_pHyprOpenGL->setMonitorTransformEnabled(true); - g_pHyprOpenGL->setRenderModifEnabled(false); - g_pHyprOpenGL->renderTexture(TEXTURE, &monbox, 1); - g_pHyprOpenGL->setRenderModifEnabled(true); - g_pHyprOpenGL->setMonitorTransformEnabled(false); - - g_pHyprOpenGL->m_RenderData.blockScreenShader = true; - g_pHyprRenderer->endRender(); - - Debug::log(TRACE, "Screencopy: copied frame via dma"); - - return true; + FRAME->self = FRAME; + FRAME->client = self; +} + +void CScreencopyClient::onTick() { + if (lastMeasure.getMillis() < 500) + return; + + framesInLastHalfSecond = frameCounter; + frameCounter = 0; + lastMeasure.reset(); + + const auto LASTFRAMEDELTA = lastFrame.getMillis() / 1000.0; + const bool FRAMEAWAITING = std::ranges::any_of(PROTO::screencopy->m_vFrames, [&](const auto& frame) { return frame->client.get() == this; }); + + if (framesInLastHalfSecond > 3 && !sentScreencast) { + EMIT_HOOK_EVENT("screencast", (std::vector{1, (uint64_t)framesInLastHalfSecond, (uint64_t)clientOwner})); + g_pEventManager->postEvent(SHyprIPCEvent{"screencast", "1," + std::to_string(clientOwner)}); + sentScreencast = true; + } else if (framesInLastHalfSecond < 4 && sentScreencast && LASTFRAMEDELTA > 1.0 && !FRAMEAWAITING) { + EMIT_HOOK_EVENT("screencast", (std::vector{0, (uint64_t)framesInLastHalfSecond, (uint64_t)clientOwner})); + g_pEventManager->postEvent(SHyprIPCEvent{"screencast", "0," + std::to_string(clientOwner)}); + sentScreencast = false; + } +} + +bool CScreencopyClient::good() { + return resource->resource(); +} + +CScreencopyProtocol::CScreencopyProtocol(const wl_interface* iface, const int& ver, const std::string& name) : IWaylandProtocol(iface, ver, name) { + m_pSoftwareCursorTimer = makeShared( + std::nullopt, + [this](SP self, void* data) { + // TODO: make it per-monitor + for (auto& m : g_pCompositor->m_vMonitors) { + g_pPointerManager->unlockSoftwareForMonitor(m); + } + m_bTimerArmed = false; + + LOGM(LOG, "Releasing software cursor lock"); + }, + nullptr); + g_pEventLoopManager->addTimer(m_pSoftwareCursorTimer); +} + +void CScreencopyProtocol::bindManager(wl_client* client, void* data, uint32_t ver, uint32_t id) { + const auto CLIENT = m_vClients.emplace_back(makeShared(makeShared(client, ver, id))); + + if (!CLIENT->good()) { + LOGM(LOG, "Failed to bind client! (out of memory)"); + CLIENT->resource->noMemory(); + m_vClients.pop_back(); + return; + } + + CLIENT->self = CLIENT; + + LOGM(LOG, "Bound client successfully!"); +} + +void CScreencopyProtocol::destroyResource(CScreencopyClient* client) { + std::erase_if(m_vClients, [&](const auto& other) { return other.get() == client; }); + std::erase_if(m_vFrames, [&](const auto& other) { return other->client.get() == client; }); + std::erase_if(m_vFramesAwaitingWrite, [&](const auto& other) { return other->client.get() == client; }); +} + +void CScreencopyProtocol::destroyResource(CScreencopyFrame* frame) { + std::erase_if(m_vFrames, [&](const auto& other) { return other.get() == frame; }); + std::erase_if(m_vFramesAwaitingWrite, [&](const auto& other) { return other.get() == frame; }); +} + +void CScreencopyProtocol::onOutputCommit(CMonitor* pMonitor) { + if (m_vFramesAwaitingWrite.empty()) { + g_pHyprRenderer->m_bDirectScanoutBlocked = false; + return; // nothing to share + } + + std::vector> framesToRemove; + + // share frame if correct output + for (auto& f : m_vFramesAwaitingWrite) { + if (!f->pMonitor || !f->buffer) { + framesToRemove.push_back(f); + continue; + } + + if (f->pMonitor != pMonitor) + continue; + + f->share(); + + f->client->lastFrame.reset(); + ++f->client->frameCounter; + + framesToRemove.push_back(f); + } + + for (auto& f : framesToRemove) { + destroyResource(f.get()); + } } diff --git a/src/protocols/Screencopy.hpp b/src/protocols/Screencopy.hpp index d564e432..cbc56edb 100644 --- a/src/protocols/Screencopy.hpp +++ b/src/protocols/Screencopy.hpp @@ -1,7 +1,8 @@ #pragma once #include "../defines.hpp" -#include "wlr-screencopy-unstable-v1-protocol.h" +#include "wlr-screencopy-unstable-v1.hpp" +#include "WaylandProtocol.hpp" #include #include @@ -20,88 +21,93 @@ enum eClientOwners { class CScreencopyClient { public: - CScreencopyClient(); + CScreencopyClient(SP resource_); ~CScreencopyClient(); - int ref = 0; - wl_resource* resource = nullptr; + bool good(); - eClientOwners clientOwner = CLIENT_SCREENCOPY; + WP self; + eClientOwners clientOwner = CLIENT_SCREENCOPY; - int frameCounter = 0; - int framesInLastHalfSecond = 0; - CTimer lastMeasure; - CTimer lastFrame; - bool sentScreencast = false; - - void onTick(); - SP tickCallback; - - bool operator==(const CScreencopyClient& other) const { - return resource == other.resource; - } -}; - -struct SScreencopyFrame { - wl_resource* resource = nullptr; - CScreencopyClient* client = nullptr; - - uint32_t shmFormat = 0; - uint32_t dmabufFormat = 0; - CBox box = {}; - int shmStride = 0; - - bool overlayCursor = false; - bool withDamage = false; - bool lockedSWCursors = false; - - bool bufferDMA = false; - - WP buffer; - - CMonitor* pMonitor = nullptr; - PHLWINDOWREF pWindow; - - bool operator==(const SScreencopyFrame& other) const { - return resource == other.resource && client == other.client; - } -}; - -class CScreencopyProtocolManager { - public: - CScreencopyProtocolManager(); - ~CScreencopyProtocolManager(); - - void bindManager(wl_client* client, void* data, uint32_t version, uint32_t id); - void removeClient(CScreencopyClient* client, bool force = false); - void removeFrame(SScreencopyFrame* frame, bool force = false); - void displayDestroy(); - - void captureOutput(wl_client* client, wl_resource* resource, uint32_t frame, int32_t overlay_cursor, wl_resource* output, CBox box = {0, 0, 0, 0}); - - void copyFrame(wl_client* client, wl_resource* resource, wl_resource* buffer); - - void onOutputCommit(CMonitor* pMonitor); - - wl_listener m_liDisplayDestroy; + CTimer lastFrame; + int frameCounter = 0; private: - wl_global* m_pGlobal = nullptr; - std::list m_lFrames; - std::list m_lClients; + SP resource; - SP m_pSoftwareCursorTimer; - bool m_bTimerArmed = false; + int framesInLastHalfSecond = 0; + CTimer lastMeasure; + bool sentScreencast = false; - std::vector m_vFramesAwaitingWrite; + SP tickCallback; + void onTick(); - SP m_pLastMonitorBackBuffer; + void captureOutput(uint32_t frame, int32_t overlayCursor, wl_resource* output, CBox box); - void shareAllFrames(CMonitor* pMonitor); - void shareFrame(SScreencopyFrame* frame); - void sendFrameDamage(SScreencopyFrame* frame); - bool copyFrameDmabuf(SScreencopyFrame* frame); - bool copyFrameShm(SScreencopyFrame* frame, timespec* now); + friend class CScreencopyProtocol; +}; +class CScreencopyFrame { + public: + CScreencopyFrame(SP resource, int32_t overlay_cursor, wl_resource* output, CBox box); + ~CScreencopyFrame(); + + bool good(); + + SP self; + WP client; + + private: + SP resource; + + CMonitor* pMonitor = nullptr; + bool overlayCursor = false; + bool withDamage = false; + bool lockedSWCursors = false; + + WP buffer; + bool bufferDMA = false; + uint32_t shmFormat = 0; + uint32_t dmabufFormat = 0; + int shmStride = 0; + CBox box = {}; + + void copy(CZwlrScreencopyFrameV1* pFrame, wl_resource* buffer); + bool copyDmabuf(); + bool copyShm(); + void share(); + + friend class CScreencopyProtocol; +}; + +class CScreencopyProtocol : public IWaylandProtocol { + public: + CScreencopyProtocol(const wl_interface* iface, const int& ver, const std::string& name); + + virtual void bindManager(wl_client* client, void* data, uint32_t version, uint32_t id); + void destroyResource(CScreencopyClient* resource); + void destroyResource(CScreencopyFrame* resource); + + void onOutputCommit(CMonitor* pMonitor); + + private: + std::vector> m_vFrames; + std::vector> m_vFramesAwaitingWrite; + std::vector> m_vClients; + + SP m_pSoftwareCursorTimer; + bool m_bTimerArmed = false; + + void shareAllFrames(CMonitor* pMonitor); + void shareFrame(CScreencopyFrame* frame); + void sendFrameDamage(CScreencopyFrame* frame); + bool copyFrameDmabuf(CScreencopyFrame* frame); + bool copyFrameShm(CScreencopyFrame* frame, timespec* now); + + friend class CScreencopyFrame; friend class CScreencopyClient; }; + +namespace PROTO { + inline UP screencopy; +}; diff --git a/src/protocols/ToplevelExport.cpp b/src/protocols/ToplevelExport.cpp index 28f49c6a..fb3fde2b 100644 --- a/src/protocols/ToplevelExport.cpp +++ b/src/protocols/ToplevelExport.cpp @@ -8,363 +8,240 @@ #include -#define TOPLEVEL_EXPORT_VERSION 2 +#define LOGM PROTO::toplevelExport->protoLog -static void bindManagerInt(wl_client* client, void* data, uint32_t version, uint32_t id) { - g_pProtocolManager->m_pToplevelExportProtocolManager->bindManager(client, data, version, id); -} - -static void handleDisplayDestroy(struct wl_listener* listener, void* data) { - CToplevelExportProtocolManager* proto = wl_container_of(listener, proto, m_liDisplayDestroy); - proto->displayDestroy(); -} - -void CToplevelExportProtocolManager::displayDestroy() { - wl_list_remove(&m_liDisplayDestroy.link); - wl_list_init(&m_liDisplayDestroy.link); - wl_global_destroy(m_pGlobal); -} - -CToplevelExportProtocolManager::~CToplevelExportProtocolManager() { - displayDestroy(); -} - -CToplevelExportProtocolManager::CToplevelExportProtocolManager() { - -#ifndef GLES32 - Debug::log(WARN, "Toplevel sharing is not supported on LEGACY_RENDERER!"); - return; -#endif - - m_pGlobal = wl_global_create(g_pCompositor->m_sWLDisplay, &hyprland_toplevel_export_manager_v1_interface, TOPLEVEL_EXPORT_VERSION, this, bindManagerInt); - - if (!m_pGlobal) { - Debug::log(ERR, "ToplevelExportManager could not start! Sharing windows will not work!"); - return; - } - - m_liDisplayDestroy.notify = handleDisplayDestroy; - wl_display_add_destroy_listener(g_pCompositor->m_sWLDisplay, &m_liDisplayDestroy); - - Debug::log(LOG, "ToplevelExportManager started successfully!"); -} - -static void handleCaptureToplevel(wl_client* client, wl_resource* resource, uint32_t frame, int32_t overlay_cursor, uint32_t handle) { - g_pProtocolManager->m_pToplevelExportProtocolManager->captureToplevel(client, resource, frame, overlay_cursor, g_pCompositor->getWindowFromHandle(handle)); -} - -static void handleCaptureToplevelWithWlr(wl_client* client, wl_resource* resource, uint32_t frame, int32_t overlay_cursor, wl_resource* handle) { - g_pProtocolManager->m_pToplevelExportProtocolManager->captureToplevel(client, resource, frame, overlay_cursor, PROTO::foreignToplevelWlr->windowFromHandleResource(handle)); -} - -static void handleDestroy(wl_client* client, wl_resource* resource) { - wl_resource_destroy(resource); -} - -static void handleCopyFrame(wl_client* client, wl_resource* resource, wl_resource* buffer, int32_t ignore_damage) { - g_pProtocolManager->m_pToplevelExportProtocolManager->copyFrame(client, resource, buffer, ignore_damage); -} - -static void handleDestroyFrame(wl_client* client, wl_resource* resource) { - wl_resource_destroy(resource); -} - -static const struct hyprland_toplevel_export_manager_v1_interface toplevelExportManagerImpl = { - .capture_toplevel = handleCaptureToplevel, - .destroy = handleDestroy, - .capture_toplevel_with_wlr_toplevel_handle = handleCaptureToplevelWithWlr, -}; - -static const struct hyprland_toplevel_export_frame_v1_interface toplevelFrameImpl = {.copy = handleCopyFrame, .destroy = handleDestroyFrame}; - -// -static CScreencopyClient* clientFromResource(wl_resource* resource) { - ASSERT(wl_resource_instance_of(resource, &hyprland_toplevel_export_manager_v1_interface, &toplevelExportManagerImpl)); - return (CScreencopyClient*)wl_resource_get_user_data(resource); -} - -static SScreencopyFrame* frameFromResource(wl_resource* resource) { - ASSERT(wl_resource_instance_of(resource, &hyprland_toplevel_export_frame_v1_interface, &toplevelFrameImpl)); - return (SScreencopyFrame*)wl_resource_get_user_data(resource); -} - -void CToplevelExportProtocolManager::removeClient(CScreencopyClient* client, bool force) { - if (!force) { - if (!client || client->ref <= 0) - return; - - if (--client->ref != 0) - return; - } - - m_lClients.remove(*client); // TODO: this doesn't get cleaned up after sharing app exits??? -} - -static void handleManagerResourceDestroy(wl_resource* resource) { - const auto PCLIENT = clientFromResource(resource); - - g_pProtocolManager->m_pToplevelExportProtocolManager->removeClient(PCLIENT, true); -} - -void CToplevelExportProtocolManager::bindManager(wl_client* client, void* data, uint32_t version, uint32_t id) { - const auto PCLIENT = &m_lClients.emplace_back(); - - PCLIENT->clientOwner = CLIENT_TOPLEVEL_EXPORT; - PCLIENT->resource = wl_resource_create(client, &hyprland_toplevel_export_manager_v1_interface, version, id); - - if (!PCLIENT->resource) { - Debug::log(ERR, "ToplevelExportManager could not bind! (out of memory?)"); - m_lClients.remove(*PCLIENT); - wl_client_post_no_memory(client); - return; - } - - PCLIENT->ref = 1; - - wl_resource_set_implementation(PCLIENT->resource, &toplevelExportManagerImpl, PCLIENT, handleManagerResourceDestroy); - - Debug::log(LOG, "ToplevelExportManager bound successfully!"); -} - -static void handleFrameResourceDestroy(wl_resource* resource) { - const auto PFRAME = frameFromResource(resource); - - g_pProtocolManager->m_pToplevelExportProtocolManager->removeFrame(PFRAME); -} - -void CToplevelExportProtocolManager::removeFrame(SScreencopyFrame* frame, bool force) { - if (!frame) +CToplevelExportClient::CToplevelExportClient(SP resource_) : resource(resource_) { + if (!good()) return; - std::erase_if(m_vFramesAwaitingWrite, [&](const auto& other) { return other == frame; }); + resource->setOnDestroy([this](CHyprlandToplevelExportManagerV1* pMgr) { PROTO::toplevelExport->destroyResource(this); }); + resource->setDestroy([this](CHyprlandToplevelExportManagerV1* pMgr) { PROTO::toplevelExport->destroyResource(this); }); + resource->setCaptureToplevel([this](CHyprlandToplevelExportManagerV1* pMgr, uint32_t frame, int32_t overlayCursor, uint32_t handle) { + this->captureToplevel(pMgr, frame, overlayCursor, g_pCompositor->getWindowFromHandle(handle)); + }); + resource->setCaptureToplevelWithWlrToplevelHandle([this](CHyprlandToplevelExportManagerV1* pMgr, uint32_t frame, int32_t overlayCursor, wl_resource* handle) { + this->captureToplevel(pMgr, frame, overlayCursor, PROTO::foreignToplevelWlr->windowFromHandleResource(handle)); + }); - wl_resource_set_user_data(frame->resource, nullptr); - if (frame->buffer && frame->buffer->locked() > 0) - frame->buffer->unlock(); - removeClient(frame->client, force); - m_lFrames.remove(*frame); + lastMeasure.reset(); + lastFrame.reset(); + tickCallback = g_pHookSystem->hookDynamic("tick", [&](void* self, SCallbackInfo& info, std::any data) { onTick(); }); } -void CToplevelExportProtocolManager::captureToplevel(wl_client* client, wl_resource* resource, uint32_t frame, int32_t overlay_cursor, PHLWINDOW pWindow) { - const auto PCLIENT = clientFromResource(resource); - +void CToplevelExportClient::captureToplevel(CHyprlandToplevelExportManagerV1* pMgr, uint32_t frame, int32_t overlayCursor_, PHLWINDOW handle) { // create a frame - const auto PFRAME = &m_lFrames.emplace_back(); - PFRAME->overlayCursor = !!overlay_cursor; - PFRAME->resource = wl_resource_create(client, &hyprland_toplevel_export_frame_v1_interface, wl_resource_get_version(resource), frame); - PFRAME->pWindow = pWindow; + const auto FRAME = PROTO::toplevelExport->m_vFrames.emplace_back( + makeShared(makeShared(resource->client(), resource->version(), frame), overlayCursor_, handle)); + + if (!FRAME->good()) { + LOGM(ERR, "Couldn't alloc frame for sharing! (no memory)"); + resource->noMemory(); + PROTO::toplevelExport->destroyResource(FRAME.get()); + return; + } + + FRAME->self = FRAME; + FRAME->client = self; +} + +void CToplevelExportClient::onTick() { + if (lastMeasure.getMillis() < 500) + return; + + framesInLastHalfSecond = frameCounter; + frameCounter = 0; + lastMeasure.reset(); + + const auto LASTFRAMEDELTA = lastFrame.getMillis() / 1000.0; + const bool FRAMEAWAITING = std::ranges::any_of(PROTO::toplevelExport->m_vFrames, [&](const auto& frame) { return frame->client.get() == this; }); + + if (framesInLastHalfSecond > 3 && !sentScreencast) { + EMIT_HOOK_EVENT("screencast", (std::vector{1, (uint64_t)framesInLastHalfSecond, (uint64_t)clientOwner})); + g_pEventManager->postEvent(SHyprIPCEvent{"screencast", "1," + std::to_string(clientOwner)}); + sentScreencast = true; + } else if (framesInLastHalfSecond < 4 && sentScreencast && LASTFRAMEDELTA > 1.0 && !FRAMEAWAITING) { + EMIT_HOOK_EVENT("screencast", (std::vector{0, (uint64_t)framesInLastHalfSecond, (uint64_t)clientOwner})); + g_pEventManager->postEvent(SHyprIPCEvent{"screencast", "0," + std::to_string(clientOwner)}); + sentScreencast = false; + } +} + +bool CToplevelExportClient::good() { + return resource->resource(); +} + +CToplevelExportFrame::~CToplevelExportFrame() { + if (buffer && buffer->locked()) + buffer->unlock(); +} + +CToplevelExportFrame::CToplevelExportFrame(SP resource_, int32_t overlayCursor_, PHLWINDOW pWindow_) : resource(resource_), pWindow(pWindow_) { + if (!good()) + return; + + overlayCursor = !!overlayCursor_; if (!pWindow) { - Debug::log(ERR, "Client requested sharing of window handle {:x} which does not exist!", pWindow); - hyprland_toplevel_export_frame_v1_send_failed(PFRAME->resource); - removeFrame(PFRAME); + LOGM(ERR, "Client requested sharing of window handle {:x} which does not exist!", pWindow); + resource->sendFailed(); + PROTO::toplevelExport->destroyResource(this); return; } if (!pWindow->m_bIsMapped || pWindow->isHidden()) { - Debug::log(ERR, "Client requested sharing of window handle {:x} which is not shareable!", pWindow); - hyprland_toplevel_export_frame_v1_send_failed(PFRAME->resource); - removeFrame(PFRAME); + LOGM(ERR, "Client requested sharing of window handle {:x} which is not shareable!", pWindow); + resource->sendFailed(); + PROTO::toplevelExport->destroyResource(this); return; } - if (!PFRAME->resource) { - Debug::log(ERR, "Couldn't alloc frame for sharing! (no memory)"); - m_lFrames.remove(*PFRAME); - wl_client_post_no_memory(client); - return; - } - - wl_resource_set_implementation(PFRAME->resource, &toplevelFrameImpl, PFRAME, handleFrameResourceDestroy); - - PFRAME->client = PCLIENT; - PCLIENT->ref++; + resource->setOnDestroy([this](CHyprlandToplevelExportFrameV1* pFrame) { PROTO::toplevelExport->destroyResource(this); }); + resource->setDestroy([this](CHyprlandToplevelExportFrameV1* pFrame) { PROTO::toplevelExport->destroyResource(this); }); + resource->setCopy([this](CHyprlandToplevelExportFrameV1* pFrame, wl_resource* res, int32_t ignoreDamage) { this->copy(pFrame, res, ignoreDamage); }); const auto PMONITOR = g_pCompositor->getMonitorFromID(pWindow->m_iMonitorID); g_pHyprRenderer->makeEGLCurrent(); - PFRAME->shmFormat = g_pHyprOpenGL->getPreferredReadFormat(PMONITOR); - if (PFRAME->shmFormat == DRM_FORMAT_INVALID) { - Debug::log(ERR, "No format supported by renderer in capture toplevel"); - hyprland_toplevel_export_frame_v1_send_failed(resource); - removeFrame(PFRAME); + shmFormat = g_pHyprOpenGL->getPreferredReadFormat(PMONITOR); + if (shmFormat == DRM_FORMAT_INVALID) { + LOGM(ERR, "No format supported by renderer in capture toplevel"); + resource->sendFailed(); + PROTO::toplevelExport->destroyResource(this); return; } - const auto PSHMINFO = FormatUtils::getPixelFormatFromDRM(PFRAME->shmFormat); + const auto PSHMINFO = FormatUtils::getPixelFormatFromDRM(shmFormat); if (!PSHMINFO) { - Debug::log(ERR, "No pixel format supported by renderer in capture toplevel"); - hyprland_toplevel_export_frame_v1_send_failed(resource); - removeFrame(PFRAME); + LOGM(ERR, "No pixel format supported by renderer in capture toplevel"); + resource->sendFailed(); + PROTO::toplevelExport->destroyResource(this); return; } - PFRAME->dmabufFormat = PMONITOR->output->state->state().drmFormat; + dmabufFormat = PMONITOR->output->state->state().drmFormat; - PFRAME->box = {0, 0, (int)(pWindow->m_vRealSize.value().x * PMONITOR->scale), (int)(pWindow->m_vRealSize.value().y * PMONITOR->scale)}; + box = {0, 0, (int)(pWindow->m_vRealSize.value().x * PMONITOR->scale), (int)(pWindow->m_vRealSize.value().y * PMONITOR->scale)}; - PFRAME->box.transform(wlTransformToHyprutils(PMONITOR->transform), PMONITOR->vecTransformedSize.x, PMONITOR->vecTransformedSize.y).round(); + box.transform(wlTransformToHyprutils(PMONITOR->transform), PMONITOR->vecTransformedSize.x, PMONITOR->vecTransformedSize.y).round(); - PFRAME->shmStride = FormatUtils::minStride(PSHMINFO, PFRAME->box.w); + shmStride = FormatUtils::minStride(PSHMINFO, box.w); - hyprland_toplevel_export_frame_v1_send_buffer(PFRAME->resource, FormatUtils::drmToShm(PFRAME->shmFormat), PFRAME->box.width, PFRAME->box.height, PFRAME->shmStride); + resource->sendBuffer(FormatUtils::drmToShm(shmFormat), box.width, box.height, shmStride); - if (PFRAME->dmabufFormat != DRM_FORMAT_INVALID) { - hyprland_toplevel_export_frame_v1_send_linux_dmabuf(PFRAME->resource, PFRAME->dmabufFormat, PFRAME->box.width, PFRAME->box.height); + if (dmabufFormat != DRM_FORMAT_INVALID) { + resource->sendLinuxDmabuf(dmabufFormat, box.width, box.height); } - hyprland_toplevel_export_frame_v1_send_buffer_done(PFRAME->resource); + resource->sendBufferDone(); } -void CToplevelExportProtocolManager::copyFrame(wl_client* client, wl_resource* resource, wl_resource* buffer, int32_t ignore_damage) { - const auto PFRAME = frameFromResource(resource); - - if (!PFRAME) { - Debug::log(ERR, "No frame in copyFrame??"); +void CToplevelExportFrame::copy(CHyprlandToplevelExportFrameV1* pFrame, wl_resource* buffer_, int32_t ignoreDamage) { + if (!good()) { + LOGM(ERR, "No frame in copyFrame??"); return; } - const auto PWINDOW = PFRAME->pWindow.lock(); - - if (!validMapped(PWINDOW)) { - Debug::log(ERR, "Client requested sharing of window handle {:x} which is gone!", PWINDOW); - hyprland_toplevel_export_frame_v1_send_failed(PFRAME->resource); - removeFrame(PFRAME); + if (!validMapped(pWindow)) { + LOGM(ERR, "Client requested sharing of window handle {:x} which is gone!", pWindow); + resource->sendFailed(); + PROTO::toplevelExport->destroyResource(this); return; } - if (!PWINDOW->m_bIsMapped || PWINDOW->isHidden()) { - Debug::log(ERR, "Client requested sharing of window handle {:x} which is not shareable (2)!", PWINDOW); - hyprland_toplevel_export_frame_v1_send_failed(PFRAME->resource); - removeFrame(PFRAME); + if (!pWindow->m_bIsMapped || pWindow->isHidden()) { + LOGM(ERR, "Client requested sharing of window handle {:x} which is not shareable (2)!", pWindow); + resource->sendFailed(); + PROTO::toplevelExport->destroyResource(this); return; } - const auto PBUFFER = CWLBufferResource::fromResource(buffer); + const auto PBUFFER = CWLBufferResource::fromResource(buffer_); if (!PBUFFER) { - wl_resource_post_error(PFRAME->resource, HYPRLAND_TOPLEVEL_EXPORT_FRAME_V1_ERROR_INVALID_BUFFER, "invalid buffer"); - removeFrame(PFRAME); + resource->error(HYPRLAND_TOPLEVEL_EXPORT_FRAME_V1_ERROR_INVALID_BUFFER, "invalid buffer"); + PROTO::toplevelExport->destroyResource(this); return; } PBUFFER->buffer->lock(); - if (PBUFFER->buffer->size != PFRAME->box.size()) { - wl_resource_post_error(PFRAME->resource, HYPRLAND_TOPLEVEL_EXPORT_FRAME_V1_ERROR_INVALID_BUFFER, "invalid buffer dimensions"); - removeFrame(PFRAME); + if (PBUFFER->buffer->size != box.size()) { + resource->error(HYPRLAND_TOPLEVEL_EXPORT_FRAME_V1_ERROR_INVALID_BUFFER, "invalid buffer dimensions"); + PROTO::toplevelExport->destroyResource(this); return; } - if (PFRAME->buffer) { - wl_resource_post_error(PFRAME->resource, HYPRLAND_TOPLEVEL_EXPORT_FRAME_V1_ERROR_ALREADY_USED, "frame already used"); - removeFrame(PFRAME); + if (buffer) { + resource->error(HYPRLAND_TOPLEVEL_EXPORT_FRAME_V1_ERROR_ALREADY_USED, "frame already used"); + PROTO::toplevelExport->destroyResource(this); return; } if (auto attrs = PBUFFER->buffer->dmabuf(); attrs.success) { - PFRAME->bufferDMA = true; + bufferDMA = true; - if (attrs.format != PFRAME->dmabufFormat) { - wl_resource_post_error(PFRAME->resource, HYPRLAND_TOPLEVEL_EXPORT_FRAME_V1_ERROR_INVALID_BUFFER, "invalid buffer format"); - removeFrame(PFRAME); + if (attrs.format != dmabufFormat) { + resource->error(HYPRLAND_TOPLEVEL_EXPORT_FRAME_V1_ERROR_INVALID_BUFFER, "invalid buffer format"); + PROTO::toplevelExport->destroyResource(this); return; } } else if (auto attrs = PBUFFER->buffer->shm(); attrs.success) { - if (attrs.format != PFRAME->shmFormat) { - wl_resource_post_error(PFRAME->resource, HYPRLAND_TOPLEVEL_EXPORT_FRAME_V1_ERROR_INVALID_BUFFER, "invalid buffer format"); - removeFrame(PFRAME); + if (attrs.format != shmFormat) { + resource->error(HYPRLAND_TOPLEVEL_EXPORT_FRAME_V1_ERROR_INVALID_BUFFER, "invalid buffer format"); + PROTO::toplevelExport->destroyResource(this); return; - } else if ((int)attrs.stride != PFRAME->shmStride) { - wl_resource_post_error(PFRAME->resource, HYPRLAND_TOPLEVEL_EXPORT_FRAME_V1_ERROR_INVALID_BUFFER, "invalid buffer stride"); - removeFrame(PFRAME); + } else if ((int)attrs.stride != shmStride) { + resource->error(HYPRLAND_TOPLEVEL_EXPORT_FRAME_V1_ERROR_INVALID_BUFFER, "invalid buffer stride"); + PROTO::toplevelExport->destroyResource(this); return; } } else { - wl_resource_post_error(PFRAME->resource, HYPRLAND_TOPLEVEL_EXPORT_FRAME_V1_ERROR_INVALID_BUFFER, "invalid buffer type"); - removeFrame(PFRAME); + resource->error(HYPRLAND_TOPLEVEL_EXPORT_FRAME_V1_ERROR_INVALID_BUFFER, "invalid buffer type"); + PROTO::toplevelExport->destroyResource(this); return; } - PFRAME->buffer = PBUFFER->buffer; + buffer = PBUFFER->buffer; - m_vFramesAwaitingWrite.emplace_back(PFRAME); + PROTO::toplevelExport->m_vFramesAwaitingWrite.emplace_back(self); } -void CToplevelExportProtocolManager::onOutputCommit(CMonitor* pMonitor) { - if (m_vFramesAwaitingWrite.empty()) - return; // nothing to share - - std::vector framesToRemove; - - // share frame if correct output - for (auto& f : m_vFramesAwaitingWrite) { - const auto PWINDOW = f->pWindow.lock(); - - if (!validMapped(PWINDOW)) { - framesToRemove.push_back(f); - continue; - } - - if (pMonitor != g_pCompositor->getMonitorFromID(PWINDOW->m_iMonitorID)) - continue; - - CBox geometry = {PWINDOW->m_vRealPosition.value().x, PWINDOW->m_vRealPosition.value().y, PWINDOW->m_vRealSize.value().x, PWINDOW->m_vRealSize.value().y}; - - if (geometry.intersection({pMonitor->vecPosition, pMonitor->vecSize}).empty()) - continue; - - shareFrame(f); - - f->client->lastFrame.reset(); - ++f->client->frameCounter; - - framesToRemove.push_back(f); - } - - for (auto& f : framesToRemove) { - removeFrame(f); - } -} - -void CToplevelExportProtocolManager::shareFrame(SScreencopyFrame* frame) { - if (!frame->buffer || !validMapped(frame->pWindow)) +void CToplevelExportFrame::share() { + if (!buffer || !validMapped(pWindow)) return; timespec now; clock_gettime(CLOCK_MONOTONIC, &now); - uint32_t flags = 0; - if (frame->bufferDMA) { - if (!copyFrameDmabuf(frame, &now)) { - hyprland_toplevel_export_frame_v1_send_failed(frame->resource); + if (bufferDMA) { + if (!copyDmabuf(&now)) { + resource->sendFailed(); return; } } else { - if (!copyFrameShm(frame, &now)) { - hyprland_toplevel_export_frame_v1_send_failed(frame->resource); + if (!copyShm(&now)) { + resource->sendFailed(); return; } } - hyprland_toplevel_export_frame_v1_send_flags(frame->resource, flags); - sendDamage(frame); + resource->sendFlags((hyprlandToplevelExportFrameV1Flags)0); + + if (!ignoreDamage) { + resource->sendDamage(0, 0, box.width, box.height); + } + uint32_t tvSecHi = (sizeof(now.tv_sec) > 4) ? now.tv_sec >> 32 : 0; uint32_t tvSecLo = now.tv_sec & 0xFFFFFFFF; - hyprland_toplevel_export_frame_v1_send_ready(frame->resource, tvSecHi, tvSecLo, now.tv_nsec); + resource->sendReady(tvSecHi, tvSecLo, now.tv_nsec); } -void CToplevelExportProtocolManager::sendDamage(SScreencopyFrame* frame) { - // TODO: send proper dmg - hyprland_toplevel_export_frame_v1_send_damage(frame->resource, 0, 0, frame->box.width, frame->box.height); -} - -bool CToplevelExportProtocolManager::copyFrameShm(SScreencopyFrame* frame, timespec* now) { - auto shm = frame->buffer->shm(); - auto [pixelData, fmt, bufLen] = frame->buffer->beginDataPtr(0); // no need for end, cuz it's shm +bool CToplevelExportFrame::copyShm(timespec* now) { + auto shm = buffer->shm(); + auto [pixelData, fmt, bufLen] = buffer->beginDataPtr(0); // no need for end, cuz it's shm // render the client - const auto PMONITOR = g_pCompositor->getMonitorFromID(frame->pWindow->m_iMonitorID); + const auto PMONITOR = g_pCompositor->getMonitorFromID(pWindow->m_iMonitorID); CRegion fakeDamage{0, 0, PMONITOR->vecPixelSize.x * 10, PMONITOR->vecPixelSize.y * 10}; g_pHyprRenderer->makeEGLCurrent(); @@ -372,7 +249,7 @@ bool CToplevelExportProtocolManager::copyFrameShm(SScreencopyFrame* frame, times CFramebuffer outFB; outFB.alloc(PMONITOR->vecPixelSize.x, PMONITOR->vecPixelSize.y, g_pHyprRenderer->isNvidia() ? DRM_FORMAT_XBGR8888 : PMONITOR->output->state->state().drmFormat); - if (frame->overlayCursor) { + if (overlayCursor) { g_pPointerManager->lockSoftwareForMonitor(PMONITOR->self.lock()); g_pPointerManager->damageCursor(PMONITOR->self.lock()); } @@ -383,12 +260,12 @@ bool CToplevelExportProtocolManager::copyFrameShm(SScreencopyFrame* frame, times g_pHyprOpenGL->clear(CColor(0, 0, 0, 1.0)); // render client at 0,0 - g_pHyprRenderer->m_bBlockSurfaceFeedback = g_pHyprRenderer->shouldRenderWindow(frame->pWindow.lock()); // block the feedback to avoid spamming the surface if it's visible - g_pHyprRenderer->renderWindow(frame->pWindow.lock(), PMONITOR, now, false, RENDER_PASS_ALL, true, true); + g_pHyprRenderer->m_bBlockSurfaceFeedback = g_pHyprRenderer->shouldRenderWindow(pWindow); // block the feedback to avoid spamming the surface if it's visible + g_pHyprRenderer->renderWindow(pWindow, PMONITOR, now, false, RENDER_PASS_ALL, true, true); g_pHyprRenderer->m_bBlockSurfaceFeedback = false; - if (frame->overlayCursor) - g_pPointerManager->renderSoftwareCursorsFor(PMONITOR->self.lock(), now, fakeDamage, g_pInputManager->getMouseCoordsInternal() - frame->pWindow->m_vRealPosition.value()); + if (overlayCursor) + g_pPointerManager->renderSoftwareCursorsFor(PMONITOR->self.lock(), now, fakeDamage, g_pInputManager->getMouseCoordsInternal() - pWindow->m_vRealPosition.value()); const auto PFORMAT = FormatUtils::getPixelFormatFromDRM(shm.format); if (!PFORMAT) { @@ -410,9 +287,9 @@ bool CToplevelExportProtocolManager::copyFrameShm(SScreencopyFrame* frame, times glPixelStorei(GL_PACK_ALIGNMENT, 1); auto glFormat = PFORMAT->flipRB ? GL_BGRA_EXT : GL_RGBA; - glReadPixels(0, 0, frame->box.width, frame->box.height, glFormat, PFORMAT->glType, pixelData); + glReadPixels(0, 0, box.width, box.height, glFormat, PFORMAT->glType, pixelData); - if (frame->overlayCursor) { + if (overlayCursor) { g_pPointerManager->unlockSoftwareForMonitor(PMONITOR->self.lock()); g_pPointerManager->damageCursor(PMONITOR->self.lock()); } @@ -420,31 +297,112 @@ bool CToplevelExportProtocolManager::copyFrameShm(SScreencopyFrame* frame, times return true; } -bool CToplevelExportProtocolManager::copyFrameDmabuf(SScreencopyFrame* frame, timespec* now) { - const auto PMONITOR = g_pCompositor->getMonitorFromID(frame->pWindow->m_iMonitorID); +bool CToplevelExportFrame::copyDmabuf(timespec* now) { + const auto PMONITOR = g_pCompositor->getMonitorFromID(pWindow->m_iMonitorID); CRegion fakeDamage{0, 0, INT16_MAX, INT16_MAX}; - if (!g_pHyprRenderer->beginRender(PMONITOR, fakeDamage, RENDER_MODE_TO_BUFFER, frame->buffer.lock())) + if (overlayCursor) { + g_pPointerManager->lockSoftwareForMonitor(PMONITOR->self.lock()); + g_pPointerManager->damageCursor(PMONITOR->self.lock()); + } + + if (!g_pHyprRenderer->beginRender(PMONITOR, fakeDamage, RENDER_MODE_TO_BUFFER, buffer.lock())) return false; g_pHyprOpenGL->clear(CColor(0, 0, 0, 1.0)); - g_pHyprRenderer->m_bBlockSurfaceFeedback = g_pHyprRenderer->shouldRenderWindow(frame->pWindow.lock()); // block the feedback to avoid spamming the surface if it's visible - g_pHyprRenderer->renderWindow(frame->pWindow.lock(), PMONITOR, now, false, RENDER_PASS_ALL, true, true); + g_pHyprRenderer->m_bBlockSurfaceFeedback = g_pHyprRenderer->shouldRenderWindow(pWindow); // block the feedback to avoid spamming the surface if it's visible + g_pHyprRenderer->renderWindow(pWindow, PMONITOR, now, false, RENDER_PASS_ALL, true, true); g_pHyprRenderer->m_bBlockSurfaceFeedback = false; - if (frame->overlayCursor) - g_pPointerManager->renderSoftwareCursorsFor(PMONITOR->self.lock(), now, fakeDamage, g_pInputManager->getMouseCoordsInternal() - frame->pWindow->m_vRealPosition.value()); + if (overlayCursor) + g_pPointerManager->renderSoftwareCursorsFor(PMONITOR->self.lock(), now, fakeDamage, g_pInputManager->getMouseCoordsInternal() - pWindow->m_vRealPosition.value()); g_pHyprOpenGL->m_RenderData.blockScreenShader = true; g_pHyprRenderer->endRender(); + + if (overlayCursor) { + g_pPointerManager->unlockSoftwareForMonitor(PMONITOR->self.lock()); + g_pPointerManager->damageCursor(PMONITOR->self.lock()); + } + return true; } -void CToplevelExportProtocolManager::onWindowUnmap(PHLWINDOW pWindow) { - for (auto& f : m_lFrames) { - if (f.pWindow.lock() == pWindow) - f.pWindow.reset(); +bool CToplevelExportFrame::good() { + return resource->resource(); +} + +CToplevelExportProtocol::CToplevelExportProtocol(const wl_interface* iface, const int& ver, const std::string& name) : IWaylandProtocol(iface, ver, name) { + ; +} + +void CToplevelExportProtocol::bindManager(wl_client* client, void* data, uint32_t ver, uint32_t id) { + const auto CLIENT = m_vClients.emplace_back(makeShared(makeShared(client, ver, id))); + + if (!CLIENT->good()) { + LOGM(LOG, "Failed to bind client! (out of memory)"); + wl_client_post_no_memory(client); + m_vClients.pop_back(); + return; + } + + CLIENT->self = CLIENT; + + LOGM(LOG, "Bound client successfully!"); +} + +void CToplevelExportProtocol::destroyResource(CToplevelExportClient* client) { + std::erase_if(m_vClients, [&](const auto& other) { return other.get() == client; }); + std::erase_if(m_vFrames, [&](const auto& other) { return other->client.get() == client; }); + std::erase_if(m_vFramesAwaitingWrite, [&](const auto& other) { return other->client.get() == client; }); +} + +void CToplevelExportProtocol::destroyResource(CToplevelExportFrame* frame) { + std::erase_if(m_vFrames, [&](const auto& other) { return other.get() == frame; }); + std::erase_if(m_vFramesAwaitingWrite, [&](const auto& other) { return other.get() == frame; }); +} + +void CToplevelExportProtocol::onOutputCommit(CMonitor* pMonitor) { + if (m_vFramesAwaitingWrite.empty()) + return; // nothing to share + + std::vector> framesToRemove; + + // share frame if correct output + for (auto& f : m_vFramesAwaitingWrite) { + if (!f->pWindow || !validMapped(f->pWindow)) { + framesToRemove.push_back(f); + continue; + } + + const auto PWINDOW = f->pWindow; + + if (pMonitor != g_pCompositor->getMonitorFromID(PWINDOW->m_iMonitorID)) + continue; + + CBox geometry = {PWINDOW->m_vRealPosition.value().x, PWINDOW->m_vRealPosition.value().y, PWINDOW->m_vRealSize.value().x, PWINDOW->m_vRealSize.value().y}; + + if (geometry.intersection({pMonitor->vecPosition, pMonitor->vecSize}).empty()) + continue; + + f->share(); + + f->client->lastFrame.reset(); + ++f->client->frameCounter; + + framesToRemove.push_back(f); + } + + for (auto& f : framesToRemove) { + destroyResource(f.get()); + } +} + +void CToplevelExportProtocol::onWindowUnmap(PHLWINDOW pWindow) { + for (auto& f : m_vFrames) { + if (f->pWindow == pWindow) + f->pWindow.reset(); } } diff --git a/src/protocols/ToplevelExport.hpp b/src/protocols/ToplevelExport.hpp index 71a6df18..638b69f0 100644 --- a/src/protocols/ToplevelExport.hpp +++ b/src/protocols/ToplevelExport.hpp @@ -1,7 +1,8 @@ #pragma once #include "../defines.hpp" -#include "hyprland-toplevel-export-v1-protocol.h" +#include "hyprland-toplevel-export-v1.hpp" +#include "WaylandProtocol.hpp" #include "Screencopy.hpp" #include @@ -10,33 +11,91 @@ class CMonitor; class CWindow; -class CToplevelExportProtocolManager { +class CToplevelExportClient { public: - CToplevelExportProtocolManager(); - ~CToplevelExportProtocolManager(); + CToplevelExportClient(SP resource_); - void bindManager(wl_client* client, void* data, uint32_t version, uint32_t id); - void captureToplevel(wl_client* client, wl_resource* resource, uint32_t frame, int32_t overlay_cursor, PHLWINDOW handle); - void removeClient(CScreencopyClient* client, bool force = false); - void removeFrame(SScreencopyFrame* frame, bool force = false); - void copyFrame(wl_client* client, wl_resource* resource, wl_resource* buffer, int32_t ignore_damage); - void displayDestroy(); - void onWindowUnmap(PHLWINDOW pWindow); - void onOutputCommit(CMonitor* pMonitor); + bool good(); - wl_listener m_liDisplayDestroy; + WP self; + eClientOwners clientOwner = CLIENT_TOPLEVEL_EXPORT; + + CTimer lastFrame; + int frameCounter = 0; private: - wl_global* m_pGlobal = nullptr; - std::list m_lFrames; - std::list m_lClients; + SP resource; - std::vector m_vFramesAwaitingWrite; + int framesInLastHalfSecond = 0; + CTimer lastMeasure; + bool sentScreencast = false; - void shareFrame(SScreencopyFrame* frame); - bool copyFrameDmabuf(SScreencopyFrame* frame, timespec* now); - bool copyFrameShm(SScreencopyFrame* frame, timespec* now); - void sendDamage(SScreencopyFrame* frame); + SP tickCallback; + void onTick(); - friend class CScreencopyClient; + void captureToplevel(CHyprlandToplevelExportManagerV1* pMgr, uint32_t frame, int32_t overlayCursor, PHLWINDOW handle); + + friend class CToplevelExportProtocol; +}; + +class CToplevelExportFrame { + public: + CToplevelExportFrame(SP resource_, int32_t overlayCursor, PHLWINDOW pWindow); + ~CToplevelExportFrame(); + + bool good(); + + SP self; + WP client; + + private: + SP resource; + + PHLWINDOW pWindow; + bool overlayCursor = false; + bool ignoreDamage = false; + bool lockedSWCursors = false; + + WP buffer; + bool bufferDMA = false; + uint32_t shmFormat = 0; + uint32_t dmabufFormat = 0; + int shmStride = 0; + CBox box = {}; + + void copy(CHyprlandToplevelExportFrameV1* pFrame, wl_resource* buffer, int32_t ignoreDamage); + bool copyDmabuf(timespec* now); + bool copyShm(timespec* now); + void share(); + + friend class CToplevelExportProtocol; +}; + +class CToplevelExportProtocol : IWaylandProtocol { + public: + CToplevelExportProtocol(const wl_interface* iface, const int& ver, const std::string& name); + + void bindManager(wl_client* client, void* data, uint32_t ver, uint32_t id); + void destroyResource(CToplevelExportClient* client); + void destroyResource(CToplevelExportFrame* frame); + + void onWindowUnmap(PHLWINDOW pWindow); + void onOutputCommit(CMonitor* pMonitor); + + private: + std::vector> m_vClients; + std::vector> m_vFrames; + std::vector> m_vFramesAwaitingWrite; + + void shareFrame(CToplevelExportFrame* frame); + bool copyFrameDmabuf(CToplevelExportFrame* frame, timespec* now); + bool copyFrameShm(CToplevelExportFrame* frame, timespec* now); + void sendDamage(CToplevelExportFrame* frame); + + friend class CToplevelExportClient; + friend class CToplevelExportFrame; +}; + +namespace PROTO { + inline UP toplevelExport; }; diff --git a/src/render/Renderer.hpp b/src/render/Renderer.hpp index 72efe8c4..a9397cac 100644 --- a/src/render/Renderer.hpp +++ b/src/render/Renderer.hpp @@ -139,7 +139,7 @@ class CHyprRenderer { std::vector> m_vRenderbuffers; friend class CHyprOpenGLImpl; - friend class CToplevelExportProtocolManager; + friend class CToplevelExportFrame; friend class CInputManager; friend class CPointerManager; };