blob: 3439a5e09d0a3e6c98883c28db81abdb0cb29ea9 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
2 * Copyright 2011 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 */
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +00007
reed@android.com78cd1052010-03-08 20:23:57 +00008#include "SkBitmap.h"
9#include "SkCanvas.h"
10#include "SkColorPriv.h"
reed@android.com9f8f48e2010-03-15 20:48:02 +000011#include "SkGradientShader.h"
reed@android.com78cd1052010-03-08 20:23:57 +000012#include "SkRect.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000013#include "Test.h"
reed@android.com78cd1052010-03-08 20:23:57 +000014
Hal Canarydb683012016-11-23 08:55:18 -070015#include "sk_tool_utils.h"
16
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +000017// these are in the same order as the SkColorType enum
commit-bot@chromium.org8ef51b92014-03-05 13:43:15 +000018static const char* gColorTypeName[] = {
19 "None", "A8", "565", "4444", "RGBA", "BGRA", "Index8"
reed@android.com78cd1052010-03-08 20:23:57 +000020};
21
22/** Returns -1 on success, else the x coord of the first bad pixel, return its
23 value in bad
24 */
25typedef int (*Proc)(const void*, int width, uint32_t expected, uint32_t* bad);
26
27static int proc_32(const void* ptr, int w, uint32_t expected, uint32_t* bad) {
28 const SkPMColor* addr = static_cast<const SkPMColor*>(ptr);
29 for (int x = 0; x < w; x++) {
30 if (addr[x] != expected) {
31 *bad = addr[x];
32 return x;
33 }
34 }
35 return -1;
36}
37
38static int proc_16(const void* ptr, int w, uint32_t expected, uint32_t* bad) {
39 const uint16_t* addr = static_cast<const uint16_t*>(ptr);
40 for (int x = 0; x < w; x++) {
41 if (addr[x] != expected) {
42 *bad = addr[x];
43 return x;
44 }
45 }
46 return -1;
47}
48
49static int proc_8(const void* ptr, int w, uint32_t expected, uint32_t* bad) {
50 const SkPMColor* addr = static_cast<const SkPMColor*>(ptr);
51 for (int x = 0; x < w; x++) {
52 if (SkGetPackedA32(addr[x]) != expected) {
53 *bad = SkGetPackedA32(addr[x]);
54 return x;
55 }
56 }
57 return -1;
58}
59
sugoi@google.com54f0d1b2013-02-27 19:17:41 +000060static int proc_bad(const void*, int, uint32_t, uint32_t* bad) {
reed@android.com78cd1052010-03-08 20:23:57 +000061 *bad = 0;
62 return 0;
63}
64
65static Proc find_proc(const SkBitmap& bm, SkPMColor expect32, uint16_t expect16,
66 uint8_t expect8, uint32_t* expect) {
commit-bot@chromium.org8ef51b92014-03-05 13:43:15 +000067 switch (bm.colorType()) {
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +000068 case kN32_SkColorType:
reed@android.com78cd1052010-03-08 20:23:57 +000069 *expect = expect32;
70 return proc_32;
commit-bot@chromium.org8ef51b92014-03-05 13:43:15 +000071 case kARGB_4444_SkColorType:
72 case kRGB_565_SkColorType:
reed@android.com78cd1052010-03-08 20:23:57 +000073 *expect = expect16;
74 return proc_16;
commit-bot@chromium.org8ef51b92014-03-05 13:43:15 +000075 case kAlpha_8_SkColorType:
reed@android.com78cd1052010-03-08 20:23:57 +000076 *expect = expect8;
77 return proc_8;
78 default:
79 *expect = 0;
80 return proc_bad;
81 }
82}
83
84static bool check_color(const SkBitmap& bm, SkPMColor expect32,
85 uint16_t expect16, uint8_t expect8,
86 skiatest::Reporter* reporter) {
87 uint32_t expect;
88 Proc proc = find_proc(bm, expect32, expect16, expect8, &expect);
89 for (int y = 0; y < bm.height(); y++) {
90 uint32_t bad;
91 int x = proc(bm.getAddr(0, y), bm.width(), expect, &bad);
92 if (x >= 0) {
commit-bot@chromium.org8ef51b92014-03-05 13:43:15 +000093 ERRORF(reporter, "BlitRow colortype=%s [%d %d] expected %x got %x",
94 gColorTypeName[bm.colorType()], x, y, expect, bad);
reed@android.com78cd1052010-03-08 20:23:57 +000095 return false;
96 }
97 }
98 return true;
99}
100
reed@android.com9f8f48e2010-03-15 20:48:02 +0000101// Make sure our blits always map src==0 to a noop, and src==FF to full opaque
102static void test_00_FF(skiatest::Reporter* reporter) {
reed@android.com78cd1052010-03-08 20:23:57 +0000103 static const int W = 256;
104
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000105 static const SkColorType gDstColorType[] = {
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000106 kN32_SkColorType,
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000107 kRGB_565_SkColorType,
reed@android.com78cd1052010-03-08 20:23:57 +0000108 };
109
110 static const struct {
111 SkColor fSrc;
112 SkColor fDst;
113 SkPMColor fResult32;
114 uint16_t fResult16;
115 uint8_t fResult8;
116 } gSrcRec[] = {
117 { 0, 0, 0, 0, 0 },
118 { 0, 0xFFFFFFFF, SkPackARGB32(0xFF, 0xFF, 0xFF, 0xFF), 0xFFFF, 0xFF },
119 { 0xFFFFFFFF, 0, SkPackARGB32(0xFF, 0xFF, 0xFF, 0xFF), 0xFFFF, 0xFF },
120 { 0xFFFFFFFF, 0xFFFFFFFF, SkPackARGB32(0xFF, 0xFF, 0xFF, 0xFF), 0xFFFF, 0xFF },
121 };
reed@android.com1db89dc2010-03-08 22:00:55 +0000122
123 SkPaint paint;
reed@android.com1db89dc2010-03-08 22:00:55 +0000124
reed@android.com78cd1052010-03-08 20:23:57 +0000125 SkBitmap srcBM;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000126 srcBM.allocN32Pixels(W, 1);
reed@android.com9f8f48e2010-03-15 20:48:02 +0000127
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000128 for (size_t i = 0; i < SK_ARRAY_COUNT(gDstColorType); i++) {
129 SkImageInfo info = SkImageInfo::Make(W, 1, gDstColorType[i],
130 kPremul_SkAlphaType);
reed@android.com78cd1052010-03-08 20:23:57 +0000131 SkBitmap dstBM;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000132 dstBM.allocPixels(info);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000133
reed@android.com78cd1052010-03-08 20:23:57 +0000134 SkCanvas canvas(dstBM);
135 for (size_t j = 0; j < SK_ARRAY_COUNT(gSrcRec); j++) {
136 srcBM.eraseColor(gSrcRec[j].fSrc);
137 dstBM.eraseColor(gSrcRec[j].fDst);
138
reed@android.com1db89dc2010-03-08 22:00:55 +0000139 for (int k = 0; k < 4; k++) {
140 bool dither = (k & 1) != 0;
141 bool blend = (k & 2) != 0;
142 if (gSrcRec[j].fSrc != 0 && blend) {
143 // can't make a numerical promise about blending anything
144 // but 0
reed@android.com8e4c93b2010-03-09 15:21:28 +0000145 // continue;
reed@android.com1db89dc2010-03-08 22:00:55 +0000146 }
147 paint.setDither(dither);
148 paint.setAlpha(blend ? 0x80 : 0xFF);
149 canvas.drawBitmap(srcBM, 0, 0, &paint);
150 if (!check_color(dstBM, gSrcRec[j].fResult32, gSrcRec[j].fResult16,
151 gSrcRec[j].fResult8, reporter)) {
152 SkDebugf("--- src index %d dither %d blend %d\n", j, dither, blend);
153 }
reed@android.com78cd1052010-03-08 20:23:57 +0000154 }
155 }
156 }
157}
158
reed@android.com9f8f48e2010-03-15 20:48:02 +0000159///////////////////////////////////////////////////////////////////////////////
160
161struct Mesh {
162 SkPoint fPts[4];
reed@android.com9f8f48e2010-03-15 20:48:02 +0000163
164 Mesh(const SkBitmap& bm, SkPaint* paint) {
165 const SkScalar w = SkIntToScalar(bm.width());
166 const SkScalar h = SkIntToScalar(bm.height());
167 fPts[0].set(0, 0);
168 fPts[1].set(w, 0);
169 fPts[2].set(w, h);
170 fPts[3].set(0, h);
reed1a9b9642016-03-13 14:13:58 -0700171 paint->setShader(SkShader::MakeBitmapShader(bm, SkShader::kClamp_TileMode,
172 SkShader::kClamp_TileMode));
reed@android.com9f8f48e2010-03-15 20:48:02 +0000173 }
174
175 void draw(SkCanvas* canvas, SkPaint* paint) {
176 canvas->drawVertices(SkCanvas::kTriangleFan_VertexMode, 4, fPts, fPts,
Mike Reed7d954ad2016-10-28 15:42:34 -0400177 nullptr, SkBlendMode::kModulate, nullptr, 0, *paint);
reed@android.com9f8f48e2010-03-15 20:48:02 +0000178 }
179};
180
181#include "SkImageEncoder.h"
182static void save_bm(const SkBitmap& bm, const char name[]) {
Hal Canarydb683012016-11-23 08:55:18 -0700183 sk_tool_utils::EncodeImageToFile(name, bm, SkEncodedImageFormat::kPNG, 100);
reed@android.com9f8f48e2010-03-15 20:48:02 +0000184}
185
186static bool gOnce;
187
188// Make sure our blits are invariant with the width of the blit (i.e. that
189// special case for 8 at a time have the same results as narrower blits)
190static void test_diagonal(skiatest::Reporter* reporter) {
191 static const int W = 64;
192 static const int H = W;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000193
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000194 static const SkColorType gDstColorType[] = {
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000195 kN32_SkColorType,
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000196 kRGB_565_SkColorType,
reed@android.com9f8f48e2010-03-15 20:48:02 +0000197 };
198
199 static const SkColor gDstBG[] = { 0, 0xFFFFFFFF };
reed320a40d2016-08-02 06:12:06 -0700200 const SkRect srcR = SkRect::MakeIWH(W, H);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000201
reed@android.com9f8f48e2010-03-15 20:48:02 +0000202 SkBitmap srcBM;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000203 srcBM.allocN32Pixels(W, H);
reed320a40d2016-08-02 06:12:06 -0700204 SkImageInfo info = SkImageInfo::Make(W, H, kUnknown_SkColorType, kPremul_SkAlphaType);
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000205
206 for (size_t i = 0; i < SK_ARRAY_COUNT(gDstColorType); i++) {
reede5ea5002014-09-03 11:54:58 -0700207 info = info.makeColorType(gDstColorType[i]);
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000208
reed@android.com9f8f48e2010-03-15 20:48:02 +0000209 SkBitmap dstBM0, dstBM1;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000210 dstBM0.allocPixels(info);
211 dstBM1.allocPixels(info);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000212
reed@android.com9f8f48e2010-03-15 20:48:02 +0000213 SkCanvas canvas0(dstBM0);
214 SkCanvas canvas1(dstBM1);
215 SkColor bgColor;
216
217 for (size_t j = 0; j < SK_ARRAY_COUNT(gDstBG); j++) {
218 bgColor = gDstBG[j];
219
220 for (int c = 0; c <= 0xFF; c++) {
reed320a40d2016-08-02 06:12:06 -0700221 // cons up a mesh to draw the bitmap with
222 SkPaint paint;
reed@android.com9f8f48e2010-03-15 20:48:02 +0000223 srcBM.eraseARGB(0xFF, c, c, c);
reed320a40d2016-08-02 06:12:06 -0700224 Mesh mesh(srcBM, &paint);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000225
reed@android.com9f8f48e2010-03-15 20:48:02 +0000226 for (int k = 0; k < 4; k++) {
227 bool dither = (k & 1) != 0;
228 uint8_t alpha = (k & 2) ? 0x80 : 0xFF;
229 paint.setDither(dither);
230 paint.setAlpha(alpha);
231
232 dstBM0.eraseColor(bgColor);
233 dstBM1.eraseColor(bgColor);
234
235 canvas0.drawRect(srcR, paint);
236 mesh.draw(&canvas1, &paint);
237
238 if (!gOnce && false) {
239 save_bm(dstBM0, "drawBitmap.png");
240 save_bm(dstBM1, "drawMesh.png");
241 gOnce = true;
242 }
243
244 if (memcmp(dstBM0.getPixels(), dstBM1.getPixels(), dstBM0.getSize())) {
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000245 ERRORF(reporter, "Diagonal colortype=%s bg=0x%x dither=%d"
halcanary@google.coma9325fa2014-01-10 14:58:10 +0000246 " alpha=0x%x src=0x%x",
commit-bot@chromium.org8ef51b92014-03-05 13:43:15 +0000247 gColorTypeName[gDstColorType[i]], bgColor, dither,
halcanary@google.coma9325fa2014-01-10 14:58:10 +0000248 alpha, c);
reed@android.com9f8f48e2010-03-15 20:48:02 +0000249 }
250 }
251 }
252 }
253 }
254}
255
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000256DEF_TEST(BlitRow, reporter) {
reed@android.com9f8f48e2010-03-15 20:48:02 +0000257 test_00_FF(reporter);
258 test_diagonal(reporter);
259}