blob: e097fe094771fe44eaa8609dd31f3108f29dcd63 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
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.com971d0c82011-08-19 17:22:05 +00007
tfarinaf168b862014-06-19 12:32:29 -07008#include "BenchLogger.h"
tfarinaf168b862014-06-19 12:32:29 -07009#include "Benchmark.h"
mtklein30e6e2a2014-06-18 11:44:15 -070010#include "CrashHandler.h"
tfarinaf168b862014-06-19 12:32:29 -070011#include "GMBench.h"
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +000012#include "ResultsWriter.h"
reed@android.combd700c32009-01-05 03:34:50 +000013#include "SkCanvas.h"
mtklein@google.com78d03792013-09-10 19:42:07 +000014#include "SkColorPriv.h"
sglez@google.com586db932013-07-24 17:24:23 +000015#include "SkCommandLineFlags.h"
reed@google.com1bc6c6a2014-02-04 14:02:44 +000016#include "SkData.h"
bsalomon@google.com82a7bfc2012-04-16 19:11:17 +000017#include "SkDeferredCanvas.h"
reed@android.com3a859a02009-01-28 00:56:29 +000018#include "SkGraphics.h"
reed@android.comb398fe82009-01-07 11:47:57 +000019#include "SkImageEncoder.h"
mtklein@google.comc2897432013-09-10 19:23:38 +000020#include "SkOSFile.h"
reed@android.com6c924ad2009-03-31 03:48:49 +000021#include "SkPicture.h"
robertphillips@google.com770963f2014-04-18 18:04:41 +000022#include "SkPictureRecorder.h"
reed@android.combd700c32009-01-05 03:34:50 +000023#include "SkString.h"
reed@google.com1bc6c6a2014-02-04 14:02:44 +000024#include "SkSurface.h"
mtklein9ac68ee2014-06-20 11:29:20 -070025#include "Timer.h"
reed@android.com29348cb2009-08-04 18:17:15 +000026
djsollen@google.comdb490e92013-11-20 13:15:40 +000027#if SK_SUPPORT_GPU
28#include "GrContext.h"
29#include "GrContextFactory.h"
30#include "GrRenderTarget.h"
djsollen@google.comdb490e92013-11-20 13:15:40 +000031#include "gl/GrGLDefines.h"
32#else
33class GrContext;
34#endif // SK_SUPPORT_GPU
35
mtklein@google.com9ef1d212013-09-13 20:39:50 +000036#include <limits>
37
mtklein@google.comc2897432013-09-10 19:23:38 +000038enum BenchMode {
39 kNormal_BenchMode,
40 kDeferred_BenchMode,
41 kDeferredSilent_BenchMode,
42 kRecord_BenchMode,
43 kPictureRecord_BenchMode
keyar@chromium.orgfdd909c2012-07-20 23:03:42 +000044};
mtklein@google.comdbd41c82013-09-13 20:11:09 +000045const char* BenchMode_Name[] = {
46 "normal", "deferred", "deferredSilent", "record", "picturerecord"
47};
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +000048
borenet@google.com4e061d32013-09-16 19:12:19 +000049static const char kDefaultsConfigStr[] = "defaults";
50
keyar@chromium.orgfdd909c2012-07-20 23:03:42 +000051///////////////////////////////////////////////////////////////////////////////
52
reed@android.combd700c32009-01-05 03:34:50 +000053class Iter {
54public:
commit-bot@chromium.org575d9cd2014-02-24 23:25:49 +000055 Iter() : fBenches(BenchRegistry::Head()), fGMs(skiagm::GMRegistry::Head()) {}
rmistry@google.comfbfcd562012-08-23 18:09:54 +000056
tfarinaf168b862014-06-19 12:32:29 -070057 Benchmark* next() {
commit-bot@chromium.org575d9cd2014-02-24 23:25:49 +000058 if (fBenches) {
59 BenchRegistry::Factory f = fBenches->factory();
60 fBenches = fBenches->next();
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +000061 return (*f)(NULL);
reed@android.combd700c32009-01-05 03:34:50 +000062 }
commit-bot@chromium.org575d9cd2014-02-24 23:25:49 +000063
64 while (fGMs) {
65 SkAutoTDelete<skiagm::GM> gm(fGMs->factory()(NULL));
66 fGMs = fGMs->next();
67 if (gm->getFlags() & skiagm::GM::kAsBench_Flag) {
tfarinaf168b862014-06-19 12:32:29 -070068 return SkNEW_ARGS(GMBench, (gm.detach()));
commit-bot@chromium.org575d9cd2014-02-24 23:25:49 +000069 }
70 }
71
reed@android.combd700c32009-01-05 03:34:50 +000072 return NULL;
73 }
bungeman@google.comd1a416a2011-05-18 18:37:07 +000074
reed@android.combd700c32009-01-05 03:34:50 +000075private:
commit-bot@chromium.org575d9cd2014-02-24 23:25:49 +000076 const BenchRegistry* fBenches;
77 const skiagm::GMRegistry* fGMs;
reed@android.combd700c32009-01-05 03:34:50 +000078};
79
80static void make_filename(const char name[], SkString* path) {
81 path->set(name);
82 for (int i = 0; name[i]; i++) {
83 switch (name[i]) {
84 case '/':
85 case '\\':
86 case ' ':
87 case ':':
88 path->writable_str()[i] = '-';
89 break;
90 default:
91 break;
92 }
93 }
94}
95
reed@android.com4c7d3d62009-01-21 03:15:13 +000096static void saveFile(const char name[], const char config[], const char dir[],
reed@google.com1bc6c6a2014-02-04 14:02:44 +000097 const SkImage* image) {
98 SkAutoTUnref<SkData> data(image->encode(SkImageEncoder::kPNG_Type, 100));
99 if (NULL == data.get()) {
reed@android.com4c7d3d62009-01-21 03:15:13 +0000100 return;
101 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000102
mtklein@google.comc2897432013-09-10 19:23:38 +0000103 SkString filename;
104 make_filename(name, &filename);
105 filename.appendf("_%s.png", config);
106 SkString path = SkOSPath::SkPathJoin(dir, filename.c_str());
107 ::remove(path.c_str());
reed@google.com1bc6c6a2014-02-04 14:02:44 +0000108
reed@google.comace13542014-02-06 22:00:58 +0000109 SkFILEWStream stream(path.c_str());
reed@google.com1bc6c6a2014-02-04 14:02:44 +0000110 stream.write(data->data(), data->size());
reed@android.com4c7d3d62009-01-21 03:15:13 +0000111}
112
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000113static void perform_clip(SkCanvas* canvas, int w, int h) {
reed@android.com4c7d3d62009-01-21 03:15:13 +0000114 SkRect r;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000115
reed@android.com4c7d3d62009-01-21 03:15:13 +0000116 r.set(SkIntToScalar(10), SkIntToScalar(10),
117 SkIntToScalar(w*2/3), SkIntToScalar(h*2/3));
118 canvas->clipRect(r, SkRegion::kIntersect_Op);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000119
reed@android.com4c7d3d62009-01-21 03:15:13 +0000120 r.set(SkIntToScalar(w/3), SkIntToScalar(h/3),
121 SkIntToScalar(w-10), SkIntToScalar(h-10));
122 canvas->clipRect(r, SkRegion::kXOR_Op);
123}
124
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000125static void perform_rotate(SkCanvas* canvas, int w, int h) {
reed@android.com4c7d3d62009-01-21 03:15:13 +0000126 const SkScalar x = SkIntToScalar(w) / 2;
127 const SkScalar y = SkIntToScalar(h) / 2;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000128
reed@android.com4c7d3d62009-01-21 03:15:13 +0000129 canvas->translate(x, y);
130 canvas->rotate(SkIntToScalar(35));
131 canvas->translate(-x, -y);
132}
133
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000134static void perform_scale(SkCanvas* canvas, int w, int h) {
reed@android.com387359e2009-08-04 01:51:09 +0000135 const SkScalar x = SkIntToScalar(w) / 2;
136 const SkScalar y = SkIntToScalar(h) / 2;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000137
reed@android.com387359e2009-08-04 01:51:09 +0000138 canvas->translate(x, y);
139 // just enough so we can't take the sprite case
140 canvas->scale(SK_Scalar1 * 99/100, SK_Scalar1 * 99/100);
141 canvas->translate(-x, -y);
142}
143
reed@google.com1bc6c6a2014-02-04 14:02:44 +0000144static SkSurface* make_surface(SkColorType colorType, const SkIPoint& size,
tfarinaf168b862014-06-19 12:32:29 -0700145 Benchmark::Backend backend, int sampleCount,
reed@google.com1bc6c6a2014-02-04 14:02:44 +0000146 GrContext* context) {
147 SkSurface* surface = NULL;
148 SkImageInfo info = SkImageInfo::Make(size.fX, size.fY, colorType,
149 kPremul_SkAlphaType);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000150
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000151 switch (backend) {
tfarinaf168b862014-06-19 12:32:29 -0700152 case Benchmark::kRaster_Backend:
reed@google.com1bc6c6a2014-02-04 14:02:44 +0000153 surface = SkSurface::NewRaster(info);
154 surface->getCanvas()->clear(SK_ColorWHITE);
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000155 break;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000156#if SK_SUPPORT_GPU
tfarinaf168b862014-06-19 12:32:29 -0700157 case Benchmark::kGPU_Backend: {
reed@google.com1bc6c6a2014-02-04 14:02:44 +0000158 surface = SkSurface::NewRenderTarget(context, info, sampleCount);
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000159 break;
bsalomon@google.comcb265352013-02-22 16:13:16 +0000160 }
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000161#endif
tfarinaf168b862014-06-19 12:32:29 -0700162 case Benchmark::kPDF_Backend:
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000163 default:
mtklein@google.com330313a2013-08-22 15:37:26 +0000164 SkDEBUGFAIL("unsupported");
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000165 }
reed@google.com1bc6c6a2014-02-04 14:02:44 +0000166 return surface;
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000167}
168
bsalomon@google.comcb265352013-02-22 16:13:16 +0000169#if SK_SUPPORT_GPU
170GrContextFactory gContextFactory;
171typedef GrContextFactory::GLContextType GLContextType;
mtklein@google.comc2897432013-09-10 19:23:38 +0000172static const GLContextType kNative = GrContextFactory::kNative_GLContextType;
commit-bot@chromium.orge8989572014-01-26 20:51:00 +0000173static const GLContextType kNVPR = GrContextFactory::kNVPR_GLContextType;
mtklein@google.comc2897432013-09-10 19:23:38 +0000174#if SK_ANGLE
175static const GLContextType kANGLE = GrContextFactory::kANGLE_GLContextType;
mtklein@google.comc2897432013-09-10 19:23:38 +0000176#endif
177static const GLContextType kDebug = GrContextFactory::kDebug_GLContextType;
178static const GLContextType kNull = GrContextFactory::kNull_GLContextType;
bsalomon@google.comcb265352013-02-22 16:13:16 +0000179#else
180typedef int GLContextType;
mtklein@google.comc2897432013-09-10 19:23:38 +0000181static const GLContextType kNative = 0, kANGLE = 0, kDebug = 0, kNull = 0;
bsalomon@google.comcb265352013-02-22 16:13:16 +0000182#endif
183
mtklein@google.comc2897432013-09-10 19:23:38 +0000184#ifdef SK_DEBUG
185static const bool kIsDebug = true;
186#else
187static const bool kIsDebug = false;
188#endif
189
190static const struct Config {
reed@google.com1bc6c6a2014-02-04 14:02:44 +0000191 SkColorType fColorType;
mtklein@google.comc2897432013-09-10 19:23:38 +0000192 const char* name;
193 int sampleCount;
tfarinaf168b862014-06-19 12:32:29 -0700194 Benchmark::Backend backend;
mtklein@google.comc2897432013-09-10 19:23:38 +0000195 GLContextType contextType;
196 bool runByDefault;
reed@android.com4bc19832009-01-19 20:08:35 +0000197} gConfigs[] = {
tfarinaf168b862014-06-19 12:32:29 -0700198 { kN32_SkColorType, "NONRENDERING", 0, Benchmark::kNonRendering_Backend, kNative, true},
199 { kN32_SkColorType, "8888", 0, Benchmark::kRaster_Backend, kNative, true},
200 { kRGB_565_SkColorType, "565", 0, Benchmark::kRaster_Backend, kNative, true},
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000201#if SK_SUPPORT_GPU
tfarinaf168b862014-06-19 12:32:29 -0700202 { kN32_SkColorType, "GPU", 0, Benchmark::kGPU_Backend, kNative, true},
203 { kN32_SkColorType, "MSAA4", 4, Benchmark::kGPU_Backend, kNative, false},
204 { kN32_SkColorType, "MSAA16", 16, Benchmark::kGPU_Backend, kNative, false},
205 { kN32_SkColorType, "NVPRMSAA4", 4, Benchmark::kGPU_Backend, kNVPR, true},
206 { kN32_SkColorType, "NVPRMSAA16", 16, Benchmark::kGPU_Backend, kNVPR, false},
robertphillips@google.comd3b9fbb2012-03-28 16:19:11 +0000207#if SK_ANGLE
tfarinaf168b862014-06-19 12:32:29 -0700208 { kN32_SkColorType, "ANGLE", 0, Benchmark::kGPU_Backend, kANGLE, true},
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000209#endif // SK_ANGLE
tfarinaf168b862014-06-19 12:32:29 -0700210 { kN32_SkColorType, "Debug", 0, Benchmark::kGPU_Backend, kDebug, kIsDebug},
211 { kN32_SkColorType, "NULLGPU", 0, Benchmark::kGPU_Backend, kNull, true},
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000212#endif // SK_SUPPORT_GPU
reed@android.com4bc19832009-01-19 20:08:35 +0000213};
214
mtklein@google.comc2897432013-09-10 19:23:38 +0000215DEFINE_string(outDir, "", "If given, image of each bench will be put in outDir.");
216DEFINE_string(timers, "cg", "Timers to display. "
217 "Options: w(all) W(all, truncated) c(pu) C(pu, truncated) g(pu)");
reed@android.com4c7d3d62009-01-21 03:15:13 +0000218
mtklein@google.comc2897432013-09-10 19:23:38 +0000219DEFINE_bool(rotate, false, "Rotate canvas before bench run?");
220DEFINE_bool(scale, false, "Scale canvas before bench run?");
221DEFINE_bool(clip, false, "Clip canvas before bench run?");
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000222
mtklein@google.comc2897432013-09-10 19:23:38 +0000223DEFINE_bool(forceAA, true, "Force anti-aliasing?");
224DEFINE_bool(forceFilter, false, "Force bitmap filtering?");
225DEFINE_string(forceDither, "default", "Force dithering: true, false, or default?");
226DEFINE_bool(forceBlend, false, "Force alpha blending?");
227
228DEFINE_int32(gpuCacheBytes, -1, "GPU cache size limit in bytes. 0 to disable cache.");
229DEFINE_int32(gpuCacheCount, -1, "GPU cache size limit in object count. 0 to disable cache.");
230
commit-bot@chromium.org6dda8272014-01-23 17:21:19 +0000231DEFINE_bool2(leaks, l, false, "show leaked ref cnt'd objects.");
mtklein@google.comc2897432013-09-10 19:23:38 +0000232DEFINE_string(match, "", "[~][^]substring[$] [...] of test name to run.\n"
233 "Multiple matches may be separated by spaces.\n"
234 "~ causes a matching test to always be skipped\n"
235 "^ requires the start of the test to match\n"
236 "$ requires the end of the test to match\n"
237 "^ and $ requires an exact match\n"
238 "If a test does not match any list entry,\n"
239 "it is skipped unless some list entry starts with ~\n");
240DEFINE_string(mode, "normal",
241 "normal: draw to a normal canvas;\n"
242 "deferred: draw to a deferred canvas;\n"
243 "deferredSilent: deferred with silent playback;\n"
244 "record: draw to an SkPicture;\n"
245 "picturerecord: draw from an SkPicture to an SkPicture.\n");
borenet@google.com4e061d32013-09-16 19:12:19 +0000246DEFINE_string(config, kDefaultsConfigStr,
247 "Run configs given. By default, runs the configs marked \"runByDefault\" in gConfigs.");
mtklein@google.comc2897432013-09-10 19:23:38 +0000248DEFINE_string(logFile, "", "Also write stdout here.");
mtklein@google.comdbd41c82013-09-13 20:11:09 +0000249DEFINE_int32(minMs, 20, "Shortest time we'll allow a benchmark to run.");
borenet332600f2014-06-24 12:40:01 -0700250DEFINE_int32(maxMs, 1000, "Longest time we'll allow a benchmark to run.");
commit-bot@chromium.org4bbe2e52014-04-21 21:12:32 +0000251DEFINE_bool(runOnce, kIsDebug, "Run each bench exactly once and don't report timings.");
mtklein@google.comdbd41c82013-09-13 20:11:09 +0000252DEFINE_double(error, 0.01,
253 "Ratio of subsequent bench measurements must drop within 1±error to converge.");
mtklein@google.comc2897432013-09-10 19:23:38 +0000254DEFINE_string(timeFormat, "%9.2f", "Format to print results, in milliseconds per 1000 loops.");
mtklein@google.comdbd41c82013-09-13 20:11:09 +0000255DEFINE_bool2(verbose, v, false, "Print more.");
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +0000256DEFINE_string(outResultsFile, "", "If given, the results will be written to the file in JSON format.");
commit-bot@chromium.org56b7a6d2014-03-13 16:22:00 +0000257DEFINE_bool(dryRun, false, "Don't actually run the tests, just print what would have been done.");
258
mtklein@google.comdbd41c82013-09-13 20:11:09 +0000259// Has this bench converged? First arguments are milliseconds / loop iteration,
260// last is overall runtime in milliseconds.
261static bool HasConverged(double prevPerLoop, double currPerLoop, double currRaw) {
262 if (currRaw < FLAGS_minMs) {
263 return false;
264 }
265 const double low = 1 - FLAGS_error, high = 1 + FLAGS_error;
266 const double ratio = currPerLoop / prevPerLoop;
267 return low < ratio && ratio < high;
268}
tomhudson@google.com86bb9b72012-04-03 13:28:57 +0000269
caryclark@google.com5987f582012-10-02 18:33:14 +0000270int tool_main(int argc, char** argv);
271int tool_main(int argc, char** argv) {
mtklein30e6e2a2014-06-18 11:44:15 -0700272 SetupCrashHandler();
commit-bot@chromium.org6dda8272014-01-23 17:21:19 +0000273 SkCommandLineFlags::Parse(argc, argv);
bsalomon@google.com4e230682013-01-15 20:37:04 +0000274#if SK_ENABLE_INST_COUNT
commit-bot@chromium.org6dda8272014-01-23 17:21:19 +0000275 if (FLAGS_leaks) {
276 gPrintInstCount = true;
277 }
bsalomon@google.com65a87cc2012-08-14 13:15:44 +0000278#endif
reed@android.com3a859a02009-01-28 00:56:29 +0000279 SkAutoGraphics ag;
bsalomon@google.com65a87cc2012-08-14 13:15:44 +0000280
mtklein@google.comc2897432013-09-10 19:23:38 +0000281 // First, parse some flags.
tfarinaf168b862014-06-19 12:32:29 -0700282 BenchLogger logger;
mtklein@google.comc2897432013-09-10 19:23:38 +0000283 if (FLAGS_logFile.count()) {
284 logger.SetLogFile(FLAGS_logFile[0]);
285 }
scroggo@google.com9a412522012-09-07 15:21:18 +0000286
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +0000287 LoggerResultsWriter logWriter(logger, FLAGS_timeFormat[0]);
288 MultiResultsWriter writer;
289 writer.add(&logWriter);
commit-bot@chromium.org61744ec2014-05-16 13:15:41 +0000290
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +0000291 SkAutoTDelete<JSONResultsWriter> jsonWriter;
292 if (FLAGS_outResultsFile.count()) {
293 jsonWriter.reset(SkNEW(JSONResultsWriter(FLAGS_outResultsFile[0])));
294 writer.add(jsonWriter.get());
295 }
commit-bot@chromium.org61744ec2014-05-16 13:15:41 +0000296
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +0000297 // Instantiate after all the writers have been added to writer so that we
298 // call close() before their destructors are called on the way out.
299 CallEnd<MultiResultsWriter> ender(writer);
300
mtklein@google.comc2897432013-09-10 19:23:38 +0000301 const uint8_t alpha = FLAGS_forceBlend ? 0x80 : 0xFF;
302 SkTriState::State dither = SkTriState::kDefault;
303 for (size_t i = 0; i < 3; i++) {
304 if (strcmp(SkTriState::Name[i], FLAGS_forceDither[0]) == 0) {
305 dither = static_cast<SkTriState::State>(i);
reed@android.comb398fe82009-01-07 11:47:57 +0000306 }
307 }
mtklein@google.comc2897432013-09-10 19:23:38 +0000308
309 BenchMode benchMode = kNormal_BenchMode;
310 for (size_t i = 0; i < SK_ARRAY_COUNT(BenchMode_Name); i++) {
311 if (strcmp(FLAGS_mode[0], BenchMode_Name[i]) == 0) {
312 benchMode = static_cast<BenchMode>(i);
313 }
keyar@chromium.orgfdd909c2012-07-20 23:03:42 +0000314 }
mtklein@google.comc2897432013-09-10 19:23:38 +0000315
316 SkTDArray<int> configs;
borenet@google.com4e061d32013-09-16 19:12:19 +0000317 bool runDefaultConfigs = false;
mtklein@google.comc2897432013-09-10 19:23:38 +0000318 // Try user-given configs first.
319 for (int i = 0; i < FLAGS_config.count(); i++) {
robertphillips@google.come9cd27d2013-10-16 17:48:11 +0000320 for (int j = 0; j < static_cast<int>(SK_ARRAY_COUNT(gConfigs)); ++j) {
mtklein@google.comc2897432013-09-10 19:23:38 +0000321 if (0 == strcmp(FLAGS_config[i], gConfigs[j].name)) {
322 *configs.append() = j;
borenet@google.com4e061d32013-09-16 19:12:19 +0000323 } else if (0 == strcmp(FLAGS_config[i], kDefaultsConfigStr)) {
324 runDefaultConfigs = true;
mtklein@google.comc2897432013-09-10 19:23:38 +0000325 }
326 }
keyar@chromium.orgfdd909c2012-07-20 23:03:42 +0000327 }
mtklein@google.comc2897432013-09-10 19:23:38 +0000328 // If there weren't any, fill in with defaults.
borenet@google.com4e061d32013-09-16 19:12:19 +0000329 if (runDefaultConfigs) {
robertphillips@google.come9cd27d2013-10-16 17:48:11 +0000330 for (int i = 0; i < static_cast<int>(SK_ARRAY_COUNT(gConfigs)); ++i) {
mtklein@google.comc2897432013-09-10 19:23:38 +0000331 if (gConfigs[i].runByDefault) {
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000332 *configs.append() = i;
333 }
tomhudson@google.com13eaaaa2012-04-16 18:00:40 +0000334 }
335 }
mtklein@google.comc2897432013-09-10 19:23:38 +0000336 // Filter out things we can't run.
337 if (kNormal_BenchMode != benchMode) {
bsalomon@google.com604a56a2013-03-15 15:42:15 +0000338 // Non-rendering configs only run in normal mode
339 for (int i = 0; i < configs.count(); ++i) {
mtklein@google.comc2897432013-09-10 19:23:38 +0000340 const Config& config = gConfigs[configs[i]];
tfarinaf168b862014-06-19 12:32:29 -0700341 if (Benchmark::kNonRendering_Backend == config.backend) {
bsalomon@google.com604a56a2013-03-15 15:42:15 +0000342 configs.remove(i, 1);
343 --i;
344 }
345 }
346 }
scroggo@google.com111fd112013-09-25 21:42:12 +0000347
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000348#if SK_SUPPORT_GPU
349 for (int i = 0; i < configs.count(); ++i) {
mtklein@google.comc2897432013-09-10 19:23:38 +0000350 const Config& config = gConfigs[configs[i]];
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000351
tfarinaf168b862014-06-19 12:32:29 -0700352 if (Benchmark::kGPU_Backend == config.backend) {
rmistry05ead8a2014-06-23 06:13:46 -0700353 GrContext* context = gContextFactory.get(config.contextType);
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000354 if (NULL == context) {
commit-bot@chromium.org0f298cc2014-04-30 15:54:29 +0000355 SkDebugf("GrContext could not be created for config %s. Config will be skipped.\n",
356 config.name);
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000357 configs.remove(i);
358 --i;
359 continue;
360 }
mtklein@google.comc2897432013-09-10 19:23:38 +0000361 if (config.sampleCount > context->getMaxSampleCount()){
commit-bot@chromium.org0f298cc2014-04-30 15:54:29 +0000362 SkDebugf(
363 "Sample count (%d) for config %s is not supported. Config will be skipped.\n",
364 config.sampleCount, config.name);
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000365 configs.remove(i);
366 --i;
367 continue;
368 }
369 }
370 }
371#endif
372
mtklein@google.comc2897432013-09-10 19:23:38 +0000373 // All flags should be parsed now. Report our settings.
commit-bot@chromium.org4bbe2e52014-04-21 21:12:32 +0000374 if (FLAGS_runOnce) {
375 logger.logError("bench was run with --runOnce, so we're going to hide the times."
376 " It's for your own good!\n");
mtklein@google.comc2897432013-09-10 19:23:38 +0000377 }
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +0000378 writer.option("mode", FLAGS_mode[0]);
379 writer.option("alpha", SkStringPrintf("0x%02X", alpha).c_str());
380 writer.option("antialias", SkStringPrintf("%d", FLAGS_forceAA).c_str());
381 writer.option("filter", SkStringPrintf("%d", FLAGS_forceFilter).c_str());
382 writer.option("dither", SkTriState::Name[dither]);
383
384 writer.option("rotate", SkStringPrintf("%d", FLAGS_rotate).c_str());
385 writer.option("scale", SkStringPrintf("%d", FLAGS_scale).c_str());
386 writer.option("clip", SkStringPrintf("%d", FLAGS_clip).c_str());
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000387
bungeman@google.coma5d48412011-06-15 17:25:46 +0000388#if defined(SK_BUILD_FOR_WIN32)
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +0000389 writer.option("system", "WIN32");
bungeman@google.coma5d48412011-06-15 17:25:46 +0000390#elif defined(SK_BUILD_FOR_MAC)
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +0000391 writer.option("system", "MAC");
bungeman@google.coma5d48412011-06-15 17:25:46 +0000392#elif defined(SK_BUILD_FOR_ANDROID)
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +0000393 writer.option("system", "ANDROID");
bungeman@google.coma5d48412011-06-15 17:25:46 +0000394#elif defined(SK_BUILD_FOR_UNIX)
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +0000395 writer.option("system", "UNIX");
bungeman@google.coma5d48412011-06-15 17:25:46 +0000396#else
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +0000397 writer.option("system", "other");
bungeman@google.coma5d48412011-06-15 17:25:46 +0000398#endif
399
400#if defined(SK_DEBUG)
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +0000401 writer.option("build", "DEBUG");
402#else
403 writer.option("build", "RELEASE");
bungeman@google.coma5d48412011-06-15 17:25:46 +0000404#endif
mtklein@google.comc2897432013-09-10 19:23:38 +0000405
406 // Set texture cache limits if non-default.
bsalomon@google.com5c90e292013-02-22 19:17:13 +0000407 for (size_t i = 0; i < SK_ARRAY_COUNT(gConfigs); ++i) {
bsalomon@google.comcb265352013-02-22 16:13:16 +0000408#if SK_SUPPORT_GPU
mtklein@google.comc2897432013-09-10 19:23:38 +0000409 const Config& config = gConfigs[i];
tfarinaf168b862014-06-19 12:32:29 -0700410 if (Benchmark::kGPU_Backend != config.backend) {
mtklein@google.comc2897432013-09-10 19:23:38 +0000411 continue;
bsalomon@google.comcb265352013-02-22 16:13:16 +0000412 }
rmistry05ead8a2014-06-23 06:13:46 -0700413 GrContext* context = gContextFactory.get(config.contextType);
mtklein@google.comc2897432013-09-10 19:23:38 +0000414 if (NULL == context) {
415 continue;
416 }
417
418 size_t bytes;
419 int count;
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000420 context->getResourceCacheLimits(&count, &bytes);
mtklein@google.comc2897432013-09-10 19:23:38 +0000421 if (-1 != FLAGS_gpuCacheBytes) {
422 bytes = static_cast<size_t>(FLAGS_gpuCacheBytes);
423 }
424 if (-1 != FLAGS_gpuCacheCount) {
425 count = FLAGS_gpuCacheCount;
426 }
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000427 context->setResourceCacheLimits(count, bytes);
bsalomon@google.comcb265352013-02-22 16:13:16 +0000428#endif
429 }
bsalomon@google.com74913722011-10-27 20:44:19 +0000430
mtklein@google.comc2897432013-09-10 19:23:38 +0000431 // Run each bench in each configuration it supports and we asked for.
432 Iter iter;
tfarinaf168b862014-06-19 12:32:29 -0700433 Benchmark* bench;
reed@android.combd700c32009-01-05 03:34:50 +0000434 while ((bench = iter.next()) != NULL) {
tfarinaf168b862014-06-19 12:32:29 -0700435 SkAutoTUnref<Benchmark> benchUnref(bench);
mtklein@google.comc2897432013-09-10 19:23:38 +0000436 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, bench->getName())) {
reed@android.comb398fe82009-01-07 11:47:57 +0000437 continue;
438 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000439
mtklein@google.comc2897432013-09-10 19:23:38 +0000440 bench->setForceAlpha(alpha);
441 bench->setForceAA(FLAGS_forceAA);
442 bench->setForceFilter(FLAGS_forceFilter);
443 bench->setDither(dither);
mtklein683e9062014-06-18 07:15:48 -0700444 bench->preDraw();
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000445
mtklein@google.comc2897432013-09-10 19:23:38 +0000446 bool loggedBenchName = false;
447 for (int i = 0; i < configs.count(); ++i) {
448 const int configIndex = configs[i];
449 const Config& config = gConfigs[configIndex];
tomhudson@google.com13eaaaa2012-04-16 18:00:40 +0000450
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000451 if (!bench->isSuitableFor(config.backend)) {
mtklein@google.comc2897432013-09-10 19:23:38 +0000452 continue;
bsalomon@google.com604a56a2013-03-15 15:42:15 +0000453 }
454
bsalomon@google.comcb265352013-02-22 16:13:16 +0000455 GrContext* context = NULL;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000456#if SK_SUPPORT_GPU
sugoi@google.com9c55f802013-03-07 20:52:59 +0000457 SkGLContextHelper* glContext = NULL;
tfarinaf168b862014-06-19 12:32:29 -0700458 if (Benchmark::kGPU_Backend == config.backend) {
rmistry05ead8a2014-06-23 06:13:46 -0700459 context = gContextFactory.get(config.contextType);
bsalomon@google.comcb265352013-02-22 16:13:16 +0000460 if (NULL == context) {
461 continue;
462 }
mtklein@google.comc2897432013-09-10 19:23:38 +0000463 glContext = gContextFactory.getGLContext(config.contextType);
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000464 }
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000465#endif
reed@google.com1bc6c6a2014-02-04 14:02:44 +0000466
mtklein@google.comc2897432013-09-10 19:23:38 +0000467 SkAutoTUnref<SkCanvas> canvas;
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000468 SkAutoTUnref<SkPicture> recordFrom;
469 SkPictureRecorder recorderTo;
mtklein@google.comc2897432013-09-10 19:23:38 +0000470 const SkIPoint dim = bench->getSize();
bsalomon@google.com604a56a2013-03-15 15:42:15 +0000471
reed@google.com1bc6c6a2014-02-04 14:02:44 +0000472 SkAutoTUnref<SkSurface> surface;
tfarinaf168b862014-06-19 12:32:29 -0700473 if (Benchmark::kNonRendering_Backend != config.backend) {
reed@google.com1bc6c6a2014-02-04 14:02:44 +0000474 surface.reset(make_surface(config.fColorType,
475 dim,
476 config.backend,
477 config.sampleCount,
478 context));
479 if (!surface.get()) {
mtklein@google.comdbd41c82013-09-13 20:11:09 +0000480 logger.logError(SkStringPrintf(
481 "Device creation failure for config %s. Will skip.\n", config.name));
mtklein@google.comc2897432013-09-10 19:23:38 +0000482 continue;
keyar@chromium.orgfdd909c2012-07-20 23:03:42 +0000483 }
junov@chromium.orgfb103892012-09-20 19:35:43 +0000484
mtklein@google.comc2897432013-09-10 19:23:38 +0000485 switch(benchMode) {
486 case kDeferredSilent_BenchMode:
487 case kDeferred_BenchMode:
reed@google.com1bc6c6a2014-02-04 14:02:44 +0000488 canvas.reset(SkDeferredCanvas::Create(surface.get()));
mtklein@google.comc2897432013-09-10 19:23:38 +0000489 break;
490 case kRecord_BenchMode:
robertphillips9f1c2412014-06-09 06:25:34 -0700491 canvas.reset(SkRef(recorderTo.beginRecording(dim.fX, dim.fY)));
mtklein@google.comc2897432013-09-10 19:23:38 +0000492 break;
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000493 case kPictureRecord_BenchMode: {
494 SkPictureRecorder recorderFrom;
robertphillips9f1c2412014-06-09 06:25:34 -0700495 bench->draw(1, recorderFrom.beginRecording(dim.fX, dim.fY));
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000496 recordFrom.reset(recorderFrom.endRecording());
robertphillips9f1c2412014-06-09 06:25:34 -0700497 canvas.reset(SkRef(recorderTo.beginRecording(dim.fX, dim.fY)));
mtklein@google.comc2897432013-09-10 19:23:38 +0000498 break;
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000499 }
mtklein@google.comc2897432013-09-10 19:23:38 +0000500 case kNormal_BenchMode:
reed@google.com1bc6c6a2014-02-04 14:02:44 +0000501 canvas.reset(SkRef(surface->getCanvas()));
mtklein@google.comc2897432013-09-10 19:23:38 +0000502 break;
503 default:
504 SkASSERT(false);
505 }
506 }
507
508 if (NULL != canvas) {
509 canvas->clear(SK_ColorWHITE);
skia.committer@gmail.com51997012014-04-14 03:04:57 +0000510 if (FLAGS_clip) {
511 perform_clip(canvas, dim.fX, dim.fY);
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000512 }
skia.committer@gmail.com51997012014-04-14 03:04:57 +0000513 if (FLAGS_scale) {
514 perform_scale(canvas, dim.fX, dim.fY);
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000515 }
skia.committer@gmail.com51997012014-04-14 03:04:57 +0000516 if (FLAGS_rotate) {
517 perform_rotate(canvas, dim.fX, dim.fY);
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000518 }
mtklein@google.comc2897432013-09-10 19:23:38 +0000519 }
520
521 if (!loggedBenchName) {
522 loggedBenchName = true;
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +0000523 writer.bench(bench->getName(), dim.fX, dim.fY);
mtklein@google.comc2897432013-09-10 19:23:38 +0000524 }
525
526#if SK_SUPPORT_GPU
527 SkGLContextHelper* contextHelper = NULL;
tfarinaf168b862014-06-19 12:32:29 -0700528 if (Benchmark::kGPU_Backend == config.backend) {
mtklein@google.comc2897432013-09-10 19:23:38 +0000529 contextHelper = gContextFactory.getGLContext(config.contextType);
530 }
mtklein9ac68ee2014-06-20 11:29:20 -0700531 Timer timer(contextHelper);
mtklein@google.comc2897432013-09-10 19:23:38 +0000532#else
mtklein9ac68ee2014-06-20 11:29:20 -0700533 Timer timer;
mtklein@google.comc2897432013-09-10 19:23:38 +0000534#endif
535
mtklein@google.com9ef1d212013-09-13 20:39:50 +0000536 double previous = std::numeric_limits<double>::infinity();
mtklein@google.comdbd41c82013-09-13 20:11:09 +0000537 bool converged = false;
djsollen@google.com70de4da2013-10-10 17:33:39 +0000538
539 // variables used to compute loopsPerFrame
540 double frameIntervalTime = 0.0f;
541 int frameIntervalTotalLoops = 0;
542
543 bool frameIntervalComputed = false;
544 int loopsPerFrame = 0;
545 int loopsPerIter = 0;
mtklein@google.comdbd41c82013-09-13 20:11:09 +0000546 if (FLAGS_verbose) { SkDebugf("%s %s: ", bench->getName(), config.name); }
commit-bot@chromium.org56b7a6d2014-03-13 16:22:00 +0000547 if (!FLAGS_dryRun) {
548 do {
549 // Ramp up 1 -> 2 -> 4 -> 8 -> 16 -> ... -> ~1 billion.
550 loopsPerIter = (loopsPerIter == 0) ? 1 : loopsPerIter * 2;
551 if (loopsPerIter >= (1<<30) || timer.fWall > FLAGS_maxMs) {
552 // If you find it takes more than a billion loops to get up to 20ms of runtime,
553 // you've got a computer clocked at several THz or have a broken benchmark. ;)
554 // "1B ought to be enough for anybody."
555 logger.logError(SkStringPrintf(
556 "\nCan't get %s %s to converge in %dms (%d loops)",
557 bench->getName(), config.name, FLAGS_maxMs, loopsPerIter));
558 break;
djsollen@google.com70de4da2013-10-10 17:33:39 +0000559 }
560
commit-bot@chromium.org56b7a6d2014-03-13 16:22:00 +0000561 if ((benchMode == kRecord_BenchMode || benchMode == kPictureRecord_BenchMode)) {
562 // Clear the recorded commands so that they do not accumulate.
robertphillips9f1c2412014-06-09 06:25:34 -0700563 canvas.reset(SkRef(recorderTo.beginRecording(dim.fX, dim.fY)));
djsollen@google.com70de4da2013-10-10 17:33:39 +0000564 }
565
commit-bot@chromium.org56b7a6d2014-03-13 16:22:00 +0000566 timer.start();
567 // Inner loop that allows us to break the run into smaller
568 // chunks (e.g. frames). This is especially useful for the GPU
569 // as we can flush and/or swap buffers to keep the GPU from
570 // queuing up too much work.
571 for (int loopCount = loopsPerIter; loopCount > 0; ) {
572 // Save and restore around each call to draw() to guarantee a pristine canvas.
573 SkAutoCanvasRestore saveRestore(canvas, true/*also save*/);
djsollen@google.com70de4da2013-10-10 17:33:39 +0000574
commit-bot@chromium.org56b7a6d2014-03-13 16:22:00 +0000575 int loops;
576 if (frameIntervalComputed && loopCount > loopsPerFrame) {
577 loops = loopsPerFrame;
578 loopCount -= loopsPerFrame;
579 } else {
580 loops = loopCount;
581 loopCount = 0;
djsollen@google.com70de4da2013-10-10 17:33:39 +0000582 }
djsollen@google.comdcfed6c2013-10-10 18:48:27 +0000583
commit-bot@chromium.org56b7a6d2014-03-13 16:22:00 +0000584 if (benchMode == kPictureRecord_BenchMode) {
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000585 recordFrom->draw(canvas);
commit-bot@chromium.org56b7a6d2014-03-13 16:22:00 +0000586 } else {
587 bench->draw(loops, canvas);
588 }
589
590 if (kDeferredSilent_BenchMode == benchMode) {
591 static_cast<SkDeferredCanvas*>(canvas.get())->silentFlush();
592 } else if (NULL != canvas) {
593 canvas->flush();
594 }
595
596 #if SK_SUPPORT_GPU
597 // swap drawing buffers on each frame to prevent the GPU
598 // from queuing up too much work
599 if (NULL != glContext) {
600 glContext->swapBuffers();
601 }
602 #endif
603 }
604
605
606
607 // Stop truncated timers before GL calls complete, and stop the full timers after.
608 timer.truncatedEnd();
609 #if SK_SUPPORT_GPU
610 if (NULL != glContext) {
611 context->flush();
612 SK_GL(*glContext, Finish());
613 }
614 #endif
615 timer.end();
616
617 // setup the frame interval for subsequent iterations
618 if (!frameIntervalComputed) {
619 frameIntervalTime += timer.fWall;
620 frameIntervalTotalLoops += loopsPerIter;
621 if (frameIntervalTime >= FLAGS_minMs) {
622 frameIntervalComputed = true;
623 loopsPerFrame =
624 (int)(((double)frameIntervalTotalLoops / frameIntervalTime) * FLAGS_minMs);
625 if (loopsPerFrame < 1) {
626 loopsPerFrame = 1;
627 }
628 // SkDebugf(" %s has %d loops in %f ms (normalized to %d)\n",
629 // bench->getName(), frameIntervalTotalLoops,
630 // timer.fWall, loopsPerFrame);
631 }
632 }
633
634 const double current = timer.fWall / loopsPerIter;
635 if (FLAGS_verbose && current > previous) { SkDebugf("↑"); }
636 if (FLAGS_verbose) { SkDebugf("%.3g ", current); }
637 converged = HasConverged(previous, current, timer.fWall);
638 previous = current;
commit-bot@chromium.org4bbe2e52014-04-21 21:12:32 +0000639 } while (!FLAGS_runOnce && !converged);
commit-bot@chromium.org56b7a6d2014-03-13 16:22:00 +0000640 }
mtklein@google.comdbd41c82013-09-13 20:11:09 +0000641 if (FLAGS_verbose) { SkDebugf("\n"); }
mtklein@google.comc2897432013-09-10 19:23:38 +0000642
tfarinaf168b862014-06-19 12:32:29 -0700643 if (!FLAGS_dryRun && FLAGS_outDir.count() && Benchmark::kNonRendering_Backend != config.backend) {
reed@google.com1bc6c6a2014-02-04 14:02:44 +0000644 SkAutoTUnref<SkImage> image(surface->newImageSnapshot());
645 if (image.get()) {
646 saveFile(bench->getName(), config.name, FLAGS_outDir[0],
647 image);
648 }
mtklein@google.comc2897432013-09-10 19:23:38 +0000649 }
650
commit-bot@chromium.org4bbe2e52014-04-21 21:12:32 +0000651 if (FLAGS_runOnce) {
652 // Let's not mislead ourselves by looking at Debug build or single iteration bench times!
mtklein@google.comc2897432013-09-10 19:23:38 +0000653 continue;
654 }
655
656 // Normalize to ms per 1000 iterations.
djsollen@google.com70de4da2013-10-10 17:33:39 +0000657 const double normalize = 1000.0 / loopsPerIter;
mtklein@google.comc2897432013-09-10 19:23:38 +0000658 const struct { char shortName; const char* longName; double ms; } times[] = {
659 {'w', "msecs", normalize * timer.fWall},
660 {'W', "Wmsecs", normalize * timer.fTruncatedWall},
661 {'c', "cmsecs", normalize * timer.fCpu},
662 {'C', "Cmsecs", normalize * timer.fTruncatedCpu},
663 {'g', "gmsecs", normalize * timer.fGpu},
664 };
665
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +0000666 writer.config(config.name);
mtklein@google.comc2897432013-09-10 19:23:38 +0000667 for (size_t i = 0; i < SK_ARRAY_COUNT(times); i++) {
668 if (strchr(FLAGS_timers[0], times[i].shortName) && times[i].ms > 0) {
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +0000669 writer.timer(times[i].longName, times[i].ms);
commit-bot@chromium.org7495f592013-06-03 19:31:07 +0000670 }
reed@google.com25df8882011-07-14 19:03:58 +0000671 }
bsalomon@google.com604a56a2013-03-15 15:42:15 +0000672 }
reed@android.combd700c32009-01-05 03:34:50 +0000673 }
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000674#if SK_SUPPORT_GPU
bsalomon@google.comcb265352013-02-22 16:13:16 +0000675 gContextFactory.destroyContexts();
robertphillips@google.com9d594202012-09-13 14:05:00 +0000676#endif
reed@android.combd700c32009-01-05 03:34:50 +0000677 return 0;
678}
caryclark@google.com5987f582012-10-02 18:33:14 +0000679
borenet@google.com7158e6a2012-11-01 17:43:44 +0000680#if !defined(SK_BUILD_FOR_IOS) && !defined(SK_BUILD_FOR_NACL)
caryclark@google.com5987f582012-10-02 18:33:14 +0000681int main(int argc, char * const argv[]) {
682 return tool_main(argc, (char**) argv);
683}
684#endif