blob: bfd5269ee905ea70854fabe2fe046aceb549d145 [file] [log] [blame]
scroggo@google.comf8d7d272013-02-22 21:38:35 +00001/*
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 */
17class SkImageCache : public SkRefCnt {
18
19public:
20 /**
21 * Allocate memory whose lifetime is managed by the cache. On success, MUST be balanced with a
22 * call to releaseCache and a call to throwAwayCache.
23 * @param bytes Number of bytes needed.
24 * @param ID Output parameter which must not be NULL. On success, ID will be set to a value
scroggo@google.combb281f72013-03-18 21:37:39 +000025 * associated with that memory which can be used as a parameter to the other functions
26 * in SkImageCache. On failure, ID is unchanged.
scroggo@google.comf8d7d272013-02-22 21:38:35 +000027 * @return Pointer to the newly allocated memory, or NULL. This memory is safe to use until
scroggo@google.combb281f72013-03-18 21:37:39 +000028 * releaseCache is called with ID.
scroggo@google.comf8d7d272013-02-22 21:38:35 +000029 */
30 virtual void* allocAndPinCache(size_t bytes, intptr_t* ID) = 0;
31
32 /**
scroggo@google.combb281f72013-03-18 21:37:39 +000033 * Output parameter for pinCache, stating whether the memory still contains the data it held
34 * when releaseCache was last called for the same ID.
scroggo@google.comf8d7d272013-02-22 21:38:35 +000035 */
scroggo@google.combb281f72013-03-18 21:37:39 +000036 enum DataStatus {
37 /**
38 * The data has been purged, and therefore needs to be rewritten to the returned memory.
39 */
40 kUninitialized_DataStatus,
41
42 /**
43 * The memory still contains the data it held when releaseCache was last called with the
44 * same ID.
45 */
46 kRetained_DataStatus,
47 };
48
49 /**
50 * Re-request the memory associated with ID and pin it so that it will not be reclaimed until
51 * the next call to releaseCache with the same ID.
52 * @param ID Unique ID for the memory block.
53 * @param status Output parameter which must not be NULL. On success (i.e. the return value is
54 * not NULL), status will be set to one of two states representing the cached memory. If
55 * status is set to kRetained_DataStatus, the memory contains the same data it did
56 * before releaseCache was called with this ID. If status is set to
57 * kUninitialized_DataStatus, the memory is still pinned, but the previous data is no
58 * longer available. If the return value is NULL, status is unchanged.
59 * @return Pointer: If non-NULL, points to the previously allocated memory, in which case
60 * this call must be balanced with a call to releaseCache. If NULL, the memory
61 * has been reclaimed, and throwAwayCache MUST NOT be called.
62 */
63 virtual void* pinCache(intptr_t ID, DataStatus* status) = 0;
scroggo@google.comf8d7d272013-02-22 21:38:35 +000064
65 /**
66 * Inform the cache that it is safe to free the block of memory corresponding to ID. After
scroggo@google.comc75764e2013-03-04 21:38:50 +000067 * calling this function, the pointer returned by allocAndPinCache or pinCache must not be
68 * used again. In order to access the same memory after this, pinCache must be called with
69 * the same ID.
scroggo@google.comf8d7d272013-02-22 21:38:35 +000070 * @param ID Unique ID for the memory block which is now safe to age out of the cache.
71 */
72 virtual void releaseCache(intptr_t ID) = 0;
73
74 /**
75 * Inform the cache that the block of memory associated with ID will not be asked for again.
76 * After this call, ID is no longer valid. Must not be called while the associated memory is
scroggo@google.comc75764e2013-03-04 21:38:50 +000077 * pinned. Must be called to balance a successful allocAndPinCache.
scroggo@google.comf8d7d272013-02-22 21:38:35 +000078 */
79 virtual void throwAwayCache(intptr_t ID) = 0;
80
81 /**
82 * ID which does not correspond to any valid cache.
83 */
84 static const intptr_t UNINITIALIZED_ID = 0;
85
86#ifdef SK_DEBUG
scroggo@google.combb281f72013-03-18 21:37:39 +000087 /**
88 * Debug only status of a memory block.
89 */
90 enum MemoryStatus {
91 /**
92 * It is safe to use the pointer returned by the most recent of allocAndPinCache(ID) or
93 * pinCache(ID) with the same ID.
94 */
95 kPinned_MemoryStatus,
96
97 /**
98 * The pointer returned by the most recent call to allocAndPinCache(ID) or pinCache(ID) has
99 * since been released by releaseCache(ID). In order to reuse it, pinCache(ID) must be
100 * called again. Note that after calling releaseCache(ID), the status of that particular
101 * ID may not be kUnpinned_MemoryStatus, depending on the implementation, but it will not
102 * be kPinned_MemoryStatus.
103 */
104 kUnpinned_MemoryStatus,
105
106 /**
107 * The memory associated with ID has been thrown away. No calls should be made using the
108 * same ID.
109 */
110 kFreed_MemoryStatus,
scroggo@google.comf8d7d272013-02-22 21:38:35 +0000111 };
112
113 /**
scroggo@google.combb281f72013-03-18 21:37:39 +0000114 * Debug only function to get the status of a particular block of memory. Safe to call after
115 * throwAwayCache has been called with this ID.
scroggo@google.comf8d7d272013-02-22 21:38:35 +0000116 */
scroggo@google.combb281f72013-03-18 21:37:39 +0000117 virtual MemoryStatus getMemoryStatus(intptr_t ID) const = 0;
118
119 /**
120 * Debug only function to clear all unpinned caches.
121 */
122 virtual void purgeAllUnpinnedCaches() = 0;
scroggo@google.comf8d7d272013-02-22 21:38:35 +0000123#endif
124};
125#endif // SkImageCache_DEFINED