blob: 284960e42d465e9efdd3aa78fd3e468fce6b3e71 [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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "src/core/SkUtils.h"
18#include "tests/Test.h"
19#include "tools/ToolUtils.h"
commit-bot@chromium.org75854792013-10-29 19:55:00 +000020
Ben Wagnerb607a8f2018-03-12 13:46:21 -040021#include <utility>
22
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000023class TestImageGenerator : public SkImageGenerator {
24public:
25 enum TestType {
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000026 kFailGetPixels_TestType,
27 kSucceedGetPixels_TestType,
28 kLast_TestType = kSucceedGetPixels_TestType
29 };
30 static int Width() { return 10; }
31 static int Height() { return 10; }
aleksandar.stojiljkovic07e26922015-11-10 04:55:15 -080032 // value choosen so that there is no loss when converting to to RGB565 and back
Mike Kleinea3f0142019-03-20 11:12:10 -050033 static SkColor Color() { return ToolUtils::color_to_565(0xffaabbcc); }
aleksandar.stojiljkovic07e26922015-11-10 04:55:15 -080034 static SkPMColor PMColor() { return SkPreMultiplyColor(Color()); }
35
36 TestImageGenerator(TestType type, skiatest::Reporter* reporter,
37 SkColorType colorType = kN32_SkColorType)
38 : INHERITED(GetMyInfo(colorType)), fType(type), fReporter(reporter) {
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000039 SkASSERT((fType <= kLast_TestType) && (fType >= 0));
40 }
Brian Salomond3b65972017-03-22 12:05:03 -040041 ~TestImageGenerator() override {}
commit-bot@chromium.org00f8d6c2014-05-29 15:57:20 +000042
43protected:
aleksandar.stojiljkovic07e26922015-11-10 04:55:15 -080044 static SkImageInfo GetMyInfo(SkColorType colorType) {
45 return SkImageInfo::Make(TestImageGenerator::Width(), TestImageGenerator::Height(),
46 colorType, kOpaque_SkAlphaType);
reed3ef71e32015-03-19 08:31:14 -070047 }
48
scroggo5315fd42015-07-09 09:08:00 -070049 bool onGetPixels(const SkImageInfo& info, void* pixels, size_t rowBytes,
Matt Sarettebb1b5c2017-05-12 11:41:27 -040050 const Options& options) override {
halcanary96fcdcc2015-08-27 07:41:13 -070051 REPORTER_ASSERT(fReporter, pixels != nullptr);
scroggo08649082015-02-13 11:13:34 -080052 REPORTER_ASSERT(fReporter, rowBytes >= info.minRowBytes());
53 if (fType != kSucceedGetPixels_TestType) {
scroggo5315fd42015-07-09 09:08:00 -070054 return false;
scroggo08649082015-02-13 11:13:34 -080055 }
aleksandar.stojiljkovic07e26922015-11-10 04:55:15 -080056 if (info.colorType() != kN32_SkColorType && info.colorType() != getInfo().colorType()) {
scroggo5315fd42015-07-09 09:08:00 -070057 return false;
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000058 }
59 char* bytePtr = static_cast<char*>(pixels);
aleksandar.stojiljkovic07e26922015-11-10 04:55:15 -080060 switch (info.colorType()) {
61 case kN32_SkColorType:
62 for (int y = 0; y < info.height(); ++y) {
63 sk_memset32((uint32_t*)bytePtr,
64 TestImageGenerator::PMColor(), info.width());
65 bytePtr += rowBytes;
66 }
67 break;
aleksandar.stojiljkovic07e26922015-11-10 04:55:15 -080068 case kRGB_565_SkColorType:
69 for (int y = 0; y < info.height(); ++y) {
70 sk_memset16((uint16_t*)bytePtr,
71 SkPixel32ToPixel16(TestImageGenerator::PMColor()), info.width());
72 bytePtr += rowBytes;
73 }
74 break;
75 default:
76 return false;
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000077 }
scroggo5315fd42015-07-09 09:08:00 -070078 return true;
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000079 }
halcanary@google.com3d50ea12014-01-02 13:15:13 +000080
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000081private:
82 const TestType fType;
83 skiatest::Reporter* const fReporter;
reed3ef71e32015-03-19 08:31:14 -070084
85 typedef SkImageGenerator INHERITED;
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000086};
halcanary@google.com3d50ea12014-01-02 13:15:13 +000087
halcanaryea4673f2014-08-18 08:27:09 -070088////////////////////////////////////////////////////////////////////////////////
89
90DEF_TEST(Image_NewFromGenerator, r) {
aleksandar.stojiljkovic07e26922015-11-10 04:55:15 -080091 const TestImageGenerator::TestType testTypes[] = {
halcanaryea4673f2014-08-18 08:27:09 -070092 TestImageGenerator::kFailGetPixels_TestType,
93 TestImageGenerator::kSucceedGetPixels_TestType,
94 };
aleksandar.stojiljkovic07e26922015-11-10 04:55:15 -080095 const SkColorType testColorTypes[] = {
96 kN32_SkColorType,
aleksandar.stojiljkovic07e26922015-11-10 04:55:15 -080097 kRGB_565_SkColorType
98 };
halcanaryea4673f2014-08-18 08:27:09 -070099 for (size_t i = 0; i < SK_ARRAY_COUNT(testTypes); ++i) {
100 TestImageGenerator::TestType test = testTypes[i];
aleksandar.stojiljkovic07e26922015-11-10 04:55:15 -0800101 for (const SkColorType testColorType : testColorTypes) {
Mike Kleinf46d5ca2019-12-11 10:45:01 -0500102 auto gen = std::make_unique<TestImageGenerator>(test, r, testColorType);
Mike Reed185130c2017-02-15 15:14:16 -0500103 sk_sp<SkImage> image(SkImage::MakeFromGenerator(std::move(gen)));
reed9ce9d672016-03-17 10:51:11 -0700104 if (nullptr == image) {
aleksandar.stojiljkovic07e26922015-11-10 04:55:15 -0800105 ERRORF(r, "SkImage::NewFromGenerator unexpecedly failed ["
106 SK_SIZE_T_SPECIFIER "]", i);
107 continue;
108 }
109 REPORTER_ASSERT(r, TestImageGenerator::Width() == image->width());
110 REPORTER_ASSERT(r, TestImageGenerator::Height() == image->height());
111 REPORTER_ASSERT(r, image->isLazyGenerated());
halcanaryea4673f2014-08-18 08:27:09 -0700112
aleksandar.stojiljkovic07e26922015-11-10 04:55:15 -0800113 SkBitmap bitmap;
114 bitmap.allocN32Pixels(TestImageGenerator::Width(), TestImageGenerator::Height());
115 SkCanvas canvas(bitmap);
116 const SkColor kDefaultColor = 0xffabcdef;
117 canvas.clear(kDefaultColor);
118 canvas.drawImage(image, 0, 0, nullptr);
119 if (TestImageGenerator::kSucceedGetPixels_TestType == test) {
120 REPORTER_ASSERT(
121 r, TestImageGenerator::Color() == bitmap.getColor(0, 0));
122 }
123 else {
124 REPORTER_ASSERT(r, kDefaultColor == bitmap.getColor(0, 0));
125 }
halcanaryea4673f2014-08-18 08:27:09 -0700126 }
127 }
128}