Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(764)

Side by Side Diff: src/core/SkScaledImageCache.h

Issue 105933003: support scaledimagecache instantiable using discardablememory (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: rebase Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « src/core/SkBitmapProcState.cpp ('k') | src/core/SkScaledImageCache.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2013 Google Inc. 2 * Copyright 2013 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #ifndef SkScaledImageCache_DEFINED 8 #ifndef SkScaledImageCache_DEFINED
9 #define SkScaledImageCache_DEFINED 9 #define SkScaledImageCache_DEFINED
10 10
11 #include "SkBitmap.h" 11 #include "SkBitmap.h"
12 12
13 class SkDiscardableMemory;
13 class SkMipMap; 14 class SkMipMap;
14 15
15 /** 16 /**
16 * Cache object for bitmaps (with possible scale in X Y as part of the key). 17 * Cache object for bitmaps (with possible scale in X Y as part of the key).
17 * 18 *
18 * Multiple caches can be instantiated, but each instance is not implicitly 19 * Multiple caches can be instantiated, but each instance is not implicitly
19 * thread-safe, so if a given instance is to be shared across threads, the 20 * thread-safe, so if a given instance is to be shared across threads, the
20 * caller must manage the access itself (e.g. via a mutex). 21 * caller must manage the access itself (e.g. via a mutex).
21 * 22 *
22 * As a convenience, a global instance is also defined, which can be safely 23 * As a convenience, a global instance is also defined, which can be safely
23 * access across threads via the static methods (e.g. FindAndLock, etc.). 24 * access across threads via the static methods (e.g. FindAndLock, etc.).
24 */ 25 */
25 class SkScaledImageCache { 26 class SkScaledImageCache {
26 public: 27 public:
27 struct ID; 28 struct ID;
28 29
30 /**
31 * Returns a locked/pinned SkDiscardableMemory instance for the specified
32 * number of bytes, or NULL on failure.
33 */
34 typedef SkDiscardableMemory* (*DiscardableFactory)(size_t bytes);
35
29 /* 36 /*
30 * The following static methods are thread-safe wrappers around a global 37 * The following static methods are thread-safe wrappers around a global
31 * instance of this cache. 38 * instance of this cache.
32 */ 39 */
33 40
34 static ID* FindAndLock(uint32_t pixelGenerationID, 41 static ID* FindAndLock(uint32_t pixelGenerationID,
35 int32_t width, 42 int32_t width,
36 int32_t height, 43 int32_t height,
37 SkBitmap* returnedBitmap); 44 SkBitmap* returnedBitmap);
38 45
(...skipping 11 matching lines...) Expand all
50 static ID* AddAndLock(const SkBitmap& original, SkScalar scaleX, 57 static ID* AddAndLock(const SkBitmap& original, SkScalar scaleX,
51 SkScalar scaleY, const SkBitmap& bitmap); 58 SkScalar scaleY, const SkBitmap& bitmap);
52 static ID* AddAndLockMip(const SkBitmap& original, const SkMipMap* mipMap); 59 static ID* AddAndLockMip(const SkBitmap& original, const SkMipMap* mipMap);
53 60
54 static void Unlock(ID*); 61 static void Unlock(ID*);
55 62
56 static size_t GetBytesUsed(); 63 static size_t GetBytesUsed();
57 static size_t GetByteLimit(); 64 static size_t GetByteLimit();
58 static size_t SetByteLimit(size_t newLimit); 65 static size_t SetByteLimit(size_t newLimit);
59 66
67 static SkBitmap::Allocator* GetAllocator();
68
60 /////////////////////////////////////////////////////////////////////////// 69 ///////////////////////////////////////////////////////////////////////////
61 70
71 /**
72 * Construct the cache to call DiscardableFactory when it
73 * allocates memory for the pixels. In this mode, the cache has
74 * not explicit budget, and so methods like getBytesUsed() and
75 * getByteLimit() will return 0, and setByteLimit will ignore its argument
76 * and return 0.
77 */
78 SkScaledImageCache(DiscardableFactory);
79
80 /**
81 * Construct the cache, allocating memory with malloc, and respect the
82 * byteLimit, purging automatically when a new image is added to the cache
83 * that pushes the total bytesUsed over the limit. Note: The limit can be
84 * changed at runtime with setByteLimit.
85 */
62 SkScaledImageCache(size_t byteLimit); 86 SkScaledImageCache(size_t byteLimit);
87
63 ~SkScaledImageCache(); 88 ~SkScaledImageCache();
64 89
65 /** 90 /**
66 * Search the cache for a matching bitmap (using generationID, 91 * Search the cache for a matching bitmap (using generationID,
67 * width, and height as a search key). If found, return it in 92 * width, and height as a search key). If found, return it in
68 * returnedBitmap, and return its ID pointer. Use the returned 93 * returnedBitmap, and return its ID pointer. Use the returned
69 * ptr to unlock the cache when you are done using 94 * ptr to unlock the cache when you are done using
70 * returnedBitmap. 95 * returnedBitmap.
71 * 96 *
72 * If a match is not found, returnedBitmap will be unmodifed, and 97 * If a match is not found, returnedBitmap will be unmodifed, and
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
117 size_t getBytesUsed() const { return fBytesUsed; } 142 size_t getBytesUsed() const { return fBytesUsed; }
118 size_t getByteLimit() const { return fByteLimit; } 143 size_t getByteLimit() const { return fByteLimit; }
119 144
120 /** 145 /**
121 * Set the maximum number of bytes available to this cache. If the current 146 * Set the maximum number of bytes available to this cache. If the current
122 * cache exceeds this new value, it will be purged to try to fit within 147 * cache exceeds this new value, it will be purged to try to fit within
123 * this new limit. 148 * this new limit.
124 */ 149 */
125 size_t setByteLimit(size_t newLimit); 150 size_t setByteLimit(size_t newLimit);
126 151
152 SkBitmap::Allocator* allocator() const { return fAllocator; };
153
127 public: 154 public:
128 struct Rec; 155 struct Rec;
129 struct Key; 156 struct Key;
130 private: 157 private:
131 Rec* fHead; 158 Rec* fHead;
132 Rec* fTail; 159 Rec* fTail;
133 160
134 class Hash; 161 class Hash;
135 Hash* fHash; 162 Hash* fHash;
136 163
164 DiscardableFactory fDiscardableFactory;
165 // the allocator is NULL or one that matches discardables
166 SkBitmap::Allocator* fAllocator;
167
137 size_t fBytesUsed; 168 size_t fBytesUsed;
138 size_t fByteLimit; 169 size_t fByteLimit;
139 int fCount; 170 int fCount;
140 171
141 Rec* findAndLock(uint32_t generationID, SkScalar sx, SkScalar sy, 172 Rec* findAndLock(uint32_t generationID, SkScalar sx, SkScalar sy,
142 const SkIRect& bounds); 173 const SkIRect& bounds);
143 Rec* findAndLock(const Key& key); 174 Rec* findAndLock(const Key& key);
144 ID* addAndLock(Rec* rec); 175 ID* addAndLock(Rec* rec);
145 176
146 void purgeAsNeeded(); 177 void purgeAsNeeded();
147 178
148 // linklist management 179 // linklist management
149 void moveToHead(Rec*); 180 void moveToHead(Rec*);
150 void addToHead(Rec*); 181 void addToHead(Rec*);
151 void detach(Rec*); 182 void detach(Rec*);
183
184 void init(); // called by constructors
185
152 #ifdef SK_DEBUG 186 #ifdef SK_DEBUG
153 void validate() const; 187 void validate() const;
154 #else 188 #else
155 void validate() const {} 189 void validate() const {}
156 #endif 190 #endif
157 }; 191 };
158 #endif 192 #endif
OLDNEW
« no previous file with comments | « src/core/SkBitmapProcState.cpp ('k') | src/core/SkScaledImageCache.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698