2 * Copyright 2019 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
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"
14 #include "dawn/dawn_proc.h"
16 static wgpu::TextureUsage kUsage = wgpu::TextureUsage::RenderAttachment |
17 wgpu::TextureUsage::CopySrc;
19 static void PrintDeviceError(WGPUErrorType, const char* message, void*) {
20 printf("Device error: %s\n", message);
26 DawnWindowContext::DawnWindowContext(const DisplayParams& params,
27 wgpu::TextureFormat swapChainFormat)
28 : WindowContext(params)
29 , fSwapChainFormat(swapChainFormat)
30 , fInstance(std::make_unique<dawn::native::Instance>()) {
33 void DawnWindowContext::initializeContext(int width, int height) {
38 fDevice = onInitializeContext();
40 fContext = GrDirectContext::MakeDawn(fDevice, fDisplayParams.fGrContextOptions);
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);
52 fSwapChain.Configure(fSwapChainFormat, kUsage, width, height);
53 fDevice.SetUncapturedErrorCallback(PrintDeviceError, 0);
56 DawnWindowContext::~DawnWindowContext() {
59 void DawnWindowContext::destroyContext() {
64 this->onDestroyContext();
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,
77 fSurface = SkSurface::MakeFromBackendRenderTarget(fContext.get(),
80 fDisplayParams.fColorType,
81 fDisplayParams.fColorSpace,
82 &fDisplayParams.fSurfaceProps);
86 void DawnWindowContext::swapBuffers() {
88 this->onSwapBuffers();
91 void DawnWindowContext::resize(int w, int 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);
102 fSwapChain.Configure(fSwapChainFormat, kUsage, fWidth, fHeight);
105 void DawnWindowContext::setDisplayParams(const DisplayParams& params) {
106 fDisplayParams = params;
109 wgpu::Device DawnWindowContext::createDevice(wgpu::BackendType type) {
110 fInstance->DiscoverDefaultAdapters();
111 DawnProcTable backendProcs = dawn::native::GetProcs();
112 dawnProcSetProcs(&backendProcs);
114 std::vector<dawn::native::Adapter> adapters = fInstance->GetAdapters();
115 for (dawn::native::Adapter adapter : adapters) {
116 wgpu::AdapterProperties properties;
117 adapter.GetProperties(&properties);
118 if (properties.backendType == type) {
119 return adapter.CreateDevice();