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