blob: fd41dd4682b01a66f3e9b8713501b778a086a6a8 [file] [log] [blame]
scroggo@google.comf8d7d272013-02-22 21:38:35 +00001/*
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
16class SkColorTable;
17class SkData;
18class SkImageCache;
19
scroggo@google.comcc690202013-03-04 19:56:21 +000020#ifdef SK_DEBUG
21 #define LAZY_CACHE_STATS 1
22#elif !defined(LAZY_CACHE_STATS)
23 #define LAZY_CACHE_STATS 0
24#endif
25
scroggo@google.comf8d7d272013-02-22 21:38:35 +000026/**
27 * PixelRef which defers decoding until SkBitmap::lockPixels() is called.
28 */
29class SkLazyPixelRef : public SkPixelRef {
30
31public:
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
scroggo@google.comcc690202013-03-04 19:56:21 +000047#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
scroggo@google.comf8d7d272013-02-22 21:38:35 +000053 // 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
59protected:
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
65private:
66 bool fErrorInDecoding;
67 SkData* fData;
68 SkBitmapFactory::DecodeProc fDecodeProc;
69 SkImageCache* fImageCache;
70 intptr_t fCacheId;
scroggo@google.combb281f72013-03-18 21:37:39 +000071 size_t fRowBytes;
scroggo@google.comf8d7d272013-02-22 21:38:35 +000072
scroggo@google.comcc690202013-03-04 19:56:21 +000073#if LAZY_CACHE_STATS
74 static int32_t gCacheHits;
75 static int32_t gCacheMisses;
76#endif
77
scroggo@google.comf8d7d272013-02-22 21:38:35 +000078 typedef SkPixelRef INHERITED;
79};
80
81#endif // SkLazyPixelRef_DEFINED