Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
GrBackendTextureImageGenerator.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2017 Google Inc.
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
23#include "src/gpu/ganesh/SkGr.h"
24
25GrBackendTextureImageGenerator::RefHelper::RefHelper(
28 std::unique_ptr<GrSemaphore> semaphore)
29 : fOriginalTexture(std::move(texture))
30 , fOwningContextID(owningContextID)
31 , fBorrowingContextReleaseProc(nullptr)
32 , fSemaphore(std::move(semaphore)) {}
33
34GrBackendTextureImageGenerator::RefHelper::~RefHelper() {
35 SkASSERT(!fBorrowingContextID.isValid());
36 // Generator has been freed, and no one is borrowing the texture. Notify the original cache
37 // that it can free the last ref, so it happens on the correct thread.
38 GrResourceCache::ReturnResourceFromThread(std::move(fOriginalTexture), fOwningContextID);
39}
40
41std::unique_ptr<GrTextureGenerator>
43 GrSurfaceOrigin origin,
44 std::unique_ptr<GrSemaphore> semaphore,
46 SkAlphaType alphaType,
47 sk_sp<SkColorSpace> colorSpace) {
48 GrDirectContext* dContext = texture->getContext();
49
50 if (!dContext->priv().caps()->areColorTypeAndFormatCompatible(
51 SkColorTypeToGrColorType(colorType), texture->backendFormat())) {
52 return nullptr;
53 }
54
55 SkColorInfo info(colorType, alphaType, std::move(colorSpace));
56 return std::unique_ptr<GrTextureGenerator>(new GrBackendTextureImageGenerator(
57 info,
58 texture,
59 origin,
60 dContext->directContextID(),
61 std::move(semaphore)));
62}
63
64GrBackendTextureImageGenerator::GrBackendTextureImageGenerator(
65 const SkColorInfo& info,
67 GrSurfaceOrigin origin,
69 std::unique_ptr<GrSemaphore> semaphore)
70 : INHERITED(SkImageInfo::Make(texture->dimensions(), info))
71 , fRefHelper(new RefHelper(texture, owningContextID, std::move(semaphore)))
72 , fBackendTexture(texture->getBackendTexture())
73 , fSurfaceOrigin(origin) {}
74
78
80 return fBackendTexture.isProtected();
81}
82
83///////////////////////////////////////////////////////////////////////////////////////////////////
84
85void GrBackendTextureImageGenerator::ReleaseRefHelper_TextureReleaseProc(void* ctx) {
86 RefHelper* refHelper = static_cast<RefHelper*>(ctx);
87 SkASSERT(refHelper);
88
89 refHelper->fBorrowingContextReleaseProc = nullptr;
90 refHelper->fBorrowingContextID.makeInvalid();
91 refHelper->unref();
92}
93
95 GrRecordingContext* rContext,
96 const SkImageInfo& info,
97 skgpu::Mipmapped mipmapped,
98 GrImageTexGenPolicy texGenPolicy) {
99 SkASSERT(rContext);
100 SkASSERT_RELEASE(info.dimensions() == fBackendTexture.dimensions());
101
102 // We currently limit GrBackendTextureImageGenerators to direct contexts since
103 // only Flutter uses them and doesn't use recording/DDL contexts. Ideally, the
104 // cross context texture functionality can be subsumed by the thread-safe cache
105 // working with utility contexts.
106 auto dContext = rContext->asDirectContext();
107 if (!dContext) {
108 return {};
109 }
110
111 if (dContext->backend() != fBackendTexture.backend()) {
112 return {};
113 }
114 if (info.colorType() != this->getInfo().colorType()) {
115 return {};
116 }
117
118 auto proxyProvider = dContext->priv().proxyProvider();
119
120 fBorrowingMutex.acquire();
121 sk_sp<skgpu::RefCntedCallback> releaseProcHelper;
122 if (fRefHelper->fBorrowingContextID.isValid()) {
123 if (fRefHelper->fBorrowingContextID != dContext->directContextID()) {
124 fBorrowingMutex.release();
125 rContext->priv().printWarningMessage(
126 "GrBackendTextureImageGenerator: Trying to use texture on two GrContexts!\n");
127 return {};
128 } else {
129 SkASSERT(fRefHelper->fBorrowingContextReleaseProc);
130 // Ref the release proc to be held by the proxy we make below
131 releaseProcHelper = sk_ref_sp(fRefHelper->fBorrowingContextReleaseProc);
132 }
133 } else {
134 SkASSERT(!fRefHelper->fBorrowingContextReleaseProc);
135 // The ref we add to fRefHelper here will be passed into and owned by the
136 // skgpu::RefCntedCallback.
137 fRefHelper->ref();
138 releaseProcHelper =
139 skgpu::RefCntedCallback::Make(ReleaseRefHelper_TextureReleaseProc, fRefHelper);
140 fRefHelper->fBorrowingContextReleaseProc = releaseProcHelper.get();
141 }
142 fRefHelper->fBorrowingContextID = dContext->directContextID();
143 if (!fRefHelper->fBorrowedTextureKey.isValid()) {
144 static const auto kDomain = skgpu::UniqueKey::GenerateDomain();
145 skgpu::UniqueKey::Builder builder(&fRefHelper->fBorrowedTextureKey, kDomain, 1);
146 builder[0] = this->uniqueID();
147 }
148 fBorrowingMutex.release();
149
150 SkASSERT(fRefHelper->fBorrowingContextID == dContext->directContextID());
151
152 GrBackendFormat backendFormat = fBackendTexture.getBackendFormat();
153 SkASSERT(backendFormat.isValid());
154
155 GrColorType grColorType = SkColorTypeToGrColorType(info.colorType());
156
157 skgpu::Mipmapped textureIsMipMapped =
158 fBackendTexture.hasMipmaps() ? skgpu::Mipmapped::kYes : skgpu::Mipmapped::kNo;
159
160 // Ganesh assumes that, when wrapping a mipmapped backend texture from a client, that its
161 // mipmaps are fully fleshed out.
162 GrMipmapStatus mipmapStatus = fBackendTexture.hasMipmaps()
164
165 skgpu::Swizzle readSwizzle = dContext->priv().caps()->getReadSwizzle(backendFormat,
166 grColorType);
167
168 // Must make copies of member variables to capture in the lambda since this image generator may
169 // be deleted before we actually execute the lambda.
170 sk_sp<GrTextureProxy> proxy = proxyProvider->createLazyProxy(
171 [refHelper = fRefHelper, releaseProcHelper, backendTexture = fBackendTexture](
172 GrResourceProvider* resourceProvider,
174 if (refHelper->fSemaphore) {
175 resourceProvider->priv().gpu()->waitSemaphore(refHelper->fSemaphore.get());
176 }
177
178 // If a client re-draws the same image multiple times, the texture we return
179 // will be cached and re-used. If they draw a subset, though, we may be
180 // re-called. In that case, we want to re-use the borrowed texture we've
181 // previously created.
183 SkASSERT(refHelper->fBorrowedTextureKey.isValid());
184 auto surf = resourceProvider->findByUniqueKey<GrSurface>(
185 refHelper->fBorrowedTextureKey);
186 if (surf) {
187 SkASSERT(surf->asTexture());
188 tex = sk_ref_sp(surf->asTexture());
189 } else {
190 // We just gained access to the texture. If we're on the original
191 // context, we could use the original texture, but we'd have no way of
192 // detecting that it's no longer in-use. So we always make a wrapped
193 // copy, where the release proc informs us that the context is done with
194 // it. This is unfortunate - we'll have two texture objects referencing
195 // the same GPU object. However, no client can ever see the original
196 // texture, so this should be safe. We make the texture uncacheable so
197 // that the release proc is called ASAP.
198 tex = resourceProvider->wrapBackendTexture(
201 if (!tex) {
202 return {};
203 }
204 tex->setRelease(releaseProcHelper);
205 tex->resourcePriv().setUniqueKey(refHelper->fBorrowedTextureKey);
206 }
207 // We use keys to avoid re-wrapping the GrBackendTexture in a GrTexture.
208 // This is unrelated to the whatever SkImage key may be assigned to the
209 // proxy.
210 return {std::move(tex), true, GrSurfaceProxy::LazyInstantiationKeyMode::kUnsynced};
211 },
212 backendFormat,
213 fBackendTexture.dimensions(),
214 textureIsMipMapped,
215 mipmapStatus,
219 GrProtected::kNo,
221 "BackendTextureImageGenerator");
222 if (!proxy) {
223 return {};
224 }
225
226 if (texGenPolicy == GrImageTexGenPolicy::kDraw &&
227 (mipmapped == skgpu::Mipmapped::kNo || proxy->mipmapped() == skgpu::Mipmapped::kYes)) {
228 // If we have the correct mip support, we're done
229 return GrSurfaceProxyView(std::move(proxy), fSurfaceOrigin, readSwizzle);
230 } else {
234
235 auto copy = GrSurfaceProxy::Copy(dContext,
236 std::move(proxy),
237 fSurfaceOrigin,
238 mipmapped,
239 SkIRect::MakeWH(info.width(), info.height()),
241 budgeted,
242 /*label=*/"BackendTextureImageGenerator_GenerateTexture");
243 return {std::move(copy), fSurfaceOrigin, readSwizzle};
244 }
245}
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition DM.cpp:213
@ kRead_GrIOType
GrMipmapStatus
@ kBorrow_GrWrapOwnership
Definition GrTypesPriv.h:78
GrColorType
static constexpr GrColorType SkColorTypeToGrColorType(SkColorType ct)
GrSurfaceOrigin
Definition GrTypes.h:147
SkAlphaType
Definition SkAlphaType.h:26
#define SkASSERT_RELEASE(cond)
Definition SkAssert.h:100
#define SkASSERT(cond)
Definition SkAssert.h:116
SkColorType
Definition SkColorType.h:19
GrImageTexGenPolicy
Definition SkGr.h:141
static SkColorType colorType(AImageDecoder *decoder, const AImageDecoderHeaderInfo *headerInfo)
static std::unique_ptr< SkEncoder > Make(SkWStream *dst, const SkPixmap *src, const SkYUVAPixmaps *srcYUVA, const SkColorSpace *srcYUVAColorSpace, const SkJpegEncoder::Options &options)
#define INHERITED(method,...)
sk_sp< T > sk_ref_sp(T *obj)
Definition SkRefCnt.h:381
bool isValid() const
GrSurfaceProxyView onGenerateTexture(GrRecordingContext *, const SkImageInfo &, skgpu::Mipmapped mipmapped, GrImageTexGenPolicy) override
static std::unique_ptr< GrTextureGenerator > Make(const sk_sp< GrTexture > &, GrSurfaceOrigin, std::unique_ptr< GrSemaphore >, SkColorType, SkAlphaType, sk_sp< SkColorSpace >)
SkISize dimensions() const
GrBackendFormat getBackendFormat() const
bool hasMipmaps() const
GrBackendApi backend() const
const GrCaps * caps() const
bool areColorTypeAndFormatCompatible(GrColorType grCT, const GrBackendFormat &format) const
Definition GrCaps.cpp:428
virtual GrDirectContext * asDirectContext()
DirectContextID directContextID() const
GrDirectContextPriv priv()
GrProxyProvider * proxyProvider()
void printWarningMessage(const char *msg) const
GrRecordingContextPriv priv()
static std::enable_if_t< std::is_base_of_v< GrGpuResource, T >, void > ReturnResourceFromThread(sk_sp< T > &&resource, GrDirectContext::DirectContextID id)
std::enable_if< std::is_base_of< GrGpuResource, T >::value, sk_sp< T > >::type findByUniqueKey(const skgpu::UniqueKey &key)
sk_sp< GrTexture > wrapBackendTexture(const GrBackendTexture &tex, GrWrapOwnership, GrWrapCacheable, GrIOType)
static sk_sp< GrSurfaceProxy > Copy(GrRecordingContext *, sk_sp< GrSurfaceProxy > src, GrSurfaceOrigin, skgpu::Mipmapped, SkIRect srcRect, SkBackingFit, skgpu::Budgeted, std::string_view label, RectsMustMatch=RectsMustMatch::kNo, sk_sp< GrRenderTask > *outTask=nullptr)
void setRelease(sk_sp< skgpu::RefCntedCallback > releaseHelper)
Definition GrSurface.cpp:60
virtual GrSurfaceOrigin origin() const
uint32_t uniqueID() const
T * get() const
Definition SkRefCnt.h:303
static sk_sp< RefCntedCallback > Make(Callback proc, Context ctx)
static Domain GenerateDomain()
FlTexture * texture
Definition copy.py:1
Budgeted
Definition GpuTypes.h:35
Mipmapped
Definition GpuTypes.h:53
Definition ref_ptr.h:256
static constexpr SkIRect MakeWH(int32_t w, int32_t h)
Definition SkRect.h:56