xref: /aosp_15_r20/external/skia/tools/window/win/GraphiteDawnD3D12WindowContext_win.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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, std::unique_ptr<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,std::unique_ptr<const DisplayParams> params)30 GraphiteDawnD3D12WindowContext_win::GraphiteDawnD3D12WindowContext_win(
31         HWND hwnd, std::unique_ptr<const DisplayParams> params)
32         : GraphiteDawnWindowContext(std::move(params), wgpu::TextureFormat::BGRA8Unorm)
33         , fWindow(hwnd) {
34     RECT rect;
35     GetClientRect(hwnd, &rect);
36     this->initializeContext(rect.right - rect.left, rect.bottom - rect.top);
37 }
38 
~GraphiteDawnD3D12WindowContext_win()39 GraphiteDawnD3D12WindowContext_win::~GraphiteDawnD3D12WindowContext_win() {
40     this->destroyContext();
41 }
42 
onInitializeContext()43 bool GraphiteDawnD3D12WindowContext_win::onInitializeContext() {
44     SkASSERT(!!fWindow);
45 
46     auto device = this->createDevice(wgpu::BackendType::D3D12);
47     if (!device) {
48         SkASSERT(device);
49         return false;
50     }
51 
52     wgpu::SurfaceDescriptorFromWindowsHWND surfaceChainedDesc;
53     surfaceChainedDesc.hwnd = fWindow;
54     surfaceChainedDesc.hinstance = GetModuleHandle(nullptr);
55     wgpu::SurfaceDescriptor surfaceDesc;
56     surfaceDesc.nextInChain = &surfaceChainedDesc;
57 
58     auto surface = wgpu::Instance(fInstance->Get()).CreateSurface(&surfaceDesc);
59     if (!surface) {
60         SkASSERT(false);
61         return false;
62     }
63 
64     fDevice = std::move(device);
65     fSurface = std::move(surface);
66     configureSurface();
67 
68     return true;
69 }
70 
onDestroyContext()71 void GraphiteDawnD3D12WindowContext_win::onDestroyContext() {}
72 
resize(int w,int h)73 void GraphiteDawnD3D12WindowContext_win::resize(int w, int h) {
74     configureSurface();
75 }
76 
77 }  // anonymous namespace
78 
79 namespace skwindow {
80 
MakeGraphiteDawnD3D12ForWin(HWND hwnd,std::unique_ptr<const DisplayParams> params)81 std::unique_ptr<WindowContext> MakeGraphiteDawnD3D12ForWin(
82         HWND hwnd, std::unique_ptr<const DisplayParams> params) {
83     std::unique_ptr<WindowContext> ctx(
84             new GraphiteDawnD3D12WindowContext_win(hwnd, std::move(params)));
85     if (!ctx->isValid()) {
86         return nullptr;
87     }
88     return ctx;
89 }
90 
91 }  // namespace skwindow
92