|
| 1 | +#include "ImageCopyCapture.hpp" |
| 2 | +#include "../managers/screenshare/ScreenshareManager.hpp" |
| 3 | +#include "LinuxDMABUF.hpp" |
| 4 | +#include "../render/OpenGL.hpp" |
| 5 | +#include <cstring> |
| 6 | + |
| 7 | +CImageCopyCaptureSession::CImageCopyCaptureSession(SP<CExtImageCopyCaptureSessionV1> resource, SP<CImageCaptureSource> source, extImageCopyCaptureManagerV1Options options) : |
| 8 | + m_resource(resource), m_source(source), m_paintCursor(options & EXT_IMAGE_COPY_CAPTURE_MANAGER_V1_OPTIONS_PAINT_CURSORS) { |
| 9 | + |
| 10 | + m_resource->setDestroy([this](CExtImageCopyCaptureSessionV1* pMgr) { PROTO::imageCopyCapture->destroyResource(this); }); |
| 11 | + m_resource->setOnDestroy([this](CExtImageCopyCaptureSessionV1* pMgr) { PROTO::imageCopyCapture->destroyResource(this); }); |
| 12 | + |
| 13 | + m_resource->setCreateFrame([this](CExtImageCopyCaptureSessionV1* pMgr, uint32_t id) { |
| 14 | + if (!m_frame.expired()) { |
| 15 | + LOGM(LOG, "Duplicate frame in session for source: \"{}\"", m_source->getName()); |
| 16 | + m_resource->error(EXT_IMAGE_COPY_CAPTURE_SESSION_V1_ERROR_DUPLICATE_FRAME, "duplicate frame"); |
| 17 | + return; |
| 18 | + } |
| 19 | + |
| 20 | + auto PFRAME = PROTO::imageCopyCapture->m_frames.emplace_back( |
| 21 | + makeShared<CImageCopyCaptureFrame>(makeShared<CExtImageCopyCaptureFrameV1>(pMgr->client(), pMgr->version(), id), m_self)); |
| 22 | + |
| 23 | + m_frame = PFRAME; |
| 24 | + }); |
| 25 | + |
| 26 | + if (m_source->m_monitor) |
| 27 | + m_session = g_pScreenshareManager->newSession(m_resource->client(), m_source->m_monitor.lock()); |
| 28 | + else |
| 29 | + m_session = g_pScreenshareManager->newSession(m_resource->client(), m_source->m_window.lock()); |
| 30 | + |
| 31 | + if UNLIKELY (!m_session) { |
| 32 | + m_resource->sendStopped(); |
| 33 | + m_resource->error(-1, "unable to share screen"); |
| 34 | + return; |
| 35 | + } |
| 36 | + |
| 37 | + sendConstraints(); |
| 38 | + |
| 39 | + m_listeners.constraintsChanged = m_session->m_events.constraintsChanged.listen([this]() { sendConstraints(); }); |
| 40 | + m_listeners.stopped = m_session->m_events.stopped.listen([this]() { PROTO::imageCopyCapture->destroyResource(this); }); |
| 41 | +} |
| 42 | + |
| 43 | +CImageCopyCaptureSession::~CImageCopyCaptureSession() { |
| 44 | + if (m_session) |
| 45 | + m_session->stop(); |
| 46 | + if (m_resource->resource()) |
| 47 | + m_resource->sendStopped(); |
| 48 | +} |
| 49 | + |
| 50 | +void CImageCopyCaptureSession::sendConstraints() { |
| 51 | + auto formats = m_session->allowedFormats(); |
| 52 | + |
| 53 | + if UNLIKELY (formats.empty()) { |
| 54 | + m_session->stop(); |
| 55 | + m_resource->error(-1, "no formats available"); |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + for (DRMFormat format : formats) { |
| 60 | + m_resource->sendShmFormat(NFormatUtils::drmToShm(format)); |
| 61 | + |
| 62 | + auto modifiers = g_pHyprOpenGL->getDRMFormatModifiers(format); |
| 63 | + |
| 64 | + wl_array modsArr; |
| 65 | + wl_array_init(&modsArr); |
| 66 | + if (!modifiers.empty()) { |
| 67 | + wl_array_add(&modsArr, modifiers.size() * sizeof(uint64_t)); |
| 68 | + memcpy(modsArr.data, modifiers.data(), modifiers.size() * sizeof(uint64_t)); |
| 69 | + } |
| 70 | + m_resource->sendDmabufFormat(format, &modsArr); |
| 71 | + wl_array_release(&modsArr); |
| 72 | + } |
| 73 | + |
| 74 | + dev_t device = PROTO::linuxDma->getMainDevice(); |
| 75 | + struct wl_array deviceArr = { |
| 76 | + .size = sizeof(device), |
| 77 | + .data = sc<void*>(&device), |
| 78 | + }; |
| 79 | + m_resource->sendDmabufDevice(&deviceArr); |
| 80 | + |
| 81 | + m_bufferSize = m_session->bufferSize(); |
| 82 | + m_resource->sendBufferSize(m_bufferSize.x, m_bufferSize.y); |
| 83 | + |
| 84 | + m_resource->sendDone(); |
| 85 | +} |
| 86 | + |
| 87 | +CImageCopyCaptureFrame::CImageCopyCaptureFrame(SP<CExtImageCopyCaptureFrameV1> resource, WP<CImageCopyCaptureSession> session) : m_resource(resource), m_session(session) { |
| 88 | + if (m_session->m_bufferSize != m_session->m_session->bufferSize()) { |
| 89 | + m_session->sendConstraints(); |
| 90 | + m_resource->sendFailed(EXT_IMAGE_COPY_CAPTURE_FRAME_V1_FAILURE_REASON_UNKNOWN); |
| 91 | + return; |
| 92 | + } |
| 93 | + |
| 94 | + m_frame = m_session->m_session->nextFrame(m_session->m_paintCursor); |
| 95 | + |
| 96 | + m_resource->setDestroy([this](CExtImageCopyCaptureFrameV1* pMgr) { PROTO::imageCopyCapture->destroyResource(this); }); |
| 97 | + m_resource->setOnDestroy([this](CExtImageCopyCaptureFrameV1* pMgr) { PROTO::imageCopyCapture->destroyResource(this); }); |
| 98 | + |
| 99 | + m_resource->setAttachBuffer([this](CExtImageCopyCaptureFrameV1* pMgr, wl_resource* buf) { |
| 100 | + if (m_captured) { |
| 101 | + LOGM(ERR, "Frame already captured in attach_buffer, {:x}", (uintptr_t)this); |
| 102 | + m_resource->error(EXT_IMAGE_COPY_CAPTURE_FRAME_V1_ERROR_ALREADY_CAPTURED, "already captured"); |
| 103 | + return; |
| 104 | + } |
| 105 | + |
| 106 | + auto PBUFFERRES = CWLBufferResource::fromResource(buf); |
| 107 | + if (!PBUFFERRES || !PBUFFERRES->m_buffer) { |
| 108 | + LOGM(ERR, "Invalid buffer in attach_buffer {:x}", (uintptr_t)this); |
| 109 | + m_resource->error(-1, "invalid buffer"); |
| 110 | + return; |
| 111 | + } |
| 112 | + |
| 113 | + m_buffer = PBUFFERRES->m_buffer.lock(); |
| 114 | + }); |
| 115 | + |
| 116 | + m_resource->setDamageBuffer([this](CExtImageCopyCaptureFrameV1* pMgr, int32_t x, int32_t y, int32_t w, int32_t h) { |
| 117 | + if (m_captured) { |
| 118 | + LOGM(ERR, "Frame already captured in damage_buffer, {:x}", (uintptr_t)this); |
| 119 | + m_resource->error(EXT_IMAGE_COPY_CAPTURE_FRAME_V1_ERROR_ALREADY_CAPTURED, "already captured"); |
| 120 | + return; |
| 121 | + } |
| 122 | + |
| 123 | + if (x < 0 || y < 0 || w <= 0 || h <= 0) { |
| 124 | + m_resource->error(EXT_IMAGE_COPY_CAPTURE_FRAME_V1_ERROR_INVALID_BUFFER_DAMAGE, "invalid buffer damage"); |
| 125 | + return; |
| 126 | + } |
| 127 | + |
| 128 | + m_clientDamage.add(x, y, w, h); |
| 129 | + }); |
| 130 | + |
| 131 | + m_resource->setCapture([this](CExtImageCopyCaptureFrameV1* pMgr) { |
| 132 | + if (m_captured) { |
| 133 | + LOGM(ERR, "Frame already captured in capture, {:x}", (uintptr_t)this); |
| 134 | + m_resource->error(EXT_IMAGE_COPY_CAPTURE_FRAME_V1_ERROR_ALREADY_CAPTURED, "already captured"); |
| 135 | + return; |
| 136 | + } |
| 137 | + |
| 138 | + auto error = m_frame->share(m_buffer, m_clientDamage, [this](eScreenshareResult result) { |
| 139 | + switch (result) { |
| 140 | + case RESULT_COPIED: m_resource->sendReady(); break; |
| 141 | + case RESULT_NOT_COPIED: m_resource->sendFailed(EXT_IMAGE_COPY_CAPTURE_FRAME_V1_FAILURE_REASON_UNKNOWN); break; |
| 142 | + case RESULT_TIMESTAMP: |
| 143 | + auto [sec, nsec] = Time::secNsec(Time::steadyNow()); |
| 144 | + uint32_t tvSecHi = (sizeof(sec) > 4) ? sec >> 32 : 0; |
| 145 | + uint32_t tvSecLo = sec & 0xFFFFFFFF; |
| 146 | + m_resource->sendPresentationTime(tvSecHi, tvSecLo, nsec); |
| 147 | + break; |
| 148 | + } |
| 149 | + }); |
| 150 | + |
| 151 | + switch (error) { |
| 152 | + case ERROR_NONE: m_captured = true; break; |
| 153 | + case ERROR_NO_BUFFER: m_resource->error(EXT_IMAGE_COPY_CAPTURE_FRAME_V1_ERROR_NO_BUFFER, "no buffer attached"); break; |
| 154 | + case ERROR_BUFFER_SIZE: |
| 155 | + case ERROR_BUFFER_FORMAT: m_resource->sendFailed(EXT_IMAGE_COPY_CAPTURE_FRAME_V1_FAILURE_REASON_BUFFER_CONSTRAINTS); break; |
| 156 | + case ERROR_STOPPED: m_resource->sendFailed(EXT_IMAGE_COPY_CAPTURE_FRAME_V1_FAILURE_REASON_STOPPED); break; |
| 157 | + case ERROR_UNKNOWN: m_resource->sendFailed(EXT_IMAGE_COPY_CAPTURE_FRAME_V1_FAILURE_REASON_UNKNOWN); break; |
| 158 | + } |
| 159 | + }); |
| 160 | + |
| 161 | + m_clientDamage.clear(); |
| 162 | + |
| 163 | + // TODO: see ScreenshareFrame::share() for "add a damage ring for output damage since last shared frame" |
| 164 | + m_resource->sendDamage(0, 0, m_session->m_bufferSize.x, m_session->m_bufferSize.y); |
| 165 | + |
| 166 | + m_resource->sendTransform(m_frame->transform()); |
| 167 | +} |
| 168 | + |
| 169 | +CImageCopyCaptureFrame::~CImageCopyCaptureFrame() { |
| 170 | + if (m_session) |
| 171 | + m_session->m_frame.reset(); |
| 172 | +} |
| 173 | + |
| 174 | +CImageCopyCaptureProtocol::CImageCopyCaptureProtocol(const wl_interface* iface, const int& ver, const std::string& name) : IWaylandProtocol(iface, ver, name) { |
| 175 | + ; |
| 176 | +} |
| 177 | + |
| 178 | +void CImageCopyCaptureProtocol::bindManager(wl_client* client, void* data, uint32_t ver, uint32_t id) { |
| 179 | + const auto RESOURCE = m_managers.emplace_back(makeShared<CExtImageCopyCaptureManagerV1>(client, ver, id)); |
| 180 | + |
| 181 | + if UNLIKELY (!RESOURCE->resource()) { |
| 182 | + wl_client_post_no_memory(client); |
| 183 | + m_managers.pop_back(); |
| 184 | + return; |
| 185 | + } |
| 186 | + |
| 187 | + RESOURCE->setDestroy([this](CExtImageCopyCaptureManagerV1* pMgr) { destroyResource(pMgr); }); |
| 188 | + RESOURCE->setOnDestroy([this](CExtImageCopyCaptureManagerV1* pMgr) { destroyResource(pMgr); }); |
| 189 | + |
| 190 | + RESOURCE->setCreateSession([this](CExtImageCopyCaptureManagerV1* pMgr, uint32_t id, wl_resource* source_, extImageCopyCaptureManagerV1Options options) { |
| 191 | + auto source = PROTO::imageCaptureSource->sourceFromResource(source_); |
| 192 | + if (!source) { |
| 193 | + LOGM(LOG, "Client tried to create image copy capture session from invalid source"); |
| 194 | + pMgr->error(-1, "invalid image capture source"); |
| 195 | + return; |
| 196 | + } |
| 197 | + |
| 198 | + if (options > 1) { |
| 199 | + LOGM(LOG, "Client tried to create image copy capture session with invalid options"); |
| 200 | + pMgr->error(EXT_IMAGE_COPY_CAPTURE_MANAGER_V1_ERROR_INVALID_OPTION, "Options can't be above 1"); |
| 201 | + return; |
| 202 | + } |
| 203 | + |
| 204 | + auto& PSESSION = |
| 205 | + m_sessions.emplace_back(makeShared<CImageCopyCaptureSession>(makeShared<CExtImageCopyCaptureSessionV1>(pMgr->client(), pMgr->version(), id), source, options)); |
| 206 | + PSESSION->m_self = PSESSION; |
| 207 | + LOGM(LOG, "New image copy capture session for source ({}): \"{}\"", source->getTypeName(), source->getName()); |
| 208 | + }); |
| 209 | + |
| 210 | + RESOURCE->setCreatePointerCursorSession([this](CExtImageCopyCaptureManagerV1* pMgr, uint32_t id, wl_resource* source_, wl_resource* pointer_) { |
| 211 | + SP<CImageCaptureSource> source = PROTO::imageCaptureSource->sourceFromResource(source_); |
| 212 | + if (!source) { |
| 213 | + LOGM(LOG, "Client tried to create image copy capture session from invalid source"); |
| 214 | + destroyResource(pMgr); |
| 215 | + return; |
| 216 | + } |
| 217 | + |
| 218 | + // TODO: find out what this would be, then implement constructor aswell |
| 219 | + // auto pointer = CWLPointerResource::fromResource(pointer_); |
| 220 | + |
| 221 | + // m_sessions.emplace_back(makeShared<CImageCopyCaptureSession>(makeShared<CExtImageCopyCaptureSessionV1>(pMgr->client(), pMgr->version(), id), source, pointer)); |
| 222 | + // LOGM(LOG, "New image copy capture session for source ({}): {}", source->getTypeName(), source->getName()); |
| 223 | + }); |
| 224 | +} |
| 225 | + |
| 226 | +void CImageCopyCaptureProtocol::destroyResource(CExtImageCopyCaptureManagerV1* resource) { |
| 227 | + std::erase_if(m_managers, [&](const auto& other) { return other.get() == resource; }); |
| 228 | +} |
| 229 | + |
| 230 | +void CImageCopyCaptureProtocol::destroyResource(CImageCopyCaptureSession* resource) { |
| 231 | + std::erase_if(m_sessions, [&](const auto& other) { return other.get() == resource; }); |
| 232 | +} |
| 233 | + |
| 234 | +void CImageCopyCaptureProtocol::destroyResource(CImageCopyCaptureFrame* resource) { |
| 235 | + std::erase_if(m_frames, [&](const auto& other) { return other.get() == resource; }); |
| 236 | +} |
0 commit comments