blob: 668f57ef302fa97aa7b66da9fbf8bcc28353cdb1 [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
robertphillips@google.com0daa1ad2013-12-13 15:24:37 +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,
reed@google.combf790232013-12-13 19:45:58 +000024 (info, generator, dst->rowBytes())));
halcanary@google.com36d08c52013-12-05 14:00:03 +000025 dst->setPixelRef(ref);
26 return true;
27}
28
reed@google.combf790232013-12-13 19:45:58 +000029SkCachingPixelRef::SkCachingPixelRef(const SkImageInfo& info,
30 SkImageGenerator* generator,
halcanary@google.com36d08c52013-12-05 14:00:03 +000031 size_t rowBytes)
reed@google.combf790232013-12-13 19:45:58 +000032 : INHERITED(info)
33 , fImageGenerator(generator)
halcanary@google.com36d08c52013-12-05 14:00:03 +000034 , fErrorInDecoding(false)
35 , fScaledCacheId(NULL)
halcanary@google.com36d08c52013-12-05 14:00:03 +000036 , fRowBytes(rowBytes) {
37 SkASSERT(fImageGenerator != NULL);
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +000038}
39SkCachingPixelRef::~SkCachingPixelRef() {
halcanary@google.com36d08c52013-12-05 14:00:03 +000040 SkDELETE(fImageGenerator);
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +000041 SkASSERT(NULL == fScaledCacheId);
42 // Assert always unlock before unref.
43}
44
reed@google.combf790232013-12-13 19:45:58 +000045void* SkCachingPixelRef::onLockPixels(SkColorTable**) {
46 const SkImageInfo& info = this->info();
47
halcanary@google.com36d08c52013-12-05 14:00:03 +000048 if (fErrorInDecoding) {
robertphillips@google.com0daa1ad2013-12-13 15:24:37 +000049 return NULL; // don't try again.
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +000050 }
51 SkBitmap bitmap;
halcanary@google.com36d08c52013-12-05 14:00:03 +000052 SkASSERT(NULL == fScaledCacheId);
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +000053 fScaledCacheId = SkScaledImageCache::FindAndLock(this->getGenerationID(),
reed@google.combf790232013-12-13 19:45:58 +000054 info.fWidth,
55 info.fHeight,
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +000056 &bitmap);
57 if (NULL == fScaledCacheId) {
58 // Cache has been purged, must re-decode.
reed@google.combf790232013-12-13 19:45:58 +000059 if ((!bitmap.setConfig(info, fRowBytes)) || !bitmap.allocPixels()) {
halcanary@google.com36d08c52013-12-05 14:00:03 +000060 fErrorInDecoding = true;
robertphillips@google.com0daa1ad2013-12-13 15:24:37 +000061 return NULL;
halcanary@google.com36d08c52013-12-05 14:00:03 +000062 }
63 SkAutoLockPixels autoLockPixels(bitmap);
reed@google.combf790232013-12-13 19:45:58 +000064 if (!fImageGenerator->getPixels(info, bitmap.getPixels(), fRowBytes)) {
halcanary@google.com36d08c52013-12-05 14:00:03 +000065 fErrorInDecoding = true;
robertphillips@google.com0daa1ad2013-12-13 15:24:37 +000066 return NULL;
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +000067 }
68 fScaledCacheId = SkScaledImageCache::AddAndLock(this->getGenerationID(),
reed@google.combf790232013-12-13 19:45:58 +000069 info.fWidth,
70 info.fHeight,
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +000071 bitmap);
72 SkASSERT(fScaledCacheId != NULL);
73 }
halcanary@google.com36d08c52013-12-05 14:00:03 +000074
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +000075 // Now bitmap should contain a concrete PixelRef of the decoded
76 // image.
77 SkAutoLockPixels autoLockPixels(bitmap);
78 void* pixels = bitmap.getPixels();
79 SkASSERT(pixels != NULL);
80 // At this point, the autoLockPixels will unlockPixels()
81 // to remove bitmap's lock on the pixels. We will then
82 // destroy bitmap. The *only* guarantee that this pointer
83 // remains valid is the guarantee made by
84 // SkScaledImageCache that it will not destroy the *other*
85 // bitmap (SkScaledImageCache::Rec.fBitmap) that holds a
86 // reference to the concrete PixelRef while this record is
87 // locked.
robertphillips@google.com0daa1ad2013-12-13 15:24:37 +000088 return pixels;
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +000089}
90
91void SkCachingPixelRef::onUnlockPixels() {
reed@google.comc83a91f2013-12-13 13:41:14 +000092 SkASSERT(fScaledCacheId != NULL);
93 SkScaledImageCache::Unlock( static_cast<SkScaledImageCache::ID*>(fScaledCacheId));
94 fScaledCacheId = NULL;
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +000095}