blob: 47fe43d79b3568d9a3ab406c0d29fe1fe1236a87 [file] [log] [blame]
Stephen Whitea800ec92019-08-02 15:04:52 -04001/*
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/GrContext.h"
11#include "src/core/SkAutoMalloc.h"
12#include "tools/sk_app/DawnWindowContext.h"
13
Stephen White1e2adcf2019-10-16 09:48:53 -040014#include "dawn/dawn_proc.h"
15
Stephen White3cc8d4f2019-10-30 09:56:23 -040016static wgpu::TextureUsage kUsage = wgpu::TextureUsage::OutputAttachment |
17 wgpu::TextureUsage::CopySrc;
Stephen White7b86d4e2019-10-18 08:53:17 -040018
Stephen Whitea521c962019-12-04 09:57:48 -050019static void PrintDeviceError(WGPUErrorType, const char* message, void*) {
Stephen Whitea800ec92019-08-02 15:04:52 -040020 printf("Device error: %s\n", message);
21 SkASSERT(false);
22}
23
24namespace sk_app {
25
26DawnWindowContext::DawnWindowContext(const DisplayParams& params,
Stephen White3cc8d4f2019-10-30 09:56:23 -040027 wgpu::TextureFormat swapChainFormat)
Stephen Whitea800ec92019-08-02 15:04:52 -040028 : WindowContext(params)
29 , fSwapChainFormat(swapChainFormat)
30 , fInstance(std::make_unique<dawn_native::Instance>()) {
31}
32
33void DawnWindowContext::initializeContext(int width, int height) {
34 fWidth = width;
35 fHeight = height;
36 fDevice = onInitializeContext();
37 fContext = GrContext::MakeDawn(fDevice, fDisplayParams.fGrContextOptions);
38
39 if (!fContext) {
40 return;
41 }
42 fSwapChainImplementation = this->createSwapChainImplementation(-1, -1, fDisplayParams);
Stephen White3cc8d4f2019-10-30 09:56:23 -040043 wgpu::SwapChainDescriptor swapChainDesc;
Stephen Whitea800ec92019-08-02 15:04:52 -040044 swapChainDesc.implementation = reinterpret_cast<int64_t>(&fSwapChainImplementation);
Sean Gilhuly695f57d2020-03-30 13:23:00 -040045 fSwapChain = fDevice.CreateSwapChain(nullptr, &swapChainDesc);
Stephen Whitea800ec92019-08-02 15:04:52 -040046 if (!fSwapChain) {
47 fContext.reset();
48 return;
49 }
Stephen White7b86d4e2019-10-18 08:53:17 -040050 fSwapChain.Configure(fSwapChainFormat, kUsage, width, height);
Stephen White20c626a2019-10-15 13:35:37 -040051 fDevice.SetUncapturedErrorCallback(PrintDeviceError, 0);
Stephen Whitea800ec92019-08-02 15:04:52 -040052}
53
54DawnWindowContext::~DawnWindowContext() {
55}
56
57void DawnWindowContext::destroyContext() {
58 if (!fDevice.Get()) {
59 return;
60 }
61
62 this->onDestroyContext();
63
64 fContext.reset();
65 fDevice = nullptr;
66}
67
68sk_sp<SkSurface> DawnWindowContext::getBackbufferSurface() {
Stephen Whitef03c1162020-01-28 12:39:45 -050069 GrDawnRenderTargetInfo rtInfo;
70 rtInfo.fTextureView = fSwapChain.GetCurrentTextureView();
71 rtInfo.fFormat = fSwapChainFormat;
72 rtInfo.fLevelCount = 1; // FIXME
73 GrBackendRenderTarget backendRenderTarget(fWidth, fHeight, fDisplayParams.fMSAASampleCount, 8,
74 rtInfo);
75 fSurface = SkSurface::MakeFromBackendRenderTarget(fContext.get(),
76 backendRenderTarget,
77 this->getRTOrigin(),
78 fDisplayParams.fColorType,
79 fDisplayParams.fColorSpace,
80 &fDisplayParams.fSurfaceProps);
Stephen Whitea800ec92019-08-02 15:04:52 -040081 return fSurface;
82}
83
84void DawnWindowContext::swapBuffers() {
Stephen Whitef03c1162020-01-28 12:39:45 -050085 fSwapChain.Present();
Stephen Whitea800ec92019-08-02 15:04:52 -040086 this->onSwapBuffers();
87}
88
89void DawnWindowContext::resize(int w, int h) {
90 fWidth = w;
91 fHeight = h;
92 fSwapChainImplementation = this->createSwapChainImplementation(w, h, fDisplayParams);
Stephen White3cc8d4f2019-10-30 09:56:23 -040093 wgpu::SwapChainDescriptor swapChainDesc;
Stephen Whitea800ec92019-08-02 15:04:52 -040094 swapChainDesc.implementation = reinterpret_cast<int64_t>(&fSwapChainImplementation);
Sean Gilhuly695f57d2020-03-30 13:23:00 -040095 fSwapChain = fDevice.CreateSwapChain(nullptr, &swapChainDesc);
Stephen Whitea800ec92019-08-02 15:04:52 -040096 if (!fSwapChain) {
97 fContext.reset();
98 return;
99 }
Stephen White7b86d4e2019-10-18 08:53:17 -0400100 fSwapChain.Configure(fSwapChainFormat, kUsage, fWidth, fHeight);
Stephen Whitea800ec92019-08-02 15:04:52 -0400101}
102
103void DawnWindowContext::setDisplayParams(const DisplayParams& params) {
104 fDisplayParams = params;
105}
106
Stephen White3cc8d4f2019-10-30 09:56:23 -0400107wgpu::Device DawnWindowContext::createDevice(dawn_native::BackendType type) {
Stephen Whitea800ec92019-08-02 15:04:52 -0400108 fInstance->DiscoverDefaultAdapters();
109 DawnProcTable backendProcs = dawn_native::GetProcs();
Stephen White1e2adcf2019-10-16 09:48:53 -0400110 dawnProcSetProcs(&backendProcs);
Stephen Whitea800ec92019-08-02 15:04:52 -0400111
112 std::vector<dawn_native::Adapter> adapters = fInstance->GetAdapters();
113 for (dawn_native::Adapter adapter : adapters) {
114 if (adapter.GetBackendType() == type) {
115 return adapter.CreateDevice();
116 }
117 }
118 return nullptr;
119}
120
121} //namespace sk_app