blob: d0518bfb1d9bb38547cd13eecaa29c959043222c [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 "Test.h"
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +00009#include "TestClassDef.h"
reed@android.com78cd1052010-03-08 20:23:57 +000010#include "SkBitmap.h"
11#include "SkCanvas.h"
12#include "SkColorPriv.h"
reed@android.com9f8f48e2010-03-15 20:48:02 +000013#include "SkGradientShader.h"
reed@android.com78cd1052010-03-08 20:23:57 +000014#include "SkRect.h"
15
reed@android.com78cd1052010-03-08 20:23:57 +000016// these are in the same order as the SkBitmap::Config enum
17static const char* gConfigName[] = {
rmistry@google.comd6bab022013-12-02 13:50:38 +000018 "None", "A8", "Index8", "565", "4444", "8888"
reed@android.com78cd1052010-03-08 20:23:57 +000019};
20
21/** Returns -1 on success, else the x coord of the first bad pixel, return its
22 value in bad
23 */
24typedef int (*Proc)(const void*, int width, uint32_t expected, uint32_t* bad);
25
26static int proc_32(const void* ptr, int w, uint32_t expected, uint32_t* bad) {
27 const SkPMColor* addr = static_cast<const SkPMColor*>(ptr);
28 for (int x = 0; x < w; x++) {
29 if (addr[x] != expected) {
30 *bad = addr[x];
31 return x;
32 }
33 }
34 return -1;
35}
36
37static int proc_16(const void* ptr, int w, uint32_t expected, uint32_t* bad) {
38 const uint16_t* addr = static_cast<const uint16_t*>(ptr);
39 for (int x = 0; x < w; x++) {
40 if (addr[x] != expected) {
41 *bad = addr[x];
42 return x;
43 }
44 }
45 return -1;
46}
47
48static int proc_8(const void* ptr, int w, uint32_t expected, uint32_t* bad) {
49 const SkPMColor* addr = static_cast<const SkPMColor*>(ptr);
50 for (int x = 0; x < w; x++) {
51 if (SkGetPackedA32(addr[x]) != expected) {
52 *bad = SkGetPackedA32(addr[x]);
53 return x;
54 }
55 }
56 return -1;
57}
58
sugoi@google.com54f0d1b2013-02-27 19:17:41 +000059static int proc_bad(const void*, int, uint32_t, uint32_t* bad) {
reed@android.com78cd1052010-03-08 20:23:57 +000060 *bad = 0;
61 return 0;
62}
63
64static Proc find_proc(const SkBitmap& bm, SkPMColor expect32, uint16_t expect16,
65 uint8_t expect8, uint32_t* expect) {
66 switch (bm.config()) {
67 case SkBitmap::kARGB_8888_Config:
68 *expect = expect32;
69 return proc_32;
70 case SkBitmap::kARGB_4444_Config:
71 case SkBitmap::kRGB_565_Config:
72 *expect = expect16;
73 return proc_16;
74 case SkBitmap::kA8_Config:
75 *expect = expect8;
76 return proc_8;
77 default:
78 *expect = 0;
79 return proc_bad;
80 }
81}
82
83static bool check_color(const SkBitmap& bm, SkPMColor expect32,
84 uint16_t expect16, uint8_t expect8,
85 skiatest::Reporter* reporter) {
86 uint32_t expect;
87 Proc proc = find_proc(bm, expect32, expect16, expect8, &expect);
88 for (int y = 0; y < bm.height(); y++) {
89 uint32_t bad;
90 int x = proc(bm.getAddr(0, y), bm.width(), expect, &bad);
91 if (x >= 0) {
92 SkString str;
93 str.printf("BlitRow config=%s [%d %d] expected %x got %x",
94 gConfigName[bm.config()], x, y, expect, bad);
95 reporter->reportFailed(str);
96 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
106 static const SkBitmap::Config gDstConfig[] = {
107 SkBitmap::kARGB_8888_Config,
108 SkBitmap::kRGB_565_Config,
reed@android.com8e4c93b2010-03-09 15:21:28 +0000109// SkBitmap::kARGB_4444_Config,
reed@android.com78cd1052010-03-08 20:23:57 +0000110// SkBitmap::kA8_Config,
111 };
112
113 static const struct {
114 SkColor fSrc;
115 SkColor fDst;
116 SkPMColor fResult32;
117 uint16_t fResult16;
118 uint8_t fResult8;
119 } gSrcRec[] = {
120 { 0, 0, 0, 0, 0 },
121 { 0, 0xFFFFFFFF, SkPackARGB32(0xFF, 0xFF, 0xFF, 0xFF), 0xFFFF, 0xFF },
122 { 0xFFFFFFFF, 0, SkPackARGB32(0xFF, 0xFF, 0xFF, 0xFF), 0xFFFF, 0xFF },
123 { 0xFFFFFFFF, 0xFFFFFFFF, SkPackARGB32(0xFF, 0xFF, 0xFF, 0xFF), 0xFFFF, 0xFF },
124 };
reed@android.com1db89dc2010-03-08 22:00:55 +0000125
126 SkPaint paint;
reed@android.com1db89dc2010-03-08 22:00:55 +0000127
reed@android.com78cd1052010-03-08 20:23:57 +0000128 SkBitmap srcBM;
129 srcBM.setConfig(SkBitmap::kARGB_8888_Config, W, 1);
130 srcBM.allocPixels();
reed@android.com9f8f48e2010-03-15 20:48:02 +0000131
reed@android.com78cd1052010-03-08 20:23:57 +0000132 for (size_t i = 0; i < SK_ARRAY_COUNT(gDstConfig); i++) {
133 SkBitmap dstBM;
134 dstBM.setConfig(gDstConfig[i], W, 1);
135 dstBM.allocPixels();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000136
reed@android.com78cd1052010-03-08 20:23:57 +0000137 SkCanvas canvas(dstBM);
138 for (size_t j = 0; j < SK_ARRAY_COUNT(gSrcRec); j++) {
139 srcBM.eraseColor(gSrcRec[j].fSrc);
140 dstBM.eraseColor(gSrcRec[j].fDst);
141
reed@android.com1db89dc2010-03-08 22:00:55 +0000142 for (int k = 0; k < 4; k++) {
143 bool dither = (k & 1) != 0;
144 bool blend = (k & 2) != 0;
145 if (gSrcRec[j].fSrc != 0 && blend) {
146 // can't make a numerical promise about blending anything
147 // but 0
reed@android.com8e4c93b2010-03-09 15:21:28 +0000148 // continue;
reed@android.com1db89dc2010-03-08 22:00:55 +0000149 }
150 paint.setDither(dither);
151 paint.setAlpha(blend ? 0x80 : 0xFF);
152 canvas.drawBitmap(srcBM, 0, 0, &paint);
153 if (!check_color(dstBM, gSrcRec[j].fResult32, gSrcRec[j].fResult16,
154 gSrcRec[j].fResult8, reporter)) {
155 SkDebugf("--- src index %d dither %d blend %d\n", j, dither, blend);
156 }
reed@android.com78cd1052010-03-08 20:23:57 +0000157 }
158 }
159 }
160}
161
reed@android.com9f8f48e2010-03-15 20:48:02 +0000162///////////////////////////////////////////////////////////////////////////////
163
164struct Mesh {
165 SkPoint fPts[4];
reed@android.com9f8f48e2010-03-15 20:48:02 +0000166
167 Mesh(const SkBitmap& bm, SkPaint* paint) {
168 const SkScalar w = SkIntToScalar(bm.width());
169 const SkScalar h = SkIntToScalar(bm.height());
170 fPts[0].set(0, 0);
171 fPts[1].set(w, 0);
172 fPts[2].set(w, h);
173 fPts[3].set(0, h);
174 SkShader* s = SkShader::CreateBitmapShader(bm, SkShader::kClamp_TileMode,
175 SkShader::kClamp_TileMode);
176 paint->setShader(s)->unref();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000177
reed@android.com9f8f48e2010-03-15 20:48:02 +0000178 }
179
180 void draw(SkCanvas* canvas, SkPaint* paint) {
181 canvas->drawVertices(SkCanvas::kTriangleFan_VertexMode, 4, fPts, fPts,
182 NULL, NULL, NULL, 0, *paint);
183 }
184};
185
186#include "SkImageEncoder.h"
187static void save_bm(const SkBitmap& bm, const char name[]) {
188 SkImageEncoder::EncodeFile(name, bm, SkImageEncoder::kPNG_Type, 100);
189}
190
191static bool gOnce;
192
193// Make sure our blits are invariant with the width of the blit (i.e. that
194// special case for 8 at a time have the same results as narrower blits)
195static void test_diagonal(skiatest::Reporter* reporter) {
196 static const int W = 64;
197 static const int H = W;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000198
reed@android.com9f8f48e2010-03-15 20:48:02 +0000199 static const SkBitmap::Config gDstConfig[] = {
200 SkBitmap::kARGB_8888_Config,
201 SkBitmap::kRGB_565_Config,
202 // SkBitmap::kARGB_4444_Config,
203 // SkBitmap::kA8_Config,
204 };
205
206 static const SkColor gDstBG[] = { 0, 0xFFFFFFFF };
207
208 SkPaint paint;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000209
reed@android.com9f8f48e2010-03-15 20:48:02 +0000210 SkBitmap srcBM;
211 srcBM.setConfig(SkBitmap::kARGB_8888_Config, W, H);
212 srcBM.allocPixels();
reed@android.comd4134452011-02-09 02:24:26 +0000213 SkRect srcR = {
214 0, 0, SkIntToScalar(srcBM.width()), SkIntToScalar(srcBM.height()) };
reed@android.com9f8f48e2010-03-15 20:48:02 +0000215
216 // cons up a mesh to draw the bitmap with
217 Mesh mesh(srcBM, &paint);
218
219 for (size_t i = 0; i < SK_ARRAY_COUNT(gDstConfig); i++) {
220 SkBitmap dstBM0, dstBM1;
221 dstBM0.setConfig(gDstConfig[i], W, H);
222 dstBM1.setConfig(gDstConfig[i], W, H);
223 dstBM0.allocPixels();
224 dstBM1.allocPixels();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000225
reed@android.com9f8f48e2010-03-15 20:48:02 +0000226 SkCanvas canvas0(dstBM0);
227 SkCanvas canvas1(dstBM1);
228 SkColor bgColor;
229
230 for (size_t j = 0; j < SK_ARRAY_COUNT(gDstBG); j++) {
231 bgColor = gDstBG[j];
232
233 for (int c = 0; c <= 0xFF; c++) {
234 srcBM.eraseARGB(0xFF, c, c, c);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000235
reed@android.com9f8f48e2010-03-15 20:48:02 +0000236 for (int k = 0; k < 4; k++) {
237 bool dither = (k & 1) != 0;
238 uint8_t alpha = (k & 2) ? 0x80 : 0xFF;
239 paint.setDither(dither);
240 paint.setAlpha(alpha);
241
242 dstBM0.eraseColor(bgColor);
243 dstBM1.eraseColor(bgColor);
244
245 canvas0.drawRect(srcR, paint);
246 mesh.draw(&canvas1, &paint);
247
248 if (!gOnce && false) {
249 save_bm(dstBM0, "drawBitmap.png");
250 save_bm(dstBM1, "drawMesh.png");
251 gOnce = true;
252 }
253
254 if (memcmp(dstBM0.getPixels(), dstBM1.getPixels(), dstBM0.getSize())) {
255 SkString str;
256 str.printf("Diagonal config=%s bg=0x%x dither=%d alpha=0x%x src=0x%x",
257 gConfigName[gDstConfig[i]], bgColor, dither, alpha, c);
258 reporter->reportFailed(str);
259 }
260 }
261 }
262 }
263 }
264}
265
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000266DEF_TEST(BlitRow, reporter) {
reed@android.com9f8f48e2010-03-15 20:48:02 +0000267 test_00_FF(reporter);
268 test_diagonal(reporter);
269}