1 /* 2 * Copyright 2022 Google Inc. 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 #ifndef GraphiteDawnWindowContext_DEFINED 8 #define GraphiteDawnWindowContext_DEFINED 9 10 #include "tools/sk_app/WindowContext.h" 11 #include "webgpu/webgpu_cpp.h" 12 #include "dawn/native/DawnNative.h" 13 14 namespace sk_app { 15 16 class GraphiteDawnWindowContext : public WindowContext { 17 public: 18 GraphiteDawnWindowContext(const DisplayParams&, wgpu::TextureFormat swapChainFormat); 19 ~GraphiteDawnWindowContext() override; 20 sk_sp<SkSurface> getBackbufferSurface() override; 21 void swapBuffers() override; isValid()22 bool isValid() override { return SkToBool(fDevice.Get()); } 23 void setDisplayParams(const DisplayParams& params) override; 24 25 protected: isGpuContext()26 bool isGpuContext() override { return true; } 27 void initializeContext(int width, int height); 28 wgpu::Device createDevice(wgpu::BackendType type); 29 wgpu::SwapChain createSwapChain(); 30 void destroyContext(); 31 32 virtual bool onInitializeContext() = 0; 33 virtual void onDestroyContext() = 0; 34 virtual void onSwapBuffers() = 0; getRTOrigin()35 virtual GrSurfaceOrigin getRTOrigin() const { return kTopLeft_GrSurfaceOrigin; } 36 37 static constexpr wgpu::TextureUsage kTextureUsage = wgpu::TextureUsage::RenderAttachment; 38 39 wgpu::TextureFormat fSwapChainFormat; 40 std::unique_ptr<dawn::native::Instance> fInstance; 41 wgpu::Device fDevice; 42 wgpu::Queue fQueue; 43 wgpu::Surface fSurface; 44 wgpu::SwapChain fSwapChain; 45 }; 46 47 } // namespace sk_app 48 49 #endif 50