1 /* 2 * Copyright 2012 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 #ifndef SkLazyPixelRef_DEFINED 9 #define SkLazyPixelRef_DEFINED 10 11 #include "SkBitmapFactory.h" 12 #include "SkImage.h" 13 #include "SkPixelRef.h" 14 #include "SkFlattenable.h" 15 16 class SkColorTable; 17 class SkData; 18 class SkImageCache; 19 20 #ifdef SK_DEBUG 21 #define LAZY_CACHE_STATS 1 22 #elif !defined(LAZY_CACHE_STATS) 23 #define LAZY_CACHE_STATS 0 24 #endif 25 26 /** 27 * PixelRef which defers decoding until SkBitmap::lockPixels() is called. 28 */ 29 class SkLazyPixelRef : public SkPixelRef { 30 31 public: 32 /** 33 * Create a new SkLazyPixelRef. 34 * @param SkData Encoded data representing the pixels. 35 * @param DecodeProc Called to decode the pixels when needed. Must be non-NULL. 36 * @param SkImageCache Object that handles allocating and freeing the pixel memory, as needed. 37 * Must not be NULL. 38 */ 39 SkLazyPixelRef(SkData*, SkBitmapFactory::DecodeProc, SkImageCache*); 40 41 virtual ~SkLazyPixelRef(); 42 43 #ifdef SK_DEBUG 44 intptr_t getCacheId() const { return fCacheId; } 45 #endif 46 47 #if LAZY_CACHE_STATS 48 static int32_t GetCacheHits() { return gCacheHits; } 49 static int32_t GetCacheMisses() { return gCacheMisses; } 50 static void ResetCacheStats() { gCacheHits = gCacheMisses = 0; } 51 #endif 52 53 // No need to flatten this object. When flattening an SkBitmap, SkOrderedWriteBuffer will check 54 // the encoded data and write that instead. 55 // Future implementations of SkFlattenableWriteBuffer will need to special case for 56 // onRefEncodedData as well. 57 SK_DECLARE_UNFLATTENABLE_OBJECT() 58 59 protected: 60 virtual void* onLockPixels(SkColorTable**) SK_OVERRIDE; 61 virtual void onUnlockPixels() SK_OVERRIDE; 62 virtual bool onLockPixelsAreWritable() const SK_OVERRIDE { return false; } 63 virtual SkData* onRefEncodedData() SK_OVERRIDE; 64 65 private: 66 bool fErrorInDecoding; 67 SkData* fData; 68 SkBitmapFactory::DecodeProc fDecodeProc; 69 SkImageCache* fImageCache; 70 intptr_t fCacheId; 71 size_t fRowBytes; 72 73 #if LAZY_CACHE_STATS 74 static int32_t gCacheHits; 75 static int32_t gCacheMisses; 76 #endif 77 78 typedef SkPixelRef INHERITED; 79 }; 80 81 #endif // SkLazyPixelRef_DEFINED 82