blob: f4fa442ae5399a6531890ad0eb64b87f3ed8cc64 [file] [log] [blame]
commit-bot@chromium.org75854792013-10-29 19:55:00 +00001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkBitmap.h"
9#include "include/core/SkCanvas.h"
10#include "include/core/SkColor.h"
11#include "include/core/SkImage.h"
12#include "include/core/SkImageGenerator.h"
13#include "include/core/SkImageInfo.h"
14#include "include/core/SkRefCnt.h"
15#include "include/core/SkTypes.h"
16#include "include/private/SkColorData.h"
17#include "src/core/SkMakeUnique.h"
18#include "src/core/SkUtils.h"
19#include "tests/Test.h"
20#include "tools/ToolUtils.h"
commit-bot@chromium.org75854792013-10-29 19:55:00 +000021
Ben Wagnerb607a8f2018-03-12 13:46:21 -040022#include <utility>
23
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000024class TestImageGenerator : public SkImageGenerator {
25public:
26 enum TestType {
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000027 kFailGetPixels_TestType,
28 kSucceedGetPixels_TestType,
29 kLast_TestType = kSucceedGetPixels_TestType
30 };
31 static int Width() { return 10; }
32 static int Height() { return 10; }
aleksandar.stojiljkovic07e26922015-11-10 04:55:15 -080033 // value choosen so that there is no loss when converting to to RGB565 and back
Mike Kleinea3f0142019-03-20 11:12:10 -050034 static SkColor Color() { return ToolUtils::color_to_565(0xffaabbcc); }
aleksandar.stojiljkovic07e26922015-11-10 04:55:15 -080035 static SkPMColor PMColor() { return SkPreMultiplyColor(Color()); }
36
37 TestImageGenerator(TestType type, skiatest::Reporter* reporter,
38 SkColorType colorType = kN32_SkColorType)
39 : INHERITED(GetMyInfo(colorType)), fType(type), fReporter(reporter) {
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000040 SkASSERT((fType <= kLast_TestType) && (fType >= 0));
41 }
Brian Salomond3b65972017-03-22 12:05:03 -040042 ~TestImageGenerator() override {}
commit-bot@chromium.org00f8d6c2014-05-29 15:57:20 +000043
44protected:
aleksandar.stojiljkovic07e26922015-11-10 04:55:15 -080045 static SkImageInfo GetMyInfo(SkColorType colorType) {
46 return SkImageInfo::Make(TestImageGenerator::Width(), TestImageGenerator::Height(),
47 colorType, kOpaque_SkAlphaType);
reed3ef71e32015-03-19 08:31:14 -070048 }
49
scroggo5315fd42015-07-09 09:08:00 -070050 bool onGetPixels(const SkImageInfo& info, void* pixels, size_t rowBytes,
Matt Sarettebb1b5c2017-05-12 11:41:27 -040051 const Options& options) override {
halcanary96fcdcc2015-08-27 07:41:13 -070052 REPORTER_ASSERT(fReporter, pixels != nullptr);
scroggo08649082015-02-13 11:13:34 -080053 REPORTER_ASSERT(fReporter, rowBytes >= info.minRowBytes());
54 if (fType != kSucceedGetPixels_TestType) {
scroggo5315fd42015-07-09 09:08:00 -070055 return false;
scroggo08649082015-02-13 11:13:34 -080056 }
aleksandar.stojiljkovic07e26922015-11-10 04:55:15 -080057 if (info.colorType() != kN32_SkColorType && info.colorType() != getInfo().colorType()) {
scroggo5315fd42015-07-09 09:08:00 -070058 return false;
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000059 }
60 char* bytePtr = static_cast<char*>(pixels);
aleksandar.stojiljkovic07e26922015-11-10 04:55:15 -080061 switch (info.colorType()) {
62 case kN32_SkColorType:
63 for (int y = 0; y < info.height(); ++y) {
64 sk_memset32((uint32_t*)bytePtr,
65 TestImageGenerator::PMColor(), info.width());
66 bytePtr += rowBytes;
67 }
68 break;
aleksandar.stojiljkovic07e26922015-11-10 04:55:15 -080069 case kRGB_565_SkColorType:
70 for (int y = 0; y < info.height(); ++y) {
71 sk_memset16((uint16_t*)bytePtr,
72 SkPixel32ToPixel16(TestImageGenerator::PMColor()), info.width());
73 bytePtr += rowBytes;
74 }
75 break;
76 default:
77 return false;
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000078 }
scroggo5315fd42015-07-09 09:08:00 -070079 return true;
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000080 }
halcanary@google.com3d50ea12014-01-02 13:15:13 +000081
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000082private:
83 const TestType fType;
84 skiatest::Reporter* const fReporter;
reed3ef71e32015-03-19 08:31:14 -070085
86 typedef SkImageGenerator INHERITED;
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000087};
halcanary@google.com3d50ea12014-01-02 13:15:13 +000088
halcanaryea4673f2014-08-18 08:27:09 -070089////////////////////////////////////////////////////////////////////////////////
90
91DEF_TEST(Image_NewFromGenerator, r) {
aleksandar.stojiljkovic07e26922015-11-10 04:55:15 -080092 const TestImageGenerator::TestType testTypes[] = {
halcanaryea4673f2014-08-18 08:27:09 -070093 TestImageGenerator::kFailGetPixels_TestType,
94 TestImageGenerator::kSucceedGetPixels_TestType,
95 };
aleksandar.stojiljkovic07e26922015-11-10 04:55:15 -080096 const SkColorType testColorTypes[] = {
97 kN32_SkColorType,
aleksandar.stojiljkovic07e26922015-11-10 04:55:15 -080098 kRGB_565_SkColorType
99 };
halcanaryea4673f2014-08-18 08:27:09 -0700100 for (size_t i = 0; i < SK_ARRAY_COUNT(testTypes); ++i) {
101 TestImageGenerator::TestType test = testTypes[i];
aleksandar.stojiljkovic07e26922015-11-10 04:55:15 -0800102 for (const SkColorType testColorType : testColorTypes) {
Mike Reed185130c2017-02-15 15:14:16 -0500103 auto gen = skstd::make_unique<TestImageGenerator>(test, r, testColorType);
104 sk_sp<SkImage> image(SkImage::MakeFromGenerator(std::move(gen)));
reed9ce9d672016-03-17 10:51:11 -0700105 if (nullptr == image) {
aleksandar.stojiljkovic07e26922015-11-10 04:55:15 -0800106 ERRORF(r, "SkImage::NewFromGenerator unexpecedly failed ["
107 SK_SIZE_T_SPECIFIER "]", i);
108 continue;
109 }
110 REPORTER_ASSERT(r, TestImageGenerator::Width() == image->width());
111 REPORTER_ASSERT(r, TestImageGenerator::Height() == image->height());
112 REPORTER_ASSERT(r, image->isLazyGenerated());
halcanaryea4673f2014-08-18 08:27:09 -0700113
aleksandar.stojiljkovic07e26922015-11-10 04:55:15 -0800114 SkBitmap bitmap;
115 bitmap.allocN32Pixels(TestImageGenerator::Width(), TestImageGenerator::Height());
116 SkCanvas canvas(bitmap);
117 const SkColor kDefaultColor = 0xffabcdef;
118 canvas.clear(kDefaultColor);
119 canvas.drawImage(image, 0, 0, nullptr);
120 if (TestImageGenerator::kSucceedGetPixels_TestType == test) {
121 REPORTER_ASSERT(
122 r, TestImageGenerator::Color() == bitmap.getColor(0, 0));
123 }
124 else {
125 REPORTER_ASSERT(r, kDefaultColor == bitmap.getColor(0, 0));
126 }
halcanaryea4673f2014-08-18 08:27:09 -0700127 }
128 }
129}