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