sugoi | 518d83d | 2014-07-21 11:37:39 -0700 | [diff] [blame] | 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 | |
reed | 1c84634 | 2015-07-09 11:47:36 -0700 | [diff] [blame] | 8 | #include "SkData.h" |
| 9 | #include "SkGraphics.h" |
sugoi | 518d83d | 2014-07-21 11:37:39 -0700 | [diff] [blame] | 10 | #include "SkImageGenerator.h" |
| 11 | #include "Test.h" |
| 12 | |
reed | d7c05bf | 2015-07-09 14:08:49 -0700 | [diff] [blame] | 13 | static bool gMyFactoryWasCalled; |
| 14 | |
| 15 | static SkImageGenerator* my_factory(SkData*) { |
| 16 | gMyFactoryWasCalled = true; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 17 | return nullptr; |
reed | 1c84634 | 2015-07-09 11:47:36 -0700 | [diff] [blame] | 18 | } |
| 19 | |
| 20 | static void test_imagegenerator_factory(skiatest::Reporter* reporter) { |
reed | d7c05bf | 2015-07-09 14:08:49 -0700 | [diff] [blame] | 21 | // just need a non-empty data to test things |
mtklein | 694307c | 2015-07-09 15:41:11 -0700 | [diff] [blame] | 22 | SkAutoTUnref<SkData> data(SkData::NewWithCString("test_imagegenerator_factory")); |
reed | d7c05bf | 2015-07-09 14:08:49 -0700 | [diff] [blame] | 23 | |
| 24 | gMyFactoryWasCalled = false; |
reed | 1c84634 | 2015-07-09 11:47:36 -0700 | [diff] [blame] | 25 | |
| 26 | SkImageGenerator* gen; |
reed | d7c05bf | 2015-07-09 14:08:49 -0700 | [diff] [blame] | 27 | REPORTER_ASSERT(reporter, !gMyFactoryWasCalled); |
reed | 1c84634 | 2015-07-09 11:47:36 -0700 | [diff] [blame] | 28 | |
| 29 | gen = SkImageGenerator::NewFromEncoded(data); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 30 | REPORTER_ASSERT(reporter, nullptr == gen); |
reed | d7c05bf | 2015-07-09 14:08:49 -0700 | [diff] [blame] | 31 | REPORTER_ASSERT(reporter, !gMyFactoryWasCalled); |
reed | 1c84634 | 2015-07-09 11:47:36 -0700 | [diff] [blame] | 32 | |
| 33 | // Test is racy, in that it hopes no other thread is changing this global... |
| 34 | SkGraphics::ImageGeneratorFromEncodedFactory prev = |
reed | d7c05bf | 2015-07-09 14:08:49 -0700 | [diff] [blame] | 35 | SkGraphics::SetImageGeneratorFromEncodedFactory(my_factory); |
reed | 1c84634 | 2015-07-09 11:47:36 -0700 | [diff] [blame] | 36 | gen = SkImageGenerator::NewFromEncoded(data); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 37 | REPORTER_ASSERT(reporter, nullptr == gen); |
reed | d7c05bf | 2015-07-09 14:08:49 -0700 | [diff] [blame] | 38 | REPORTER_ASSERT(reporter, gMyFactoryWasCalled); |
reed | 1c84634 | 2015-07-09 11:47:36 -0700 | [diff] [blame] | 39 | SkGraphics::SetImageGeneratorFromEncodedFactory(prev); |
| 40 | } |
| 41 | |
reed | 3ef71e3 | 2015-03-19 08:31:14 -0700 | [diff] [blame] | 42 | class MyImageGenerator : public SkImageGenerator { |
| 43 | public: |
| 44 | MyImageGenerator() : SkImageGenerator(SkImageInfo::MakeN32Premul(0, 0)) {} |
| 45 | }; |
| 46 | |
sugoi | 518d83d | 2014-07-21 11:37:39 -0700 | [diff] [blame] | 47 | DEF_TEST(ImageGenerator, reporter) { |
reed | 3ef71e3 | 2015-03-19 08:31:14 -0700 | [diff] [blame] | 48 | MyImageGenerator ig; |
sugoi | 518d83d | 2014-07-21 11:37:39 -0700 | [diff] [blame] | 49 | SkISize sizes[3]; |
| 50 | sizes[0] = SkISize::Make(200, 200); |
| 51 | sizes[1] = SkISize::Make(100, 100); |
| 52 | sizes[2] = SkISize::Make( 50, 50); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 53 | void* planes[3] = { nullptr }; |
sugoi | 518d83d | 2014-07-21 11:37:39 -0700 | [diff] [blame] | 54 | size_t rowBytes[3] = { 0 }; |
rileya | abaef86 | 2014-09-12 17:45:58 -0700 | [diff] [blame] | 55 | SkYUVColorSpace colorSpace; |
sugoi | 518d83d | 2014-07-21 11:37:39 -0700 | [diff] [blame] | 56 | |
| 57 | // Check that the YUV decoding API does not cause any crashes |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 58 | ig.getYUV8Planes(sizes, nullptr, nullptr, &colorSpace); |
| 59 | ig.getYUV8Planes(sizes, nullptr, nullptr, nullptr); |
| 60 | ig.getYUV8Planes(sizes, planes, nullptr, nullptr); |
| 61 | ig.getYUV8Planes(sizes, nullptr, rowBytes, nullptr); |
| 62 | ig.getYUV8Planes(sizes, planes, rowBytes, nullptr); |
rileya | abaef86 | 2014-09-12 17:45:58 -0700 | [diff] [blame] | 63 | ig.getYUV8Planes(sizes, planes, rowBytes, &colorSpace); |
sugoi | 518d83d | 2014-07-21 11:37:39 -0700 | [diff] [blame] | 64 | |
| 65 | int dummy; |
| 66 | planes[0] = planes[1] = planes[2] = &dummy; |
| 67 | rowBytes[0] = rowBytes[1] = rowBytes[2] = 250; |
| 68 | |
rileya | abaef86 | 2014-09-12 17:45:58 -0700 | [diff] [blame] | 69 | ig.getYUV8Planes(sizes, planes, rowBytes, &colorSpace); |
reed | 1c84634 | 2015-07-09 11:47:36 -0700 | [diff] [blame] | 70 | |
fmalita | 5dd918b | 2015-09-14 14:51:04 -0700 | [diff] [blame] | 71 | // Suppressed due to https://code.google.com/p/skia/issues/detail?id=4339 |
| 72 | if (false) { |
| 73 | test_imagegenerator_factory(reporter); |
| 74 | } |
sugoi | 518d83d | 2014-07-21 11:37:39 -0700 | [diff] [blame] | 75 | } |