blob: 3b0c94e2261bee164c26cf07eb1b6a718118ef7c [file] [log] [blame]
reed@android.com78cd1052010-03-08 20:23:57 +00001#include "Test.h"
2#include "SkBitmap.h"
3#include "SkCanvas.h"
4#include "SkColorPriv.h"
5#include "SkRect.h"
6
7static inline const char* boolStr(bool value) {
8 return value ? "true" : "false";
9}
10
11// these are in the same order as the SkBitmap::Config enum
12static const char* gConfigName[] = {
13 "None", "A1", "A8", "Index8", "565", "4444", "8888", "RLE_Index8"
14};
15
16/** Returns -1 on success, else the x coord of the first bad pixel, return its
17 value in bad
18 */
19typedef int (*Proc)(const void*, int width, uint32_t expected, uint32_t* bad);
20
21static int proc_32(const void* ptr, int w, uint32_t expected, uint32_t* bad) {
22 const SkPMColor* addr = static_cast<const SkPMColor*>(ptr);
23 for (int x = 0; x < w; x++) {
24 if (addr[x] != expected) {
25 *bad = addr[x];
26 return x;
27 }
28 }
29 return -1;
30}
31
32static int proc_16(const void* ptr, int w, uint32_t expected, uint32_t* bad) {
33 const uint16_t* addr = static_cast<const uint16_t*>(ptr);
34 for (int x = 0; x < w; x++) {
35 if (addr[x] != expected) {
36 *bad = addr[x];
37 return x;
38 }
39 }
40 return -1;
41}
42
43static int proc_8(const void* ptr, int w, uint32_t expected, uint32_t* bad) {
44 const SkPMColor* addr = static_cast<const SkPMColor*>(ptr);
45 for (int x = 0; x < w; x++) {
46 if (SkGetPackedA32(addr[x]) != expected) {
47 *bad = SkGetPackedA32(addr[x]);
48 return x;
49 }
50 }
51 return -1;
52}
53
54static int proc_bad(const void* ptr, int, uint32_t, uint32_t* bad) {
55 *bad = 0;
56 return 0;
57}
58
59static Proc find_proc(const SkBitmap& bm, SkPMColor expect32, uint16_t expect16,
60 uint8_t expect8, uint32_t* expect) {
61 switch (bm.config()) {
62 case SkBitmap::kARGB_8888_Config:
63 *expect = expect32;
64 return proc_32;
65 case SkBitmap::kARGB_4444_Config:
66 case SkBitmap::kRGB_565_Config:
67 *expect = expect16;
68 return proc_16;
69 case SkBitmap::kA8_Config:
70 *expect = expect8;
71 return proc_8;
72 default:
73 *expect = 0;
74 return proc_bad;
75 }
76}
77
78static bool check_color(const SkBitmap& bm, SkPMColor expect32,
79 uint16_t expect16, uint8_t expect8,
80 skiatest::Reporter* reporter) {
81 uint32_t expect;
82 Proc proc = find_proc(bm, expect32, expect16, expect8, &expect);
83 for (int y = 0; y < bm.height(); y++) {
84 uint32_t bad;
85 int x = proc(bm.getAddr(0, y), bm.width(), expect, &bad);
86 if (x >= 0) {
87 SkString str;
88 str.printf("BlitRow config=%s [%d %d] expected %x got %x",
89 gConfigName[bm.config()], x, y, expect, bad);
90 reporter->reportFailed(str);
91 return false;
92 }
93 }
94 return true;
95}
96
97static void TestBlitRow(skiatest::Reporter* reporter) {
98 static const int W = 256;
99
100 static const SkBitmap::Config gDstConfig[] = {
101 SkBitmap::kARGB_8888_Config,
102 SkBitmap::kRGB_565_Config,
103 SkBitmap::kARGB_4444_Config,
104// SkBitmap::kA8_Config,
105 };
106
107 static const struct {
108 SkColor fSrc;
109 SkColor fDst;
110 SkPMColor fResult32;
111 uint16_t fResult16;
112 uint8_t fResult8;
113 } gSrcRec[] = {
114 { 0, 0, 0, 0, 0 },
115 { 0, 0xFFFFFFFF, SkPackARGB32(0xFF, 0xFF, 0xFF, 0xFF), 0xFFFF, 0xFF },
116 { 0xFFFFFFFF, 0, SkPackARGB32(0xFF, 0xFF, 0xFF, 0xFF), 0xFFFF, 0xFF },
117 { 0xFFFFFFFF, 0xFFFFFFFF, SkPackARGB32(0xFF, 0xFF, 0xFF, 0xFF), 0xFFFF, 0xFF },
118 };
119
120 SkBitmap srcBM;
121 srcBM.setConfig(SkBitmap::kARGB_8888_Config, W, 1);
122 srcBM.allocPixels();
123
124 for (size_t i = 0; i < SK_ARRAY_COUNT(gDstConfig); i++) {
125 SkBitmap dstBM;
126 dstBM.setConfig(gDstConfig[i], W, 1);
127 dstBM.allocPixels();
128
129 SkCanvas canvas(dstBM);
130 for (size_t j = 0; j < SK_ARRAY_COUNT(gSrcRec); j++) {
131 srcBM.eraseColor(gSrcRec[j].fSrc);
132 dstBM.eraseColor(gSrcRec[j].fDst);
133
134 canvas.drawBitmap(srcBM, 0, 0, NULL);
135 if (!check_color(dstBM, gSrcRec[j].fResult32, gSrcRec[j].fResult16,
136 gSrcRec[j].fResult8, reporter)) {
137 SkDebugf("--- src index %d\n", j);
138 }
139 }
140 }
141}
142
143#include "TestClassDef.h"
144DEFINE_TESTCLASS("BlitRow", TestBlitRowClass, TestBlitRow)