在 C++ 中,我想加载图像,并使用 winrt 合成将其渲染到 win32 窗口。我只是想为我的其他应用程序构建一个简单的测试平台来测试 2dtexture 合成。
到目前为止,我已经尝试了很多实现方法。
void CreateDesktopWindowTarget(HWND window)
{
namespace abi = ABI::Windows::UI::Composition::Desktop;
auto interop = m_compositor.as<abi::ICompositorDesktopInterop>();
DesktopWindowTarget target{ nullptr };
check_hresult(interop->CreateDesktopWindowTarget(window, false, reinterpret_cast<abi::IDesktopWindowTarget**>(put_abi(target))));
m_target = target;
}
他们的大多数示例和示例代码都是这样的。这里失败了:
check_hresult(interop->CreateDesktopWindowTarget(window, false, reinterpret_cast<abi::IDesktopWindowTarget**>(put_abi(target))));
和:
C++: Exception thrown: read access violation. decoder.**ptr** was nullptr. (decoder.ptr was nullptr.)
我不知道或不理解这种 abi 类型反射在做什么。很难调试。失败只是深奥难懂。
这个临时文件就是我所在的位置:
#define NOMINMAX
#include <windows.h>
#include <windows.ui.composition.h>
#include <windows.ui.composition.desktop.h>
#include <windows.ui.composition.interop.h>
#include <unknwn.h> // For IUnknown and related COM interfaces
#include <activation.h> // For IActivationFactory
#include <roapi.h> // For RoInitialize, RoUninitialize, RoGetActivationFactory
#include <wrl.h> // For Microsoft::WRL::ComPtr
#include <d3d11.h>
#include <dxgi1_2.h>
#include <dxgidebug.h>
#include <cstdio>
#include <combaseapi.h>
// Link necessary libraries
#pragma comment(lib, "runtimeobject.lib")
#pragma comment(lib, "d3d11.lib")
#pragma comment(lib, "dxguid.lib")
#include <wrl/client.h> // For Microsoft::WRL::ComPtr
// WIL (Windows Implementation Library)
#include <wil/cppwinrt.h> // Needs to come before C++/WinRT headers
#include <wil/resource.h>
#include <wil/cppwinrt_helpers.h>
#include <wil/coroutine.h>
// C++/WinRT headers
#include <winrt/Windows.Foundation.h>
#include <winrt/Windows.Foundation.Collections.h>
#include <winrt/Windows.Foundation.Metadata.h>
#include <winrt/Windows.Graphics.Capture.h>
#include <winrt/Windows.Graphics.DirectX.h>
#include <winrt/Windows.Graphics.DirectX.Direct3D11.h>
#include <winrt/Windows.Graphics.Imaging.h>
#include <winrt/Windows.Security.Authorization.AppCapabilityAccess.h>
#include <winrt/Windows.Storage.h>
#include <winrt/Windows.Storage.Pickers.h>
#include <winrt/Windows.Storage.Streams.h>
#include <winrt/Windows.System.h>
#include <winrt/Windows.UI.h>
#include <winrt/Windows.UI.Composition.h>
#include <winrt/Windows.UI.Composition.Desktop.h>
#include <winrt/Windows.UI.Popups.h>
#include <winrt/Windows.Foundation.Numerics.h>
#include <winrt/Windows.UI.Composition.Interactions.h>
// STL headers
#include <atomic>
#include <memory>
#include <algorithm>
#include <unordered_set>
#include <vector>
#include <optional>
#include <future>
#include <mutex>
// D3D headers
#include <d3d11.h>
#include <d3d11_4.h>
#include <dxgi1_6.h>
#include <d2d1_3.h>
#include <wincodec.h>
// Helper headers
#include "ComPtr.hpp"
#include <robmikh.common/composition.interop.h>
#include <robmikh.common/composition.desktop.interop.h>
#include <robmikh.common/d3d11Helpers.h>
#include <robmikh.common/d3d11Helpers.desktop.h>
#include <robmikh.common/direct3d11.interop.h>
#include <robmikh.common/d2dHelpers.h>
#include <robmikh.common/capture.desktop.interop.h>
#include <robmikh.common/dispatcherqueue.desktop.interop.h>
#include <robmikh.common/stream.interop.h>
#include <robmikh.common/hwnd.interop.h>
#include <robmikh.common/ControlsHelper.h>
#pragma comment(lib, "d3d11.lib")
#pragma comment(lib, "windowscodecs.lib")
#pragma comment(lib, "user32.lib")
#pragma comment(lib, "gdi32.lib")
#pragma comment(lib, "comctl32.lib")
using namespace winrt;
namespace winrt
{
using namespace Windows::UI::Composition;
using namespace Windows::UI::Composition::Desktop;
using namespace Windows::Graphics::DirectX::Direct3D11;
using namespace Windows::Foundation;
using namespace Windows::Foundation::Numerics;
using namespace Windows::Foundation;
using namespace Windows::Foundation::Numerics;
using namespace Windows::Graphics;
using namespace Windows::Graphics::Capture;
using namespace Windows::Graphics::DirectX;
using namespace Windows::Graphics::DirectX::Direct3D11;
using namespace Windows::System;
using namespace Windows::UI;
using namespace Windows::UI::Composition;
using namespace Windows::System;
using namespace Windows::UI::Composition::Desktop;
using namespace Windows::Foundation::Numerics;
}
namespace util
{
using namespace robmikh::common::uwp;
using namespace robmikh::common::desktop;
}
winrt::Windows::UI::Composition::Compositor m_compositor{ nullptr };
winrt::Windows::UI::Composition::Desktop::DesktopWindowTarget m_target{ nullptr };
winrt::Windows::UI::Composition::ContainerVisual m_root{ nullptr };
winrt::Windows::UI::Composition::SpriteVisual m_content{ nullptr };
winrt::Windows::UI::Composition::CompositionSurfaceBrush m_brush{ nullptr };
winrt::Windows::Graphics::DirectX::Direct3D11::IDirect3DDevice m_device{ nullptr };
winrt::com_ptr<IDXGISwapChain1> m_swapChain{ nullptr };
winrt::com_ptr<ID3D11Device> m_d3dDevice{ nullptr };
winrt::com_ptr<ID3D11DeviceContext> m_d3dContext{ nullptr };
winrt::Windows::System::DispatcherQueueController m_dispatcherQueueController{ nullptr };
void CreateDesktopWindowTarget(HWND window)
{
namespace abi = ABI::Windows::UI::Composition::Desktop;
auto interop = m_compositor.as<abi::ICompositorDesktopInterop>();
DesktopWindowTarget target{ nullptr };
check_hresult(interop->CreateDesktopWindowTarget(window, false, reinterpret_cast<abi::IDesktopWindowTarget**>(put_abi(target))));
m_target = target;
}
void CreateCompositionRoot()
{
auto root = m_compositor.CreateContainerVisual();
root.RelativeSizeAdjustment({ 1.0f, 1.0f });
root.Offset({ 124, 12, 0 });
m_target.Root(root);
}
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
if (uMsg == WM_DESTROY)
{
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
void EnsureDispatcherQueue()
{
namespace abi = ABI::Windows::System;
if (m_dispatcherQueueController == nullptr)
{
DispatcherQueueOptions options
{
sizeof(DispatcherQueueOptions), /* dwSize */
DQTYPE_THREAD_CURRENT, /* threadType */
DQTAT_COM_ASTA /* apartmentType */
};
winrt::Windows::System::DispatcherQueueController controller{ nullptr };
check_hresult(CreateDispatcherQueueController(options, reinterpret_cast<abi::IDispatcherQueueController**>(put_abi(controller))));
m_dispatcherQueueController = controller;
}
}
int main()
{
winrt::init_apartment(winrt::apartment_type::multi_threaded);
EnsureDispatcherQueue();
auto m_compositor = winrt::Compositor();
const wchar_t CLASS_NAME[] = L"FrameShowWindowClass";
WNDCLASS wc = {};
wc.lpfnWndProc = WindowProc;
wc.hInstance = GetModuleHandle(NULL);
wc.lpszClassName = CLASS_NAME;
wc.style = CS_HREDRAW | CS_VREDRAW;
RegisterClass(&wc);
DWORD style = WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX;
DWORD exStyle = 0;
RECT rect = { 0, 0, 1024, 1024 };
AdjustWindowRectEx(&rect, style, FALSE, exStyle);
HWND hwnd = CreateWindowEx(
exStyle,
CLASS_NAME,
L"Frame Show",
style,
CW_USEDEFAULT, CW_USEDEFAULT,
rect.right - rect.left,
rect.bottom - rect.top,
NULL,
NULL,
wc.hInstance,
NULL
);
if (!hwnd)
{
return -1;
}
ShowWindow(hwnd, SW_SHOW);
const wchar_t CHILD_CLASS_NAME[] = L"ChildWindowClass";
WNDCLASS child_wc = {};
child_wc.lpfnWndProc = WindowProc;
child_wc.hInstance = GetModuleHandle(NULL);
child_wc.lpszClassName = CHILD_CLASS_NAME;
child_wc.style = CS_HREDRAW | CS_VREDRAW;
RegisterClass(&child_wc);
HWND hwndChild = CreateWindowEx(
0,
CHILD_CLASS_NAME,
NULL,
WS_CHILD | WS_VISIBLE,
0, 0, 1024, 1024,
hwnd,
NULL,
wc.hInstance,
NULL
);
auto d3dDevice = util::CreateD3D11Device();
auto dxgiDevice = d3dDevice.as<IDXGIDevice>();
m_device = CreateDirect3DDevice(dxgiDevice.get());
auto d2dFactory = util::CreateD2DFactory();
auto d2dDevice = util::CreateD2DDevice(d2dFactory, d3dDevice);
winrt::com_ptr<ID2D1DeviceContext> d2dContext;
ComPtr<IWICImagingFactory> wicFactory;
CoCreateInstance(
CLSID_WICImagingFactory,
NULL,
CLSCTX_INPROC_SERVER,
IID_PPV_ARGS(&wicFactory)
);
ComPtr<IWICBitmapDecoder> decoder;
std::wstring imagePath = L"C:\\data\\train - Copy\\outpu21567151.bmp";
wicFactory->CreateDecoderFromFilename(
imagePath.c_str(),
nullptr,
GENERIC_READ,
WICDecodeMetadataCacheOnLoad,
&decoder
);
ComPtr<IWICBitmapFrameDecode> frame;
decoder->GetFrame(0, &frame);
ComPtr<IWICFormatConverter> converter;
wicFactory->CreateFormatConverter(&converter);
converter->Initialize(
frame.Get(),
GUID_WICPixelFormat32bppRGBA,
WICBitmapDitherTypeNone,
nullptr,
0.f,
WICBitmapPaletteTypeCustom
);
UINT width, height;
converter->GetSize(&width, &height);
UINT stride = width * 4;
UINT imageSize = stride * height;
std::unique_ptr<BYTE[]> pixels(new BYTE[imageSize]);
converter->CopyPixels(NULL, stride, imageSize, pixels.get());
D3D11_TEXTURE2D_DESC texDesc = {};
texDesc.Width = 1024;
texDesc.Height = 1024;
texDesc.MipLevels = 1;
texDesc.ArraySize = 1;
texDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
texDesc.SampleDesc.Count = 1;
texDesc.Usage = D3D11_USAGE_DEFAULT;
texDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
texDesc.CPUAccessFlags = 0;
texDesc.MiscFlags = 0;
ComPtr<ID3D11Texture2D> texture;
d3dDevice->CreateTexture2D(&texDesc, nullptr, &texture);
d3dDevice->GetImmediateContext(m_d3dContext.put());
m_d3dContext->UpdateSubresource(texture.Get(), 0, nullptr, pixels.get(), stride, 0);
CreateDesktopWindowTarget(hwndChild);
CreateCompositionRoot();
auto compositionGraphicsDevice = util::CreateCompositionGraphicsDevice(m_compositor, d2dDevice.get());
auto compositionSurface = util::CreateCompositionSurfaceForSwapChain(m_compositor, texture.Get());
SpriteVisual spriteVisual = m_compositor.CreateSpriteVisual();
CompositionSurfaceBrush surfaceBrush = m_compositor.CreateSurfaceBrush(compositionSurface);
spriteVisual.Brush(surfaceBrush);
spriteVisual.RelativeSizeAdjustment({ 1.0f, 1.0f });
m_target.Root(spriteVisual);
MSG msg = {};
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
这只是一个临时文件。但我应该能够让它工作。如果你知道,我必须有一个调度队列吗?使用屏幕截图,我只需初始化一个单线程单元。