blob: 045ce2c384f168c94129408d65b67ffaa6274d14 [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
25 * associated with that memory which can be used as a parameter to the other functions
26 * in SkImageCache. On failure, ID is unchanged.
27 * @return Pointer to the newly allocated memory, or NULL. This memory is safe to use until
28 * releaseCache is called with ID.
29 */
30 virtual void* allocAndPinCache(size_t bytes, intptr_t* ID) = 0;
31
32 /**
33 * Re-request the memory associated with ID.
34 * @param ID Unique ID for the memory block.
35 * @return Pointer: If non-NULL, points to the previously allocated memory, in which case
36 * this call must be balanced with a call to releaseCache. If NULL, the memory
37 * has been reclaimed, so allocAndPinCache must be called again, and ID is no
38 * longer valid (thus throwAwayCache need not be called).
39 */
40 virtual void* pinCache(intptr_t ID) = 0;
41
42 /**
43 * Inform the cache that it is safe to free the block of memory corresponding to ID. After
44 * calling this function, the pointer returnted by allocAndPinCache or pinCache must not be
45 * used again. In order to access the same memory after this, pinCache must be called.
46 * @param ID Unique ID for the memory block which is now safe to age out of the cache.
47 */
48 virtual void releaseCache(intptr_t ID) = 0;
49
50 /**
51 * Inform the cache that the block of memory associated with ID will not be asked for again.
52 * After this call, ID is no longer valid. Must not be called while the associated memory is
53 * pinned. Must be called to balance a successful allocAndPinCache, unless a later pinCache
54 * returns NULL.
55 */
56 virtual void throwAwayCache(intptr_t ID) = 0;
57
58 /**
59 * ID which does not correspond to any valid cache.
60 */
61 static const intptr_t UNINITIALIZED_ID = 0;
62
63#ifdef SK_DEBUG
64 enum CacheStatus {
65 kPinned_CacheStatus,
66 kUnpinned_CacheStatus,
67 kThrownAway_CacheStatus,
68 };
69
70 /**
71 * Debug only function to get the status of a particular block of memory.
72 */
73 virtual CacheStatus getCacheStatus(intptr_t ID) const = 0;
74#endif
75};
76#endif // SkImageCache_DEFINED