blob: fb807679993a503bea8d423687c93c1f1e649f7a [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;
40 GrTexture* asTextureRef(GrContext*, const GrSamplerParams&, SkColorSpace*,
Robert Phillips67c18d62017-01-20 12:44:06 -050041 sk_sp<SkColorSpace>*, SkScalar scaleAdjust[2]) const override;
reed85d91782015-09-10 14:33:38 -070042 bool onIsLazyGenerated() const override { return true; }
Matt Sarett6de13102017-03-14 14:10:48 -040043 sk_sp<SkImage> onMakeColorSpace(sk_sp<SkColorSpace>) const override;
reed85d91782015-09-10 14:33:38 -070044
45private:
fmalita7929e3a2016-10-27 08:15:44 -070046 mutable SkImageCacherator fCache;
reed85d91782015-09-10 14:33:38 -070047
48 typedef SkImage_Base INHERITED;
49};
50
51///////////////////////////////////////////////////////////////////////////////
52
reed85d91782015-09-10 14:33:38 -070053bool SkImage_Generator::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRB,
reed09553032015-11-23 12:32:16 -080054 int srcX, int srcY, CachingHint chint) const {
Brian Osman61624f02016-12-09 14:51:59 -050055 SkColorSpace* dstColorSpace = dstInfo.colorSpace();
reed85d91782015-09-10 14:33:38 -070056 SkBitmap bm;
reed6868c3f2015-11-24 11:44:47 -080057 if (kDisallow_CachingHint == chint) {
Brian Osman61624f02016-12-09 14:51:59 -050058 SkImageCacherator::CachedFormat cacheFormat = fCache.chooseCacheFormat(dstColorSpace);
Brian Osman7992da32016-11-18 11:28:24 -050059 if (fCache.lockAsBitmapOnlyIfAlreadyCached(&bm, cacheFormat)) {
reed6868c3f2015-11-24 11:44:47 -080060 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.
fmalita7929e3a2016-10-27 08:15:44 -070065 if (fCache.directGeneratePixels(dstInfo, dstPixels, dstRB, srcX, srcY)) {
reed6868c3f2015-11-24 11:44:47 -080066 return true;
67 }
68 // else fall through
69 }
70 }
71
Brian Osman61624f02016-12-09 14:51:59 -050072 if (this->getROPixels(&bm, dstColorSpace, chint)) {
reed85d91782015-09-10 14:33:38 -070073 return bm.readPixels(dstInfo, dstPixels, dstRB, srcX, srcY);
74 }
75 return false;
76}
77
reed05dd2512016-01-05 09:16:19 -080078SkData* SkImage_Generator::onRefEncoded(GrContext* ctx) const {
fmalita7929e3a2016-10-27 08:15:44 -070079 return fCache.refEncoded(ctx);
reed85d91782015-09-10 14:33:38 -070080}
81
Brian Osman61624f02016-12-09 14:51:59 -050082bool SkImage_Generator::getROPixels(SkBitmap* bitmap, SkColorSpace* dstColorSpace,
Brian Osman7992da32016-11-18 11:28:24 -050083 CachingHint chint) const {
Robert Phillips4447b642017-03-03 11:10:18 -050084 return fCache.lockAsBitmap(nullptr, bitmap, this, dstColorSpace, chint);
reed85d91782015-09-10 14:33:38 -070085}
86
Robert Phillipsb726d582017-03-09 16:36:32 -050087#if SK_SUPPORT_GPU
88sk_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 Salomon514baff2016-11-17 15:17:07 -0500103GrTexture* SkImage_Generator::asTextureRef(GrContext* ctx, const GrSamplerParams& params,
Brian Osman61624f02016-12-09 14:51:59 -0500104 SkColorSpace* dstColorSpace,
Robert Phillips67c18d62017-01-20 12:44:06 -0500105 sk_sp<SkColorSpace>* texColorSpace,
106 SkScalar scaleAdjust[2]) const {
107 return fCache.lockAsTexture(ctx, params, dstColorSpace, texColorSpace, this, scaleAdjust);
reed85d91782015-09-10 14:33:38 -0700108}
109
reed7fb4f8b2016-03-11 04:33:52 -0800110sk_sp<SkImage> SkImage_Generator::onMakeSubset(const SkIRect& subset) const {
fmalita8bd45cd2016-11-04 11:36:31 -0700111 SkASSERT(fCache.info().bounds().contains(subset));
112 SkASSERT(fCache.info().bounds() != subset);
reed7b6945b2015-09-24 00:50:58 -0700113
fmalita8bd45cd2016-11-04 11:36:31 -0700114 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;
reed7b6945b2015-09-24 00:50:58 -0700117}
118
Matt Sarett6de13102017-03-14 14:10:48 -0400119sk_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 Reed185130c2017-02-15 15:14:16 -0500136sk_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);
fmalita7929e3a2016-10-27 08:15:44 -0700140
141 return validator ? sk_make_sp<SkImage_Generator>(&validator) : nullptr;
reed85d91782015-09-10 14:33:38 -0700142}