blob: 51fb281c26154c84947937460db34d9b9284caa9 [file] [log] [blame]
reed85d91782015-09-10 14:33:38 -07001/*
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"
reed85d91782015-09-10 14:33:38 -070014
15class SkImage_Generator : public SkImage_Base {
16public:
fmalita7929e3a2016-10-27 08:15:44 -070017 SkImage_Generator(SkImageCacherator::Validator* validator)
18 : INHERITED(validator->fInfo.width(), validator->fInfo.height(), validator->fUniqueID)
19 , fCache(validator)
reed85d91782015-09-10 14:33:38 -070020 {}
21
herba7c9d632016-04-19 12:30:22 -070022 virtual SkImageInfo onImageInfo() const override {
fmalita7929e3a2016-10-27 08:15:44 -070023 return fCache.info();
herba7c9d632016-04-19 12:30:22 -070024 }
brianosman69c166d2016-08-17 14:01:05 -070025 SkAlphaType onAlphaType() const override {
fmalita7929e3a2016-10-27 08:15:44 -070026 return fCache.info().alphaType();
brianosman69c166d2016-08-17 14:01:05 -070027 }
herba7c9d632016-04-19 12:30:22 -070028
Robert Phillipsb726d582017-03-09 16:36:32 -050029 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
fmalita7929e3a2016-10-27 08:15:44 -070036 SkImageCacherator* peekCacherator() const override { return &fCache; }
reed05dd2512016-01-05 09:16:19 -080037 SkData* onRefEncoded(GrContext*) const override;
reed7fb4f8b2016-03-11 04:33:52 -080038 sk_sp<SkImage> onMakeSubset(const SkIRect&) const override;
Brian Osman61624f02016-12-09 14:51:59 -050039 bool getROPixels(SkBitmap*, SkColorSpace* dstColorSpace, CachingHint) const override;
reed85d91782015-09-10 14:33:38 -070040 bool onIsLazyGenerated() const override { return true; }
Matt Sarett6de13102017-03-14 14:10:48 -040041 sk_sp<SkImage> onMakeColorSpace(sk_sp<SkColorSpace>) const override;
reed85d91782015-09-10 14:33:38 -070042
43private:
fmalita7929e3a2016-10-27 08:15:44 -070044 mutable SkImageCacherator fCache;
reed85d91782015-09-10 14:33:38 -070045
46 typedef SkImage_Base INHERITED;
47};
48
49///////////////////////////////////////////////////////////////////////////////
50
reed85d91782015-09-10 14:33:38 -070051bool SkImage_Generator::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRB,
reed09553032015-11-23 12:32:16 -080052 int srcX, int srcY, CachingHint chint) const {
Brian Osman61624f02016-12-09 14:51:59 -050053 SkColorSpace* dstColorSpace = dstInfo.colorSpace();
reed85d91782015-09-10 14:33:38 -070054 SkBitmap bm;
reed6868c3f2015-11-24 11:44:47 -080055 if (kDisallow_CachingHint == chint) {
Brian Osman61624f02016-12-09 14:51:59 -050056 SkImageCacherator::CachedFormat cacheFormat = fCache.chooseCacheFormat(dstColorSpace);
Brian Osman7992da32016-11-18 11:28:24 -050057 if (fCache.lockAsBitmapOnlyIfAlreadyCached(&bm, cacheFormat)) {
reed6868c3f2015-11-24 11:44:47 -080058 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 Sarettd531ca02017-03-24 16:31:19 -040063 if (fCache.directGeneratePixels(dstInfo, dstPixels, dstRB, srcX, srcY,
64 SkTransferFunctionBehavior::kRespect)) {
reed6868c3f2015-11-24 11:44:47 -080065 return true;
66 }
67 // else fall through
68 }
69 }
70
Brian Osman61624f02016-12-09 14:51:59 -050071 if (this->getROPixels(&bm, dstColorSpace, chint)) {
reed85d91782015-09-10 14:33:38 -070072 return bm.readPixels(dstInfo, dstPixels, dstRB, srcX, srcY);
73 }
74 return false;
75}
76
reed05dd2512016-01-05 09:16:19 -080077SkData* SkImage_Generator::onRefEncoded(GrContext* ctx) const {
fmalita7929e3a2016-10-27 08:15:44 -070078 return fCache.refEncoded(ctx);
reed85d91782015-09-10 14:33:38 -070079}
80
Brian Osman61624f02016-12-09 14:51:59 -050081bool SkImage_Generator::getROPixels(SkBitmap* bitmap, SkColorSpace* dstColorSpace,
Brian Osman7992da32016-11-18 11:28:24 -050082 CachingHint chint) const {
Robert Phillips4447b642017-03-03 11:10:18 -050083 return fCache.lockAsBitmap(nullptr, bitmap, this, dstColorSpace, chint);
reed85d91782015-09-10 14:33:38 -070084}
85
Robert Phillipsb726d582017-03-09 16:36:32 -050086#if SK_SUPPORT_GPU
87sk_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 Phillips4f358be2017-03-23 08:21:00 -040092 return fCache.lockAsTextureProxy(context, params, dstColorSpace,
93 texColorSpace, this, scaleAdjust);
Robert Phillipsb726d582017-03-09 16:36:32 -050094}
95#endif
96
reed7fb4f8b2016-03-11 04:33:52 -080097sk_sp<SkImage> SkImage_Generator::onMakeSubset(const SkIRect& subset) const {
fmalita8bd45cd2016-11-04 11:36:31 -070098 SkASSERT(fCache.info().bounds().contains(subset));
99 SkASSERT(fCache.info().bounds() != subset);
reed7b6945b2015-09-24 00:50:58 -0700100
fmalita8bd45cd2016-11-04 11:36:31 -0700101 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;
reed7b6945b2015-09-24 00:50:58 -0700104}
105
Matt Sarett6de13102017-03-14 14:10:48 -0400106sk_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 Sarettf86bc2c2017-03-24 16:35:22 -0400110 kGray_8_SkColorType == dstInfo.colorType() ||
111 kRGB_565_SkColorType == dstInfo.colorType()) {
Matt Sarett6de13102017-03-14 14:10:48 -0400112 dstInfo = dstInfo.makeColorType(kN32_SkColorType);
113 }
114 dst.allocPixels(dstInfo);
115
Matt Sarettd531ca02017-03-24 16:31:19 -0400116 // 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 Sarett6de13102017-03-14 14:10:48 -0400120 return nullptr;
121 }
122
123 dst.setImmutable();
124 return SkImage::MakeFromBitmap(dst);
125}
126
Mike Reed185130c2017-02-15 15:14:16 -0500127sk_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);
fmalita7929e3a2016-10-27 08:15:44 -0700131
132 return validator ? sk_make_sp<SkImage_Generator>(&validator) : nullptr;
reed85d91782015-09-10 14:33:38 -0700133}