blob: 695ac7eba6736a335c61d62d960a84dee0b5261a [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"
bsalomon@google.com82a7bfc2012-04-16 19:11:17 +000016#include "SkDeferredCanvas.h"
reed@android.com3a859a02009-01-28 00:56:29 +000017#include "SkGraphics.h"
reed@android.comb398fe82009-01-07 11:47:57 +000018#include "SkImageEncoder.h"
mtklein@google.comc2897432013-09-10 19:23:38 +000019#include "SkOSFile.h"
reed@android.com6c924ad2009-03-31 03:48:49 +000020#include "SkPicture.h"
reed@android.combd700c32009-01-05 03:34:50 +000021#include "SkString.h"
reed@android.com29348cb2009-08-04 18:17:15 +000022
djsollen@google.comdb490e92013-11-20 13:15:40 +000023#if SK_SUPPORT_GPU
24#include "GrContext.h"
25#include "GrContextFactory.h"
26#include "GrRenderTarget.h"
27#include "SkGpuDevice.h"
28#include "gl/GrGLDefines.h"
29#else
30class GrContext;
31#endif // SK_SUPPORT_GPU
32
mtklein@google.com9ef1d212013-09-13 20:39:50 +000033#include <limits>
34
mtklein@google.comc2897432013-09-10 19:23:38 +000035enum BenchMode {
36 kNormal_BenchMode,
37 kDeferred_BenchMode,
38 kDeferredSilent_BenchMode,
39 kRecord_BenchMode,
40 kPictureRecord_BenchMode
keyar@chromium.orgfdd909c2012-07-20 23:03:42 +000041};
mtklein@google.comdbd41c82013-09-13 20:11:09 +000042const char* BenchMode_Name[] = {
43 "normal", "deferred", "deferredSilent", "record", "picturerecord"
44};
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +000045
borenet@google.com4e061d32013-09-16 19:12:19 +000046static const char kDefaultsConfigStr[] = "defaults";
47
keyar@chromium.orgfdd909c2012-07-20 23:03:42 +000048///////////////////////////////////////////////////////////////////////////////
49
reed@android.com6c924ad2009-03-31 03:48:49 +000050static void erase(SkBitmap& bm) {
51 if (bm.config() == SkBitmap::kA8_Config) {
junov@google.comdbfac8a2012-12-06 21:47:40 +000052 bm.eraseColor(SK_ColorTRANSPARENT);
reed@android.com6c924ad2009-03-31 03:48:49 +000053 } else {
54 bm.eraseColor(SK_ColorWHITE);
55 }
56}
57
reed@android.combd700c32009-01-05 03:34:50 +000058class Iter {
59public:
mtklein@google.comc2897432013-09-10 19:23:38 +000060 Iter() : fBench(BenchRegistry::Head()) {}
rmistry@google.comfbfcd562012-08-23 18:09:54 +000061
reed@android.combd700c32009-01-05 03:34:50 +000062 SkBenchmark* next() {
63 if (fBench) {
64 BenchRegistry::Factory f = fBench->factory();
65 fBench = fBench->next();
mtklein@google.com410e6e82013-09-13 19:52:27 +000066 return f();
reed@android.combd700c32009-01-05 03:34:50 +000067 }
68 return NULL;
69 }
bungeman@google.comd1a416a2011-05-18 18:37:07 +000070
reed@android.combd700c32009-01-05 03:34:50 +000071private:
72 const BenchRegistry* fBench;
73};
74
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +000075class AutoPrePostDraw {
76public:
77 AutoPrePostDraw(SkBenchmark* bench) : fBench(bench) {
78 fBench->preDraw();
79 }
80 ~AutoPrePostDraw() {
81 fBench->postDraw();
82 }
83private:
84 SkBenchmark* fBench;
85};
86
reed@android.combd700c32009-01-05 03:34:50 +000087static void make_filename(const char name[], SkString* path) {
88 path->set(name);
89 for (int i = 0; name[i]; i++) {
90 switch (name[i]) {
91 case '/':
92 case '\\':
93 case ' ':
94 case ':':
95 path->writable_str()[i] = '-';
96 break;
97 default:
98 break;
99 }
100 }
101}
102
reed@android.com4c7d3d62009-01-21 03:15:13 +0000103static void saveFile(const char name[], const char config[], const char dir[],
104 const SkBitmap& bm) {
reed@android.com4c7d3d62009-01-21 03:15:13 +0000105 SkBitmap copy;
106 if (!bm.copyTo(&copy, SkBitmap::kARGB_8888_Config)) {
107 return;
108 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000109
reed@android.comf523e252009-01-26 23:15:37 +0000110 if (bm.config() == SkBitmap::kA8_Config) {
111 // turn alpha into gray-scale
112 size_t size = copy.getSize() >> 2;
113 SkPMColor* p = copy.getAddr32(0, 0);
114 for (size_t i = 0; i < size; i++) {
115 int c = (*p >> SK_A32_SHIFT) & 0xFF;
116 c = 255 - c;
117 c |= (c << 24) | (c << 16) | (c << 8);
118 *p++ = c | (SK_A32_MASK << SK_A32_SHIFT);
119 }
120 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000121
mtklein@google.comc2897432013-09-10 19:23:38 +0000122 SkString filename;
123 make_filename(name, &filename);
124 filename.appendf("_%s.png", config);
125 SkString path = SkOSPath::SkPathJoin(dir, filename.c_str());
126 ::remove(path.c_str());
127 SkImageEncoder::EncodeFile(path.c_str(), copy, SkImageEncoder::kPNG_Type, 100);
reed@android.com4c7d3d62009-01-21 03:15:13 +0000128}
129
130static void performClip(SkCanvas* canvas, int w, int h) {
131 SkRect r;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000132
reed@android.com4c7d3d62009-01-21 03:15:13 +0000133 r.set(SkIntToScalar(10), SkIntToScalar(10),
134 SkIntToScalar(w*2/3), SkIntToScalar(h*2/3));
135 canvas->clipRect(r, SkRegion::kIntersect_Op);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000136
reed@android.com4c7d3d62009-01-21 03:15:13 +0000137 r.set(SkIntToScalar(w/3), SkIntToScalar(h/3),
138 SkIntToScalar(w-10), SkIntToScalar(h-10));
139 canvas->clipRect(r, SkRegion::kXOR_Op);
140}
141
142static void performRotate(SkCanvas* canvas, int w, int h) {
143 const SkScalar x = SkIntToScalar(w) / 2;
144 const SkScalar y = SkIntToScalar(h) / 2;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000145
reed@android.com4c7d3d62009-01-21 03:15:13 +0000146 canvas->translate(x, y);
147 canvas->rotate(SkIntToScalar(35));
148 canvas->translate(-x, -y);
149}
150
reed@android.com387359e2009-08-04 01:51:09 +0000151static void performScale(SkCanvas* canvas, int w, int h) {
152 const SkScalar x = SkIntToScalar(w) / 2;
153 const SkScalar y = SkIntToScalar(h) / 2;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000154
reed@android.com387359e2009-08-04 01:51:09 +0000155 canvas->translate(x, y);
156 // just enough so we can't take the sprite case
157 canvas->scale(SK_Scalar1 * 99/100, SK_Scalar1 * 99/100);
158 canvas->translate(-x, -y);
159}
160
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000161static SkBaseDevice* make_device(SkBitmap::Config config, const SkIPoint& size,
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000162 SkBenchmark::Backend backend, int sampleCount, GrContext* context) {
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000163 SkBaseDevice* device = NULL;
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000164 SkBitmap bitmap;
165 bitmap.setConfig(config, size.fX, size.fY);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000166
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000167 switch (backend) {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000168 case SkBenchmark::kRaster_Backend:
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000169 bitmap.allocPixels();
170 erase(bitmap);
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000171 device = SkNEW_ARGS(SkBitmapDevice, (bitmap));
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000172 break;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000173#if SK_SUPPORT_GPU
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000174 case SkBenchmark::kGPU_Backend: {
bsalomon@google.comcb265352013-02-22 16:13:16 +0000175 GrTextureDesc desc;
176 desc.fConfig = kSkia8888_GrPixelConfig;
177 desc.fFlags = kRenderTarget_GrTextureFlagBit;
178 desc.fWidth = size.fX;
179 desc.fHeight = size.fY;
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000180 desc.fSampleCnt = sampleCount;
bsalomon@google.comcb265352013-02-22 16:13:16 +0000181 SkAutoTUnref<GrTexture> texture(context->createUncachedTexture(desc, NULL, 0));
182 if (!texture) {
183 return NULL;
184 }
185 device = SkNEW_ARGS(SkGpuDevice, (context, texture.get()));
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000186 break;
bsalomon@google.comcb265352013-02-22 16:13:16 +0000187 }
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000188#endif
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000189 case SkBenchmark::kPDF_Backend:
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000190 default:
mtklein@google.com330313a2013-08-22 15:37:26 +0000191 SkDEBUGFAIL("unsupported");
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000192 }
193 return device;
194}
195
bsalomon@google.comcb265352013-02-22 16:13:16 +0000196#if SK_SUPPORT_GPU
197GrContextFactory gContextFactory;
198typedef GrContextFactory::GLContextType GLContextType;
mtklein@google.comc2897432013-09-10 19:23:38 +0000199static const GLContextType kNative = GrContextFactory::kNative_GLContextType;
200#if SK_ANGLE
201static const GLContextType kANGLE = GrContextFactory::kANGLE_GLContextType;
mtklein@google.comc2897432013-09-10 19:23:38 +0000202#endif
203static const GLContextType kDebug = GrContextFactory::kDebug_GLContextType;
204static const GLContextType kNull = GrContextFactory::kNull_GLContextType;
bsalomon@google.comcb265352013-02-22 16:13:16 +0000205#else
206typedef int GLContextType;
mtklein@google.comc2897432013-09-10 19:23:38 +0000207static const GLContextType kNative = 0, kANGLE = 0, kDebug = 0, kNull = 0;
bsalomon@google.comcb265352013-02-22 16:13:16 +0000208#endif
209
mtklein@google.comc2897432013-09-10 19:23:38 +0000210#ifdef SK_DEBUG
211static const bool kIsDebug = true;
212#else
213static const bool kIsDebug = false;
214#endif
215
216static const struct Config {
217 SkBitmap::Config config;
218 const char* name;
219 int sampleCount;
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000220 SkBenchmark::Backend backend;
mtklein@google.comc2897432013-09-10 19:23:38 +0000221 GLContextType contextType;
222 bool runByDefault;
reed@android.com4bc19832009-01-19 20:08:35 +0000223} gConfigs[] = {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000224 { SkBitmap::kNo_Config, "NONRENDERING", 0, SkBenchmark::kNonRendering_Backend, kNative, true},
225 { SkBitmap::kARGB_8888_Config, "8888", 0, SkBenchmark::kRaster_Backend, kNative, true},
226 { SkBitmap::kRGB_565_Config, "565", 0, SkBenchmark::kRaster_Backend, kNative, true},
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000227#if SK_SUPPORT_GPU
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000228 { SkBitmap::kARGB_8888_Config, "GPU", 0, SkBenchmark::kGPU_Backend, kNative, true},
229 { SkBitmap::kARGB_8888_Config, "MSAA4", 4, SkBenchmark::kGPU_Backend, kNative, false},
230 { SkBitmap::kARGB_8888_Config, "MSAA16", 16, SkBenchmark::kGPU_Backend, kNative, false},
robertphillips@google.comd3b9fbb2012-03-28 16:19:11 +0000231#if SK_ANGLE
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000232 { SkBitmap::kARGB_8888_Config, "ANGLE", 0, SkBenchmark::kGPU_Backend, kANGLE, true},
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000233#endif // SK_ANGLE
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000234 { SkBitmap::kARGB_8888_Config, "Debug", 0, SkBenchmark::kGPU_Backend, kDebug, kIsDebug},
235 { SkBitmap::kARGB_8888_Config, "NULLGPU", 0, SkBenchmark::kGPU_Backend, kNull, true},
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000236#endif // SK_SUPPORT_GPU
reed@android.com4bc19832009-01-19 20:08:35 +0000237};
238
mtklein@google.comc2897432013-09-10 19:23:38 +0000239DEFINE_string(outDir, "", "If given, image of each bench will be put in outDir.");
240DEFINE_string(timers, "cg", "Timers to display. "
241 "Options: w(all) W(all, truncated) c(pu) C(pu, truncated) g(pu)");
reed@android.com4c7d3d62009-01-21 03:15:13 +0000242
mtklein@google.comc2897432013-09-10 19:23:38 +0000243DEFINE_bool(rotate, false, "Rotate canvas before bench run?");
244DEFINE_bool(scale, false, "Scale canvas before bench run?");
245DEFINE_bool(clip, false, "Clip canvas before bench run?");
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000246
mtklein@google.comc2897432013-09-10 19:23:38 +0000247DEFINE_bool(forceAA, true, "Force anti-aliasing?");
248DEFINE_bool(forceFilter, false, "Force bitmap filtering?");
249DEFINE_string(forceDither, "default", "Force dithering: true, false, or default?");
250DEFINE_bool(forceBlend, false, "Force alpha blending?");
251
252DEFINE_int32(gpuCacheBytes, -1, "GPU cache size limit in bytes. 0 to disable cache.");
253DEFINE_int32(gpuCacheCount, -1, "GPU cache size limit in object count. 0 to disable cache.");
254
255DEFINE_string(match, "", "[~][^]substring[$] [...] of test name to run.\n"
256 "Multiple matches may be separated by spaces.\n"
257 "~ causes a matching test to always be skipped\n"
258 "^ requires the start of the test to match\n"
259 "$ requires the end of the test to match\n"
260 "^ and $ requires an exact match\n"
261 "If a test does not match any list entry,\n"
262 "it is skipped unless some list entry starts with ~\n");
263DEFINE_string(mode, "normal",
264 "normal: draw to a normal canvas;\n"
265 "deferred: draw to a deferred canvas;\n"
266 "deferredSilent: deferred with silent playback;\n"
267 "record: draw to an SkPicture;\n"
268 "picturerecord: draw from an SkPicture to an SkPicture.\n");
borenet@google.com4e061d32013-09-16 19:12:19 +0000269DEFINE_string(config, kDefaultsConfigStr,
270 "Run configs given. By default, runs the configs marked \"runByDefault\" in gConfigs.");
mtklein@google.comc2897432013-09-10 19:23:38 +0000271DEFINE_string(logFile, "", "Also write stdout here.");
mtklein@google.comdbd41c82013-09-13 20:11:09 +0000272DEFINE_int32(minMs, 20, "Shortest time we'll allow a benchmark to run.");
273DEFINE_int32(maxMs, 4000, "Longest time we'll allow a benchmark to run.");
274DEFINE_double(error, 0.01,
275 "Ratio of subsequent bench measurements must drop within 1±error to converge.");
mtklein@google.comc2897432013-09-10 19:23:38 +0000276DEFINE_string(timeFormat, "%9.2f", "Format to print results, in milliseconds per 1000 loops.");
mtklein@google.comdbd41c82013-09-13 20:11:09 +0000277DEFINE_bool2(verbose, v, false, "Print more.");
scroggo@google.com111fd112013-09-25 21:42:12 +0000278DEFINE_string2(resourcePath, i, NULL, "directory for test resources.");
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +0000279DEFINE_string(outResultsFile, "", "If given, the results will be written to the file in JSON format.");
mtklein@google.comdbd41c82013-09-13 20:11:09 +0000280
281// Has this bench converged? First arguments are milliseconds / loop iteration,
282// last is overall runtime in milliseconds.
283static bool HasConverged(double prevPerLoop, double currPerLoop, double currRaw) {
284 if (currRaw < FLAGS_minMs) {
285 return false;
286 }
287 const double low = 1 - FLAGS_error, high = 1 + FLAGS_error;
288 const double ratio = currPerLoop / prevPerLoop;
289 return low < ratio && ratio < high;
290}
tomhudson@google.com86bb9b72012-04-03 13:28:57 +0000291
caryclark@google.com5987f582012-10-02 18:33:14 +0000292int tool_main(int argc, char** argv);
293int tool_main(int argc, char** argv) {
bsalomon@google.com4e230682013-01-15 20:37:04 +0000294#if SK_ENABLE_INST_COUNT
bsalomon@google.com65a87cc2012-08-14 13:15:44 +0000295 gPrintInstCount = true;
296#endif
reed@android.com3a859a02009-01-28 00:56:29 +0000297 SkAutoGraphics ag;
mtklein@google.comc2897432013-09-10 19:23:38 +0000298 SkCommandLineFlags::Parse(argc, argv);
bsalomon@google.com65a87cc2012-08-14 13:15:44 +0000299
mtklein@google.comc2897432013-09-10 19:23:38 +0000300 // First, parse some flags.
scroggo@google.com9a412522012-09-07 15:21:18 +0000301 SkBenchLogger logger;
mtklein@google.comc2897432013-09-10 19:23:38 +0000302 if (FLAGS_logFile.count()) {
303 logger.SetLogFile(FLAGS_logFile[0]);
304 }
scroggo@google.com9a412522012-09-07 15:21:18 +0000305
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +0000306 LoggerResultsWriter logWriter(logger, FLAGS_timeFormat[0]);
307 MultiResultsWriter writer;
308 writer.add(&logWriter);
309 SkAutoTDelete<JSONResultsWriter> jsonWriter;
310 if (FLAGS_outResultsFile.count()) {
311 jsonWriter.reset(SkNEW(JSONResultsWriter(FLAGS_outResultsFile[0])));
312 writer.add(jsonWriter.get());
313 }
314 // Instantiate after all the writers have been added to writer so that we
315 // call close() before their destructors are called on the way out.
316 CallEnd<MultiResultsWriter> ender(writer);
317
mtklein@google.comc2897432013-09-10 19:23:38 +0000318 const uint8_t alpha = FLAGS_forceBlend ? 0x80 : 0xFF;
319 SkTriState::State dither = SkTriState::kDefault;
320 for (size_t i = 0; i < 3; i++) {
321 if (strcmp(SkTriState::Name[i], FLAGS_forceDither[0]) == 0) {
322 dither = static_cast<SkTriState::State>(i);
reed@android.comb398fe82009-01-07 11:47:57 +0000323 }
324 }
mtklein@google.comc2897432013-09-10 19:23:38 +0000325
326 BenchMode benchMode = kNormal_BenchMode;
327 for (size_t i = 0; i < SK_ARRAY_COUNT(BenchMode_Name); i++) {
328 if (strcmp(FLAGS_mode[0], BenchMode_Name[i]) == 0) {
329 benchMode = static_cast<BenchMode>(i);
330 }
keyar@chromium.orgfdd909c2012-07-20 23:03:42 +0000331 }
mtklein@google.comc2897432013-09-10 19:23:38 +0000332
333 SkTDArray<int> configs;
borenet@google.com4e061d32013-09-16 19:12:19 +0000334 bool runDefaultConfigs = false;
mtklein@google.comc2897432013-09-10 19:23:38 +0000335 // Try user-given configs first.
336 for (int i = 0; i < FLAGS_config.count(); i++) {
robertphillips@google.come9cd27d2013-10-16 17:48:11 +0000337 for (int j = 0; j < static_cast<int>(SK_ARRAY_COUNT(gConfigs)); ++j) {
mtklein@google.comc2897432013-09-10 19:23:38 +0000338 if (0 == strcmp(FLAGS_config[i], gConfigs[j].name)) {
339 *configs.append() = j;
borenet@google.com4e061d32013-09-16 19:12:19 +0000340 } else if (0 == strcmp(FLAGS_config[i], kDefaultsConfigStr)) {
341 runDefaultConfigs = true;
mtklein@google.comc2897432013-09-10 19:23:38 +0000342 }
343 }
keyar@chromium.orgfdd909c2012-07-20 23:03:42 +0000344 }
mtklein@google.comc2897432013-09-10 19:23:38 +0000345 // If there weren't any, fill in with defaults.
borenet@google.com4e061d32013-09-16 19:12:19 +0000346 if (runDefaultConfigs) {
robertphillips@google.come9cd27d2013-10-16 17:48:11 +0000347 for (int i = 0; i < static_cast<int>(SK_ARRAY_COUNT(gConfigs)); ++i) {
mtklein@google.comc2897432013-09-10 19:23:38 +0000348 if (gConfigs[i].runByDefault) {
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000349 *configs.append() = i;
350 }
tomhudson@google.com13eaaaa2012-04-16 18:00:40 +0000351 }
352 }
mtklein@google.comc2897432013-09-10 19:23:38 +0000353 // Filter out things we can't run.
354 if (kNormal_BenchMode != benchMode) {
bsalomon@google.com604a56a2013-03-15 15:42:15 +0000355 // Non-rendering configs only run in normal mode
356 for (int i = 0; i < configs.count(); ++i) {
mtklein@google.comc2897432013-09-10 19:23:38 +0000357 const Config& config = gConfigs[configs[i]];
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000358 if (SkBenchmark::kNonRendering_Backend == config.backend) {
bsalomon@google.com604a56a2013-03-15 15:42:15 +0000359 configs.remove(i, 1);
360 --i;
361 }
362 }
363 }
scroggo@google.com111fd112013-09-25 21:42:12 +0000364 // Set the resource path.
365 if (!FLAGS_resourcePath.isEmpty()) {
366 SkBenchmark::SetResourcePath(FLAGS_resourcePath[0]);
367 }
368
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000369#if SK_SUPPORT_GPU
370 for (int i = 0; i < configs.count(); ++i) {
mtklein@google.comc2897432013-09-10 19:23:38 +0000371 const Config& config = gConfigs[configs[i]];
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000372
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000373 if (SkBenchmark::kGPU_Backend == config.backend) {
mtklein@google.comc2897432013-09-10 19:23:38 +0000374 GrContext* context = gContextFactory.get(config.contextType);
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000375 if (NULL == context) {
mtklein@google.comdbd41c82013-09-13 20:11:09 +0000376 logger.logError(SkStringPrintf(
377 "Error creating GrContext for config %s. Config will be skipped.\n",
378 config.name));
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000379 configs.remove(i);
380 --i;
381 continue;
382 }
mtklein@google.comc2897432013-09-10 19:23:38 +0000383 if (config.sampleCount > context->getMaxSampleCount()){
mtklein@google.comdbd41c82013-09-13 20:11:09 +0000384 logger.logError(SkStringPrintf(
385 "Sample count (%d) for config %s is unsupported. Config will be skipped.\n",
386 config.sampleCount, config.name));
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000387 configs.remove(i);
388 --i;
389 continue;
390 }
391 }
392 }
393#endif
394
mtklein@google.comc2897432013-09-10 19:23:38 +0000395 // All flags should be parsed now. Report our settings.
396 if (kIsDebug) {
397 logger.logError("bench was built in Debug mode, so we're going to hide the times."
398 " It's for your own good!\n");
399 }
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +0000400 writer.option("mode", FLAGS_mode[0]);
401 writer.option("alpha", SkStringPrintf("0x%02X", alpha).c_str());
402 writer.option("antialias", SkStringPrintf("%d", FLAGS_forceAA).c_str());
403 writer.option("filter", SkStringPrintf("%d", FLAGS_forceFilter).c_str());
404 writer.option("dither", SkTriState::Name[dither]);
405
406 writer.option("rotate", SkStringPrintf("%d", FLAGS_rotate).c_str());
407 writer.option("scale", SkStringPrintf("%d", FLAGS_scale).c_str());
408 writer.option("clip", SkStringPrintf("%d", FLAGS_clip).c_str());
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000409
bungeman@google.coma5d48412011-06-15 17:25:46 +0000410#if defined(SK_BUILD_FOR_WIN32)
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +0000411 writer.option("system", "WIN32");
bungeman@google.coma5d48412011-06-15 17:25:46 +0000412#elif defined(SK_BUILD_FOR_MAC)
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +0000413 writer.option("system", "MAC");
bungeman@google.coma5d48412011-06-15 17:25:46 +0000414#elif defined(SK_BUILD_FOR_ANDROID)
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +0000415 writer.option("system", "ANDROID");
bungeman@google.coma5d48412011-06-15 17:25:46 +0000416#elif defined(SK_BUILD_FOR_UNIX)
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +0000417 writer.option("system", "UNIX");
bungeman@google.coma5d48412011-06-15 17:25:46 +0000418#else
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +0000419 writer.option("system", "other");
bungeman@google.coma5d48412011-06-15 17:25:46 +0000420#endif
421
422#if defined(SK_DEBUG)
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +0000423 writer.option("build", "DEBUG");
424#else
425 writer.option("build", "RELEASE");
bungeman@google.coma5d48412011-06-15 17:25:46 +0000426#endif
mtklein@google.comc2897432013-09-10 19:23:38 +0000427
428 // Set texture cache limits if non-default.
bsalomon@google.com5c90e292013-02-22 19:17:13 +0000429 for (size_t i = 0; i < SK_ARRAY_COUNT(gConfigs); ++i) {
bsalomon@google.comcb265352013-02-22 16:13:16 +0000430#if SK_SUPPORT_GPU
mtklein@google.comc2897432013-09-10 19:23:38 +0000431 const Config& config = gConfigs[i];
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000432 if (SkBenchmark::kGPU_Backend != config.backend) {
mtklein@google.comc2897432013-09-10 19:23:38 +0000433 continue;
bsalomon@google.comcb265352013-02-22 16:13:16 +0000434 }
mtklein@google.comc2897432013-09-10 19:23:38 +0000435 GrContext* context = gContextFactory.get(config.contextType);
436 if (NULL == context) {
437 continue;
438 }
439
440 size_t bytes;
441 int count;
442 context->getTextureCacheLimits(&count, &bytes);
443 if (-1 != FLAGS_gpuCacheBytes) {
444 bytes = static_cast<size_t>(FLAGS_gpuCacheBytes);
445 }
446 if (-1 != FLAGS_gpuCacheCount) {
447 count = FLAGS_gpuCacheCount;
448 }
449 context->setTextureCacheLimits(count, bytes);
bsalomon@google.comcb265352013-02-22 16:13:16 +0000450#endif
451 }
bsalomon@google.com74913722011-10-27 20:44:19 +0000452
mtklein@google.comc2897432013-09-10 19:23:38 +0000453 // Run each bench in each configuration it supports and we asked for.
454 Iter iter;
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +0000455 SkBenchmark* bench;
reed@android.combd700c32009-01-05 03:34:50 +0000456 while ((bench = iter.next()) != NULL) {
bsalomon@google.com7fbc6042012-08-13 22:10:05 +0000457 SkAutoTUnref<SkBenchmark> benchUnref(bench);
mtklein@google.comc2897432013-09-10 19:23:38 +0000458 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, bench->getName())) {
reed@android.comb398fe82009-01-07 11:47:57 +0000459 continue;
460 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000461
mtklein@google.comc2897432013-09-10 19:23:38 +0000462 bench->setForceAlpha(alpha);
463 bench->setForceAA(FLAGS_forceAA);
464 bench->setForceFilter(FLAGS_forceFilter);
465 bench->setDither(dither);
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000466 AutoPrePostDraw appd(bench);
467
mtklein@google.comc2897432013-09-10 19:23:38 +0000468 bool loggedBenchName = false;
469 for (int i = 0; i < configs.count(); ++i) {
470 const int configIndex = configs[i];
471 const Config& config = gConfigs[configIndex];
tomhudson@google.com13eaaaa2012-04-16 18:00:40 +0000472
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000473 if (!bench->isSuitableFor(config.backend)) {
mtklein@google.comc2897432013-09-10 19:23:38 +0000474 continue;
bsalomon@google.com604a56a2013-03-15 15:42:15 +0000475 }
476
bsalomon@google.comcb265352013-02-22 16:13:16 +0000477 GrContext* context = NULL;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000478#if SK_SUPPORT_GPU
sugoi@google.com9c55f802013-03-07 20:52:59 +0000479 SkGLContextHelper* glContext = NULL;
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000480 if (SkBenchmark::kGPU_Backend == config.backend) {
mtklein@google.comc2897432013-09-10 19:23:38 +0000481 context = gContextFactory.get(config.contextType);
bsalomon@google.comcb265352013-02-22 16:13:16 +0000482 if (NULL == context) {
483 continue;
484 }
mtklein@google.comc2897432013-09-10 19:23:38 +0000485 glContext = gContextFactory.getGLContext(config.contextType);
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000486 }
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000487#endif
mtklein@google.comc2897432013-09-10 19:23:38 +0000488 SkAutoTUnref<SkBaseDevice> device;
489 SkAutoTUnref<SkCanvas> canvas;
490 SkPicture recordFrom, recordTo;
491 const SkIPoint dim = bench->getSize();
bsalomon@google.com604a56a2013-03-15 15:42:15 +0000492
mtklein@google.comc2897432013-09-10 19:23:38 +0000493 const SkPicture::RecordingFlags kRecordFlags =
494 SkPicture::kUsePathBoundsForClip_RecordingFlag;
495
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000496 if (SkBenchmark::kNonRendering_Backend != config.backend) {
mtklein@google.comc2897432013-09-10 19:23:38 +0000497 device.reset(make_device(config.config,
498 dim,
499 config.backend,
500 config.sampleCount,
501 context));
502 if (!device.get()) {
mtklein@google.comdbd41c82013-09-13 20:11:09 +0000503 logger.logError(SkStringPrintf(
504 "Device creation failure for config %s. Will skip.\n", config.name));
mtklein@google.comc2897432013-09-10 19:23:38 +0000505 continue;
keyar@chromium.orgfdd909c2012-07-20 23:03:42 +0000506 }
junov@chromium.orgfb103892012-09-20 19:35:43 +0000507
mtklein@google.comc2897432013-09-10 19:23:38 +0000508 switch(benchMode) {
509 case kDeferredSilent_BenchMode:
510 case kDeferred_BenchMode:
511 canvas.reset(SkDeferredCanvas::Create(device.get()));
512 break;
513 case kRecord_BenchMode:
514 canvas.reset(SkRef(recordTo.beginRecording(dim.fX, dim.fY, kRecordFlags)));
515 break;
516 case kPictureRecord_BenchMode:
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000517 bench->draw(1, recordFrom.beginRecording(dim.fX, dim.fY, kRecordFlags));
mtklein@google.comc2897432013-09-10 19:23:38 +0000518 recordFrom.endRecording();
519 canvas.reset(SkRef(recordTo.beginRecording(dim.fX, dim.fY, kRecordFlags)));
520 break;
521 case kNormal_BenchMode:
522 canvas.reset(new SkCanvas(device.get()));
523 break;
524 default:
525 SkASSERT(false);
526 }
527 }
528
529 if (NULL != canvas) {
530 canvas->clear(SK_ColorWHITE);
531 if (FLAGS_clip) { performClip(canvas, dim.fX, dim.fY); }
532 if (FLAGS_scale) { performScale(canvas, dim.fX, dim.fY); }
533 if (FLAGS_rotate) { performRotate(canvas, dim.fX, dim.fY); }
534 }
535
536 if (!loggedBenchName) {
537 loggedBenchName = true;
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +0000538 writer.bench(bench->getName(), dim.fX, dim.fY);
mtklein@google.comc2897432013-09-10 19:23:38 +0000539 }
540
541#if SK_SUPPORT_GPU
542 SkGLContextHelper* contextHelper = NULL;
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000543 if (SkBenchmark::kGPU_Backend == config.backend) {
mtklein@google.comc2897432013-09-10 19:23:38 +0000544 contextHelper = gContextFactory.getGLContext(config.contextType);
545 }
546 BenchTimer timer(contextHelper);
547#else
548 BenchTimer timer;
549#endif
550
mtklein@google.com9ef1d212013-09-13 20:39:50 +0000551 double previous = std::numeric_limits<double>::infinity();
mtklein@google.comdbd41c82013-09-13 20:11:09 +0000552 bool converged = false;
djsollen@google.com70de4da2013-10-10 17:33:39 +0000553
554 // variables used to compute loopsPerFrame
555 double frameIntervalTime = 0.0f;
556 int frameIntervalTotalLoops = 0;
557
558 bool frameIntervalComputed = false;
559 int loopsPerFrame = 0;
560 int loopsPerIter = 0;
mtklein@google.comdbd41c82013-09-13 20:11:09 +0000561 if (FLAGS_verbose) { SkDebugf("%s %s: ", bench->getName(), config.name); }
mtklein@google.comc2897432013-09-10 19:23:38 +0000562 do {
djsollen@google.com70de4da2013-10-10 17:33:39 +0000563 // Ramp up 1 -> 2 -> 4 -> 8 -> 16 -> ... -> ~1 billion.
564 loopsPerIter = (loopsPerIter == 0) ? 1 : loopsPerIter * 2;
565 if (loopsPerIter >= (1<<30) || timer.fWall > FLAGS_maxMs) {
mtklein@google.comc2897432013-09-10 19:23:38 +0000566 // If you find it takes more than a billion loops to get up to 20ms of runtime,
567 // you've got a computer clocked at several THz or have a broken benchmark. ;)
568 // "1B ought to be enough for anybody."
mtklein@google.comdbd41c82013-09-13 20:11:09 +0000569 logger.logError(SkStringPrintf(
djsollen@google.com70de4da2013-10-10 17:33:39 +0000570 "\nCan't get %s %s to converge in %dms (%d loops)",
571 bench->getName(), config.name, FLAGS_maxMs, loopsPerIter));
mtklein@google.comc2897432013-09-10 19:23:38 +0000572 break;
573 }
mtklein@google.comc2897432013-09-10 19:23:38 +0000574
575 if ((benchMode == kRecord_BenchMode || benchMode == kPictureRecord_BenchMode)) {
576 // Clear the recorded commands so that they do not accumulate.
577 canvas.reset(recordTo.beginRecording(dim.fX, dim.fY, kRecordFlags));
junov@chromium.orgfb103892012-09-20 19:35:43 +0000578 }
robertphillips@google.com91ee3a12012-08-28 12:18:40 +0000579
mtklein@google.comc2897432013-09-10 19:23:38 +0000580 timer.start();
djsollen@google.com70de4da2013-10-10 17:33:39 +0000581 // Inner loop that allows us to break the run into smaller
582 // chunks (e.g. frames). This is especially useful for the GPU
583 // as we can flush and/or swap buffers to keep the GPU from
584 // queuing up too much work.
585 for (int loopCount = loopsPerIter; loopCount > 0; ) {
commit-bot@chromium.org28871192013-10-14 15:28:01 +0000586 // Save and restore around each call to draw() to guarantee a pristine canvas.
587 SkAutoCanvasRestore saveRestore(canvas, true/*also save*/);
588
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000589 int loops;
djsollen@google.com70de4da2013-10-10 17:33:39 +0000590 if (frameIntervalComputed && loopCount > loopsPerFrame) {
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000591 loops = loopsPerFrame;
djsollen@google.com70de4da2013-10-10 17:33:39 +0000592 loopCount -= loopsPerFrame;
593 } else {
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000594 loops = loopCount;
djsollen@google.com70de4da2013-10-10 17:33:39 +0000595 loopCount = 0;
596 }
597
598 if (benchMode == kPictureRecord_BenchMode) {
599 recordFrom.draw(canvas);
600 } else {
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000601 bench->draw(loops, canvas);
djsollen@google.com70de4da2013-10-10 17:33:39 +0000602 }
603
604 if (kDeferredSilent_BenchMode == benchMode) {
605 static_cast<SkDeferredCanvas*>(canvas.get())->silentFlush();
606 } else if (NULL != canvas) {
607 canvas->flush();
608 }
609
610#if SK_SUPPORT_GPU
611 // swap drawing buffers on each frame to prevent the GPU
612 // from queuing up too much work
613 if (NULL != glContext) {
614 glContext->swapBuffers();
615 }
616#endif
mtklein@google.comc2897432013-09-10 19:23:38 +0000617 }
618
mtklein@google.comc2897432013-09-10 19:23:38 +0000619
620
621 // Stop truncated timers before GL calls complete, and stop the full timers after.
622 timer.truncatedEnd();
623#if SK_SUPPORT_GPU
624 if (NULL != glContext) {
625 context->flush();
626 SK_GL(*glContext, Finish());
627 }
628#endif
629 timer.end();
djsollen@google.com70de4da2013-10-10 17:33:39 +0000630
djsollen@google.comdcfed6c2013-10-10 18:48:27 +0000631 // setup the frame interval for subsequent iterations
632 if (!frameIntervalComputed) {
djsollen@google.com70de4da2013-10-10 17:33:39 +0000633 frameIntervalTime += timer.fWall;
634 frameIntervalTotalLoops += loopsPerIter;
635 if (frameIntervalTime >= FLAGS_minMs) {
636 frameIntervalComputed = true;
637 loopsPerFrame =
djsollen@google.com4e1d4b32013-10-10 17:50:52 +0000638 (int)(((double)frameIntervalTotalLoops / frameIntervalTime) * FLAGS_minMs);
djsollen@google.com70de4da2013-10-10 17:33:39 +0000639 if (loopsPerFrame < 1) {
640 loopsPerFrame = 1;
641 }
642// SkDebugf(" %s has %d loops in %f ms (normalized to %d)\n",
643// bench->getName(), frameIntervalTotalLoops,
644// timer.fWall, loopsPerFrame);
645 }
646 }
djsollen@google.comdcfed6c2013-10-10 18:48:27 +0000647
djsollen@google.com70de4da2013-10-10 17:33:39 +0000648 const double current = timer.fWall / loopsPerIter;
mtklein@google.comdbd41c82013-09-13 20:11:09 +0000649 if (FLAGS_verbose && current > previous) { SkDebugf("↑"); }
650 if (FLAGS_verbose) { SkDebugf("%.3g ", current); }
651 converged = HasConverged(previous, current, timer.fWall);
652 previous = current;
653 } while (!kIsDebug && !converged);
654 if (FLAGS_verbose) { SkDebugf("\n"); }
mtklein@google.comc2897432013-09-10 19:23:38 +0000655
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000656 if (FLAGS_outDir.count() && SkBenchmark::kNonRendering_Backend != config.backend) {
mtklein@google.comc2897432013-09-10 19:23:38 +0000657 saveFile(bench->getName(),
658 config.name,
659 FLAGS_outDir[0],
660 device->accessBitmap(false));
661 }
662
663 if (kIsDebug) {
664 // Let's not mislead ourselves by looking at Debug build bench times!
665 continue;
666 }
667
668 // Normalize to ms per 1000 iterations.
djsollen@google.com70de4da2013-10-10 17:33:39 +0000669 const double normalize = 1000.0 / loopsPerIter;
mtklein@google.comc2897432013-09-10 19:23:38 +0000670 const struct { char shortName; const char* longName; double ms; } times[] = {
671 {'w', "msecs", normalize * timer.fWall},
672 {'W', "Wmsecs", normalize * timer.fTruncatedWall},
673 {'c', "cmsecs", normalize * timer.fCpu},
674 {'C', "Cmsecs", normalize * timer.fTruncatedCpu},
675 {'g', "gmsecs", normalize * timer.fGpu},
676 };
677
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +0000678 writer.config(config.name);
mtklein@google.comc2897432013-09-10 19:23:38 +0000679 for (size_t i = 0; i < SK_ARRAY_COUNT(times); i++) {
680 if (strchr(FLAGS_timers[0], times[i].shortName) && times[i].ms > 0) {
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +0000681 writer.timer(times[i].longName, times[i].ms);
commit-bot@chromium.org7495f592013-06-03 19:31:07 +0000682 }
reed@google.com25df8882011-07-14 19:03:58 +0000683 }
bsalomon@google.com604a56a2013-03-15 15:42:15 +0000684 }
reed@android.combd700c32009-01-05 03:34:50 +0000685 }
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000686#if SK_SUPPORT_GPU
bsalomon@google.comcb265352013-02-22 16:13:16 +0000687 gContextFactory.destroyContexts();
robertphillips@google.com9d594202012-09-13 14:05:00 +0000688#endif
reed@android.combd700c32009-01-05 03:34:50 +0000689 return 0;
690}
caryclark@google.com5987f582012-10-02 18:33:14 +0000691
borenet@google.com7158e6a2012-11-01 17:43:44 +0000692#if !defined(SK_BUILD_FOR_IOS) && !defined(SK_BUILD_FOR_NACL)
caryclark@google.com5987f582012-10-02 18:33:14 +0000693int main(int argc, char * const argv[]) {
694 return tool_main(argc, (char**) argv);
695}
696#endif