Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
BackendSurfaceFactory.cpp
Go to the documentation of this file.
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
9
16
17#ifdef SK_GRAPHITE
19#ifdef SK_DAWN
20#include "webgpu/webgpu_cpp.h" // NO_G3_REWRITE
21#endif
22#endif
23
24namespace sk_gpu_test {
25
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
58 SkISize dimensions,
59 GrSurfaceOrigin origin,
60 int sampleCnt,
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));
68 dContext, ii, origin, sampleCnt, mipmapped, isProtected, props);
69}
71 const SkImageInfo& ii,
72 GrSurfaceOrigin origin,
73 int sampleCnt,
74 GrProtected isProtected,
75 const SkSurfaceProps* props) {
77 return nullptr;
78 }
79 auto ct = SkColorTypeToGrColorType(ii.colorType());
80
81 struct ReleaseContext {
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
100 dContext, bert, origin, ii.colorType(), ii.refColorSpace(), props, proc, rc);
101}
102
104 SkISize dimensions,
105 GrSurfaceOrigin origin,
106 int sampleCnt,
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
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
141sk_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());
166
167 wgpu::TextureView view = texture.CreateView();
168 SkASSERT(view);
169
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
static constexpr GrColorType SkColorTypeToGrColorType(SkColorType ct)
GrSurfaceOrigin
Definition GrTypes.h:147
kUnpremul_SkAlphaType
@ kUnknown_SkAlphaType
uninitialized
Definition SkAlphaType.h:27
@ kPremul_SkAlphaType
pixel components are premultiplied by alpha
Definition SkAlphaType.h:29
#define SkASSERT(cond)
Definition SkAssert.h:116
SkColorType
Definition SkColorType.h:19
static SkColorType colorType(AImageDecoder *decoder, const AImageDecoderHeaderInfo *headerInfo)
sk_sp< T > sk_ref_sp(T *obj)
Definition SkRefCnt.h:381
const Context & fContext
GrDirectContextPriv priv()
T * release()
Definition SkRefCnt.h:324
BackendApi backend() const
Definition Recorder.cpp:147
FlTexture * texture
SK_API sk_sp< SkSurface > WrapBackendRenderTarget(GrRecordingContext *context, const GrBackendRenderTarget &backendRenderTarget, GrSurfaceOrigin origin, SkColorType colorType, sk_sp< SkColorSpace > colorSpace, const SkSurfaceProps *surfaceProps, RenderTargetReleaseProc releaseProc=nullptr, ReleaseContext releaseContext=nullptr)
SK_API sk_sp< SkSurface > WrapBackendTexture(GrRecordingContext *context, const GrBackendTexture &backendTexture, GrSurfaceOrigin origin, int sampleCnt, SkColorType colorType, sk_sp< SkColorSpace > colorSpace, const SkSurfaceProps *surfaceProps, TextureReleaseProc textureReleaseProc=nullptr, ReleaseContext releaseContext=nullptr)
sk_sp< SkSurface > MakeBackendRenderTargetSurface(GrDirectContext *dContext, const SkImageInfo &ii, GrSurfaceOrigin origin, int sampleCnt, GrProtected isProtected, const SkSurfaceProps *props)
sk_sp< SkSurface > MakeBackendTextureSurface(GrDirectContext *dContext, const SkImageInfo &ii, GrSurfaceOrigin origin, int sampleCnt, skgpu::Mipmapped mipmapped, GrProtected isProtected, const SkSurfaceProps *props)
Mipmapped
Definition GpuTypes.h:53
Protected
Definition GpuTypes.h:61
sk_sp< SkColorSpace > refColorSpace() const
SkISize dimensions() const
int width() const
static SkImageInfo Make(int width, int height, SkColorType ct, SkAlphaType at)
SkAlphaType alphaType() const
SkColorType colorType() const
int height() const
wgpu::TextureFormat fFormat
Definition DawnTypes.h:21
wgpu::TextureAspect fAspect
Definition DawnTypes.h:26
wgpu::TextureUsage fUsage
Definition DawnTypes.h:24