OLD | NEW |
(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 "SkTypes.h" |
| 12 |
| 13 class SkDiscardableMemory; |
| 14 |
| 15 class SkCachedData : ::SkNoncopyable { |
| 16 public: |
| 17 static SkCachedData* Create(size_t bytes, SkDiscardableMemory* = NULL); |
| 18 |
| 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(); |
| 27 void unref(); |
| 28 |
| 29 int testing_only_getRefCnt() const { return fRefCnt >> 1; } |
| 30 bool testing_only_isInCache() const { return fRefCnt & 1; } |
| 31 |
| 32 protected: |
| 33 SkCachedData(void* data, size_t size); |
| 34 |
| 35 virtual void onLock() = 0; // called when owner count rises above 1 |
| 36 virtual void onUnlock() = 0; // called when owner count falls back to 1 |
| 37 |
| 38 void setData(void* data) { fData = data; } |
| 39 |
| 40 private: |
| 41 void* fData; |
| 42 size_t fSize; |
| 43 int32_t fRefCnt; // low-bit means we're owned by the cache |
| 44 |
| 45 void lock() { |
| 46 this->onLock(); |
| 47 } |
| 48 |
| 49 void unlock() { |
| 50 this->onUnlock(); |
| 51 fData = NULL; // signal that we're unlocked |
| 52 } |
| 53 |
| 54 public: |
| 55 void ref_from_cache(); |
| 56 void unref_from_cache(); |
| 57 }; |
| 58 |
| 59 #endif |
OLD | NEW |