blob: 7288ecf0a93416375bdbdf640af26f4be958f3d0 [file] [log] [blame]
sugoi518d83d2014-07-21 11:37:39 -07001/*
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
reed1c846342015-07-09 11:47:36 -07008#include "SkData.h"
9#include "SkGraphics.h"
sugoi518d83d2014-07-21 11:37:39 -070010#include "SkImageGenerator.h"
11#include "Test.h"
12
reedd7c05bf2015-07-09 14:08:49 -070013static bool gMyFactoryWasCalled;
14
15static SkImageGenerator* my_factory(SkData*) {
16 gMyFactoryWasCalled = true;
halcanary96fcdcc2015-08-27 07:41:13 -070017 return nullptr;
reed1c846342015-07-09 11:47:36 -070018}
19
20static void test_imagegenerator_factory(skiatest::Reporter* reporter) {
reedd7c05bf2015-07-09 14:08:49 -070021 // just need a non-empty data to test things
mtklein694307c2015-07-09 15:41:11 -070022 SkAutoTUnref<SkData> data(SkData::NewWithCString("test_imagegenerator_factory"));
reedd7c05bf2015-07-09 14:08:49 -070023
24 gMyFactoryWasCalled = false;
reed1c846342015-07-09 11:47:36 -070025
26 SkImageGenerator* gen;
reedd7c05bf2015-07-09 14:08:49 -070027 REPORTER_ASSERT(reporter, !gMyFactoryWasCalled);
reed1c846342015-07-09 11:47:36 -070028
29 gen = SkImageGenerator::NewFromEncoded(data);
halcanary96fcdcc2015-08-27 07:41:13 -070030 REPORTER_ASSERT(reporter, nullptr == gen);
reedd7c05bf2015-07-09 14:08:49 -070031 REPORTER_ASSERT(reporter, !gMyFactoryWasCalled);
reed1c846342015-07-09 11:47:36 -070032
33 // Test is racy, in that it hopes no other thread is changing this global...
34 SkGraphics::ImageGeneratorFromEncodedFactory prev =
reedd7c05bf2015-07-09 14:08:49 -070035 SkGraphics::SetImageGeneratorFromEncodedFactory(my_factory);
reed1c846342015-07-09 11:47:36 -070036 gen = SkImageGenerator::NewFromEncoded(data);
halcanary96fcdcc2015-08-27 07:41:13 -070037 REPORTER_ASSERT(reporter, nullptr == gen);
reedd7c05bf2015-07-09 14:08:49 -070038 REPORTER_ASSERT(reporter, gMyFactoryWasCalled);
reed1c846342015-07-09 11:47:36 -070039 SkGraphics::SetImageGeneratorFromEncodedFactory(prev);
40}
41
reed3ef71e32015-03-19 08:31:14 -070042class MyImageGenerator : public SkImageGenerator {
43public:
44 MyImageGenerator() : SkImageGenerator(SkImageInfo::MakeN32Premul(0, 0)) {}
45};
46
sugoi518d83d2014-07-21 11:37:39 -070047DEF_TEST(ImageGenerator, reporter) {
reed3ef71e32015-03-19 08:31:14 -070048 MyImageGenerator ig;
sugoi518d83d2014-07-21 11:37:39 -070049 SkISize sizes[3];
50 sizes[0] = SkISize::Make(200, 200);
51 sizes[1] = SkISize::Make(100, 100);
52 sizes[2] = SkISize::Make( 50, 50);
halcanary96fcdcc2015-08-27 07:41:13 -070053 void* planes[3] = { nullptr };
sugoi518d83d2014-07-21 11:37:39 -070054 size_t rowBytes[3] = { 0 };
rileyaabaef862014-09-12 17:45:58 -070055 SkYUVColorSpace colorSpace;
sugoi518d83d2014-07-21 11:37:39 -070056
57 // Check that the YUV decoding API does not cause any crashes
halcanary96fcdcc2015-08-27 07:41:13 -070058 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);
rileyaabaef862014-09-12 17:45:58 -070063 ig.getYUV8Planes(sizes, planes, rowBytes, &colorSpace);
sugoi518d83d2014-07-21 11:37:39 -070064
65 int dummy;
66 planes[0] = planes[1] = planes[2] = &dummy;
67 rowBytes[0] = rowBytes[1] = rowBytes[2] = 250;
68
rileyaabaef862014-09-12 17:45:58 -070069 ig.getYUV8Planes(sizes, planes, rowBytes, &colorSpace);
reed1c846342015-07-09 11:47:36 -070070
fmalita5dd918b2015-09-14 14:51:04 -070071 // Suppressed due to https://code.google.com/p/skia/issues/detail?id=4339
72 if (false) {
73 test_imagegenerator_factory(reporter);
74 }
sugoi518d83d2014-07-21 11:37:39 -070075}