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