blob: 5cfd410ad0a9812c4b7bd795267e55d5d7586949 [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}
Matt Sarettdc792702017-06-08 09:37:01 -0400112
113#include "SkImagePriv.h"
114
115DEF_TEST(ColorXformGenerator, r) {
Brian Osmane5312072017-06-20 09:35:51 -0400116 SkBitmap a, b, c, d, e;
Matt Sarettdc792702017-06-08 09:37:01 -0400117 SkImageInfo info = SkImageInfo::MakeS32(1, 1, kPremul_SkAlphaType);
118 a.allocPixels(info);
119 b.allocPixels(info.makeColorSpace(nullptr));
120 c.allocPixels(info.makeColorSpace(SkColorSpace::MakeRGB(SkColorSpace::kSRGB_RenderTargetGamma,
121 SkColorSpace::kRec2020_Gamut)));
122 d.allocPixels(info.makeColorSpace(SkColorSpace::MakeRGB(SkColorSpace::kSRGB_RenderTargetGamma,
123 SkColorSpace::kAdobeRGB_Gamut)));
Brian Osmane5312072017-06-20 09:35:51 -0400124 e.allocPixels(info);
Matt Sarettdc792702017-06-08 09:37:01 -0400125 a.eraseColor(0);
126 b.eraseColor(1);
127 c.eraseColor(2);
128 d.eraseColor(3);
Brian Osmane5312072017-06-20 09:35:51 -0400129 e.eraseColor(4);
Matt Sarettdc792702017-06-08 09:37:01 -0400130
131 sk_sp<SkColorSpace> srgb = SkColorSpace::MakeSRGB();
132 sk_sp<SkImage> ia = SkMakeImageInColorSpace(a, srgb, 0);
133 sk_sp<SkImage> ib = SkMakeImageInColorSpace(b, srgb, b.getGenerationID());
134 sk_sp<SkImage> ic = SkMakeImageInColorSpace(c, srgb, c.getGenerationID());
135 sk_sp<SkImage> id = SkMakeImageInColorSpace(d, srgb, 0);
Brian Osmane5312072017-06-20 09:35:51 -0400136 sk_sp<SkImage> ie = SkMakeImageInColorSpace(e, srgb, e.getGenerationID(),
137 kAlways_SkCopyPixelsMode);
Matt Sarettdc792702017-06-08 09:37:01 -0400138
139 // Equal because sRGB->sRGB is a no-op.
140 REPORTER_ASSERT(r, ia->uniqueID() == a.getGenerationID());
141
142 // Equal because nullptr->sRGB is a no-op (nullptr is treated as sRGB), and because
143 // we pass the explicit id that we want. In the no-op case, the implementation
144 // actually asserts that if we pass an id, it must match the id on the bitmap.
145 REPORTER_ASSERT(r, ib->uniqueID() == b.getGenerationID());
146
147 // Equal because we pass in an explicit id.
148 REPORTER_ASSERT(r, ic->uniqueID() == c.getGenerationID());
149
150 // Not equal because sRGB->Adobe is not a no-op and we do not pass an explicit id.
151 REPORTER_ASSERT(r, id->uniqueID() != d.getGenerationID());
Brian Osmane5312072017-06-20 09:35:51 -0400152
153 // Equal because we pass in an explicit id. Forcing a copy, but still want the id respected.
154 REPORTER_ASSERT(r, ie->uniqueID() == e.getGenerationID());
Matt Sarettdc792702017-06-08 09:37:01 -0400155}