blob: ae3f9f83f41e504430dae988d0120851cf645317 [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; }
43
44private:
fmalita7929e3a2016-10-27 08:15:44 -070045 mutable SkImageCacherator fCache;
reed85d91782015-09-10 14:33:38 -070046
47 typedef SkImage_Base INHERITED;
48};
49
50///////////////////////////////////////////////////////////////////////////////
51
reed85d91782015-09-10 14:33:38 -070052bool SkImage_Generator::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRB,
reed09553032015-11-23 12:32:16 -080053 int srcX, int srcY, CachingHint chint) const {
Brian Osman61624f02016-12-09 14:51:59 -050054 SkColorSpace* dstColorSpace = dstInfo.colorSpace();
reed85d91782015-09-10 14:33:38 -070055 SkBitmap bm;
reed6868c3f2015-11-24 11:44:47 -080056 if (kDisallow_CachingHint == chint) {
Brian Osman61624f02016-12-09 14:51:59 -050057 SkImageCacherator::CachedFormat cacheFormat = fCache.chooseCacheFormat(dstColorSpace);
Brian Osman7992da32016-11-18 11:28:24 -050058 if (fCache.lockAsBitmapOnlyIfAlreadyCached(&bm, cacheFormat)) {
reed6868c3f2015-11-24 11:44:47 -080059 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.
fmalita7929e3a2016-10-27 08:15:44 -070064 if (fCache.directGeneratePixels(dstInfo, dstPixels, dstRB, srcX, srcY)) {
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 {
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 Salomon514baff2016-11-17 15:17:07 -0500102GrTexture* SkImage_Generator::asTextureRef(GrContext* ctx, const GrSamplerParams& params,
Brian Osman61624f02016-12-09 14:51:59 -0500103 SkColorSpace* dstColorSpace,
Robert Phillips67c18d62017-01-20 12:44:06 -0500104 sk_sp<SkColorSpace>* texColorSpace,
105 SkScalar scaleAdjust[2]) const {
106 return fCache.lockAsTexture(ctx, params, dstColorSpace, texColorSpace, this, scaleAdjust);
reed85d91782015-09-10 14:33:38 -0700107}
108
reed7fb4f8b2016-03-11 04:33:52 -0800109sk_sp<SkImage> SkImage_Generator::onMakeSubset(const SkIRect& subset) const {
fmalita8bd45cd2016-11-04 11:36:31 -0700110 SkASSERT(fCache.info().bounds().contains(subset));
111 SkASSERT(fCache.info().bounds() != subset);
reed7b6945b2015-09-24 00:50:58 -0700112
fmalita8bd45cd2016-11-04 11:36:31 -0700113 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;
reed7b6945b2015-09-24 00:50:58 -0700116}
117
Mike Reed185130c2017-02-15 15:14:16 -0500118sk_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);
fmalita7929e3a2016-10-27 08:15:44 -0700122
123 return validator ? sk_make_sp<SkImage_Generator>(&validator) : nullptr;
reed85d91782015-09-10 14:33:38 -0700124}