blob: fba98455639e0b1e59fafe9c271a2d60fa5883f0 [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;
14 SkASSERT(generator != NULL);
15 SkASSERT(dst != NULL);
16 if ((NULL == generator)
17 || !(generator->getInfo(&info))
18 || !dst->setConfig(info, 0)) {
19 SkDELETE(generator);
20 return false;
21 }
22 SkAutoTUnref<SkCachingPixelRef> ref(SkNEW_ARGS(SkCachingPixelRef,
23 (generator,
24 info,
25 dst->rowBytes())));
26 dst->setPixelRef(ref);
27 return true;
28}
29
30SkCachingPixelRef::SkCachingPixelRef(SkImageGenerator* generator,
31 const SkImageInfo& info,
32 size_t rowBytes)
reed@google.com3e895242013-12-06 18:41:33 +000033 : INHERITED(info)
34 , fImageGenerator(generator)
halcanary@google.com36d08c52013-12-05 14:00:03 +000035 , fErrorInDecoding(false)
36 , fScaledCacheId(NULL)
halcanary@google.com36d08c52013-12-05 14:00:03 +000037 , fRowBytes(rowBytes) {
38 SkASSERT(fImageGenerator != NULL);
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +000039}
40SkCachingPixelRef::~SkCachingPixelRef() {
halcanary@google.com36d08c52013-12-05 14:00:03 +000041 SkDELETE(fImageGenerator);
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +000042 SkASSERT(NULL == fScaledCacheId);
43 // Assert always unlock before unref.
44}
45
reed@google.com3e895242013-12-06 18:41:33 +000046bool SkCachingPixelRef::onNewLockPixels(LockRec* rec) {
halcanary@google.com36d08c52013-12-05 14:00:03 +000047 if (fErrorInDecoding) {
reed@google.com3e895242013-12-06 18:41:33 +000048 return false; // don't try again.
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +000049 }
reed@google.com3e895242013-12-06 18:41:33 +000050
51 const SkImageInfo& info = this->info();
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +000052 SkBitmap bitmap;
halcanary@google.com36d08c52013-12-05 14:00:03 +000053 SkASSERT(NULL == fScaledCacheId);
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +000054 fScaledCacheId = SkScaledImageCache::FindAndLock(this->getGenerationID(),
reed@google.com3e895242013-12-06 18:41:33 +000055 info.fWidth,
56 info.fHeight,
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +000057 &bitmap);
58 if (NULL == fScaledCacheId) {
59 // Cache has been purged, must re-decode.
reed@google.com3e895242013-12-06 18:41:33 +000060 if ((!bitmap.setConfig(info, fRowBytes)) || !bitmap.allocPixels()) {
halcanary@google.com36d08c52013-12-05 14:00:03 +000061 fErrorInDecoding = true;
reed@google.com3e895242013-12-06 18:41:33 +000062 return false;
halcanary@google.com36d08c52013-12-05 14:00:03 +000063 }
64 SkAutoLockPixels autoLockPixels(bitmap);
reed@google.com3e895242013-12-06 18:41:33 +000065 if (!fImageGenerator->getPixels(info, bitmap.getPixels(), fRowBytes)) {
halcanary@google.com36d08c52013-12-05 14:00:03 +000066 fErrorInDecoding = true;
reed@google.com3e895242013-12-06 18:41:33 +000067 return false;
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +000068 }
69 fScaledCacheId = SkScaledImageCache::AddAndLock(this->getGenerationID(),
reed@google.com3e895242013-12-06 18:41:33 +000070 info.fWidth,
71 info.fHeight,
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +000072 bitmap);
73 SkASSERT(fScaledCacheId != NULL);
74 }
halcanary@google.com36d08c52013-12-05 14:00:03 +000075
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +000076 // Now bitmap should contain a concrete PixelRef of the decoded
77 // image.
78 SkAutoLockPixels autoLockPixels(bitmap);
79 void* pixels = bitmap.getPixels();
80 SkASSERT(pixels != NULL);
reed@google.com3e895242013-12-06 18:41:33 +000081
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +000082 // At this point, the autoLockPixels will unlockPixels()
83 // to remove bitmap's lock on the pixels. We will then
84 // destroy bitmap. The *only* guarantee that this pointer
85 // remains valid is the guarantee made by
86 // SkScaledImageCache that it will not destroy the *other*
87 // bitmap (SkScaledImageCache::Rec.fBitmap) that holds a
88 // reference to the concrete PixelRef while this record is
89 // locked.
reed@google.com3e895242013-12-06 18:41:33 +000090 rec->fPixels = pixels;
91 rec->fColorTable = NULL;
92 rec->fRowBytes = bitmap.rowBytes();
93 return true;
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +000094}
95
96void SkCachingPixelRef::onUnlockPixels() {
97 if (fScaledCacheId != NULL) {
98 SkScaledImageCache::Unlock(
99 static_cast<SkScaledImageCache::ID*>(fScaledCacheId));
100 fScaledCacheId = NULL;
101 }
102}