1*c8dee2aaSAndroid Build Coastguard Worker /*
2*c8dee2aaSAndroid Build Coastguard Worker * Copyright 2022 Google LLC
3*c8dee2aaSAndroid Build Coastguard Worker *
4*c8dee2aaSAndroid Build Coastguard Worker * Use of this source code is governed by a BSD-style license that can be
5*c8dee2aaSAndroid Build Coastguard Worker * found in the LICENSE file.
6*c8dee2aaSAndroid Build Coastguard Worker */
7*c8dee2aaSAndroid Build Coastguard Worker // Important to put this first because webgpu_cpp.h and X.h don't get along.
8*c8dee2aaSAndroid Build Coastguard Worker // Include these first, before X11 defines None, Success, Status etc.
9*c8dee2aaSAndroid Build Coastguard Worker #include "dawn/native/DawnNative.h" // NO_G3_REWRITE
10*c8dee2aaSAndroid Build Coastguard Worker #include "webgpu/webgpu_cpp.h" // NO_G3_REWRITE
11*c8dee2aaSAndroid Build Coastguard Worker
12*c8dee2aaSAndroid Build Coastguard Worker #include "tools/window/unix/GraphiteDawnVulkanWindowContext_unix.h"
13*c8dee2aaSAndroid Build Coastguard Worker
14*c8dee2aaSAndroid Build Coastguard Worker #include "tools/window/GraphiteDawnWindowContext.h"
15*c8dee2aaSAndroid Build Coastguard Worker #include "tools/window/unix/XlibWindowInfo.h"
16*c8dee2aaSAndroid Build Coastguard Worker
17*c8dee2aaSAndroid Build Coastguard Worker using skwindow::XlibWindowInfo;
18*c8dee2aaSAndroid Build Coastguard Worker using skwindow::DisplayParams;
19*c8dee2aaSAndroid Build Coastguard Worker using skwindow::internal::GraphiteDawnWindowContext;
20*c8dee2aaSAndroid Build Coastguard Worker
21*c8dee2aaSAndroid Build Coastguard Worker namespace {
22*c8dee2aaSAndroid Build Coastguard Worker
23*c8dee2aaSAndroid Build Coastguard Worker class GraphiteDawnVulkanWindowContext_unix : public GraphiteDawnWindowContext {
24*c8dee2aaSAndroid Build Coastguard Worker public:
25*c8dee2aaSAndroid Build Coastguard Worker GraphiteDawnVulkanWindowContext_unix(const XlibWindowInfo& info,
26*c8dee2aaSAndroid Build Coastguard Worker std::unique_ptr<const DisplayParams> params);
27*c8dee2aaSAndroid Build Coastguard Worker
28*c8dee2aaSAndroid Build Coastguard Worker ~GraphiteDawnVulkanWindowContext_unix() override;
29*c8dee2aaSAndroid Build Coastguard Worker
30*c8dee2aaSAndroid Build Coastguard Worker bool onInitializeContext() override;
31*c8dee2aaSAndroid Build Coastguard Worker void onDestroyContext() override;
32*c8dee2aaSAndroid Build Coastguard Worker void resize(int w, int h) override;
33*c8dee2aaSAndroid Build Coastguard Worker
34*c8dee2aaSAndroid Build Coastguard Worker private:
35*c8dee2aaSAndroid Build Coastguard Worker Display* fDisplay;
36*c8dee2aaSAndroid Build Coastguard Worker XWindow fWindow;
37*c8dee2aaSAndroid Build Coastguard Worker };
38*c8dee2aaSAndroid Build Coastguard Worker
GraphiteDawnVulkanWindowContext_unix(const XlibWindowInfo & info,std::unique_ptr<const DisplayParams> params)39*c8dee2aaSAndroid Build Coastguard Worker GraphiteDawnVulkanWindowContext_unix::GraphiteDawnVulkanWindowContext_unix(
40*c8dee2aaSAndroid Build Coastguard Worker const XlibWindowInfo& info, std::unique_ptr<const DisplayParams> params)
41*c8dee2aaSAndroid Build Coastguard Worker : GraphiteDawnWindowContext(std::move(params), wgpu::TextureFormat::BGRA8Unorm)
42*c8dee2aaSAndroid Build Coastguard Worker , fDisplay(info.fDisplay)
43*c8dee2aaSAndroid Build Coastguard Worker , fWindow(info.fWindow) {
44*c8dee2aaSAndroid Build Coastguard Worker XWindow root;
45*c8dee2aaSAndroid Build Coastguard Worker int x, y;
46*c8dee2aaSAndroid Build Coastguard Worker unsigned int border_width, depth;
47*c8dee2aaSAndroid Build Coastguard Worker unsigned int width, height;
48*c8dee2aaSAndroid Build Coastguard Worker XGetGeometry(fDisplay, fWindow, &root, &x, &y, &width, &height, &border_width, &depth);
49*c8dee2aaSAndroid Build Coastguard Worker this->initializeContext(width, height);
50*c8dee2aaSAndroid Build Coastguard Worker }
51*c8dee2aaSAndroid Build Coastguard Worker
~GraphiteDawnVulkanWindowContext_unix()52*c8dee2aaSAndroid Build Coastguard Worker GraphiteDawnVulkanWindowContext_unix::~GraphiteDawnVulkanWindowContext_unix() {
53*c8dee2aaSAndroid Build Coastguard Worker this->destroyContext();
54*c8dee2aaSAndroid Build Coastguard Worker }
55*c8dee2aaSAndroid Build Coastguard Worker
onInitializeContext()56*c8dee2aaSAndroid Build Coastguard Worker bool GraphiteDawnVulkanWindowContext_unix::onInitializeContext() {
57*c8dee2aaSAndroid Build Coastguard Worker SkASSERT(!!fWindow);
58*c8dee2aaSAndroid Build Coastguard Worker
59*c8dee2aaSAndroid Build Coastguard Worker auto device = this->createDevice(wgpu::BackendType::Vulkan);
60*c8dee2aaSAndroid Build Coastguard Worker if (!device) {
61*c8dee2aaSAndroid Build Coastguard Worker SkASSERT(device);
62*c8dee2aaSAndroid Build Coastguard Worker return false;
63*c8dee2aaSAndroid Build Coastguard Worker }
64*c8dee2aaSAndroid Build Coastguard Worker
65*c8dee2aaSAndroid Build Coastguard Worker wgpu::SurfaceDescriptorFromXlibWindow surfaceChainedDesc;
66*c8dee2aaSAndroid Build Coastguard Worker surfaceChainedDesc.display = fDisplay;
67*c8dee2aaSAndroid Build Coastguard Worker surfaceChainedDesc.window = fWindow;
68*c8dee2aaSAndroid Build Coastguard Worker
69*c8dee2aaSAndroid Build Coastguard Worker wgpu::SurfaceDescriptor surfaceDesc;
70*c8dee2aaSAndroid Build Coastguard Worker surfaceDesc.nextInChain = &surfaceChainedDesc;
71*c8dee2aaSAndroid Build Coastguard Worker
72*c8dee2aaSAndroid Build Coastguard Worker auto surface = wgpu::Instance(fInstance->Get()).CreateSurface(&surfaceDesc);
73*c8dee2aaSAndroid Build Coastguard Worker if (!surface) {
74*c8dee2aaSAndroid Build Coastguard Worker SkASSERT(false);
75*c8dee2aaSAndroid Build Coastguard Worker return false;
76*c8dee2aaSAndroid Build Coastguard Worker }
77*c8dee2aaSAndroid Build Coastguard Worker
78*c8dee2aaSAndroid Build Coastguard Worker fDevice = std::move(device);
79*c8dee2aaSAndroid Build Coastguard Worker fSurface = std::move(surface);
80*c8dee2aaSAndroid Build Coastguard Worker configureSurface();
81*c8dee2aaSAndroid Build Coastguard Worker
82*c8dee2aaSAndroid Build Coastguard Worker return true;
83*c8dee2aaSAndroid Build Coastguard Worker }
84*c8dee2aaSAndroid Build Coastguard Worker
onDestroyContext()85*c8dee2aaSAndroid Build Coastguard Worker void GraphiteDawnVulkanWindowContext_unix::onDestroyContext() {}
86*c8dee2aaSAndroid Build Coastguard Worker
resize(int w,int h)87*c8dee2aaSAndroid Build Coastguard Worker void GraphiteDawnVulkanWindowContext_unix::resize(int w, int h) {
88*c8dee2aaSAndroid Build Coastguard Worker configureSurface();
89*c8dee2aaSAndroid Build Coastguard Worker }
90*c8dee2aaSAndroid Build Coastguard Worker
91*c8dee2aaSAndroid Build Coastguard Worker } // anonymous namespace
92*c8dee2aaSAndroid Build Coastguard Worker
93*c8dee2aaSAndroid Build Coastguard Worker namespace skwindow {
94*c8dee2aaSAndroid Build Coastguard Worker
MakeGraphiteDawnVulkanForXlib(const XlibWindowInfo & info,std::unique_ptr<const DisplayParams> params)95*c8dee2aaSAndroid Build Coastguard Worker std::unique_ptr<WindowContext> MakeGraphiteDawnVulkanForXlib(
96*c8dee2aaSAndroid Build Coastguard Worker const XlibWindowInfo& info, std::unique_ptr<const DisplayParams> params) {
97*c8dee2aaSAndroid Build Coastguard Worker std::unique_ptr<WindowContext> ctx(
98*c8dee2aaSAndroid Build Coastguard Worker new GraphiteDawnVulkanWindowContext_unix(info, std::move(params)));
99*c8dee2aaSAndroid Build Coastguard Worker if (!ctx->isValid()) {
100*c8dee2aaSAndroid Build Coastguard Worker return nullptr;
101*c8dee2aaSAndroid Build Coastguard Worker }
102*c8dee2aaSAndroid Build Coastguard Worker return ctx;
103*c8dee2aaSAndroid Build Coastguard Worker }
104*c8dee2aaSAndroid Build Coastguard Worker
105*c8dee2aaSAndroid Build Coastguard Worker } // namespace skwindow
106