2019-08-02 19:04:52 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2019 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "include/core/SkSurface.h"
|
|
|
|
#include "include/gpu/GrBackendSurface.h"
|
2020-07-10 17:55:21 +00:00
|
|
|
#include "include/gpu/GrDirectContext.h"
|
2019-08-02 19:04:52 +00:00
|
|
|
#include "src/core/SkAutoMalloc.h"
|
|
|
|
#include "tools/sk_app/DawnWindowContext.h"
|
|
|
|
|
2019-10-16 13:48:53 +00:00
|
|
|
#include "dawn/dawn_proc.h"
|
|
|
|
|
2021-05-17 12:13:36 +00:00
|
|
|
static wgpu::TextureUsage kUsage = wgpu::TextureUsage::RenderAttachment |
|
2019-10-30 13:56:23 +00:00
|
|
|
wgpu::TextureUsage::CopySrc;
|
2019-10-18 12:53:17 +00:00
|
|
|
|
2019-12-04 14:57:48 +00:00
|
|
|
static void PrintDeviceError(WGPUErrorType, const char* message, void*) {
|
2019-08-02 19:04:52 +00:00
|
|
|
printf("Device error: %s\n", message);
|
|
|
|
SkASSERT(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace sk_app {
|
|
|
|
|
|
|
|
DawnWindowContext::DawnWindowContext(const DisplayParams& params,
|
2019-10-30 13:56:23 +00:00
|
|
|
wgpu::TextureFormat swapChainFormat)
|
2019-08-02 19:04:52 +00:00
|
|
|
: WindowContext(params)
|
|
|
|
, fSwapChainFormat(swapChainFormat)
|
|
|
|
, fInstance(std::make_unique<dawn_native::Instance>()) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void DawnWindowContext::initializeContext(int width, int height) {
|
2020-07-10 17:55:21 +00:00
|
|
|
SkASSERT(!fContext);
|
|
|
|
|
2019-08-02 19:04:52 +00:00
|
|
|
fWidth = width;
|
|
|
|
fHeight = height;
|
|
|
|
fDevice = onInitializeContext();
|
|
|
|
|
2020-07-13 20:13:31 +00:00
|
|
|
fContext = GrDirectContext::MakeDawn(fDevice, fDisplayParams.fGrContextOptions);
|
2019-08-02 19:04:52 +00:00
|
|
|
if (!fContext) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
fSwapChainImplementation = this->createSwapChainImplementation(-1, -1, fDisplayParams);
|
2019-10-30 13:56:23 +00:00
|
|
|
wgpu::SwapChainDescriptor swapChainDesc;
|
2019-08-02 19:04:52 +00:00
|
|
|
swapChainDesc.implementation = reinterpret_cast<int64_t>(&fSwapChainImplementation);
|
2020-03-30 17:23:00 +00:00
|
|
|
fSwapChain = fDevice.CreateSwapChain(nullptr, &swapChainDesc);
|
2019-08-02 19:04:52 +00:00
|
|
|
if (!fSwapChain) {
|
|
|
|
fContext.reset();
|
|
|
|
return;
|
|
|
|
}
|
2019-10-18 12:53:17 +00:00
|
|
|
fSwapChain.Configure(fSwapChainFormat, kUsage, width, height);
|
2019-10-15 17:35:37 +00:00
|
|
|
fDevice.SetUncapturedErrorCallback(PrintDeviceError, 0);
|
2019-08-02 19:04:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DawnWindowContext::~DawnWindowContext() {
|
|
|
|
}
|
|
|
|
|
|
|
|
void DawnWindowContext::destroyContext() {
|
|
|
|
if (!fDevice.Get()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this->onDestroyContext();
|
|
|
|
|
|
|
|
fContext.reset();
|
|
|
|
fDevice = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
sk_sp<SkSurface> DawnWindowContext::getBackbufferSurface() {
|
2020-01-28 17:39:45 +00:00
|
|
|
GrDawnRenderTargetInfo rtInfo;
|
|
|
|
rtInfo.fTextureView = fSwapChain.GetCurrentTextureView();
|
|
|
|
rtInfo.fFormat = fSwapChainFormat;
|
|
|
|
rtInfo.fLevelCount = 1; // FIXME
|
|
|
|
GrBackendRenderTarget backendRenderTarget(fWidth, fHeight, fDisplayParams.fMSAASampleCount, 8,
|
|
|
|
rtInfo);
|
|
|
|
fSurface = SkSurface::MakeFromBackendRenderTarget(fContext.get(),
|
|
|
|
backendRenderTarget,
|
|
|
|
this->getRTOrigin(),
|
|
|
|
fDisplayParams.fColorType,
|
|
|
|
fDisplayParams.fColorSpace,
|
|
|
|
&fDisplayParams.fSurfaceProps);
|
2019-08-02 19:04:52 +00:00
|
|
|
return fSurface;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DawnWindowContext::swapBuffers() {
|
2020-01-28 17:39:45 +00:00
|
|
|
fSwapChain.Present();
|
2019-08-02 19:04:52 +00:00
|
|
|
this->onSwapBuffers();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DawnWindowContext::resize(int w, int h) {
|
|
|
|
fWidth = w;
|
|
|
|
fHeight = h;
|
|
|
|
fSwapChainImplementation = this->createSwapChainImplementation(w, h, fDisplayParams);
|
2019-10-30 13:56:23 +00:00
|
|
|
wgpu::SwapChainDescriptor swapChainDesc;
|
2019-08-02 19:04:52 +00:00
|
|
|
swapChainDesc.implementation = reinterpret_cast<int64_t>(&fSwapChainImplementation);
|
2020-03-30 17:23:00 +00:00
|
|
|
fSwapChain = fDevice.CreateSwapChain(nullptr, &swapChainDesc);
|
2019-08-02 19:04:52 +00:00
|
|
|
if (!fSwapChain) {
|
|
|
|
fContext.reset();
|
|
|
|
return;
|
|
|
|
}
|
2019-10-18 12:53:17 +00:00
|
|
|
fSwapChain.Configure(fSwapChainFormat, kUsage, fWidth, fHeight);
|
2019-08-02 19:04:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DawnWindowContext::setDisplayParams(const DisplayParams& params) {
|
|
|
|
fDisplayParams = params;
|
|
|
|
}
|
|
|
|
|
2019-10-30 13:56:23 +00:00
|
|
|
wgpu::Device DawnWindowContext::createDevice(dawn_native::BackendType type) {
|
2019-08-02 19:04:52 +00:00
|
|
|
fInstance->DiscoverDefaultAdapters();
|
|
|
|
DawnProcTable backendProcs = dawn_native::GetProcs();
|
2019-10-16 13:48:53 +00:00
|
|
|
dawnProcSetProcs(&backendProcs);
|
2019-08-02 19:04:52 +00:00
|
|
|
|
|
|
|
std::vector<dawn_native::Adapter> adapters = fInstance->GetAdapters();
|
|
|
|
for (dawn_native::Adapter adapter : adapters) {
|
|
|
|
if (adapter.GetBackendType() == type) {
|
|
|
|
return adapter.CreateDevice();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
} //namespace sk_app
|