blob: 3a57f2de334ea6687db22971557a13b4230fbb15 [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.comc2897432013-09-10 19:23:38 +000040const char* BenchMode_Name[] = { "normal", "deferred", "deferredSilent", "record", "picturerecord" };
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +000041
keyar@chromium.orgfdd909c2012-07-20 23:03:42 +000042///////////////////////////////////////////////////////////////////////////////
43
reed@android.com6c924ad2009-03-31 03:48:49 +000044static void erase(SkBitmap& bm) {
45 if (bm.config() == SkBitmap::kA8_Config) {
junov@google.comdbfac8a2012-12-06 21:47:40 +000046 bm.eraseColor(SK_ColorTRANSPARENT);
reed@android.com6c924ad2009-03-31 03:48:49 +000047 } else {
48 bm.eraseColor(SK_ColorWHITE);
49 }
50}
51
reed@android.combd700c32009-01-05 03:34:50 +000052class Iter {
53public:
mtklein@google.comc2897432013-09-10 19:23:38 +000054 Iter() : fBench(BenchRegistry::Head()) {}
rmistry@google.comfbfcd562012-08-23 18:09:54 +000055
reed@android.combd700c32009-01-05 03:34:50 +000056 SkBenchmark* next() {
57 if (fBench) {
58 BenchRegistry::Factory f = fBench->factory();
59 fBench = fBench->next();
mtklein@google.com410e6e82013-09-13 19:52:27 +000060 return f();
reed@android.combd700c32009-01-05 03:34:50 +000061 }
62 return NULL;
63 }
bungeman@google.comd1a416a2011-05-18 18:37:07 +000064
reed@android.combd700c32009-01-05 03:34:50 +000065private:
66 const BenchRegistry* fBench;
67};
68
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +000069class AutoPrePostDraw {
70public:
71 AutoPrePostDraw(SkBenchmark* bench) : fBench(bench) {
72 fBench->preDraw();
73 }
74 ~AutoPrePostDraw() {
75 fBench->postDraw();
76 }
77private:
78 SkBenchmark* fBench;
79};
80
reed@android.combd700c32009-01-05 03:34:50 +000081static void make_filename(const char name[], SkString* path) {
82 path->set(name);
83 for (int i = 0; name[i]; i++) {
84 switch (name[i]) {
85 case '/':
86 case '\\':
87 case ' ':
88 case ':':
89 path->writable_str()[i] = '-';
90 break;
91 default:
92 break;
93 }
94 }
95}
96
reed@android.com4c7d3d62009-01-21 03:15:13 +000097static void saveFile(const char name[], const char config[], const char dir[],
98 const SkBitmap& bm) {
reed@android.com4c7d3d62009-01-21 03:15:13 +000099 SkBitmap copy;
100 if (!bm.copyTo(&copy, SkBitmap::kARGB_8888_Config)) {
101 return;
102 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000103
reed@android.comf523e252009-01-26 23:15:37 +0000104 if (bm.config() == SkBitmap::kA8_Config) {
105 // turn alpha into gray-scale
106 size_t size = copy.getSize() >> 2;
107 SkPMColor* p = copy.getAddr32(0, 0);
108 for (size_t i = 0; i < size; i++) {
109 int c = (*p >> SK_A32_SHIFT) & 0xFF;
110 c = 255 - c;
111 c |= (c << 24) | (c << 16) | (c << 8);
112 *p++ = c | (SK_A32_MASK << SK_A32_SHIFT);
113 }
114 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000115
mtklein@google.comc2897432013-09-10 19:23:38 +0000116 SkString filename;
117 make_filename(name, &filename);
118 filename.appendf("_%s.png", config);
119 SkString path = SkOSPath::SkPathJoin(dir, filename.c_str());
120 ::remove(path.c_str());
121 SkImageEncoder::EncodeFile(path.c_str(), copy, SkImageEncoder::kPNG_Type, 100);
reed@android.com4c7d3d62009-01-21 03:15:13 +0000122}
123
124static void performClip(SkCanvas* canvas, int w, int h) {
125 SkRect r;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000126
reed@android.com4c7d3d62009-01-21 03:15:13 +0000127 r.set(SkIntToScalar(10), SkIntToScalar(10),
128 SkIntToScalar(w*2/3), SkIntToScalar(h*2/3));
129 canvas->clipRect(r, SkRegion::kIntersect_Op);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000130
reed@android.com4c7d3d62009-01-21 03:15:13 +0000131 r.set(SkIntToScalar(w/3), SkIntToScalar(h/3),
132 SkIntToScalar(w-10), SkIntToScalar(h-10));
133 canvas->clipRect(r, SkRegion::kXOR_Op);
134}
135
136static void performRotate(SkCanvas* canvas, int w, int h) {
137 const SkScalar x = SkIntToScalar(w) / 2;
138 const SkScalar y = SkIntToScalar(h) / 2;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000139
reed@android.com4c7d3d62009-01-21 03:15:13 +0000140 canvas->translate(x, y);
141 canvas->rotate(SkIntToScalar(35));
142 canvas->translate(-x, -y);
143}
144
reed@android.com387359e2009-08-04 01:51:09 +0000145static void performScale(SkCanvas* canvas, int w, int h) {
146 const SkScalar x = SkIntToScalar(w) / 2;
147 const SkScalar y = SkIntToScalar(h) / 2;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000148
reed@android.com387359e2009-08-04 01:51:09 +0000149 canvas->translate(x, y);
150 // just enough so we can't take the sprite case
151 canvas->scale(SK_Scalar1 * 99/100, SK_Scalar1 * 99/100);
152 canvas->translate(-x, -y);
153}
154
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000155enum Backend {
bsalomon@google.com604a56a2013-03-15 15:42:15 +0000156 kNonRendering_Backend,
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000157 kRaster_Backend,
158 kGPU_Backend,
159 kPDF_Backend,
160};
161
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000162static SkBaseDevice* make_device(SkBitmap::Config config, const SkIPoint& size,
163 Backend backend, int sampleCount, GrContext* context) {
164 SkBaseDevice* device = NULL;
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000165 SkBitmap bitmap;
166 bitmap.setConfig(config, size.fX, size.fY);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000167
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000168 switch (backend) {
169 case kRaster_Backend:
170 bitmap.allocPixels();
171 erase(bitmap);
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000172 device = SkNEW_ARGS(SkBitmapDevice, (bitmap));
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000173 break;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000174#if SK_SUPPORT_GPU
bsalomon@google.comcb265352013-02-22 16:13:16 +0000175 case kGPU_Backend: {
176 GrTextureDesc desc;
177 desc.fConfig = kSkia8888_GrPixelConfig;
178 desc.fFlags = kRenderTarget_GrTextureFlagBit;
179 desc.fWidth = size.fX;
180 desc.fHeight = size.fY;
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000181 desc.fSampleCnt = sampleCount;
bsalomon@google.comcb265352013-02-22 16:13:16 +0000182 SkAutoTUnref<GrTexture> texture(context->createUncachedTexture(desc, NULL, 0));
183 if (!texture) {
184 return NULL;
185 }
186 device = SkNEW_ARGS(SkGpuDevice, (context, texture.get()));
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000187 break;
bsalomon@google.comcb265352013-02-22 16:13:16 +0000188 }
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000189#endif
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000190 case kPDF_Backend:
191 default:
mtklein@google.com330313a2013-08-22 15:37:26 +0000192 SkDEBUGFAIL("unsupported");
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000193 }
194 return device;
195}
196
bsalomon@google.comcb265352013-02-22 16:13:16 +0000197#if SK_SUPPORT_GPU
198GrContextFactory gContextFactory;
199typedef GrContextFactory::GLContextType GLContextType;
mtklein@google.comc2897432013-09-10 19:23:38 +0000200static const GLContextType kNative = GrContextFactory::kNative_GLContextType;
201#if SK_ANGLE
202static const GLContextType kANGLE = GrContextFactory::kANGLE_GLContextType;
203#else
204static const GLContextType kANGLE = kNative;
205#endif
206static const GLContextType kDebug = GrContextFactory::kDebug_GLContextType;
207static const GLContextType kNull = GrContextFactory::kNull_GLContextType;
bsalomon@google.comcb265352013-02-22 16:13:16 +0000208#else
209typedef int GLContextType;
mtklein@google.comc2897432013-09-10 19:23:38 +0000210static const GLContextType kNative = 0, kANGLE = 0, kDebug = 0, kNull = 0;
bsalomon@google.comcb265352013-02-22 16:13:16 +0000211#endif
212
mtklein@google.comc2897432013-09-10 19:23:38 +0000213#ifdef SK_DEBUG
214static const bool kIsDebug = true;
215#else
216static const bool kIsDebug = false;
217#endif
218
219static const struct Config {
220 SkBitmap::Config config;
221 const char* name;
222 int sampleCount;
223 Backend backend;
224 GLContextType contextType;
225 bool runByDefault;
reed@android.com4bc19832009-01-19 20:08:35 +0000226} gConfigs[] = {
mtklein@google.comc2897432013-09-10 19:23:38 +0000227 { SkBitmap::kNo_Config, "NONRENDERING", 0, kNonRendering_Backend, kNative, true},
228 { SkBitmap::kARGB_8888_Config, "8888", 0, kRaster_Backend, kNative, true},
229 { SkBitmap::kRGB_565_Config, "565", 0, kRaster_Backend, kNative, true},
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000230#if SK_SUPPORT_GPU
mtklein@google.comc2897432013-09-10 19:23:38 +0000231 { SkBitmap::kARGB_8888_Config, "GPU", 0, kGPU_Backend, kNative, true},
232 { SkBitmap::kARGB_8888_Config, "MSAA4", 4, kGPU_Backend, kNative, false},
233 { SkBitmap::kARGB_8888_Config, "MSAA16", 16, kGPU_Backend, kNative, false},
robertphillips@google.comd3b9fbb2012-03-28 16:19:11 +0000234#if SK_ANGLE
mtklein@google.comc2897432013-09-10 19:23:38 +0000235 { SkBitmap::kARGB_8888_Config, "ANGLE", 0, kGPU_Backend, kANGLE, true},
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000236#endif // SK_ANGLE
mtklein@google.comc2897432013-09-10 19:23:38 +0000237 { SkBitmap::kARGB_8888_Config, "Debug", 0, kGPU_Backend, kDebug, kIsDebug},
238 { SkBitmap::kARGB_8888_Config, "NULLGPU", 0, kGPU_Backend, kNull, true},
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000239#endif // SK_SUPPORT_GPU
reed@android.com4bc19832009-01-19 20:08:35 +0000240};
241
mtklein@google.comc2897432013-09-10 19:23:38 +0000242DEFINE_string(outDir, "", "If given, image of each bench will be put in outDir.");
243DEFINE_string(timers, "cg", "Timers to display. "
244 "Options: w(all) W(all, truncated) c(pu) C(pu, truncated) g(pu)");
reed@android.com4c7d3d62009-01-21 03:15:13 +0000245
mtklein@google.comc2897432013-09-10 19:23:38 +0000246DEFINE_bool(rotate, false, "Rotate canvas before bench run?");
247DEFINE_bool(scale, false, "Scale canvas before bench run?");
248DEFINE_bool(clip, false, "Clip canvas before bench run?");
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000249
mtklein@google.comc2897432013-09-10 19:23:38 +0000250DEFINE_bool(forceAA, true, "Force anti-aliasing?");
251DEFINE_bool(forceFilter, false, "Force bitmap filtering?");
252DEFINE_string(forceDither, "default", "Force dithering: true, false, or default?");
253DEFINE_bool(forceBlend, false, "Force alpha blending?");
254
255DEFINE_int32(gpuCacheBytes, -1, "GPU cache size limit in bytes. 0 to disable cache.");
256DEFINE_int32(gpuCacheCount, -1, "GPU cache size limit in object count. 0 to disable cache.");
257
258DEFINE_string(match, "", "[~][^]substring[$] [...] of test name to run.\n"
259 "Multiple matches may be separated by spaces.\n"
260 "~ causes a matching test to always be skipped\n"
261 "^ requires the start of the test to match\n"
262 "$ requires the end of the test to match\n"
263 "^ and $ requires an exact match\n"
264 "If a test does not match any list entry,\n"
265 "it is skipped unless some list entry starts with ~\n");
266DEFINE_string(mode, "normal",
267 "normal: draw to a normal canvas;\n"
268 "deferred: draw to a deferred canvas;\n"
269 "deferredSilent: deferred with silent playback;\n"
270 "record: draw to an SkPicture;\n"
271 "picturerecord: draw from an SkPicture to an SkPicture.\n");
272DEFINE_string(config, "", "Run configs given. If empty, runs the defaults set in gConfigs.");
273DEFINE_string(logFile, "", "Also write stdout here.");
274DEFINE_int32(benchMs, 20, "Target time in ms to run each benchmark config.");
275DEFINE_string(timeFormat, "%9.2f", "Format to print results, in milliseconds per 1000 loops.");
tomhudson@google.com86bb9b72012-04-03 13:28:57 +0000276
caryclark@google.com5987f582012-10-02 18:33:14 +0000277int tool_main(int argc, char** argv);
278int tool_main(int argc, char** argv) {
bsalomon@google.com4e230682013-01-15 20:37:04 +0000279#if SK_ENABLE_INST_COUNT
bsalomon@google.com65a87cc2012-08-14 13:15:44 +0000280 gPrintInstCount = true;
281#endif
reed@android.com3a859a02009-01-28 00:56:29 +0000282 SkAutoGraphics ag;
mtklein@google.comc2897432013-09-10 19:23:38 +0000283 SkCommandLineFlags::Parse(argc, argv);
bsalomon@google.com65a87cc2012-08-14 13:15:44 +0000284
mtklein@google.comc2897432013-09-10 19:23:38 +0000285 // First, parse some flags.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000286
scroggo@google.com9a412522012-09-07 15:21:18 +0000287 SkBenchLogger logger;
mtklein@google.comc2897432013-09-10 19:23:38 +0000288 if (FLAGS_logFile.count()) {
289 logger.SetLogFile(FLAGS_logFile[0]);
290 }
scroggo@google.com9a412522012-09-07 15:21:18 +0000291
mtklein@google.comc2897432013-09-10 19:23:38 +0000292 const uint8_t alpha = FLAGS_forceBlend ? 0x80 : 0xFF;
293 SkTriState::State dither = SkTriState::kDefault;
294 for (size_t i = 0; i < 3; i++) {
295 if (strcmp(SkTriState::Name[i], FLAGS_forceDither[0]) == 0) {
296 dither = static_cast<SkTriState::State>(i);
reed@android.comb398fe82009-01-07 11:47:57 +0000297 }
298 }
mtklein@google.comc2897432013-09-10 19:23:38 +0000299
300 BenchMode benchMode = kNormal_BenchMode;
301 for (size_t i = 0; i < SK_ARRAY_COUNT(BenchMode_Name); i++) {
302 if (strcmp(FLAGS_mode[0], BenchMode_Name[i]) == 0) {
303 benchMode = static_cast<BenchMode>(i);
304 }
keyar@chromium.orgfdd909c2012-07-20 23:03:42 +0000305 }
mtklein@google.comc2897432013-09-10 19:23:38 +0000306
307 SkTDArray<int> configs;
308 // Try user-given configs first.
309 for (int i = 0; i < FLAGS_config.count(); i++) {
310 for (size_t j = 0; j < SK_ARRAY_COUNT(gConfigs); j++) {
311 if (0 == strcmp(FLAGS_config[i], gConfigs[j].name)) {
312 *configs.append() = j;
313 }
314 }
keyar@chromium.orgfdd909c2012-07-20 23:03:42 +0000315 }
mtklein@google.comc2897432013-09-10 19:23:38 +0000316 // If there weren't any, fill in with defaults.
317 if (configs.count() == 0) {
318 for (size_t i = 0; i < SK_ARRAY_COUNT(gConfigs); ++i) {
319 if (gConfigs[i].runByDefault) {
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000320 *configs.append() = i;
321 }
tomhudson@google.com13eaaaa2012-04-16 18:00:40 +0000322 }
323 }
mtklein@google.comc2897432013-09-10 19:23:38 +0000324 // Filter out things we can't run.
325 if (kNormal_BenchMode != benchMode) {
bsalomon@google.com604a56a2013-03-15 15:42:15 +0000326 // Non-rendering configs only run in normal mode
327 for (int i = 0; i < configs.count(); ++i) {
mtklein@google.comc2897432013-09-10 19:23:38 +0000328 const Config& config = gConfigs[configs[i]];
329 if (kNonRendering_Backend == config.backend) {
bsalomon@google.com604a56a2013-03-15 15:42:15 +0000330 configs.remove(i, 1);
331 --i;
332 }
333 }
334 }
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000335#if SK_SUPPORT_GPU
336 for (int i = 0; i < configs.count(); ++i) {
mtklein@google.comc2897432013-09-10 19:23:38 +0000337 const Config& config = gConfigs[configs[i]];
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000338
mtklein@google.comc2897432013-09-10 19:23:38 +0000339 if (kGPU_Backend == config.backend) {
340 GrContext* context = gContextFactory.get(config.contextType);
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000341 if (NULL == context) {
342 SkString error;
343 error.printf("Error creating GrContext for config %s. Config will be skipped.\n",
mtklein@google.comc2897432013-09-10 19:23:38 +0000344 config.name);
345 logger.logError(error);
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000346 configs.remove(i);
347 --i;
348 continue;
349 }
mtklein@google.comc2897432013-09-10 19:23:38 +0000350 if (config.sampleCount > context->getMaxSampleCount()){
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000351 SkString error;
352 error.printf("Sample count (%d) for config %s is unsupported. "
353 "Config will be skipped.\n",
mtklein@google.comc2897432013-09-10 19:23:38 +0000354 config.sampleCount, config.name);
355 logger.logError(error);
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000356 configs.remove(i);
357 --i;
358 continue;
359 }
360 }
361 }
362#endif
363
mtklein@google.comc2897432013-09-10 19:23:38 +0000364 // All flags should be parsed now. Report our settings.
365 if (kIsDebug) {
366 logger.logError("bench was built in Debug mode, so we're going to hide the times."
367 " It's for your own good!\n");
368 }
369 SkString str("skia bench:");
370 str.appendf(" mode=%s", FLAGS_mode[0]);
371 str.appendf(" alpha=0x%02X antialias=%d filter=%d dither=%s",
372 alpha, FLAGS_forceAA, FLAGS_forceFilter, SkTriState::Name[dither]);
373 str.appendf(" rotate=%d scale=%d clip=%d", FLAGS_rotate, FLAGS_scale, FLAGS_clip);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000374
mtklein@google.comc2897432013-09-10 19:23:38 +0000375#if defined(SK_SCALAR_IS_FIXED)
376 str.append(" scalar=fixed");
377#else
378 str.append(" scalar=float");
bungeman@google.coma5d48412011-06-15 17:25:46 +0000379#endif
380
381#if defined(SK_BUILD_FOR_WIN32)
mtklein@google.comc2897432013-09-10 19:23:38 +0000382 str.append(" system=WIN32");
bungeman@google.coma5d48412011-06-15 17:25:46 +0000383#elif defined(SK_BUILD_FOR_MAC)
mtklein@google.comc2897432013-09-10 19:23:38 +0000384 str.append(" system=MAC");
bungeman@google.coma5d48412011-06-15 17:25:46 +0000385#elif defined(SK_BUILD_FOR_ANDROID)
mtklein@google.comc2897432013-09-10 19:23:38 +0000386 str.append(" system=ANDROID");
bungeman@google.coma5d48412011-06-15 17:25:46 +0000387#elif defined(SK_BUILD_FOR_UNIX)
mtklein@google.comc2897432013-09-10 19:23:38 +0000388 str.append(" system=UNIX");
bungeman@google.coma5d48412011-06-15 17:25:46 +0000389#else
mtklein@google.comc2897432013-09-10 19:23:38 +0000390 str.append(" system=other");
bungeman@google.coma5d48412011-06-15 17:25:46 +0000391#endif
392
393#if defined(SK_DEBUG)
mtklein@google.comc2897432013-09-10 19:23:38 +0000394 str.append(" DEBUG");
bungeman@google.coma5d48412011-06-15 17:25:46 +0000395#endif
mtklein@google.comc2897432013-09-10 19:23:38 +0000396 str.append("\n");
397 logger.logProgress(str);
bsalomon@google.com508824b2011-12-13 16:49:49 +0000398
mtklein@google.comc2897432013-09-10 19:23:38 +0000399
400 // Set texture cache limits if non-default.
bsalomon@google.com5c90e292013-02-22 19:17:13 +0000401 for (size_t i = 0; i < SK_ARRAY_COUNT(gConfigs); ++i) {
bsalomon@google.comcb265352013-02-22 16:13:16 +0000402#if SK_SUPPORT_GPU
mtklein@google.comc2897432013-09-10 19:23:38 +0000403 const Config& config = gConfigs[i];
404 if (kGPU_Backend != config.backend) {
405 continue;
bsalomon@google.comcb265352013-02-22 16:13:16 +0000406 }
mtklein@google.comc2897432013-09-10 19:23:38 +0000407 GrContext* context = gContextFactory.get(config.contextType);
408 if (NULL == context) {
409 continue;
410 }
411
412 size_t bytes;
413 int count;
414 context->getTextureCacheLimits(&count, &bytes);
415 if (-1 != FLAGS_gpuCacheBytes) {
416 bytes = static_cast<size_t>(FLAGS_gpuCacheBytes);
417 }
418 if (-1 != FLAGS_gpuCacheCount) {
419 count = FLAGS_gpuCacheCount;
420 }
421 context->setTextureCacheLimits(count, bytes);
bsalomon@google.comcb265352013-02-22 16:13:16 +0000422#endif
423 }
bsalomon@google.com74913722011-10-27 20:44:19 +0000424
mtklein@google.comc2897432013-09-10 19:23:38 +0000425 // Find the longest name of the benches we're going to run to make the output pretty.
426 Iter names;
reed@android.combd700c32009-01-05 03:34:50 +0000427 SkBenchmark* bench;
mtklein@google.comc2897432013-09-10 19:23:38 +0000428 int longestName = 0;
429 while ((bench = names.next()) != NULL) {
430 SkAutoTUnref<SkBenchmark> benchUnref(bench);
431 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, bench->getName())) {
432 continue;
433 }
434 const int length = strlen(bench->getName());
435 longestName = length > longestName ? length : longestName;
436 }
437
438 // Run each bench in each configuration it supports and we asked for.
439 Iter iter;
reed@android.combd700c32009-01-05 03:34:50 +0000440 while ((bench = iter.next()) != NULL) {
bsalomon@google.com7fbc6042012-08-13 22:10:05 +0000441 SkAutoTUnref<SkBenchmark> benchUnref(bench);
mtklein@google.comc2897432013-09-10 19:23:38 +0000442 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, bench->getName())) {
reed@android.comb398fe82009-01-07 11:47:57 +0000443 continue;
444 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000445
mtklein@google.comc2897432013-09-10 19:23:38 +0000446 bench->setForceAlpha(alpha);
447 bench->setForceAA(FLAGS_forceAA);
448 bench->setForceFilter(FLAGS_forceFilter);
449 bench->setDither(dither);
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000450 AutoPrePostDraw appd(bench);
451
mtklein@google.comc2897432013-09-10 19:23:38 +0000452 bool loggedBenchName = false;
453 for (int i = 0; i < configs.count(); ++i) {
454 const int configIndex = configs[i];
455 const Config& config = gConfigs[configIndex];
tomhudson@google.com13eaaaa2012-04-16 18:00:40 +0000456
mtklein@google.comc2897432013-09-10 19:23:38 +0000457 if ((kNonRendering_Backend == config.backend) == bench->isRendering()) {
458 continue;
bsalomon@google.com604a56a2013-03-15 15:42:15 +0000459 }
460
bsalomon@google.comcb265352013-02-22 16:13:16 +0000461 GrContext* context = NULL;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000462#if SK_SUPPORT_GPU
sugoi@google.com9c55f802013-03-07 20:52:59 +0000463 SkGLContextHelper* glContext = NULL;
mtklein@google.comc2897432013-09-10 19:23:38 +0000464 if (kGPU_Backend == config.backend) {
465 context = gContextFactory.get(config.contextType);
bsalomon@google.comcb265352013-02-22 16:13:16 +0000466 if (NULL == context) {
467 continue;
468 }
mtklein@google.comc2897432013-09-10 19:23:38 +0000469 glContext = gContextFactory.getGLContext(config.contextType);
mike@reedtribe.orga9015f82011-05-17 02:25:05 +0000470 }
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000471#endif
mtklein@google.comc2897432013-09-10 19:23:38 +0000472 SkAutoTUnref<SkBaseDevice> device;
473 SkAutoTUnref<SkCanvas> canvas;
474 SkPicture recordFrom, recordTo;
475 const SkIPoint dim = bench->getSize();
bsalomon@google.com604a56a2013-03-15 15:42:15 +0000476
mtklein@google.comc2897432013-09-10 19:23:38 +0000477 const SkPicture::RecordingFlags kRecordFlags =
478 SkPicture::kUsePathBoundsForClip_RecordingFlag;
479
480 if (kNonRendering_Backend != config.backend) {
481 device.reset(make_device(config.config,
482 dim,
483 config.backend,
484 config.sampleCount,
485 context));
486 if (!device.get()) {
commit-bot@chromium.org7495f592013-06-03 19:31:07 +0000487 SkString error;
mtklein@google.comc2897432013-09-10 19:23:38 +0000488 error.printf("Device creation failure for config %s. Will skip.\n", config.name);
489 logger.logError(error);
490 continue;
keyar@chromium.orgfdd909c2012-07-20 23:03:42 +0000491 }
junov@chromium.orgfb103892012-09-20 19:35:43 +0000492
mtklein@google.comc2897432013-09-10 19:23:38 +0000493 switch(benchMode) {
494 case kDeferredSilent_BenchMode:
495 case kDeferred_BenchMode:
496 canvas.reset(SkDeferredCanvas::Create(device.get()));
497 break;
498 case kRecord_BenchMode:
499 canvas.reset(SkRef(recordTo.beginRecording(dim.fX, dim.fY, kRecordFlags)));
500 break;
501 case kPictureRecord_BenchMode:
502 bench->draw(recordFrom.beginRecording(dim.fX, dim.fY, kRecordFlags));
503 recordFrom.endRecording();
504 canvas.reset(SkRef(recordTo.beginRecording(dim.fX, dim.fY, kRecordFlags)));
505 break;
506 case kNormal_BenchMode:
507 canvas.reset(new SkCanvas(device.get()));
508 break;
509 default:
510 SkASSERT(false);
511 }
512 }
513
514 if (NULL != canvas) {
515 canvas->clear(SK_ColorWHITE);
516 if (FLAGS_clip) { performClip(canvas, dim.fX, dim.fY); }
517 if (FLAGS_scale) { performScale(canvas, dim.fX, dim.fY); }
518 if (FLAGS_rotate) { performRotate(canvas, dim.fX, dim.fY); }
519 }
520
521 if (!loggedBenchName) {
522 loggedBenchName = true;
523 SkString str;
524 str.printf("running bench [%3d %3d] %*s ",
525 dim.fX, dim.fY, longestName, bench->getName());
526 logger.logProgress(str);
527 }
528
529#if SK_SUPPORT_GPU
530 SkGLContextHelper* contextHelper = NULL;
531 if (kGPU_Backend == config.backend) {
532 contextHelper = gContextFactory.getGLContext(config.contextType);
533 }
534 BenchTimer timer(contextHelper);
535#else
536 BenchTimer timer;
537#endif
538
539 bench->setLoops(0);
540 do {
541 // Ramp up 1 -> 4 -> 16 -> ... -> ~1 billion.
542 const int loops = bench->getLoops();
543 if (loops >= (1<<30)) {
544 // If you find it takes more than a billion loops to get up to 20ms of runtime,
545 // you've got a computer clocked at several THz or have a broken benchmark. ;)
546 // "1B ought to be enough for anybody."
commit-bot@chromium.org7495f592013-06-03 19:31:07 +0000547 SkString str;
mtklein@google.comc2897432013-09-10 19:23:38 +0000548 str.printf("Can't ramp %s to %dms.\n", bench->getName(), FLAGS_benchMs);
549 logger.logError(str);
550 break;
551 }
552 bench->setLoops(loops == 0 ? 1 : loops * 4);
553
554 if ((benchMode == kRecord_BenchMode || benchMode == kPictureRecord_BenchMode)) {
555 // Clear the recorded commands so that they do not accumulate.
556 canvas.reset(recordTo.beginRecording(dim.fX, dim.fY, kRecordFlags));
junov@chromium.orgfb103892012-09-20 19:35:43 +0000557 }
robertphillips@google.com91ee3a12012-08-28 12:18:40 +0000558
mtklein@google.comc2897432013-09-10 19:23:38 +0000559 timer.start();
560 if (NULL != canvas) {
561 canvas->save();
commit-bot@chromium.org7495f592013-06-03 19:31:07 +0000562 }
mtklein@google.comc2897432013-09-10 19:23:38 +0000563 if (benchMode == kPictureRecord_BenchMode) {
564 recordFrom.draw(canvas);
borenet@google.com8ad29ce2013-09-10 17:22:43 +0000565 } else {
mtklein@google.comc2897432013-09-10 19:23:38 +0000566 bench->draw(canvas);
commit-bot@chromium.org7495f592013-06-03 19:31:07 +0000567 }
borenet@google.com8ad29ce2013-09-10 17:22:43 +0000568
mtklein@google.comc2897432013-09-10 19:23:38 +0000569 if (kDeferredSilent_BenchMode == benchMode) {
570 static_cast<SkDeferredCanvas*>(canvas.get())->silentFlush();
571 } else if (NULL != canvas) {
572 canvas->flush();
573 }
574
575 if (NULL != canvas) {
576 canvas->restore();
577 }
578
579
580 // Stop truncated timers before GL calls complete, and stop the full timers after.
581 timer.truncatedEnd();
582#if SK_SUPPORT_GPU
583 if (NULL != glContext) {
584 context->flush();
585 SK_GL(*glContext, Finish());
586 }
587#endif
588 timer.end();
589 } while (!kIsDebug && timer.fWall < FLAGS_benchMs); // One loop only in debug mode.
590
591 if (FLAGS_outDir.count() && kNonRendering_Backend != config.backend) {
592 saveFile(bench->getName(),
593 config.name,
594 FLAGS_outDir[0],
595 device->accessBitmap(false));
596 }
597
598 if (kIsDebug) {
599 // Let's not mislead ourselves by looking at Debug build bench times!
600 continue;
601 }
602
603 // Normalize to ms per 1000 iterations.
604 const double normalize = 1000.0 / bench->getLoops();
605 const struct { char shortName; const char* longName; double ms; } times[] = {
606 {'w', "msecs", normalize * timer.fWall},
607 {'W', "Wmsecs", normalize * timer.fTruncatedWall},
608 {'c', "cmsecs", normalize * timer.fCpu},
609 {'C', "Cmsecs", normalize * timer.fTruncatedCpu},
610 {'g', "gmsecs", normalize * timer.fGpu},
611 };
612
613 SkString result;
614 result.appendf(" %s:", config.name);
615 for (size_t i = 0; i < SK_ARRAY_COUNT(times); i++) {
616 if (strchr(FLAGS_timers[0], times[i].shortName) && times[i].ms > 0) {
617 result.appendf(" %s = ", times[i].longName);
618 result.appendf(FLAGS_timeFormat[0], times[i].ms);
commit-bot@chromium.org7495f592013-06-03 19:31:07 +0000619 }
reed@google.com25df8882011-07-14 19:03:58 +0000620 }
mtklein@google.comc2897432013-09-10 19:23:38 +0000621 logger.logProgress(result);
bensong@google.com24ed8d72013-06-12 14:45:03 +0000622 }
mtklein@google.comc2897432013-09-10 19:23:38 +0000623 if (loggedBenchName) {
624 logger.logProgress("\n");
bsalomon@google.com604a56a2013-03-15 15:42:15 +0000625 }
reed@android.combd700c32009-01-05 03:34:50 +0000626 }
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000627#if SK_SUPPORT_GPU
bsalomon@google.comcb265352013-02-22 16:13:16 +0000628 gContextFactory.destroyContexts();
robertphillips@google.com9d594202012-09-13 14:05:00 +0000629#endif
reed@android.combd700c32009-01-05 03:34:50 +0000630 return 0;
631}
caryclark@google.com5987f582012-10-02 18:33:14 +0000632
borenet@google.com7158e6a2012-11-01 17:43:44 +0000633#if !defined(SK_BUILD_FOR_IOS) && !defined(SK_BUILD_FOR_NACL)
caryclark@google.com5987f582012-10-02 18:33:14 +0000634int main(int argc, char * const argv[]) {
635 return tool_main(argc, (char**) argv);
636}
637#endif