joshualitt | da7b843 | 2015-05-27 09:19:03 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 | * |
| 7 | */ |
| 8 | |
| 9 | #include "VisualBench.h" |
| 10 | |
joshualitt | 7fe8ee4 | 2015-06-01 10:03:54 -0700 | [diff] [blame] | 11 | #include "ProcStats.h" |
joshualitt | da7b843 | 2015-05-27 09:19:03 -0700 | [diff] [blame] | 12 | #include "SkApplication.h" |
| 13 | #include "SkCanvas.h" |
| 14 | #include "SkCommandLineFlags.h" |
| 15 | #include "SkCommonFlags.h" |
| 16 | #include "SkForceLinking.h" |
| 17 | #include "SkGraphics.h" |
| 18 | #include "SkGr.h" |
| 19 | #include "SkImageDecoder.h" |
| 20 | #include "SkOSFile.h" |
| 21 | #include "SkStream.h" |
joshualitt | 7fe8ee4 | 2015-06-01 10:03:54 -0700 | [diff] [blame] | 22 | #include "Stats.h" |
joshualitt | da7b843 | 2015-05-27 09:19:03 -0700 | [diff] [blame] | 23 | #include "gl/GrGLInterface.h" |
| 24 | |
| 25 | __SK_FORCE_IMAGE_DECODER_LINKING; |
| 26 | |
joshualitt | 7fe8ee4 | 2015-06-01 10:03:54 -0700 | [diff] [blame] | 27 | DEFINE_int32(gpuFrameLag, 5, "Overestimate of maximum number of frames GPU allows to lag."); |
| 28 | DEFINE_int32(samples, 10, "Number of times to render each skp."); |
| 29 | DEFINE_int32(loops, 5, "Number of times to time."); |
| 30 | DEFINE_int32(msaa, 0, "Number of msaa samples."); |
| 31 | |
| 32 | static SkString humanize(double ms) { |
| 33 | if (FLAGS_verbose) { |
| 34 | return SkStringPrintf("%llu", (uint64_t)(ms*1e6)); |
| 35 | } |
| 36 | return HumanizeMs(ms); |
| 37 | } |
| 38 | |
| 39 | #define HUMANIZE(time) humanize(time).c_str() |
| 40 | |
joshualitt | da7b843 | 2015-05-27 09:19:03 -0700 | [diff] [blame] | 41 | VisualBench::VisualBench(void* hwnd, int argc, char** argv) |
| 42 | : INHERITED(hwnd) |
joshualitt | 7fe8ee4 | 2015-06-01 10:03:54 -0700 | [diff] [blame] | 43 | , fLoop(0) |
joshualitt | da7b843 | 2015-05-27 09:19:03 -0700 | [diff] [blame] | 44 | , fCurrentPicture(0) |
joshualitt | 7fe8ee4 | 2015-06-01 10:03:54 -0700 | [diff] [blame] | 45 | , fCurrentSample(0) |
| 46 | , fState(kPreWarm_State) { |
joshualitt | da7b843 | 2015-05-27 09:19:03 -0700 | [diff] [blame] | 47 | SkCommandLineFlags::Parse(argc, argv); |
| 48 | |
joshualitt | 7fe8ee4 | 2015-06-01 10:03:54 -0700 | [diff] [blame] | 49 | // load all SKPs |
joshualitt | da7b843 | 2015-05-27 09:19:03 -0700 | [diff] [blame] | 50 | SkTArray<SkString> skps; |
| 51 | for (int i = 0; i < FLAGS_skps.count(); i++) { |
| 52 | if (SkStrEndsWith(FLAGS_skps[i], ".skp")) { |
| 53 | skps.push_back() = FLAGS_skps[i]; |
joshualitt | 7fe8ee4 | 2015-06-01 10:03:54 -0700 | [diff] [blame] | 54 | fTimings.push_back().fName = FLAGS_skps[i]; |
joshualitt | da7b843 | 2015-05-27 09:19:03 -0700 | [diff] [blame] | 55 | } else { |
| 56 | SkOSFile::Iter it(FLAGS_skps[i], ".skp"); |
| 57 | SkString path; |
| 58 | while (it.next(&path)) { |
joshualitt | 030dc84 | 2015-06-12 12:51:44 -0700 | [diff] [blame^] | 59 | skps.push_back() = SkOSPath::Join(FLAGS_skps[i], path.c_str()); |
joshualitt | 7fe8ee4 | 2015-06-01 10:03:54 -0700 | [diff] [blame] | 60 | fTimings.push_back().fName = path.c_str(); |
joshualitt | da7b843 | 2015-05-27 09:19:03 -0700 | [diff] [blame] | 61 | } |
| 62 | } |
| 63 | } |
| 64 | |
joshualitt | da7b843 | 2015-05-27 09:19:03 -0700 | [diff] [blame] | 65 | for (int i = 0; i < skps.count(); i++) { |
| 66 | SkFILEStream stream(skps[i].c_str()); |
| 67 | if (stream.isValid()) { |
| 68 | fPictures.push_back(SkPicture::CreateFromStream(&stream)); |
| 69 | } else { |
| 70 | SkDebugf("couldn't load picture at \"path\"\n", skps[i].c_str()); |
| 71 | } |
| 72 | } |
joshualitt | 7fe8ee4 | 2015-06-01 10:03:54 -0700 | [diff] [blame] | 73 | |
| 74 | if (fPictures.empty()) { |
| 75 | SkDebugf("no valid skps found\n"); |
| 76 | } |
| 77 | |
| 78 | this->setTitle(); |
| 79 | this->setupBackend(); |
joshualitt | da7b843 | 2015-05-27 09:19:03 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | VisualBench::~VisualBench() { |
| 83 | for (int i = 0; i < fPictures.count(); i++) { |
| 84 | fPictures[i]->~SkPicture(); |
| 85 | } |
| 86 | INHERITED::detach(); |
| 87 | } |
| 88 | |
| 89 | void VisualBench::setTitle() { |
| 90 | SkString title("VisualBench"); |
| 91 | INHERITED::setTitle(title.c_str()); |
| 92 | } |
| 93 | |
| 94 | SkSurface* VisualBench::createSurface() { |
| 95 | SkSurfaceProps props(INHERITED::getSurfaceProps()); |
| 96 | return SkSurface::NewRenderTargetDirect(fRenderTarget, &props); |
| 97 | } |
| 98 | |
| 99 | bool VisualBench::setupBackend() { |
| 100 | this->setColorType(kRGBA_8888_SkColorType); |
| 101 | this->setVisibleP(true); |
| 102 | this->setClipToBounds(false); |
| 103 | |
joshualitt | 7fe8ee4 | 2015-06-01 10:03:54 -0700 | [diff] [blame] | 104 | if (!this->attach(kNativeGL_BackEndType, FLAGS_msaa, &fAttachmentInfo)) { |
joshualitt | da7b843 | 2015-05-27 09:19:03 -0700 | [diff] [blame] | 105 | SkDebugf("Not possible to create backend.\n"); |
| 106 | INHERITED::detach(); |
| 107 | return false; |
| 108 | } |
| 109 | |
| 110 | this->setFullscreen(true); |
| 111 | this->setVsync(false); |
joshualitt | 7fe8ee4 | 2015-06-01 10:03:54 -0700 | [diff] [blame] | 112 | this->resetContext(); |
| 113 | return true; |
| 114 | } |
joshualitt | da7b843 | 2015-05-27 09:19:03 -0700 | [diff] [blame] | 115 | |
joshualitt | 7fe8ee4 | 2015-06-01 10:03:54 -0700 | [diff] [blame] | 116 | void VisualBench::resetContext() { |
joshualitt | da7b843 | 2015-05-27 09:19:03 -0700 | [diff] [blame] | 117 | fInterface.reset(GrGLCreateNativeInterface()); |
| 118 | SkASSERT(fInterface); |
| 119 | |
| 120 | // setup contexts |
| 121 | fContext.reset(GrContext::Create(kOpenGL_GrBackend, (GrBackendContext)fInterface.get())); |
| 122 | SkASSERT(fContext); |
| 123 | |
| 124 | // setup rendertargets |
| 125 | this->setupRenderTarget(); |
joshualitt | da7b843 | 2015-05-27 09:19:03 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | void VisualBench::setupRenderTarget() { |
joshualitt | 030dc84 | 2015-06-12 12:51:44 -0700 | [diff] [blame^] | 129 | if (fContext) { |
| 130 | fRenderTarget.reset(this->renderTarget(fAttachmentInfo, fInterface, fContext)); |
| 131 | } |
joshualitt | da7b843 | 2015-05-27 09:19:03 -0700 | [diff] [blame] | 132 | } |
| 133 | |
joshualitt | 7fe8ee4 | 2015-06-01 10:03:54 -0700 | [diff] [blame] | 134 | inline void VisualBench::renderFrame(SkCanvas* canvas) { |
| 135 | canvas->drawPicture(fPictures[fCurrentPicture]); |
joshualitt | da7b843 | 2015-05-27 09:19:03 -0700 | [diff] [blame] | 136 | fContext->flush(); |
| 137 | INHERITED::present(); |
joshualitt | 7fe8ee4 | 2015-06-01 10:03:54 -0700 | [diff] [blame] | 138 | } |
joshualitt | da7b843 | 2015-05-27 09:19:03 -0700 | [diff] [blame] | 139 | |
joshualitt | 7fe8ee4 | 2015-06-01 10:03:54 -0700 | [diff] [blame] | 140 | void VisualBench::printStats() { |
| 141 | const SkTArray<double>& measurements = fTimings[fCurrentPicture].fMeasurements; |
| 142 | if (FLAGS_verbose) { |
| 143 | for (int i = 0; i < measurements.count(); i++) { |
| 144 | SkDebugf("%s ", HUMANIZE(measurements[i])); |
| 145 | } |
| 146 | SkDebugf("%s\n", fTimings[fCurrentPicture].fName.c_str()); |
| 147 | } else { |
| 148 | SkASSERT(measurements.count()); |
| 149 | Stats stats(measurements.begin(), measurements.count()); |
| 150 | const double stdDevPercent = 100 * sqrt(stats.var) / stats.mean; |
| 151 | SkDebugf("%4d/%-4dMB\t%s\t%s\t%s\t%s\t%.0f%%\t%s\n", |
| 152 | sk_tools::getCurrResidentSetSizeMB(), |
| 153 | sk_tools::getMaxResidentSetSizeMB(), |
| 154 | HUMANIZE(stats.min), |
| 155 | HUMANIZE(stats.median), |
| 156 | HUMANIZE(stats.mean), |
| 157 | HUMANIZE(stats.max), |
| 158 | stdDevPercent, |
| 159 | fTimings[fCurrentPicture].fName.c_str()); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | void VisualBench::timePicture(SkCanvas* canvas) { |
| 164 | this->renderFrame(canvas); |
| 165 | switch (fState) { |
| 166 | case kPreWarm_State: { |
| 167 | if (fCurrentSample >= FLAGS_gpuFrameLag) { |
| 168 | // TODO we currently time across all frames to make sure we capture all GPU work |
| 169 | // We should also rendering an empty SKP to get a baseline to subtract from |
| 170 | // our timing |
| 171 | fState = kTiming_State; |
| 172 | fCurrentSample -= FLAGS_gpuFrameLag; |
| 173 | fTimer.start(); |
| 174 | } else { |
| 175 | fCurrentSample++; |
| 176 | } |
| 177 | break; |
| 178 | } |
| 179 | case kTiming_State: { |
| 180 | if (fCurrentSample >= FLAGS_samples) { |
| 181 | fTimer.end(); |
| 182 | fTimings[fCurrentPicture].fMeasurements.push_back(fTimer.fWall / FLAGS_samples); |
| 183 | this->resetContext(); |
| 184 | fTimer = WallTimer(); |
| 185 | fState = kPreWarm_State; |
| 186 | fCurrentSample = 0; |
| 187 | if (fLoop++ > FLAGS_loops) { |
| 188 | this->printStats(); |
| 189 | fCurrentPicture++; |
| 190 | fLoop = 0; |
| 191 | } |
| 192 | } else { |
| 193 | fCurrentSample++; |
| 194 | } |
| 195 | break; |
| 196 | } |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | void VisualBench::draw(SkCanvas* canvas) { |
| 201 | if (fCurrentPicture < fPictures.count()) { |
| 202 | this->timePicture(canvas); |
| 203 | } else { |
| 204 | this->closeWindow(); |
| 205 | } |
joshualitt | da7b843 | 2015-05-27 09:19:03 -0700 | [diff] [blame] | 206 | |
| 207 | // Invalidate the window to force a redraw. Poor man's animation mechanism. |
| 208 | this->inval(NULL); |
| 209 | } |
| 210 | |
| 211 | void VisualBench::onSizeChange() { |
| 212 | this->setupRenderTarget(); |
| 213 | } |
| 214 | |
| 215 | bool VisualBench::onHandleChar(SkUnichar unichar) { |
| 216 | return true; |
| 217 | } |
| 218 | |
| 219 | // Externally declared entry points |
| 220 | void application_init() { |
| 221 | SkGraphics::Init(); |
| 222 | SkEvent::Init(); |
| 223 | } |
| 224 | |
| 225 | void application_term() { |
| 226 | SkEvent::Term(); |
| 227 | SkGraphics::Term(); |
| 228 | } |
| 229 | |
| 230 | SkOSWindow* create_sk_window(void* hwnd, int argc, char** argv) { |
| 231 | return new VisualBench(hwnd, argc, argv); |
| 232 | } |
| 233 | |