gfx/skia/trunk/src/gpu/GrResourceCache.h

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1
michael@0 2 /*
michael@0 3 * Copyright 2011 Google Inc.
michael@0 4 *
michael@0 5 * Use of this source code is governed by a BSD-style license that can be
michael@0 6 * found in the LICENSE file.
michael@0 7 */
michael@0 8
michael@0 9
michael@0 10
michael@0 11 #ifndef GrResourceCache_DEFINED
michael@0 12 #define GrResourceCache_DEFINED
michael@0 13
michael@0 14 #include "GrConfig.h"
michael@0 15 #include "GrTypes.h"
michael@0 16 #include "GrTMultiMap.h"
michael@0 17 #include "GrBinHashKey.h"
michael@0 18 #include "SkMessageBus.h"
michael@0 19 #include "SkTInternalLList.h"
michael@0 20
michael@0 21 class GrResource;
michael@0 22 class GrResourceEntry;
michael@0 23
michael@0 24 class GrResourceKey {
michael@0 25 public:
michael@0 26 static GrCacheID::Domain ScratchDomain() {
michael@0 27 static const GrCacheID::Domain gDomain = GrCacheID::GenerateDomain();
michael@0 28 return gDomain;
michael@0 29 }
michael@0 30
michael@0 31 /** Uniquely identifies the GrResource subclass in the key to avoid collisions
michael@0 32 across resource types. */
michael@0 33 typedef uint8_t ResourceType;
michael@0 34
michael@0 35 /** Flags set by the GrResource subclass. */
michael@0 36 typedef uint8_t ResourceFlags;
michael@0 37
michael@0 38 /** Generate a unique ResourceType */
michael@0 39 static ResourceType GenerateResourceType();
michael@0 40
michael@0 41 /** Creates a key for resource */
michael@0 42 GrResourceKey(const GrCacheID& id, ResourceType type, ResourceFlags flags) {
michael@0 43 this->init(id.getDomain(), id.getKey(), type, flags);
michael@0 44 };
michael@0 45
michael@0 46 GrResourceKey(const GrResourceKey& src) {
michael@0 47 fKey = src.fKey;
michael@0 48 }
michael@0 49
michael@0 50 GrResourceKey() {
michael@0 51 fKey.reset();
michael@0 52 }
michael@0 53
michael@0 54 void reset(const GrCacheID& id, ResourceType type, ResourceFlags flags) {
michael@0 55 this->init(id.getDomain(), id.getKey(), type, flags);
michael@0 56 }
michael@0 57
michael@0 58 uint32_t getHash() const {
michael@0 59 return fKey.getHash();
michael@0 60 }
michael@0 61
michael@0 62 bool isScratch() const {
michael@0 63 return ScratchDomain() ==
michael@0 64 *reinterpret_cast<const GrCacheID::Domain*>(fKey.getData() +
michael@0 65 kCacheIDDomainOffset);
michael@0 66 }
michael@0 67
michael@0 68 ResourceType getResourceType() const {
michael@0 69 return *reinterpret_cast<const ResourceType*>(fKey.getData() +
michael@0 70 kResourceTypeOffset);
michael@0 71 }
michael@0 72
michael@0 73 ResourceFlags getResourceFlags() const {
michael@0 74 return *reinterpret_cast<const ResourceFlags*>(fKey.getData() +
michael@0 75 kResourceFlagsOffset);
michael@0 76 }
michael@0 77
michael@0 78 bool operator==(const GrResourceKey& other) const { return fKey == other.fKey; }
michael@0 79
michael@0 80 private:
michael@0 81 enum {
michael@0 82 kCacheIDKeyOffset = 0,
michael@0 83 kCacheIDDomainOffset = kCacheIDKeyOffset + sizeof(GrCacheID::Key),
michael@0 84 kResourceTypeOffset = kCacheIDDomainOffset + sizeof(GrCacheID::Domain),
michael@0 85 kResourceFlagsOffset = kResourceTypeOffset + sizeof(ResourceType),
michael@0 86 kPadOffset = kResourceFlagsOffset + sizeof(ResourceFlags),
michael@0 87 kKeySize = SkAlign4(kPadOffset),
michael@0 88 kPadSize = kKeySize - kPadOffset
michael@0 89 };
michael@0 90
michael@0 91 void init(const GrCacheID::Domain domain,
michael@0 92 const GrCacheID::Key& key,
michael@0 93 ResourceType type,
michael@0 94 ResourceFlags flags) {
michael@0 95 union {
michael@0 96 uint8_t fKey8[kKeySize];
michael@0 97 uint32_t fKey32[kKeySize / 4];
michael@0 98 } keyData;
michael@0 99
michael@0 100 uint8_t* k = keyData.fKey8;
michael@0 101 memcpy(k + kCacheIDKeyOffset, key.fData8, sizeof(GrCacheID::Key));
michael@0 102 memcpy(k + kCacheIDDomainOffset, &domain, sizeof(GrCacheID::Domain));
michael@0 103 memcpy(k + kResourceTypeOffset, &type, sizeof(ResourceType));
michael@0 104 memcpy(k + kResourceFlagsOffset, &flags, sizeof(ResourceFlags));
michael@0 105 memset(k + kPadOffset, 0, kPadSize);
michael@0 106 fKey.setKeyData(keyData.fKey32);
michael@0 107 }
michael@0 108 GrBinHashKey<kKeySize> fKey;
michael@0 109 };
michael@0 110
michael@0 111 // The cache listens for these messages to purge junk resources proactively.
michael@0 112 struct GrResourceInvalidatedMessage {
michael@0 113 GrResourceKey key;
michael@0 114 };
michael@0 115
michael@0 116 ///////////////////////////////////////////////////////////////////////////////
michael@0 117
michael@0 118 class GrResourceEntry {
michael@0 119 public:
michael@0 120 GrResource* resource() const { return fResource; }
michael@0 121 const GrResourceKey& key() const { return fKey; }
michael@0 122
michael@0 123 static const GrResourceKey& GetKey(const GrResourceEntry& e) { return e.key(); }
michael@0 124 static uint32_t Hash(const GrResourceKey& key) { return key.getHash(); }
michael@0 125 static bool Equal(const GrResourceEntry& a, const GrResourceKey& b) {
michael@0 126 return a.key() == b;
michael@0 127 }
michael@0 128 #ifdef SK_DEBUG
michael@0 129 void validate() const;
michael@0 130 #else
michael@0 131 void validate() const {}
michael@0 132 #endif
michael@0 133
michael@0 134 private:
michael@0 135 GrResourceEntry(const GrResourceKey& key, GrResource* resource);
michael@0 136 ~GrResourceEntry();
michael@0 137
michael@0 138 GrResourceKey fKey;
michael@0 139 GrResource* fResource;
michael@0 140
michael@0 141 // Linked list for the LRU ordering.
michael@0 142 SK_DECLARE_INTERNAL_LLIST_INTERFACE(GrResourceEntry);
michael@0 143
michael@0 144 friend class GrResourceCache;
michael@0 145 };
michael@0 146
michael@0 147 ///////////////////////////////////////////////////////////////////////////////
michael@0 148
michael@0 149 /**
michael@0 150 * Cache of GrResource objects.
michael@0 151 *
michael@0 152 * These have a corresponding GrResourceKey, built from 128bits identifying the
michael@0 153 * resource. Multiple resources can map to same GrResourceKey.
michael@0 154 *
michael@0 155 * The cache stores the entries in a double-linked list, which is its LRU.
michael@0 156 * When an entry is "locked" (i.e. given to the caller), it is moved to the
michael@0 157 * head of the list. If/when we must purge some of the entries, we walk the
michael@0 158 * list backwards from the tail, since those are the least recently used.
michael@0 159 *
michael@0 160 * For fast searches, we maintain a hash map based on the GrResourceKey.
michael@0 161 *
michael@0 162 * It is a goal to make the GrResourceCache the central repository and bookkeeper
michael@0 163 * of all resources. It should replace the linked list of GrResources that
michael@0 164 * GrGpu uses to call abandon/release.
michael@0 165 */
michael@0 166 class GrResourceCache {
michael@0 167 public:
michael@0 168 GrResourceCache(int maxCount, size_t maxBytes);
michael@0 169 ~GrResourceCache();
michael@0 170
michael@0 171 /**
michael@0 172 * Return the current resource cache limits.
michael@0 173 *
michael@0 174 * @param maxResource If non-null, returns maximum number of resources
michael@0 175 * that can be held in the cache.
michael@0 176 * @param maxBytes If non-null, returns maximum number of bytes of
michael@0 177 * gpu memory that can be held in the cache.
michael@0 178 */
michael@0 179 void getLimits(int* maxResources, size_t* maxBytes) const;
michael@0 180
michael@0 181 /**
michael@0 182 * Specify the resource cache limits. If the current cache exceeds either
michael@0 183 * of these, it will be purged (LRU) to keep the cache within these limits.
michael@0 184 *
michael@0 185 * @param maxResources The maximum number of resources that can be held in
michael@0 186 * the cache.
michael@0 187 * @param maxBytes The maximum number of bytes of resource memory that
michael@0 188 * can be held in the cache.
michael@0 189 */
michael@0 190 void setLimits(int maxResources, size_t maxResourceBytes);
michael@0 191
michael@0 192 /**
michael@0 193 * The callback function used by the cache when it is still over budget
michael@0 194 * after a purge. The passed in 'data' is the same 'data' handed to
michael@0 195 * setOverbudgetCallback. The callback returns true if some resources
michael@0 196 * have been freed.
michael@0 197 */
michael@0 198 typedef bool (*PFOverbudgetCB)(void* data);
michael@0 199
michael@0 200 /**
michael@0 201 * Set the callback the cache should use when it is still over budget
michael@0 202 * after a purge. The 'data' provided here will be passed back to the
michael@0 203 * callback. Note that the cache will attempt to purge any resources newly
michael@0 204 * freed by the callback.
michael@0 205 */
michael@0 206 void setOverbudgetCallback(PFOverbudgetCB overbudgetCB, void* data) {
michael@0 207 fOverbudgetCB = overbudgetCB;
michael@0 208 fOverbudgetData = data;
michael@0 209 }
michael@0 210
michael@0 211 /**
michael@0 212 * Returns the number of bytes consumed by cached resources.
michael@0 213 */
michael@0 214 size_t getCachedResourceBytes() const { return fEntryBytes; }
michael@0 215
michael@0 216 // For a found or added resource to be completely exclusive to the caller
michael@0 217 // both the kNoOtherOwners and kHide flags need to be specified
michael@0 218 enum OwnershipFlags {
michael@0 219 kNoOtherOwners_OwnershipFlag = 0x1, // found/added resource has no other owners
michael@0 220 kHide_OwnershipFlag = 0x2 // found/added resource is hidden from future 'find's
michael@0 221 };
michael@0 222
michael@0 223 /**
michael@0 224 * Search for an entry with the same Key. If found, return it.
michael@0 225 * If not found, return null.
michael@0 226 * If ownershipFlags includes kNoOtherOwners and a resource is returned
michael@0 227 * then that resource has no other refs to it.
michael@0 228 * If ownershipFlags includes kHide and a resource is returned then that
michael@0 229 * resource will not be returned from future 'find' calls until it is
michael@0 230 * 'freed' (and recycled) or makeNonExclusive is called.
michael@0 231 * For a resource to be completely exclusive to a caller both kNoOtherOwners
michael@0 232 * and kHide must be specified.
michael@0 233 */
michael@0 234 GrResource* find(const GrResourceKey& key,
michael@0 235 uint32_t ownershipFlags = 0);
michael@0 236
michael@0 237 /**
michael@0 238 * Add the new resource to the cache (by creating a new cache entry based
michael@0 239 * on the provided key and resource).
michael@0 240 *
michael@0 241 * Ownership of the resource is transferred to the resource cache,
michael@0 242 * which will unref() it when it is purged or deleted.
michael@0 243 *
michael@0 244 * If ownershipFlags includes kHide, subsequent calls to 'find' will not
michael@0 245 * return 'resource' until it is 'freed' (and recycled) or makeNonExclusive
michael@0 246 * is called.
michael@0 247 */
michael@0 248 void addResource(const GrResourceKey& key,
michael@0 249 GrResource* resource,
michael@0 250 uint32_t ownershipFlags = 0);
michael@0 251
michael@0 252 /**
michael@0 253 * Determines if the cache contains an entry matching a key. If a matching
michael@0 254 * entry exists but was detached then it will not be found.
michael@0 255 */
michael@0 256 bool hasKey(const GrResourceKey& key) const { return NULL != fCache.find(key); }
michael@0 257
michael@0 258 /**
michael@0 259 * Hide 'entry' so that future searches will not find it. Such
michael@0 260 * hidden entries will not be purged. The entry still counts against
michael@0 261 * the cache's budget and should be made non-exclusive when exclusive access
michael@0 262 * is no longer needed.
michael@0 263 */
michael@0 264 void makeExclusive(GrResourceEntry* entry);
michael@0 265
michael@0 266 /**
michael@0 267 * Restore 'entry' so that it can be found by future searches. 'entry'
michael@0 268 * will also be purgeable (provided its lock count is now 0.)
michael@0 269 */
michael@0 270 void makeNonExclusive(GrResourceEntry* entry);
michael@0 271
michael@0 272 /**
michael@0 273 * Remove a resource from the cache and delete it!
michael@0 274 */
michael@0 275 void deleteResource(GrResourceEntry* entry);
michael@0 276
michael@0 277 /**
michael@0 278 * Removes every resource in the cache that isn't locked.
michael@0 279 */
michael@0 280 void purgeAllUnlocked();
michael@0 281
michael@0 282 /**
michael@0 283 * Allow cache to purge unused resources to obey resource limitations
michael@0 284 * Note: this entry point will be hidden (again) once totally ref-driven
michael@0 285 * cache maintenance is implemented. Note that the overbudget callback
michael@0 286 * will be called if the initial purge doesn't get the cache under
michael@0 287 * its budget.
michael@0 288 *
michael@0 289 * extraCount and extraBytes are added to the current resource allocation
michael@0 290 * to make sure enough room is available for future additions (e.g,
michael@0 291 * 10MB across 10 textures is about to be added).
michael@0 292 */
michael@0 293 void purgeAsNeeded(int extraCount = 0, size_t extraBytes = 0);
michael@0 294
michael@0 295 #ifdef SK_DEBUG
michael@0 296 void validate() const;
michael@0 297 #else
michael@0 298 void validate() const {}
michael@0 299 #endif
michael@0 300
michael@0 301 #if GR_CACHE_STATS
michael@0 302 void printStats();
michael@0 303 #endif
michael@0 304
michael@0 305 private:
michael@0 306 enum BudgetBehaviors {
michael@0 307 kAccountFor_BudgetBehavior,
michael@0 308 kIgnore_BudgetBehavior
michael@0 309 };
michael@0 310
michael@0 311 void internalDetach(GrResourceEntry*, BudgetBehaviors behavior = kAccountFor_BudgetBehavior);
michael@0 312 void attachToHead(GrResourceEntry*, BudgetBehaviors behavior = kAccountFor_BudgetBehavior);
michael@0 313
michael@0 314 void removeInvalidResource(GrResourceEntry* entry);
michael@0 315
michael@0 316 GrTMultiMap<GrResourceEntry,
michael@0 317 GrResourceKey,
michael@0 318 GrResourceEntry::GetKey,
michael@0 319 GrResourceEntry::Hash,
michael@0 320 GrResourceEntry::Equal> fCache;
michael@0 321
michael@0 322 // We're an internal doubly linked list
michael@0 323 typedef SkTInternalLList<GrResourceEntry> EntryList;
michael@0 324 EntryList fList;
michael@0 325
michael@0 326 #ifdef SK_DEBUG
michael@0 327 // These objects cannot be returned by a search
michael@0 328 EntryList fExclusiveList;
michael@0 329 #endif
michael@0 330
michael@0 331 // our budget, used in purgeAsNeeded()
michael@0 332 int fMaxCount;
michael@0 333 size_t fMaxBytes;
michael@0 334
michael@0 335 // our current stats, related to our budget
michael@0 336 #if GR_CACHE_STATS
michael@0 337 int fHighWaterEntryCount;
michael@0 338 size_t fHighWaterEntryBytes;
michael@0 339 int fHighWaterClientDetachedCount;
michael@0 340 size_t fHighWaterClientDetachedBytes;
michael@0 341 #endif
michael@0 342
michael@0 343 int fEntryCount;
michael@0 344 size_t fEntryBytes;
michael@0 345 int fClientDetachedCount;
michael@0 346 size_t fClientDetachedBytes;
michael@0 347
michael@0 348 // prevents recursive purging
michael@0 349 bool fPurging;
michael@0 350
michael@0 351 PFOverbudgetCB fOverbudgetCB;
michael@0 352 void* fOverbudgetData;
michael@0 353
michael@0 354 void internalPurge(int extraCount, size_t extraBytes);
michael@0 355
michael@0 356 // Listen for messages that a resource has been invalidated and purge cached junk proactively.
michael@0 357 SkMessageBus<GrResourceInvalidatedMessage>::Inbox fInvalidationInbox;
michael@0 358 void purgeInvalidated();
michael@0 359
michael@0 360 #ifdef SK_DEBUG
michael@0 361 static size_t countBytes(const SkTInternalLList<GrResourceEntry>& list);
michael@0 362 #endif
michael@0 363 };
michael@0 364
michael@0 365 ///////////////////////////////////////////////////////////////////////////////
michael@0 366
michael@0 367 #ifdef SK_DEBUG
michael@0 368 class GrAutoResourceCacheValidate {
michael@0 369 public:
michael@0 370 GrAutoResourceCacheValidate(GrResourceCache* cache) : fCache(cache) {
michael@0 371 cache->validate();
michael@0 372 }
michael@0 373 ~GrAutoResourceCacheValidate() {
michael@0 374 fCache->validate();
michael@0 375 }
michael@0 376 private:
michael@0 377 GrResourceCache* fCache;
michael@0 378 };
michael@0 379 #else
michael@0 380 class GrAutoResourceCacheValidate {
michael@0 381 public:
michael@0 382 GrAutoResourceCacheValidate(GrResourceCache*) {}
michael@0 383 };
michael@0 384 #endif
michael@0 385
michael@0 386 #endif

mercurial