• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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"
10 #include "include/gpu/GrContext.h"
11 #include "src/core/SkAutoMalloc.h"
12 #include "tools/sk_app/DawnWindowContext.h"
13 
PrintDeviceError(const char * message,void *)14 static void PrintDeviceError(const char* message, void*) {
15     printf("Device error: %s\n", message);
16     SkASSERT(false);
17 }
18 
19 namespace sk_app {
20 
DawnWindowContext(const DisplayParams & params,dawn::TextureFormat swapChainFormat)21 DawnWindowContext::DawnWindowContext(const DisplayParams& params,
22                                      dawn::TextureFormat swapChainFormat)
23     : WindowContext(params)
24     , fSwapChainFormat(swapChainFormat)
25     , fInstance(std::make_unique<dawn_native::Instance>()) {
26 }
27 
initializeContext(int width,int height)28 void DawnWindowContext::initializeContext(int width, int height) {
29     fWidth = width;
30     fHeight = height;
31     fDevice = onInitializeContext();
32     fContext = GrContext::MakeDawn(fDevice, fDisplayParams.fGrContextOptions);
33 
34     if (!fContext) {
35         return;
36     }
37     fSwapChainImplementation = this->createSwapChainImplementation(-1, -1, fDisplayParams);
38     dawn::SwapChainDescriptor swapChainDesc;
39     swapChainDesc.implementation = reinterpret_cast<int64_t>(&fSwapChainImplementation);
40     fSwapChain = fDevice.CreateSwapChain(&swapChainDesc);
41     if (!fSwapChain) {
42         fContext.reset();
43         return;
44     }
45     fSwapChain.Configure(fSwapChainFormat, dawn::TextureUsageBit::OutputAttachment, width, height);
46     fDevice.SetErrorCallback(PrintDeviceError, 0);
47 }
48 
~DawnWindowContext()49 DawnWindowContext::~DawnWindowContext() {
50 }
51 
destroyContext()52 void DawnWindowContext::destroyContext() {
53     if (!fDevice.Get()) {
54         return;
55     }
56 
57     this->onDestroyContext();
58 
59     fContext.reset();
60     fDevice = nullptr;
61 }
62 
getBackbufferSurface()63 sk_sp<SkSurface> DawnWindowContext::getBackbufferSurface() {
64     GrDawnImageInfo imageInfo;
65     imageInfo.fTexture = fSwapChain.GetNextTexture();
66     imageInfo.fFormat = fSwapChainFormat;
67     imageInfo.fLevelCount = 1; // FIXME
68     GrBackendTexture backendTexture(fWidth, fHeight, imageInfo);
69     fSurface = SkSurface::MakeFromBackendTextureAsRenderTarget(fContext.get(),
70                                                                backendTexture,
71                                                                this->getRTOrigin(),
72                                                                fDisplayParams.fMSAASampleCount,
73                                                                fDisplayParams.fColorType,
74                                                                fDisplayParams.fColorSpace,
75                                                                &fDisplayParams.fSurfaceProps);
76     return fSurface;
77 }
78 
swapBuffers()79 void DawnWindowContext::swapBuffers() {
80     GrBackendRenderTarget backendRT = fSurface->getBackendRenderTarget(
81         SkSurface::kFlushRead_BackendHandleAccess);
82     GrDawnImageInfo imageInfo;
83     SkAssertResult(backendRT.getDawnImageInfo(&imageInfo));
84 
85     fSwapChain.Present(imageInfo.fTexture);
86     this->onSwapBuffers();
87 }
88 
resize(int w,int h)89 void DawnWindowContext::resize(int w, int h) {
90     fWidth = w;
91     fHeight = h;
92     fSwapChainImplementation = this->createSwapChainImplementation(w, h, fDisplayParams);
93     dawn::SwapChainDescriptor swapChainDesc;
94     swapChainDesc.implementation = reinterpret_cast<int64_t>(&fSwapChainImplementation);
95     fSwapChain = fDevice.CreateSwapChain(&swapChainDesc);
96     if (!fSwapChain) {
97         fContext.reset();
98         return;
99     }
100     fSwapChain.Configure(fSwapChainFormat, dawn::TextureUsageBit::OutputAttachment, fWidth,
101                          fHeight);
102 }
103 
setDisplayParams(const DisplayParams & params)104 void DawnWindowContext::setDisplayParams(const DisplayParams& params) {
105     fDisplayParams = params;
106 }
107 
createDevice(dawn_native::BackendType type)108 dawn::Device DawnWindowContext::createDevice(dawn_native::BackendType type) {
109     fInstance->DiscoverDefaultAdapters();
110     DawnProcTable backendProcs = dawn_native::GetProcs();
111     dawnSetProcs(&backendProcs);
112 
113     std::vector<dawn_native::Adapter> adapters = fInstance->GetAdapters();
114     for (dawn_native::Adapter adapter : adapters) {
115         if (adapter.GetBackendType() == type) {
116             return adapter.CreateDevice();
117         }
118     }
119     return nullptr;
120 }
121 
122 }   //namespace sk_app
123