1/*
2 * Copyright 2018 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
8#include "src/image/SkImage_GpuBase.h"
9
10#include "include/core/SkPromiseImageTexture.h"
11#include "include/gpu/GrBackendSurface.h"
12#include "include/gpu/GrDirectContext.h"
13#include "include/gpu/GrRecordingContext.h"
14#include "src/core/SkBitmapCache.h"
15#include "src/core/SkTLList.h"
16#include "src/gpu/GrContextPriv.h"
17#include "src/gpu/GrImageInfo.h"
18#include "src/gpu/GrProxyProvider.h"
19#include "src/gpu/GrRecordingContextPriv.h"
20#include "src/gpu/GrRenderTargetContext.h"
21#include "src/gpu/GrTexture.h"
22#include "src/gpu/GrTextureAdjuster.h"
23#include "src/gpu/effects/GrYUVtoRGBEffect.h"
24#include "src/image/SkImage_Gpu.h"
25#include "src/image/SkReadPixelsRec.h"
26
27SkImage_GpuBase::SkImage_GpuBase(sk_sp<GrContext> context, SkISize size, uint32_t uniqueID,
28 SkColorType ct, SkAlphaType at, sk_sp<SkColorSpace> cs)
29 : INHERITED(SkImageInfo::Make(size, ct, at, std::move(cs)), uniqueID)
30 , fContext(std::move(context)) {}
31
32//////////////////////////////////////////////////////////////////////////////////////////////////
33
34#if GR_TEST_UTILS
35void SkImage_GpuBase::resetContext(sk_sp<GrContext> newContext) {
36 SkASSERT(fContext->priv().matches(newContext.get()));
37 fContext = newContext;
38}
39#endif
40
41bool SkImage_GpuBase::ValidateBackendTexture(const GrCaps* caps, const GrBackendTexture& tex,
42 GrColorType grCT, SkColorType ct, SkAlphaType at,
43 sk_sp<SkColorSpace> cs) {
44 if (!tex.isValid()) {
45 return false;
46 }
47 SkColorInfo info(ct, at, cs);
48 if (!SkColorInfoIsValid(info)) {
49 return false;
50 }
51 GrBackendFormat backendFormat = tex.getBackendFormat();
52 if (!backendFormat.isValid()) {
53 return false;
54 }
55
56 return caps->areColorTypeAndFormatCompatible(grCT, backendFormat);
57}
58
59bool SkImage_GpuBase::ValidateCompressedBackendTexture(const GrCaps* caps,
60 const GrBackendTexture& tex,
61 SkAlphaType at) {
62 if (!tex.isValid() || tex.width() <= 0 || tex.height() <= 0) {
63 return false;
64 }
65
66 if (tex.width() > caps->maxTextureSize() || tex.height() > caps->maxTextureSize()) {
67 return false;
68 }
69
70 if (at == kUnknown_SkAlphaType) {
71 return false;
72 }
73
74 GrBackendFormat backendFormat = tex.getBackendFormat();
75 if (!backendFormat.isValid()) {
76 return false;
77 }
78
79 if (!caps->isFormatCompressed(backendFormat)) {
80 return false;
81 }
82
83 return true;
84}
85
86//////////////////////////////////////////////////////////////////////////////////////////////////
87
88bool SkImage_GpuBase::getROPixels(SkBitmap* dst, CachingHint chint) const {
89 auto dContext = fContext->asDirectContext();
90 if (!dContext) {
91 // DDL TODO: buffer up the readback so it occurs when the DDL is drawn?
92 return false;
93 }
94
95 const auto desc = SkBitmapCacheDesc::Make(this);
96 if (SkBitmapCache::Find(desc, dst)) {
97 SkASSERT(dst->isImmutable());
98 SkASSERT(dst->getPixels());
99 return true;
100 }
101
102 SkBitmapCache::RecPtr rec = nullptr;
103 SkPixmap pmap;
104 if (kAllow_CachingHint == chint) {
105 rec = SkBitmapCache::Alloc(desc, this->imageInfo(), &pmap);
106 if (!rec) {
107 return false;
108 }
109 } else {
110 if (!dst->tryAllocPixels(this->imageInfo()) || !dst->peekPixels(&pmap)) {
111 return false;
112 }
113 }
114
115 const GrSurfaceProxyView* view = this->view(dContext);
116 SkASSERT(view);
117 GrColorType grColorType = SkColorTypeAndFormatToGrColorType(
118 fContext->priv().caps(), this->colorType(), view->proxy()->backendFormat());
119
120 auto sContext = GrSurfaceContext::Make(dContext, *view, grColorType, this->alphaType(),
121 this->refColorSpace());
122 if (!sContext) {
123 return false;
124 }
125
126 if (!sContext->readPixels(dContext, pmap.info(), pmap.writable_addr(), pmap.rowBytes(),
127 {0, 0})) {
128 return false;
129 }
130
131 if (rec) {
132 SkBitmapCache::Add(std::move(rec), dst);
133 this->notifyAddedToRasterCache();
134 }
135 return true;
136}
137
138sk_sp<SkImage> SkImage_GpuBase::onMakeSubset(const SkIRect& subset,
139 GrDirectContext* direct) const {
140 if (!fContext->priv().matches(direct)) {
141 return nullptr;
142 }
143
144 const GrSurfaceProxyView* view = this->view(direct);
145 SkASSERT(view && view->proxy());
146
147 auto copyView = GrSurfaceProxyView::Copy(direct, *view, GrMipmapped::kNo, subset,
148 SkBackingFit::kExact, view->proxy()->isBudgeted());
149
150 if (!copyView) {
151 return nullptr;
152 }
153
154 // MDB: this call is okay bc we know 'sContext' was kExact
155 return sk_make_sp<SkImage_Gpu>(sk_ref_sp(direct), kNeedNewImageUniqueID, std::move(copyView),
156 this->colorType(), this->alphaType(), this->refColorSpace());
157}
158
159bool SkImage_GpuBase::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRB,
160 int srcX, int srcY, CachingHint) const {
161 auto dContext = fContext->asDirectContext();
162 if (!dContext) {
163 // DDL TODO: buffer up the readback so it occurs when the DDL is drawn?
164 return false;
165 }
166
167 if (!SkImageInfoValidConversion(dstInfo, this->imageInfo())) {
168 return false;
169 }
170
171 const GrSurfaceProxyView* view = this->view(dContext);
172 SkASSERT(view);
173 GrColorType grColorType = SkColorTypeAndFormatToGrColorType(
174 dContext->priv().caps(), this->colorType(), view->proxy()->backendFormat());
175
176 auto sContext = GrSurfaceContext::Make(dContext, *view, grColorType, this->alphaType(),
177 this->refColorSpace());
178 if (!sContext) {
179 return false;
180 }
181
182 return sContext->readPixels(dContext, dstInfo, dstPixels, dstRB, {srcX, srcY});
183}
184
185GrSurfaceProxyView SkImage_GpuBase::refView(GrRecordingContext* context,
186 GrMipmapped mipMapped) const {
187 if (!context || !fContext->priv().matches(context)) {
188 SkASSERT(0);
189 return {};
190 }
191
192 GrTextureAdjuster adjuster(fContext.get(), *this->view(context), this->imageInfo().colorInfo(),
193 this->uniqueID());
194 return adjuster.view(mipMapped);
195}
196
197GrBackendTexture SkImage_GpuBase::onGetBackendTexture(bool flushPendingGrContextIO,
198 GrSurfaceOrigin* origin) const {
199 auto direct = fContext->asDirectContext();
200 if (!direct) {
201 // This image was created with a DDL context and cannot be instantiated.
202 return GrBackendTexture(); // invalid
203 }
204
205 const GrSurfaceProxyView* view = this->view(direct);
206 SkASSERT(view && *view);
207 GrSurfaceProxy* proxy = view->proxy();
208
209 if (!proxy->isInstantiated()) {
210 auto resourceProvider = direct->priv().resourceProvider();
211
212 if (!proxy->instantiate(resourceProvider)) {
213 return GrBackendTexture(); // invalid
214 }
215 }
216
217 GrTexture* texture = proxy->peekTexture();
218 if (texture) {
219 if (flushPendingGrContextIO) {
220 direct->priv().flushSurface(proxy);
221 }
222 if (origin) {
223 *origin = view->origin();
224 }
225 return texture->getBackendTexture();
226 }
227 return GrBackendTexture(); // invalid
228}
229
230GrTexture* SkImage_GpuBase::getTexture() const {
231 GrTextureProxy* proxy = this->peekProxy();
232 if (proxy && proxy->isInstantiated()) {
233 return proxy->peekTexture();
234 }
235
236 auto direct = fContext->asDirectContext();
237 if (!direct) {
238 // This image was created with a DDL context and cannot be instantiated.
239 return nullptr;
240 }
241
242 const GrSurfaceProxyView* view = this->view(direct);
243 SkASSERT(view && *view && !view->proxy()->isInstantiated());
244
245 if (!view->proxy()->instantiate(direct->priv().resourceProvider())) {
246 return nullptr;
247 }
248
249 return view->proxy()->peekTexture();
250}
251
252bool SkImage_GpuBase::onIsValid(GrRecordingContext* context) const {
253 // The base class has already checked that 'context' isn't abandoned (if it's not nullptr)
254 if (fContext->abandoned()) {
255 return false;
256 }
257
258 if (context && !fContext->priv().matches(context)) {
259 return false;
260 }
261
262 return true;
263}
264
265bool SkImage_GpuBase::MakeTempTextureProxies(GrRecordingContext* rContext,
266 const GrBackendTexture yuvaTextures[],
267 int numTextures,
268 const SkYUVAIndex yuvaIndices[4],
269 GrSurfaceOrigin imageOrigin,
270 GrSurfaceProxyView tempViews[4],
271 sk_sp<GrRefCntedCallback> releaseHelper) {
272 GrProxyProvider* proxyProvider = rContext->priv().proxyProvider();
273 for (int textureIndex = 0; textureIndex < numTextures; ++textureIndex) {
274 const GrBackendFormat& backendFormat = yuvaTextures[textureIndex].getBackendFormat();
275 if (!backendFormat.isValid()) {
276 return false;
277 }
278
279 SkASSERT(yuvaTextures[textureIndex].isValid());
280
281 auto proxy = proxyProvider->wrapBackendTexture(yuvaTextures[textureIndex],
282 kBorrow_GrWrapOwnership,
283 GrWrapCacheable::kNo, kRead_GrIOType,
284 releaseHelper);
285 if (!proxy) {
286 return false;
287 }
288 tempViews[textureIndex] =
289 GrSurfaceProxyView(std::move(proxy), imageOrigin, GrSwizzle("rgba"));
290
291 // Check that each texture contains the channel data for the corresponding YUVA index
292 auto formatChannelMask = backendFormat.channelMask();
293 if (formatChannelMask & kGray_SkColorChannelFlag) {
294 formatChannelMask |= kRGB_SkColorChannelFlags;
295 }
296 for (int yuvaIndex = 0; yuvaIndex < SkYUVAIndex::kIndexCount; ++yuvaIndex) {
297 if (yuvaIndices[yuvaIndex].fIndex == textureIndex) {
298 uint32_t channelAsMask = 1 << static_cast<int>(yuvaIndices[yuvaIndex].fChannel);
299 if (!(channelAsMask & formatChannelMask)) {
300 return false;
301 }
302 }
303 }
304 }
305
306 return true;
307}
308
309bool SkImage_GpuBase::RenderYUVAToRGBA(const GrCaps& caps,
310 GrRenderTargetContext* renderTargetContext,
311 const SkRect& rect, SkYUVColorSpace yuvColorSpace,
312 sk_sp<GrColorSpaceXform> colorSpaceXform,
313 GrSurfaceProxyView views[4],
314 const SkYUVAIndex yuvaIndices[4]) {
315 SkASSERT(renderTargetContext);
316 if (!renderTargetContext->asSurfaceProxy()) {
317 return false;
318 }
319
320 GrPaint paint;
321 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
322
323 auto fp = GrYUVtoRGBEffect::Make(views, yuvaIndices, yuvColorSpace,
324 GrSamplerState::Filter::kNearest, caps);
325 if (colorSpaceXform) {
326 fp = GrColorSpaceXformEffect::Make(std::move(fp), std::move(colorSpaceXform));
327 }
328 paint.setColorFragmentProcessor(std::move(fp));
329
330 renderTargetContext->drawRect(nullptr, std::move(paint), GrAA::kNo, SkMatrix::I(), rect);
331 return true;
332}
333
334sk_sp<GrTextureProxy> SkImage_GpuBase::MakePromiseImageLazyProxy(
335 GrRecordingContext* context, int width, int height,
336 GrBackendFormat backendFormat, GrMipmapped mipMapped,
337 PromiseImageTextureFulfillProc fulfillProc, PromiseImageTextureReleaseProc releaseProc,
338 PromiseImageTextureDoneProc doneProc, PromiseImageTextureContext textureContext,
339 PromiseImageApiVersion version) {
340 SkASSERT(context);
341 SkASSERT(width > 0 && height > 0);
342 SkASSERT(doneProc);
343
344 if (!fulfillProc || !releaseProc) {
345 doneProc(textureContext);
346 return nullptr;
347 }
348
349 if (mipMapped == GrMipmapped::kYes &&
350 GrTextureTypeHasRestrictedSampling(backendFormat.textureType())) {
351 // It is invalid to have a GL_TEXTURE_EXTERNAL or GL_TEXTURE_RECTANGLE and have mips as
352 // well.
353 doneProc(textureContext);
354 return nullptr;
355 }
356
357 /**
358 * This class is the lazy instantiation callback for promise images. It manages calling the
359 * client's Fulfill, Release, and Done procs. It attempts to reuse a GrTexture instance in
360 * cases where the client provides the same SkPromiseImageTexture as Fulfill results for
361 * multiple SkImages. The created GrTexture is given a key based on a unique ID associated with
362 * the SkPromiseImageTexture.
363 *
364 * The GrTexutre idle proc mechanism is used to call the Release and Done procs. We use this
365 * instead of the GrSurface release proc because the GrTexture is cached and therefore may
366 * outlive the proxy into which this callback is installed.
367 *
368 * A key invalidation message is installed on the SkPromiseImageTexture so that the GrTexture
369 * is deleted once it can no longer be used to instantiate a proxy.
370 */
371 class PromiseLazyInstantiateCallback {
372 public:
373 PromiseLazyInstantiateCallback(PromiseImageTextureFulfillProc fulfillProc,
374 PromiseImageTextureReleaseProc releaseProc,
375 PromiseImageTextureDoneProc doneProc,
376 PromiseImageTextureContext context,
377 PromiseImageApiVersion version)
378 : fFulfillProc(fulfillProc)
379 , fReleaseProc(releaseProc)
380 , fVersion(version) {
381 fDoneCallback = sk_make_sp<GrRefCntedCallback>(doneProc, context);
382 }
383 PromiseLazyInstantiateCallback(PromiseLazyInstantiateCallback&&) = default;
384 PromiseLazyInstantiateCallback(const PromiseLazyInstantiateCallback&) {
385 // Because we get wrapped in std::function we must be copyable. But we should never
386 // be copied.
387 SkASSERT(false);
388 }
389 PromiseLazyInstantiateCallback& operator=(PromiseLazyInstantiateCallback&&) = default;
390 PromiseLazyInstantiateCallback& operator=(const PromiseLazyInstantiateCallback&) {
391 SkASSERT(false);
392 return *this;
393 }
394
395 ~PromiseLazyInstantiateCallback() {
396 // Our destructor can run on any thread. We trigger the unref of fTexture by message.
397 // This unreffed texture pointer is a real problem! When the context has been
398 // abandoned, the GrTexture pointed to by this pointer is deleted! Due to virtual
399 // inheritance any manipulation of this pointer at that point will cause a crash.
400 // For now we "work around" the problem by just passing it, untouched, into the
401 // message bus but this very fragile.
402 // In the future the GrSurface class hierarchy refactoring should eliminate this
403 // difficulty by removing the virtual inheritance.
404 if (fTexture) {
405 SkMessageBus<GrTextureFreedMessage>::Post({fTexture, fTextureContextID});
406 }
407 }
408
409 GrSurfaceProxy::LazyCallbackResult operator()(GrResourceProvider* resourceProvider,
410 const GrSurfaceProxy::LazySurfaceDesc&) {
411 // We use the unique key in a way that is unrelated to the SkImage-based key that the
412 // proxy may receive, hence kUnsynced.
413 static constexpr auto kKeySyncMode =
414 GrSurfaceProxy::LazyInstantiationKeyMode::kUnsynced;
415
416 // In order to make the SkImage "thread safe" we rely on holding an extra ref to the
417 // texture in the callback and signalling the unref via a message to the resource cache.
418 // We need to extend the callback's lifetime to that of the proxy.
419 static constexpr auto kReleaseCallbackOnInstantiation = false;
420
421 // Our proxy is getting instantiated for the second+ time. We are only allowed to call
422 // Fulfill once. So return our cached result.
423 if (fTexture) {
424 return {sk_ref_sp(fTexture), kReleaseCallbackOnInstantiation, kKeySyncMode};
425 } else if (fFulfillProcFailed) {
426 // We've already called fulfill and it failed. Our contract says that we should only
427 // call each callback once.
428 return {};
429 }
430 SkASSERT(fDoneCallback);
431 PromiseImageTextureContext textureContext = fDoneCallback->context();
432 sk_sp<SkPromiseImageTexture> promiseTexture = fFulfillProc(textureContext);
433 // From here on out our contract is that the release proc must be called, even if
434 // the return from fulfill was invalid or we fail for some other reason.
435 auto releaseCallback = sk_make_sp<GrRefCntedCallback>(fReleaseProc, textureContext);
436 if (!promiseTexture) {
437 fFulfillProcFailed = true;
438 return {};
439 }
440
441 const GrBackendTexture& backendTexture = promiseTexture->backendTexture();
442 if (!backendTexture.isValid()) {
443 return {};
444 }
445
446 sk_sp<GrTexture> tex;
447 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
448 GrUniqueKey key;
449 GrUniqueKey::Builder builder(&key, kDomain, 1, "promise");
450 builder[0] = promiseTexture->uniqueID();
451 builder.finish();
452 // A texture with this key may already exist from a different instance of this lazy
453 // callback. This could happen if the client fulfills a promise image with a texture
454 // that was previously used to fulfill a different promise image.
455 if (auto surf = resourceProvider->findByUniqueKey<GrSurface>(key)) {
456 tex = sk_ref_sp(surf->asTexture());
457 SkASSERT(tex);
458 } else {
459 if ((tex = resourceProvider->wrapBackendTexture(
460 backendTexture, kBorrow_GrWrapOwnership, GrWrapCacheable::kYes,
461 kRead_GrIOType))) {
462 tex->resourcePriv().setUniqueKey(key);
463 } else {
464 return {};
465 }
466 }
467 auto releaseIdleState = fVersion == PromiseImageApiVersion::kLegacy
468 ? GrTexture::IdleState::kFinished
469 : GrTexture::IdleState::kFlushed;
470 tex->addIdleProc(std::move(releaseCallback), releaseIdleState);
471 tex->addIdleProc(std::move(fDoneCallback), GrTexture::IdleState::kFinished);
472 promiseTexture->addKeyToInvalidate(tex->getContext()->priv().contextID(), key);
473 fTexture = tex.get();
474 // We need to hold on to the GrTexture in case our proxy gets reinstantiated. However,
475 // we can't unref in our destructor because we may be on another thread then. So we
476 // let the cache know it is waiting on an unref message. We will send that message from
477 // our destructor.
478 GrContext* context = fTexture->getContext();
479 context->priv().getResourceCache()->insertDelayedTextureUnref(fTexture);
480 fTextureContextID = context->priv().contextID();
481 return {std::move(tex), kReleaseCallbackOnInstantiation, kKeySyncMode};
482 }
483
484 private:
485 PromiseImageTextureFulfillProc fFulfillProc;
486 PromiseImageTextureReleaseProc fReleaseProc;
487 sk_sp<GrRefCntedCallback> fDoneCallback;
488 GrTexture* fTexture = nullptr;
489 uint32_t fTextureContextID = SK_InvalidUniqueID;
490 PromiseImageApiVersion fVersion;
491 bool fFulfillProcFailed = false;
492 } callback(fulfillProc, releaseProc, doneProc, textureContext, version);
493
494 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
495
496 // Ganesh assumes that, when wrapping a mipmapped backend texture from a client, that its
497 // mipmaps are fully fleshed out.
498 GrMipmapStatus mipmapStatus = (GrMipmapped::kYes == mipMapped) ? GrMipmapStatus::kValid
499 : GrMipmapStatus::kNotAllocated;
500
501 // We pass kReadOnly here since we should treat content of the client's texture as immutable.
502 // The promise API provides no way for the client to indicated that the texture is protected.
503 return proxyProvider->createLazyProxy(
504 std::move(callback), backendFormat, {width, height}, GrRenderable::kNo, 1, mipMapped,
505 mipmapStatus, GrInternalSurfaceFlags::kReadOnly, SkBackingFit::kExact, SkBudgeted::kNo,
506 GrProtected::kNo, GrSurfaceProxy::UseAllocator::kYes);
507}
508