diff options
author | reed <reed@google.com> | 2015-07-09 11:47:36 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-07-09 11:47:36 -0700 |
commit | 1c84634454aa78fb26f23875b86a243aa4596c59 (patch) | |
tree | ced1b33e730a196a7ad6fb9721543af9b62f3565 | |
parent | 3547505278f5f9fe9602ec767c20d461f7a5dab6 (diff) |
add runtime option to provide data->imagegenerator factory
BUG=skia:
Review URL: https://codereview.chromium.org/1229933003
-rw-r--r-- | gm/factory.cpp | 2 | ||||
-rw-r--r-- | gm/mipmap.cpp | 2 | ||||
-rw-r--r-- | include/core/SkGraphics.h | 13 | ||||
-rw-r--r-- | include/core/SkImageGenerator.h | 4 | ||||
-rw-r--r-- | src/core/SkImageGenerator.cpp | 26 | ||||
-rw-r--r-- | src/image/SkImage.cpp | 2 | ||||
-rw-r--r-- | src/lazy/SkDiscardablePixelRef.cpp | 2 | ||||
-rw-r--r-- | src/ports/SkImageGenerator_none.cpp | 2 | ||||
-rw-r--r-- | src/ports/SkImageGenerator_skia.cpp | 6 | ||||
-rw-r--r-- | tests/CachedDecodingPixelRefTest.cpp | 2 | ||||
-rw-r--r-- | tests/ImageGeneratorTest.cpp | 32 | ||||
-rw-r--r-- | tools/LazyDecodeBitmap.cpp | 2 |
12 files changed, 82 insertions, 13 deletions
diff --git a/gm/factory.cpp b/gm/factory.cpp index 4af37636f9..0a938629b9 100644 --- a/gm/factory.cpp +++ b/gm/factory.cpp @@ -36,7 +36,7 @@ protected: // bitmap is unlocked. SkAutoTUnref<SkDiscardableMemoryPool> pool( SkDiscardableMemoryPool::Create(1)); - SkAssertResult(SkInstallDiscardablePixelRef(SkImageGenerator::NewFromData(data), + SkAssertResult(SkInstallDiscardablePixelRef(SkImageGenerator::NewFromEncoded(data), NULL, &fBitmap, pool)); } } diff --git a/gm/mipmap.cpp b/gm/mipmap.cpp index 2d4ee0d26e..ad45a17e86 100644 --- a/gm/mipmap.cpp +++ b/gm/mipmap.cpp @@ -29,7 +29,7 @@ static SkImage* make_image() { } static void test_mip(SkCanvas* canvas) { - SkAutoTUnref<SkImage> img(make_image());//SkImage::NewFromData(data)); + SkAutoTUnref<SkImage> img(make_image());//SkImage::NewFromEncoded(data)); SkPaint paint; const SkRect dst = SkRect::MakeWH(177, 15); diff --git a/include/core/SkGraphics.h b/include/core/SkGraphics.h index ea74c586d2..e552633d5f 100644 --- a/include/core/SkGraphics.h +++ b/include/core/SkGraphics.h @@ -10,6 +10,9 @@ #include "SkTypes.h" +class SkData; +class SkImageGenerator; + class SK_API SkGraphics { public: /** @@ -140,6 +143,16 @@ public: * global font cache. */ static void SetTLSFontCacheLimit(size_t bytes); + + typedef SkImageGenerator* (*ImageGeneratorFromEncodedFactory)(SkData*); + + /** + * To instantiate images from encoded data, first looks at this runtime function-ptr. If it + * exists, it is called to create an SkImageGenerator from SkData. If there is no function-ptr + * or there is, but it returns NULL, then skia will call its internal default implementation. + */ + static ImageGeneratorFromEncodedFactory GetImageGeneratorFromEncodedFactory(); + static void SetImageGeneratorFromEncodedFactory(ImageGeneratorFromEncodedFactory); }; class SkAutoGraphics { diff --git a/include/core/SkImageGenerator.h b/include/core/SkImageGenerator.h index a398697e6b..46001c8537 100644 --- a/include/core/SkImageGenerator.h +++ b/include/core/SkImageGenerator.h @@ -199,7 +199,7 @@ public: * this returns a new ImageGenerator for it. Otherwise this returns NULL. Either way * the caller is still responsible for managing their ownership of the data. */ - static SkImageGenerator* NewFromData(SkData*); + static SkImageGenerator* NewFromEncoded(SkData*); protected: SkImageGenerator(const SkImageInfo& info) : fInfo(info) {} @@ -220,6 +220,8 @@ protected: private: const SkImageInfo fInfo; + + static SkImageGenerator* NewFromEncodedImpl(SkData*); }; #endif // SkImageGenerator_DEFINED diff --git a/src/core/SkImageGenerator.cpp b/src/core/SkImageGenerator.cpp index b4a3fc21c8..7da4d40b0c 100644 --- a/src/core/SkImageGenerator.cpp +++ b/src/core/SkImageGenerator.cpp @@ -130,3 +130,29 @@ bool SkImageGenerator::onGetPixels(const SkImageInfo& info, void* dst, size_t rb return false; } #endif + +/////////////////////////////////////////////////////////////////////////////////////////////////// + +#include "SkGraphics.h" + +static SkGraphics::ImageGeneratorFromEncodedFactory gImageGeneratorFromEncodedFactory; + +SkGraphics::ImageGeneratorFromEncodedFactory SkGraphics::GetImageGeneratorFromEncodedFactory() { + return gImageGeneratorFromEncodedFactory; +} + +void SkGraphics::SetImageGeneratorFromEncodedFactory(ImageGeneratorFromEncodedFactory factory) { + gImageGeneratorFromEncodedFactory = factory; +} + +SkImageGenerator* SkImageGenerator::NewFromEncoded(SkData* data) { + if (NULL == data) { + return NULL; + } + if (gImageGeneratorFromEncodedFactory) { + if (SkImageGenerator* generator = gImageGeneratorFromEncodedFactory(data)) { + return generator; + } + } + return SkImageGenerator::NewFromEncodedImpl(data); +} diff --git a/src/image/SkImage.cpp b/src/image/SkImage.cpp index e8a3ead2b1..ff0b8c9819 100644 --- a/src/image/SkImage.cpp +++ b/src/image/SkImage.cpp @@ -76,7 +76,7 @@ SkImage* SkImage::NewFromEncoded(SkData* encoded, const SkIRect* subset) { if (NULL == encoded || 0 == encoded->size()) { return NULL; } - SkImageGenerator* generator = SkImageGenerator::NewFromData(encoded); + SkImageGenerator* generator = SkImageGenerator::NewFromEncoded(encoded); return generator ? SkImage::NewFromGenerator(generator, subset) : NULL; } diff --git a/src/lazy/SkDiscardablePixelRef.cpp b/src/lazy/SkDiscardablePixelRef.cpp index 19bfc8ccc7..4f9178e6e1 100644 --- a/src/lazy/SkDiscardablePixelRef.cpp +++ b/src/lazy/SkDiscardablePixelRef.cpp @@ -150,6 +150,6 @@ bool SkInstallDiscardablePixelRef(SkImageGenerator* generator, SkBitmap* dst) { } bool SkInstallDiscardablePixelRef(SkData* encoded, SkBitmap* dst) { - SkImageGenerator* generator = SkImageGenerator::NewFromData(encoded); + SkImageGenerator* generator = SkImageGenerator::NewFromEncoded(encoded); return generator ? SkInstallDiscardablePixelRef(generator, NULL, dst, NULL) : false; } diff --git a/src/ports/SkImageGenerator_none.cpp b/src/ports/SkImageGenerator_none.cpp index 78d408aa35..c0126cae79 100644 --- a/src/ports/SkImageGenerator_none.cpp +++ b/src/ports/SkImageGenerator_none.cpp @@ -7,6 +7,6 @@ #include "SkImageGenerator.h" -SkImageGenerator* SkImageGenerator::NewFromData(SkData*) { +SkImageGenerator* SkImageGenerator::NewFromEncodedImpl(SkData*) { return NULL; } diff --git a/src/ports/SkImageGenerator_skia.cpp b/src/ports/SkImageGenerator_skia.cpp index f972253306..1dbc9a5f33 100644 --- a/src/ports/SkImageGenerator_skia.cpp +++ b/src/ports/SkImageGenerator_skia.cpp @@ -108,11 +108,7 @@ private: typedef SkImageGenerator INHERITED; }; -SkImageGenerator* SkImageGenerator::NewFromData(SkData* data) { - if (NULL == data) { - return NULL; - } - +SkImageGenerator* SkImageGenerator::NewFromEncodedImpl(SkData* data) { SkMemoryStream stream(data->data(), data->size(), false); SkImageDecoder* decoder = SkImageDecoder::Factory(&stream); if (NULL == decoder) { diff --git a/tests/CachedDecodingPixelRefTest.cpp b/tests/CachedDecodingPixelRefTest.cpp index a2032ba1de..78adc34d5a 100644 --- a/tests/CachedDecodingPixelRefTest.cpp +++ b/tests/CachedDecodingPixelRefTest.cpp @@ -142,7 +142,7 @@ static void test_three_encodings(skiatest::Reporter* reporter, //////////////////////////////////////////////////////////////////////////////// static bool install_skCachingPixelRef(SkData* encoded, SkBitmap* dst) { - return SkCachingPixelRef::Install(SkImageGenerator::NewFromData(encoded), dst); + return SkCachingPixelRef::Install(SkImageGenerator::NewFromEncoded(encoded), dst); } static bool install_skDiscardablePixelRef(SkData* encoded, SkBitmap* dst) { // Use system-default discardable memory. diff --git a/tests/ImageGeneratorTest.cpp b/tests/ImageGeneratorTest.cpp index 94867bc945..c856d8be60 100644 --- a/tests/ImageGeneratorTest.cpp +++ b/tests/ImageGeneratorTest.cpp @@ -5,9 +5,39 @@ * found in the LICENSE file. */ +#include "SkData.h" +#include "SkGraphics.h" #include "SkImageGenerator.h" #include "Test.h" +static SkImageGenerator* my_factory(SkData* data) { + int* ptr = *(int**)data->data(); + *ptr = 1; // signal that we were called + return NULL; +} + +static void test_imagegenerator_factory(skiatest::Reporter* reporter) { + int factoryHasBeenCalled = 0; + int* sentinelPtr = &factoryHasBeenCalled; + SkData* data = SkData::NewWithCopy(&sentinelPtr, sizeof(sentinelPtr)); + + SkImageGenerator* gen; + REPORTER_ASSERT(reporter, 0 == *sentinelPtr); + + gen = SkImageGenerator::NewFromEncoded(data); + REPORTER_ASSERT(reporter, NULL == gen); + REPORTER_ASSERT(reporter, 0 == *sentinelPtr); + + // Test is racy, in that it hopes no other thread is changing this global... + SkGraphics::ImageGeneratorFromEncodedFactory prev = + SkGraphics::GetImageGeneratorFromEncodedFactory(); + SkGraphics::SetImageGeneratorFromEncodedFactory(my_factory); + gen = SkImageGenerator::NewFromEncoded(data); + REPORTER_ASSERT(reporter, NULL == gen); + REPORTER_ASSERT(reporter, 1 == *sentinelPtr); + SkGraphics::SetImageGeneratorFromEncodedFactory(prev); +} + class MyImageGenerator : public SkImageGenerator { public: MyImageGenerator() : SkImageGenerator(SkImageInfo::MakeN32Premul(0, 0)) {} @@ -36,4 +66,6 @@ DEF_TEST(ImageGenerator, reporter) { rowBytes[0] = rowBytes[1] = rowBytes[2] = 250; ig.getYUV8Planes(sizes, planes, rowBytes, &colorSpace); + + test_imagegenerator_factory(reporter); } diff --git a/tools/LazyDecodeBitmap.cpp b/tools/LazyDecodeBitmap.cpp index 6c655da9e9..bcd8cad44d 100644 --- a/tools/LazyDecodeBitmap.cpp +++ b/tools/LazyDecodeBitmap.cpp @@ -28,7 +28,7 @@ bool sk_tools::LazyDecodeBitmap(const void* src, size_t length, SkBitmap* dst) { return false; } - SkAutoTDelete<SkImageGenerator> gen(SkImageGenerator::NewFromData(data)); + SkAutoTDelete<SkImageGenerator> gen(SkImageGenerator::NewFromEncoded(data)); if (NULL == gen.get()) { return false; } |