scroggo@google.com | f8d7d27 | 2013-02-22 21:38:35 +0000 | [diff] [blame] | 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: |
commit-bot@chromium.org | ef284a8 | 2013-07-11 22:29:29 +0000 | [diff] [blame] | 20 | SK_DECLARE_INST_COUNT(SkImageCache) |
| 21 | |
reed@google.com | 6e8b7dd | 2013-07-09 21:31:54 +0000 | [diff] [blame] | 22 | typedef intptr_t ID; |
| 23 | |
scroggo@google.com | f8d7d27 | 2013-02-22 21:38:35 +0000 | [diff] [blame] | 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 be set to a value |
scroggo@google.com | bb281f7 | 2013-03-18 21:37:39 +0000 | [diff] [blame] | 29 | * associated with that memory which can be used as a parameter to the other functions |
| 30 | * in SkImageCache. On failure, ID is unchanged. |
scroggo@google.com | f8d7d27 | 2013-02-22 21:38:35 +0000 | [diff] [blame] | 31 | * @return Pointer to the newly allocated memory, or NULL. This memory is safe to use until |
scroggo@google.com | bb281f7 | 2013-03-18 21:37:39 +0000 | [diff] [blame] | 32 | * releaseCache is called with ID. |
scroggo@google.com | f8d7d27 | 2013-02-22 21:38:35 +0000 | [diff] [blame] | 33 | */ |
reed@google.com | 6e8b7dd | 2013-07-09 21:31:54 +0000 | [diff] [blame] | 34 | virtual void* allocAndPinCache(size_t bytes, ID*) = 0; |
scroggo@google.com | f8d7d27 | 2013-02-22 21:38:35 +0000 | [diff] [blame] | 35 | |
| 36 | /** |
scroggo@google.com | bb281f7 | 2013-03-18 21:37:39 +0000 | [diff] [blame] | 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. |
scroggo@google.com | f8d7d27 | 2013-02-22 21:38:35 +0000 | [diff] [blame] | 39 | */ |
scroggo@google.com | bb281f7 | 2013-03-18 21:37:39 +0000 | [diff] [blame] | 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 last 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 previous 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 | */ |
reed@google.com | 6e8b7dd | 2013-07-09 21:31:54 +0000 | [diff] [blame] | 67 | virtual void* pinCache(ID, DataStatus* status) = 0; |
scroggo@google.com | f8d7d27 | 2013-02-22 21:38:35 +0000 | [diff] [blame] | 68 | |
| 69 | /** |
| 70 | * Inform the cache that it is safe to free the block of memory corresponding to ID. After |
scroggo@google.com | c75764e | 2013-03-04 21:38:50 +0000 | [diff] [blame] | 71 | * calling this function, the pointer returned by allocAndPinCache or pinCache must not be |
| 72 | * used again. In order to access the same memory after this, pinCache must be called with |
| 73 | * the same ID. |
scroggo@google.com | f8d7d27 | 2013-02-22 21:38:35 +0000 | [diff] [blame] | 74 | * @param ID Unique ID for the memory block which is now safe to age out of the cache. |
| 75 | */ |
reed@google.com | 6e8b7dd | 2013-07-09 21:31:54 +0000 | [diff] [blame] | 76 | virtual void releaseCache(ID) = 0; |
scroggo@google.com | f8d7d27 | 2013-02-22 21:38:35 +0000 | [diff] [blame] | 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 associated memory is |
scroggo@google.com | c75764e | 2013-03-04 21:38:50 +0000 | [diff] [blame] | 81 | * pinned. Must be called to balance a successful allocAndPinCache. |
scroggo@google.com | f8d7d27 | 2013-02-22 21:38:35 +0000 | [diff] [blame] | 82 | */ |
reed@google.com | 6e8b7dd | 2013-07-09 21:31:54 +0000 | [diff] [blame] | 83 | virtual void throwAwayCache(ID) = 0; |
scroggo@google.com | f8d7d27 | 2013-02-22 21:38:35 +0000 | [diff] [blame] | 84 | |
| 85 | /** |
| 86 | * ID which does not correspond to any valid cache. |
| 87 | */ |
reed@google.com | 6e8b7dd | 2013-07-09 21:31:54 +0000 | [diff] [blame] | 88 | static const ID UNINITIALIZED_ID = 0; |
scroggo@google.com | f8d7d27 | 2013-02-22 21:38:35 +0000 | [diff] [blame] | 89 | |
| 90 | #ifdef SK_DEBUG |
scroggo@google.com | bb281f7 | 2013-03-18 21:37:39 +0000 | [diff] [blame] | 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 allocAndPinCache(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, pinCache(ID) must be |
| 104 | * called again. Note that after calling releaseCache(ID), the status of that particular |
| 105 | * ID may not be kUnpinned_MemoryStatus, depending on the implementation, 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, |
scroggo@google.com | f8d7d27 | 2013-02-22 21:38:35 +0000 | [diff] [blame] | 115 | }; |
| 116 | |
| 117 | /** |
scroggo@google.com | bb281f7 | 2013-03-18 21:37:39 +0000 | [diff] [blame] | 118 | * Debug only function to get the status of a particular block of memory. Safe to call after |
| 119 | * throwAwayCache has been called with this ID. |
scroggo@google.com | f8d7d27 | 2013-02-22 21:38:35 +0000 | [diff] [blame] | 120 | */ |
scroggo@google.com | bb281f7 | 2013-03-18 21:37:39 +0000 | [diff] [blame] | 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; |
scroggo@google.com | f8d7d27 | 2013-02-22 21:38:35 +0000 | [diff] [blame] | 127 | #endif |
commit-bot@chromium.org | ef284a8 | 2013-07-11 22:29:29 +0000 | [diff] [blame] | 128 | |
| 129 | private: |
| 130 | typedef SkRefCnt INHERITED; |
scroggo@google.com | f8d7d27 | 2013-02-22 21:38:35 +0000 | [diff] [blame] | 131 | }; |
| 132 | #endif // SkImageCache_DEFINED |