reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 1 | #include "SkCanvas.h" |
reed@android.com | f523e25 | 2009-01-26 23:15:37 +0000 | [diff] [blame] | 2 | #include "SkColorPriv.h" |
reed@android.com | 3a859a0 | 2009-01-28 00:56:29 +0000 | [diff] [blame] | 3 | #include "SkGraphics.h" |
reed@android.com | b398fe8 | 2009-01-07 11:47:57 +0000 | [diff] [blame] | 4 | #include "SkImageEncoder.h" |
reed@android.com | 6c924ad | 2009-03-31 03:48:49 +0000 | [diff] [blame] | 5 | #include "SkNWayCanvas.h" |
| 6 | #include "SkPicture.h" |
reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 7 | #include "SkString.h" |
reed@android.com | 4bc1983 | 2009-01-19 20:08:35 +0000 | [diff] [blame] | 8 | #include "SkTime.h" |
reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 9 | |
| 10 | #include "SkBenchmark.h" |
| 11 | |
reed@android.com | 6c924ad | 2009-03-31 03:48:49 +0000 | [diff] [blame] | 12 | static void erase(SkBitmap& bm) { |
| 13 | if (bm.config() == SkBitmap::kA8_Config) { |
| 14 | bm.eraseColor(0); |
| 15 | } else { |
| 16 | bm.eraseColor(SK_ColorWHITE); |
| 17 | } |
| 18 | } |
| 19 | |
| 20 | static bool equal(const SkBitmap& bm1, const SkBitmap& bm2) { |
| 21 | if (bm1.width() != bm2.width() || |
| 22 | bm1.height() != bm2.height() || |
| 23 | bm1.config() != bm2.config()) { |
| 24 | return false; |
| 25 | } |
| 26 | |
| 27 | size_t pixelBytes = bm1.width() * bm1.bytesPerPixel(); |
| 28 | for (int y = 0; y < bm1.height(); y++) { |
| 29 | if (memcmp(bm1.getAddr(0, y), bm2.getAddr(0, y), pixelBytes)) { |
| 30 | return false; |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | return true; |
| 35 | } |
| 36 | |
reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 37 | class Iter { |
| 38 | public: |
| 39 | Iter() { |
| 40 | fBench = BenchRegistry::Head(); |
| 41 | } |
| 42 | |
| 43 | SkBenchmark* next() { |
| 44 | if (fBench) { |
| 45 | BenchRegistry::Factory f = fBench->factory(); |
| 46 | fBench = fBench->next(); |
reed@android.com | a70ba36 | 2009-01-22 17:41:57 +0000 | [diff] [blame] | 47 | return f(0); |
reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 48 | } |
| 49 | return NULL; |
| 50 | } |
| 51 | |
| 52 | private: |
| 53 | const BenchRegistry* fBench; |
| 54 | }; |
| 55 | |
| 56 | static void make_filename(const char name[], SkString* path) { |
| 57 | path->set(name); |
| 58 | for (int i = 0; name[i]; i++) { |
| 59 | switch (name[i]) { |
| 60 | case '/': |
| 61 | case '\\': |
| 62 | case ' ': |
| 63 | case ':': |
| 64 | path->writable_str()[i] = '-'; |
| 65 | break; |
| 66 | default: |
| 67 | break; |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | |
reed@android.com | 4c7d3d6 | 2009-01-21 03:15:13 +0000 | [diff] [blame] | 72 | static void saveFile(const char name[], const char config[], const char dir[], |
| 73 | const SkBitmap& bm) { |
reed@android.com | 4c7d3d6 | 2009-01-21 03:15:13 +0000 | [diff] [blame] | 74 | SkBitmap copy; |
| 75 | if (!bm.copyTo(©, SkBitmap::kARGB_8888_Config)) { |
| 76 | return; |
| 77 | } |
reed@android.com | f523e25 | 2009-01-26 23:15:37 +0000 | [diff] [blame] | 78 | |
| 79 | if (bm.config() == SkBitmap::kA8_Config) { |
| 80 | // turn alpha into gray-scale |
| 81 | size_t size = copy.getSize() >> 2; |
| 82 | SkPMColor* p = copy.getAddr32(0, 0); |
| 83 | for (size_t i = 0; i < size; i++) { |
| 84 | int c = (*p >> SK_A32_SHIFT) & 0xFF; |
| 85 | c = 255 - c; |
| 86 | c |= (c << 24) | (c << 16) | (c << 8); |
| 87 | *p++ = c | (SK_A32_MASK << SK_A32_SHIFT); |
| 88 | } |
| 89 | } |
reed@android.com | 4c7d3d6 | 2009-01-21 03:15:13 +0000 | [diff] [blame] | 90 | |
| 91 | SkString str; |
| 92 | make_filename(name, &str); |
| 93 | str.appendf("_%s.png", config); |
| 94 | str.prepend(dir); |
| 95 | ::remove(str.c_str()); |
| 96 | SkImageEncoder::EncodeFile(str.c_str(), copy, SkImageEncoder::kPNG_Type, |
| 97 | 100); |
reed@android.com | 4c7d3d6 | 2009-01-21 03:15:13 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | static void performClip(SkCanvas* canvas, int w, int h) { |
| 101 | SkRect r; |
| 102 | |
| 103 | r.set(SkIntToScalar(10), SkIntToScalar(10), |
| 104 | SkIntToScalar(w*2/3), SkIntToScalar(h*2/3)); |
| 105 | canvas->clipRect(r, SkRegion::kIntersect_Op); |
| 106 | |
| 107 | r.set(SkIntToScalar(w/3), SkIntToScalar(h/3), |
| 108 | SkIntToScalar(w-10), SkIntToScalar(h-10)); |
| 109 | canvas->clipRect(r, SkRegion::kXOR_Op); |
| 110 | } |
| 111 | |
| 112 | static void performRotate(SkCanvas* canvas, int w, int h) { |
| 113 | const SkScalar x = SkIntToScalar(w) / 2; |
| 114 | const SkScalar y = SkIntToScalar(h) / 2; |
| 115 | |
| 116 | canvas->translate(x, y); |
| 117 | canvas->rotate(SkIntToScalar(35)); |
| 118 | canvas->translate(-x, -y); |
| 119 | } |
| 120 | |
reed@android.com | 6c924ad | 2009-03-31 03:48:49 +0000 | [diff] [blame] | 121 | static void compare_pict_to_bitmap(SkPicture* pict, const SkBitmap& bm) { |
| 122 | SkBitmap bm2; |
| 123 | |
| 124 | bm2.setConfig(bm.config(), bm.width(), bm.height()); |
| 125 | bm2.allocPixels(); |
| 126 | erase(bm2); |
| 127 | |
| 128 | SkCanvas canvas(bm2); |
| 129 | canvas.drawPicture(*pict); |
| 130 | |
| 131 | if (!equal(bm, bm2)) { |
| 132 | SkDebugf("----- compare_pict_to_bitmap failed\n"); |
| 133 | } |
| 134 | } |
| 135 | |
reed@android.com | 4bc1983 | 2009-01-19 20:08:35 +0000 | [diff] [blame] | 136 | static const struct { |
| 137 | SkBitmap::Config fConfig; |
| 138 | const char* fName; |
| 139 | } gConfigs[] = { |
| 140 | { SkBitmap::kARGB_8888_Config, "8888" }, |
| 141 | { SkBitmap::kRGB_565_Config, "565", }, |
| 142 | { SkBitmap::kARGB_4444_Config, "4444", }, |
| 143 | { SkBitmap::kA8_Config, "A8", } |
| 144 | }; |
| 145 | |
reed@android.com | 4c7d3d6 | 2009-01-21 03:15:13 +0000 | [diff] [blame] | 146 | static int findConfig(const char config[]) { |
| 147 | for (size_t i = 0; i < SK_ARRAY_COUNT(gConfigs); i++) { |
| 148 | if (!strcmp(config, gConfigs[i].fName)) { |
| 149 | return i; |
| 150 | } |
| 151 | } |
| 152 | return -1; |
| 153 | } |
| 154 | |
reed@android.com | 3a859a0 | 2009-01-28 00:56:29 +0000 | [diff] [blame] | 155 | class SkAutoGraphics { |
| 156 | public: |
reed@android.com | 5e5adfd | 2009-03-07 03:39:23 +0000 | [diff] [blame] | 157 | SkAutoGraphics() { |
| 158 | SkGraphics::Init(); |
reed@android.com | 3a859a0 | 2009-01-28 00:56:29 +0000 | [diff] [blame] | 159 | } |
| 160 | ~SkAutoGraphics() { |
| 161 | SkGraphics::Term(); |
| 162 | } |
| 163 | }; |
| 164 | |
reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 165 | int main (int argc, char * const argv[]) { |
reed@android.com | 3a859a0 | 2009-01-28 00:56:29 +0000 | [diff] [blame] | 166 | SkAutoGraphics ag; |
| 167 | |
reed@android.com | 4bc1983 | 2009-01-19 20:08:35 +0000 | [diff] [blame] | 168 | int repeatDraw = 1; |
| 169 | int forceAlpha = 0xFF; |
| 170 | bool forceAA = true; |
reed@android.com | 4c7d3d6 | 2009-01-21 03:15:13 +0000 | [diff] [blame] | 171 | bool doRotate = false; |
| 172 | bool doClip = false; |
reed@android.com | 6c924ad | 2009-03-31 03:48:49 +0000 | [diff] [blame] | 173 | bool doPict = false; |
reed@android.com | 4bc1983 | 2009-01-19 20:08:35 +0000 | [diff] [blame] | 174 | |
reed@android.com | b398fe8 | 2009-01-07 11:47:57 +0000 | [diff] [blame] | 175 | SkString outDir; |
| 176 | SkBitmap::Config outConfig = SkBitmap::kARGB_8888_Config; |
reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 177 | |
reed@android.com | b398fe8 | 2009-01-07 11:47:57 +0000 | [diff] [blame] | 178 | char* const* stop = argv + argc; |
| 179 | for (++argv; argv < stop; ++argv) { |
| 180 | if (strcmp(*argv, "-o") == 0) { |
| 181 | argv++; |
| 182 | if (argv < stop && **argv) { |
| 183 | outDir.set(*argv); |
| 184 | if (outDir.c_str()[outDir.size() - 1] != '/') { |
| 185 | outDir.append("/"); |
| 186 | } |
| 187 | } |
reed@android.com | 6c924ad | 2009-03-31 03:48:49 +0000 | [diff] [blame] | 188 | } else if (strcmp(*argv, "-pict") == 0) { |
| 189 | doPict = true; |
reed@android.com | 4bc1983 | 2009-01-19 20:08:35 +0000 | [diff] [blame] | 190 | } else if (strcmp(*argv, "-repeat") == 0) { |
| 191 | argv++; |
| 192 | if (argv < stop) { |
| 193 | repeatDraw = atoi(*argv); |
| 194 | if (repeatDraw < 1) { |
| 195 | repeatDraw = 1; |
| 196 | } |
| 197 | } else { |
| 198 | fprintf(stderr, "missing arg for -repeat\n"); |
| 199 | return -1; |
| 200 | } |
reed@android.com | 4c7d3d6 | 2009-01-21 03:15:13 +0000 | [diff] [blame] | 201 | } else if (!strcmp(*argv, "-rotate")) { |
| 202 | doRotate = true; |
| 203 | } else if (!strcmp(*argv, "-clip")) { |
| 204 | doClip = true; |
reed@android.com | 4bc1983 | 2009-01-19 20:08:35 +0000 | [diff] [blame] | 205 | } else if (strcmp(*argv, "-forceAA") == 0) { |
| 206 | forceAA = true; |
| 207 | } else if (strcmp(*argv, "-forceBW") == 0) { |
| 208 | forceAA = false; |
| 209 | } else if (strcmp(*argv, "-forceBlend") == 0) { |
| 210 | forceAlpha = 0x80; |
| 211 | } else if (strcmp(*argv, "-forceOpaque") == 0) { |
| 212 | forceAlpha = 0xFF; |
reed@android.com | 4c7d3d6 | 2009-01-21 03:15:13 +0000 | [diff] [blame] | 213 | } else { |
| 214 | int index = findConfig(*argv); |
| 215 | if (index >= 0) { |
| 216 | outConfig = gConfigs[index].fConfig; |
| 217 | } |
reed@android.com | b398fe8 | 2009-01-07 11:47:57 +0000 | [diff] [blame] | 218 | } |
| 219 | } |
reed@android.com | 4bc1983 | 2009-01-19 20:08:35 +0000 | [diff] [blame] | 220 | |
| 221 | const char* configName = ""; |
| 222 | int configCount = SK_ARRAY_COUNT(gConfigs); |
| 223 | |
reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 224 | Iter iter; |
| 225 | SkBenchmark* bench; |
| 226 | while ((bench = iter.next()) != NULL) { |
reed@android.com | b398fe8 | 2009-01-07 11:47:57 +0000 | [diff] [blame] | 227 | SkIPoint dim = bench->getSize(); |
| 228 | if (dim.fX <= 0 || dim.fY <= 0) { |
| 229 | continue; |
| 230 | } |
reed@android.com | b398fe8 | 2009-01-07 11:47:57 +0000 | [diff] [blame] | 231 | |
reed@android.com | 4bc1983 | 2009-01-19 20:08:35 +0000 | [diff] [blame] | 232 | bench->setForceAlpha(forceAlpha); |
| 233 | bench->setForceAA(forceAA); |
| 234 | |
reed@android.com | f523e25 | 2009-01-26 23:15:37 +0000 | [diff] [blame] | 235 | printf("running bench %16s", bench->getName()); |
reed@android.com | 4bc1983 | 2009-01-19 20:08:35 +0000 | [diff] [blame] | 236 | |
| 237 | for (int configIndex = 0; configIndex < configCount; configIndex++) { |
| 238 | if (configCount > 1) { |
| 239 | outConfig = gConfigs[configIndex].fConfig; |
| 240 | configName = gConfigs[configIndex].fName; |
| 241 | } |
| 242 | |
| 243 | SkBitmap bm; |
| 244 | bm.setConfig(outConfig, dim.fX, dim.fY); |
| 245 | bm.allocPixels(); |
reed@android.com | 6c924ad | 2009-03-31 03:48:49 +0000 | [diff] [blame] | 246 | erase(bm); |
reed@android.com | f523e25 | 2009-01-26 23:15:37 +0000 | [diff] [blame] | 247 | |
reed@android.com | 4bc1983 | 2009-01-19 20:08:35 +0000 | [diff] [blame] | 248 | SkCanvas canvas(bm); |
reed@android.com | f523e25 | 2009-01-26 23:15:37 +0000 | [diff] [blame] | 249 | |
reed@android.com | 4c7d3d6 | 2009-01-21 03:15:13 +0000 | [diff] [blame] | 250 | if (doClip) { |
| 251 | performClip(&canvas, dim.fX, dim.fY); |
| 252 | } |
| 253 | if (doRotate) { |
| 254 | performRotate(&canvas, dim.fX, dim.fY); |
| 255 | } |
| 256 | |
reed@android.com | 4bc1983 | 2009-01-19 20:08:35 +0000 | [diff] [blame] | 257 | SkMSec now = SkTime::GetMSecs(); |
| 258 | for (int i = 0; i < repeatDraw; i++) { |
reed@android.com | 6c924ad | 2009-03-31 03:48:49 +0000 | [diff] [blame] | 259 | SkCanvas* c = &canvas; |
| 260 | |
| 261 | SkNWayCanvas nway; |
| 262 | SkPicture* pict = NULL; |
| 263 | if (doPict) { |
| 264 | pict = new SkPicture; |
| 265 | nway.addCanvas(pict->beginRecording(bm.width(), bm.height())); |
| 266 | nway.addCanvas(&canvas); |
| 267 | c = &nway; |
| 268 | } |
| 269 | |
| 270 | SkAutoCanvasRestore acr(c, true); |
| 271 | bench->draw(c); |
| 272 | |
| 273 | if (pict) { |
| 274 | compare_pict_to_bitmap(pict, bm); |
| 275 | pict->unref(); |
| 276 | } |
reed@android.com | 4bc1983 | 2009-01-19 20:08:35 +0000 | [diff] [blame] | 277 | } |
| 278 | if (repeatDraw > 1) { |
| 279 | printf(" %4s:%7.2f", configName, |
| 280 | (SkTime::GetMSecs() - now) / (double)repeatDraw); |
| 281 | } |
reed@android.com | 4c7d3d6 | 2009-01-21 03:15:13 +0000 | [diff] [blame] | 282 | if (outDir.size() > 0) { |
| 283 | saveFile(bench->getName(), configName, outDir.c_str(), bm); |
| 284 | } |
reed@android.com | 4bc1983 | 2009-01-19 20:08:35 +0000 | [diff] [blame] | 285 | } |
| 286 | printf("\n"); |
reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | return 0; |
| 290 | } |