blob: 9873c72fd6d38312005bb5c01f6d7a80a07496fa [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
bsalomon@google.com971d0c82011-08-19 17:22:05 +00008
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00009#if SK_SUPPORT_GPU
bsalomon@google.com971d0c82011-08-19 17:22:05 +000010#include "GrContext.h"
bsalomon@google.comcb265352013-02-22 16:13:16 +000011#include "GrContextFactory.h"
bsalomon@google.com971d0c82011-08-19 17:22:05 +000012#include "GrRenderTarget.h"
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000013#include "SkGpuDevice.h"
mtklein@google.comc2897432013-09-10 19:23:38 +000014#include "gl/GrGLDefines.h"
bsalomon@google.com41809932013-02-22 16:25:28 +000015#else
16class GrContext;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000017#endif // SK_SUPPORT_GPU
bsalomon@google.com971d0c82011-08-19 17:22:05 +000018
mtklein@google.comc2897432013-09-10 19:23:38 +000019#include "BenchTimer.h"
scroggo@google.com9a412522012-09-07 15:21:18 +000020#include "SkBenchLogger.h"
bsalomon@google.com971d0c82011-08-19 17:22:05 +000021#include "SkBenchmark.h"
robertphillips@google.com73672252013-08-29 12:40:26 +000022#include "SkBitmapDevice.h"
reed@android.combd700c32009-01-05 03:34:50 +000023#include "SkCanvas.h"
mtklein@google.com78d03792013-09-10 19:42:07 +000024#include "SkColorPriv.h"
sglez@google.com586db932013-07-24 17:24:23 +000025#include "SkCommandLineFlags.h"
bsalomon@google.com82a7bfc2012-04-16 19:11:17 +000026#include "SkDeferredCanvas.h"
reed@android.com3a859a02009-01-28 00:56:29 +000027#include "SkGraphics.h"
reed@android.comb398fe82009-01-07 11:47:57 +000028#include "SkImageEncoder.h"
mtklein@google.comc2897432013-09-10 19:23:38 +000029#include "SkOSFile.h"
reed@android.com6c924ad2009-03-31 03:48:49 +000030#include "SkPicture.h"
reed@android.combd700c32009-01-05 03:34:50 +000031#include "SkString.h"
reed@android.com29348cb2009-08-04 18:17:15 +000032
mtklein@google.comc2897432013-09-10 19:23:38 +000033enum BenchMode {
34 kNormal_BenchMode,
35 kDeferred_BenchMode,
36 kDeferredSilent_BenchMode,
37 kRecord_BenchMode,
38 kPictureRecord_BenchMode
keyar@chromium.orgfdd909c2012-07-20 23:03:42 +000039};
mtklein@google.comdbd41c82013-09-13 20:11:09 +000040const char* BenchMode_Name[] = {
41 "normal", "deferred", "deferredSilent", "record", "picturerecord"
42};
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +000043
keyar@chromium.orgfdd909c2012-07-20 23:03:42 +000044///////////////////////////////////////////////////////////////////////////////
45
reed@android.com6c924ad2009-03-31 03:48:49 +000046static void erase(SkBitmap& bm) {
47 if (bm.config() == SkBitmap::kA8_Config) {
junov@google.comdbfac8a2012-12-06 21:47:40 +000048 bm.eraseColor(SK_ColorTRANSPARENT);
reed@android.com6c924ad2009-03-31 03:48:49 +000049 } else {
50 bm.eraseColor(SK_ColorWHITE);
51 }
52}
53
reed@android.combd700c32009-01-05 03:34:50 +000054class Iter {
55public:
mtklein@google.comc2897432013-09-10 19:23:38 +000056 Iter() : fBench(BenchRegistry::Head()) {}
rmistry@google.comfbfcd562012-08-23 18:09:54 +000057
reed@android.combd700c32009-01-05 03:34:50 +000058 SkBenchmark* next() {
59 if (fBench) {
60 BenchRegistry::Factory f = fBench->factory();
61 fBench = fBench->next();
mtklein@google.com410e6e82013-09-13 19:52:27 +000062 return f();
reed@android.combd700c32009-01-05 03:34:50 +000063 }
64 return NULL;
65 }
bungeman@google.comd1a416a2011-05-18 18:37:07 +000066
reed@android.combd700c32009-01-05 03:34:50 +000067private:
68 const BenchRegistry* fBench;
69};
70
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +000071class AutoPrePostDraw {
72public:
73 AutoPrePostDraw(SkBenchmark* bench) : fBench(bench) {
74 fBench->preDraw();
75 }
76 ~AutoPrePostDraw() {
77 fBench->postDraw();
78 }
79private:
80 SkBenchmark* fBench;
81};
82
reed@android.combd700c32009-01-05 03:34:50 +000083static void make_filename(const char name[], SkString* path) {
84 path->set(name);
85 for (int i = 0; name[i]; i++) {
86 switch (name[i]) {
87 case '/':
88 case '\\':
89 case ' ':
90 case ':':
91 path->writable_str()[i] = '-';
92 break;
93 default:
94 break;
95 }
96 }
97}
98
reed@android.com4c7d3d62009-01-21 03:15:13 +000099static void saveFile(const char name[], const char config[], const char dir[],
100 const SkBitmap& bm) {
reed@android.com4c7d3d62009-01-21 03:15:13 +0000101 SkBitmap copy;
102 if (!bm.copyTo(&copy, SkBitmap::kARGB_8888_Config)) {
103 return;
104 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000105
reed@android.comf523e252009-01-26 23:15:37 +0000106 if (bm.config() == SkBitmap::kA8_Config) {
107 // turn alpha into gray-scale
108 size_t size = copy.getSize() >> 2;
109 SkPMColor* p = copy.getAddr32(0, 0);
110 for (size_t i = 0; i < size; i++) {
111 int c = (*p >> SK_A32_SHIFT) & 0xFF;
112 c = 255 - c;
113 c |= (c << 24) | (c << 16) | (c << 8);
114 *p++ = c | (SK_A32_MASK << SK_A32_SHIFT);
115 }
116 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000117
mtklein@google.comc2897432013-09-10 19:23:38 +0000118 SkString filename;
119 make_filename(name, &filename);
120 filename.appendf("_%s.png", config);
121 SkString path = SkOSPath::SkPathJoin(dir, filename.c_str());
122 ::remove(path.c_str());
123 SkImageEncoder::EncodeFile(path.c_str(), copy, SkImageEncoder::kPNG_Type, 100);
reed@android.com4c7d3d62009-01-21 03:15:13 +0000124}
125
126static void performClip(SkCanvas* canvas, int w, int h) {
127 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
138static void performRotate(SkCanvas* canvas, int w, int h) {
139 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
reed@android.com387359e2009-08-04 01:51:09 +0000147static void performScale(SkCanvas* canvas, int w, int h) {
148 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
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000157enum Backend {
bsalomon@google.com604a56a2013-03-15 15:42:15 +0000158 kNonRendering_Backend,
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000159 kRaster_Backend,
160 kGPU_Backend,
161 kPDF_Backend,
162};
163
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000164static SkBaseDevice* make_device(SkBitmap::Config config, const SkIPoint& size,
165 Backend backend, int sampleCount, GrContext* context) {
166 SkBaseDevice* device = NULL;
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000167 SkBitmap bitmap;
168 bitmap.setConfig(config, size.fX, size.fY);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000169
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000170 switch (backend) {
171 case kRaster_Backend:
172 bitmap.allocPixels();
173 erase(bitmap);
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000174 device = SkNEW_ARGS(SkBitmapDevice, (bitmap));
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000175 break;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000176#if SK_SUPPORT_GPU
bsalomon@google.comcb265352013-02-22 16:13:16 +0000177 case kGPU_Backend: {
178 GrTextureDesc desc;
179 desc.fConfig = kSkia8888_GrPixelConfig;
180 desc.fFlags = kRenderTarget_GrTextureFlagBit;
181 desc.fWidth = size.fX;
182 desc.fHeight = size.fY;
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000183 desc.fSampleCnt = sampleCount;
bsalomon@google.comcb265352013-02-22 16:13:16 +0000184 SkAutoTUnref<GrTexture> texture(context->createUncachedTexture(desc, NULL, 0));
185 if (!texture) {
186 return NULL;
187 }
188 device = SkNEW_ARGS(SkGpuDevice, (context, texture.get()));
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000189 break;
bsalomon@google.comcb265352013-02-22 16:13:16 +0000190 }
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000191#endif
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000192 case kPDF_Backend:
193 default:
mtklein@google.com330313a2013-08-22 15:37:26 +0000194 SkDEBUGFAIL("unsupported");
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000195 }
196 return device;
197}
198
bsalomon@google.comcb265352013-02-22 16:13:16 +0000199#if SK_SUPPORT_GPU
200GrContextFactory gContextFactory;
201typedef GrContextFactory::GLContextType GLContextType;
mtklein@google.comc2897432013-09-10 19:23:38 +0000202static const GLContextType kNative = GrContextFactory::kNative_GLContextType;
203#if SK_ANGLE
204static const GLContextType kANGLE = GrContextFactory::kANGLE_GLContextType;
205#else
206static const GLContextType kANGLE = kNative;
207#endif
208static const GLContextType kDebug = GrContextFactory::kDebug_GLContextType;
209static const GLContextType kNull = GrContextFactory::kNull_GLContextType;
bsalomon@google.comcb265352013-02-22 16:13:16 +0000210#else
211typedef int GLContextType;
mtklein@google.comc2897432013-09-10 19:23:38 +0000212static const GLContextType kNative = 0, kANGLE = 0, kDebug = 0, kNull = 0;
bsalomon@google.comcb265352013-02-22 16:13:16 +0000213#endif
214
mtklein@google.comc2897432013-09-10 19:23:38 +0000215#ifdef SK_DEBUG
216static const bool kIsDebug = true;
217#else
218static const bool kIsDebug = false;
219#endif
220
221static const struct Config {
222 SkBitmap::Config config;
223 const char* name;
224 int sampleCount;
225 Backend backend;
226 GLContextType contextType;
227 bool runByDefault;
reed@android.com4bc19832009-01-19 20:08:35 +0000228} gConfigs[] = {
mtklein@google.comc2897432013-09-10 19:23:38 +0000229 { SkBitmap::kNo_Config, "NONRENDERING", 0, kNonRendering_Backend, kNative, true},
230 { SkBitmap::kARGB_8888_Config, "8888", 0, kRaster_Backend, kNative, true},
231 { SkBitmap::kRGB_565_Config, "565", 0, kRaster_Backend, kNative, true},
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000232#if SK_SUPPORT_GPU
mtklein@google.comc2897432013-09-10 19:23:38 +0000233 { SkBitmap::kARGB_8888_Config, "GPU", 0, kGPU_Backend, kNative, true},
234 { SkBitmap::kARGB_8888_Config, "MSAA4", 4, kGPU_Backend, kNative, false},
235 { SkBitmap::kARGB_8888_Config, "MSAA16", 16, kGPU_Backend, kNative, false},
robertphillips@google.comd3b9fbb2012-03-28 16:19:11 +0000236#if SK_ANGLE
mtklein@google.comc2897432013-09-10 19:23:38 +0000237 { SkBitmap::kARGB_8888_Config, "ANGLE", 0, kGPU_Backend, kANGLE, true},
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000238#endif // SK_ANGLE
mtklein@google.comc2897432013-09-10 19:23:38 +0000239 { SkBitmap::kARGB_8888_Config, "Debug", 0, kGPU_Backend, kDebug, kIsDebug},
240 { SkBitmap::kARGB_8888_Config, "NULLGPU", 0, kGPU_Backend, kNull, true},
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000241#endif // SK_SUPPORT_GPU
reed@android.com4bc19832009-01-19 20:08:35 +0000242};
243
mtklein@google.comc2897432013-09-10 19:23:38 +0000244DEFINE_string(outDir, "", "If given, image of each bench will be put in outDir.");
245DEFINE_string(timers, "cg", "Timers to display. "
246 "Options: w(all) W(all, truncated) c(pu) C(pu, truncated) g(pu)");
reed@android.com4c7d3d62009-01-21 03:15:13 +0000247
mtklein@google.comc2897432013-09-10 19:23:38 +0000248DEFINE_bool(rotate, false, "Rotate canvas before bench run?");
249DEFINE_bool(scale, false, "Scale canvas before bench run?");
250DEFINE_bool(clip, false, "Clip canvas before bench run?");
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000251
mtklein@google.comc2897432013-09-10 19:23:38 +0000252DEFINE_bool(forceAA, true, "Force anti-aliasing?");
253DEFINE_bool(forceFilter, false, "Force bitmap filtering?");
254DEFINE_string(forceDither, "default", "Force dithering: true, false, or default?");
255DEFINE_bool(forceBlend, false, "Force alpha blending?");
256
257DEFINE_int32(gpuCacheBytes, -1, "GPU cache size limit in bytes. 0 to disable cache.");
258DEFINE_int32(gpuCacheCount, -1, "GPU cache size limit in object count. 0 to disable cache.");
259
260DEFINE_string(match, "", "[~][^]substring[$] [...] of test name to run.\n"
261 "Multiple matches may be separated by spaces.\n"
262 "~ causes a matching test to always be skipped\n"
263 "^ requires the start of the test to match\n"
264 "$ requires the end of the test to match\n"
265 "^ and $ requires an exact match\n"
266 "If a test does not match any list entry,\n"
267 "it is skipped unless some list entry starts with ~\n");
268DEFINE_string(mode, "normal",
269 "normal: draw to a normal canvas;\n"
270 "deferred: draw to a deferred canvas;\n"
271 "deferredSilent: deferred with silent playback;\n"
272 "record: draw to an SkPicture;\n"
273 "picturerecord: draw from an SkPicture to an SkPicture.\n");
274DEFINE_string(config, "", "Run configs given. If empty, runs the defaults set in gConfigs.");
275DEFINE_string(logFile, "", "Also write stdout here.");
mtklein@google.comdbd41c82013-09-13 20:11:09 +0000276DEFINE_int32(minMs, 20, "Shortest time we'll allow a benchmark to run.");
277DEFINE_int32(maxMs, 4000, "Longest time we'll allow a benchmark to run.");
278DEFINE_double(error, 0.01,
279 "Ratio of subsequent bench measurements must drop within 1±error to converge.");
mtklein@google.comc2897432013-09-10 19:23:38 +0000280DEFINE_string(timeFormat, "%9.2f", "Format to print results, in milliseconds per 1000 loops.");
mtklein@google.comdbd41c82013-09-13 20:11:09 +0000281DEFINE_bool2(verbose, v, false, "Print more.");
282
283// Has this bench converged? First arguments are milliseconds / loop iteration,
284// last is overall runtime in milliseconds.
285static bool HasConverged(double prevPerLoop, double currPerLoop, double currRaw) {
286 if (currRaw < FLAGS_minMs) {
287 return false;
288 }
289 const double low = 1 - FLAGS_error, high = 1 + FLAGS_error;
290 const double ratio = currPerLoop / prevPerLoop;
291 return low < ratio && ratio < high;
292}
tomhudson@google.com86bb9b72012-04-03 13:28:57 +0000293
caryclark@google.com5987f582012-10-02 18:33:14 +0000294int tool_main(int argc, char** argv);
295int tool_main(int argc, char** argv) {
bsalomon@google.com4e230682013-01-15 20:37:04 +0000296#if SK_ENABLE_INST_COUNT
bsalomon@google.com65a87cc2012-08-14 13:15:44 +0000297 gPrintInstCount = true;
298#endif
reed@android.com3a859a02009-01-28 00:56:29 +0000299 SkAutoGraphics ag;
mtklein@google.comc2897432013-09-10 19:23:38 +0000300 SkCommandLineFlags::Parse(argc, argv);
bsalomon@google.com65a87cc2012-08-14 13:15:44 +0000301
mtklein@google.comc2897432013-09-10 19:23:38 +0000302 // First, parse some flags.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000303
scroggo@google.com9a412522012-09-07 15:21:18 +0000304 SkBenchLogger logger;
mtklein@google.comc2897432013-09-10 19:23:38 +0000305 if (FLAGS_logFile.count()) {
306 logger.SetLogFile(FLAGS_logFile[0]);
307 }
scroggo@google.com9a412522012-09-07 15:21:18 +0000308
mtklein@google.comc2897432013-09-10 19:23:38 +0000309 const uint8_t alpha = FLAGS_forceBlend ? 0x80 : 0xFF;
310 SkTriState::State dither = SkTriState::kDefault;
311 for (size_t i = 0; i < 3; i++) {
312 if (strcmp(SkTriState::Name[i], FLAGS_forceDither[0]) == 0) {
313 dither = static_cast<SkTriState::State>(i);
reed@android.comb398fe82009-01-07 11:47:57 +0000314 }
315 }
mtklein@google.comc2897432013-09-10 19:23:38 +0000316
317 BenchMode benchMode = kNormal_BenchMode;
318 for (size_t i = 0; i < SK_ARRAY_COUNT(BenchMode_Name); i++) {
319 if (strcmp(FLAGS_mode[0], BenchMode_Name[i]) == 0) {
320 benchMode = static_cast<BenchMode>(i);
321 }
keyar@chromium.orgfdd909c2012-07-20 23:03:42 +0000322 }
mtklein@google.comc2897432013-09-10 19:23:38 +0000323
324 SkTDArray<int> configs;
325 // Try user-given configs first.
326 for (int i = 0; i < FLAGS_config.count(); i++) {
327 for (size_t j = 0; j < SK_ARRAY_COUNT(gConfigs); j++) {
328 if (0 == strcmp(FLAGS_config[i], gConfigs[j].name)) {
329 *configs.append() = j;
330 }
331 }
keyar@chromium.orgfdd909c2012-07-20 23:03:42 +0000332 }
mtklein@google.comc2897432013-09-10 19:23:38 +0000333 // If there weren't any, fill in with defaults.
334 if (configs.count() == 0) {
335 for (size_t i = 0; i < SK_ARRAY_COUNT(gConfigs); ++i) {
336 if (gConfigs[i].runByDefault) {
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000337 *configs.append() = i;
338 }
tomhudson@google.com13eaaaa2012-04-16 18:00:40 +0000339 }
340 }
mtklein@google.comc2897432013-09-10 19:23:38 +0000341 // Filter out things we can't run.
342 if (kNormal_BenchMode != benchMode) {
bsalomon@google.com604a56a2013-03-15 15:42:15 +0000343 // Non-rendering configs only run in normal mode
344 for (int i = 0; i < configs.count(); ++i) {
mtklein@google.comc2897432013-09-10 19:23:38 +0000345 const Config& config = gConfigs[configs[i]];
346 if (kNonRendering_Backend == config.backend) {
bsalomon@google.com604a56a2013-03-15 15:42:15 +0000347 configs.remove(i, 1);
348 --i;
349 }
350 }
351 }
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000352#if SK_SUPPORT_GPU
353 for (int i = 0; i < configs.count(); ++i) {
mtklein@google.comc2897432013-09-10 19:23:38 +0000354 const Config& config = gConfigs[configs[i]];
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000355
mtklein@google.comc2897432013-09-10 19:23:38 +0000356 if (kGPU_Backend == config.backend) {
357 GrContext* context = gContextFactory.get(config.contextType);
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000358 if (NULL == context) {
mtklein@google.comdbd41c82013-09-13 20:11:09 +0000359 logger.logError(SkStringPrintf(
360 "Error creating GrContext for config %s. Config will be skipped.\n",
361 config.name));
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000362 configs.remove(i);
363 --i;
364 continue;
365 }
mtklein@google.comc2897432013-09-10 19:23:38 +0000366 if (config.sampleCount > context->getMaxSampleCount()){
mtklein@google.comdbd41c82013-09-13 20:11:09 +0000367 logger.logError(SkStringPrintf(
368 "Sample count (%d) for config %s is unsupported. Config will be skipped.\n",
369 config.sampleCount, config.name));
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000370 configs.remove(i);
371 --i;
372 continue;
373 }
374 }
375 }
376#endif
377
mtklein@google.comc2897432013-09-10 19:23:38 +0000378 // All flags should be parsed now. Report our settings.
379 if (kIsDebug) {
380 logger.logError("bench was built in Debug mode, so we're going to hide the times."
381 " It's for your own good!\n");
382 }
383 SkString str("skia bench:");
384 str.appendf(" mode=%s", FLAGS_mode[0]);
385 str.appendf(" alpha=0x%02X antialias=%d filter=%d dither=%s",
386 alpha, FLAGS_forceAA, FLAGS_forceFilter, SkTriState::Name[dither]);
387 str.appendf(" rotate=%d scale=%d clip=%d", FLAGS_rotate, FLAGS_scale, FLAGS_clip);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000388
mtklein@google.comc2897432013-09-10 19:23:38 +0000389#if defined(SK_SCALAR_IS_FIXED)
390 str.append(" scalar=fixed");
391#else
392 str.append(" scalar=float");
bungeman@google.coma5d48412011-06-15 17:25:46 +0000393#endif
394
395#if defined(SK_BUILD_FOR_WIN32)
mtklein@google.comc2897432013-09-10 19:23:38 +0000396 str.append(" system=WIN32");
bungeman@google.coma5d48412011-06-15 17:25:46 +0000397#elif defined(SK_BUILD_FOR_MAC)
mtklein@google.comc2897432013-09-10 19:23:38 +0000398 str.append(" system=MAC");
bungeman@google.coma5d48412011-06-15 17:25:46 +0000399#elif defined(SK_BUILD_FOR_ANDROID)
mtklein@google.comc2897432013-09-10 19:23:38 +0000400 str.append(" system=ANDROID");
bungeman@google.coma5d48412011-06-15 17:25:46 +0000401#elif defined(SK_BUILD_FOR_UNIX)
mtklein@google.comc2897432013-09-10 19:23:38 +0000402 str.append(" system=UNIX");
bungeman@google.coma5d48412011-06-15 17:25:46 +0000403#else
mtklein@google.comc2897432013-09-10 19:23:38 +0000404 str.append(" system=other");
bungeman@google.coma5d48412011-06-15 17:25:46 +0000405#endif
406
407#if defined(SK_DEBUG)
mtklein@google.comc2897432013-09-10 19:23:38 +0000408 str.append(" DEBUG");
bungeman@google.coma5d48412011-06-15 17:25:46 +0000409#endif
mtklein@google.comc2897432013-09-10 19:23:38 +0000410 str.append("\n");
411 logger.logProgress(str);
bsalomon@google.com508824b2011-12-13 16:49:49 +0000412
mtklein@google.comc2897432013-09-10 19:23:38 +0000413
414 // Set texture cache limits if non-default.
bsalomon@google.com5c90e292013-02-22 19:17:13 +0000415 for (size_t i = 0; i < SK_ARRAY_COUNT(gConfigs); ++i) {
bsalomon@google.comcb265352013-02-22 16:13:16 +0000416#if SK_SUPPORT_GPU
mtklein@google.comc2897432013-09-10 19:23:38 +0000417 const Config& config = gConfigs[i];
418 if (kGPU_Backend != config.backend) {
419 continue;
bsalomon@google.comcb265352013-02-22 16:13:16 +0000420 }
mtklein@google.comc2897432013-09-10 19:23:38 +0000421 GrContext* context = gContextFactory.get(config.contextType);
422 if (NULL == context) {
423 continue;
424 }
425
426 size_t bytes;
427 int count;
428 context->getTextureCacheLimits(&count, &bytes);
429 if (-1 != FLAGS_gpuCacheBytes) {
430 bytes = static_cast<size_t>(FLAGS_gpuCacheBytes);
431 }
432 if (-1 != FLAGS_gpuCacheCount) {
433 count = FLAGS_gpuCacheCount;
434 }
435 context->setTextureCacheLimits(count, bytes);
bsalomon@google.comcb265352013-02-22 16:13:16 +0000436#endif
437 }
bsalomon@google.com74913722011-10-27 20:44:19 +0000438
mtklein@google.comc2897432013-09-10 19:23:38 +0000439 // Find the longest name of the benches we're going to run to make the output pretty.
440 Iter names;
reed@android.combd700c32009-01-05 03:34:50 +0000441 SkBenchmark* bench;
mtklein@google.comc2897432013-09-10 19:23:38 +0000442 int longestName = 0;
443 while ((bench = names.next()) != NULL) {
444 SkAutoTUnref<SkBenchmark> benchUnref(bench);
445 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, bench->getName())) {
446 continue;
447 }
448 const int length = strlen(bench->getName());
449 longestName = length > longestName ? length : longestName;
450 }
451
452 // Run each bench in each configuration it supports and we asked for.
453 Iter iter;
reed@android.combd700c32009-01-05 03:34:50 +0000454 while ((bench = iter.next()) != NULL) {
bsalomon@google.com7fbc6042012-08-13 22:10:05 +0000455 SkAutoTUnref<SkBenchmark> benchUnref(bench);
mtklein@google.comc2897432013-09-10 19:23:38 +0000456 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, bench->getName())) {
reed@android.comb398fe82009-01-07 11:47:57 +0000457 continue;
458 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000459
mtklein@google.comc2897432013-09-10 19:23:38 +0000460 bench->setForceAlpha(alpha);
461 bench->setForceAA(FLAGS_forceAA);
462 bench->setForceFilter(FLAGS_forceFilter);
463 bench->setDither(dither);
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000464 AutoPrePostDraw appd(bench);
465
mtklein@google.comc2897432013-09-10 19:23:38 +0000466 bool loggedBenchName = false;
467 for (int i = 0; i < configs.count(); ++i) {
468 const int configIndex = configs[i];
469 const Config& config = gConfigs[configIndex];
tomhudson@google.com13eaaaa2012-04-16 18:00:40 +0000470
mtklein@google.comc2897432013-09-10 19:23:38 +0000471 if ((kNonRendering_Backend == config.backend) == bench->isRendering()) {
472 continue;
bsalomon@google.com604a56a2013-03-15 15:42:15 +0000473 }
474
bsalomon@google.comcb265352013-02-22 16:13:16 +0000475 GrContext* context = NULL;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000476#if SK_SUPPORT_GPU
sugoi@google.com9c55f802013-03-07 20:52:59 +0000477 SkGLContextHelper* glContext = NULL;
mtklein@google.comc2897432013-09-10 19:23:38 +0000478 if (kGPU_Backend == config.backend) {
479 context = gContextFactory.get(config.contextType);
bsalomon@google.comcb265352013-02-22 16:13:16 +0000480 if (NULL == context) {
481 continue;
482 }
mtklein@google.comc2897432013-09-10 19:23:38 +0000483 glContext = gContextFactory.getGLContext(config.contextType);
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000484 }
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000485#endif
mtklein@google.comc2897432013-09-10 19:23:38 +0000486 SkAutoTUnref<SkBaseDevice> device;
487 SkAutoTUnref<SkCanvas> canvas;
488 SkPicture recordFrom, recordTo;
489 const SkIPoint dim = bench->getSize();
bsalomon@google.com604a56a2013-03-15 15:42:15 +0000490
mtklein@google.comc2897432013-09-10 19:23:38 +0000491 const SkPicture::RecordingFlags kRecordFlags =
492 SkPicture::kUsePathBoundsForClip_RecordingFlag;
493
494 if (kNonRendering_Backend != config.backend) {
495 device.reset(make_device(config.config,
496 dim,
497 config.backend,
498 config.sampleCount,
499 context));
500 if (!device.get()) {
mtklein@google.comdbd41c82013-09-13 20:11:09 +0000501 logger.logError(SkStringPrintf(
502 "Device creation failure for config %s. Will skip.\n", config.name));
mtklein@google.comc2897432013-09-10 19:23:38 +0000503 continue;
keyar@chromium.orgfdd909c2012-07-20 23:03:42 +0000504 }
junov@chromium.orgfb103892012-09-20 19:35:43 +0000505
mtklein@google.comc2897432013-09-10 19:23:38 +0000506 switch(benchMode) {
507 case kDeferredSilent_BenchMode:
508 case kDeferred_BenchMode:
509 canvas.reset(SkDeferredCanvas::Create(device.get()));
510 break;
511 case kRecord_BenchMode:
512 canvas.reset(SkRef(recordTo.beginRecording(dim.fX, dim.fY, kRecordFlags)));
513 break;
514 case kPictureRecord_BenchMode:
515 bench->draw(recordFrom.beginRecording(dim.fX, dim.fY, kRecordFlags));
516 recordFrom.endRecording();
517 canvas.reset(SkRef(recordTo.beginRecording(dim.fX, dim.fY, kRecordFlags)));
518 break;
519 case kNormal_BenchMode:
520 canvas.reset(new SkCanvas(device.get()));
521 break;
522 default:
523 SkASSERT(false);
524 }
525 }
526
527 if (NULL != canvas) {
528 canvas->clear(SK_ColorWHITE);
529 if (FLAGS_clip) { performClip(canvas, dim.fX, dim.fY); }
530 if (FLAGS_scale) { performScale(canvas, dim.fX, dim.fY); }
531 if (FLAGS_rotate) { performRotate(canvas, dim.fX, dim.fY); }
532 }
533
534 if (!loggedBenchName) {
535 loggedBenchName = true;
536 SkString str;
537 str.printf("running bench [%3d %3d] %*s ",
538 dim.fX, dim.fY, longestName, bench->getName());
539 logger.logProgress(str);
540 }
541
542#if SK_SUPPORT_GPU
543 SkGLContextHelper* contextHelper = NULL;
544 if (kGPU_Backend == config.backend) {
545 contextHelper = gContextFactory.getGLContext(config.contextType);
546 }
547 BenchTimer timer(contextHelper);
548#else
549 BenchTimer timer;
550#endif
551
mtklein@google.comdbd41c82013-09-13 20:11:09 +0000552 double previous = 1.0/0.0;
553 bool converged = false;
mtklein@google.comc2897432013-09-10 19:23:38 +0000554 bench->setLoops(0);
mtklein@google.comdbd41c82013-09-13 20:11:09 +0000555 if (FLAGS_verbose) { SkDebugf("%s %s: ", bench->getName(), config.name); }
mtklein@google.comc2897432013-09-10 19:23:38 +0000556 do {
557 // Ramp up 1 -> 4 -> 16 -> ... -> ~1 billion.
558 const int loops = bench->getLoops();
mtklein@google.comdbd41c82013-09-13 20:11:09 +0000559 if (loops >= (1<<30) || timer.fWall > FLAGS_maxMs) {
mtklein@google.comc2897432013-09-10 19:23:38 +0000560 // If you find it takes more than a billion loops to get up to 20ms of runtime,
561 // you've got a computer clocked at several THz or have a broken benchmark. ;)
562 // "1B ought to be enough for anybody."
mtklein@google.comdbd41c82013-09-13 20:11:09 +0000563 logger.logError(SkStringPrintf(
564 "Can't get %s %s to converge in %dms.\n",
565 bench->getName(), config.name, FLAGS_maxMs));
mtklein@google.comc2897432013-09-10 19:23:38 +0000566 break;
567 }
mtklein@google.comdbd41c82013-09-13 20:11:09 +0000568 bench->setLoops(loops == 0 ? 1 : loops * 2);
mtklein@google.comc2897432013-09-10 19:23:38 +0000569
570 if ((benchMode == kRecord_BenchMode || benchMode == kPictureRecord_BenchMode)) {
571 // Clear the recorded commands so that they do not accumulate.
572 canvas.reset(recordTo.beginRecording(dim.fX, dim.fY, kRecordFlags));
junov@chromium.orgfb103892012-09-20 19:35:43 +0000573 }
robertphillips@google.com91ee3a12012-08-28 12:18:40 +0000574
mtklein@google.comc2897432013-09-10 19:23:38 +0000575 timer.start();
576 if (NULL != canvas) {
577 canvas->save();
commit-bot@chromium.org7495f592013-06-03 19:31:07 +0000578 }
mtklein@google.comc2897432013-09-10 19:23:38 +0000579 if (benchMode == kPictureRecord_BenchMode) {
580 recordFrom.draw(canvas);
borenet@google.com8ad29ce2013-09-10 17:22:43 +0000581 } else {
mtklein@google.comc2897432013-09-10 19:23:38 +0000582 bench->draw(canvas);
commit-bot@chromium.org7495f592013-06-03 19:31:07 +0000583 }
borenet@google.com8ad29ce2013-09-10 17:22:43 +0000584
mtklein@google.comc2897432013-09-10 19:23:38 +0000585 if (kDeferredSilent_BenchMode == benchMode) {
586 static_cast<SkDeferredCanvas*>(canvas.get())->silentFlush();
587 } else if (NULL != canvas) {
588 canvas->flush();
589 }
590
591 if (NULL != canvas) {
592 canvas->restore();
593 }
594
595
596 // Stop truncated timers before GL calls complete, and stop the full timers after.
597 timer.truncatedEnd();
598#if SK_SUPPORT_GPU
599 if (NULL != glContext) {
600 context->flush();
601 SK_GL(*glContext, Finish());
602 }
603#endif
604 timer.end();
mtklein@google.comdbd41c82013-09-13 20:11:09 +0000605 const double current = timer.fWall / bench->getLoops();
606 if (FLAGS_verbose && current > previous) { SkDebugf("↑"); }
607 if (FLAGS_verbose) { SkDebugf("%.3g ", current); }
608 converged = HasConverged(previous, current, timer.fWall);
609 previous = current;
610 } while (!kIsDebug && !converged);
611 if (FLAGS_verbose) { SkDebugf("\n"); }
mtklein@google.comc2897432013-09-10 19:23:38 +0000612
613 if (FLAGS_outDir.count() && kNonRendering_Backend != config.backend) {
614 saveFile(bench->getName(),
615 config.name,
616 FLAGS_outDir[0],
617 device->accessBitmap(false));
618 }
619
620 if (kIsDebug) {
621 // Let's not mislead ourselves by looking at Debug build bench times!
622 continue;
623 }
624
625 // Normalize to ms per 1000 iterations.
626 const double normalize = 1000.0 / bench->getLoops();
627 const struct { char shortName; const char* longName; double ms; } times[] = {
628 {'w', "msecs", normalize * timer.fWall},
629 {'W', "Wmsecs", normalize * timer.fTruncatedWall},
630 {'c', "cmsecs", normalize * timer.fCpu},
631 {'C', "Cmsecs", normalize * timer.fTruncatedCpu},
632 {'g', "gmsecs", normalize * timer.fGpu},
633 };
634
635 SkString result;
636 result.appendf(" %s:", config.name);
637 for (size_t i = 0; i < SK_ARRAY_COUNT(times); i++) {
638 if (strchr(FLAGS_timers[0], times[i].shortName) && times[i].ms > 0) {
639 result.appendf(" %s = ", times[i].longName);
640 result.appendf(FLAGS_timeFormat[0], times[i].ms);
commit-bot@chromium.org7495f592013-06-03 19:31:07 +0000641 }
reed@google.com25df8882011-07-14 19:03:58 +0000642 }
mtklein@google.comc2897432013-09-10 19:23:38 +0000643 logger.logProgress(result);
bensong@google.com24ed8d72013-06-12 14:45:03 +0000644 }
mtklein@google.comc2897432013-09-10 19:23:38 +0000645 if (loggedBenchName) {
646 logger.logProgress("\n");
bsalomon@google.com604a56a2013-03-15 15:42:15 +0000647 }
reed@android.combd700c32009-01-05 03:34:50 +0000648 }
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000649#if SK_SUPPORT_GPU
bsalomon@google.comcb265352013-02-22 16:13:16 +0000650 gContextFactory.destroyContexts();
robertphillips@google.com9d594202012-09-13 14:05:00 +0000651#endif
reed@android.combd700c32009-01-05 03:34:50 +0000652 return 0;
653}
caryclark@google.com5987f582012-10-02 18:33:14 +0000654
borenet@google.com7158e6a2012-11-01 17:43:44 +0000655#if !defined(SK_BUILD_FOR_IOS) && !defined(SK_BUILD_FOR_NACL)
caryclark@google.com5987f582012-10-02 18:33:14 +0000656int main(int argc, char * const argv[]) {
657 return tool_main(argc, (char**) argv);
658}
659#endif