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