Debian Codegraph @

tools/window/android/GraphiteDawnWindowContext_android.cpp

raw
1 /*
2 * Copyright 2025 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/android/WindowContextFactory_android.h"
9 #include "tools/window/GraphiteDawnWindowContext.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::kGraphiteDawnVulkan:
19 return wgpu::BackendType::Vulkan;
20 case sk_app::Window::BackendType::kGraphiteDawnOpenGLES:
21 return wgpu::BackendType::OpenGLES;
22 default:
23 SkASSERT(false);
24 return wgpu::BackendType::Vulkan;
25 }
26 }
27
28 class GraphiteDawnWindowContext_android : public GraphiteDawnWindowContext {
29 public:
30 GraphiteDawnWindowContext_android(ANativeWindow* window,
31 std::unique_ptr<const DisplayParams> params,
32 sk_app::Window::BackendType backendType);
33
34 ~GraphiteDawnWindowContext_android() override;
35
36 bool onInitializeContext() override;
37 void onDestroyContext() override;
38 void resize(int w, int h) override;
39
40 private:
41 ANativeWindow* fWindow;
42 wgpu::BackendType fBackendType;
43 };
44
45 GraphiteDawnWindowContext_android::GraphiteDawnWindowContext_android(
46 ANativeWindow* window,
47 std::unique_ptr<const DisplayParams> params,
48 sk_app::Window::BackendType backendType)
49 : GraphiteDawnWindowContext(std::move(params), wgpu::TextureFormat::RGBA8Unorm)
50 , fWindow(window)
51 , fBackendType(ToDawnBackendType(backendType)) {
52 auto width = ANativeWindow_getWidth(window);
53 auto height = ANativeWindow_getHeight(window);
54
55 this->initializeContext(width, height);
56 }
57
58 GraphiteDawnWindowContext_android::~GraphiteDawnWindowContext_android() {
59 this->destroyContext();
60 }
61
62 bool GraphiteDawnWindowContext_android::onInitializeContext() {
63 SkASSERT(!!fWindow);
64
65 auto device = this->createDevice(fBackendType);
66 if (!device) {
67 SkASSERT(device);
68 return false;
69 }
70
71 wgpu::SurfaceSourceAndroidNativeWindow surfaceChainedDesc;
72 surfaceChainedDesc.window = fWindow;
73
74 wgpu::SurfaceDescriptor surfaceDesc;
75 surfaceDesc.nextInChain = &surfaceChainedDesc;
76
77 auto surface = wgpu::Instance(fInstance->Get()).CreateSurface(&surfaceDesc);
78 if (!surface) {
79 SkASSERT(false);
80 return false;
81 }
82
83 fDevice = std::move(device);
84 fSurface = std::move(surface);
85 configureSurface();
86
87 return true;
88 }
89
90 void GraphiteDawnWindowContext_android::onDestroyContext() {}
91
92 void GraphiteDawnWindowContext_android::resize(int w, int h) {
93 configureSurface();
94 }
95
96 } // anonymous namespace
97
98 namespace skwindow {
99
100 std::unique_ptr<WindowContext> MakeGraphiteDawnForAndroid(
101 ANativeWindow* window, std::unique_ptr<const DisplayParams> params,
102 sk_app::Window::BackendType backendType) {
103 std::unique_ptr<WindowContext> ctx(
104 new GraphiteDawnWindowContext_android(window, std::move(params), backendType));
105 if (!ctx->isValid()) {
106 return nullptr;
107 }
108 return ctx;
109 }
110
111 } // namespace skwindow