blob: 1846f5762ebb65cf92839ddae5f9fe635885226b [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;
reed1c846342015-07-09 11:47:36 -070017 return NULL;
18}
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);
30 REPORTER_ASSERT(reporter, NULL == 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);
37 REPORTER_ASSERT(reporter, NULL == 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);
53 void* planes[3] = { NULL };
54 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
rileyaabaef862014-09-12 17:45:58 -070058 ig.getYUV8Planes(sizes, NULL, NULL, &colorSpace);
59 ig.getYUV8Planes(sizes, NULL, NULL, NULL);
60 ig.getYUV8Planes(sizes, planes, NULL, NULL);
61 ig.getYUV8Planes(sizes, NULL, rowBytes, NULL);
62 ig.getYUV8Planes(sizes, planes, rowBytes, NULL);
63 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
71 test_imagegenerator_factory(reporter);
sugoi518d83d2014-07-21 11:37:39 -070072}