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