OLD | NEW |
(Empty) | |
| 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" |
| 10 #include "SkData.h" |
| 11 #include "SkImageCacherator.h" |
| 12 #include "SkImagePriv.h" |
| 13 #include "SkPixelRef.h" |
| 14 #include "SkSurface.h" |
| 15 |
| 16 class SkImage_Generator : public SkImage_Base { |
| 17 public: |
| 18 SkImage_Generator(SkImageCacherator* cache) |
| 19 : INHERITED(cache->info().width(), cache->info().height(), kNeedNewImage
UniqueID, NULL) |
| 20 , fCache(cache) // take ownership |
| 21 {} |
| 22 |
| 23 SkSurface* onNewSurface(const SkImageInfo&, const SkSurfaceProps&) const ove
rride; |
| 24 bool onReadPixels(const SkImageInfo&, void*, size_t, int srcX, int srcY) con
st 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(); } |
| 28 |
| 29 bool getROPixels(SkBitmap*) const override; |
| 30 GrTexture* asTextureRef(GrContext*, SkImageUsageType) const override; |
| 31 |
| 32 SkShader* onNewShader(SkShader::TileMode, |
| 33 SkShader::TileMode, |
| 34 const SkMatrix* localMatrix) const override; |
| 35 |
| 36 bool onIsLazyGenerated() const override { return true; } |
| 37 |
| 38 private: |
| 39 SkAutoTDelete<SkImageCacherator> fCache; |
| 40 |
| 41 typedef SkImage_Base INHERITED; |
| 42 }; |
| 43 |
| 44 /////////////////////////////////////////////////////////////////////////////// |
| 45 |
| 46 SkShader* SkImage_Generator::onNewShader(SkShader::TileMode tileX, SkShader::Til
eMode tileY, |
| 47 const SkMatrix* localMatrix) const { |
| 48 // TODO: need a native Shader that takes Cacherator (or this image) so we ca
n natively return |
| 49 // textures as output from the shader. |
| 50 SkBitmap bm; |
| 51 if (this->getROPixels(&bm)) { |
| 52 return SkShader::CreateBitmapShader(bm, tileX, tileY, localMatrix); |
| 53 } |
| 54 return nullptr; |
| 55 } |
| 56 |
| 57 SkSurface* SkImage_Generator::onNewSurface(const SkImageInfo& info, |
| 58 const SkSurfaceProps& props) const { |
| 59 return SkSurface::NewRaster(info, &props); |
| 60 } |
| 61 |
| 62 bool SkImage_Generator::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels
, size_t dstRB, |
| 63 int srcX, int srcY) const { |
| 64 SkBitmap bm; |
| 65 if (this->getROPixels(&bm)) { |
| 66 return bm.readPixels(dstInfo, dstPixels, dstRB, srcX, srcY); |
| 67 } |
| 68 return false; |
| 69 } |
| 70 |
| 71 const void* SkImage_Generator::onPeekPixels(SkImageInfo* infoPtr, size_t* rowByt
esPtr) const { |
| 72 return NULL; |
| 73 } |
| 74 |
| 75 SkData* SkImage_Generator::onRefEncoded() const { |
| 76 return fCache->refEncoded(); |
| 77 } |
| 78 |
| 79 bool SkImage_Generator::getROPixels(SkBitmap* bitmap) const { |
| 80 return fCache->lockAsBitmap(bitmap); |
| 81 } |
| 82 |
| 83 GrTexture* SkImage_Generator::asTextureRef(GrContext* ctx, SkImageUsageType usag
e) const { |
| 84 return fCache->lockAsTexture(ctx, usage); |
| 85 } |
| 86 |
| 87 #ifndef SK_SUPPORT_LEGACY_NEWFROMGENERATOR |
| 88 SkImage* SkImage::NewFromGenerator(SkImageGenerator* generator, const SkIRect* s
ubset) { |
| 89 SkImageCacherator* cache = SkImageCacherator::NewFromGenerator(generator, su
bset); |
| 90 if (!cache) { |
| 91 return nullptr; |
| 92 } |
| 93 return SkNEW_ARGS(SkImage_Generator, (cache)); |
| 94 } |
| 95 #endif |
OLD | NEW |