blob: ba9bfcac56d4e1b82bfe44872d5e256a2ab18ba6 [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"
reed@android.combd700c32009-01-05 03:34:50 +000023#include "SkString.h"
reed@google.com1bc6c6a2014-02-04 14:02:44 +000024#include "SkSurface.h"
reed@android.com29348cb2009-08-04 18:17:15 +000025
djsollen@google.comdb490e92013-11-20 13:15:40 +000026#if SK_SUPPORT_GPU
27#include "GrContext.h"
28#include "GrContextFactory.h"
29#include "GrRenderTarget.h"
30#include "SkGpuDevice.h"
31#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
commit-bot@chromium.org6adce672014-02-03 14:48:17 +000038// Note that ~SkTDArray is not virtual. This inherits privately to bar using this as a SkTDArray*.
39class RefCntArray : private SkTDArray<SkRefCnt*> {
40public:
41 SkRefCnt** append() { return this->INHERITED::append(); }
42 ~RefCntArray() { this->unrefAll(); }
43private:
44 typedef SkTDArray<SkRefCnt*> INHERITED;
45};
46
47class GMBenchFactory : public SkBenchmarkFactory {
48public:
49 GMBenchFactory(const skiagm::GMRegistry* gmreg)
50 : fGMFactory(gmreg->factory()) {
51 fSelfRegistry = SkNEW_ARGS(BenchRegistry, (this));
52 }
53
54 virtual ~GMBenchFactory() { SkDELETE(fSelfRegistry); }
55
56 virtual SkBenchmark* operator()() const SK_OVERRIDE {
57 skiagm::GM* gm = fGMFactory(NULL);
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +000058 gm->setMode(skiagm::GM::kBench_Mode);
commit-bot@chromium.org6adce672014-02-03 14:48:17 +000059 return SkNEW_ARGS(SkGMBench, (gm));
60 }
61
62private:
63 skiagm::GMRegistry::Factory fGMFactory;
64 BenchRegistry* fSelfRegistry;
65};
66
67static void register_gm_benches() {
68 static bool gOnce;
69 static RefCntArray gGMBenchFactories;
70
71 if (!gOnce) {
72 const skiagm::GMRegistry* gmreg = skiagm::GMRegistry::Head();
73 while (gmreg) {
74 skiagm::GM* gm = gmreg->factory()(NULL);
75 if (NULL != gm && skiagm::GM::kAsBench_Flag & gm->getFlags()) {
76 *gGMBenchFactories.append() = SkNEW_ARGS(GMBenchFactory, (gmreg));
77 }
78 SkDELETE(gm);
79 gmreg = gmreg->next();
80 }
81 gOnce = true;
82 }
83}
84
mtklein@google.comc2897432013-09-10 19:23:38 +000085enum BenchMode {
86 kNormal_BenchMode,
87 kDeferred_BenchMode,
88 kDeferredSilent_BenchMode,
89 kRecord_BenchMode,
90 kPictureRecord_BenchMode
keyar@chromium.orgfdd909c2012-07-20 23:03:42 +000091};
mtklein@google.comdbd41c82013-09-13 20:11:09 +000092const char* BenchMode_Name[] = {
93 "normal", "deferred", "deferredSilent", "record", "picturerecord"
94};
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +000095
borenet@google.com4e061d32013-09-16 19:12:19 +000096static const char kDefaultsConfigStr[] = "defaults";
97
keyar@chromium.orgfdd909c2012-07-20 23:03:42 +000098///////////////////////////////////////////////////////////////////////////////
99
reed@android.combd700c32009-01-05 03:34:50 +0000100class Iter {
101public:
mtklein@google.comc2897432013-09-10 19:23:38 +0000102 Iter() : fBench(BenchRegistry::Head()) {}
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000103
reed@android.combd700c32009-01-05 03:34:50 +0000104 SkBenchmark* next() {
105 if (fBench) {
106 BenchRegistry::Factory f = fBench->factory();
107 fBench = fBench->next();
commit-bot@chromium.org6adce672014-02-03 14:48:17 +0000108 return (*f)();
reed@android.combd700c32009-01-05 03:34:50 +0000109 }
110 return NULL;
111 }
bungeman@google.comd1a416a2011-05-18 18:37:07 +0000112
reed@android.combd700c32009-01-05 03:34:50 +0000113private:
114 const BenchRegistry* fBench;
115};
116
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000117class AutoPrePostDraw {
118public:
119 AutoPrePostDraw(SkBenchmark* bench) : fBench(bench) {
120 fBench->preDraw();
121 }
122 ~AutoPrePostDraw() {
123 fBench->postDraw();
124 }
125private:
126 SkBenchmark* fBench;
127};
128
reed@android.combd700c32009-01-05 03:34:50 +0000129static void make_filename(const char name[], SkString* path) {
130 path->set(name);
131 for (int i = 0; name[i]; i++) {
132 switch (name[i]) {
133 case '/':
134 case '\\':
135 case ' ':
136 case ':':
137 path->writable_str()[i] = '-';
138 break;
139 default:
140 break;
141 }
142 }
143}
144
reed@android.com4c7d3d62009-01-21 03:15:13 +0000145static void saveFile(const char name[], const char config[], const char dir[],
reed@google.com1bc6c6a2014-02-04 14:02:44 +0000146 const SkImage* image) {
147 SkAutoTUnref<SkData> data(image->encode(SkImageEncoder::kPNG_Type, 100));
148 if (NULL == data.get()) {
reed@android.com4c7d3d62009-01-21 03:15:13 +0000149 return;
150 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000151
mtklein@google.comc2897432013-09-10 19:23:38 +0000152 SkString filename;
153 make_filename(name, &filename);
154 filename.appendf("_%s.png", config);
155 SkString path = SkOSPath::SkPathJoin(dir, filename.c_str());
156 ::remove(path.c_str());
reed@google.com1bc6c6a2014-02-04 14:02:44 +0000157
reed@google.comace13542014-02-06 22:00:58 +0000158 SkFILEWStream stream(path.c_str());
reed@google.com1bc6c6a2014-02-04 14:02:44 +0000159 stream.write(data->data(), data->size());
reed@android.com4c7d3d62009-01-21 03:15:13 +0000160}
161
162static void performClip(SkCanvas* canvas, int w, int h) {
163 SkRect r;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000164
reed@android.com4c7d3d62009-01-21 03:15:13 +0000165 r.set(SkIntToScalar(10), SkIntToScalar(10),
166 SkIntToScalar(w*2/3), SkIntToScalar(h*2/3));
167 canvas->clipRect(r, SkRegion::kIntersect_Op);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000168
reed@android.com4c7d3d62009-01-21 03:15:13 +0000169 r.set(SkIntToScalar(w/3), SkIntToScalar(h/3),
170 SkIntToScalar(w-10), SkIntToScalar(h-10));
171 canvas->clipRect(r, SkRegion::kXOR_Op);
172}
173
174static void performRotate(SkCanvas* canvas, int w, int h) {
175 const SkScalar x = SkIntToScalar(w) / 2;
176 const SkScalar y = SkIntToScalar(h) / 2;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000177
reed@android.com4c7d3d62009-01-21 03:15:13 +0000178 canvas->translate(x, y);
179 canvas->rotate(SkIntToScalar(35));
180 canvas->translate(-x, -y);
181}
182
reed@android.com387359e2009-08-04 01:51:09 +0000183static void performScale(SkCanvas* canvas, int w, int h) {
184 const SkScalar x = SkIntToScalar(w) / 2;
185 const SkScalar y = SkIntToScalar(h) / 2;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000186
reed@android.com387359e2009-08-04 01:51:09 +0000187 canvas->translate(x, y);
188 // just enough so we can't take the sprite case
189 canvas->scale(SK_Scalar1 * 99/100, SK_Scalar1 * 99/100);
190 canvas->translate(-x, -y);
191}
192
reed@google.com1bc6c6a2014-02-04 14:02:44 +0000193static SkSurface* make_surface(SkColorType colorType, const SkIPoint& size,
194 SkBenchmark::Backend backend, int sampleCount,
195 GrContext* context) {
196 SkSurface* surface = NULL;
197 SkImageInfo info = SkImageInfo::Make(size.fX, size.fY, colorType,
198 kPremul_SkAlphaType);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000199
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000200 switch (backend) {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000201 case SkBenchmark::kRaster_Backend:
reed@google.com1bc6c6a2014-02-04 14:02:44 +0000202 surface = SkSurface::NewRaster(info);
203 surface->getCanvas()->clear(SK_ColorWHITE);
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000204 break;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000205#if SK_SUPPORT_GPU
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000206 case SkBenchmark::kGPU_Backend: {
reed@google.com1bc6c6a2014-02-04 14:02:44 +0000207 surface = SkSurface::NewRenderTarget(context, info, sampleCount);
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000208 break;
bsalomon@google.comcb265352013-02-22 16:13:16 +0000209 }
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000210#endif
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000211 case SkBenchmark::kPDF_Backend:
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000212 default:
mtklein@google.com330313a2013-08-22 15:37:26 +0000213 SkDEBUGFAIL("unsupported");
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000214 }
reed@google.com1bc6c6a2014-02-04 14:02:44 +0000215 return surface;
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000216}
217
bsalomon@google.comcb265352013-02-22 16:13:16 +0000218#if SK_SUPPORT_GPU
219GrContextFactory gContextFactory;
220typedef GrContextFactory::GLContextType GLContextType;
mtklein@google.comc2897432013-09-10 19:23:38 +0000221static const GLContextType kNative = GrContextFactory::kNative_GLContextType;
commit-bot@chromium.orge8989572014-01-26 20:51:00 +0000222static const GLContextType kNVPR = GrContextFactory::kNVPR_GLContextType;
mtklein@google.comc2897432013-09-10 19:23:38 +0000223#if SK_ANGLE
224static const GLContextType kANGLE = GrContextFactory::kANGLE_GLContextType;
mtklein@google.comc2897432013-09-10 19:23:38 +0000225#endif
226static const GLContextType kDebug = GrContextFactory::kDebug_GLContextType;
227static const GLContextType kNull = GrContextFactory::kNull_GLContextType;
bsalomon@google.comcb265352013-02-22 16:13:16 +0000228#else
229typedef int GLContextType;
mtklein@google.comc2897432013-09-10 19:23:38 +0000230static const GLContextType kNative = 0, kANGLE = 0, kDebug = 0, kNull = 0;
bsalomon@google.comcb265352013-02-22 16:13:16 +0000231#endif
232
mtklein@google.comc2897432013-09-10 19:23:38 +0000233#ifdef SK_DEBUG
234static const bool kIsDebug = true;
235#else
236static const bool kIsDebug = false;
237#endif
238
239static const struct Config {
reed@google.com1bc6c6a2014-02-04 14:02:44 +0000240 SkColorType fColorType;
mtklein@google.comc2897432013-09-10 19:23:38 +0000241 const char* name;
242 int sampleCount;
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000243 SkBenchmark::Backend backend;
mtklein@google.comc2897432013-09-10 19:23:38 +0000244 GLContextType contextType;
245 bool runByDefault;
reed@android.com4bc19832009-01-19 20:08:35 +0000246} gConfigs[] = {
reed@google.com1bc6c6a2014-02-04 14:02:44 +0000247 { kPMColor_SkColorType, "NONRENDERING", 0, SkBenchmark::kNonRendering_Backend, kNative, true},
248 { kPMColor_SkColorType, "8888", 0, SkBenchmark::kRaster_Backend, kNative, true},
249 { kRGB_565_SkColorType, "565", 0, SkBenchmark::kRaster_Backend, kNative, true},
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000250#if SK_SUPPORT_GPU
reed@google.com1bc6c6a2014-02-04 14:02:44 +0000251 { kPMColor_SkColorType, "GPU", 0, SkBenchmark::kGPU_Backend, kNative, true},
252 { kPMColor_SkColorType, "MSAA4", 4, SkBenchmark::kGPU_Backend, kNative, false},
253 { kPMColor_SkColorType, "MSAA16", 16, SkBenchmark::kGPU_Backend, kNative, false},
254 { kPMColor_SkColorType, "NVPRMSAA4", 4, SkBenchmark::kGPU_Backend, kNVPR, true},
255 { kPMColor_SkColorType, "NVPRMSAA16", 16, SkBenchmark::kGPU_Backend, kNVPR, false},
robertphillips@google.comd3b9fbb2012-03-28 16:19:11 +0000256#if SK_ANGLE
reed@google.com1bc6c6a2014-02-04 14:02:44 +0000257 { kPMColor_SkColorType, "ANGLE", 0, SkBenchmark::kGPU_Backend, kANGLE, true},
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000258#endif // SK_ANGLE
reed@google.com1bc6c6a2014-02-04 14:02:44 +0000259 { kPMColor_SkColorType, "Debug", 0, SkBenchmark::kGPU_Backend, kDebug, kIsDebug},
260 { kPMColor_SkColorType, "NULLGPU", 0, SkBenchmark::kGPU_Backend, kNull, true},
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000261#endif // SK_SUPPORT_GPU
reed@android.com4bc19832009-01-19 20:08:35 +0000262};
263
mtklein@google.comc2897432013-09-10 19:23:38 +0000264DEFINE_string(outDir, "", "If given, image of each bench will be put in outDir.");
265DEFINE_string(timers, "cg", "Timers to display. "
266 "Options: w(all) W(all, truncated) c(pu) C(pu, truncated) g(pu)");
reed@android.com4c7d3d62009-01-21 03:15:13 +0000267
mtklein@google.comc2897432013-09-10 19:23:38 +0000268DEFINE_bool(rotate, false, "Rotate canvas before bench run?");
269DEFINE_bool(scale, false, "Scale canvas before bench run?");
270DEFINE_bool(clip, false, "Clip canvas before bench run?");
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000271
mtklein@google.comc2897432013-09-10 19:23:38 +0000272DEFINE_bool(forceAA, true, "Force anti-aliasing?");
273DEFINE_bool(forceFilter, false, "Force bitmap filtering?");
274DEFINE_string(forceDither, "default", "Force dithering: true, false, or default?");
275DEFINE_bool(forceBlend, false, "Force alpha blending?");
276
277DEFINE_int32(gpuCacheBytes, -1, "GPU cache size limit in bytes. 0 to disable cache.");
278DEFINE_int32(gpuCacheCount, -1, "GPU cache size limit in object count. 0 to disable cache.");
279
commit-bot@chromium.org6dda8272014-01-23 17:21:19 +0000280DEFINE_bool2(leaks, l, false, "show leaked ref cnt'd objects.");
mtklein@google.comc2897432013-09-10 19:23:38 +0000281DEFINE_string(match, "", "[~][^]substring[$] [...] of test name to run.\n"
282 "Multiple matches may be separated by spaces.\n"
283 "~ causes a matching test to always be skipped\n"
284 "^ requires the start of the test to match\n"
285 "$ requires the end of the test to match\n"
286 "^ and $ requires an exact match\n"
287 "If a test does not match any list entry,\n"
288 "it is skipped unless some list entry starts with ~\n");
289DEFINE_string(mode, "normal",
290 "normal: draw to a normal canvas;\n"
291 "deferred: draw to a deferred canvas;\n"
292 "deferredSilent: deferred with silent playback;\n"
293 "record: draw to an SkPicture;\n"
294 "picturerecord: draw from an SkPicture to an SkPicture.\n");
borenet@google.com4e061d32013-09-16 19:12:19 +0000295DEFINE_string(config, kDefaultsConfigStr,
296 "Run configs given. By default, runs the configs marked \"runByDefault\" in gConfigs.");
mtklein@google.comc2897432013-09-10 19:23:38 +0000297DEFINE_string(logFile, "", "Also write stdout here.");
mtklein@google.comdbd41c82013-09-13 20:11:09 +0000298DEFINE_int32(minMs, 20, "Shortest time we'll allow a benchmark to run.");
299DEFINE_int32(maxMs, 4000, "Longest time we'll allow a benchmark to run.");
300DEFINE_double(error, 0.01,
301 "Ratio of subsequent bench measurements must drop within 1±error to converge.");
mtklein@google.comc2897432013-09-10 19:23:38 +0000302DEFINE_string(timeFormat, "%9.2f", "Format to print results, in milliseconds per 1000 loops.");
mtklein@google.comdbd41c82013-09-13 20:11:09 +0000303DEFINE_bool2(verbose, v, false, "Print more.");
tfarina@chromium.org725a64c2013-12-31 14:29:52 +0000304DEFINE_string2(resourcePath, i, "resources", "directory for test resources.");
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +0000305DEFINE_string(outResultsFile, "", "If given, the results will be written to the file in JSON format.");
mtklein@google.comdbd41c82013-09-13 20:11:09 +0000306
307// Has this bench converged? First arguments are milliseconds / loop iteration,
308// last is overall runtime in milliseconds.
309static bool HasConverged(double prevPerLoop, double currPerLoop, double currRaw) {
310 if (currRaw < FLAGS_minMs) {
311 return false;
312 }
313 const double low = 1 - FLAGS_error, high = 1 + FLAGS_error;
314 const double ratio = currPerLoop / prevPerLoop;
315 return low < ratio && ratio < high;
316}
tomhudson@google.com86bb9b72012-04-03 13:28:57 +0000317
caryclark@google.com5987f582012-10-02 18:33:14 +0000318int tool_main(int argc, char** argv);
319int tool_main(int argc, char** argv) {
commit-bot@chromium.org6adce672014-02-03 14:48:17 +0000320 register_gm_benches();
commit-bot@chromium.org6dda8272014-01-23 17:21:19 +0000321 SkCommandLineFlags::Parse(argc, argv);
bsalomon@google.com4e230682013-01-15 20:37:04 +0000322#if SK_ENABLE_INST_COUNT
commit-bot@chromium.org6dda8272014-01-23 17:21:19 +0000323 if (FLAGS_leaks) {
324 gPrintInstCount = true;
325 }
bsalomon@google.com65a87cc2012-08-14 13:15:44 +0000326#endif
reed@android.com3a859a02009-01-28 00:56:29 +0000327 SkAutoGraphics ag;
bsalomon@google.com65a87cc2012-08-14 13:15:44 +0000328
mtklein@google.comc2897432013-09-10 19:23:38 +0000329 // First, parse some flags.
scroggo@google.com9a412522012-09-07 15:21:18 +0000330 SkBenchLogger logger;
mtklein@google.comc2897432013-09-10 19:23:38 +0000331 if (FLAGS_logFile.count()) {
332 logger.SetLogFile(FLAGS_logFile[0]);
333 }
scroggo@google.com9a412522012-09-07 15:21:18 +0000334
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +0000335 LoggerResultsWriter logWriter(logger, FLAGS_timeFormat[0]);
336 MultiResultsWriter writer;
337 writer.add(&logWriter);
338 SkAutoTDelete<JSONResultsWriter> jsonWriter;
339 if (FLAGS_outResultsFile.count()) {
340 jsonWriter.reset(SkNEW(JSONResultsWriter(FLAGS_outResultsFile[0])));
341 writer.add(jsonWriter.get());
342 }
343 // Instantiate after all the writers have been added to writer so that we
344 // call close() before their destructors are called on the way out.
345 CallEnd<MultiResultsWriter> ender(writer);
346
mtklein@google.comc2897432013-09-10 19:23:38 +0000347 const uint8_t alpha = FLAGS_forceBlend ? 0x80 : 0xFF;
348 SkTriState::State dither = SkTriState::kDefault;
349 for (size_t i = 0; i < 3; i++) {
350 if (strcmp(SkTriState::Name[i], FLAGS_forceDither[0]) == 0) {
351 dither = static_cast<SkTriState::State>(i);
reed@android.comb398fe82009-01-07 11:47:57 +0000352 }
353 }
mtklein@google.comc2897432013-09-10 19:23:38 +0000354
355 BenchMode benchMode = kNormal_BenchMode;
356 for (size_t i = 0; i < SK_ARRAY_COUNT(BenchMode_Name); i++) {
357 if (strcmp(FLAGS_mode[0], BenchMode_Name[i]) == 0) {
358 benchMode = static_cast<BenchMode>(i);
359 }
keyar@chromium.orgfdd909c2012-07-20 23:03:42 +0000360 }
mtklein@google.comc2897432013-09-10 19:23:38 +0000361
362 SkTDArray<int> configs;
borenet@google.com4e061d32013-09-16 19:12:19 +0000363 bool runDefaultConfigs = false;
mtklein@google.comc2897432013-09-10 19:23:38 +0000364 // Try user-given configs first.
365 for (int i = 0; i < FLAGS_config.count(); i++) {
robertphillips@google.come9cd27d2013-10-16 17:48:11 +0000366 for (int j = 0; j < static_cast<int>(SK_ARRAY_COUNT(gConfigs)); ++j) {
mtklein@google.comc2897432013-09-10 19:23:38 +0000367 if (0 == strcmp(FLAGS_config[i], gConfigs[j].name)) {
368 *configs.append() = j;
borenet@google.com4e061d32013-09-16 19:12:19 +0000369 } else if (0 == strcmp(FLAGS_config[i], kDefaultsConfigStr)) {
370 runDefaultConfigs = true;
mtklein@google.comc2897432013-09-10 19:23:38 +0000371 }
372 }
keyar@chromium.orgfdd909c2012-07-20 23:03:42 +0000373 }
mtklein@google.comc2897432013-09-10 19:23:38 +0000374 // If there weren't any, fill in with defaults.
borenet@google.com4e061d32013-09-16 19:12:19 +0000375 if (runDefaultConfigs) {
robertphillips@google.come9cd27d2013-10-16 17:48:11 +0000376 for (int i = 0; i < static_cast<int>(SK_ARRAY_COUNT(gConfigs)); ++i) {
mtklein@google.comc2897432013-09-10 19:23:38 +0000377 if (gConfigs[i].runByDefault) {
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000378 *configs.append() = i;
379 }
tomhudson@google.com13eaaaa2012-04-16 18:00:40 +0000380 }
381 }
mtklein@google.comc2897432013-09-10 19:23:38 +0000382 // Filter out things we can't run.
383 if (kNormal_BenchMode != benchMode) {
bsalomon@google.com604a56a2013-03-15 15:42:15 +0000384 // Non-rendering configs only run in normal mode
385 for (int i = 0; i < configs.count(); ++i) {
mtklein@google.comc2897432013-09-10 19:23:38 +0000386 const Config& config = gConfigs[configs[i]];
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000387 if (SkBenchmark::kNonRendering_Backend == config.backend) {
bsalomon@google.com604a56a2013-03-15 15:42:15 +0000388 configs.remove(i, 1);
389 --i;
390 }
391 }
392 }
scroggo@google.com111fd112013-09-25 21:42:12 +0000393 // Set the resource path.
394 if (!FLAGS_resourcePath.isEmpty()) {
395 SkBenchmark::SetResourcePath(FLAGS_resourcePath[0]);
396 }
397
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000398#if SK_SUPPORT_GPU
399 for (int i = 0; i < configs.count(); ++i) {
mtklein@google.comc2897432013-09-10 19:23:38 +0000400 const Config& config = gConfigs[configs[i]];
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000401
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000402 if (SkBenchmark::kGPU_Backend == config.backend) {
mtklein@google.comc2897432013-09-10 19:23:38 +0000403 GrContext* context = gContextFactory.get(config.contextType);
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000404 if (NULL == context) {
mtklein@google.comdbd41c82013-09-13 20:11:09 +0000405 logger.logError(SkStringPrintf(
406 "Error creating GrContext for config %s. Config will be skipped.\n",
407 config.name));
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000408 configs.remove(i);
409 --i;
410 continue;
411 }
mtklein@google.comc2897432013-09-10 19:23:38 +0000412 if (config.sampleCount > context->getMaxSampleCount()){
mtklein@google.comdbd41c82013-09-13 20:11:09 +0000413 logger.logError(SkStringPrintf(
414 "Sample count (%d) for config %s is unsupported. Config will be skipped.\n",
415 config.sampleCount, config.name));
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000416 configs.remove(i);
417 --i;
418 continue;
419 }
420 }
421 }
422#endif
423
mtklein@google.comc2897432013-09-10 19:23:38 +0000424 // All flags should be parsed now. Report our settings.
425 if (kIsDebug) {
426 logger.logError("bench was built in Debug mode, so we're going to hide the times."
427 " It's for your own good!\n");
428 }
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +0000429 writer.option("mode", FLAGS_mode[0]);
430 writer.option("alpha", SkStringPrintf("0x%02X", alpha).c_str());
431 writer.option("antialias", SkStringPrintf("%d", FLAGS_forceAA).c_str());
432 writer.option("filter", SkStringPrintf("%d", FLAGS_forceFilter).c_str());
433 writer.option("dither", SkTriState::Name[dither]);
434
435 writer.option("rotate", SkStringPrintf("%d", FLAGS_rotate).c_str());
436 writer.option("scale", SkStringPrintf("%d", FLAGS_scale).c_str());
437 writer.option("clip", SkStringPrintf("%d", FLAGS_clip).c_str());
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000438
bungeman@google.coma5d48412011-06-15 17:25:46 +0000439#if defined(SK_BUILD_FOR_WIN32)
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +0000440 writer.option("system", "WIN32");
bungeman@google.coma5d48412011-06-15 17:25:46 +0000441#elif defined(SK_BUILD_FOR_MAC)
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +0000442 writer.option("system", "MAC");
bungeman@google.coma5d48412011-06-15 17:25:46 +0000443#elif defined(SK_BUILD_FOR_ANDROID)
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +0000444 writer.option("system", "ANDROID");
bungeman@google.coma5d48412011-06-15 17:25:46 +0000445#elif defined(SK_BUILD_FOR_UNIX)
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +0000446 writer.option("system", "UNIX");
bungeman@google.coma5d48412011-06-15 17:25:46 +0000447#else
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +0000448 writer.option("system", "other");
bungeman@google.coma5d48412011-06-15 17:25:46 +0000449#endif
450
451#if defined(SK_DEBUG)
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +0000452 writer.option("build", "DEBUG");
453#else
454 writer.option("build", "RELEASE");
bungeman@google.coma5d48412011-06-15 17:25:46 +0000455#endif
mtklein@google.comc2897432013-09-10 19:23:38 +0000456
457 // Set texture cache limits if non-default.
bsalomon@google.com5c90e292013-02-22 19:17:13 +0000458 for (size_t i = 0; i < SK_ARRAY_COUNT(gConfigs); ++i) {
bsalomon@google.comcb265352013-02-22 16:13:16 +0000459#if SK_SUPPORT_GPU
mtklein@google.comc2897432013-09-10 19:23:38 +0000460 const Config& config = gConfigs[i];
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000461 if (SkBenchmark::kGPU_Backend != config.backend) {
mtklein@google.comc2897432013-09-10 19:23:38 +0000462 continue;
bsalomon@google.comcb265352013-02-22 16:13:16 +0000463 }
mtklein@google.comc2897432013-09-10 19:23:38 +0000464 GrContext* context = gContextFactory.get(config.contextType);
465 if (NULL == context) {
466 continue;
467 }
468
469 size_t bytes;
470 int count;
471 context->getTextureCacheLimits(&count, &bytes);
472 if (-1 != FLAGS_gpuCacheBytes) {
473 bytes = static_cast<size_t>(FLAGS_gpuCacheBytes);
474 }
475 if (-1 != FLAGS_gpuCacheCount) {
476 count = FLAGS_gpuCacheCount;
477 }
478 context->setTextureCacheLimits(count, bytes);
bsalomon@google.comcb265352013-02-22 16:13:16 +0000479#endif
480 }
bsalomon@google.com74913722011-10-27 20:44:19 +0000481
mtklein@google.comc2897432013-09-10 19:23:38 +0000482 // Run each bench in each configuration it supports and we asked for.
483 Iter iter;
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +0000484 SkBenchmark* bench;
reed@android.combd700c32009-01-05 03:34:50 +0000485 while ((bench = iter.next()) != NULL) {
bsalomon@google.com7fbc6042012-08-13 22:10:05 +0000486 SkAutoTUnref<SkBenchmark> benchUnref(bench);
mtklein@google.comc2897432013-09-10 19:23:38 +0000487 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, bench->getName())) {
reed@android.comb398fe82009-01-07 11:47:57 +0000488 continue;
489 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000490
mtklein@google.comc2897432013-09-10 19:23:38 +0000491 bench->setForceAlpha(alpha);
492 bench->setForceAA(FLAGS_forceAA);
493 bench->setForceFilter(FLAGS_forceFilter);
494 bench->setDither(dither);
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000495 AutoPrePostDraw appd(bench);
496
mtklein@google.comc2897432013-09-10 19:23:38 +0000497 bool loggedBenchName = false;
498 for (int i = 0; i < configs.count(); ++i) {
499 const int configIndex = configs[i];
500 const Config& config = gConfigs[configIndex];
tomhudson@google.com13eaaaa2012-04-16 18:00:40 +0000501
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000502 if (!bench->isSuitableFor(config.backend)) {
mtklein@google.comc2897432013-09-10 19:23:38 +0000503 continue;
bsalomon@google.com604a56a2013-03-15 15:42:15 +0000504 }
505
bsalomon@google.comcb265352013-02-22 16:13:16 +0000506 GrContext* context = NULL;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000507#if SK_SUPPORT_GPU
sugoi@google.com9c55f802013-03-07 20:52:59 +0000508 SkGLContextHelper* glContext = NULL;
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000509 if (SkBenchmark::kGPU_Backend == config.backend) {
mtklein@google.comc2897432013-09-10 19:23:38 +0000510 context = gContextFactory.get(config.contextType);
bsalomon@google.comcb265352013-02-22 16:13:16 +0000511 if (NULL == context) {
512 continue;
513 }
mtklein@google.comc2897432013-09-10 19:23:38 +0000514 glContext = gContextFactory.getGLContext(config.contextType);
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000515 }
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000516#endif
reed@google.com1bc6c6a2014-02-04 14:02:44 +0000517
mtklein@google.comc2897432013-09-10 19:23:38 +0000518 SkAutoTUnref<SkCanvas> canvas;
519 SkPicture recordFrom, recordTo;
520 const SkIPoint dim = bench->getSize();
bsalomon@google.com604a56a2013-03-15 15:42:15 +0000521
mtklein@google.comc2897432013-09-10 19:23:38 +0000522 const SkPicture::RecordingFlags kRecordFlags =
523 SkPicture::kUsePathBoundsForClip_RecordingFlag;
524
reed@google.com1bc6c6a2014-02-04 14:02:44 +0000525 SkAutoTUnref<SkSurface> surface;
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000526 if (SkBenchmark::kNonRendering_Backend != config.backend) {
reed@google.com1bc6c6a2014-02-04 14:02:44 +0000527 surface.reset(make_surface(config.fColorType,
528 dim,
529 config.backend,
530 config.sampleCount,
531 context));
532 if (!surface.get()) {
mtklein@google.comdbd41c82013-09-13 20:11:09 +0000533 logger.logError(SkStringPrintf(
534 "Device creation failure for config %s. Will skip.\n", config.name));
mtklein@google.comc2897432013-09-10 19:23:38 +0000535 continue;
keyar@chromium.orgfdd909c2012-07-20 23:03:42 +0000536 }
junov@chromium.orgfb103892012-09-20 19:35:43 +0000537
mtklein@google.comc2897432013-09-10 19:23:38 +0000538 switch(benchMode) {
539 case kDeferredSilent_BenchMode:
540 case kDeferred_BenchMode:
reed@google.com1bc6c6a2014-02-04 14:02:44 +0000541 canvas.reset(SkDeferredCanvas::Create(surface.get()));
mtklein@google.comc2897432013-09-10 19:23:38 +0000542 break;
543 case kRecord_BenchMode:
544 canvas.reset(SkRef(recordTo.beginRecording(dim.fX, dim.fY, kRecordFlags)));
545 break;
546 case kPictureRecord_BenchMode:
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000547 bench->draw(1, recordFrom.beginRecording(dim.fX, dim.fY, kRecordFlags));
mtklein@google.comc2897432013-09-10 19:23:38 +0000548 recordFrom.endRecording();
549 canvas.reset(SkRef(recordTo.beginRecording(dim.fX, dim.fY, kRecordFlags)));
550 break;
551 case kNormal_BenchMode:
reed@google.com1bc6c6a2014-02-04 14:02:44 +0000552 canvas.reset(SkRef(surface->getCanvas()));
mtklein@google.comc2897432013-09-10 19:23:38 +0000553 break;
554 default:
555 SkASSERT(false);
556 }
557 }
558
559 if (NULL != canvas) {
560 canvas->clear(SK_ColorWHITE);
561 if (FLAGS_clip) { performClip(canvas, dim.fX, dim.fY); }
562 if (FLAGS_scale) { performScale(canvas, dim.fX, dim.fY); }
563 if (FLAGS_rotate) { performRotate(canvas, dim.fX, dim.fY); }
564 }
565
566 if (!loggedBenchName) {
567 loggedBenchName = true;
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +0000568 writer.bench(bench->getName(), dim.fX, dim.fY);
mtklein@google.comc2897432013-09-10 19:23:38 +0000569 }
570
571#if SK_SUPPORT_GPU
572 SkGLContextHelper* contextHelper = NULL;
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000573 if (SkBenchmark::kGPU_Backend == config.backend) {
mtklein@google.comc2897432013-09-10 19:23:38 +0000574 contextHelper = gContextFactory.getGLContext(config.contextType);
575 }
576 BenchTimer timer(contextHelper);
577#else
578 BenchTimer timer;
579#endif
580
mtklein@google.com9ef1d212013-09-13 20:39:50 +0000581 double previous = std::numeric_limits<double>::infinity();
mtklein@google.comdbd41c82013-09-13 20:11:09 +0000582 bool converged = false;
djsollen@google.com70de4da2013-10-10 17:33:39 +0000583
584 // variables used to compute loopsPerFrame
585 double frameIntervalTime = 0.0f;
586 int frameIntervalTotalLoops = 0;
587
588 bool frameIntervalComputed = false;
589 int loopsPerFrame = 0;
590 int loopsPerIter = 0;
mtklein@google.comdbd41c82013-09-13 20:11:09 +0000591 if (FLAGS_verbose) { SkDebugf("%s %s: ", bench->getName(), config.name); }
mtklein@google.comc2897432013-09-10 19:23:38 +0000592 do {
djsollen@google.com70de4da2013-10-10 17:33:39 +0000593 // Ramp up 1 -> 2 -> 4 -> 8 -> 16 -> ... -> ~1 billion.
594 loopsPerIter = (loopsPerIter == 0) ? 1 : loopsPerIter * 2;
595 if (loopsPerIter >= (1<<30) || timer.fWall > FLAGS_maxMs) {
mtklein@google.comc2897432013-09-10 19:23:38 +0000596 // If you find it takes more than a billion loops to get up to 20ms of runtime,
597 // you've got a computer clocked at several THz or have a broken benchmark. ;)
598 // "1B ought to be enough for anybody."
mtklein@google.comdbd41c82013-09-13 20:11:09 +0000599 logger.logError(SkStringPrintf(
djsollen@google.com70de4da2013-10-10 17:33:39 +0000600 "\nCan't get %s %s to converge in %dms (%d loops)",
601 bench->getName(), config.name, FLAGS_maxMs, loopsPerIter));
mtklein@google.comc2897432013-09-10 19:23:38 +0000602 break;
603 }
mtklein@google.comc2897432013-09-10 19:23:38 +0000604
605 if ((benchMode == kRecord_BenchMode || benchMode == kPictureRecord_BenchMode)) {
606 // Clear the recorded commands so that they do not accumulate.
commit-bot@chromium.org0f531222014-02-04 19:32:37 +0000607 canvas.reset(SkRef(recordTo.beginRecording(dim.fX, dim.fY, kRecordFlags)));
junov@chromium.orgfb103892012-09-20 19:35:43 +0000608 }
robertphillips@google.com91ee3a12012-08-28 12:18:40 +0000609
mtklein@google.comc2897432013-09-10 19:23:38 +0000610 timer.start();
djsollen@google.com70de4da2013-10-10 17:33:39 +0000611 // Inner loop that allows us to break the run into smaller
612 // chunks (e.g. frames). This is especially useful for the GPU
613 // as we can flush and/or swap buffers to keep the GPU from
614 // queuing up too much work.
615 for (int loopCount = loopsPerIter; loopCount > 0; ) {
commit-bot@chromium.org28871192013-10-14 15:28:01 +0000616 // Save and restore around each call to draw() to guarantee a pristine canvas.
617 SkAutoCanvasRestore saveRestore(canvas, true/*also save*/);
618
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000619 int loops;
djsollen@google.com70de4da2013-10-10 17:33:39 +0000620 if (frameIntervalComputed && loopCount > loopsPerFrame) {
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000621 loops = loopsPerFrame;
djsollen@google.com70de4da2013-10-10 17:33:39 +0000622 loopCount -= loopsPerFrame;
623 } else {
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000624 loops = loopCount;
djsollen@google.com70de4da2013-10-10 17:33:39 +0000625 loopCount = 0;
626 }
627
628 if (benchMode == kPictureRecord_BenchMode) {
629 recordFrom.draw(canvas);
630 } else {
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000631 bench->draw(loops, canvas);
djsollen@google.com70de4da2013-10-10 17:33:39 +0000632 }
633
634 if (kDeferredSilent_BenchMode == benchMode) {
635 static_cast<SkDeferredCanvas*>(canvas.get())->silentFlush();
636 } else if (NULL != canvas) {
637 canvas->flush();
638 }
639
640#if SK_SUPPORT_GPU
641 // swap drawing buffers on each frame to prevent the GPU
642 // from queuing up too much work
643 if (NULL != glContext) {
644 glContext->swapBuffers();
645 }
646#endif
mtklein@google.comc2897432013-09-10 19:23:38 +0000647 }
648
mtklein@google.comc2897432013-09-10 19:23:38 +0000649
650
651 // Stop truncated timers before GL calls complete, and stop the full timers after.
652 timer.truncatedEnd();
653#if SK_SUPPORT_GPU
654 if (NULL != glContext) {
655 context->flush();
656 SK_GL(*glContext, Finish());
657 }
658#endif
659 timer.end();
djsollen@google.com70de4da2013-10-10 17:33:39 +0000660
djsollen@google.comdcfed6c2013-10-10 18:48:27 +0000661 // setup the frame interval for subsequent iterations
662 if (!frameIntervalComputed) {
djsollen@google.com70de4da2013-10-10 17:33:39 +0000663 frameIntervalTime += timer.fWall;
664 frameIntervalTotalLoops += loopsPerIter;
665 if (frameIntervalTime >= FLAGS_minMs) {
666 frameIntervalComputed = true;
667 loopsPerFrame =
djsollen@google.com4e1d4b32013-10-10 17:50:52 +0000668 (int)(((double)frameIntervalTotalLoops / frameIntervalTime) * FLAGS_minMs);
djsollen@google.com70de4da2013-10-10 17:33:39 +0000669 if (loopsPerFrame < 1) {
670 loopsPerFrame = 1;
671 }
672// SkDebugf(" %s has %d loops in %f ms (normalized to %d)\n",
673// bench->getName(), frameIntervalTotalLoops,
674// timer.fWall, loopsPerFrame);
675 }
676 }
djsollen@google.comdcfed6c2013-10-10 18:48:27 +0000677
djsollen@google.com70de4da2013-10-10 17:33:39 +0000678 const double current = timer.fWall / loopsPerIter;
mtklein@google.comdbd41c82013-09-13 20:11:09 +0000679 if (FLAGS_verbose && current > previous) { SkDebugf("↑"); }
680 if (FLAGS_verbose) { SkDebugf("%.3g ", current); }
681 converged = HasConverged(previous, current, timer.fWall);
682 previous = current;
683 } while (!kIsDebug && !converged);
684 if (FLAGS_verbose) { SkDebugf("\n"); }
mtklein@google.comc2897432013-09-10 19:23:38 +0000685
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000686 if (FLAGS_outDir.count() && SkBenchmark::kNonRendering_Backend != config.backend) {
reed@google.com1bc6c6a2014-02-04 14:02:44 +0000687 SkAutoTUnref<SkImage> image(surface->newImageSnapshot());
688 if (image.get()) {
689 saveFile(bench->getName(), config.name, FLAGS_outDir[0],
690 image);
691 }
mtklein@google.comc2897432013-09-10 19:23:38 +0000692 }
693
694 if (kIsDebug) {
695 // Let's not mislead ourselves by looking at Debug build bench times!
696 continue;
697 }
698
699 // Normalize to ms per 1000 iterations.
djsollen@google.com70de4da2013-10-10 17:33:39 +0000700 const double normalize = 1000.0 / loopsPerIter;
mtklein@google.comc2897432013-09-10 19:23:38 +0000701 const struct { char shortName; const char* longName; double ms; } times[] = {
702 {'w', "msecs", normalize * timer.fWall},
703 {'W', "Wmsecs", normalize * timer.fTruncatedWall},
704 {'c', "cmsecs", normalize * timer.fCpu},
705 {'C', "Cmsecs", normalize * timer.fTruncatedCpu},
706 {'g', "gmsecs", normalize * timer.fGpu},
707 };
708
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +0000709 writer.config(config.name);
mtklein@google.comc2897432013-09-10 19:23:38 +0000710 for (size_t i = 0; i < SK_ARRAY_COUNT(times); i++) {
711 if (strchr(FLAGS_timers[0], times[i].shortName) && times[i].ms > 0) {
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +0000712 writer.timer(times[i].longName, times[i].ms);
commit-bot@chromium.org7495f592013-06-03 19:31:07 +0000713 }
reed@google.com25df8882011-07-14 19:03:58 +0000714 }
bsalomon@google.com604a56a2013-03-15 15:42:15 +0000715 }
reed@android.combd700c32009-01-05 03:34:50 +0000716 }
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000717#if SK_SUPPORT_GPU
bsalomon@google.comcb265352013-02-22 16:13:16 +0000718 gContextFactory.destroyContexts();
robertphillips@google.com9d594202012-09-13 14:05:00 +0000719#endif
reed@android.combd700c32009-01-05 03:34:50 +0000720 return 0;
721}
caryclark@google.com5987f582012-10-02 18:33:14 +0000722
borenet@google.com7158e6a2012-11-01 17:43:44 +0000723#if !defined(SK_BUILD_FOR_IOS) && !defined(SK_BUILD_FOR_NACL)
caryclark@google.com5987f582012-10-02 18:33:14 +0000724int main(int argc, char * const argv[]) {
725 return tool_main(argc, (char**) argv);
726}
727#endif