| 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 |
// Important to put this first because webgpu_cpp.h and X.h don't get along. |
| 8 |
// Include these first, before X11 defines None, Success, Status etc. |
| 9 |
#include "dawn/native/DawnNative.h" // NO_G3_REWRITE |
| 10 |
#include "webgpu/webgpu_cpp.h" // NO_G3_REWRITE |
| 11 |
|
| 12 |
#include "tools/window/unix/GraphiteDawnXlibWindowContext_unix.h" |
| 13 |
|
| 14 |
#include "tools/window/GraphiteDawnWindowContext.h" |
| 15 |
#include "tools/window/unix/XlibWindowInfo.h" |
| 16 |
|
| 17 |
using skwindow::XlibWindowInfo; |
| 18 |
using skwindow::DisplayParams; |
| 19 |
using skwindow::internal::GraphiteDawnWindowContext; |
| 20 |
|
| 21 |
namespace { |
| 22 |
|
| 23 |
wgpu::BackendType ToDawnBackendType(sk_app::Window::BackendType backendType) { |
| 24 |
switch (backendType) { |
| 25 |
case sk_app::Window::BackendType::kGraphiteDawnVulkan: |
| 26 |
return wgpu::BackendType::Vulkan; |
| 27 |
case sk_app::Window::BackendType::kGraphiteDawnOpenGLES: |
| 28 |
return wgpu::BackendType::OpenGLES; |
| 29 |
default: |
| 30 |
SkASSERT(false); |
| 31 |
return wgpu::BackendType::Vulkan; |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
wgpu::TextureFormat GetPreferredFormat(sk_app::Window::BackendType backendType) { |
| 36 |
if (backendType == sk_app::Window::BackendType::kGraphiteDawnOpenGLES) { |
| 37 |
return wgpu::TextureFormat::RGBA8Unorm; |
| 38 |
} |
| 39 |
return wgpu::TextureFormat::BGRA8Unorm; |
| 40 |
} |
| 41 |
|
| 42 |
class GraphiteDawnXlibWindowContext_unix : public GraphiteDawnWindowContext { |
| 43 |
public: |
| 44 |
GraphiteDawnXlibWindowContext_unix(const XlibWindowInfo& info, |
| 45 |
std::unique_ptr<const DisplayParams> params, |
| 46 |
sk_app::Window::BackendType backendType); |
| 47 |
|
| 48 |
~GraphiteDawnXlibWindowContext_unix() override; |
| 49 |
|
| 50 |
bool onInitializeContext() override; |
| 51 |
void onDestroyContext() override; |
| 52 |
void resize(int w, int h) override; |
| 53 |
|
| 54 |
private: |
| 55 |
Display* fDisplay; |
| 56 |
XWindow fWindow; |
| 57 |
wgpu::BackendType fBackendType; |
| 58 |
}; |
| 59 |
|
| 60 |
GraphiteDawnXlibWindowContext_unix::GraphiteDawnXlibWindowContext_unix( |
| 61 |
const XlibWindowInfo& info, |
| 62 |
std::unique_ptr<const DisplayParams> params, |
| 63 |
sk_app::Window::BackendType backendType) |
| 64 |
: GraphiteDawnWindowContext(std::move(params), GetPreferredFormat(backendType)) |
| 65 |
, fDisplay(info.fDisplay) |
| 66 |
, fWindow(info.fWindow) |
| 67 |
, fBackendType(ToDawnBackendType(backendType)) { |
| 68 |
XWindow root; |
| 69 |
int x, y; |
| 70 |
unsigned int border_width, depth; |
| 71 |
unsigned int width, height; |
| 72 |
XGetGeometry(fDisplay, fWindow, &root, &x, &y, &width, &height, &border_width, &depth); |
| 73 |
this->initializeContext(width, height); |
| 74 |
} |
| 75 |
|
| 76 |
GraphiteDawnXlibWindowContext_unix::~GraphiteDawnXlibWindowContext_unix() { |
| 77 |
this->destroyContext(); |
| 78 |
} |
| 79 |
|
| 80 |
bool GraphiteDawnXlibWindowContext_unix::onInitializeContext() { |
| 81 |
SkASSERT(!!fWindow); |
| 82 |
|
| 83 |
auto device = this->createDevice(fBackendType); |
| 84 |
if (!device) { |
| 85 |
SkASSERT(device); |
| 86 |
return false; |
| 87 |
} |
| 88 |
|
| 89 |
wgpu::SurfaceSourceXlibWindow surfaceChainedDesc; |
| 90 |
surfaceChainedDesc.display = fDisplay; |
| 91 |
surfaceChainedDesc.window = fWindow; |
| 92 |
|
| 93 |
wgpu::SurfaceDescriptor surfaceDesc; |
| 94 |
surfaceDesc.nextInChain = &surfaceChainedDesc; |
| 95 |
|
| 96 |
auto surface = wgpu::Instance(fInstance->Get()).CreateSurface(&surfaceDesc); |
| 97 |
if (!surface) { |
| 98 |
SkASSERT(false); |
| 99 |
return false; |
| 100 |
} |
| 101 |
|
| 102 |
fDevice = std::move(device); |
| 103 |
fSurface = std::move(surface); |
| 104 |
configureSurface(); |
| 105 |
|
| 106 |
return true; |
| 107 |
} |
| 108 |
|
| 109 |
void GraphiteDawnXlibWindowContext_unix::onDestroyContext() {} |
| 110 |
|
| 111 |
void GraphiteDawnXlibWindowContext_unix::resize(int w, int h) { |
| 112 |
configureSurface(); |
| 113 |
} |
| 114 |
|
| 115 |
} // anonymous namespace |
| 116 |
|
| 117 |
namespace skwindow { |
| 118 |
|
| 119 |
std::unique_ptr<WindowContext> MakeGraphiteDawnForXlib( |
| 120 |
const XlibWindowInfo& info, |
| 121 |
std::unique_ptr<const DisplayParams> params, |
| 122 |
sk_app::Window::BackendType backendType) { |
| 123 |
std::unique_ptr<WindowContext> ctx( |
| 124 |
new GraphiteDawnXlibWindowContext_unix(info, std::move(params), backendType)); |
| 125 |
if (!ctx->isValid()) { |
| 126 |
return nullptr; |
| 127 |
} |
| 128 |
return ctx; |
| 129 |
} |
| 130 |
|
| 131 |
} // namespace skwindow |