diff options
author | kjlubick <kjlubick@google.com> | 2016-02-10 11:25:07 -0800 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2016-02-10 11:25:07 -0800 |
commit | 1de415f2c33febbac668500afbbb0c9b120c125f (patch) | |
tree | 6fea38167e591a5908c024866851a294dbd8e4c5 | |
parent | 026388a01864c74208ad57d1ba4f711602d101c6 (diff) |
Revert of Make SkPicture/SkImageGenerator default to SkCodec (patchset #7 id:120001 of https://codereview.chromium.org/1671193002/ )
Reason for revert:
Breaks Ubuntu and Mac CMAKE
Original issue's description:
> Make SkPicture/SkImageGenerator default to SkCodec
>
> Remove reference to SkImageDecoder from SkPicture. Make the default
> InstallPixelRefProc passed to CreateFromStream use
> SkImageGenerator::NewFromEncoded instead.
>
> Make SkImageGenerator::NewFromEncoded create an SkCodecImageGenerator.
> Remove the old version that used SkImageDecoder.
>
> Remove all versions of lazy_decode_bitmap/LazyDecodeBitmap. The default
> now behaves lazily.
>
> Update all clients to use the default.
>
> Move SkImageDecoderGenerator into KtxTest.cpp, and use it directly.
>
> BUG=skia:4691
> BUG=skia:4290
> GOLD_TRYBOT_URL= https://gold.skia.org/search2?unt=true&query=source_type%3Dgm&master=false&issue=1671193002
>
> Committed: https://skia.googlesource.com/skia/+/026388a01864c74208ad57d1ba4f711602d101c6
TBR=msarett@google.com,reed@google.com,scroggo@google.com
# Skipping CQ checks because original CL landed less than 1 days ago.
NOPRESUBMIT=true
NOTREECHECKS=true
NOTRY=true
BUG=skia:4691
Review URL: https://codereview.chromium.org/1685963004
-rw-r--r-- | bench/ETCBitmapBench.cpp | 7 | ||||
-rw-r--r-- | dm/DMSrcSink.cpp | 9 | ||||
-rw-r--r-- | gyp/codec.gyp | 1 | ||||
-rw-r--r-- | gyp/images.gyp | 2 | ||||
-rw-r--r-- | gyp/tools.gyp | 18 | ||||
-rw-r--r-- | include/core/SkPicture.h | 15 | ||||
-rw-r--r-- | public.bzl | 2 | ||||
-rw-r--r-- | src/core/SkPicture.cpp | 11 | ||||
-rw-r--r-- | src/images/SkImageDecoder_ktx.cpp | 99 | ||||
-rw-r--r-- | src/ports/SkImageGenerator_skia.cpp | 96 | ||||
-rw-r--r-- | tests/KtxTest.cpp | 5 | ||||
-rw-r--r-- | tests/PictureTest.cpp | 34 | ||||
-rw-r--r-- | tools/BUILD.public.expected | 2 | ||||
-rw-r--r-- | tools/LazyDecodeBitmap.cpp | 44 | ||||
-rw-r--r-- | tools/LazyDecodeBitmap.h | 24 | ||||
-rw-r--r-- | tools/dump_record.cpp | 4 | ||||
-rw-r--r-- | tools/gpuveto.cpp | 5 | ||||
-rw-r--r-- | tools/lua/lua_pictures.cpp | 3 | ||||
-rw-r--r-- | tools/pinspect.cpp | 3 |
19 files changed, 211 insertions, 173 deletions
diff --git a/bench/ETCBitmapBench.cpp b/bench/ETCBitmapBench.cpp index 305f492108..1b06112996 100644 --- a/bench/ETCBitmapBench.cpp +++ b/bench/ETCBitmapBench.cpp @@ -73,9 +73,6 @@ static etc1_byte* create_expanded_etc1_bitmap(const uint8_t* orig, int factor) { return newData; } -// Defined in SkImageDecoder_ktx.cpp -extern SkImageGenerator* decoder_image_generator(SkData*); - // This is the base class for all of the benches in this file. In general // the ETC1 benches should all be working on the same data. Due to the // simplicity of the PKM file, that data is the 128x128 mandrill etc1 @@ -154,10 +151,10 @@ protected: } if (fDecompress) { - SkAutoTDelete<SkImageGenerator> gen(decoder_image_generator(fPKMData)); + SkAutoTDelete<SkImageGenerator> gen(SkImageGenerator::NewFromEncoded(fPKMData)); gen->generateBitmap(&fBitmap); } else { - fImage.reset(SkImage::NewFromGenerator(decoder_image_generator(fPKMData))); + fImage.reset(SkImage::NewFromEncoded(fPKMData)); } } diff --git a/dm/DMSrcSink.cpp b/dm/DMSrcSink.cpp index 971e47fef1..9a62362010 100644 --- a/dm/DMSrcSink.cpp +++ b/dm/DMSrcSink.cpp @@ -40,6 +40,11 @@ DEFINE_bool(multiPage, false, "For document-type backends, render the source" " into multiple pages"); DEFINE_bool(RAW_threading, true, "Allow RAW decodes to run on multiple threads?"); +static bool lazy_decode_bitmap(const void* src, size_t size, SkBitmap* dst) { + SkAutoTUnref<SkData> encoded(SkData::NewWithCopy(src, size)); + return encoded && SkDEPRECATED_InstallDiscardablePixelRef(encoded, dst); +} + namespace DM { GMSrc::GMSrc(skiagm::GMRegistry::Factory factory) : fFactory(factory) {} @@ -799,7 +804,7 @@ Error SKPSrc::draw(SkCanvas* canvas) const { if (!stream) { return SkStringPrintf("Couldn't read %s.", fPath.c_str()); } - SkAutoTUnref<SkPicture> pic(SkPicture::CreateFromStream(stream)); + SkAutoTUnref<SkPicture> pic(SkPicture::CreateFromStream(stream, &lazy_decode_bitmap)); if (!pic) { return SkStringPrintf("Couldn't decode %s as a picture.", fPath.c_str()); } @@ -1172,7 +1177,7 @@ Error ViaSerialization::draw( SkDynamicMemoryWStream wStream; pic->serialize(&wStream); SkAutoTDelete<SkStream> rStream(wStream.detachAsStream()); - SkAutoTUnref<SkPicture> deserialized(SkPicture::CreateFromStream(rStream)); + SkAutoTUnref<SkPicture> deserialized(SkPicture::CreateFromStream(rStream, &lazy_decode_bitmap)); return draw_to_canvas(fSink, bitmap, stream, log, size, [&](SkCanvas* canvas) { canvas->drawPicture(deserialized); diff --git a/gyp/codec.gyp b/gyp/codec.gyp index 760d35c0fd..2043b35567 100644 --- a/gyp/codec.gyp +++ b/gyp/codec.gyp @@ -58,7 +58,6 @@ '../src/codec/SkWebpCodec.cpp', '../src/codec/SkCodecImageGenerator.cpp', - '../src/ports/SkImageGenerator_skia.cpp', ], 'direct_dependent_settings': { 'include_dirs': [ diff --git a/gyp/images.gyp b/gyp/images.gyp index 3760bae468..1c434ae9bf 100644 --- a/gyp/images.gyp +++ b/gyp/images.gyp @@ -77,6 +77,8 @@ '../src/images/SkScaledBitmapSampler.cpp', '../src/images/SkScaledBitmapSampler.h', + '../src/ports/SkImageGenerator_skia.cpp', + '../src/ports/SkImageDecoder_CG.cpp', '../src/ports/SkImageDecoder_WIC.cpp', ], diff --git a/gyp/tools.gyp b/gyp/tools.gyp index de15413324..e428076bb9 100644 --- a/gyp/tools.gyp +++ b/gyp/tools.gyp @@ -82,6 +82,7 @@ ], 'dependencies': [ 'flags.gyp:flags', + 'lazy_decode_bitmap', 'skia_lib.gyp:skia_lib', ], }, @@ -265,6 +266,20 @@ ], }, { + 'target_name': 'lazy_decode_bitmap', + 'type': 'static_library', + 'sources': [ '../tools/LazyDecodeBitmap.cpp' ], + 'include_dirs': [ + '../include/private', + '../src/core', + '../src/lazy', + ], + 'dependencies': [ + 'flags.gyp:flags', + 'skia_lib.gyp:skia_lib' + ], + }, + { 'target_name': 'gpuveto', 'type': 'executable', 'sources': [ @@ -276,6 +291,7 @@ '../src/images', ], 'dependencies': [ + 'lazy_decode_bitmap', 'flags.gyp:flags', 'skia_lib.gyp:skia_lib', ], @@ -317,6 +333,7 @@ '../src/core/', ], 'dependencies': [ + 'lazy_decode_bitmap', 'effects.gyp:effects', 'flags.gyp:flags', 'images.gyp:images', @@ -350,6 +367,7 @@ '../tools/pinspect.cpp', ], 'dependencies': [ + 'lazy_decode_bitmap', 'flags.gyp:flags', 'skia_lib.gyp:skia_lib', ], diff --git a/include/core/SkPicture.h b/include/core/SkPicture.h index 7c8c187fd8..720747e9a3 100644 --- a/include/core/SkPicture.h +++ b/include/core/SkPicture.h @@ -54,19 +54,8 @@ public: * @return A new SkPicture representing the serialized data, or NULL if the stream is * invalid. */ - static SkPicture* CreateFromStream(SkStream*, InstallPixelRefProc proc); - - /** - * Recreate a picture that was serialized into a stream. - * - * Any serialized images in the stream will be passed to - * SkImageGenerator::NewFromEncoded. - * - * @param SkStream Serialized picture data. Ownership is unchanged by this call. - * @return A new SkPicture representing the serialized data, or NULL if the stream is - * invalid. - */ - static SkPicture* CreateFromStream(SkStream*); + static SkPicture* CreateFromStream(SkStream*, + InstallPixelRefProc proc = &SkImageDecoder::DecodeMemory); /** * Recreate a picture that was serialized into a buffer. If the creation requires bitmap diff --git a/public.bzl b/public.bzl index 72f3332ac5..8bf328dd4c 100644 --- a/public.bzl +++ b/public.bzl @@ -380,6 +380,8 @@ DM_SRCS_ALL = struct( "tests/*.h", "tools/CrashHandler.cpp", "tools/CrashHandler.h", + "tools/LazyDecodeBitmap.cpp", + "tools/LazyDecodeBitmap.h", "tools/ProcStats.cpp", "tools/ProcStats.h", "tools/Resources.cpp", diff --git a/src/core/SkPicture.cpp b/src/core/SkPicture.cpp index 04f2f93ba7..8247336d49 100644 --- a/src/core/SkPicture.cpp +++ b/src/core/SkPicture.cpp @@ -6,7 +6,6 @@ */ #include "SkAtomics.h" -#include "SkImageGenerator.h" #include "SkMessageBus.h" #include "SkPicture.h" #include "SkPictureData.h" @@ -139,16 +138,6 @@ SkPicture* SkPicture::Forwardport(const SkPictInfo& info, const SkPictureData* d return r.endRecording(); } -static bool default_install(const void* src, size_t length, SkBitmap* dst) { - SkAutoTUnref<SkData> encoded(SkData::NewWithCopy(src, length)); - return encoded && SkDEPRECATED_InstallDiscardablePixelRef( - SkImageGenerator::NewFromEncoded(encoded), dst); -} - -SkPicture* SkPicture::CreateFromStream(SkStream* stream) { - return CreateFromStream(stream, &default_install, nullptr); -} - SkPicture* SkPicture::CreateFromStream(SkStream* stream, InstallPixelRefProc proc) { return CreateFromStream(stream, proc, nullptr); } diff --git a/src/images/SkImageDecoder_ktx.cpp b/src/images/SkImageDecoder_ktx.cpp index 019fa97678..a95ab6f602 100644 --- a/src/images/SkImageDecoder_ktx.cpp +++ b/src/images/SkImageDecoder_ktx.cpp @@ -7,7 +7,6 @@ #include "SkColorPriv.h" #include "SkImageDecoder.h" -#include "SkImageGenerator.h" #include "SkPixelRef.h" #include "SkScaledBitmapSampler.h" #include "SkStream.h" @@ -329,101 +328,3 @@ SkImageEncoder* sk_libktx_efactory(SkImageEncoder::Type t) { static SkImageDecoder_DecodeReg gReg(sk_libktx_dfactory); static SkImageDecoder_FormatReg gFormatReg(get_format_ktx); static SkImageEncoder_EncodeReg gEReg(sk_libktx_efactory); - -///////////////////////////////////////////////////////////////////////////////////////// -// Old implementation of SkImageGenerator::NewFromEncoded which uses SkImageDecoder. -// Here because it is only needed by DM and tests for Ktx. -class BareMemoryAllocator : public SkBitmap::Allocator { - const SkImageInfo fInfo; - void* const fMemory; - const size_t fRowBytes; - -public: - BareMemoryAllocator(const SkImageInfo& info, void* memory, size_t rowBytes) - : fInfo(info), fMemory(memory), fRowBytes(rowBytes) - {} - -protected: - bool allocPixelRef(SkBitmap* bm, SkColorTable* ctable) override { - const SkImageInfo bmi = bm->info(); - if (bmi.width() != fInfo.width() || bmi.height() != fInfo.height() || - bmi.colorType() != fInfo.colorType()) - { - return false; - } - return bm->installPixels(bmi, fMemory, fRowBytes, ctable, nullptr, nullptr); - } -}; - -class SkImageDecoderGenerator : public SkImageGenerator { - const SkImageInfo fInfo; - SkAutoTDelete<SkImageDecoder> fDecoder; - SkAutoTUnref<SkData> fData; - -public: - SkImageDecoderGenerator(const SkImageInfo& info, SkImageDecoder* decoder, SkData* data) - : INHERITED(info), fInfo(info), fDecoder(decoder), fData(SkRef(data)) - {} - -protected: - SkData* onRefEncodedData(SK_REFENCODEDDATA_CTXPARAM) override { - return SkRef(fData.get()); - } - bool onGetPixels(const SkImageInfo& info, void* pixels, size_t rowBytes, - SkPMColor ctableEntries[], int* ctableCount) override { - SkMemoryStream stream(fData->data(), fData->size(), false); - SkAutoTUnref<BareMemoryAllocator> allocator( - new BareMemoryAllocator(info, pixels, rowBytes)); - fDecoder->setAllocator(allocator); - fDecoder->setRequireUnpremultipliedColors(kUnpremul_SkAlphaType == info.alphaType()); - - SkBitmap bm; - const SkImageDecoder::Result result = fDecoder->decode(&stream, &bm, info.colorType(), - SkImageDecoder::kDecodePixels_Mode); - if (SkImageDecoder::kFailure == result) { - return false; - } - - SkASSERT(info.colorType() == bm.info().colorType()); - - if (kIndex_8_SkColorType == info.colorType()) { - SkASSERT(ctableEntries); - - SkColorTable* ctable = bm.getColorTable(); - if (nullptr == ctable) { - return false; - } - const int count = ctable->count(); - memcpy(ctableEntries, ctable->readColors(), count * sizeof(SkPMColor)); - *ctableCount = count; - } - return true; - } - - bool onGetYUV8Planes(SkISize sizes[3], void* planes[3], size_t rowBytes[3], - SkYUVColorSpace* colorSpace) override { - SkMemoryStream stream(fData->data(), fData->size(), false); - return fDecoder->decodeYUV8Planes(&stream, sizes, planes, rowBytes, colorSpace); - } - -private: - typedef SkImageGenerator INHERITED; -}; - -SkImageGenerator* decoder_image_generator(SkData* data) { - SkMemoryStream stream(data->data(), data->size(), false); - SkImageDecoder* decoder = SkImageDecoder::Factory(&stream); - if (nullptr == decoder) { - return nullptr; - } - - SkBitmap bm; - stream.rewind(); - if (!decoder->decode(&stream, &bm, kUnknown_SkColorType, SkImageDecoder::kDecodeBounds_Mode)) { - delete decoder; - return nullptr; - } - - return new SkImageDecoderGenerator(bm.info(), decoder, data); -} - diff --git a/src/ports/SkImageGenerator_skia.cpp b/src/ports/SkImageGenerator_skia.cpp index b6ddee935a..8dbad555dd 100644 --- a/src/ports/SkImageGenerator_skia.cpp +++ b/src/ports/SkImageGenerator_skia.cpp @@ -6,8 +6,100 @@ */ #include "SkData.h" -#include "SkCodecImageGenerator.h" +#include "SkImageDecoder.h" +#include "SkImageGenerator.h" +#include "SkStream.h" + +class BareMemoryAllocator : public SkBitmap::Allocator { + const SkImageInfo fInfo; + void* const fMemory; + const size_t fRowBytes; + +public: + BareMemoryAllocator(const SkImageInfo& info, void* memory, size_t rowBytes) + : fInfo(info), fMemory(memory), fRowBytes(rowBytes) + {} + +protected: + bool allocPixelRef(SkBitmap* bm, SkColorTable* ctable) override { + const SkImageInfo bmi = bm->info(); + if (bmi.width() != fInfo.width() || bmi.height() != fInfo.height() || + bmi.colorType() != fInfo.colorType()) + { + return false; + } + return bm->installPixels(bmi, fMemory, fRowBytes, ctable, nullptr, nullptr); + } +}; + +class SkImageDecoderGenerator : public SkImageGenerator { + const SkImageInfo fInfo; + SkAutoTDelete<SkImageDecoder> fDecoder; + SkAutoTUnref<SkData> fData; + +public: + SkImageDecoderGenerator(const SkImageInfo& info, SkImageDecoder* decoder, SkData* data) + : INHERITED(info), fInfo(info), fDecoder(decoder), fData(SkRef(data)) + {} + +protected: + SkData* onRefEncodedData(SK_REFENCODEDDATA_CTXPARAM) override { + return SkRef(fData.get()); + } + bool onGetPixels(const SkImageInfo& info, void* pixels, size_t rowBytes, + SkPMColor ctableEntries[], int* ctableCount) override { + SkMemoryStream stream(fData->data(), fData->size(), false); + SkAutoTUnref<BareMemoryAllocator> allocator( + new BareMemoryAllocator(info, pixels, rowBytes)); + fDecoder->setAllocator(allocator); + fDecoder->setRequireUnpremultipliedColors(kUnpremul_SkAlphaType == info.alphaType()); + + SkBitmap bm; + const SkImageDecoder::Result result = fDecoder->decode(&stream, &bm, info.colorType(), + SkImageDecoder::kDecodePixels_Mode); + if (SkImageDecoder::kFailure == result) { + return false; + } + + SkASSERT(info.colorType() == bm.info().colorType()); + + if (kIndex_8_SkColorType == info.colorType()) { + SkASSERT(ctableEntries); + + SkColorTable* ctable = bm.getColorTable(); + if (nullptr == ctable) { + return false; + } + const int count = ctable->count(); + memcpy(ctableEntries, ctable->readColors(), count * sizeof(SkPMColor)); + *ctableCount = count; + } + return true; + } + + bool onGetYUV8Planes(SkISize sizes[3], void* planes[3], size_t rowBytes[3], + SkYUVColorSpace* colorSpace) override { + SkMemoryStream stream(fData->data(), fData->size(), false); + return fDecoder->decodeYUV8Planes(&stream, sizes, planes, rowBytes, colorSpace); + } + +private: + typedef SkImageGenerator INHERITED; +}; SkImageGenerator* SkImageGenerator::NewFromEncodedImpl(SkData* data) { - return SkCodecImageGenerator::NewFromEncodedCodec(data); + SkMemoryStream stream(data->data(), data->size(), false); + SkImageDecoder* decoder = SkImageDecoder::Factory(&stream); + if (nullptr == decoder) { + return nullptr; + } + + SkBitmap bm; + stream.rewind(); + if (!decoder->decode(&stream, &bm, kUnknown_SkColorType, SkImageDecoder::kDecodeBounds_Mode)) { + delete decoder; + return nullptr; + } + + return new SkImageDecoderGenerator(bm.info(), decoder, data); } diff --git a/tests/KtxTest.cpp b/tests/KtxTest.cpp index 99bae83329..84d162c0f8 100644 --- a/tests/KtxTest.cpp +++ b/tests/KtxTest.cpp @@ -143,9 +143,6 @@ DEF_TEST(KtxReadUnpremul, reporter) { } } -// For KtxReexportPKM, below. Defined in SkImageDecoder_ktx.cpp -extern SkImageGenerator* decoder_image_generator(SkData*); - /** * Finally, make sure that if we get ETC1 data from a PKM file that we can then * accurately write it out into a KTX file (i.e. transferring the ETC1 data from @@ -163,7 +160,7 @@ DEF_TEST(KtxReexportPKM, reporter) { } bool installDiscardablePixelRefSuccess = - SkDEPRECATED_InstallDiscardablePixelRef(decoder_image_generator(fileData), &etcBitmap); + SkDEPRECATED_InstallDiscardablePixelRef(fileData, &etcBitmap); if (!installDiscardablePixelRefSuccess) { ERRORF(reporter, "failed to create discardable pixelRef from KTX file"); return; diff --git a/tests/PictureTest.cpp b/tests/PictureTest.cpp index 448e079958..3d05a824a2 100644 --- a/tests/PictureTest.cpp +++ b/tests/PictureTest.cpp @@ -17,7 +17,6 @@ #include "SkImageEncoder.h" #include "SkImageGenerator.h" #include "SkLayerInfo.h" -#include "SkMD5.h" #include "SkPaint.h" #include "SkPicture.h" #include "SkPictureRecorder.h" @@ -872,18 +871,7 @@ static void assert_one_parse_error_cb(SkError error, void* context) { SkGetLastErrorString()); } -static void md5(const SkBitmap& bm, SkMD5::Digest* digest) { - SkAutoLockPixels autoLockPixels(bm); - SkASSERT(bm.getPixels()); - SkMD5 md5; - size_t rowLen = bm.info().bytesPerPixel() * bm.width(); - for (int y = 0; y < bm.height(); ++y) { - md5.update(static_cast<uint8_t*>(bm.getAddr(0, y)), rowLen); - } - md5.finish(*digest); -} - -DEF_TEST(Picture_EncodedData, reporter) { +static void test_bitmap_with_encoded_data(skiatest::Reporter* reporter) { // Create a bitmap that will be encoded. SkBitmap original; make_bm(&original, 100, 100, SK_ColorBLUE, true); @@ -903,7 +891,6 @@ DEF_TEST(Picture_EncodedData, reporter) { SkAutoDataUnref picture1(serialized_picture_from_bitmap(original)); SkAutoDataUnref picture2(serialized_picture_from_bitmap(bm)); REPORTER_ASSERT(reporter, picture1->equals(picture2)); - // Now test that a parse error was generated when trying to create a new SkPicture without // providing a function to decode the bitmap. ErrorContext context; @@ -916,24 +903,6 @@ DEF_TEST(Picture_EncodedData, reporter) { REPORTER_ASSERT(reporter, pictureFromStream.get() != nullptr); SkClearLastError(); SkSetErrorCallback(nullptr, nullptr); - - // Test that using the version of CreateFromStream that just takes a stream also decodes the - // bitmap. Drawing this picture should look exactly like the original bitmap. - SkMD5::Digest referenceDigest; - md5(original, &referenceDigest); - - SkBitmap dst; - dst.allocPixels(original.info()); - dst.eraseColor(SK_ColorRED); - SkCanvas canvas(dst); - - pictureStream.rewind(); - pictureFromStream.reset(SkPicture::CreateFromStream(&pictureStream)); - canvas.drawPicture(pictureFromStream.get()); - - SkMD5::Digest digest2; - md5(dst, &digest2); - REPORTER_ASSERT(reporter, referenceDigest == digest2); } static void test_clip_bound_opt(skiatest::Reporter* reporter) { @@ -1205,6 +1174,7 @@ DEF_TEST(Picture, reporter) { test_has_text(reporter); test_images_are_found_by_willPlayBackBitmaps(reporter); test_analysis(reporter); + test_bitmap_with_encoded_data(reporter); test_clip_bound_opt(reporter); test_clip_expansion(reporter); test_hierarchical(reporter); diff --git a/tools/BUILD.public.expected b/tools/BUILD.public.expected index b1707077c3..de6edf5e7e 100644 --- a/tools/BUILD.public.expected +++ b/tools/BUILD.public.expected @@ -2174,6 +2174,8 @@ DM_SRCS = ['dm/DM.cpp', 'tests/YUVCacheTest.cpp', 'tools/CrashHandler.cpp', 'tools/CrashHandler.h', + 'tools/LazyDecodeBitmap.cpp', + 'tools/LazyDecodeBitmap.h', 'tools/ProcStats.cpp', 'tools/ProcStats.h', 'tools/Resources.cpp', diff --git a/tools/LazyDecodeBitmap.cpp b/tools/LazyDecodeBitmap.cpp new file mode 100644 index 0000000000..d41889b861 --- /dev/null +++ b/tools/LazyDecodeBitmap.cpp @@ -0,0 +1,44 @@ +/* + * 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 "SkData.h" +#include "SkDiscardableMemoryPool.h" +#include "SkImageGeneratorPriv.h" +#include "SkForceLinking.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."); + +// Fits SkPicture::InstallPixelRefProc call signature. +// Used in SkPictureData::CreateFromStream +bool sk_tools::LazyDecodeBitmap(const void* src, size_t length, SkBitmap* dst) { + SkAutoDataUnref data(SkData::NewWithCopy(src, length)); + if (nullptr == data.get()) { + return false; + } + + SkAutoTDelete<SkImageGenerator> gen(SkImageGenerator::NewFromEncoded(data)); + if (nullptr == gen.get()) { + return false; + } + const SkImageInfo info = gen->getInfo(); + SkDiscardableMemory::Factory* pool = nullptr; + if ((!FLAGS_useVolatileCache) || (info.width() * info.height() < 32 * 1024)) { + // how to do switching with SkDiscardableMemory. + pool = SkGetGlobalDiscardableMemoryPool(); + // Only meaningful if platform has a default discardable + // memory implementation that differs from the global DM pool. + } + return SkDEPRECATED_InstallDiscardablePixelRef(gen.detach(), nullptr, dst, pool); +} diff --git a/tools/LazyDecodeBitmap.h b/tools/LazyDecodeBitmap.h new file mode 100644 index 0000000000..ecceb94f7e --- /dev/null +++ b/tools/LazyDecodeBitmap.h @@ -0,0 +1,24 @@ +/* + * Copyright 2013 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#ifndef LazyDecodeBitmap_DEFINED +#define LazyDecodeBitmap_DEFINED + +#include "SkTypes.h" + +class SkBitmap; + +namespace sk_tools { + +/** + * Decode the image with DecodeMemoryToTarget but defer the process until it is needed. + */ +bool LazyDecodeBitmap(const void* buffer, size_t size, SkBitmap* bitmap); + +} + +#endif // LazyDecodeBitmap_DEFINED diff --git a/tools/dump_record.cpp b/tools/dump_record.cpp index 96b893735b..029638efb1 100644 --- a/tools/dump_record.cpp +++ b/tools/dump_record.cpp @@ -6,6 +6,7 @@ */ #include "DumpRecord.h" +#include "LazyDecodeBitmap.h" #include "SkCommandLineFlags.h" #include "SkPicture.h" #include "SkPictureRecorder.h" @@ -49,7 +50,8 @@ int tool_main(int argc, char** argv) { SkDebugf("Could not read %s.\n", FLAGS_skps[i]); return 1; } - SkAutoTUnref<SkPicture> src(SkPicture::CreateFromStream(stream)); + SkAutoTUnref<SkPicture> src( + SkPicture::CreateFromStream(stream, sk_tools::LazyDecodeBitmap)); if (!src) { SkDebugf("Could not read %s as an SkPicture.\n", FLAGS_skps[i]); return 1; diff --git a/tools/gpuveto.cpp b/tools/gpuveto.cpp index f2e103e1e3..204f44965b 100644 --- a/tools/gpuveto.cpp +++ b/tools/gpuveto.cpp @@ -5,6 +5,7 @@ * found in the LICENSE file. */ +#include "LazyDecodeBitmap.h" #include "SkCommandLineFlags.h" #include "SkPicture.h" #include "SkPictureRecorder.h" @@ -41,7 +42,9 @@ int tool_main(int argc, char** argv) { return kError; } - SkAutoTUnref<SkPicture> picture(SkPicture::CreateFromStream(&inputStream)); + SkPicture::InstallPixelRefProc proc = &sk_tools::LazyDecodeBitmap; + + SkAutoTUnref<SkPicture> picture(SkPicture::CreateFromStream(&inputStream, proc)); if (nullptr == picture.get()) { if (!FLAGS_quiet) { SkDebugf("Could not read the SkPicture\n"); diff --git a/tools/lua/lua_pictures.cpp b/tools/lua/lua_pictures.cpp index 57b0dcce8e..6fa5813d77 100644 --- a/tools/lua/lua_pictures.cpp +++ b/tools/lua/lua_pictures.cpp @@ -5,6 +5,7 @@ * found in the LICENSE file. */ +#include "LazyDecodeBitmap.h" #include "SkLua.h" #include "SkLuaCanvas.h" #include "SkPicture.h" @@ -43,7 +44,7 @@ static SkPicture* load_picture(const char path[]) { SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(path)); SkPicture* pic = nullptr; if (stream.get()) { - pic = SkPicture::CreateFromStream(stream.get()); + pic = SkPicture::CreateFromStream(stream.get(), &sk_tools::LazyDecodeBitmap); } return pic; } diff --git a/tools/pinspect.cpp b/tools/pinspect.cpp index b8deba707a..5c04608c4e 100644 --- a/tools/pinspect.cpp +++ b/tools/pinspect.cpp @@ -5,6 +5,7 @@ * found in the LICENSE file. */ +#include "LazyDecodeBitmap.h" #include "SkBitmap.h" #include "SkCanvas.h" #include "SkGraphics.h" @@ -34,7 +35,7 @@ static SkPicture* inspect(const char path[]) { } stream.rewind(); - SkPicture* pic = SkPicture::CreateFromStream(&stream); + SkPicture* pic = SkPicture::CreateFromStream(&stream, &sk_tools::LazyDecodeBitmap); if (nullptr == pic) { SkDebugf("Could not create SkPicture: %s\n", path); return nullptr; |