1 /*
2 * Copyright 2020 Google LLC
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 "tools/gpu/BackendSurfaceFactory.h"
9
10 #include "include/core/SkSurface.h"
11 #include "include/gpu/GrDirectContext.h"
12 #include "include/gpu/ganesh/SkSurfaceGanesh.h"
13 #include "src/gpu/ganesh/GrDirectContextPriv.h"
14 #include "src/gpu/ganesh/GrGpu.h"
15 #include "tools/gpu/ManagedBackendTexture.h"
16
17 #ifdef SK_GRAPHITE
18 #include "include/gpu/graphite/Surface.h"
19 #ifdef SK_DAWN
20 #include "webgpu/webgpu_cpp.h" // NO_G3_REWRITE
21 #endif
22 #endif
23
24 namespace sk_gpu_test {
25
MakeBackendTextureSurface(GrDirectContext * dContext,const SkImageInfo & ii,GrSurfaceOrigin origin,int sampleCnt,skgpu::Mipmapped mipmapped,GrProtected isProtected,const SkSurfaceProps * props)26 sk_sp<SkSurface> MakeBackendTextureSurface(GrDirectContext* dContext,
27 const SkImageInfo& ii,
28 GrSurfaceOrigin origin,
29 int sampleCnt,
30 skgpu::Mipmapped mipmapped,
31 GrProtected isProtected,
32 const SkSurfaceProps* props) {
33 if (ii.alphaType() == kUnpremul_SkAlphaType) {
34 return nullptr;
35 }
36 auto mbet = ManagedBackendTexture::MakeWithoutData(dContext,
37 ii.width(),
38 ii.height(),
39 ii.colorType(),
40 mipmapped,
41 GrRenderable::kYes,
42 isProtected);
43 if (!mbet) {
44 return nullptr;
45 }
46 return SkSurfaces::WrapBackendTexture(dContext,
47 mbet->texture(),
48 origin,
49 sampleCnt,
50 ii.colorType(),
51 ii.refColorSpace(),
52 props,
53 ManagedBackendTexture::ReleaseProc,
54 mbet->releaseContext());
55 }
56
MakeBackendTextureSurface(GrDirectContext * dContext,SkISize dimensions,GrSurfaceOrigin origin,int sampleCnt,SkColorType colorType,sk_sp<SkColorSpace> colorSpace,skgpu::Mipmapped mipmapped,GrProtected isProtected,const SkSurfaceProps * props)57 sk_sp<SkSurface> MakeBackendTextureSurface(GrDirectContext* dContext,
58 SkISize dimensions,
59 GrSurfaceOrigin origin,
60 int sampleCnt,
61 SkColorType colorType,
62 sk_sp<SkColorSpace> colorSpace,
63 skgpu::Mipmapped mipmapped,
64 GrProtected isProtected,
65 const SkSurfaceProps* props) {
66 auto ii = SkImageInfo::Make(dimensions, colorType, kPremul_SkAlphaType, std::move(colorSpace));
67 return MakeBackendTextureSurface(
68 dContext, ii, origin, sampleCnt, mipmapped, isProtected, props);
69 }
MakeBackendRenderTargetSurface(GrDirectContext * dContext,const SkImageInfo & ii,GrSurfaceOrigin origin,int sampleCnt,GrProtected isProtected,const SkSurfaceProps * props)70 sk_sp<SkSurface> MakeBackendRenderTargetSurface(GrDirectContext* dContext,
71 const SkImageInfo& ii,
72 GrSurfaceOrigin origin,
73 int sampleCnt,
74 GrProtected isProtected,
75 const SkSurfaceProps* props) {
76 if (ii.alphaType() == kUnpremul_SkAlphaType || ii.alphaType() == kUnknown_SkAlphaType) {
77 return nullptr;
78 }
79 auto ct = SkColorTypeToGrColorType(ii.colorType());
80
81 struct ReleaseContext {
82 sk_sp<GrDirectContext> fContext;
83 GrBackendRenderTarget fRenderTarget;
84 };
85
86 auto bert = dContext->priv().getGpu()->createTestingOnlyBackendRenderTarget(
87 ii.dimensions(), ct, sampleCnt, isProtected);
88 auto rc = new ReleaseContext{sk_ref_sp(dContext), bert};
89 SkASSERT(!bert.isValid() || bert.sampleCnt() >= sampleCnt);
90
91 auto proc = [](void* c) {
92 const auto* rc = static_cast<ReleaseContext*>(c);
93 if (auto gpu = rc->fContext->priv().getGpu(); gpu && rc->fRenderTarget.isValid()) {
94 gpu->deleteTestingOnlyBackendRenderTarget(rc->fRenderTarget);
95 }
96 delete rc;
97 };
98
99 return SkSurfaces::WrapBackendRenderTarget(
100 dContext, bert, origin, ii.colorType(), ii.refColorSpace(), props, proc, rc);
101 }
102
MakeBackendRenderTargetSurface(GrDirectContext * dContext,SkISize dimensions,GrSurfaceOrigin origin,int sampleCnt,SkColorType colorType,sk_sp<SkColorSpace> colorSpace,GrProtected isProtected,const SkSurfaceProps * props)103 sk_sp<SkSurface> MakeBackendRenderTargetSurface(GrDirectContext* dContext,
104 SkISize dimensions,
105 GrSurfaceOrigin origin,
106 int sampleCnt,
107 SkColorType colorType,
108 sk_sp<SkColorSpace> colorSpace,
109 GrProtected isProtected,
110 const SkSurfaceProps* props) {
111 auto ii = SkImageInfo::Make(dimensions, colorType, kPremul_SkAlphaType, std::move(colorSpace));
112 return MakeBackendRenderTargetSurface(dContext, ii, origin, sampleCnt, isProtected, props);
113 }
114
115 #ifdef SK_GRAPHITE
MakeBackendTextureSurface(skgpu::graphite::Recorder * recorder,const SkImageInfo & ii,skgpu::Mipmapped mipmapped,skgpu::Protected isProtected,const SkSurfaceProps * props)116 sk_sp<SkSurface> MakeBackendTextureSurface(skgpu::graphite::Recorder* recorder,
117 const SkImageInfo& ii,
118 skgpu::Mipmapped mipmapped,
119 skgpu::Protected isProtected,
120 const SkSurfaceProps* props) {
121 if (ii.alphaType() == kUnpremul_SkAlphaType) {
122 return nullptr;
123 }
124 sk_sp<ManagedGraphiteTexture> mbet = ManagedGraphiteTexture::MakeUnInit(recorder,
125 ii,
126 mipmapped,
127 skgpu::Renderable::kYes,
128 isProtected);
129 if (!mbet) {
130 return nullptr;
131 }
132 return SkSurfaces::WrapBackendTexture(recorder,
133 mbet->texture(),
134 ii.colorType(),
135 ii.refColorSpace(),
136 props,
137 ManagedGraphiteTexture::ReleaseProc,
138 mbet->releaseContext());
139 }
140
MakeBackendTextureViewSurface(skgpu::graphite::Recorder * recorder,const SkImageInfo & ii,skgpu::Mipmapped mipmapped,skgpu::Protected isProtected,const SkSurfaceProps * props)141 sk_sp<SkSurface> MakeBackendTextureViewSurface(skgpu::graphite::Recorder* recorder,
142 const SkImageInfo& ii,
143 skgpu::Mipmapped mipmapped,
144 skgpu::Protected isProtected,
145 const SkSurfaceProps* props) {
146 #ifdef SK_DAWN
147 if (recorder->backend() != skgpu::BackendApi::kDawn) {
148 return nullptr;
149 }
150
151 if (ii.alphaType() == kUnpremul_SkAlphaType) {
152 return nullptr;
153 }
154
155 auto mbet = ManagedGraphiteTexture::MakeUnInit(recorder,
156 ii,
157 mipmapped,
158 skgpu::Renderable::kYes,
159 isProtected);
160 if (!mbet) {
161 return nullptr;
162 }
163
164 wgpu::Texture texture(mbet->texture().getDawnTexturePtr());
165 SkASSERT(texture);
166
167 wgpu::TextureView view = texture.CreateView();
168 SkASSERT(view);
169
170 skgpu::graphite::DawnTextureInfo textureInfo;
171 textureInfo.fAspect = wgpu::TextureAspect::All;
172 textureInfo.fFormat = texture.GetFormat();
173 textureInfo.fMipmapped = mipmapped;
174 textureInfo.fSampleCount = texture.GetSampleCount();
175 textureInfo.fUsage = texture.GetUsage();
176
177 skgpu::graphite::BackendTexture betFromView(ii.dimensions(), textureInfo, view.Get());
178
179 auto release = [](void* ctx) { static_cast<ManagedGraphiteTexture*>(ctx)->unref(); };
180
181 return SkSurfaces::WrapBackendTexture(recorder,
182 betFromView,
183 ii.colorType(),
184 ii.refColorSpace(),
185 props,
186 release,
187 mbet.release());
188 #endif
189 return nullptr;
190 }
191
192 #endif // SK_GRAPHITE
193
194 } // namespace sk_gpu_test
195