blob: c59682854bc04e56e47ae9e504606387c95da69e [file] [log] [blame]
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +00001/*
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"
reed04617132014-08-21 09:46:49 -07009#include "SkBitmapCache.h"
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +000010
halcanary@google.com36d08c52013-12-05 14:00:03 +000011bool SkCachingPixelRef::Install(SkImageGenerator* generator,
12 SkBitmap* dst) {
13 SkImageInfo info;
halcanary@google.com36d08c52013-12-05 14:00:03 +000014 SkASSERT(dst != NULL);
commit-bot@chromium.orge13af712014-01-13 20:39:14 +000015 if ((NULL == generator)
16 || !(generator->getInfo(&info))
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +000017 || !dst->setInfo(info)) {
halcanary@google.com36d08c52013-12-05 14:00:03 +000018 SkDELETE(generator);
19 return false;
20 }
21 SkAutoTUnref<SkCachingPixelRef> ref(SkNEW_ARGS(SkCachingPixelRef,
reed@google.combf790232013-12-13 19:45:58 +000022 (info, generator, dst->rowBytes())));
commit-bot@chromium.orge13af712014-01-13 20:39:14 +000023 dst->setPixelRef(ref);
halcanary@google.com36d08c52013-12-05 14:00:03 +000024 return true;
25}
26
reed@google.combf790232013-12-13 19:45:58 +000027SkCachingPixelRef::SkCachingPixelRef(const SkImageInfo& info,
28 SkImageGenerator* generator,
halcanary@google.com36d08c52013-12-05 14:00:03 +000029 size_t rowBytes)
reed@google.combf790232013-12-13 19:45:58 +000030 : INHERITED(info)
31 , fImageGenerator(generator)
halcanary@google.com36d08c52013-12-05 14:00:03 +000032 , fErrorInDecoding(false)
33 , fScaledCacheId(NULL)
halcanary@google.com36d08c52013-12-05 14:00:03 +000034 , fRowBytes(rowBytes) {
35 SkASSERT(fImageGenerator != NULL);
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +000036}
37SkCachingPixelRef::~SkCachingPixelRef() {
halcanary@google.com36d08c52013-12-05 14:00:03 +000038 SkDELETE(fImageGenerator);
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +000039 SkASSERT(NULL == fScaledCacheId);
40 // Assert always unlock before unref.
41}
42
reed@google.comd0419b12014-01-06 17:08:27 +000043bool SkCachingPixelRef::onNewLockPixels(LockRec* rec) {
commit-bot@chromium.org27f89022014-01-03 16:32:45 +000044 if (fErrorInDecoding) {
reed@google.comd0419b12014-01-06 17:08:27 +000045 return false; // don't try again.
commit-bot@chromium.org27f89022014-01-03 16:32:45 +000046 }
reed@google.comba82bd12014-01-06 13:34:39 +000047
reed@google.comd0419b12014-01-06 17:08:27 +000048 const SkImageInfo& info = this->info();
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +000049 SkBitmap bitmap;
halcanary@google.com36d08c52013-12-05 14:00:03 +000050 SkASSERT(NULL == fScaledCacheId);
reed04617132014-08-21 09:46:49 -070051 fScaledCacheId = SkBitmapCache::FindAndLock(this->getGenerationID(), info.fWidth, info.fHeight,
52 &bitmap);
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +000053 if (NULL == fScaledCacheId) {
54 // Cache has been purged, must re-decode.
reedbae704b2014-06-28 14:26:35 -070055 if (!bitmap.allocPixels(info, fRowBytes)) {
halcanary@google.com36d08c52013-12-05 14:00:03 +000056 fErrorInDecoding = true;
reed@google.comd0419b12014-01-06 17:08:27 +000057 return false;
halcanary@google.com36d08c52013-12-05 14:00:03 +000058 }
59 SkAutoLockPixels autoLockPixels(bitmap);
reed@google.combf790232013-12-13 19:45:58 +000060 if (!fImageGenerator->getPixels(info, bitmap.getPixels(), fRowBytes)) {
halcanary@google.com36d08c52013-12-05 14:00:03 +000061 fErrorInDecoding = true;
reed@google.comd0419b12014-01-06 17:08:27 +000062 return false;
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +000063 }
reed04617132014-08-21 09:46:49 -070064 fScaledCacheId = SkBitmapCache::AddAndLock(this->getGenerationID(),
65 info.fWidth, info.fHeight, bitmap);
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +000066 SkASSERT(fScaledCacheId != NULL);
67 }
halcanary@google.com36d08c52013-12-05 14:00:03 +000068
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +000069 // 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.com98272d92014-01-04 07:01:40 +000074
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +000075 // 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.comd0419b12014-01-06 17:08:27 +000083 rec->fPixels = pixels;
84 rec->fColorTable = NULL;
85 rec->fRowBytes = bitmap.rowBytes();
86 return true;
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +000087}
88
89void SkCachingPixelRef::onUnlockPixels() {
reed@google.comc83a91f2013-12-13 13:41:14 +000090 SkASSERT(fScaledCacheId != NULL);
91 SkScaledImageCache::Unlock( static_cast<SkScaledImageCache::ID*>(fScaledCacheId));
92 fScaledCacheId = NULL;
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +000093}