1518d83dbc1c899e316e8c896af5defb58b83120fsugoi/*
2518d83dbc1c899e316e8c896af5defb58b83120fsugoi * Copyright 2014 Google Inc.
3518d83dbc1c899e316e8c896af5defb58b83120fsugoi *
4518d83dbc1c899e316e8c896af5defb58b83120fsugoi * Use of this source code is governed by a BSD-style license that can be
5518d83dbc1c899e316e8c896af5defb58b83120fsugoi * found in the LICENSE file.
6518d83dbc1c899e316e8c896af5defb58b83120fsugoi */
7518d83dbc1c899e316e8c896af5defb58b83120fsugoi
81c84634454aa78fb26f23875b86a243aa4596c59reed#include "SkData.h"
91c84634454aa78fb26f23875b86a243aa4596c59reed#include "SkGraphics.h"
10518d83dbc1c899e316e8c896af5defb58b83120fsugoi#include "SkImageGenerator.h"
11518d83dbc1c899e316e8c896af5defb58b83120fsugoi#include "Test.h"
12518d83dbc1c899e316e8c896af5defb58b83120fsugoi
13d7c05bfd7c81a835a78b4163026a4dfa573faa89reedstatic bool gMyFactoryWasCalled;
14d7c05bfd7c81a835a78b4163026a4dfa573faa89reed
15d7c05bfd7c81a835a78b4163026a4dfa573faa89reedstatic SkImageGenerator* my_factory(SkData*) {
16d7c05bfd7c81a835a78b4163026a4dfa573faa89reed    gMyFactoryWasCalled = true;
172880df2609eba09b555ca37be04b6ad89290c765Tom Hudson    return nullptr;
181c84634454aa78fb26f23875b86a243aa4596c59reed}
191c84634454aa78fb26f23875b86a243aa4596c59reed
201c84634454aa78fb26f23875b86a243aa4596c59reedstatic void test_imagegenerator_factory(skiatest::Reporter* reporter) {
21d7c05bfd7c81a835a78b4163026a4dfa573faa89reed    // just need a non-empty data to test things
22694307c9f1ade20a3c5f968c248a66b6a2d151a8mtklein    SkAutoTUnref<SkData> data(SkData::NewWithCString("test_imagegenerator_factory"));
23d7c05bfd7c81a835a78b4163026a4dfa573faa89reed
24d7c05bfd7c81a835a78b4163026a4dfa573faa89reed    gMyFactoryWasCalled = false;
251c84634454aa78fb26f23875b86a243aa4596c59reed
261c84634454aa78fb26f23875b86a243aa4596c59reed    SkImageGenerator* gen;
27d7c05bfd7c81a835a78b4163026a4dfa573faa89reed    REPORTER_ASSERT(reporter, !gMyFactoryWasCalled);
281c84634454aa78fb26f23875b86a243aa4596c59reed
291c84634454aa78fb26f23875b86a243aa4596c59reed    gen = SkImageGenerator::NewFromEncoded(data);
302880df2609eba09b555ca37be04b6ad89290c765Tom Hudson    REPORTER_ASSERT(reporter, nullptr == gen);
31d7c05bfd7c81a835a78b4163026a4dfa573faa89reed    REPORTER_ASSERT(reporter, !gMyFactoryWasCalled);
321c84634454aa78fb26f23875b86a243aa4596c59reed
331c84634454aa78fb26f23875b86a243aa4596c59reed    // Test is racy, in that it hopes no other thread is changing this global...
341c84634454aa78fb26f23875b86a243aa4596c59reed    SkGraphics::ImageGeneratorFromEncodedFactory prev =
35d7c05bfd7c81a835a78b4163026a4dfa573faa89reed                                    SkGraphics::SetImageGeneratorFromEncodedFactory(my_factory);
361c84634454aa78fb26f23875b86a243aa4596c59reed    gen = SkImageGenerator::NewFromEncoded(data);
372880df2609eba09b555ca37be04b6ad89290c765Tom Hudson    REPORTER_ASSERT(reporter, nullptr == gen);
38d7c05bfd7c81a835a78b4163026a4dfa573faa89reed    REPORTER_ASSERT(reporter, gMyFactoryWasCalled);
391c84634454aa78fb26f23875b86a243aa4596c59reed    SkGraphics::SetImageGeneratorFromEncodedFactory(prev);
401c84634454aa78fb26f23875b86a243aa4596c59reed}
411c84634454aa78fb26f23875b86a243aa4596c59reed
423ef71e343bf075888fb50892350390b4dd47de24reedclass MyImageGenerator : public SkImageGenerator {
433ef71e343bf075888fb50892350390b4dd47de24reedpublic:
443ef71e343bf075888fb50892350390b4dd47de24reed    MyImageGenerator() : SkImageGenerator(SkImageInfo::MakeN32Premul(0, 0)) {}
453ef71e343bf075888fb50892350390b4dd47de24reed};
463ef71e343bf075888fb50892350390b4dd47de24reed
47518d83dbc1c899e316e8c896af5defb58b83120fsugoiDEF_TEST(ImageGenerator, reporter) {
483ef71e343bf075888fb50892350390b4dd47de24reed    MyImageGenerator ig;
49518d83dbc1c899e316e8c896af5defb58b83120fsugoi    SkISize sizes[3];
50518d83dbc1c899e316e8c896af5defb58b83120fsugoi    sizes[0] = SkISize::Make(200, 200);
51518d83dbc1c899e316e8c896af5defb58b83120fsugoi    sizes[1] = SkISize::Make(100, 100);
52518d83dbc1c899e316e8c896af5defb58b83120fsugoi    sizes[2] = SkISize::Make( 50,  50);
532880df2609eba09b555ca37be04b6ad89290c765Tom Hudson    void*   planes[3] = { nullptr };
54518d83dbc1c899e316e8c896af5defb58b83120fsugoi    size_t  rowBytes[3] = { 0 };
55abaef86f2b37d8a939506a2076da07f6db456951rileya    SkYUVColorSpace colorSpace;
56518d83dbc1c899e316e8c896af5defb58b83120fsugoi
57518d83dbc1c899e316e8c896af5defb58b83120fsugoi    // Check that the YUV decoding API does not cause any crashes
582880df2609eba09b555ca37be04b6ad89290c765Tom Hudson    ig.getYUV8Planes(sizes, nullptr, nullptr, &colorSpace);
592880df2609eba09b555ca37be04b6ad89290c765Tom Hudson    ig.getYUV8Planes(sizes, nullptr, nullptr, nullptr);
602880df2609eba09b555ca37be04b6ad89290c765Tom Hudson    ig.getYUV8Planes(sizes, planes, nullptr, nullptr);
612880df2609eba09b555ca37be04b6ad89290c765Tom Hudson    ig.getYUV8Planes(sizes, nullptr, rowBytes, nullptr);
622880df2609eba09b555ca37be04b6ad89290c765Tom Hudson    ig.getYUV8Planes(sizes, planes, rowBytes, nullptr);
63abaef86f2b37d8a939506a2076da07f6db456951rileya    ig.getYUV8Planes(sizes, planes, rowBytes, &colorSpace);
64518d83dbc1c899e316e8c896af5defb58b83120fsugoi
65518d83dbc1c899e316e8c896af5defb58b83120fsugoi    int dummy;
66518d83dbc1c899e316e8c896af5defb58b83120fsugoi    planes[0] = planes[1] = planes[2] = &dummy;
67518d83dbc1c899e316e8c896af5defb58b83120fsugoi    rowBytes[0] = rowBytes[1] = rowBytes[2] = 250;
68518d83dbc1c899e316e8c896af5defb58b83120fsugoi
69abaef86f2b37d8a939506a2076da07f6db456951rileya    ig.getYUV8Planes(sizes, planes, rowBytes, &colorSpace);
701c84634454aa78fb26f23875b86a243aa4596c59reed
712880df2609eba09b555ca37be04b6ad89290c765Tom Hudson    // Suppressed due to https://code.google.com/p/skia/issues/detail?id=4339
722880df2609eba09b555ca37be04b6ad89290c765Tom Hudson    if (false) {
732880df2609eba09b555ca37be04b6ad89290c765Tom Hudson        test_imagegenerator_factory(reporter);
742880df2609eba09b555ca37be04b6ad89290c765Tom Hudson    }
75518d83dbc1c899e316e8c896af5defb58b83120fsugoi}
76