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 | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 24 | bool onReadPixels(const SkImageInfo&, void*, size_t, int srcX, int srcY) const override; |
| 25 | const void* onPeekPixels(SkImageInfo*, size_t* /*rowBytes*/) const override; |
| 26 | SkData* onRefEncoded() const override; |
| 27 | bool isOpaque() const override { return fCache->info().isOpaque(); } |
reed | 7b6945b | 2015-09-24 00:50:58 -0700 | [diff] [blame] | 28 | SkImage* onNewSubset(const SkIRect&) const override; |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 29 | bool getROPixels(SkBitmap*) 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, |
| 42 | int srcX, int srcY) const { |
| 43 | SkBitmap bm; |
| 44 | if (this->getROPixels(&bm)) { |
| 45 | return bm.readPixels(dstInfo, dstPixels, dstRB, srcX, srcY); |
| 46 | } |
| 47 | return false; |
| 48 | } |
| 49 | |
| 50 | const void* SkImage_Generator::onPeekPixels(SkImageInfo* infoPtr, size_t* rowBytesPtr) const { |
| 51 | return NULL; |
| 52 | } |
| 53 | |
| 54 | SkData* SkImage_Generator::onRefEncoded() const { |
| 55 | return fCache->refEncoded(); |
| 56 | } |
| 57 | |
| 58 | bool SkImage_Generator::getROPixels(SkBitmap* bitmap) const { |
fmalita | 3b0d532 | 2015-09-18 08:07:31 -0700 | [diff] [blame] | 59 | return fCache->lockAsBitmap(bitmap, this); |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 60 | } |
| 61 | |
bsalomon | afa95e2 | 2015-10-12 10:39:46 -0700 | [diff] [blame] | 62 | GrTexture* SkImage_Generator::asTextureRef(GrContext* ctx, const GrTextureParams& params) const { |
| 63 | return fCache->lockAsTexture(ctx, params, this); |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 64 | } |
| 65 | |
reed | 7b6945b | 2015-09-24 00:50:58 -0700 | [diff] [blame] | 66 | SkImage* SkImage_Generator::onNewSubset(const SkIRect& subset) const { |
| 67 | // TODO: make this lazy, by wrapping the subset inside a new generator or something |
| 68 | // For now, we do effectively what we did before, make it a raster |
| 69 | |
| 70 | const SkImageInfo info = SkImageInfo::MakeN32(subset.width(), subset.height(), |
| 71 | this->isOpaque() ? kOpaque_SkAlphaType : kPremul_SkAlphaType); |
| 72 | SkAutoTUnref<SkSurface> surface(SkSurface::NewRaster(info)); |
| 73 | if (!surface) { |
| 74 | return nullptr; |
| 75 | } |
| 76 | surface->getCanvas()->clear(0); |
| 77 | surface->getCanvas()->drawImage(this, SkIntToScalar(-subset.x()), SkIntToScalar(-subset.y()), |
| 78 | nullptr); |
| 79 | return surface->newImageSnapshot(); |
| 80 | } |
| 81 | |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 82 | SkImage* SkImage::NewFromGenerator(SkImageGenerator* generator, const SkIRect* subset) { |
| 83 | SkImageCacherator* cache = SkImageCacherator::NewFromGenerator(generator, subset); |
| 84 | if (!cache) { |
| 85 | return nullptr; |
| 86 | } |
mdempsky | 106b124 | 2015-09-22 06:10:35 -0700 | [diff] [blame] | 87 | return new SkImage_Generator(cache); |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 88 | } |