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/SkMemset.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 : SkImageGenerator(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
92 ////////////////////////////////////////////////////////////////////////////////
93
DEF_TEST(Image_NewFromGenerator,r)94 DEF_TEST(Image_NewFromGenerator, r) {
95 const TestImageGenerator::TestType testTypes[] = {
96 TestImageGenerator::kFailGetPixels_TestType,
97 TestImageGenerator::kSucceedGetPixels_TestType,
98 };
99 const SkColorType testColorTypes[] = {
100 kN32_SkColorType,
101 kRGB_565_SkColorType
102 };
103 for (size_t i = 0; i < std::size(testTypes); ++i) {
104 TestImageGenerator::TestType test = testTypes[i];
105 for (const SkColorType testColorType : testColorTypes) {
106 auto gen = std::make_unique<TestImageGenerator>(test, r, testColorType);
107 sk_sp<SkImage> image(SkImages::DeferredFromGenerator(std::move(gen)));
108 if (nullptr == image) {
109 ERRORF(r, "SkImage::NewFromGenerator unexpecedly failed [%zu]", i);
110 continue;
111 }
112 REPORTER_ASSERT(r, TestImageGenerator::Width() == image->width());
113 REPORTER_ASSERT(r, TestImageGenerator::Height() == image->height());
114 REPORTER_ASSERT(r, image->isLazyGenerated());
115
116 SkBitmap bitmap;
117 bitmap.allocN32Pixels(TestImageGenerator::Width(), TestImageGenerator::Height());
118 SkCanvas canvas(bitmap);
119 const SkColor kDefaultColor = 0xffabcdef;
120 canvas.clear(kDefaultColor);
121 canvas.drawImage(image, 0, 0);
122 if (TestImageGenerator::kSucceedGetPixels_TestType == test) {
123 REPORTER_ASSERT(
124 r, TestImageGenerator::Color() == bitmap.getColor(0, 0));
125 }
126 else {
127 REPORTER_ASSERT(r, kDefaultColor == bitmap.getColor(0, 0));
128 }
129 }
130 }
131 }
132