OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright 2013 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 SkImageCache_DEFINED | |
9 #define SkImageCache_DEFINED | |
10 | |
11 #include "SkRefCnt.h" | |
12 #include "SkTypes.h" | |
13 | |
14 /** | |
15 * Interface for a cache that manages pixel memory. | |
16 */ | |
17 class SkImageCache : public SkRefCnt { | |
18 | |
19 public: | |
20 SK_DECLARE_INST_COUNT(SkImageCache) | |
21 | |
22 typedef intptr_t ID; | |
23 | |
24 /** | |
25 * Allocate memory whose lifetime is managed by the cache. On success, MUST
be balanced with a | |
26 * call to releaseCache and a call to throwAwayCache. | |
27 * @param bytes Number of bytes needed. | |
28 * @param ID Output parameter which must not be NULL. On success, ID will b
e set to a value | |
29 * associated with that memory which can be used as a parameter to the
other functions | |
30 * in SkImageCache. On failure, ID is unchanged. | |
31 * @return Pointer to the newly allocated memory, or NULL. This memory is s
afe to use until | |
32 * releaseCache is called with ID. | |
33 */ | |
34 virtual void* allocAndPinCache(size_t bytes, ID*) = 0; | |
35 | |
36 /** | |
37 * Output parameter for pinCache, stating whether the memory still contains
the data it held | |
38 * when releaseCache was last called for the same ID. | |
39 */ | |
40 enum DataStatus { | |
41 /** | |
42 * The data has been purged, and therefore needs to be rewritten to the
returned memory. | |
43 */ | |
44 kUninitialized_DataStatus, | |
45 | |
46 /** | |
47 * The memory still contains the data it held when releaseCache was las
t called with the | |
48 * same ID. | |
49 */ | |
50 kRetained_DataStatus, | |
51 }; | |
52 | |
53 /** | |
54 * Re-request the memory associated with ID and pin it so that it will not
be reclaimed until | |
55 * the next call to releaseCache with the same ID. | |
56 * @param ID Unique ID for the memory block. | |
57 * @param status Output parameter which must not be NULL. On success (i.e.
the return value is | |
58 * not NULL), status will be set to one of two states representing the
cached memory. If | |
59 * status is set to kRetained_DataStatus, the memory contains the same
data it did | |
60 * before releaseCache was called with this ID. If status is set to | |
61 * kUninitialized_DataStatus, the memory is still pinned, but the previ
ous data is no | |
62 * longer available. If the return value is NULL, status is unchanged. | |
63 * @return Pointer: If non-NULL, points to the previously allocated memory,
in which case | |
64 * this call must be balanced with a call to releaseCache. If NULL, the
memory | |
65 * has been reclaimed, and throwAwayCache MUST NOT be called. | |
66 */ | |
67 virtual void* pinCache(ID, DataStatus* status) = 0; | |
68 | |
69 /** | |
70 * Inform the cache that it is safe to free the block of memory correspondi
ng to ID. After | |
71 * calling this function, the pointer returned by allocAndPinCache or pinCa
che must not be | |
72 * used again. In order to access the same memory after this, pinCache must
be called with | |
73 * the same ID. | |
74 * @param ID Unique ID for the memory block which is now safe to age out of
the cache. | |
75 */ | |
76 virtual void releaseCache(ID) = 0; | |
77 | |
78 /** | |
79 * Inform the cache that the block of memory associated with ID will not be
asked for again. | |
80 * After this call, ID is no longer valid. Must not be called while the ass
ociated memory is | |
81 * pinned. Must be called to balance a successful allocAndPinCache. | |
82 */ | |
83 virtual void throwAwayCache(ID) = 0; | |
84 | |
85 /** | |
86 * ID which does not correspond to any valid cache. | |
87 */ | |
88 static const ID UNINITIALIZED_ID = 0; | |
89 | |
90 #ifdef SK_DEBUG | |
91 /** | |
92 * Debug only status of a memory block. | |
93 */ | |
94 enum MemoryStatus { | |
95 /** | |
96 * It is safe to use the pointer returned by the most recent of allocAn
dPinCache(ID) or | |
97 * pinCache(ID) with the same ID. | |
98 */ | |
99 kPinned_MemoryStatus, | |
100 | |
101 /** | |
102 * The pointer returned by the most recent call to allocAndPinCache(ID)
or pinCache(ID) has | |
103 * since been released by releaseCache(ID). In order to reuse it, pinCa
che(ID) must be | |
104 * called again. Note that after calling releaseCache(ID), the status o
f that particular | |
105 * ID may not be kUnpinned_MemoryStatus, depending on the implementatio
n, but it will not | |
106 * be kPinned_MemoryStatus. | |
107 */ | |
108 kUnpinned_MemoryStatus, | |
109 | |
110 /** | |
111 * The memory associated with ID has been thrown away. No calls should
be made using the | |
112 * same ID. | |
113 */ | |
114 kFreed_MemoryStatus, | |
115 }; | |
116 | |
117 /** | |
118 * Debug only function to get the status of a particular block of memory. S
afe to call after | |
119 * throwAwayCache has been called with this ID. | |
120 */ | |
121 virtual MemoryStatus getMemoryStatus(intptr_t ID) const = 0; | |
122 | |
123 /** | |
124 * Debug only function to clear all unpinned caches. | |
125 */ | |
126 virtual void purgeAllUnpinnedCaches() = 0; | |
127 #endif | |
128 | |
129 private: | |
130 typedef SkRefCnt INHERITED; | |
131 }; | |
132 #endif // SkImageCache_DEFINED | |
OLD | NEW |