2023-04-03 18:01:05 +02:00
|
|
|
#include "Screencopy.hpp"
|
|
|
|
#include "../Compositor.hpp"
|
2024-05-11 23:10:42 +02:00
|
|
|
#include "../managers/eventLoop/EventLoopManager.hpp"
|
2024-05-05 23:18:10 +02:00
|
|
|
#include "../managers/PointerManager.hpp"
|
2023-04-03 18:01:05 +02:00
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
#include "ToplevelExportWlrFuncs.hpp"
|
|
|
|
|
|
|
|
#define SCREENCOPY_VERSION 3
|
|
|
|
|
|
|
|
static void bindManagerInt(wl_client* client, void* data, uint32_t version, uint32_t id) {
|
|
|
|
g_pProtocolManager->m_pScreencopyProtocolManager->bindManager(client, data, version, id);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void handleDisplayDestroy(struct wl_listener* listener, void* data) {
|
|
|
|
g_pProtocolManager->m_pScreencopyProtocolManager->displayDestroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CScreencopyProtocolManager::displayDestroy() {
|
|
|
|
wl_global_destroy(m_pGlobal);
|
|
|
|
}
|
|
|
|
|
|
|
|
static SScreencopyFrame* frameFromResource(wl_resource*);
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_liDisplayDestroy.notify = handleDisplayDestroy;
|
|
|
|
wl_display_add_destroy_listener(g_pCompositor->m_sWLDisplay, &m_liDisplayDestroy);
|
|
|
|
|
|
|
|
Debug::log(LOG, "ScreencopyProtocolManager started successfully!");
|
2024-05-11 23:10:42 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
Debug::log(LOG, "[screencopy] Releasing software cursor lock");
|
|
|
|
},
|
|
|
|
nullptr);
|
|
|
|
g_pEventLoopManager->addTimer(m_pSoftwareCursorTimer);
|
2023-04-03 18:01:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2023-04-12 22:40:51 +02:00
|
|
|
const auto PFRAME = frameFromResource(resource);
|
|
|
|
|
|
|
|
if (!PFRAME)
|
|
|
|
return;
|
|
|
|
|
2023-04-03 18:01:05 +02:00
|
|
|
g_pProtocolManager->m_pScreencopyProtocolManager->copyFrame(client, resource, buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void handleCopyWithDamage(wl_client* client, wl_resource* resource, wl_resource* buffer) {
|
2023-04-12 22:40:51 +02:00
|
|
|
const auto PFRAME = frameFromResource(resource);
|
|
|
|
|
|
|
|
if (!PFRAME)
|
|
|
|
return;
|
|
|
|
|
2023-04-03 18:01:05 +02:00
|
|
|
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,
|
|
|
|
};
|
|
|
|
|
2023-04-16 00:43:41 +02:00
|
|
|
static CScreencopyClient* clientFromResource(wl_resource* resource) {
|
2023-04-03 18:01:05 +02:00
|
|
|
ASSERT(wl_resource_instance_of(resource, &zwlr_screencopy_manager_v1_interface, &screencopyMgrImpl));
|
2023-04-16 00:43:41 +02:00
|
|
|
return (CScreencopyClient*)wl_resource_get_user_data(resource);
|
2023-04-03 18:01:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2023-04-16 00:43:41 +02:00
|
|
|
void CScreencopyProtocolManager::removeClient(CScreencopyClient* client, bool force) {
|
2024-02-27 13:23:59 +01:00
|
|
|
if (!client)
|
|
|
|
return;
|
|
|
|
|
2023-04-03 18:01:05 +02:00
|
|
|
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???
|
2024-02-27 13:23:59 +01:00
|
|
|
|
|
|
|
for (auto& f : m_lFrames) {
|
|
|
|
// avoid dangling ptrs
|
|
|
|
if (f.client == client)
|
|
|
|
f.client = nullptr;
|
|
|
|
}
|
2023-04-03 18:01:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void handleManagerResourceDestroy(wl_resource* resource) {
|
|
|
|
const auto PCLIENT = clientFromResource(resource);
|
|
|
|
|
|
|
|
g_pProtocolManager->m_pScreencopyProtocolManager->removeClient(PCLIENT, true);
|
|
|
|
}
|
|
|
|
|
2023-04-16 00:43:41 +02:00
|
|
|
CScreencopyClient::~CScreencopyClient() {
|
|
|
|
g_pHookSystem->unhook(tickCallback);
|
|
|
|
}
|
|
|
|
|
|
|
|
CScreencopyClient::CScreencopyClient() {
|
2023-04-17 23:57:24 +02:00
|
|
|
lastMeasure.reset();
|
|
|
|
lastFrame.reset();
|
2023-10-21 15:52:43 +02:00
|
|
|
tickCallback = g_pHookSystem->hookDynamic("tick", [&](void* self, SCallbackInfo& info, std::any data) { onTick(); });
|
2023-04-16 00:43:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CScreencopyClient::onTick() {
|
2023-04-17 23:57:24 +02:00
|
|
|
if (lastMeasure.getMillis() < 500)
|
2023-04-16 00:43:41 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
framesInLastHalfSecond = frameCounter;
|
|
|
|
frameCounter = 0;
|
2023-04-17 23:57:24 +02:00
|
|
|
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; });
|
2023-04-16 00:43:41 +02:00
|
|
|
|
|
|
|
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;
|
2023-04-17 23:57:24 +02:00
|
|
|
} else if (framesInLastHalfSecond < 4 && sentScreencast && LASTFRAMEDELTA > 1.0 && !FRAMEAWAITING) {
|
2023-04-16 00:43:41 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-03 18:01:05 +02:00
|
|
|
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);
|
2023-04-07 19:04:02 +02:00
|
|
|
if (frame->buffer && frame->buffer->n_locks > 0)
|
|
|
|
wlr_buffer_unlock(frame->buffer);
|
2023-04-03 18:01:05 +02:00
|
|
|
removeClient(frame->client, force);
|
|
|
|
m_lFrames.remove(*frame);
|
|
|
|
}
|
|
|
|
|
2023-11-04 18:03:05 +01:00
|
|
|
void CScreencopyProtocolManager::captureOutput(wl_client* client, wl_resource* resource, uint32_t frame, int32_t overlay_cursor, wl_resource* output, CBox box) {
|
2023-04-03 18:01:05 +02:00
|
|
|
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 = g_pCompositor->getMonitorFromOutput(wlr_output_from_resource(output));
|
|
|
|
|
|
|
|
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++;
|
|
|
|
|
2023-12-03 23:04:07 +01:00
|
|
|
g_pHyprRenderer->makeEGLCurrent();
|
2024-02-15 01:58:58 +01:00
|
|
|
|
|
|
|
if (g_pHyprOpenGL->m_mMonitorRenderResources.contains(PFRAME->pMonitor)) {
|
2024-02-15 03:01:40 +01:00
|
|
|
const auto& RDATA = g_pHyprOpenGL->m_mMonitorRenderResources.at(PFRAME->pMonitor);
|
2024-02-15 01:58:58 +01:00
|
|
|
// bind the fb for its format. Suppress gl errors.
|
|
|
|
#ifndef GLES2
|
|
|
|
glBindFramebuffer(GL_READ_FRAMEBUFFER, RDATA.offloadFB.m_iFb);
|
|
|
|
#else
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, RDATA.offloadFB.m_iFb);
|
|
|
|
#endif
|
|
|
|
} else
|
|
|
|
Debug::log(ERR, "No RDATA in screencopy???");
|
|
|
|
|
2023-12-01 18:20:56 +01:00
|
|
|
PFRAME->shmFormat = g_pHyprOpenGL->getPreferredReadFormat(PFRAME->pMonitor);
|
2023-04-03 18:01:05 +02:00
|
|
|
if (PFRAME->shmFormat == DRM_FORMAT_INVALID) {
|
|
|
|
Debug::log(ERR, "No format supported by renderer in capture output");
|
2023-04-05 16:19:49 +02:00
|
|
|
zwlr_screencopy_frame_v1_send_failed(PFRAME->resource);
|
2023-04-03 18:01:05 +02:00
|
|
|
removeFrame(PFRAME);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto PSHMINFO = drm_get_pixel_format_info(PFRAME->shmFormat);
|
|
|
|
if (!PSHMINFO) {
|
|
|
|
Debug::log(ERR, "No pixel format supported by renderer in capture output");
|
2023-04-05 16:19:49 +02:00
|
|
|
zwlr_screencopy_frame_v1_send_failed(PFRAME->resource);
|
2023-04-03 18:01:05 +02:00
|
|
|
removeFrame(PFRAME);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (PFRAME->pMonitor->output->allocator && (PFRAME->pMonitor->output->allocator->buffer_caps & WLR_BUFFER_CAP_DMABUF)) {
|
|
|
|
PFRAME->dmabufFormat = PFRAME->pMonitor->output->render_format;
|
|
|
|
} else {
|
|
|
|
PFRAME->dmabufFormat = DRM_FORMAT_INVALID;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (box.width == 0 && box.height == 0)
|
2023-07-04 11:40:28 +02:00
|
|
|
PFRAME->box = {0, 0, (int)(PFRAME->pMonitor->vecSize.x), (int)(PFRAME->pMonitor->vecSize.y)};
|
2023-04-03 18:01:05 +02:00
|
|
|
else {
|
|
|
|
PFRAME->box = box;
|
|
|
|
}
|
|
|
|
int ow, oh;
|
|
|
|
wlr_output_effective_resolution(PFRAME->pMonitor->output, &ow, &oh);
|
2023-11-08 13:43:07 +01:00
|
|
|
PFRAME->box.transform(PFRAME->pMonitor->transform, ow, oh).scale(PFRAME->pMonitor->scale).round();
|
2023-04-03 18:01:05 +02:00
|
|
|
|
2023-12-03 23:04:07 +01:00
|
|
|
PFRAME->shmStride = pixel_format_info_min_stride(PSHMINFO, PFRAME->box.w);
|
2023-04-03 18:01:05 +02:00
|
|
|
|
|
|
|
zwlr_screencopy_frame_v1_send_buffer(PFRAME->resource, convert_drm_format_to_wl_shm(PFRAME->shmFormat), PFRAME->box.width, PFRAME->box.height, PFRAME->shmStride);
|
|
|
|
|
|
|
|
if (wl_resource_get_version(resource) >= 3) {
|
2023-04-12 22:40:51 +02:00
|
|
|
if (PFRAME->dmabufFormat != DRM_FORMAT_INVALID) {
|
|
|
|
zwlr_screencopy_frame_v1_send_linux_dmabuf(PFRAME->resource, PFRAME->dmabufFormat, PFRAME->box.width, PFRAME->box.height);
|
|
|
|
}
|
2023-04-03 18:01:05 +02:00
|
|
|
|
|
|
|
zwlr_screencopy_frame_v1_send_buffer_done(PFRAME->resource);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CScreencopyProtocolManager::copyFrame(wl_client* client, wl_resource* resource, wl_resource* buffer) {
|
|
|
|
const auto PFRAME = frameFromResource(resource);
|
|
|
|
|
|
|
|
if (!PFRAME) {
|
|
|
|
Debug::log(ERR, "No frame in copyFrame??");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-02-08 00:47:14 +01:00
|
|
|
if (!g_pCompositor->monitorExists(PFRAME->pMonitor)) {
|
|
|
|
Debug::log(ERR, "client requested sharing of a monitor that is gone");
|
|
|
|
zwlr_screencopy_frame_v1_send_failed(PFRAME->resource);
|
|
|
|
removeFrame(PFRAME);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-10-25 01:27:16 +02:00
|
|
|
const auto PBUFFER = wlr_buffer_try_from_resource(buffer);
|
2023-04-03 18:01:05 +02:00
|
|
|
if (!PBUFFER) {
|
2023-10-10 00:51:55 +02:00
|
|
|
Debug::log(ERR, "[sc] invalid buffer in {:x}", (uintptr_t)PFRAME);
|
2023-04-03 18:01:05 +02:00
|
|
|
wl_resource_post_error(PFRAME->resource, ZWLR_SCREENCOPY_FRAME_V1_ERROR_INVALID_BUFFER, "invalid buffer");
|
|
|
|
removeFrame(PFRAME);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (PBUFFER->width != PFRAME->box.width || PBUFFER->height != PFRAME->box.height) {
|
2023-10-10 00:51:55 +02:00
|
|
|
Debug::log(ERR, "[sc] invalid dimensions in {:x}", (uintptr_t)PFRAME);
|
2023-04-03 18:01:05 +02:00
|
|
|
wl_resource_post_error(PFRAME->resource, ZWLR_SCREENCOPY_FRAME_V1_ERROR_INVALID_BUFFER, "invalid buffer dimensions");
|
|
|
|
removeFrame(PFRAME);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (PFRAME->buffer) {
|
2023-10-10 00:51:55 +02:00
|
|
|
Debug::log(ERR, "[sc] buffer used in {:x}", (uintptr_t)PFRAME);
|
2023-04-03 18:01:05 +02:00
|
|
|
wl_resource_post_error(PFRAME->resource, ZWLR_SCREENCOPY_FRAME_V1_ERROR_ALREADY_USED, "frame already used");
|
|
|
|
removeFrame(PFRAME);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
wlr_dmabuf_attributes dmabufAttrs;
|
|
|
|
void* wlrBufferAccessData;
|
|
|
|
uint32_t wlrBufferAccessFormat;
|
|
|
|
size_t wlrBufferAccessStride;
|
|
|
|
if (wlr_buffer_get_dmabuf(PBUFFER, &dmabufAttrs)) {
|
|
|
|
PFRAME->bufferCap = WLR_BUFFER_CAP_DMABUF;
|
|
|
|
|
|
|
|
if (dmabufAttrs.format != PFRAME->dmabufFormat) {
|
2023-10-10 00:51:55 +02:00
|
|
|
Debug::log(ERR, "[sc] invalid buffer dma format in {:x}", (uintptr_t)PFRAME);
|
2023-04-03 18:01:05 +02:00
|
|
|
wl_resource_post_error(PFRAME->resource, ZWLR_SCREENCOPY_FRAME_V1_ERROR_INVALID_BUFFER, "invalid buffer format");
|
|
|
|
removeFrame(PFRAME);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else if (wlr_buffer_begin_data_ptr_access(PBUFFER, WLR_BUFFER_DATA_PTR_ACCESS_WRITE, &wlrBufferAccessData, &wlrBufferAccessFormat, &wlrBufferAccessStride)) {
|
|
|
|
wlr_buffer_end_data_ptr_access(PBUFFER);
|
|
|
|
|
|
|
|
if (wlrBufferAccessFormat != PFRAME->shmFormat) {
|
2023-10-10 00:51:55 +02:00
|
|
|
Debug::log(ERR, "[sc] invalid buffer shm format in {:x}", (uintptr_t)PFRAME);
|
2023-04-03 18:01:05 +02:00
|
|
|
wl_resource_post_error(PFRAME->resource, ZWLR_SCREENCOPY_FRAME_V1_ERROR_INVALID_BUFFER, "invalid buffer format");
|
|
|
|
removeFrame(PFRAME);
|
|
|
|
return;
|
|
|
|
} else if ((int)wlrBufferAccessStride != PFRAME->shmStride) {
|
2023-10-10 00:51:55 +02:00
|
|
|
Debug::log(ERR, "[sc] invalid buffer shm stride in {:x}", (uintptr_t)PFRAME);
|
2023-04-03 18:01:05 +02:00
|
|
|
wl_resource_post_error(PFRAME->resource, ZWLR_SCREENCOPY_FRAME_V1_ERROR_INVALID_BUFFER, "invalid buffer stride");
|
|
|
|
removeFrame(PFRAME);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
2023-10-10 00:51:55 +02:00
|
|
|
Debug::log(ERR, "[sc] invalid buffer type in {:x}", (uintptr_t)PFRAME);
|
2023-04-03 18:01:05 +02:00
|
|
|
wl_resource_post_error(PFRAME->resource, ZWLR_SCREENCOPY_FRAME_V1_ERROR_INVALID_BUFFER, "invalid buffer type");
|
|
|
|
removeFrame(PFRAME);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
PFRAME->buffer = PBUFFER;
|
|
|
|
|
|
|
|
m_vFramesAwaitingWrite.emplace_back(PFRAME);
|
|
|
|
|
|
|
|
g_pHyprRenderer->m_bDirectScanoutBlocked = true;
|
2024-05-05 23:18:10 +02:00
|
|
|
if (PFRAME->overlayCursor && !PFRAME->lockedSWCursors) {
|
|
|
|
PFRAME->lockedSWCursors = true;
|
2024-05-11 23:10:42 +02:00
|
|
|
// TODO: make it per-monitor
|
|
|
|
if (!m_bTimerArmed) {
|
|
|
|
for (auto& m : g_pCompositor->m_vMonitors) {
|
|
|
|
g_pPointerManager->lockSoftwareForMonitor(m);
|
|
|
|
}
|
|
|
|
m_bTimerArmed = true;
|
|
|
|
Debug::log(LOG, "[screencopy] Locking sw cursors due to screensharing");
|
|
|
|
}
|
|
|
|
m_pSoftwareCursorTimer->updateTimeout(std::chrono::seconds(1));
|
2024-05-05 23:18:10 +02:00
|
|
|
}
|
2023-04-07 19:04:02 +02:00
|
|
|
|
|
|
|
if (!PFRAME->withDamage)
|
2024-02-20 16:22:54 +01:00
|
|
|
g_pHyprRenderer->damageMonitor(PFRAME->pMonitor);
|
2023-04-03 18:01:05 +02:00
|
|
|
}
|
|
|
|
|
2023-04-12 22:40:51 +02:00
|
|
|
void CScreencopyProtocolManager::onOutputCommit(CMonitor* pMonitor, wlr_output_event_commit* e) {
|
2023-10-25 01:27:16 +02:00
|
|
|
m_pLastMonitorBackBuffer = e->state->buffer;
|
2023-07-19 00:51:38 +02:00
|
|
|
shareAllFrames(pMonitor);
|
2023-04-12 22:40:51 +02:00
|
|
|
m_pLastMonitorBackBuffer = nullptr;
|
|
|
|
}
|
|
|
|
|
2023-07-19 00:51:38 +02:00
|
|
|
void CScreencopyProtocolManager::shareAllFrames(CMonitor* pMonitor) {
|
2023-04-03 18:01:05 +02:00
|
|
|
if (m_vFramesAwaitingWrite.empty())
|
|
|
|
return; // nothing to share
|
|
|
|
|
|
|
|
std::vector<SScreencopyFrame*> framesToRemove;
|
|
|
|
|
|
|
|
// share frame if correct output
|
|
|
|
for (auto& f : m_vFramesAwaitingWrite) {
|
2023-07-18 15:52:53 +02:00
|
|
|
if (!f->pMonitor || !f->buffer) {
|
2023-04-03 18:01:05 +02:00
|
|
|
framesToRemove.push_back(f);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-07-19 00:51:38 +02:00
|
|
|
if (f->pMonitor != pMonitor)
|
2023-04-07 20:11:30 +02:00
|
|
|
continue;
|
|
|
|
|
2023-04-03 18:01:05 +02:00
|
|
|
shareFrame(f);
|
|
|
|
|
2023-04-17 23:57:24 +02:00
|
|
|
f->client->lastFrame.reset();
|
2023-04-16 00:43:41 +02:00
|
|
|
++f->client->frameCounter;
|
|
|
|
|
2023-04-03 18:01:05 +02:00
|
|
|
framesToRemove.push_back(f);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto& f : framesToRemove) {
|
|
|
|
removeFrame(f);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_vFramesAwaitingWrite.empty()) {
|
|
|
|
g_pHyprRenderer->m_bDirectScanoutBlocked = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CScreencopyProtocolManager::shareFrame(SScreencopyFrame* frame) {
|
2023-04-07 19:04:02 +02:00
|
|
|
if (!frame->buffer)
|
2023-04-03 18:01:05 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
timespec now;
|
|
|
|
clock_gettime(CLOCK_MONOTONIC, &now);
|
|
|
|
|
|
|
|
uint32_t flags = 0;
|
|
|
|
if (frame->bufferCap == WLR_BUFFER_CAP_DMABUF) {
|
|
|
|
if (!copyFrameDmabuf(frame)) {
|
2023-10-10 00:51:55 +02:00
|
|
|
Debug::log(ERR, "[sc] dmabuf copy failed in {:x}", (uintptr_t)frame);
|
2023-04-03 18:01:05 +02:00
|
|
|
zwlr_screencopy_frame_v1_send_failed(frame->resource);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!copyFrameShm(frame, &now)) {
|
2023-10-10 00:51:55 +02:00
|
|
|
Debug::log(ERR, "[sc] shm copy failed in {:x}", (uintptr_t)frame);
|
2023-04-03 18:01:05 +02:00
|
|
|
zwlr_screencopy_frame_v1_send_failed(frame->resource);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
zwlr_screencopy_frame_v1_send_flags(frame->resource, flags);
|
|
|
|
sendFrameDamage(frame);
|
|
|
|
uint32_t tvSecHi = (sizeof(now.tv_sec) > 4) ? now.tv_sec >> 32 : 0;
|
|
|
|
uint32_t tvSecLo = now.tv_sec & 0xFFFFFFFF;
|
|
|
|
zwlr_screencopy_frame_v1_send_ready(frame->resource, tvSecHi, tvSecLo, now.tv_nsec);
|
|
|
|
}
|
2023-04-12 22:40:51 +02:00
|
|
|
|
2023-04-03 18:01:05 +02:00
|
|
|
void CScreencopyProtocolManager::sendFrameDamage(SScreencopyFrame* frame) {
|
|
|
|
if (!frame->withDamage)
|
|
|
|
return;
|
|
|
|
|
2024-02-23 02:02:32 +01:00
|
|
|
// TODO:
|
|
|
|
// add a damage ring for this.
|
2023-07-19 13:10:31 +02:00
|
|
|
|
2024-02-23 02:02:32 +01:00
|
|
|
// for (auto& RECT : frame->pMonitor->lastFrameDamage.getRects()) {
|
2023-07-19 13:10:31 +02:00
|
|
|
|
2024-02-23 02:02:32 +01:00
|
|
|
// if (frame->buffer->width < 1 || frame->buffer->height < 1 || frame->buffer->width - RECT.x1 < 1 || frame->buffer->height - RECT.y1 < 1) {
|
|
|
|
// Debug::log(ERR, "[sc] Failed to send damage");
|
|
|
|
// break;
|
|
|
|
// }
|
|
|
|
|
|
|
|
// zwlr_screencopy_frame_v1_send_damage(frame->resource, std::clamp(RECT.x1, 0, frame->buffer->width), std::clamp(RECT.y1, 0, frame->buffer->height),
|
|
|
|
// std::clamp(RECT.x2 - RECT.x1, 0, frame->buffer->width - RECT.x1), std::clamp(RECT.y2 - RECT.y1, 0, frame->buffer->height - RECT.y1));
|
|
|
|
// }
|
2024-02-27 15:51:27 +01:00
|
|
|
|
|
|
|
zwlr_screencopy_frame_v1_send_damage(frame->resource, 0, 0, frame->buffer->width, frame->buffer->height);
|
2023-04-03 18:01:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CScreencopyProtocolManager::copyFrameShm(SScreencopyFrame* frame, timespec* now) {
|
2023-11-30 11:14:35 +01:00
|
|
|
wlr_texture* sourceTex = wlr_texture_from_buffer(g_pCompositor->m_sWLRRenderer, m_pLastMonitorBackBuffer);
|
|
|
|
if (!sourceTex)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
void* data;
|
|
|
|
uint32_t format;
|
|
|
|
size_t stride;
|
|
|
|
if (!wlr_buffer_begin_data_ptr_access(frame->buffer, WLR_BUFFER_DATA_PTR_ACCESS_WRITE, &data, &format, &stride)) {
|
|
|
|
wlr_texture_destroy(sourceTex);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
CRegion fakeDamage = {0, 0, INT16_MAX, INT16_MAX};
|
|
|
|
|
|
|
|
g_pHyprRenderer->makeEGLCurrent();
|
|
|
|
|
|
|
|
CFramebuffer fb;
|
2023-12-04 02:44:06 +01:00
|
|
|
fb.alloc(frame->box.w, frame->box.h, g_pHyprRenderer->isNvidia() ? DRM_FORMAT_XBGR8888 : frame->pMonitor->drmFormat);
|
2023-11-30 11:14:35 +01:00
|
|
|
|
|
|
|
if (!g_pHyprRenderer->beginRender(frame->pMonitor, fakeDamage, RENDER_MODE_FULL_FAKE, nullptr, &fb)) {
|
|
|
|
wlr_texture_destroy(sourceTex);
|
|
|
|
wlr_buffer_end_data_ptr_access(frame->buffer);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-12-03 20:06:51 +01:00
|
|
|
CBox monbox = CBox{0, 0, frame->pMonitor->vecTransformedSize.x, frame->pMonitor->vecTransformedSize.y}.translate({-frame->box.x, -frame->box.y});
|
2023-11-30 11:14:35 +01:00
|
|
|
g_pHyprOpenGL->setMonitorTransformEnabled(true);
|
2024-04-03 15:09:58 +02:00
|
|
|
g_pHyprOpenGL->setRenderModifEnabled(false);
|
2024-03-03 03:18:00 +01:00
|
|
|
g_pHyprOpenGL->renderTexture(sourceTex, &monbox, 1);
|
2024-04-03 15:09:58 +02:00
|
|
|
g_pHyprOpenGL->setRenderModifEnabled(true);
|
2024-03-03 03:18:00 +01:00
|
|
|
g_pHyprOpenGL->setMonitorTransformEnabled(false);
|
2023-11-30 11:14:35 +01:00
|
|
|
|
2023-12-05 15:59:12 +01:00
|
|
|
#ifndef GLES2
|
2023-11-30 11:14:35 +01:00
|
|
|
glBindFramebuffer(GL_READ_FRAMEBUFFER, fb.m_iFb);
|
2023-12-05 15:59:12 +01:00
|
|
|
#else
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, fb.m_iFb);
|
|
|
|
#endif
|
2023-11-30 11:14:35 +01:00
|
|
|
|
2023-12-04 04:52:54 +01:00
|
|
|
const auto PFORMAT = g_pHyprOpenGL->getPixelFormatFromDRM(format);
|
|
|
|
if (!PFORMAT) {
|
|
|
|
g_pHyprRenderer->endRender();
|
|
|
|
wlr_texture_destroy(sourceTex);
|
|
|
|
wlr_buffer_end_data_ptr_access(frame->buffer);
|
|
|
|
return false;
|
2023-12-03 23:04:07 +01:00
|
|
|
}
|
2023-11-30 11:14:35 +01:00
|
|
|
|
2024-04-03 22:35:16 +02:00
|
|
|
g_pHyprOpenGL->m_RenderData.blockScreenShader = true;
|
2023-11-30 12:07:17 +01:00
|
|
|
g_pHyprRenderer->endRender();
|
|
|
|
|
|
|
|
g_pHyprRenderer->makeEGLCurrent();
|
|
|
|
g_pHyprOpenGL->m_RenderData.pMonitor = frame->pMonitor;
|
|
|
|
fb.bind();
|
|
|
|
|
2023-12-03 23:04:07 +01:00
|
|
|
glPixelStorei(GL_PACK_ALIGNMENT, 1);
|
|
|
|
|
2023-12-04 04:52:54 +01:00
|
|
|
const wlr_pixel_format_info* drmFmtWlr = drm_get_pixel_format_info(format);
|
2023-12-03 23:04:07 +01:00
|
|
|
uint32_t packStride = pixel_format_info_min_stride(drmFmtWlr, frame->box.w);
|
|
|
|
|
|
|
|
if (packStride == stride) {
|
2023-12-05 21:04:53 +01:00
|
|
|
glReadPixels(0, 0, frame->box.w, frame->box.h, PFORMAT->glFormat, PFORMAT->glType, data);
|
2023-12-03 23:04:07 +01:00
|
|
|
} else {
|
|
|
|
for (size_t i = 0; i < frame->box.h; ++i) {
|
2023-12-05 21:04:53 +01:00
|
|
|
uint32_t y = i;
|
|
|
|
glReadPixels(0, y, frame->box.w, 1, PFORMAT->glFormat, PFORMAT->glType, ((unsigned char*)data) + i * stride);
|
2023-12-03 23:04:07 +01:00
|
|
|
}
|
|
|
|
}
|
2023-11-30 11:14:35 +01:00
|
|
|
|
2023-11-30 12:07:17 +01:00
|
|
|
g_pHyprOpenGL->m_RenderData.pMonitor = nullptr;
|
2023-11-30 11:14:35 +01:00
|
|
|
|
|
|
|
wlr_buffer_end_data_ptr_access(frame->buffer);
|
|
|
|
wlr_texture_destroy(sourceTex);
|
|
|
|
|
|
|
|
return true;
|
2023-04-03 18:01:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CScreencopyProtocolManager::copyFrameDmabuf(SScreencopyFrame* frame) {
|
2023-04-12 22:40:51 +02:00
|
|
|
wlr_texture* sourceTex = wlr_texture_from_buffer(g_pCompositor->m_sWLRRenderer, m_pLastMonitorBackBuffer);
|
|
|
|
if (!sourceTex)
|
|
|
|
return false;
|
|
|
|
|
2024-03-03 03:18:00 +01:00
|
|
|
CRegion fakeDamage = {0, 0, INT16_MAX, INT16_MAX};
|
2023-11-24 11:54:21 +01:00
|
|
|
|
|
|
|
if (!g_pHyprRenderer->beginRender(frame->pMonitor, fakeDamage, RENDER_MODE_TO_BUFFER, frame->buffer))
|
2023-04-12 22:40:51 +02:00
|
|
|
return false;
|
|
|
|
|
2024-03-03 03:18:00 +01:00
|
|
|
CBox monbox = CBox{0, 0, frame->pMonitor->vecPixelSize.x, frame->pMonitor->vecPixelSize.y}
|
|
|
|
.translate({-frame->box.x, -frame->box.y}) // vvvv kinda ass-backwards but that's how I designed the renderer... sigh.
|
|
|
|
.transform(wlr_output_transform_invert(frame->pMonitor->output->transform), frame->pMonitor->vecPixelSize.x, frame->pMonitor->vecPixelSize.y);
|
2023-11-24 11:54:21 +01:00
|
|
|
g_pHyprOpenGL->setMonitorTransformEnabled(true);
|
2024-04-03 15:09:58 +02:00
|
|
|
g_pHyprOpenGL->setRenderModifEnabled(false);
|
2024-03-03 03:18:00 +01:00
|
|
|
g_pHyprOpenGL->renderTexture(sourceTex, &monbox, 1);
|
2024-04-03 15:09:58 +02:00
|
|
|
g_pHyprOpenGL->setRenderModifEnabled(true);
|
2024-03-03 03:18:00 +01:00
|
|
|
g_pHyprOpenGL->setMonitorTransformEnabled(false);
|
2023-04-12 22:40:51 +02:00
|
|
|
|
2024-04-03 22:35:16 +02:00
|
|
|
g_pHyprOpenGL->m_RenderData.blockScreenShader = true;
|
2023-11-24 11:54:21 +01:00
|
|
|
g_pHyprRenderer->endRender();
|
2023-04-12 22:40:51 +02:00
|
|
|
|
2023-11-24 11:54:21 +01:00
|
|
|
wlr_texture_destroy(sourceTex);
|
2023-04-12 22:40:51 +02:00
|
|
|
|
|
|
|
return true;
|
2023-04-07 20:11:30 +02:00
|
|
|
}
|