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 | |
Robert Phillips | b726d58 | 2017-03-09 16:36:32 -0500 | [diff] [blame^] | 29 | bool onReadPixels(const SkImageInfo&, void*, size_t, int srcX, int srcY, |
| 30 | CachingHint) const override; |
| 31 | #if SK_SUPPORT_GPU |
| 32 | sk_sp<GrTextureProxy> asTextureProxyRef(GrContext*, const GrSamplerParams&, |
| 33 | SkColorSpace*, sk_sp<SkColorSpace>*, |
| 34 | SkScalar scaleAdjust[2]) const override; |
| 35 | #endif |
fmalita | 7929e3a | 2016-10-27 08:15:44 -0700 | [diff] [blame] | 36 | SkImageCacherator* peekCacherator() const override { return &fCache; } |
reed | 05dd251 | 2016-01-05 09:16:19 -0800 | [diff] [blame] | 37 | SkData* onRefEncoded(GrContext*) const override; |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 38 | sk_sp<SkImage> onMakeSubset(const SkIRect&) const override; |
Brian Osman | 61624f0 | 2016-12-09 14:51:59 -0500 | [diff] [blame] | 39 | bool getROPixels(SkBitmap*, SkColorSpace* dstColorSpace, CachingHint) const override; |
| 40 | GrTexture* asTextureRef(GrContext*, const GrSamplerParams&, SkColorSpace*, |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 41 | sk_sp<SkColorSpace>*, SkScalar scaleAdjust[2]) const override; |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 42 | bool onIsLazyGenerated() const override { return true; } |
| 43 | |
| 44 | private: |
fmalita | 7929e3a | 2016-10-27 08:15:44 -0700 | [diff] [blame] | 45 | mutable SkImageCacherator fCache; |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 46 | |
| 47 | typedef SkImage_Base INHERITED; |
| 48 | }; |
| 49 | |
| 50 | /////////////////////////////////////////////////////////////////////////////// |
| 51 | |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 52 | bool SkImage_Generator::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRB, |
reed | 0955303 | 2015-11-23 12:32:16 -0800 | [diff] [blame] | 53 | int srcX, int srcY, CachingHint chint) const { |
Brian Osman | 61624f0 | 2016-12-09 14:51:59 -0500 | [diff] [blame] | 54 | SkColorSpace* dstColorSpace = dstInfo.colorSpace(); |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 55 | SkBitmap bm; |
reed | 6868c3f | 2015-11-24 11:44:47 -0800 | [diff] [blame] | 56 | if (kDisallow_CachingHint == chint) { |
Brian Osman | 61624f0 | 2016-12-09 14:51:59 -0500 | [diff] [blame] | 57 | SkImageCacherator::CachedFormat cacheFormat = fCache.chooseCacheFormat(dstColorSpace); |
Brian Osman | 7992da3 | 2016-11-18 11:28:24 -0500 | [diff] [blame] | 58 | if (fCache.lockAsBitmapOnlyIfAlreadyCached(&bm, cacheFormat)) { |
reed | 6868c3f | 2015-11-24 11:44:47 -0800 | [diff] [blame] | 59 | return bm.readPixels(dstInfo, dstPixels, dstRB, srcX, srcY); |
| 60 | } else { |
| 61 | // Try passing the caller's buffer directly down to the generator. If this fails we |
| 62 | // may still succeed in the general case, as the generator may prefer some other |
| 63 | // config, which we could then convert via SkBitmap::readPixels. |
fmalita | 7929e3a | 2016-10-27 08:15:44 -0700 | [diff] [blame] | 64 | if (fCache.directGeneratePixels(dstInfo, dstPixels, dstRB, srcX, srcY)) { |
reed | 6868c3f | 2015-11-24 11:44:47 -0800 | [diff] [blame] | 65 | return true; |
| 66 | } |
| 67 | // else fall through |
| 68 | } |
| 69 | } |
| 70 | |
Brian Osman | 61624f0 | 2016-12-09 14:51:59 -0500 | [diff] [blame] | 71 | if (this->getROPixels(&bm, dstColorSpace, chint)) { |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 72 | return bm.readPixels(dstInfo, dstPixels, dstRB, srcX, srcY); |
| 73 | } |
| 74 | return false; |
| 75 | } |
| 76 | |
reed | 05dd251 | 2016-01-05 09:16:19 -0800 | [diff] [blame] | 77 | SkData* SkImage_Generator::onRefEncoded(GrContext* ctx) const { |
fmalita | 7929e3a | 2016-10-27 08:15:44 -0700 | [diff] [blame] | 78 | return fCache.refEncoded(ctx); |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 79 | } |
| 80 | |
Brian Osman | 61624f0 | 2016-12-09 14:51:59 -0500 | [diff] [blame] | 81 | bool SkImage_Generator::getROPixels(SkBitmap* bitmap, SkColorSpace* dstColorSpace, |
Brian Osman | 7992da3 | 2016-11-18 11:28:24 -0500 | [diff] [blame] | 82 | CachingHint chint) const { |
Robert Phillips | 4447b64 | 2017-03-03 11:10:18 -0500 | [diff] [blame] | 83 | return fCache.lockAsBitmap(nullptr, bitmap, this, dstColorSpace, chint); |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Robert Phillips | b726d58 | 2017-03-09 16:36:32 -0500 | [diff] [blame^] | 86 | #if SK_SUPPORT_GPU |
| 87 | sk_sp<GrTextureProxy> SkImage_Generator::asTextureProxyRef(GrContext* context, |
| 88 | const GrSamplerParams& params, |
| 89 | SkColorSpace* dstColorSpace, |
| 90 | sk_sp<SkColorSpace>* texColorSpace, |
| 91 | SkScalar scaleAdjust[2]) const { |
| 92 | sk_sp<GrTexture> tex(fCache.lockAsTexture(context, params, dstColorSpace, |
| 93 | texColorSpace, this, scaleAdjust)); |
| 94 | if (!tex) { |
| 95 | return nullptr; |
| 96 | } |
| 97 | |
| 98 | return GrSurfaceProxy::MakeWrapped(std::move(tex)); |
| 99 | } |
| 100 | #endif |
| 101 | |
Brian Salomon | 514baff | 2016-11-17 15:17:07 -0500 | [diff] [blame] | 102 | GrTexture* SkImage_Generator::asTextureRef(GrContext* ctx, const GrSamplerParams& params, |
Brian Osman | 61624f0 | 2016-12-09 14:51:59 -0500 | [diff] [blame] | 103 | SkColorSpace* dstColorSpace, |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 104 | sk_sp<SkColorSpace>* texColorSpace, |
| 105 | SkScalar scaleAdjust[2]) const { |
| 106 | return fCache.lockAsTexture(ctx, params, dstColorSpace, texColorSpace, this, scaleAdjust); |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 107 | } |
| 108 | |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 109 | sk_sp<SkImage> SkImage_Generator::onMakeSubset(const SkIRect& subset) const { |
fmalita | 8bd45cd | 2016-11-04 11:36:31 -0700 | [diff] [blame] | 110 | SkASSERT(fCache.info().bounds().contains(subset)); |
| 111 | SkASSERT(fCache.info().bounds() != subset); |
reed | 7b6945b | 2015-09-24 00:50:58 -0700 | [diff] [blame] | 112 | |
fmalita | 8bd45cd | 2016-11-04 11:36:31 -0700 | [diff] [blame] | 113 | const SkIRect generatorSubset = subset.makeOffset(fCache.fOrigin.x(), fCache.fOrigin.y()); |
| 114 | SkImageCacherator::Validator validator(fCache.fSharedGenerator, &generatorSubset); |
| 115 | return validator ? sk_sp<SkImage>(new SkImage_Generator(&validator)) : nullptr; |
reed | 7b6945b | 2015-09-24 00:50:58 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Mike Reed | 185130c | 2017-02-15 15:14:16 -0500 | [diff] [blame] | 118 | sk_sp<SkImage> SkImage::MakeFromGenerator(std::unique_ptr<SkImageGenerator> generator, |
| 119 | const SkIRect* subset) { |
| 120 | SkImageCacherator::Validator validator( |
| 121 | SkImageCacherator::SharedGenerator::Make(std::move(generator)), subset); |
fmalita | 7929e3a | 2016-10-27 08:15:44 -0700 | [diff] [blame] | 122 | |
| 123 | return validator ? sk_make_sp<SkImage_Generator>(&validator) : nullptr; |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 124 | } |