1 /*
2 * Copyright 2019 Google Inc.
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/sk_app/DawnWindowContext.h"
9 #include "tools/sk_app/win/WindowContextFactory_win.h"
10 #include "dawn/webgpu_cpp.h"
11 #include "dawn/dawn_wsi.h"
12 #include "dawn_native/DawnNative.h"
13 #include "dawn_native/D3D12Backend.h"
14 #include "common/SwapChainUtils.h"
15
16 namespace sk_app {
17
18 class DawnD3D12WindowContext : public DawnWindowContext {
19 public:
20 DawnD3D12WindowContext(HWND hwnd, const DisplayParams& params);
21 ~DawnD3D12WindowContext() override;
22 wgpu::Device onInitializeContext() override;
23 void onDestroyContext() override;
24 DawnSwapChainImplementation createSwapChainImplementation(
25 int width, int height, const DisplayParams& params) override;
26 void onSwapBuffers() override;
27 private:
28 HWND fWindow;
29 };
30
31 // NOTE: this texture format must match the one in D3D12's swap chain impl
DawnD3D12WindowContext(HWND hwnd,const DisplayParams & params)32 DawnD3D12WindowContext::DawnD3D12WindowContext(HWND hwnd, const DisplayParams& params)
33 : DawnWindowContext(params, wgpu::TextureFormat::RGBA8Unorm)
34 , fWindow(hwnd) {
35 RECT rect;
36 GetClientRect(hwnd, &rect);
37 this->initializeContext(rect.right - rect.left, rect.bottom - rect.top);
38 }
39
~DawnD3D12WindowContext()40 DawnD3D12WindowContext::~DawnD3D12WindowContext() {
41 this->destroyContext();
42 }
43
createSwapChainImplementation(int width,int height,const DisplayParams & params)44 DawnSwapChainImplementation DawnD3D12WindowContext::createSwapChainImplementation(
45 int width, int height, const DisplayParams& params) {
46 return dawn_native::d3d12::CreateNativeSwapChainImpl(fDevice.Get(), fWindow);
47 }
48
onInitializeContext()49 wgpu::Device DawnD3D12WindowContext::onInitializeContext() {
50 return this->createDevice(dawn_native::BackendType::D3D12);
51 }
52
onDestroyContext()53 void DawnD3D12WindowContext::onDestroyContext() {
54 }
55
onSwapBuffers()56 void DawnD3D12WindowContext::onSwapBuffers() {
57 }
58
59 namespace window_context_factory {
60
MakeDawnD3D12ForWin(HWND hwnd,const DisplayParams & params)61 std::unique_ptr<WindowContext> MakeDawnD3D12ForWin(HWND hwnd,
62 const DisplayParams& params) {
63 std::unique_ptr<WindowContext> ctx(new DawnD3D12WindowContext(hwnd, params));
64 if (!ctx->isValid()) {
65 return nullptr;
66 }
67 return ctx;
68 }
69
70 }
71
72 } //namespace sk_app
73