2 * Copyright 2018 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "src/image/SkImage_GpuBase.h"
10 #include "include/core/SkBitmap.h"
11 #include "include/core/SkPromiseImageTexture.h"
12 #include "include/gpu/GrBackendSurface.h"
13 #include "include/gpu/GrDirectContext.h"
14 #include "include/gpu/GrRecordingContext.h"
15 #include "include/gpu/GrYUVABackendTextures.h"
16 #include "src/core/SkBitmapCache.h"
17 #include "src/gpu/ganesh/GrDirectContextPriv.h"
18 #include "src/gpu/ganesh/GrImageContextPriv.h"
19 #include "src/gpu/ganesh/GrImageInfo.h"
20 #include "src/gpu/ganesh/GrProxyProvider.h"
21 #include "src/gpu/ganesh/GrRecordingContextPriv.h"
22 #include "src/gpu/ganesh/GrResourceProvider.h"
23 #include "src/gpu/ganesh/GrTexture.h"
24 #include "src/gpu/ganesh/GrYUVATextureProxies.h"
25 #include "src/gpu/ganesh/SurfaceContext.h"
26 #include "src/gpu/ganesh/effects/GrYUVtoRGBEffect.h"
27 #include "src/image/SkImage_Gpu.h"
28 #include "src/image/SkReadPixelsRec.h"
30 SkImage_GpuBase::SkImage_GpuBase(sk_sp<GrImageContext> context, SkImageInfo info, uint32_t uniqueID)
31 : INHERITED(std::move(info), uniqueID)
32 , fContext(std::move(context)) {}
34 //////////////////////////////////////////////////////////////////////////////////////////////////
36 bool SkImage_GpuBase::ValidateBackendTexture(const GrCaps* caps, const GrBackendTexture& tex,
37 GrColorType grCT, SkColorType ct, SkAlphaType at,
38 sk_sp<SkColorSpace> cs) {
42 SkColorInfo info(ct, at, cs);
43 if (!SkColorInfoIsValid(info)) {
46 GrBackendFormat backendFormat = tex.getBackendFormat();
47 if (!backendFormat.isValid()) {
51 return caps->areColorTypeAndFormatCompatible(grCT, backendFormat);
54 bool SkImage_GpuBase::ValidateCompressedBackendTexture(const GrCaps* caps,
55 const GrBackendTexture& tex,
57 if (!tex.isValid() || tex.width() <= 0 || tex.height() <= 0) {
61 if (tex.width() > caps->maxTextureSize() || tex.height() > caps->maxTextureSize()) {
65 if (at == kUnknown_SkAlphaType) {
69 GrBackendFormat backendFormat = tex.getBackendFormat();
70 if (!backendFormat.isValid()) {
74 if (!caps->isFormatCompressed(backendFormat)) {
81 //////////////////////////////////////////////////////////////////////////////////////////////////
83 bool SkImage_GpuBase::getROPixels(GrDirectContext* dContext,
85 CachingHint chint) const {
86 if (!fContext->priv().matches(dContext)) {
90 const auto desc = SkBitmapCacheDesc::Make(this);
91 if (SkBitmapCache::Find(desc, dst)) {
92 SkASSERT(dst->isImmutable());
93 SkASSERT(dst->getPixels());
97 SkBitmapCache::RecPtr rec = nullptr;
99 if (kAllow_CachingHint == chint) {
100 rec = SkBitmapCache::Alloc(desc, this->imageInfo(), &pmap);
105 if (!dst->tryAllocPixels(this->imageInfo()) || !dst->peekPixels(&pmap)) {
110 auto [view, ct] = this->asView(dContext, GrMipmapped::kNo);
115 GrColorInfo colorInfo(ct, this->alphaType(), this->refColorSpace());
116 auto sContext = dContext->priv().makeSC(std::move(view), std::move(colorInfo));
121 if (!sContext->readPixels(dContext, pmap, {0, 0})) {
126 SkBitmapCache::Add(std::move(rec), dst);
127 this->notifyAddedToRasterCache();
132 sk_sp<SkImage> SkImage_GpuBase::onMakeSubset(const SkIRect& subset,
133 GrDirectContext* direct) const {
134 if (!fContext->priv().matches(direct)) {
138 auto [view, ct] = this->asView(direct, GrMipmapped::kNo);
140 SkASSERT(ct == SkColorTypeToGrColorType(this->colorType()));
142 SkBudgeted isBudgeted = view.proxy()->isBudgeted();
143 auto copyView = GrSurfaceProxyView::Copy(direct,
147 SkBackingFit::kExact,
154 return sk_make_sp<SkImage_Gpu>(sk_ref_sp(direct),
155 kNeedNewImageUniqueID,
157 this->imageInfo().colorInfo());
160 bool SkImage_GpuBase::onReadPixels(GrDirectContext* dContext,
161 const SkImageInfo& dstInfo,
167 if (!fContext->priv().matches(dContext) ||
168 !SkImageInfoValidConversion(dstInfo, this->imageInfo())) {
172 auto [view, ct] = this->asView(dContext, GrMipmapped::kNo);
175 GrColorInfo colorInfo(ct, this->alphaType(), this->refColorSpace());
176 auto sContext = dContext->priv().makeSC(std::move(view), colorInfo);
181 return sContext->readPixels(dContext, {dstInfo, dstPixels, dstRB}, {srcX, srcY});
184 bool SkImage_GpuBase::onIsValid(GrRecordingContext* context) const {
185 // The base class has already checked that 'context' isn't abandoned (if it's not nullptr)
186 if (fContext->priv().abandoned()) {
190 if (context && !fContext->priv().matches(context)) {
197 sk_sp<GrTextureProxy> SkImage_GpuBase::MakePromiseImageLazyProxy(
198 GrContextThreadSafeProxy* tsp,
200 GrBackendFormat backendFormat,
201 GrMipmapped mipmapped,
202 PromiseImageTextureFulfillProc fulfillProc,
203 sk_sp<skgpu::RefCntedCallback> releaseHelper) {
205 SkASSERT(!dimensions.isEmpty());
206 SkASSERT(releaseHelper);
212 if (mipmapped == GrMipmapped::kYes &&
213 GrTextureTypeHasRestrictedSampling(backendFormat.textureType())) {
214 // It is invalid to have a GL_TEXTURE_EXTERNAL or GL_TEXTURE_RECTANGLE and have mips as
220 * This class is the lazy instantiation callback for promise images. It manages calling the
221 * client's Fulfill, Release, and Done procs. It attempts to reuse a GrTexture instance in
222 * cases where the client provides the same SkPromiseImageTexture as Fulfill results for
223 * multiple SkImages. The created GrTexture is given a key based on a unique ID associated with
224 * the SkPromiseImageTexture.
226 * A key invalidation message is installed on the SkPromiseImageTexture so that the GrTexture
227 * is deleted once it can no longer be used to instantiate a proxy.
229 class PromiseLazyInstantiateCallback {
231 PromiseLazyInstantiateCallback(PromiseImageTextureFulfillProc fulfillProc,
232 sk_sp<skgpu::RefCntedCallback> releaseHelper)
233 : fFulfillProc(fulfillProc), fReleaseHelper(std::move(releaseHelper)) {}
234 PromiseLazyInstantiateCallback(PromiseLazyInstantiateCallback&&) = default;
235 PromiseLazyInstantiateCallback(const PromiseLazyInstantiateCallback&) {
236 // Because we get wrapped in std::function we must be copyable. But we should never
240 PromiseLazyInstantiateCallback& operator=(PromiseLazyInstantiateCallback&&) = default;
241 PromiseLazyInstantiateCallback& operator=(const PromiseLazyInstantiateCallback&) {
246 ~PromiseLazyInstantiateCallback() {
247 // Our destructor can run on any thread. We trigger the unref of fTexture by message.
249 GrResourceCache::ReturnResourceFromThread(std::move(fTexture), fTextureContextID);
253 GrSurfaceProxy::LazyCallbackResult operator()(GrResourceProvider* resourceProvider,
254 const GrSurfaceProxy::LazySurfaceDesc&) {
255 // We use the unique key in a way that is unrelated to the SkImage-based key that the
256 // proxy may receive, hence kUnsynced.
257 static constexpr auto kKeySyncMode =
258 GrSurfaceProxy::LazyInstantiationKeyMode::kUnsynced;
260 // In order to make the SkImage "thread safe" we rely on holding an extra ref to the
261 // texture in the callback and signalling the unref via a message to the resource cache.
262 // We need to extend the callback's lifetime to that of the proxy.
263 static constexpr auto kReleaseCallbackOnInstantiation = false;
265 // Our proxy is getting instantiated for the second+ time. We are only allowed to call
266 // Fulfill once. So return our cached result.
268 return {fTexture, kReleaseCallbackOnInstantiation, kKeySyncMode};
269 } else if (fFulfillProcFailed) {
270 // We've already called fulfill and it failed. Our contract says that we should only
271 // call each callback once.
275 PromiseImageTextureContext textureContext = fReleaseHelper->context();
276 sk_sp<SkPromiseImageTexture> promiseTexture = fFulfillProc(textureContext);
278 if (!promiseTexture) {
279 fFulfillProcFailed = true;
283 const GrBackendTexture& backendTexture = promiseTexture->backendTexture();
284 if (!backendTexture.isValid()) {
288 fTexture = resourceProvider->wrapBackendTexture(backendTexture,
289 kBorrow_GrWrapOwnership,
290 GrWrapCacheable::kNo,
295 fTexture->setRelease(fReleaseHelper);
296 auto dContext = fTexture->getContext();
297 fTextureContextID = dContext->directContextID();
298 return {fTexture, kReleaseCallbackOnInstantiation, kKeySyncMode};
302 PromiseImageTextureFulfillProc fFulfillProc;
303 sk_sp<skgpu::RefCntedCallback> fReleaseHelper;
304 sk_sp<GrTexture> fTexture;
305 GrDirectContext::DirectContextID fTextureContextID;
306 bool fFulfillProcFailed = false;
307 } callback(fulfillProc, std::move(releaseHelper));
309 return GrProxyProvider::CreatePromiseProxy(tsp, std::move(callback), backendFormat, dimensions,