blob: 092a577c6126bcd0012c9274e5e2cc98f00c564f [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
mtklein@google.comc2897432013-09-10 19:23:38 +0000410#if defined(SK_SCALAR_IS_FIXED)
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +0000411 writer.option("scalar", "fixed");
mtklein@google.comc2897432013-09-10 19:23:38 +0000412#else
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +0000413 writer.option("scalar", "float");
bungeman@google.coma5d48412011-06-15 17:25:46 +0000414#endif
415
416#if defined(SK_BUILD_FOR_WIN32)
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +0000417 writer.option("system", "WIN32");
bungeman@google.coma5d48412011-06-15 17:25:46 +0000418#elif defined(SK_BUILD_FOR_MAC)
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +0000419 writer.option("system", "MAC");
bungeman@google.coma5d48412011-06-15 17:25:46 +0000420#elif defined(SK_BUILD_FOR_ANDROID)
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +0000421 writer.option("system", "ANDROID");
bungeman@google.coma5d48412011-06-15 17:25:46 +0000422#elif defined(SK_BUILD_FOR_UNIX)
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +0000423 writer.option("system", "UNIX");
bungeman@google.coma5d48412011-06-15 17:25:46 +0000424#else
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +0000425 writer.option("system", "other");
bungeman@google.coma5d48412011-06-15 17:25:46 +0000426#endif
427
428#if defined(SK_DEBUG)
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +0000429 writer.option("build", "DEBUG");
430#else
431 writer.option("build", "RELEASE");
bungeman@google.coma5d48412011-06-15 17:25:46 +0000432#endif
mtklein@google.comc2897432013-09-10 19:23:38 +0000433
434 // Set texture cache limits if non-default.
bsalomon@google.com5c90e292013-02-22 19:17:13 +0000435 for (size_t i = 0; i < SK_ARRAY_COUNT(gConfigs); ++i) {
bsalomon@google.comcb265352013-02-22 16:13:16 +0000436#if SK_SUPPORT_GPU
mtklein@google.comc2897432013-09-10 19:23:38 +0000437 const Config& config = gConfigs[i];
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000438 if (SkBenchmark::kGPU_Backend != config.backend) {
mtklein@google.comc2897432013-09-10 19:23:38 +0000439 continue;
bsalomon@google.comcb265352013-02-22 16:13:16 +0000440 }
mtklein@google.comc2897432013-09-10 19:23:38 +0000441 GrContext* context = gContextFactory.get(config.contextType);
442 if (NULL == context) {
443 continue;
444 }
445
446 size_t bytes;
447 int count;
448 context->getTextureCacheLimits(&count, &bytes);
449 if (-1 != FLAGS_gpuCacheBytes) {
450 bytes = static_cast<size_t>(FLAGS_gpuCacheBytes);
451 }
452 if (-1 != FLAGS_gpuCacheCount) {
453 count = FLAGS_gpuCacheCount;
454 }
455 context->setTextureCacheLimits(count, bytes);
bsalomon@google.comcb265352013-02-22 16:13:16 +0000456#endif
457 }
bsalomon@google.com74913722011-10-27 20:44:19 +0000458
mtklein@google.comc2897432013-09-10 19:23:38 +0000459 // Run each bench in each configuration it supports and we asked for.
460 Iter iter;
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +0000461 SkBenchmark* bench;
reed@android.combd700c32009-01-05 03:34:50 +0000462 while ((bench = iter.next()) != NULL) {
bsalomon@google.com7fbc6042012-08-13 22:10:05 +0000463 SkAutoTUnref<SkBenchmark> benchUnref(bench);
mtklein@google.comc2897432013-09-10 19:23:38 +0000464 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, bench->getName())) {
reed@android.comb398fe82009-01-07 11:47:57 +0000465 continue;
466 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000467
mtklein@google.comc2897432013-09-10 19:23:38 +0000468 bench->setForceAlpha(alpha);
469 bench->setForceAA(FLAGS_forceAA);
470 bench->setForceFilter(FLAGS_forceFilter);
471 bench->setDither(dither);
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000472 AutoPrePostDraw appd(bench);
473
mtklein@google.comc2897432013-09-10 19:23:38 +0000474 bool loggedBenchName = false;
475 for (int i = 0; i < configs.count(); ++i) {
476 const int configIndex = configs[i];
477 const Config& config = gConfigs[configIndex];
tomhudson@google.com13eaaaa2012-04-16 18:00:40 +0000478
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000479 if (!bench->isSuitableFor(config.backend)) {
mtklein@google.comc2897432013-09-10 19:23:38 +0000480 continue;
bsalomon@google.com604a56a2013-03-15 15:42:15 +0000481 }
482
bsalomon@google.comcb265352013-02-22 16:13:16 +0000483 GrContext* context = NULL;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000484#if SK_SUPPORT_GPU
sugoi@google.com9c55f802013-03-07 20:52:59 +0000485 SkGLContextHelper* glContext = NULL;
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000486 if (SkBenchmark::kGPU_Backend == config.backend) {
mtklein@google.comc2897432013-09-10 19:23:38 +0000487 context = gContextFactory.get(config.contextType);
bsalomon@google.comcb265352013-02-22 16:13:16 +0000488 if (NULL == context) {
489 continue;
490 }
mtklein@google.comc2897432013-09-10 19:23:38 +0000491 glContext = gContextFactory.getGLContext(config.contextType);
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000492 }
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000493#endif
mtklein@google.comc2897432013-09-10 19:23:38 +0000494 SkAutoTUnref<SkBaseDevice> device;
495 SkAutoTUnref<SkCanvas> canvas;
496 SkPicture recordFrom, recordTo;
497 const SkIPoint dim = bench->getSize();
bsalomon@google.com604a56a2013-03-15 15:42:15 +0000498
mtklein@google.comc2897432013-09-10 19:23:38 +0000499 const SkPicture::RecordingFlags kRecordFlags =
500 SkPicture::kUsePathBoundsForClip_RecordingFlag;
501
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000502 if (SkBenchmark::kNonRendering_Backend != config.backend) {
mtklein@google.comc2897432013-09-10 19:23:38 +0000503 device.reset(make_device(config.config,
504 dim,
505 config.backend,
506 config.sampleCount,
507 context));
508 if (!device.get()) {
mtklein@google.comdbd41c82013-09-13 20:11:09 +0000509 logger.logError(SkStringPrintf(
510 "Device creation failure for config %s. Will skip.\n", config.name));
mtklein@google.comc2897432013-09-10 19:23:38 +0000511 continue;
keyar@chromium.orgfdd909c2012-07-20 23:03:42 +0000512 }
junov@chromium.orgfb103892012-09-20 19:35:43 +0000513
mtklein@google.comc2897432013-09-10 19:23:38 +0000514 switch(benchMode) {
515 case kDeferredSilent_BenchMode:
516 case kDeferred_BenchMode:
517 canvas.reset(SkDeferredCanvas::Create(device.get()));
518 break;
519 case kRecord_BenchMode:
520 canvas.reset(SkRef(recordTo.beginRecording(dim.fX, dim.fY, kRecordFlags)));
521 break;
522 case kPictureRecord_BenchMode:
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000523 bench->draw(1, recordFrom.beginRecording(dim.fX, dim.fY, kRecordFlags));
mtklein@google.comc2897432013-09-10 19:23:38 +0000524 recordFrom.endRecording();
525 canvas.reset(SkRef(recordTo.beginRecording(dim.fX, dim.fY, kRecordFlags)));
526 break;
527 case kNormal_BenchMode:
528 canvas.reset(new SkCanvas(device.get()));
529 break;
530 default:
531 SkASSERT(false);
532 }
533 }
534
535 if (NULL != canvas) {
536 canvas->clear(SK_ColorWHITE);
537 if (FLAGS_clip) { performClip(canvas, dim.fX, dim.fY); }
538 if (FLAGS_scale) { performScale(canvas, dim.fX, dim.fY); }
539 if (FLAGS_rotate) { performRotate(canvas, dim.fX, dim.fY); }
540 }
541
542 if (!loggedBenchName) {
543 loggedBenchName = true;
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +0000544 writer.bench(bench->getName(), dim.fX, dim.fY);
mtklein@google.comc2897432013-09-10 19:23:38 +0000545 }
546
547#if SK_SUPPORT_GPU
548 SkGLContextHelper* contextHelper = NULL;
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000549 if (SkBenchmark::kGPU_Backend == config.backend) {
mtklein@google.comc2897432013-09-10 19:23:38 +0000550 contextHelper = gContextFactory.getGLContext(config.contextType);
551 }
552 BenchTimer timer(contextHelper);
553#else
554 BenchTimer timer;
555#endif
556
mtklein@google.com9ef1d212013-09-13 20:39:50 +0000557 double previous = std::numeric_limits<double>::infinity();
mtklein@google.comdbd41c82013-09-13 20:11:09 +0000558 bool converged = false;
djsollen@google.com70de4da2013-10-10 17:33:39 +0000559
560 // variables used to compute loopsPerFrame
561 double frameIntervalTime = 0.0f;
562 int frameIntervalTotalLoops = 0;
563
564 bool frameIntervalComputed = false;
565 int loopsPerFrame = 0;
566 int loopsPerIter = 0;
mtklein@google.comdbd41c82013-09-13 20:11:09 +0000567 if (FLAGS_verbose) { SkDebugf("%s %s: ", bench->getName(), config.name); }
mtklein@google.comc2897432013-09-10 19:23:38 +0000568 do {
djsollen@google.com70de4da2013-10-10 17:33:39 +0000569 // Ramp up 1 -> 2 -> 4 -> 8 -> 16 -> ... -> ~1 billion.
570 loopsPerIter = (loopsPerIter == 0) ? 1 : loopsPerIter * 2;
571 if (loopsPerIter >= (1<<30) || timer.fWall > FLAGS_maxMs) {
mtklein@google.comc2897432013-09-10 19:23:38 +0000572 // If you find it takes more than a billion loops to get up to 20ms of runtime,
573 // you've got a computer clocked at several THz or have a broken benchmark. ;)
574 // "1B ought to be enough for anybody."
mtklein@google.comdbd41c82013-09-13 20:11:09 +0000575 logger.logError(SkStringPrintf(
djsollen@google.com70de4da2013-10-10 17:33:39 +0000576 "\nCan't get %s %s to converge in %dms (%d loops)",
577 bench->getName(), config.name, FLAGS_maxMs, loopsPerIter));
mtklein@google.comc2897432013-09-10 19:23:38 +0000578 break;
579 }
mtklein@google.comc2897432013-09-10 19:23:38 +0000580
581 if ((benchMode == kRecord_BenchMode || benchMode == kPictureRecord_BenchMode)) {
582 // Clear the recorded commands so that they do not accumulate.
583 canvas.reset(recordTo.beginRecording(dim.fX, dim.fY, kRecordFlags));
junov@chromium.orgfb103892012-09-20 19:35:43 +0000584 }
robertphillips@google.com91ee3a12012-08-28 12:18:40 +0000585
mtklein@google.comc2897432013-09-10 19:23:38 +0000586 timer.start();
djsollen@google.com70de4da2013-10-10 17:33:39 +0000587 // Inner loop that allows us to break the run into smaller
588 // chunks (e.g. frames). This is especially useful for the GPU
589 // as we can flush and/or swap buffers to keep the GPU from
590 // queuing up too much work.
591 for (int loopCount = loopsPerIter; loopCount > 0; ) {
commit-bot@chromium.org28871192013-10-14 15:28:01 +0000592 // Save and restore around each call to draw() to guarantee a pristine canvas.
593 SkAutoCanvasRestore saveRestore(canvas, true/*also save*/);
594
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000595 int loops;
djsollen@google.com70de4da2013-10-10 17:33:39 +0000596 if (frameIntervalComputed && loopCount > loopsPerFrame) {
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000597 loops = loopsPerFrame;
djsollen@google.com70de4da2013-10-10 17:33:39 +0000598 loopCount -= loopsPerFrame;
599 } else {
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000600 loops = loopCount;
djsollen@google.com70de4da2013-10-10 17:33:39 +0000601 loopCount = 0;
602 }
603
604 if (benchMode == kPictureRecord_BenchMode) {
605 recordFrom.draw(canvas);
606 } else {
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000607 bench->draw(loops, canvas);
djsollen@google.com70de4da2013-10-10 17:33:39 +0000608 }
609
610 if (kDeferredSilent_BenchMode == benchMode) {
611 static_cast<SkDeferredCanvas*>(canvas.get())->silentFlush();
612 } else if (NULL != canvas) {
613 canvas->flush();
614 }
615
616#if SK_SUPPORT_GPU
617 // swap drawing buffers on each frame to prevent the GPU
618 // from queuing up too much work
619 if (NULL != glContext) {
620 glContext->swapBuffers();
621 }
622#endif
mtklein@google.comc2897432013-09-10 19:23:38 +0000623 }
624
mtklein@google.comc2897432013-09-10 19:23:38 +0000625
626
627 // Stop truncated timers before GL calls complete, and stop the full timers after.
628 timer.truncatedEnd();
629#if SK_SUPPORT_GPU
630 if (NULL != glContext) {
631 context->flush();
632 SK_GL(*glContext, Finish());
633 }
634#endif
635 timer.end();
djsollen@google.com70de4da2013-10-10 17:33:39 +0000636
djsollen@google.comdcfed6c2013-10-10 18:48:27 +0000637 // setup the frame interval for subsequent iterations
638 if (!frameIntervalComputed) {
djsollen@google.com70de4da2013-10-10 17:33:39 +0000639 frameIntervalTime += timer.fWall;
640 frameIntervalTotalLoops += loopsPerIter;
641 if (frameIntervalTime >= FLAGS_minMs) {
642 frameIntervalComputed = true;
643 loopsPerFrame =
djsollen@google.com4e1d4b32013-10-10 17:50:52 +0000644 (int)(((double)frameIntervalTotalLoops / frameIntervalTime) * FLAGS_minMs);
djsollen@google.com70de4da2013-10-10 17:33:39 +0000645 if (loopsPerFrame < 1) {
646 loopsPerFrame = 1;
647 }
648// SkDebugf(" %s has %d loops in %f ms (normalized to %d)\n",
649// bench->getName(), frameIntervalTotalLoops,
650// timer.fWall, loopsPerFrame);
651 }
652 }
djsollen@google.comdcfed6c2013-10-10 18:48:27 +0000653
djsollen@google.com70de4da2013-10-10 17:33:39 +0000654 const double current = timer.fWall / loopsPerIter;
mtklein@google.comdbd41c82013-09-13 20:11:09 +0000655 if (FLAGS_verbose && current > previous) { SkDebugf("↑"); }
656 if (FLAGS_verbose) { SkDebugf("%.3g ", current); }
657 converged = HasConverged(previous, current, timer.fWall);
658 previous = current;
659 } while (!kIsDebug && !converged);
660 if (FLAGS_verbose) { SkDebugf("\n"); }
mtklein@google.comc2897432013-09-10 19:23:38 +0000661
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000662 if (FLAGS_outDir.count() && SkBenchmark::kNonRendering_Backend != config.backend) {
mtklein@google.comc2897432013-09-10 19:23:38 +0000663 saveFile(bench->getName(),
664 config.name,
665 FLAGS_outDir[0],
666 device->accessBitmap(false));
667 }
668
669 if (kIsDebug) {
670 // Let's not mislead ourselves by looking at Debug build bench times!
671 continue;
672 }
673
674 // Normalize to ms per 1000 iterations.
djsollen@google.com70de4da2013-10-10 17:33:39 +0000675 const double normalize = 1000.0 / loopsPerIter;
mtklein@google.comc2897432013-09-10 19:23:38 +0000676 const struct { char shortName; const char* longName; double ms; } times[] = {
677 {'w', "msecs", normalize * timer.fWall},
678 {'W', "Wmsecs", normalize * timer.fTruncatedWall},
679 {'c', "cmsecs", normalize * timer.fCpu},
680 {'C', "Cmsecs", normalize * timer.fTruncatedCpu},
681 {'g', "gmsecs", normalize * timer.fGpu},
682 };
683
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +0000684 writer.config(config.name);
mtklein@google.comc2897432013-09-10 19:23:38 +0000685 for (size_t i = 0; i < SK_ARRAY_COUNT(times); i++) {
686 if (strchr(FLAGS_timers[0], times[i].shortName) && times[i].ms > 0) {
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +0000687 writer.timer(times[i].longName, times[i].ms);
commit-bot@chromium.org7495f592013-06-03 19:31:07 +0000688 }
reed@google.com25df8882011-07-14 19:03:58 +0000689 }
bsalomon@google.com604a56a2013-03-15 15:42:15 +0000690 }
reed@android.combd700c32009-01-05 03:34:50 +0000691 }
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000692#if SK_SUPPORT_GPU
bsalomon@google.comcb265352013-02-22 16:13:16 +0000693 gContextFactory.destroyContexts();
robertphillips@google.com9d594202012-09-13 14:05:00 +0000694#endif
reed@android.combd700c32009-01-05 03:34:50 +0000695 return 0;
696}
caryclark@google.com5987f582012-10-02 18:33:14 +0000697
borenet@google.com7158e6a2012-11-01 17:43:44 +0000698#if !defined(SK_BUILD_FOR_IOS) && !defined(SK_BUILD_FOR_NACL)
caryclark@google.com5987f582012-10-02 18:33:14 +0000699int main(int argc, char * const argv[]) {
700 return tool_main(argc, (char**) argv);
701}
702#endif