blob: 3f3f529462ca5d400c585ef8b1a65f4f44b277a6 [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"
9#include "SkBitmap.h"
10#include "SkCanvas.h"
11#include "SkColorPriv.h"
reed@android.com9f8f48e2010-03-15 20:48:02 +000012#include "SkGradientShader.h"
reed@android.com78cd1052010-03-08 20:23:57 +000013#include "SkRect.h"
14
reed@android.com78cd1052010-03-08 20:23:57 +000015// these are in the same order as the SkBitmap::Config enum
16static const char* gConfigName[] = {
rmistry@google.comd6bab022013-12-02 13:50:38 +000017 "None", "A8", "Index8", "565", "4444", "8888"
reed@android.com78cd1052010-03-08 20:23:57 +000018};
19
20/** Returns -1 on success, else the x coord of the first bad pixel, return its
21 value in bad
22 */
23typedef int (*Proc)(const void*, int width, uint32_t expected, uint32_t* bad);
24
25static int proc_32(const void* ptr, int w, uint32_t expected, uint32_t* bad) {
26 const SkPMColor* addr = static_cast<const SkPMColor*>(ptr);
27 for (int x = 0; x < w; x++) {
28 if (addr[x] != expected) {
29 *bad = addr[x];
30 return x;
31 }
32 }
33 return -1;
34}
35
36static int proc_16(const void* ptr, int w, uint32_t expected, uint32_t* bad) {
37 const uint16_t* addr = static_cast<const uint16_t*>(ptr);
38 for (int x = 0; x < w; x++) {
39 if (addr[x] != expected) {
40 *bad = addr[x];
41 return x;
42 }
43 }
44 return -1;
45}
46
47static int proc_8(const void* ptr, int w, uint32_t expected, uint32_t* bad) {
48 const SkPMColor* addr = static_cast<const SkPMColor*>(ptr);
49 for (int x = 0; x < w; x++) {
50 if (SkGetPackedA32(addr[x]) != expected) {
51 *bad = SkGetPackedA32(addr[x]);
52 return x;
53 }
54 }
55 return -1;
56}
57
sugoi@google.com54f0d1b2013-02-27 19:17:41 +000058static int proc_bad(const void*, int, uint32_t, uint32_t* bad) {
reed@android.com78cd1052010-03-08 20:23:57 +000059 *bad = 0;
60 return 0;
61}
62
63static Proc find_proc(const SkBitmap& bm, SkPMColor expect32, uint16_t expect16,
64 uint8_t expect8, uint32_t* expect) {
65 switch (bm.config()) {
66 case SkBitmap::kARGB_8888_Config:
67 *expect = expect32;
68 return proc_32;
69 case SkBitmap::kARGB_4444_Config:
70 case SkBitmap::kRGB_565_Config:
71 *expect = expect16;
72 return proc_16;
73 case SkBitmap::kA8_Config:
74 *expect = expect8;
75 return proc_8;
76 default:
77 *expect = 0;
78 return proc_bad;
79 }
80}
81
82static bool check_color(const SkBitmap& bm, SkPMColor expect32,
83 uint16_t expect16, uint8_t expect8,
84 skiatest::Reporter* reporter) {
85 uint32_t expect;
86 Proc proc = find_proc(bm, expect32, expect16, expect8, &expect);
87 for (int y = 0; y < bm.height(); y++) {
88 uint32_t bad;
89 int x = proc(bm.getAddr(0, y), bm.width(), expect, &bad);
90 if (x >= 0) {
halcanary@google.coma9325fa2014-01-10 14:58:10 +000091 ERRORF(reporter, "BlitRow config=%s [%d %d] expected %x got %x",
92 gConfigName[bm.config()], x, y, expect, bad);
reed@android.com78cd1052010-03-08 20:23:57 +000093 return false;
94 }
95 }
96 return true;
97}
98
reed@android.com9f8f48e2010-03-15 20:48:02 +000099// Make sure our blits always map src==0 to a noop, and src==FF to full opaque
100static void test_00_FF(skiatest::Reporter* reporter) {
reed@android.com78cd1052010-03-08 20:23:57 +0000101 static const int W = 256;
102
103 static const SkBitmap::Config gDstConfig[] = {
104 SkBitmap::kARGB_8888_Config,
105 SkBitmap::kRGB_565_Config,
reed@android.com8e4c93b2010-03-09 15:21:28 +0000106// SkBitmap::kARGB_4444_Config,
reed@android.com78cd1052010-03-08 20:23:57 +0000107// SkBitmap::kA8_Config,
108 };
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;
126 srcBM.setConfig(SkBitmap::kARGB_8888_Config, W, 1);
127 srcBM.allocPixels();
reed@android.com9f8f48e2010-03-15 20:48:02 +0000128
reed@android.com78cd1052010-03-08 20:23:57 +0000129 for (size_t i = 0; i < SK_ARRAY_COUNT(gDstConfig); i++) {
130 SkBitmap dstBM;
131 dstBM.setConfig(gDstConfig[i], W, 1);
132 dstBM.allocPixels();
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);
171 SkShader* s = SkShader::CreateBitmapShader(bm, SkShader::kClamp_TileMode,
172 SkShader::kClamp_TileMode);
173 paint->setShader(s)->unref();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000174
reed@android.com9f8f48e2010-03-15 20:48:02 +0000175 }
176
177 void draw(SkCanvas* canvas, SkPaint* paint) {
178 canvas->drawVertices(SkCanvas::kTriangleFan_VertexMode, 4, fPts, fPts,
179 NULL, NULL, NULL, 0, *paint);
180 }
181};
182
183#include "SkImageEncoder.h"
184static void save_bm(const SkBitmap& bm, const char name[]) {
185 SkImageEncoder::EncodeFile(name, bm, SkImageEncoder::kPNG_Type, 100);
186}
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
reed@android.com9f8f48e2010-03-15 20:48:02 +0000196 static const SkBitmap::Config gDstConfig[] = {
197 SkBitmap::kARGB_8888_Config,
198 SkBitmap::kRGB_565_Config,
199 // SkBitmap::kARGB_4444_Config,
200 // SkBitmap::kA8_Config,
201 };
202
203 static const SkColor gDstBG[] = { 0, 0xFFFFFFFF };
204
205 SkPaint paint;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000206
reed@android.com9f8f48e2010-03-15 20:48:02 +0000207 SkBitmap srcBM;
208 srcBM.setConfig(SkBitmap::kARGB_8888_Config, W, H);
209 srcBM.allocPixels();
reed@android.comd4134452011-02-09 02:24:26 +0000210 SkRect srcR = {
211 0, 0, SkIntToScalar(srcBM.width()), SkIntToScalar(srcBM.height()) };
reed@android.com9f8f48e2010-03-15 20:48:02 +0000212
213 // cons up a mesh to draw the bitmap with
214 Mesh mesh(srcBM, &paint);
215
216 for (size_t i = 0; i < SK_ARRAY_COUNT(gDstConfig); i++) {
217 SkBitmap dstBM0, dstBM1;
218 dstBM0.setConfig(gDstConfig[i], W, H);
219 dstBM1.setConfig(gDstConfig[i], W, H);
220 dstBM0.allocPixels();
221 dstBM1.allocPixels();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000222
reed@android.com9f8f48e2010-03-15 20:48:02 +0000223 SkCanvas canvas0(dstBM0);
224 SkCanvas canvas1(dstBM1);
225 SkColor bgColor;
226
227 for (size_t j = 0; j < SK_ARRAY_COUNT(gDstBG); j++) {
228 bgColor = gDstBG[j];
229
230 for (int c = 0; c <= 0xFF; c++) {
231 srcBM.eraseARGB(0xFF, c, c, c);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000232
reed@android.com9f8f48e2010-03-15 20:48:02 +0000233 for (int k = 0; k < 4; k++) {
234 bool dither = (k & 1) != 0;
235 uint8_t alpha = (k & 2) ? 0x80 : 0xFF;
236 paint.setDither(dither);
237 paint.setAlpha(alpha);
238
239 dstBM0.eraseColor(bgColor);
240 dstBM1.eraseColor(bgColor);
241
242 canvas0.drawRect(srcR, paint);
243 mesh.draw(&canvas1, &paint);
244
245 if (!gOnce && false) {
246 save_bm(dstBM0, "drawBitmap.png");
247 save_bm(dstBM1, "drawMesh.png");
248 gOnce = true;
249 }
250
251 if (memcmp(dstBM0.getPixels(), dstBM1.getPixels(), dstBM0.getSize())) {
halcanary@google.coma9325fa2014-01-10 14:58:10 +0000252 ERRORF(reporter, "Diagonal config=%s bg=0x%x dither=%d"
253 " alpha=0x%x src=0x%x",
254 gConfigName[gDstConfig[i]], bgColor, dither,
255 alpha, c);
reed@android.com9f8f48e2010-03-15 20:48:02 +0000256 }
257 }
258 }
259 }
260 }
261}
262
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000263DEF_TEST(BlitRow, reporter) {
reed@android.com9f8f48e2010-03-15 20:48:02 +0000264 test_00_FF(reporter);
265 test_diagonal(reporter);
266}