blob: 3d750b2c7327cc909ba78fa5911d7c711a4c4ed5 [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;
msarett4984c3c2016-03-10 05:44:43 -080049 SkYUVSizeInfo sizeInfo;
50 sizeInfo.fSizes[SkYUVSizeInfo::kY] = SkISize::Make(200, 200);
51 sizeInfo.fSizes[SkYUVSizeInfo::kU] = SkISize::Make(100, 100);
52 sizeInfo.fSizes[SkYUVSizeInfo::kV] = SkISize::Make( 50, 50);
53 sizeInfo.fWidthBytes[SkYUVSizeInfo::kY] = 0;
54 sizeInfo.fWidthBytes[SkYUVSizeInfo::kU] = 0;
55 sizeInfo.fWidthBytes[SkYUVSizeInfo::kV] = 0;
56 void* planes[3] = { nullptr };
rileyaabaef862014-09-12 17:45:58 -070057 SkYUVColorSpace colorSpace;
sugoi518d83d2014-07-21 11:37:39 -070058
59 // Check that the YUV decoding API does not cause any crashes
msarett4984c3c2016-03-10 05:44:43 -080060 ig.queryYUV8(&sizeInfo, nullptr);
61 ig.queryYUV8(&sizeInfo, &colorSpace);
62 sizeInfo.fWidthBytes[SkYUVSizeInfo::kY] = 250;
63 sizeInfo.fWidthBytes[SkYUVSizeInfo::kU] = 250;
64 sizeInfo.fWidthBytes[SkYUVSizeInfo::kV] = 250;
sugoi518d83d2014-07-21 11:37:39 -070065 int dummy;
msarett4984c3c2016-03-10 05:44:43 -080066 planes[SkYUVSizeInfo::kY] = planes[SkYUVSizeInfo::kU] = planes[SkYUVSizeInfo::kV] = &dummy;
67 ig.getYUV8Planes(sizeInfo, planes);
reed1c846342015-07-09 11:47:36 -070068
fmalita5dd918b2015-09-14 14:51:04 -070069 // Suppressed due to https://code.google.com/p/skia/issues/detail?id=4339
70 if (false) {
71 test_imagegenerator_factory(reporter);
72 }
sugoi518d83d2014-07-21 11:37:39 -070073}