blob: 6cd064ba5bfae6cd73672975080cf4d5d82ef318 [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
scroggo@google.comc75764e2013-03-04 21:38:50 +000037 * has been reclaimed, so allocAndPinCache must be called again with a pointer to
38 * the same ID.
scroggo@google.comf8d7d272013-02-22 21:38:35 +000039 */
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
scroggo@google.comc75764e2013-03-04 21:38:50 +000044 * calling this function, the pointer returned by allocAndPinCache or pinCache must not be
45 * used again. In order to access the same memory after this, pinCache must be called with
46 * the same ID.
scroggo@google.comf8d7d272013-02-22 21:38:35 +000047 * @param ID Unique ID for the memory block which is now safe to age out of the cache.
48 */
49 virtual void releaseCache(intptr_t ID) = 0;
50
51 /**
52 * Inform the cache that the block of memory associated with ID will not be asked for again.
53 * 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 +000054 * pinned. Must be called to balance a successful allocAndPinCache.
scroggo@google.comf8d7d272013-02-22 21:38:35 +000055 */
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