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" |
piotaixr | 91cab83 | 2014-09-03 12:34:57 -0700 | [diff] [blame] | 10 | #include "SkRect.h" |
commit-bot@chromium.org | 6e3e422 | 2013-11-06 10:08:30 +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; |
halcanary@google.com | 36d08c5 | 2013-12-05 14:00:03 +0000 | [diff] [blame] | 15 | SkASSERT(dst != NULL); |
commit-bot@chromium.org | e13af71 | 2014-01-13 20:39:14 +0000 | [diff] [blame] | 16 | if ((NULL == generator) |
| 17 | || !(generator->getInfo(&info)) |
commit-bot@chromium.org | a3264e5 | 2014-05-30 13:26:10 +0000 | [diff] [blame] | 18 | || !dst->setInfo(info)) { |
halcanary@google.com | 36d08c5 | 2013-12-05 14:00:03 +0000 | [diff] [blame] | 19 | SkDELETE(generator); |
| 20 | return false; |
| 21 | } |
| 22 | SkAutoTUnref<SkCachingPixelRef> ref(SkNEW_ARGS(SkCachingPixelRef, |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 23 | (info, generator, dst->rowBytes()))); |
commit-bot@chromium.org | e13af71 | 2014-01-13 20:39:14 +0000 | [diff] [blame] | 24 | dst->setPixelRef(ref); |
halcanary@google.com | 36d08c5 | 2013-12-05 14:00:03 +0000 | [diff] [blame] | 25 | return true; |
| 26 | } |
| 27 | |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 28 | SkCachingPixelRef::SkCachingPixelRef(const SkImageInfo& info, |
| 29 | SkImageGenerator* generator, |
halcanary@google.com | 36d08c5 | 2013-12-05 14:00:03 +0000 | [diff] [blame] | 30 | size_t rowBytes) |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 31 | : INHERITED(info) |
| 32 | , fImageGenerator(generator) |
halcanary@google.com | 36d08c5 | 2013-12-05 14:00:03 +0000 | [diff] [blame] | 33 | , fErrorInDecoding(false) |
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 | // Assert always unlock before unref. |
| 40 | } |
| 41 | |
reed@google.com | d0419b1 | 2014-01-06 17:08:27 +0000 | [diff] [blame] | 42 | bool SkCachingPixelRef::onNewLockPixels(LockRec* rec) { |
commit-bot@chromium.org | 27f8902 | 2014-01-03 16:32:45 +0000 | [diff] [blame] | 43 | if (fErrorInDecoding) { |
reed@google.com | d0419b1 | 2014-01-06 17:08:27 +0000 | [diff] [blame] | 44 | return false; // don't try again. |
commit-bot@chromium.org | 27f8902 | 2014-01-03 16:32:45 +0000 | [diff] [blame] | 45 | } |
reed@google.com | ba82bd1 | 2014-01-06 13:34:39 +0000 | [diff] [blame] | 46 | |
reed@google.com | d0419b1 | 2014-01-06 17:08:27 +0000 | [diff] [blame] | 47 | const SkImageInfo& info = this->info(); |
halcanary | f622a6c | 2014-10-24 12:54:53 -0700 | [diff] [blame] | 48 | if (!SkBitmapCache::Find( |
| 49 | this->getGenerationID(), info.bounds(), &fLockedBitmap)) { |
commit-bot@chromium.org | 6e3e422 | 2013-11-06 10:08:30 +0000 | [diff] [blame] | 50 | // Cache has been purged, must re-decode. |
reed | 8482504 | 2014-09-02 12:50:45 -0700 | [diff] [blame] | 51 | if (!fLockedBitmap.tryAllocPixels(info, fRowBytes)) { |
halcanary@google.com | 36d08c5 | 2013-12-05 14:00:03 +0000 | [diff] [blame] | 52 | fErrorInDecoding = true; |
reed@google.com | d0419b1 | 2014-01-06 17:08:27 +0000 | [diff] [blame] | 53 | return false; |
halcanary@google.com | 36d08c5 | 2013-12-05 14:00:03 +0000 | [diff] [blame] | 54 | } |
reed | 680fb9e | 2014-08-26 09:08:04 -0700 | [diff] [blame] | 55 | if (!fImageGenerator->getPixels(info, fLockedBitmap.getPixels(), 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; |
commit-bot@chromium.org | 6e3e422 | 2013-11-06 10:08:30 +0000 | [diff] [blame] | 58 | } |
reed | 14b6aba | 2014-08-29 10:25:26 -0700 | [diff] [blame] | 59 | fLockedBitmap.setImmutable(); |
halcanary | f622a6c | 2014-10-24 12:54:53 -0700 | [diff] [blame] | 60 | SkBitmapCache::Add( |
| 61 | this->getGenerationID(), info.bounds(), fLockedBitmap); |
commit-bot@chromium.org | 6e3e422 | 2013-11-06 10:08:30 +0000 | [diff] [blame] | 62 | } |
halcanary@google.com | 36d08c5 | 2013-12-05 14:00:03 +0000 | [diff] [blame] | 63 | |
reed | 680fb9e | 2014-08-26 09:08:04 -0700 | [diff] [blame] | 64 | // Now bitmap should contain a concrete PixelRef of the decoded image. |
| 65 | void* pixels = fLockedBitmap.getPixels(); |
commit-bot@chromium.org | 6e3e422 | 2013-11-06 10:08:30 +0000 | [diff] [blame] | 66 | SkASSERT(pixels != NULL); |
reed@google.com | d0419b1 | 2014-01-06 17:08:27 +0000 | [diff] [blame] | 67 | rec->fPixels = pixels; |
| 68 | rec->fColorTable = NULL; |
reed | 680fb9e | 2014-08-26 09:08:04 -0700 | [diff] [blame] | 69 | rec->fRowBytes = fLockedBitmap.rowBytes(); |
reed@google.com | d0419b1 | 2014-01-06 17:08:27 +0000 | [diff] [blame] | 70 | return true; |
commit-bot@chromium.org | 6e3e422 | 2013-11-06 10:08:30 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | void SkCachingPixelRef::onUnlockPixels() { |
reed | 680fb9e | 2014-08-26 09:08:04 -0700 | [diff] [blame] | 74 | fLockedBitmap.reset(); |
commit-bot@chromium.org | 6e3e422 | 2013-11-06 10:08:30 +0000 | [diff] [blame] | 75 | } |