blob: 6d30ae73f06337afc744690aee43414d5e742ea9 [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:
commit-bot@chromium.orgef284a82013-07-11 22:29:29 +000020 SK_DECLARE_INST_COUNT(SkImageCache)
21
reed@google.com6e8b7dd2013-07-09 21:31:54 +000022 typedef intptr_t ID;
23
scroggo@google.comf8d7d272013-02-22 21:38:35 +000024 /**
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.combb281f72013-03-18 21:37:39 +000029 * 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.comf8d7d272013-02-22 21:38:35 +000031 * @return Pointer to the newly allocated memory, or NULL. This memory is safe to use until
scroggo@google.combb281f72013-03-18 21:37:39 +000032 * releaseCache is called with ID.
scroggo@google.comf8d7d272013-02-22 21:38:35 +000033 */
reed@google.com6e8b7dd2013-07-09 21:31:54 +000034 virtual void* allocAndPinCache(size_t bytes, ID*) = 0;
scroggo@google.comf8d7d272013-02-22 21:38:35 +000035
36 /**
scroggo@google.combb281f72013-03-18 21:37:39 +000037 * 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.comf8d7d272013-02-22 21:38:35 +000039 */
scroggo@google.combb281f72013-03-18 21:37:39 +000040 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.com6e8b7dd2013-07-09 21:31:54 +000067 virtual void* pinCache(ID, DataStatus* status) = 0;
scroggo@google.comf8d7d272013-02-22 21:38:35 +000068
69 /**
70 * 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 +000071 * 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.comf8d7d272013-02-22 21:38:35 +000074 * @param ID Unique ID for the memory block which is now safe to age out of the cache.
75 */
reed@google.com6e8b7dd2013-07-09 21:31:54 +000076 virtual void releaseCache(ID) = 0;
scroggo@google.comf8d7d272013-02-22 21:38:35 +000077
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.comc75764e2013-03-04 21:38:50 +000081 * pinned. Must be called to balance a successful allocAndPinCache.
scroggo@google.comf8d7d272013-02-22 21:38:35 +000082 */
reed@google.com6e8b7dd2013-07-09 21:31:54 +000083 virtual void throwAwayCache(ID) = 0;
scroggo@google.comf8d7d272013-02-22 21:38:35 +000084
85 /**
86 * ID which does not correspond to any valid cache.
87 */
reed@google.com6e8b7dd2013-07-09 21:31:54 +000088 static const ID UNINITIALIZED_ID = 0;
scroggo@google.comf8d7d272013-02-22 21:38:35 +000089
90#ifdef SK_DEBUG
scroggo@google.combb281f72013-03-18 21:37:39 +000091 /**
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.comf8d7d272013-02-22 21:38:35 +0000115 };
116
117 /**
scroggo@google.combb281f72013-03-18 21:37:39 +0000118 * 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.comf8d7d272013-02-22 21:38:35 +0000120 */
scroggo@google.combb281f72013-03-18 21:37:39 +0000121 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.comf8d7d272013-02-22 21:38:35 +0000127#endif
commit-bot@chromium.orgef284a82013-07-11 22:29:29 +0000128
129private:
130 typedef SkRefCnt INHERITED;
scroggo@google.comf8d7d272013-02-22 21:38:35 +0000131};
132#endif // SkImageCache_DEFINED