OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 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 #ifndef SkCachingPixelRef_DEFINED | 8 #ifndef SkCachingPixelRef_DEFINED |
9 #define SkCachingPixelRef_DEFINED | 9 #define SkCachingPixelRef_DEFINED |
10 | 10 |
11 #include "SkImage.h" | 11 #include "SkImageInfo.h" |
| 12 #include "SkImageGenerator.h" |
12 #include "SkPixelRef.h" | 13 #include "SkPixelRef.h" |
13 | 14 |
14 class SkColorTable; | 15 class SkColorTable; |
15 | 16 |
16 /** | 17 /** |
17 * PixelRef which defers decoding until SkBitmap::lockPixels() is | 18 * PixelRef which defers decoding until SkBitmap::lockPixels() is |
18 * called. Caches the decoded images in the global | 19 * called. Caches the decoded images in the global |
19 * SkScaledImageCache. When the pixels are unlocked, this cache may | 20 * SkScaledImageCache. When the pixels are unlocked, this cache may |
20 * or be destroyed before the next lock. If so, onLockPixels will | 21 * or be destroyed before the next lock. If so, onLockPixels will |
21 * attempt to re-decode. | 22 * attempt to re-decode. |
22 * | 23 * |
23 * Decoding is handled by the pure-virtual functions onDecodeInfo() | 24 * Decoding is handled by the SkImageGenerator |
24 * and onDecodePixels(). Subclasses of this class need only provide | |
25 * those two functions. | |
26 */ | 25 */ |
27 class SkCachingPixelRef : public SkPixelRef { | 26 class SkCachingPixelRef : public SkPixelRef { |
28 public: | 27 public: |
29 SkCachingPixelRef(); | 28 /** |
30 virtual ~SkCachingPixelRef(); | 29 * Takes ownership of SkImageGenerator. If this method fails for |
| 30 * whatever reason, it will return false and immediatetely delete |
| 31 * the generator. If it succeeds, it will modify destination |
| 32 * bitmap. |
| 33 * |
| 34 * If Install fails or when the SkCachingPixelRef that is |
| 35 * installed into destination is destroyed, it will call |
| 36 * SkDELETE() on the generator. Therefore, generator should be |
| 37 * allocated with SkNEW() or SkNEW_ARGS(). |
| 38 */ |
| 39 static bool Install(SkImageGenerator* gen, SkBitmap* dst); |
31 | 40 |
32 protected: | 41 protected: |
| 42 virtual ~SkCachingPixelRef(); |
33 virtual void* onLockPixels(SkColorTable** colorTable) SK_OVERRIDE; | 43 virtual void* onLockPixels(SkColorTable** colorTable) SK_OVERRIDE; |
34 virtual void onUnlockPixels() SK_OVERRIDE; | 44 virtual void onUnlockPixels() SK_OVERRIDE; |
35 virtual bool onLockPixelsAreWritable() const SK_OVERRIDE { return false; } | 45 virtual bool onLockPixelsAreWritable() const SK_OVERRIDE { return false; } |
36 virtual bool onImplementsDecodeInto() SK_OVERRIDE { return true; } | |
37 virtual bool onDecodeInto(int pow2, SkBitmap*) SK_OVERRIDE; | |
38 | 46 |
39 /** | 47 virtual SkData* onRefEncodedData() SK_OVERRIDE { |
40 * Configure the supplied bitmap for this pixelRef, based on | 48 return fImageGenerator->refEncodedData(); |
41 * information provided by onDecodeInfo(). Does not set the | 49 } |
42 * bitmap's pixelRef. */ | 50 // No need to flatten this object. When flattening an SkBitmap, |
43 bool configure(SkBitmap* bitmap); | 51 // SkOrderedWriteBuffer will check the encoded data and write that |
44 | 52 // instead. |
45 /** | 53 // Future implementations of SkFlattenableWriteBuffer will need to |
46 * Cache info from onDecodeInfo(). Returns false on failure. | 54 // special case for onRefEncodedData as well. |
47 */ | 55 SK_DECLARE_UNFLATTENABLE_OBJECT() |
48 bool getInfo(SkImageInfo* info); | |
49 | |
50 /** | |
51 * Return some information about the pixels, allowing this class | |
52 * to allocate pixels. @return false if anything goes wrong. | |
53 */ | |
54 virtual bool onDecodeInfo(SkImageInfo* info) = 0; | |
55 /** | |
56 * Decode into the given pixels, a block of memory of size | |
57 * (info.fHeight - 1) * rowBytes + (info.fWidth * bytesPerPixel) | |
58 * | |
59 * @param info Should be identical to the info returned by | |
60 * onDecodeInfo so that the implementation can confirm | |
61 * that the caller knows what it is asking for (config, | |
62 * size). Thiscontract also allows the caller to specify | |
63 * different output-configs, which the implementation can | |
64 * decide to support or not. | |
65 * | |
66 * @return false if anything goes wrong. | |
67 */ | |
68 virtual bool onDecodePixels(const SkImageInfo& info, | |
69 void* pixels, | |
70 size_t rowBytes) = 0; | |
71 | 56 |
72 private: | 57 private: |
73 bool fErrorInDecoding; | 58 SkImageGenerator* const fImageGenerator; |
74 void* fScaledCacheId; | 59 bool fErrorInDecoding; |
75 SkImageInfo fInfo; | 60 void* fScaledCacheId; |
| 61 const SkImageInfo fInfo; |
| 62 const size_t fRowBytes; |
76 | 63 |
| 64 SkCachingPixelRef(SkImageGenerator* imageGenerator, |
| 65 const SkImageInfo& info, |
| 66 size_t rowBytes); |
77 typedef SkPixelRef INHERITED; | 67 typedef SkPixelRef INHERITED; |
78 }; | 68 }; |
79 | 69 |
80 #endif // SkCachingPixelRef_DEFINED | 70 #endif // SkCachingPixelRef_DEFINED |
| 71 |
OLD | NEW |