blob: 83fca5f3b28fd1c0d332e2ae5967bc4506653fcb [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 "SkDecodingImageGenerator.h"
12#include "SkDiscardableMemoryPool.h"
halcanary@google.comedd370f2013-12-10 21:11:12 +000013#include "SkImageGenerator.h"
commit-bot@chromium.org56799e22013-07-16 18:21:46 +000014#include "SkForceLinking.h"
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000015
commit-bot@chromium.org56799e22013-07-16 18:21:46 +000016#include "SkCommandLineFlags.h"
17
18__SK_FORCE_IMAGE_DECODER_LINKING;
19
20DEFINE_bool(useVolatileCache, false, "Use a volatile cache for deferred image decoding pixels. "
21 "Only meaningful if --deferImageDecoding is set to true and the platform has an "
22 "implementation.");
23
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000024// Fits SkPicture::InstallPixelRefProc call signature.
25// Used in SkPicturePlayback::CreateFromStream
26bool sk_tools::LazyDecodeBitmap(const void* src,
27 size_t length,
28 SkBitmap* dst) {
29 SkAutoDataUnref data(SkData::NewWithCopy(src, length));
30 if (NULL == data.get()) {
31 return false;
commit-bot@chromium.org56799e22013-07-16 18:21:46 +000032 }
33
halcanary@google.com3d50ea12014-01-02 13:15:13 +000034 SkAutoTDelete<SkImageGenerator> gen(
35 SkDecodingImageGenerator::Create(
36 data, SkDecodingImageGenerator::Options()));
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000037 SkImageInfo info;
halcanary@google.com3d50ea12014-01-02 13:15:13 +000038 if ((NULL == gen.get()) || !gen->getInfo(&info)) {
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000039 return false;
commit-bot@chromium.org56799e22013-07-16 18:21:46 +000040 }
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000041 SkDiscardableMemory::Factory* pool = NULL;
halcanary@google.combc55eec2013-12-10 18:33:07 +000042 if ((!FLAGS_useVolatileCache) || (info.fWidth * info.fHeight < 32 * 1024)) {
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000043 // how to do switching with SkDiscardableMemory.
44 pool = SkGetGlobalDiscardableMemoryPool();
halcanary@google.combc55eec2013-12-10 18:33:07 +000045 // Only meaningful if platform has a default discardable
46 // memory implementation that differs from the global DM pool.
commit-bot@chromium.org56799e22013-07-16 18:21:46 +000047 }
halcanary@google.comedd370f2013-12-10 21:11:12 +000048 return SkInstallDiscardablePixelRef(gen.detach(), dst, pool);
commit-bot@chromium.org56799e22013-07-16 18:21:46 +000049}