protocols: move screencopy and toplevel export to hyprwayland-scanner (#7065)

* move screencopy and toplevel export to hyprwayland-scanner

* oops
This commit is contained in:
Ikalco 2024-07-27 10:02:02 -05:00 committed by GitHub
parent 963816b9a6
commit ec672b1ab9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 695 additions and 798 deletions

View file

@ -288,17 +288,16 @@ endfunction()
target_link_libraries(Hyprland OpenGL::EGL OpenGL::GL Threads::Threads target_link_libraries(Hyprland OpenGL::EGL OpenGL::GL Threads::Threads
libudis86 uuid) libudis86 uuid)
protocol("protocols/wlr-screencopy-unstable-v1.xml"
"wlr-screencopy-unstable-v1" true)
protocol( protocol(
"subprojects/hyprland-protocols/protocols/hyprland-global-shortcuts-v1.xml" "subprojects/hyprland-protocols/protocols/hyprland-global-shortcuts-v1.xml"
"hyprland-global-shortcuts-v1" true) "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-gamma-control-unstable-v1" true)
protocolnew("protocols" "wlr-foreign-toplevel-management-unstable-v1" true) protocolnew("protocols" "wlr-foreign-toplevel-management-unstable-v1" true)
protocolnew("protocols" "wlr-output-power-management-unstable-v1" true) protocolnew("protocols" "wlr-output-power-management-unstable-v1" true)

View file

@ -25,8 +25,6 @@ hyprwayland_scanner = find_program(
protocols = [ protocols = [
[wl_protocol_dir, 'unstable/text-input/text-input-unstable-v1.xml'], [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'] [hl_protocol_dir, 'protocols/hyprland-global-shortcuts-v1.xml']
] ]
@ -42,6 +40,8 @@ new_protocols = [
['wlr-layer-shell-unstable-v1.xml'], ['wlr-layer-shell-unstable-v1.xml'],
['wayland-drm.xml'], ['wayland-drm.xml'],
['wlr-data-control-unstable-v1.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'], [hl_protocol_dir, 'protocols/hyprland-focus-grab-v1.xml'],
[wl_protocol_dir, 'staging/tearing-control/tearing-control-v1.xml'], [wl_protocol_dir, 'staging/tearing-control/tearing-control-v1.xml'],
[wl_protocol_dir, 'staging/fractional-scale/fractional-scale-v1.xml'], [wl_protocol_dir, 'staging/fractional-scale/fractional-scale-v1.xml'],

View file

@ -5,6 +5,8 @@
#include "Events.hpp" #include "Events.hpp"
#include "../debug/HyprCtl.hpp" #include "../debug/HyprCtl.hpp"
#include "../config/ConfigValue.hpp" #include "../config/ConfigValue.hpp"
#include "../protocols/Screencopy.hpp"
#include "../protocols/ToplevelExport.hpp"
#include <aquamarine/output/Output.hpp> #include <aquamarine/output/Output.hpp>
// --------------------------------------------------------- // // --------------------------------------------------------- //
@ -118,8 +120,8 @@ void Events::listener_monitorCommit(void* owner, void* data) {
const auto PMONITOR = (CMonitor*)owner; const auto PMONITOR = (CMonitor*)owner;
if (true) { // FIXME: E->state->committed & WLR_OUTPUT_STATE_BUFFER if (true) { // FIXME: E->state->committed & WLR_OUTPUT_STATE_BUFFER
g_pProtocolManager->m_pScreencopyProtocolManager->onOutputCommit(PMONITOR); PROTO::screencopy->onOutputCommit(PMONITOR);
g_pProtocolManager->m_pToplevelExportProtocolManager->onOutputCommit(PMONITOR); PROTO::toplevelExport->onOutputCommit(PMONITOR);
} }
} }

View file

@ -10,6 +10,7 @@
#include "../protocols/LayerShell.hpp" #include "../protocols/LayerShell.hpp"
#include "../protocols/XDGShell.hpp" #include "../protocols/XDGShell.hpp"
#include "../protocols/core/Compositor.hpp" #include "../protocols/core/Compositor.hpp"
#include "../protocols/ToplevelExport.hpp"
#include "../xwayland/XSurface.hpp" #include "../xwayland/XSurface.hpp"
#include <hyprutils/string/String.hpp> #include <hyprutils/string/String.hpp>
@ -601,7 +602,7 @@ void Events::listener_unmapWindow(void* owner, void* data) {
g_pEventManager->postEvent(SHyprIPCEvent{"closewindow", std::format("{:x}", PWINDOW)}); g_pEventManager->postEvent(SHyprIPCEvent{"closewindow", std::format("{:x}", PWINDOW)});
EMIT_HOOK_EVENT("closeWindow", PWINDOW); EMIT_HOOK_EVENT("closeWindow", PWINDOW);
g_pProtocolManager->m_pToplevelExportProtocolManager->onWindowUnmap(PWINDOW); PROTO::toplevelExport->onWindowUnmap(PWINDOW);
if (PWINDOW->m_bIsFullscreen) if (PWINDOW->m_bIsFullscreen)
g_pCompositor->setWindowFullscreen(PWINDOW, false, FULLSCREEN_FULL); g_pCompositor->setWindowFullscreen(PWINDOW, false, FULLSCREEN_FULL);

View file

@ -39,6 +39,8 @@
#include "../protocols/LinuxDMABUF.hpp" #include "../protocols/LinuxDMABUF.hpp"
#include "../protocols/DRMLease.hpp" #include "../protocols/DRMLease.hpp"
#include "../protocols/DRMSyncobj.hpp" #include "../protocols/DRMSyncobj.hpp"
#include "../protocols/Screencopy.hpp"
#include "../protocols/ToplevelExport.hpp"
#include "../protocols/core/Seat.hpp" #include "../protocols/core/Seat.hpp"
#include "../protocols/core/DataDevice.hpp" #include "../protocols/core/DataDevice.hpp"
@ -142,6 +144,8 @@ CProtocolManager::CProtocolManager() {
PROTO::dataWlr = std::make_unique<CDataDeviceWLRProtocol>(&zwlr_data_control_manager_v1_interface, 2, "DataDeviceWlr"); PROTO::dataWlr = std::make_unique<CDataDeviceWLRProtocol>(&zwlr_data_control_manager_v1_interface, 2, "DataDeviceWlr");
PROTO::primarySelection = std::make_unique<CPrimarySelectionProtocol>(&zwp_primary_selection_device_manager_v1_interface, 1, "PrimarySelection"); PROTO::primarySelection = std::make_unique<CPrimarySelectionProtocol>(&zwp_primary_selection_device_manager_v1_interface, 1, "PrimarySelection");
PROTO::xwaylandShell = std::make_unique<CXWaylandShellProtocol>(&xwayland_shell_v1_interface, 1, "XWaylandShell"); PROTO::xwaylandShell = std::make_unique<CXWaylandShellProtocol>(&xwayland_shell_v1_interface, 1, "XWaylandShell");
PROTO::screencopy = std::make_unique<CScreencopyProtocol>(&zwlr_screencopy_manager_v1_interface, 3, "Screencopy");
PROTO::toplevelExport = std::make_unique<CToplevelExportProtocol>(&hyprland_toplevel_export_manager_v1_interface, 2, "ToplevelExport");
for (auto& b : g_pCompositor->m_pAqBackend->getImplementations()) { for (auto& b : g_pCompositor->m_pAqBackend->getImplementations()) {
if (b->type() != Aquamarine::AQ_BACKEND_DRM) if (b->type() != Aquamarine::AQ_BACKEND_DRM)
@ -161,10 +165,8 @@ CProtocolManager::CProtocolManager() {
// Old protocol implementations. // Old protocol implementations.
// TODO: rewrite them to use hyprwayland-scanner. // TODO: rewrite them to use hyprwayland-scanner.
m_pToplevelExportProtocolManager = std::make_unique<CToplevelExportProtocolManager>();
m_pTextInputV1ProtocolManager = std::make_unique<CTextInputV1ProtocolManager>(); m_pTextInputV1ProtocolManager = std::make_unique<CTextInputV1ProtocolManager>();
m_pGlobalShortcutsProtocolManager = std::make_unique<CGlobalShortcutsProtocolManager>(); m_pGlobalShortcutsProtocolManager = std::make_unique<CGlobalShortcutsProtocolManager>();
m_pScreencopyProtocolManager = std::make_unique<CScreencopyProtocolManager>();
} }
CProtocolManager::~CProtocolManager() { CProtocolManager::~CProtocolManager() {
@ -214,6 +216,8 @@ CProtocolManager::~CProtocolManager() {
PROTO::dataWlr.reset(); PROTO::dataWlr.reset();
PROTO::primarySelection.reset(); PROTO::primarySelection.reset();
PROTO::xwaylandShell.reset(); PROTO::xwaylandShell.reset();
PROTO::screencopy.reset();
PROTO::toplevelExport.reset();
PROTO::lease.reset(); PROTO::lease.reset();
PROTO::sync.reset(); PROTO::sync.reset();

View file

@ -1,10 +1,9 @@
#pragma once #pragma once
#include "../defines.hpp" #include "../defines.hpp"
#include "../protocols/ToplevelExport.hpp"
#include "../protocols/TextInputV1.hpp" #include "../protocols/TextInputV1.hpp"
#include "../protocols/GlobalShortcuts.hpp" #include "../protocols/GlobalShortcuts.hpp"
#include "../protocols/Screencopy.hpp" #include "../helpers/Monitor.hpp"
#include "../helpers/memory/Memory.hpp" #include "../helpers/memory/Memory.hpp"
#include "../helpers/signal/Signal.hpp" #include "../helpers/signal/Signal.hpp"
#include <unordered_map> #include <unordered_map>
@ -15,10 +14,8 @@ class CProtocolManager {
~CProtocolManager(); ~CProtocolManager();
// TODO: rewrite to use the new protocol framework // TODO: rewrite to use the new protocol framework
std::unique_ptr<CToplevelExportProtocolManager> m_pToplevelExportProtocolManager;
std::unique_ptr<CTextInputV1ProtocolManager> m_pTextInputV1ProtocolManager; std::unique_ptr<CTextInputV1ProtocolManager> m_pTextInputV1ProtocolManager;
std::unique_ptr<CGlobalShortcutsProtocolManager> m_pGlobalShortcutsProtocolManager; std::unique_ptr<CGlobalShortcutsProtocolManager> m_pGlobalShortcutsProtocolManager;
std::unique_ptr<CScreencopyProtocolManager> m_pScreencopyProtocolManager;
private: private:
std::unordered_map<std::string, CHyprSignalListener> m_mModeChangeListeners; std::unordered_map<std::string, CHyprSignalListener> m_mModeChangeListeners;

View file

@ -9,245 +9,39 @@
#include <algorithm> #include <algorithm>
#define SCREENCOPY_VERSION 3 #define LOGM PROTO::screencopy->protoLog
static void bindManagerInt(wl_client* client, void* data, uint32_t version, uint32_t id) { CScreencopyFrame::~CScreencopyFrame() {
g_pProtocolManager->m_pScreencopyProtocolManager->bindManager(client, data, version, id); if (buffer && buffer->locked())
buffer->unlock();
} }
static void handleDisplayDestroy(struct wl_listener* listener, void* data) { CScreencopyFrame::CScreencopyFrame(SP<CZwlrScreencopyFrameV1> resource_, int32_t overlay_cursor, wl_resource* output, CBox box_) : resource(resource_) {
CScreencopyProtocolManager* proto = wl_container_of(listener, proto, m_liDisplayDestroy); if (!good())
proto->displayDestroy(); return;
}
void CScreencopyProtocolManager::displayDestroy() { overlayCursor = !!overlay_cursor;
wl_list_remove(&m_liDisplayDestroy.link); pMonitor = CWLOutputResource::fromResource(output)->monitor.get();
wl_list_init(&m_liDisplayDestroy.link);
wl_global_destroy(m_pGlobal);
}
static SScreencopyFrame* frameFromResource(wl_resource*); if (!pMonitor) {
LOGM(ERR, "Client requested sharing of a monitor that doesnt exist");
CScreencopyProtocolManager::~CScreencopyProtocolManager() { resource->sendFailed();
displayDestroy(); PROTO::screencopy->destroyResource(this);
}
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!");
return; return;
} }
m_liDisplayDestroy.notify = handleDisplayDestroy; resource->setOnDestroy([this](CZwlrScreencopyFrameV1* pMgr) { PROTO::screencopy->destroyResource(this); });
wl_display_add_destroy_listener(g_pCompositor->m_sWLDisplay, &m_liDisplayDestroy); resource->setDestroy([this](CZwlrScreencopyFrameV1* pFrame) { PROTO::screencopy->destroyResource(this); });
resource->setCopy([this](CZwlrScreencopyFrameV1* pFrame, wl_resource* res) { this->copy(pFrame, res); });
Debug::log(LOG, "ScreencopyProtocolManager started successfully!"); resource->setCopyWithDamage([this](CZwlrScreencopyFrameV1* pFrame, wl_resource* res) {
withDamage = true;
m_pSoftwareCursorTimer = makeShared<CEventLoopTimer>( this->copy(pFrame, res);
std::nullopt, });
[this](SP<CEventLoopTimer> 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<uint64_t>{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<uint64_t>{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++;
g_pHyprRenderer->makeEGLCurrent(); g_pHyprRenderer->makeEGLCurrent();
if (g_pHyprOpenGL->m_mMonitorRenderResources.contains(PFRAME->pMonitor)) { if (g_pHyprOpenGL->m_mMonitorRenderResources.contains(pMonitor)) {
const auto& RDATA = g_pHyprOpenGL->m_mMonitorRenderResources.at(PFRAME->pMonitor); const auto& RDATA = g_pHyprOpenGL->m_mMonitorRenderResources.at(pMonitor);
// bind the fb for its format. Suppress gl errors. // bind the fb for its format. Suppress gl errors.
#ifndef GLES2 #ifndef GLES2
glBindFramebuffer(GL_READ_FRAMEBUFFER, RDATA.offloadFB.m_iFb); 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); glBindFramebuffer(GL_FRAMEBUFFER, RDATA.offloadFB.m_iFb);
#endif #endif
} else } else
Debug::log(ERR, "No RDATA in screencopy???"); LOGM(ERR, "No RDATA in screencopy???");
PFRAME->shmFormat = g_pHyprOpenGL->getPreferredReadFormat(PFRAME->pMonitor); shmFormat = g_pHyprOpenGL->getPreferredReadFormat(pMonitor);
if (PFRAME->shmFormat == DRM_FORMAT_INVALID) { if (shmFormat == DRM_FORMAT_INVALID) {
Debug::log(ERR, "No format supported by renderer in capture output"); LOGM(ERR, "No format supported by renderer in capture output");
zwlr_screencopy_frame_v1_send_failed(PFRAME->resource); resource->sendFailed();
removeFrame(PFRAME); PROTO::screencopy->destroyResource(this);
return; return;
} }
const auto PSHMINFO = FormatUtils::getPixelFormatFromDRM(PFRAME->shmFormat); const auto PSHMINFO = FormatUtils::getPixelFormatFromDRM(shmFormat);
if (!PSHMINFO) { if (!PSHMINFO) {
Debug::log(ERR, "No pixel format supported by renderer in capture output"); LOGM(ERR, "No pixel format supported by renderer in capture output");
zwlr_screencopy_frame_v1_send_failed(PFRAME->resource); resource->sendFailed();
removeFrame(PFRAME); PROTO::screencopy->destroyResource(this);
return; return;
} }
PFRAME->dmabufFormat = PFRAME->pMonitor->output->state->state().drmFormat; dmabufFormat = pMonitor->output->state->state().drmFormat;
if (box.width == 0 && box.height == 0) if (box_.width == 0 && box_.height == 0)
PFRAME->box = {0, 0, (int)(PFRAME->pMonitor->vecSize.x), (int)(PFRAME->pMonitor->vecSize.y)}; box = {0, 0, (int)(pMonitor->vecSize.x), (int)(pMonitor->vecSize.y)};
else { else {
PFRAME->box = box; box = box_;
} }
PFRAME->box.transform(wlTransformToHyprutils(PFRAME->pMonitor->transform), PFRAME->pMonitor->vecTransformedSize.x, PFRAME->pMonitor->vecTransformedSize.y) box.transform(wlTransformToHyprutils(pMonitor->transform), pMonitor->vecTransformedSize.x, pMonitor->vecTransformedSize.y).scale(pMonitor->scale).round();
.scale(PFRAME->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 (resource->version() >= 3) {
if (PFRAME->dmabufFormat != DRM_FORMAT_INVALID) { if (dmabufFormat != DRM_FORMAT_INVALID) {
zwlr_screencopy_frame_v1_send_linux_dmabuf(PFRAME->resource, PFRAME->dmabufFormat, PFRAME->box.width, PFRAME->box.height); 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) { void CScreencopyFrame::copy(CZwlrScreencopyFrameV1* pFrame, wl_resource* buffer_) {
const auto PFRAME = frameFromResource(resource); if (!good()) {
LOGM(ERR, "No frame in copyFrame??");
if (!PFRAME) {
Debug::log(ERR, "No frame in copyFrame??");
return; return;
} }
if (!g_pCompositor->monitorExists(PFRAME->pMonitor)) { if (!g_pCompositor->monitorExists(pMonitor)) {
Debug::log(ERR, "client requested sharing of a monitor that is gone"); LOGM(ERR, "Client requested sharing of a monitor that is gone");
zwlr_screencopy_frame_v1_send_failed(PFRAME->resource); resource->sendFailed();
removeFrame(PFRAME); PROTO::screencopy->destroyResource(this);
return; return;
} }
const auto PBUFFER = CWLBufferResource::fromResource(buffer); const auto PBUFFER = CWLBufferResource::fromResource(buffer_);
if (!PBUFFER) { if (!PBUFFER) {
Debug::log(ERR, "[sc] invalid buffer in {:x}", (uintptr_t)PFRAME); LOGM(ERR, "Invalid buffer in {:x}", (uintptr_t)this);
wl_resource_post_error(PFRAME->resource, ZWLR_SCREENCOPY_FRAME_V1_ERROR_INVALID_BUFFER, "invalid buffer"); resource->error(ZWLR_SCREENCOPY_FRAME_V1_ERROR_INVALID_BUFFER, "invalid buffer");
removeFrame(PFRAME); PROTO::screencopy->destroyResource(this);
return; return;
} }
PBUFFER->buffer->lock(); PBUFFER->buffer->lock();
if (PBUFFER->buffer->size != PFRAME->box.size()) { if (PBUFFER->buffer->size != box.size()) {
Debug::log(ERR, "[sc] invalid dimensions in {:x}", (uintptr_t)PFRAME); LOGM(ERR, "Invalid dimensions in {:x}", (uintptr_t)this);
wl_resource_post_error(PFRAME->resource, ZWLR_SCREENCOPY_FRAME_V1_ERROR_INVALID_BUFFER, "invalid buffer dimensions"); resource->error(ZWLR_SCREENCOPY_FRAME_V1_ERROR_INVALID_BUFFER, "invalid buffer dimensions");
removeFrame(PFRAME); PROTO::screencopy->destroyResource(this);
return; return;
} }
if (PFRAME->buffer) { if (buffer) {
Debug::log(ERR, "[sc] buffer used in {:x}", (uintptr_t)PFRAME); LOGM(ERR, "Buffer used in {:x}", (uintptr_t)this);
wl_resource_post_error(PFRAME->resource, ZWLR_SCREENCOPY_FRAME_V1_ERROR_ALREADY_USED, "frame already used"); resource->error(ZWLR_SCREENCOPY_FRAME_V1_ERROR_ALREADY_USED, "frame already used");
removeFrame(PFRAME); PROTO::screencopy->destroyResource(this);
return; return;
} }
if (auto attrs = PBUFFER->buffer->dmabuf(); attrs.success) { if (auto attrs = PBUFFER->buffer->dmabuf(); attrs.success) {
PFRAME->bufferDMA = true; bufferDMA = true;
if (attrs.format != PFRAME->dmabufFormat) { if (attrs.format != dmabufFormat) {
Debug::log(ERR, "[sc] invalid buffer dma format in {:x}", (uintptr_t)PFRAME); LOGM(ERR, "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"); resource->error(ZWLR_SCREENCOPY_FRAME_V1_ERROR_INVALID_BUFFER, "invalid buffer format");
removeFrame(PFRAME); PROTO::screencopy->destroyResource(this);
return; return;
} }
} else if (auto attrs = PBUFFER->buffer->shm(); attrs.success) { } else if (auto attrs = PBUFFER->buffer->shm(); attrs.success) {
if (attrs.format != PFRAME->shmFormat) { if (attrs.format != shmFormat) {
Debug::log(ERR, "[sc] invalid buffer shm format in {:x}", (uintptr_t)PFRAME); LOGM(ERR, "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"); resource->error(ZWLR_SCREENCOPY_FRAME_V1_ERROR_INVALID_BUFFER, "invalid buffer format");
removeFrame(PFRAME); PROTO::screencopy->destroyResource(this);
return; return;
} else if ((int)attrs.stride != PFRAME->shmStride) { } else if ((int)attrs.stride != shmStride) {
Debug::log(ERR, "[sc] invalid buffer shm stride in {:x}", (uintptr_t)PFRAME); LOGM(ERR, "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"); resource->error(ZWLR_SCREENCOPY_FRAME_V1_ERROR_INVALID_BUFFER, "invalid buffer stride");
removeFrame(PFRAME); PROTO::screencopy->destroyResource(this);
return; return;
} }
} else { } else {
Debug::log(ERR, "[sc] invalid buffer type in {:x}", (uintptr_t)PFRAME); LOGM(ERR, "Invalid buffer type in {:x}", (uintptr_t)pFrame);
wl_resource_post_error(PFRAME->resource, ZWLR_SCREENCOPY_FRAME_V1_ERROR_INVALID_BUFFER, "invalid buffer type"); resource->error(ZWLR_SCREENCOPY_FRAME_V1_ERROR_INVALID_BUFFER, "invalid buffer type");
removeFrame(PFRAME); PROTO::screencopy->destroyResource(this);
return; 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; g_pHyprRenderer->m_bDirectScanoutBlocked = true;
if (PFRAME->overlayCursor && !PFRAME->lockedSWCursors) { if (overlayCursor && !lockedSWCursors) {
PFRAME->lockedSWCursors = true; lockedSWCursors = true;
// TODO: make it per-monitor // TODO: make it per-monitor
if (!m_bTimerArmed) { if (!PROTO::screencopy->m_bTimerArmed) {
for (auto& m : g_pCompositor->m_vMonitors) { for (auto& m : g_pCompositor->m_vMonitors) {
g_pPointerManager->lockSoftwareForMonitor(m); g_pPointerManager->lockSoftwareForMonitor(m);
} }
m_bTimerArmed = true; PROTO::screencopy->m_bTimerArmed = true;
Debug::log(LOG, "[screencopy] Locking sw cursors due to screensharing"); 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) if (!withDamage)
g_pHyprRenderer->damageMonitor(PFRAME->pMonitor); g_pHyprRenderer->damageMonitor(pMonitor);
} }
void CScreencopyProtocolManager::onOutputCommit(CMonitor* pMonitor) { void CScreencopyFrame::share() {
m_pLastMonitorBackBuffer = pMonitor->output->state->state().buffer; if (!buffer || !pMonitor)
shareAllFrames(pMonitor);
m_pLastMonitorBackBuffer.reset();
}
void CScreencopyProtocolManager::shareAllFrames(CMonitor* pMonitor) {
if (m_vFramesAwaitingWrite.empty())
return; // nothing to share
std::vector<SScreencopyFrame*> 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)
return; return;
timespec now; timespec now;
clock_gettime(CLOCK_MONOTONIC, &now); clock_gettime(CLOCK_MONOTONIC, &now);
uint32_t flags = 0; if (bufferDMA) {
if (frame->bufferDMA) { if (!copyDmabuf()) {
if (!copyFrameDmabuf(frame)) { LOGM(ERR, "Dmabuf copy failed in {:x}", (uintptr_t)this);
Debug::log(ERR, "[sc] dmabuf copy failed in {:x}", (uintptr_t)frame); resource->sendFailed();
zwlr_screencopy_frame_v1_send_failed(frame->resource);
return; return;
} }
} else { } else {
if (!copyFrameShm(frame, &now)) { if (!copyShm()) {
Debug::log(ERR, "[sc] shm copy failed in {:x}", (uintptr_t)frame); LOGM(ERR, "Shm copy failed in {:x}", (uintptr_t)this);
zwlr_screencopy_frame_v1_send_failed(frame->resource); resource->sendFailed();
return; return;
} }
} }
zwlr_screencopy_frame_v1_send_flags(frame->resource, flags); resource->sendFlags((zwlrScreencopyFrameV1Flags)0);
sendFrameDamage(frame); 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 tvSecHi = (sizeof(now.tv_sec) > 4) ? now.tv_sec >> 32 : 0;
uint32_t tvSecLo = now.tv_sec & 0xFFFFFFFF; 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) { bool CScreencopyFrame::copyDmabuf() {
if (!frame->withDamage) auto TEXTURE = makeShared<CTexture>(pMonitor->output->state->state().buffer);
return;
// TODO: CRegion fakeDamage = {0, 0, INT16_MAX, INT16_MAX};
// add a damage ring for this.
// 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) { CBox monbox = CBox{0, 0, pMonitor->vecPixelSize.x, pMonitor->vecPixelSize.y}
// Debug::log(ERR, "[sc] Failed to send damage"); .translate({-box.x, -box.y}) // vvvv kinda ass-backwards but that's how I designed the renderer... sigh.
// break; .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), g_pHyprOpenGL->m_RenderData.blockScreenShader = true;
// 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_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) { bool CScreencopyFrame::copyShm() {
auto TEXTURE = makeShared<CTexture>(m_pLastMonitorBackBuffer); auto TEXTURE = makeShared<CTexture>(pMonitor->output->state->state().buffer);
auto shm = frame->buffer->shm(); auto shm = buffer->shm();
auto [pixelData, fmt, bufLen] = frame->buffer->beginDataPtr(0); // no need for end, cuz it's shm auto [pixelData, fmt, bufLen] = buffer->beginDataPtr(0); // no need for end, cuz it's shm
CRegion fakeDamage = {0, 0, INT16_MAX, INT16_MAX}; CRegion fakeDamage = {0, 0, INT16_MAX, INT16_MAX};
g_pHyprRenderer->makeEGLCurrent(); g_pHyprRenderer->makeEGLCurrent();
CFramebuffer fb; 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)) { if (!g_pHyprRenderer->beginRender(pMonitor, fakeDamage, RENDER_MODE_FULL_FAKE, nullptr, &fb, true)) {
Debug::log(ERR, "Screencopy: can't copy: failed to begin rendering"); LOGM(ERR, "Can't copy: failed to begin rendering");
return false; 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->setMonitorTransformEnabled(true);
g_pHyprOpenGL->setRenderModifEnabled(false); g_pHyprOpenGL->setRenderModifEnabled(false);
g_pHyprOpenGL->renderTexture(TEXTURE, &monbox, 1); g_pHyprOpenGL->renderTexture(TEXTURE, &monbox, 1);
@ -509,7 +269,7 @@ bool CScreencopyProtocolManager::copyFrameShm(SScreencopyFrame* frame, timespec*
const auto PFORMAT = FormatUtils::getPixelFormatFromDRM(shm.format); const auto PFORMAT = FormatUtils::getPixelFormatFromDRM(shm.format);
if (!PFORMAT) { 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(); g_pHyprRenderer->endRender();
return false; return false;
} }
@ -520,53 +280,164 @@ bool CScreencopyProtocolManager::copyFrameShm(SScreencopyFrame* frame, timespec*
g_pHyprRenderer->endRender(); g_pHyprRenderer->endRender();
g_pHyprRenderer->makeEGLCurrent(); g_pHyprRenderer->makeEGLCurrent();
g_pHyprOpenGL->m_RenderData.pMonitor = frame->pMonitor; g_pHyprOpenGL->m_RenderData.pMonitor = pMonitor;
fb.bind(); fb.bind();
glPixelStorei(GL_PACK_ALIGNMENT, 1); glPixelStorei(GL_PACK_ALIGNMENT, 1);
const auto drmFmt = FormatUtils::getPixelFormatFromDRM(shm.format); 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) { 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 { } else {
for (size_t i = 0; i < frame->box.h; ++i) { for (size_t i = 0; i < box.h; ++i) {
uint32_t y = 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; g_pHyprOpenGL->m_RenderData.pMonitor = nullptr;
Debug::log(TRACE, "Screencopy: copied frame via shm"); LOGM(TRACE, "Copied frame via shm");
return true; return true;
} }
bool CScreencopyProtocolManager::copyFrameDmabuf(SScreencopyFrame* frame) { bool CScreencopyFrame::good() {
auto TEXTURE = makeShared<CTexture>(m_pLastMonitorBackBuffer); 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)) { CScreencopyClient::CScreencopyClient(SP<CZwlrScreencopyManagerV1> resource_) : resource(resource_) {
Debug::log(ERR, "Screencopy: can't copy: failed to begin rendering to dma frame"); if (!good())
return false; 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<CScreencopyFrame>(makeShared<CZwlrScreencopyFrameV1>(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} FRAME->self = FRAME;
.translate({-frame->box.x, -frame->box.y}) // vvvv kinda ass-backwards but that's how I designed the renderer... sigh. FRAME->client = self;
.transform(wlTransformToHyprutils(invertTransform(frame->pMonitor->transform)), frame->pMonitor->vecPixelSize.x, frame->pMonitor->vecPixelSize.y); }
g_pHyprOpenGL->setMonitorTransformEnabled(true);
g_pHyprOpenGL->setRenderModifEnabled(false); void CScreencopyClient::onTick() {
g_pHyprOpenGL->renderTexture(TEXTURE, &monbox, 1); if (lastMeasure.getMillis() < 500)
g_pHyprOpenGL->setRenderModifEnabled(true); return;
g_pHyprOpenGL->setMonitorTransformEnabled(false);
framesInLastHalfSecond = frameCounter;
g_pHyprOpenGL->m_RenderData.blockScreenShader = true; frameCounter = 0;
g_pHyprRenderer->endRender(); lastMeasure.reset();
Debug::log(TRACE, "Screencopy: copied frame via dma"); 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; });
return true;
if (framesInLastHalfSecond > 3 && !sentScreencast) {
EMIT_HOOK_EVENT("screencast", (std::vector<uint64_t>{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<uint64_t>{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<CEventLoopTimer>(
std::nullopt,
[this](SP<CEventLoopTimer> 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<CScreencopyClient>(makeShared<CZwlrScreencopyManagerV1>(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<WP<CScreencopyFrame>> 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());
}
} }

View file

@ -1,7 +1,8 @@
#pragma once #pragma once
#include "../defines.hpp" #include "../defines.hpp"
#include "wlr-screencopy-unstable-v1-protocol.h" #include "wlr-screencopy-unstable-v1.hpp"
#include "WaylandProtocol.hpp"
#include <list> #include <list>
#include <vector> #include <vector>
@ -20,88 +21,93 @@ enum eClientOwners {
class CScreencopyClient { class CScreencopyClient {
public: public:
CScreencopyClient(); CScreencopyClient(SP<CZwlrScreencopyManagerV1> resource_);
~CScreencopyClient(); ~CScreencopyClient();
int ref = 0; bool good();
wl_resource* resource = nullptr;
eClientOwners clientOwner = CLIENT_SCREENCOPY; WP<CScreencopyClient> self;
eClientOwners clientOwner = CLIENT_SCREENCOPY;
int frameCounter = 0; CTimer lastFrame;
int framesInLastHalfSecond = 0; int frameCounter = 0;
CTimer lastMeasure;
CTimer lastFrame;
bool sentScreencast = false;
void onTick();
SP<HOOK_CALLBACK_FN> 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<IHLBuffer> 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;
private: private:
wl_global* m_pGlobal = nullptr; SP<CZwlrScreencopyManagerV1> resource;
std::list<SScreencopyFrame> m_lFrames;
std::list<CScreencopyClient> m_lClients;
SP<CEventLoopTimer> m_pSoftwareCursorTimer; int framesInLastHalfSecond = 0;
bool m_bTimerArmed = false; CTimer lastMeasure;
bool sentScreencast = false;
std::vector<SScreencopyFrame*> m_vFramesAwaitingWrite; SP<HOOK_CALLBACK_FN> tickCallback;
void onTick();
SP<Aquamarine::IBuffer> m_pLastMonitorBackBuffer; void captureOutput(uint32_t frame, int32_t overlayCursor, wl_resource* output, CBox box);
void shareAllFrames(CMonitor* pMonitor); friend class CScreencopyProtocol;
void shareFrame(SScreencopyFrame* frame); };
void sendFrameDamage(SScreencopyFrame* frame);
bool copyFrameDmabuf(SScreencopyFrame* frame);
bool copyFrameShm(SScreencopyFrame* frame, timespec* now);
class CScreencopyFrame {
public:
CScreencopyFrame(SP<CZwlrScreencopyFrameV1> resource, int32_t overlay_cursor, wl_resource* output, CBox box);
~CScreencopyFrame();
bool good();
SP<CScreencopyFrame> self;
WP<CScreencopyClient> client;
private:
SP<CZwlrScreencopyFrameV1> resource;
CMonitor* pMonitor = nullptr;
bool overlayCursor = false;
bool withDamage = false;
bool lockedSWCursors = false;
WP<IHLBuffer> 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<SP<CScreencopyFrame>> m_vFrames;
std::vector<SP<CScreencopyFrame>> m_vFramesAwaitingWrite;
std::vector<SP<CScreencopyClient>> m_vClients;
SP<CEventLoopTimer> 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; friend class CScreencopyClient;
}; };
namespace PROTO {
inline UP<CScreencopyProtocol> screencopy;
};

View file

@ -8,363 +8,240 @@
#include <algorithm> #include <algorithm>
#define TOPLEVEL_EXPORT_VERSION 2 #define LOGM PROTO::toplevelExport->protoLog
static void bindManagerInt(wl_client* client, void* data, uint32_t version, uint32_t id) { CToplevelExportClient::CToplevelExportClient(SP<CHyprlandToplevelExportManagerV1> resource_) : resource(resource_) {
g_pProtocolManager->m_pToplevelExportProtocolManager->bindManager(client, data, version, id); if (!good())
}
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)
return; 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); lastMeasure.reset();
if (frame->buffer && frame->buffer->locked() > 0) lastFrame.reset();
frame->buffer->unlock(); tickCallback = g_pHookSystem->hookDynamic("tick", [&](void* self, SCallbackInfo& info, std::any data) { onTick(); });
removeClient(frame->client, force);
m_lFrames.remove(*frame);
} }
void CToplevelExportProtocolManager::captureToplevel(wl_client* client, wl_resource* resource, uint32_t frame, int32_t overlay_cursor, PHLWINDOW pWindow) { void CToplevelExportClient::captureToplevel(CHyprlandToplevelExportManagerV1* pMgr, uint32_t frame, int32_t overlayCursor_, PHLWINDOW handle) {
const auto PCLIENT = clientFromResource(resource);
// create a frame // create a frame
const auto PFRAME = &m_lFrames.emplace_back(); const auto FRAME = PROTO::toplevelExport->m_vFrames.emplace_back(
PFRAME->overlayCursor = !!overlay_cursor; makeShared<CToplevelExportFrame>(makeShared<CHyprlandToplevelExportFrameV1>(resource->client(), resource->version(), frame), overlayCursor_, handle));
PFRAME->resource = wl_resource_create(client, &hyprland_toplevel_export_frame_v1_interface, wl_resource_get_version(resource), frame);
PFRAME->pWindow = pWindow; 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<uint64_t>{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<uint64_t>{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<CHyprlandToplevelExportFrameV1> resource_, int32_t overlayCursor_, PHLWINDOW pWindow_) : resource(resource_), pWindow(pWindow_) {
if (!good())
return;
overlayCursor = !!overlayCursor_;
if (!pWindow) { if (!pWindow) {
Debug::log(ERR, "Client requested sharing of window handle {:x} which does not exist!", pWindow); LOGM(ERR, "Client requested sharing of window handle {:x} which does not exist!", pWindow);
hyprland_toplevel_export_frame_v1_send_failed(PFRAME->resource); resource->sendFailed();
removeFrame(PFRAME); PROTO::toplevelExport->destroyResource(this);
return; return;
} }
if (!pWindow->m_bIsMapped || pWindow->isHidden()) { if (!pWindow->m_bIsMapped || pWindow->isHidden()) {
Debug::log(ERR, "Client requested sharing of window handle {:x} which is not shareable!", pWindow); LOGM(ERR, "Client requested sharing of window handle {:x} which is not shareable!", pWindow);
hyprland_toplevel_export_frame_v1_send_failed(PFRAME->resource); resource->sendFailed();
removeFrame(PFRAME); PROTO::toplevelExport->destroyResource(this);
return; return;
} }
if (!PFRAME->resource) { resource->setOnDestroy([this](CHyprlandToplevelExportFrameV1* pFrame) { PROTO::toplevelExport->destroyResource(this); });
Debug::log(ERR, "Couldn't alloc frame for sharing! (no memory)"); resource->setDestroy([this](CHyprlandToplevelExportFrameV1* pFrame) { PROTO::toplevelExport->destroyResource(this); });
m_lFrames.remove(*PFRAME); resource->setCopy([this](CHyprlandToplevelExportFrameV1* pFrame, wl_resource* res, int32_t ignoreDamage) { this->copy(pFrame, res, ignoreDamage); });
wl_client_post_no_memory(client);
return;
}
wl_resource_set_implementation(PFRAME->resource, &toplevelFrameImpl, PFRAME, handleFrameResourceDestroy);
PFRAME->client = PCLIENT;
PCLIENT->ref++;
const auto PMONITOR = g_pCompositor->getMonitorFromID(pWindow->m_iMonitorID); const auto PMONITOR = g_pCompositor->getMonitorFromID(pWindow->m_iMonitorID);
g_pHyprRenderer->makeEGLCurrent(); g_pHyprRenderer->makeEGLCurrent();
PFRAME->shmFormat = g_pHyprOpenGL->getPreferredReadFormat(PMONITOR); shmFormat = g_pHyprOpenGL->getPreferredReadFormat(PMONITOR);
if (PFRAME->shmFormat == DRM_FORMAT_INVALID) { if (shmFormat == DRM_FORMAT_INVALID) {
Debug::log(ERR, "No format supported by renderer in capture toplevel"); LOGM(ERR, "No format supported by renderer in capture toplevel");
hyprland_toplevel_export_frame_v1_send_failed(resource); resource->sendFailed();
removeFrame(PFRAME); PROTO::toplevelExport->destroyResource(this);
return; return;
} }
const auto PSHMINFO = FormatUtils::getPixelFormatFromDRM(PFRAME->shmFormat); const auto PSHMINFO = FormatUtils::getPixelFormatFromDRM(shmFormat);
if (!PSHMINFO) { if (!PSHMINFO) {
Debug::log(ERR, "No pixel format supported by renderer in capture toplevel"); LOGM(ERR, "No pixel format supported by renderer in capture toplevel");
hyprland_toplevel_export_frame_v1_send_failed(resource); resource->sendFailed();
removeFrame(PFRAME); PROTO::toplevelExport->destroyResource(this);
return; 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) { if (dmabufFormat != DRM_FORMAT_INVALID) {
hyprland_toplevel_export_frame_v1_send_linux_dmabuf(PFRAME->resource, PFRAME->dmabufFormat, PFRAME->box.width, PFRAME->box.height); 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) { void CToplevelExportFrame::copy(CHyprlandToplevelExportFrameV1* pFrame, wl_resource* buffer_, int32_t ignoreDamage) {
const auto PFRAME = frameFromResource(resource); if (!good()) {
LOGM(ERR, "No frame in copyFrame??");
if (!PFRAME) {
Debug::log(ERR, "No frame in copyFrame??");
return; return;
} }
const auto PWINDOW = PFRAME->pWindow.lock(); if (!validMapped(pWindow)) {
LOGM(ERR, "Client requested sharing of window handle {:x} which is gone!", pWindow);
if (!validMapped(PWINDOW)) { resource->sendFailed();
Debug::log(ERR, "Client requested sharing of window handle {:x} which is gone!", PWINDOW); PROTO::toplevelExport->destroyResource(this);
hyprland_toplevel_export_frame_v1_send_failed(PFRAME->resource);
removeFrame(PFRAME);
return; return;
} }
if (!PWINDOW->m_bIsMapped || PWINDOW->isHidden()) { if (!pWindow->m_bIsMapped || pWindow->isHidden()) {
Debug::log(ERR, "Client requested sharing of window handle {:x} which is not shareable (2)!", PWINDOW); LOGM(ERR, "Client requested sharing of window handle {:x} which is not shareable (2)!", pWindow);
hyprland_toplevel_export_frame_v1_send_failed(PFRAME->resource); resource->sendFailed();
removeFrame(PFRAME); PROTO::toplevelExport->destroyResource(this);
return; return;
} }
const auto PBUFFER = CWLBufferResource::fromResource(buffer); const auto PBUFFER = CWLBufferResource::fromResource(buffer_);
if (!PBUFFER) { if (!PBUFFER) {
wl_resource_post_error(PFRAME->resource, HYPRLAND_TOPLEVEL_EXPORT_FRAME_V1_ERROR_INVALID_BUFFER, "invalid buffer"); resource->error(HYPRLAND_TOPLEVEL_EXPORT_FRAME_V1_ERROR_INVALID_BUFFER, "invalid buffer");
removeFrame(PFRAME); PROTO::toplevelExport->destroyResource(this);
return; return;
} }
PBUFFER->buffer->lock(); PBUFFER->buffer->lock();
if (PBUFFER->buffer->size != PFRAME->box.size()) { if (PBUFFER->buffer->size != box.size()) {
wl_resource_post_error(PFRAME->resource, HYPRLAND_TOPLEVEL_EXPORT_FRAME_V1_ERROR_INVALID_BUFFER, "invalid buffer dimensions"); resource->error(HYPRLAND_TOPLEVEL_EXPORT_FRAME_V1_ERROR_INVALID_BUFFER, "invalid buffer dimensions");
removeFrame(PFRAME); PROTO::toplevelExport->destroyResource(this);
return; return;
} }
if (PFRAME->buffer) { if (buffer) {
wl_resource_post_error(PFRAME->resource, HYPRLAND_TOPLEVEL_EXPORT_FRAME_V1_ERROR_ALREADY_USED, "frame already used"); resource->error(HYPRLAND_TOPLEVEL_EXPORT_FRAME_V1_ERROR_ALREADY_USED, "frame already used");
removeFrame(PFRAME); PROTO::toplevelExport->destroyResource(this);
return; return;
} }
if (auto attrs = PBUFFER->buffer->dmabuf(); attrs.success) { if (auto attrs = PBUFFER->buffer->dmabuf(); attrs.success) {
PFRAME->bufferDMA = true; bufferDMA = true;
if (attrs.format != PFRAME->dmabufFormat) { if (attrs.format != dmabufFormat) {
wl_resource_post_error(PFRAME->resource, HYPRLAND_TOPLEVEL_EXPORT_FRAME_V1_ERROR_INVALID_BUFFER, "invalid buffer format"); resource->error(HYPRLAND_TOPLEVEL_EXPORT_FRAME_V1_ERROR_INVALID_BUFFER, "invalid buffer format");
removeFrame(PFRAME); PROTO::toplevelExport->destroyResource(this);
return; return;
} }
} else if (auto attrs = PBUFFER->buffer->shm(); attrs.success) { } else if (auto attrs = PBUFFER->buffer->shm(); attrs.success) {
if (attrs.format != PFRAME->shmFormat) { if (attrs.format != shmFormat) {
wl_resource_post_error(PFRAME->resource, HYPRLAND_TOPLEVEL_EXPORT_FRAME_V1_ERROR_INVALID_BUFFER, "invalid buffer format"); resource->error(HYPRLAND_TOPLEVEL_EXPORT_FRAME_V1_ERROR_INVALID_BUFFER, "invalid buffer format");
removeFrame(PFRAME); PROTO::toplevelExport->destroyResource(this);
return; return;
} else if ((int)attrs.stride != PFRAME->shmStride) { } else if ((int)attrs.stride != shmStride) {
wl_resource_post_error(PFRAME->resource, HYPRLAND_TOPLEVEL_EXPORT_FRAME_V1_ERROR_INVALID_BUFFER, "invalid buffer stride"); resource->error(HYPRLAND_TOPLEVEL_EXPORT_FRAME_V1_ERROR_INVALID_BUFFER, "invalid buffer stride");
removeFrame(PFRAME); PROTO::toplevelExport->destroyResource(this);
return; return;
} }
} else { } else {
wl_resource_post_error(PFRAME->resource, HYPRLAND_TOPLEVEL_EXPORT_FRAME_V1_ERROR_INVALID_BUFFER, "invalid buffer type"); resource->error(HYPRLAND_TOPLEVEL_EXPORT_FRAME_V1_ERROR_INVALID_BUFFER, "invalid buffer type");
removeFrame(PFRAME); PROTO::toplevelExport->destroyResource(this);
return; 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) { void CToplevelExportFrame::share() {
if (m_vFramesAwaitingWrite.empty()) if (!buffer || !validMapped(pWindow))
return; // nothing to share
std::vector<SScreencopyFrame*> 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))
return; return;
timespec now; timespec now;
clock_gettime(CLOCK_MONOTONIC, &now); clock_gettime(CLOCK_MONOTONIC, &now);
uint32_t flags = 0; if (bufferDMA) {
if (frame->bufferDMA) { if (!copyDmabuf(&now)) {
if (!copyFrameDmabuf(frame, &now)) { resource->sendFailed();
hyprland_toplevel_export_frame_v1_send_failed(frame->resource);
return; return;
} }
} else { } else {
if (!copyFrameShm(frame, &now)) { if (!copyShm(&now)) {
hyprland_toplevel_export_frame_v1_send_failed(frame->resource); resource->sendFailed();
return; return;
} }
} }
hyprland_toplevel_export_frame_v1_send_flags(frame->resource, flags); resource->sendFlags((hyprlandToplevelExportFrameV1Flags)0);
sendDamage(frame);
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 tvSecHi = (sizeof(now.tv_sec) > 4) ? now.tv_sec >> 32 : 0;
uint32_t tvSecLo = now.tv_sec & 0xFFFFFFFF; 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) { bool CToplevelExportFrame::copyShm(timespec* now) {
// TODO: send proper dmg auto shm = buffer->shm();
hyprland_toplevel_export_frame_v1_send_damage(frame->resource, 0, 0, frame->box.width, frame->box.height); auto [pixelData, fmt, bufLen] = buffer->beginDataPtr(0); // no need for end, cuz it's shm
}
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
// render the client // 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}; CRegion fakeDamage{0, 0, PMONITOR->vecPixelSize.x * 10, PMONITOR->vecPixelSize.y * 10};
g_pHyprRenderer->makeEGLCurrent(); g_pHyprRenderer->makeEGLCurrent();
@ -372,7 +249,7 @@ bool CToplevelExportProtocolManager::copyFrameShm(SScreencopyFrame* frame, times
CFramebuffer outFB; CFramebuffer outFB;
outFB.alloc(PMONITOR->vecPixelSize.x, PMONITOR->vecPixelSize.y, g_pHyprRenderer->isNvidia() ? DRM_FORMAT_XBGR8888 : PMONITOR->output->state->state().drmFormat); 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->lockSoftwareForMonitor(PMONITOR->self.lock());
g_pPointerManager->damageCursor(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)); g_pHyprOpenGL->clear(CColor(0, 0, 0, 1.0));
// render client at 0,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->m_bBlockSurfaceFeedback = g_pHyprRenderer->shouldRenderWindow(pWindow); // 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->renderWindow(pWindow, PMONITOR, now, false, RENDER_PASS_ALL, true, true);
g_pHyprRenderer->m_bBlockSurfaceFeedback = false; g_pHyprRenderer->m_bBlockSurfaceFeedback = false;
if (frame->overlayCursor) if (overlayCursor)
g_pPointerManager->renderSoftwareCursorsFor(PMONITOR->self.lock(), now, fakeDamage, g_pInputManager->getMouseCoordsInternal() - frame->pWindow->m_vRealPosition.value()); g_pPointerManager->renderSoftwareCursorsFor(PMONITOR->self.lock(), now, fakeDamage, g_pInputManager->getMouseCoordsInternal() - pWindow->m_vRealPosition.value());
const auto PFORMAT = FormatUtils::getPixelFormatFromDRM(shm.format); const auto PFORMAT = FormatUtils::getPixelFormatFromDRM(shm.format);
if (!PFORMAT) { if (!PFORMAT) {
@ -410,9 +287,9 @@ bool CToplevelExportProtocolManager::copyFrameShm(SScreencopyFrame* frame, times
glPixelStorei(GL_PACK_ALIGNMENT, 1); glPixelStorei(GL_PACK_ALIGNMENT, 1);
auto glFormat = PFORMAT->flipRB ? GL_BGRA_EXT : GL_RGBA; 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->unlockSoftwareForMonitor(PMONITOR->self.lock());
g_pPointerManager->damageCursor(PMONITOR->self.lock()); g_pPointerManager->damageCursor(PMONITOR->self.lock());
} }
@ -420,31 +297,112 @@ bool CToplevelExportProtocolManager::copyFrameShm(SScreencopyFrame* frame, times
return true; return true;
} }
bool CToplevelExportProtocolManager::copyFrameDmabuf(SScreencopyFrame* frame, timespec* now) { bool CToplevelExportFrame::copyDmabuf(timespec* now) {
const auto PMONITOR = g_pCompositor->getMonitorFromID(frame->pWindow->m_iMonitorID); const auto PMONITOR = g_pCompositor->getMonitorFromID(pWindow->m_iMonitorID);
CRegion fakeDamage{0, 0, INT16_MAX, INT16_MAX}; 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; return false;
g_pHyprOpenGL->clear(CColor(0, 0, 0, 1.0)); 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->m_bBlockSurfaceFeedback = g_pHyprRenderer->shouldRenderWindow(pWindow); // 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->renderWindow(pWindow, PMONITOR, now, false, RENDER_PASS_ALL, true, true);
g_pHyprRenderer->m_bBlockSurfaceFeedback = false; g_pHyprRenderer->m_bBlockSurfaceFeedback = false;
if (frame->overlayCursor) if (overlayCursor)
g_pPointerManager->renderSoftwareCursorsFor(PMONITOR->self.lock(), now, fakeDamage, g_pInputManager->getMouseCoordsInternal() - frame->pWindow->m_vRealPosition.value()); g_pPointerManager->renderSoftwareCursorsFor(PMONITOR->self.lock(), now, fakeDamage, g_pInputManager->getMouseCoordsInternal() - pWindow->m_vRealPosition.value());
g_pHyprOpenGL->m_RenderData.blockScreenShader = true; g_pHyprOpenGL->m_RenderData.blockScreenShader = true;
g_pHyprRenderer->endRender(); g_pHyprRenderer->endRender();
if (overlayCursor) {
g_pPointerManager->unlockSoftwareForMonitor(PMONITOR->self.lock());
g_pPointerManager->damageCursor(PMONITOR->self.lock());
}
return true; return true;
} }
void CToplevelExportProtocolManager::onWindowUnmap(PHLWINDOW pWindow) { bool CToplevelExportFrame::good() {
for (auto& f : m_lFrames) { return resource->resource();
if (f.pWindow.lock() == pWindow) }
f.pWindow.reset();
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<CToplevelExportClient>(makeShared<CHyprlandToplevelExportManagerV1>(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<WP<CToplevelExportFrame>> 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();
} }
} }

View file

@ -1,7 +1,8 @@
#pragma once #pragma once
#include "../defines.hpp" #include "../defines.hpp"
#include "hyprland-toplevel-export-v1-protocol.h" #include "hyprland-toplevel-export-v1.hpp"
#include "WaylandProtocol.hpp"
#include "Screencopy.hpp" #include "Screencopy.hpp"
#include <list> #include <list>
@ -10,33 +11,91 @@
class CMonitor; class CMonitor;
class CWindow; class CWindow;
class CToplevelExportProtocolManager { class CToplevelExportClient {
public: public:
CToplevelExportProtocolManager(); CToplevelExportClient(SP<CHyprlandToplevelExportManagerV1> resource_);
~CToplevelExportProtocolManager();
void bindManager(wl_client* client, void* data, uint32_t version, uint32_t id); bool good();
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);
wl_listener m_liDisplayDestroy; WP<CToplevelExportClient> self;
eClientOwners clientOwner = CLIENT_TOPLEVEL_EXPORT;
CTimer lastFrame;
int frameCounter = 0;
private: private:
wl_global* m_pGlobal = nullptr; SP<CHyprlandToplevelExportManagerV1> resource;
std::list<SScreencopyFrame> m_lFrames;
std::list<CScreencopyClient> m_lClients;
std::vector<SScreencopyFrame*> m_vFramesAwaitingWrite; int framesInLastHalfSecond = 0;
CTimer lastMeasure;
bool sentScreencast = false;
void shareFrame(SScreencopyFrame* frame); SP<HOOK_CALLBACK_FN> tickCallback;
bool copyFrameDmabuf(SScreencopyFrame* frame, timespec* now); void onTick();
bool copyFrameShm(SScreencopyFrame* frame, timespec* now);
void sendDamage(SScreencopyFrame* frame);
friend class CScreencopyClient; void captureToplevel(CHyprlandToplevelExportManagerV1* pMgr, uint32_t frame, int32_t overlayCursor, PHLWINDOW handle);
friend class CToplevelExportProtocol;
};
class CToplevelExportFrame {
public:
CToplevelExportFrame(SP<CHyprlandToplevelExportFrameV1> resource_, int32_t overlayCursor, PHLWINDOW pWindow);
~CToplevelExportFrame();
bool good();
SP<CToplevelExportFrame> self;
WP<CToplevelExportClient> client;
private:
SP<CHyprlandToplevelExportFrameV1> resource;
PHLWINDOW pWindow;
bool overlayCursor = false;
bool ignoreDamage = false;
bool lockedSWCursors = false;
WP<IHLBuffer> 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<SP<CToplevelExportClient>> m_vClients;
std::vector<SP<CToplevelExportFrame>> m_vFrames;
std::vector<SP<CToplevelExportFrame>> 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<CToplevelExportProtocol> toplevelExport;
}; };

View file

@ -139,7 +139,7 @@ class CHyprRenderer {
std::vector<SP<CRenderbuffer>> m_vRenderbuffers; std::vector<SP<CRenderbuffer>> m_vRenderbuffers;
friend class CHyprOpenGLImpl; friend class CHyprOpenGLImpl;
friend class CToplevelExportProtocolManager; friend class CToplevelExportFrame;
friend class CInputManager; friend class CInputManager;
friend class CPointerManager; friend class CPointerManager;
}; };