epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 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 | */ |
bsalomon@google.com | 971d0c8 | 2011-08-19 17:22:05 +0000 | [diff] [blame] | 8 | |
| 9 | |
| 10 | #include "BenchTimer.h" |
| 11 | |
| 12 | #include "GrContext.h" |
| 13 | #include "GrRenderTarget.h" |
| 14 | |
| 15 | #include "SkBenchmark.h" |
reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 16 | #include "SkCanvas.h" |
reed@android.com | f523e25 | 2009-01-26 23:15:37 +0000 | [diff] [blame] | 17 | #include "SkColorPriv.h" |
bsalomon@google.com | 971d0c8 | 2011-08-19 17:22:05 +0000 | [diff] [blame] | 18 | #include "SkGpuDevice.h" |
reed@android.com | 3a859a0 | 2009-01-28 00:56:29 +0000 | [diff] [blame] | 19 | #include "SkGraphics.h" |
reed@android.com | b398fe8 | 2009-01-07 11:47:57 +0000 | [diff] [blame] | 20 | #include "SkImageEncoder.h" |
tomhudson@google.com | 6bf38b5 | 2012-02-14 15:11:59 +0000 | [diff] [blame] | 21 | #include "gl/SkNativeGLContext.h" |
| 22 | #include "gl/SkNullGLContext.h" |
robertphillips@google.com | 0da3719 | 2012-03-19 14:42:13 +0000 | [diff] [blame^] | 23 | #include "gl/SkDebugGLContext.h" |
reed@android.com | 6c924ad | 2009-03-31 03:48:49 +0000 | [diff] [blame] | 24 | #include "SkNWayCanvas.h" |
| 25 | #include "SkPicture.h" |
reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 26 | #include "SkString.h" |
reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 27 | |
djsollen@google.com | 56c6977 | 2011-11-08 19:00:26 +0000 | [diff] [blame] | 28 | #ifdef SK_BUILD_FOR_ANDROID |
reed@android.com | 29348cb | 2009-08-04 18:17:15 +0000 | [diff] [blame] | 29 | static void log_error(const char msg[]) { SkDebugf("%s", msg); } |
| 30 | static void log_progress(const char msg[]) { SkDebugf("%s", msg); } |
| 31 | #else |
| 32 | static void log_error(const char msg[]) { fprintf(stderr, "%s", msg); } |
| 33 | static void log_progress(const char msg[]) { printf("%s", msg); } |
| 34 | #endif |
| 35 | |
| 36 | static void log_error(const SkString& str) { log_error(str.c_str()); } |
| 37 | static void log_progress(const SkString& str) { log_progress(str.c_str()); } |
| 38 | |
| 39 | /////////////////////////////////////////////////////////////////////////////// |
| 40 | |
reed@android.com | 6c924ad | 2009-03-31 03:48:49 +0000 | [diff] [blame] | 41 | static void erase(SkBitmap& bm) { |
| 42 | if (bm.config() == SkBitmap::kA8_Config) { |
| 43 | bm.eraseColor(0); |
| 44 | } else { |
| 45 | bm.eraseColor(SK_ColorWHITE); |
| 46 | } |
| 47 | } |
| 48 | |
mike@reedtribe.org | a9015f8 | 2011-05-17 02:25:05 +0000 | [diff] [blame] | 49 | #if 0 |
reed@android.com | 6c924ad | 2009-03-31 03:48:49 +0000 | [diff] [blame] | 50 | static bool equal(const SkBitmap& bm1, const SkBitmap& bm2) { |
| 51 | if (bm1.width() != bm2.width() || |
| 52 | bm1.height() != bm2.height() || |
| 53 | bm1.config() != bm2.config()) { |
| 54 | return false; |
| 55 | } |
| 56 | |
| 57 | size_t pixelBytes = bm1.width() * bm1.bytesPerPixel(); |
| 58 | for (int y = 0; y < bm1.height(); y++) { |
| 59 | if (memcmp(bm1.getAddr(0, y), bm2.getAddr(0, y), pixelBytes)) { |
| 60 | return false; |
| 61 | } |
| 62 | } |
reed@android.com | 6c924ad | 2009-03-31 03:48:49 +0000 | [diff] [blame] | 63 | return true; |
| 64 | } |
mike@reedtribe.org | a9015f8 | 2011-05-17 02:25:05 +0000 | [diff] [blame] | 65 | #endif |
reed@android.com | 6c924ad | 2009-03-31 03:48:49 +0000 | [diff] [blame] | 66 | |
reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 67 | class Iter { |
| 68 | public: |
reed@android.com | e9d0060 | 2009-09-02 21:12:42 +0000 | [diff] [blame] | 69 | Iter(void* param) { |
reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 70 | fBench = BenchRegistry::Head(); |
reed@android.com | e9d0060 | 2009-09-02 21:12:42 +0000 | [diff] [blame] | 71 | fParam = param; |
reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | SkBenchmark* next() { |
| 75 | if (fBench) { |
| 76 | BenchRegistry::Factory f = fBench->factory(); |
| 77 | fBench = fBench->next(); |
reed@android.com | e9d0060 | 2009-09-02 21:12:42 +0000 | [diff] [blame] | 78 | return f(fParam); |
reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 79 | } |
| 80 | return NULL; |
| 81 | } |
bungeman@google.com | d1a416a | 2011-05-18 18:37:07 +0000 | [diff] [blame] | 82 | |
reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 83 | private: |
| 84 | const BenchRegistry* fBench; |
reed@android.com | e9d0060 | 2009-09-02 21:12:42 +0000 | [diff] [blame] | 85 | void* fParam; |
reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 86 | }; |
| 87 | |
| 88 | static void make_filename(const char name[], SkString* path) { |
| 89 | path->set(name); |
| 90 | for (int i = 0; name[i]; i++) { |
| 91 | switch (name[i]) { |
| 92 | case '/': |
| 93 | case '\\': |
| 94 | case ' ': |
| 95 | case ':': |
| 96 | path->writable_str()[i] = '-'; |
| 97 | break; |
| 98 | default: |
| 99 | break; |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | |
reed@android.com | 4c7d3d6 | 2009-01-21 03:15:13 +0000 | [diff] [blame] | 104 | static void saveFile(const char name[], const char config[], const char dir[], |
| 105 | const SkBitmap& bm) { |
reed@android.com | 4c7d3d6 | 2009-01-21 03:15:13 +0000 | [diff] [blame] | 106 | SkBitmap copy; |
| 107 | if (!bm.copyTo(©, SkBitmap::kARGB_8888_Config)) { |
| 108 | return; |
| 109 | } |
reed@android.com | f523e25 | 2009-01-26 23:15:37 +0000 | [diff] [blame] | 110 | |
| 111 | if (bm.config() == SkBitmap::kA8_Config) { |
| 112 | // turn alpha into gray-scale |
| 113 | size_t size = copy.getSize() >> 2; |
| 114 | SkPMColor* p = copy.getAddr32(0, 0); |
| 115 | for (size_t i = 0; i < size; i++) { |
| 116 | int c = (*p >> SK_A32_SHIFT) & 0xFF; |
| 117 | c = 255 - c; |
| 118 | c |= (c << 24) | (c << 16) | (c << 8); |
| 119 | *p++ = c | (SK_A32_MASK << SK_A32_SHIFT); |
| 120 | } |
| 121 | } |
bungeman@google.com | d1a416a | 2011-05-18 18:37:07 +0000 | [diff] [blame] | 122 | |
reed@android.com | 4c7d3d6 | 2009-01-21 03:15:13 +0000 | [diff] [blame] | 123 | SkString str; |
| 124 | make_filename(name, &str); |
| 125 | str.appendf("_%s.png", config); |
| 126 | str.prepend(dir); |
| 127 | ::remove(str.c_str()); |
| 128 | SkImageEncoder::EncodeFile(str.c_str(), copy, SkImageEncoder::kPNG_Type, |
| 129 | 100); |
reed@android.com | 4c7d3d6 | 2009-01-21 03:15:13 +0000 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | static void performClip(SkCanvas* canvas, int w, int h) { |
| 133 | SkRect r; |
| 134 | |
| 135 | r.set(SkIntToScalar(10), SkIntToScalar(10), |
| 136 | SkIntToScalar(w*2/3), SkIntToScalar(h*2/3)); |
| 137 | canvas->clipRect(r, SkRegion::kIntersect_Op); |
bungeman@google.com | d1a416a | 2011-05-18 18:37:07 +0000 | [diff] [blame] | 138 | |
reed@android.com | 4c7d3d6 | 2009-01-21 03:15:13 +0000 | [diff] [blame] | 139 | r.set(SkIntToScalar(w/3), SkIntToScalar(h/3), |
| 140 | SkIntToScalar(w-10), SkIntToScalar(h-10)); |
| 141 | canvas->clipRect(r, SkRegion::kXOR_Op); |
| 142 | } |
| 143 | |
| 144 | static void performRotate(SkCanvas* canvas, int w, int h) { |
| 145 | const SkScalar x = SkIntToScalar(w) / 2; |
| 146 | const SkScalar y = SkIntToScalar(h) / 2; |
| 147 | |
| 148 | canvas->translate(x, y); |
| 149 | canvas->rotate(SkIntToScalar(35)); |
| 150 | canvas->translate(-x, -y); |
| 151 | } |
| 152 | |
reed@android.com | 387359e | 2009-08-04 01:51:09 +0000 | [diff] [blame] | 153 | static void performScale(SkCanvas* canvas, int w, int h) { |
| 154 | const SkScalar x = SkIntToScalar(w) / 2; |
| 155 | const SkScalar y = SkIntToScalar(h) / 2; |
| 156 | |
| 157 | canvas->translate(x, y); |
| 158 | // just enough so we can't take the sprite case |
| 159 | canvas->scale(SK_Scalar1 * 99/100, SK_Scalar1 * 99/100); |
| 160 | canvas->translate(-x, -y); |
| 161 | } |
| 162 | |
reed@android.com | 29348cb | 2009-08-04 18:17:15 +0000 | [diff] [blame] | 163 | static bool parse_bool_arg(char * const* argv, char* const* stop, bool* var) { |
| 164 | if (argv < stop) { |
| 165 | *var = atoi(*argv) != 0; |
| 166 | return true; |
| 167 | } |
| 168 | return false; |
| 169 | } |
| 170 | |
mike@reedtribe.org | a9015f8 | 2011-05-17 02:25:05 +0000 | [diff] [blame] | 171 | enum Backend { |
| 172 | kRaster_Backend, |
| 173 | kGPU_Backend, |
| 174 | kPDF_Backend, |
| 175 | }; |
| 176 | |
bsalomon@google.com | 508824b | 2011-12-13 16:49:49 +0000 | [diff] [blame] | 177 | class GLHelper { |
| 178 | public: |
| 179 | GLHelper() { |
| 180 | } |
| 181 | |
| 182 | bool init(SkGLContext* glCtx, int width, int height) { |
| 183 | GrContext* grCtx; |
| 184 | GrRenderTarget* rt; |
| 185 | if (glCtx->init(width, height)) { |
| 186 | GrPlatform3DContext ctx = |
| 187 | reinterpret_cast<GrPlatform3DContext>(glCtx->gl()); |
| 188 | grCtx = GrContext::Create(kOpenGL_Shaders_GrEngine, ctx); |
| 189 | if (NULL != grCtx) { |
| 190 | GrPlatformRenderTargetDesc desc; |
| 191 | desc.fConfig = kSkia8888_PM_GrPixelConfig; |
| 192 | desc.fWidth = width; |
| 193 | desc.fHeight = height; |
| 194 | desc.fStencilBits = 8; |
| 195 | desc.fRenderTargetHandle = glCtx->getFBOID(); |
| 196 | rt = grCtx->createPlatformRenderTarget(desc); |
| 197 | if (NULL == rt) { |
| 198 | grCtx->unref(); |
| 199 | return false; |
| 200 | } |
| 201 | } |
| 202 | } else { |
| 203 | return false; |
| 204 | } |
| 205 | glCtx->ref(); |
| 206 | fGLContext.reset(glCtx); |
| 207 | fGrContext.reset(grCtx); |
| 208 | fRenderTarget.reset(rt); |
| 209 | return true; |
| 210 | } |
| 211 | |
robertphillips@google.com | 0da3719 | 2012-03-19 14:42:13 +0000 | [diff] [blame^] | 212 | void cleanup() { |
| 213 | fGLContext.reset(NULL); |
| 214 | fGrContext.reset(NULL); |
| 215 | fRenderTarget.reset(NULL); |
| 216 | } |
| 217 | |
bsalomon@google.com | 508824b | 2011-12-13 16:49:49 +0000 | [diff] [blame] | 218 | bool isValid() { |
| 219 | return NULL != fGLContext.get(); |
| 220 | } |
| 221 | |
| 222 | SkGLContext* glContext() { |
| 223 | return fGLContext.get(); |
| 224 | } |
| 225 | |
| 226 | GrRenderTarget* renderTarget() { |
| 227 | return fRenderTarget.get(); |
| 228 | } |
| 229 | |
| 230 | GrContext* grContext() { |
| 231 | return fGrContext.get(); |
| 232 | } |
| 233 | private: |
| 234 | SkAutoTUnref<SkGLContext> fGLContext; |
| 235 | SkAutoTUnref<GrContext> fGrContext; |
| 236 | SkAutoTUnref<GrRenderTarget> fRenderTarget; |
| 237 | }; |
| 238 | |
| 239 | static GLHelper gRealGLHelper; |
| 240 | static GLHelper gNullGLHelper; |
robertphillips@google.com | 0da3719 | 2012-03-19 14:42:13 +0000 | [diff] [blame^] | 241 | static GLHelper gDebugGLHelper; |
bsalomon@google.com | 508824b | 2011-12-13 16:49:49 +0000 | [diff] [blame] | 242 | |
mike@reedtribe.org | a9015f8 | 2011-05-17 02:25:05 +0000 | [diff] [blame] | 243 | static SkDevice* make_device(SkBitmap::Config config, const SkIPoint& size, |
bsalomon@google.com | 508824b | 2011-12-13 16:49:49 +0000 | [diff] [blame] | 244 | Backend backend, GLHelper* glHelper) { |
mike@reedtribe.org | a9015f8 | 2011-05-17 02:25:05 +0000 | [diff] [blame] | 245 | SkDevice* device = NULL; |
| 246 | SkBitmap bitmap; |
| 247 | bitmap.setConfig(config, size.fX, size.fY); |
bungeman@google.com | d1a416a | 2011-05-18 18:37:07 +0000 | [diff] [blame] | 248 | |
mike@reedtribe.org | a9015f8 | 2011-05-17 02:25:05 +0000 | [diff] [blame] | 249 | switch (backend) { |
| 250 | case kRaster_Backend: |
| 251 | bitmap.allocPixels(); |
| 252 | erase(bitmap); |
reed@google.com | af951c9 | 2011-06-16 19:10:39 +0000 | [diff] [blame] | 253 | device = new SkDevice(bitmap); |
mike@reedtribe.org | a9015f8 | 2011-05-17 02:25:05 +0000 | [diff] [blame] | 254 | break; |
| 255 | case kGPU_Backend: |
bsalomon@google.com | 508824b | 2011-12-13 16:49:49 +0000 | [diff] [blame] | 256 | device = new SkGpuDevice(glHelper->grContext(), |
| 257 | glHelper->renderTarget()); |
mike@reedtribe.org | a9015f8 | 2011-05-17 02:25:05 +0000 | [diff] [blame] | 258 | break; |
| 259 | case kPDF_Backend: |
| 260 | default: |
| 261 | SkASSERT(!"unsupported"); |
| 262 | } |
| 263 | return device; |
| 264 | } |
| 265 | |
reed@android.com | 4bc1983 | 2009-01-19 20:08:35 +0000 | [diff] [blame] | 266 | static const struct { |
| 267 | SkBitmap::Config fConfig; |
| 268 | const char* fName; |
mike@reedtribe.org | a9015f8 | 2011-05-17 02:25:05 +0000 | [diff] [blame] | 269 | Backend fBackend; |
bsalomon@google.com | 508824b | 2011-12-13 16:49:49 +0000 | [diff] [blame] | 270 | GLHelper* fGLHelper; |
reed@android.com | 4bc1983 | 2009-01-19 20:08:35 +0000 | [diff] [blame] | 271 | } gConfigs[] = { |
bsalomon@google.com | 508824b | 2011-12-13 16:49:49 +0000 | [diff] [blame] | 272 | { SkBitmap::kARGB_8888_Config, "8888", kRaster_Backend, NULL }, |
| 273 | { SkBitmap::kRGB_565_Config, "565", kRaster_Backend, NULL }, |
| 274 | { SkBitmap::kARGB_8888_Config, "GPU", kGPU_Backend, &gRealGLHelper }, |
| 275 | { SkBitmap::kARGB_8888_Config, "NULLGPU", kGPU_Backend, &gNullGLHelper }, |
reed@android.com | 4bc1983 | 2009-01-19 20:08:35 +0000 | [diff] [blame] | 276 | }; |
| 277 | |
reed@android.com | 4c7d3d6 | 2009-01-21 03:15:13 +0000 | [diff] [blame] | 278 | static int findConfig(const char config[]) { |
| 279 | for (size_t i = 0; i < SK_ARRAY_COUNT(gConfigs); i++) { |
| 280 | if (!strcmp(config, gConfigs[i].fName)) { |
| 281 | return i; |
| 282 | } |
| 283 | } |
| 284 | return -1; |
| 285 | } |
| 286 | |
tomhudson@google.com | 8382394 | 2011-10-28 14:59:07 +0000 | [diff] [blame] | 287 | static void determine_gpu_context_size(SkTDict<const char*>& defineDict, |
| 288 | int* contextWidth, |
| 289 | int* contextHeight) { |
| 290 | Iter iter(&defineDict); |
| 291 | SkBenchmark* bench; |
| 292 | while ((bench = iter.next()) != NULL) { |
| 293 | SkIPoint dim = bench->getSize(); |
| 294 | if (*contextWidth < dim.fX) { |
| 295 | *contextWidth = dim.fX; |
| 296 | } |
| 297 | if (*contextHeight < dim.fY) { |
| 298 | *contextHeight = dim.fY; |
| 299 | } |
| 300 | } |
| 301 | } |
| 302 | |
reed@google.com | 1a7eabc | 2011-10-31 16:33:52 +0000 | [diff] [blame] | 303 | static bool skip_name(const SkTDArray<const char*> array, const char name[]) { |
| 304 | if (0 == array.count()) { |
| 305 | // no names, so don't skip anything |
| 306 | return false; |
| 307 | } |
| 308 | for (int i = 0; i < array.count(); ++i) { |
| 309 | if (strstr(name, array[i])) { |
| 310 | // found the name, so don't skip |
| 311 | return false; |
| 312 | } |
| 313 | } |
| 314 | return true; |
| 315 | } |
| 316 | |
reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 317 | int main (int argc, char * const argv[]) { |
reed@android.com | 3a859a0 | 2009-01-28 00:56:29 +0000 | [diff] [blame] | 318 | SkAutoGraphics ag; |
bungeman@google.com | d1a416a | 2011-05-18 18:37:07 +0000 | [diff] [blame] | 319 | |
reed@android.com | e9d0060 | 2009-09-02 21:12:42 +0000 | [diff] [blame] | 320 | SkTDict<const char*> defineDict(1024); |
reed@android.com | 4bc1983 | 2009-01-19 20:08:35 +0000 | [diff] [blame] | 321 | int repeatDraw = 1; |
| 322 | int forceAlpha = 0xFF; |
| 323 | bool forceAA = true; |
reed@android.com | 29348cb | 2009-08-04 18:17:15 +0000 | [diff] [blame] | 324 | bool forceFilter = false; |
reed@android.com | 4e635f9 | 2009-10-19 17:39:46 +0000 | [diff] [blame] | 325 | SkTriState::State forceDither = SkTriState::kDefault; |
bungeman@google.com | be9ad4e | 2011-06-07 19:16:02 +0000 | [diff] [blame] | 326 | bool timerWall = false; |
| 327 | bool timerCpu = true; |
| 328 | bool timerGpu = true; |
reed@android.com | 387359e | 2009-08-04 01:51:09 +0000 | [diff] [blame] | 329 | bool doScale = false; |
reed@android.com | 4c7d3d6 | 2009-01-21 03:15:13 +0000 | [diff] [blame] | 330 | bool doRotate = false; |
| 331 | bool doClip = false; |
agl@chromium.org | 652807b | 2010-04-27 15:47:34 +0000 | [diff] [blame] | 332 | bool hasStrokeWidth = false; |
| 333 | float strokeWidth; |
reed@google.com | 1a7eabc | 2011-10-31 16:33:52 +0000 | [diff] [blame] | 334 | SkTDArray<const char*> fMatches; |
bungeman@google.com | d1a416a | 2011-05-18 18:37:07 +0000 | [diff] [blame] | 335 | |
reed@android.com | b398fe8 | 2009-01-07 11:47:57 +0000 | [diff] [blame] | 336 | SkString outDir; |
reed@android.com | 387359e | 2009-08-04 01:51:09 +0000 | [diff] [blame] | 337 | SkBitmap::Config outConfig = SkBitmap::kNo_Config; |
bsalomon@google.com | cadbcb8 | 2012-01-06 19:22:11 +0000 | [diff] [blame] | 338 | GLHelper* glHelper = NULL; |
reed@android.com | 387359e | 2009-08-04 01:51:09 +0000 | [diff] [blame] | 339 | const char* configName = ""; |
mike@reedtribe.org | a9015f8 | 2011-05-17 02:25:05 +0000 | [diff] [blame] | 340 | Backend backend = kRaster_Backend; // for warning |
reed@android.com | 387359e | 2009-08-04 01:51:09 +0000 | [diff] [blame] | 341 | int configCount = SK_ARRAY_COUNT(gConfigs); |
bungeman@google.com | d1a416a | 2011-05-18 18:37:07 +0000 | [diff] [blame] | 342 | |
reed@android.com | b398fe8 | 2009-01-07 11:47:57 +0000 | [diff] [blame] | 343 | char* const* stop = argv + argc; |
| 344 | for (++argv; argv < stop; ++argv) { |
| 345 | if (strcmp(*argv, "-o") == 0) { |
| 346 | argv++; |
| 347 | if (argv < stop && **argv) { |
| 348 | outDir.set(*argv); |
| 349 | if (outDir.c_str()[outDir.size() - 1] != '/') { |
| 350 | outDir.append("/"); |
| 351 | } |
| 352 | } |
reed@android.com | 4bc1983 | 2009-01-19 20:08:35 +0000 | [diff] [blame] | 353 | } else if (strcmp(*argv, "-repeat") == 0) { |
| 354 | argv++; |
| 355 | if (argv < stop) { |
| 356 | repeatDraw = atoi(*argv); |
| 357 | if (repeatDraw < 1) { |
| 358 | repeatDraw = 1; |
| 359 | } |
| 360 | } else { |
reed@android.com | 29348cb | 2009-08-04 18:17:15 +0000 | [diff] [blame] | 361 | log_error("missing arg for -repeat\n"); |
reed@android.com | 4bc1983 | 2009-01-19 20:08:35 +0000 | [diff] [blame] | 362 | return -1; |
| 363 | } |
bungeman@google.com | be9ad4e | 2011-06-07 19:16:02 +0000 | [diff] [blame] | 364 | } else if (strcmp(*argv, "-timers") == 0) { |
| 365 | argv++; |
| 366 | if (argv < stop) { |
| 367 | timerWall = false; |
| 368 | timerCpu = false; |
| 369 | timerGpu = false; |
| 370 | for (char* t = *argv; *t; ++t) { |
| 371 | switch (*t) { |
| 372 | case 'w': timerWall = true; break; |
| 373 | case 'c': timerCpu = true; break; |
| 374 | case 'g': timerGpu = true; break; |
| 375 | } |
| 376 | } |
| 377 | } else { |
| 378 | log_error("missing arg for -timers\n"); |
| 379 | return -1; |
| 380 | } |
reed@android.com | 4c7d3d6 | 2009-01-21 03:15:13 +0000 | [diff] [blame] | 381 | } else if (!strcmp(*argv, "-rotate")) { |
| 382 | doRotate = true; |
reed@android.com | 387359e | 2009-08-04 01:51:09 +0000 | [diff] [blame] | 383 | } else if (!strcmp(*argv, "-scale")) { |
| 384 | doScale = true; |
reed@android.com | 4c7d3d6 | 2009-01-21 03:15:13 +0000 | [diff] [blame] | 385 | } else if (!strcmp(*argv, "-clip")) { |
| 386 | doClip = true; |
reed@android.com | 4bc1983 | 2009-01-19 20:08:35 +0000 | [diff] [blame] | 387 | } else if (strcmp(*argv, "-forceAA") == 0) { |
reed@android.com | 29348cb | 2009-08-04 18:17:15 +0000 | [diff] [blame] | 388 | if (!parse_bool_arg(++argv, stop, &forceAA)) { |
| 389 | log_error("missing arg for -forceAA\n"); |
| 390 | return -1; |
| 391 | } |
| 392 | } else if (strcmp(*argv, "-forceFilter") == 0) { |
| 393 | if (!parse_bool_arg(++argv, stop, &forceFilter)) { |
| 394 | log_error("missing arg for -forceFilter\n"); |
| 395 | return -1; |
| 396 | } |
reed@android.com | 4e635f9 | 2009-10-19 17:39:46 +0000 | [diff] [blame] | 397 | } else if (strcmp(*argv, "-forceDither") == 0) { |
| 398 | bool tmp; |
| 399 | if (!parse_bool_arg(++argv, stop, &tmp)) { |
| 400 | log_error("missing arg for -forceDither\n"); |
| 401 | return -1; |
| 402 | } |
| 403 | forceDither = tmp ? SkTriState::kTrue : SkTriState::kFalse; |
reed@android.com | 4bc1983 | 2009-01-19 20:08:35 +0000 | [diff] [blame] | 404 | } else if (strcmp(*argv, "-forceBlend") == 0) { |
reed@android.com | 29348cb | 2009-08-04 18:17:15 +0000 | [diff] [blame] | 405 | bool wantAlpha = false; |
| 406 | if (!parse_bool_arg(++argv, stop, &wantAlpha)) { |
| 407 | log_error("missing arg for -forceBlend\n"); |
| 408 | return -1; |
| 409 | } |
| 410 | forceAlpha = wantAlpha ? 0x80 : 0xFF; |
agl@chromium.org | 652807b | 2010-04-27 15:47:34 +0000 | [diff] [blame] | 411 | } else if (strcmp(*argv, "-strokeWidth") == 0) { |
| 412 | argv++; |
| 413 | if (argv < stop) { |
| 414 | const char *strokeWidthStr = *argv; |
| 415 | if (sscanf(strokeWidthStr, "%f", &strokeWidth) != 1) { |
| 416 | log_error("bad arg for -strokeWidth\n"); |
| 417 | return -1; |
| 418 | } |
| 419 | hasStrokeWidth = true; |
| 420 | } else { |
| 421 | log_error("missing arg for -strokeWidth\n"); |
| 422 | return -1; |
| 423 | } |
reed@android.com | 387359e | 2009-08-04 01:51:09 +0000 | [diff] [blame] | 424 | } else if (strcmp(*argv, "-match") == 0) { |
| 425 | argv++; |
| 426 | if (argv < stop) { |
reed@google.com | 1a7eabc | 2011-10-31 16:33:52 +0000 | [diff] [blame] | 427 | *fMatches.append() = *argv; |
reed@android.com | 387359e | 2009-08-04 01:51:09 +0000 | [diff] [blame] | 428 | } else { |
reed@android.com | 29348cb | 2009-08-04 18:17:15 +0000 | [diff] [blame] | 429 | log_error("missing arg for -match\n"); |
reed@android.com | 387359e | 2009-08-04 01:51:09 +0000 | [diff] [blame] | 430 | return -1; |
reed@android.com | 4c7d3d6 | 2009-01-21 03:15:13 +0000 | [diff] [blame] | 431 | } |
reed@android.com | 387359e | 2009-08-04 01:51:09 +0000 | [diff] [blame] | 432 | } else if (strcmp(*argv, "-config") == 0) { |
| 433 | argv++; |
| 434 | if (argv < stop) { |
| 435 | int index = findConfig(*argv); |
| 436 | if (index >= 0) { |
| 437 | outConfig = gConfigs[index].fConfig; |
| 438 | configName = gConfigs[index].fName; |
mike@reedtribe.org | a9015f8 | 2011-05-17 02:25:05 +0000 | [diff] [blame] | 439 | backend = gConfigs[index].fBackend; |
bsalomon@google.com | 508824b | 2011-12-13 16:49:49 +0000 | [diff] [blame] | 440 | glHelper = gConfigs[index].fGLHelper; |
reed@android.com | 387359e | 2009-08-04 01:51:09 +0000 | [diff] [blame] | 441 | configCount = 1; |
| 442 | } else { |
reed@android.com | 29348cb | 2009-08-04 18:17:15 +0000 | [diff] [blame] | 443 | SkString str; |
| 444 | str.printf("unrecognized config %s\n", *argv); |
| 445 | log_error(str); |
reed@android.com | 387359e | 2009-08-04 01:51:09 +0000 | [diff] [blame] | 446 | return -1; |
| 447 | } |
| 448 | } else { |
reed@android.com | 29348cb | 2009-08-04 18:17:15 +0000 | [diff] [blame] | 449 | log_error("missing arg for -config\n"); |
reed@android.com | 387359e | 2009-08-04 01:51:09 +0000 | [diff] [blame] | 450 | return -1; |
| 451 | } |
reed@android.com | 0c9da39 | 2010-02-22 19:50:13 +0000 | [diff] [blame] | 452 | } else if (strlen(*argv) > 2 && strncmp(*argv, "-D", 2) == 0) { |
reed@android.com | e9d0060 | 2009-09-02 21:12:42 +0000 | [diff] [blame] | 453 | argv++; |
reed@android.com | 0c9da39 | 2010-02-22 19:50:13 +0000 | [diff] [blame] | 454 | if (argv < stop) { |
reed@android.com | e9d0060 | 2009-09-02 21:12:42 +0000 | [diff] [blame] | 455 | defineDict.set(argv[-1] + 2, *argv); |
| 456 | } else { |
| 457 | log_error("incomplete '-Dfoo bar' definition\n"); |
| 458 | return -1; |
| 459 | } |
reed@android.com | 387359e | 2009-08-04 01:51:09 +0000 | [diff] [blame] | 460 | } else { |
reed@android.com | 29348cb | 2009-08-04 18:17:15 +0000 | [diff] [blame] | 461 | SkString str; |
| 462 | str.printf("unrecognized arg %s\n", *argv); |
| 463 | log_error(str); |
reed@android.com | 387359e | 2009-08-04 01:51:09 +0000 | [diff] [blame] | 464 | return -1; |
reed@android.com | b398fe8 | 2009-01-07 11:47:57 +0000 | [diff] [blame] | 465 | } |
| 466 | } |
bungeman@google.com | d1a416a | 2011-05-18 18:37:07 +0000 | [diff] [blame] | 467 | |
reed@google.com | 3b14dc1 | 2011-04-04 14:31:36 +0000 | [diff] [blame] | 468 | // report our current settings |
| 469 | { |
| 470 | SkString str; |
bungeman@google.com | a5d4841 | 2011-06-15 17:25:46 +0000 | [diff] [blame] | 471 | str.printf("skia bench: alpha=0x%02X antialias=%d filter=%d", |
reed@google.com | 3b14dc1 | 2011-04-04 14:31:36 +0000 | [diff] [blame] | 472 | forceAlpha, forceAA, forceFilter); |
bungeman@google.com | a5d4841 | 2011-06-15 17:25:46 +0000 | [diff] [blame] | 473 | str.appendf(" rotate=%d scale=%d clip=%d", |
| 474 | doRotate, doScale, doClip); |
| 475 | |
| 476 | const char * ditherName; |
| 477 | switch (forceDither) { |
| 478 | case SkTriState::kDefault: ditherName = "default"; break; |
| 479 | case SkTriState::kTrue: ditherName = "true"; break; |
| 480 | case SkTriState::kFalse: ditherName = "false"; break; |
| 481 | default: ditherName = "<invalid>"; break; |
| 482 | } |
| 483 | str.appendf(" dither=%s", ditherName); |
| 484 | |
| 485 | if (hasStrokeWidth) { |
| 486 | str.appendf(" strokeWidth=%f", strokeWidth); |
| 487 | } else { |
| 488 | str.append(" strokeWidth=none"); |
| 489 | } |
| 490 | |
| 491 | #if defined(SK_SCALAR_IS_FLOAT) |
| 492 | str.append(" scalar=float"); |
| 493 | #elif defined(SK_SCALAR_IS_FIXED) |
| 494 | str.append(" scalar=fixed"); |
| 495 | #endif |
| 496 | |
| 497 | #if defined(SK_BUILD_FOR_WIN32) |
| 498 | str.append(" system=WIN32"); |
| 499 | #elif defined(SK_BUILD_FOR_MAC) |
| 500 | str.append(" system=MAC"); |
| 501 | #elif defined(SK_BUILD_FOR_ANDROID) |
| 502 | str.append(" system=ANDROID"); |
| 503 | #elif defined(SK_BUILD_FOR_UNIX) |
| 504 | str.append(" system=UNIX"); |
| 505 | #else |
| 506 | str.append(" system=other"); |
| 507 | #endif |
| 508 | |
| 509 | #if defined(SK_DEBUG) |
| 510 | str.append(" DEBUG"); |
| 511 | #endif |
| 512 | str.append("\n"); |
reed@google.com | 3b14dc1 | 2011-04-04 14:31:36 +0000 | [diff] [blame] | 513 | log_progress(str); |
| 514 | } |
bsalomon@google.com | 508824b | 2011-12-13 16:49:49 +0000 | [diff] [blame] | 515 | |
bungeman@google.com | a5d4841 | 2011-06-15 17:25:46 +0000 | [diff] [blame] | 516 | //Don't do GL when fixed. |
| 517 | #if !defined(SK_SCALAR_IS_FIXED) |
tomhudson@google.com | 8382394 | 2011-10-28 14:59:07 +0000 | [diff] [blame] | 518 | int contextWidth = 1024; |
| 519 | int contextHeight = 1024; |
| 520 | determine_gpu_context_size(defineDict, &contextWidth, &contextHeight); |
bsalomon@google.com | 508824b | 2011-12-13 16:49:49 +0000 | [diff] [blame] | 521 | SkAutoTUnref<SkGLContext> realGLCtx(new SkNativeGLContext); |
| 522 | SkAutoTUnref<SkGLContext> nullGLCtx(new SkNullGLContext); |
robertphillips@google.com | 0da3719 | 2012-03-19 14:42:13 +0000 | [diff] [blame^] | 523 | SkAutoTUnref<SkGLContext> debugGLCtx(new SkDebugGLContext); |
bsalomon@google.com | 508824b | 2011-12-13 16:49:49 +0000 | [diff] [blame] | 524 | gRealGLHelper.init(realGLCtx.get(), contextWidth, contextHeight); |
| 525 | gNullGLHelper.init(nullGLCtx.get(), contextWidth, contextHeight); |
robertphillips@google.com | 0da3719 | 2012-03-19 14:42:13 +0000 | [diff] [blame^] | 526 | gDebugGLHelper.init(debugGLCtx.get(), contextWidth, contextHeight); |
bungeman@google.com | a5d4841 | 2011-06-15 17:25:46 +0000 | [diff] [blame] | 527 | #endif |
bsalomon@google.com | 508824b | 2011-12-13 16:49:49 +0000 | [diff] [blame] | 528 | BenchTimer timer = BenchTimer(gRealGLHelper.glContext()); |
bsalomon@google.com | 7491372 | 2011-10-27 20:44:19 +0000 | [diff] [blame] | 529 | |
reed@android.com | e9d0060 | 2009-09-02 21:12:42 +0000 | [diff] [blame] | 530 | Iter iter(&defineDict); |
reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 531 | SkBenchmark* bench; |
| 532 | while ((bench = iter.next()) != NULL) { |
reed@android.com | b398fe8 | 2009-01-07 11:47:57 +0000 | [diff] [blame] | 533 | SkIPoint dim = bench->getSize(); |
| 534 | if (dim.fX <= 0 || dim.fY <= 0) { |
| 535 | continue; |
| 536 | } |
reed@android.com | b398fe8 | 2009-01-07 11:47:57 +0000 | [diff] [blame] | 537 | |
reed@android.com | 4bc1983 | 2009-01-19 20:08:35 +0000 | [diff] [blame] | 538 | bench->setForceAlpha(forceAlpha); |
| 539 | bench->setForceAA(forceAA); |
reed@android.com | 29348cb | 2009-08-04 18:17:15 +0000 | [diff] [blame] | 540 | bench->setForceFilter(forceFilter); |
reed@android.com | 4e635f9 | 2009-10-19 17:39:46 +0000 | [diff] [blame] | 541 | bench->setDither(forceDither); |
agl@chromium.org | 652807b | 2010-04-27 15:47:34 +0000 | [diff] [blame] | 542 | if (hasStrokeWidth) { |
| 543 | bench->setStrokeWidth(strokeWidth); |
| 544 | } |
bungeman@google.com | d1a416a | 2011-05-18 18:37:07 +0000 | [diff] [blame] | 545 | |
reed@android.com | 387359e | 2009-08-04 01:51:09 +0000 | [diff] [blame] | 546 | // only run benchmarks if their name contains matchStr |
reed@google.com | 1a7eabc | 2011-10-31 16:33:52 +0000 | [diff] [blame] | 547 | if (skip_name(fMatches, bench->getName())) { |
reed@android.com | 387359e | 2009-08-04 01:51:09 +0000 | [diff] [blame] | 548 | continue; |
| 549 | } |
bungeman@google.com | d1a416a | 2011-05-18 18:37:07 +0000 | [diff] [blame] | 550 | |
reed@android.com | 29348cb | 2009-08-04 18:17:15 +0000 | [diff] [blame] | 551 | { |
| 552 | SkString str; |
reed@google.com | d34658a | 2011-04-11 13:12:51 +0000 | [diff] [blame] | 553 | str.printf("running bench [%d %d] %28s", dim.fX, dim.fY, |
reed@android.com | 0c9da39 | 2010-02-22 19:50:13 +0000 | [diff] [blame] | 554 | bench->getName()); |
reed@android.com | 29348cb | 2009-08-04 18:17:15 +0000 | [diff] [blame] | 555 | log_progress(str); |
| 556 | } |
bungeman@google.com | d1a416a | 2011-05-18 18:37:07 +0000 | [diff] [blame] | 557 | |
reed@android.com | 4bc1983 | 2009-01-19 20:08:35 +0000 | [diff] [blame] | 558 | for (int configIndex = 0; configIndex < configCount; configIndex++) { |
| 559 | if (configCount > 1) { |
| 560 | outConfig = gConfigs[configIndex].fConfig; |
| 561 | configName = gConfigs[configIndex].fName; |
mike@reedtribe.org | a9015f8 | 2011-05-17 02:25:05 +0000 | [diff] [blame] | 562 | backend = gConfigs[configIndex].fBackend; |
bsalomon@google.com | 508824b | 2011-12-13 16:49:49 +0000 | [diff] [blame] | 563 | glHelper = gConfigs[configIndex].fGLHelper; |
reed@android.com | 4bc1983 | 2009-01-19 20:08:35 +0000 | [diff] [blame] | 564 | } |
bsalomon@google.com | 508824b | 2011-12-13 16:49:49 +0000 | [diff] [blame] | 565 | |
| 566 | if (kGPU_Backend == backend && |
| 567 | (NULL == glHelper || !glHelper->isValid())) { |
mike@reedtribe.org | a9015f8 | 2011-05-17 02:25:05 +0000 | [diff] [blame] | 568 | continue; |
| 569 | } |
bungeman@google.com | d1a416a | 2011-05-18 18:37:07 +0000 | [diff] [blame] | 570 | |
bsalomon@google.com | 508824b | 2011-12-13 16:49:49 +0000 | [diff] [blame] | 571 | SkDevice* device = make_device(outConfig, dim, backend, glHelper); |
mike@reedtribe.org | a9015f8 | 2011-05-17 02:25:05 +0000 | [diff] [blame] | 572 | SkCanvas canvas(device); |
| 573 | device->unref(); |
bungeman@google.com | d1a416a | 2011-05-18 18:37:07 +0000 | [diff] [blame] | 574 | |
reed@android.com | 4c7d3d6 | 2009-01-21 03:15:13 +0000 | [diff] [blame] | 575 | if (doClip) { |
| 576 | performClip(&canvas, dim.fX, dim.fY); |
| 577 | } |
reed@android.com | 387359e | 2009-08-04 01:51:09 +0000 | [diff] [blame] | 578 | if (doScale) { |
| 579 | performScale(&canvas, dim.fX, dim.fY); |
| 580 | } |
reed@android.com | 4c7d3d6 | 2009-01-21 03:15:13 +0000 | [diff] [blame] | 581 | if (doRotate) { |
| 582 | performRotate(&canvas, dim.fX, dim.fY); |
| 583 | } |
bsalomon@google.com | 508824b | 2011-12-13 16:49:49 +0000 | [diff] [blame] | 584 | |
bungeman@google.com | d1a416a | 2011-05-18 18:37:07 +0000 | [diff] [blame] | 585 | //warm up caches if needed |
reed@android.com | eca4836 | 2010-12-20 18:34:17 +0000 | [diff] [blame] | 586 | if (repeatDraw > 1) { |
| 587 | SkAutoCanvasRestore acr(&canvas, true); |
| 588 | bench->draw(&canvas); |
bsalomon@google.com | 508824b | 2011-12-13 16:49:49 +0000 | [diff] [blame] | 589 | if (glHelper) { |
| 590 | glHelper->grContext()->flush(); |
| 591 | SK_GL(*glHelper->glContext(), Finish()); |
bungeman@google.com | d1a416a | 2011-05-18 18:37:07 +0000 | [diff] [blame] | 592 | } |
reed@android.com | eca4836 | 2010-12-20 18:34:17 +0000 | [diff] [blame] | 593 | } |
bungeman@google.com | d1a416a | 2011-05-18 18:37:07 +0000 | [diff] [blame] | 594 | |
bungeman@google.com | be9ad4e | 2011-06-07 19:16:02 +0000 | [diff] [blame] | 595 | timer.start(); |
reed@android.com | 4bc1983 | 2009-01-19 20:08:35 +0000 | [diff] [blame] | 596 | for (int i = 0; i < repeatDraw; i++) { |
mike@reedtribe.org | a9015f8 | 2011-05-17 02:25:05 +0000 | [diff] [blame] | 597 | SkAutoCanvasRestore acr(&canvas, true); |
| 598 | bench->draw(&canvas); |
bsalomon@google.com | 508824b | 2011-12-13 16:49:49 +0000 | [diff] [blame] | 599 | if (glHelper) { |
| 600 | glHelper->grContext()->flush(); |
reed@google.com | 25df888 | 2011-07-14 19:03:58 +0000 | [diff] [blame] | 601 | } |
| 602 | } |
bsalomon@google.com | 508824b | 2011-12-13 16:49:49 +0000 | [diff] [blame] | 603 | if (glHelper) { |
| 604 | SK_GL(*glHelper->glContext(), Finish()); |
bsalomon@google.com | 788e247 | 2011-10-19 20:56:30 +0000 | [diff] [blame] | 605 | } |
bsalomon@google.com | 788e247 | 2011-10-19 20:56:30 +0000 | [diff] [blame] | 606 | timer.end(); |
bungeman@google.com | d1a416a | 2011-05-18 18:37:07 +0000 | [diff] [blame] | 607 | |
reed@android.com | 4bc1983 | 2009-01-19 20:08:35 +0000 | [diff] [blame] | 608 | if (repeatDraw > 1) { |
reed@android.com | 29348cb | 2009-08-04 18:17:15 +0000 | [diff] [blame] | 609 | SkString str; |
bungeman@google.com | be9ad4e | 2011-06-07 19:16:02 +0000 | [diff] [blame] | 610 | str.printf(" %4s:", configName); |
| 611 | if (timerWall) { |
| 612 | str.appendf(" msecs = %6.2f", timer.fWall / repeatDraw); |
| 613 | } |
| 614 | if (timerCpu) { |
| 615 | str.appendf(" cmsecs = %6.2f", timer.fCpu / repeatDraw); |
| 616 | } |
bsalomon@google.com | 508824b | 2011-12-13 16:49:49 +0000 | [diff] [blame] | 617 | if (timerGpu && glHelper && timer.fGpu > 0) { |
bungeman@google.com | be9ad4e | 2011-06-07 19:16:02 +0000 | [diff] [blame] | 618 | str.appendf(" gmsecs = %6.2f", timer.fGpu / repeatDraw); |
| 619 | } |
reed@android.com | 29348cb | 2009-08-04 18:17:15 +0000 | [diff] [blame] | 620 | log_progress(str); |
reed@android.com | 4bc1983 | 2009-01-19 20:08:35 +0000 | [diff] [blame] | 621 | } |
reed@android.com | 4c7d3d6 | 2009-01-21 03:15:13 +0000 | [diff] [blame] | 622 | if (outDir.size() > 0) { |
mike@reedtribe.org | a9015f8 | 2011-05-17 02:25:05 +0000 | [diff] [blame] | 623 | saveFile(bench->getName(), configName, outDir.c_str(), |
| 624 | device->accessBitmap(false)); |
reed@android.com | 4c7d3d6 | 2009-01-21 03:15:13 +0000 | [diff] [blame] | 625 | } |
reed@android.com | 4bc1983 | 2009-01-19 20:08:35 +0000 | [diff] [blame] | 626 | } |
reed@android.com | 29348cb | 2009-08-04 18:17:15 +0000 | [diff] [blame] | 627 | log_progress("\n"); |
reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 628 | } |
bsalomon@google.com | 971d0c8 | 2011-08-19 17:22:05 +0000 | [diff] [blame] | 629 | |
robertphillips@google.com | 0da3719 | 2012-03-19 14:42:13 +0000 | [diff] [blame^] | 630 | // need to clean up here rather than post-main to allow leak detection to work |
| 631 | gDebugGLHelper.cleanup(); |
| 632 | |
reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 633 | return 0; |
| 634 | } |