blob: 134842d6a9870180a9e5223324457ec7b2eade9c [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"
Robert Phillipsed653392020-07-10 13:55:21 -040010#include "include/gpu/GrDirectContext.h"
Stephen Whitea800ec92019-08-02 15:04:52 -040011#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
Corentin Wallez19f8f272021-05-17 14:13:36 +020016static wgpu::TextureUsage kUsage = wgpu::TextureUsage::RenderAttachment |
Stephen White3cc8d4f2019-10-30 09:56:23 -040017 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) {
Robert Phillipsed653392020-07-10 13:55:21 -040034 SkASSERT(!fContext);
35
Stephen Whitea800ec92019-08-02 15:04:52 -040036 fWidth = width;
37 fHeight = height;
38 fDevice = onInitializeContext();
Stephen Whitea800ec92019-08-02 15:04:52 -040039
Robert Phillipsf4f80112020-07-13 16:13:31 -040040 fContext = GrDirectContext::MakeDawn(fDevice, fDisplayParams.fGrContextOptions);
Stephen Whitea800ec92019-08-02 15:04:52 -040041 if (!fContext) {
42 return;
43 }
44 fSwapChainImplementation = this->createSwapChainImplementation(-1, -1, fDisplayParams);
Stephen White3cc8d4f2019-10-30 09:56:23 -040045 wgpu::SwapChainDescriptor swapChainDesc;
Stephen Whitea800ec92019-08-02 15:04:52 -040046 swapChainDesc.implementation = reinterpret_cast<int64_t>(&fSwapChainImplementation);
Sean Gilhuly695f57d2020-03-30 13:23:00 -040047 fSwapChain = fDevice.CreateSwapChain(nullptr, &swapChainDesc);
Stephen Whitea800ec92019-08-02 15:04:52 -040048 if (!fSwapChain) {
49 fContext.reset();
50 return;
51 }
Stephen White7b86d4e2019-10-18 08:53:17 -040052 fSwapChain.Configure(fSwapChainFormat, kUsage, width, height);
Stephen White20c626a2019-10-15 13:35:37 -040053 fDevice.SetUncapturedErrorCallback(PrintDeviceError, 0);
Stephen Whitea800ec92019-08-02 15:04:52 -040054}
55
56DawnWindowContext::~DawnWindowContext() {
57}
58
59void DawnWindowContext::destroyContext() {
60 if (!fDevice.Get()) {
61 return;
62 }
63
64 this->onDestroyContext();
65
66 fContext.reset();
67 fDevice = nullptr;
68}
69
70sk_sp<SkSurface> DawnWindowContext::getBackbufferSurface() {
Stephen Whitef03c1162020-01-28 12:39:45 -050071 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);
Stephen Whitea800ec92019-08-02 15:04:52 -040083 return fSurface;
84}
85
86void DawnWindowContext::swapBuffers() {
Stephen Whitef03c1162020-01-28 12:39:45 -050087 fSwapChain.Present();
Stephen Whitea800ec92019-08-02 15:04:52 -040088 this->onSwapBuffers();
89}
90
91void DawnWindowContext::resize(int w, int h) {
92 fWidth = w;
93 fHeight = h;
94 fSwapChainImplementation = this->createSwapChainImplementation(w, h, fDisplayParams);
Stephen White3cc8d4f2019-10-30 09:56:23 -040095 wgpu::SwapChainDescriptor swapChainDesc;
Stephen Whitea800ec92019-08-02 15:04:52 -040096 swapChainDesc.implementation = reinterpret_cast<int64_t>(&fSwapChainImplementation);
Sean Gilhuly695f57d2020-03-30 13:23:00 -040097 fSwapChain = fDevice.CreateSwapChain(nullptr, &swapChainDesc);
Stephen Whitea800ec92019-08-02 15:04:52 -040098 if (!fSwapChain) {
99 fContext.reset();
100 return;
101 }
Stephen White7b86d4e2019-10-18 08:53:17 -0400102 fSwapChain.Configure(fSwapChainFormat, kUsage, fWidth, fHeight);
Stephen Whitea800ec92019-08-02 15:04:52 -0400103}
104
105void DawnWindowContext::setDisplayParams(const DisplayParams& params) {
106 fDisplayParams = params;
107}
108
Stephen White3cc8d4f2019-10-30 09:56:23 -0400109wgpu::Device DawnWindowContext::createDevice(dawn_native::BackendType type) {
Stephen Whitea800ec92019-08-02 15:04:52 -0400110 fInstance->DiscoverDefaultAdapters();
111 DawnProcTable backendProcs = dawn_native::GetProcs();
Stephen White1e2adcf2019-10-16 09:48:53 -0400112 dawnProcSetProcs(&backendProcs);
Stephen Whitea800ec92019-08-02 15:04:52 -0400113
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