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