mirror of
https://github.com/yuanyuanxiang/SimpleRemoter.git
synced 2026-01-21 23:13:08 +08:00
style: Change files encoding format to UTF8-BOM
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
#pragma once
|
||||
#pragma once
|
||||
#include "stdafx.h"
|
||||
#include "ScreenCapture.h"
|
||||
#include "common/commands.h"
|
||||
|
||||
// ֻҪ<EFBFBD>㰲װ<EFBFBD><EFBFBD> Windows 8 SDK <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>߰汾<EFBFBD><EFBFBD> Windows SDK<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ҵ<EFBFBD> dxgi1_2.h <EFBFBD><EFBFBD><EFBFBD>ɹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>롣
|
||||
// <EFBFBD><EFBFBD><EFBFBD><EFBFBD> Windows 8 <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>°汾<EFBFBD><EFBFBD><EFBFBD><EFBFBD>֧<EFBFBD><EFBFBD>
|
||||
// 只要你安装了 Windows 8 SDK 或更高版本的 Windows SDK,编译器就能找到 dxgi1_2.h 并成功编译。
|
||||
// 仅在 Windows 8 及更新版本上受支持
|
||||
#include <dxgi1_2.h>
|
||||
#include <d3d11.h>
|
||||
|
||||
@@ -13,7 +13,7 @@
|
||||
|
||||
// author: ChatGPT
|
||||
// update: 962914132@qq.com
|
||||
// DXGI 1.2<EFBFBD><EFBFBD>IDXGIOutputDuplication<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȴ<EFBFBD>ͳ GDI <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͨ<EFBFBD><CDA8><EFBFBD><EFBFBD> 3 <20><><EFBFBD><EFBFBD> 10 <20><>֮<EFBFBD>䣬<EFBFBD><E4A3AC><EFBFBD><EFBFBD>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>Ӳ<EFBFBD><D3B2><EFBFBD><EFBFBD><EFBFBD>ֱ<EFBFBD><D6B1>ʺ<EFBFBD>ʹ<EFBFBD>ó<EFBFBD><C3B3><EFBFBD><EFBFBD><EFBFBD>
|
||||
// DXGI 1.2(IDXGIOutputDuplication)相比传统 GDI 截屏,性能提升通常在 3 倍到 10 倍之间,具体取决于硬件、分辨率和使用场景。
|
||||
class ScreenCapturerDXGI : public ScreenCapture
|
||||
{
|
||||
private:
|
||||
@@ -49,7 +49,7 @@ public:
|
||||
{
|
||||
m_iScreenX = 0;
|
||||
m_iScreenY = 0;
|
||||
// 1. <EFBFBD><EFBFBD><EFBFBD><EFBFBD> D3D11 <EFBFBD>豸
|
||||
// 1. 创建 D3D11 设备
|
||||
D3D11CreateDevice(nullptr, D3D_DRIVER_TYPE_HARDWARE, NULL, 0, nullptr, 0, D3D11_SDK_VERSION, &d3dDevice, nullptr, &d3dContext);
|
||||
|
||||
IDXGIFactory1* pFactory = nullptr;
|
||||
@@ -57,18 +57,18 @@ public:
|
||||
IDXGIOutput* dxgiOutput = nullptr;
|
||||
IDXGIOutput1* dxgiOutput1 = nullptr;
|
||||
|
||||
// 2. <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
// 2. 创建工厂
|
||||
CreateDXGIFactory1(__uuidof(IDXGIFactory1), (void**)&pFactory);
|
||||
if (!pFactory) return;
|
||||
|
||||
do {
|
||||
// 3. <EFBFBD><EFBFBD>ȡ<EFBFBD>豸
|
||||
// 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. <EFBFBD><EFBFBD>ȡ DXGI <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ļ<EFBFBD><EFBFBD>
|
||||
// 4. 获取 DXGI 输出(屏幕)
|
||||
static UINT screen = 0;
|
||||
HRESULT r = dxgiAdapter->EnumOutputs(screen++, &dxgiOutput);
|
||||
if (r == DXGI_ERROR_NOT_FOUND && all) {
|
||||
@@ -80,21 +80,21 @@ public:
|
||||
}
|
||||
if (!dxgiOutput)break;
|
||||
|
||||
// 5. <EFBFBD><EFBFBD>ȡ DXGI <EFBFBD><EFBFBD><EFBFBD><EFBFBD> 1
|
||||
// 5. 获取 DXGI 输出 1
|
||||
dxgiOutput->QueryInterface(__uuidof(IDXGIOutput1), (void**)&dxgiOutput1);
|
||||
if (!dxgiOutput1)break;
|
||||
|
||||
// 6. <EFBFBD><EFBFBD><EFBFBD><EFBFBD> Desktop Duplication
|
||||
// 6. 创建 Desktop Duplication
|
||||
dxgiOutput1->DuplicateOutput(d3dDevice, &deskDupl);
|
||||
if (!deskDupl)break;
|
||||
|
||||
// 7. <EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD>Ļ<EFBFBD><EFBFBD>С
|
||||
// 7. 获取屏幕大小
|
||||
DXGI_OUTDUPL_DESC duplDesc;
|
||||
deskDupl->GetDesc(&duplDesc);
|
||||
m_ulFullWidth = duplDesc.ModeDesc.Width;
|
||||
m_ulFullHeight = duplDesc.ModeDesc.Height;
|
||||
|
||||
// 8. <EFBFBD><EFBFBD><EFBFBD><EFBFBD> CPU <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
// 8. 创建 CPU 访问纹理
|
||||
D3D11_TEXTURE2D_DESC desc = {};
|
||||
desc.Width = m_ulFullWidth;
|
||||
desc.Height = m_ulFullHeight;
|
||||
@@ -106,7 +106,7 @@ public:
|
||||
desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
|
||||
d3dDevice->CreateTexture2D(&desc, NULL, &cpuTexture);
|
||||
|
||||
// 9. <EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD> BITMAPINFO
|
||||
// 9. 初始化 BITMAPINFO
|
||||
m_BitmapInfor_Full = ConstructBitmapInfo(32, m_ulFullWidth, m_ulFullHeight);
|
||||
m_BitmapInfor_Send = new BITMAPINFO(*m_BitmapInfor_Full);
|
||||
if (m_bAlgorithm != ALGORITHM_H264) {
|
||||
@@ -117,7 +117,7 @@ public:
|
||||
4 * m_BitmapInfor_Send->bmiHeader.biHeight;
|
||||
}
|
||||
|
||||
// 10. <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ļ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
// 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];
|
||||
@@ -127,7 +127,7 @@ public:
|
||||
break;
|
||||
} while (true);
|
||||
|
||||
// <EFBFBD>ͷ<EFBFBD> DXGI <EFBFBD><EFBFBD>Դ
|
||||
// 释放 DXGI 资源
|
||||
if (dxgiOutput1) dxgiOutput1->Release();
|
||||
if (dxgiOutput) dxgiOutput->Release();
|
||||
if (dxgiAdapter) dxgiAdapter->Release();
|
||||
@@ -188,7 +188,7 @@ public:
|
||||
}
|
||||
|
||||
private:
|
||||
// <EFBFBD><EFBFBD><EFBFBD>³<EFBFBD>ʼ<EFBFBD><EFBFBD> Desktop Duplication
|
||||
// 重新初始化 Desktop Duplication
|
||||
BOOL ReinitDuplication()
|
||||
{
|
||||
if (deskDupl) {
|
||||
@@ -231,11 +231,11 @@ private:
|
||||
if (!ReinitDuplication()) return -10;
|
||||
}
|
||||
|
||||
// 1. <EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD>һ֡
|
||||
// 1. 获取下一帧
|
||||
IDXGIResource* desktopResource = nullptr;
|
||||
DXGI_OUTDUPL_FRAME_INFO frameInfo;
|
||||
HRESULT hr = deskDupl->AcquireNextFrame(100, &frameInfo, &desktopResource);
|
||||
// <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȫ<EFBFBD><EFBFBD><EFBFBD>л<EFBFBD><EFBFBD><EFBFBD><EFBFBD>µķ<EFBFBD><EFBFBD>ʶ<EFBFBD>ʧ
|
||||
// 处理全屏切换导致的访问丢失
|
||||
if (hr == DXGI_ERROR_ACCESS_LOST) {
|
||||
if (ReinitDuplication()) {
|
||||
hr = deskDupl->AcquireNextFrame(100, &frameInfo, &desktopResource);
|
||||
@@ -246,7 +246,7 @@ private:
|
||||
return -1;
|
||||
}
|
||||
|
||||
// 2. <EFBFBD><EFBFBD>ȡ ID3D11Texture2D
|
||||
// 2. 获取 ID3D11Texture2D
|
||||
ID3D11Texture2D* texture = nullptr;
|
||||
hr = desktopResource->QueryInterface(__uuidof(ID3D11Texture2D), (void**)&texture);
|
||||
if (FAILED(hr)) {
|
||||
@@ -254,24 +254,24 @@ private:
|
||||
return -2;
|
||||
}
|
||||
|
||||
// 3. <EFBFBD><EFBFBD><EFBFBD>Ƶ<EFBFBD> CPU <EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
// 3. 复制到 CPU 纹理
|
||||
d3dContext->CopyResource(cpuTexture, texture);
|
||||
|
||||
// 4. <EFBFBD>ͷ<EFBFBD> DXGI <EFBFBD><EFBFBD>Դ
|
||||
// 4. 释放 DXGI 资源
|
||||
deskDupl->ReleaseFrame();
|
||||
texture->Release();
|
||||
desktopResource->Release();
|
||||
|
||||
// 5. <EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
// 5. 读取纹理数据
|
||||
D3D11_MAPPED_SUBRESOURCE mapped;
|
||||
hr = d3dContext->Map(cpuTexture, 0, D3D11_MAP_READ, 0, &mapped);
|
||||
if (FAILED(hr)) {
|
||||
return -3;
|
||||
}
|
||||
|
||||
// 6. <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֱ<EFBFBD><EFBFBD>ת<EFBFBD><EFBFBD>
|
||||
// 6. 复制数据到缓冲区(垂直翻转)
|
||||
BYTE* pData = (BYTE*)mapped.pData;
|
||||
int rowSize = m_ulFullWidth * 4; // ÿ<EFBFBD>е<EFBFBD><EFBFBD>ֽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>RGBA<EFBFBD><EFBFBD>
|
||||
int rowSize = m_ulFullWidth * 4; // 每行的字节数(RGBA)
|
||||
|
||||
BYTE* dest = buffer + reservedBytes + (m_ulFullHeight - 1) * rowSize;
|
||||
BYTE* src = pData;
|
||||
@@ -281,7 +281,7 @@ private:
|
||||
src += mapped.RowPitch;
|
||||
}
|
||||
|
||||
// 7. <EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
// 7. 清理
|
||||
d3dContext->Unmap(cpuTexture, 0);
|
||||
|
||||
*frameSize = m_ulFullWidth * m_ulFullHeight * 4;
|
||||
|
||||
Reference in New Issue
Block a user