• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "SkCanvas.h"
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:
SkImage_Generator(SkImageCacherator * cache)19     SkImage_Generator(SkImageCacherator* cache)
20         : INHERITED(cache->info().width(), cache->info().height(), cache->uniqueID())
21         , fCache(cache) // take ownership
22     {}
23 
24     bool onReadPixels(const SkImageInfo&, void*, size_t, int srcX, int srcY, CachingHint) const override;
25     const void* onPeekPixels(SkImageInfo*, size_t* /*rowBytes*/) const override;
peekCacherator() const26     SkImageCacherator* peekCacherator() const override { return fCache; }
27     SkData* onRefEncoded(GrContext*) const override;
isOpaque() const28     bool isOpaque() const override { return fCache->info().isOpaque(); }
29     SkImage* onNewSubset(const SkIRect&) const override;
30     bool getROPixels(SkBitmap*, CachingHint) const override;
31     GrTexture* asTextureRef(GrContext*, const GrTextureParams&) const override;
onIsLazyGenerated() const32     bool onIsLazyGenerated() const override { return true; }
33 
34 private:
35     SkAutoTDelete<SkImageCacherator> fCache;
36 
37     typedef SkImage_Base INHERITED;
38 };
39 
40 ///////////////////////////////////////////////////////////////////////////////
41 
onReadPixels(const SkImageInfo & dstInfo,void * dstPixels,size_t dstRB,int srcX,int srcY,CachingHint chint) const42 bool SkImage_Generator::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRB,
43                                      int srcX, int srcY, CachingHint chint) const {
44     SkBitmap bm;
45     if (kDisallow_CachingHint == chint) {
46         if (fCache->lockAsBitmapOnlyIfAlreadyCached(&bm)) {
47             return bm.readPixels(dstInfo, dstPixels, dstRB, srcX, srcY);
48         } else {
49             // Try passing the caller's buffer directly down to the generator. If this fails we
50             // may still succeed in the general case, as the generator may prefer some other
51             // config, which we could then convert via SkBitmap::readPixels.
52             if (fCache->directGeneratePixels(dstInfo, dstPixels, dstRB, srcX, srcY)) {
53                 return true;
54             }
55             // else fall through
56         }
57     }
58 
59     if (this->getROPixels(&bm, chint)) {
60         return bm.readPixels(dstInfo, dstPixels, dstRB, srcX, srcY);
61     }
62     return false;
63 }
64 
onPeekPixels(SkImageInfo * infoPtr,size_t * rowBytesPtr) const65 const void* SkImage_Generator::onPeekPixels(SkImageInfo* infoPtr, size_t* rowBytesPtr) const {
66     return NULL;
67 }
68 
onRefEncoded(GrContext * ctx) const69 SkData* SkImage_Generator::onRefEncoded(GrContext* ctx) const {
70     return fCache->refEncoded(ctx);
71 }
72 
getROPixels(SkBitmap * bitmap,CachingHint chint) const73 bool SkImage_Generator::getROPixels(SkBitmap* bitmap, CachingHint chint) const {
74     return fCache->lockAsBitmap(bitmap, this, chint);
75 }
76 
asTextureRef(GrContext * ctx,const GrTextureParams & params) const77 GrTexture* SkImage_Generator::asTextureRef(GrContext* ctx, const GrTextureParams& params) const {
78     return fCache->lockAsTexture(ctx, params, this);
79 }
80 
onNewSubset(const SkIRect & subset) const81 SkImage* SkImage_Generator::onNewSubset(const SkIRect& subset) const {
82     // TODO: make this lazy, by wrapping the subset inside a new generator or something
83     // For now, we do effectively what we did before, make it a raster
84 
85     const SkImageInfo info = SkImageInfo::MakeN32(subset.width(), subset.height(),
86                                       this->isOpaque() ? kOpaque_SkAlphaType : kPremul_SkAlphaType);
87     SkAutoTUnref<SkSurface> surface(SkSurface::NewRaster(info));
88     if (!surface) {
89         return nullptr;
90     }
91     surface->getCanvas()->clear(0);
92     surface->getCanvas()->drawImage(this, SkIntToScalar(-subset.x()), SkIntToScalar(-subset.y()),
93                                     nullptr);
94     return surface->newImageSnapshot();
95 }
96 
NewFromGenerator(SkImageGenerator * generator,const SkIRect * subset)97 SkImage* SkImage::NewFromGenerator(SkImageGenerator* generator, const SkIRect* subset) {
98     SkImageCacherator* cache = SkImageCacherator::NewFromGenerator(generator, subset);
99     if (!cache) {
100         return nullptr;
101     }
102     return new SkImage_Generator(cache);
103 }
104