Flutter Engine
The Flutter Engine
ManagedBackendTexture.h
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
8#ifndef ManagedBackendTexture_DEFINED
9#define ManagedBackendTexture_DEFINED
10
13#ifdef SK_GANESH
16#endif
17#ifdef SK_GRAPHITE
21#endif
22
23namespace skgpu {
24class RefCntedCallback;
25}
26namespace skgpu::graphite {
27class Recorder;
28}
29class SkBitmap;
30struct SkImageInfo;
31
32#ifdef SK_GRAPHITE
34#endif
35
36namespace sk_gpu_test {
37
38#ifdef SK_GANESH
39class ManagedBackendTexture : public SkNVRefCnt<ManagedBackendTexture> {
40public:
41 /**
42 * Make a managed backend texture with initial pixmap/color data. The 'Args' are any valid set
43 * of arguments to GrDirectContext::createBackendTexture that takes data but with the release
44 * proc/context omitted as the ManagedBackendTexture will provide them.
45 */
46 template <typename... Args>
48
49 /**
50 * Make a managed backend texture without initial data. The 'Args' are any valid set of
51 * arguments to GrDirectContext::createBackendTexture that does not take data. Because our
52 * createBackendTexture methods that *do* take data also use default args for the proc/context
53 * this can be used to make a texture with data but then the MBET won't be able to ensure that
54 * the upload has completed before the texture is deleted. Use the WithData variant instead to
55 * avoid this issue.
56 */
57 template <typename... Args>
58 static sk_sp<ManagedBackendTexture> MakeWithoutData(GrDirectContext*, Args&&...);
59
60
61 static sk_sp<ManagedBackendTexture> MakeFromInfo(GrDirectContext* dContext,
62 const SkImageInfo&,
66
68 const SkBitmap&,
72
73 static sk_sp<ManagedBackendTexture> MakeFromPixmap(GrDirectContext*,
74 const SkPixmap&,
78
79 /** GrGpuFinishedProc or image/surface release proc. */
80 static void ReleaseProc(void* context);
81
82 ~ManagedBackendTexture();
83
84 /**
85 * The context to use with ReleaseProc. This adds a ref so it *must* be balanced by a call to
86 * ReleaseProc. If a wrappedProc is provided then it will be called by ReleaseProc.
87 */
88 void* releaseContext(GrGpuFinishedProc wrappedProc = nullptr,
89 GrGpuFinishedContext wrappedContext = nullptr) const;
90
91 sk_sp<skgpu::RefCntedCallback> refCountedCallback() const;
92
93 /**
94 * Call if the underlying GrBackendTexture was adopted by a GrContext. This clears this out the
95 * MBET without deleting the texture.
96 */
97 void wasAdopted();
98
99 /**
100 * SkImages::TextureFromYUVATextures takes a single release proc that is called once for all the
101 * textures. This makes a single release context for the group of textures. It's used with the
102 * standard ReleaseProc. Like releaseContext(), it must be balanced by a ReleaseProc call for
103 * proper ref counting.
104 */
105 static void* MakeYUVAReleaseContext(const sk_sp<ManagedBackendTexture>[SkYUVAInfo::kMaxPlanes]);
106
107 const GrBackendTexture& texture() { return fTexture; }
108
109private:
110 ManagedBackendTexture() = default;
111 ManagedBackendTexture(const ManagedBackendTexture&) = delete;
112 ManagedBackendTexture(ManagedBackendTexture&&) = delete;
113
115 GrBackendTexture fTexture;
116};
117
118template <typename... Args>
120 Args&&... args) {
121 sk_sp<ManagedBackendTexture> mbet(new ManagedBackendTexture);
122 mbet->fDContext = sk_ref_sp(dContext);
123 mbet->fTexture = dContext->createBackendTexture(std::forward<Args>(args)...,
124 ReleaseProc,
125 mbet->releaseContext());
126 if (!mbet->fTexture.isValid()) {
127 return nullptr;
128 }
129 return mbet;
130}
131
132template <typename... Args>
133inline sk_sp<ManagedBackendTexture> ManagedBackendTexture::MakeWithoutData(
134 GrDirectContext* dContext,
135 Args&&... args) {
137 dContext->createBackendTexture(std::forward<Args>(args)...);
138 if (!texture.isValid()) {
139 return nullptr;
140 }
141 sk_sp<ManagedBackendTexture> mbet(new ManagedBackendTexture);
142 mbet->fDContext = sk_ref_sp(dContext);
143 mbet->fTexture = std::move(texture);
144 return mbet;
145}
146#endif // SK_GANESH
147
148#ifdef SK_GRAPHITE
149/*
150 * Graphite version of ManagedBackendTexture
151 */
152class ManagedGraphiteTexture : public SkNVRefCnt<ManagedGraphiteTexture> {
153public:
154 static sk_sp<ManagedGraphiteTexture> MakeUnInit(Recorder*,
155 const SkImageInfo&,
159
160 static sk_sp<ManagedGraphiteTexture> MakeFromPixmap(Recorder*,
161 const SkPixmap&,
165
166 static sk_sp<ManagedGraphiteTexture> MakeMipmappedFromPixmaps(
167 Recorder*,
171
172 static sk_sp<ManagedGraphiteTexture> MakeFromCompressedData(
173 Recorder*,
174 SkISize dimmensions,
179
180 /** finished and image/surface release procs */
181 static void FinishedProc(void* context, skgpu::CallbackResult);
182 static void ReleaseProc(void* context);
183 static void ImageReleaseProc(void* context);
184
185 ~ManagedGraphiteTexture();
186
187 /**
188 * The context to use with the ReleaseProcs. This adds a ref so it *must* be balanced by a call
189 * to TextureReleaseProc or ImageReleaseProc.
190 */
191 void* releaseContext() const;
192
193 sk_sp<skgpu::RefCntedCallback> refCountedCallback() const;
194
195 /**
196 * SkImage::MakeGraphiteFromYUVABackendTextures takes a single release proc that is called once
197 * for all the textures. This makes a single release context for the group of textures. It's
198 * used with the standard ReleaseProc. Like releaseContext(), it must be balanced by a
199 * ReleaseProc call for proper ref counting.
200 */
201 static void* MakeYUVAReleaseContext(const sk_sp<ManagedGraphiteTexture>[SkYUVAInfo::kMaxPlanes]);
202
203 const skgpu::graphite::BackendTexture& texture() { return fTexture; }
204
205private:
206 ManagedGraphiteTexture() = default;
207 ManagedGraphiteTexture(const ManagedGraphiteTexture&) = delete;
208 ManagedGraphiteTexture(ManagedGraphiteTexture&&) = delete;
209
212};
213
214#endif // SK_GRAPHITE
215
216} // namespace sk_gpu_test
217
218#endif // ManagedBackendTexture_DEFINED
void * GrGpuFinishedContext
Definition: GrTypes.h:178
void(* GrGpuFinishedProc)(GrGpuFinishedContext finishedContext)
Definition: GrTypes.h:179
sk_sp< T > sk_ref_sp(T *obj)
Definition: SkRefCnt.h:381
const Context & fContext
SkTextureCompressionType
GrDirectContext * fDContext
GrBackendTexture createBackendTexture(int width, int height, const GrBackendFormat &, skgpu::Mipmapped, GrRenderable, GrProtected=GrProtected::kNo, std::string_view label={})
static constexpr int kMaxPlanes
Definition: SkYUVAInfo.h:98
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
FlTexture * texture
SK_API sk_sp< SkPixelRef > MakeWithData(const SkImageInfo &, size_t rowBytes, sk_sp< SkData > data)
sk_sp< SkImage > MakeFromBitmap(Recorder *recorder, const SkColorInfo &colorInfo, const SkBitmap &bitmap, sk_sp< SkMipmap > mipmaps, Budgeted budgeted, SkImage::RequiredProperties requiredProps, std::string_view label)
Definition: GpuTools.h:21
Renderable
Definition: GpuTypes.h:69
CallbackResult
Definition: GpuTypes.h:45
Mipmapped
Definition: GpuTypes.h:53
Protected
Definition: GpuTypes.h:61
Definition: SkSize.h:16
skgpu::graphite::Recorder Recorder