blob: 9ed18e258f9499063dccf2fdaf135bd49c1538be [file] [log] [blame]
mtklein92007582014-08-01 07:46:52 -07001/*
2 * Copyright 2014 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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "bench/SKPBench.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -05009#include "include/core/SkSurface.h"
Robert Phillipsf0288102020-07-06 13:45:34 -040010#include "include/gpu/GrDirectContext.h"
Adlai Hollera0693042020-10-14 11:23:11 -040011#include "src/gpu/GrDirectContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "tools/flags/CommandLineFlags.h"
mtkleinc7f7f462014-10-21 12:29:25 -070013
cdaltone6d20242015-10-26 13:45:29 -070014
mtkleinbf9e6002015-06-16 10:41:27 -070015// These CPU tile sizes are not good per se, but they are similar to what Chrome uses.
Mike Klein5b3f3432019-03-21 11:42:21 -050016static DEFINE_int(CPUbenchTileW, 256, "Tile width used for CPU SKP playback.");
17static DEFINE_int(CPUbenchTileH, 256, "Tile height used for CPU SKP playback.");
mtkleinbf9e6002015-06-16 10:41:27 -070018
Mike Klein5b3f3432019-03-21 11:42:21 -050019static DEFINE_int(GPUbenchTileW, 1600, "Tile width used for GPU SKP playback.");
20static DEFINE_int(GPUbenchTileH, 512, "Tile height used for GPU SKP playback.");
mtklein92007582014-08-01 07:46:52 -070021
robertphillips5b693772014-11-21 06:19:36 -080022SKPBench::SKPBench(const char* name, const SkPicture* pic, const SkIRect& clip, SkScalar scale,
Brian Salomon626349b2020-08-27 10:54:36 -040023 bool doLooping)
mtklein92007582014-08-01 07:46:52 -070024 : fPic(SkRef(pic))
25 , fClip(clip)
mtklein96289052014-09-10 12:05:59 -070026 , fScale(scale)
robertphillips5b693772014-11-21 06:19:36 -080027 , fName(name)
cdaltonb4022962015-06-25 10:51:56 -070028 , fDoLooping(doLooping) {
tfarina0004e7d2015-01-26 06:47:55 -080029 fUniqueName.printf("%s_%.2g", name, scale); // Scale makes this unqiue for perf.skia.org traces.
robertphillips5b693772014-11-21 06:19:36 -080030}
31
32SKPBench::~SKPBench() {
33 for (int i = 0; i < fSurfaces.count(); ++i) {
34 fSurfaces[i]->unref();
35 }
mtklein92007582014-08-01 07:46:52 -070036}
37
38const char* SKPBench::onGetName() {
39 return fName.c_str();
40}
41
mtklein96289052014-09-10 12:05:59 -070042const char* SKPBench::onGetUniqueName() {
43 return fUniqueName.c_str();
44}
45
robertphillips5b693772014-11-21 06:19:36 -080046void SKPBench::onPerCanvasPreDraw(SkCanvas* canvas) {
Mike Reed918e1442017-01-23 11:39:45 -050047 SkIRect bounds = canvas->getDeviceClipBounds();
48 SkAssertResult(!bounds.isEmpty());
robertphillips5b693772014-11-21 06:19:36 -080049
Robert Phillipsf0288102020-07-06 13:45:34 -040050 const bool gpu = canvas->recordingContext() != nullptr;
mtkleinbf9e6002015-06-16 10:41:27 -070051 int tileW = gpu ? FLAGS_GPUbenchTileW : FLAGS_CPUbenchTileW,
52 tileH = gpu ? FLAGS_GPUbenchTileH : FLAGS_CPUbenchTileH;
53
Brian Osman788b9162020-02-07 10:36:46 -050054 tileW = std::min(tileW, bounds.width());
55 tileH = std::min(tileH, bounds.height());
bsalomoncc4d6672015-03-05 13:42:27 -080056
57 int xTiles = SkScalarCeilToInt(bounds.width() / SkIntToScalar(tileW));
58 int yTiles = SkScalarCeilToInt(bounds.height() / SkIntToScalar(tileH));
robertphillips5b693772014-11-21 06:19:36 -080059
John Stilesf4bda742020-10-14 16:57:41 -040060 fSurfaces.reserve_back(xTiles * yTiles);
robertphillips5b693772014-11-21 06:19:36 -080061 fTileRects.setReserve(xTiles * yTiles);
62
bsalomoncc4d6672015-03-05 13:42:27 -080063 SkImageInfo ii = canvas->imageInfo().makeWH(tileW, tileH);
robertphillips5b693772014-11-21 06:19:36 -080064
bsalomoncc4d6672015-03-05 13:42:27 -080065 for (int y = bounds.fTop; y < bounds.fBottom; y += tileH) {
66 for (int x = bounds.fLeft; x < bounds.fRight; x += tileW) {
67 const SkIRect tileRect = SkIRect::MakeXYWH(x, y, tileW, tileH);
robertphillips63242d72014-12-04 08:31:02 -080068 *fTileRects.append() = tileRect;
Ben Wagner9ec70c62018-07-12 13:30:47 -040069 fSurfaces.emplace_back(canvas->makeSurface(ii));
robertphillips63242d72014-12-04 08:31:02 -080070
71 // Never want the contents of a tile to include stuff the parent
72 // canvas clips out
73 SkRect clip = SkRect::Make(bounds);
74 clip.offset(-SkIntToScalar(tileRect.fLeft), -SkIntToScalar(tileRect.fTop));
Ben Wagner9ec70c62018-07-12 13:30:47 -040075 fSurfaces.back()->getCanvas()->clipRect(clip);
robertphillips63242d72014-12-04 08:31:02 -080076
Ben Wagner9ec70c62018-07-12 13:30:47 -040077 fSurfaces.back()->getCanvas()->setMatrix(canvas->getTotalMatrix());
78 fSurfaces.back()->getCanvas()->scale(fScale, fScale);
robertphillips5b693772014-11-21 06:19:36 -080079 }
80 }
81}
82
83void SKPBench::onPerCanvasPostDraw(SkCanvas* canvas) {
Leon Scroggins III1ff07062020-07-27 14:52:19 -040084 // Draw the last set of tiles into the main canvas in case we're
robertphillips5b693772014-11-21 06:19:36 -080085 // saving the images
86 for (int i = 0; i < fTileRects.count(); ++i) {
reed9ce9d672016-03-17 10:51:11 -070087 sk_sp<SkImage> image(fSurfaces[i]->makeImageSnapshot());
robertphillips186a08e2014-11-21 06:53:00 -080088 canvas->drawImage(image,
robertphillips5b693772014-11-21 06:19:36 -080089 SkIntToScalar(fTileRects[i].fLeft), SkIntToScalar(fTileRects[i].fTop));
robertphillips5b693772014-11-21 06:19:36 -080090 }
91
Ben Wagner9ec70c62018-07-12 13:30:47 -040092 fSurfaces.reset();
robertphillips5b693772014-11-21 06:19:36 -080093 fTileRects.rewind();
94}
95
mtklein92007582014-08-01 07:46:52 -070096bool SKPBench::isSuitableFor(Backend backend) {
97 return backend != kNonRendering_Backend;
98}
99
100SkIPoint SKPBench::onGetSize() {
101 return SkIPoint::Make(fClip.width(), fClip.height());
102}
103
mtkleina1ebeb22015-10-01 09:43:39 -0700104void SKPBench::onDraw(int loops, SkCanvas* canvas) {
cdaltonb4022962015-06-25 10:51:56 -0700105 SkASSERT(fDoLooping || 1 == loops);
cdalton31c45bb2016-02-22 08:08:25 -0800106 while (1) {
Brian Salomon626349b2020-08-27 10:54:36 -0400107 this->drawPicture();
cdalton31c45bb2016-02-22 08:08:25 -0800108 if (0 == --loops) {
109 break;
110 }
Robert Phillipsf0288102020-07-06 13:45:34 -0400111
112 auto direct = canvas->recordingContext() ? canvas->recordingContext()->asDirectContext()
113 : nullptr;
Brian Salomon09d994e2016-12-21 11:14:46 -0500114 // Ensure the GrContext doesn't combine ops across draw loops.
Robert Phillipsf0288102020-07-06 13:45:34 -0400115 if (direct) {
116 direct->flushAndSubmit();
cdalton31c45bb2016-02-22 08:08:25 -0800117 }
cdalton31c45bb2016-02-22 08:08:25 -0800118 }
mtklein92007582014-08-01 07:46:52 -0700119}
joshualitt261c3ad2015-04-27 09:16:57 -0700120
121void SKPBench::drawMPDPicture() {
Mike Reed2319b802019-11-15 10:39:50 -0500122 // TODO: remove me
joshualitt261c3ad2015-04-27 09:16:57 -0700123}
124
125void SKPBench::drawPicture() {
126 for (int j = 0; j < fTileRects.count(); ++j) {
Mike Reed1f607332020-05-21 12:11:27 -0400127 const SkMatrix trans = SkMatrix::Translate(-fTileRects[j].fLeft / fScale,
joshualitt261c3ad2015-04-27 09:16:57 -0700128 -fTileRects[j].fTop / fScale);
Hal Canary2db83612016-11-04 13:02:54 -0400129 fSurfaces[j]->getCanvas()->drawPicture(fPic.get(), &trans, nullptr);
joshualitt261c3ad2015-04-27 09:16:57 -0700130 }
131
132 for (int j = 0; j < fTileRects.count(); ++j) {
133 fSurfaces[j]->getCanvas()->flush();
134 }
135}
joshualitte45c81c2015-12-02 09:05:37 -0800136
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500137#include "src/gpu/GrGpu.h"
Robert Phillipsf0288102020-07-06 13:45:34 -0400138static void draw_pic_for_stats(SkCanvas* canvas, GrDirectContext* context, const SkPicture* picture,
Brian Osmanf2ae6af2019-03-25 13:32:28 -0400139 SkTArray<SkString>* keys, SkTArray<double>* values) {
Robert Phillips9da87e02019-02-04 13:26:26 -0500140 context->priv().resetGpuStats();
Robert Phillips273f1072020-05-05 13:03:07 -0400141 context->priv().resetContextStats();
joshualitte45c81c2015-12-02 09:05:37 -0800142 canvas->drawPicture(picture);
143 canvas->flush();
144
Robert Phillips9da87e02019-02-04 13:26:26 -0500145 context->priv().dumpGpuStatsKeyValuePairs(keys, values);
146 context->priv().dumpCacheStatsKeyValuePairs(keys, values);
Robert Phillips273f1072020-05-05 13:03:07 -0400147 context->priv().dumpContextStatsKeyValuePairs(keys, values);
joshualitte45c81c2015-12-02 09:05:37 -0800148}
joshualitte45c81c2015-12-02 09:05:37 -0800149
150void SKPBench::getGpuStats(SkCanvas* canvas, SkTArray<SkString>* keys, SkTArray<double>* values) {
joshualitte45c81c2015-12-02 09:05:37 -0800151 // we do a special single draw and then dump the key / value pairs
Robert Phillipsf0288102020-07-06 13:45:34 -0400152 auto direct = canvas->recordingContext() ? canvas->recordingContext()->asDirectContext()
153 : nullptr;
154 if (!direct) {
joshualitte45c81c2015-12-02 09:05:37 -0800155 return;
156 }
157
158 // TODO refactor this out if we want to test other subclasses of skpbench
Robert Phillipsf0288102020-07-06 13:45:34 -0400159 direct->flushAndSubmit();
160 direct->freeGpuResources();
161 direct->resetContext();
162 direct->priv().getGpu()->resetShaderCacheForTesting();
163 draw_pic_for_stats(canvas, direct, fPic.get(), keys, values);
joshualitte45c81c2015-12-02 09:05:37 -0800164}