OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright 2013 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 #include "SkCachingPixelRef.h" |
| 9 |
| 10 SkCachingPixelRef::SkCachingPixelRef(SkScaledImageCache* cache) |
| 11 : fErrorInDecoding(false) |
| 12 , fCache(cache) |
| 13 , fScaledCacheId(NULL) { |
| 14 // Please note: a non-NULL cache is not thread-safe. |
| 15 memset(&fInfo, 0xFF, sizeof(fInfo)); |
| 16 } |
| 17 SkCachingPixelRef::~SkCachingPixelRef() { |
| 18 SkASSERT(NULL == fScaledCacheId); |
| 19 // Assert always unlock before unref. |
| 20 } |
| 21 |
| 22 const SkImageInfo* SkCachingPixelRef::getInfo() { |
| 23 if (fErrorInDecoding) { |
| 24 SkASSERT(fInfo.fWidth < 0); |
| 25 return NULL; // Don't try again. |
| 26 } |
| 27 if (fInfo.fWidth >= 0) { |
| 28 return &fInfo; // already successful |
| 29 } |
| 30 SkImageInfo tmp; |
| 31 if (!this->onDecodeInfo(&tmp)) { |
| 32 fErrorInDecoding = true; |
| 33 return NULL; |
| 34 } |
| 35 fInfo = tmp; |
| 36 SkASSERT(fInfo.fWidth >= 0); |
| 37 return &fInfo; |
| 38 } |
| 39 |
| 40 bool SkCachingPixelRef::configure(SkBitmap* bitmap) { |
| 41 const SkImageInfo* info = this->getInfo(); |
| 42 if (NULL == info) { |
| 43 return false; |
| 44 } |
| 45 return bitmap->setConfig(*info, 0); |
| 46 } |
| 47 |
| 48 void* SkCachingPixelRef::onLockPixels(SkColorTable** colorTable) { |
| 49 (void)colorTable; |
| 50 const SkImageInfo* info = this->getInfo(); |
| 51 if (NULL == info) { |
| 52 return NULL; |
| 53 } |
| 54 SkBitmap bitmap; |
| 55 |
| 56 if (NULL == fCache) { |
| 57 fScaledCacheId = SkScaledImageCache::FindAndLock( |
| 58 this->getGenerationID(), |
| 59 info->fWidth, |
| 60 info->fHeight, |
| 61 &bitmap); |
| 62 } else { |
| 63 fScaledCacheId = fCache->findAndLock(this->getGenerationID(), |
| 64 info->fWidth, |
| 65 info->fHeight, |
| 66 &bitmap); |
| 67 } |
| 68 if (NULL == fScaledCacheId) { |
| 69 // Cache has been purged, must re-decode. |
| 70 if (!this->onDecodeInto(0, &bitmap)) { |
| 71 return NULL; |
| 72 } |
| 73 if (NULL == fCache) { |
| 74 fScaledCacheId = SkScaledImageCache::AddAndLock( |
| 75 this->getGenerationID(), |
| 76 info->fWidth, |
| 77 info->fHeight, |
| 78 bitmap); |
| 79 } else { |
| 80 fScaledCacheId = fCache->addAndLock(this->getGenerationID(), |
| 81 info->fWidth, |
| 82 info->fHeight, |
| 83 bitmap); |
| 84 } |
| 85 SkASSERT(fScaledCacheId != NULL); |
| 86 } |
| 87 // Now bitmap should contain a concrete PixelRef of the decoded |
| 88 // image. |
| 89 SkAutoLockPixels autoLockPixels(bitmap); |
| 90 void* pixels = bitmap.getPixels(); |
| 91 SkASSERT(NULL != pixels); |
| 92 // At this point, the autoLockPixels will unlockPixels() |
| 93 // to remove bitmap's lock on the pixels. We will then |
| 94 // destroy bitmap. The *only* guarantee that this pointer |
| 95 // remains valid is the guarantee made by |
| 96 // SkScaledImageCache that it will not destroy the *other* |
| 97 // bitmap (SkScaledImageCache::Rec.fBitmap) that holds a |
| 98 // reference to the concrete PixelRef while this record is |
| 99 // locked. |
| 100 return pixels; |
| 101 } |
| 102 |
| 103 void SkCachingPixelRef::onUnlockPixels() { |
| 104 if (NULL != fScaledCacheId) { |
| 105 if (NULL == fCache) { |
| 106 SkScaledImageCache::Unlock(fScaledCacheId); |
| 107 } else { |
| 108 fCache->unlock(fScaledCacheId); |
| 109 } |
| 110 fScaledCacheId = NULL; |
| 111 } |
| 112 } |
| 113 |
| 114 bool SkCachingPixelRef::onDecodeInto(int pow2, SkBitmap* bitmap) { |
| 115 SkBitmap tmp; |
| 116 // TODO(halcanary) - Enable SkCachingPixelRef to use a custom |
| 117 // allocator. `tmp.allocPixels(fAllocator, NULL)` |
| 118 if (!(this->configure(&tmp) && tmp.allocPixels(NULL, NULL))) { |
| 119 fErrorInDecoding = true; |
| 120 return false; |
| 121 } |
| 122 if (!this->onDecode(tmp.getPixels(), tmp.rowBytes())) { |
| 123 fErrorInDecoding = true; |
| 124 return false; |
| 125 } |
| 126 *bitmap = tmp; |
| 127 return true; |
| 128 } |
| 129 |
OLD | NEW |