epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | /* |
| 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.com | 971d0c8 | 2011-08-19 17:22:05 +0000 | [diff] [blame] | 7 | |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 8 | #include "BenchTimer.h" |
commit-bot@chromium.org | e3bb3bc | 2013-12-03 18:16:48 +0000 | [diff] [blame] | 9 | #include "ResultsWriter.h" |
scroggo@google.com | 9a41252 | 2012-09-07 15:21:18 +0000 | [diff] [blame] | 10 | #include "SkBenchLogger.h" |
bsalomon@google.com | 971d0c8 | 2011-08-19 17:22:05 +0000 | [diff] [blame] | 11 | #include "SkBenchmark.h" |
robertphillips@google.com | 7367225 | 2013-08-29 12:40:26 +0000 | [diff] [blame] | 12 | #include "SkBitmapDevice.h" |
reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 13 | #include "SkCanvas.h" |
mtklein@google.com | 78d0379 | 2013-09-10 19:42:07 +0000 | [diff] [blame] | 14 | #include "SkColorPriv.h" |
sglez@google.com | 586db93 | 2013-07-24 17:24:23 +0000 | [diff] [blame] | 15 | #include "SkCommandLineFlags.h" |
reed@google.com | 1bc6c6a | 2014-02-04 14:02:44 +0000 | [diff] [blame] | 16 | #include "SkData.h" |
bsalomon@google.com | 82a7bfc | 2012-04-16 19:11:17 +0000 | [diff] [blame] | 17 | #include "SkDeferredCanvas.h" |
commit-bot@chromium.org | 6adce67 | 2014-02-03 14:48:17 +0000 | [diff] [blame] | 18 | #include "SkGMBench.h" |
reed@android.com | 3a859a0 | 2009-01-28 00:56:29 +0000 | [diff] [blame] | 19 | #include "SkGraphics.h" |
reed@android.com | b398fe8 | 2009-01-07 11:47:57 +0000 | [diff] [blame] | 20 | #include "SkImageEncoder.h" |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 21 | #include "SkOSFile.h" |
reed@android.com | 6c924ad | 2009-03-31 03:48:49 +0000 | [diff] [blame] | 22 | #include "SkPicture.h" |
reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 23 | #include "SkString.h" |
reed@google.com | 1bc6c6a | 2014-02-04 14:02:44 +0000 | [diff] [blame] | 24 | #include "SkSurface.h" |
reed@android.com | 29348cb | 2009-08-04 18:17:15 +0000 | [diff] [blame] | 25 | |
djsollen@google.com | db490e9 | 2013-11-20 13:15:40 +0000 | [diff] [blame] | 26 | #if SK_SUPPORT_GPU |
| 27 | #include "GrContext.h" |
| 28 | #include "GrContextFactory.h" |
| 29 | #include "GrRenderTarget.h" |
| 30 | #include "SkGpuDevice.h" |
| 31 | #include "gl/GrGLDefines.h" |
| 32 | #else |
| 33 | class GrContext; |
| 34 | #endif // SK_SUPPORT_GPU |
| 35 | |
mtklein@google.com | 9ef1d21 | 2013-09-13 20:39:50 +0000 | [diff] [blame] | 36 | #include <limits> |
| 37 | |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 38 | enum BenchMode { |
| 39 | kNormal_BenchMode, |
| 40 | kDeferred_BenchMode, |
| 41 | kDeferredSilent_BenchMode, |
| 42 | kRecord_BenchMode, |
| 43 | kPictureRecord_BenchMode |
keyar@chromium.org | fdd909c | 2012-07-20 23:03:42 +0000 | [diff] [blame] | 44 | }; |
mtklein@google.com | dbd41c8 | 2013-09-13 20:11:09 +0000 | [diff] [blame] | 45 | const char* BenchMode_Name[] = { |
| 46 | "normal", "deferred", "deferredSilent", "record", "picturerecord" |
| 47 | }; |
commit-bot@chromium.org | 515dcd3 | 2013-08-28 14:17:03 +0000 | [diff] [blame] | 48 | |
borenet@google.com | 4e061d3 | 2013-09-16 19:12:19 +0000 | [diff] [blame] | 49 | static const char kDefaultsConfigStr[] = "defaults"; |
| 50 | |
keyar@chromium.org | fdd909c | 2012-07-20 23:03:42 +0000 | [diff] [blame] | 51 | /////////////////////////////////////////////////////////////////////////////// |
| 52 | |
reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 53 | class Iter { |
| 54 | public: |
commit-bot@chromium.org | 575d9cd | 2014-02-24 23:25:49 +0000 | [diff] [blame] | 55 | Iter() : fBenches(BenchRegistry::Head()), fGMs(skiagm::GMRegistry::Head()) {} |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 56 | |
reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 57 | SkBenchmark* next() { |
commit-bot@chromium.org | 575d9cd | 2014-02-24 23:25:49 +0000 | [diff] [blame] | 58 | if (fBenches) { |
| 59 | BenchRegistry::Factory f = fBenches->factory(); |
| 60 | fBenches = fBenches->next(); |
commit-bot@chromium.org | 38aeb0f | 2014-02-26 23:01:57 +0000 | [diff] [blame] | 61 | return (*f)(NULL); |
reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 62 | } |
commit-bot@chromium.org | 575d9cd | 2014-02-24 23:25:49 +0000 | [diff] [blame] | 63 | |
| 64 | while (fGMs) { |
| 65 | SkAutoTDelete<skiagm::GM> gm(fGMs->factory()(NULL)); |
| 66 | fGMs = fGMs->next(); |
| 67 | if (gm->getFlags() & skiagm::GM::kAsBench_Flag) { |
| 68 | return SkNEW_ARGS(SkGMBench, (gm.detach())); |
| 69 | } |
| 70 | } |
| 71 | |
reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 72 | return NULL; |
| 73 | } |
bungeman@google.com | d1a416a | 2011-05-18 18:37:07 +0000 | [diff] [blame] | 74 | |
reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 75 | private: |
commit-bot@chromium.org | 575d9cd | 2014-02-24 23:25:49 +0000 | [diff] [blame] | 76 | const BenchRegistry* fBenches; |
| 77 | const skiagm::GMRegistry* fGMs; |
reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 78 | }; |
| 79 | |
bsalomon@google.com | 30e6d2c | 2012-08-13 14:03:31 +0000 | [diff] [blame] | 80 | class AutoPrePostDraw { |
| 81 | public: |
| 82 | AutoPrePostDraw(SkBenchmark* bench) : fBench(bench) { |
| 83 | fBench->preDraw(); |
| 84 | } |
| 85 | ~AutoPrePostDraw() { |
| 86 | fBench->postDraw(); |
| 87 | } |
| 88 | private: |
| 89 | SkBenchmark* fBench; |
| 90 | }; |
| 91 | |
reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 92 | static void make_filename(const char name[], SkString* path) { |
| 93 | path->set(name); |
| 94 | for (int i = 0; name[i]; i++) { |
| 95 | switch (name[i]) { |
| 96 | case '/': |
| 97 | case '\\': |
| 98 | case ' ': |
| 99 | case ':': |
| 100 | path->writable_str()[i] = '-'; |
| 101 | break; |
| 102 | default: |
| 103 | break; |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | |
reed@android.com | 4c7d3d6 | 2009-01-21 03:15:13 +0000 | [diff] [blame] | 108 | static void saveFile(const char name[], const char config[], const char dir[], |
reed@google.com | 1bc6c6a | 2014-02-04 14:02:44 +0000 | [diff] [blame] | 109 | const SkImage* image) { |
| 110 | SkAutoTUnref<SkData> data(image->encode(SkImageEncoder::kPNG_Type, 100)); |
| 111 | if (NULL == data.get()) { |
reed@android.com | 4c7d3d6 | 2009-01-21 03:15:13 +0000 | [diff] [blame] | 112 | return; |
| 113 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 114 | |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 115 | SkString filename; |
| 116 | make_filename(name, &filename); |
| 117 | filename.appendf("_%s.png", config); |
| 118 | SkString path = SkOSPath::SkPathJoin(dir, filename.c_str()); |
| 119 | ::remove(path.c_str()); |
reed@google.com | 1bc6c6a | 2014-02-04 14:02:44 +0000 | [diff] [blame] | 120 | |
reed@google.com | ace1354 | 2014-02-06 22:00:58 +0000 | [diff] [blame] | 121 | SkFILEWStream stream(path.c_str()); |
reed@google.com | 1bc6c6a | 2014-02-04 14:02:44 +0000 | [diff] [blame] | 122 | stream.write(data->data(), data->size()); |
reed@android.com | 4c7d3d6 | 2009-01-21 03:15:13 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | static void performClip(SkCanvas* canvas, int w, int h) { |
| 126 | SkRect r; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 127 | |
reed@android.com | 4c7d3d6 | 2009-01-21 03:15:13 +0000 | [diff] [blame] | 128 | r.set(SkIntToScalar(10), SkIntToScalar(10), |
| 129 | SkIntToScalar(w*2/3), SkIntToScalar(h*2/3)); |
| 130 | canvas->clipRect(r, SkRegion::kIntersect_Op); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 131 | |
reed@android.com | 4c7d3d6 | 2009-01-21 03:15:13 +0000 | [diff] [blame] | 132 | r.set(SkIntToScalar(w/3), SkIntToScalar(h/3), |
| 133 | SkIntToScalar(w-10), SkIntToScalar(h-10)); |
| 134 | canvas->clipRect(r, SkRegion::kXOR_Op); |
| 135 | } |
| 136 | |
| 137 | static void performRotate(SkCanvas* canvas, int w, int h) { |
| 138 | const SkScalar x = SkIntToScalar(w) / 2; |
| 139 | const SkScalar y = SkIntToScalar(h) / 2; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 140 | |
reed@android.com | 4c7d3d6 | 2009-01-21 03:15:13 +0000 | [diff] [blame] | 141 | canvas->translate(x, y); |
| 142 | canvas->rotate(SkIntToScalar(35)); |
| 143 | canvas->translate(-x, -y); |
| 144 | } |
| 145 | |
reed@android.com | 387359e | 2009-08-04 01:51:09 +0000 | [diff] [blame] | 146 | static void performScale(SkCanvas* canvas, int w, int h) { |
| 147 | const SkScalar x = SkIntToScalar(w) / 2; |
| 148 | const SkScalar y = SkIntToScalar(h) / 2; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 149 | |
reed@android.com | 387359e | 2009-08-04 01:51:09 +0000 | [diff] [blame] | 150 | canvas->translate(x, y); |
| 151 | // just enough so we can't take the sprite case |
| 152 | canvas->scale(SK_Scalar1 * 99/100, SK_Scalar1 * 99/100); |
| 153 | canvas->translate(-x, -y); |
| 154 | } |
| 155 | |
reed@google.com | 1bc6c6a | 2014-02-04 14:02:44 +0000 | [diff] [blame] | 156 | static SkSurface* make_surface(SkColorType colorType, const SkIPoint& size, |
| 157 | SkBenchmark::Backend backend, int sampleCount, |
| 158 | GrContext* context) { |
| 159 | SkSurface* surface = NULL; |
| 160 | SkImageInfo info = SkImageInfo::Make(size.fX, size.fY, colorType, |
| 161 | kPremul_SkAlphaType); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 162 | |
mike@reedtribe.org | a9015f8 | 2011-05-17 02:25:05 +0000 | [diff] [blame] | 163 | switch (backend) { |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 164 | case SkBenchmark::kRaster_Backend: |
reed@google.com | 1bc6c6a | 2014-02-04 14:02:44 +0000 | [diff] [blame] | 165 | surface = SkSurface::NewRaster(info); |
| 166 | surface->getCanvas()->clear(SK_ColorWHITE); |
mike@reedtribe.org | a9015f8 | 2011-05-17 02:25:05 +0000 | [diff] [blame] | 167 | break; |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 168 | #if SK_SUPPORT_GPU |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 169 | case SkBenchmark::kGPU_Backend: { |
reed@google.com | 1bc6c6a | 2014-02-04 14:02:44 +0000 | [diff] [blame] | 170 | surface = SkSurface::NewRenderTarget(context, info, sampleCount); |
mike@reedtribe.org | a9015f8 | 2011-05-17 02:25:05 +0000 | [diff] [blame] | 171 | break; |
bsalomon@google.com | cb26535 | 2013-02-22 16:13:16 +0000 | [diff] [blame] | 172 | } |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 173 | #endif |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 174 | case SkBenchmark::kPDF_Backend: |
mike@reedtribe.org | a9015f8 | 2011-05-17 02:25:05 +0000 | [diff] [blame] | 175 | default: |
mtklein@google.com | 330313a | 2013-08-22 15:37:26 +0000 | [diff] [blame] | 176 | SkDEBUGFAIL("unsupported"); |
mike@reedtribe.org | a9015f8 | 2011-05-17 02:25:05 +0000 | [diff] [blame] | 177 | } |
reed@google.com | 1bc6c6a | 2014-02-04 14:02:44 +0000 | [diff] [blame] | 178 | return surface; |
mike@reedtribe.org | a9015f8 | 2011-05-17 02:25:05 +0000 | [diff] [blame] | 179 | } |
| 180 | |
bsalomon@google.com | cb26535 | 2013-02-22 16:13:16 +0000 | [diff] [blame] | 181 | #if SK_SUPPORT_GPU |
| 182 | GrContextFactory gContextFactory; |
| 183 | typedef GrContextFactory::GLContextType GLContextType; |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 184 | static const GLContextType kNative = GrContextFactory::kNative_GLContextType; |
commit-bot@chromium.org | e898957 | 2014-01-26 20:51:00 +0000 | [diff] [blame] | 185 | static const GLContextType kNVPR = GrContextFactory::kNVPR_GLContextType; |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 186 | #if SK_ANGLE |
| 187 | static const GLContextType kANGLE = GrContextFactory::kANGLE_GLContextType; |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 188 | #endif |
| 189 | static const GLContextType kDebug = GrContextFactory::kDebug_GLContextType; |
| 190 | static const GLContextType kNull = GrContextFactory::kNull_GLContextType; |
bsalomon@google.com | cb26535 | 2013-02-22 16:13:16 +0000 | [diff] [blame] | 191 | #else |
| 192 | typedef int GLContextType; |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 193 | static const GLContextType kNative = 0, kANGLE = 0, kDebug = 0, kNull = 0; |
bsalomon@google.com | cb26535 | 2013-02-22 16:13:16 +0000 | [diff] [blame] | 194 | #endif |
| 195 | |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 196 | #ifdef SK_DEBUG |
| 197 | static const bool kIsDebug = true; |
| 198 | #else |
| 199 | static const bool kIsDebug = false; |
| 200 | #endif |
| 201 | |
| 202 | static const struct Config { |
reed@google.com | 1bc6c6a | 2014-02-04 14:02:44 +0000 | [diff] [blame] | 203 | SkColorType fColorType; |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 204 | const char* name; |
| 205 | int sampleCount; |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 206 | SkBenchmark::Backend backend; |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 207 | GLContextType contextType; |
| 208 | bool runByDefault; |
reed@android.com | 4bc1983 | 2009-01-19 20:08:35 +0000 | [diff] [blame] | 209 | } gConfigs[] = { |
commit-bot@chromium.org | 757ebd2 | 2014-04-10 22:36:34 +0000 | [diff] [blame^] | 210 | { kPMColor_SkColorType, "NONRENDERING", 0, SkBenchmark::kNonRendering_Backend, kNative, true}, |
| 211 | { kPMColor_SkColorType, "8888", 0, SkBenchmark::kRaster_Backend, kNative, true}, |
reed@google.com | 1bc6c6a | 2014-02-04 14:02:44 +0000 | [diff] [blame] | 212 | { kRGB_565_SkColorType, "565", 0, SkBenchmark::kRaster_Backend, kNative, true}, |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 213 | #if SK_SUPPORT_GPU |
commit-bot@chromium.org | 757ebd2 | 2014-04-10 22:36:34 +0000 | [diff] [blame^] | 214 | { kPMColor_SkColorType, "GPU", 0, SkBenchmark::kGPU_Backend, kNative, true}, |
| 215 | { kPMColor_SkColorType, "MSAA4", 4, SkBenchmark::kGPU_Backend, kNative, false}, |
| 216 | { kPMColor_SkColorType, "MSAA16", 16, SkBenchmark::kGPU_Backend, kNative, false}, |
| 217 | { kPMColor_SkColorType, "NVPRMSAA4", 4, SkBenchmark::kGPU_Backend, kNVPR, true}, |
| 218 | { kPMColor_SkColorType, "NVPRMSAA16", 16, SkBenchmark::kGPU_Backend, kNVPR, false}, |
robertphillips@google.com | d3b9fbb | 2012-03-28 16:19:11 +0000 | [diff] [blame] | 219 | #if SK_ANGLE |
commit-bot@chromium.org | 757ebd2 | 2014-04-10 22:36:34 +0000 | [diff] [blame^] | 220 | { kPMColor_SkColorType, "ANGLE", 0, SkBenchmark::kGPU_Backend, kANGLE, true}, |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 221 | #endif // SK_ANGLE |
commit-bot@chromium.org | 757ebd2 | 2014-04-10 22:36:34 +0000 | [diff] [blame^] | 222 | { kPMColor_SkColorType, "Debug", 0, SkBenchmark::kGPU_Backend, kDebug, kIsDebug}, |
| 223 | { kPMColor_SkColorType, "NULLGPU", 0, SkBenchmark::kGPU_Backend, kNull, true}, |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 224 | #endif // SK_SUPPORT_GPU |
reed@android.com | 4bc1983 | 2009-01-19 20:08:35 +0000 | [diff] [blame] | 225 | }; |
| 226 | |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 227 | DEFINE_string(outDir, "", "If given, image of each bench will be put in outDir."); |
| 228 | DEFINE_string(timers, "cg", "Timers to display. " |
| 229 | "Options: w(all) W(all, truncated) c(pu) C(pu, truncated) g(pu)"); |
reed@android.com | 4c7d3d6 | 2009-01-21 03:15:13 +0000 | [diff] [blame] | 230 | |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 231 | DEFINE_bool(rotate, false, "Rotate canvas before bench run?"); |
| 232 | DEFINE_bool(scale, false, "Scale canvas before bench run?"); |
| 233 | DEFINE_bool(clip, false, "Clip canvas before bench run?"); |
bsalomon@google.com | 8a70eef | 2013-03-19 13:58:55 +0000 | [diff] [blame] | 234 | |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 235 | DEFINE_bool(forceAA, true, "Force anti-aliasing?"); |
| 236 | DEFINE_bool(forceFilter, false, "Force bitmap filtering?"); |
| 237 | DEFINE_string(forceDither, "default", "Force dithering: true, false, or default?"); |
| 238 | DEFINE_bool(forceBlend, false, "Force alpha blending?"); |
| 239 | |
| 240 | DEFINE_int32(gpuCacheBytes, -1, "GPU cache size limit in bytes. 0 to disable cache."); |
| 241 | DEFINE_int32(gpuCacheCount, -1, "GPU cache size limit in object count. 0 to disable cache."); |
| 242 | |
commit-bot@chromium.org | 6dda827 | 2014-01-23 17:21:19 +0000 | [diff] [blame] | 243 | DEFINE_bool2(leaks, l, false, "show leaked ref cnt'd objects."); |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 244 | DEFINE_string(match, "", "[~][^]substring[$] [...] of test name to run.\n" |
| 245 | "Multiple matches may be separated by spaces.\n" |
| 246 | "~ causes a matching test to always be skipped\n" |
| 247 | "^ requires the start of the test to match\n" |
| 248 | "$ requires the end of the test to match\n" |
| 249 | "^ and $ requires an exact match\n" |
| 250 | "If a test does not match any list entry,\n" |
| 251 | "it is skipped unless some list entry starts with ~\n"); |
| 252 | DEFINE_string(mode, "normal", |
| 253 | "normal: draw to a normal canvas;\n" |
| 254 | "deferred: draw to a deferred canvas;\n" |
| 255 | "deferredSilent: deferred with silent playback;\n" |
| 256 | "record: draw to an SkPicture;\n" |
| 257 | "picturerecord: draw from an SkPicture to an SkPicture.\n"); |
borenet@google.com | 4e061d3 | 2013-09-16 19:12:19 +0000 | [diff] [blame] | 258 | DEFINE_string(config, kDefaultsConfigStr, |
| 259 | "Run configs given. By default, runs the configs marked \"runByDefault\" in gConfigs."); |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 260 | DEFINE_string(logFile, "", "Also write stdout here."); |
mtklein@google.com | dbd41c8 | 2013-09-13 20:11:09 +0000 | [diff] [blame] | 261 | DEFINE_int32(minMs, 20, "Shortest time we'll allow a benchmark to run."); |
| 262 | DEFINE_int32(maxMs, 4000, "Longest time we'll allow a benchmark to run."); |
| 263 | DEFINE_double(error, 0.01, |
| 264 | "Ratio of subsequent bench measurements must drop within 1±error to converge."); |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 265 | DEFINE_string(timeFormat, "%9.2f", "Format to print results, in milliseconds per 1000 loops."); |
mtklein@google.com | dbd41c8 | 2013-09-13 20:11:09 +0000 | [diff] [blame] | 266 | DEFINE_bool2(verbose, v, false, "Print more."); |
tfarina@chromium.org | 725a64c | 2013-12-31 14:29:52 +0000 | [diff] [blame] | 267 | DEFINE_string2(resourcePath, i, "resources", "directory for test resources."); |
commit-bot@chromium.org | e3bb3bc | 2013-12-03 18:16:48 +0000 | [diff] [blame] | 268 | DEFINE_string(outResultsFile, "", "If given, the results will be written to the file in JSON format."); |
mtklein@google.com | dbd41c8 | 2013-09-13 20:11:09 +0000 | [diff] [blame] | 269 | |
commit-bot@chromium.org | 56b7a6d | 2014-03-13 16:22:00 +0000 | [diff] [blame] | 270 | DEFINE_bool(dryRun, false, "Don't actually run the tests, just print what would have been done."); |
| 271 | |
mtklein@google.com | dbd41c8 | 2013-09-13 20:11:09 +0000 | [diff] [blame] | 272 | // Has this bench converged? First arguments are milliseconds / loop iteration, |
| 273 | // last is overall runtime in milliseconds. |
| 274 | static bool HasConverged(double prevPerLoop, double currPerLoop, double currRaw) { |
| 275 | if (currRaw < FLAGS_minMs) { |
| 276 | return false; |
| 277 | } |
| 278 | const double low = 1 - FLAGS_error, high = 1 + FLAGS_error; |
| 279 | const double ratio = currPerLoop / prevPerLoop; |
| 280 | return low < ratio && ratio < high; |
| 281 | } |
tomhudson@google.com | 86bb9b7 | 2012-04-03 13:28:57 +0000 | [diff] [blame] | 282 | |
caryclark@google.com | 5987f58 | 2012-10-02 18:33:14 +0000 | [diff] [blame] | 283 | int tool_main(int argc, char** argv); |
| 284 | int tool_main(int argc, char** argv) { |
commit-bot@chromium.org | 6dda827 | 2014-01-23 17:21:19 +0000 | [diff] [blame] | 285 | SkCommandLineFlags::Parse(argc, argv); |
bsalomon@google.com | 4e23068 | 2013-01-15 20:37:04 +0000 | [diff] [blame] | 286 | #if SK_ENABLE_INST_COUNT |
commit-bot@chromium.org | 6dda827 | 2014-01-23 17:21:19 +0000 | [diff] [blame] | 287 | if (FLAGS_leaks) { |
| 288 | gPrintInstCount = true; |
| 289 | } |
bsalomon@google.com | 65a87cc | 2012-08-14 13:15:44 +0000 | [diff] [blame] | 290 | #endif |
reed@android.com | 3a859a0 | 2009-01-28 00:56:29 +0000 | [diff] [blame] | 291 | SkAutoGraphics ag; |
bsalomon@google.com | 65a87cc | 2012-08-14 13:15:44 +0000 | [diff] [blame] | 292 | |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 293 | // First, parse some flags. |
scroggo@google.com | 9a41252 | 2012-09-07 15:21:18 +0000 | [diff] [blame] | 294 | SkBenchLogger logger; |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 295 | if (FLAGS_logFile.count()) { |
| 296 | logger.SetLogFile(FLAGS_logFile[0]); |
| 297 | } |
scroggo@google.com | 9a41252 | 2012-09-07 15:21:18 +0000 | [diff] [blame] | 298 | |
commit-bot@chromium.org | e3bb3bc | 2013-12-03 18:16:48 +0000 | [diff] [blame] | 299 | LoggerResultsWriter logWriter(logger, FLAGS_timeFormat[0]); |
| 300 | MultiResultsWriter writer; |
| 301 | writer.add(&logWriter); |
| 302 | SkAutoTDelete<JSONResultsWriter> jsonWriter; |
| 303 | if (FLAGS_outResultsFile.count()) { |
| 304 | jsonWriter.reset(SkNEW(JSONResultsWriter(FLAGS_outResultsFile[0]))); |
| 305 | writer.add(jsonWriter.get()); |
| 306 | } |
| 307 | // Instantiate after all the writers have been added to writer so that we |
| 308 | // call close() before their destructors are called on the way out. |
| 309 | CallEnd<MultiResultsWriter> ender(writer); |
| 310 | |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 311 | const uint8_t alpha = FLAGS_forceBlend ? 0x80 : 0xFF; |
| 312 | SkTriState::State dither = SkTriState::kDefault; |
| 313 | for (size_t i = 0; i < 3; i++) { |
| 314 | if (strcmp(SkTriState::Name[i], FLAGS_forceDither[0]) == 0) { |
| 315 | dither = static_cast<SkTriState::State>(i); |
reed@android.com | b398fe8 | 2009-01-07 11:47:57 +0000 | [diff] [blame] | 316 | } |
| 317 | } |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 318 | |
| 319 | BenchMode benchMode = kNormal_BenchMode; |
| 320 | for (size_t i = 0; i < SK_ARRAY_COUNT(BenchMode_Name); i++) { |
| 321 | if (strcmp(FLAGS_mode[0], BenchMode_Name[i]) == 0) { |
| 322 | benchMode = static_cast<BenchMode>(i); |
| 323 | } |
keyar@chromium.org | fdd909c | 2012-07-20 23:03:42 +0000 | [diff] [blame] | 324 | } |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 325 | |
| 326 | SkTDArray<int> configs; |
borenet@google.com | 4e061d3 | 2013-09-16 19:12:19 +0000 | [diff] [blame] | 327 | bool runDefaultConfigs = false; |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 328 | // Try user-given configs first. |
| 329 | for (int i = 0; i < FLAGS_config.count(); i++) { |
robertphillips@google.com | e9cd27d | 2013-10-16 17:48:11 +0000 | [diff] [blame] | 330 | for (int j = 0; j < static_cast<int>(SK_ARRAY_COUNT(gConfigs)); ++j) { |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 331 | if (0 == strcmp(FLAGS_config[i], gConfigs[j].name)) { |
| 332 | *configs.append() = j; |
borenet@google.com | 4e061d3 | 2013-09-16 19:12:19 +0000 | [diff] [blame] | 333 | } else if (0 == strcmp(FLAGS_config[i], kDefaultsConfigStr)) { |
| 334 | runDefaultConfigs = true; |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 335 | } |
| 336 | } |
keyar@chromium.org | fdd909c | 2012-07-20 23:03:42 +0000 | [diff] [blame] | 337 | } |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 338 | // If there weren't any, fill in with defaults. |
borenet@google.com | 4e061d3 | 2013-09-16 19:12:19 +0000 | [diff] [blame] | 339 | if (runDefaultConfigs) { |
robertphillips@google.com | e9cd27d | 2013-10-16 17:48:11 +0000 | [diff] [blame] | 340 | for (int i = 0; i < static_cast<int>(SK_ARRAY_COUNT(gConfigs)); ++i) { |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 341 | if (gConfigs[i].runByDefault) { |
bsalomon@google.com | 8a70eef | 2013-03-19 13:58:55 +0000 | [diff] [blame] | 342 | *configs.append() = i; |
| 343 | } |
tomhudson@google.com | 13eaaaa | 2012-04-16 18:00:40 +0000 | [diff] [blame] | 344 | } |
| 345 | } |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 346 | // Filter out things we can't run. |
| 347 | if (kNormal_BenchMode != benchMode) { |
bsalomon@google.com | 604a56a | 2013-03-15 15:42:15 +0000 | [diff] [blame] | 348 | // Non-rendering configs only run in normal mode |
| 349 | for (int i = 0; i < configs.count(); ++i) { |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 350 | const Config& config = gConfigs[configs[i]]; |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 351 | if (SkBenchmark::kNonRendering_Backend == config.backend) { |
bsalomon@google.com | 604a56a | 2013-03-15 15:42:15 +0000 | [diff] [blame] | 352 | configs.remove(i, 1); |
| 353 | --i; |
| 354 | } |
| 355 | } |
| 356 | } |
scroggo@google.com | 111fd11 | 2013-09-25 21:42:12 +0000 | [diff] [blame] | 357 | // Set the resource path. |
| 358 | if (!FLAGS_resourcePath.isEmpty()) { |
| 359 | SkBenchmark::SetResourcePath(FLAGS_resourcePath[0]); |
| 360 | } |
| 361 | |
bsalomon@google.com | 8a70eef | 2013-03-19 13:58:55 +0000 | [diff] [blame] | 362 | #if SK_SUPPORT_GPU |
| 363 | for (int i = 0; i < configs.count(); ++i) { |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 364 | const Config& config = gConfigs[configs[i]]; |
bsalomon@google.com | 8a70eef | 2013-03-19 13:58:55 +0000 | [diff] [blame] | 365 | |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 366 | if (SkBenchmark::kGPU_Backend == config.backend) { |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 367 | GrContext* context = gContextFactory.get(config.contextType); |
bsalomon@google.com | 8a70eef | 2013-03-19 13:58:55 +0000 | [diff] [blame] | 368 | if (NULL == context) { |
mtklein@google.com | dbd41c8 | 2013-09-13 20:11:09 +0000 | [diff] [blame] | 369 | logger.logError(SkStringPrintf( |
| 370 | "Error creating GrContext for config %s. Config will be skipped.\n", |
| 371 | config.name)); |
bsalomon@google.com | 8a70eef | 2013-03-19 13:58:55 +0000 | [diff] [blame] | 372 | configs.remove(i); |
| 373 | --i; |
| 374 | continue; |
| 375 | } |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 376 | if (config.sampleCount > context->getMaxSampleCount()){ |
mtklein@google.com | dbd41c8 | 2013-09-13 20:11:09 +0000 | [diff] [blame] | 377 | logger.logError(SkStringPrintf( |
| 378 | "Sample count (%d) for config %s is unsupported. Config will be skipped.\n", |
| 379 | config.sampleCount, config.name)); |
bsalomon@google.com | 8a70eef | 2013-03-19 13:58:55 +0000 | [diff] [blame] | 380 | configs.remove(i); |
| 381 | --i; |
| 382 | continue; |
| 383 | } |
| 384 | } |
| 385 | } |
| 386 | #endif |
| 387 | |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 388 | // All flags should be parsed now. Report our settings. |
| 389 | if (kIsDebug) { |
| 390 | logger.logError("bench was built in Debug mode, so we're going to hide the times." |
| 391 | " It's for your own good!\n"); |
| 392 | } |
commit-bot@chromium.org | e3bb3bc | 2013-12-03 18:16:48 +0000 | [diff] [blame] | 393 | writer.option("mode", FLAGS_mode[0]); |
| 394 | writer.option("alpha", SkStringPrintf("0x%02X", alpha).c_str()); |
| 395 | writer.option("antialias", SkStringPrintf("%d", FLAGS_forceAA).c_str()); |
| 396 | writer.option("filter", SkStringPrintf("%d", FLAGS_forceFilter).c_str()); |
| 397 | writer.option("dither", SkTriState::Name[dither]); |
| 398 | |
| 399 | writer.option("rotate", SkStringPrintf("%d", FLAGS_rotate).c_str()); |
| 400 | writer.option("scale", SkStringPrintf("%d", FLAGS_scale).c_str()); |
| 401 | writer.option("clip", SkStringPrintf("%d", FLAGS_clip).c_str()); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 402 | |
bungeman@google.com | a5d4841 | 2011-06-15 17:25:46 +0000 | [diff] [blame] | 403 | #if defined(SK_BUILD_FOR_WIN32) |
commit-bot@chromium.org | e3bb3bc | 2013-12-03 18:16:48 +0000 | [diff] [blame] | 404 | writer.option("system", "WIN32"); |
bungeman@google.com | a5d4841 | 2011-06-15 17:25:46 +0000 | [diff] [blame] | 405 | #elif defined(SK_BUILD_FOR_MAC) |
commit-bot@chromium.org | e3bb3bc | 2013-12-03 18:16:48 +0000 | [diff] [blame] | 406 | writer.option("system", "MAC"); |
bungeman@google.com | a5d4841 | 2011-06-15 17:25:46 +0000 | [diff] [blame] | 407 | #elif defined(SK_BUILD_FOR_ANDROID) |
commit-bot@chromium.org | e3bb3bc | 2013-12-03 18:16:48 +0000 | [diff] [blame] | 408 | writer.option("system", "ANDROID"); |
bungeman@google.com | a5d4841 | 2011-06-15 17:25:46 +0000 | [diff] [blame] | 409 | #elif defined(SK_BUILD_FOR_UNIX) |
commit-bot@chromium.org | e3bb3bc | 2013-12-03 18:16:48 +0000 | [diff] [blame] | 410 | writer.option("system", "UNIX"); |
bungeman@google.com | a5d4841 | 2011-06-15 17:25:46 +0000 | [diff] [blame] | 411 | #else |
commit-bot@chromium.org | e3bb3bc | 2013-12-03 18:16:48 +0000 | [diff] [blame] | 412 | writer.option("system", "other"); |
bungeman@google.com | a5d4841 | 2011-06-15 17:25:46 +0000 | [diff] [blame] | 413 | #endif |
| 414 | |
| 415 | #if defined(SK_DEBUG) |
commit-bot@chromium.org | e3bb3bc | 2013-12-03 18:16:48 +0000 | [diff] [blame] | 416 | writer.option("build", "DEBUG"); |
| 417 | #else |
| 418 | writer.option("build", "RELEASE"); |
bungeman@google.com | a5d4841 | 2011-06-15 17:25:46 +0000 | [diff] [blame] | 419 | #endif |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 420 | |
| 421 | // Set texture cache limits if non-default. |
bsalomon@google.com | 5c90e29 | 2013-02-22 19:17:13 +0000 | [diff] [blame] | 422 | for (size_t i = 0; i < SK_ARRAY_COUNT(gConfigs); ++i) { |
bsalomon@google.com | cb26535 | 2013-02-22 16:13:16 +0000 | [diff] [blame] | 423 | #if SK_SUPPORT_GPU |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 424 | const Config& config = gConfigs[i]; |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 425 | if (SkBenchmark::kGPU_Backend != config.backend) { |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 426 | continue; |
bsalomon@google.com | cb26535 | 2013-02-22 16:13:16 +0000 | [diff] [blame] | 427 | } |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 428 | GrContext* context = gContextFactory.get(config.contextType); |
| 429 | if (NULL == context) { |
| 430 | continue; |
| 431 | } |
| 432 | |
| 433 | size_t bytes; |
| 434 | int count; |
| 435 | context->getTextureCacheLimits(&count, &bytes); |
| 436 | if (-1 != FLAGS_gpuCacheBytes) { |
| 437 | bytes = static_cast<size_t>(FLAGS_gpuCacheBytes); |
| 438 | } |
| 439 | if (-1 != FLAGS_gpuCacheCount) { |
| 440 | count = FLAGS_gpuCacheCount; |
| 441 | } |
| 442 | context->setTextureCacheLimits(count, bytes); |
bsalomon@google.com | cb26535 | 2013-02-22 16:13:16 +0000 | [diff] [blame] | 443 | #endif |
| 444 | } |
bsalomon@google.com | 7491372 | 2011-10-27 20:44:19 +0000 | [diff] [blame] | 445 | |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 446 | // Run each bench in each configuration it supports and we asked for. |
| 447 | Iter iter; |
commit-bot@chromium.org | e3bb3bc | 2013-12-03 18:16:48 +0000 | [diff] [blame] | 448 | SkBenchmark* bench; |
reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 449 | while ((bench = iter.next()) != NULL) { |
bsalomon@google.com | 7fbc604 | 2012-08-13 22:10:05 +0000 | [diff] [blame] | 450 | SkAutoTUnref<SkBenchmark> benchUnref(bench); |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 451 | if (SkCommandLineFlags::ShouldSkip(FLAGS_match, bench->getName())) { |
reed@android.com | b398fe8 | 2009-01-07 11:47:57 +0000 | [diff] [blame] | 452 | continue; |
| 453 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 454 | |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 455 | bench->setForceAlpha(alpha); |
| 456 | bench->setForceAA(FLAGS_forceAA); |
| 457 | bench->setForceFilter(FLAGS_forceFilter); |
| 458 | bench->setDither(dither); |
bsalomon@google.com | 30e6d2c | 2012-08-13 14:03:31 +0000 | [diff] [blame] | 459 | AutoPrePostDraw appd(bench); |
| 460 | |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 461 | bool loggedBenchName = false; |
| 462 | for (int i = 0; i < configs.count(); ++i) { |
| 463 | const int configIndex = configs[i]; |
| 464 | const Config& config = gConfigs[configIndex]; |
tomhudson@google.com | 13eaaaa | 2012-04-16 18:00:40 +0000 | [diff] [blame] | 465 | |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 466 | if (!bench->isSuitableFor(config.backend)) { |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 467 | continue; |
bsalomon@google.com | 604a56a | 2013-03-15 15:42:15 +0000 | [diff] [blame] | 468 | } |
| 469 | |
bsalomon@google.com | cb26535 | 2013-02-22 16:13:16 +0000 | [diff] [blame] | 470 | GrContext* context = NULL; |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 471 | #if SK_SUPPORT_GPU |
sugoi@google.com | 9c55f80 | 2013-03-07 20:52:59 +0000 | [diff] [blame] | 472 | SkGLContextHelper* glContext = NULL; |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 473 | if (SkBenchmark::kGPU_Backend == config.backend) { |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 474 | context = gContextFactory.get(config.contextType); |
bsalomon@google.com | cb26535 | 2013-02-22 16:13:16 +0000 | [diff] [blame] | 475 | if (NULL == context) { |
| 476 | continue; |
| 477 | } |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 478 | glContext = gContextFactory.getGLContext(config.contextType); |
mike@reedtribe.org | a9015f8 | 2011-05-17 02:25:05 +0000 | [diff] [blame] | 479 | } |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 480 | #endif |
reed@google.com | 1bc6c6a | 2014-02-04 14:02:44 +0000 | [diff] [blame] | 481 | |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 482 | SkAutoTUnref<SkCanvas> canvas; |
| 483 | SkPicture recordFrom, recordTo; |
| 484 | const SkIPoint dim = bench->getSize(); |
bsalomon@google.com | 604a56a | 2013-03-15 15:42:15 +0000 | [diff] [blame] | 485 | |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 486 | const SkPicture::RecordingFlags kRecordFlags = |
| 487 | SkPicture::kUsePathBoundsForClip_RecordingFlag; |
| 488 | |
reed@google.com | 1bc6c6a | 2014-02-04 14:02:44 +0000 | [diff] [blame] | 489 | SkAutoTUnref<SkSurface> surface; |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 490 | if (SkBenchmark::kNonRendering_Backend != config.backend) { |
reed@google.com | 1bc6c6a | 2014-02-04 14:02:44 +0000 | [diff] [blame] | 491 | surface.reset(make_surface(config.fColorType, |
| 492 | dim, |
| 493 | config.backend, |
| 494 | config.sampleCount, |
| 495 | context)); |
| 496 | if (!surface.get()) { |
mtklein@google.com | dbd41c8 | 2013-09-13 20:11:09 +0000 | [diff] [blame] | 497 | logger.logError(SkStringPrintf( |
| 498 | "Device creation failure for config %s. Will skip.\n", config.name)); |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 499 | continue; |
keyar@chromium.org | fdd909c | 2012-07-20 23:03:42 +0000 | [diff] [blame] | 500 | } |
junov@chromium.org | fb10389 | 2012-09-20 19:35:43 +0000 | [diff] [blame] | 501 | |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 502 | switch(benchMode) { |
| 503 | case kDeferredSilent_BenchMode: |
| 504 | case kDeferred_BenchMode: |
reed@google.com | 1bc6c6a | 2014-02-04 14:02:44 +0000 | [diff] [blame] | 505 | canvas.reset(SkDeferredCanvas::Create(surface.get())); |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 506 | break; |
| 507 | case kRecord_BenchMode: |
| 508 | canvas.reset(SkRef(recordTo.beginRecording(dim.fX, dim.fY, kRecordFlags))); |
| 509 | break; |
| 510 | case kPictureRecord_BenchMode: |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 511 | bench->draw(1, recordFrom.beginRecording(dim.fX, dim.fY, kRecordFlags)); |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 512 | recordFrom.endRecording(); |
| 513 | canvas.reset(SkRef(recordTo.beginRecording(dim.fX, dim.fY, kRecordFlags))); |
| 514 | break; |
| 515 | case kNormal_BenchMode: |
reed@google.com | 1bc6c6a | 2014-02-04 14:02:44 +0000 | [diff] [blame] | 516 | canvas.reset(SkRef(surface->getCanvas())); |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 517 | break; |
| 518 | default: |
| 519 | SkASSERT(false); |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | if (NULL != canvas) { |
| 524 | canvas->clear(SK_ColorWHITE); |
| 525 | if (FLAGS_clip) { performClip(canvas, dim.fX, dim.fY); } |
| 526 | if (FLAGS_scale) { performScale(canvas, dim.fX, dim.fY); } |
| 527 | if (FLAGS_rotate) { performRotate(canvas, dim.fX, dim.fY); } |
| 528 | } |
| 529 | |
| 530 | if (!loggedBenchName) { |
| 531 | loggedBenchName = true; |
commit-bot@chromium.org | e3bb3bc | 2013-12-03 18:16:48 +0000 | [diff] [blame] | 532 | writer.bench(bench->getName(), dim.fX, dim.fY); |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 533 | } |
| 534 | |
| 535 | #if SK_SUPPORT_GPU |
| 536 | SkGLContextHelper* contextHelper = NULL; |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 537 | if (SkBenchmark::kGPU_Backend == config.backend) { |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 538 | contextHelper = gContextFactory.getGLContext(config.contextType); |
| 539 | } |
| 540 | BenchTimer timer(contextHelper); |
| 541 | #else |
| 542 | BenchTimer timer; |
| 543 | #endif |
| 544 | |
mtklein@google.com | 9ef1d21 | 2013-09-13 20:39:50 +0000 | [diff] [blame] | 545 | double previous = std::numeric_limits<double>::infinity(); |
mtklein@google.com | dbd41c8 | 2013-09-13 20:11:09 +0000 | [diff] [blame] | 546 | bool converged = false; |
djsollen@google.com | 70de4da | 2013-10-10 17:33:39 +0000 | [diff] [blame] | 547 | |
| 548 | // variables used to compute loopsPerFrame |
| 549 | double frameIntervalTime = 0.0f; |
| 550 | int frameIntervalTotalLoops = 0; |
| 551 | |
| 552 | bool frameIntervalComputed = false; |
| 553 | int loopsPerFrame = 0; |
| 554 | int loopsPerIter = 0; |
mtklein@google.com | dbd41c8 | 2013-09-13 20:11:09 +0000 | [diff] [blame] | 555 | if (FLAGS_verbose) { SkDebugf("%s %s: ", bench->getName(), config.name); } |
commit-bot@chromium.org | 56b7a6d | 2014-03-13 16:22:00 +0000 | [diff] [blame] | 556 | if (!FLAGS_dryRun) { |
| 557 | do { |
| 558 | // Ramp up 1 -> 2 -> 4 -> 8 -> 16 -> ... -> ~1 billion. |
| 559 | loopsPerIter = (loopsPerIter == 0) ? 1 : loopsPerIter * 2; |
| 560 | if (loopsPerIter >= (1<<30) || timer.fWall > FLAGS_maxMs) { |
| 561 | // If you find it takes more than a billion loops to get up to 20ms of runtime, |
| 562 | // you've got a computer clocked at several THz or have a broken benchmark. ;) |
| 563 | // "1B ought to be enough for anybody." |
| 564 | logger.logError(SkStringPrintf( |
| 565 | "\nCan't get %s %s to converge in %dms (%d loops)", |
| 566 | bench->getName(), config.name, FLAGS_maxMs, loopsPerIter)); |
| 567 | break; |
djsollen@google.com | 70de4da | 2013-10-10 17:33:39 +0000 | [diff] [blame] | 568 | } |
| 569 | |
commit-bot@chromium.org | 56b7a6d | 2014-03-13 16:22:00 +0000 | [diff] [blame] | 570 | if ((benchMode == kRecord_BenchMode || benchMode == kPictureRecord_BenchMode)) { |
| 571 | // Clear the recorded commands so that they do not accumulate. |
| 572 | canvas.reset(SkRef(recordTo.beginRecording(dim.fX, dim.fY, kRecordFlags))); |
djsollen@google.com | 70de4da | 2013-10-10 17:33:39 +0000 | [diff] [blame] | 573 | } |
| 574 | |
commit-bot@chromium.org | 56b7a6d | 2014-03-13 16:22:00 +0000 | [diff] [blame] | 575 | timer.start(); |
| 576 | // Inner loop that allows us to break the run into smaller |
| 577 | // chunks (e.g. frames). This is especially useful for the GPU |
| 578 | // as we can flush and/or swap buffers to keep the GPU from |
| 579 | // queuing up too much work. |
| 580 | for (int loopCount = loopsPerIter; loopCount > 0; ) { |
| 581 | // Save and restore around each call to draw() to guarantee a pristine canvas. |
| 582 | SkAutoCanvasRestore saveRestore(canvas, true/*also save*/); |
djsollen@google.com | 70de4da | 2013-10-10 17:33:39 +0000 | [diff] [blame] | 583 | |
commit-bot@chromium.org | 56b7a6d | 2014-03-13 16:22:00 +0000 | [diff] [blame] | 584 | int loops; |
| 585 | if (frameIntervalComputed && loopCount > loopsPerFrame) { |
| 586 | loops = loopsPerFrame; |
| 587 | loopCount -= loopsPerFrame; |
| 588 | } else { |
| 589 | loops = loopCount; |
| 590 | loopCount = 0; |
djsollen@google.com | 70de4da | 2013-10-10 17:33:39 +0000 | [diff] [blame] | 591 | } |
djsollen@google.com | dcfed6c | 2013-10-10 18:48:27 +0000 | [diff] [blame] | 592 | |
commit-bot@chromium.org | 56b7a6d | 2014-03-13 16:22:00 +0000 | [diff] [blame] | 593 | if (benchMode == kPictureRecord_BenchMode) { |
| 594 | recordFrom.draw(canvas); |
| 595 | } else { |
| 596 | bench->draw(loops, canvas); |
| 597 | } |
| 598 | |
| 599 | if (kDeferredSilent_BenchMode == benchMode) { |
| 600 | static_cast<SkDeferredCanvas*>(canvas.get())->silentFlush(); |
| 601 | } else if (NULL != canvas) { |
| 602 | canvas->flush(); |
| 603 | } |
| 604 | |
| 605 | #if SK_SUPPORT_GPU |
| 606 | // swap drawing buffers on each frame to prevent the GPU |
| 607 | // from queuing up too much work |
| 608 | if (NULL != glContext) { |
| 609 | glContext->swapBuffers(); |
| 610 | } |
| 611 | #endif |
| 612 | } |
| 613 | |
| 614 | |
| 615 | |
| 616 | // Stop truncated timers before GL calls complete, and stop the full timers after. |
| 617 | timer.truncatedEnd(); |
| 618 | #if SK_SUPPORT_GPU |
| 619 | if (NULL != glContext) { |
| 620 | context->flush(); |
| 621 | SK_GL(*glContext, Finish()); |
| 622 | } |
| 623 | #endif |
| 624 | timer.end(); |
| 625 | |
| 626 | // setup the frame interval for subsequent iterations |
| 627 | if (!frameIntervalComputed) { |
| 628 | frameIntervalTime += timer.fWall; |
| 629 | frameIntervalTotalLoops += loopsPerIter; |
| 630 | if (frameIntervalTime >= FLAGS_minMs) { |
| 631 | frameIntervalComputed = true; |
| 632 | loopsPerFrame = |
| 633 | (int)(((double)frameIntervalTotalLoops / frameIntervalTime) * FLAGS_minMs); |
| 634 | if (loopsPerFrame < 1) { |
| 635 | loopsPerFrame = 1; |
| 636 | } |
| 637 | // SkDebugf(" %s has %d loops in %f ms (normalized to %d)\n", |
| 638 | // bench->getName(), frameIntervalTotalLoops, |
| 639 | // timer.fWall, loopsPerFrame); |
| 640 | } |
| 641 | } |
| 642 | |
| 643 | const double current = timer.fWall / loopsPerIter; |
| 644 | if (FLAGS_verbose && current > previous) { SkDebugf("↑"); } |
| 645 | if (FLAGS_verbose) { SkDebugf("%.3g ", current); } |
| 646 | converged = HasConverged(previous, current, timer.fWall); |
| 647 | previous = current; |
| 648 | } while (!kIsDebug && !converged); |
| 649 | } |
mtklein@google.com | dbd41c8 | 2013-09-13 20:11:09 +0000 | [diff] [blame] | 650 | if (FLAGS_verbose) { SkDebugf("\n"); } |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 651 | |
commit-bot@chromium.org | 56b7a6d | 2014-03-13 16:22:00 +0000 | [diff] [blame] | 652 | if (!FLAGS_dryRun && FLAGS_outDir.count() && SkBenchmark::kNonRendering_Backend != config.backend) { |
reed@google.com | 1bc6c6a | 2014-02-04 14:02:44 +0000 | [diff] [blame] | 653 | SkAutoTUnref<SkImage> image(surface->newImageSnapshot()); |
| 654 | if (image.get()) { |
| 655 | saveFile(bench->getName(), config.name, FLAGS_outDir[0], |
| 656 | image); |
| 657 | } |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 658 | } |
| 659 | |
| 660 | if (kIsDebug) { |
| 661 | // Let's not mislead ourselves by looking at Debug build bench times! |
| 662 | continue; |
| 663 | } |
| 664 | |
| 665 | // Normalize to ms per 1000 iterations. |
djsollen@google.com | 70de4da | 2013-10-10 17:33:39 +0000 | [diff] [blame] | 666 | const double normalize = 1000.0 / loopsPerIter; |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 667 | const struct { char shortName; const char* longName; double ms; } times[] = { |
| 668 | {'w', "msecs", normalize * timer.fWall}, |
| 669 | {'W', "Wmsecs", normalize * timer.fTruncatedWall}, |
| 670 | {'c', "cmsecs", normalize * timer.fCpu}, |
| 671 | {'C', "Cmsecs", normalize * timer.fTruncatedCpu}, |
| 672 | {'g', "gmsecs", normalize * timer.fGpu}, |
| 673 | }; |
| 674 | |
commit-bot@chromium.org | e3bb3bc | 2013-12-03 18:16:48 +0000 | [diff] [blame] | 675 | writer.config(config.name); |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 676 | for (size_t i = 0; i < SK_ARRAY_COUNT(times); i++) { |
| 677 | if (strchr(FLAGS_timers[0], times[i].shortName) && times[i].ms > 0) { |
commit-bot@chromium.org | e3bb3bc | 2013-12-03 18:16:48 +0000 | [diff] [blame] | 678 | writer.timer(times[i].longName, times[i].ms); |
commit-bot@chromium.org | 7495f59 | 2013-06-03 19:31:07 +0000 | [diff] [blame] | 679 | } |
reed@google.com | 25df888 | 2011-07-14 19:03:58 +0000 | [diff] [blame] | 680 | } |
bsalomon@google.com | 604a56a | 2013-03-15 15:42:15 +0000 | [diff] [blame] | 681 | } |
reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 682 | } |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 683 | #if SK_SUPPORT_GPU |
bsalomon@google.com | cb26535 | 2013-02-22 16:13:16 +0000 | [diff] [blame] | 684 | gContextFactory.destroyContexts(); |
robertphillips@google.com | 9d59420 | 2012-09-13 14:05:00 +0000 | [diff] [blame] | 685 | #endif |
reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 686 | return 0; |
| 687 | } |
caryclark@google.com | 5987f58 | 2012-10-02 18:33:14 +0000 | [diff] [blame] | 688 | |
borenet@google.com | 7158e6a | 2012-11-01 17:43:44 +0000 | [diff] [blame] | 689 | #if !defined(SK_BUILD_FOR_IOS) && !defined(SK_BUILD_FOR_NACL) |
caryclark@google.com | 5987f58 | 2012-10-02 18:33:14 +0000 | [diff] [blame] | 690 | int main(int argc, char * const argv[]) { |
| 691 | return tool_main(argc, (char**) argv); |
| 692 | } |
| 693 | #endif |