Repository: hyprwm/Hyprland
File: src/protocols/Screencopy.cpp (C++)

Code before:
        uint32_t tvSecLo = sec & 0xFFFFFFFF;
        m_resource->sendReady(tvSecHi, tvSecLo, nsec);
    };

    if (m_bufferDMA)
        copyDmabuf(callback);
    else
        callback(copyShm());
}

void CScreencopyFrame::renderMon() {
    auto    TEXTURE = makeShared<CTexture>(m_monitor->m_output->state->state().buffer);

    CRegion fakeDamage = {0, 0, INT16_MAX, INT16_MAX};

    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);
    g_pHyprOpenGL->setMonitorTransformEnabled(true);
    g_pHyprOpenGL->setRenderModifEnabled(false);
    g_pHyprOpenGL->renderTexture(TEXTURE, monbox, 1);
    g_pHyprOpenGL->setRenderModifEnabled(true);
    g_pHyprOpenGL->setMonitorTransformEnabled(false);
    for (auto const& w : g_pCompositor->m_windows) {
        if (w->m_windowData.noScreenShare.valueOrDefault()) {
            const auto PWORKSPACE = w->m_workspace;
            if (!PWORKSPACE) {
                continue;
            }
            const auto REALPOS          = w->m_realPosition->value() + (w->m_pinned ? Vector2D{} : PWORKSPACE->m_renderOffset->value());
            const auto noScreenShareBox = CBox{REALPOS.x, REALPOS.y, std::max(w->m_realSize->value().x, 5.0), std::max(w->m_realSize->value().y, 5.0)}
                                              .scale(m_monitor->m_scale)
                                              .translate({-m_monitor->m_position.x, -m_monitor->m_position.y})
                                              .translate({-m_box.x, -m_box.y});
            g_pHyprOpenGL->renderRect(noScreenShareBox, {0, 0, 0, 255});
        }
    }
    if (m_overlayCursor)
        g_pPointerManager->renderSoftwareCursorsFor(m_monitor.lock(), Time::steadyNow(), fakeDamage,
                                                    g_pInputManager->getMouseCoordsInternal() - m_monitor->m_position - m_box.pos(), true);
}

void CScreencopyFrame::storeTempFB() {
    g_pHyprRenderer->makeEGLCurrent();

    m_tempFb.alloc(m_box.w, m_box.h);

    CRegion fakeDamage = {0, 0, INT16_MAX, INT16_MAX};

    if (!g_pHyprRenderer->beginRender(m_monitor.lock(), fakeDamage, RENDER_MODE_FULL_FAKE, nullptr, &m_tempFb, true)) {
        LOGM(ERR, "Can't copy: failed to begin rendering to temp fb");

Review comment (suggestion):
needs some work:
Firstly, this will draw over other windows if there are any above the "private" window.
Secondly, it doesnt check the current workspace... will render even if the window is on a different workspace.
Lastly, doesn't take rounding into account.

Find windows above the window being "private'd" (rudimentary is okay)

An idea like this isn't even that bad:
- Every time a frame is rendered, send weak window pointers to the screencopy protocol with: Window, geometry.
- When you find one that is private when sharing, take the geometries that come _later_ and use them as a mask if they occlude our private window.

Code after:
        uint32_t tvSecHi = (sizeof(sec) > 4) ? sec >> 32 : 0;
        uint32_t tvSecLo = sec & 0xFFFFFFFF;
        m_resource->sendReady(tvSecHi, tvSecLo, nsec);
    };

    if (m_bufferDMA)
        copyDmabuf(callback);
    else
        callback(copyShm());
}

void CScreencopyFrame::renderMon() {
    auto    TEXTURE = makeShared<CTexture>(m_monitor->m_output->state->state().buffer);

    CRegion fakeDamage = {0, 0, INT16_MAX, INT16_MAX};

    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);
    g_pHyprOpenGL->setMonitorTransformEnabled(true);
    g_pHyprOpenGL->setRenderModifEnabled(false);
    g_pHyprOpenGL->renderTexture(TEXTURE, monbox, 1);
    g_pHyprOpenGL->setRenderModifEnabled(true);
    g_pHyprOpenGL->setMonitorTransformEnabled(false);

    for (auto const& w : g_pCompositor->m_windows) {
        if (!w->m_windowData.noScreenShare.valueOrDefault())
            continue;

        if (!g_pHyprRenderer->shouldRenderWindow(w))
            continue;

        const auto PWORKSPACE = w->m_workspace;

        if UNLIKELY (!PWORKSPACE)
            continue;

        const auto REALPOS          = w->m_realPosition->value() + (w->m_pinned ? Vector2D{} : PWORKSPACE->m_renderOffset->value());
        const auto noScreenShareBox = CBox{REALPOS.x, REALPOS.y, std::max(w->m_realSize->value().x, 5.0), std::max(w->m_realSize->value().y, 5.0)}
                                          .scale(m_monitor->m_scale)
                                          .translate({-m_monitor->m_position.x, -m_monitor->m_position.y})
                                          .translate({-m_box.x, -m_box.y});

        const auto dontRound     = w->isEffectiveInternalFSMode(FSMODE_FULLSCREEN) || w->m_windowData.noRounding.valueOrDefault();
        const auto rounding      = dontRound ? 0 : w->rounding() * m_monitor->m_scale;
        const auto roundingPower = dontRound ? 2.0f : w->roundingPower();

        g_pHyprOpenGL->renderRect(noScreenShareBox, {0, 0, 0, 255}, rounding, roundingPower);

        if (w->m_isX11 || !w->m_popupHead)
            continue;