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; |
reed | 0955303 | 2015-11-23 12:32:16 -0800 | [diff] [blame] | 33 | bool getROPixels(SkBitmap*, CachingHint) const override; |
brianosman | 982eb7f | 2016-06-06 13:10:58 -0700 | [diff] [blame] | 34 | GrTexture* asTextureRef(GrContext*, const GrTextureParams&, |
| 35 | SkSourceGammaTreatment) 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 { |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 48 | SkBitmap bm; |
reed | 6868c3f | 2015-11-24 11:44:47 -0800 | [diff] [blame] | 49 | if (kDisallow_CachingHint == chint) { |
fmalita | 7929e3a | 2016-10-27 08:15:44 -0700 | [diff] [blame] | 50 | if (fCache.lockAsBitmapOnlyIfAlreadyCached(&bm)) { |
reed | 6868c3f | 2015-11-24 11:44:47 -0800 | [diff] [blame] | 51 | return bm.readPixels(dstInfo, dstPixels, dstRB, srcX, srcY); |
| 52 | } else { |
| 53 | // Try passing the caller's buffer directly down to the generator. If this fails we |
| 54 | // may still succeed in the general case, as the generator may prefer some other |
| 55 | // config, which we could then convert via SkBitmap::readPixels. |
fmalita | 7929e3a | 2016-10-27 08:15:44 -0700 | [diff] [blame] | 56 | if (fCache.directGeneratePixels(dstInfo, dstPixels, dstRB, srcX, srcY)) { |
reed | 6868c3f | 2015-11-24 11:44:47 -0800 | [diff] [blame] | 57 | return true; |
| 58 | } |
| 59 | // else fall through |
| 60 | } |
| 61 | } |
| 62 | |
reed | 0955303 | 2015-11-23 12:32:16 -0800 | [diff] [blame] | 63 | if (this->getROPixels(&bm, chint)) { |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 64 | return bm.readPixels(dstInfo, dstPixels, dstRB, srcX, srcY); |
| 65 | } |
| 66 | return false; |
| 67 | } |
| 68 | |
reed | 05dd251 | 2016-01-05 09:16:19 -0800 | [diff] [blame] | 69 | SkData* SkImage_Generator::onRefEncoded(GrContext* ctx) const { |
fmalita | 7929e3a | 2016-10-27 08:15:44 -0700 | [diff] [blame] | 70 | return fCache.refEncoded(ctx); |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 71 | } |
| 72 | |
reed | 0955303 | 2015-11-23 12:32:16 -0800 | [diff] [blame] | 73 | bool SkImage_Generator::getROPixels(SkBitmap* bitmap, CachingHint chint) const { |
fmalita | 7929e3a | 2016-10-27 08:15:44 -0700 | [diff] [blame] | 74 | return fCache.lockAsBitmap(bitmap, this, chint); |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 75 | } |
| 76 | |
brianosman | 982eb7f | 2016-06-06 13:10:58 -0700 | [diff] [blame] | 77 | GrTexture* SkImage_Generator::asTextureRef(GrContext* ctx, const GrTextureParams& params, |
| 78 | SkSourceGammaTreatment gammaTreatment) const { |
fmalita | 7929e3a | 2016-10-27 08:15:44 -0700 | [diff] [blame] | 79 | return fCache.lockAsTexture(ctx, params, gammaTreatment, this); |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 80 | } |
| 81 | |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 82 | sk_sp<SkImage> SkImage_Generator::onMakeSubset(const SkIRect& subset) const { |
fmalita | 8bd45cd | 2016-11-04 11:36:31 -0700 | [diff] [blame] | 83 | SkASSERT(fCache.info().bounds().contains(subset)); |
| 84 | SkASSERT(fCache.info().bounds() != subset); |
reed | 7b6945b | 2015-09-24 00:50:58 -0700 | [diff] [blame] | 85 | |
fmalita | 8bd45cd | 2016-11-04 11:36:31 -0700 | [diff] [blame] | 86 | const SkIRect generatorSubset = subset.makeOffset(fCache.fOrigin.x(), fCache.fOrigin.y()); |
| 87 | SkImageCacherator::Validator validator(fCache.fSharedGenerator, &generatorSubset); |
| 88 | return validator ? sk_sp<SkImage>(new SkImage_Generator(&validator)) : nullptr; |
reed | 7b6945b | 2015-09-24 00:50:58 -0700 | [diff] [blame] | 89 | } |
| 90 | |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 91 | sk_sp<SkImage> SkImage::MakeFromGenerator(SkImageGenerator* generator, const SkIRect* subset) { |
fmalita | 8bd45cd | 2016-11-04 11:36:31 -0700 | [diff] [blame] | 92 | SkImageCacherator::Validator validator(SkImageCacherator::SharedGenerator::Make(generator), |
| 93 | subset); |
fmalita | 7929e3a | 2016-10-27 08:15:44 -0700 | [diff] [blame] | 94 | |
| 95 | return validator ? sk_make_sp<SkImage_Generator>(&validator) : nullptr; |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 96 | } |