1 /*
2 * Copyright 2013 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 "include/core/SkAlphaType.h"
9 #include "include/core/SkBitmap.h"
10 #include "include/core/SkCanvas.h"
11 #include "include/core/SkColor.h"
12 #include "include/core/SkColorType.h"
13 #include "include/core/SkImage.h"
14 #include "include/core/SkImageGenerator.h"
15 #include "include/core/SkImageInfo.h"
16 #include "include/core/SkRefCnt.h"
17 #include "include/core/SkTypes.h"
18 #include "include/private/SkColorData.h"
19 #include "src/core/SkOpts.h"
20 #include "tests/Test.h"
21 #include "tools/ToolUtils.h"
22
23 #include <array>
24 #include <cstddef>
25 #include <cstdint>
26 #include <memory>
27 #include <utility>
28
29 class TestImageGenerator : public SkImageGenerator {
30 public:
31 enum TestType {
32 kFailGetPixels_TestType,
33 kSucceedGetPixels_TestType,
34 kLast_TestType = kSucceedGetPixels_TestType
35 };
Width()36 static int Width() { return 10; }
Height()37 static int Height() { return 10; }
38 // value choosen so that there is no loss when converting to to RGB565 and back
Color()39 static SkColor Color() { return ToolUtils::color_to_565(0xffaabbcc); }
PMColor()40 static SkPMColor PMColor() { return SkPreMultiplyColor(Color()); }
41
TestImageGenerator(TestType type,skiatest::Reporter * reporter,SkColorType colorType=kN32_SkColorType)42 TestImageGenerator(TestType type, skiatest::Reporter* reporter,
43 SkColorType colorType = kN32_SkColorType)
44 : INHERITED(GetMyInfo(colorType)), fType(type), fReporter(reporter) {
45 SkASSERT((fType <= kLast_TestType) && (fType >= 0));
46 }
~TestImageGenerator()47 ~TestImageGenerator() override {}
48
49 protected:
GetMyInfo(SkColorType colorType)50 static SkImageInfo GetMyInfo(SkColorType colorType) {
51 return SkImageInfo::Make(TestImageGenerator::Width(), TestImageGenerator::Height(),
52 colorType, kOpaque_SkAlphaType);
53 }
54
onGetPixels(const SkImageInfo & info,void * pixels,size_t rowBytes,const Options & options)55 bool onGetPixels(const SkImageInfo& info, void* pixels, size_t rowBytes,
56 const Options& options) override {
57 REPORTER_ASSERT(fReporter, pixels != nullptr);
58 REPORTER_ASSERT(fReporter, rowBytes >= info.minRowBytes());
59 if (fType != kSucceedGetPixels_TestType) {
60 return false;
61 }
62 if (info.colorType() != kN32_SkColorType && info.colorType() != getInfo().colorType()) {
63 return false;
64 }
65 char* bytePtr = static_cast<char*>(pixels);
66 switch (info.colorType()) {
67 case kN32_SkColorType:
68 for (int y = 0; y < info.height(); ++y) {
69 SkOpts::memset32((uint32_t*)bytePtr,
70 TestImageGenerator::PMColor(), info.width());
71 bytePtr += rowBytes;
72 }
73 break;
74 case kRGB_565_SkColorType:
75 for (int y = 0; y < info.height(); ++y) {
76 SkOpts::memset16((uint16_t*)bytePtr,
77 SkPixel32ToPixel16(TestImageGenerator::PMColor()), info.width());
78 bytePtr += rowBytes;
79 }
80 break;
81 default:
82 return false;
83 }
84 return true;
85 }
86
87 private:
88 const TestType fType;
89 skiatest::Reporter* const fReporter;
90
91 using INHERITED = SkImageGenerator;
92 };
93
94 ////////////////////////////////////////////////////////////////////////////////
95
DEF_TEST(Image_NewFromGenerator,r)96 DEF_TEST(Image_NewFromGenerator, r) {
97 const TestImageGenerator::TestType testTypes[] = {
98 TestImageGenerator::kFailGetPixels_TestType,
99 TestImageGenerator::kSucceedGetPixels_TestType,
100 };
101 const SkColorType testColorTypes[] = {
102 kN32_SkColorType,
103 kRGB_565_SkColorType
104 };
105 for (size_t i = 0; i < std::size(testTypes); ++i) {
106 TestImageGenerator::TestType test = testTypes[i];
107 for (const SkColorType testColorType : testColorTypes) {
108 auto gen = std::make_unique<TestImageGenerator>(test, r, testColorType);
109 sk_sp<SkImage> image(SkImage::MakeFromGenerator(std::move(gen)));
110 if (nullptr == image) {
111 ERRORF(r, "SkImage::NewFromGenerator unexpecedly failed [%zu]", i);
112 continue;
113 }
114 REPORTER_ASSERT(r, TestImageGenerator::Width() == image->width());
115 REPORTER_ASSERT(r, TestImageGenerator::Height() == image->height());
116 REPORTER_ASSERT(r, image->isLazyGenerated());
117
118 SkBitmap bitmap;
119 bitmap.allocN32Pixels(TestImageGenerator::Width(), TestImageGenerator::Height());
120 SkCanvas canvas(bitmap);
121 const SkColor kDefaultColor = 0xffabcdef;
122 canvas.clear(kDefaultColor);
123 canvas.drawImage(image, 0, 0);
124 if (TestImageGenerator::kSucceedGetPixels_TestType == test) {
125 REPORTER_ASSERT(
126 r, TestImageGenerator::Color() == bitmap.getColor(0, 0));
127 }
128 else {
129 REPORTER_ASSERT(r, kDefaultColor == bitmap.getColor(0, 0));
130 }
131 }
132 }
133 }
134