| 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 | 11 |
| 12 bool SkCachingPixelRef::Install(SkImageGenerator* generator, | 12 bool SkCachingPixelRef::Install(SkImageGenerator* generator, |
| 13 SkBitmap* dst) { | 13 SkBitmap* dst) { |
| 14 SkImageInfo info; | 14 SkImageInfo info; |
| 15 SkASSERT(generator != NULL); | 15 SkASSERT(generator != NULL); |
| 16 SkASSERT(dst != NULL); | 16 SkASSERT(dst != NULL); |
| 17 if ((NULL == generator) | 17 if ((NULL == generator) |
| 18 || !(generator->getInfo(&info)) | 18 || !(generator->getInfo(&info)) |
| 19 || !dst->setConfig(info, 0)) { | 19 || !dst->setConfig(info, 0)) { |
| 20 SkDELETE(generator); | 20 SkDELETE(generator); |
| 21 return false; | 21 return false; |
| 22 } | 22 } |
| 23 SkAutoTUnref<SkCachingPixelRef> ref(SkNEW_ARGS(SkCachingPixelRef, | 23 SkAutoTUnref<SkCachingPixelRef> ref(SkNEW_ARGS(SkCachingPixelRef, |
| 24 (generator, | 24 (info, generator, dst->rowBytes()))); |
| 25 info, | |
| 26 dst->rowBytes()))); | |
| 27 dst->setPixelRef(ref); | 25 dst->setPixelRef(ref); |
| 28 return true; | 26 return true; |
| 29 } | 27 } |
| 30 | 28 |
| 31 SkCachingPixelRef::SkCachingPixelRef(SkImageGenerator* generator, | 29 SkCachingPixelRef::SkCachingPixelRef(const SkImageInfo& info, |
| 32 const SkImageInfo& info, | 30 SkImageGenerator* generator, |
| 33 size_t rowBytes) | 31 size_t rowBytes) |
| 34 : fImageGenerator(generator) | 32 : INHERITED(info) |
| 33 , fImageGenerator(generator) |
| 35 , fErrorInDecoding(false) | 34 , fErrorInDecoding(false) |
| 36 , fScaledCacheId(NULL) | 35 , fScaledCacheId(NULL) |
| 37 , fInfo(info) | |
| 38 , fRowBytes(rowBytes) { | 36 , fRowBytes(rowBytes) { |
| 39 SkASSERT(fImageGenerator != NULL); | 37 SkASSERT(fImageGenerator != NULL); |
| 40 } | 38 } |
| 41 SkCachingPixelRef::~SkCachingPixelRef() { | 39 SkCachingPixelRef::~SkCachingPixelRef() { |
| 42 SkDELETE(fImageGenerator); | 40 SkDELETE(fImageGenerator); |
| 43 SkASSERT(NULL == fScaledCacheId); | 41 SkASSERT(NULL == fScaledCacheId); |
| 44 // Assert always unlock before unref. | 42 // Assert always unlock before unref. |
| 45 } | 43 } |
| 46 | 44 |
| 47 void* SkCachingPixelRef::onLockPixels(SkColorTable** colorTable) { | 45 void* SkCachingPixelRef::onLockPixels(SkColorTable**) { |
| 48 (void)colorTable; | 46 const SkImageInfo& info = this->info(); |
| 47 |
| 49 if (fErrorInDecoding) { | 48 if (fErrorInDecoding) { |
| 50 return NULL; // don't try again. | 49 return NULL; // don't try again. |
| 51 } | 50 } |
| 52 SkBitmap bitmap; | 51 SkBitmap bitmap; |
| 53 SkASSERT(NULL == fScaledCacheId); | 52 SkASSERT(NULL == fScaledCacheId); |
| 54 fScaledCacheId = SkScaledImageCache::FindAndLock(this->getGenerationID(), | 53 fScaledCacheId = SkScaledImageCache::FindAndLock(this->getGenerationID(), |
| 55 fInfo.fWidth, | 54 info.fWidth, |
| 56 fInfo.fHeight, | 55 info.fHeight, |
| 57 &bitmap); | 56 &bitmap); |
| 58 if (NULL == fScaledCacheId) { | 57 if (NULL == fScaledCacheId) { |
| 59 // Cache has been purged, must re-decode. | 58 // Cache has been purged, must re-decode. |
| 60 if ((!bitmap.setConfig(fInfo, fRowBytes)) || !bitmap.allocPixels()) { | 59 if ((!bitmap.setConfig(info, fRowBytes)) || !bitmap.allocPixels()) { |
| 61 fErrorInDecoding = true; | 60 fErrorInDecoding = true; |
| 62 return NULL; | 61 return NULL; |
| 63 } | 62 } |
| 64 SkAutoLockPixels autoLockPixels(bitmap); | 63 SkAutoLockPixels autoLockPixels(bitmap); |
| 65 if (!fImageGenerator->getPixels(fInfo, bitmap.getPixels(), fRowBytes)) { | 64 if (!fImageGenerator->getPixels(info, bitmap.getPixels(), fRowBytes)) { |
| 66 fErrorInDecoding = true; | 65 fErrorInDecoding = true; |
| 67 return NULL; | 66 return NULL; |
| 68 } | 67 } |
| 69 fScaledCacheId = SkScaledImageCache::AddAndLock(this->getGenerationID(), | 68 fScaledCacheId = SkScaledImageCache::AddAndLock(this->getGenerationID(), |
| 70 fInfo.fWidth, | 69 info.fWidth, |
| 71 fInfo.fHeight, | 70 info.fHeight, |
| 72 bitmap); | 71 bitmap); |
| 73 SkASSERT(fScaledCacheId != NULL); | 72 SkASSERT(fScaledCacheId != NULL); |
| 74 } | 73 } |
| 75 | 74 |
| 76 // Now bitmap should contain a concrete PixelRef of the decoded | 75 // Now bitmap should contain a concrete PixelRef of the decoded |
| 77 // image. | 76 // image. |
| 78 SkAutoLockPixels autoLockPixels(bitmap); | 77 SkAutoLockPixels autoLockPixels(bitmap); |
| 79 void* pixels = bitmap.getPixels(); | 78 void* pixels = bitmap.getPixels(); |
| 80 SkASSERT(pixels != NULL); | 79 SkASSERT(pixels != NULL); |
| 81 // At this point, the autoLockPixels will unlockPixels() | 80 // At this point, the autoLockPixels will unlockPixels() |
| 82 // to remove bitmap's lock on the pixels. We will then | 81 // to remove bitmap's lock on the pixels. We will then |
| 83 // destroy bitmap. The *only* guarantee that this pointer | 82 // destroy bitmap. The *only* guarantee that this pointer |
| 84 // remains valid is the guarantee made by | 83 // remains valid is the guarantee made by |
| 85 // SkScaledImageCache that it will not destroy the *other* | 84 // SkScaledImageCache that it will not destroy the *other* |
| 86 // bitmap (SkScaledImageCache::Rec.fBitmap) that holds a | 85 // bitmap (SkScaledImageCache::Rec.fBitmap) that holds a |
| 87 // reference to the concrete PixelRef while this record is | 86 // reference to the concrete PixelRef while this record is |
| 88 // locked. | 87 // locked. |
| 89 return pixels; | 88 return pixels; |
| 90 } | 89 } |
| 91 | 90 |
| 92 void SkCachingPixelRef::onUnlockPixels() { | 91 void SkCachingPixelRef::onUnlockPixels() { |
| 93 SkASSERT(fScaledCacheId != NULL); | 92 SkASSERT(fScaledCacheId != NULL); |
| 94 SkScaledImageCache::Unlock( static_cast<SkScaledImageCache::ID*>(fScaledCach
eId)); | 93 SkScaledImageCache::Unlock( static_cast<SkScaledImageCache::ID*>(fScaledCach
eId)); |
| 95 fScaledCacheId = NULL; | 94 fScaledCacheId = NULL; |
| 96 } | 95 } |
| OLD | NEW |