commit-bot@chromium.org | 6e3e422 | 2013-11-06 10:08:30 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "SkCachingPixelRef.h" |
| 9 | #include "SkScaledImageCache.h" |
| 10 | |
reed@google.com | 398337b | 2013-12-11 21:22:39 +0000 | [diff] [blame^] | 11 | |
halcanary@google.com | 36d08c5 | 2013-12-05 14:00:03 +0000 | [diff] [blame] | 12 | bool SkCachingPixelRef::Install(SkImageGenerator* generator, |
| 13 | SkBitmap* dst) { |
| 14 | SkImageInfo info; |
| 15 | SkASSERT(generator != NULL); |
| 16 | SkASSERT(dst != NULL); |
| 17 | if ((NULL == generator) |
| 18 | || !(generator->getInfo(&info)) |
| 19 | || !dst->setConfig(info, 0)) { |
| 20 | SkDELETE(generator); |
| 21 | return false; |
| 22 | } |
| 23 | SkAutoTUnref<SkCachingPixelRef> ref(SkNEW_ARGS(SkCachingPixelRef, |
| 24 | (generator, |
| 25 | info, |
| 26 | dst->rowBytes()))); |
| 27 | dst->setPixelRef(ref); |
| 28 | return true; |
| 29 | } |
| 30 | |
| 31 | SkCachingPixelRef::SkCachingPixelRef(SkImageGenerator* generator, |
| 32 | const SkImageInfo& info, |
| 33 | size_t rowBytes) |
reed@google.com | 398337b | 2013-12-11 21:22:39 +0000 | [diff] [blame^] | 34 | : fImageGenerator(generator) |
halcanary@google.com | 36d08c5 | 2013-12-05 14:00:03 +0000 | [diff] [blame] | 35 | , fErrorInDecoding(false) |
| 36 | , fScaledCacheId(NULL) |
reed@google.com | 398337b | 2013-12-11 21:22:39 +0000 | [diff] [blame^] | 37 | , fInfo(info) |
halcanary@google.com | 36d08c5 | 2013-12-05 14:00:03 +0000 | [diff] [blame] | 38 | , fRowBytes(rowBytes) { |
| 39 | SkASSERT(fImageGenerator != NULL); |
commit-bot@chromium.org | 6e3e422 | 2013-11-06 10:08:30 +0000 | [diff] [blame] | 40 | } |
| 41 | SkCachingPixelRef::~SkCachingPixelRef() { |
halcanary@google.com | 36d08c5 | 2013-12-05 14:00:03 +0000 | [diff] [blame] | 42 | SkDELETE(fImageGenerator); |
commit-bot@chromium.org | 6e3e422 | 2013-11-06 10:08:30 +0000 | [diff] [blame] | 43 | SkASSERT(NULL == fScaledCacheId); |
| 44 | // Assert always unlock before unref. |
| 45 | } |
| 46 | |
reed@google.com | 398337b | 2013-12-11 21:22:39 +0000 | [diff] [blame^] | 47 | void* SkCachingPixelRef::onLockPixels(SkColorTable** colorTable) { |
| 48 | (void)colorTable; |
halcanary@google.com | 36d08c5 | 2013-12-05 14:00:03 +0000 | [diff] [blame] | 49 | if (fErrorInDecoding) { |
reed@google.com | 398337b | 2013-12-11 21:22:39 +0000 | [diff] [blame^] | 50 | return NULL; // don't try again. |
commit-bot@chromium.org | 6e3e422 | 2013-11-06 10:08:30 +0000 | [diff] [blame] | 51 | } |
| 52 | SkBitmap bitmap; |
halcanary@google.com | 36d08c5 | 2013-12-05 14:00:03 +0000 | [diff] [blame] | 53 | SkASSERT(NULL == fScaledCacheId); |
commit-bot@chromium.org | 6e3e422 | 2013-11-06 10:08:30 +0000 | [diff] [blame] | 54 | fScaledCacheId = SkScaledImageCache::FindAndLock(this->getGenerationID(), |
reed@google.com | 398337b | 2013-12-11 21:22:39 +0000 | [diff] [blame^] | 55 | fInfo.fWidth, |
| 56 | fInfo.fHeight, |
commit-bot@chromium.org | 6e3e422 | 2013-11-06 10:08:30 +0000 | [diff] [blame] | 57 | &bitmap); |
| 58 | if (NULL == fScaledCacheId) { |
| 59 | // Cache has been purged, must re-decode. |
reed@google.com | 398337b | 2013-12-11 21:22:39 +0000 | [diff] [blame^] | 60 | if ((!bitmap.setConfig(fInfo, fRowBytes)) || !bitmap.allocPixels()) { |
halcanary@google.com | 36d08c5 | 2013-12-05 14:00:03 +0000 | [diff] [blame] | 61 | fErrorInDecoding = true; |
reed@google.com | 398337b | 2013-12-11 21:22:39 +0000 | [diff] [blame^] | 62 | return NULL; |
halcanary@google.com | 36d08c5 | 2013-12-05 14:00:03 +0000 | [diff] [blame] | 63 | } |
| 64 | SkAutoLockPixels autoLockPixels(bitmap); |
reed@google.com | 398337b | 2013-12-11 21:22:39 +0000 | [diff] [blame^] | 65 | if (!fImageGenerator->getPixels(fInfo, bitmap.getPixels(), fRowBytes)) { |
halcanary@google.com | 36d08c5 | 2013-12-05 14:00:03 +0000 | [diff] [blame] | 66 | fErrorInDecoding = true; |
reed@google.com | 398337b | 2013-12-11 21:22:39 +0000 | [diff] [blame^] | 67 | return NULL; |
commit-bot@chromium.org | 6e3e422 | 2013-11-06 10:08:30 +0000 | [diff] [blame] | 68 | } |
| 69 | fScaledCacheId = SkScaledImageCache::AddAndLock(this->getGenerationID(), |
reed@google.com | 398337b | 2013-12-11 21:22:39 +0000 | [diff] [blame^] | 70 | fInfo.fWidth, |
| 71 | fInfo.fHeight, |
commit-bot@chromium.org | 6e3e422 | 2013-11-06 10:08:30 +0000 | [diff] [blame] | 72 | bitmap); |
| 73 | SkASSERT(fScaledCacheId != NULL); |
| 74 | } |
halcanary@google.com | 36d08c5 | 2013-12-05 14:00:03 +0000 | [diff] [blame] | 75 | |
commit-bot@chromium.org | 6e3e422 | 2013-11-06 10:08:30 +0000 | [diff] [blame] | 76 | // Now bitmap should contain a concrete PixelRef of the decoded |
| 77 | // image. |
| 78 | SkAutoLockPixels autoLockPixels(bitmap); |
| 79 | void* pixels = bitmap.getPixels(); |
| 80 | SkASSERT(pixels != NULL); |
| 81 | // At this point, the autoLockPixels will unlockPixels() |
| 82 | // to remove bitmap's lock on the pixels. We will then |
| 83 | // destroy bitmap. The *only* guarantee that this pointer |
| 84 | // remains valid is the guarantee made by |
| 85 | // SkScaledImageCache that it will not destroy the *other* |
| 86 | // bitmap (SkScaledImageCache::Rec.fBitmap) that holds a |
| 87 | // reference to the concrete PixelRef while this record is |
| 88 | // locked. |
reed@google.com | 398337b | 2013-12-11 21:22:39 +0000 | [diff] [blame^] | 89 | return pixels; |
commit-bot@chromium.org | 6e3e422 | 2013-11-06 10:08:30 +0000 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | void SkCachingPixelRef::onUnlockPixels() { |
| 93 | if (fScaledCacheId != NULL) { |
| 94 | SkScaledImageCache::Unlock( |
| 95 | static_cast<SkScaledImageCache::ID*>(fScaledCacheId)); |
| 96 | fScaledCacheId = NULL; |
| 97 | } |
| 98 | } |