blob: 452ea4ed03eb8c1f473cae08324f684cb5ef9e73 [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.comba82bd12014-01-06 13:34:39 +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.comba82bd12014-01-06 13:34:39 +000044void* SkCachingPixelRef::onLockPixels(SkColorTable**) {
commit-bot@chromium.org27f89022014-01-03 16:32:45 +000045 if (fErrorInDecoding) {
reed@google.comba82bd12014-01-06 13:34:39 +000046 return NULL; // don't try again.
commit-bot@chromium.org27f89022014-01-03 16:32:45 +000047 }
reed@google.comba82bd12014-01-06 13:34:39 +000048
reed@google.com92713892014-01-03 17:58:57 +000049 const SkImageInfo& info = this->info();
reed@google.comba82bd12014-01-06 13:34:39 +000050
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +000051 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;
reed@google.comba82bd12014-01-06 13:34:39 +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;
reed@google.comba82bd12014-01-06 13:34:39 +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);
skia.committer@gmail.com98272d92014-01-04 07:01:40 +000080
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +000081 // 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.comba82bd12014-01-06 13:34:39 +000089 return pixels;
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +000090}
91
92void SkCachingPixelRef::onUnlockPixels() {
reed@google.comc83a91f2013-12-13 13:41:14 +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}