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