blob: b7eaf574aac436223220923d7cd00fab2faa66af [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
reed@google.com398337b2013-12-11 21:22:39 +000011
halcanary@google.com36d08c52013-12-05 14:00:03 +000012bool SkCachingPixelRef::Install(SkImageGenerator* generator,
13 SkBitmap* dst) {
14 SkImageInfo info;
15 SkASSERT(generator != NULL);
16 SkASSERT(dst != NULL);
17 if ((NULL == generator)
18 || !(generator->getInfo(&info))
19 || !dst->setConfig(info, 0)) {
20 SkDELETE(generator);
21 return false;
22 }
23 SkAutoTUnref<SkCachingPixelRef> ref(SkNEW_ARGS(SkCachingPixelRef,
24 (generator,
25 info,
26 dst->rowBytes())));
27 dst->setPixelRef(ref);
28 return true;
29}
30
31SkCachingPixelRef::SkCachingPixelRef(SkImageGenerator* generator,
32 const SkImageInfo& info,
33 size_t rowBytes)
reed@google.com398337b2013-12-11 21:22:39 +000034 : fImageGenerator(generator)
halcanary@google.com36d08c52013-12-05 14:00:03 +000035 , fErrorInDecoding(false)
36 , fScaledCacheId(NULL)
reed@google.com398337b2013-12-11 21:22:39 +000037 , fInfo(info)
halcanary@google.com36d08c52013-12-05 14:00:03 +000038 , fRowBytes(rowBytes) {
39 SkASSERT(fImageGenerator != NULL);
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +000040}
41SkCachingPixelRef::~SkCachingPixelRef() {
halcanary@google.com36d08c52013-12-05 14:00:03 +000042 SkDELETE(fImageGenerator);
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +000043 SkASSERT(NULL == fScaledCacheId);
44 // Assert always unlock before unref.
45}
46
reed@google.com398337b2013-12-11 21:22:39 +000047void* SkCachingPixelRef::onLockPixels(SkColorTable** colorTable) {
48 (void)colorTable;
halcanary@google.com36d08c52013-12-05 14:00:03 +000049 if (fErrorInDecoding) {
reed@google.com398337b2013-12-11 21:22:39 +000050 return NULL; // don't try again.
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +000051 }
52 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.com398337b2013-12-11 21:22:39 +000055 fInfo.fWidth,
56 fInfo.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.com398337b2013-12-11 21:22:39 +000060 if ((!bitmap.setConfig(fInfo, fRowBytes)) || !bitmap.allocPixels()) {
halcanary@google.com36d08c52013-12-05 14:00:03 +000061 fErrorInDecoding = true;
reed@google.com398337b2013-12-11 21:22:39 +000062 return NULL;
halcanary@google.com36d08c52013-12-05 14:00:03 +000063 }
64 SkAutoLockPixels autoLockPixels(bitmap);
reed@google.com398337b2013-12-11 21:22:39 +000065 if (!fImageGenerator->getPixels(fInfo, bitmap.getPixels(), fRowBytes)) {
halcanary@google.com36d08c52013-12-05 14:00:03 +000066 fErrorInDecoding = true;
reed@google.com398337b2013-12-11 21:22:39 +000067 return NULL;
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +000068 }
69 fScaledCacheId = SkScaledImageCache::AddAndLock(this->getGenerationID(),
reed@google.com398337b2013-12-11 21:22:39 +000070 fInfo.fWidth,
71 fInfo.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);
81 // At this point, the autoLockPixels will unlockPixels()
82 // to remove bitmap's lock on the pixels. We will then
83 // destroy bitmap. The *only* guarantee that this pointer
84 // remains valid is the guarantee made by
85 // SkScaledImageCache that it will not destroy the *other*
86 // bitmap (SkScaledImageCache::Rec.fBitmap) that holds a
87 // reference to the concrete PixelRef while this record is
88 // locked.
reed@google.com398337b2013-12-11 21:22:39 +000089 return pixels;
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +000090}
91
92void SkCachingPixelRef::onUnlockPixels() {
reed@google.com772d8522013-12-12 16:27:12 +000093 SkASSERT(fScaledCacheId != NULL);
94 SkScaledImageCache::Unlock( static_cast<SkScaledImageCache::ID*>(fScaledCacheId));
95 fScaledCacheId = NULL;
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +000096}