commit-bot@chromium.org | 7585479 | 2013-10-29 19:55:00 +0000 | [diff] [blame] | 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #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 Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 17 | #include "src/core/SkUtils.h" |
| 18 | #include "tests/Test.h" |
| 19 | #include "tools/ToolUtils.h" |
commit-bot@chromium.org | 7585479 | 2013-10-29 19:55:00 +0000 | [diff] [blame] | 20 | |
Ben Wagner | b607a8f | 2018-03-12 13:46:21 -0400 | [diff] [blame] | 21 | #include <utility> |
| 22 | |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 23 | class TestImageGenerator : public SkImageGenerator { |
| 24 | public: |
| 25 | enum TestType { |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 26 | kFailGetPixels_TestType, |
| 27 | kSucceedGetPixels_TestType, |
| 28 | kLast_TestType = kSucceedGetPixels_TestType |
| 29 | }; |
| 30 | static int Width() { return 10; } |
| 31 | static int Height() { return 10; } |
aleksandar.stojiljkovic | 07e2692 | 2015-11-10 04:55:15 -0800 | [diff] [blame] | 32 | // value choosen so that there is no loss when converting to to RGB565 and back |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 33 | static SkColor Color() { return ToolUtils::color_to_565(0xffaabbcc); } |
aleksandar.stojiljkovic | 07e2692 | 2015-11-10 04:55:15 -0800 | [diff] [blame] | 34 | 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.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 39 | SkASSERT((fType <= kLast_TestType) && (fType >= 0)); |
| 40 | } |
Brian Salomon | d3b6597 | 2017-03-22 12:05:03 -0400 | [diff] [blame] | 41 | ~TestImageGenerator() override {} |
commit-bot@chromium.org | 00f8d6c | 2014-05-29 15:57:20 +0000 | [diff] [blame] | 42 | |
| 43 | protected: |
aleksandar.stojiljkovic | 07e2692 | 2015-11-10 04:55:15 -0800 | [diff] [blame] | 44 | static SkImageInfo GetMyInfo(SkColorType colorType) { |
| 45 | return SkImageInfo::Make(TestImageGenerator::Width(), TestImageGenerator::Height(), |
| 46 | colorType, kOpaque_SkAlphaType); |
reed | 3ef71e3 | 2015-03-19 08:31:14 -0700 | [diff] [blame] | 47 | } |
| 48 | |
scroggo | 5315fd4 | 2015-07-09 09:08:00 -0700 | [diff] [blame] | 49 | bool onGetPixels(const SkImageInfo& info, void* pixels, size_t rowBytes, |
Matt Sarett | ebb1b5c | 2017-05-12 11:41:27 -0400 | [diff] [blame] | 50 | const Options& options) override { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 51 | REPORTER_ASSERT(fReporter, pixels != nullptr); |
scroggo | 0864908 | 2015-02-13 11:13:34 -0800 | [diff] [blame] | 52 | REPORTER_ASSERT(fReporter, rowBytes >= info.minRowBytes()); |
| 53 | if (fType != kSucceedGetPixels_TestType) { |
scroggo | 5315fd4 | 2015-07-09 09:08:00 -0700 | [diff] [blame] | 54 | return false; |
scroggo | 0864908 | 2015-02-13 11:13:34 -0800 | [diff] [blame] | 55 | } |
aleksandar.stojiljkovic | 07e2692 | 2015-11-10 04:55:15 -0800 | [diff] [blame] | 56 | if (info.colorType() != kN32_SkColorType && info.colorType() != getInfo().colorType()) { |
scroggo | 5315fd4 | 2015-07-09 09:08:00 -0700 | [diff] [blame] | 57 | return false; |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 58 | } |
| 59 | char* bytePtr = static_cast<char*>(pixels); |
aleksandar.stojiljkovic | 07e2692 | 2015-11-10 04:55:15 -0800 | [diff] [blame] | 60 | 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.stojiljkovic | 07e2692 | 2015-11-10 04:55:15 -0800 | [diff] [blame] | 68 | 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.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 77 | } |
scroggo | 5315fd4 | 2015-07-09 09:08:00 -0700 | [diff] [blame] | 78 | return true; |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 79 | } |
halcanary@google.com | 3d50ea1 | 2014-01-02 13:15:13 +0000 | [diff] [blame] | 80 | |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 81 | private: |
| 82 | const TestType fType; |
| 83 | skiatest::Reporter* const fReporter; |
reed | 3ef71e3 | 2015-03-19 08:31:14 -0700 | [diff] [blame] | 84 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 85 | using INHERITED = SkImageGenerator; |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 86 | }; |
halcanary@google.com | 3d50ea1 | 2014-01-02 13:15:13 +0000 | [diff] [blame] | 87 | |
halcanary | ea4673f | 2014-08-18 08:27:09 -0700 | [diff] [blame] | 88 | //////////////////////////////////////////////////////////////////////////////// |
| 89 | |
| 90 | DEF_TEST(Image_NewFromGenerator, r) { |
aleksandar.stojiljkovic | 07e2692 | 2015-11-10 04:55:15 -0800 | [diff] [blame] | 91 | const TestImageGenerator::TestType testTypes[] = { |
halcanary | ea4673f | 2014-08-18 08:27:09 -0700 | [diff] [blame] | 92 | TestImageGenerator::kFailGetPixels_TestType, |
| 93 | TestImageGenerator::kSucceedGetPixels_TestType, |
| 94 | }; |
aleksandar.stojiljkovic | 07e2692 | 2015-11-10 04:55:15 -0800 | [diff] [blame] | 95 | const SkColorType testColorTypes[] = { |
| 96 | kN32_SkColorType, |
aleksandar.stojiljkovic | 07e2692 | 2015-11-10 04:55:15 -0800 | [diff] [blame] | 97 | kRGB_565_SkColorType |
| 98 | }; |
halcanary | ea4673f | 2014-08-18 08:27:09 -0700 | [diff] [blame] | 99 | for (size_t i = 0; i < SK_ARRAY_COUNT(testTypes); ++i) { |
| 100 | TestImageGenerator::TestType test = testTypes[i]; |
aleksandar.stojiljkovic | 07e2692 | 2015-11-10 04:55:15 -0800 | [diff] [blame] | 101 | for (const SkColorType testColorType : testColorTypes) { |
Mike Klein | f46d5ca | 2019-12-11 10:45:01 -0500 | [diff] [blame] | 102 | auto gen = std::make_unique<TestImageGenerator>(test, r, testColorType); |
Mike Reed | 185130c | 2017-02-15 15:14:16 -0500 | [diff] [blame] | 103 | sk_sp<SkImage> image(SkImage::MakeFromGenerator(std::move(gen))); |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 104 | if (nullptr == image) { |
John Stiles | 7094159 | 2020-05-04 23:17:21 -0400 | [diff] [blame] | 105 | ERRORF(r, "SkImage::NewFromGenerator unexpecedly failed [%zu]", i); |
aleksandar.stojiljkovic | 07e2692 | 2015-11-10 04:55:15 -0800 | [diff] [blame] | 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()); |
halcanary | ea4673f | 2014-08-18 08:27:09 -0700 | [diff] [blame] | 111 | |
aleksandar.stojiljkovic | 07e2692 | 2015-11-10 04:55:15 -0800 | [diff] [blame] | 112 | SkBitmap bitmap; |
| 113 | bitmap.allocN32Pixels(TestImageGenerator::Width(), TestImageGenerator::Height()); |
| 114 | SkCanvas canvas(bitmap); |
| 115 | const SkColor kDefaultColor = 0xffabcdef; |
| 116 | canvas.clear(kDefaultColor); |
Mike Reed | 039f136 | 2021-01-27 21:21:08 -0500 | [diff] [blame] | 117 | canvas.drawImage(image, 0, 0); |
aleksandar.stojiljkovic | 07e2692 | 2015-11-10 04:55:15 -0800 | [diff] [blame] | 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 | } |
halcanary | ea4673f | 2014-08-18 08:27:09 -0700 | [diff] [blame] | 125 | } |
| 126 | } |
| 127 | } |