2 * Copyright 2013 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "SkCachingPixelRef.h"
9 #include "SkBitmapCache.h"
12 bool SkCachingPixelRef::Install(SkImageGenerator* generator,
15 SkASSERT(dst != NULL);
16 if ((NULL == generator)
17 || !(generator->getInfo(&info))
18 || !dst->setInfo(info)) {
22 SkAutoTUnref<SkCachingPixelRef> ref(SkNEW_ARGS(SkCachingPixelRef,
23 (info, generator, dst->rowBytes())));
24 dst->setPixelRef(ref);
28 SkCachingPixelRef::SkCachingPixelRef(const SkImageInfo& info,
29 SkImageGenerator* generator,
32 , fImageGenerator(generator)
33 , fErrorInDecoding(false)
34 , fRowBytes(rowBytes) {
35 SkASSERT(fImageGenerator != NULL);
37 SkCachingPixelRef::~SkCachingPixelRef() {
38 SkDELETE(fImageGenerator);
39 // Assert always unlock before unref.
42 bool SkCachingPixelRef::onNewLockPixels(LockRec* rec) {
43 if (fErrorInDecoding) {
44 return false; // don't try again.
47 const SkImageInfo& info = this->info();
48 if (!SkBitmapCache::Find(
49 this->getGenerationID(), info.bounds(), &fLockedBitmap)) {
50 // Cache has been purged, must re-decode.
51 if (!fLockedBitmap.tryAllocPixels(info, fRowBytes)) {
52 fErrorInDecoding = true;
55 if (!fImageGenerator->getPixels(info, fLockedBitmap.getPixels(), fRowBytes)) {
56 fErrorInDecoding = true;
59 fLockedBitmap.setImmutable();
61 this->getGenerationID(), info.bounds(), fLockedBitmap);
64 // Now bitmap should contain a concrete PixelRef of the decoded image.
65 void* pixels = fLockedBitmap.getPixels();
66 SkASSERT(pixels != NULL);
67 rec->fPixels = pixels;
68 rec->fColorTable = NULL;
69 rec->fRowBytes = fLockedBitmap.rowBytes();
73 void SkCachingPixelRef::onUnlockPixels() {
74 fLockedBitmap.reset();