reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 "SkImage_Base.h" |
| 9 | #include "SkBitmap.h" |
| 10 | #include "SkData.h" |
| 11 | #include "SkImageCacherator.h" |
| 12 | #include "SkImagePriv.h" |
| 13 | #include "SkPixelRef.h" |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 14 | |
| 15 | class SkImage_Generator : public SkImage_Base { |
| 16 | public: |
fmalita | 7929e3a | 2016-10-27 08:15:44 -0700 | [diff] [blame] | 17 | SkImage_Generator(SkImageCacherator::Validator* validator) |
| 18 | : INHERITED(validator->fInfo.width(), validator->fInfo.height(), validator->fUniqueID) |
| 19 | , fCache(validator) |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 20 | {} |
| 21 | |
herb | a7c9d63 | 2016-04-19 12:30:22 -0700 | [diff] [blame] | 22 | virtual SkImageInfo onImageInfo() const override { |
fmalita | 7929e3a | 2016-10-27 08:15:44 -0700 | [diff] [blame] | 23 | return fCache.info(); |
herb | a7c9d63 | 2016-04-19 12:30:22 -0700 | [diff] [blame] | 24 | } |
brianosman | 69c166d | 2016-08-17 14:01:05 -0700 | [diff] [blame] | 25 | SkAlphaType onAlphaType() const override { |
fmalita | 7929e3a | 2016-10-27 08:15:44 -0700 | [diff] [blame] | 26 | return fCache.info().alphaType(); |
brianosman | 69c166d | 2016-08-17 14:01:05 -0700 | [diff] [blame] | 27 | } |
herb | a7c9d63 | 2016-04-19 12:30:22 -0700 | [diff] [blame] | 28 | |
reed | 0955303 | 2015-11-23 12:32:16 -0800 | [diff] [blame] | 29 | bool onReadPixels(const SkImageInfo&, void*, size_t, int srcX, int srcY, CachingHint) const override; |
fmalita | 7929e3a | 2016-10-27 08:15:44 -0700 | [diff] [blame] | 30 | SkImageCacherator* peekCacherator() const override { return &fCache; } |
reed | 05dd251 | 2016-01-05 09:16:19 -0800 | [diff] [blame] | 31 | SkData* onRefEncoded(GrContext*) const override; |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 32 | sk_sp<SkImage> onMakeSubset(const SkIRect&) const override; |
Brian Osman | 61624f0 | 2016-12-09 14:51:59 -0500 | [diff] [blame] | 33 | bool getROPixels(SkBitmap*, SkColorSpace* dstColorSpace, CachingHint) const override; |
| 34 | GrTexture* asTextureRef(GrContext*, const GrSamplerParams&, SkColorSpace*, |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 35 | sk_sp<SkColorSpace>*, SkScalar scaleAdjust[2]) const override; |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 36 | bool onIsLazyGenerated() const override { return true; } |
| 37 | |
| 38 | private: |
fmalita | 7929e3a | 2016-10-27 08:15:44 -0700 | [diff] [blame] | 39 | mutable SkImageCacherator fCache; |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 40 | |
| 41 | typedef SkImage_Base INHERITED; |
| 42 | }; |
| 43 | |
| 44 | /////////////////////////////////////////////////////////////////////////////// |
| 45 | |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 46 | bool SkImage_Generator::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRB, |
reed | 0955303 | 2015-11-23 12:32:16 -0800 | [diff] [blame] | 47 | int srcX, int srcY, CachingHint chint) const { |
Brian Osman | 61624f0 | 2016-12-09 14:51:59 -0500 | [diff] [blame] | 48 | SkColorSpace* dstColorSpace = dstInfo.colorSpace(); |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 49 | SkBitmap bm; |
reed | 6868c3f | 2015-11-24 11:44:47 -0800 | [diff] [blame] | 50 | if (kDisallow_CachingHint == chint) { |
Brian Osman | 61624f0 | 2016-12-09 14:51:59 -0500 | [diff] [blame] | 51 | SkImageCacherator::CachedFormat cacheFormat = fCache.chooseCacheFormat(dstColorSpace); |
Brian Osman | 7992da3 | 2016-11-18 11:28:24 -0500 | [diff] [blame] | 52 | if (fCache.lockAsBitmapOnlyIfAlreadyCached(&bm, cacheFormat)) { |
reed | 6868c3f | 2015-11-24 11:44:47 -0800 | [diff] [blame] | 53 | return bm.readPixels(dstInfo, dstPixels, dstRB, srcX, srcY); |
| 54 | } else { |
| 55 | // Try passing the caller's buffer directly down to the generator. If this fails we |
| 56 | // may still succeed in the general case, as the generator may prefer some other |
| 57 | // config, which we could then convert via SkBitmap::readPixels. |
fmalita | 7929e3a | 2016-10-27 08:15:44 -0700 | [diff] [blame] | 58 | if (fCache.directGeneratePixels(dstInfo, dstPixels, dstRB, srcX, srcY)) { |
reed | 6868c3f | 2015-11-24 11:44:47 -0800 | [diff] [blame] | 59 | return true; |
| 60 | } |
| 61 | // else fall through |
| 62 | } |
| 63 | } |
| 64 | |
Brian Osman | 61624f0 | 2016-12-09 14:51:59 -0500 | [diff] [blame] | 65 | if (this->getROPixels(&bm, dstColorSpace, chint)) { |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 66 | return bm.readPixels(dstInfo, dstPixels, dstRB, srcX, srcY); |
| 67 | } |
| 68 | return false; |
| 69 | } |
| 70 | |
reed | 05dd251 | 2016-01-05 09:16:19 -0800 | [diff] [blame] | 71 | SkData* SkImage_Generator::onRefEncoded(GrContext* ctx) const { |
fmalita | 7929e3a | 2016-10-27 08:15:44 -0700 | [diff] [blame] | 72 | return fCache.refEncoded(ctx); |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 73 | } |
| 74 | |
Brian Osman | 61624f0 | 2016-12-09 14:51:59 -0500 | [diff] [blame] | 75 | bool SkImage_Generator::getROPixels(SkBitmap* bitmap, SkColorSpace* dstColorSpace, |
Brian Osman | 7992da3 | 2016-11-18 11:28:24 -0500 | [diff] [blame] | 76 | CachingHint chint) const { |
Brian Osman | 61624f0 | 2016-12-09 14:51:59 -0500 | [diff] [blame] | 77 | return fCache.lockAsBitmap(bitmap, this, dstColorSpace, chint); |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Brian Salomon | 514baff | 2016-11-17 15:17:07 -0500 | [diff] [blame] | 80 | GrTexture* SkImage_Generator::asTextureRef(GrContext* ctx, const GrSamplerParams& params, |
Brian Osman | 61624f0 | 2016-12-09 14:51:59 -0500 | [diff] [blame] | 81 | SkColorSpace* dstColorSpace, |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 82 | sk_sp<SkColorSpace>* texColorSpace, |
| 83 | SkScalar scaleAdjust[2]) const { |
| 84 | return fCache.lockAsTexture(ctx, params, dstColorSpace, texColorSpace, this, scaleAdjust); |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 85 | } |
| 86 | |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 87 | sk_sp<SkImage> SkImage_Generator::onMakeSubset(const SkIRect& subset) const { |
fmalita | 8bd45cd | 2016-11-04 11:36:31 -0700 | [diff] [blame] | 88 | SkASSERT(fCache.info().bounds().contains(subset)); |
| 89 | SkASSERT(fCache.info().bounds() != subset); |
reed | 7b6945b | 2015-09-24 00:50:58 -0700 | [diff] [blame] | 90 | |
fmalita | 8bd45cd | 2016-11-04 11:36:31 -0700 | [diff] [blame] | 91 | const SkIRect generatorSubset = subset.makeOffset(fCache.fOrigin.x(), fCache.fOrigin.y()); |
| 92 | SkImageCacherator::Validator validator(fCache.fSharedGenerator, &generatorSubset); |
| 93 | return validator ? sk_sp<SkImage>(new SkImage_Generator(&validator)) : nullptr; |
reed | 7b6945b | 2015-09-24 00:50:58 -0700 | [diff] [blame] | 94 | } |
| 95 | |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 96 | sk_sp<SkImage> SkImage::MakeFromGenerator(SkImageGenerator* generator, const SkIRect* subset) { |
fmalita | 8bd45cd | 2016-11-04 11:36:31 -0700 | [diff] [blame] | 97 | SkImageCacherator::Validator validator(SkImageCacherator::SharedGenerator::Make(generator), |
| 98 | subset); |
fmalita | 7929e3a | 2016-10-27 08:15:44 -0700 | [diff] [blame] | 99 | |
| 100 | return validator ? sk_make_sp<SkImage_Generator>(&validator) : nullptr; |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 101 | } |