blob: 033a6b5ebba17cb866faca4b23c2c48ce1fba77e [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"
9#include "SkScaledImageCache.h"
10
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);
15 if ((NULL == generator)
16 || !(generator->getInfo(&info))
17 || !dst->setConfig(info, 0)) {
18 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())));
halcanary@google.com36d08c52013-12-05 14:00:03 +000023 dst->setPixelRef(ref);
24 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.comf1ce0522014-01-03 16:22:48 +000043bool SkCachingPixelRef::onNewLockPixels(LockRec* rec) {
halcanary@google.com36d08c52013-12-05 14:00:03 +000044 if (fErrorInDecoding) {
reed@google.comf1ce0522014-01-03 16:22:48 +000045 return false; // don't try again.
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +000046 }
reed@google.comf1ce0522014-01-03 16:22:48 +000047
48 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);
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +000051 fScaledCacheId = SkScaledImageCache::FindAndLock(this->getGenerationID(),
reed@google.combf790232013-12-13 19:45:58 +000052 info.fWidth,
53 info.fHeight,
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +000054 &bitmap);
55 if (NULL == fScaledCacheId) {
56 // Cache has been purged, must re-decode.
reed@google.combf790232013-12-13 19:45:58 +000057 if ((!bitmap.setConfig(info, fRowBytes)) || !bitmap.allocPixels()) {
halcanary@google.com36d08c52013-12-05 14:00:03 +000058 fErrorInDecoding = true;
reed@google.comf1ce0522014-01-03 16:22:48 +000059 return false;
halcanary@google.com36d08c52013-12-05 14:00:03 +000060 }
61 SkAutoLockPixels autoLockPixels(bitmap);
reed@google.combf790232013-12-13 19:45:58 +000062 if (!fImageGenerator->getPixels(info, bitmap.getPixels(), fRowBytes)) {
halcanary@google.com36d08c52013-12-05 14:00:03 +000063 fErrorInDecoding = true;
reed@google.comf1ce0522014-01-03 16:22:48 +000064 return false;
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +000065 }
66 fScaledCacheId = SkScaledImageCache::AddAndLock(this->getGenerationID(),
reed@google.combf790232013-12-13 19:45:58 +000067 info.fWidth,
68 info.fHeight,
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +000069 bitmap);
70 SkASSERT(fScaledCacheId != NULL);
71 }
halcanary@google.com36d08c52013-12-05 14:00:03 +000072
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +000073 // Now bitmap should contain a concrete PixelRef of the decoded
74 // image.
75 SkAutoLockPixels autoLockPixels(bitmap);
76 void* pixels = bitmap.getPixels();
77 SkASSERT(pixels != NULL);
reed@google.comf1ce0522014-01-03 16:22:48 +000078
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +000079 // At this point, the autoLockPixels will unlockPixels()
80 // to remove bitmap's lock on the pixels. We will then
81 // destroy bitmap. The *only* guarantee that this pointer
82 // remains valid is the guarantee made by
83 // SkScaledImageCache that it will not destroy the *other*
84 // bitmap (SkScaledImageCache::Rec.fBitmap) that holds a
85 // reference to the concrete PixelRef while this record is
86 // locked.
reed@google.comf1ce0522014-01-03 16:22:48 +000087 rec->fPixels = pixels;
88 rec->fColorTable = NULL;
89 rec->fRowBytes = bitmap.rowBytes();
90 return true;
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +000091}
92
93void SkCachingPixelRef::onUnlockPixels() {
reed@google.comc83a91f2013-12-13 13:41:14 +000094 SkASSERT(fScaledCacheId != NULL);
95 SkScaledImageCache::Unlock( static_cast<SkScaledImageCache::ID*>(fScaledCacheId));
96 fScaledCacheId = NULL;
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +000097}