blob: 3d8253471764d4e27af18978db579eef3a7817ac [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"
Mike Reedc090c642017-05-16 10:39:06 -04009#include "SkCanvas.h"
reed1c846342015-07-09 11:47:36 -070010#include "SkGraphics.h"
sugoi518d83d2014-07-21 11:37:39 -070011#include "SkImageGenerator.h"
12#include "Test.h"
13
reedd7c05bf2015-07-09 14:08:49 -070014static bool gMyFactoryWasCalled;
15
Mike Reed185130c2017-02-15 15:14:16 -050016static std::unique_ptr<SkImageGenerator> my_factory(sk_sp<SkData>) {
reedd7c05bf2015-07-09 14:08:49 -070017 gMyFactoryWasCalled = true;
halcanary96fcdcc2015-08-27 07:41:13 -070018 return nullptr;
reed1c846342015-07-09 11:47:36 -070019}
20
21static void test_imagegenerator_factory(skiatest::Reporter* reporter) {
reedd7c05bf2015-07-09 14:08:49 -070022 // just need a non-empty data to test things
bungeman38d909e2016-08-02 14:40:46 -070023 sk_sp<SkData> data(SkData::MakeWithCString("test_imagegenerator_factory"));
reedd7c05bf2015-07-09 14:08:49 -070024
25 gMyFactoryWasCalled = false;
reed1c846342015-07-09 11:47:36 -070026
reedd7c05bf2015-07-09 14:08:49 -070027 REPORTER_ASSERT(reporter, !gMyFactoryWasCalled);
reed1c846342015-07-09 11:47:36 -070028
Mike Reed185130c2017-02-15 15:14:16 -050029 std::unique_ptr<SkImageGenerator> gen = SkImageGenerator::MakeFromEncoded(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...
Mike Reed185130c2017-02-15 15:14:16 -050034 auto prev = SkGraphics::SetImageGeneratorFromEncodedDataFactory(my_factory);
35 gen = SkImageGenerator::MakeFromEncoded(data);
halcanary96fcdcc2015-08-27 07:41:13 -070036 REPORTER_ASSERT(reporter, nullptr == gen);
reedd7c05bf2015-07-09 14:08:49 -070037 REPORTER_ASSERT(reporter, gMyFactoryWasCalled);
Mike Reed185130c2017-02-15 15:14:16 -050038 SkGraphics::SetImageGeneratorFromEncodedDataFactory(prev);
reed1c846342015-07-09 11:47:36 -070039}
40
reed3ef71e32015-03-19 08:31:14 -070041class MyImageGenerator : public SkImageGenerator {
42public:
43 MyImageGenerator() : SkImageGenerator(SkImageInfo::MakeN32Premul(0, 0)) {}
44};
45
sugoi518d83d2014-07-21 11:37:39 -070046DEF_TEST(ImageGenerator, reporter) {
reed3ef71e32015-03-19 08:31:14 -070047 MyImageGenerator ig;
msarett4984c3c2016-03-10 05:44:43 -080048 SkYUVSizeInfo sizeInfo;
49 sizeInfo.fSizes[SkYUVSizeInfo::kY] = SkISize::Make(200, 200);
50 sizeInfo.fSizes[SkYUVSizeInfo::kU] = SkISize::Make(100, 100);
51 sizeInfo.fSizes[SkYUVSizeInfo::kV] = SkISize::Make( 50, 50);
52 sizeInfo.fWidthBytes[SkYUVSizeInfo::kY] = 0;
53 sizeInfo.fWidthBytes[SkYUVSizeInfo::kU] = 0;
54 sizeInfo.fWidthBytes[SkYUVSizeInfo::kV] = 0;
55 void* planes[3] = { nullptr };
rileyaabaef862014-09-12 17:45:58 -070056 SkYUVColorSpace colorSpace;
sugoi518d83d2014-07-21 11:37:39 -070057
58 // Check that the YUV decoding API does not cause any crashes
msarett4984c3c2016-03-10 05:44:43 -080059 ig.queryYUV8(&sizeInfo, nullptr);
60 ig.queryYUV8(&sizeInfo, &colorSpace);
61 sizeInfo.fWidthBytes[SkYUVSizeInfo::kY] = 250;
62 sizeInfo.fWidthBytes[SkYUVSizeInfo::kU] = 250;
63 sizeInfo.fWidthBytes[SkYUVSizeInfo::kV] = 250;
sugoi518d83d2014-07-21 11:37:39 -070064 int dummy;
msarett4984c3c2016-03-10 05:44:43 -080065 planes[SkYUVSizeInfo::kY] = planes[SkYUVSizeInfo::kU] = planes[SkYUVSizeInfo::kV] = &dummy;
66 ig.getYUV8Planes(sizeInfo, planes);
reed1c846342015-07-09 11:47:36 -070067
fmalita5dd918b2015-09-14 14:51:04 -070068 // Suppressed due to https://code.google.com/p/skia/issues/detail?id=4339
69 if (false) {
70 test_imagegenerator_factory(reporter);
71 }
sugoi518d83d2014-07-21 11:37:39 -070072}
Mike Reedc756c7a2017-04-13 15:13:36 -040073
74#include "SkAutoMalloc.h"
75#include "SkPictureRecorder.h"
76
77static sk_sp<SkPicture> make_picture() {
78 SkPictureRecorder recorder;
79 recorder.beginRecording(100, 100)->drawColor(SK_ColorRED);
80 return recorder.finishRecordingAsPicture();
81}
82
83DEF_TEST(PictureImageGenerator, reporter) {
84 const struct {
85 SkColorType fColorType;
86 SkAlphaType fAlphaType;
87 bool fExpectSuccess;
88 } recs[] = {
89 { kRGBA_8888_SkColorType, kPremul_SkAlphaType, kRGBA_8888_SkColorType == kN32_SkColorType },
90 { kBGRA_8888_SkColorType, kPremul_SkAlphaType, kBGRA_8888_SkColorType == kN32_SkColorType },
91 { kRGBA_F16_SkColorType, kPremul_SkAlphaType, true },
92
93 { kRGBA_8888_SkColorType, kUnpremul_SkAlphaType, false },
94 { kBGRA_8888_SkColorType, kUnpremul_SkAlphaType, false },
95 { kRGBA_F16_SkColorType, kUnpremul_SkAlphaType, false },
96 };
97
98 auto colorspace = SkColorSpace::MakeSRGB();
99 auto picture = make_picture();
100 auto gen = SkImageGenerator::MakeFromPicture({100, 100}, picture, nullptr, nullptr,
101 SkImage::BitDepth::kU8, colorspace);
102
103 // worst case for all requests
104 SkAutoMalloc storage(100 * 100 * SkColorTypeBytesPerPixel(kRGBA_F16_SkColorType));
105
106 for (const auto& rec : recs) {
107 SkImageInfo info = SkImageInfo::Make(100, 100, rec.fColorType, rec.fAlphaType, colorspace);
108 bool success = gen->getPixels(info, storage.get(), info.minRowBytes());
109 REPORTER_ASSERT(reporter, success == rec.fExpectSuccess);
110 }
111}