2014-08-01 14:46:52 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2014 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "SKPBench.h"
|
2014-10-21 19:29:25 +00:00
|
|
|
#include "SkCommandLineFlags.h"
|
2014-11-21 14:19:36 +00:00
|
|
|
#include "SkMultiPictureDraw.h"
|
|
|
|
#include "SkSurface.h"
|
2014-10-21 19:29:25 +00:00
|
|
|
|
2015-10-26 20:45:29 +00:00
|
|
|
#if SK_SUPPORT_GPU
|
|
|
|
#include "GrContext.h"
|
|
|
|
#endif
|
|
|
|
|
2015-06-16 17:41:27 +00:00
|
|
|
// These CPU tile sizes are not good per se, but they are similar to what Chrome uses.
|
|
|
|
DEFINE_int32(CPUbenchTileW, 256, "Tile width used for CPU SKP playback.");
|
|
|
|
DEFINE_int32(CPUbenchTileH, 256, "Tile height used for CPU SKP playback.");
|
|
|
|
|
|
|
|
DEFINE_int32(GPUbenchTileW, 1600, "Tile width used for GPU SKP playback.");
|
|
|
|
DEFINE_int32(GPUbenchTileH, 512, "Tile height used for GPU SKP playback.");
|
2014-08-01 14:46:52 +00:00
|
|
|
|
2014-11-21 14:19:36 +00:00
|
|
|
SKPBench::SKPBench(const char* name, const SkPicture* pic, const SkIRect& clip, SkScalar scale,
|
2015-06-25 17:51:56 +00:00
|
|
|
bool useMultiPictureDraw, bool doLooping)
|
2014-08-01 14:46:52 +00:00
|
|
|
: fPic(SkRef(pic))
|
|
|
|
, fClip(clip)
|
2014-09-10 19:05:59 +00:00
|
|
|
, fScale(scale)
|
2014-11-21 14:19:36 +00:00
|
|
|
, fName(name)
|
2015-06-25 17:51:56 +00:00
|
|
|
, fUseMultiPictureDraw(useMultiPictureDraw)
|
|
|
|
, fDoLooping(doLooping) {
|
2015-01-26 14:47:55 +00:00
|
|
|
fUniqueName.printf("%s_%.2g", name, scale); // Scale makes this unqiue for perf.skia.org traces.
|
2014-11-21 14:19:36 +00:00
|
|
|
if (useMultiPictureDraw) {
|
|
|
|
fUniqueName.append("_mpd");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SKPBench::~SKPBench() {
|
|
|
|
for (int i = 0; i < fSurfaces.count(); ++i) {
|
|
|
|
fSurfaces[i]->unref();
|
|
|
|
}
|
2014-08-01 14:46:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const char* SKPBench::onGetName() {
|
|
|
|
return fName.c_str();
|
|
|
|
}
|
|
|
|
|
2014-09-10 19:05:59 +00:00
|
|
|
const char* SKPBench::onGetUniqueName() {
|
|
|
|
return fUniqueName.c_str();
|
|
|
|
}
|
|
|
|
|
2014-11-21 14:19:36 +00:00
|
|
|
void SKPBench::onPerCanvasPreDraw(SkCanvas* canvas) {
|
|
|
|
SkIRect bounds;
|
|
|
|
SkAssertResult(canvas->getClipDeviceBounds(&bounds));
|
|
|
|
|
2015-06-16 17:41:27 +00:00
|
|
|
const bool gpu = canvas->getGrContext() != nullptr;
|
|
|
|
int tileW = gpu ? FLAGS_GPUbenchTileW : FLAGS_CPUbenchTileW,
|
|
|
|
tileH = gpu ? FLAGS_GPUbenchTileH : FLAGS_CPUbenchTileH;
|
|
|
|
|
|
|
|
tileW = SkTMin(tileW, bounds.width());
|
|
|
|
tileH = SkTMin(tileH, bounds.height());
|
2015-03-05 21:42:27 +00:00
|
|
|
|
|
|
|
int xTiles = SkScalarCeilToInt(bounds.width() / SkIntToScalar(tileW));
|
|
|
|
int yTiles = SkScalarCeilToInt(bounds.height() / SkIntToScalar(tileH));
|
2014-11-21 14:19:36 +00:00
|
|
|
|
|
|
|
fSurfaces.setReserve(xTiles * yTiles);
|
|
|
|
fTileRects.setReserve(xTiles * yTiles);
|
|
|
|
|
2015-03-05 21:42:27 +00:00
|
|
|
SkImageInfo ii = canvas->imageInfo().makeWH(tileW, tileH);
|
2014-11-21 14:19:36 +00:00
|
|
|
|
2015-03-05 21:42:27 +00:00
|
|
|
for (int y = bounds.fTop; y < bounds.fBottom; y += tileH) {
|
|
|
|
for (int x = bounds.fLeft; x < bounds.fRight; x += tileW) {
|
|
|
|
const SkIRect tileRect = SkIRect::MakeXYWH(x, y, tileW, tileH);
|
2014-12-04 16:31:02 +00:00
|
|
|
*fTileRects.append() = tileRect;
|
2016-03-24 01:59:25 +00:00
|
|
|
*fSurfaces.push() = canvas->makeSurface(ii).release();
|
2014-12-04 16:31:02 +00:00
|
|
|
|
|
|
|
// Never want the contents of a tile to include stuff the parent
|
|
|
|
// canvas clips out
|
|
|
|
SkRect clip = SkRect::Make(bounds);
|
|
|
|
clip.offset(-SkIntToScalar(tileRect.fLeft), -SkIntToScalar(tileRect.fTop));
|
|
|
|
fSurfaces.top()->getCanvas()->clipRect(clip);
|
|
|
|
|
2014-11-21 14:19:36 +00:00
|
|
|
fSurfaces.top()->getCanvas()->setMatrix(canvas->getTotalMatrix());
|
|
|
|
fSurfaces.top()->getCanvas()->scale(fScale, fScale);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SKPBench::onPerCanvasPostDraw(SkCanvas* canvas) {
|
|
|
|
// Draw the last set of tiles into the master canvas in case we're
|
|
|
|
// saving the images
|
|
|
|
for (int i = 0; i < fTileRects.count(); ++i) {
|
2016-03-17 17:51:11 +00:00
|
|
|
sk_sp<SkImage> image(fSurfaces[i]->makeImageSnapshot());
|
2014-11-21 14:53:00 +00:00
|
|
|
canvas->drawImage(image,
|
2014-11-21 14:19:36 +00:00
|
|
|
SkIntToScalar(fTileRects[i].fLeft), SkIntToScalar(fTileRects[i].fTop));
|
|
|
|
SkSafeSetNull(fSurfaces[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
fSurfaces.rewind();
|
|
|
|
fTileRects.rewind();
|
|
|
|
}
|
|
|
|
|
2014-08-01 14:46:52 +00:00
|
|
|
bool SKPBench::isSuitableFor(Backend backend) {
|
|
|
|
return backend != kNonRendering_Backend;
|
|
|
|
}
|
|
|
|
|
|
|
|
SkIPoint SKPBench::onGetSize() {
|
|
|
|
return SkIPoint::Make(fClip.width(), fClip.height());
|
|
|
|
}
|
|
|
|
|
2015-10-01 16:43:39 +00:00
|
|
|
void SKPBench::onDraw(int loops, SkCanvas* canvas) {
|
2015-06-25 17:51:56 +00:00
|
|
|
SkASSERT(fDoLooping || 1 == loops);
|
2016-02-22 16:08:25 +00:00
|
|
|
while (1) {
|
|
|
|
if (fUseMultiPictureDraw) {
|
2015-04-27 16:16:57 +00:00
|
|
|
this->drawMPDPicture();
|
2016-02-22 16:08:25 +00:00
|
|
|
} else {
|
2015-04-27 16:16:57 +00:00
|
|
|
this->drawPicture();
|
2014-11-21 14:19:36 +00:00
|
|
|
}
|
2016-02-22 16:08:25 +00:00
|
|
|
if (0 == --loops) {
|
|
|
|
break;
|
|
|
|
}
|
2015-10-26 20:45:29 +00:00
|
|
|
#if SK_SUPPORT_GPU
|
2016-12-21 16:14:46 +00:00
|
|
|
// Ensure the GrContext doesn't combine ops across draw loops.
|
2016-02-22 16:08:25 +00:00
|
|
|
if (GrContext* context = canvas->getGrContext()) {
|
|
|
|
context->flush();
|
|
|
|
}
|
2015-10-26 20:45:29 +00:00
|
|
|
#endif
|
2016-02-22 16:08:25 +00:00
|
|
|
}
|
2014-08-01 14:46:52 +00:00
|
|
|
}
|
2015-04-27 16:16:57 +00:00
|
|
|
|
|
|
|
void SKPBench::drawMPDPicture() {
|
|
|
|
SkMultiPictureDraw mpd;
|
|
|
|
|
|
|
|
for (int j = 0; j < fTileRects.count(); ++j) {
|
|
|
|
SkMatrix trans;
|
|
|
|
trans.setTranslate(-fTileRects[j].fLeft/fScale,
|
|
|
|
-fTileRects[j].fTop/fScale);
|
2016-11-04 17:02:54 +00:00
|
|
|
mpd.add(fSurfaces[j]->getCanvas(), fPic.get(), &trans);
|
2015-04-27 16:16:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mpd.draw();
|
|
|
|
|
|
|
|
for (int j = 0; j < fTileRects.count(); ++j) {
|
|
|
|
fSurfaces[j]->getCanvas()->flush();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SKPBench::drawPicture() {
|
|
|
|
for (int j = 0; j < fTileRects.count(); ++j) {
|
|
|
|
const SkMatrix trans = SkMatrix::MakeTrans(-fTileRects[j].fLeft / fScale,
|
|
|
|
-fTileRects[j].fTop / fScale);
|
2016-11-04 17:02:54 +00:00
|
|
|
fSurfaces[j]->getCanvas()->drawPicture(fPic.get(), &trans, nullptr);
|
2015-04-27 16:16:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (int j = 0; j < fTileRects.count(); ++j) {
|
|
|
|
fSurfaces[j]->getCanvas()->flush();
|
|
|
|
}
|
|
|
|
}
|
2015-12-02 17:05:37 +00:00
|
|
|
|
|
|
|
#if SK_SUPPORT_GPU
|
2015-12-02 21:36:47 +00:00
|
|
|
#include "GrGpu.h"
|
2015-12-02 17:05:37 +00:00
|
|
|
static void draw_pic_for_stats(SkCanvas* canvas, GrContext* context, const SkPicture* picture,
|
|
|
|
SkTArray<SkString>* keys, SkTArray<double>* values,
|
|
|
|
const char* tag) {
|
|
|
|
context->resetGpuStats();
|
|
|
|
canvas->drawPicture(picture);
|
|
|
|
canvas->flush();
|
|
|
|
|
|
|
|
int offset = keys->count();
|
|
|
|
context->dumpGpuStatsKeyValuePairs(keys, values);
|
2015-12-02 22:08:25 +00:00
|
|
|
context->dumpCacheStatsKeyValuePairs(keys, values);
|
2015-12-02 17:05:37 +00:00
|
|
|
|
|
|
|
// append tag, but only to new tags
|
|
|
|
for (int i = offset; i < keys->count(); i++, offset++) {
|
|
|
|
(*keys)[i].appendf("_%s", tag);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void SKPBench::getGpuStats(SkCanvas* canvas, SkTArray<SkString>* keys, SkTArray<double>* values) {
|
|
|
|
#if SK_SUPPORT_GPU
|
|
|
|
// we do a special single draw and then dump the key / value pairs
|
|
|
|
GrContext* context = canvas->getGrContext();
|
|
|
|
if (!context) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO refactor this out if we want to test other subclasses of skpbench
|
|
|
|
context->flush();
|
|
|
|
context->freeGpuResources();
|
|
|
|
context->resetContext();
|
2015-12-02 21:36:47 +00:00
|
|
|
context->getGpu()->resetShaderCacheForTesting();
|
2016-11-04 17:02:54 +00:00
|
|
|
draw_pic_for_stats(canvas, context, fPic.get(), keys, values, "first_frame");
|
2015-12-02 17:05:37 +00:00
|
|
|
|
|
|
|
// draw second frame
|
2016-11-04 17:02:54 +00:00
|
|
|
draw_pic_for_stats(canvas, context, fPic.get(), keys, values, "second_frame");
|
2015-12-02 17:05:37 +00:00
|
|
|
|
|
|
|
#endif
|
|
|
|
}
|