blob: 574c8e872808307e098a91dea9d5a9ca4a5d0cca [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"
Mike Reed7fcfb622018-02-09 13:26:46 -050012#include "SkImageInfoPriv.h"
sugoi518d83d2014-07-21 11:37:39 -070013#include "Test.h"
14
reedd7c05bf2015-07-09 14:08:49 -070015static bool gMyFactoryWasCalled;
16
Mike Reed185130c2017-02-15 15:14:16 -050017static std::unique_ptr<SkImageGenerator> my_factory(sk_sp<SkData>) {
reedd7c05bf2015-07-09 14:08:49 -070018 gMyFactoryWasCalled = true;
halcanary96fcdcc2015-08-27 07:41:13 -070019 return nullptr;
reed1c846342015-07-09 11:47:36 -070020}
21
22static void test_imagegenerator_factory(skiatest::Reporter* reporter) {
reedd7c05bf2015-07-09 14:08:49 -070023 // just need a non-empty data to test things
bungeman38d909e2016-08-02 14:40:46 -070024 sk_sp<SkData> data(SkData::MakeWithCString("test_imagegenerator_factory"));
reedd7c05bf2015-07-09 14:08:49 -070025
26 gMyFactoryWasCalled = false;
reed1c846342015-07-09 11:47:36 -070027
reedd7c05bf2015-07-09 14:08:49 -070028 REPORTER_ASSERT(reporter, !gMyFactoryWasCalled);
reed1c846342015-07-09 11:47:36 -070029
Mike Reed185130c2017-02-15 15:14:16 -050030 std::unique_ptr<SkImageGenerator> gen = SkImageGenerator::MakeFromEncoded(data);
halcanary96fcdcc2015-08-27 07:41:13 -070031 REPORTER_ASSERT(reporter, nullptr == gen);
reedd7c05bf2015-07-09 14:08:49 -070032 REPORTER_ASSERT(reporter, !gMyFactoryWasCalled);
reed1c846342015-07-09 11:47:36 -070033
34 // Test is racy, in that it hopes no other thread is changing this global...
Mike Reed185130c2017-02-15 15:14:16 -050035 auto prev = SkGraphics::SetImageGeneratorFromEncodedDataFactory(my_factory);
36 gen = SkImageGenerator::MakeFromEncoded(data);
halcanary96fcdcc2015-08-27 07:41:13 -070037 REPORTER_ASSERT(reporter, nullptr == gen);
reedd7c05bf2015-07-09 14:08:49 -070038 REPORTER_ASSERT(reporter, gMyFactoryWasCalled);
Mike Reed185130c2017-02-15 15:14:16 -050039 SkGraphics::SetImageGeneratorFromEncodedDataFactory(prev);
reed1c846342015-07-09 11:47:36 -070040}
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}
Mike Reedc756c7a2017-04-13 15:13:36 -040074
75#include "SkAutoMalloc.h"
76#include "SkPictureRecorder.h"
77
78static sk_sp<SkPicture> make_picture() {
79 SkPictureRecorder recorder;
80 recorder.beginRecording(100, 100)->drawColor(SK_ColorRED);
81 return recorder.finishRecordingAsPicture();
82}
83
84DEF_TEST(PictureImageGenerator, reporter) {
85 const struct {
86 SkColorType fColorType;
87 SkAlphaType fAlphaType;
88 bool fExpectSuccess;
89 } recs[] = {
90 { kRGBA_8888_SkColorType, kPremul_SkAlphaType, kRGBA_8888_SkColorType == kN32_SkColorType },
91 { kBGRA_8888_SkColorType, kPremul_SkAlphaType, kBGRA_8888_SkColorType == kN32_SkColorType },
92 { kRGBA_F16_SkColorType, kPremul_SkAlphaType, true },
Brian Osman10fc6fd2018-03-02 11:01:10 -050093 { kRGBA_1010102_SkColorType, kPremul_SkAlphaType, true },
Mike Reedc756c7a2017-04-13 15:13:36 -040094
95 { kRGBA_8888_SkColorType, kUnpremul_SkAlphaType, false },
96 { kBGRA_8888_SkColorType, kUnpremul_SkAlphaType, false },
97 { kRGBA_F16_SkColorType, kUnpremul_SkAlphaType, false },
Brian Osman10fc6fd2018-03-02 11:01:10 -050098 { kRGBA_1010102_SkColorType, kUnpremul_SkAlphaType, false },
Mike Reedc756c7a2017-04-13 15:13:36 -040099 };
100
101 auto colorspace = SkColorSpace::MakeSRGB();
102 auto picture = make_picture();
103 auto gen = SkImageGenerator::MakeFromPicture({100, 100}, picture, nullptr, nullptr,
104 SkImage::BitDepth::kU8, colorspace);
105
106 // worst case for all requests
107 SkAutoMalloc storage(100 * 100 * SkColorTypeBytesPerPixel(kRGBA_F16_SkColorType));
108
109 for (const auto& rec : recs) {
110 SkImageInfo info = SkImageInfo::Make(100, 100, rec.fColorType, rec.fAlphaType, colorspace);
111 bool success = gen->getPixels(info, storage.get(), info.minRowBytes());
112 REPORTER_ASSERT(reporter, success == rec.fExpectSuccess);
113 }
114}
Matt Sarettdc792702017-06-08 09:37:01 -0400115