blob: b65765f96cb6b0079ac3399f03479667a02364c0 [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"
reed011f39a2014-08-28 13:35:23 -070015#include "SkResourceCache.h"
commit-bot@chromium.org75854792013-10-29 19:55:00 +000016#include "SkStream.h"
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000017#include "SkUtils.h"
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +000018
commit-bot@chromium.org75854792013-10-29 19:55:00 +000019#include "Test.h"
20
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000021class TestImageGenerator : public SkImageGenerator {
22public:
23 enum TestType {
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000024 kFailGetPixels_TestType,
25 kSucceedGetPixels_TestType,
26 kLast_TestType = kSucceedGetPixels_TestType
27 };
28 static int Width() { return 10; }
29 static int Height() { return 10; }
aleksandar.stojiljkovic07e26922015-11-10 04:55:15 -080030 // value choosen so that there is no loss when converting to to RGB565 and back
31 static SkColor Color() { return 0xff10345a; }
32 static SkPMColor PMColor() { return SkPreMultiplyColor(Color()); }
33
34 TestImageGenerator(TestType type, skiatest::Reporter* reporter,
35 SkColorType colorType = kN32_SkColorType)
36 : INHERITED(GetMyInfo(colorType)), fType(type), fReporter(reporter) {
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000037 SkASSERT((fType <= kLast_TestType) && (fType >= 0));
38 }
tfarina@chromium.org58674812014-01-21 23:39:22 +000039 virtual ~TestImageGenerator() { }
commit-bot@chromium.org00f8d6c2014-05-29 15:57:20 +000040
41protected:
aleksandar.stojiljkovic07e26922015-11-10 04:55:15 -080042 static SkImageInfo GetMyInfo(SkColorType colorType) {
43 return SkImageInfo::Make(TestImageGenerator::Width(), TestImageGenerator::Height(),
44 colorType, kOpaque_SkAlphaType);
reed3ef71e32015-03-19 08:31:14 -070045 }
46
scroggo5315fd42015-07-09 09:08:00 -070047 bool onGetPixels(const SkImageInfo& info, void* pixels, size_t rowBytes,
48 SkPMColor ctable[], int* ctableCount) override {
halcanary96fcdcc2015-08-27 07:41:13 -070049 REPORTER_ASSERT(fReporter, pixels != nullptr);
scroggo08649082015-02-13 11:13:34 -080050 REPORTER_ASSERT(fReporter, rowBytes >= info.minRowBytes());
51 if (fType != kSucceedGetPixels_TestType) {
scroggo5315fd42015-07-09 09:08:00 -070052 return false;
scroggo08649082015-02-13 11:13:34 -080053 }
aleksandar.stojiljkovic07e26922015-11-10 04:55:15 -080054 if (info.colorType() != kN32_SkColorType && info.colorType() != getInfo().colorType()) {
scroggo5315fd42015-07-09 09:08:00 -070055 return false;
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000056 }
57 char* bytePtr = static_cast<char*>(pixels);
aleksandar.stojiljkovic07e26922015-11-10 04:55:15 -080058 switch (info.colorType()) {
59 case kN32_SkColorType:
60 for (int y = 0; y < info.height(); ++y) {
61 sk_memset32((uint32_t*)bytePtr,
62 TestImageGenerator::PMColor(), info.width());
63 bytePtr += rowBytes;
64 }
65 break;
66 case kIndex_8_SkColorType:
67 *ctableCount = 1;
68 ctable[0] = TestImageGenerator::PMColor();
69 for (int y = 0; y < info.height(); ++y) {
70 memset(bytePtr, 0, info.width());
71 bytePtr += rowBytes;
72 }
73 break;
74 case kRGB_565_SkColorType:
75 for (int y = 0; y < info.height(); ++y) {
76 sk_memset16((uint16_t*)bytePtr,
77 SkPixel32ToPixel16(TestImageGenerator::PMColor()), info.width());
78 bytePtr += rowBytes;
79 }
80 break;
81 default:
82 return false;
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000083 }
scroggo5315fd42015-07-09 09:08:00 -070084 return true;
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000085 }
halcanary@google.com3d50ea12014-01-02 13:15:13 +000086
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000087private:
88 const TestType fType;
89 skiatest::Reporter* const fReporter;
reed3ef71e32015-03-19 08:31:14 -070090
91 typedef SkImageGenerator INHERITED;
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000092};
halcanary@google.com3d50ea12014-01-02 13:15:13 +000093
halcanaryea4673f2014-08-18 08:27:09 -070094////////////////////////////////////////////////////////////////////////////////
95
96DEF_TEST(Image_NewFromGenerator, r) {
aleksandar.stojiljkovic07e26922015-11-10 04:55:15 -080097 const TestImageGenerator::TestType testTypes[] = {
halcanaryea4673f2014-08-18 08:27:09 -070098 TestImageGenerator::kFailGetPixels_TestType,
99 TestImageGenerator::kSucceedGetPixels_TestType,
100 };
aleksandar.stojiljkovic07e26922015-11-10 04:55:15 -0800101 const SkColorType testColorTypes[] = {
102 kN32_SkColorType,
103 kIndex_8_SkColorType,
104 kRGB_565_SkColorType
105 };
halcanaryea4673f2014-08-18 08:27:09 -0700106 for (size_t i = 0; i < SK_ARRAY_COUNT(testTypes); ++i) {
107 TestImageGenerator::TestType test = testTypes[i];
aleksandar.stojiljkovic07e26922015-11-10 04:55:15 -0800108 for (const SkColorType testColorType : testColorTypes) {
109 SkImageGenerator* gen = new TestImageGenerator(test, r, testColorType);
reed9ce9d672016-03-17 10:51:11 -0700110 sk_sp<SkImage> image(SkImage::MakeFromGenerator(gen));
111 if (nullptr == image) {
aleksandar.stojiljkovic07e26922015-11-10 04:55:15 -0800112 ERRORF(r, "SkImage::NewFromGenerator unexpecedly failed ["
113 SK_SIZE_T_SPECIFIER "]", i);
114 continue;
115 }
116 REPORTER_ASSERT(r, TestImageGenerator::Width() == image->width());
117 REPORTER_ASSERT(r, TestImageGenerator::Height() == image->height());
118 REPORTER_ASSERT(r, image->isLazyGenerated());
halcanaryea4673f2014-08-18 08:27:09 -0700119
aleksandar.stojiljkovic07e26922015-11-10 04:55:15 -0800120 SkBitmap bitmap;
121 bitmap.allocN32Pixels(TestImageGenerator::Width(), TestImageGenerator::Height());
122 SkCanvas canvas(bitmap);
123 const SkColor kDefaultColor = 0xffabcdef;
124 canvas.clear(kDefaultColor);
125 canvas.drawImage(image, 0, 0, nullptr);
126 if (TestImageGenerator::kSucceedGetPixels_TestType == test) {
127 REPORTER_ASSERT(
128 r, TestImageGenerator::Color() == bitmap.getColor(0, 0));
129 }
130 else {
131 REPORTER_ASSERT(r, kDefaultColor == bitmap.getColor(0, 0));
132 }
halcanaryea4673f2014-08-18 08:27:09 -0700133 }
134 }
135}