OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkChecksum.h" | 8 #include "SkChecksum.h" |
9 #include "SkMessageBus.h" | 9 #include "SkMessageBus.h" |
10 #include "SkMipMap.h" | 10 #include "SkMipMap.h" |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
72 fTotalByteLimit = 0; | 72 fTotalByteLimit = 0; |
73 fDiscardableFactory = nullptr; | 73 fDiscardableFactory = nullptr; |
74 } | 74 } |
75 | 75 |
76 #include "SkDiscardableMemory.h" | 76 #include "SkDiscardableMemory.h" |
77 | 77 |
78 class SkOneShotDiscardablePixelRef : public SkPixelRef { | 78 class SkOneShotDiscardablePixelRef : public SkPixelRef { |
79 public: | 79 public: |
80 | 80 |
81 // Ownership of the discardablememory is transfered to the pixelref | 81 // Ownership of the discardablememory is transfered to the pixelref |
82 SkOneShotDiscardablePixelRef(const SkImageInfo&, SkDiscardableMemory*, size_
t rowBytes); | 82 // The pixelref will ref() the colortable (if not NULL), and unref() in dest
ructor |
| 83 SkOneShotDiscardablePixelRef(const SkImageInfo&, SkDiscardableMemory*, size_
t rowBytes, |
| 84 SkColorTable*); |
83 ~SkOneShotDiscardablePixelRef(); | 85 ~SkOneShotDiscardablePixelRef(); |
84 | 86 |
85 protected: | 87 protected: |
86 bool onNewLockPixels(LockRec*) override; | 88 bool onNewLockPixels(LockRec*) override; |
87 void onUnlockPixels() override; | 89 void onUnlockPixels() override; |
88 size_t getAllocatedSizeInBytes() const override; | 90 size_t getAllocatedSizeInBytes() const override; |
89 | 91 |
90 SkDiscardableMemory* diagnostic_only_getDiscardable() const override { retur
n fDM; } | 92 SkDiscardableMemory* diagnostic_only_getDiscardable() const override { retur
n fDM; } |
91 | 93 |
92 private: | 94 private: |
93 SkDiscardableMemory* fDM; | 95 SkDiscardableMemory* fDM; |
94 size_t fRB; | 96 size_t fRB; |
95 bool fFirstTime; | 97 bool fFirstTime; |
| 98 SkColorTable* fCTable; |
96 | 99 |
97 typedef SkPixelRef INHERITED; | 100 typedef SkPixelRef INHERITED; |
98 }; | 101 }; |
99 | 102 |
100 SkOneShotDiscardablePixelRef::SkOneShotDiscardablePixelRef(const SkImageInfo& in
fo, | 103 SkOneShotDiscardablePixelRef::SkOneShotDiscardablePixelRef(const SkImageInfo& in
fo, |
101 SkDiscardableMemory* dm, | 104 SkDiscardableMemory* dm, |
102 size_t rowBytes) | 105 size_t rowBytes, |
| 106 SkColorTable* ctable) |
103 : INHERITED(info) | 107 : INHERITED(info) |
104 , fDM(dm) | 108 , fDM(dm) |
105 , fRB(rowBytes) | 109 , fRB(rowBytes) |
| 110 , fCTable(ctable) |
106 { | 111 { |
107 SkASSERT(dm->data()); | 112 SkASSERT(dm->data()); |
108 fFirstTime = true; | 113 fFirstTime = true; |
| 114 SkSafeRef(ctable); |
109 } | 115 } |
110 | 116 |
111 SkOneShotDiscardablePixelRef::~SkOneShotDiscardablePixelRef() { delete fDM; } | 117 SkOneShotDiscardablePixelRef::~SkOneShotDiscardablePixelRef() { |
| 118 delete fDM; |
| 119 SkSafeUnref(fCTable); |
| 120 } |
112 | 121 |
113 bool SkOneShotDiscardablePixelRef::onNewLockPixels(LockRec* rec) { | 122 bool SkOneShotDiscardablePixelRef::onNewLockPixels(LockRec* rec) { |
114 if (fFirstTime) { | 123 if (fFirstTime) { |
115 // we're already locked | 124 // we're already locked |
116 SkASSERT(fDM->data()); | 125 SkASSERT(fDM->data()); |
117 fFirstTime = false; | 126 fFirstTime = false; |
118 goto SUCCESS; | 127 goto SUCCESS; |
119 } | 128 } |
120 | 129 |
121 // A previous call to onUnlock may have deleted our DM, so check for that | 130 // A previous call to onUnlock may have deleted our DM, so check for that |
122 if (nullptr == fDM) { | 131 if (nullptr == fDM) { |
123 return false; | 132 return false; |
124 } | 133 } |
125 | 134 |
126 if (!fDM->lock()) { | 135 if (!fDM->lock()) { |
127 // since it failed, we delete it now, to free-up the resource | 136 // since it failed, we delete it now, to free-up the resource |
128 delete fDM; | 137 delete fDM; |
129 fDM = nullptr; | 138 fDM = nullptr; |
130 return false; | 139 return false; |
131 } | 140 } |
132 | 141 |
133 SUCCESS: | 142 SUCCESS: |
134 rec->fPixels = fDM->data(); | 143 rec->fPixels = fDM->data(); |
135 rec->fColorTable = nullptr; | 144 rec->fColorTable = fCTable; |
136 rec->fRowBytes = fRB; | 145 rec->fRowBytes = fRB; |
137 return true; | 146 return true; |
138 } | 147 } |
139 | 148 |
140 void SkOneShotDiscardablePixelRef::onUnlockPixels() { | 149 void SkOneShotDiscardablePixelRef::onUnlockPixels() { |
141 SkASSERT(!fFirstTime); | 150 SkASSERT(!fFirstTime); |
142 fDM->unlock(); | 151 fDM->unlock(); |
143 } | 152 } |
144 | 153 |
145 size_t SkOneShotDiscardablePixelRef::getAllocatedSizeInBytes() const { | 154 size_t SkOneShotDiscardablePixelRef::getAllocatedSizeInBytes() const { |
(...skipping 13 matching lines...) Expand all Loading... |
159 SkResourceCache::DiscardableFactory fFactory; | 168 SkResourceCache::DiscardableFactory fFactory; |
160 }; | 169 }; |
161 | 170 |
162 bool SkResourceCacheDiscardableAllocator::allocPixelRef(SkBitmap* bitmap, SkColo
rTable* ctable) { | 171 bool SkResourceCacheDiscardableAllocator::allocPixelRef(SkBitmap* bitmap, SkColo
rTable* ctable) { |
163 size_t size = bitmap->getSize(); | 172 size_t size = bitmap->getSize(); |
164 uint64_t size64 = bitmap->computeSize64(); | 173 uint64_t size64 = bitmap->computeSize64(); |
165 if (0 == size || size64 > (uint64_t)size) { | 174 if (0 == size || size64 > (uint64_t)size) { |
166 return false; | 175 return false; |
167 } | 176 } |
168 | 177 |
| 178 if (kIndex_8_SkColorType == bitmap->colorType()) { |
| 179 if (!ctable) { |
| 180 return false; |
| 181 } |
| 182 } else { |
| 183 ctable = nullptr; |
| 184 } |
| 185 |
169 SkDiscardableMemory* dm = fFactory(size); | 186 SkDiscardableMemory* dm = fFactory(size); |
170 if (nullptr == dm) { | 187 if (nullptr == dm) { |
171 return false; | 188 return false; |
172 } | 189 } |
173 | 190 |
174 // can we relax this? | |
175 if (kN32_SkColorType != bitmap->colorType()) { | |
176 return false; | |
177 } | |
178 | |
179 SkImageInfo info = bitmap->info(); | 191 SkImageInfo info = bitmap->info(); |
180 bitmap->setPixelRef(new SkOneShotDiscardablePixelRef(info, dm, bitmap->rowBy
tes()))->unref(); | 192 bitmap->setPixelRef(new SkOneShotDiscardablePixelRef(info, dm, bitmap->rowBy
tes(), |
| 193 ctable))->unref(); |
181 bitmap->lockPixels(); | 194 bitmap->lockPixels(); |
182 return bitmap->readyToDraw(); | 195 return bitmap->readyToDraw(); |
183 } | 196 } |
184 | 197 |
185 SkResourceCache::SkResourceCache(DiscardableFactory factory) { | 198 SkResourceCache::SkResourceCache(DiscardableFactory factory) { |
186 this->init(); | 199 this->init(); |
187 fDiscardableFactory = factory; | 200 fDiscardableFactory = factory; |
188 | 201 |
189 fAllocator = new SkResourceCacheDiscardableAllocator(factory); | 202 fAllocator = new SkResourceCacheDiscardableAllocator(factory); |
190 } | 203 } |
(...skipping 496 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
687 dump->dumpNumericValue(dumpName.c_str(), "size", "bytes", rec.bytesUsed(
)); | 700 dump->dumpNumericValue(dumpName.c_str(), "size", "bytes", rec.bytesUsed(
)); |
688 dump->setMemoryBacking(dumpName.c_str(), "malloc", nullptr); | 701 dump->setMemoryBacking(dumpName.c_str(), "malloc", nullptr); |
689 } | 702 } |
690 } | 703 } |
691 | 704 |
692 void SkResourceCache::DumpMemoryStatistics(SkTraceMemoryDump* dump) { | 705 void SkResourceCache::DumpMemoryStatistics(SkTraceMemoryDump* dump) { |
693 // Since resource could be backed by malloc or discardable, the cache always
dumps detailed | 706 // Since resource could be backed by malloc or discardable, the cache always
dumps detailed |
694 // stats to be accurate. | 707 // stats to be accurate. |
695 VisitAll(sk_trace_dump_visitor, dump); | 708 VisitAll(sk_trace_dump_visitor, dump); |
696 } | 709 } |
OLD | NEW |