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" |
reed | 7b6945b | 2015-09-24 00:50:58 -0700 | [diff] [blame] | 10 | #include "SkCanvas.h" |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 11 | #include "SkData.h" |
| 12 | #include "SkImageCacherator.h" |
| 13 | #include "SkImagePriv.h" |
| 14 | #include "SkPixelRef.h" |
| 15 | #include "SkSurface.h" |
| 16 | |
| 17 | class SkImage_Generator : public SkImage_Base { |
| 18 | public: |
| 19 | SkImage_Generator(SkImageCacherator* cache) |
reed | af3fbfc | 2015-10-04 11:28:36 -0700 | [diff] [blame] | 20 | : INHERITED(cache->info().width(), cache->info().height(), cache->uniqueID()) |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 21 | , fCache(cache) // take ownership |
| 22 | {} |
| 23 | |
reed | 0955303 | 2015-11-23 12:32:16 -0800 | [diff] [blame] | 24 | bool onReadPixels(const SkImageInfo&, void*, size_t, int srcX, int srcY, CachingHint) const override; |
bsalomon | 1cf6f9b | 2015-12-08 10:53:43 -0800 | [diff] [blame] | 25 | SkImageCacherator* peekCacherator() const override { return fCache; } |
reed | 05dd251 | 2016-01-05 09:16:19 -0800 | [diff] [blame] | 26 | SkData* onRefEncoded(GrContext*) const override; |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 27 | bool isOpaque() const override { return fCache->info().isOpaque(); } |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 28 | sk_sp<SkImage> onMakeSubset(const SkIRect&) const override; |
reed | 0955303 | 2015-11-23 12:32:16 -0800 | [diff] [blame] | 29 | bool getROPixels(SkBitmap*, CachingHint) const override; |
bsalomon | afa95e2 | 2015-10-12 10:39:46 -0700 | [diff] [blame] | 30 | GrTexture* asTextureRef(GrContext*, const GrTextureParams&) const override; |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 31 | bool onIsLazyGenerated() const override { return true; } |
| 32 | |
| 33 | private: |
| 34 | SkAutoTDelete<SkImageCacherator> fCache; |
| 35 | |
| 36 | typedef SkImage_Base INHERITED; |
| 37 | }; |
| 38 | |
| 39 | /////////////////////////////////////////////////////////////////////////////// |
| 40 | |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 41 | bool SkImage_Generator::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRB, |
reed | 0955303 | 2015-11-23 12:32:16 -0800 | [diff] [blame] | 42 | int srcX, int srcY, CachingHint chint) const { |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 43 | SkBitmap bm; |
reed | 6868c3f | 2015-11-24 11:44:47 -0800 | [diff] [blame] | 44 | if (kDisallow_CachingHint == chint) { |
| 45 | if (fCache->lockAsBitmapOnlyIfAlreadyCached(&bm)) { |
| 46 | return bm.readPixels(dstInfo, dstPixels, dstRB, srcX, srcY); |
| 47 | } else { |
| 48 | // Try passing the caller's buffer directly down to the generator. If this fails we |
| 49 | // may still succeed in the general case, as the generator may prefer some other |
| 50 | // config, which we could then convert via SkBitmap::readPixels. |
| 51 | if (fCache->directGeneratePixels(dstInfo, dstPixels, dstRB, srcX, srcY)) { |
| 52 | return true; |
| 53 | } |
| 54 | // else fall through |
| 55 | } |
| 56 | } |
| 57 | |
reed | 0955303 | 2015-11-23 12:32:16 -0800 | [diff] [blame] | 58 | if (this->getROPixels(&bm, chint)) { |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 59 | return bm.readPixels(dstInfo, dstPixels, dstRB, srcX, srcY); |
| 60 | } |
| 61 | return false; |
| 62 | } |
| 63 | |
reed | 05dd251 | 2016-01-05 09:16:19 -0800 | [diff] [blame] | 64 | SkData* SkImage_Generator::onRefEncoded(GrContext* ctx) const { |
| 65 | return fCache->refEncoded(ctx); |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 66 | } |
| 67 | |
reed | 0955303 | 2015-11-23 12:32:16 -0800 | [diff] [blame] | 68 | bool SkImage_Generator::getROPixels(SkBitmap* bitmap, CachingHint chint) const { |
| 69 | return fCache->lockAsBitmap(bitmap, this, chint); |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 70 | } |
| 71 | |
bsalomon | afa95e2 | 2015-10-12 10:39:46 -0700 | [diff] [blame] | 72 | GrTexture* SkImage_Generator::asTextureRef(GrContext* ctx, const GrTextureParams& params) const { |
| 73 | return fCache->lockAsTexture(ctx, params, this); |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 74 | } |
| 75 | |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 76 | sk_sp<SkImage> SkImage_Generator::onMakeSubset(const SkIRect& subset) const { |
reed | 7b6945b | 2015-09-24 00:50:58 -0700 | [diff] [blame] | 77 | // TODO: make this lazy, by wrapping the subset inside a new generator or something |
| 78 | // For now, we do effectively what we did before, make it a raster |
| 79 | |
| 80 | const SkImageInfo info = SkImageInfo::MakeN32(subset.width(), subset.height(), |
| 81 | this->isOpaque() ? kOpaque_SkAlphaType : kPremul_SkAlphaType); |
| 82 | SkAutoTUnref<SkSurface> surface(SkSurface::NewRaster(info)); |
| 83 | if (!surface) { |
| 84 | return nullptr; |
| 85 | } |
| 86 | surface->getCanvas()->clear(0); |
| 87 | surface->getCanvas()->drawImage(this, SkIntToScalar(-subset.x()), SkIntToScalar(-subset.y()), |
| 88 | nullptr); |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 89 | return surface->makeImageSnapshot(); |
reed | 7b6945b | 2015-09-24 00:50:58 -0700 | [diff] [blame] | 90 | } |
| 91 | |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 92 | sk_sp<SkImage> SkImage::MakeFromGenerator(SkImageGenerator* generator, const SkIRect* subset) { |
| 93 | if (!generator) { |
| 94 | return nullptr; |
| 95 | } |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 96 | SkImageCacherator* cache = SkImageCacherator::NewFromGenerator(generator, subset); |
| 97 | if (!cache) { |
| 98 | return nullptr; |
| 99 | } |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 100 | return sk_make_sp<SkImage_Generator>(cache); |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 101 | } |