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