blob: af708782822186e6516efa94665d9ebf04fa5607 [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
mtklein@google.comc2897432013-09-10 19:23:38 +00008#include "BenchTimer.h"
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +00009#include "ResultsWriter.h"
scroggo@google.com9a412522012-09-07 15:21:18 +000010#include "SkBenchLogger.h"
bsalomon@google.com971d0c82011-08-19 17:22:05 +000011#include "SkBenchmark.h"
robertphillips@google.com73672252013-08-29 12:40:26 +000012#include "SkBitmapDevice.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"
commit-bot@chromium.org6adce672014-02-03 14:48:17 +000018#include "SkGMBench.h"
reed@android.com3a859a02009-01-28 00:56:29 +000019#include "SkGraphics.h"
reed@android.comb398fe82009-01-07 11:47:57 +000020#include "SkImageEncoder.h"
mtklein@google.comc2897432013-09-10 19:23:38 +000021#include "SkOSFile.h"
reed@android.com6c924ad2009-03-31 03:48:49 +000022#include "SkPicture.h"
robertphillips@google.com770963f2014-04-18 18:04:41 +000023#include "SkPictureRecorder.h"
reed@android.combd700c32009-01-05 03:34:50 +000024#include "SkString.h"
reed@google.com1bc6c6a2014-02-04 14:02:44 +000025#include "SkSurface.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"
31#include "SkGpuDevice.h"
32#include "gl/GrGLDefines.h"
33#else
34class GrContext;
35#endif // SK_SUPPORT_GPU
36
mtklein@google.com9ef1d212013-09-13 20:39:50 +000037#include <limits>
38
mtklein@google.comc2897432013-09-10 19:23:38 +000039enum BenchMode {
40 kNormal_BenchMode,
41 kDeferred_BenchMode,
42 kDeferredSilent_BenchMode,
43 kRecord_BenchMode,
44 kPictureRecord_BenchMode
keyar@chromium.orgfdd909c2012-07-20 23:03:42 +000045};
mtklein@google.comdbd41c82013-09-13 20:11:09 +000046const char* BenchMode_Name[] = {
47 "normal", "deferred", "deferredSilent", "record", "picturerecord"
48};
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +000049
borenet@google.com4e061d32013-09-16 19:12:19 +000050static const char kDefaultsConfigStr[] = "defaults";
51
keyar@chromium.orgfdd909c2012-07-20 23:03:42 +000052///////////////////////////////////////////////////////////////////////////////
53
reed@android.combd700c32009-01-05 03:34:50 +000054class Iter {
55public:
commit-bot@chromium.org575d9cd2014-02-24 23:25:49 +000056 Iter() : fBenches(BenchRegistry::Head()), fGMs(skiagm::GMRegistry::Head()) {}
rmistry@google.comfbfcd562012-08-23 18:09:54 +000057
reed@android.combd700c32009-01-05 03:34:50 +000058 SkBenchmark* next() {
commit-bot@chromium.org575d9cd2014-02-24 23:25:49 +000059 if (fBenches) {
60 BenchRegistry::Factory f = fBenches->factory();
61 fBenches = fBenches->next();
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +000062 return (*f)(NULL);
reed@android.combd700c32009-01-05 03:34:50 +000063 }
commit-bot@chromium.org575d9cd2014-02-24 23:25:49 +000064
65 while (fGMs) {
66 SkAutoTDelete<skiagm::GM> gm(fGMs->factory()(NULL));
67 fGMs = fGMs->next();
68 if (gm->getFlags() & skiagm::GM::kAsBench_Flag) {
69 return SkNEW_ARGS(SkGMBench, (gm.detach()));
70 }
71 }
72
reed@android.combd700c32009-01-05 03:34:50 +000073 return NULL;
74 }
bungeman@google.comd1a416a2011-05-18 18:37:07 +000075
reed@android.combd700c32009-01-05 03:34:50 +000076private:
commit-bot@chromium.org575d9cd2014-02-24 23:25:49 +000077 const BenchRegistry* fBenches;
78 const skiagm::GMRegistry* fGMs;
reed@android.combd700c32009-01-05 03:34:50 +000079};
80
81static void make_filename(const char name[], SkString* path) {
82 path->set(name);
83 for (int i = 0; name[i]; i++) {
84 switch (name[i]) {
85 case '/':
86 case '\\':
87 case ' ':
88 case ':':
89 path->writable_str()[i] = '-';
90 break;
91 default:
92 break;
93 }
94 }
95}
96
reed@android.com4c7d3d62009-01-21 03:15:13 +000097static void saveFile(const char name[], const char config[], const char dir[],
reed@google.com1bc6c6a2014-02-04 14:02:44 +000098 const SkImage* image) {
99 SkAutoTUnref<SkData> data(image->encode(SkImageEncoder::kPNG_Type, 100));
100 if (NULL == data.get()) {
reed@android.com4c7d3d62009-01-21 03:15:13 +0000101 return;
102 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000103
mtklein@google.comc2897432013-09-10 19:23:38 +0000104 SkString filename;
105 make_filename(name, &filename);
106 filename.appendf("_%s.png", config);
107 SkString path = SkOSPath::SkPathJoin(dir, filename.c_str());
108 ::remove(path.c_str());
reed@google.com1bc6c6a2014-02-04 14:02:44 +0000109
reed@google.comace13542014-02-06 22:00:58 +0000110 SkFILEWStream stream(path.c_str());
reed@google.com1bc6c6a2014-02-04 14:02:44 +0000111 stream.write(data->data(), data->size());
reed@android.com4c7d3d62009-01-21 03:15:13 +0000112}
113
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000114static void perform_clip(SkCanvas* canvas, int w, int h) {
reed@android.com4c7d3d62009-01-21 03:15:13 +0000115 SkRect r;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000116
reed@android.com4c7d3d62009-01-21 03:15:13 +0000117 r.set(SkIntToScalar(10), SkIntToScalar(10),
118 SkIntToScalar(w*2/3), SkIntToScalar(h*2/3));
119 canvas->clipRect(r, SkRegion::kIntersect_Op);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000120
reed@android.com4c7d3d62009-01-21 03:15:13 +0000121 r.set(SkIntToScalar(w/3), SkIntToScalar(h/3),
122 SkIntToScalar(w-10), SkIntToScalar(h-10));
123 canvas->clipRect(r, SkRegion::kXOR_Op);
124}
125
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000126static void perform_rotate(SkCanvas* canvas, int w, int h) {
reed@android.com4c7d3d62009-01-21 03:15:13 +0000127 const SkScalar x = SkIntToScalar(w) / 2;
128 const SkScalar y = SkIntToScalar(h) / 2;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000129
reed@android.com4c7d3d62009-01-21 03:15:13 +0000130 canvas->translate(x, y);
131 canvas->rotate(SkIntToScalar(35));
132 canvas->translate(-x, -y);
133}
134
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000135static void perform_scale(SkCanvas* canvas, int w, int h) {
reed@android.com387359e2009-08-04 01:51:09 +0000136 const SkScalar x = SkIntToScalar(w) / 2;
137 const SkScalar y = SkIntToScalar(h) / 2;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000138
reed@android.com387359e2009-08-04 01:51:09 +0000139 canvas->translate(x, y);
140 // just enough so we can't take the sprite case
141 canvas->scale(SK_Scalar1 * 99/100, SK_Scalar1 * 99/100);
142 canvas->translate(-x, -y);
143}
144
reed@google.com1bc6c6a2014-02-04 14:02:44 +0000145static SkSurface* make_surface(SkColorType colorType, const SkIPoint& size,
146 SkBenchmark::Backend backend, int sampleCount,
147 GrContext* context) {
148 SkSurface* surface = NULL;
149 SkImageInfo info = SkImageInfo::Make(size.fX, size.fY, colorType,
150 kPremul_SkAlphaType);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000151
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000152 switch (backend) {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000153 case SkBenchmark::kRaster_Backend:
reed@google.com1bc6c6a2014-02-04 14:02:44 +0000154 surface = SkSurface::NewRaster(info);
155 surface->getCanvas()->clear(SK_ColorWHITE);
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000156 break;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000157#if SK_SUPPORT_GPU
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000158 case SkBenchmark::kGPU_Backend: {
reed@google.com1bc6c6a2014-02-04 14:02:44 +0000159 surface = SkSurface::NewRenderTarget(context, info, sampleCount);
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000160 break;
bsalomon@google.comcb265352013-02-22 16:13:16 +0000161 }
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000162#endif
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000163 case SkBenchmark::kPDF_Backend:
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000164 default:
mtklein@google.com330313a2013-08-22 15:37:26 +0000165 SkDEBUGFAIL("unsupported");
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000166 }
reed@google.com1bc6c6a2014-02-04 14:02:44 +0000167 return surface;
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000168}
169
bsalomon@google.comcb265352013-02-22 16:13:16 +0000170#if SK_SUPPORT_GPU
171GrContextFactory gContextFactory;
172typedef GrContextFactory::GLContextType GLContextType;
mtklein@google.comc2897432013-09-10 19:23:38 +0000173static const GLContextType kNative = GrContextFactory::kNative_GLContextType;
commit-bot@chromium.orge8989572014-01-26 20:51:00 +0000174static const GLContextType kNVPR = GrContextFactory::kNVPR_GLContextType;
mtklein@google.comc2897432013-09-10 19:23:38 +0000175#if SK_ANGLE
176static const GLContextType kANGLE = GrContextFactory::kANGLE_GLContextType;
mtklein@google.comc2897432013-09-10 19:23:38 +0000177#endif
178static const GLContextType kDebug = GrContextFactory::kDebug_GLContextType;
179static const GLContextType kNull = GrContextFactory::kNull_GLContextType;
bsalomon@google.comcb265352013-02-22 16:13:16 +0000180#else
181typedef int GLContextType;
mtklein@google.comc2897432013-09-10 19:23:38 +0000182static const GLContextType kNative = 0, kANGLE = 0, kDebug = 0, kNull = 0;
bsalomon@google.comcb265352013-02-22 16:13:16 +0000183#endif
184
mtklein@google.comc2897432013-09-10 19:23:38 +0000185#ifdef SK_DEBUG
186static const bool kIsDebug = true;
187#else
188static const bool kIsDebug = false;
189#endif
190
191static const struct Config {
reed@google.com1bc6c6a2014-02-04 14:02:44 +0000192 SkColorType fColorType;
mtklein@google.comc2897432013-09-10 19:23:38 +0000193 const char* name;
194 int sampleCount;
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000195 SkBenchmark::Backend backend;
mtklein@google.comc2897432013-09-10 19:23:38 +0000196 GLContextType contextType;
197 bool runByDefault;
reed@android.com4bc19832009-01-19 20:08:35 +0000198} gConfigs[] = {
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000199 { kN32_SkColorType, "NONRENDERING", 0, SkBenchmark::kNonRendering_Backend, kNative, true},
200 { kN32_SkColorType, "8888", 0, SkBenchmark::kRaster_Backend, kNative, true},
reed@google.com1bc6c6a2014-02-04 14:02:44 +0000201 { kRGB_565_SkColorType, "565", 0, SkBenchmark::kRaster_Backend, kNative, true},
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000202#if SK_SUPPORT_GPU
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000203 { kN32_SkColorType, "GPU", 0, SkBenchmark::kGPU_Backend, kNative, true},
204 { kN32_SkColorType, "MSAA4", 4, SkBenchmark::kGPU_Backend, kNative, false},
205 { kN32_SkColorType, "MSAA16", 16, SkBenchmark::kGPU_Backend, kNative, false},
206 { kN32_SkColorType, "NVPRMSAA4", 4, SkBenchmark::kGPU_Backend, kNVPR, true},
207 { kN32_SkColorType, "NVPRMSAA16", 16, SkBenchmark::kGPU_Backend, kNVPR, false},
robertphillips@google.comd3b9fbb2012-03-28 16:19:11 +0000208#if SK_ANGLE
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000209 { kN32_SkColorType, "ANGLE", 0, SkBenchmark::kGPU_Backend, kANGLE, true},
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000210#endif // SK_ANGLE
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000211 { kN32_SkColorType, "Debug", 0, SkBenchmark::kGPU_Backend, kDebug, kIsDebug},
212 { kN32_SkColorType, "NULLGPU", 0, SkBenchmark::kGPU_Backend, kNull, true},
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000213#endif // SK_SUPPORT_GPU
reed@android.com4bc19832009-01-19 20:08:35 +0000214};
215
mtklein@google.comc2897432013-09-10 19:23:38 +0000216DEFINE_string(outDir, "", "If given, image of each bench will be put in outDir.");
217DEFINE_string(timers, "cg", "Timers to display. "
218 "Options: w(all) W(all, truncated) c(pu) C(pu, truncated) g(pu)");
reed@android.com4c7d3d62009-01-21 03:15:13 +0000219
mtklein@google.comc2897432013-09-10 19:23:38 +0000220DEFINE_bool(rotate, false, "Rotate canvas before bench run?");
221DEFINE_bool(scale, false, "Scale canvas before bench run?");
222DEFINE_bool(clip, false, "Clip canvas before bench run?");
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000223
mtklein@google.comc2897432013-09-10 19:23:38 +0000224DEFINE_bool(forceAA, true, "Force anti-aliasing?");
225DEFINE_bool(forceFilter, false, "Force bitmap filtering?");
226DEFINE_string(forceDither, "default", "Force dithering: true, false, or default?");
227DEFINE_bool(forceBlend, false, "Force alpha blending?");
228
229DEFINE_int32(gpuCacheBytes, -1, "GPU cache size limit in bytes. 0 to disable cache.");
230DEFINE_int32(gpuCacheCount, -1, "GPU cache size limit in object count. 0 to disable cache.");
231
commit-bot@chromium.org6dda8272014-01-23 17:21:19 +0000232DEFINE_bool2(leaks, l, false, "show leaked ref cnt'd objects.");
mtklein@google.comc2897432013-09-10 19:23:38 +0000233DEFINE_string(match, "", "[~][^]substring[$] [...] of test name to run.\n"
234 "Multiple matches may be separated by spaces.\n"
235 "~ causes a matching test to always be skipped\n"
236 "^ requires the start of the test to match\n"
237 "$ requires the end of the test to match\n"
238 "^ and $ requires an exact match\n"
239 "If a test does not match any list entry,\n"
240 "it is skipped unless some list entry starts with ~\n");
241DEFINE_string(mode, "normal",
242 "normal: draw to a normal canvas;\n"
243 "deferred: draw to a deferred canvas;\n"
244 "deferredSilent: deferred with silent playback;\n"
245 "record: draw to an SkPicture;\n"
246 "picturerecord: draw from an SkPicture to an SkPicture.\n");
borenet@google.com4e061d32013-09-16 19:12:19 +0000247DEFINE_string(config, kDefaultsConfigStr,
248 "Run configs given. By default, runs the configs marked \"runByDefault\" in gConfigs.");
mtklein@google.comc2897432013-09-10 19:23:38 +0000249DEFINE_string(logFile, "", "Also write stdout here.");
mtklein@google.comdbd41c82013-09-13 20:11:09 +0000250DEFINE_int32(minMs, 20, "Shortest time we'll allow a benchmark to run.");
251DEFINE_int32(maxMs, 4000, "Longest time we'll allow a benchmark to run.");
commit-bot@chromium.org4bbe2e52014-04-21 21:12:32 +0000252DEFINE_bool(runOnce, kIsDebug, "Run each bench exactly once and don't report timings.");
mtklein@google.comdbd41c82013-09-13 20:11:09 +0000253DEFINE_double(error, 0.01,
254 "Ratio of subsequent bench measurements must drop within 1±error to converge.");
mtklein@google.comc2897432013-09-10 19:23:38 +0000255DEFINE_string(timeFormat, "%9.2f", "Format to print results, in milliseconds per 1000 loops.");
mtklein@google.comdbd41c82013-09-13 20:11:09 +0000256DEFINE_bool2(verbose, v, false, "Print more.");
tfarina@chromium.org725a64c2013-12-31 14:29:52 +0000257DEFINE_string2(resourcePath, i, "resources", "directory for test resources.");
commit-bot@chromium.org61744ec2014-05-16 13:15:41 +0000258#ifdef SK_BUILD_JSON_WRITER
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +0000259DEFINE_string(outResultsFile, "", "If given, the results will be written to the file in JSON format.");
commit-bot@chromium.org61744ec2014-05-16 13:15:41 +0000260#endif
commit-bot@chromium.org56b7a6d2014-03-13 16:22:00 +0000261DEFINE_bool(dryRun, false, "Don't actually run the tests, just print what would have been done.");
262
mtklein@google.comdbd41c82013-09-13 20:11:09 +0000263// Has this bench converged? First arguments are milliseconds / loop iteration,
264// last is overall runtime in milliseconds.
265static bool HasConverged(double prevPerLoop, double currPerLoop, double currRaw) {
266 if (currRaw < FLAGS_minMs) {
267 return false;
268 }
269 const double low = 1 - FLAGS_error, high = 1 + FLAGS_error;
270 const double ratio = currPerLoop / prevPerLoop;
271 return low < ratio && ratio < high;
272}
tomhudson@google.com86bb9b72012-04-03 13:28:57 +0000273
caryclark@google.com5987f582012-10-02 18:33:14 +0000274int tool_main(int argc, char** argv);
275int tool_main(int argc, char** argv) {
commit-bot@chromium.org6dda8272014-01-23 17:21:19 +0000276 SkCommandLineFlags::Parse(argc, argv);
bsalomon@google.com4e230682013-01-15 20:37:04 +0000277#if SK_ENABLE_INST_COUNT
commit-bot@chromium.org6dda8272014-01-23 17:21:19 +0000278 if (FLAGS_leaks) {
279 gPrintInstCount = true;
280 }
bsalomon@google.com65a87cc2012-08-14 13:15:44 +0000281#endif
reed@android.com3a859a02009-01-28 00:56:29 +0000282 SkAutoGraphics ag;
bsalomon@google.com65a87cc2012-08-14 13:15:44 +0000283
mtklein@google.comc2897432013-09-10 19:23:38 +0000284 // First, parse some flags.
scroggo@google.com9a412522012-09-07 15:21:18 +0000285 SkBenchLogger logger;
mtklein@google.comc2897432013-09-10 19:23:38 +0000286 if (FLAGS_logFile.count()) {
287 logger.SetLogFile(FLAGS_logFile[0]);
288 }
scroggo@google.com9a412522012-09-07 15:21:18 +0000289
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +0000290 LoggerResultsWriter logWriter(logger, FLAGS_timeFormat[0]);
291 MultiResultsWriter writer;
292 writer.add(&logWriter);
commit-bot@chromium.org61744ec2014-05-16 13:15:41 +0000293
294#ifdef SK_BUILD_JSON_WRITER
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +0000295 SkAutoTDelete<JSONResultsWriter> jsonWriter;
296 if (FLAGS_outResultsFile.count()) {
297 jsonWriter.reset(SkNEW(JSONResultsWriter(FLAGS_outResultsFile[0])));
298 writer.add(jsonWriter.get());
299 }
commit-bot@chromium.org61744ec2014-05-16 13:15:41 +0000300#endif
301
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +0000302 // Instantiate after all the writers have been added to writer so that we
303 // call close() before their destructors are called on the way out.
304 CallEnd<MultiResultsWriter> ender(writer);
305
mtklein@google.comc2897432013-09-10 19:23:38 +0000306 const uint8_t alpha = FLAGS_forceBlend ? 0x80 : 0xFF;
307 SkTriState::State dither = SkTriState::kDefault;
308 for (size_t i = 0; i < 3; i++) {
309 if (strcmp(SkTriState::Name[i], FLAGS_forceDither[0]) == 0) {
310 dither = static_cast<SkTriState::State>(i);
reed@android.comb398fe82009-01-07 11:47:57 +0000311 }
312 }
mtklein@google.comc2897432013-09-10 19:23:38 +0000313
314 BenchMode benchMode = kNormal_BenchMode;
315 for (size_t i = 0; i < SK_ARRAY_COUNT(BenchMode_Name); i++) {
316 if (strcmp(FLAGS_mode[0], BenchMode_Name[i]) == 0) {
317 benchMode = static_cast<BenchMode>(i);
318 }
keyar@chromium.orgfdd909c2012-07-20 23:03:42 +0000319 }
mtklein@google.comc2897432013-09-10 19:23:38 +0000320
321 SkTDArray<int> configs;
borenet@google.com4e061d32013-09-16 19:12:19 +0000322 bool runDefaultConfigs = false;
mtklein@google.comc2897432013-09-10 19:23:38 +0000323 // Try user-given configs first.
324 for (int i = 0; i < FLAGS_config.count(); i++) {
robertphillips@google.come9cd27d2013-10-16 17:48:11 +0000325 for (int j = 0; j < static_cast<int>(SK_ARRAY_COUNT(gConfigs)); ++j) {
mtklein@google.comc2897432013-09-10 19:23:38 +0000326 if (0 == strcmp(FLAGS_config[i], gConfigs[j].name)) {
327 *configs.append() = j;
borenet@google.com4e061d32013-09-16 19:12:19 +0000328 } else if (0 == strcmp(FLAGS_config[i], kDefaultsConfigStr)) {
329 runDefaultConfigs = true;
mtklein@google.comc2897432013-09-10 19:23:38 +0000330 }
331 }
keyar@chromium.orgfdd909c2012-07-20 23:03:42 +0000332 }
mtklein@google.comc2897432013-09-10 19:23:38 +0000333 // If there weren't any, fill in with defaults.
borenet@google.com4e061d32013-09-16 19:12:19 +0000334 if (runDefaultConfigs) {
robertphillips@google.come9cd27d2013-10-16 17:48:11 +0000335 for (int i = 0; i < static_cast<int>(SK_ARRAY_COUNT(gConfigs)); ++i) {
mtklein@google.comc2897432013-09-10 19:23:38 +0000336 if (gConfigs[i].runByDefault) {
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000337 *configs.append() = i;
338 }
tomhudson@google.com13eaaaa2012-04-16 18:00:40 +0000339 }
340 }
mtklein@google.comc2897432013-09-10 19:23:38 +0000341 // Filter out things we can't run.
342 if (kNormal_BenchMode != benchMode) {
bsalomon@google.com604a56a2013-03-15 15:42:15 +0000343 // Non-rendering configs only run in normal mode
344 for (int i = 0; i < configs.count(); ++i) {
mtklein@google.comc2897432013-09-10 19:23:38 +0000345 const Config& config = gConfigs[configs[i]];
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000346 if (SkBenchmark::kNonRendering_Backend == config.backend) {
bsalomon@google.com604a56a2013-03-15 15:42:15 +0000347 configs.remove(i, 1);
348 --i;
349 }
350 }
351 }
scroggo@google.com111fd112013-09-25 21:42:12 +0000352 // Set the resource path.
353 if (!FLAGS_resourcePath.isEmpty()) {
354 SkBenchmark::SetResourcePath(FLAGS_resourcePath[0]);
355 }
356
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000357#if SK_SUPPORT_GPU
358 for (int i = 0; i < configs.count(); ++i) {
mtklein@google.comc2897432013-09-10 19:23:38 +0000359 const Config& config = gConfigs[configs[i]];
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000360
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000361 if (SkBenchmark::kGPU_Backend == config.backend) {
mtklein@google.comc2897432013-09-10 19:23:38 +0000362 GrContext* context = gContextFactory.get(config.contextType);
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000363 if (NULL == context) {
commit-bot@chromium.org0f298cc2014-04-30 15:54:29 +0000364 SkDebugf("GrContext could not be created for config %s. Config will be skipped.\n",
365 config.name);
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000366 configs.remove(i);
367 --i;
368 continue;
369 }
mtklein@google.comc2897432013-09-10 19:23:38 +0000370 if (config.sampleCount > context->getMaxSampleCount()){
commit-bot@chromium.org0f298cc2014-04-30 15:54:29 +0000371 SkDebugf(
372 "Sample count (%d) for config %s is not supported. Config will be skipped.\n",
373 config.sampleCount, config.name);
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000374 configs.remove(i);
375 --i;
376 continue;
377 }
378 }
379 }
380#endif
381
mtklein@google.comc2897432013-09-10 19:23:38 +0000382 // All flags should be parsed now. Report our settings.
commit-bot@chromium.org4bbe2e52014-04-21 21:12:32 +0000383 if (FLAGS_runOnce) {
384 logger.logError("bench was run with --runOnce, so we're going to hide the times."
385 " It's for your own good!\n");
mtklein@google.comc2897432013-09-10 19:23:38 +0000386 }
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +0000387 writer.option("mode", FLAGS_mode[0]);
388 writer.option("alpha", SkStringPrintf("0x%02X", alpha).c_str());
389 writer.option("antialias", SkStringPrintf("%d", FLAGS_forceAA).c_str());
390 writer.option("filter", SkStringPrintf("%d", FLAGS_forceFilter).c_str());
391 writer.option("dither", SkTriState::Name[dither]);
392
393 writer.option("rotate", SkStringPrintf("%d", FLAGS_rotate).c_str());
394 writer.option("scale", SkStringPrintf("%d", FLAGS_scale).c_str());
395 writer.option("clip", SkStringPrintf("%d", FLAGS_clip).c_str());
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000396
bungeman@google.coma5d48412011-06-15 17:25:46 +0000397#if defined(SK_BUILD_FOR_WIN32)
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +0000398 writer.option("system", "WIN32");
bungeman@google.coma5d48412011-06-15 17:25:46 +0000399#elif defined(SK_BUILD_FOR_MAC)
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +0000400 writer.option("system", "MAC");
bungeman@google.coma5d48412011-06-15 17:25:46 +0000401#elif defined(SK_BUILD_FOR_ANDROID)
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +0000402 writer.option("system", "ANDROID");
bungeman@google.coma5d48412011-06-15 17:25:46 +0000403#elif defined(SK_BUILD_FOR_UNIX)
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +0000404 writer.option("system", "UNIX");
bungeman@google.coma5d48412011-06-15 17:25:46 +0000405#else
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +0000406 writer.option("system", "other");
bungeman@google.coma5d48412011-06-15 17:25:46 +0000407#endif
408
409#if defined(SK_DEBUG)
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +0000410 writer.option("build", "DEBUG");
411#else
412 writer.option("build", "RELEASE");
bungeman@google.coma5d48412011-06-15 17:25:46 +0000413#endif
mtklein@google.comc2897432013-09-10 19:23:38 +0000414
415 // Set texture cache limits if non-default.
bsalomon@google.com5c90e292013-02-22 19:17:13 +0000416 for (size_t i = 0; i < SK_ARRAY_COUNT(gConfigs); ++i) {
bsalomon@google.comcb265352013-02-22 16:13:16 +0000417#if SK_SUPPORT_GPU
mtklein@google.comc2897432013-09-10 19:23:38 +0000418 const Config& config = gConfigs[i];
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000419 if (SkBenchmark::kGPU_Backend != config.backend) {
mtklein@google.comc2897432013-09-10 19:23:38 +0000420 continue;
bsalomon@google.comcb265352013-02-22 16:13:16 +0000421 }
mtklein@google.comc2897432013-09-10 19:23:38 +0000422 GrContext* context = gContextFactory.get(config.contextType);
423 if (NULL == context) {
424 continue;
425 }
426
427 size_t bytes;
428 int count;
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000429 context->getResourceCacheLimits(&count, &bytes);
mtklein@google.comc2897432013-09-10 19:23:38 +0000430 if (-1 != FLAGS_gpuCacheBytes) {
431 bytes = static_cast<size_t>(FLAGS_gpuCacheBytes);
432 }
433 if (-1 != FLAGS_gpuCacheCount) {
434 count = FLAGS_gpuCacheCount;
435 }
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000436 context->setResourceCacheLimits(count, bytes);
bsalomon@google.comcb265352013-02-22 16:13:16 +0000437#endif
438 }
bsalomon@google.com74913722011-10-27 20:44:19 +0000439
mtklein@google.comc2897432013-09-10 19:23:38 +0000440 // Run each bench in each configuration it supports and we asked for.
441 Iter iter;
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +0000442 SkBenchmark* bench;
reed@android.combd700c32009-01-05 03:34:50 +0000443 while ((bench = iter.next()) != NULL) {
bsalomon@google.com7fbc6042012-08-13 22:10:05 +0000444 SkAutoTUnref<SkBenchmark> benchUnref(bench);
mtklein@google.comc2897432013-09-10 19:23:38 +0000445 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, bench->getName())) {
reed@android.comb398fe82009-01-07 11:47:57 +0000446 continue;
447 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000448
mtklein@google.comc2897432013-09-10 19:23:38 +0000449 bench->setForceAlpha(alpha);
450 bench->setForceAA(FLAGS_forceAA);
451 bench->setForceFilter(FLAGS_forceFilter);
452 bench->setDither(dither);
mtklein683e9062014-06-18 07:15:48 -0700453 bench->preDraw();
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000454
mtklein@google.comc2897432013-09-10 19:23:38 +0000455 bool loggedBenchName = false;
456 for (int i = 0; i < configs.count(); ++i) {
457 const int configIndex = configs[i];
458 const Config& config = gConfigs[configIndex];
tomhudson@google.com13eaaaa2012-04-16 18:00:40 +0000459
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000460 if (!bench->isSuitableFor(config.backend)) {
mtklein@google.comc2897432013-09-10 19:23:38 +0000461 continue;
bsalomon@google.com604a56a2013-03-15 15:42:15 +0000462 }
463
bsalomon@google.comcb265352013-02-22 16:13:16 +0000464 GrContext* context = NULL;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000465#if SK_SUPPORT_GPU
sugoi@google.com9c55f802013-03-07 20:52:59 +0000466 SkGLContextHelper* glContext = NULL;
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000467 if (SkBenchmark::kGPU_Backend == config.backend) {
mtklein@google.comc2897432013-09-10 19:23:38 +0000468 context = gContextFactory.get(config.contextType);
bsalomon@google.comcb265352013-02-22 16:13:16 +0000469 if (NULL == context) {
470 continue;
471 }
mtklein@google.comc2897432013-09-10 19:23:38 +0000472 glContext = gContextFactory.getGLContext(config.contextType);
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000473 }
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000474#endif
reed@google.com1bc6c6a2014-02-04 14:02:44 +0000475
mtklein@google.comc2897432013-09-10 19:23:38 +0000476 SkAutoTUnref<SkCanvas> canvas;
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000477 SkAutoTUnref<SkPicture> recordFrom;
478 SkPictureRecorder recorderTo;
mtklein@google.comc2897432013-09-10 19:23:38 +0000479 const SkIPoint dim = bench->getSize();
bsalomon@google.com604a56a2013-03-15 15:42:15 +0000480
reed@google.com1bc6c6a2014-02-04 14:02:44 +0000481 SkAutoTUnref<SkSurface> surface;
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000482 if (SkBenchmark::kNonRendering_Backend != config.backend) {
reed@google.com1bc6c6a2014-02-04 14:02:44 +0000483 surface.reset(make_surface(config.fColorType,
484 dim,
485 config.backend,
486 config.sampleCount,
487 context));
488 if (!surface.get()) {
mtklein@google.comdbd41c82013-09-13 20:11:09 +0000489 logger.logError(SkStringPrintf(
490 "Device creation failure for config %s. Will skip.\n", config.name));
mtklein@google.comc2897432013-09-10 19:23:38 +0000491 continue;
keyar@chromium.orgfdd909c2012-07-20 23:03:42 +0000492 }
junov@chromium.orgfb103892012-09-20 19:35:43 +0000493
mtklein@google.comc2897432013-09-10 19:23:38 +0000494 switch(benchMode) {
495 case kDeferredSilent_BenchMode:
496 case kDeferred_BenchMode:
reed@google.com1bc6c6a2014-02-04 14:02:44 +0000497 canvas.reset(SkDeferredCanvas::Create(surface.get()));
mtklein@google.comc2897432013-09-10 19:23:38 +0000498 break;
499 case kRecord_BenchMode:
robertphillips9f1c2412014-06-09 06:25:34 -0700500 canvas.reset(SkRef(recorderTo.beginRecording(dim.fX, dim.fY)));
mtklein@google.comc2897432013-09-10 19:23:38 +0000501 break;
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000502 case kPictureRecord_BenchMode: {
503 SkPictureRecorder recorderFrom;
robertphillips9f1c2412014-06-09 06:25:34 -0700504 bench->draw(1, recorderFrom.beginRecording(dim.fX, dim.fY));
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000505 recordFrom.reset(recorderFrom.endRecording());
robertphillips9f1c2412014-06-09 06:25:34 -0700506 canvas.reset(SkRef(recorderTo.beginRecording(dim.fX, dim.fY)));
mtklein@google.comc2897432013-09-10 19:23:38 +0000507 break;
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000508 }
mtklein@google.comc2897432013-09-10 19:23:38 +0000509 case kNormal_BenchMode:
reed@google.com1bc6c6a2014-02-04 14:02:44 +0000510 canvas.reset(SkRef(surface->getCanvas()));
mtklein@google.comc2897432013-09-10 19:23:38 +0000511 break;
512 default:
513 SkASSERT(false);
514 }
515 }
516
517 if (NULL != canvas) {
518 canvas->clear(SK_ColorWHITE);
skia.committer@gmail.com51997012014-04-14 03:04:57 +0000519 if (FLAGS_clip) {
520 perform_clip(canvas, dim.fX, dim.fY);
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000521 }
skia.committer@gmail.com51997012014-04-14 03:04:57 +0000522 if (FLAGS_scale) {
523 perform_scale(canvas, dim.fX, dim.fY);
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000524 }
skia.committer@gmail.com51997012014-04-14 03:04:57 +0000525 if (FLAGS_rotate) {
526 perform_rotate(canvas, dim.fX, dim.fY);
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000527 }
mtklein@google.comc2897432013-09-10 19:23:38 +0000528 }
529
530 if (!loggedBenchName) {
531 loggedBenchName = true;
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +0000532 writer.bench(bench->getName(), dim.fX, dim.fY);
mtklein@google.comc2897432013-09-10 19:23:38 +0000533 }
534
535#if SK_SUPPORT_GPU
536 SkGLContextHelper* contextHelper = NULL;
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000537 if (SkBenchmark::kGPU_Backend == config.backend) {
mtklein@google.comc2897432013-09-10 19:23:38 +0000538 contextHelper = gContextFactory.getGLContext(config.contextType);
539 }
540 BenchTimer timer(contextHelper);
541#else
542 BenchTimer timer;
543#endif
544
mtklein@google.com9ef1d212013-09-13 20:39:50 +0000545 double previous = std::numeric_limits<double>::infinity();
mtklein@google.comdbd41c82013-09-13 20:11:09 +0000546 bool converged = false;
djsollen@google.com70de4da2013-10-10 17:33:39 +0000547
548 // variables used to compute loopsPerFrame
549 double frameIntervalTime = 0.0f;
550 int frameIntervalTotalLoops = 0;
551
552 bool frameIntervalComputed = false;
553 int loopsPerFrame = 0;
554 int loopsPerIter = 0;
mtklein@google.comdbd41c82013-09-13 20:11:09 +0000555 if (FLAGS_verbose) { SkDebugf("%s %s: ", bench->getName(), config.name); }
commit-bot@chromium.org56b7a6d2014-03-13 16:22:00 +0000556 if (!FLAGS_dryRun) {
557 do {
558 // Ramp up 1 -> 2 -> 4 -> 8 -> 16 -> ... -> ~1 billion.
559 loopsPerIter = (loopsPerIter == 0) ? 1 : loopsPerIter * 2;
560 if (loopsPerIter >= (1<<30) || timer.fWall > FLAGS_maxMs) {
561 // If you find it takes more than a billion loops to get up to 20ms of runtime,
562 // you've got a computer clocked at several THz or have a broken benchmark. ;)
563 // "1B ought to be enough for anybody."
564 logger.logError(SkStringPrintf(
565 "\nCan't get %s %s to converge in %dms (%d loops)",
566 bench->getName(), config.name, FLAGS_maxMs, loopsPerIter));
567 break;
djsollen@google.com70de4da2013-10-10 17:33:39 +0000568 }
569
commit-bot@chromium.org56b7a6d2014-03-13 16:22:00 +0000570 if ((benchMode == kRecord_BenchMode || benchMode == kPictureRecord_BenchMode)) {
571 // Clear the recorded commands so that they do not accumulate.
robertphillips9f1c2412014-06-09 06:25:34 -0700572 canvas.reset(SkRef(recorderTo.beginRecording(dim.fX, dim.fY)));
djsollen@google.com70de4da2013-10-10 17:33:39 +0000573 }
574
commit-bot@chromium.org56b7a6d2014-03-13 16:22:00 +0000575 timer.start();
576 // Inner loop that allows us to break the run into smaller
577 // chunks (e.g. frames). This is especially useful for the GPU
578 // as we can flush and/or swap buffers to keep the GPU from
579 // queuing up too much work.
580 for (int loopCount = loopsPerIter; loopCount > 0; ) {
581 // Save and restore around each call to draw() to guarantee a pristine canvas.
582 SkAutoCanvasRestore saveRestore(canvas, true/*also save*/);
djsollen@google.com70de4da2013-10-10 17:33:39 +0000583
commit-bot@chromium.org56b7a6d2014-03-13 16:22:00 +0000584 int loops;
585 if (frameIntervalComputed && loopCount > loopsPerFrame) {
586 loops = loopsPerFrame;
587 loopCount -= loopsPerFrame;
588 } else {
589 loops = loopCount;
590 loopCount = 0;
djsollen@google.com70de4da2013-10-10 17:33:39 +0000591 }
djsollen@google.comdcfed6c2013-10-10 18:48:27 +0000592
commit-bot@chromium.org56b7a6d2014-03-13 16:22:00 +0000593 if (benchMode == kPictureRecord_BenchMode) {
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000594 recordFrom->draw(canvas);
commit-bot@chromium.org56b7a6d2014-03-13 16:22:00 +0000595 } else {
596 bench->draw(loops, canvas);
597 }
598
599 if (kDeferredSilent_BenchMode == benchMode) {
600 static_cast<SkDeferredCanvas*>(canvas.get())->silentFlush();
601 } else if (NULL != canvas) {
602 canvas->flush();
603 }
604
605 #if SK_SUPPORT_GPU
606 // swap drawing buffers on each frame to prevent the GPU
607 // from queuing up too much work
608 if (NULL != glContext) {
609 glContext->swapBuffers();
610 }
611 #endif
612 }
613
614
615
616 // Stop truncated timers before GL calls complete, and stop the full timers after.
617 timer.truncatedEnd();
618 #if SK_SUPPORT_GPU
619 if (NULL != glContext) {
620 context->flush();
621 SK_GL(*glContext, Finish());
622 }
623 #endif
624 timer.end();
625
626 // setup the frame interval for subsequent iterations
627 if (!frameIntervalComputed) {
628 frameIntervalTime += timer.fWall;
629 frameIntervalTotalLoops += loopsPerIter;
630 if (frameIntervalTime >= FLAGS_minMs) {
631 frameIntervalComputed = true;
632 loopsPerFrame =
633 (int)(((double)frameIntervalTotalLoops / frameIntervalTime) * FLAGS_minMs);
634 if (loopsPerFrame < 1) {
635 loopsPerFrame = 1;
636 }
637 // SkDebugf(" %s has %d loops in %f ms (normalized to %d)\n",
638 // bench->getName(), frameIntervalTotalLoops,
639 // timer.fWall, loopsPerFrame);
640 }
641 }
642
643 const double current = timer.fWall / loopsPerIter;
644 if (FLAGS_verbose && current > previous) { SkDebugf("↑"); }
645 if (FLAGS_verbose) { SkDebugf("%.3g ", current); }
646 converged = HasConverged(previous, current, timer.fWall);
647 previous = current;
commit-bot@chromium.org4bbe2e52014-04-21 21:12:32 +0000648 } while (!FLAGS_runOnce && !converged);
commit-bot@chromium.org56b7a6d2014-03-13 16:22:00 +0000649 }
mtklein@google.comdbd41c82013-09-13 20:11:09 +0000650 if (FLAGS_verbose) { SkDebugf("\n"); }
mtklein@google.comc2897432013-09-10 19:23:38 +0000651
commit-bot@chromium.org56b7a6d2014-03-13 16:22:00 +0000652 if (!FLAGS_dryRun && FLAGS_outDir.count() && SkBenchmark::kNonRendering_Backend != config.backend) {
reed@google.com1bc6c6a2014-02-04 14:02:44 +0000653 SkAutoTUnref<SkImage> image(surface->newImageSnapshot());
654 if (image.get()) {
655 saveFile(bench->getName(), config.name, FLAGS_outDir[0],
656 image);
657 }
mtklein@google.comc2897432013-09-10 19:23:38 +0000658 }
659
commit-bot@chromium.org4bbe2e52014-04-21 21:12:32 +0000660 if (FLAGS_runOnce) {
661 // Let's not mislead ourselves by looking at Debug build or single iteration bench times!
mtklein@google.comc2897432013-09-10 19:23:38 +0000662 continue;
663 }
664
665 // Normalize to ms per 1000 iterations.
djsollen@google.com70de4da2013-10-10 17:33:39 +0000666 const double normalize = 1000.0 / loopsPerIter;
mtklein@google.comc2897432013-09-10 19:23:38 +0000667 const struct { char shortName; const char* longName; double ms; } times[] = {
668 {'w', "msecs", normalize * timer.fWall},
669 {'W', "Wmsecs", normalize * timer.fTruncatedWall},
670 {'c', "cmsecs", normalize * timer.fCpu},
671 {'C', "Cmsecs", normalize * timer.fTruncatedCpu},
672 {'g', "gmsecs", normalize * timer.fGpu},
673 };
674
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +0000675 writer.config(config.name);
mtklein@google.comc2897432013-09-10 19:23:38 +0000676 for (size_t i = 0; i < SK_ARRAY_COUNT(times); i++) {
677 if (strchr(FLAGS_timers[0], times[i].shortName) && times[i].ms > 0) {
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +0000678 writer.timer(times[i].longName, times[i].ms);
commit-bot@chromium.org7495f592013-06-03 19:31:07 +0000679 }
reed@google.com25df8882011-07-14 19:03:58 +0000680 }
bsalomon@google.com604a56a2013-03-15 15:42:15 +0000681 }
reed@android.combd700c32009-01-05 03:34:50 +0000682 }
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000683#if SK_SUPPORT_GPU
bsalomon@google.comcb265352013-02-22 16:13:16 +0000684 gContextFactory.destroyContexts();
robertphillips@google.com9d594202012-09-13 14:05:00 +0000685#endif
reed@android.combd700c32009-01-05 03:34:50 +0000686 return 0;
687}
caryclark@google.com5987f582012-10-02 18:33:14 +0000688
borenet@google.com7158e6a2012-11-01 17:43:44 +0000689#if !defined(SK_BUILD_FOR_IOS) && !defined(SK_BUILD_FOR_NACL)
caryclark@google.com5987f582012-10-02 18:33:14 +0000690int main(int argc, char * const argv[]) {
691 return tool_main(argc, (char**) argv);
692}
693#endif