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