Stephen White | a800ec9 | 2019-08-02 15:04:52 -0400 | [diff] [blame] | 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" |
Robert Phillips | ed65339 | 2020-07-10 13:55:21 -0400 | [diff] [blame] | 10 | #include "include/gpu/GrDirectContext.h" |
Stephen White | a800ec9 | 2019-08-02 15:04:52 -0400 | [diff] [blame] | 11 | #include "src/core/SkAutoMalloc.h" |
| 12 | #include "tools/sk_app/DawnWindowContext.h" |
| 13 | |
Stephen White | 1e2adcf | 2019-10-16 09:48:53 -0400 | [diff] [blame] | 14 | #include "dawn/dawn_proc.h" |
| 15 | |
Corentin Wallez | 19f8f27 | 2021-05-17 14:13:36 +0200 | [diff] [blame] | 16 | static wgpu::TextureUsage kUsage = wgpu::TextureUsage::RenderAttachment | |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 17 | wgpu::TextureUsage::CopySrc; |
Stephen White | 7b86d4e | 2019-10-18 08:53:17 -0400 | [diff] [blame] | 18 | |
Stephen White | a521c96 | 2019-12-04 09:57:48 -0500 | [diff] [blame] | 19 | static void PrintDeviceError(WGPUErrorType, const char* message, void*) { |
Stephen White | a800ec9 | 2019-08-02 15:04:52 -0400 | [diff] [blame] | 20 | printf("Device error: %s\n", message); |
| 21 | SkASSERT(false); |
| 22 | } |
| 23 | |
| 24 | namespace sk_app { |
| 25 | |
| 26 | DawnWindowContext::DawnWindowContext(const DisplayParams& params, |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 27 | wgpu::TextureFormat swapChainFormat) |
Stephen White | a800ec9 | 2019-08-02 15:04:52 -0400 | [diff] [blame] | 28 | : WindowContext(params) |
| 29 | , fSwapChainFormat(swapChainFormat) |
| 30 | , fInstance(std::make_unique<dawn_native::Instance>()) { |
| 31 | } |
| 32 | |
| 33 | void DawnWindowContext::initializeContext(int width, int height) { |
Robert Phillips | ed65339 | 2020-07-10 13:55:21 -0400 | [diff] [blame] | 34 | SkASSERT(!fContext); |
| 35 | |
Stephen White | a800ec9 | 2019-08-02 15:04:52 -0400 | [diff] [blame] | 36 | fWidth = width; |
| 37 | fHeight = height; |
| 38 | fDevice = onInitializeContext(); |
Stephen White | a800ec9 | 2019-08-02 15:04:52 -0400 | [diff] [blame] | 39 | |
Robert Phillips | f4f8011 | 2020-07-13 16:13:31 -0400 | [diff] [blame] | 40 | fContext = GrDirectContext::MakeDawn(fDevice, fDisplayParams.fGrContextOptions); |
Stephen White | a800ec9 | 2019-08-02 15:04:52 -0400 | [diff] [blame] | 41 | if (!fContext) { |
| 42 | return; |
| 43 | } |
| 44 | fSwapChainImplementation = this->createSwapChainImplementation(-1, -1, fDisplayParams); |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 45 | wgpu::SwapChainDescriptor swapChainDesc; |
Stephen White | a800ec9 | 2019-08-02 15:04:52 -0400 | [diff] [blame] | 46 | swapChainDesc.implementation = reinterpret_cast<int64_t>(&fSwapChainImplementation); |
Sean Gilhuly | 695f57d | 2020-03-30 13:23:00 -0400 | [diff] [blame] | 47 | fSwapChain = fDevice.CreateSwapChain(nullptr, &swapChainDesc); |
Stephen White | a800ec9 | 2019-08-02 15:04:52 -0400 | [diff] [blame] | 48 | if (!fSwapChain) { |
| 49 | fContext.reset(); |
| 50 | return; |
| 51 | } |
Stephen White | 7b86d4e | 2019-10-18 08:53:17 -0400 | [diff] [blame] | 52 | fSwapChain.Configure(fSwapChainFormat, kUsage, width, height); |
Stephen White | 20c626a | 2019-10-15 13:35:37 -0400 | [diff] [blame] | 53 | fDevice.SetUncapturedErrorCallback(PrintDeviceError, 0); |
Stephen White | a800ec9 | 2019-08-02 15:04:52 -0400 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | DawnWindowContext::~DawnWindowContext() { |
| 57 | } |
| 58 | |
| 59 | void DawnWindowContext::destroyContext() { |
| 60 | if (!fDevice.Get()) { |
| 61 | return; |
| 62 | } |
| 63 | |
| 64 | this->onDestroyContext(); |
| 65 | |
| 66 | fContext.reset(); |
| 67 | fDevice = nullptr; |
| 68 | } |
| 69 | |
| 70 | sk_sp<SkSurface> DawnWindowContext::getBackbufferSurface() { |
Stephen White | f03c116 | 2020-01-28 12:39:45 -0500 | [diff] [blame] | 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); |
Stephen White | a800ec9 | 2019-08-02 15:04:52 -0400 | [diff] [blame] | 83 | return fSurface; |
| 84 | } |
| 85 | |
| 86 | void DawnWindowContext::swapBuffers() { |
Stephen White | f03c116 | 2020-01-28 12:39:45 -0500 | [diff] [blame] | 87 | fSwapChain.Present(); |
Stephen White | a800ec9 | 2019-08-02 15:04:52 -0400 | [diff] [blame] | 88 | this->onSwapBuffers(); |
| 89 | } |
| 90 | |
| 91 | void DawnWindowContext::resize(int w, int h) { |
| 92 | fWidth = w; |
| 93 | fHeight = h; |
| 94 | fSwapChainImplementation = this->createSwapChainImplementation(w, h, fDisplayParams); |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 95 | wgpu::SwapChainDescriptor swapChainDesc; |
Stephen White | a800ec9 | 2019-08-02 15:04:52 -0400 | [diff] [blame] | 96 | swapChainDesc.implementation = reinterpret_cast<int64_t>(&fSwapChainImplementation); |
Sean Gilhuly | 695f57d | 2020-03-30 13:23:00 -0400 | [diff] [blame] | 97 | fSwapChain = fDevice.CreateSwapChain(nullptr, &swapChainDesc); |
Stephen White | a800ec9 | 2019-08-02 15:04:52 -0400 | [diff] [blame] | 98 | if (!fSwapChain) { |
| 99 | fContext.reset(); |
| 100 | return; |
| 101 | } |
Stephen White | 7b86d4e | 2019-10-18 08:53:17 -0400 | [diff] [blame] | 102 | fSwapChain.Configure(fSwapChainFormat, kUsage, fWidth, fHeight); |
Stephen White | a800ec9 | 2019-08-02 15:04:52 -0400 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | void DawnWindowContext::setDisplayParams(const DisplayParams& params) { |
| 106 | fDisplayParams = params; |
| 107 | } |
| 108 | |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 109 | wgpu::Device DawnWindowContext::createDevice(dawn_native::BackendType type) { |
Stephen White | a800ec9 | 2019-08-02 15:04:52 -0400 | [diff] [blame] | 110 | fInstance->DiscoverDefaultAdapters(); |
| 111 | DawnProcTable backendProcs = dawn_native::GetProcs(); |
Stephen White | 1e2adcf | 2019-10-16 09:48:53 -0400 | [diff] [blame] | 112 | dawnProcSetProcs(&backendProcs); |
Stephen White | a800ec9 | 2019-08-02 15:04:52 -0400 | [diff] [blame] | 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 |