blob: 3b6f17e741195e8e6ab362a5e737f932db4748cf [file] [log] [blame]
commit-bot@chromium.org56799e22013-07-16 18:21:46 +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 "LazyDecodeBitmap.h"
9
commit-bot@chromium.org56799e22013-07-16 18:21:46 +000010#include "SkData.h"
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000011#include "SkDiscardableMemoryPool.h"
commit-bot@chromium.org2d970b52014-05-27 14:14:22 +000012#include "SkImageGeneratorPriv.h"
commit-bot@chromium.org56799e22013-07-16 18:21:46 +000013#include "SkForceLinking.h"
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000014
commit-bot@chromium.org56799e22013-07-16 18:21:46 +000015#include "SkCommandLineFlags.h"
16
17__SK_FORCE_IMAGE_DECODER_LINKING;
18
19DEFINE_bool(useVolatileCache, false, "Use a volatile cache for deferred image decoding pixels. "
20 "Only meaningful if --deferImageDecoding is set to true and the platform has an "
21 "implementation.");
22
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000023// Fits SkPicture::InstallPixelRefProc call signature.
robertphillipsdb539902014-07-01 08:47:04 -070024// Used in SkPictureData::CreateFromStream
reed3ef71e32015-03-19 08:31:14 -070025bool sk_tools::LazyDecodeBitmap(const void* src, size_t length, SkBitmap* dst) {
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000026 SkAutoDataUnref data(SkData::NewWithCopy(src, length));
27 if (NULL == data.get()) {
28 return false;
commit-bot@chromium.org56799e22013-07-16 18:21:46 +000029 }
30
reed5965c8a2015-01-07 18:04:45 -080031 SkAutoTDelete<SkImageGenerator> gen(SkImageGenerator::NewFromData(data));
reed3ef71e32015-03-19 08:31:14 -070032 if (NULL == gen.get()) {
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000033 return false;
commit-bot@chromium.org56799e22013-07-16 18:21:46 +000034 }
reed3ef71e32015-03-19 08:31:14 -070035 const SkImageInfo info = gen->getInfo();
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000036 SkDiscardableMemory::Factory* pool = NULL;
reede5ea5002014-09-03 11:54:58 -070037 if ((!FLAGS_useVolatileCache) || (info.width() * info.height() < 32 * 1024)) {
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000038 // how to do switching with SkDiscardableMemory.
39 pool = SkGetGlobalDiscardableMemoryPool();
halcanary@google.combc55eec2013-12-10 18:33:07 +000040 // Only meaningful if platform has a default discardable
41 // memory implementation that differs from the global DM pool.
commit-bot@chromium.org56799e22013-07-16 18:21:46 +000042 }
halcanary@google.comedd370f2013-12-10 21:11:12 +000043 return SkInstallDiscardablePixelRef(gen.detach(), dst, pool);
commit-bot@chromium.org56799e22013-07-16 18:21:46 +000044}