2023-04-03 17:01:05 +01:00
|
|
|
#include "Screencopy.hpp"
|
|
|
|
|
#include "../Compositor.hpp"
|
2024-05-11 22:10:42 +01:00
|
|
|
#include "../managers/eventLoop/EventLoopManager.hpp"
|
2024-05-05 22:18:10 +01:00
|
|
|
#include "../managers/PointerManager.hpp"
|
2025-04-28 20:18:02 +01:00
|
|
|
#include "../managers/input/InputManager.hpp"
|
2025-01-17 15:21:35 +00:00
|
|
|
#include "../managers/EventManager.hpp"
|
2025-04-08 19:39:53 +02:00
|
|
|
#include "../managers/permissions/DynamicPermissionManager.hpp"
|
2025-01-17 15:21:35 +00:00
|
|
|
#include "../render/Renderer.hpp"
|
|
|
|
|
#include "../render/OpenGL.hpp"
|
|
|
|
|
#include "../helpers/Monitor.hpp"
|
2024-06-08 10:07:59 +02:00
|
|
|
#include "core/Output.hpp"
|
|
|
|
|
#include "types/WLBuffer.hpp"
|
|
|
|
|
#include "types/Buffer.hpp"
|
|
|
|
|
#include "../helpers/Format.hpp"
|
2025-04-16 01:37:48 +01:00
|
|
|
#include "../helpers/time/Time.hpp"
|
2023-04-03 17:01:05 +01:00
|
|
|
|
|
|
|
|
#include <algorithm>
|
2025-03-22 11:01:14 -05:00
|
|
|
#include <functional>
|
2023-04-03 17:01:05 +01:00
|
|
|
|
2025-05-04 19:21:36 +02:00
|
|
|
CScreencopyFrame::CScreencopyFrame(SP<CZwlrScreencopyFrameV1> resource_, int32_t overlay_cursor, wl_resource* output, CBox box_) : m_resource(resource_) {
|
2025-01-17 18:21:34 +01:00
|
|
|
if UNLIKELY (!good())
|
2023-04-03 17:01:05 +01:00
|
|
|
return;
|
|
|
|
|
|
2025-05-04 19:21:36 +02:00
|
|
|
m_overlayCursor = !!overlay_cursor;
|
|
|
|
|
m_monitor = CWLOutputResource::fromResource(output)->m_monitor;
|
2023-04-03 17:01:05 +01:00
|
|
|
|
2025-05-04 19:21:36 +02:00
|
|
|
if (!m_monitor) {
|
2024-07-27 10:02:02 -05:00
|
|
|
LOGM(ERR, "Client requested sharing of a monitor that doesnt exist");
|
2025-05-04 19:21:36 +02:00
|
|
|
m_resource->sendFailed();
|
2023-04-03 17:01:05 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-04 19:21:36 +02:00
|
|
|
m_resource->setOnDestroy([this](CZwlrScreencopyFrameV1* pMgr) { PROTO::screencopy->destroyResource(this); });
|
|
|
|
|
m_resource->setDestroy([this](CZwlrScreencopyFrameV1* pFrame) { PROTO::screencopy->destroyResource(this); });
|
|
|
|
|
m_resource->setCopy([this](CZwlrScreencopyFrameV1* pFrame, wl_resource* res) { this->copy(pFrame, res); });
|
|
|
|
|
m_resource->setCopyWithDamage([this](CZwlrScreencopyFrameV1* pFrame, wl_resource* res) {
|
|
|
|
|
m_withDamage = true;
|
2024-07-27 10:02:02 -05:00
|
|
|
this->copy(pFrame, res);
|
|
|
|
|
});
|
2023-04-03 17:01:05 +01:00
|
|
|
|
2023-12-03 22:04:07 +00:00
|
|
|
g_pHyprRenderer->makeEGLCurrent();
|
2024-02-15 00:58:58 +00:00
|
|
|
|
2025-05-04 19:21:36 +02:00
|
|
|
m_shmFormat = g_pHyprOpenGL->getPreferredReadFormat(m_monitor.lock());
|
|
|
|
|
if (m_shmFormat == DRM_FORMAT_INVALID) {
|
2024-07-27 10:02:02 -05:00
|
|
|
LOGM(ERR, "No format supported by renderer in capture output");
|
2025-05-04 19:21:36 +02:00
|
|
|
m_resource->sendFailed();
|
2023-04-03 17:01:05 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-18 22:57:21 +02:00
|
|
|
// TODO: hack, we can't bit flip so we'll format flip heh, GL_BGRA_EXT wont work here
|
2025-05-04 19:21:36 +02:00
|
|
|
if (m_shmFormat == DRM_FORMAT_XRGB2101010 || m_shmFormat == DRM_FORMAT_ARGB2101010)
|
|
|
|
|
m_shmFormat = DRM_FORMAT_XBGR2101010;
|
2024-08-18 22:57:21 +02:00
|
|
|
|
2025-05-04 19:21:36 +02:00
|
|
|
const auto PSHMINFO = NFormatUtils::getPixelFormatFromDRM(m_shmFormat);
|
2023-04-03 17:01:05 +01:00
|
|
|
if (!PSHMINFO) {
|
2024-07-27 10:02:02 -05:00
|
|
|
LOGM(ERR, "No pixel format supported by renderer in capture output");
|
2025-05-04 19:21:36 +02:00
|
|
|
m_resource->sendFailed();
|
2023-04-03 17:01:05 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-04 19:21:36 +02:00
|
|
|
m_dmabufFormat = m_monitor->m_output->state->state().drmFormat;
|
2023-04-03 17:01:05 +01:00
|
|
|
|
2024-07-27 10:02:02 -05:00
|
|
|
if (box_.width == 0 && box_.height == 0)
|
2025-05-04 19:21:36 +02:00
|
|
|
m_box = {0, 0, (int)(m_monitor->m_size.x), (int)(m_monitor->m_size.y)};
|
2023-04-03 17:01:05 +01:00
|
|
|
else {
|
2025-05-04 19:21:36 +02:00
|
|
|
m_box = box_;
|
2023-04-03 17:01:05 +01:00
|
|
|
}
|
2024-07-21 13:09:54 +02:00
|
|
|
|
2025-05-04 19:21:36 +02:00
|
|
|
m_box.transform(wlTransformToHyprutils(m_monitor->m_transform), m_monitor->m_transformedSize.x, m_monitor->m_transformedSize.y).scale(m_monitor->m_scale).round();
|
2023-04-03 17:01:05 +01:00
|
|
|
|
2025-05-04 19:21:36 +02:00
|
|
|
m_shmStride = NFormatUtils::minStride(PSHMINFO, m_box.w);
|
2023-04-03 17:01:05 +01:00
|
|
|
|
2025-05-04 19:21:36 +02:00
|
|
|
m_resource->sendBuffer(NFormatUtils::drmToShm(m_shmFormat), m_box.width, m_box.height, m_shmStride);
|
2023-04-03 17:01:05 +01:00
|
|
|
|
2025-05-04 19:21:36 +02:00
|
|
|
if (m_resource->version() >= 3) {
|
|
|
|
|
if LIKELY (m_dmabufFormat != DRM_FORMAT_INVALID)
|
|
|
|
|
m_resource->sendLinuxDmabuf(m_dmabufFormat, m_box.width, m_box.height);
|
2023-04-03 17:01:05 +01:00
|
|
|
|
2025-05-04 19:21:36 +02:00
|
|
|
m_resource->sendBufferDone();
|
2023-04-03 17:01:05 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-27 10:02:02 -05:00
|
|
|
void CScreencopyFrame::copy(CZwlrScreencopyFrameV1* pFrame, wl_resource* buffer_) {
|
2025-01-17 18:21:34 +01:00
|
|
|
if UNLIKELY (!good()) {
|
2024-07-27 10:02:02 -05:00
|
|
|
LOGM(ERR, "No frame in copyFrame??");
|
2023-04-03 17:01:05 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-04 19:21:36 +02:00
|
|
|
if UNLIKELY (!g_pCompositor->monitorExists(m_monitor.lock())) {
|
2024-07-27 10:02:02 -05:00
|
|
|
LOGM(ERR, "Client requested sharing of a monitor that is gone");
|
2025-05-04 19:21:36 +02:00
|
|
|
m_resource->sendFailed();
|
2024-02-07 23:47:14 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-27 10:02:02 -05:00
|
|
|
const auto PBUFFER = CWLBufferResource::fromResource(buffer_);
|
2025-01-17 18:21:34 +01:00
|
|
|
if UNLIKELY (!PBUFFER) {
|
2024-07-27 10:02:02 -05:00
|
|
|
LOGM(ERR, "Invalid buffer in {:x}", (uintptr_t)this);
|
2025-05-04 19:21:36 +02:00
|
|
|
m_resource->error(ZWLR_SCREENCOPY_FRAME_V1_ERROR_INVALID_BUFFER, "invalid buffer");
|
2024-07-27 10:02:02 -05:00
|
|
|
PROTO::screencopy->destroyResource(this);
|
2023-04-03 17:01:05 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-04 19:21:36 +02:00
|
|
|
if UNLIKELY (PBUFFER->m_buffer->size != m_box.size()) {
|
2024-07-27 10:02:02 -05:00
|
|
|
LOGM(ERR, "Invalid dimensions in {:x}", (uintptr_t)this);
|
2025-05-04 19:21:36 +02:00
|
|
|
m_resource->error(ZWLR_SCREENCOPY_FRAME_V1_ERROR_INVALID_BUFFER, "invalid buffer dimensions");
|
2024-07-27 10:02:02 -05:00
|
|
|
PROTO::screencopy->destroyResource(this);
|
2023-04-03 17:01:05 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-04 19:21:36 +02:00
|
|
|
if UNLIKELY (m_buffer) {
|
2024-07-27 10:02:02 -05:00
|
|
|
LOGM(ERR, "Buffer used in {:x}", (uintptr_t)this);
|
2025-05-04 19:21:36 +02:00
|
|
|
m_resource->error(ZWLR_SCREENCOPY_FRAME_V1_ERROR_ALREADY_USED, "frame already used");
|
2024-07-27 10:02:02 -05:00
|
|
|
PROTO::screencopy->destroyResource(this);
|
2023-04-03 17:01:05 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-03 18:54:50 +02:00
|
|
|
if (auto attrs = PBUFFER->m_buffer->dmabuf(); attrs.success) {
|
2025-05-04 19:21:36 +02:00
|
|
|
m_bufferDMA = true;
|
2023-04-03 17:01:05 +01:00
|
|
|
|
2025-05-04 19:21:36 +02:00
|
|
|
if (attrs.format != m_dmabufFormat) {
|
2024-07-27 10:02:02 -05:00
|
|
|
LOGM(ERR, "Invalid buffer dma format in {:x}", (uintptr_t)pFrame);
|
2025-05-04 19:21:36 +02:00
|
|
|
m_resource->error(ZWLR_SCREENCOPY_FRAME_V1_ERROR_INVALID_BUFFER, "invalid buffer format");
|
2024-07-27 10:02:02 -05:00
|
|
|
PROTO::screencopy->destroyResource(this);
|
2023-04-03 17:01:05 +01:00
|
|
|
return;
|
|
|
|
|
}
|
2025-05-03 18:54:50 +02:00
|
|
|
} else if (auto attrs = PBUFFER->m_buffer->shm(); attrs.success) {
|
2025-05-04 19:21:36 +02:00
|
|
|
if (attrs.format != m_shmFormat) {
|
2024-07-27 10:02:02 -05:00
|
|
|
LOGM(ERR, "Invalid buffer shm format in {:x}", (uintptr_t)pFrame);
|
2025-05-04 19:21:36 +02:00
|
|
|
m_resource->error(ZWLR_SCREENCOPY_FRAME_V1_ERROR_INVALID_BUFFER, "invalid buffer format");
|
2024-07-27 10:02:02 -05:00
|
|
|
PROTO::screencopy->destroyResource(this);
|
2023-04-03 17:01:05 +01:00
|
|
|
return;
|
2025-05-04 19:21:36 +02:00
|
|
|
} else if ((int)attrs.stride != m_shmStride) {
|
2024-07-27 10:02:02 -05:00
|
|
|
LOGM(ERR, "Invalid buffer shm stride in {:x}", (uintptr_t)pFrame);
|
2025-05-04 19:21:36 +02:00
|
|
|
m_resource->error(ZWLR_SCREENCOPY_FRAME_V1_ERROR_INVALID_BUFFER, "invalid buffer stride");
|
2024-07-27 10:02:02 -05:00
|
|
|
PROTO::screencopy->destroyResource(this);
|
2023-04-03 17:01:05 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2024-07-27 10:02:02 -05:00
|
|
|
LOGM(ERR, "Invalid buffer type in {:x}", (uintptr_t)pFrame);
|
2025-05-04 19:21:36 +02:00
|
|
|
m_resource->error(ZWLR_SCREENCOPY_FRAME_V1_ERROR_INVALID_BUFFER, "invalid buffer type");
|
2024-07-27 10:02:02 -05:00
|
|
|
PROTO::screencopy->destroyResource(this);
|
2023-04-03 17:01:05 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-04 19:21:36 +02:00
|
|
|
m_buffer = CHLBufferReference(PBUFFER->m_buffer.lock());
|
2023-04-03 17:01:05 +01:00
|
|
|
|
2025-05-04 19:21:36 +02:00
|
|
|
PROTO::screencopy->m_framesAwaitingWrite.emplace_back(m_self);
|
2023-04-03 17:01:05 +01:00
|
|
|
|
|
|
|
|
g_pHyprRenderer->m_bDirectScanoutBlocked = true;
|
|
|
|
|
|
2025-05-04 19:21:36 +02:00
|
|
|
if (!m_withDamage)
|
|
|
|
|
g_pHyprRenderer->damageMonitor(m_monitor.lock());
|
2023-04-03 17:01:05 +01:00
|
|
|
}
|
|
|
|
|
|
2024-07-27 10:02:02 -05:00
|
|
|
void CScreencopyFrame::share() {
|
2025-05-04 19:21:36 +02:00
|
|
|
if (!m_buffer || !m_monitor)
|
2023-04-03 17:01:05 +01:00
|
|
|
return;
|
|
|
|
|
|
2025-04-16 01:37:48 +01:00
|
|
|
const auto NOW = Time::steadyNow();
|
2023-04-03 17:01:05 +01:00
|
|
|
|
2025-05-04 19:21:36 +02:00
|
|
|
auto callback = [this, NOW, weak = m_self](bool success) {
|
2025-03-22 11:01:14 -05:00
|
|
|
if (weak.expired())
|
2023-04-03 17:01:05 +01:00
|
|
|
return;
|
2025-03-22 11:01:14 -05:00
|
|
|
|
|
|
|
|
if (!success) {
|
2025-05-04 19:21:36 +02:00
|
|
|
LOGM(ERR, "{} copy failed in {:x}", m_bufferDMA ? "Dmabuf" : "Shm", (uintptr_t)this);
|
|
|
|
|
m_resource->sendFailed();
|
2023-04-03 17:01:05 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-04 19:21:36 +02:00
|
|
|
m_resource->sendFlags((zwlrScreencopyFrameV1Flags)0);
|
|
|
|
|
if (m_withDamage) {
|
2025-03-22 11:01:14 -05:00
|
|
|
// TODO: add a damage ring for this.
|
2025-05-04 19:21:36 +02:00
|
|
|
m_resource->sendDamage(0, 0, m_buffer->size.x, m_buffer->size.y);
|
2025-03-22 11:01:14 -05:00
|
|
|
}
|
2024-07-27 10:02:02 -05:00
|
|
|
|
2025-04-16 01:37:48 +01:00
|
|
|
const auto [sec, nsec] = Time::secNsec(NOW);
|
|
|
|
|
|
|
|
|
|
uint32_t tvSecHi = (sizeof(sec) > 4) ? sec >> 32 : 0;
|
|
|
|
|
uint32_t tvSecLo = sec & 0xFFFFFFFF;
|
2025-05-04 19:21:36 +02:00
|
|
|
m_resource->sendReady(tvSecHi, tvSecLo, nsec);
|
2025-03-22 11:01:14 -05:00
|
|
|
};
|
|
|
|
|
|
2025-05-04 19:21:36 +02:00
|
|
|
if (m_bufferDMA)
|
2025-03-22 11:01:14 -05:00
|
|
|
copyDmabuf(callback);
|
|
|
|
|
else
|
|
|
|
|
callback(copyShm());
|
2023-04-03 17:01:05 +01:00
|
|
|
}
|
2023-04-12 21:40:51 +01:00
|
|
|
|
2025-03-22 11:01:14 -05:00
|
|
|
void CScreencopyFrame::copyDmabuf(std::function<void(bool)> callback) {
|
2025-05-04 19:21:36 +02:00
|
|
|
const auto PERM = g_pDynamicPermissionManager->clientPermissionMode(m_resource->client(), PERMISSION_TYPE_SCREENCOPY);
|
|
|
|
|
auto TEXTURE = makeShared<CTexture>(m_monitor->m_output->state->state().buffer);
|
2023-04-03 17:01:05 +01:00
|
|
|
|
2025-04-08 19:39:53 +02:00
|
|
|
CRegion fakeDamage = {0, 0, INT16_MAX, INT16_MAX};
|
2024-07-27 10:02:02 -05:00
|
|
|
|
2025-05-04 19:21:36 +02:00
|
|
|
if (!g_pHyprRenderer->beginRender(m_monitor.lock(), fakeDamage, RENDER_MODE_TO_BUFFER, m_buffer.m_buffer, nullptr, true)) {
|
2024-07-27 10:02:02 -05:00
|
|
|
LOGM(ERR, "Can't copy: failed to begin rendering to dma frame");
|
2025-03-22 11:01:14 -05:00
|
|
|
callback(false);
|
|
|
|
|
return;
|
2024-07-27 10:02:02 -05:00
|
|
|
}
|
2023-07-19 13:10:31 +02:00
|
|
|
|
2025-04-08 19:39:53 +02:00
|
|
|
if (PERM == PERMISSION_RULE_ALLOW_MODE_ALLOW) {
|
2025-05-04 19:21:36 +02:00
|
|
|
CBox monbox = CBox{0, 0, m_monitor->m_pixelSize.x, m_monitor->m_pixelSize.y}
|
|
|
|
|
.translate({-m_box.x, -m_box.y}) // vvvv kinda ass-backwards but that's how I designed the renderer... sigh.
|
|
|
|
|
.transform(wlTransformToHyprutils(invertTransform(m_monitor->m_transform)), m_monitor->m_pixelSize.x, m_monitor->m_pixelSize.y);
|
2025-04-08 19:39:53 +02:00
|
|
|
g_pHyprOpenGL->setMonitorTransformEnabled(true);
|
|
|
|
|
g_pHyprOpenGL->setRenderModifEnabled(false);
|
|
|
|
|
g_pHyprOpenGL->renderTexture(TEXTURE, monbox, 1);
|
|
|
|
|
g_pHyprOpenGL->setRenderModifEnabled(true);
|
|
|
|
|
g_pHyprOpenGL->setMonitorTransformEnabled(false);
|
2025-05-04 19:21:36 +02:00
|
|
|
if (m_overlayCursor)
|
|
|
|
|
g_pPointerManager->renderSoftwareCursorsFor(m_monitor.lock(), Time::steadyNow(), fakeDamage,
|
|
|
|
|
g_pInputManager->getMouseCoordsInternal() - m_monitor->m_position - m_box.pos(), true);
|
2025-04-08 19:39:53 +02:00
|
|
|
} else if (PERM == PERMISSION_RULE_ALLOW_MODE_PENDING)
|
|
|
|
|
g_pHyprOpenGL->clear(Colors::BLACK);
|
|
|
|
|
else {
|
|
|
|
|
g_pHyprOpenGL->clear(Colors::BLACK);
|
|
|
|
|
CBox texbox =
|
2025-05-04 19:21:36 +02:00
|
|
|
CBox{m_monitor->m_transformedSize / 2.F, g_pHyprOpenGL->m_pScreencopyDeniedTexture->m_vSize}.translate(-g_pHyprOpenGL->m_pScreencopyDeniedTexture->m_vSize / 2.F);
|
2025-04-08 19:39:53 +02:00
|
|
|
g_pHyprOpenGL->renderTexture(g_pHyprOpenGL->m_pScreencopyDeniedTexture, texbox, 1);
|
|
|
|
|
}
|
2023-07-19 13:10:31 +02:00
|
|
|
|
2024-07-27 10:02:02 -05:00
|
|
|
g_pHyprOpenGL->m_RenderData.blockScreenShader = true;
|
2025-04-30 11:35:25 -05:00
|
|
|
|
|
|
|
|
g_pHyprRenderer->endRender([callback]() {
|
|
|
|
|
LOGM(TRACE, "Copied frame via dma");
|
|
|
|
|
callback(true);
|
|
|
|
|
});
|
2023-04-03 17:01:05 +01:00
|
|
|
}
|
|
|
|
|
|
2024-07-27 10:02:02 -05:00
|
|
|
bool CScreencopyFrame::copyShm() {
|
2025-05-04 19:21:36 +02:00
|
|
|
const auto PERM = g_pDynamicPermissionManager->clientPermissionMode(m_resource->client(), PERMISSION_TYPE_SCREENCOPY);
|
|
|
|
|
auto TEXTURE = makeShared<CTexture>(m_monitor->m_output->state->state().buffer);
|
2024-06-08 10:07:59 +02:00
|
|
|
|
2025-05-04 19:21:36 +02:00
|
|
|
auto shm = m_buffer->shm();
|
|
|
|
|
auto [pixelData, fmt, bufLen] = m_buffer->beginDataPtr(0); // no need for end, cuz it's shm
|
2023-11-30 10:14:35 +00:00
|
|
|
|
|
|
|
|
CRegion fakeDamage = {0, 0, INT16_MAX, INT16_MAX};
|
|
|
|
|
|
|
|
|
|
g_pHyprRenderer->makeEGLCurrent();
|
|
|
|
|
|
|
|
|
|
CFramebuffer fb;
|
2025-05-04 19:21:36 +02:00
|
|
|
fb.alloc(m_box.w, m_box.h, m_monitor->m_output->state->state().drmFormat);
|
2023-11-30 10:14:35 +00:00
|
|
|
|
2025-05-04 19:21:36 +02:00
|
|
|
if (!g_pHyprRenderer->beginRender(m_monitor.lock(), fakeDamage, RENDER_MODE_FULL_FAKE, nullptr, &fb, true)) {
|
2024-07-27 10:02:02 -05:00
|
|
|
LOGM(ERR, "Can't copy: failed to begin rendering");
|
2023-11-30 10:14:35 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-08 19:39:53 +02:00
|
|
|
if (PERM == PERMISSION_RULE_ALLOW_MODE_ALLOW) {
|
2025-05-04 19:21:36 +02:00
|
|
|
CBox monbox = CBox{0, 0, m_monitor->m_transformedSize.x, m_monitor->m_transformedSize.y}.translate({-m_box.x, -m_box.y});
|
2025-04-08 19:39:53 +02:00
|
|
|
g_pHyprOpenGL->setMonitorTransformEnabled(true);
|
|
|
|
|
g_pHyprOpenGL->setRenderModifEnabled(false);
|
|
|
|
|
g_pHyprOpenGL->renderTexture(TEXTURE, monbox, 1);
|
|
|
|
|
g_pHyprOpenGL->setRenderModifEnabled(true);
|
|
|
|
|
g_pHyprOpenGL->setMonitorTransformEnabled(false);
|
2025-05-04 19:21:36 +02:00
|
|
|
if (m_overlayCursor)
|
|
|
|
|
g_pPointerManager->renderSoftwareCursorsFor(m_monitor.lock(), Time::steadyNow(), fakeDamage,
|
|
|
|
|
g_pInputManager->getMouseCoordsInternal() - m_monitor->m_position - m_box.pos(), true);
|
2025-04-08 19:39:53 +02:00
|
|
|
} else if (PERM == PERMISSION_RULE_ALLOW_MODE_PENDING)
|
|
|
|
|
g_pHyprOpenGL->clear(Colors::BLACK);
|
|
|
|
|
else {
|
|
|
|
|
g_pHyprOpenGL->clear(Colors::BLACK);
|
|
|
|
|
CBox texbox =
|
2025-05-04 19:21:36 +02:00
|
|
|
CBox{m_monitor->m_transformedSize / 2.F, g_pHyprOpenGL->m_pScreencopyDeniedTexture->m_vSize}.translate(-g_pHyprOpenGL->m_pScreencopyDeniedTexture->m_vSize / 2.F);
|
2025-04-08 19:39:53 +02:00
|
|
|
g_pHyprOpenGL->renderTexture(g_pHyprOpenGL->m_pScreencopyDeniedTexture, texbox, 1);
|
|
|
|
|
}
|
2023-11-30 10:14:35 +00:00
|
|
|
|
2023-12-05 14:59:12 +00:00
|
|
|
#ifndef GLES2
|
2024-11-02 15:26:25 +00:00
|
|
|
glBindFramebuffer(GL_READ_FRAMEBUFFER, fb.getFBID());
|
2023-12-05 14:59:12 +00:00
|
|
|
#else
|
2024-11-05 09:59:03 +00:00
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, fb.getFBID());
|
2023-12-05 14:59:12 +00:00
|
|
|
#endif
|
2023-11-30 10:14:35 +00:00
|
|
|
|
2024-12-07 18:51:18 +01:00
|
|
|
const auto PFORMAT = NFormatUtils::getPixelFormatFromDRM(shm.format);
|
2023-12-04 03:52:54 +00:00
|
|
|
if (!PFORMAT) {
|
2024-07-27 10:02:02 -05:00
|
|
|
LOGM(ERR, "Can't copy: failed to find a pixel format");
|
2023-12-04 03:52:54 +00:00
|
|
|
g_pHyprRenderer->endRender();
|
|
|
|
|
return false;
|
2023-12-03 22:04:07 +00:00
|
|
|
}
|
2023-11-30 10:14:35 +00:00
|
|
|
|
2024-06-08 10:07:59 +02:00
|
|
|
auto glFormat = PFORMAT->flipRB ? GL_BGRA_EXT : GL_RGBA;
|
|
|
|
|
|
2024-04-03 21:35:16 +01:00
|
|
|
g_pHyprOpenGL->m_RenderData.blockScreenShader = true;
|
2023-11-30 11:07:17 +00:00
|
|
|
g_pHyprRenderer->endRender();
|
|
|
|
|
|
|
|
|
|
g_pHyprRenderer->makeEGLCurrent();
|
2025-05-04 19:21:36 +02:00
|
|
|
g_pHyprOpenGL->m_RenderData.pMonitor = m_monitor;
|
2023-11-30 11:07:17 +00:00
|
|
|
fb.bind();
|
|
|
|
|
|
2023-12-03 22:04:07 +00:00
|
|
|
glPixelStorei(GL_PACK_ALIGNMENT, 1);
|
|
|
|
|
|
2024-12-07 18:51:18 +01:00
|
|
|
const auto drmFmt = NFormatUtils::getPixelFormatFromDRM(shm.format);
|
2025-05-04 19:21:36 +02:00
|
|
|
uint32_t packStride = NFormatUtils::minStride(drmFmt, m_box.w);
|
2023-12-03 22:04:07 +00:00
|
|
|
|
2025-03-22 11:01:14 -05:00
|
|
|
// This could be optimized by using a pixel buffer object to make this async,
|
|
|
|
|
// but really clients should just use a dma buffer anyways.
|
2024-06-08 10:07:59 +02:00
|
|
|
if (packStride == (uint32_t)shm.stride) {
|
2025-05-04 19:21:36 +02:00
|
|
|
glReadPixels(0, 0, m_box.w, m_box.h, glFormat, PFORMAT->glType, pixelData);
|
2023-12-03 22:04:07 +00:00
|
|
|
} else {
|
2025-05-04 19:21:36 +02:00
|
|
|
for (size_t i = 0; i < m_box.h; ++i) {
|
2023-12-05 20:04:53 +00:00
|
|
|
uint32_t y = i;
|
2025-05-04 19:21:36 +02:00
|
|
|
glReadPixels(0, y, m_box.w, 1, glFormat, PFORMAT->glType, ((unsigned char*)pixelData) + i * shm.stride);
|
2023-12-03 22:04:07 +00:00
|
|
|
}
|
|
|
|
|
}
|
2023-11-30 10:14:35 +00:00
|
|
|
|
2024-10-19 23:03:29 +01:00
|
|
|
g_pHyprOpenGL->m_RenderData.pMonitor.reset();
|
2023-11-30 10:14:35 +00:00
|
|
|
|
2025-02-08 01:46:26 +01:00
|
|
|
#ifndef GLES2
|
|
|
|
|
glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);
|
|
|
|
|
#else
|
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
|
|
|
|
#endif
|
|
|
|
|
|
2024-07-27 10:02:02 -05:00
|
|
|
LOGM(TRACE, "Copied frame via shm");
|
2023-11-30 10:14:35 +00:00
|
|
|
|
|
|
|
|
return true;
|
2023-04-03 17:01:05 +01:00
|
|
|
}
|
|
|
|
|
|
2024-07-27 10:02:02 -05:00
|
|
|
bool CScreencopyFrame::good() {
|
2025-05-04 19:21:36 +02:00
|
|
|
return m_resource->resource();
|
2024-07-27 10:02:02 -05:00
|
|
|
}
|
2024-06-08 10:07:59 +02:00
|
|
|
|
2024-07-27 10:02:02 -05:00
|
|
|
CScreencopyClient::~CScreencopyClient() {
|
2025-05-04 19:21:36 +02:00
|
|
|
g_pHookSystem->unhook(m_tickCallback);
|
2024-07-27 10:02:02 -05:00
|
|
|
}
|
2023-11-24 10:54:21 +00:00
|
|
|
|
2025-05-04 19:21:36 +02:00
|
|
|
CScreencopyClient::CScreencopyClient(SP<CZwlrScreencopyManagerV1> resource_) : m_resource(resource_) {
|
2025-01-17 18:21:34 +01:00
|
|
|
if UNLIKELY (!good())
|
2024-07-27 10:02:02 -05:00
|
|
|
return;
|
|
|
|
|
|
2025-05-04 19:21:36 +02:00
|
|
|
m_resource->setDestroy([this](CZwlrScreencopyManagerV1* pMgr) { PROTO::screencopy->destroyResource(this); });
|
|
|
|
|
m_resource->setOnDestroy([this](CZwlrScreencopyManagerV1* pMgr) { PROTO::screencopy->destroyResource(this); });
|
|
|
|
|
m_resource->setCaptureOutput(
|
2024-07-27 10:02:02 -05:00
|
|
|
[this](CZwlrScreencopyManagerV1* pMgr, uint32_t frame, int32_t overlayCursor, wl_resource* output) { this->captureOutput(frame, overlayCursor, output, {}); });
|
2025-05-04 19:21:36 +02:00
|
|
|
m_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}); });
|
2024-07-27 10:02:02 -05:00
|
|
|
|
2025-05-04 19:21:36 +02:00
|
|
|
m_lastMeasure.reset();
|
|
|
|
|
m_lastFrame.reset();
|
|
|
|
|
m_tickCallback = g_pHookSystem->hookDynamic("tick", [&](void* self, SCallbackInfo& info, std::any data) { onTick(); });
|
2024-07-27 10:02:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CScreencopyClient::captureOutput(uint32_t frame, int32_t overlayCursor_, wl_resource* output, CBox box) {
|
2025-05-04 19:21:36 +02:00
|
|
|
const auto FRAME = PROTO::screencopy->m_frames.emplace_back(
|
|
|
|
|
makeShared<CScreencopyFrame>(makeShared<CZwlrScreencopyFrameV1>(m_resource->client(), m_resource->version(), frame), overlayCursor_, output, box));
|
2024-07-27 10:02:02 -05:00
|
|
|
|
|
|
|
|
if (!FRAME->good()) {
|
|
|
|
|
LOGM(ERR, "Couldn't alloc frame for sharing! (no memory)");
|
2025-05-04 19:21:36 +02:00
|
|
|
m_resource->noMemory();
|
2024-07-27 10:02:02 -05:00
|
|
|
PROTO::screencopy->destroyResource(FRAME.get());
|
|
|
|
|
return;
|
2024-07-21 13:09:54 +02:00
|
|
|
}
|
2023-04-12 21:40:51 +01:00
|
|
|
|
2025-05-04 19:21:36 +02:00
|
|
|
FRAME->m_self = FRAME;
|
|
|
|
|
FRAME->m_client = m_self;
|
2024-07-27 10:02:02 -05:00
|
|
|
}
|
2023-04-12 21:40:51 +01:00
|
|
|
|
2024-07-27 10:02:02 -05:00
|
|
|
void CScreencopyClient::onTick() {
|
2025-05-04 19:21:36 +02:00
|
|
|
if (m_lastMeasure.getMillis() < 500)
|
2024-07-27 10:02:02 -05:00
|
|
|
return;
|
2023-04-12 21:40:51 +01:00
|
|
|
|
2025-05-04 19:21:36 +02:00
|
|
|
m_framesInLastHalfSecond = m_frameCounter;
|
|
|
|
|
m_frameCounter = 0;
|
|
|
|
|
m_lastMeasure.reset();
|
|
|
|
|
|
|
|
|
|
const auto LASTFRAMEDELTA = m_lastFrame.getMillis() / 1000.0;
|
|
|
|
|
const bool FRAMEAWAITING = std::ranges::any_of(PROTO::screencopy->m_frames, [&](const auto& frame) { return frame->m_client.get() == this; });
|
|
|
|
|
|
|
|
|
|
if (m_framesInLastHalfSecond > 3 && !m_sentScreencast) {
|
|
|
|
|
EMIT_HOOK_EVENT("screencast", (std::vector<uint64_t>{1, (uint64_t)m_framesInLastHalfSecond, (uint64_t)m_clientOwner}));
|
|
|
|
|
g_pEventManager->postEvent(SHyprIPCEvent{"screencast", "1," + std::to_string(m_clientOwner)});
|
|
|
|
|
m_sentScreencast = true;
|
|
|
|
|
} else if (m_framesInLastHalfSecond < 4 && m_sentScreencast && LASTFRAMEDELTA > 1.0 && !FRAMEAWAITING) {
|
|
|
|
|
EMIT_HOOK_EVENT("screencast", (std::vector<uint64_t>{0, (uint64_t)m_framesInLastHalfSecond, (uint64_t)m_clientOwner}));
|
|
|
|
|
g_pEventManager->postEvent(SHyprIPCEvent{"screencast", "0," + std::to_string(m_clientOwner)});
|
|
|
|
|
m_sentScreencast = false;
|
2024-07-27 10:02:02 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool CScreencopyClient::good() {
|
2025-05-04 19:21:36 +02:00
|
|
|
return m_resource->resource();
|
2024-07-27 10:02:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CScreencopyProtocol::CScreencopyProtocol(const wl_interface* iface, const int& ver, const std::string& name) : IWaylandProtocol(iface, ver, name) {
|
2025-04-28 20:18:02 +01:00
|
|
|
;
|
2024-07-27 10:02:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CScreencopyProtocol::bindManager(wl_client* client, void* data, uint32_t ver, uint32_t id) {
|
2025-05-04 19:21:36 +02:00
|
|
|
const auto CLIENT = m_clients.emplace_back(makeShared<CScreencopyClient>(makeShared<CZwlrScreencopyManagerV1>(client, ver, id)));
|
2024-07-27 10:02:02 -05:00
|
|
|
|
|
|
|
|
if (!CLIENT->good()) {
|
|
|
|
|
LOGM(LOG, "Failed to bind client! (out of memory)");
|
2025-05-04 19:21:36 +02:00
|
|
|
CLIENT->m_resource->noMemory();
|
|
|
|
|
m_clients.pop_back();
|
2024-07-27 10:02:02 -05:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-04 19:21:36 +02:00
|
|
|
CLIENT->m_self = CLIENT;
|
2024-07-27 10:02:02 -05:00
|
|
|
|
|
|
|
|
LOGM(LOG, "Bound client successfully!");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CScreencopyProtocol::destroyResource(CScreencopyClient* client) {
|
2025-05-04 19:21:36 +02:00
|
|
|
std::erase_if(m_clients, [&](const auto& other) { return other.get() == client; });
|
|
|
|
|
std::erase_if(m_frames, [&](const auto& other) { return other->m_client.get() == client; });
|
|
|
|
|
std::erase_if(m_framesAwaitingWrite, [&](const auto& other) { return !other || other->m_client.get() == client; });
|
2024-07-27 10:02:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CScreencopyProtocol::destroyResource(CScreencopyFrame* frame) {
|
2025-05-04 19:21:36 +02:00
|
|
|
std::erase_if(m_frames, [&](const auto& other) { return other.get() == frame; });
|
|
|
|
|
std::erase_if(m_framesAwaitingWrite, [&](const auto& other) { return !other || other.get() == frame; });
|
2024-07-27 10:02:02 -05:00
|
|
|
}
|
|
|
|
|
|
2024-10-19 23:03:29 +01:00
|
|
|
void CScreencopyProtocol::onOutputCommit(PHLMONITOR pMonitor) {
|
2025-05-04 19:21:36 +02:00
|
|
|
if (m_framesAwaitingWrite.empty()) {
|
2024-07-27 10:02:02 -05:00
|
|
|
g_pHyprRenderer->m_bDirectScanoutBlocked = false;
|
|
|
|
|
return; // nothing to share
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::vector<WP<CScreencopyFrame>> framesToRemove;
|
2025-01-19 10:38:42 +00:00
|
|
|
// reserve number of elements to avoid reallocations
|
2025-05-04 19:21:36 +02:00
|
|
|
framesToRemove.reserve(m_framesAwaitingWrite.size());
|
2024-07-27 10:02:02 -05:00
|
|
|
|
|
|
|
|
// share frame if correct output
|
2025-05-04 19:21:36 +02:00
|
|
|
for (auto const& f : m_framesAwaitingWrite) {
|
2024-10-07 19:23:48 -05:00
|
|
|
if (!f)
|
|
|
|
|
continue;
|
|
|
|
|
|
2025-04-08 19:39:53 +02:00
|
|
|
// check permissions
|
2025-05-04 19:21:36 +02:00
|
|
|
const auto PERM = g_pDynamicPermissionManager->clientPermissionMode(f->m_resource->client(), PERMISSION_TYPE_SCREENCOPY);
|
2025-04-08 19:39:53 +02:00
|
|
|
|
|
|
|
|
if (PERM == PERMISSION_RULE_ALLOW_MODE_PENDING)
|
|
|
|
|
continue; // pending an answer, don't do anything yet.
|
|
|
|
|
|
|
|
|
|
// otherwise share. If it's denied, it will be black.
|
|
|
|
|
|
2025-05-04 19:21:36 +02:00
|
|
|
if (!f->m_monitor || !f->m_buffer) {
|
2024-10-07 19:23:48 -05:00
|
|
|
framesToRemove.emplace_back(f);
|
2024-07-27 10:02:02 -05:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-04 19:21:36 +02:00
|
|
|
if (f->m_monitor != pMonitor)
|
2024-07-27 10:02:02 -05:00
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
f->share();
|
|
|
|
|
|
2025-05-04 19:21:36 +02:00
|
|
|
f->m_client->m_lastFrame.reset();
|
|
|
|
|
++f->m_client->m_frameCounter;
|
2024-07-27 10:02:02 -05:00
|
|
|
|
2024-10-07 19:23:48 -05:00
|
|
|
framesToRemove.emplace_back(f);
|
2024-07-27 10:02:02 -05:00
|
|
|
}
|
|
|
|
|
|
2024-08-26 20:24:30 +02:00
|
|
|
for (auto const& f : framesToRemove) {
|
2025-05-04 19:21:36 +02:00
|
|
|
std::erase(m_framesAwaitingWrite, f);
|
2024-07-27 10:02:02 -05:00
|
|
|
}
|
2023-04-07 19:11:30 +01:00
|
|
|
}
|