Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1034)

Unified Diff: src/lazy/SkCachingPixelRef.cpp

Issue 54203006: Break up SkLazyPixelRef functionally into class hierarchy. (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: changes for reed and scroggo Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: src/lazy/SkCachingPixelRef.cpp
diff --git a/src/lazy/SkCachingPixelRef.cpp b/src/lazy/SkCachingPixelRef.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..647d2a4c8893109f3ddc792878ec851d25ab45f0
--- /dev/null
+++ b/src/lazy/SkCachingPixelRef.cpp
@@ -0,0 +1,129 @@
+/*
+ * Copyright 2013 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include "SkCachingPixelRef.h"
+
+SkCachingPixelRef::SkCachingPixelRef(SkScaledImageCache* cache)
+ : fErrorInDecoding(false)
+ , fCache(cache)
+ , fScaledCacheId(NULL) {
+ // Please note: a non-NULL cache is not thread-safe.
+ memset(&fInfo, 0xFF, sizeof(fInfo));
+}
+SkCachingPixelRef::~SkCachingPixelRef() {
+ SkASSERT(NULL == fScaledCacheId);
+ // Assert always unlock before unref.
+}
+
+const SkImageInfo* SkCachingPixelRef::getInfo() {
+ if (fErrorInDecoding) {
+ SkASSERT(fInfo.fWidth < 0);
+ return NULL; // Don't try again.
+ }
+ if (fInfo.fWidth >= 0) {
+ return &fInfo; // already successful
+ }
+ SkImageInfo tmp;
+ if (!this->onDecodeInfo(&tmp)) {
+ fErrorInDecoding = true;
+ return NULL;
+ }
+ fInfo = tmp;
+ SkASSERT(fInfo.fWidth >= 0);
+ return &fInfo;
+}
+
+bool SkCachingPixelRef::configure(SkBitmap* bitmap) {
+ const SkImageInfo* info = this->getInfo();
+ if (NULL == info) {
+ return false;
+ }
+ return bitmap->setConfig(*info, 0);
+}
+
+void* SkCachingPixelRef::onLockPixels(SkColorTable** colorTable) {
+ (void)colorTable;
+ const SkImageInfo* info = this->getInfo();
+ if (NULL == info) {
+ return NULL;
+ }
+ SkBitmap bitmap;
+
+ if (NULL == fCache) {
+ fScaledCacheId = SkScaledImageCache::FindAndLock(
+ this->getGenerationID(),
+ info->fWidth,
+ info->fHeight,
+ &bitmap);
+ } else {
+ fScaledCacheId = fCache->findAndLock(this->getGenerationID(),
+ info->fWidth,
+ info->fHeight,
+ &bitmap);
+ }
+ if (NULL == fScaledCacheId) {
+ // Cache has been purged, must re-decode.
+ if (!this->onDecodeInto(0, &bitmap)) {
+ return NULL;
+ }
+ if (NULL == fCache) {
+ fScaledCacheId = SkScaledImageCache::AddAndLock(
+ this->getGenerationID(),
+ info->fWidth,
+ info->fHeight,
+ bitmap);
+ } else {
+ fScaledCacheId = fCache->addAndLock(this->getGenerationID(),
+ info->fWidth,
+ info->fHeight,
+ bitmap);
+ }
+ SkASSERT(fScaledCacheId != NULL);
+ }
+ // Now bitmap should contain a concrete PixelRef of the decoded
+ // image.
+ SkAutoLockPixels autoLockPixels(bitmap);
+ void* pixels = bitmap.getPixels();
+ SkASSERT(NULL != pixels);
+ // At this point, the autoLockPixels will unlockPixels()
+ // to remove bitmap's lock on the pixels. We will then
+ // destroy bitmap. The *only* guarantee that this pointer
+ // remains valid is the guarantee made by
+ // SkScaledImageCache that it will not destroy the *other*
+ // bitmap (SkScaledImageCache::Rec.fBitmap) that holds a
+ // reference to the concrete PixelRef while this record is
+ // locked.
+ return pixels;
+}
+
+void SkCachingPixelRef::onUnlockPixels() {
+ if (NULL != fScaledCacheId) {
+ if (NULL == fCache) {
+ SkScaledImageCache::Unlock(fScaledCacheId);
+ } else {
+ fCache->unlock(fScaledCacheId);
+ }
+ fScaledCacheId = NULL;
+ }
+}
+
+bool SkCachingPixelRef::onDecodeInto(int pow2, SkBitmap* bitmap) {
+ SkBitmap tmp;
+ // TODO(halcanary) - Enable SkCachingPixelRef to use a custom
+ // allocator. `tmp.allocPixels(fAllocator, NULL)`
+ if (!(this->configure(&tmp) && tmp.allocPixels(NULL, NULL))) {
+ fErrorInDecoding = true;
+ return false;
+ }
+ if (!this->onDecode(tmp.getPixels(), tmp.rowBytes())) {
+ fErrorInDecoding = true;
+ return false;
+ }
+ *bitmap = tmp;
+ return true;
+}
+

Powered by Google App Engine
This is Rietveld 408576698