1 /*
2 * Copyright 2022 Google LLC
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include "tools/window/GraphiteDawnWindowContext.h"
9 #include "tools/window/win/WindowContextFactory_win.h"
10
11 using skwindow::DisplayParams;
12 using skwindow::internal::GraphiteDawnWindowContext;
13
14 namespace {
15
16 class GraphiteDawnD3D12WindowContext_win : public GraphiteDawnWindowContext {
17 public:
18 GraphiteDawnD3D12WindowContext_win(HWND hwnd, const DisplayParams& params);
19
20 ~GraphiteDawnD3D12WindowContext_win() override;
21
22 bool onInitializeContext() override;
23 void onDestroyContext() override;
24 void resize(int w, int h) override;
25
26 private:
27 HWND fWindow;
28 };
29
GraphiteDawnD3D12WindowContext_win(HWND hwnd,const DisplayParams & params)30 GraphiteDawnD3D12WindowContext_win::GraphiteDawnD3D12WindowContext_win(HWND hwnd,
31 const DisplayParams& params)
32 : GraphiteDawnWindowContext(params, wgpu::TextureFormat::BGRA8Unorm), fWindow(hwnd) {
33 RECT rect;
34 GetClientRect(hwnd, &rect);
35 this->initializeContext(rect.right - rect.left, rect.bottom - rect.top);
36 }
37
~GraphiteDawnD3D12WindowContext_win()38 GraphiteDawnD3D12WindowContext_win::~GraphiteDawnD3D12WindowContext_win() {
39 this->destroyContext();
40 }
41
onInitializeContext()42 bool GraphiteDawnD3D12WindowContext_win::onInitializeContext() {
43 SkASSERT(!!fWindow);
44
45 auto device = this->createDevice(wgpu::BackendType::D3D12);
46 if (!device) {
47 SkASSERT(device);
48 return false;
49 }
50
51 wgpu::SurfaceDescriptorFromWindowsHWND surfaceChainedDesc;
52 surfaceChainedDesc.hwnd = fWindow;
53 surfaceChainedDesc.hinstance = GetModuleHandle(nullptr);
54 wgpu::SurfaceDescriptor surfaceDesc;
55 surfaceDesc.nextInChain = &surfaceChainedDesc;
56
57 auto surface = wgpu::Instance(fInstance->Get()).CreateSurface(&surfaceDesc);
58 if (!surface) {
59 SkASSERT(false);
60 return false;
61 }
62
63 fDevice = std::move(device);
64 fSurface = std::move(surface);
65 fSwapChain = this->createSwapChain();
66
67 return true;
68 }
69
onDestroyContext()70 void GraphiteDawnD3D12WindowContext_win::onDestroyContext() {}
71
resize(int w,int h)72 void GraphiteDawnD3D12WindowContext_win::resize(int w, int h) {
73 fSwapChain = this->createSwapChain();
74 }
75
76 } // anonymous namespace
77
78 namespace skwindow {
79
MakeGraphiteDawnD3D12ForWin(HWND hwnd,const DisplayParams & params)80 std::unique_ptr<WindowContext> MakeGraphiteDawnD3D12ForWin(HWND hwnd, const DisplayParams& params) {
81 std::unique_ptr<WindowContext> ctx(new GraphiteDawnD3D12WindowContext_win(hwnd, params));
82 if (!ctx->isValid()) {
83 return nullptr;
84 }
85 return ctx;
86 }
87
88 } // namespace skwindow
89