| epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | /* |
| 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 | */ |
| bsalomon@google.com | 971d0c8 | 2011-08-19 17:22:05 +0000 | [diff] [blame] | 7 | |
| mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 8 | #include "BenchTimer.h" |
| scroggo@google.com | 9a41252 | 2012-09-07 15:21:18 +0000 | [diff] [blame] | 9 | #include "SkBenchLogger.h" |
| bsalomon@google.com | 971d0c8 | 2011-08-19 17:22:05 +0000 | [diff] [blame] | 10 | #include "SkBenchmark.h" |
| robertphillips@google.com | 7367225 | 2013-08-29 12:40:26 +0000 | [diff] [blame] | 11 | #include "SkBitmapDevice.h" |
| reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 12 | #include "SkCanvas.h" |
| mtklein@google.com | 78d0379 | 2013-09-10 19:42:07 +0000 | [diff] [blame] | 13 | #include "SkColorPriv.h" |
| sglez@google.com | 586db93 | 2013-07-24 17:24:23 +0000 | [diff] [blame] | 14 | #include "SkCommandLineFlags.h" |
| bsalomon@google.com | 82a7bfc | 2012-04-16 19:11:17 +0000 | [diff] [blame] | 15 | #include "SkDeferredCanvas.h" |
| reed@android.com | 3a859a0 | 2009-01-28 00:56:29 +0000 | [diff] [blame] | 16 | #include "SkGraphics.h" |
| reed@android.com | b398fe8 | 2009-01-07 11:47:57 +0000 | [diff] [blame] | 17 | #include "SkImageEncoder.h" |
| mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 18 | #include "SkOSFile.h" |
| reed@android.com | 6c924ad | 2009-03-31 03:48:49 +0000 | [diff] [blame] | 19 | #include "SkPicture.h" |
| reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 20 | #include "SkString.h" |
| reed@android.com | 29348cb | 2009-08-04 18:17:15 +0000 | [diff] [blame] | 21 | |
| djsollen@google.com | db490e9 | 2013-11-20 13:15:40 +0000 | [diff] [blame] | 22 | #if SK_SUPPORT_GPU |
| 23 | #include "GrContext.h" |
| 24 | #include "GrContextFactory.h" |
| 25 | #include "GrRenderTarget.h" |
| 26 | #include "SkGpuDevice.h" |
| 27 | #include "gl/GrGLDefines.h" |
| 28 | #else |
| 29 | class GrContext; |
| 30 | #endif // SK_SUPPORT_GPU |
| 31 | |
| mtklein@google.com | 9ef1d21 | 2013-09-13 20:39:50 +0000 | [diff] [blame] | 32 | #include <limits> |
| 33 | |
| mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 34 | enum BenchMode { |
| 35 | kNormal_BenchMode, |
| 36 | kDeferred_BenchMode, |
| 37 | kDeferredSilent_BenchMode, |
| 38 | kRecord_BenchMode, |
| 39 | kPictureRecord_BenchMode |
| keyar@chromium.org | fdd909c | 2012-07-20 23:03:42 +0000 | [diff] [blame] | 40 | }; |
| mtklein@google.com | dbd41c8 | 2013-09-13 20:11:09 +0000 | [diff] [blame] | 41 | const char* BenchMode_Name[] = { |
| 42 | "normal", "deferred", "deferredSilent", "record", "picturerecord" |
| 43 | }; |
| commit-bot@chromium.org | 515dcd3 | 2013-08-28 14:17:03 +0000 | [diff] [blame] | 44 | |
| borenet@google.com | 4e061d3 | 2013-09-16 19:12:19 +0000 | [diff] [blame] | 45 | static const char kDefaultsConfigStr[] = "defaults"; |
| 46 | |
| keyar@chromium.org | fdd909c | 2012-07-20 23:03:42 +0000 | [diff] [blame] | 47 | /////////////////////////////////////////////////////////////////////////////// |
| 48 | |
| reed@android.com | 6c924ad | 2009-03-31 03:48:49 +0000 | [diff] [blame] | 49 | static void erase(SkBitmap& bm) { |
| 50 | if (bm.config() == SkBitmap::kA8_Config) { |
| junov@google.com | dbfac8a | 2012-12-06 21:47:40 +0000 | [diff] [blame] | 51 | bm.eraseColor(SK_ColorTRANSPARENT); |
| reed@android.com | 6c924ad | 2009-03-31 03:48:49 +0000 | [diff] [blame] | 52 | } else { |
| 53 | bm.eraseColor(SK_ColorWHITE); |
| 54 | } |
| 55 | } |
| 56 | |
| reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 57 | class Iter { |
| 58 | public: |
| mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 59 | Iter() : fBench(BenchRegistry::Head()) {} |
| rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 60 | |
| reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 61 | SkBenchmark* next() { |
| 62 | if (fBench) { |
| 63 | BenchRegistry::Factory f = fBench->factory(); |
| 64 | fBench = fBench->next(); |
| mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 65 | return f(); |
| reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 66 | } |
| 67 | return NULL; |
| 68 | } |
| bungeman@google.com | d1a416a | 2011-05-18 18:37:07 +0000 | [diff] [blame] | 69 | |
| reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 70 | private: |
| 71 | const BenchRegistry* fBench; |
| 72 | }; |
| 73 | |
| bsalomon@google.com | 30e6d2c | 2012-08-13 14:03:31 +0000 | [diff] [blame] | 74 | class AutoPrePostDraw { |
| 75 | public: |
| 76 | AutoPrePostDraw(SkBenchmark* bench) : fBench(bench) { |
| 77 | fBench->preDraw(); |
| 78 | } |
| 79 | ~AutoPrePostDraw() { |
| 80 | fBench->postDraw(); |
| 81 | } |
| 82 | private: |
| 83 | SkBenchmark* fBench; |
| 84 | }; |
| 85 | |
| reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 86 | static void make_filename(const char name[], SkString* path) { |
| 87 | path->set(name); |
| 88 | for (int i = 0; name[i]; i++) { |
| 89 | switch (name[i]) { |
| 90 | case '/': |
| 91 | case '\\': |
| 92 | case ' ': |
| 93 | case ':': |
| 94 | path->writable_str()[i] = '-'; |
| 95 | break; |
| 96 | default: |
| 97 | break; |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | |
| reed@android.com | 4c7d3d6 | 2009-01-21 03:15:13 +0000 | [diff] [blame] | 102 | static void saveFile(const char name[], const char config[], const char dir[], |
| 103 | const SkBitmap& bm) { |
| reed@android.com | 4c7d3d6 | 2009-01-21 03:15:13 +0000 | [diff] [blame] | 104 | SkBitmap copy; |
| 105 | if (!bm.copyTo(©, SkBitmap::kARGB_8888_Config)) { |
| 106 | return; |
| 107 | } |
| rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 108 | |
| reed@android.com | f523e25 | 2009-01-26 23:15:37 +0000 | [diff] [blame] | 109 | if (bm.config() == SkBitmap::kA8_Config) { |
| 110 | // turn alpha into gray-scale |
| 111 | size_t size = copy.getSize() >> 2; |
| 112 | SkPMColor* p = copy.getAddr32(0, 0); |
| 113 | for (size_t i = 0; i < size; i++) { |
| 114 | int c = (*p >> SK_A32_SHIFT) & 0xFF; |
| 115 | c = 255 - c; |
| 116 | c |= (c << 24) | (c << 16) | (c << 8); |
| 117 | *p++ = c | (SK_A32_MASK << SK_A32_SHIFT); |
| 118 | } |
| 119 | } |
| rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 120 | |
| mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 121 | SkString filename; |
| 122 | make_filename(name, &filename); |
| 123 | filename.appendf("_%s.png", config); |
| 124 | SkString path = SkOSPath::SkPathJoin(dir, filename.c_str()); |
| 125 | ::remove(path.c_str()); |
| 126 | SkImageEncoder::EncodeFile(path.c_str(), copy, SkImageEncoder::kPNG_Type, 100); |
| reed@android.com | 4c7d3d6 | 2009-01-21 03:15:13 +0000 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | static void performClip(SkCanvas* canvas, int w, int h) { |
| 130 | SkRect r; |
| rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 131 | |
| reed@android.com | 4c7d3d6 | 2009-01-21 03:15:13 +0000 | [diff] [blame] | 132 | r.set(SkIntToScalar(10), SkIntToScalar(10), |
| 133 | SkIntToScalar(w*2/3), SkIntToScalar(h*2/3)); |
| 134 | canvas->clipRect(r, SkRegion::kIntersect_Op); |
| rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 135 | |
| reed@android.com | 4c7d3d6 | 2009-01-21 03:15:13 +0000 | [diff] [blame] | 136 | r.set(SkIntToScalar(w/3), SkIntToScalar(h/3), |
| 137 | SkIntToScalar(w-10), SkIntToScalar(h-10)); |
| 138 | canvas->clipRect(r, SkRegion::kXOR_Op); |
| 139 | } |
| 140 | |
| 141 | static void performRotate(SkCanvas* canvas, int w, int h) { |
| 142 | const SkScalar x = SkIntToScalar(w) / 2; |
| 143 | const SkScalar y = SkIntToScalar(h) / 2; |
| rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 144 | |
| reed@android.com | 4c7d3d6 | 2009-01-21 03:15:13 +0000 | [diff] [blame] | 145 | canvas->translate(x, y); |
| 146 | canvas->rotate(SkIntToScalar(35)); |
| 147 | canvas->translate(-x, -y); |
| 148 | } |
| 149 | |
| reed@android.com | 387359e | 2009-08-04 01:51:09 +0000 | [diff] [blame] | 150 | static void performScale(SkCanvas* canvas, int w, int h) { |
| 151 | const SkScalar x = SkIntToScalar(w) / 2; |
| 152 | const SkScalar y = SkIntToScalar(h) / 2; |
| rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 153 | |
| reed@android.com | 387359e | 2009-08-04 01:51:09 +0000 | [diff] [blame] | 154 | canvas->translate(x, y); |
| 155 | // just enough so we can't take the sprite case |
| 156 | canvas->scale(SK_Scalar1 * 99/100, SK_Scalar1 * 99/100); |
| 157 | canvas->translate(-x, -y); |
| 158 | } |
| 159 | |
| robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 160 | static SkBaseDevice* make_device(SkBitmap::Config config, const SkIPoint& size, |
| commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame^] | 161 | SkBenchmark::Backend backend, int sampleCount, GrContext* context) { |
| robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 162 | SkBaseDevice* device = NULL; |
| mike@reedtribe.org | a9015f8 | 2011-05-17 02:25:05 +0000 | [diff] [blame] | 163 | SkBitmap bitmap; |
| 164 | bitmap.setConfig(config, size.fX, size.fY); |
| rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 165 | |
| mike@reedtribe.org | a9015f8 | 2011-05-17 02:25:05 +0000 | [diff] [blame] | 166 | switch (backend) { |
| commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame^] | 167 | case SkBenchmark::kRaster_Backend: |
| mike@reedtribe.org | a9015f8 | 2011-05-17 02:25:05 +0000 | [diff] [blame] | 168 | bitmap.allocPixels(); |
| 169 | erase(bitmap); |
| robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 170 | device = SkNEW_ARGS(SkBitmapDevice, (bitmap)); |
| mike@reedtribe.org | a9015f8 | 2011-05-17 02:25:05 +0000 | [diff] [blame] | 171 | break; |
| bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 172 | #if SK_SUPPORT_GPU |
| commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame^] | 173 | case SkBenchmark::kGPU_Backend: { |
| bsalomon@google.com | cb26535 | 2013-02-22 16:13:16 +0000 | [diff] [blame] | 174 | GrTextureDesc desc; |
| 175 | desc.fConfig = kSkia8888_GrPixelConfig; |
| 176 | desc.fFlags = kRenderTarget_GrTextureFlagBit; |
| 177 | desc.fWidth = size.fX; |
| 178 | desc.fHeight = size.fY; |
| bsalomon@google.com | 8a70eef | 2013-03-19 13:58:55 +0000 | [diff] [blame] | 179 | desc.fSampleCnt = sampleCount; |
| bsalomon@google.com | cb26535 | 2013-02-22 16:13:16 +0000 | [diff] [blame] | 180 | SkAutoTUnref<GrTexture> texture(context->createUncachedTexture(desc, NULL, 0)); |
| 181 | if (!texture) { |
| 182 | return NULL; |
| 183 | } |
| 184 | device = SkNEW_ARGS(SkGpuDevice, (context, texture.get())); |
| mike@reedtribe.org | a9015f8 | 2011-05-17 02:25:05 +0000 | [diff] [blame] | 185 | break; |
| bsalomon@google.com | cb26535 | 2013-02-22 16:13:16 +0000 | [diff] [blame] | 186 | } |
| bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 187 | #endif |
| commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame^] | 188 | case SkBenchmark::kPDF_Backend: |
| mike@reedtribe.org | a9015f8 | 2011-05-17 02:25:05 +0000 | [diff] [blame] | 189 | default: |
| mtklein@google.com | 330313a | 2013-08-22 15:37:26 +0000 | [diff] [blame] | 190 | SkDEBUGFAIL("unsupported"); |
| mike@reedtribe.org | a9015f8 | 2011-05-17 02:25:05 +0000 | [diff] [blame] | 191 | } |
| 192 | return device; |
| 193 | } |
| 194 | |
| bsalomon@google.com | cb26535 | 2013-02-22 16:13:16 +0000 | [diff] [blame] | 195 | #if SK_SUPPORT_GPU |
| 196 | GrContextFactory gContextFactory; |
| 197 | typedef GrContextFactory::GLContextType GLContextType; |
| mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 198 | static const GLContextType kNative = GrContextFactory::kNative_GLContextType; |
| 199 | #if SK_ANGLE |
| 200 | static const GLContextType kANGLE = GrContextFactory::kANGLE_GLContextType; |
| mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 201 | #endif |
| 202 | static const GLContextType kDebug = GrContextFactory::kDebug_GLContextType; |
| 203 | static const GLContextType kNull = GrContextFactory::kNull_GLContextType; |
| bsalomon@google.com | cb26535 | 2013-02-22 16:13:16 +0000 | [diff] [blame] | 204 | #else |
| 205 | typedef int GLContextType; |
| mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 206 | static const GLContextType kNative = 0, kANGLE = 0, kDebug = 0, kNull = 0; |
| bsalomon@google.com | cb26535 | 2013-02-22 16:13:16 +0000 | [diff] [blame] | 207 | #endif |
| 208 | |
| mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 209 | #ifdef SK_DEBUG |
| 210 | static const bool kIsDebug = true; |
| 211 | #else |
| 212 | static const bool kIsDebug = false; |
| 213 | #endif |
| 214 | |
| 215 | static const struct Config { |
| 216 | SkBitmap::Config config; |
| 217 | const char* name; |
| 218 | int sampleCount; |
| commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame^] | 219 | SkBenchmark::Backend backend; |
| mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 220 | GLContextType contextType; |
| 221 | bool runByDefault; |
| reed@android.com | 4bc1983 | 2009-01-19 20:08:35 +0000 | [diff] [blame] | 222 | } gConfigs[] = { |
| commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame^] | 223 | { SkBitmap::kNo_Config, "NONRENDERING", 0, SkBenchmark::kNonRendering_Backend, kNative, true}, |
| 224 | { SkBitmap::kARGB_8888_Config, "8888", 0, SkBenchmark::kRaster_Backend, kNative, true}, |
| 225 | { SkBitmap::kRGB_565_Config, "565", 0, SkBenchmark::kRaster_Backend, kNative, true}, |
| bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 226 | #if SK_SUPPORT_GPU |
| commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame^] | 227 | { SkBitmap::kARGB_8888_Config, "GPU", 0, SkBenchmark::kGPU_Backend, kNative, true}, |
| 228 | { SkBitmap::kARGB_8888_Config, "MSAA4", 4, SkBenchmark::kGPU_Backend, kNative, false}, |
| 229 | { SkBitmap::kARGB_8888_Config, "MSAA16", 16, SkBenchmark::kGPU_Backend, kNative, false}, |
| robertphillips@google.com | d3b9fbb | 2012-03-28 16:19:11 +0000 | [diff] [blame] | 230 | #if SK_ANGLE |
| commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame^] | 231 | { SkBitmap::kARGB_8888_Config, "ANGLE", 0, SkBenchmark::kGPU_Backend, kANGLE, true}, |
| bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 232 | #endif // SK_ANGLE |
| commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame^] | 233 | { SkBitmap::kARGB_8888_Config, "Debug", 0, SkBenchmark::kGPU_Backend, kDebug, kIsDebug}, |
| 234 | { SkBitmap::kARGB_8888_Config, "NULLGPU", 0, SkBenchmark::kGPU_Backend, kNull, true}, |
| bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 235 | #endif // SK_SUPPORT_GPU |
| reed@android.com | 4bc1983 | 2009-01-19 20:08:35 +0000 | [diff] [blame] | 236 | }; |
| 237 | |
| mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 238 | DEFINE_string(outDir, "", "If given, image of each bench will be put in outDir."); |
| 239 | DEFINE_string(timers, "cg", "Timers to display. " |
| 240 | "Options: w(all) W(all, truncated) c(pu) C(pu, truncated) g(pu)"); |
| reed@android.com | 4c7d3d6 | 2009-01-21 03:15:13 +0000 | [diff] [blame] | 241 | |
| mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 242 | DEFINE_bool(rotate, false, "Rotate canvas before bench run?"); |
| 243 | DEFINE_bool(scale, false, "Scale canvas before bench run?"); |
| 244 | DEFINE_bool(clip, false, "Clip canvas before bench run?"); |
| bsalomon@google.com | 8a70eef | 2013-03-19 13:58:55 +0000 | [diff] [blame] | 245 | |
| mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 246 | DEFINE_bool(forceAA, true, "Force anti-aliasing?"); |
| 247 | DEFINE_bool(forceFilter, false, "Force bitmap filtering?"); |
| 248 | DEFINE_string(forceDither, "default", "Force dithering: true, false, or default?"); |
| 249 | DEFINE_bool(forceBlend, false, "Force alpha blending?"); |
| 250 | |
| 251 | DEFINE_int32(gpuCacheBytes, -1, "GPU cache size limit in bytes. 0 to disable cache."); |
| 252 | DEFINE_int32(gpuCacheCount, -1, "GPU cache size limit in object count. 0 to disable cache."); |
| 253 | |
| 254 | DEFINE_string(match, "", "[~][^]substring[$] [...] of test name to run.\n" |
| 255 | "Multiple matches may be separated by spaces.\n" |
| 256 | "~ causes a matching test to always be skipped\n" |
| 257 | "^ requires the start of the test to match\n" |
| 258 | "$ requires the end of the test to match\n" |
| 259 | "^ and $ requires an exact match\n" |
| 260 | "If a test does not match any list entry,\n" |
| 261 | "it is skipped unless some list entry starts with ~\n"); |
| 262 | DEFINE_string(mode, "normal", |
| 263 | "normal: draw to a normal canvas;\n" |
| 264 | "deferred: draw to a deferred canvas;\n" |
| 265 | "deferredSilent: deferred with silent playback;\n" |
| 266 | "record: draw to an SkPicture;\n" |
| 267 | "picturerecord: draw from an SkPicture to an SkPicture.\n"); |
| borenet@google.com | 4e061d3 | 2013-09-16 19:12:19 +0000 | [diff] [blame] | 268 | DEFINE_string(config, kDefaultsConfigStr, |
| 269 | "Run configs given. By default, runs the configs marked \"runByDefault\" in gConfigs."); |
| mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 270 | DEFINE_string(logFile, "", "Also write stdout here."); |
| mtklein@google.com | dbd41c8 | 2013-09-13 20:11:09 +0000 | [diff] [blame] | 271 | DEFINE_int32(minMs, 20, "Shortest time we'll allow a benchmark to run."); |
| 272 | DEFINE_int32(maxMs, 4000, "Longest time we'll allow a benchmark to run."); |
| 273 | DEFINE_double(error, 0.01, |
| 274 | "Ratio of subsequent bench measurements must drop within 1±error to converge."); |
| mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 275 | DEFINE_string(timeFormat, "%9.2f", "Format to print results, in milliseconds per 1000 loops."); |
| mtklein@google.com | dbd41c8 | 2013-09-13 20:11:09 +0000 | [diff] [blame] | 276 | DEFINE_bool2(verbose, v, false, "Print more."); |
| scroggo@google.com | 111fd11 | 2013-09-25 21:42:12 +0000 | [diff] [blame] | 277 | DEFINE_string2(resourcePath, i, NULL, "directory for test resources."); |
| mtklein@google.com | dbd41c8 | 2013-09-13 20:11:09 +0000 | [diff] [blame] | 278 | |
| 279 | // Has this bench converged? First arguments are milliseconds / loop iteration, |
| 280 | // last is overall runtime in milliseconds. |
| 281 | static bool HasConverged(double prevPerLoop, double currPerLoop, double currRaw) { |
| 282 | if (currRaw < FLAGS_minMs) { |
| 283 | return false; |
| 284 | } |
| 285 | const double low = 1 - FLAGS_error, high = 1 + FLAGS_error; |
| 286 | const double ratio = currPerLoop / prevPerLoop; |
| 287 | return low < ratio && ratio < high; |
| 288 | } |
| tomhudson@google.com | 86bb9b7 | 2012-04-03 13:28:57 +0000 | [diff] [blame] | 289 | |
| caryclark@google.com | 5987f58 | 2012-10-02 18:33:14 +0000 | [diff] [blame] | 290 | int tool_main(int argc, char** argv); |
| 291 | int tool_main(int argc, char** argv) { |
| bsalomon@google.com | 4e23068 | 2013-01-15 20:37:04 +0000 | [diff] [blame] | 292 | #if SK_ENABLE_INST_COUNT |
| bsalomon@google.com | 65a87cc | 2012-08-14 13:15:44 +0000 | [diff] [blame] | 293 | gPrintInstCount = true; |
| 294 | #endif |
| reed@android.com | 3a859a0 | 2009-01-28 00:56:29 +0000 | [diff] [blame] | 295 | SkAutoGraphics ag; |
| mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 296 | SkCommandLineFlags::Parse(argc, argv); |
| bsalomon@google.com | 65a87cc | 2012-08-14 13:15:44 +0000 | [diff] [blame] | 297 | |
| mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 298 | // First, parse some flags. |
| rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 299 | |
| scroggo@google.com | 9a41252 | 2012-09-07 15:21:18 +0000 | [diff] [blame] | 300 | SkBenchLogger logger; |
| mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 301 | if (FLAGS_logFile.count()) { |
| 302 | logger.SetLogFile(FLAGS_logFile[0]); |
| 303 | } |
| scroggo@google.com | 9a41252 | 2012-09-07 15:21:18 +0000 | [diff] [blame] | 304 | |
| mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 305 | const uint8_t alpha = FLAGS_forceBlend ? 0x80 : 0xFF; |
| 306 | SkTriState::State dither = SkTriState::kDefault; |
| 307 | for (size_t i = 0; i < 3; i++) { |
| 308 | if (strcmp(SkTriState::Name[i], FLAGS_forceDither[0]) == 0) { |
| 309 | dither = static_cast<SkTriState::State>(i); |
| reed@android.com | b398fe8 | 2009-01-07 11:47:57 +0000 | [diff] [blame] | 310 | } |
| 311 | } |
| mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 312 | |
| 313 | BenchMode benchMode = kNormal_BenchMode; |
| 314 | for (size_t i = 0; i < SK_ARRAY_COUNT(BenchMode_Name); i++) { |
| 315 | if (strcmp(FLAGS_mode[0], BenchMode_Name[i]) == 0) { |
| 316 | benchMode = static_cast<BenchMode>(i); |
| 317 | } |
| keyar@chromium.org | fdd909c | 2012-07-20 23:03:42 +0000 | [diff] [blame] | 318 | } |
| mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 319 | |
| 320 | SkTDArray<int> configs; |
| borenet@google.com | 4e061d3 | 2013-09-16 19:12:19 +0000 | [diff] [blame] | 321 | bool runDefaultConfigs = false; |
| mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 322 | // Try user-given configs first. |
| 323 | for (int i = 0; i < FLAGS_config.count(); i++) { |
| robertphillips@google.com | e9cd27d | 2013-10-16 17:48:11 +0000 | [diff] [blame] | 324 | for (int j = 0; j < static_cast<int>(SK_ARRAY_COUNT(gConfigs)); ++j) { |
| mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 325 | if (0 == strcmp(FLAGS_config[i], gConfigs[j].name)) { |
| 326 | *configs.append() = j; |
| borenet@google.com | 4e061d3 | 2013-09-16 19:12:19 +0000 | [diff] [blame] | 327 | } else if (0 == strcmp(FLAGS_config[i], kDefaultsConfigStr)) { |
| 328 | runDefaultConfigs = true; |
| mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 329 | } |
| 330 | } |
| keyar@chromium.org | fdd909c | 2012-07-20 23:03:42 +0000 | [diff] [blame] | 331 | } |
| mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 332 | // If there weren't any, fill in with defaults. |
| borenet@google.com | 4e061d3 | 2013-09-16 19:12:19 +0000 | [diff] [blame] | 333 | if (runDefaultConfigs) { |
| robertphillips@google.com | e9cd27d | 2013-10-16 17:48:11 +0000 | [diff] [blame] | 334 | for (int i = 0; i < static_cast<int>(SK_ARRAY_COUNT(gConfigs)); ++i) { |
| mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 335 | if (gConfigs[i].runByDefault) { |
| bsalomon@google.com | 8a70eef | 2013-03-19 13:58:55 +0000 | [diff] [blame] | 336 | *configs.append() = i; |
| 337 | } |
| tomhudson@google.com | 13eaaaa | 2012-04-16 18:00:40 +0000 | [diff] [blame] | 338 | } |
| 339 | } |
| mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 340 | // Filter out things we can't run. |
| 341 | if (kNormal_BenchMode != benchMode) { |
| bsalomon@google.com | 604a56a | 2013-03-15 15:42:15 +0000 | [diff] [blame] | 342 | // Non-rendering configs only run in normal mode |
| 343 | for (int i = 0; i < configs.count(); ++i) { |
| mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 344 | const Config& config = gConfigs[configs[i]]; |
| commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame^] | 345 | if (SkBenchmark::kNonRendering_Backend == config.backend) { |
| bsalomon@google.com | 604a56a | 2013-03-15 15:42:15 +0000 | [diff] [blame] | 346 | configs.remove(i, 1); |
| 347 | --i; |
| 348 | } |
| 349 | } |
| 350 | } |
| scroggo@google.com | 111fd11 | 2013-09-25 21:42:12 +0000 | [diff] [blame] | 351 | // Set the resource path. |
| 352 | if (!FLAGS_resourcePath.isEmpty()) { |
| 353 | SkBenchmark::SetResourcePath(FLAGS_resourcePath[0]); |
| 354 | } |
| 355 | |
| bsalomon@google.com | 8a70eef | 2013-03-19 13:58:55 +0000 | [diff] [blame] | 356 | #if SK_SUPPORT_GPU |
| 357 | for (int i = 0; i < configs.count(); ++i) { |
| mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 358 | const Config& config = gConfigs[configs[i]]; |
| bsalomon@google.com | 8a70eef | 2013-03-19 13:58:55 +0000 | [diff] [blame] | 359 | |
| commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame^] | 360 | if (SkBenchmark::kGPU_Backend == config.backend) { |
| mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 361 | GrContext* context = gContextFactory.get(config.contextType); |
| bsalomon@google.com | 8a70eef | 2013-03-19 13:58:55 +0000 | [diff] [blame] | 362 | if (NULL == context) { |
| mtklein@google.com | dbd41c8 | 2013-09-13 20:11:09 +0000 | [diff] [blame] | 363 | logger.logError(SkStringPrintf( |
| 364 | "Error creating GrContext for config %s. Config will be skipped.\n", |
| 365 | config.name)); |
| bsalomon@google.com | 8a70eef | 2013-03-19 13:58:55 +0000 | [diff] [blame] | 366 | configs.remove(i); |
| 367 | --i; |
| 368 | continue; |
| 369 | } |
| mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 370 | if (config.sampleCount > context->getMaxSampleCount()){ |
| mtklein@google.com | dbd41c8 | 2013-09-13 20:11:09 +0000 | [diff] [blame] | 371 | logger.logError(SkStringPrintf( |
| 372 | "Sample count (%d) for config %s is unsupported. Config will be skipped.\n", |
| 373 | config.sampleCount, config.name)); |
| bsalomon@google.com | 8a70eef | 2013-03-19 13:58:55 +0000 | [diff] [blame] | 374 | configs.remove(i); |
| 375 | --i; |
| 376 | continue; |
| 377 | } |
| 378 | } |
| 379 | } |
| 380 | #endif |
| 381 | |
| mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 382 | // All flags should be parsed now. Report our settings. |
| 383 | if (kIsDebug) { |
| 384 | logger.logError("bench was built in Debug mode, so we're going to hide the times." |
| 385 | " It's for your own good!\n"); |
| 386 | } |
| 387 | SkString str("skia bench:"); |
| 388 | str.appendf(" mode=%s", FLAGS_mode[0]); |
| 389 | str.appendf(" alpha=0x%02X antialias=%d filter=%d dither=%s", |
| 390 | alpha, FLAGS_forceAA, FLAGS_forceFilter, SkTriState::Name[dither]); |
| 391 | str.appendf(" rotate=%d scale=%d clip=%d", FLAGS_rotate, FLAGS_scale, FLAGS_clip); |
| rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 392 | |
| mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 393 | #if defined(SK_SCALAR_IS_FIXED) |
| 394 | str.append(" scalar=fixed"); |
| 395 | #else |
| 396 | str.append(" scalar=float"); |
| bungeman@google.com | a5d4841 | 2011-06-15 17:25:46 +0000 | [diff] [blame] | 397 | #endif |
| 398 | |
| 399 | #if defined(SK_BUILD_FOR_WIN32) |
| mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 400 | str.append(" system=WIN32"); |
| bungeman@google.com | a5d4841 | 2011-06-15 17:25:46 +0000 | [diff] [blame] | 401 | #elif defined(SK_BUILD_FOR_MAC) |
| mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 402 | str.append(" system=MAC"); |
| bungeman@google.com | a5d4841 | 2011-06-15 17:25:46 +0000 | [diff] [blame] | 403 | #elif defined(SK_BUILD_FOR_ANDROID) |
| mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 404 | str.append(" system=ANDROID"); |
| bungeman@google.com | a5d4841 | 2011-06-15 17:25:46 +0000 | [diff] [blame] | 405 | #elif defined(SK_BUILD_FOR_UNIX) |
| mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 406 | str.append(" system=UNIX"); |
| bungeman@google.com | a5d4841 | 2011-06-15 17:25:46 +0000 | [diff] [blame] | 407 | #else |
| mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 408 | str.append(" system=other"); |
| bungeman@google.com | a5d4841 | 2011-06-15 17:25:46 +0000 | [diff] [blame] | 409 | #endif |
| 410 | |
| 411 | #if defined(SK_DEBUG) |
| mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 412 | str.append(" DEBUG"); |
| bungeman@google.com | a5d4841 | 2011-06-15 17:25:46 +0000 | [diff] [blame] | 413 | #endif |
| mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 414 | str.append("\n"); |
| 415 | logger.logProgress(str); |
| bsalomon@google.com | 508824b | 2011-12-13 16:49:49 +0000 | [diff] [blame] | 416 | |
| mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 417 | |
| 418 | // Set texture cache limits if non-default. |
| bsalomon@google.com | 5c90e29 | 2013-02-22 19:17:13 +0000 | [diff] [blame] | 419 | for (size_t i = 0; i < SK_ARRAY_COUNT(gConfigs); ++i) { |
| bsalomon@google.com | cb26535 | 2013-02-22 16:13:16 +0000 | [diff] [blame] | 420 | #if SK_SUPPORT_GPU |
| mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 421 | const Config& config = gConfigs[i]; |
| commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame^] | 422 | if (SkBenchmark::kGPU_Backend != config.backend) { |
| mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 423 | continue; |
| bsalomon@google.com | cb26535 | 2013-02-22 16:13:16 +0000 | [diff] [blame] | 424 | } |
| mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 425 | GrContext* context = gContextFactory.get(config.contextType); |
| 426 | if (NULL == context) { |
| 427 | continue; |
| 428 | } |
| 429 | |
| 430 | size_t bytes; |
| 431 | int count; |
| 432 | context->getTextureCacheLimits(&count, &bytes); |
| 433 | if (-1 != FLAGS_gpuCacheBytes) { |
| 434 | bytes = static_cast<size_t>(FLAGS_gpuCacheBytes); |
| 435 | } |
| 436 | if (-1 != FLAGS_gpuCacheCount) { |
| 437 | count = FLAGS_gpuCacheCount; |
| 438 | } |
| 439 | context->setTextureCacheLimits(count, bytes); |
| bsalomon@google.com | cb26535 | 2013-02-22 16:13:16 +0000 | [diff] [blame] | 440 | #endif |
| 441 | } |
| bsalomon@google.com | 7491372 | 2011-10-27 20:44:19 +0000 | [diff] [blame] | 442 | |
| mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 443 | // Find the longest name of the benches we're going to run to make the output pretty. |
| 444 | Iter names; |
| reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 445 | SkBenchmark* bench; |
| robertphillips@google.com | 8c99c9f | 2013-11-20 15:56:14 +0000 | [diff] [blame] | 446 | int longestName = 0; |
| mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 447 | while ((bench = names.next()) != NULL) { |
| 448 | SkAutoTUnref<SkBenchmark> benchUnref(bench); |
| 449 | if (SkCommandLineFlags::ShouldSkip(FLAGS_match, bench->getName())) { |
| 450 | continue; |
| 451 | } |
| robertphillips@google.com | 8c99c9f | 2013-11-20 15:56:14 +0000 | [diff] [blame] | 452 | const int length = strlen(bench->getName()); |
| mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 453 | longestName = length > longestName ? length : longestName; |
| 454 | } |
| 455 | |
| 456 | // Run each bench in each configuration it supports and we asked for. |
| 457 | Iter iter; |
| reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 458 | while ((bench = iter.next()) != NULL) { |
| bsalomon@google.com | 7fbc604 | 2012-08-13 22:10:05 +0000 | [diff] [blame] | 459 | SkAutoTUnref<SkBenchmark> benchUnref(bench); |
| mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 460 | if (SkCommandLineFlags::ShouldSkip(FLAGS_match, bench->getName())) { |
| reed@android.com | b398fe8 | 2009-01-07 11:47:57 +0000 | [diff] [blame] | 461 | continue; |
| 462 | } |
| rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 463 | |
| mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 464 | bench->setForceAlpha(alpha); |
| 465 | bench->setForceAA(FLAGS_forceAA); |
| 466 | bench->setForceFilter(FLAGS_forceFilter); |
| 467 | bench->setDither(dither); |
| bsalomon@google.com | 30e6d2c | 2012-08-13 14:03:31 +0000 | [diff] [blame] | 468 | AutoPrePostDraw appd(bench); |
| 469 | |
| mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 470 | bool loggedBenchName = false; |
| 471 | for (int i = 0; i < configs.count(); ++i) { |
| 472 | const int configIndex = configs[i]; |
| 473 | const Config& config = gConfigs[configIndex]; |
| tomhudson@google.com | 13eaaaa | 2012-04-16 18:00:40 +0000 | [diff] [blame] | 474 | |
| commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame^] | 475 | if (!bench->isSuitableFor(config.backend)) { |
| mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 476 | continue; |
| bsalomon@google.com | 604a56a | 2013-03-15 15:42:15 +0000 | [diff] [blame] | 477 | } |
| 478 | |
| bsalomon@google.com | cb26535 | 2013-02-22 16:13:16 +0000 | [diff] [blame] | 479 | GrContext* context = NULL; |
| bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 480 | #if SK_SUPPORT_GPU |
| sugoi@google.com | 9c55f80 | 2013-03-07 20:52:59 +0000 | [diff] [blame] | 481 | SkGLContextHelper* glContext = NULL; |
| commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame^] | 482 | if (SkBenchmark::kGPU_Backend == config.backend) { |
| mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 483 | context = gContextFactory.get(config.contextType); |
| bsalomon@google.com | cb26535 | 2013-02-22 16:13:16 +0000 | [diff] [blame] | 484 | if (NULL == context) { |
| 485 | continue; |
| 486 | } |
| mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 487 | glContext = gContextFactory.getGLContext(config.contextType); |
| mike@reedtribe.org | a9015f8 | 2011-05-17 02:25:05 +0000 | [diff] [blame] | 488 | } |
| bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 489 | #endif |
| mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 490 | SkAutoTUnref<SkBaseDevice> device; |
| 491 | SkAutoTUnref<SkCanvas> canvas; |
| 492 | SkPicture recordFrom, recordTo; |
| 493 | const SkIPoint dim = bench->getSize(); |
| bsalomon@google.com | 604a56a | 2013-03-15 15:42:15 +0000 | [diff] [blame] | 494 | |
| mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 495 | const SkPicture::RecordingFlags kRecordFlags = |
| 496 | SkPicture::kUsePathBoundsForClip_RecordingFlag; |
| 497 | |
| commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame^] | 498 | if (SkBenchmark::kNonRendering_Backend != config.backend) { |
| mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 499 | device.reset(make_device(config.config, |
| 500 | dim, |
| 501 | config.backend, |
| 502 | config.sampleCount, |
| 503 | context)); |
| 504 | if (!device.get()) { |
| mtklein@google.com | dbd41c8 | 2013-09-13 20:11:09 +0000 | [diff] [blame] | 505 | logger.logError(SkStringPrintf( |
| 506 | "Device creation failure for config %s. Will skip.\n", config.name)); |
| mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 507 | continue; |
| keyar@chromium.org | fdd909c | 2012-07-20 23:03:42 +0000 | [diff] [blame] | 508 | } |
| junov@chromium.org | fb10389 | 2012-09-20 19:35:43 +0000 | [diff] [blame] | 509 | |
| mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 510 | switch(benchMode) { |
| 511 | case kDeferredSilent_BenchMode: |
| 512 | case kDeferred_BenchMode: |
| 513 | canvas.reset(SkDeferredCanvas::Create(device.get())); |
| 514 | break; |
| 515 | case kRecord_BenchMode: |
| 516 | canvas.reset(SkRef(recordTo.beginRecording(dim.fX, dim.fY, kRecordFlags))); |
| 517 | break; |
| 518 | case kPictureRecord_BenchMode: |
| 519 | bench->draw(recordFrom.beginRecording(dim.fX, dim.fY, kRecordFlags)); |
| 520 | recordFrom.endRecording(); |
| 521 | canvas.reset(SkRef(recordTo.beginRecording(dim.fX, dim.fY, kRecordFlags))); |
| 522 | break; |
| 523 | case kNormal_BenchMode: |
| 524 | canvas.reset(new SkCanvas(device.get())); |
| 525 | break; |
| 526 | default: |
| 527 | SkASSERT(false); |
| 528 | } |
| 529 | } |
| 530 | |
| 531 | if (NULL != canvas) { |
| 532 | canvas->clear(SK_ColorWHITE); |
| 533 | if (FLAGS_clip) { performClip(canvas, dim.fX, dim.fY); } |
| 534 | if (FLAGS_scale) { performScale(canvas, dim.fX, dim.fY); } |
| 535 | if (FLAGS_rotate) { performRotate(canvas, dim.fX, dim.fY); } |
| 536 | } |
| 537 | |
| 538 | if (!loggedBenchName) { |
| 539 | loggedBenchName = true; |
| 540 | SkString str; |
| 541 | str.printf("running bench [%3d %3d] %*s ", |
| 542 | dim.fX, dim.fY, longestName, bench->getName()); |
| 543 | logger.logProgress(str); |
| 544 | } |
| 545 | |
| 546 | #if SK_SUPPORT_GPU |
| 547 | SkGLContextHelper* contextHelper = NULL; |
| commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame^] | 548 | if (SkBenchmark::kGPU_Backend == config.backend) { |
| mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 549 | contextHelper = gContextFactory.getGLContext(config.contextType); |
| 550 | } |
| 551 | BenchTimer timer(contextHelper); |
| 552 | #else |
| 553 | BenchTimer timer; |
| 554 | #endif |
| 555 | |
| mtklein@google.com | 9ef1d21 | 2013-09-13 20:39:50 +0000 | [diff] [blame] | 556 | double previous = std::numeric_limits<double>::infinity(); |
| mtklein@google.com | dbd41c8 | 2013-09-13 20:11:09 +0000 | [diff] [blame] | 557 | bool converged = false; |
| djsollen@google.com | 70de4da | 2013-10-10 17:33:39 +0000 | [diff] [blame] | 558 | |
| 559 | // variables used to compute loopsPerFrame |
| 560 | double frameIntervalTime = 0.0f; |
| 561 | int frameIntervalTotalLoops = 0; |
| 562 | |
| 563 | bool frameIntervalComputed = false; |
| 564 | int loopsPerFrame = 0; |
| 565 | int loopsPerIter = 0; |
| mtklein@google.com | dbd41c8 | 2013-09-13 20:11:09 +0000 | [diff] [blame] | 566 | if (FLAGS_verbose) { SkDebugf("%s %s: ", bench->getName(), config.name); } |
| mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 567 | do { |
| djsollen@google.com | 70de4da | 2013-10-10 17:33:39 +0000 | [diff] [blame] | 568 | // Ramp up 1 -> 2 -> 4 -> 8 -> 16 -> ... -> ~1 billion. |
| 569 | loopsPerIter = (loopsPerIter == 0) ? 1 : loopsPerIter * 2; |
| 570 | if (loopsPerIter >= (1<<30) || timer.fWall > FLAGS_maxMs) { |
| mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 571 | // If you find it takes more than a billion loops to get up to 20ms of runtime, |
| 572 | // you've got a computer clocked at several THz or have a broken benchmark. ;) |
| 573 | // "1B ought to be enough for anybody." |
| mtklein@google.com | dbd41c8 | 2013-09-13 20:11:09 +0000 | [diff] [blame] | 574 | logger.logError(SkStringPrintf( |
| djsollen@google.com | 70de4da | 2013-10-10 17:33:39 +0000 | [diff] [blame] | 575 | "\nCan't get %s %s to converge in %dms (%d loops)", |
| 576 | bench->getName(), config.name, FLAGS_maxMs, loopsPerIter)); |
| mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 577 | break; |
| 578 | } |
| mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 579 | |
| 580 | if ((benchMode == kRecord_BenchMode || benchMode == kPictureRecord_BenchMode)) { |
| 581 | // Clear the recorded commands so that they do not accumulate. |
| 582 | canvas.reset(recordTo.beginRecording(dim.fX, dim.fY, kRecordFlags)); |
| junov@chromium.org | fb10389 | 2012-09-20 19:35:43 +0000 | [diff] [blame] | 583 | } |
| robertphillips@google.com | 91ee3a1 | 2012-08-28 12:18:40 +0000 | [diff] [blame] | 584 | |
| mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 585 | timer.start(); |
| djsollen@google.com | 70de4da | 2013-10-10 17:33:39 +0000 | [diff] [blame] | 586 | // Inner loop that allows us to break the run into smaller |
| 587 | // chunks (e.g. frames). This is especially useful for the GPU |
| 588 | // as we can flush and/or swap buffers to keep the GPU from |
| 589 | // queuing up too much work. |
| 590 | for (int loopCount = loopsPerIter; loopCount > 0; ) { |
| commit-bot@chromium.org | 2887119 | 2013-10-14 15:28:01 +0000 | [diff] [blame] | 591 | // Save and restore around each call to draw() to guarantee a pristine canvas. |
| 592 | SkAutoCanvasRestore saveRestore(canvas, true/*also save*/); |
| 593 | |
| djsollen@google.com | 70de4da | 2013-10-10 17:33:39 +0000 | [diff] [blame] | 594 | if (frameIntervalComputed && loopCount > loopsPerFrame) { |
| 595 | bench->setLoops(loopsPerFrame); |
| 596 | loopCount -= loopsPerFrame; |
| 597 | } else { |
| 598 | bench->setLoops(loopCount); |
| 599 | loopCount = 0; |
| 600 | } |
| 601 | |
| 602 | if (benchMode == kPictureRecord_BenchMode) { |
| 603 | recordFrom.draw(canvas); |
| 604 | } else { |
| 605 | bench->draw(canvas); |
| 606 | } |
| 607 | |
| 608 | if (kDeferredSilent_BenchMode == benchMode) { |
| 609 | static_cast<SkDeferredCanvas*>(canvas.get())->silentFlush(); |
| 610 | } else if (NULL != canvas) { |
| 611 | canvas->flush(); |
| 612 | } |
| 613 | |
| 614 | #if SK_SUPPORT_GPU |
| 615 | // swap drawing buffers on each frame to prevent the GPU |
| 616 | // from queuing up too much work |
| 617 | if (NULL != glContext) { |
| 618 | glContext->swapBuffers(); |
| 619 | } |
| 620 | #endif |
| mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 621 | } |
| 622 | |
| mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 623 | |
| 624 | |
| 625 | // Stop truncated timers before GL calls complete, and stop the full timers after. |
| 626 | timer.truncatedEnd(); |
| 627 | #if SK_SUPPORT_GPU |
| 628 | if (NULL != glContext) { |
| 629 | context->flush(); |
| 630 | SK_GL(*glContext, Finish()); |
| 631 | } |
| 632 | #endif |
| 633 | timer.end(); |
| djsollen@google.com | 70de4da | 2013-10-10 17:33:39 +0000 | [diff] [blame] | 634 | |
| djsollen@google.com | dcfed6c | 2013-10-10 18:48:27 +0000 | [diff] [blame] | 635 | // setup the frame interval for subsequent iterations |
| 636 | if (!frameIntervalComputed) { |
| djsollen@google.com | 70de4da | 2013-10-10 17:33:39 +0000 | [diff] [blame] | 637 | frameIntervalTime += timer.fWall; |
| 638 | frameIntervalTotalLoops += loopsPerIter; |
| 639 | if (frameIntervalTime >= FLAGS_minMs) { |
| 640 | frameIntervalComputed = true; |
| 641 | loopsPerFrame = |
| djsollen@google.com | 4e1d4b3 | 2013-10-10 17:50:52 +0000 | [diff] [blame] | 642 | (int)(((double)frameIntervalTotalLoops / frameIntervalTime) * FLAGS_minMs); |
| djsollen@google.com | 70de4da | 2013-10-10 17:33:39 +0000 | [diff] [blame] | 643 | if (loopsPerFrame < 1) { |
| 644 | loopsPerFrame = 1; |
| 645 | } |
| 646 | // SkDebugf(" %s has %d loops in %f ms (normalized to %d)\n", |
| 647 | // bench->getName(), frameIntervalTotalLoops, |
| 648 | // timer.fWall, loopsPerFrame); |
| 649 | } |
| 650 | } |
| djsollen@google.com | dcfed6c | 2013-10-10 18:48:27 +0000 | [diff] [blame] | 651 | |
| djsollen@google.com | 70de4da | 2013-10-10 17:33:39 +0000 | [diff] [blame] | 652 | const double current = timer.fWall / loopsPerIter; |
| mtklein@google.com | dbd41c8 | 2013-09-13 20:11:09 +0000 | [diff] [blame] | 653 | if (FLAGS_verbose && current > previous) { SkDebugf("↑"); } |
| 654 | if (FLAGS_verbose) { SkDebugf("%.3g ", current); } |
| 655 | converged = HasConverged(previous, current, timer.fWall); |
| 656 | previous = current; |
| 657 | } while (!kIsDebug && !converged); |
| 658 | if (FLAGS_verbose) { SkDebugf("\n"); } |
| mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 659 | |
| commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame^] | 660 | if (FLAGS_outDir.count() && SkBenchmark::kNonRendering_Backend != config.backend) { |
| mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 661 | saveFile(bench->getName(), |
| 662 | config.name, |
| 663 | FLAGS_outDir[0], |
| 664 | device->accessBitmap(false)); |
| 665 | } |
| 666 | |
| 667 | if (kIsDebug) { |
| 668 | // Let's not mislead ourselves by looking at Debug build bench times! |
| 669 | continue; |
| 670 | } |
| 671 | |
| 672 | // Normalize to ms per 1000 iterations. |
| djsollen@google.com | 70de4da | 2013-10-10 17:33:39 +0000 | [diff] [blame] | 673 | const double normalize = 1000.0 / loopsPerIter; |
| mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 674 | const struct { char shortName; const char* longName; double ms; } times[] = { |
| 675 | {'w', "msecs", normalize * timer.fWall}, |
| 676 | {'W', "Wmsecs", normalize * timer.fTruncatedWall}, |
| 677 | {'c', "cmsecs", normalize * timer.fCpu}, |
| 678 | {'C', "Cmsecs", normalize * timer.fTruncatedCpu}, |
| 679 | {'g', "gmsecs", normalize * timer.fGpu}, |
| 680 | }; |
| 681 | |
| 682 | SkString result; |
| 683 | result.appendf(" %s:", config.name); |
| 684 | for (size_t i = 0; i < SK_ARRAY_COUNT(times); i++) { |
| 685 | if (strchr(FLAGS_timers[0], times[i].shortName) && times[i].ms > 0) { |
| 686 | result.appendf(" %s = ", times[i].longName); |
| 687 | result.appendf(FLAGS_timeFormat[0], times[i].ms); |
| commit-bot@chromium.org | 7495f59 | 2013-06-03 19:31:07 +0000 | [diff] [blame] | 688 | } |
| reed@google.com | 25df888 | 2011-07-14 19:03:58 +0000 | [diff] [blame] | 689 | } |
| mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 690 | logger.logProgress(result); |
| bensong@google.com | 24ed8d7 | 2013-06-12 14:45:03 +0000 | [diff] [blame] | 691 | } |
| mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 692 | if (loggedBenchName) { |
| 693 | logger.logProgress("\n"); |
| bsalomon@google.com | 604a56a | 2013-03-15 15:42:15 +0000 | [diff] [blame] | 694 | } |
| reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 695 | } |
| bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 696 | #if SK_SUPPORT_GPU |
| bsalomon@google.com | cb26535 | 2013-02-22 16:13:16 +0000 | [diff] [blame] | 697 | gContextFactory.destroyContexts(); |
| robertphillips@google.com | 9d59420 | 2012-09-13 14:05:00 +0000 | [diff] [blame] | 698 | #endif |
| reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 699 | return 0; |
| 700 | } |
| caryclark@google.com | 5987f58 | 2012-10-02 18:33:14 +0000 | [diff] [blame] | 701 | |
| borenet@google.com | 7158e6a | 2012-11-01 17:43:44 +0000 | [diff] [blame] | 702 | #if !defined(SK_BUILD_FOR_IOS) && !defined(SK_BUILD_FOR_NACL) |
| caryclark@google.com | 5987f58 | 2012-10-02 18:33:14 +0000 | [diff] [blame] | 703 | int main(int argc, char * const argv[]) { |
| 704 | return tool_main(argc, (char**) argv); |
| 705 | } |
| 706 | #endif |