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