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