2015-09-15 14:40:56 +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 "VisualInteractiveModule.h"
|
|
|
|
|
|
|
|
#include "ProcStats.h"
|
|
|
|
#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"
|
|
|
|
#include "Stats.h"
|
|
|
|
#include "gl/GrGLInterface.h"
|
|
|
|
|
|
|
|
__SK_FORCE_IMAGE_DECODER_LINKING;
|
|
|
|
|
|
|
|
VisualInteractiveModule::VisualInteractiveModule(VisualBench* owner)
|
|
|
|
: fCurrentMeasurement(0)
|
|
|
|
, fBenchmark(nullptr)
|
2015-10-05 20:24:55 +00:00
|
|
|
, fAdvance(false)
|
2015-10-05 20:58:26 +00:00
|
|
|
, fHasBeenReset(false)
|
2015-09-15 14:40:56 +00:00
|
|
|
, fOwner(SkRef(owner)) {
|
|
|
|
fBenchmarkStream.reset(new VisualBenchmarkStream);
|
|
|
|
|
|
|
|
memset(fMeasurements, 0, sizeof(fMeasurements));
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void VisualInteractiveModule::renderFrame(SkCanvas* canvas) {
|
2015-10-05 20:24:55 +00:00
|
|
|
fBenchmark->draw(fTSM.loops(), canvas);
|
2015-09-15 14:40:56 +00:00
|
|
|
this->drawStats(canvas);
|
|
|
|
canvas->flush();
|
|
|
|
fOwner->present();
|
|
|
|
}
|
|
|
|
|
|
|
|
void VisualInteractiveModule::drawStats(SkCanvas* canvas) {
|
|
|
|
static const float kPixelPerMS = 2.0f;
|
|
|
|
static const int kDisplayWidth = 130;
|
|
|
|
static const int kDisplayHeight = 100;
|
|
|
|
static const int kDisplayPadding = 10;
|
|
|
|
static const int kGraphPadding = 3;
|
|
|
|
static const float kBaseMS = 1000.f / 60.f; // ms/frame to hit 60 fps
|
|
|
|
|
|
|
|
SkISize canvasSize = canvas->getDeviceSize();
|
|
|
|
SkRect rect = SkRect::MakeXYWH(SkIntToScalar(canvasSize.fWidth-kDisplayWidth-kDisplayPadding),
|
|
|
|
SkIntToScalar(kDisplayPadding),
|
|
|
|
SkIntToScalar(kDisplayWidth), SkIntToScalar(kDisplayHeight));
|
|
|
|
SkPaint paint;
|
|
|
|
canvas->clipRect(rect);
|
|
|
|
paint.setColor(SK_ColorBLACK);
|
|
|
|
canvas->drawRect(rect, paint);
|
|
|
|
// draw the 16ms line
|
|
|
|
paint.setColor(SK_ColorLTGRAY);
|
|
|
|
canvas->drawLine(rect.fLeft, rect.fBottom - kBaseMS*kPixelPerMS,
|
|
|
|
rect.fRight, rect.fBottom - kBaseMS*kPixelPerMS, paint);
|
|
|
|
paint.setColor(SK_ColorRED);
|
|
|
|
paint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
canvas->drawRect(rect, paint);
|
|
|
|
|
|
|
|
int x = SkScalarTruncToInt(rect.fLeft) + kGraphPadding;
|
|
|
|
const int xStep = 2;
|
|
|
|
const int startY = SkScalarTruncToInt(rect.fBottom);
|
|
|
|
int i = fCurrentMeasurement;
|
|
|
|
do {
|
|
|
|
int endY = startY - (int)(fMeasurements[i] * kPixelPerMS + 0.5); // round to nearest value
|
|
|
|
canvas->drawLine(SkIntToScalar(x), SkIntToScalar(startY),
|
|
|
|
SkIntToScalar(x), SkIntToScalar(endY), paint);
|
|
|
|
i++;
|
|
|
|
i &= (kMeasurementCount - 1); // fast mod
|
|
|
|
x += xStep;
|
|
|
|
} while (i != fCurrentMeasurement);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
bool VisualInteractiveModule::advanceRecordIfNecessary(SkCanvas* canvas) {
|
|
|
|
if (fBenchmark) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
fBenchmark.reset(fBenchmarkStream->next());
|
|
|
|
if (!fBenchmark) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// clear both buffers
|
|
|
|
fOwner->clear(canvas, SK_ColorWHITE, 2);
|
|
|
|
|
2015-09-30 19:11:07 +00:00
|
|
|
fBenchmark->delayedSetup();
|
2015-10-05 20:58:26 +00:00
|
|
|
fBenchmark->preTimingHooks(canvas);
|
2015-09-15 14:40:56 +00:00
|
|
|
return true;
|
|
|
|
}
|
2015-10-05 20:24:55 +00:00
|
|
|
#include "GrGpu.h"
|
|
|
|
#include "GrResourceCache.h"
|
2015-09-15 14:40:56 +00:00
|
|
|
void VisualInteractiveModule::draw(SkCanvas* canvas) {
|
|
|
|
if (!this->advanceRecordIfNecessary(canvas)) {
|
|
|
|
SkDebugf("Exiting VisualBench successfully\n");
|
|
|
|
fOwner->closeWindow();
|
|
|
|
return;
|
|
|
|
}
|
2015-10-05 20:58:26 +00:00
|
|
|
|
|
|
|
if (fHasBeenReset) {
|
|
|
|
fHasBeenReset = false;
|
|
|
|
fBenchmark->preTimingHooks(canvas);
|
|
|
|
}
|
|
|
|
|
2015-09-15 14:40:56 +00:00
|
|
|
this->renderFrame(canvas);
|
2015-10-05 20:58:26 +00:00
|
|
|
TimingStateMachine::ParentEvents event = fTSM.nextFrame(false);
|
2015-10-05 20:24:55 +00:00
|
|
|
switch (event) {
|
|
|
|
case TimingStateMachine::kReset_ParentEvents:
|
2015-10-05 20:58:26 +00:00
|
|
|
fBenchmark->postTimingHooks(canvas);
|
|
|
|
fHasBeenReset = true;
|
2015-10-05 20:24:55 +00:00
|
|
|
fOwner->reset();
|
2015-09-15 14:40:56 +00:00
|
|
|
break;
|
2015-10-05 20:24:55 +00:00
|
|
|
case TimingStateMachine::kTiming_ParentEvents:
|
2015-09-15 14:40:56 +00:00
|
|
|
break;
|
2015-10-05 20:24:55 +00:00
|
|
|
case TimingStateMachine::kTimingFinished_ParentEvents:
|
|
|
|
// Record measurements
|
|
|
|
fMeasurements[fCurrentMeasurement++] = fTSM.lastMeasurement();
|
|
|
|
fCurrentMeasurement &= (kMeasurementCount-1); // fast mod
|
|
|
|
SkASSERT(fCurrentMeasurement < kMeasurementCount);
|
|
|
|
this->drawStats(canvas);
|
|
|
|
if (fAdvance) {
|
|
|
|
fAdvance = false;
|
|
|
|
fTSM.nextBenchmark(canvas, fBenchmark);
|
2015-10-05 20:58:26 +00:00
|
|
|
fBenchmark->postTimingHooks(canvas);
|
2015-10-05 20:24:55 +00:00
|
|
|
fBenchmark.reset(nullptr);
|
|
|
|
fOwner->reset();
|
2015-10-05 20:58:26 +00:00
|
|
|
fHasBeenReset = true;
|
2015-10-05 20:24:55 +00:00
|
|
|
}
|
2015-09-15 14:40:56 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool VisualInteractiveModule::onHandleChar(SkUnichar c) {
|
|
|
|
if (' ' == c) {
|
2015-10-05 20:24:55 +00:00
|
|
|
fAdvance = true;
|
2015-09-15 14:40:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|