2 * Copyright 2015 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_Lazy.h"
10 #include "include/core/SkBitmap.h"
11 #include "include/core/SkColorSpace.h"
12 #include "include/core/SkData.h"
13 #include "include/core/SkImageGenerator.h"
14 #include "src/core/SkBitmapCache.h"
15 #include "src/core/SkCachedData.h"
16 #include "src/core/SkImagePriv.h"
17 #include "src/core/SkNextID.h"
20 #include "include/gpu/GrDirectContext.h"
21 #include "include/gpu/GrRecordingContext.h"
22 #include "src/core/SkResourceCache.h"
23 #include "src/core/SkYUVPlanesCache.h"
24 #include "src/gpu/ResourceKey.h"
25 #include "src/gpu/ganesh/GrCaps.h"
26 #include "src/gpu/ganesh/GrColorSpaceXform.h"
27 #include "src/gpu/ganesh/GrGpuResourcePriv.h"
28 #include "src/gpu/ganesh/GrPaint.h"
29 #include "src/gpu/ganesh/GrProxyProvider.h"
30 #include "src/gpu/ganesh/GrRecordingContextPriv.h"
31 #include "src/gpu/ganesh/GrSamplerState.h"
32 #include "src/gpu/ganesh/GrYUVATextureProxies.h"
33 #include "src/gpu/ganesh/SkGr.h"
34 #include "src/gpu/ganesh/SurfaceFillContext.h"
35 #include "src/gpu/ganesh/effects/GrYUVtoRGBEffect.h"
38 // Ref-counted tuple(SkImageGenerator, SkMutex) which allows sharing one generator among N images
39 class SharedGenerator final : public SkNVRefCnt<SharedGenerator> {
41 static sk_sp<SharedGenerator> Make(std::unique_ptr<SkImageGenerator> gen) {
42 return gen ? sk_sp<SharedGenerator>(new SharedGenerator(std::move(gen))) : nullptr;
45 // This is thread safe. It is a const field set in the constructor.
46 const SkImageInfo& getInfo() { return fGenerator->getInfo(); }
49 explicit SharedGenerator(std::unique_ptr<SkImageGenerator> gen)
50 : fGenerator(std::move(gen)) {
54 friend class ScopedGenerator;
55 friend class SkImage_Lazy;
57 std::unique_ptr<SkImageGenerator> fGenerator;
61 ///////////////////////////////////////////////////////////////////////////////
63 SkImage_Lazy::Validator::Validator(sk_sp<SharedGenerator> gen, const SkColorType* colorType,
64 sk_sp<SkColorSpace> colorSpace)
65 : fSharedGenerator(std::move(gen)) {
66 if (!fSharedGenerator) {
70 // The following generator accessors are safe without acquiring the mutex (const getters).
71 // TODO: refactor to use a ScopedGenerator instead, for clarity.
72 fInfo = fSharedGenerator->fGenerator->getInfo();
73 if (fInfo.isEmpty()) {
74 fSharedGenerator.reset();
78 fUniqueID = fSharedGenerator->fGenerator->uniqueID();
80 if (colorType && (*colorType == fInfo.colorType())) {
84 if (colorType || colorSpace) {
86 fInfo = fInfo.makeColorType(*colorType);
89 fInfo = fInfo.makeColorSpace(colorSpace);
91 fUniqueID = SkNextID::ImageID();
95 ///////////////////////////////////////////////////////////////////////////////
97 // Helper for exclusive access to a shared generator.
98 class SkImage_Lazy::ScopedGenerator {
100 ScopedGenerator(const sk_sp<SharedGenerator>& gen)
101 : fSharedGenerator(gen)
102 , fAutoAquire(gen->fMutex) {}
104 SkImageGenerator* operator->() const {
105 fSharedGenerator->fMutex.assertHeld();
106 return fSharedGenerator->fGenerator.get();
109 operator SkImageGenerator*() const {
110 fSharedGenerator->fMutex.assertHeld();
111 return fSharedGenerator->fGenerator.get();
115 const sk_sp<SharedGenerator>& fSharedGenerator;
116 SkAutoMutexExclusive fAutoAquire;
119 ///////////////////////////////////////////////////////////////////////////////
121 SkImage_Lazy::SkImage_Lazy(Validator* validator)
122 : INHERITED(validator->fInfo, validator->fUniqueID)
123 , fSharedGenerator(std::move(validator->fSharedGenerator))
125 SkASSERT(fSharedGenerator);
129 //////////////////////////////////////////////////////////////////////////////////////////////////
131 bool SkImage_Lazy::getROPixels(GrDirectContext*, SkBitmap* bitmap,
132 SkImage::CachingHint chint) const {
133 auto check_output_bitmap = [bitmap]() {
134 SkASSERT(bitmap->isImmutable());
135 SkASSERT(bitmap->getPixels());
139 auto desc = SkBitmapCacheDesc::Make(this);
140 if (SkBitmapCache::Find(desc, bitmap)) {
141 check_output_bitmap();
145 if (SkImage::kAllow_CachingHint == chint) {
147 SkBitmapCache::RecPtr cacheRec = SkBitmapCache::Alloc(desc, this->imageInfo(), &pmap);
148 if (!cacheRec || !ScopedGenerator(fSharedGenerator)->getPixels(pmap)) {
151 SkBitmapCache::Add(std::move(cacheRec), bitmap);
152 this->notifyAddedToRasterCache();
154 if (!bitmap->tryAllocPixels(this->imageInfo()) ||
155 !ScopedGenerator(fSharedGenerator)->getPixels(bitmap->pixmap())) {
158 bitmap->setImmutable();
161 check_output_bitmap();
165 //////////////////////////////////////////////////////////////////////////////////////////////////
167 bool SkImage_Lazy::onReadPixels(GrDirectContext* dContext,
168 const SkImageInfo& dstInfo,
173 CachingHint chint) const {
175 if (this->getROPixels(dContext, &bm, chint)) {
176 return bm.readPixels(dstInfo, dstPixels, dstRB, srcX, srcY);
181 sk_sp<SkData> SkImage_Lazy::onRefEncoded() const {
182 // check that we aren't a subset or colortype/etc modification of the original
183 if (fSharedGenerator->fGenerator->uniqueID() == this->uniqueID()) {
184 ScopedGenerator generator(fSharedGenerator);
185 return generator->refEncodedData();
190 bool SkImage_Lazy::onIsValid(GrRecordingContext* context) const {
191 ScopedGenerator generator(fSharedGenerator);
192 return generator->isValid(context);
195 ///////////////////////////////////////////////////////////////////////////////////////////////////
197 sk_sp<SkImage> SkImage_Lazy::onMakeSubset(const SkIRect& subset, GrDirectContext* direct) const {
198 // TODO: can we do this more efficiently, by telling the generator we want to
199 // "realize" a subset?
202 auto pixels = direct ? this->makeTextureImage(direct)
203 : this->makeRasterImage();
205 auto pixels = this->makeRasterImage();
207 return pixels ? pixels->makeSubset(subset, direct) : nullptr;
210 sk_sp<SkImage> SkImage_Lazy::onMakeColorTypeAndColorSpace(SkColorType targetCT,
211 sk_sp<SkColorSpace> targetCS,
212 GrDirectContext*) const {
213 SkAutoMutexExclusive autoAquire(fOnMakeColorTypeAndSpaceMutex);
214 if (fOnMakeColorTypeAndSpaceResult &&
215 targetCT == fOnMakeColorTypeAndSpaceResult->colorType() &&
216 SkColorSpace::Equals(targetCS.get(), fOnMakeColorTypeAndSpaceResult->colorSpace())) {
217 return fOnMakeColorTypeAndSpaceResult;
219 Validator validator(fSharedGenerator, &targetCT, targetCS);
220 sk_sp<SkImage> result = validator ? sk_sp<SkImage>(new SkImage_Lazy(&validator)) : nullptr;
222 fOnMakeColorTypeAndSpaceResult = result;
227 sk_sp<SkImage> SkImage_Lazy::onReinterpretColorSpace(sk_sp<SkColorSpace> newCS) const {
228 // TODO: The correct thing is to clone the generator, and modify its color space. That's hard,
229 // because we don't have a clone method, and generator is public (and derived-from by clients).
230 // So do the simple/inefficient thing here, and fallback to raster when this is called.
232 // We allocate the bitmap with the new color space, then generate the image using the original.
234 if (bitmap.tryAllocPixels(this->imageInfo().makeColorSpace(std::move(newCS)))) {
235 SkPixmap pixmap = bitmap.pixmap();
236 pixmap.setColorSpace(this->refColorSpace());
237 if (ScopedGenerator(fSharedGenerator)->getPixels(pixmap)) {
238 bitmap.setImmutable();
239 return bitmap.asImage();
245 sk_sp<SkImage> SkImage::MakeFromGenerator(std::unique_ptr<SkImageGenerator> generator) {
246 SkImage_Lazy::Validator
247 validator(SharedGenerator::Make(std::move(generator)), nullptr, nullptr);
249 return validator ? sk_make_sp<SkImage_Lazy>(&validator) : nullptr;
254 std::tuple<GrSurfaceProxyView, GrColorType> SkImage_Lazy::onAsView(
255 GrRecordingContext* context,
256 GrMipmapped mipmapped,
257 GrImageTexGenPolicy policy) const {
258 GrColorType ct = this->colorTypeOfLockTextureProxy(context->priv().caps());
259 return {this->lockTextureProxyView(context, policy, mipmapped), ct};
262 std::unique_ptr<GrFragmentProcessor> SkImage_Lazy::onAsFragmentProcessor(
263 GrRecordingContext* rContext,
264 SkSamplingOptions sampling,
265 const SkTileMode tileModes[2],
267 const SkRect* subset,
268 const SkRect* domain) const {
269 // TODO: If the CPU data is extracted as planes return a FP that reconstructs the image from
271 auto mm = sampling.mipmap == SkMipmapMode::kNone ? GrMipmapped::kNo : GrMipmapped::kYes;
272 return MakeFragmentProcessorFromView(rContext,
273 std::get<0>(this->asView(rContext, mm)),
282 GrSurfaceProxyView SkImage_Lazy::textureProxyViewFromPlanes(GrRecordingContext* ctx,
283 SkBudgeted budgeted) const {
284 SkYUVAPixmapInfo::SupportedDataTypes supportedDataTypes(*ctx);
285 SkYUVAPixmaps yuvaPixmaps;
286 sk_sp<SkCachedData> dataStorage = this->getPlanes(supportedDataTypes, &yuvaPixmaps);
291 GrSurfaceProxyView views[SkYUVAInfo::kMaxPlanes];
292 GrColorType pixmapColorTypes[SkYUVAInfo::kMaxPlanes];
293 for (int i = 0; i < yuvaPixmaps.numPlanes(); ++i) {
294 // If the sizes of the components are not all the same we choose to create exact-match
295 // textures for the smaller ones rather than add a texture domain to the draw.
296 // TODO: revisit this decision to improve texture reuse?
297 SkBackingFit fit = yuvaPixmaps.plane(i).dimensions() == this->dimensions()
298 ? SkBackingFit::kApprox
299 : SkBackingFit::kExact;
301 // We grab a ref to cached yuv data. When the SkBitmap we create below goes away it will
302 // call releaseProc which will release this ref.
303 // DDL TODO: Currently we end up creating a lazy proxy that will hold onto a ref to the
304 // SkImage in its lambda. This means that we'll keep the ref on the YUV data around for the
305 // life time of the proxy and not just upload. For non-DDL draws we should look into
306 // releasing this SkImage after uploads (by deleting the lambda after instantiation).
307 auto releaseProc = [](void*, void* data) {
308 auto cachedData = static_cast<SkCachedData*>(data);
309 SkASSERT(cachedData);
313 bitmap.installPixels(yuvaPixmaps.plane(i).info(),
314 yuvaPixmaps.plane(i).writable_addr(),
315 yuvaPixmaps.plane(i).rowBytes(),
317 SkRef(dataStorage.get()));
318 bitmap.setImmutable();
320 std::tie(views[i], std::ignore) = GrMakeUncachedBitmapProxyView(ctx,
327 pixmapColorTypes[i] = SkColorTypeToGrColorType(bitmap.colorType());
330 // TODO: investigate preallocating mip maps here
331 GrImageInfo info(SkColorTypeToGrColorType(this->colorType()),
333 /*color space*/ nullptr,
336 auto sfc = ctx->priv().makeSFC(info,
337 SkBackingFit::kExact,
341 kTopLeft_GrSurfaceOrigin,
347 GrYUVATextureProxies yuvaProxies(yuvaPixmaps.yuvaInfo(), views, pixmapColorTypes);
348 SkAssertResult(yuvaProxies.isValid());
350 std::unique_ptr<GrFragmentProcessor> fp = GrYUVtoRGBEffect::Make(
352 GrSamplerState::Filter::kNearest,
353 *ctx->priv().caps());
355 // The pixels after yuv->rgb will be in the generator's color space.
356 // If onMakeColorTypeAndColorSpace has been called then this will not match this image's
357 // color space. To correct this, apply a color space conversion from the generator's color
358 // space to this image's color space.
359 SkColorSpace* srcColorSpace;
361 ScopedGenerator generator(fSharedGenerator);
362 srcColorSpace = generator->getInfo().colorSpace();
364 SkColorSpace* dstColorSpace = this->colorSpace();
366 // If the caller expects the pixels in a different color space than the one from the image,
367 // apply a color conversion to do this.
368 fp = GrColorSpaceXformEffect::Make(std::move(fp),
369 srcColorSpace, kOpaque_SkAlphaType,
370 dstColorSpace, kOpaque_SkAlphaType);
371 sfc->fillWithFP(std::move(fp));
373 return sfc->readSurfaceView();
376 sk_sp<SkCachedData> SkImage_Lazy::getPlanes(
377 const SkYUVAPixmapInfo::SupportedDataTypes& supportedDataTypes,
378 SkYUVAPixmaps* yuvaPixmaps) const {
379 ScopedGenerator generator(fSharedGenerator);
381 sk_sp<SkCachedData> data(SkYUVPlanesCache::FindAndRef(generator->uniqueID(), yuvaPixmaps));
384 SkASSERT(yuvaPixmaps->isValid());
385 SkASSERT(yuvaPixmaps->yuvaInfo().dimensions() == this->dimensions());
388 SkYUVAPixmapInfo yuvaPixmapInfo;
389 if (!generator->queryYUVAInfo(supportedDataTypes, &yuvaPixmapInfo) ||
390 yuvaPixmapInfo.yuvaInfo().dimensions() != this->dimensions()) {
393 data.reset(SkResourceCache::NewCachedData(yuvaPixmapInfo.computeTotalBytes()));
394 SkYUVAPixmaps tempPixmaps = SkYUVAPixmaps::FromExternalMemory(yuvaPixmapInfo,
395 data->writable_data());
396 SkASSERT(tempPixmaps.isValid());
397 if (!generator->getYUVAPlanes(tempPixmaps)) {
400 // Decoding is done, cache the resulting YUV planes
401 *yuvaPixmaps = tempPixmaps;
402 SkYUVPlanesCache::Add(this->uniqueID(), data.get(), *yuvaPixmaps);
407 * We have 4 ways to try to return a texture (in sorted order)
409 * 1. Check the cache for a pre-existing one
410 * 2. Ask the generator to natively create one
411 * 3. Ask the generator to return YUV planes, which the GPU can convert
412 * 4. Ask the generator to return RGB(A) data, which the GPU can convert
414 GrSurfaceProxyView SkImage_Lazy::lockTextureProxyView(GrRecordingContext* rContext,
415 GrImageTexGenPolicy texGenPolicy,
416 GrMipmapped mipmapped) const {
417 // Values representing the various texture lock paths we can take. Used for logging the path
418 // taken to a histogram.
419 enum LockTexturePath {
420 kFailure_LockTexturePath,
421 kPreExisting_LockTexturePath,
422 kNative_LockTexturePath,
423 kCompressed_LockTexturePath, // Deprecated
424 kYUV_LockTexturePath,
425 kRGBA_LockTexturePath,
428 enum { kLockTexturePathCount = kRGBA_LockTexturePath + 1 };
430 skgpu::UniqueKey key;
431 if (texGenPolicy == GrImageTexGenPolicy::kDraw) {
432 GrMakeKeyFromImageID(&key, this->uniqueID(), SkIRect::MakeSize(this->dimensions()));
435 const GrCaps* caps = rContext->priv().caps();
436 GrProxyProvider* proxyProvider = rContext->priv().proxyProvider();
438 auto installKey = [&](const GrSurfaceProxyView& view) {
439 SkASSERT(view && view.asTextureProxy());
441 auto listener = GrMakeUniqueKeyInvalidationListener(&key, rContext->priv().contextID());
442 this->addUniqueIDListener(std::move(listener));
443 proxyProvider->assignUniqueKeyToProxy(key, view.asTextureProxy());
447 auto ct = this->colorTypeOfLockTextureProxy(caps);
449 // 1. Check the cache for a pre-existing one.
451 auto proxy = proxyProvider->findOrCreateProxyByUniqueKey(key);
453 skgpu::Swizzle swizzle = caps->getReadSwizzle(proxy->backendFormat(), ct);
454 GrSurfaceOrigin origin = ScopedGenerator(fSharedGenerator)->origin();
455 GrSurfaceProxyView view(std::move(proxy), origin, swizzle);
456 if (mipmapped == GrMipmapped::kNo ||
457 view.asTextureProxy()->mipmapped() == GrMipmapped::kYes) {
460 // We need a mipped proxy, but we found a cached proxy that wasn't mipped. Thus we
461 // generate a new mipped surface and copy the original proxy into the base layer. We
462 // will then let the gpu generate the rest of the mips.
463 auto mippedView = GrCopyBaseMipMapToView(rContext, view);
465 // We failed to make a mipped proxy with the base copied into it. This could
466 // have been from failure to make the proxy or failure to do the copy. Thus we
467 // will fall back to just using the non mipped proxy; See skbug.com/7094.
470 proxyProvider->removeUniqueKeyFromProxy(view.asTextureProxy());
471 installKey(mippedView);
477 // 2. Ask the generator to natively create one.
479 ScopedGenerator generator(fSharedGenerator);
480 if (auto view = generator->generateTexture(rContext,
490 // 3. Ask the generator to return YUV planes, which the GPU can convert. If we will be mipping
491 // the texture we skip this step so the CPU generate non-planar MIP maps for us.
492 if (mipmapped == GrMipmapped::kNo && !rContext->priv().options().fDisableGpuYUVConversion) {
493 // TODO: Update to create the mipped surface in the textureProxyViewFromPlanes generator and
494 // draw the base layer directly into the mipped surface.
495 SkBudgeted budgeted = texGenPolicy == GrImageTexGenPolicy::kNew_Uncached_Unbudgeted
498 auto view = this->textureProxyViewFromPlanes(rContext, budgeted);
505 // 4. Ask the generator to return a bitmap, which the GPU can convert.
506 auto hint = texGenPolicy == GrImageTexGenPolicy::kDraw ? CachingHint::kAllow_CachingHint
507 : CachingHint::kDisallow_CachingHint;
508 if (SkBitmap bitmap; this->getROPixels(nullptr, &bitmap, hint)) {
509 // We always make an uncached bitmap here because we will cache it based on passed in policy
510 // with *our* key, not a key derived from bitmap. We're just making the proxy here.
511 auto budgeted = texGenPolicy == GrImageTexGenPolicy::kNew_Uncached_Unbudgeted
514 auto view = std::get<0>(GrMakeUncachedBitmapProxyView(rContext,
517 SkBackingFit::kExact,
528 GrColorType SkImage_Lazy::colorTypeOfLockTextureProxy(const GrCaps* caps) const {
529 GrColorType ct = SkColorTypeToGrColorType(this->colorType());
530 GrBackendFormat format = caps->getDefaultBackendFormat(ct, GrRenderable::kNo);
531 if (!format.isValid()) {
532 ct = GrColorType::kRGBA_8888;
537 void SkImage_Lazy::addUniqueIDListener(sk_sp<SkIDChangeListener> listener) const {
538 fUniqueIDListeners.add(std::move(listener));