OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2012 Google Inc. | 3 * Copyright 2012 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 #include "SkBitmapHeap.h" | 9 #include "SkBitmapHeap.h" |
10 #include "SkBitmap.h" | 10 #include "SkBitmap.h" |
(...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
220 | 220 |
221 int SkBitmapHeap::findInLookupTable(const LookupEntry& indexEntry, SkBitmapHeapE
ntry** entry) { | 221 int SkBitmapHeap::findInLookupTable(const LookupEntry& indexEntry, SkBitmapHeapE
ntry** entry) { |
222 int index = SkTSearch<const LookupEntry, LookupEntry::Less>( | 222 int index = SkTSearch<const LookupEntry, LookupEntry::Less>( |
223 (const LookupEntry**)fLookupTable.b
egin(), | 223 (const LookupEntry**)fLookupTable.b
egin(), |
224 fLookupTable.count(), | 224 fLookupTable.count(), |
225 &indexEntry, sizeof(void*)); | 225 &indexEntry, sizeof(void*)); |
226 | 226 |
227 if (index < 0) { | 227 if (index < 0) { |
228 // insert ourselves into the bitmapIndex | 228 // insert ourselves into the bitmapIndex |
229 index = ~index; | 229 index = ~index; |
230 *fLookupTable.insert(index) = SkNEW_ARGS(LookupEntry, (indexEntry)); | 230 *fLookupTable.insert(index) = new LookupEntry(indexEntry); |
231 } else if (entry != NULL) { | 231 } else if (entry != NULL) { |
232 // populate the entry if needed | 232 // populate the entry if needed |
233 *entry = fStorage[fLookupTable[index]->fStorageSlot]; | 233 *entry = fStorage[fLookupTable[index]->fStorageSlot]; |
234 } | 234 } |
235 | 235 |
236 return index; | 236 return index; |
237 } | 237 } |
238 | 238 |
239 bool SkBitmapHeap::copyBitmap(const SkBitmap& originalBitmap, SkBitmap& copiedBi
tmap) { | 239 bool SkBitmapHeap::copyBitmap(const SkBitmap& originalBitmap, SkBitmap& copiedBi
tmap) { |
240 SkASSERT(!fExternalStorage); | 240 SkASSERT(!fExternalStorage); |
(...skipping 16 matching lines...) Expand all Loading... |
257 } | 257 } |
258 | 258 |
259 int SkBitmapHeap::removeEntryFromLookupTable(LookupEntry* entry) { | 259 int SkBitmapHeap::removeEntryFromLookupTable(LookupEntry* entry) { |
260 // remove the bitmap index for the deleted entry | 260 // remove the bitmap index for the deleted entry |
261 SkDEBUGCODE(int count = fLookupTable.count();) | 261 SkDEBUGCODE(int count = fLookupTable.count();) |
262 int index = this->findInLookupTable(*entry, NULL); | 262 int index = this->findInLookupTable(*entry, NULL); |
263 // Verify that findInLookupTable found an existing entry rather than adding | 263 // Verify that findInLookupTable found an existing entry rather than adding |
264 // a new entry to the lookup table. | 264 // a new entry to the lookup table. |
265 SkASSERT(count == fLookupTable.count()); | 265 SkASSERT(count == fLookupTable.count()); |
266 fBytesAllocated -= fStorage[entry->fStorageSlot]->fBytesAllocated; | 266 fBytesAllocated -= fStorage[entry->fStorageSlot]->fBytesAllocated; |
267 SkDELETE(fLookupTable[index]); | 267 delete fLookupTable[index]; |
268 fLookupTable.remove(index); | 268 fLookupTable.remove(index); |
269 return index; | 269 return index; |
270 } | 270 } |
271 | 271 |
272 int32_t SkBitmapHeap::insert(const SkBitmap& originalBitmap) { | 272 int32_t SkBitmapHeap::insert(const SkBitmap& originalBitmap) { |
273 SkBitmapHeapEntry* entry = NULL; | 273 SkBitmapHeapEntry* entry = NULL; |
274 int searchIndex = this->findInLookupTable(LookupEntry(originalBitmap), &entr
y); | 274 int searchIndex = this->findInLookupTable(LookupEntry(originalBitmap), &entr
y); |
275 | 275 |
276 if (entry) { | 276 if (entry) { |
277 // Already had a copy of the bitmap in the heap. | 277 // Already had a copy of the bitmap in the heap. |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
310 } | 310 } |
311 } | 311 } |
312 | 312 |
313 // if we didn't have an entry yet we need to create one | 313 // if we didn't have an entry yet we need to create one |
314 if (!entry) { | 314 if (!entry) { |
315 if (fPreferredCount != UNLIMITED_SIZE && fUnusedSlots.count() > 0) { | 315 if (fPreferredCount != UNLIMITED_SIZE && fUnusedSlots.count() > 0) { |
316 int slot; | 316 int slot; |
317 fUnusedSlots.pop(&slot); | 317 fUnusedSlots.pop(&slot); |
318 entry = fStorage[slot]; | 318 entry = fStorage[slot]; |
319 } else { | 319 } else { |
320 entry = SkNEW(SkBitmapHeapEntry); | 320 entry = new SkBitmapHeapEntry; |
321 fStorage.append(1, &entry); | 321 fStorage.append(1, &entry); |
322 entry->fSlot = fStorage.count() - 1; | 322 entry->fSlot = fStorage.count() - 1; |
323 fBytesAllocated += sizeof(SkBitmapHeapEntry); | 323 fBytesAllocated += sizeof(SkBitmapHeapEntry); |
324 } | 324 } |
325 } | 325 } |
326 | 326 |
327 // create a copy of the bitmap | 327 // create a copy of the bitmap |
328 bool copySucceeded; | 328 bool copySucceeded; |
329 if (fExternalStorage) { | 329 if (fExternalStorage) { |
330 copySucceeded = fExternalStorage->insert(originalBitmap, entry->fSlot); | 330 copySucceeded = fExternalStorage->insert(originalBitmap, entry->fSlot); |
331 } else { | 331 } else { |
332 copySucceeded = copyBitmap(originalBitmap, entry->fBitmap); | 332 copySucceeded = copyBitmap(originalBitmap, entry->fBitmap); |
333 } | 333 } |
334 | 334 |
335 // if the copy failed then we must abort | 335 // if the copy failed then we must abort |
336 if (!copySucceeded) { | 336 if (!copySucceeded) { |
337 // delete the index | 337 // delete the index |
338 SkDELETE(fLookupTable[searchIndex]); | 338 delete fLookupTable[searchIndex]; |
339 fLookupTable.remove(searchIndex); | 339 fLookupTable.remove(searchIndex); |
340 // If entry is the last slot in storage, it is safe to delete it. | 340 // If entry is the last slot in storage, it is safe to delete it. |
341 if (fStorage.count() - 1 == entry->fSlot) { | 341 if (fStorage.count() - 1 == entry->fSlot) { |
342 // free the slot | 342 // free the slot |
343 fStorage.remove(entry->fSlot); | 343 fStorage.remove(entry->fSlot); |
344 fBytesAllocated -= sizeof(SkBitmapHeapEntry); | 344 fBytesAllocated -= sizeof(SkBitmapHeapEntry); |
345 SkDELETE(entry); | 345 delete entry; |
346 } else { | 346 } else { |
347 fUnusedSlots.push(entry->fSlot); | 347 fUnusedSlots.push(entry->fSlot); |
348 } | 348 } |
349 return INVALID_SLOT; | 349 return INVALID_SLOT; |
350 } | 350 } |
351 | 351 |
352 // update the index with the appropriate slot in the heap | 352 // update the index with the appropriate slot in the heap |
353 fLookupTable[searchIndex]->fStorageSlot = entry->fSlot; | 353 fLookupTable[searchIndex]->fStorageSlot = entry->fSlot; |
354 | 354 |
355 // compute the space taken by this entry | 355 // compute the space taken by this entry |
(...skipping 27 matching lines...) Expand all Loading... |
383 for (int i = 0; i < fDeferredEntries.count(); i++) { | 383 for (int i = 0; i < fDeferredEntries.count(); i++) { |
384 SkASSERT(fOwnerCount != IGNORE_OWNERS); | 384 SkASSERT(fOwnerCount != IGNORE_OWNERS); |
385 SkBitmapHeapEntry* heapEntry = this->getEntry(fDeferredEntries[i]); | 385 SkBitmapHeapEntry* heapEntry = this->getEntry(fDeferredEntries[i]); |
386 SkASSERT(heapEntry != NULL); | 386 SkASSERT(heapEntry != NULL); |
387 heapEntry->addReferences(fOwnerCount); | 387 heapEntry->addReferences(fOwnerCount); |
388 } | 388 } |
389 } | 389 } |
390 fDeferAddingOwners = false; | 390 fDeferAddingOwners = false; |
391 fDeferredEntries.reset(); | 391 fDeferredEntries.reset(); |
392 } | 392 } |
OLD | NEW |