1 /* 2 * Copyright 2019 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 DawnWindowContext_DEFINED 8 #define DawnWindowContext_DEFINED 9 10 #include "include/core/SkRefCnt.h" 11 #include "include/core/SkSurface.h" 12 13 #include "tools/sk_app/WindowContext.h" 14 #include "dawn/webgpu_cpp.h" 15 #include "dawn_native/DawnNative.h" 16 #include "dawn/dawn_wsi.h" 17 18 class GrContext; 19 20 namespace sk_app { 21 22 class DawnWindowContext : public WindowContext { 23 public: 24 DawnWindowContext(const DisplayParams&, wgpu::TextureFormat swapChainFormat); 25 ~DawnWindowContext() override; 26 sk_sp<SkSurface> getBackbufferSurface() override; 27 void swapBuffers() override; isValid()28 bool isValid() override { return SkToBool(fDevice.Get()); } 29 30 void resize(int w, int h) override; 31 32 void setDisplayParams(const DisplayParams& params) override; 33 34 protected: isGpuContext()35 bool isGpuContext() override { return true; } 36 void initializeContext(int width, int height); 37 wgpu::Device createDevice(dawn_native::BackendType type); 38 virtual wgpu::Device onInitializeContext() = 0; 39 virtual void onDestroyContext() = 0; 40 virtual void onSwapBuffers() = 0; getRTOrigin()41 virtual GrSurfaceOrigin getRTOrigin() const { return kTopLeft_GrSurfaceOrigin; } 42 void destroyContext(); 43 virtual DawnSwapChainImplementation createSwapChainImplementation( int width, int height, 44 const DisplayParams& params) = 0; 45 46 sk_sp<SkSurface> fSurface; 47 DawnSwapChainImplementation fSwapChainImplementation; 48 wgpu::TextureFormat fSwapChainFormat; 49 wgpu::SwapChain fSwapChain; 50 wgpu::Device fDevice; 51 std::unique_ptr<dawn_native::Instance> fInstance; 52 }; 53 54 } // namespace sk_app 55 56 #endif 57