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