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