OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkImage_Base.h" | 8 #include "SkImage_Base.h" |
9 #include "SkBitmap.h" | 9 #include "SkBitmap.h" |
10 #include "SkCanvas.h" | 10 #include "SkCanvas.h" |
11 #include "SkData.h" | 11 #include "SkData.h" |
12 #include "SkImageCacherator.h" | 12 #include "SkImageCacherator.h" |
13 #include "SkImagePriv.h" | 13 #include "SkImagePriv.h" |
14 #include "SkPixelRef.h" | 14 #include "SkPixelRef.h" |
15 #include "SkSurface.h" | 15 #include "SkSurface.h" |
16 | 16 |
17 class SkImage_Generator : public SkImage_Base { | 17 class SkImage_Generator : public SkImage_Base { |
18 public: | 18 public: |
19 SkImage_Generator(SkImageCacherator* cache) | 19 SkImage_Generator(SkImageCacherator::Validator* validator) |
20 : INHERITED(cache->info().width(), cache->info().height(), cache->unique ID()) | 20 : INHERITED(validator->fInfo.width(), validator->fInfo.height(), validat or->fUniqueID) |
21 , fCache(cache) // take ownership | 21 , fCache(validator) |
22 {} | 22 {} |
23 | 23 |
24 virtual SkImageInfo onImageInfo() const override { | 24 virtual SkImageInfo onImageInfo() const override { |
25 return fCache->info(); | 25 return fCache.info(); |
26 } | 26 } |
27 SkAlphaType onAlphaType() const override { | 27 SkAlphaType onAlphaType() const override { |
28 return fCache->info().alphaType(); | 28 return fCache.info().alphaType(); |
29 } | 29 } |
30 | 30 |
31 bool onReadPixels(const SkImageInfo&, void*, size_t, int srcX, int srcY, Cac hingHint) const override; | 31 bool onReadPixels(const SkImageInfo&, void*, size_t, int srcX, int srcY, Cac hingHint) const override; |
32 SkImageCacherator* peekCacherator() const override { return fCache; } | 32 SkImageCacherator* peekCacherator() const override { return &fCache; } |
33 SkData* onRefEncoded(GrContext*) const override; | 33 SkData* onRefEncoded(GrContext*) const override; |
34 sk_sp<SkImage> onMakeSubset(const SkIRect&) const override; | 34 sk_sp<SkImage> onMakeSubset(const SkIRect&) const override; |
35 bool getROPixels(SkBitmap*, CachingHint) const override; | 35 bool getROPixels(SkBitmap*, CachingHint) const override; |
36 GrTexture* asTextureRef(GrContext*, const GrTextureParams&, | 36 GrTexture* asTextureRef(GrContext*, const GrTextureParams&, |
37 SkSourceGammaTreatment) const override; | 37 SkSourceGammaTreatment) const override; |
38 bool onIsLazyGenerated() const override { return true; } | 38 bool onIsLazyGenerated() const override { return true; } |
39 | 39 |
40 private: | 40 private: |
41 SkAutoTDelete<SkImageCacherator> fCache; | 41 mutable SkImageCacherator fCache; |
42 | 42 |
43 typedef SkImage_Base INHERITED; | 43 typedef SkImage_Base INHERITED; |
44 }; | 44 }; |
45 | 45 |
46 /////////////////////////////////////////////////////////////////////////////// | 46 /////////////////////////////////////////////////////////////////////////////// |
47 | 47 |
48 bool SkImage_Generator::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels , size_t dstRB, | 48 bool SkImage_Generator::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels , size_t dstRB, |
49 int srcX, int srcY, CachingHint chint) cons t { | 49 int srcX, int srcY, CachingHint chint) cons t { |
50 SkBitmap bm; | 50 SkBitmap bm; |
51 if (kDisallow_CachingHint == chint) { | 51 if (kDisallow_CachingHint == chint) { |
52 if (fCache->lockAsBitmapOnlyIfAlreadyCached(&bm)) { | 52 if (fCache.lockAsBitmapOnlyIfAlreadyCached(&bm)) { |
53 return bm.readPixels(dstInfo, dstPixels, dstRB, srcX, srcY); | 53 return bm.readPixels(dstInfo, dstPixels, dstRB, srcX, srcY); |
54 } else { | 54 } else { |
55 // Try passing the caller's buffer directly down to the generator. I f this fails we | 55 // Try passing the caller's buffer directly down to the generator. I f this fails we |
56 // may still succeed in the general case, as the generator may prefe r some other | 56 // may still succeed in the general case, as the generator may prefe r some other |
57 // config, which we could then convert via SkBitmap::readPixels. | 57 // config, which we could then convert via SkBitmap::readPixels. |
58 if (fCache->directGeneratePixels(dstInfo, dstPixels, dstRB, srcX, sr cY)) { | 58 if (fCache.directGeneratePixels(dstInfo, dstPixels, dstRB, srcX, src Y)) { |
59 return true; | 59 return true; |
60 } | 60 } |
61 // else fall through | 61 // else fall through |
62 } | 62 } |
63 } | 63 } |
64 | 64 |
65 if (this->getROPixels(&bm, chint)) { | 65 if (this->getROPixels(&bm, chint)) { |
66 return bm.readPixels(dstInfo, dstPixels, dstRB, srcX, srcY); | 66 return bm.readPixels(dstInfo, dstPixels, dstRB, srcX, srcY); |
67 } | 67 } |
68 return false; | 68 return false; |
69 } | 69 } |
70 | 70 |
71 SkData* SkImage_Generator::onRefEncoded(GrContext* ctx) const { | 71 SkData* SkImage_Generator::onRefEncoded(GrContext* ctx) const { |
72 return fCache->refEncoded(ctx); | 72 return fCache.refEncoded(ctx); |
73 } | 73 } |
74 | 74 |
75 bool SkImage_Generator::getROPixels(SkBitmap* bitmap, CachingHint chint) const { | 75 bool SkImage_Generator::getROPixels(SkBitmap* bitmap, CachingHint chint) const { |
76 return fCache->lockAsBitmap(bitmap, this, chint); | 76 return fCache.lockAsBitmap(bitmap, this, chint); |
77 } | 77 } |
78 | 78 |
79 GrTexture* SkImage_Generator::asTextureRef(GrContext* ctx, const GrTextureParams & params, | 79 GrTexture* SkImage_Generator::asTextureRef(GrContext* ctx, const GrTextureParams & params, |
80 SkSourceGammaTreatment gammaTreatment ) const { | 80 SkSourceGammaTreatment gammaTreatment ) const { |
81 return fCache->lockAsTexture(ctx, params, gammaTreatment, this); | 81 return fCache.lockAsTexture(ctx, params, gammaTreatment, this); |
82 } | 82 } |
83 | 83 |
84 sk_sp<SkImage> SkImage_Generator::onMakeSubset(const SkIRect& subset) const { | 84 sk_sp<SkImage> SkImage_Generator::onMakeSubset(const SkIRect& subset) const { |
85 // TODO: make this lazy, by wrapping the subset inside a new generator or so mething | 85 // TODO: make this lazy, by wrapping the subset inside a new generator or so mething |
86 // For now, we do effectively what we did before, make it a raster | 86 // For now, we do effectively what we did before, make it a raster |
87 | 87 |
88 const SkImageInfo info = SkImageInfo::MakeN32(subset.width(), subset.height( ), | 88 const SkImageInfo info = SkImageInfo::MakeN32(subset.width(), subset.height( ), |
89 this->alphaType()); | 89 this->alphaType()); |
90 auto surface(SkSurface::MakeRaster(info)); | 90 auto surface(SkSurface::MakeRaster(info)); |
91 if (!surface) { | 91 if (!surface) { |
92 return nullptr; | 92 return nullptr; |
93 } | 93 } |
94 surface->getCanvas()->clear(0); | 94 surface->getCanvas()->clear(0); |
95 surface->getCanvas()->drawImage(this, SkIntToScalar(-subset.x()), SkIntToSca lar(-subset.y()), | 95 surface->getCanvas()->drawImage(this, SkIntToScalar(-subset.x()), SkIntToSca lar(-subset.y()), |
96 nullptr); | 96 nullptr); |
97 return surface->makeImageSnapshot(); | 97 return surface->makeImageSnapshot(); |
98 } | 98 } |
99 | 99 |
100 sk_sp<SkImage> SkImage::MakeFromGenerator(SkImageGenerator* generator, const SkI Rect* subset) { | 100 sk_sp<SkImage> SkImage::MakeFromGenerator(SkImageGenerator* generator, const SkI Rect* subset) { |
101 if (!generator) { | 101 SkImageCacherator::Validator validator(generator, subset); |
102 return nullptr; | 102 |
103 } | 103 return validator ? sk_make_sp<SkImage_Generator>(&validator) : nullptr; |
reed1
2016/10/27 13:45:19
Can we force somebody to assert (Validator?) if we
f(malita)
2016/10/27 14:17:07
The SkImageCacherator ctor (the only "user" of val
| |
104 SkImageCacherator* cache = SkImageCacherator::NewFromGenerator(generator, su bset); | |
105 if (!cache) { | |
106 return nullptr; | |
107 } | |
108 return sk_make_sp<SkImage_Generator>(cache); | |
109 } | 104 } |
OLD | NEW |