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
8 #include "include/core/SkSurface.h"
9 #include "include/gpu/GrBackendSurface.h"
10 #include "include/gpu/GrDirectContext.h"
11 #include "src/core/SkAutoMalloc.h"
12 #include "tools/sk_app/DawnWindowContext.h"
13
14 #include "dawn/dawn_proc.h"
15
16 static wgpu::TextureUsage kUsage = wgpu::TextureUsage::RenderAttachment |
17 wgpu::TextureUsage::CopySrc;
18
PrintDeviceError(WGPUErrorType,const char * message,void *)19 static void PrintDeviceError(WGPUErrorType, const char* message, void*) {
20 printf("Device error: %s\n", message);
21 SkASSERT(false);
22 }
23
24 namespace sk_app {
25
DawnWindowContext(const DisplayParams & params,wgpu::TextureFormat swapChainFormat)26 DawnWindowContext::DawnWindowContext(const DisplayParams& params,
27 wgpu::TextureFormat swapChainFormat)
28 : WindowContext(params)
29 , fSwapChainFormat(swapChainFormat)
30 , fInstance(std::make_unique<dawn_native::Instance>()) {
31 }
32
initializeContext(int width,int height)33 void DawnWindowContext::initializeContext(int width, int height) {
34 SkASSERT(!fContext);
35
36 fWidth = width;
37 fHeight = height;
38 fDevice = onInitializeContext();
39
40 fContext = GrDirectContext::MakeDawn(fDevice, fDisplayParams.fGrContextOptions);
41 if (!fContext) {
42 return;
43 }
44 fSwapChainImplementation = this->createSwapChainImplementation(-1, -1, fDisplayParams);
45 wgpu::SwapChainDescriptor swapChainDesc;
46 swapChainDesc.implementation = reinterpret_cast<int64_t>(&fSwapChainImplementation);
47 fSwapChain = fDevice.CreateSwapChain(nullptr, &swapChainDesc);
48 if (!fSwapChain) {
49 fContext.reset();
50 return;
51 }
52 fSwapChain.Configure(fSwapChainFormat, kUsage, width, height);
53 fDevice.SetUncapturedErrorCallback(PrintDeviceError, 0);
54 }
55
~DawnWindowContext()56 DawnWindowContext::~DawnWindowContext() {
57 }
58
destroyContext()59 void DawnWindowContext::destroyContext() {
60 if (!fDevice.Get()) {
61 return;
62 }
63
64 this->onDestroyContext();
65
66 fContext.reset();
67 fDevice = nullptr;
68 }
69
getBackbufferSurface()70 sk_sp<SkSurface> DawnWindowContext::getBackbufferSurface() {
71 GrDawnRenderTargetInfo rtInfo;
72 rtInfo.fTextureView = fSwapChain.GetCurrentTextureView();
73 rtInfo.fFormat = fSwapChainFormat;
74 rtInfo.fLevelCount = 1; // FIXME
75 GrBackendRenderTarget backendRenderTarget(fWidth, fHeight, fDisplayParams.fMSAASampleCount, 8,
76 rtInfo);
77 fSurface = SkSurface::MakeFromBackendRenderTarget(fContext.get(),
78 backendRenderTarget,
79 this->getRTOrigin(),
80 fDisplayParams.fColorType,
81 fDisplayParams.fColorSpace,
82 &fDisplayParams.fSurfaceProps);
83 return fSurface;
84 }
85
swapBuffers()86 void DawnWindowContext::swapBuffers() {
87 fSwapChain.Present();
88 this->onSwapBuffers();
89 }
90
resize(int w,int h)91 void DawnWindowContext::resize(int w, int h) {
92 fWidth = w;
93 fHeight = h;
94 fSwapChainImplementation = this->createSwapChainImplementation(w, h, fDisplayParams);
95 wgpu::SwapChainDescriptor swapChainDesc;
96 swapChainDesc.implementation = reinterpret_cast<int64_t>(&fSwapChainImplementation);
97 fSwapChain = fDevice.CreateSwapChain(nullptr, &swapChainDesc);
98 if (!fSwapChain) {
99 fContext.reset();
100 return;
101 }
102 fSwapChain.Configure(fSwapChainFormat, kUsage, fWidth, fHeight);
103 }
104
setDisplayParams(const DisplayParams & params)105 void DawnWindowContext::setDisplayParams(const DisplayParams& params) {
106 fDisplayParams = params;
107 }
108
createDevice(dawn_native::BackendType type)109 wgpu::Device DawnWindowContext::createDevice(dawn_native::BackendType type) {
110 fInstance->DiscoverDefaultAdapters();
111 DawnProcTable backendProcs = dawn_native::GetProcs();
112 dawnProcSetProcs(&backendProcs);
113
114 std::vector<dawn_native::Adapter> adapters = fInstance->GetAdapters();
115 for (dawn_native::Adapter adapter : adapters) {
116 if (adapter.GetBackendType() == type) {
117 return adapter.CreateDevice();
118 }
119 }
120 return nullptr;
121 }
122
123 } //namespace sk_app
124