blob: c42344178eba86445f76ee54e2772433b14a3895 [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
8#include "SkBitmap.h"
9#include "SkCanvas.h"
10#include "SkData.h"
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000011#include "SkDiscardableMemoryPool.h"
msarett7f7ec202016-03-01 12:12:27 -080012#include "SkImage.h"
13#include "SkImageEncoder.h"
Mike Reed76147942016-10-25 09:57:13 -040014#include "SkImageGenerator.h"
Mike Reed185130c2017-02-15 15:14:16 -050015#include "SkMakeUnique.h"
reed011f39a2014-08-28 13:35:23 -070016#include "SkResourceCache.h"
commit-bot@chromium.org75854792013-10-29 19:55:00 +000017#include "SkStream.h"
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000018#include "SkUtils.h"
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +000019
commit-bot@chromium.org75854792013-10-29 19:55:00 +000020#include "Test.h"
21
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000022class TestImageGenerator : public SkImageGenerator {
23public:
24 enum TestType {
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000025 kFailGetPixels_TestType,
26 kSucceedGetPixels_TestType,
27 kLast_TestType = kSucceedGetPixels_TestType
28 };
29 static int Width() { return 10; }
30 static int Height() { return 10; }
aleksandar.stojiljkovic07e26922015-11-10 04:55:15 -080031 // value choosen so that there is no loss when converting to to RGB565 and back
32 static SkColor Color() { return 0xff10345a; }
33 static SkPMColor PMColor() { return SkPreMultiplyColor(Color()); }
34
35 TestImageGenerator(TestType type, skiatest::Reporter* reporter,
36 SkColorType colorType = kN32_SkColorType)
37 : INHERITED(GetMyInfo(colorType)), fType(type), fReporter(reporter) {
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000038 SkASSERT((fType <= kLast_TestType) && (fType >= 0));
39 }
Brian Salomond3b65972017-03-22 12:05:03 -040040 ~TestImageGenerator() override {}
commit-bot@chromium.org00f8d6c2014-05-29 15:57:20 +000041
42protected:
aleksandar.stojiljkovic07e26922015-11-10 04:55:15 -080043 static SkImageInfo GetMyInfo(SkColorType colorType) {
44 return SkImageInfo::Make(TestImageGenerator::Width(), TestImageGenerator::Height(),
45 colorType, kOpaque_SkAlphaType);
reed3ef71e32015-03-19 08:31:14 -070046 }
47
scroggo5315fd42015-07-09 09:08:00 -070048 bool onGetPixels(const SkImageInfo& info, void* pixels, size_t rowBytes,
49 SkPMColor ctable[], int* ctableCount) override {
halcanary96fcdcc2015-08-27 07:41:13 -070050 REPORTER_ASSERT(fReporter, pixels != nullptr);
scroggo08649082015-02-13 11:13:34 -080051 REPORTER_ASSERT(fReporter, rowBytes >= info.minRowBytes());
52 if (fType != kSucceedGetPixels_TestType) {
scroggo5315fd42015-07-09 09:08:00 -070053 return false;
scroggo08649082015-02-13 11:13:34 -080054 }
aleksandar.stojiljkovic07e26922015-11-10 04:55:15 -080055 if (info.colorType() != kN32_SkColorType && info.colorType() != getInfo().colorType()) {
scroggo5315fd42015-07-09 09:08:00 -070056 return false;
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000057 }
58 char* bytePtr = static_cast<char*>(pixels);
aleksandar.stojiljkovic07e26922015-11-10 04:55:15 -080059 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 kIndex_8_SkColorType:
68 *ctableCount = 1;
69 ctable[0] = TestImageGenerator::PMColor();
70 for (int y = 0; y < info.height(); ++y) {
71 memset(bytePtr, 0, info.width());
72 bytePtr += rowBytes;
73 }
74 break;
75 case kRGB_565_SkColorType:
76 for (int y = 0; y < info.height(); ++y) {
77 sk_memset16((uint16_t*)bytePtr,
78 SkPixel32ToPixel16(TestImageGenerator::PMColor()), info.width());
79 bytePtr += rowBytes;
80 }
81 break;
82 default:
83 return false;
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000084 }
scroggo5315fd42015-07-09 09:08:00 -070085 return true;
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000086 }
halcanary@google.com3d50ea12014-01-02 13:15:13 +000087
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000088private:
89 const TestType fType;
90 skiatest::Reporter* const fReporter;
reed3ef71e32015-03-19 08:31:14 -070091
92 typedef SkImageGenerator INHERITED;
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000093};
halcanary@google.com3d50ea12014-01-02 13:15:13 +000094
halcanaryea4673f2014-08-18 08:27:09 -070095////////////////////////////////////////////////////////////////////////////////
96
97DEF_TEST(Image_NewFromGenerator, r) {
aleksandar.stojiljkovic07e26922015-11-10 04:55:15 -080098 const TestImageGenerator::TestType testTypes[] = {
halcanaryea4673f2014-08-18 08:27:09 -070099 TestImageGenerator::kFailGetPixels_TestType,
100 TestImageGenerator::kSucceedGetPixels_TestType,
101 };
aleksandar.stojiljkovic07e26922015-11-10 04:55:15 -0800102 const SkColorType testColorTypes[] = {
103 kN32_SkColorType,
104 kIndex_8_SkColorType,
105 kRGB_565_SkColorType
106 };
halcanaryea4673f2014-08-18 08:27:09 -0700107 for (size_t i = 0; i < SK_ARRAY_COUNT(testTypes); ++i) {
108 TestImageGenerator::TestType test = testTypes[i];
aleksandar.stojiljkovic07e26922015-11-10 04:55:15 -0800109 for (const SkColorType testColorType : testColorTypes) {
Mike Reed185130c2017-02-15 15:14:16 -0500110 auto gen = skstd::make_unique<TestImageGenerator>(test, r, testColorType);
111 sk_sp<SkImage> image(SkImage::MakeFromGenerator(std::move(gen)));
reed9ce9d672016-03-17 10:51:11 -0700112 if (nullptr == image) {
aleksandar.stojiljkovic07e26922015-11-10 04:55:15 -0800113 ERRORF(r, "SkImage::NewFromGenerator unexpecedly failed ["
114 SK_SIZE_T_SPECIFIER "]", i);
115 continue;
116 }
117 REPORTER_ASSERT(r, TestImageGenerator::Width() == image->width());
118 REPORTER_ASSERT(r, TestImageGenerator::Height() == image->height());
119 REPORTER_ASSERT(r, image->isLazyGenerated());
halcanaryea4673f2014-08-18 08:27:09 -0700120
aleksandar.stojiljkovic07e26922015-11-10 04:55:15 -0800121 SkBitmap bitmap;
122 bitmap.allocN32Pixels(TestImageGenerator::Width(), TestImageGenerator::Height());
123 SkCanvas canvas(bitmap);
124 const SkColor kDefaultColor = 0xffabcdef;
125 canvas.clear(kDefaultColor);
126 canvas.drawImage(image, 0, 0, nullptr);
127 if (TestImageGenerator::kSucceedGetPixels_TestType == test) {
128 REPORTER_ASSERT(
129 r, TestImageGenerator::Color() == bitmap.getColor(0, 0));
130 }
131 else {
132 REPORTER_ASSERT(r, kDefaultColor == bitmap.getColor(0, 0));
133 }
halcanaryea4673f2014-08-18 08:27:09 -0700134 }
135 }
136}