1 /*
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
8 #include "SkData.h"
9 #include "SkCanvas.h"
10 #include "SkGraphics.h"
11 #include "SkImageGenerator.h"
12 #include "Test.h"
13
14 static bool gMyFactoryWasCalled;
15
my_factory(sk_sp<SkData>)16 static std::unique_ptr<SkImageGenerator> my_factory(sk_sp<SkData>) {
17 gMyFactoryWasCalled = true;
18 return nullptr;
19 }
20
test_imagegenerator_factory(skiatest::Reporter * reporter)21 static void test_imagegenerator_factory(skiatest::Reporter* reporter) {
22 // just need a non-empty data to test things
23 sk_sp<SkData> data(SkData::MakeWithCString("test_imagegenerator_factory"));
24
25 gMyFactoryWasCalled = false;
26
27 REPORTER_ASSERT(reporter, !gMyFactoryWasCalled);
28
29 std::unique_ptr<SkImageGenerator> gen = SkImageGenerator::MakeFromEncoded(data);
30 REPORTER_ASSERT(reporter, nullptr == gen);
31 REPORTER_ASSERT(reporter, !gMyFactoryWasCalled);
32
33 // Test is racy, in that it hopes no other thread is changing this global...
34 auto prev = SkGraphics::SetImageGeneratorFromEncodedDataFactory(my_factory);
35 gen = SkImageGenerator::MakeFromEncoded(data);
36 REPORTER_ASSERT(reporter, nullptr == gen);
37 REPORTER_ASSERT(reporter, gMyFactoryWasCalled);
38 SkGraphics::SetImageGeneratorFromEncodedDataFactory(prev);
39 }
40
41 class MyImageGenerator : public SkImageGenerator {
42 public:
MyImageGenerator()43 MyImageGenerator() : SkImageGenerator(SkImageInfo::MakeN32Premul(0, 0)) {}
44 };
45
DEF_TEST(ImageGenerator,reporter)46 DEF_TEST(ImageGenerator, reporter) {
47 MyImageGenerator ig;
48 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 };
56 SkYUVColorSpace colorSpace;
57
58 // Check that the YUV decoding API does not cause any crashes
59 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;
64 int dummy;
65 planes[SkYUVSizeInfo::kY] = planes[SkYUVSizeInfo::kU] = planes[SkYUVSizeInfo::kV] = &dummy;
66 ig.getYUV8Planes(sizeInfo, planes);
67
68 // Suppressed due to https://code.google.com/p/skia/issues/detail?id=4339
69 if (false) {
70 test_imagegenerator_factory(reporter);
71 }
72 }
73
74 #include "SkAutoMalloc.h"
75 #include "SkPictureRecorder.h"
76
make_picture()77 static sk_sp<SkPicture> make_picture() {
78 SkPictureRecorder recorder;
79 recorder.beginRecording(100, 100)->drawColor(SK_ColorRED);
80 return recorder.finishRecordingAsPicture();
81 }
82
DEF_TEST(PictureImageGenerator,reporter)83 DEF_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 }
112
113