blob: d2f333dbb6e3e15c2b61d1e6a518d34952abfd7f [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"
Mike Reed887cdf12017-04-03 11:11:09 -040013#include "SkVertices.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000014#include "Test.h"
reed@android.com78cd1052010-03-08 20:23:57 +000015
Hal Canarydb683012016-11-23 08:55:18 -070016#include "sk_tool_utils.h"
17
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +000018// these are in the same order as the SkColorType enum
commit-bot@chromium.org8ef51b92014-03-05 13:43:15 +000019static const char* gColorTypeName[] = {
20 "None", "A8", "565", "4444", "RGBA", "BGRA", "Index8"
reed@android.com78cd1052010-03-08 20:23:57 +000021};
22
23/** Returns -1 on success, else the x coord of the first bad pixel, return its
24 value in bad
25 */
26typedef int (*Proc)(const void*, int width, uint32_t expected, uint32_t* bad);
27
28static int proc_32(const void* ptr, int w, uint32_t expected, uint32_t* bad) {
29 const SkPMColor* addr = static_cast<const SkPMColor*>(ptr);
30 for (int x = 0; x < w; x++) {
31 if (addr[x] != expected) {
32 *bad = addr[x];
33 return x;
34 }
35 }
36 return -1;
37}
38
39static int proc_16(const void* ptr, int w, uint32_t expected, uint32_t* bad) {
40 const uint16_t* addr = static_cast<const uint16_t*>(ptr);
41 for (int x = 0; x < w; x++) {
42 if (addr[x] != expected) {
43 *bad = addr[x];
44 return x;
45 }
46 }
47 return -1;
48}
49
50static int proc_8(const void* ptr, int w, uint32_t expected, uint32_t* bad) {
51 const SkPMColor* addr = static_cast<const SkPMColor*>(ptr);
52 for (int x = 0; x < w; x++) {
53 if (SkGetPackedA32(addr[x]) != expected) {
54 *bad = SkGetPackedA32(addr[x]);
55 return x;
56 }
57 }
58 return -1;
59}
60
sugoi@google.com54f0d1b2013-02-27 19:17:41 +000061static int proc_bad(const void*, int, uint32_t, uint32_t* bad) {
reed@android.com78cd1052010-03-08 20:23:57 +000062 *bad = 0;
63 return 0;
64}
65
66static Proc find_proc(const SkBitmap& bm, SkPMColor expect32, uint16_t expect16,
67 uint8_t expect8, uint32_t* expect) {
commit-bot@chromium.org8ef51b92014-03-05 13:43:15 +000068 switch (bm.colorType()) {
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +000069 case kN32_SkColorType:
reed@android.com78cd1052010-03-08 20:23:57 +000070 *expect = expect32;
71 return proc_32;
commit-bot@chromium.org8ef51b92014-03-05 13:43:15 +000072 case kARGB_4444_SkColorType:
73 case kRGB_565_SkColorType:
reed@android.com78cd1052010-03-08 20:23:57 +000074 *expect = expect16;
75 return proc_16;
commit-bot@chromium.org8ef51b92014-03-05 13:43:15 +000076 case kAlpha_8_SkColorType:
reed@android.com78cd1052010-03-08 20:23:57 +000077 *expect = expect8;
78 return proc_8;
79 default:
80 *expect = 0;
81 return proc_bad;
82 }
83}
84
85static bool check_color(const SkBitmap& bm, SkPMColor expect32,
86 uint16_t expect16, uint8_t expect8,
87 skiatest::Reporter* reporter) {
88 uint32_t expect;
89 Proc proc = find_proc(bm, expect32, expect16, expect8, &expect);
90 for (int y = 0; y < bm.height(); y++) {
91 uint32_t bad;
92 int x = proc(bm.getAddr(0, y), bm.width(), expect, &bad);
93 if (x >= 0) {
commit-bot@chromium.org8ef51b92014-03-05 13:43:15 +000094 ERRORF(reporter, "BlitRow colortype=%s [%d %d] expected %x got %x",
95 gColorTypeName[bm.colorType()], x, y, expect, bad);
reed@android.com78cd1052010-03-08 20:23:57 +000096 return false;
97 }
98 }
99 return true;
100}
101
reed@android.com9f8f48e2010-03-15 20:48:02 +0000102// Make sure our blits always map src==0 to a noop, and src==FF to full opaque
103static void test_00_FF(skiatest::Reporter* reporter) {
reed@android.com78cd1052010-03-08 20:23:57 +0000104 static const int W = 256;
105
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000106 static const SkColorType gDstColorType[] = {
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000107 kN32_SkColorType,
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000108 kRGB_565_SkColorType,
reed@android.com78cd1052010-03-08 20:23:57 +0000109 };
110
111 static const struct {
112 SkColor fSrc;
113 SkColor fDst;
114 SkPMColor fResult32;
115 uint16_t fResult16;
116 uint8_t fResult8;
117 } gSrcRec[] = {
118 { 0, 0, 0, 0, 0 },
119 { 0, 0xFFFFFFFF, SkPackARGB32(0xFF, 0xFF, 0xFF, 0xFF), 0xFFFF, 0xFF },
120 { 0xFFFFFFFF, 0, SkPackARGB32(0xFF, 0xFF, 0xFF, 0xFF), 0xFFFF, 0xFF },
121 { 0xFFFFFFFF, 0xFFFFFFFF, SkPackARGB32(0xFF, 0xFF, 0xFF, 0xFF), 0xFFFF, 0xFF },
122 };
reed@android.com1db89dc2010-03-08 22:00:55 +0000123
124 SkPaint paint;
reed@android.com1db89dc2010-03-08 22:00:55 +0000125
reed@android.com78cd1052010-03-08 20:23:57 +0000126 SkBitmap srcBM;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000127 srcBM.allocN32Pixels(W, 1);
reed@android.com9f8f48e2010-03-15 20:48:02 +0000128
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000129 for (size_t i = 0; i < SK_ARRAY_COUNT(gDstColorType); i++) {
130 SkImageInfo info = SkImageInfo::Make(W, 1, gDstColorType[i],
131 kPremul_SkAlphaType);
reed@android.com78cd1052010-03-08 20:23:57 +0000132 SkBitmap dstBM;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000133 dstBM.allocPixels(info);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000134
reed@android.com78cd1052010-03-08 20:23:57 +0000135 SkCanvas canvas(dstBM);
136 for (size_t j = 0; j < SK_ARRAY_COUNT(gSrcRec); j++) {
137 srcBM.eraseColor(gSrcRec[j].fSrc);
138 dstBM.eraseColor(gSrcRec[j].fDst);
139
reed@android.com1db89dc2010-03-08 22:00:55 +0000140 for (int k = 0; k < 4; k++) {
141 bool dither = (k & 1) != 0;
142 bool blend = (k & 2) != 0;
143 if (gSrcRec[j].fSrc != 0 && blend) {
144 // can't make a numerical promise about blending anything
145 // but 0
reed@android.com8e4c93b2010-03-09 15:21:28 +0000146 // continue;
reed@android.com1db89dc2010-03-08 22:00:55 +0000147 }
148 paint.setDither(dither);
149 paint.setAlpha(blend ? 0x80 : 0xFF);
150 canvas.drawBitmap(srcBM, 0, 0, &paint);
151 if (!check_color(dstBM, gSrcRec[j].fResult32, gSrcRec[j].fResult16,
152 gSrcRec[j].fResult8, reporter)) {
153 SkDebugf("--- src index %d dither %d blend %d\n", j, dither, blend);
154 }
reed@android.com78cd1052010-03-08 20:23:57 +0000155 }
156 }
157 }
158}
159
reed@android.com9f8f48e2010-03-15 20:48:02 +0000160///////////////////////////////////////////////////////////////////////////////
161
162struct Mesh {
163 SkPoint fPts[4];
reed@android.com9f8f48e2010-03-15 20:48:02 +0000164
165 Mesh(const SkBitmap& bm, SkPaint* paint) {
166 const SkScalar w = SkIntToScalar(bm.width());
167 const SkScalar h = SkIntToScalar(bm.height());
168 fPts[0].set(0, 0);
169 fPts[1].set(w, 0);
170 fPts[2].set(w, h);
171 fPts[3].set(0, h);
reed1a9b9642016-03-13 14:13:58 -0700172 paint->setShader(SkShader::MakeBitmapShader(bm, SkShader::kClamp_TileMode,
173 SkShader::kClamp_TileMode));
reed@android.com9f8f48e2010-03-15 20:48:02 +0000174 }
175
176 void draw(SkCanvas* canvas, SkPaint* paint) {
Mike Reed887cdf12017-04-03 11:11:09 -0400177 canvas->drawVertices(SkVertices::MakeCopy(SkVertices::kTriangleFan_VertexMode, 4, fPts,
178 fPts, nullptr),
179 SkBlendMode::kModulate, *paint);
reed@android.com9f8f48e2010-03-15 20:48:02 +0000180 }
181};
182
183#include "SkImageEncoder.h"
184static void save_bm(const SkBitmap& bm, const char name[]) {
Hal Canarydb683012016-11-23 08:55:18 -0700185 sk_tool_utils::EncodeImageToFile(name, bm, SkEncodedImageFormat::kPNG, 100);
reed@android.com9f8f48e2010-03-15 20:48:02 +0000186}
187
188static bool gOnce;
189
190// Make sure our blits are invariant with the width of the blit (i.e. that
191// special case for 8 at a time have the same results as narrower blits)
192static void test_diagonal(skiatest::Reporter* reporter) {
193 static const int W = 64;
194 static const int H = W;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000195
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000196 static const SkColorType gDstColorType[] = {
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000197 kN32_SkColorType,
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000198 kRGB_565_SkColorType,
reed@android.com9f8f48e2010-03-15 20:48:02 +0000199 };
200
201 static const SkColor gDstBG[] = { 0, 0xFFFFFFFF };
reed320a40d2016-08-02 06:12:06 -0700202 const SkRect srcR = SkRect::MakeIWH(W, H);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000203
reed@android.com9f8f48e2010-03-15 20:48:02 +0000204 SkBitmap srcBM;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000205 srcBM.allocN32Pixels(W, H);
reed320a40d2016-08-02 06:12:06 -0700206 SkImageInfo info = SkImageInfo::Make(W, H, kUnknown_SkColorType, kPremul_SkAlphaType);
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000207
208 for (size_t i = 0; i < SK_ARRAY_COUNT(gDstColorType); i++) {
reede5ea5002014-09-03 11:54:58 -0700209 info = info.makeColorType(gDstColorType[i]);
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000210
reed@android.com9f8f48e2010-03-15 20:48:02 +0000211 SkBitmap dstBM0, dstBM1;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000212 dstBM0.allocPixels(info);
213 dstBM1.allocPixels(info);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000214
reed@android.com9f8f48e2010-03-15 20:48:02 +0000215 SkCanvas canvas0(dstBM0);
216 SkCanvas canvas1(dstBM1);
217 SkColor bgColor;
218
219 for (size_t j = 0; j < SK_ARRAY_COUNT(gDstBG); j++) {
220 bgColor = gDstBG[j];
221
222 for (int c = 0; c <= 0xFF; c++) {
reed320a40d2016-08-02 06:12:06 -0700223 // cons up a mesh to draw the bitmap with
224 SkPaint paint;
reed@android.com9f8f48e2010-03-15 20:48:02 +0000225 srcBM.eraseARGB(0xFF, c, c, c);
reed320a40d2016-08-02 06:12:06 -0700226 Mesh mesh(srcBM, &paint);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000227
reed@android.com9f8f48e2010-03-15 20:48:02 +0000228 for (int k = 0; k < 4; k++) {
229 bool dither = (k & 1) != 0;
230 uint8_t alpha = (k & 2) ? 0x80 : 0xFF;
231 paint.setDither(dither);
232 paint.setAlpha(alpha);
233
234 dstBM0.eraseColor(bgColor);
235 dstBM1.eraseColor(bgColor);
236
237 canvas0.drawRect(srcR, paint);
238 mesh.draw(&canvas1, &paint);
239
240 if (!gOnce && false) {
241 save_bm(dstBM0, "drawBitmap.png");
242 save_bm(dstBM1, "drawMesh.png");
243 gOnce = true;
244 }
245
246 if (memcmp(dstBM0.getPixels(), dstBM1.getPixels(), dstBM0.getSize())) {
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000247 ERRORF(reporter, "Diagonal colortype=%s bg=0x%x dither=%d"
halcanary@google.coma9325fa2014-01-10 14:58:10 +0000248 " alpha=0x%x src=0x%x",
commit-bot@chromium.org8ef51b92014-03-05 13:43:15 +0000249 gColorTypeName[gDstColorType[i]], bgColor, dither,
halcanary@google.coma9325fa2014-01-10 14:58:10 +0000250 alpha, c);
reed@android.com9f8f48e2010-03-15 20:48:02 +0000251 }
252 }
253 }
254 }
255 }
256}
257
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000258DEF_TEST(BlitRow, reporter) {
reed@android.com9f8f48e2010-03-15 20:48:02 +0000259 test_00_FF(reporter);
260 test_diagonal(reporter);
261}