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