diff options
author | scroggo@google.com <scroggo@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81> | 2013-02-22 21:38:35 +0000 |
---|---|---|
committer | scroggo@google.com <scroggo@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81> | 2013-02-22 21:38:35 +0000 |
commit | f8d7d2731318cdf510ab68e6b3f5ec68ab22c8e2 (patch) | |
tree | b62a29fabe0b0af3f8f17ffbe8d607508c1d2c00 /tools | |
parent | 5c90e291425b2788f47679266d9584845ceefc2e (diff) |
Create SkLazyPixelRef which performs lazy decoding.
The new pixel ref behaves similarly to SkImageRef, with some key differences:
It does not depend on the images project.
It requires an SkImageCache, which handles allocation and caching of the pixel
memory.
It takes a function signature for decoding which decodes into already allocated
pixel memory rather than into an SkBitmap.
Add two implementations of SkImageCache: SkLruImageCache and SkAshmemImageCache.
Replace SkSerializationHelpers::DecodeBitmap with SkPicture::InstallPixelRefProc,
and update sites that referenced it.
SkBitmapFactory now sets the pixel ref to a new object of the new
class SkLazyPixelRef, provided it has an SkImageCache for caching.
Provide an option to do lazy decodes in render_pictures and bench_pictures.
SkPicture:
Eliminate the default parameters in the constructor.
If a proc for decoding bitmaps is installed, use it to decode any encoded
data in subpictures.
When parsing deserializing subpictures, check for success.
When serializing subpictures, pass the picture's bitmap encoder to the
subpicture's call to serialize.
Update BitmapFactoryTest to test its new behavior.
BUG=https://code.google.com/p/skia/issues/detail?id=1008
BUG=https://code.google.com/p/skia/issues/detail?id=1009
Review URL: https://codereview.appspot.com/7060052
git-svn-id: http://skia.googlecode.com/svn/trunk@7835 2bbb7eff-a529-9590-31e7-b0007b416f81
Diffstat (limited to 'tools')
-rw-r--r-- | tools/bench_pictures_main.cpp | 37 | ||||
-rw-r--r-- | tools/filtermain.cpp | 2 | ||||
-rw-r--r-- | tools/render_pdfs_main.cpp | 3 | ||||
-rw-r--r-- | tools/render_pictures_main.cpp | 48 |
4 files changed, 81 insertions, 9 deletions
diff --git a/tools/bench_pictures_main.cpp b/tools/bench_pictures_main.cpp index 579830d639..60d79fcd8c 100644 --- a/tools/bench_pictures_main.cpp +++ b/tools/bench_pictures_main.cpp @@ -9,6 +9,7 @@ #include "CopyTilesRenderer.h" #include "PictureBenchmark.h" #include "SkBenchLogger.h" +#include "SkBitmapFactory.h" #include "SkCanvas.h" #include "SkGraphics.h" #include "SkImageDecoder.h" @@ -126,6 +127,7 @@ static void usage(const char* argv0) { " [--pipe]\n" " [--bbh bbhType]\n" " [--multi numThreads]\n" +" [--enable-deferred-image-decoding]\n" " [--viewport width height][--scale sf]\n" " [--device bitmap" #if SK_SUPPORT_GPU @@ -186,6 +188,8 @@ static void usage(const char* argv0) { SkDebugf( " --multi numThreads : Set the number of threads for multi threaded drawing. Must be greater\n" " than 1. Only works with tiled rendering.\n" +" --enable-deferred-image-decoding : Defer decoding until drawing images. Has no effect if\n" +" the provided skp does not have its images encoded.\n" " --viewport width height : Set the viewport.\n" " --scale sf : Scale drawing by sf.\n" " --pipe: Benchmark SkGPipe rendering. Currently incompatible with \"mode\".\n"); @@ -227,6 +231,22 @@ static void usage(const char* argv0) { SkBenchLogger gLogger; +bool lazy_decode = false; + +#include "SkData.h" +#include "SkLruImageCache.h" + +static SkLruImageCache gLruImageCache(1024*1024); + +static bool lazy_decode_bitmap(const void* buffer, size_t size, SkBitmap* bitmap) { + void* copiedBuffer = sk_malloc_throw(size); + memcpy(copiedBuffer, buffer, size); + SkAutoDataUnref data(SkData::NewFromMalloc(copiedBuffer, size)); + SkBitmapFactory factory(&SkImageDecoder::DecodeMemoryToTarget); + factory.setImageCache(&gLruImageCache); + return factory.installPixelRef(data, bitmap); +} + static bool run_single_benchmark(const SkString& inputPath, sk_tools::PictureBenchmark& benchmark) { SkFILEStream inputStream; @@ -240,7 +260,14 @@ static bool run_single_benchmark(const SkString& inputPath, } bool success = false; - SkPicture picture(&inputStream, &success, &SkImageDecoder::DecodeStream); + SkPicture* picture; + if (lazy_decode) { + picture = SkNEW_ARGS(SkPicture, (&inputStream, &success, &lazy_decode_bitmap)); + } else { + picture = SkNEW_ARGS(SkPicture, (&inputStream, &success, &SkImageDecoder::DecodeMemory)); + } + SkAutoTDelete<SkPicture> ad(picture); + if (!success) { SkString err; err.printf("Could not read an SkPicture from %s\n", inputPath.c_str()); @@ -252,11 +279,11 @@ static bool run_single_benchmark(const SkString& inputPath, sk_tools::get_basename(&filename, inputPath); SkString result; - result.printf("running bench [%i %i] %s ", picture.width(), - picture.height(), filename.c_str()); + result.printf("running bench [%i %i] %s ", picture->width(), picture->height(), + filename.c_str()); gLogger.logProgress(result); - benchmark.run(&picture); + benchmark.run(picture); return true; } @@ -538,6 +565,8 @@ static void parse_commandline(int argc, char* const argv[], SkTArray<SkString>* benchmark->setTimeIndividualTiles(true); } else if (0 == strcmp(*argv, "--min")) { benchmark->setPrintMin(true); + } else if (0 == strcmp(*argv, "--enable-deferred-image-decoding")) { + lazy_decode = true; } else if (0 == strcmp(*argv, "--logPerIter")) { ++argv; if (argv < stop) { diff --git a/tools/filtermain.cpp b/tools/filtermain.cpp index 3133ada8e5..e0ab8350f8 100644 --- a/tools/filtermain.cpp +++ b/tools/filtermain.cpp @@ -181,7 +181,7 @@ static int filter_picture(const SkString& inFile, const SkString& outFile) { SkFILEStream inStream(inFile.c_str()); if (inStream.isValid()) { - inPicture = SkNEW_ARGS(SkPicture, (&inStream, NULL, &SkImageDecoder::DecodeStream)); + inPicture = SkNEW_ARGS(SkPicture, (&inStream, NULL, &SkImageDecoder::DecodeMemory)); } if (NULL == inPicture) { diff --git a/tools/render_pdfs_main.cpp b/tools/render_pdfs_main.cpp index 3aac682434..6c2543b87d 100644 --- a/tools/render_pdfs_main.cpp +++ b/tools/render_pdfs_main.cpp @@ -8,6 +8,7 @@ #include "SkCanvas.h" #include "SkDevice.h" #include "SkGraphics.h" +#include "SkImageDecoder.h" #include "SkOSFile.h" #include "SkPicture.h" #include "SkStream.h" @@ -129,7 +130,7 @@ static bool render_pdf(const SkString& inputPath, const SkString& outputDir, bool success = false; SkAutoTUnref<SkPicture> - picture(SkNEW_ARGS(SkPicture, (&inputStream, &success))); + picture(SkNEW_ARGS(SkPicture, (&inputStream, &success, &SkImageDecoder::DecodeMemory))); if (!success) { SkDebugf("Could not read an SkPicture from %s\n", inputPath.c_str()); diff --git a/tools/render_pictures_main.cpp b/tools/render_pictures_main.cpp index 3ab5628e6c..146b04552f 100644 --- a/tools/render_pictures_main.cpp +++ b/tools/render_pictures_main.cpp @@ -7,6 +7,7 @@ #include "CopyTilesRenderer.h" #include "SkBitmap.h" +#include "SkBitmapFactory.h" #include "SkCanvas.h" #include "SkDevice.h" #include "SkGraphics.h" @@ -35,6 +36,7 @@ static void usage(const char* argv0) { " [--validate [--maxComponentDiff n]]\n" " [--writeWholeImage]\n" " [--clone n]\n" +" [--enable-deferred-image-decoding]\n" " [--viewport width height][--scale sf]\n" " [--device bitmap" #if SK_SUPPORT_GPU @@ -83,6 +85,8 @@ static void usage(const char* argv0) { SkDebugf( " --multi count : Set the number of threads for multi threaded drawing. Must be greater\n" " than 1. Only works with tiled rendering.\n" +" --enable-deferred-image-decoding : Defer decoding until drawing images. Has no effect if\n" +" the provided skp does not have its images encoded.\n" " --viewport width height : Set the viewport.\n" " --scale sf : Scale drawing by sf.\n" " --pipe: Benchmark SkGPipe rendering. Currently incompatible with \"mode\".\n"); @@ -127,6 +131,39 @@ static void make_output_filepath(SkString* path, const SkString& dir, path->remove(path->size() - 4, 4); } +bool lazy_decode = false; + +#include "SkData.h" +#include "SkLruImageCache.h" + +static SkLruImageCache gLruImageCache(1024*1024); + +#ifdef SK_BUILD_FOR_ANDROID +#include "SkAshmemImageCache.h" +#include "SkImage.h" + +static SkImageCache* cache_selector(const SkImage::Info& info) { + if (info.fWidth * info.fHeight > 32 * 1024) { + return SkAshmemImageCache::GetAshmemImageCache(); + } + return &gLruImageCache; +} + +#endif + +static bool lazy_decode_bitmap(const void* buffer, size_t size, SkBitmap* bitmap) { + void* copiedBuffer = sk_malloc_throw(size); + memcpy(copiedBuffer, buffer, size); + SkAutoDataUnref data(SkData::NewFromMalloc(copiedBuffer, size)); + SkBitmapFactory factory(&SkImageDecoder::DecodeMemoryToTarget); +#ifdef SK_BUILD_FOR_ANDROID + factory.setCacheSelector(&cache_selector); +#else + factory.setImageCache(&gLruImageCache); +#endif + return factory.installPixelRef(data, bitmap); +} + static bool render_picture(const SkString& inputPath, const SkString* outputDir, sk_tools::PictureRenderer& renderer, SkBitmap** out, @@ -142,8 +179,12 @@ static bool render_picture(const SkString& inputPath, const SkString* outputDir, } bool success = false; - SkPicture* picture = SkNEW_ARGS(SkPicture, - (&inputStream, &success, &SkImageDecoder::DecodeStream)); + SkPicture* picture; + if (lazy_decode) { + picture = SkNEW_ARGS(SkPicture, (&inputStream, &success, &lazy_decode_bitmap)); + } else { + picture = SkNEW_ARGS(SkPicture, (&inputStream, &success, &SkImageDecoder::DecodeMemory)); + } if (!success) { SkDebugf("Could not read an SkPicture from %s\n", inputPath.c_str()); return false; @@ -536,7 +577,8 @@ static void parse_commandline(int argc, char* const argv[], SkTArray<SkString>* usage(argv0); exit(-1); } - + } else if (0 == strcmp(*argv, "--enable-deferred-image-decoding")) { + lazy_decode = true; } else if ((0 == strcmp(*argv, "-h")) || (0 == strcmp(*argv, "--help"))) { SkSafeUnref(renderer); usage(argv0); |