blob: 9cdaa68ebc27854323e1b39b4a6e192c57e1534f [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
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +000081class AutoPrePostDraw {
82public:
83 AutoPrePostDraw(SkBenchmark* bench) : fBench(bench) {
84 fBench->preDraw();
85 }
86 ~AutoPrePostDraw() {
87 fBench->postDraw();
88 }
89private:
90 SkBenchmark* fBench;
91};
92
reed@android.combd700c32009-01-05 03:34:50 +000093static void make_filename(const char name[], SkString* path) {
94 path->set(name);
95 for (int i = 0; name[i]; i++) {
96 switch (name[i]) {
97 case '/':
98 case '\\':
99 case ' ':
100 case ':':
101 path->writable_str()[i] = '-';
102 break;
103 default:
104 break;
105 }
106 }
107}
108
reed@android.com4c7d3d62009-01-21 03:15:13 +0000109static void saveFile(const char name[], const char config[], const char dir[],
reed@google.com1bc6c6a2014-02-04 14:02:44 +0000110 const SkImage* image) {
111 SkAutoTUnref<SkData> data(image->encode(SkImageEncoder::kPNG_Type, 100));
112 if (NULL == data.get()) {
reed@android.com4c7d3d62009-01-21 03:15:13 +0000113 return;
114 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000115
mtklein@google.comc2897432013-09-10 19:23:38 +0000116 SkString filename;
117 make_filename(name, &filename);
118 filename.appendf("_%s.png", config);
119 SkString path = SkOSPath::SkPathJoin(dir, filename.c_str());
120 ::remove(path.c_str());
reed@google.com1bc6c6a2014-02-04 14:02:44 +0000121
reed@google.comace13542014-02-06 22:00:58 +0000122 SkFILEWStream stream(path.c_str());
reed@google.com1bc6c6a2014-02-04 14:02:44 +0000123 stream.write(data->data(), data->size());
reed@android.com4c7d3d62009-01-21 03:15:13 +0000124}
125
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000126static void perform_clip(SkCanvas* canvas, int w, int h) {
reed@android.com4c7d3d62009-01-21 03:15:13 +0000127 SkRect r;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000128
reed@android.com4c7d3d62009-01-21 03:15:13 +0000129 r.set(SkIntToScalar(10), SkIntToScalar(10),
130 SkIntToScalar(w*2/3), SkIntToScalar(h*2/3));
131 canvas->clipRect(r, SkRegion::kIntersect_Op);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000132
reed@android.com4c7d3d62009-01-21 03:15:13 +0000133 r.set(SkIntToScalar(w/3), SkIntToScalar(h/3),
134 SkIntToScalar(w-10), SkIntToScalar(h-10));
135 canvas->clipRect(r, SkRegion::kXOR_Op);
136}
137
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000138static void perform_rotate(SkCanvas* canvas, int w, int h) {
reed@android.com4c7d3d62009-01-21 03:15:13 +0000139 const SkScalar x = SkIntToScalar(w) / 2;
140 const SkScalar y = SkIntToScalar(h) / 2;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000141
reed@android.com4c7d3d62009-01-21 03:15:13 +0000142 canvas->translate(x, y);
143 canvas->rotate(SkIntToScalar(35));
144 canvas->translate(-x, -y);
145}
146
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000147static void perform_scale(SkCanvas* canvas, int w, int h) {
reed@android.com387359e2009-08-04 01:51:09 +0000148 const SkScalar x = SkIntToScalar(w) / 2;
149 const SkScalar y = SkIntToScalar(h) / 2;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000150
reed@android.com387359e2009-08-04 01:51:09 +0000151 canvas->translate(x, y);
152 // just enough so we can't take the sprite case
153 canvas->scale(SK_Scalar1 * 99/100, SK_Scalar1 * 99/100);
154 canvas->translate(-x, -y);
155}
156
reed@google.com1bc6c6a2014-02-04 14:02:44 +0000157static SkSurface* make_surface(SkColorType colorType, const SkIPoint& size,
158 SkBenchmark::Backend backend, int sampleCount,
159 GrContext* context) {
160 SkSurface* surface = NULL;
161 SkImageInfo info = SkImageInfo::Make(size.fX, size.fY, colorType,
162 kPremul_SkAlphaType);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000163
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000164 switch (backend) {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000165 case SkBenchmark::kRaster_Backend:
reed@google.com1bc6c6a2014-02-04 14:02:44 +0000166 surface = SkSurface::NewRaster(info);
167 surface->getCanvas()->clear(SK_ColorWHITE);
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000168 break;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000169#if SK_SUPPORT_GPU
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000170 case SkBenchmark::kGPU_Backend: {
reed@google.com1bc6c6a2014-02-04 14:02:44 +0000171 surface = SkSurface::NewRenderTarget(context, info, sampleCount);
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000172 break;
bsalomon@google.comcb265352013-02-22 16:13:16 +0000173 }
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000174#endif
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000175 case SkBenchmark::kPDF_Backend:
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000176 default:
mtklein@google.com330313a2013-08-22 15:37:26 +0000177 SkDEBUGFAIL("unsupported");
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000178 }
reed@google.com1bc6c6a2014-02-04 14:02:44 +0000179 return surface;
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000180}
181
bsalomon@google.comcb265352013-02-22 16:13:16 +0000182#if SK_SUPPORT_GPU
183GrContextFactory gContextFactory;
184typedef GrContextFactory::GLContextType GLContextType;
mtklein@google.comc2897432013-09-10 19:23:38 +0000185static const GLContextType kNative = GrContextFactory::kNative_GLContextType;
commit-bot@chromium.orge8989572014-01-26 20:51:00 +0000186static const GLContextType kNVPR = GrContextFactory::kNVPR_GLContextType;
mtklein@google.comc2897432013-09-10 19:23:38 +0000187#if SK_ANGLE
188static const GLContextType kANGLE = GrContextFactory::kANGLE_GLContextType;
mtklein@google.comc2897432013-09-10 19:23:38 +0000189#endif
190static const GLContextType kDebug = GrContextFactory::kDebug_GLContextType;
191static const GLContextType kNull = GrContextFactory::kNull_GLContextType;
bsalomon@google.comcb265352013-02-22 16:13:16 +0000192#else
193typedef int GLContextType;
mtklein@google.comc2897432013-09-10 19:23:38 +0000194static const GLContextType kNative = 0, kANGLE = 0, kDebug = 0, kNull = 0;
bsalomon@google.comcb265352013-02-22 16:13:16 +0000195#endif
196
mtklein@google.comc2897432013-09-10 19:23:38 +0000197#ifdef SK_DEBUG
198static const bool kIsDebug = true;
199#else
200static const bool kIsDebug = false;
201#endif
202
203static const struct Config {
reed@google.com1bc6c6a2014-02-04 14:02:44 +0000204 SkColorType fColorType;
mtklein@google.comc2897432013-09-10 19:23:38 +0000205 const char* name;
206 int sampleCount;
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000207 SkBenchmark::Backend backend;
mtklein@google.comc2897432013-09-10 19:23:38 +0000208 GLContextType contextType;
209 bool runByDefault;
reed@android.com4bc19832009-01-19 20:08:35 +0000210} gConfigs[] = {
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000211 { kN32_SkColorType, "NONRENDERING", 0, SkBenchmark::kNonRendering_Backend, kNative, true},
212 { kN32_SkColorType, "8888", 0, SkBenchmark::kRaster_Backend, kNative, true},
reed@google.com1bc6c6a2014-02-04 14:02:44 +0000213 { kRGB_565_SkColorType, "565", 0, SkBenchmark::kRaster_Backend, kNative, true},
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000214#if SK_SUPPORT_GPU
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000215 { kN32_SkColorType, "GPU", 0, SkBenchmark::kGPU_Backend, kNative, true},
216 { kN32_SkColorType, "MSAA4", 4, SkBenchmark::kGPU_Backend, kNative, false},
217 { kN32_SkColorType, "MSAA16", 16, SkBenchmark::kGPU_Backend, kNative, false},
218 { kN32_SkColorType, "NVPRMSAA4", 4, SkBenchmark::kGPU_Backend, kNVPR, true},
219 { kN32_SkColorType, "NVPRMSAA16", 16, SkBenchmark::kGPU_Backend, kNVPR, false},
robertphillips@google.comd3b9fbb2012-03-28 16:19:11 +0000220#if SK_ANGLE
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000221 { kN32_SkColorType, "ANGLE", 0, SkBenchmark::kGPU_Backend, kANGLE, true},
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000222#endif // SK_ANGLE
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000223 { kN32_SkColorType, "Debug", 0, SkBenchmark::kGPU_Backend, kDebug, kIsDebug},
224 { kN32_SkColorType, "NULLGPU", 0, SkBenchmark::kGPU_Backend, kNull, true},
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000225#endif // SK_SUPPORT_GPU
reed@android.com4bc19832009-01-19 20:08:35 +0000226};
227
mtklein@google.comc2897432013-09-10 19:23:38 +0000228DEFINE_string(outDir, "", "If given, image of each bench will be put in outDir.");
229DEFINE_string(timers, "cg", "Timers to display. "
230 "Options: w(all) W(all, truncated) c(pu) C(pu, truncated) g(pu)");
reed@android.com4c7d3d62009-01-21 03:15:13 +0000231
mtklein@google.comc2897432013-09-10 19:23:38 +0000232DEFINE_bool(rotate, false, "Rotate canvas before bench run?");
233DEFINE_bool(scale, false, "Scale canvas before bench run?");
234DEFINE_bool(clip, false, "Clip canvas before bench run?");
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000235
mtklein@google.comc2897432013-09-10 19:23:38 +0000236DEFINE_bool(forceAA, true, "Force anti-aliasing?");
237DEFINE_bool(forceFilter, false, "Force bitmap filtering?");
238DEFINE_string(forceDither, "default", "Force dithering: true, false, or default?");
239DEFINE_bool(forceBlend, false, "Force alpha blending?");
240
241DEFINE_int32(gpuCacheBytes, -1, "GPU cache size limit in bytes. 0 to disable cache.");
242DEFINE_int32(gpuCacheCount, -1, "GPU cache size limit in object count. 0 to disable cache.");
243
commit-bot@chromium.org6dda8272014-01-23 17:21:19 +0000244DEFINE_bool2(leaks, l, false, "show leaked ref cnt'd objects.");
mtklein@google.comc2897432013-09-10 19:23:38 +0000245DEFINE_string(match, "", "[~][^]substring[$] [...] of test name to run.\n"
246 "Multiple matches may be separated by spaces.\n"
247 "~ causes a matching test to always be skipped\n"
248 "^ requires the start of the test to match\n"
249 "$ requires the end of the test to match\n"
250 "^ and $ requires an exact match\n"
251 "If a test does not match any list entry,\n"
252 "it is skipped unless some list entry starts with ~\n");
253DEFINE_string(mode, "normal",
254 "normal: draw to a normal canvas;\n"
255 "deferred: draw to a deferred canvas;\n"
256 "deferredSilent: deferred with silent playback;\n"
257 "record: draw to an SkPicture;\n"
258 "picturerecord: draw from an SkPicture to an SkPicture.\n");
borenet@google.com4e061d32013-09-16 19:12:19 +0000259DEFINE_string(config, kDefaultsConfigStr,
260 "Run configs given. By default, runs the configs marked \"runByDefault\" in gConfigs.");
mtklein@google.comc2897432013-09-10 19:23:38 +0000261DEFINE_string(logFile, "", "Also write stdout here.");
mtklein@google.comdbd41c82013-09-13 20:11:09 +0000262DEFINE_int32(minMs, 20, "Shortest time we'll allow a benchmark to run.");
263DEFINE_int32(maxMs, 4000, "Longest time we'll allow a benchmark to run.");
264DEFINE_double(error, 0.01,
265 "Ratio of subsequent bench measurements must drop within 1±error to converge.");
mtklein@google.comc2897432013-09-10 19:23:38 +0000266DEFINE_string(timeFormat, "%9.2f", "Format to print results, in milliseconds per 1000 loops.");
mtklein@google.comdbd41c82013-09-13 20:11:09 +0000267DEFINE_bool2(verbose, v, false, "Print more.");
tfarina@chromium.org725a64c2013-12-31 14:29:52 +0000268DEFINE_string2(resourcePath, i, "resources", "directory for test resources.");
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +0000269DEFINE_string(outResultsFile, "", "If given, the results will be written to the file in JSON format.");
mtklein@google.comdbd41c82013-09-13 20:11:09 +0000270
commit-bot@chromium.org56b7a6d2014-03-13 16:22:00 +0000271DEFINE_bool(dryRun, false, "Don't actually run the tests, just print what would have been done.");
272
mtklein@google.comdbd41c82013-09-13 20:11:09 +0000273// Has this bench converged? First arguments are milliseconds / loop iteration,
274// last is overall runtime in milliseconds.
275static bool HasConverged(double prevPerLoop, double currPerLoop, double currRaw) {
276 if (currRaw < FLAGS_minMs) {
277 return false;
278 }
279 const double low = 1 - FLAGS_error, high = 1 + FLAGS_error;
280 const double ratio = currPerLoop / prevPerLoop;
281 return low < ratio && ratio < high;
282}
tomhudson@google.com86bb9b72012-04-03 13:28:57 +0000283
caryclark@google.com5987f582012-10-02 18:33:14 +0000284int tool_main(int argc, char** argv);
285int tool_main(int argc, char** argv) {
commit-bot@chromium.org6dda8272014-01-23 17:21:19 +0000286 SkCommandLineFlags::Parse(argc, argv);
bsalomon@google.com4e230682013-01-15 20:37:04 +0000287#if SK_ENABLE_INST_COUNT
commit-bot@chromium.org6dda8272014-01-23 17:21:19 +0000288 if (FLAGS_leaks) {
289 gPrintInstCount = true;
290 }
bsalomon@google.com65a87cc2012-08-14 13:15:44 +0000291#endif
reed@android.com3a859a02009-01-28 00:56:29 +0000292 SkAutoGraphics ag;
bsalomon@google.com65a87cc2012-08-14 13:15:44 +0000293
mtklein@google.comc2897432013-09-10 19:23:38 +0000294 // First, parse some flags.
scroggo@google.com9a412522012-09-07 15:21:18 +0000295 SkBenchLogger logger;
mtklein@google.comc2897432013-09-10 19:23:38 +0000296 if (FLAGS_logFile.count()) {
297 logger.SetLogFile(FLAGS_logFile[0]);
298 }
scroggo@google.com9a412522012-09-07 15:21:18 +0000299
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +0000300 LoggerResultsWriter logWriter(logger, FLAGS_timeFormat[0]);
301 MultiResultsWriter writer;
302 writer.add(&logWriter);
303 SkAutoTDelete<JSONResultsWriter> jsonWriter;
304 if (FLAGS_outResultsFile.count()) {
305 jsonWriter.reset(SkNEW(JSONResultsWriter(FLAGS_outResultsFile[0])));
306 writer.add(jsonWriter.get());
307 }
308 // Instantiate after all the writers have been added to writer so that we
309 // call close() before their destructors are called on the way out.
310 CallEnd<MultiResultsWriter> ender(writer);
311
mtklein@google.comc2897432013-09-10 19:23:38 +0000312 const uint8_t alpha = FLAGS_forceBlend ? 0x80 : 0xFF;
313 SkTriState::State dither = SkTriState::kDefault;
314 for (size_t i = 0; i < 3; i++) {
315 if (strcmp(SkTriState::Name[i], FLAGS_forceDither[0]) == 0) {
316 dither = static_cast<SkTriState::State>(i);
reed@android.comb398fe82009-01-07 11:47:57 +0000317 }
318 }
mtklein@google.comc2897432013-09-10 19:23:38 +0000319
320 BenchMode benchMode = kNormal_BenchMode;
321 for (size_t i = 0; i < SK_ARRAY_COUNT(BenchMode_Name); i++) {
322 if (strcmp(FLAGS_mode[0], BenchMode_Name[i]) == 0) {
323 benchMode = static_cast<BenchMode>(i);
324 }
keyar@chromium.orgfdd909c2012-07-20 23:03:42 +0000325 }
mtklein@google.comc2897432013-09-10 19:23:38 +0000326
327 SkTDArray<int> configs;
borenet@google.com4e061d32013-09-16 19:12:19 +0000328 bool runDefaultConfigs = false;
mtklein@google.comc2897432013-09-10 19:23:38 +0000329 // Try user-given configs first.
330 for (int i = 0; i < FLAGS_config.count(); i++) {
robertphillips@google.come9cd27d2013-10-16 17:48:11 +0000331 for (int j = 0; j < static_cast<int>(SK_ARRAY_COUNT(gConfigs)); ++j) {
mtklein@google.comc2897432013-09-10 19:23:38 +0000332 if (0 == strcmp(FLAGS_config[i], gConfigs[j].name)) {
333 *configs.append() = j;
borenet@google.com4e061d32013-09-16 19:12:19 +0000334 } else if (0 == strcmp(FLAGS_config[i], kDefaultsConfigStr)) {
335 runDefaultConfigs = true;
mtklein@google.comc2897432013-09-10 19:23:38 +0000336 }
337 }
keyar@chromium.orgfdd909c2012-07-20 23:03:42 +0000338 }
mtklein@google.comc2897432013-09-10 19:23:38 +0000339 // If there weren't any, fill in with defaults.
borenet@google.com4e061d32013-09-16 19:12:19 +0000340 if (runDefaultConfigs) {
robertphillips@google.come9cd27d2013-10-16 17:48:11 +0000341 for (int i = 0; i < static_cast<int>(SK_ARRAY_COUNT(gConfigs)); ++i) {
mtklein@google.comc2897432013-09-10 19:23:38 +0000342 if (gConfigs[i].runByDefault) {
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000343 *configs.append() = i;
344 }
tomhudson@google.com13eaaaa2012-04-16 18:00:40 +0000345 }
346 }
mtklein@google.comc2897432013-09-10 19:23:38 +0000347 // Filter out things we can't run.
348 if (kNormal_BenchMode != benchMode) {
bsalomon@google.com604a56a2013-03-15 15:42:15 +0000349 // Non-rendering configs only run in normal mode
350 for (int i = 0; i < configs.count(); ++i) {
mtklein@google.comc2897432013-09-10 19:23:38 +0000351 const Config& config = gConfigs[configs[i]];
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000352 if (SkBenchmark::kNonRendering_Backend == config.backend) {
bsalomon@google.com604a56a2013-03-15 15:42:15 +0000353 configs.remove(i, 1);
354 --i;
355 }
356 }
357 }
scroggo@google.com111fd112013-09-25 21:42:12 +0000358 // Set the resource path.
359 if (!FLAGS_resourcePath.isEmpty()) {
360 SkBenchmark::SetResourcePath(FLAGS_resourcePath[0]);
361 }
362
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000363#if SK_SUPPORT_GPU
364 for (int i = 0; i < configs.count(); ++i) {
mtklein@google.comc2897432013-09-10 19:23:38 +0000365 const Config& config = gConfigs[configs[i]];
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000366
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000367 if (SkBenchmark::kGPU_Backend == config.backend) {
mtklein@google.comc2897432013-09-10 19:23:38 +0000368 GrContext* context = gContextFactory.get(config.contextType);
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000369 if (NULL == context) {
mtklein@google.comdbd41c82013-09-13 20:11:09 +0000370 logger.logError(SkStringPrintf(
371 "Error creating GrContext for config %s. Config will be skipped.\n",
372 config.name));
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000373 configs.remove(i);
374 --i;
375 continue;
376 }
mtklein@google.comc2897432013-09-10 19:23:38 +0000377 if (config.sampleCount > context->getMaxSampleCount()){
mtklein@google.comdbd41c82013-09-13 20:11:09 +0000378 logger.logError(SkStringPrintf(
379 "Sample count (%d) for config %s is unsupported. Config will be skipped.\n",
380 config.sampleCount, config.name));
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000381 configs.remove(i);
382 --i;
383 continue;
384 }
385 }
386 }
387#endif
388
mtklein@google.comc2897432013-09-10 19:23:38 +0000389 // All flags should be parsed now. Report our settings.
390 if (kIsDebug) {
391 logger.logError("bench was built in Debug mode, so we're going to hide the times."
392 " It's for your own good!\n");
393 }
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +0000394 writer.option("mode", FLAGS_mode[0]);
395 writer.option("alpha", SkStringPrintf("0x%02X", alpha).c_str());
396 writer.option("antialias", SkStringPrintf("%d", FLAGS_forceAA).c_str());
397 writer.option("filter", SkStringPrintf("%d", FLAGS_forceFilter).c_str());
398 writer.option("dither", SkTriState::Name[dither]);
399
400 writer.option("rotate", SkStringPrintf("%d", FLAGS_rotate).c_str());
401 writer.option("scale", SkStringPrintf("%d", FLAGS_scale).c_str());
402 writer.option("clip", SkStringPrintf("%d", FLAGS_clip).c_str());
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000403
bungeman@google.coma5d48412011-06-15 17:25:46 +0000404#if defined(SK_BUILD_FOR_WIN32)
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +0000405 writer.option("system", "WIN32");
bungeman@google.coma5d48412011-06-15 17:25:46 +0000406#elif defined(SK_BUILD_FOR_MAC)
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +0000407 writer.option("system", "MAC");
bungeman@google.coma5d48412011-06-15 17:25:46 +0000408#elif defined(SK_BUILD_FOR_ANDROID)
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +0000409 writer.option("system", "ANDROID");
bungeman@google.coma5d48412011-06-15 17:25:46 +0000410#elif defined(SK_BUILD_FOR_UNIX)
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +0000411 writer.option("system", "UNIX");
bungeman@google.coma5d48412011-06-15 17:25:46 +0000412#else
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +0000413 writer.option("system", "other");
bungeman@google.coma5d48412011-06-15 17:25:46 +0000414#endif
415
416#if defined(SK_DEBUG)
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +0000417 writer.option("build", "DEBUG");
418#else
419 writer.option("build", "RELEASE");
bungeman@google.coma5d48412011-06-15 17:25:46 +0000420#endif
mtklein@google.comc2897432013-09-10 19:23:38 +0000421
422 // Set texture cache limits if non-default.
bsalomon@google.com5c90e292013-02-22 19:17:13 +0000423 for (size_t i = 0; i < SK_ARRAY_COUNT(gConfigs); ++i) {
bsalomon@google.comcb265352013-02-22 16:13:16 +0000424#if SK_SUPPORT_GPU
mtklein@google.comc2897432013-09-10 19:23:38 +0000425 const Config& config = gConfigs[i];
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000426 if (SkBenchmark::kGPU_Backend != config.backend) {
mtklein@google.comc2897432013-09-10 19:23:38 +0000427 continue;
bsalomon@google.comcb265352013-02-22 16:13:16 +0000428 }
mtklein@google.comc2897432013-09-10 19:23:38 +0000429 GrContext* context = gContextFactory.get(config.contextType);
430 if (NULL == context) {
431 continue;
432 }
433
434 size_t bytes;
435 int count;
436 context->getTextureCacheLimits(&count, &bytes);
437 if (-1 != FLAGS_gpuCacheBytes) {
438 bytes = static_cast<size_t>(FLAGS_gpuCacheBytes);
439 }
440 if (-1 != FLAGS_gpuCacheCount) {
441 count = FLAGS_gpuCacheCount;
442 }
443 context->setTextureCacheLimits(count, bytes);
bsalomon@google.comcb265352013-02-22 16:13:16 +0000444#endif
445 }
bsalomon@google.com74913722011-10-27 20:44:19 +0000446
mtklein@google.comc2897432013-09-10 19:23:38 +0000447 // Run each bench in each configuration it supports and we asked for.
448 Iter iter;
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +0000449 SkBenchmark* bench;
reed@android.combd700c32009-01-05 03:34:50 +0000450 while ((bench = iter.next()) != NULL) {
bsalomon@google.com7fbc6042012-08-13 22:10:05 +0000451 SkAutoTUnref<SkBenchmark> benchUnref(bench);
mtklein@google.comc2897432013-09-10 19:23:38 +0000452 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, bench->getName())) {
reed@android.comb398fe82009-01-07 11:47:57 +0000453 continue;
454 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000455
mtklein@google.comc2897432013-09-10 19:23:38 +0000456 bench->setForceAlpha(alpha);
457 bench->setForceAA(FLAGS_forceAA);
458 bench->setForceFilter(FLAGS_forceFilter);
459 bench->setDither(dither);
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000460 AutoPrePostDraw appd(bench);
461
mtklein@google.comc2897432013-09-10 19:23:38 +0000462 bool loggedBenchName = false;
463 for (int i = 0; i < configs.count(); ++i) {
464 const int configIndex = configs[i];
465 const Config& config = gConfigs[configIndex];
tomhudson@google.com13eaaaa2012-04-16 18:00:40 +0000466
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000467 if (!bench->isSuitableFor(config.backend)) {
mtklein@google.comc2897432013-09-10 19:23:38 +0000468 continue;
bsalomon@google.com604a56a2013-03-15 15:42:15 +0000469 }
470
bsalomon@google.comcb265352013-02-22 16:13:16 +0000471 GrContext* context = NULL;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000472#if SK_SUPPORT_GPU
sugoi@google.com9c55f802013-03-07 20:52:59 +0000473 SkGLContextHelper* glContext = NULL;
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000474 if (SkBenchmark::kGPU_Backend == config.backend) {
mtklein@google.comc2897432013-09-10 19:23:38 +0000475 context = gContextFactory.get(config.contextType);
bsalomon@google.comcb265352013-02-22 16:13:16 +0000476 if (NULL == context) {
477 continue;
478 }
mtklein@google.comc2897432013-09-10 19:23:38 +0000479 glContext = gContextFactory.getGLContext(config.contextType);
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000480 }
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000481#endif
reed@google.com1bc6c6a2014-02-04 14:02:44 +0000482
mtklein@google.comc2897432013-09-10 19:23:38 +0000483 SkAutoTUnref<SkCanvas> canvas;
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000484 SkAutoTUnref<SkPicture> recordFrom;
485 SkPictureRecorder recorderTo;
mtklein@google.comc2897432013-09-10 19:23:38 +0000486 const SkIPoint dim = bench->getSize();
bsalomon@google.com604a56a2013-03-15 15:42:15 +0000487
mtklein@google.comc2897432013-09-10 19:23:38 +0000488 const SkPicture::RecordingFlags kRecordFlags =
489 SkPicture::kUsePathBoundsForClip_RecordingFlag;
490
reed@google.com1bc6c6a2014-02-04 14:02:44 +0000491 SkAutoTUnref<SkSurface> surface;
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000492 if (SkBenchmark::kNonRendering_Backend != config.backend) {
reed@google.com1bc6c6a2014-02-04 14:02:44 +0000493 surface.reset(make_surface(config.fColorType,
494 dim,
495 config.backend,
496 config.sampleCount,
497 context));
498 if (!surface.get()) {
mtklein@google.comdbd41c82013-09-13 20:11:09 +0000499 logger.logError(SkStringPrintf(
500 "Device creation failure for config %s. Will skip.\n", config.name));
mtklein@google.comc2897432013-09-10 19:23:38 +0000501 continue;
keyar@chromium.orgfdd909c2012-07-20 23:03:42 +0000502 }
junov@chromium.orgfb103892012-09-20 19:35:43 +0000503
mtklein@google.comc2897432013-09-10 19:23:38 +0000504 switch(benchMode) {
505 case kDeferredSilent_BenchMode:
506 case kDeferred_BenchMode:
reed@google.com1bc6c6a2014-02-04 14:02:44 +0000507 canvas.reset(SkDeferredCanvas::Create(surface.get()));
mtklein@google.comc2897432013-09-10 19:23:38 +0000508 break;
509 case kRecord_BenchMode:
skia.committer@gmail.com60bd7512014-04-18 03:03:54 +0000510 canvas.reset(SkRef(recorderTo.beginRecording(dim.fX, dim.fY,
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000511 NULL, kRecordFlags)));
mtklein@google.comc2897432013-09-10 19:23:38 +0000512 break;
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000513 case kPictureRecord_BenchMode: {
514 SkPictureRecorder recorderFrom;
skia.committer@gmail.com60bd7512014-04-18 03:03:54 +0000515 bench->draw(1, recorderFrom.beginRecording(dim.fX, dim.fY,
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000516 NULL, kRecordFlags));
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000517 recordFrom.reset(recorderFrom.endRecording());
skia.committer@gmail.com60bd7512014-04-18 03:03:54 +0000518 canvas.reset(SkRef(recorderTo.beginRecording(dim.fX, dim.fY,
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000519 NULL, kRecordFlags)));
mtklein@google.comc2897432013-09-10 19:23:38 +0000520 break;
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000521 }
mtklein@google.comc2897432013-09-10 19:23:38 +0000522 case kNormal_BenchMode:
reed@google.com1bc6c6a2014-02-04 14:02:44 +0000523 canvas.reset(SkRef(surface->getCanvas()));
mtklein@google.comc2897432013-09-10 19:23:38 +0000524 break;
525 default:
526 SkASSERT(false);
527 }
528 }
529
530 if (NULL != canvas) {
531 canvas->clear(SK_ColorWHITE);
skia.committer@gmail.com51997012014-04-14 03:04:57 +0000532 if (FLAGS_clip) {
533 perform_clip(canvas, dim.fX, dim.fY);
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000534 }
skia.committer@gmail.com51997012014-04-14 03:04:57 +0000535 if (FLAGS_scale) {
536 perform_scale(canvas, dim.fX, dim.fY);
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000537 }
skia.committer@gmail.com51997012014-04-14 03:04:57 +0000538 if (FLAGS_rotate) {
539 perform_rotate(canvas, dim.fX, dim.fY);
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000540 }
mtklein@google.comc2897432013-09-10 19:23:38 +0000541 }
542
543 if (!loggedBenchName) {
544 loggedBenchName = true;
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +0000545 writer.bench(bench->getName(), dim.fX, dim.fY);
mtklein@google.comc2897432013-09-10 19:23:38 +0000546 }
547
548#if SK_SUPPORT_GPU
549 SkGLContextHelper* contextHelper = NULL;
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000550 if (SkBenchmark::kGPU_Backend == config.backend) {
mtklein@google.comc2897432013-09-10 19:23:38 +0000551 contextHelper = gContextFactory.getGLContext(config.contextType);
552 }
553 BenchTimer timer(contextHelper);
554#else
555 BenchTimer timer;
556#endif
557
mtklein@google.com9ef1d212013-09-13 20:39:50 +0000558 double previous = std::numeric_limits<double>::infinity();
mtklein@google.comdbd41c82013-09-13 20:11:09 +0000559 bool converged = false;
djsollen@google.com70de4da2013-10-10 17:33:39 +0000560
561 // variables used to compute loopsPerFrame
562 double frameIntervalTime = 0.0f;
563 int frameIntervalTotalLoops = 0;
564
565 bool frameIntervalComputed = false;
566 int loopsPerFrame = 0;
567 int loopsPerIter = 0;
mtklein@google.comdbd41c82013-09-13 20:11:09 +0000568 if (FLAGS_verbose) { SkDebugf("%s %s: ", bench->getName(), config.name); }
commit-bot@chromium.org56b7a6d2014-03-13 16:22:00 +0000569 if (!FLAGS_dryRun) {
570 do {
571 // Ramp up 1 -> 2 -> 4 -> 8 -> 16 -> ... -> ~1 billion.
572 loopsPerIter = (loopsPerIter == 0) ? 1 : loopsPerIter * 2;
573 if (loopsPerIter >= (1<<30) || timer.fWall > FLAGS_maxMs) {
574 // If you find it takes more than a billion loops to get up to 20ms of runtime,
575 // you've got a computer clocked at several THz or have a broken benchmark. ;)
576 // "1B ought to be enough for anybody."
577 logger.logError(SkStringPrintf(
578 "\nCan't get %s %s to converge in %dms (%d loops)",
579 bench->getName(), config.name, FLAGS_maxMs, loopsPerIter));
580 break;
djsollen@google.com70de4da2013-10-10 17:33:39 +0000581 }
582
commit-bot@chromium.org56b7a6d2014-03-13 16:22:00 +0000583 if ((benchMode == kRecord_BenchMode || benchMode == kPictureRecord_BenchMode)) {
584 // Clear the recorded commands so that they do not accumulate.
skia.committer@gmail.com60bd7512014-04-18 03:03:54 +0000585 canvas.reset(SkRef(recorderTo.beginRecording(dim.fX, dim.fY,
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000586 NULL, kRecordFlags)));
djsollen@google.com70de4da2013-10-10 17:33:39 +0000587 }
588
commit-bot@chromium.org56b7a6d2014-03-13 16:22:00 +0000589 timer.start();
590 // Inner loop that allows us to break the run into smaller
591 // chunks (e.g. frames). This is especially useful for the GPU
592 // as we can flush and/or swap buffers to keep the GPU from
593 // queuing up too much work.
594 for (int loopCount = loopsPerIter; loopCount > 0; ) {
595 // Save and restore around each call to draw() to guarantee a pristine canvas.
596 SkAutoCanvasRestore saveRestore(canvas, true/*also save*/);
djsollen@google.com70de4da2013-10-10 17:33:39 +0000597
commit-bot@chromium.org56b7a6d2014-03-13 16:22:00 +0000598 int loops;
599 if (frameIntervalComputed && loopCount > loopsPerFrame) {
600 loops = loopsPerFrame;
601 loopCount -= loopsPerFrame;
602 } else {
603 loops = loopCount;
604 loopCount = 0;
djsollen@google.com70de4da2013-10-10 17:33:39 +0000605 }
djsollen@google.comdcfed6c2013-10-10 18:48:27 +0000606
commit-bot@chromium.org56b7a6d2014-03-13 16:22:00 +0000607 if (benchMode == kPictureRecord_BenchMode) {
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000608 recordFrom->draw(canvas);
commit-bot@chromium.org56b7a6d2014-03-13 16:22:00 +0000609 } else {
610 bench->draw(loops, canvas);
611 }
612
613 if (kDeferredSilent_BenchMode == benchMode) {
614 static_cast<SkDeferredCanvas*>(canvas.get())->silentFlush();
615 } else if (NULL != canvas) {
616 canvas->flush();
617 }
618
619 #if SK_SUPPORT_GPU
620 // swap drawing buffers on each frame to prevent the GPU
621 // from queuing up too much work
622 if (NULL != glContext) {
623 glContext->swapBuffers();
624 }
625 #endif
626 }
627
628
629
630 // Stop truncated timers before GL calls complete, and stop the full timers after.
631 timer.truncatedEnd();
632 #if SK_SUPPORT_GPU
633 if (NULL != glContext) {
634 context->flush();
635 SK_GL(*glContext, Finish());
636 }
637 #endif
638 timer.end();
639
640 // setup the frame interval for subsequent iterations
641 if (!frameIntervalComputed) {
642 frameIntervalTime += timer.fWall;
643 frameIntervalTotalLoops += loopsPerIter;
644 if (frameIntervalTime >= FLAGS_minMs) {
645 frameIntervalComputed = true;
646 loopsPerFrame =
647 (int)(((double)frameIntervalTotalLoops / frameIntervalTime) * FLAGS_minMs);
648 if (loopsPerFrame < 1) {
649 loopsPerFrame = 1;
650 }
651 // SkDebugf(" %s has %d loops in %f ms (normalized to %d)\n",
652 // bench->getName(), frameIntervalTotalLoops,
653 // timer.fWall, loopsPerFrame);
654 }
655 }
656
657 const double current = timer.fWall / loopsPerIter;
658 if (FLAGS_verbose && current > previous) { SkDebugf("↑"); }
659 if (FLAGS_verbose) { SkDebugf("%.3g ", current); }
660 converged = HasConverged(previous, current, timer.fWall);
661 previous = current;
662 } while (!kIsDebug && !converged);
663 }
mtklein@google.comdbd41c82013-09-13 20:11:09 +0000664 if (FLAGS_verbose) { SkDebugf("\n"); }
mtklein@google.comc2897432013-09-10 19:23:38 +0000665
commit-bot@chromium.org56b7a6d2014-03-13 16:22:00 +0000666 if (!FLAGS_dryRun && FLAGS_outDir.count() && SkBenchmark::kNonRendering_Backend != config.backend) {
reed@google.com1bc6c6a2014-02-04 14:02:44 +0000667 SkAutoTUnref<SkImage> image(surface->newImageSnapshot());
668 if (image.get()) {
669 saveFile(bench->getName(), config.name, FLAGS_outDir[0],
670 image);
671 }
mtklein@google.comc2897432013-09-10 19:23:38 +0000672 }
673
674 if (kIsDebug) {
675 // Let's not mislead ourselves by looking at Debug build bench times!
676 continue;
677 }
678
679 // Normalize to ms per 1000 iterations.
djsollen@google.com70de4da2013-10-10 17:33:39 +0000680 const double normalize = 1000.0 / loopsPerIter;
mtklein@google.comc2897432013-09-10 19:23:38 +0000681 const struct { char shortName; const char* longName; double ms; } times[] = {
682 {'w', "msecs", normalize * timer.fWall},
683 {'W', "Wmsecs", normalize * timer.fTruncatedWall},
684 {'c', "cmsecs", normalize * timer.fCpu},
685 {'C', "Cmsecs", normalize * timer.fTruncatedCpu},
686 {'g', "gmsecs", normalize * timer.fGpu},
687 };
688
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +0000689 writer.config(config.name);
mtklein@google.comc2897432013-09-10 19:23:38 +0000690 for (size_t i = 0; i < SK_ARRAY_COUNT(times); i++) {
691 if (strchr(FLAGS_timers[0], times[i].shortName) && times[i].ms > 0) {
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +0000692 writer.timer(times[i].longName, times[i].ms);
commit-bot@chromium.org7495f592013-06-03 19:31:07 +0000693 }
reed@google.com25df8882011-07-14 19:03:58 +0000694 }
bsalomon@google.com604a56a2013-03-15 15:42:15 +0000695 }
reed@android.combd700c32009-01-05 03:34:50 +0000696 }
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000697#if SK_SUPPORT_GPU
bsalomon@google.comcb265352013-02-22 16:13:16 +0000698 gContextFactory.destroyContexts();
robertphillips@google.com9d594202012-09-13 14:05:00 +0000699#endif
reed@android.combd700c32009-01-05 03:34:50 +0000700 return 0;
701}
caryclark@google.com5987f582012-10-02 18:33:14 +0000702
borenet@google.com7158e6a2012-11-01 17:43:44 +0000703#if !defined(SK_BUILD_FOR_IOS) && !defined(SK_BUILD_FOR_NACL)
caryclark@google.com5987f582012-10-02 18:33:14 +0000704int main(int argc, char * const argv[]) {
705 return tool_main(argc, (char**) argv);
706}
707#endif