blob: 404eace479831ab25c8d2b338538bd1c45274747 [file] [log] [blame]
reed@google.comdc731fd2010-12-23 15:19:47 +00001/*
reed@google.com57c3a862010-12-23 20:34:08 +00002 Copyright 2010 Google Inc.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
reed@google.comdc731fd2010-12-23 15:19:47 +000015 */
16
17#include "SkBitmapCache.h"
18
19struct SkBitmapCache::Entry {
20 Entry* fPrev;
21 Entry* fNext;
22
23 void* fBuffer;
24 size_t fSize;
25 SkBitmap fBitmap;
26
vandebo@chromium.org3c898182011-06-22 05:01:28 +000027 Entry(const void* buffer, size_t size, const SkBitmap& bm)
28 : fPrev(NULL),
29 fNext(NULL),
30 fBitmap(bm) {
reed@google.comdc731fd2010-12-23 15:19:47 +000031 fBuffer = sk_malloc_throw(size);
32 fSize = size;
33 memcpy(fBuffer, buffer, size);
34 }
35
36 ~Entry() { sk_free(fBuffer); }
37
38 bool equals(const void* buffer, size_t size) const {
39 return (fSize == size) && !memcmp(fBuffer, buffer, size);
40 }
41};
42
43SkBitmapCache::SkBitmapCache(int max) : fMaxEntries(max) {
44 fEntryCount = 0;
45 fHead = fTail = NULL;
46
47 this->validate();
48}
49
50SkBitmapCache::~SkBitmapCache() {
51 this->validate();
52
53 Entry* entry = fHead;
54 while (entry) {
55 Entry* next = entry->fNext;
56 delete entry;
57 entry = next;
58 }
59}
60
61SkBitmapCache::Entry* SkBitmapCache::detach(Entry* entry) const {
62 if (entry->fPrev) {
63 SkASSERT(fHead != entry);
64 entry->fPrev->fNext = entry->fNext;
65 } else {
66 SkASSERT(fHead == entry);
67 fHead = entry->fNext;
68 }
69 if (entry->fNext) {
70 SkASSERT(fTail != entry);
71 entry->fNext->fPrev = entry->fPrev;
72 } else {
73 SkASSERT(fTail == entry);
74 fTail = entry->fPrev;
75 }
76 return entry;
77}
78
79void SkBitmapCache::attachToHead(Entry* entry) const {
80 entry->fPrev = NULL;
81 entry->fNext = fHead;
82 if (fHead) {
83 fHead->fPrev = entry;
84 } else {
85 fTail = entry;
86 }
87 fHead = entry;
88}
89
90bool SkBitmapCache::find(const void* buffer, size_t size, SkBitmap* bm) const {
91 AutoValidate av(this);
92
93 Entry* entry = fHead;
94 while (entry) {
95 if (entry->equals(buffer, size)) {
96 if (bm) {
97 *bm = entry->fBitmap;
98 }
99 // move to the head of our list, so we purge it last
100 this->detach(entry);
101 this->attachToHead(entry);
102 return true;
103 }
104 entry = entry->fNext;
105 }
106 return false;
107}
108
109void SkBitmapCache::add(const void* buffer, size_t len, const SkBitmap& bm) {
110 AutoValidate av(this);
reed@google.come49d5712011-01-19 14:30:26 +0000111
reed@google.comdc731fd2010-12-23 15:19:47 +0000112 if (fEntryCount == fMaxEntries) {
reed@google.come49d5712011-01-19 14:30:26 +0000113 SkASSERT(fTail);
114 delete this->detach(fTail);
115 fEntryCount -= 1;
reed@google.comdc731fd2010-12-23 15:19:47 +0000116 }
117
118 Entry* entry = new Entry(buffer, len, bm);
119 this->attachToHead(entry);
120 fEntryCount += 1;
121}
122
123///////////////////////////////////////////////////////////////////////////////
124
125#ifdef SK_DEBUG
126
127void SkBitmapCache::validate() const {
128 SkASSERT(fEntryCount >= 0 && fEntryCount <= fMaxEntries);
reed@google.come49d5712011-01-19 14:30:26 +0000129
reed@google.comdc731fd2010-12-23 15:19:47 +0000130 if (fEntryCount > 0) {
131 SkASSERT(NULL == fHead->fPrev);
132 SkASSERT(NULL == fTail->fNext);
133
134 if (fEntryCount == 1) {
135 SkASSERT(fHead == fTail);
136 } else {
137 SkASSERT(fHead != fTail);
138 }
139
140 Entry* entry = fHead;
141 int count = 0;
142 while (entry) {
143 count += 1;
144 entry = entry->fNext;
145 }
146 SkASSERT(count == fEntryCount);
reed@google.come49d5712011-01-19 14:30:26 +0000147
reed@google.comdc731fd2010-12-23 15:19:47 +0000148 entry = fTail;
149 while (entry) {
150 count -= 1;
151 entry = entry->fPrev;
152 }
153 SkASSERT(0 == count);
154 } else {
155 SkASSERT(NULL == fHead);
156 SkASSERT(NULL == fTail);
reed@google.come49d5712011-01-19 14:30:26 +0000157 }
reed@google.comdc731fd2010-12-23 15:19:47 +0000158}
159
160#endif
161