Every quality change restarts the capturer on both sides, and two defects showed up along that path. Monitor jump: on a multi-monitor client the view moved to another monitor. switchScreen=false means "reuse the index", but index is the *next* monitor to switch to, not the one being shown. Track `current` and read it on the non-switch path; the switch path still advances. H264 stall: switching from RGB565/DIFF back to H264 froze the view for up to ten seconds. The client encoder kept its reference frames across the gap, so it emitted P frames the server could not decode until the next natural IDR. It is now rebuilt on the first H264 frame after an algorithm or tuning change. The rebuild is requested with an atomic dirty flag that the capture thread consumes in ensureEncoder(), because resetting the encoder from the network thread raced with encode(). On the server, ApplyQualityLevel() only sets m_bResolutionChanging when the client will really restart the capture: it compares the target maxWidth with the client's reported bitmap width (cached atomically on TOKEN_BITMAPINFO) and stays conservative while a previous resize is still in flight. The fallback timeout drops from 5s to 1s, flushes the decoder and asks the client for a keyframe (COMMAND_SCREEN_KEYFRAME_REQ). Verified: Debug client and server build clean; the 1920-wide High->Good switch no longer waits out the gate. Not yet retested on a live session. Co-Authored-By: deepseek-flash
688 lines
30 KiB
C++
688 lines
30 KiB
C++
#pragma once
|
||
#include "stdafx.h"
|
||
#include "ScreenCapture.h"
|
||
#include "common/commands.h"
|
||
|
||
// 只要你安装了 Windows 8 SDK 或更高版本的 Windows SDK,编译器就能找到 dxgi1_2.h 并成功编译。
|
||
// 仅在 Windows 8 及更新版本上受支持
|
||
#include <dxgi1_2.h>
|
||
#include <d3d11.h>
|
||
#include <common/iniFile.h>
|
||
|
||
#pragma comment(lib, "d3d11.lib")
|
||
#pragma comment(lib, "dxgi.lib")
|
||
|
||
// author: ChatGPT
|
||
// update: 962914132@qq.com
|
||
// DXGI 1.2(IDXGIOutputDuplication)相比传统 GDI 截屏,性能提升通常在 3 倍到 10 倍之间,具体取决于硬件、分辨率和使用场景。
|
||
class ScreenCapturerDXGI : public ScreenCapture
|
||
{
|
||
private:
|
||
ID3D11Device* d3dDevice = nullptr;
|
||
ID3D11DeviceContext* d3dContext = nullptr;
|
||
IDXGIOutputDuplication* deskDupl = nullptr;
|
||
ID3D11Texture2D* cpuTexture = nullptr;
|
||
BYTE* m_NextBuffer = nullptr;
|
||
|
||
// 各缓冲是否已成功抓到过至少一帧。
|
||
// DXGI 的语义是「只在桌面有变化时才返回新帧」,静止桌面下 AcquireNextFrame
|
||
// 必然超时(DXGI_ERROR_WAIT_TIMEOUT)。超时必须复用上一帧而不是当作失败,
|
||
// 否则 ScanNextScreen 恒返回 nullptr → 只发光标帧 → H.264 编码器永远拿不到
|
||
// 首帧 IDR → 服务端 Decode() 不执行 → m_bIsFirst 无法解除 → 永久黑屏。
|
||
bool m_bFirstValid = false;
|
||
bool m_bNextValid = false;
|
||
static constexpr int kNoNewFrame = -100; // 无新帧,非错误
|
||
|
||
// 旋转输出(竖屏)下 DXGI 给的表面是「未旋转」的横向位图,
|
||
// 与桌面逻辑尺寸互为宽高交换,需要自行旋转。这里记录表面真实尺寸与旋转角。
|
||
int m_surfW = 0;
|
||
int m_surfH = 0;
|
||
DXGI_MODE_ROTATION m_rotation = DXGI_MODE_ROTATION_IDENTITY;
|
||
|
||
// InitDXGI 探测阶段抓到的帧。DuplicateOutput 之后 DXGI 会推送一帧当前桌面,
|
||
// 静止桌面下这往往就是唯一能拿到的一帧,丢弃它会导致首屏只能等桌面变化。
|
||
// 因此探测完先留在手上(不 ReleaseFrame),由 ConsumeProbeFrame() 消费。
|
||
IDXGIResource* m_probeRes = nullptr;
|
||
|
||
// DXGI 在部分环境下完全拿不到帧(无头/虚拟显示、Session 0 桌面、驱动不推送
|
||
// duplication 通知等),连首屏都要靠 GDI 兜底。这种情况下本会话直接降级为
|
||
// GDI 抓屏,否则 ScanNextScreen 永远返回 nullptr,表现为「首屏有画面,
|
||
// 但之后切换窗口画面不再更新」。
|
||
bool m_bGDIFallback = false;
|
||
|
||
// GDI 兜底用的离屏缓冲。降级后是每帧都要走的持续路径,必须复用,
|
||
// 否则每帧 CreateDIBSection/DeleteObject 会反复分配十几 MB 造成抖动。
|
||
HDC m_hGdiMemDC = nullptr;
|
||
HBITMAP m_hGdiBmp = nullptr;
|
||
HGDIOBJ m_hGdiOldBmp = nullptr;
|
||
void* m_pGdiBits = nullptr;
|
||
|
||
public:
|
||
ScreenCapturerDXGI(BYTE algo, int gop = DEFAULT_GOP, BOOL all = FALSE, int level = LEVEL_H264_SOFT,
|
||
RECT rc = {0}, BOOL switchScreen = TRUE)
|
||
: ScreenCapture(32, algo, all, level, rc, switchScreen)
|
||
{
|
||
m_GOP = gop;
|
||
InitDXGI(all, switchScreen);
|
||
Mprintf("Capture screen with DXGI: GOP= %d\n", m_GOP);
|
||
}
|
||
|
||
~ScreenCapturerDXGI()
|
||
{
|
||
CleanupDXGI();
|
||
|
||
SAFE_DELETE_ARRAY(m_FirstBuffer);
|
||
SAFE_DELETE_ARRAY(m_NextBuffer);
|
||
SAFE_DELETE_ARRAY(m_RectBuffer);
|
||
}
|
||
|
||
virtual BOOL UsingDXGI() const
|
||
{
|
||
return TRUE;
|
||
}
|
||
|
||
void InitDXGI(BOOL all, BOOL switchScreen)
|
||
{
|
||
m_iScreenX = 0;
|
||
m_iScreenY = 0;
|
||
// 1. 创建 D3D11 设备
|
||
D3D11CreateDevice(nullptr, D3D_DRIVER_TYPE_HARDWARE, NULL, 0, nullptr, 0, D3D11_SDK_VERSION, &d3dDevice, nullptr, &d3dContext);
|
||
|
||
IDXGIFactory1* pFactory = nullptr;
|
||
IDXGIAdapter1* dxgiAdapter = nullptr;
|
||
IDXGIOutput* dxgiOutput = nullptr;
|
||
IDXGIOutput1* dxgiOutput1 = nullptr;
|
||
|
||
// 2. 创建工厂
|
||
CreateDXGIFactory1(__uuidof(IDXGIFactory1), (void**)&pFactory);
|
||
if (!pFactory) return;
|
||
|
||
do {
|
||
// 3. 获取设备
|
||
static UINT idx = 0;
|
||
idx = pFactory->EnumAdapters1(idx, &dxgiAdapter) == DXGI_ERROR_NOT_FOUND ? 0 : idx;
|
||
if (!dxgiAdapter) pFactory->EnumAdapters1(idx, &dxgiAdapter);
|
||
if (!dxgiAdapter)break;
|
||
|
||
// 4. 获取 DXGI 输出(屏幕)
|
||
// screen = 下一次切屏要用的输出索引(沿用原语义:切屏时自增)
|
||
// current = 当前正在用的输出。重建截屏对象(质量/分辨率变化)必须沿用它:
|
||
// 读 screen 会跳到下一个输出,多显示器下表现为"调个质量就换屏",
|
||
// 单显示器下 EnumOutputs(1) 不存在,会让 DXGI 初始化失败降级成 GDI。
|
||
static UINT screen = 0;
|
||
static UINT current = 0;
|
||
UINT outIndex = switchScreen ? screen++ : current;
|
||
HRESULT r = dxgiAdapter->EnumOutputs(outIndex, &dxgiOutput);
|
||
if (r == DXGI_ERROR_NOT_FOUND && all) {
|
||
// 索引在当前适配器上不存在(拔掉外接屏等):回到主输出再试,
|
||
// 避免拿一个无效索引去遍历适配器,最后降级成 GDI。
|
||
screen = 0;
|
||
current = 0;
|
||
idx ++;
|
||
dxgiAdapter->Release();
|
||
dxgiAdapter = nullptr;
|
||
continue;
|
||
}
|
||
current = outIndex;
|
||
if (!dxgiOutput)break;
|
||
|
||
// 4.1 获取屏幕位置(多显示器支持)
|
||
DXGI_OUTPUT_DESC outputDesc;
|
||
if (SUCCEEDED(dxgiOutput->GetDesc(&outputDesc))) {
|
||
m_iScreenX = outputDesc.DesktopCoordinates.left;
|
||
m_iScreenY = outputDesc.DesktopCoordinates.top;
|
||
}
|
||
|
||
// 5. 获取 DXGI 输出 1
|
||
dxgiOutput->QueryInterface(__uuidof(IDXGIOutput1), (void**)&dxgiOutput1);
|
||
if (!dxgiOutput1)break;
|
||
|
||
// 6. 创建 Desktop Duplication
|
||
dxgiOutput1->DuplicateOutput(d3dDevice, &deskDupl);
|
||
if (!deskDupl)break;
|
||
|
||
// 7. 获取屏幕大小
|
||
DXGI_OUTDUPL_DESC duplDesc;
|
||
deskDupl->GetDesc(&duplDesc);
|
||
|
||
// 旋转输出(竖屏)下 ModeDesc 报告的是逻辑桌面尺寸,而实际捕获表面是
|
||
// 「未旋转」的横向位图,两者宽高互换。若按 ModeDesc 建 CPU 纹理,
|
||
// CopyResource 会静默不复制(返回 void)→ cpuTexture 全零 → 黑屏。
|
||
// 先取一帧读真实纹理描述;取不到时按旋转关系推导,避免退回错误尺寸。
|
||
m_rotation = duplDesc.Rotation;
|
||
const bool swapWH = (m_rotation == DXGI_MODE_ROTATION_ROTATE90 ||
|
||
m_rotation == DXGI_MODE_ROTATION_ROTATE270);
|
||
|
||
UINT realW = swapWH ? duplDesc.ModeDesc.Height : duplDesc.ModeDesc.Width;
|
||
UINT realH = swapWH ? duplDesc.ModeDesc.Width : duplDesc.ModeDesc.Height;
|
||
DXGI_FORMAT realFmt = DXGI_FORMAT_B8G8R8A8_UNORM;
|
||
{
|
||
IDXGIResource* probeRes = nullptr;
|
||
DXGI_OUTDUPL_FRAME_INFO probeInfo = {};
|
||
HRESULT phr = deskDupl->AcquireNextFrame(200, &probeInfo, &probeRes);
|
||
if (SUCCEEDED(phr)) {
|
||
if (probeRes) {
|
||
ID3D11Texture2D* probeTex = nullptr;
|
||
if (SUCCEEDED(probeRes->QueryInterface(__uuidof(ID3D11Texture2D), (void**)&probeTex)) && probeTex) {
|
||
D3D11_TEXTURE2D_DESC td = {};
|
||
probeTex->GetDesc(&td);
|
||
realW = td.Width;
|
||
realH = td.Height;
|
||
realFmt = td.Format;
|
||
probeTex->Release();
|
||
}
|
||
// 不要丢弃这一帧:暂存起来,等缓冲分配完由 ConsumeProbeFrame()
|
||
// 直接填充首屏。否则静止桌面下首屏 AcquireNextFrame 必然超时。
|
||
m_probeRes = probeRes;
|
||
} else {
|
||
deskDupl->ReleaseFrame();
|
||
}
|
||
}
|
||
}
|
||
// 内存布局按 4 字节/像素计算,非 4 字节格式回退 BGRA
|
||
switch (realFmt) {
|
||
case DXGI_FORMAT_B8G8R8A8_UNORM:
|
||
case DXGI_FORMAT_B8G8R8A8_UNORM_SRGB:
|
||
case DXGI_FORMAT_R8G8B8A8_UNORM:
|
||
case DXGI_FORMAT_R8G8B8A8_UNORM_SRGB:
|
||
case DXGI_FORMAT_R10G10B10A2_UNORM:
|
||
break;
|
||
default:
|
||
Mprintf("[DXGI] unsupported fmt=%d, fallback BGRA\n", (int)realFmt);
|
||
realFmt = DXGI_FORMAT_B8G8R8A8_UNORM;
|
||
break;
|
||
}
|
||
// 后续所有缓冲/位图信息/缩放都按「正立」的逻辑尺寸计算
|
||
m_surfW = (int)realW;
|
||
m_surfH = (int)realH;
|
||
m_ulFullWidth = swapWH ? realH : realW;
|
||
m_ulFullHeight = swapWH ? realW : realH;
|
||
|
||
// 8. 创建 CPU 访问纹理(按表面原始尺寸/格式,保证 CopyResource 成功)
|
||
D3D11_TEXTURE2D_DESC desc = {};
|
||
desc.Width = realW;
|
||
desc.Height = realH;
|
||
desc.MipLevels = 1;
|
||
desc.ArraySize = 1;
|
||
desc.Format = realFmt;
|
||
desc.SampleDesc.Count = 1;
|
||
desc.Usage = D3D11_USAGE_STAGING;
|
||
desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
|
||
d3dDevice->CreateTexture2D(&desc, NULL, &cpuTexture);
|
||
|
||
// 9. 初始化 BITMAPINFO
|
||
m_BitmapInfor_Full = ConstructBitmapInfo(32, m_ulFullWidth, m_ulFullHeight);
|
||
iniFile cfg(CLIENT_PATH);
|
||
int strategy = cfg.GetInt("settings", "ScreenStrategy", 0);
|
||
int maxWidth = cfg.GetInt("settings", "ScreenWidth", 0);
|
||
m_BitmapInfor_Send = new BITMAPINFO(*m_BitmapInfor_Full);
|
||
m_nInstructionSet = cfg.GetInt("settings", "CpuSpeedup", 0);
|
||
|
||
if (strategy == 1) {
|
||
// strategy=1 或不支持缩放: 原始分辨率
|
||
} else if (maxWidth > 0 && maxWidth < m_BitmapInfor_Send->bmiHeader.biWidth) {
|
||
// maxWidth>0: 自定义 maxWidth,等比缩放(自适应质量使用)
|
||
float ratio = (float)maxWidth / m_BitmapInfor_Send->bmiHeader.biWidth;
|
||
m_BitmapInfor_Send->bmiHeader.biWidth = maxWidth;
|
||
m_BitmapInfor_Send->bmiHeader.biHeight = (LONG)(m_BitmapInfor_Send->bmiHeader.biHeight * ratio);
|
||
m_BitmapInfor_Send->bmiHeader.biSizeImage =
|
||
((m_BitmapInfor_Send->bmiHeader.biWidth * m_BitmapInfor_Send->bmiHeader.biBitCount + 31) / 32) *
|
||
4 * m_BitmapInfor_Send->bmiHeader.biHeight;
|
||
} else {
|
||
// strategy=0 或 maxWidth=0: 默认 1080p 限制(等比,避免竖屏被压扁)
|
||
FitDefaultSendSize(m_BitmapInfor_Send->bmiHeader.biWidth, m_BitmapInfor_Send->bmiHeader.biHeight);
|
||
m_BitmapInfor_Send->bmiHeader.biSizeImage =
|
||
((m_BitmapInfor_Send->bmiHeader.biWidth * m_BitmapInfor_Send->bmiHeader.biBitCount + 31) / 32) *
|
||
4 * m_BitmapInfor_Send->bmiHeader.biHeight;
|
||
}
|
||
// 10. 分配屏幕缓冲区
|
||
m_FirstBuffer = new BYTE[m_BitmapInfor_Full->bmiHeader.biSizeImage + 1];
|
||
m_NextBuffer = new BYTE[m_BitmapInfor_Full->bmiHeader.biSizeImage + 1];
|
||
m_RectBuffer = new BYTE[m_BitmapInfor_Full->bmiHeader.biSizeImage * 2 + 12];
|
||
m_BmpZoomBuffer = new BYTE[m_BitmapInfor_Send->bmiHeader.biSizeImage * 2 + 12];
|
||
m_BmpZoomFirst = nullptr;
|
||
|
||
// 缓冲就绪,把探测阶段暂存的那一帧直接填成首屏内容。
|
||
// 这一步让首屏不再依赖「桌面发生变化」,连接后立即可出画面。
|
||
if (m_probeRes) {
|
||
ConsumeProbeFrame();
|
||
}
|
||
|
||
// ROI
|
||
int w = m_ROI.right - m_ROI.left, h = m_ROI.bottom - m_ROI.top;
|
||
if (w > 0 && h > 0) {
|
||
m_nScaleSendWidth = m_BitmapInfor_Send->bmiHeader.biWidth;
|
||
m_nScaleSendHeight = m_BitmapInfor_Send->bmiHeader.biHeight;
|
||
m_BitmapInfor_Send->bmiHeader.biWidth = w;
|
||
m_BitmapInfor_Send->bmiHeader.biHeight = h;
|
||
m_BitmapInfor_Send->bmiHeader.biSizeImage = w * h * 4;
|
||
}
|
||
Mprintf("[DXGI] out=%u mode=%ux%u surf=%ux%u fmt=%d rot=%d -> send=%dx%d\n",
|
||
outIndex, duplDesc.ModeDesc.Width, duplDesc.ModeDesc.Height,
|
||
m_surfW, m_surfH, (int)realFmt, (int)m_rotation,
|
||
(int)m_BitmapInfor_Send->bmiHeader.biWidth,
|
||
(int)m_BitmapInfor_Send->bmiHeader.biHeight);
|
||
break;
|
||
} while (true);
|
||
|
||
// 释放 DXGI 资源
|
||
if (dxgiOutput1) dxgiOutput1->Release();
|
||
if (dxgiOutput) dxgiOutput->Release();
|
||
if (dxgiAdapter) dxgiAdapter->Release();
|
||
if (pFactory) pFactory->Release();
|
||
}
|
||
|
||
bool IsInitSucceed() const
|
||
{
|
||
return cpuTexture;
|
||
}
|
||
|
||
void CleanupDXGI()
|
||
{
|
||
// GDI 兜底缓冲:先把旧位图选回 DC,再删 DC,最后删位图
|
||
if (m_hGdiMemDC) {
|
||
// SelectObject 失败会返回 HGDI_ERROR,不能拿它去恢复,否则位图仍被
|
||
// DC 选入,后面的 DeleteObject 会失败
|
||
if (m_hGdiOldBmp && m_hGdiOldBmp != HGDI_ERROR)
|
||
SelectObject(m_hGdiMemDC, m_hGdiOldBmp);
|
||
DeleteDC(m_hGdiMemDC);
|
||
m_hGdiMemDC = nullptr;
|
||
m_hGdiOldBmp = nullptr;
|
||
}
|
||
if (m_hGdiBmp) {
|
||
DeleteObject(m_hGdiBmp);
|
||
m_hGdiBmp = nullptr;
|
||
m_pGdiBits = nullptr;
|
||
}
|
||
// 探测帧若还挂着(例如 InitDXGI 中途失败),必须归还,否则 duplication 无法释放
|
||
if (m_probeRes) {
|
||
if (deskDupl) deskDupl->ReleaseFrame();
|
||
m_probeRes->Release();
|
||
m_probeRes = nullptr;
|
||
}
|
||
if (cpuTexture) cpuTexture->Release();
|
||
if (deskDupl) deskDupl->Release();
|
||
if (d3dContext) d3dContext->Release();
|
||
if (d3dDevice) d3dDevice->Release();
|
||
}
|
||
|
||
virtual LPBYTE scaleBitmap(LPBYTE target, LPBYTE bitmap) override
|
||
{
|
||
int scaledW = m_nScaleSendWidth ? m_nScaleSendWidth : (int)m_BitmapInfor_Send->bmiHeader.biWidth;
|
||
int scaledH = m_nScaleSendHeight ? m_nScaleSendHeight : (int)m_BitmapInfor_Send->bmiHeader.biHeight;
|
||
if ((ULONG)scaledW == m_ulFullWidth && (ULONG)scaledH == m_ulFullHeight)
|
||
memcpy(target, bitmap, scaledW * scaledH * 4);
|
||
else
|
||
ScaleBitmap(target, (uint8_t*)bitmap, m_ulFullWidth, m_ulFullHeight, scaledW, scaledH, m_nInstructionSet);
|
||
if (m_nScaleSendWidth)
|
||
applyROICrop(target, target, scaledW, scaledH);
|
||
return target;
|
||
}
|
||
|
||
LPBYTE GetFirstScreenData(ULONG* ulFirstScreenLength) override
|
||
{
|
||
// 首屏是一次性同步操作:ScreenManager 在调用前已把 m_SendFirst 置 TRUE,
|
||
// 一旦返回 nullptr 就不会再重试。所以这里尽量拿到内容——探测阶段暂存的
|
||
// 帧通常已填充好,静止桌面下偶尔无新帧时再用短超时补抓几次。
|
||
const DWORD startTick = GetTickCount();
|
||
int ret = 0;
|
||
int attempts = 0;
|
||
if (!m_bFirstValid) { // 探测帧已填充时直接用,不再白等一次 AcquireNextFrame
|
||
ret = kNoNewFrame;
|
||
while (attempts < 3) {
|
||
++attempts;
|
||
ret = CaptureFrame(m_FirstBuffer, ulFirstScreenLength, 1, &m_bFirstValid);
|
||
if (ret != kNoNewFrame) break;
|
||
if (m_bFirstValid) { ret = 0; break; }
|
||
Sleep(20);
|
||
}
|
||
}
|
||
const DWORD elapsed = GetTickCount() - startTick;
|
||
if (elapsed > 300) {
|
||
// 只在明显偏慢时记录,正常连接不会产生这行
|
||
Mprintf("[DXGI] first screen slow: %ums attempts=%d ok=%d\n",
|
||
elapsed, attempts, ret == 0);
|
||
}
|
||
if (ret != 0) {
|
||
// DXGI 抓帧失败(无新帧 / 尺寸异常 / Map 失败等)。首屏不能放弃:一旦
|
||
// 返回 nullptr,服务端 m_bIsFirst 就再无机会解锁(ScreenManager 的
|
||
// m_SendFirst 已置 TRUE 不会重试),只能等桌面变化,表现为一直"请等待"。
|
||
if (CaptureByGDI(m_FirstBuffer, 1)) {
|
||
// duplication 连首帧都产不出来,本会话后续帧也必然是空的。
|
||
// 直接降级为 GDI 抓屏,保证画面能持续更新(否则只有首屏是活的)。
|
||
m_bGDIFallback = true;
|
||
if (deskDupl) {
|
||
deskDupl->Release();
|
||
deskDupl = nullptr;
|
||
}
|
||
Mprintf("[DXGI] no frame from duplication (ret=%d), session falls back to GDI\n", ret);
|
||
ret = 0;
|
||
} else {
|
||
*ulFirstScreenLength = 0;
|
||
return nullptr;
|
||
}
|
||
}
|
||
// reservedBytes=1,实际位图从 m_FirstBuffer+1 开始;
|
||
// 服务端也是从偏移 1 开始拷贝,写回时必须保持同样的偏移
|
||
scaleBitmap(m_BmpZoomBuffer, m_FirstBuffer + 1);
|
||
memcpy(m_FirstBuffer + 1, m_BmpZoomBuffer, m_BitmapInfor_Send->bmiHeader.biSizeImage);
|
||
// 缓冲已被缩放图覆盖,+1 处不再是完整帧,后续无新帧时不可再复用
|
||
m_bFirstValid = false;
|
||
if (m_bAlgorithm == ALGORITHM_GRAY) {
|
||
ToGray(1 + m_RectBuffer, 1 + m_RectBuffer, m_BitmapInfor_Send->bmiHeader.biSizeImage);
|
||
}
|
||
// 长度必须是缩放后的发送尺寸,不能沿用 CaptureFrame 写入的全尺寸,
|
||
// 否则会把整帧原始数据(数倍于图像)一并发给服务端
|
||
*ulFirstScreenLength = m_BitmapInfor_Send->bmiHeader.biSizeImage;
|
||
m_FirstBuffer[0] = TOKEN_FIRSTSCREEN;
|
||
return m_FirstBuffer;
|
||
}
|
||
|
||
LPBYTE ScanNextScreen() override
|
||
{
|
||
ULONG ulNextScreenLength = 0;
|
||
int ret = CaptureFrame(m_NextBuffer, &ulNextScreenLength, 0, &m_bNextValid);
|
||
if (ret == kNoNewFrame) {
|
||
// 无新帧:m_NextBuffer 里已是上次缩放后的结果,必须原样返回,
|
||
// 不能再走一遍 scaleBitmap(源已是缩放图,重复缩放会损坏画面)
|
||
return m_bNextValid ? m_NextBuffer : nullptr;
|
||
}
|
||
if (ret)
|
||
return nullptr;
|
||
scaleBitmap(m_BmpZoomBuffer, m_NextBuffer);
|
||
memcpy(m_NextBuffer, m_BmpZoomBuffer, m_BitmapInfor_Send->bmiHeader.biSizeImage);
|
||
|
||
return m_NextBuffer;
|
||
}
|
||
|
||
virtual LPBYTE GetFirstBuffer() const
|
||
{
|
||
return m_FirstBuffer + 1;
|
||
}
|
||
|
||
private:
|
||
// 重新初始化 Desktop Duplication
|
||
BOOL ReinitDuplication()
|
||
{
|
||
// 重建前必须把暂存帧归还,否则旧的 duplication 无法释放
|
||
if (m_probeRes) {
|
||
if (deskDupl) deskDupl->ReleaseFrame();
|
||
m_probeRes->Release();
|
||
m_probeRes = nullptr;
|
||
}
|
||
if (deskDupl) {
|
||
deskDupl->Release();
|
||
deskDupl = nullptr;
|
||
}
|
||
|
||
if (!d3dDevice) return FALSE;
|
||
|
||
IDXGIDevice* dxgiDevice = nullptr;
|
||
IDXGIAdapter* dxgiAdapter = nullptr;
|
||
IDXGIOutput* dxgiOutput = nullptr;
|
||
IDXGIOutput1* dxgiOutput1 = nullptr;
|
||
|
||
HRESULT hr = d3dDevice->QueryInterface(__uuidof(IDXGIDevice), (void**)&dxgiDevice);
|
||
if (FAILED(hr)) return FALSE;
|
||
|
||
hr = dxgiDevice->GetAdapter(&dxgiAdapter);
|
||
dxgiDevice->Release();
|
||
if (FAILED(hr)) return FALSE;
|
||
|
||
hr = dxgiAdapter->EnumOutputs(0, &dxgiOutput);
|
||
dxgiAdapter->Release();
|
||
if (FAILED(hr)) return FALSE;
|
||
|
||
hr = dxgiOutput->QueryInterface(__uuidof(IDXGIOutput1), (void**)&dxgiOutput1);
|
||
dxgiOutput->Release();
|
||
if (FAILED(hr)) return FALSE;
|
||
|
||
Sleep(100);
|
||
|
||
hr = dxgiOutput1->DuplicateOutput(d3dDevice, &deskDupl);
|
||
dxgiOutput1->Release();
|
||
|
||
return SUCCEEDED(hr) && deskDupl;
|
||
}
|
||
// 惰性创建 GDI 离屏 DIB(尺寸与 DXGI 表面一致,32bpp bottom-up,与发送格式相同)
|
||
bool EnsureGDIBuffer()
|
||
{
|
||
if (m_hGdiMemDC && m_hGdiBmp && m_pGdiBits) return true;
|
||
// m_BitmapInfor_Full 已是 32bpp BI_RGB、biHeight 为正(bottom-up)、
|
||
// 尺寸等于 m_ulFullWidth/Height,直接复用,不必另建一份 BITMAPINFO
|
||
if (!m_BitmapInfor_Full || m_ulFullWidth == 0 || m_ulFullHeight == 0) return false;
|
||
|
||
HDC hScreen = GetDC(NULL);
|
||
if (!hScreen) return false;
|
||
|
||
if (!m_hGdiMemDC) m_hGdiMemDC = CreateCompatibleDC(hScreen);
|
||
if (!m_hGdiBmp) {
|
||
m_hGdiBmp = CreateDIBSection(hScreen, m_BitmapInfor_Full, DIB_RGB_COLORS,
|
||
&m_pGdiBits, NULL, 0);
|
||
}
|
||
ReleaseDC(NULL, hScreen);
|
||
|
||
if (!m_hGdiMemDC || !m_hGdiBmp || !m_pGdiBits) return false;
|
||
|
||
HGDIOBJ old = SelectObject(m_hGdiMemDC, m_hGdiBmp);
|
||
if (!old || old == HGDI_ERROR) {
|
||
// 选入失败:销毁位图,否则下次会被上面的「已就绪」判断误认为可用,
|
||
// 而实际 BitBlt 写不到 m_pGdiBits,抓出来是未初始化的内存
|
||
DeleteObject(m_hGdiBmp);
|
||
m_hGdiBmp = nullptr;
|
||
m_pGdiBits = nullptr;
|
||
return false;
|
||
}
|
||
m_hGdiOldBmp = old;
|
||
return true;
|
||
}
|
||
|
||
// DXGI 的 AcquireNextFrame 只在桌面有新内容呈现时才返回。在无头/虚拟显示环境,
|
||
// 或驱动在 DuplicateOutput 后不推送初始帧时,它可能永远超时。此时首屏发不出去,
|
||
// 服务端 m_bIsFirst 永不解锁,表现为远程桌面一直停在"请等待......"。
|
||
// GDI BitBlt 不依赖桌面变化,这里用它兜底抓一帧。
|
||
bool CaptureByGDI(LPBYTE buffer, int reservedBytes)
|
||
{
|
||
if (!EnsureGDIBuffer()) return false;
|
||
|
||
HDC hScreen = GetDC(NULL);
|
||
if (!hScreen) return false;
|
||
BOOL blt = BitBlt(m_hGdiMemDC, 0, 0, (int)m_ulFullWidth, (int)m_ulFullHeight,
|
||
hScreen, m_iScreenX, m_iScreenY, SRCCOPY);
|
||
ReleaseDC(NULL, hScreen);
|
||
if (!blt) return false;
|
||
|
||
memcpy(buffer + reservedBytes, m_pGdiBits,
|
||
(size_t)m_ulFullWidth * m_ulFullHeight * 4);
|
||
return true;
|
||
}
|
||
|
||
// 把 DXGI 表面(top-down,srcW×srcH,BGRA)按 rotation 摆正后写入目标
|
||
// (bottom-up DIB,m_ulFullWidth×m_ulFullHeight)。分块处理保证缓存友好。
|
||
void BlitRotated(BYTE* dst, const BYTE* src, int srcPitch,
|
||
int srcW, int srcH, DXGI_MODE_ROTATION rot)
|
||
{
|
||
const int dstW = (int)m_ulFullWidth;
|
||
const int dstH = (int)m_ulFullHeight;
|
||
const int dstPitch = dstW * 4;
|
||
|
||
auto put = [&](int x, int y, int sx, int sy) {
|
||
memcpy(dst + (size_t)(dstH - 1 - y) * dstPitch + (size_t)x * 4,
|
||
src + (size_t)sy * srcPitch + (size_t)sx * 4, 4);
|
||
};
|
||
|
||
if (rot == DXGI_MODE_ROTATION_ROTATE90 || rot == DXGI_MODE_ROTATION_ROTATE270) {
|
||
const bool cw = (rot == DXGI_MODE_ROTATION_ROTATE90);
|
||
const int T = 32;
|
||
for (int y0 = 0; y0 < dstH; y0 += T) {
|
||
const int yEnd = (y0 + T < dstH) ? y0 + T : dstH;
|
||
for (int x0 = 0; x0 < dstW; x0 += T) {
|
||
const int xEnd = (x0 + T < dstW) ? x0 + T : dstW;
|
||
for (int y = y0; y < yEnd; y++) {
|
||
for (int x = x0; x < xEnd; x++) {
|
||
const int sx = cw ? y : (srcW - 1 - y);
|
||
const int sy = cw ? (srcH - 1 - x) : x;
|
||
put(x, y, sx, sy);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
} else if (rot == DXGI_MODE_ROTATION_ROTATE180) {
|
||
for (int y = 0; y < dstH; y++)
|
||
for (int x = 0; x < dstW; x++)
|
||
put(x, y, srcW - 1 - x, srcH - 1 - y);
|
||
} else {
|
||
// IDENTITY:整行拷贝(快路径)
|
||
for (int y = 0; y < dstH; y++)
|
||
memcpy(dst + (size_t)(dstH - 1 - y) * dstPitch,
|
||
src + (size_t)y * srcPitch, (size_t)dstPitch);
|
||
}
|
||
}
|
||
|
||
// 消费 InitDXGI 探测阶段暂存的那一帧,直接填充 m_FirstBuffer(偏移 1 起)。
|
||
// 无论成败都会释放暂存资源与 DXGI 帧,避免泄漏。
|
||
bool ConsumeProbeFrame()
|
||
{
|
||
if (!m_probeRes) return false;
|
||
|
||
bool ok = false;
|
||
ID3D11Texture2D* tex = nullptr;
|
||
if (cpuTexture && m_FirstBuffer &&
|
||
SUCCEEDED(m_probeRes->QueryInterface(__uuidof(ID3D11Texture2D), (void**)&tex)) && tex) {
|
||
// 尺寸与格式都必须与建 cpuTexture 时一致,否则 CopyResource 静默不复制
|
||
// (读出来是全零)。探测阶段 QI 失败会退回推导尺寸、格式白名单也可能把
|
||
// 非常见格式回退成 BGRA,这里再核对一次,不符就放弃消费,让
|
||
// GetFirstScreenData 走正常抓帧路径,避免把全黑画面当成首屏发出去。
|
||
D3D11_TEXTURE2D_DESC sd = {}, dd = {};
|
||
tex->GetDesc(&sd);
|
||
cpuTexture->GetDesc(&dd);
|
||
if (sd.Width == dd.Width && sd.Height == dd.Height && sd.Format == dd.Format) {
|
||
d3dContext->CopyResource(cpuTexture, tex);
|
||
|
||
D3D11_MAPPED_SUBRESOURCE mapped = {};
|
||
if (SUCCEEDED(d3dContext->Map(cpuTexture, 0, D3D11_MAP_READ, 0, &mapped))) {
|
||
BlitRotated(m_FirstBuffer + 1, (const BYTE*)mapped.pData,
|
||
(int)mapped.RowPitch, m_surfW, m_surfH, m_rotation);
|
||
d3dContext->Unmap(cpuTexture, 0);
|
||
m_bFirstValid = true;
|
||
ok = true;
|
||
}
|
||
} else {
|
||
Mprintf("[DXGI] probe %ux%u fmt=%d != cpu %ux%u fmt=%d, skip probe frame\n",
|
||
sd.Width, sd.Height, (int)sd.Format, dd.Width, dd.Height, (int)dd.Format);
|
||
}
|
||
tex->Release();
|
||
}
|
||
if (deskDupl) deskDupl->ReleaseFrame();
|
||
m_probeRes->Release();
|
||
m_probeRes = nullptr;
|
||
return ok;
|
||
}
|
||
|
||
int CaptureFrame(LPBYTE buffer, ULONG* frameSize, int reservedBytes, bool* valid)
|
||
{
|
||
// 本会话已降级为 GDI(首屏就是靠 GDI 兜底才成功的,说明 duplication 在这个
|
||
// 环境产不出帧)。继续轮询 DXGI 只会一直返回「无新帧」,画面就停住不动。
|
||
if (m_bGDIFallback) {
|
||
if (!CaptureByGDI(buffer, reservedBytes)) return -1;
|
||
*frameSize = m_ulFullWidth * m_ulFullHeight * 4;
|
||
if (valid) *valid = true;
|
||
return 0;
|
||
}
|
||
|
||
if (!deskDupl) {
|
||
if (!ReinitDuplication()) return -10;
|
||
}
|
||
|
||
// 超时保持 100ms(与修复前一致)。AcquireNextFrame 的 timeout 是「最多等
|
||
// 多久」而不是「一定等这么久」,有帧会立即返回;静止桌面下白等只会拖慢
|
||
// 抓屏线程的轮询节奏(曾用 1000ms,导致首屏和之后每一帧都阻塞 1 秒)。
|
||
const UINT timeout = 100;
|
||
|
||
// 1. 获取下一帧
|
||
IDXGIResource* desktopResource = nullptr;
|
||
DXGI_OUTDUPL_FRAME_INFO frameInfo;
|
||
HRESULT hr = deskDupl->AcquireNextFrame(timeout, &frameInfo, &desktopResource);
|
||
// 处理全屏切换导致的访问丢失
|
||
if (hr == DXGI_ERROR_ACCESS_LOST) {
|
||
if (ReinitDuplication()) {
|
||
hr = deskDupl->AcquireNextFrame(timeout, &frameInfo, &desktopResource);
|
||
}
|
||
}
|
||
|
||
// 桌面静止时没有新帧,这是 DXGI 的正常语义,不能当成错误。
|
||
// 调用方据此复用上一帧内容,保证编码器持续有输入。
|
||
if (hr == DXGI_ERROR_WAIT_TIMEOUT) {
|
||
return kNoNewFrame;
|
||
}
|
||
|
||
if (FAILED(hr)) {
|
||
return -1;
|
||
}
|
||
|
||
// 2. 获取 ID3D11Texture2D
|
||
ID3D11Texture2D* texture = nullptr;
|
||
hr = desktopResource->QueryInterface(__uuidof(ID3D11Texture2D), (void**)&texture);
|
||
if (FAILED(hr)) {
|
||
deskDupl->ReleaseFrame();
|
||
desktopResource->Release(); // 原来漏了,每次失败都会泄漏一个 DXGI 资源
|
||
return -2;
|
||
}
|
||
|
||
// 3. 复制到 CPU 纹理
|
||
// ID3D11DeviceContext::CopyResource 返回 void:尺寸/格式不匹配时不会报错,
|
||
// 只会静默不复制(目标保持原值 = 全零)。这里显式比对描述。
|
||
// 尺寸不一致时必须放弃这一帧:BlitRotated 按 m_surfW/H 计算源偏移,
|
||
// 表面变小会越界读取(崩溃或花屏),宁可跳过等 ReinitDuplication。
|
||
{
|
||
D3D11_TEXTURE2D_DESC sd = {};
|
||
texture->GetDesc(&sd);
|
||
if ((int)sd.Width != m_surfW || (int)sd.Height != m_surfH) {
|
||
static LONG s_sizeLog = 0;
|
||
if (s_sizeLog++ < 3) {
|
||
Mprintf("[DXGI] surface changed %dx%d -> %ux%u, drop frame\n",
|
||
m_surfW, m_surfH, sd.Width, sd.Height);
|
||
}
|
||
deskDupl->ReleaseFrame();
|
||
texture->Release();
|
||
desktopResource->Release();
|
||
return -4;
|
||
}
|
||
static LONG s_fmtLog = 0;
|
||
if (s_fmtLog < 3) {
|
||
D3D11_TEXTURE2D_DESC dd = {};
|
||
cpuTexture->GetDesc(&dd);
|
||
if (sd.Format != dd.Format) {
|
||
Mprintf("[DXGI] DESC MISMATCH fmt src=%d dst=%d\n",
|
||
(int)sd.Format, (int)dd.Format);
|
||
}
|
||
s_fmtLog++;
|
||
}
|
||
}
|
||
d3dContext->CopyResource(cpuTexture, texture);
|
||
|
||
// 4. 释放 DXGI 资源
|
||
deskDupl->ReleaseFrame();
|
||
texture->Release();
|
||
desktopResource->Release();
|
||
|
||
// 5. 读取纹理数据
|
||
D3D11_MAPPED_SUBRESOURCE mapped;
|
||
hr = d3dContext->Map(cpuTexture, 0, D3D11_MAP_READ, 0, &mapped);
|
||
if (FAILED(hr)) {
|
||
return -3;
|
||
}
|
||
|
||
// 6. 复制数据到缓冲区(按旋转角摆正 + 垂直翻转到 bottom-up DIB)
|
||
BlitRotated(buffer + reservedBytes, (const BYTE*)mapped.pData,
|
||
(int)mapped.RowPitch, m_surfW, m_surfH, m_rotation);
|
||
|
||
// 7. 清理
|
||
d3dContext->Unmap(cpuTexture, 0);
|
||
|
||
*frameSize = m_ulFullWidth * m_ulFullHeight * 4;
|
||
if (valid) *valid = true;
|
||
return 0;
|
||
}
|
||
};
|