1 /* 2 * Copyright 2014 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 "SkData.h" 9 #include "SkGraphics.h" 10 #include "SkImageGenerator.h" 11 #include "Test.h" 12 13 static bool gMyFactoryWasCalled; 14 15 static std::unique_ptr<SkImageGenerator> my_factory(sk_sp<SkData>) { 16 gMyFactoryWasCalled = true; 17 return nullptr; 18 } 19 20 static void test_imagegenerator_factory(skiatest::Reporter* reporter) { 21 // just need a non-empty data to test things 22 sk_sp<SkData> data(SkData::MakeWithCString("test_imagegenerator_factory")); 23 24 gMyFactoryWasCalled = false; 25 26 REPORTER_ASSERT(reporter, !gMyFactoryWasCalled); 27 28 std::unique_ptr<SkImageGenerator> gen = SkImageGenerator::MakeFromEncoded(data); 29 REPORTER_ASSERT(reporter, nullptr == gen); 30 REPORTER_ASSERT(reporter, !gMyFactoryWasCalled); 31 32 // Test is racy, in that it hopes no other thread is changing this global... 33 auto prev = SkGraphics::SetImageGeneratorFromEncodedDataFactory(my_factory); 34 gen = SkImageGenerator::MakeFromEncoded(data); 35 REPORTER_ASSERT(reporter, nullptr == gen); 36 REPORTER_ASSERT(reporter, gMyFactoryWasCalled); 37 SkGraphics::SetImageGeneratorFromEncodedDataFactory(prev); 38 } 39 40 class MyImageGenerator : public SkImageGenerator { 41 public: 42 MyImageGenerator() : SkImageGenerator(SkImageInfo::MakeN32Premul(0, 0)) {} 43 }; 44 45 DEF_TEST(ImageGenerator, reporter) { 46 MyImageGenerator ig; 47 SkYUVSizeInfo sizeInfo; 48 sizeInfo.fSizes[SkYUVSizeInfo::kY] = SkISize::Make(200, 200); 49 sizeInfo.fSizes[SkYUVSizeInfo::kU] = SkISize::Make(100, 100); 50 sizeInfo.fSizes[SkYUVSizeInfo::kV] = SkISize::Make( 50, 50); 51 sizeInfo.fWidthBytes[SkYUVSizeInfo::kY] = 0; 52 sizeInfo.fWidthBytes[SkYUVSizeInfo::kU] = 0; 53 sizeInfo.fWidthBytes[SkYUVSizeInfo::kV] = 0; 54 void* planes[3] = { nullptr }; 55 SkYUVColorSpace colorSpace; 56 57 // Check that the YUV decoding API does not cause any crashes 58 ig.queryYUV8(&sizeInfo, nullptr); 59 ig.queryYUV8(&sizeInfo, &colorSpace); 60 sizeInfo.fWidthBytes[SkYUVSizeInfo::kY] = 250; 61 sizeInfo.fWidthBytes[SkYUVSizeInfo::kU] = 250; 62 sizeInfo.fWidthBytes[SkYUVSizeInfo::kV] = 250; 63 int dummy; 64 planes[SkYUVSizeInfo::kY] = planes[SkYUVSizeInfo::kU] = planes[SkYUVSizeInfo::kV] = &dummy; 65 ig.getYUV8Planes(sizeInfo, planes); 66 67 // Suppressed due to https://code.google.com/p/skia/issues/detail?id=4339 68 if (false) { 69 test_imagegenerator_factory(reporter); 70 } 71 } 72