blob: 7c4926109aeb87260f525f848bef5d12e142e87a [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:
reed@google.com6e8b7dd2013-07-09 21:31:54 +000020 typedef intptr_t ID;
21
scroggo@google.comf8d7d272013-02-22 21:38:35 +000022 /**
23 * Allocate memory whose lifetime is managed by the cache. On success, MUST be balanced with a
24 * call to releaseCache and a call to throwAwayCache.
25 * @param bytes Number of bytes needed.
26 * @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 +000027 * associated with that memory which can be used as a parameter to the other functions
28 * in SkImageCache. On failure, ID is unchanged.
scroggo@google.comf8d7d272013-02-22 21:38:35 +000029 * @return Pointer to the newly allocated memory, or NULL. This memory is safe to use until
scroggo@google.combb281f72013-03-18 21:37:39 +000030 * releaseCache is called with ID.
scroggo@google.comf8d7d272013-02-22 21:38:35 +000031 */
reed@google.com6e8b7dd2013-07-09 21:31:54 +000032 virtual void* allocAndPinCache(size_t bytes, ID*) = 0;
scroggo@google.comf8d7d272013-02-22 21:38:35 +000033
34 /**
scroggo@google.combb281f72013-03-18 21:37:39 +000035 * Output parameter for pinCache, stating whether the memory still contains the data it held
36 * when releaseCache was last called for the same ID.
scroggo@google.comf8d7d272013-02-22 21:38:35 +000037 */
scroggo@google.combb281f72013-03-18 21:37:39 +000038 enum DataStatus {
39 /**
40 * The data has been purged, and therefore needs to be rewritten to the returned memory.
41 */
42 kUninitialized_DataStatus,
43
44 /**
45 * The memory still contains the data it held when releaseCache was last called with the
46 * same ID.
47 */
48 kRetained_DataStatus,
49 };
50
51 /**
52 * Re-request the memory associated with ID and pin it so that it will not be reclaimed until
53 * the next call to releaseCache with the same ID.
54 * @param ID Unique ID for the memory block.
55 * @param status Output parameter which must not be NULL. On success (i.e. the return value is
56 * not NULL), status will be set to one of two states representing the cached memory. If
57 * status is set to kRetained_DataStatus, the memory contains the same data it did
58 * before releaseCache was called with this ID. If status is set to
59 * kUninitialized_DataStatus, the memory is still pinned, but the previous data is no
60 * longer available. If the return value is NULL, status is unchanged.
61 * @return Pointer: If non-NULL, points to the previously allocated memory, in which case
62 * this call must be balanced with a call to releaseCache. If NULL, the memory
63 * has been reclaimed, and throwAwayCache MUST NOT be called.
64 */
reed@google.com6e8b7dd2013-07-09 21:31:54 +000065 virtual void* pinCache(ID, DataStatus* status) = 0;
scroggo@google.comf8d7d272013-02-22 21:38:35 +000066
67 /**
68 * 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 +000069 * calling this function, the pointer returned by allocAndPinCache or pinCache must not be
70 * used again. In order to access the same memory after this, pinCache must be called with
71 * the same ID.
scroggo@google.comf8d7d272013-02-22 21:38:35 +000072 * @param ID Unique ID for the memory block which is now safe to age out of the cache.
73 */
reed@google.com6e8b7dd2013-07-09 21:31:54 +000074 virtual void releaseCache(ID) = 0;
scroggo@google.comf8d7d272013-02-22 21:38:35 +000075
76 /**
77 * Inform the cache that the block of memory associated with ID will not be asked for again.
78 * 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 +000079 * pinned. Must be called to balance a successful allocAndPinCache.
scroggo@google.comf8d7d272013-02-22 21:38:35 +000080 */
reed@google.com6e8b7dd2013-07-09 21:31:54 +000081 virtual void throwAwayCache(ID) = 0;
scroggo@google.comf8d7d272013-02-22 21:38:35 +000082
83 /**
84 * ID which does not correspond to any valid cache.
85 */
reed@google.com6e8b7dd2013-07-09 21:31:54 +000086 static const ID UNINITIALIZED_ID = 0;
scroggo@google.comf8d7d272013-02-22 21:38:35 +000087
88#ifdef SK_DEBUG
scroggo@google.combb281f72013-03-18 21:37:39 +000089 /**
90 * Debug only status of a memory block.
91 */
92 enum MemoryStatus {
93 /**
94 * It is safe to use the pointer returned by the most recent of allocAndPinCache(ID) or
95 * pinCache(ID) with the same ID.
96 */
97 kPinned_MemoryStatus,
98
99 /**
100 * The pointer returned by the most recent call to allocAndPinCache(ID) or pinCache(ID) has
101 * since been released by releaseCache(ID). In order to reuse it, pinCache(ID) must be
102 * called again. Note that after calling releaseCache(ID), the status of that particular
103 * ID may not be kUnpinned_MemoryStatus, depending on the implementation, but it will not
104 * be kPinned_MemoryStatus.
105 */
106 kUnpinned_MemoryStatus,
107
108 /**
109 * The memory associated with ID has been thrown away. No calls should be made using the
110 * same ID.
111 */
112 kFreed_MemoryStatus,
scroggo@google.comf8d7d272013-02-22 21:38:35 +0000113 };
114
115 /**
scroggo@google.combb281f72013-03-18 21:37:39 +0000116 * Debug only function to get the status of a particular block of memory. Safe to call after
117 * throwAwayCache has been called with this ID.
scroggo@google.comf8d7d272013-02-22 21:38:35 +0000118 */
scroggo@google.combb281f72013-03-18 21:37:39 +0000119 virtual MemoryStatus getMemoryStatus(intptr_t ID) const = 0;
120
121 /**
122 * Debug only function to clear all unpinned caches.
123 */
124 virtual void purgeAllUnpinnedCaches() = 0;
scroggo@google.comf8d7d272013-02-22 21:38:35 +0000125#endif
126};
127#endif // SkImageCache_DEFINED