1
2 /*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8 #include "Test.h"
9 #include "SkBitmap.h"
10 #include "SkCanvas.h"
11 #include "SkColorPriv.h"
12 #include "SkGradientShader.h"
13 #include "SkRect.h"
14
boolStr(bool value)15 static inline const char* boolStr(bool value) {
16 return value ? "true" : "false";
17 }
18
19 // these are in the same order as the SkBitmap::Config enum
20 static const char* gConfigName[] = {
21 "None", "A1", "A8", "Index8", "565", "4444", "8888", "RLE_Index8"
22 };
23
24 /** Returns -1 on success, else the x coord of the first bad pixel, return its
25 value in bad
26 */
27 typedef int (*Proc)(const void*, int width, uint32_t expected, uint32_t* bad);
28
proc_32(const void * ptr,int w,uint32_t expected,uint32_t * bad)29 static int proc_32(const void* ptr, int w, uint32_t expected, uint32_t* bad) {
30 const SkPMColor* addr = static_cast<const SkPMColor*>(ptr);
31 for (int x = 0; x < w; x++) {
32 if (addr[x] != expected) {
33 *bad = addr[x];
34 return x;
35 }
36 }
37 return -1;
38 }
39
proc_16(const void * ptr,int w,uint32_t expected,uint32_t * bad)40 static int proc_16(const void* ptr, int w, uint32_t expected, uint32_t* bad) {
41 const uint16_t* addr = static_cast<const uint16_t*>(ptr);
42 for (int x = 0; x < w; x++) {
43 if (addr[x] != expected) {
44 *bad = addr[x];
45 return x;
46 }
47 }
48 return -1;
49 }
50
proc_8(const void * ptr,int w,uint32_t expected,uint32_t * bad)51 static int proc_8(const void* ptr, int w, uint32_t expected, uint32_t* bad) {
52 const SkPMColor* addr = static_cast<const SkPMColor*>(ptr);
53 for (int x = 0; x < w; x++) {
54 if (SkGetPackedA32(addr[x]) != expected) {
55 *bad = SkGetPackedA32(addr[x]);
56 return x;
57 }
58 }
59 return -1;
60 }
61
proc_bad(const void * ptr,int,uint32_t,uint32_t * bad)62 static int proc_bad(const void* ptr, int, uint32_t, uint32_t* bad) {
63 *bad = 0;
64 return 0;
65 }
66
find_proc(const SkBitmap & bm,SkPMColor expect32,uint16_t expect16,uint8_t expect8,uint32_t * expect)67 static Proc find_proc(const SkBitmap& bm, SkPMColor expect32, uint16_t expect16,
68 uint8_t expect8, uint32_t* expect) {
69 switch (bm.config()) {
70 case SkBitmap::kARGB_8888_Config:
71 *expect = expect32;
72 return proc_32;
73 case SkBitmap::kARGB_4444_Config:
74 case SkBitmap::kRGB_565_Config:
75 *expect = expect16;
76 return proc_16;
77 case SkBitmap::kA8_Config:
78 *expect = expect8;
79 return proc_8;
80 default:
81 *expect = 0;
82 return proc_bad;
83 }
84 }
85
check_color(const SkBitmap & bm,SkPMColor expect32,uint16_t expect16,uint8_t expect8,skiatest::Reporter * reporter)86 static bool check_color(const SkBitmap& bm, SkPMColor expect32,
87 uint16_t expect16, uint8_t expect8,
88 skiatest::Reporter* reporter) {
89 uint32_t expect;
90 Proc proc = find_proc(bm, expect32, expect16, expect8, &expect);
91 for (int y = 0; y < bm.height(); y++) {
92 uint32_t bad;
93 int x = proc(bm.getAddr(0, y), bm.width(), expect, &bad);
94 if (x >= 0) {
95 SkString str;
96 str.printf("BlitRow config=%s [%d %d] expected %x got %x",
97 gConfigName[bm.config()], x, y, expect, bad);
98 reporter->reportFailed(str);
99 return false;
100 }
101 }
102 return true;
103 }
104
105 // Make sure our blits always map src==0 to a noop, and src==FF to full opaque
test_00_FF(skiatest::Reporter * reporter)106 static void test_00_FF(skiatest::Reporter* reporter) {
107 static const int W = 256;
108
109 static const SkBitmap::Config gDstConfig[] = {
110 SkBitmap::kARGB_8888_Config,
111 SkBitmap::kRGB_565_Config,
112 // SkBitmap::kARGB_4444_Config,
113 // SkBitmap::kA8_Config,
114 };
115
116 static const struct {
117 SkColor fSrc;
118 SkColor fDst;
119 SkPMColor fResult32;
120 uint16_t fResult16;
121 uint8_t fResult8;
122 } gSrcRec[] = {
123 { 0, 0, 0, 0, 0 },
124 { 0, 0xFFFFFFFF, SkPackARGB32(0xFF, 0xFF, 0xFF, 0xFF), 0xFFFF, 0xFF },
125 { 0xFFFFFFFF, 0, SkPackARGB32(0xFF, 0xFF, 0xFF, 0xFF), 0xFFFF, 0xFF },
126 { 0xFFFFFFFF, 0xFFFFFFFF, SkPackARGB32(0xFF, 0xFF, 0xFF, 0xFF), 0xFFFF, 0xFF },
127 };
128
129 SkPaint paint;
130
131 SkBitmap srcBM;
132 srcBM.setConfig(SkBitmap::kARGB_8888_Config, W, 1);
133 srcBM.allocPixels();
134
135 for (size_t i = 0; i < SK_ARRAY_COUNT(gDstConfig); i++) {
136 SkBitmap dstBM;
137 dstBM.setConfig(gDstConfig[i], W, 1);
138 dstBM.allocPixels();
139
140 SkCanvas canvas(dstBM);
141 for (size_t j = 0; j < SK_ARRAY_COUNT(gSrcRec); j++) {
142 srcBM.eraseColor(gSrcRec[j].fSrc);
143 dstBM.eraseColor(gSrcRec[j].fDst);
144
145 for (int k = 0; k < 4; k++) {
146 bool dither = (k & 1) != 0;
147 bool blend = (k & 2) != 0;
148 if (gSrcRec[j].fSrc != 0 && blend) {
149 // can't make a numerical promise about blending anything
150 // but 0
151 // continue;
152 }
153 paint.setDither(dither);
154 paint.setAlpha(blend ? 0x80 : 0xFF);
155 canvas.drawBitmap(srcBM, 0, 0, &paint);
156 if (!check_color(dstBM, gSrcRec[j].fResult32, gSrcRec[j].fResult16,
157 gSrcRec[j].fResult8, reporter)) {
158 SkDebugf("--- src index %d dither %d blend %d\n", j, dither, blend);
159 }
160 }
161 }
162 }
163 }
164
165 ///////////////////////////////////////////////////////////////////////////////
166
167 struct Mesh {
168 SkPoint fPts[4];
169 uint16_t fIndices[6];
170
MeshMesh171 Mesh(const SkBitmap& bm, SkPaint* paint) {
172 const SkScalar w = SkIntToScalar(bm.width());
173 const SkScalar h = SkIntToScalar(bm.height());
174 fPts[0].set(0, 0);
175 fPts[1].set(w, 0);
176 fPts[2].set(w, h);
177 fPts[3].set(0, h);
178 SkShader* s = SkShader::CreateBitmapShader(bm, SkShader::kClamp_TileMode,
179 SkShader::kClamp_TileMode);
180 paint->setShader(s)->unref();
181
182 }
183
drawMesh184 void draw(SkCanvas* canvas, SkPaint* paint) {
185 canvas->drawVertices(SkCanvas::kTriangleFan_VertexMode, 4, fPts, fPts,
186 NULL, NULL, NULL, 0, *paint);
187 }
188 };
189
190 #include "SkImageEncoder.h"
save_bm(const SkBitmap & bm,const char name[])191 static void save_bm(const SkBitmap& bm, const char name[]) {
192 SkImageEncoder::EncodeFile(name, bm, SkImageEncoder::kPNG_Type, 100);
193 }
194
195 static bool gOnce;
196
197 // Make sure our blits are invariant with the width of the blit (i.e. that
198 // special case for 8 at a time have the same results as narrower blits)
test_diagonal(skiatest::Reporter * reporter)199 static void test_diagonal(skiatest::Reporter* reporter) {
200 static const int W = 64;
201 static const int H = W;
202
203 static const SkBitmap::Config gDstConfig[] = {
204 SkBitmap::kARGB_8888_Config,
205 SkBitmap::kRGB_565_Config,
206 // SkBitmap::kARGB_4444_Config,
207 // SkBitmap::kA8_Config,
208 };
209
210 static const SkColor gDstBG[] = { 0, 0xFFFFFFFF };
211
212 SkPaint paint;
213
214 SkBitmap srcBM;
215 srcBM.setConfig(SkBitmap::kARGB_8888_Config, W, H);
216 srcBM.allocPixels();
217 SkRect srcR = {
218 0, 0, SkIntToScalar(srcBM.width()), SkIntToScalar(srcBM.height()) };
219
220 // cons up a mesh to draw the bitmap with
221 Mesh mesh(srcBM, &paint);
222
223 for (size_t i = 0; i < SK_ARRAY_COUNT(gDstConfig); i++) {
224 SkBitmap dstBM0, dstBM1;
225 dstBM0.setConfig(gDstConfig[i], W, H);
226 dstBM1.setConfig(gDstConfig[i], W, H);
227 dstBM0.allocPixels();
228 dstBM1.allocPixels();
229
230 SkCanvas canvas0(dstBM0);
231 SkCanvas canvas1(dstBM1);
232 SkColor bgColor;
233
234 for (size_t j = 0; j < SK_ARRAY_COUNT(gDstBG); j++) {
235 bgColor = gDstBG[j];
236
237 for (int c = 0; c <= 0xFF; c++) {
238 srcBM.eraseARGB(0xFF, c, c, c);
239
240 for (int k = 0; k < 4; k++) {
241 bool dither = (k & 1) != 0;
242 uint8_t alpha = (k & 2) ? 0x80 : 0xFF;
243 paint.setDither(dither);
244 paint.setAlpha(alpha);
245
246 dstBM0.eraseColor(bgColor);
247 dstBM1.eraseColor(bgColor);
248
249 canvas0.drawRect(srcR, paint);
250 mesh.draw(&canvas1, &paint);
251
252 if (!gOnce && false) {
253 save_bm(dstBM0, "drawBitmap.png");
254 save_bm(dstBM1, "drawMesh.png");
255 gOnce = true;
256 }
257
258 if (memcmp(dstBM0.getPixels(), dstBM1.getPixels(), dstBM0.getSize())) {
259 SkString str;
260 str.printf("Diagonal config=%s bg=0x%x dither=%d alpha=0x%x src=0x%x",
261 gConfigName[gDstConfig[i]], bgColor, dither, alpha, c);
262 reporter->reportFailed(str);
263 }
264 }
265 }
266 }
267 }
268 }
269
TestBlitRow(skiatest::Reporter * reporter)270 static void TestBlitRow(skiatest::Reporter* reporter) {
271 test_00_FF(reporter);
272 test_diagonal(reporter);
273 }
274
275 #include "TestClassDef.h"
276 DEFINE_TESTCLASS("BlitRow", TestBlitRowClass, TestBlitRow)
277