Debian Codegraph @

tools/gpu/BackendSurfaceFactory.cpp

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