OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2010 Google Inc. | 3 * Copyright 2010 Google Inc. |
4 * | 4 * |
5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
7 */ | 7 */ |
8 | 8 |
9 | 9 |
10 #include "SkBitmapCache.h" | 10 #include "SkGradientBitmapCache.h" |
11 | 11 |
12 struct SkBitmapCache::Entry { | 12 struct SkGradientBitmapCache::Entry { |
13 Entry* fPrev; | 13 Entry* fPrev; |
14 Entry* fNext; | 14 Entry* fNext; |
15 | 15 |
16 void* fBuffer; | 16 void* fBuffer; |
17 size_t fSize; | 17 size_t fSize; |
18 SkBitmap fBitmap; | 18 SkBitmap fBitmap; |
19 | 19 |
20 Entry(const void* buffer, size_t size, const SkBitmap& bm) | 20 Entry(const void* buffer, size_t size, const SkBitmap& bm) |
21 : fPrev(NULL), | 21 : fPrev(NULL), |
22 fNext(NULL), | 22 fNext(NULL), |
23 fBitmap(bm) { | 23 fBitmap(bm) { |
24 fBuffer = sk_malloc_throw(size); | 24 fBuffer = sk_malloc_throw(size); |
25 fSize = size; | 25 fSize = size; |
26 memcpy(fBuffer, buffer, size); | 26 memcpy(fBuffer, buffer, size); |
27 } | 27 } |
28 | 28 |
29 ~Entry() { sk_free(fBuffer); } | 29 ~Entry() { sk_free(fBuffer); } |
30 | 30 |
31 bool equals(const void* buffer, size_t size) const { | 31 bool equals(const void* buffer, size_t size) const { |
32 return (fSize == size) && !memcmp(fBuffer, buffer, size); | 32 return (fSize == size) && !memcmp(fBuffer, buffer, size); |
33 } | 33 } |
34 }; | 34 }; |
35 | 35 |
36 SkBitmapCache::SkBitmapCache(int max) : fMaxEntries(max) { | 36 SkGradientBitmapCache::SkGradientBitmapCache(int max) : fMaxEntries(max) { |
37 fEntryCount = 0; | 37 fEntryCount = 0; |
38 fHead = fTail = NULL; | 38 fHead = fTail = NULL; |
39 | 39 |
40 this->validate(); | 40 this->validate(); |
41 } | 41 } |
42 | 42 |
43 SkBitmapCache::~SkBitmapCache() { | 43 SkGradientBitmapCache::~SkGradientBitmapCache() { |
44 this->validate(); | 44 this->validate(); |
45 | 45 |
46 Entry* entry = fHead; | 46 Entry* entry = fHead; |
47 while (entry) { | 47 while (entry) { |
48 Entry* next = entry->fNext; | 48 Entry* next = entry->fNext; |
49 delete entry; | 49 delete entry; |
50 entry = next; | 50 entry = next; |
51 } | 51 } |
52 } | 52 } |
53 | 53 |
54 SkBitmapCache::Entry* SkBitmapCache::detach(Entry* entry) const { | 54 SkGradientBitmapCache::Entry* SkGradientBitmapCache::detach(Entry* entry) const
{ |
55 if (entry->fPrev) { | 55 if (entry->fPrev) { |
56 SkASSERT(fHead != entry); | 56 SkASSERT(fHead != entry); |
57 entry->fPrev->fNext = entry->fNext; | 57 entry->fPrev->fNext = entry->fNext; |
58 } else { | 58 } else { |
59 SkASSERT(fHead == entry); | 59 SkASSERT(fHead == entry); |
60 fHead = entry->fNext; | 60 fHead = entry->fNext; |
61 } | 61 } |
62 if (entry->fNext) { | 62 if (entry->fNext) { |
63 SkASSERT(fTail != entry); | 63 SkASSERT(fTail != entry); |
64 entry->fNext->fPrev = entry->fPrev; | 64 entry->fNext->fPrev = entry->fPrev; |
65 } else { | 65 } else { |
66 SkASSERT(fTail == entry); | 66 SkASSERT(fTail == entry); |
67 fTail = entry->fPrev; | 67 fTail = entry->fPrev; |
68 } | 68 } |
69 return entry; | 69 return entry; |
70 } | 70 } |
71 | 71 |
72 void SkBitmapCache::attachToHead(Entry* entry) const { | 72 void SkGradientBitmapCache::attachToHead(Entry* entry) const { |
73 entry->fPrev = NULL; | 73 entry->fPrev = NULL; |
74 entry->fNext = fHead; | 74 entry->fNext = fHead; |
75 if (fHead) { | 75 if (fHead) { |
76 fHead->fPrev = entry; | 76 fHead->fPrev = entry; |
77 } else { | 77 } else { |
78 fTail = entry; | 78 fTail = entry; |
79 } | 79 } |
80 fHead = entry; | 80 fHead = entry; |
81 } | 81 } |
82 | 82 |
83 bool SkBitmapCache::find(const void* buffer, size_t size, SkBitmap* bm) const { | 83 bool SkGradientBitmapCache::find(const void* buffer, size_t size, SkBitmap* bm)
const { |
84 AutoValidate av(this); | 84 AutoValidate av(this); |
85 | 85 |
86 Entry* entry = fHead; | 86 Entry* entry = fHead; |
87 while (entry) { | 87 while (entry) { |
88 if (entry->equals(buffer, size)) { | 88 if (entry->equals(buffer, size)) { |
89 if (bm) { | 89 if (bm) { |
90 *bm = entry->fBitmap; | 90 *bm = entry->fBitmap; |
91 } | 91 } |
92 // move to the head of our list, so we purge it last | 92 // move to the head of our list, so we purge it last |
93 this->detach(entry); | 93 this->detach(entry); |
94 this->attachToHead(entry); | 94 this->attachToHead(entry); |
95 return true; | 95 return true; |
96 } | 96 } |
97 entry = entry->fNext; | 97 entry = entry->fNext; |
98 } | 98 } |
99 return false; | 99 return false; |
100 } | 100 } |
101 | 101 |
102 void SkBitmapCache::add(const void* buffer, size_t len, const SkBitmap& bm) { | 102 void SkGradientBitmapCache::add(const void* buffer, size_t len, const SkBitmap&
bm) { |
103 AutoValidate av(this); | 103 AutoValidate av(this); |
104 | 104 |
105 if (fEntryCount == fMaxEntries) { | 105 if (fEntryCount == fMaxEntries) { |
106 SkASSERT(fTail); | 106 SkASSERT(fTail); |
107 delete this->detach(fTail); | 107 delete this->detach(fTail); |
108 fEntryCount -= 1; | 108 fEntryCount -= 1; |
109 } | 109 } |
110 | 110 |
111 Entry* entry = SkNEW_ARGS(Entry, (buffer, len, bm)); | 111 Entry* entry = SkNEW_ARGS(Entry, (buffer, len, bm)); |
112 this->attachToHead(entry); | 112 this->attachToHead(entry); |
113 fEntryCount += 1; | 113 fEntryCount += 1; |
114 } | 114 } |
115 | 115 |
116 /////////////////////////////////////////////////////////////////////////////// | 116 /////////////////////////////////////////////////////////////////////////////// |
117 | 117 |
118 #ifdef SK_DEBUG | 118 #ifdef SK_DEBUG |
119 | 119 |
120 void SkBitmapCache::validate() const { | 120 void SkGradientBitmapCache::validate() const { |
121 SkASSERT(fEntryCount >= 0 && fEntryCount <= fMaxEntries); | 121 SkASSERT(fEntryCount >= 0 && fEntryCount <= fMaxEntries); |
122 | 122 |
123 if (fEntryCount > 0) { | 123 if (fEntryCount > 0) { |
124 SkASSERT(NULL == fHead->fPrev); | 124 SkASSERT(NULL == fHead->fPrev); |
125 SkASSERT(NULL == fTail->fNext); | 125 SkASSERT(NULL == fTail->fNext); |
126 | 126 |
127 if (fEntryCount == 1) { | 127 if (fEntryCount == 1) { |
128 SkASSERT(fHead == fTail); | 128 SkASSERT(fHead == fTail); |
129 } else { | 129 } else { |
130 SkASSERT(fHead != fTail); | 130 SkASSERT(fHead != fTail); |
(...skipping 13 matching lines...) Expand all Loading... |
144 entry = entry->fPrev; | 144 entry = entry->fPrev; |
145 } | 145 } |
146 SkASSERT(0 == count); | 146 SkASSERT(0 == count); |
147 } else { | 147 } else { |
148 SkASSERT(NULL == fHead); | 148 SkASSERT(NULL == fHead); |
149 SkASSERT(NULL == fTail); | 149 SkASSERT(NULL == fTail); |
150 } | 150 } |
151 } | 151 } |
152 | 152 |
153 #endif | 153 #endif |
OLD | NEW |