blob: fb30d051e165a4a992792703131a9f3912a33a0e [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;
halcanary@google.com36d08c52013-12-05 14:00:03 +000015 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,
reed@google.combf790232013-12-13 19:45:58 +000023 (info, generator, dst->rowBytes())));
halcanary@google.com36d08c52013-12-05 14:00:03 +000024 dst->setPixelRef(ref);
25 return true;
26}
27
reed@google.combf790232013-12-13 19:45:58 +000028SkCachingPixelRef::SkCachingPixelRef(const SkImageInfo& info,
29 SkImageGenerator* generator,
halcanary@google.com36d08c52013-12-05 14:00:03 +000030 size_t rowBytes)
reed@google.combf790232013-12-13 19:45:58 +000031 : INHERITED(info)
32 , fImageGenerator(generator)
halcanary@google.com36d08c52013-12-05 14:00:03 +000033 , fErrorInDecoding(false)
34 , fScaledCacheId(NULL)
halcanary@google.com36d08c52013-12-05 14:00:03 +000035 , fRowBytes(rowBytes) {
36 SkASSERT(fImageGenerator != NULL);
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +000037}
38SkCachingPixelRef::~SkCachingPixelRef() {
halcanary@google.com36d08c52013-12-05 14:00:03 +000039 SkDELETE(fImageGenerator);
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +000040 SkASSERT(NULL == fScaledCacheId);
41 // Assert always unlock before unref.
42}
43
reed@google.combf790232013-12-13 19:45:58 +000044void* SkCachingPixelRef::onLockPixels(SkColorTable**) {
45 const SkImageInfo& info = this->info();
46
halcanary@google.com36d08c52013-12-05 14:00:03 +000047 if (fErrorInDecoding) {
robertphillips@google.com0daa1ad2013-12-13 15:24:37 +000048 return NULL; // don't try again.
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +000049 }
50 SkBitmap bitmap;
halcanary@google.com36d08c52013-12-05 14:00:03 +000051 SkASSERT(NULL == fScaledCacheId);
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +000052 fScaledCacheId = SkScaledImageCache::FindAndLock(this->getGenerationID(),
reed@google.combf790232013-12-13 19:45:58 +000053 info.fWidth,
54 info.fHeight,
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +000055 &bitmap);
56 if (NULL == fScaledCacheId) {
57 // Cache has been purged, must re-decode.
reed@google.combf790232013-12-13 19:45:58 +000058 if ((!bitmap.setConfig(info, fRowBytes)) || !bitmap.allocPixels()) {
halcanary@google.com36d08c52013-12-05 14:00:03 +000059 fErrorInDecoding = true;
robertphillips@google.com0daa1ad2013-12-13 15:24:37 +000060 return NULL;
halcanary@google.com36d08c52013-12-05 14:00:03 +000061 }
62 SkAutoLockPixels autoLockPixels(bitmap);
reed@google.combf790232013-12-13 19:45:58 +000063 if (!fImageGenerator->getPixels(info, bitmap.getPixels(), fRowBytes)) {
halcanary@google.com36d08c52013-12-05 14:00:03 +000064 fErrorInDecoding = true;
robertphillips@google.com0daa1ad2013-12-13 15:24:37 +000065 return NULL;
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +000066 }
67 fScaledCacheId = SkScaledImageCache::AddAndLock(this->getGenerationID(),
reed@google.combf790232013-12-13 19:45:58 +000068 info.fWidth,
69 info.fHeight,
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +000070 bitmap);
71 SkASSERT(fScaledCacheId != NULL);
72 }
halcanary@google.com36d08c52013-12-05 14:00:03 +000073
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +000074 // Now bitmap should contain a concrete PixelRef of the decoded
75 // image.
76 SkAutoLockPixels autoLockPixels(bitmap);
77 void* pixels = bitmap.getPixels();
78 SkASSERT(pixels != NULL);
79 // 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.
robertphillips@google.com0daa1ad2013-12-13 15:24:37 +000087 return pixels;
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +000088}
89
90void SkCachingPixelRef::onUnlockPixels() {
reed@google.comc83a91f2013-12-13 13:41:14 +000091 SkASSERT(fScaledCacheId != NULL);
92 SkScaledImageCache::Unlock( static_cast<SkScaledImageCache::ID*>(fScaledCacheId));
93 fScaledCacheId = NULL;
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +000094}