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; |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 40 | bool onIsLazyGenerated() const override { return true; } |
Matt Sarett | 6de1310 | 2017-03-14 14:10:48 -0400 | [diff] [blame] | 41 | sk_sp<SkImage> onMakeColorSpace(sk_sp<SkColorSpace>) const override; |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 42 | |
| 43 | private: |
fmalita | 7929e3a | 2016-10-27 08:15:44 -0700 | [diff] [blame] | 44 | mutable SkImageCacherator fCache; |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 45 | |
| 46 | typedef SkImage_Base INHERITED; |
| 47 | }; |
| 48 | |
| 49 | /////////////////////////////////////////////////////////////////////////////// |
| 50 | |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 51 | bool SkImage_Generator::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRB, |
reed | 0955303 | 2015-11-23 12:32:16 -0800 | [diff] [blame] | 52 | int srcX, int srcY, CachingHint chint) const { |
Brian Osman | 61624f0 | 2016-12-09 14:51:59 -0500 | [diff] [blame] | 53 | SkColorSpace* dstColorSpace = dstInfo.colorSpace(); |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 54 | SkBitmap bm; |
reed | 6868c3f | 2015-11-24 11:44:47 -0800 | [diff] [blame] | 55 | if (kDisallow_CachingHint == chint) { |
Brian Osman | 61624f0 | 2016-12-09 14:51:59 -0500 | [diff] [blame] | 56 | SkImageCacherator::CachedFormat cacheFormat = fCache.chooseCacheFormat(dstColorSpace); |
Brian Osman | 7992da3 | 2016-11-18 11:28:24 -0500 | [diff] [blame] | 57 | if (fCache.lockAsBitmapOnlyIfAlreadyCached(&bm, cacheFormat)) { |
reed | 6868c3f | 2015-11-24 11:44:47 -0800 | [diff] [blame] | 58 | return bm.readPixels(dstInfo, dstPixels, dstRB, srcX, srcY); |
| 59 | } else { |
| 60 | // Try passing the caller's buffer directly down to the generator. If this fails we |
| 61 | // may still succeed in the general case, as the generator may prefer some other |
| 62 | // config, which we could then convert via SkBitmap::readPixels. |
Matt Sarett | d531ca0 | 2017-03-24 16:31:19 -0400 | [diff] [blame] | 63 | if (fCache.directGeneratePixels(dstInfo, dstPixels, dstRB, srcX, srcY, |
| 64 | SkTransferFunctionBehavior::kRespect)) { |
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 { |
Robert Phillips | 4f358be | 2017-03-23 08:21:00 -0400 | [diff] [blame] | 92 | return fCache.lockAsTextureProxy(context, params, dstColorSpace, |
| 93 | texColorSpace, this, scaleAdjust); |
Robert Phillips | b726d58 | 2017-03-09 16:36:32 -0500 | [diff] [blame] | 94 | } |
| 95 | #endif |
| 96 | |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 97 | sk_sp<SkImage> SkImage_Generator::onMakeSubset(const SkIRect& subset) const { |
fmalita | 8bd45cd | 2016-11-04 11:36:31 -0700 | [diff] [blame] | 98 | SkASSERT(fCache.info().bounds().contains(subset)); |
| 99 | SkASSERT(fCache.info().bounds() != subset); |
reed | 7b6945b | 2015-09-24 00:50:58 -0700 | [diff] [blame] | 100 | |
fmalita | 8bd45cd | 2016-11-04 11:36:31 -0700 | [diff] [blame] | 101 | const SkIRect generatorSubset = subset.makeOffset(fCache.fOrigin.x(), fCache.fOrigin.y()); |
| 102 | SkImageCacherator::Validator validator(fCache.fSharedGenerator, &generatorSubset); |
| 103 | return validator ? sk_sp<SkImage>(new SkImage_Generator(&validator)) : nullptr; |
reed | 7b6945b | 2015-09-24 00:50:58 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Matt Sarett | 6de1310 | 2017-03-14 14:10:48 -0400 | [diff] [blame] | 106 | sk_sp<SkImage> SkImage_Generator::onMakeColorSpace(sk_sp<SkColorSpace> target) const { |
| 107 | SkBitmap dst; |
| 108 | SkImageInfo dstInfo = fCache.info().makeColorSpace(target); |
| 109 | if (kIndex_8_SkColorType == dstInfo.colorType() || |
Matt Sarett | f86bc2c | 2017-03-24 16:35:22 -0400 | [diff] [blame] | 110 | kGray_8_SkColorType == dstInfo.colorType() || |
| 111 | kRGB_565_SkColorType == dstInfo.colorType()) { |
Matt Sarett | 6de1310 | 2017-03-14 14:10:48 -0400 | [diff] [blame] | 112 | dstInfo = dstInfo.makeColorType(kN32_SkColorType); |
| 113 | } |
| 114 | dst.allocPixels(dstInfo); |
| 115 | |
Matt Sarett | d531ca0 | 2017-03-24 16:31:19 -0400 | [diff] [blame] | 116 | // Use kIgnore for transfer function behavior. This is used by the SkColorSpaceXformCanvas, |
| 117 | // which wants to pre-xform the inputs but ignore the transfer function on blends. |
| 118 | if (!fCache.directGeneratePixels(dstInfo, dst.getPixels(), dst.rowBytes(), 0, 0, |
| 119 | SkTransferFunctionBehavior::kIgnore)) { |
Matt Sarett | 6de1310 | 2017-03-14 14:10:48 -0400 | [diff] [blame] | 120 | return nullptr; |
| 121 | } |
| 122 | |
| 123 | dst.setImmutable(); |
| 124 | return SkImage::MakeFromBitmap(dst); |
| 125 | } |
| 126 | |
Mike Reed | 185130c | 2017-02-15 15:14:16 -0500 | [diff] [blame] | 127 | sk_sp<SkImage> SkImage::MakeFromGenerator(std::unique_ptr<SkImageGenerator> generator, |
| 128 | const SkIRect* subset) { |
| 129 | SkImageCacherator::Validator validator( |
| 130 | SkImageCacherator::SharedGenerator::Make(std::move(generator)), subset); |
fmalita | 7929e3a | 2016-10-27 08:15:44 -0700 | [diff] [blame] | 131 | |
| 132 | return validator ? sk_make_sp<SkImage_Generator>(&validator) : nullptr; |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 133 | } |