/src/skia/tools/gpu/BackendSurfaceFactory.cpp
Line | Count | Source (jump to first uncovered line) |
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 | | |
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 | 0 | const SkSurfaceProps* props) { |
33 | 0 | if (ii.alphaType() == kUnpremul_SkAlphaType) { |
34 | 0 | return nullptr; |
35 | 0 | } |
36 | 0 | auto mbet = ManagedBackendTexture::MakeWithoutData(dContext, |
37 | 0 | ii.width(), |
38 | 0 | ii.height(), |
39 | 0 | ii.colorType(), |
40 | 0 | mipmapped, |
41 | 0 | GrRenderable::kYes, |
42 | 0 | isProtected); |
43 | 0 | if (!mbet) { |
44 | 0 | return nullptr; |
45 | 0 | } |
46 | 0 | return SkSurfaces::WrapBackendTexture(dContext, |
47 | 0 | mbet->texture(), |
48 | 0 | origin, |
49 | 0 | sampleCnt, |
50 | 0 | ii.colorType(), |
51 | 0 | ii.refColorSpace(), |
52 | 0 | props, |
53 | 0 | ManagedBackendTexture::ReleaseProc, |
54 | 0 | mbet->releaseContext()); |
55 | 0 | } |
56 | | |
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 | 0 | const SkSurfaceProps* props) { |
66 | 0 | auto ii = SkImageInfo::Make(dimensions, colorType, kPremul_SkAlphaType, std::move(colorSpace)); |
67 | 0 | return MakeBackendTextureSurface( |
68 | 0 | dContext, ii, origin, sampleCnt, mipmapped, isProtected, props); |
69 | 0 | } |
70 | | sk_sp<SkSurface> MakeBackendRenderTargetSurface(GrDirectContext* dContext, |
71 | | const SkImageInfo& ii, |
72 | | GrSurfaceOrigin origin, |
73 | | int sampleCnt, |
74 | | GrProtected isProtected, |
75 | 0 | const SkSurfaceProps* props) { |
76 | 0 | if (ii.alphaType() == kUnpremul_SkAlphaType || ii.alphaType() == kUnknown_SkAlphaType) { |
77 | 0 | return nullptr; |
78 | 0 | } |
79 | 0 | auto ct = SkColorTypeToGrColorType(ii.colorType()); |
80 | |
|
81 | 0 | struct ReleaseContext { |
82 | 0 | sk_sp<GrDirectContext> fContext; |
83 | 0 | GrBackendRenderTarget fRenderTarget; |
84 | 0 | }; |
85 | |
|
86 | 0 | auto bert = dContext->priv().getGpu()->createTestingOnlyBackendRenderTarget( |
87 | 0 | ii.dimensions(), ct, sampleCnt, isProtected); |
88 | 0 | auto rc = new ReleaseContext{sk_ref_sp(dContext), bert}; |
89 | 0 | SkASSERT(!bert.isValid() || bert.sampleCnt() >= sampleCnt); |
90 | |
|
91 | 0 | auto proc = [](void* c) { |
92 | 0 | const auto* rc = static_cast<ReleaseContext*>(c); |
93 | 0 | if (auto gpu = rc->fContext->priv().getGpu(); gpu && rc->fRenderTarget.isValid()) { |
94 | 0 | gpu->deleteTestingOnlyBackendRenderTarget(rc->fRenderTarget); |
95 | 0 | } |
96 | 0 | delete rc; |
97 | 0 | }; Unexecuted instantiation: BackendSurfaceFactory.cpp:sk_gpu_test::MakeBackendRenderTargetSurface(GrDirectContext*, SkImageInfo const&, GrSurfaceOrigin, int, skgpu::Protected, SkSurfaceProps const*)::$_0::operator()(void*) const Unexecuted instantiation: BackendSurfaceFactory.cpp:sk_gpu_test::MakeBackendRenderTargetSurface(GrDirectContext*, SkImageInfo const&, GrSurfaceOrigin, int, skgpu::Protected, SkSurfaceProps const*)::$_1::operator()(void*) const |
98 | |
|
99 | 0 | return SkSurfaces::WrapBackendRenderTarget( |
100 | 0 | dContext, bert, origin, ii.colorType(), ii.refColorSpace(), props, proc, rc); |
101 | 0 | } Unexecuted instantiation: sk_gpu_test::MakeBackendRenderTargetSurface(GrDirectContext*, SkImageInfo const&, GrSurfaceOrigin, int, skgpu::Protected, SkSurfaceProps const*) Unexecuted instantiation: sk_gpu_test::MakeBackendRenderTargetSurface(GrDirectContext*, SkImageInfo const&, GrSurfaceOrigin, int, skgpu::Protected, SkSurfaceProps const*) |
102 | | |
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 | 0 | const SkSurfaceProps* props) { |
111 | 0 | auto ii = SkImageInfo::Make(dimensions, colorType, kPremul_SkAlphaType, std::move(colorSpace)); |
112 | 0 | return MakeBackendRenderTargetSurface(dContext, ii, origin, sampleCnt, isProtected, props); |
113 | 0 | } |
114 | | |
115 | | #ifdef SK_GRAPHITE |
116 | | sk_sp<SkSurface> MakeBackendTextureSurface(skgpu::graphite::Recorder* recorder, |
117 | | const SkImageInfo& ii, |
118 | | skgpu::Mipmapped mipmapped, |
119 | | skgpu::Protected isProtected, |
120 | 0 | const SkSurfaceProps* props) { |
121 | 0 | if (ii.alphaType() == kUnpremul_SkAlphaType) { |
122 | 0 | return nullptr; |
123 | 0 | } |
124 | 0 | sk_sp<ManagedGraphiteTexture> mbet = ManagedGraphiteTexture::MakeUnInit(recorder, |
125 | 0 | ii, |
126 | 0 | mipmapped, |
127 | 0 | skgpu::Renderable::kYes, |
128 | 0 | isProtected); |
129 | 0 | if (!mbet) { |
130 | 0 | return nullptr; |
131 | 0 | } |
132 | 0 | return SkSurfaces::WrapBackendTexture(recorder, |
133 | 0 | mbet->texture(), |
134 | 0 | ii.colorType(), |
135 | 0 | ii.refColorSpace(), |
136 | 0 | props, |
137 | 0 | ManagedGraphiteTexture::ReleaseProc, |
138 | 0 | mbet->releaseContext()); |
139 | 0 | } |
140 | | |
141 | | sk_sp<SkSurface> MakeBackendTextureViewSurface(skgpu::graphite::Recorder* recorder, |
142 | | const SkImageInfo& ii, |
143 | | skgpu::Mipmapped mipmapped, |
144 | | skgpu::Protected isProtected, |
145 | 0 | 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 | 0 | return nullptr; |
190 | 0 | } |
191 | | |
192 | | #endif // SK_GRAPHITE |
193 | | |
194 | | } // namespace sk_gpu_test |