gfx/skia/trunk/src/core/SkScaledImageCache.h

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

michael@0 1 /*
michael@0 2 * Copyright 2013 Google Inc.
michael@0 3 *
michael@0 4 * Use of this source code is governed by a BSD-style license that can be
michael@0 5 * found in the LICENSE file.
michael@0 6 */
michael@0 7
michael@0 8 #ifndef SkScaledImageCache_DEFINED
michael@0 9 #define SkScaledImageCache_DEFINED
michael@0 10
michael@0 11 #include "SkBitmap.h"
michael@0 12
michael@0 13 class SkDiscardableMemory;
michael@0 14 class SkMipMap;
michael@0 15
michael@0 16 /**
michael@0 17 * Cache object for bitmaps (with possible scale in X Y as part of the key).
michael@0 18 *
michael@0 19 * Multiple caches can be instantiated, but each instance is not implicitly
michael@0 20 * thread-safe, so if a given instance is to be shared across threads, the
michael@0 21 * caller must manage the access itself (e.g. via a mutex).
michael@0 22 *
michael@0 23 * As a convenience, a global instance is also defined, which can be safely
michael@0 24 * access across threads via the static methods (e.g. FindAndLock, etc.).
michael@0 25 */
michael@0 26 class SkScaledImageCache {
michael@0 27 public:
michael@0 28 struct ID;
michael@0 29
michael@0 30 /**
michael@0 31 * Returns a locked/pinned SkDiscardableMemory instance for the specified
michael@0 32 * number of bytes, or NULL on failure.
michael@0 33 */
michael@0 34 typedef SkDiscardableMemory* (*DiscardableFactory)(size_t bytes);
michael@0 35
michael@0 36 /*
michael@0 37 * The following static methods are thread-safe wrappers around a global
michael@0 38 * instance of this cache.
michael@0 39 */
michael@0 40
michael@0 41 static ID* FindAndLock(uint32_t pixelGenerationID,
michael@0 42 int32_t width,
michael@0 43 int32_t height,
michael@0 44 SkBitmap* returnedBitmap);
michael@0 45
michael@0 46 static ID* FindAndLock(const SkBitmap& original, SkScalar scaleX,
michael@0 47 SkScalar scaleY, SkBitmap* returnedBitmap);
michael@0 48 static ID* FindAndLockMip(const SkBitmap& original,
michael@0 49 SkMipMap const** returnedMipMap);
michael@0 50
michael@0 51
michael@0 52 static ID* AddAndLock(uint32_t pixelGenerationID,
michael@0 53 int32_t width,
michael@0 54 int32_t height,
michael@0 55 const SkBitmap& bitmap);
michael@0 56
michael@0 57 static ID* AddAndLock(const SkBitmap& original, SkScalar scaleX,
michael@0 58 SkScalar scaleY, const SkBitmap& bitmap);
michael@0 59 static ID* AddAndLockMip(const SkBitmap& original, const SkMipMap* mipMap);
michael@0 60
michael@0 61 static void Unlock(ID*);
michael@0 62
michael@0 63 static size_t GetBytesUsed();
michael@0 64 static size_t GetByteLimit();
michael@0 65 static size_t SetByteLimit(size_t newLimit);
michael@0 66
michael@0 67 static SkBitmap::Allocator* GetAllocator();
michael@0 68
michael@0 69 /**
michael@0 70 * Call SkDebugf() with diagnostic information about the state of the cache
michael@0 71 */
michael@0 72 static void Dump();
michael@0 73
michael@0 74 ///////////////////////////////////////////////////////////////////////////
michael@0 75
michael@0 76 /**
michael@0 77 * Construct the cache to call DiscardableFactory when it
michael@0 78 * allocates memory for the pixels. In this mode, the cache has
michael@0 79 * not explicit budget, and so methods like getBytesUsed() and
michael@0 80 * getByteLimit() will return 0, and setByteLimit will ignore its argument
michael@0 81 * and return 0.
michael@0 82 */
michael@0 83 SkScaledImageCache(DiscardableFactory);
michael@0 84
michael@0 85 /**
michael@0 86 * Construct the cache, allocating memory with malloc, and respect the
michael@0 87 * byteLimit, purging automatically when a new image is added to the cache
michael@0 88 * that pushes the total bytesUsed over the limit. Note: The limit can be
michael@0 89 * changed at runtime with setByteLimit.
michael@0 90 */
michael@0 91 SkScaledImageCache(size_t byteLimit);
michael@0 92
michael@0 93 ~SkScaledImageCache();
michael@0 94
michael@0 95 /**
michael@0 96 * Search the cache for a matching bitmap (using generationID,
michael@0 97 * width, and height as a search key). If found, return it in
michael@0 98 * returnedBitmap, and return its ID pointer. Use the returned
michael@0 99 * ptr to unlock the cache when you are done using
michael@0 100 * returnedBitmap.
michael@0 101 *
michael@0 102 * If a match is not found, returnedBitmap will be unmodifed, and
michael@0 103 * NULL will be returned.
michael@0 104 *
michael@0 105 * This is used if there is no scaling or subsetting, for example
michael@0 106 * by SkLazyPixelRef.
michael@0 107 */
michael@0 108 ID* findAndLock(uint32_t pixelGenerationID, int32_t width, int32_t height,
michael@0 109 SkBitmap* returnedBitmap);
michael@0 110
michael@0 111 /**
michael@0 112 * Search the cache for a scaled version of original. If found,
michael@0 113 * return it in returnedBitmap, and return its ID pointer. Use
michael@0 114 * the returned ptr to unlock the cache when you are done using
michael@0 115 * returnedBitmap.
michael@0 116 *
michael@0 117 * If a match is not found, returnedBitmap will be unmodifed, and
michael@0 118 * NULL will be returned.
michael@0 119 */
michael@0 120 ID* findAndLock(const SkBitmap& original, SkScalar scaleX,
michael@0 121 SkScalar scaleY, SkBitmap* returnedBitmap);
michael@0 122 ID* findAndLockMip(const SkBitmap& original,
michael@0 123 SkMipMap const** returnedMipMap);
michael@0 124
michael@0 125 /**
michael@0 126 * To add a new bitmap (or mipMap) to the cache, call
michael@0 127 * AddAndLock. Use the returned ptr to unlock the cache when you
michael@0 128 * are done using scaled.
michael@0 129 *
michael@0 130 * Use (generationID, width, and height) or (original, scaleX,
michael@0 131 * scaleY) or (original) as a search key
michael@0 132 */
michael@0 133 ID* addAndLock(uint32_t pixelGenerationID, int32_t width, int32_t height,
michael@0 134 const SkBitmap& bitmap);
michael@0 135 ID* addAndLock(const SkBitmap& original, SkScalar scaleX,
michael@0 136 SkScalar scaleY, const SkBitmap& bitmap);
michael@0 137 ID* addAndLockMip(const SkBitmap& original, const SkMipMap* mipMap);
michael@0 138
michael@0 139 /**
michael@0 140 * Given a non-null ID ptr returned by either findAndLock or addAndLock,
michael@0 141 * this releases the associated resources to be available to be purged
michael@0 142 * if needed. After this, the cached bitmap should no longer be
michael@0 143 * referenced by the caller.
michael@0 144 */
michael@0 145 void unlock(ID*);
michael@0 146
michael@0 147 size_t getBytesUsed() const { return fBytesUsed; }
michael@0 148 size_t getByteLimit() const { return fByteLimit; }
michael@0 149
michael@0 150 /**
michael@0 151 * Set the maximum number of bytes available to this cache. If the current
michael@0 152 * cache exceeds this new value, it will be purged to try to fit within
michael@0 153 * this new limit.
michael@0 154 */
michael@0 155 size_t setByteLimit(size_t newLimit);
michael@0 156
michael@0 157 SkBitmap::Allocator* allocator() const { return fAllocator; };
michael@0 158
michael@0 159 /**
michael@0 160 * Call SkDebugf() with diagnostic information about the state of the cache
michael@0 161 */
michael@0 162 void dump() const;
michael@0 163
michael@0 164 public:
michael@0 165 struct Rec;
michael@0 166 struct Key;
michael@0 167 private:
michael@0 168 Rec* fHead;
michael@0 169 Rec* fTail;
michael@0 170
michael@0 171 class Hash;
michael@0 172 Hash* fHash;
michael@0 173
michael@0 174 DiscardableFactory fDiscardableFactory;
michael@0 175 // the allocator is NULL or one that matches discardables
michael@0 176 SkBitmap::Allocator* fAllocator;
michael@0 177
michael@0 178 size_t fBytesUsed;
michael@0 179 size_t fByteLimit;
michael@0 180 int fCount;
michael@0 181
michael@0 182 Rec* findAndLock(uint32_t generationID, SkScalar sx, SkScalar sy,
michael@0 183 const SkIRect& bounds);
michael@0 184 Rec* findAndLock(const Key& key);
michael@0 185 ID* addAndLock(Rec* rec);
michael@0 186
michael@0 187 void purgeRec(Rec*);
michael@0 188 void purgeAsNeeded();
michael@0 189
michael@0 190 // linklist management
michael@0 191 void moveToHead(Rec*);
michael@0 192 void addToHead(Rec*);
michael@0 193 void detach(Rec*);
michael@0 194
michael@0 195 void init(); // called by constructors
michael@0 196
michael@0 197 #ifdef SK_DEBUG
michael@0 198 void validate() const;
michael@0 199 #else
michael@0 200 void validate() const {}
michael@0 201 #endif
michael@0 202 };
michael@0 203 #endif

mercurial