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

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

Issue 592843003: Add SkCachedData and use it for SkMipMap (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: use natural bools for state tracking Created 6 years, 2 months 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
OLDNEW
(Empty)
1 /*
2 * Copyright 2014 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 SkCachedData_DEFINED
9 #define SkCachedData_DEFINED
10
11 #include "SkThread.h"
12
13 class SkDiscardableMemory;
14
15 class SkCachedData : ::SkNoncopyable {
16 public:
17 SkCachedData(void* mallocData, size_t size);
18 SkCachedData(size_t size, SkDiscardableMemory*);
19 virtual ~SkCachedData();
20
21 size_t size() const { return fSize; }
22 const void* data() const { return fData; }
23
24 void* writable_data() { return fData; }
25
26 void ref() const { this->internalRef(false); }
27 void unref() const { this->internalUnref(false); }
28
29 int testing_only_getRefCnt() const { return fRefCnt; }
30 bool testing_only_isLocked() const { return fIsLocked; }
31 bool testing_only_isInCache() const { return fInCache; }
32
33 protected:
34 // called when fData changes. could be NULL.
35 virtual void onDataChange(void* oldData, void* newData) {}
36
37 private:
38 SkMutex fMutex; // could use a pool of these...
39
40 enum StorageType {
41 kDiscardableMemory_StorageType,
42 kMalloc_StorageType
43 };
44
45 union {
46 SkDiscardableMemory* fDM;
47 void* fMalloc;
48 } fStorage;
49 void* fData;
50 size_t fSize;
51 int fRefCnt; // low-bit means we're owned by the cache
52 StorageType fStorageType;
53 bool fInCache;
54 bool fIsLocked;
55
56 void internalRef(bool fromCache) const;
57 void internalUnref(bool fromCache) const;
58 bool doInternalUnref(bool fromCache) const;
59
60 SkMutex& get_mutex() { return fMutex; }
mtklein 2014/09/27 14:47:49 breaking your own naming rules for these next 4 me
reed1 2014/09/27 17:25:58 Done.
61 void in_mutex_lock();
62 void in_mutex_unlock();
63
64 void assert_in_mutex() {
65 fMutex.assertHeld();
66 }
67
68 // called whenever our fData might change (lock or unlock)
69 void setData(void* newData) {
70 if (newData != fData) {
71 // notify our subclasses of the change
72 this->onDataChange(fData, newData);
73 fData = newData;
74 }
75 }
76
77 class AutoMutexWritable;
78 friend class AutoMutexWritable;
mtklein 2014/09/27 14:47:49 I _think_ you can drop this? The aren't nested cl
reed1 2014/09/27 17:25:58 Done.
79
80 public:
81 #ifdef SK_DEBUG
82 void validate() const;
83 #else
84 void validate() const {}
85 #endif
86
87 /*
88 * Attaching a data to to a SkResourceCache (only one at a time) enables th e data to be
89 * unlocked when the cache is the only owner, thus freeing it to be purged (assuming the
90 * data is backed by a SkDiscardableMemory).
91 *
92 * When attached, it also automatically attempts to "lock" the data when th e first client
93 * ref's the data (typically from a find(key, visitor) call).
94 *
95 * Thus the data will always be "locked" when a non-cache has a ref on it ( whether or not
96 * the lock succeeded to recover the memory -- check data() to see if it is NULL).
97 */
98
99 /*
100 * Call when adding this instance to a SkResourceCache::Rec subclass
101 * (typically in the Rec's constructor).
102 */
103 void attachToCacheAndRef() const { this->internalRef(true); }
104
105 /*
106 * Call when removing this instance from a SkResourceCache::Rec subclass
107 * (typically in the Rec's destructor).
108 */
109 void detachFromCacheAndUnref() const { this->internalUnref(true); }
110 };
111
112 #endif
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698