blob: 20f4863d82f2a2839902cc7bf993af8a46014747 [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"
reed7b6945b2015-09-24 00:50:58 -070010#include "SkCanvas.h"
reed85d91782015-09-10 14:33:38 -070011#include "SkData.h"
12#include "SkImageCacherator.h"
13#include "SkImagePriv.h"
14#include "SkPixelRef.h"
15#include "SkSurface.h"
16
17class SkImage_Generator : public SkImage_Base {
18public:
19 SkImage_Generator(SkImageCacherator* cache)
reedaf3fbfc2015-10-04 11:28:36 -070020 : INHERITED(cache->info().width(), cache->info().height(), cache->uniqueID())
reed85d91782015-09-10 14:33:38 -070021 , fCache(cache) // take ownership
22 {}
23
herba7c9d632016-04-19 12:30:22 -070024 virtual SkImageInfo onImageInfo() const override {
25 return fCache->info();
26 }
27
reed09553032015-11-23 12:32:16 -080028 bool onReadPixels(const SkImageInfo&, void*, size_t, int srcX, int srcY, CachingHint) const override;
bsalomon1cf6f9b2015-12-08 10:53:43 -080029 SkImageCacherator* peekCacherator() const override { return fCache; }
reed05dd2512016-01-05 09:16:19 -080030 SkData* onRefEncoded(GrContext*) const override;
reed85d91782015-09-10 14:33:38 -070031 bool isOpaque() const override { return fCache->info().isOpaque(); }
reed7fb4f8b2016-03-11 04:33:52 -080032 sk_sp<SkImage> onMakeSubset(const SkIRect&) const override;
reed09553032015-11-23 12:32:16 -080033 bool getROPixels(SkBitmap*, CachingHint) const override;
brianosman982eb7f2016-06-06 13:10:58 -070034 GrTexture* asTextureRef(GrContext*, const GrTextureParams&,
35 SkSourceGammaTreatment) const override;
reed85d91782015-09-10 14:33:38 -070036 bool onIsLazyGenerated() const override { return true; }
37
38private:
39 SkAutoTDelete<SkImageCacherator> fCache;
40
41 typedef SkImage_Base INHERITED;
42};
43
44///////////////////////////////////////////////////////////////////////////////
45
reed85d91782015-09-10 14:33:38 -070046bool SkImage_Generator::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRB,
reed09553032015-11-23 12:32:16 -080047 int srcX, int srcY, CachingHint chint) const {
reed85d91782015-09-10 14:33:38 -070048 SkBitmap bm;
reed6868c3f2015-11-24 11:44:47 -080049 if (kDisallow_CachingHint == chint) {
50 if (fCache->lockAsBitmapOnlyIfAlreadyCached(&bm)) {
51 return bm.readPixels(dstInfo, dstPixels, dstRB, srcX, srcY);
52 } else {
53 // Try passing the caller's buffer directly down to the generator. If this fails we
54 // may still succeed in the general case, as the generator may prefer some other
55 // config, which we could then convert via SkBitmap::readPixels.
56 if (fCache->directGeneratePixels(dstInfo, dstPixels, dstRB, srcX, srcY)) {
57 return true;
58 }
59 // else fall through
60 }
61 }
62
reed09553032015-11-23 12:32:16 -080063 if (this->getROPixels(&bm, chint)) {
reed85d91782015-09-10 14:33:38 -070064 return bm.readPixels(dstInfo, dstPixels, dstRB, srcX, srcY);
65 }
66 return false;
67}
68
reed05dd2512016-01-05 09:16:19 -080069SkData* SkImage_Generator::onRefEncoded(GrContext* ctx) const {
70 return fCache->refEncoded(ctx);
reed85d91782015-09-10 14:33:38 -070071}
72
reed09553032015-11-23 12:32:16 -080073bool SkImage_Generator::getROPixels(SkBitmap* bitmap, CachingHint chint) const {
74 return fCache->lockAsBitmap(bitmap, this, chint);
reed85d91782015-09-10 14:33:38 -070075}
76
brianosman982eb7f2016-06-06 13:10:58 -070077GrTexture* SkImage_Generator::asTextureRef(GrContext* ctx, const GrTextureParams& params,
78 SkSourceGammaTreatment gammaTreatment) const {
79 return fCache->lockAsTexture(ctx, params, gammaTreatment, this);
reed85d91782015-09-10 14:33:38 -070080}
81
reed7fb4f8b2016-03-11 04:33:52 -080082sk_sp<SkImage> SkImage_Generator::onMakeSubset(const SkIRect& subset) const {
reed7b6945b2015-09-24 00:50:58 -070083 // TODO: make this lazy, by wrapping the subset inside a new generator or something
84 // For now, we do effectively what we did before, make it a raster
85
86 const SkImageInfo info = SkImageInfo::MakeN32(subset.width(), subset.height(),
87 this->isOpaque() ? kOpaque_SkAlphaType : kPremul_SkAlphaType);
reede8f30622016-03-23 18:59:25 -070088 auto surface(SkSurface::MakeRaster(info));
reed7b6945b2015-09-24 00:50:58 -070089 if (!surface) {
90 return nullptr;
91 }
92 surface->getCanvas()->clear(0);
93 surface->getCanvas()->drawImage(this, SkIntToScalar(-subset.x()), SkIntToScalar(-subset.y()),
94 nullptr);
reed9ce9d672016-03-17 10:51:11 -070095 return surface->makeImageSnapshot();
reed7b6945b2015-09-24 00:50:58 -070096}
97
reed7fb4f8b2016-03-11 04:33:52 -080098sk_sp<SkImage> SkImage::MakeFromGenerator(SkImageGenerator* generator, const SkIRect* subset) {
99 if (!generator) {
100 return nullptr;
101 }
reed85d91782015-09-10 14:33:38 -0700102 SkImageCacherator* cache = SkImageCacherator::NewFromGenerator(generator, subset);
103 if (!cache) {
104 return nullptr;
105 }
reed7fb4f8b2016-03-11 04:33:52 -0800106 return sk_make_sp<SkImage_Generator>(cache);
reed85d91782015-09-10 14:33:38 -0700107}