2015-05-27 16:19:03 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2015 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "VisualBench.h"
|
|
|
|
|
2015-06-01 17:03:54 +00:00
|
|
|
#include "ProcStats.h"
|
2015-05-27 16:19:03 +00:00
|
|
|
#include "SkApplication.h"
|
|
|
|
#include "SkCanvas.h"
|
|
|
|
#include "SkCommandLineFlags.h"
|
|
|
|
#include "SkForceLinking.h"
|
|
|
|
#include "SkGraphics.h"
|
|
|
|
#include "SkGr.h"
|
|
|
|
#include "SkImageDecoder.h"
|
|
|
|
#include "SkOSFile.h"
|
|
|
|
#include "SkStream.h"
|
2015-06-01 17:03:54 +00:00
|
|
|
#include "Stats.h"
|
2015-05-27 16:19:03 +00:00
|
|
|
#include "gl/GrGLInterface.h"
|
|
|
|
|
|
|
|
__SK_FORCE_IMAGE_DECODER_LINKING;
|
|
|
|
|
2015-06-23 14:10:59 +00:00
|
|
|
// Between samples we reset context
|
|
|
|
// Between frames we swap buffers
|
|
|
|
// Between flushes we call flush on GrContext
|
|
|
|
|
2015-06-01 17:03:54 +00:00
|
|
|
DEFINE_int32(gpuFrameLag, 5, "Overestimate of maximum number of frames GPU allows to lag.");
|
2015-06-23 14:10:59 +00:00
|
|
|
DEFINE_int32(samples, 10, "Number of times to time each skp.");
|
|
|
|
DEFINE_int32(frames, 5, "Number of frames of each skp to render per sample.");
|
|
|
|
DEFINE_double(flushMs, 20, "Target flush time in millseconds.");
|
|
|
|
DEFINE_double(loopMs, 5, "Target loop time in millseconds.");
|
2015-06-01 17:03:54 +00:00
|
|
|
DEFINE_int32(msaa, 0, "Number of msaa samples.");
|
2015-06-16 20:52:18 +00:00
|
|
|
DEFINE_bool2(fullscreen, f, true, "Run fullscreen.");
|
2015-06-30 14:43:14 +00:00
|
|
|
DEFINE_bool2(verbose, v, false, "enable verbose output from the test driver.");
|
2015-07-20 20:38:56 +00:00
|
|
|
DEFINE_string(key, "", ""); // dummy to enable gm tests that have platform-specific names
|
2015-08-18 19:13:34 +00:00
|
|
|
DEFINE_string(outResultsFile, "", "If given, write results here as JSON.");
|
|
|
|
DEFINE_string(properties, "",
|
|
|
|
"Space-separated key/value pairs to add to JSON identifying this run.");
|
2015-06-01 17:03:54 +00:00
|
|
|
|
|
|
|
static SkString humanize(double ms) {
|
|
|
|
if (FLAGS_verbose) {
|
|
|
|
return SkStringPrintf("%llu", (uint64_t)(ms*1e6));
|
|
|
|
}
|
|
|
|
return HumanizeMs(ms);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define HUMANIZE(time) humanize(time).c_str()
|
|
|
|
|
2015-05-27 16:19:03 +00:00
|
|
|
VisualBench::VisualBench(void* hwnd, int argc, char** argv)
|
|
|
|
: INHERITED(hwnd)
|
2015-06-01 17:03:54 +00:00
|
|
|
, fCurrentSample(0)
|
2015-06-23 14:10:59 +00:00
|
|
|
, fCurrentFrame(0)
|
|
|
|
, fFlushes(1)
|
|
|
|
, fLoops(1)
|
2015-06-30 14:43:14 +00:00
|
|
|
, fState(kPreWarmLoops_State)
|
2015-08-18 19:13:34 +00:00
|
|
|
, fBenchmark(NULL)
|
2015-08-26 20:07:48 +00:00
|
|
|
, fResults(new ResultsWriter) {
|
2015-05-27 16:19:03 +00:00
|
|
|
SkCommandLineFlags::Parse(argc, argv);
|
|
|
|
|
2015-06-01 17:03:54 +00:00
|
|
|
this->setTitle();
|
|
|
|
this->setupBackend();
|
2015-06-23 14:10:59 +00:00
|
|
|
|
2015-08-26 20:07:48 +00:00
|
|
|
fBenchmarkStream.reset(new VisualBenchmarkStream);
|
2015-06-30 14:43:14 +00:00
|
|
|
|
2015-06-23 14:10:59 +00:00
|
|
|
// Print header
|
|
|
|
SkDebugf("curr/maxrss\tloops\tflushes\tmin\tmedian\tmean\tmax\tstddev\tbench\n");
|
2015-08-18 19:13:34 +00:00
|
|
|
|
|
|
|
// setup json logging if required
|
|
|
|
if (!FLAGS_outResultsFile.isEmpty()) {
|
2015-08-26 20:07:48 +00:00
|
|
|
fResults.reset(new NanoJSONResultsWriter(FLAGS_outResultsFile[0]));
|
2015-08-18 19:13:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (1 == FLAGS_properties.count() % 2) {
|
|
|
|
SkDebugf("ERROR: --properties must be passed with an even number of arguments.\n");
|
|
|
|
} else {
|
|
|
|
for (int i = 1; i < FLAGS_properties.count(); i += 2) {
|
|
|
|
fResults->property(FLAGS_properties[i - 1], FLAGS_properties[i]);
|
|
|
|
}
|
|
|
|
}
|
2015-05-27 16:19:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VisualBench::~VisualBench() {
|
|
|
|
INHERITED::detach();
|
|
|
|
}
|
|
|
|
|
|
|
|
void VisualBench::setTitle() {
|
|
|
|
SkString title("VisualBench");
|
|
|
|
INHERITED::setTitle(title.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
SkSurface* VisualBench::createSurface() {
|
2015-07-15 13:18:57 +00:00
|
|
|
if (!fSurface) {
|
|
|
|
SkSurfaceProps props(INHERITED::getSurfaceProps());
|
|
|
|
fSurface.reset(SkSurface::NewRenderTargetDirect(fRenderTarget, &props));
|
|
|
|
}
|
|
|
|
|
|
|
|
// The caller will wrap the SkSurface in an SkAutoTUnref
|
|
|
|
return SkRef(fSurface.get());
|
2015-05-27 16:19:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool VisualBench::setupBackend() {
|
|
|
|
this->setColorType(kRGBA_8888_SkColorType);
|
|
|
|
this->setVisibleP(true);
|
|
|
|
this->setClipToBounds(false);
|
|
|
|
|
2015-06-16 20:52:18 +00:00
|
|
|
if (FLAGS_fullscreen) {
|
|
|
|
if (!this->makeFullscreen()) {
|
|
|
|
SkDebugf("Could not go fullscreen!");
|
|
|
|
}
|
2015-06-16 19:47:25 +00:00
|
|
|
}
|
2015-06-01 17:03:54 +00:00
|
|
|
if (!this->attach(kNativeGL_BackEndType, FLAGS_msaa, &fAttachmentInfo)) {
|
2015-05-27 16:19:03 +00:00
|
|
|
SkDebugf("Not possible to create backend.\n");
|
|
|
|
INHERITED::detach();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
this->setVsync(false);
|
2015-06-01 17:03:54 +00:00
|
|
|
this->resetContext();
|
|
|
|
return true;
|
|
|
|
}
|
2015-05-27 16:19:03 +00:00
|
|
|
|
2015-06-01 17:03:54 +00:00
|
|
|
void VisualBench::resetContext() {
|
2015-07-16 20:40:51 +00:00
|
|
|
fSurface.reset(NULL);
|
|
|
|
|
2015-05-27 16:19:03 +00:00
|
|
|
fInterface.reset(GrGLCreateNativeInterface());
|
|
|
|
SkASSERT(fInterface);
|
|
|
|
|
|
|
|
// setup contexts
|
|
|
|
fContext.reset(GrContext::Create(kOpenGL_GrBackend, (GrBackendContext)fInterface.get()));
|
|
|
|
SkASSERT(fContext);
|
|
|
|
|
|
|
|
// setup rendertargets
|
|
|
|
this->setupRenderTarget();
|
|
|
|
}
|
|
|
|
|
|
|
|
void VisualBench::setupRenderTarget() {
|
2015-06-12 19:51:44 +00:00
|
|
|
if (fContext) {
|
|
|
|
fRenderTarget.reset(this->renderTarget(fAttachmentInfo, fInterface, fContext));
|
|
|
|
}
|
2015-05-27 16:19:03 +00:00
|
|
|
}
|
|
|
|
|
2015-06-01 17:03:54 +00:00
|
|
|
inline void VisualBench::renderFrame(SkCanvas* canvas) {
|
2015-06-23 14:10:59 +00:00
|
|
|
for (int flush = 0; flush < fFlushes; flush++) {
|
2015-06-30 14:43:14 +00:00
|
|
|
fBenchmark->draw(fLoops, canvas);
|
2015-06-23 14:10:59 +00:00
|
|
|
canvas->flush();
|
|
|
|
}
|
2015-05-27 16:19:03 +00:00
|
|
|
INHERITED::present();
|
2015-06-01 17:03:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void VisualBench::printStats() {
|
2015-06-30 14:43:14 +00:00
|
|
|
const SkTArray<double>& measurements = fRecords.back().fMeasurements;
|
|
|
|
const char* shortName = fBenchmark->getUniqueName();
|
2015-08-18 19:13:34 +00:00
|
|
|
|
|
|
|
// update log
|
|
|
|
// Note: We currently log only the minimum. It would be interesting to log more information
|
|
|
|
SkString configName;
|
|
|
|
if (FLAGS_msaa > 0) {
|
|
|
|
configName.appendf("msaa_%d", FLAGS_msaa);
|
|
|
|
} else {
|
|
|
|
configName.appendf("gpu");
|
|
|
|
}
|
|
|
|
fResults->config(configName.c_str());
|
|
|
|
fResults->configOption("name", fBenchmark->getUniqueName());
|
|
|
|
SkASSERT(measurements.count());
|
|
|
|
Stats stats(measurements);
|
|
|
|
fResults->metric("min_ms", stats.min);
|
|
|
|
|
|
|
|
// Print output
|
2015-06-01 17:03:54 +00:00
|
|
|
if (FLAGS_verbose) {
|
|
|
|
for (int i = 0; i < measurements.count(); i++) {
|
|
|
|
SkDebugf("%s ", HUMANIZE(measurements[i]));
|
|
|
|
}
|
2015-06-30 14:43:14 +00:00
|
|
|
SkDebugf("%s\n", shortName);
|
2015-06-01 17:03:54 +00:00
|
|
|
} else {
|
|
|
|
const double stdDevPercent = 100 * sqrt(stats.var) / stats.mean;
|
2015-06-23 14:10:59 +00:00
|
|
|
SkDebugf("%4d/%-4dMB\t%d\t%d\t%s\t%s\t%s\t%s\t%.0f%%\t%s\n",
|
2015-06-01 17:03:54 +00:00
|
|
|
sk_tools::getCurrResidentSetSizeMB(),
|
|
|
|
sk_tools::getMaxResidentSetSizeMB(),
|
2015-06-23 14:10:59 +00:00
|
|
|
fLoops,
|
|
|
|
fFlushes,
|
2015-06-01 17:03:54 +00:00
|
|
|
HUMANIZE(stats.min),
|
|
|
|
HUMANIZE(stats.median),
|
|
|
|
HUMANIZE(stats.mean),
|
|
|
|
HUMANIZE(stats.max),
|
|
|
|
stdDevPercent,
|
2015-06-30 14:43:14 +00:00
|
|
|
shortName);
|
2015-06-01 17:03:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-30 14:43:14 +00:00
|
|
|
bool VisualBench::advanceRecordIfNecessary(SkCanvas* canvas) {
|
|
|
|
if (fBenchmark) {
|
2015-06-16 20:52:18 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-07-16 21:23:22 +00:00
|
|
|
fBenchmark.reset(fBenchmarkStream->next());
|
2015-06-30 14:43:14 +00:00
|
|
|
if (!fBenchmark) {
|
|
|
|
return false;
|
2015-06-16 20:52:18 +00:00
|
|
|
}
|
2015-06-30 14:43:14 +00:00
|
|
|
|
|
|
|
canvas->clear(0xffffffff);
|
|
|
|
fBenchmark->preDraw();
|
|
|
|
fRecords.push_back();
|
2015-08-18 19:13:34 +00:00
|
|
|
|
|
|
|
// Log bench name
|
|
|
|
fResults->bench(fBenchmark->getUniqueName(), fBenchmark->getSize().fX,
|
|
|
|
fBenchmark->getSize().fY);
|
2015-06-30 14:43:14 +00:00
|
|
|
return true;
|
2015-06-16 20:52:18 +00:00
|
|
|
}
|
|
|
|
|
2015-07-17 16:09:23 +00:00
|
|
|
inline void VisualBench::nextState(State nextState) {
|
|
|
|
fState = nextState;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VisualBench::perCanvasPreDraw(SkCanvas* canvas, State nextState) {
|
|
|
|
fBenchmark->perCanvasPreDraw(canvas);
|
|
|
|
fCurrentFrame = 0;
|
|
|
|
this->nextState(nextState);
|
|
|
|
}
|
|
|
|
|
2015-06-23 14:10:59 +00:00
|
|
|
void VisualBench::preWarm(State nextState) {
|
|
|
|
if (fCurrentFrame >= FLAGS_gpuFrameLag) {
|
|
|
|
// we currently time across all frames to make sure we capture all GPU work
|
2015-07-17 16:09:23 +00:00
|
|
|
this->nextState(nextState);
|
2015-06-23 14:10:59 +00:00
|
|
|
fCurrentFrame = 0;
|
|
|
|
fTimer.start();
|
|
|
|
} else {
|
|
|
|
fCurrentFrame++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-16 20:52:18 +00:00
|
|
|
void VisualBench::draw(SkCanvas* canvas) {
|
2015-06-30 14:43:14 +00:00
|
|
|
if (!this->advanceRecordIfNecessary(canvas)) {
|
2015-08-19 15:21:48 +00:00
|
|
|
SkDebugf("Exiting VisualBench successfully\n");
|
2015-06-16 20:52:18 +00:00
|
|
|
this->closeWindow();
|
|
|
|
return;
|
|
|
|
}
|
2015-06-01 17:03:54 +00:00
|
|
|
this->renderFrame(canvas);
|
|
|
|
switch (fState) {
|
2015-07-17 16:09:23 +00:00
|
|
|
case kPreWarmLoopsPerCanvasPreDraw_State: {
|
|
|
|
this->perCanvasPreDraw(canvas, kPreWarmLoops_State);
|
|
|
|
break;
|
|
|
|
}
|
2015-06-23 14:10:59 +00:00
|
|
|
case kPreWarmLoops_State: {
|
|
|
|
this->preWarm(kTuneLoops_State);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case kTuneLoops_State: {
|
2015-07-17 16:09:23 +00:00
|
|
|
this->tuneLoops();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case kPreWarmTimingPerCanvasPreDraw_State: {
|
|
|
|
this->perCanvasPreDraw(canvas, kPreWarmTiming_State);
|
2015-06-01 17:03:54 +00:00
|
|
|
break;
|
|
|
|
}
|
2015-06-23 14:10:59 +00:00
|
|
|
case kPreWarmTiming_State: {
|
|
|
|
this->preWarm(kTiming_State);
|
|
|
|
break;
|
|
|
|
}
|
2015-06-01 17:03:54 +00:00
|
|
|
case kTiming_State: {
|
2015-07-17 16:09:23 +00:00
|
|
|
this->timing(canvas);
|
2015-06-01 17:03:54 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-05-27 16:19:03 +00:00
|
|
|
|
|
|
|
// Invalidate the window to force a redraw. Poor man's animation mechanism.
|
|
|
|
this->inval(NULL);
|
|
|
|
}
|
|
|
|
|
2015-07-17 16:09:23 +00:00
|
|
|
inline double VisualBench::elapsed() {
|
|
|
|
fTimer.end();
|
|
|
|
return fTimer.fWall;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VisualBench::resetTimingState() {
|
|
|
|
fCurrentFrame = 0;
|
|
|
|
fTimer = WallTimer();
|
|
|
|
this->resetContext();
|
|
|
|
}
|
|
|
|
|
|
|
|
void VisualBench::scaleLoops(double elapsedMs) {
|
|
|
|
// Scale back the number of loops
|
|
|
|
fLoops = (int)ceil(fLoops * FLAGS_loopMs / elapsedMs);
|
|
|
|
fFlushes = (int)ceil(FLAGS_flushMs / elapsedMs);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void VisualBench::tuneLoops() {
|
|
|
|
if (1 << 30 == fLoops) {
|
|
|
|
// We're about to wrap. Something's wrong with the bench.
|
|
|
|
SkDebugf("InnerLoops wrapped\n");
|
|
|
|
fLoops = 0;
|
|
|
|
} else {
|
|
|
|
double elapsedMs = this->elapsed();
|
|
|
|
if (elapsedMs > FLAGS_loopMs) {
|
|
|
|
this->scaleLoops(elapsedMs);
|
|
|
|
this->nextState(kPreWarmTimingPerCanvasPreDraw_State);
|
|
|
|
} else {
|
|
|
|
fLoops *= 2;
|
|
|
|
this->nextState(kPreWarmLoops_State);
|
|
|
|
}
|
|
|
|
this->resetTimingState();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void VisualBench::recordMeasurement() {
|
|
|
|
double measurement = this->elapsed() / (FLAGS_frames * fLoops * fFlushes);
|
|
|
|
fRecords.back().fMeasurements.push_back(measurement);
|
|
|
|
}
|
|
|
|
|
|
|
|
void VisualBench::postDraw(SkCanvas* canvas) {
|
|
|
|
fBenchmark->perCanvasPostDraw(canvas);
|
|
|
|
fBenchmark.reset(NULL);
|
|
|
|
fCurrentSample = 0;
|
|
|
|
fFlushes = 1;
|
|
|
|
fLoops = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void VisualBench::timing(SkCanvas* canvas) {
|
|
|
|
if (fCurrentFrame >= FLAGS_frames) {
|
|
|
|
this->recordMeasurement();
|
|
|
|
if (fCurrentSample++ >= FLAGS_samples) {
|
|
|
|
this->printStats();
|
|
|
|
this->postDraw(canvas);
|
|
|
|
this->nextState(kPreWarmLoopsPerCanvasPreDraw_State);
|
|
|
|
} else {
|
|
|
|
this->nextState(kPreWarmTimingPerCanvasPreDraw_State);
|
|
|
|
}
|
|
|
|
this->resetTimingState();
|
|
|
|
} else {
|
|
|
|
fCurrentFrame++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-27 16:19:03 +00:00
|
|
|
void VisualBench::onSizeChange() {
|
|
|
|
this->setupRenderTarget();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool VisualBench::onHandleChar(SkUnichar unichar) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Externally declared entry points
|
|
|
|
void application_init() {
|
|
|
|
SkGraphics::Init();
|
|
|
|
SkEvent::Init();
|
|
|
|
}
|
|
|
|
|
|
|
|
void application_term() {
|
|
|
|
SkEvent::Term();
|
|
|
|
SkGraphics::Term();
|
|
|
|
}
|
|
|
|
|
|
|
|
SkOSWindow* create_sk_window(void* hwnd, int argc, char** argv) {
|
|
|
|
return new VisualBench(hwnd, argc, argv);
|
|
|
|
}
|
|
|
|
|