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

Unified Diff: tools/LazyDecodeBitmap.cpp

Issue 19109002: Add the lazy decoder from PictureFlags to SkImageDecoder (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: Add include Created 7 years, 5 months 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
« no previous file with comments | « tools/LazyDecodeBitmap.h ('k') | tools/PictureRenderingFlags.cpp » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: tools/LazyDecodeBitmap.cpp
diff --git a/tools/LazyDecodeBitmap.cpp b/tools/LazyDecodeBitmap.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..23671541111b69d2cdb6b3287f293cac0c4ceeb1
--- /dev/null
+++ b/tools/LazyDecodeBitmap.cpp
@@ -0,0 +1,76 @@
+/*
+ * 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 "LazyDecodeBitmap.h"
+
+#include "PictureRenderingFlags.h" // --deferImageDecoding is defined here.
+#include "SkBitmap.h"
+#include "SkData.h"
+#include "SkImageDecoder.h"
+#include "SkForceLinking.h"
+#include "SkLruImageCache.h"
+#include "SkPurgeableImageCache.h"
+#include "SkCommandLineFlags.h"
+
+__SK_FORCE_IMAGE_DECODER_LINKING;
+
+DEFINE_bool(useVolatileCache, false, "Use a volatile cache for deferred image decoding pixels. "
+ "Only meaningful if --deferImageDecoding is set to true and the platform has an "
+ "implementation.");
+
+SkLruImageCache gLruImageCache(1024 * 1024);
+
+namespace sk_tools {
+
+// Simple cache selector to choose between a purgeable cache for large images and the standard one
+// for smaller images.
+//
+class CacheSelector : public SkBitmapFactory::CacheSelector {
+
+public:
+ CacheSelector() {
+ fPurgeableImageCache = SkPurgeableImageCache::Create();
+ }
+
+ ~CacheSelector() {
+ SkSafeUnref(fPurgeableImageCache);
+ }
+
+ virtual SkImageCache* selectCache(const SkImage::Info& info) SK_OVERRIDE {
+ if (info.fWidth * info.fHeight > 32 * 1024 && fPurgeableImageCache != NULL) {
+ return fPurgeableImageCache;
+ }
+ return &gLruImageCache;
+ }
+private:
+ SkImageCache* fPurgeableImageCache;
+};
+
+static CacheSelector gCacheSelector;
+static SkBitmapFactory gFactory(&SkImageDecoder::DecodeMemoryToTarget);
+
+bool LazyDecodeBitmap(const void* buffer, size_t size, SkBitmap* bitmap) {
+ void* copiedBuffer = sk_malloc_throw(size);
+ memcpy(copiedBuffer, buffer, size);
+ SkAutoDataUnref data(SkData::NewFromMalloc(copiedBuffer, size));
+
+ static bool gOnce;
+ if (!gOnce) {
+ // Only use the cache selector if there is a purgeable image cache to use for large
+ // images.
+ if (FLAGS_useVolatileCache && SkAutoTUnref<SkImageCache>(
+ SkPurgeableImageCache::Create()).get() != NULL) {
+ gFactory.setCacheSelector(&gCacheSelector);
+ } else {
+ gFactory.setImageCache(&gLruImageCache);
+ }
+ gOnce = true;
+ }
+ return gFactory.installPixelRef(data, bitmap);
+}
+
+} // namespace sk_tools
« no previous file with comments | « tools/LazyDecodeBitmap.h ('k') | tools/PictureRenderingFlags.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698