| 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 |
wgpu::BackendType ToDawnBackendType(sk_app::Window::BackendType backendType) { |
| 17 |
switch (backendType) { |
| 18 |
case sk_app::Window::BackendType::kGraphiteDawnD3D11: |
| 19 |
return wgpu::BackendType::D3D11; |
| 20 |
case sk_app::Window::BackendType::kGraphiteDawnD3D12: |
| 21 |
return wgpu::BackendType::D3D12; |
| 22 |
default: |
| 23 |
SkASSERT(false); |
| 24 |
return wgpu::BackendType::D3D12; |
| 25 |
} |
| 26 |
} |
| 27 |
|
| 28 |
class GraphiteDawnWindowContext_win : public GraphiteDawnWindowContext { |
| 29 |
public: |
| 30 |
GraphiteDawnWindowContext_win(HWND hwnd, std::unique_ptr<const DisplayParams> params, |
| 31 |
sk_app::Window::BackendType backendType); |
| 32 |
|
| 33 |
~GraphiteDawnWindowContext_win() override; |
| 34 |
|
| 35 |
bool onInitializeContext() override; |
| 36 |
void onDestroyContext() override; |
| 37 |
void resize(int w, int h) override; |
| 38 |
|
| 39 |
private: |
| 40 |
HWND fWindow; |
| 41 |
wgpu::BackendType fBackendType; |
| 42 |
}; |
| 43 |
|
| 44 |
GraphiteDawnWindowContext_win::GraphiteDawnWindowContext_win( |
| 45 |
HWND hwnd, |
| 46 |
std::unique_ptr<const DisplayParams> params, |
| 47 |
sk_app::Window::BackendType backendType) |
| 48 |
: GraphiteDawnWindowContext(std::move(params), wgpu::TextureFormat::BGRA8Unorm) |
| 49 |
, fWindow(hwnd) |
| 50 |
, fBackendType(ToDawnBackendType(backendType)) { |
| 51 |
RECT rect; |
| 52 |
GetClientRect(hwnd, &rect); |
| 53 |
this->initializeContext(rect.right - rect.left, rect.bottom - rect.top); |
| 54 |
} |
| 55 |
|
| 56 |
GraphiteDawnWindowContext_win::~GraphiteDawnWindowContext_win() { |
| 57 |
this->destroyContext(); |
| 58 |
} |
| 59 |
|
| 60 |
bool GraphiteDawnWindowContext_win::onInitializeContext() { |
| 61 |
SkASSERT(!!fWindow); |
| 62 |
|
| 63 |
auto device = this->createDevice(fBackendType); |
| 64 |
if (!device) { |
| 65 |
SkASSERT(device); |
| 66 |
return false; |
| 67 |
} |
| 68 |
|
| 69 |
wgpu::SurfaceSourceWindowsHWND surfaceChainedDesc; |
| 70 |
surfaceChainedDesc.hwnd = fWindow; |
| 71 |
surfaceChainedDesc.hinstance = GetModuleHandle(nullptr); |
| 72 |
wgpu::SurfaceDescriptor surfaceDesc; |
| 73 |
surfaceDesc.nextInChain = &surfaceChainedDesc; |
| 74 |
|
| 75 |
auto surface = wgpu::Instance(fInstance->Get()).CreateSurface(&surfaceDesc); |
| 76 |
if (!surface) { |
| 77 |
SkASSERT(false); |
| 78 |
return false; |
| 79 |
} |
| 80 |
|
| 81 |
fDevice = std::move(device); |
| 82 |
fSurface = std::move(surface); |
| 83 |
configureSurface(); |
| 84 |
|
| 85 |
return true; |
| 86 |
} |
| 87 |
|
| 88 |
void GraphiteDawnWindowContext_win::onDestroyContext() {} |
| 89 |
|
| 90 |
void GraphiteDawnWindowContext_win::resize(int w, int h) { |
| 91 |
configureSurface(); |
| 92 |
} |
| 93 |
|
| 94 |
} // anonymous namespace |
| 95 |
|
| 96 |
namespace skwindow { |
| 97 |
|
| 98 |
std::unique_ptr<WindowContext> MakeGraphiteDawnForWin( |
| 99 |
HWND hwnd, std::unique_ptr<const DisplayParams> params, sk_app::Window::BackendType backendType) { |
| 100 |
std::unique_ptr<WindowContext> ctx( |
| 101 |
new GraphiteDawnWindowContext_win(hwnd, std::move(params), backendType)); |
| 102 |
if (!ctx->isValid()) { |
| 103 |
return nullptr; |
| 104 |
} |
| 105 |
return ctx; |
| 106 |
} |
| 107 |
|
| 108 |
} // namespace skwindow |