blob: fdc87db784db953430d00f86e3d61844105bfb94 [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();
Chris Dalton2579c282021-04-26 23:09:31 -060048 bounds.intersect(fClip);
49 bounds.intersect(fPic->cullRect().roundOut());
Mike Reed918e1442017-01-23 11:39:45 -050050 SkAssertResult(!bounds.isEmpty());
robertphillips5b693772014-11-21 06:19:36 -080051
Robert Phillipsf0288102020-07-06 13:45:34 -040052 const bool gpu = canvas->recordingContext() != nullptr;
mtkleinbf9e6002015-06-16 10:41:27 -070053 int tileW = gpu ? FLAGS_GPUbenchTileW : FLAGS_CPUbenchTileW,
54 tileH = gpu ? FLAGS_GPUbenchTileH : FLAGS_CPUbenchTileH;
55
Brian Osman788b9162020-02-07 10:36:46 -050056 tileW = std::min(tileW, bounds.width());
57 tileH = std::min(tileH, bounds.height());
bsalomoncc4d6672015-03-05 13:42:27 -080058
59 int xTiles = SkScalarCeilToInt(bounds.width() / SkIntToScalar(tileW));
60 int yTiles = SkScalarCeilToInt(bounds.height() / SkIntToScalar(tileH));
robertphillips5b693772014-11-21 06:19:36 -080061
John Stilesf4bda742020-10-14 16:57:41 -040062 fSurfaces.reserve_back(xTiles * yTiles);
robertphillips5b693772014-11-21 06:19:36 -080063 fTileRects.setReserve(xTiles * yTiles);
64
bsalomoncc4d6672015-03-05 13:42:27 -080065 SkImageInfo ii = canvas->imageInfo().makeWH(tileW, tileH);
robertphillips5b693772014-11-21 06:19:36 -080066
bsalomoncc4d6672015-03-05 13:42:27 -080067 for (int y = bounds.fTop; y < bounds.fBottom; y += tileH) {
68 for (int x = bounds.fLeft; x < bounds.fRight; x += tileW) {
69 const SkIRect tileRect = SkIRect::MakeXYWH(x, y, tileW, tileH);
robertphillips63242d72014-12-04 08:31:02 -080070 *fTileRects.append() = tileRect;
Ben Wagner9ec70c62018-07-12 13:30:47 -040071 fSurfaces.emplace_back(canvas->makeSurface(ii));
robertphillips63242d72014-12-04 08:31:02 -080072
73 // Never want the contents of a tile to include stuff the parent
74 // canvas clips out
75 SkRect clip = SkRect::Make(bounds);
76 clip.offset(-SkIntToScalar(tileRect.fLeft), -SkIntToScalar(tileRect.fTop));
Ben Wagner9ec70c62018-07-12 13:30:47 -040077 fSurfaces.back()->getCanvas()->clipRect(clip);
robertphillips63242d72014-12-04 08:31:02 -080078
Mike Reed1a4140e2020-12-03 11:21:31 -050079 fSurfaces.back()->getCanvas()->setMatrix(canvas->getLocalToDevice());
Ben Wagner9ec70c62018-07-12 13:30:47 -040080 fSurfaces.back()->getCanvas()->scale(fScale, fScale);
robertphillips5b693772014-11-21 06:19:36 -080081 }
82 }
83}
84
85void SKPBench::onPerCanvasPostDraw(SkCanvas* canvas) {
Leon Scroggins III1ff07062020-07-27 14:52:19 -040086 // Draw the last set of tiles into the main canvas in case we're
robertphillips5b693772014-11-21 06:19:36 -080087 // saving the images
88 for (int i = 0; i < fTileRects.count(); ++i) {
reed9ce9d672016-03-17 10:51:11 -070089 sk_sp<SkImage> image(fSurfaces[i]->makeImageSnapshot());
robertphillips186a08e2014-11-21 06:53:00 -080090 canvas->drawImage(image,
robertphillips5b693772014-11-21 06:19:36 -080091 SkIntToScalar(fTileRects[i].fLeft), SkIntToScalar(fTileRects[i].fTop));
robertphillips5b693772014-11-21 06:19:36 -080092 }
93
Ben Wagner9ec70c62018-07-12 13:30:47 -040094 fSurfaces.reset();
robertphillips5b693772014-11-21 06:19:36 -080095 fTileRects.rewind();
96}
97
mtklein92007582014-08-01 07:46:52 -070098bool SKPBench::isSuitableFor(Backend backend) {
99 return backend != kNonRendering_Backend;
100}
101
102SkIPoint SKPBench::onGetSize() {
103 return SkIPoint::Make(fClip.width(), fClip.height());
104}
105
mtkleina1ebeb22015-10-01 09:43:39 -0700106void SKPBench::onDraw(int loops, SkCanvas* canvas) {
cdaltonb4022962015-06-25 10:51:56 -0700107 SkASSERT(fDoLooping || 1 == loops);
cdalton31c45bb2016-02-22 08:08:25 -0800108 while (1) {
Brian Salomon626349b2020-08-27 10:54:36 -0400109 this->drawPicture();
cdalton31c45bb2016-02-22 08:08:25 -0800110 if (0 == --loops) {
111 break;
112 }
Robert Phillipsf0288102020-07-06 13:45:34 -0400113
114 auto direct = canvas->recordingContext() ? canvas->recordingContext()->asDirectContext()
115 : nullptr;
Brian Salomon09d994e2016-12-21 11:14:46 -0500116 // Ensure the GrContext doesn't combine ops across draw loops.
Robert Phillipsf0288102020-07-06 13:45:34 -0400117 if (direct) {
118 direct->flushAndSubmit();
cdalton31c45bb2016-02-22 08:08:25 -0800119 }
cdalton31c45bb2016-02-22 08:08:25 -0800120 }
mtklein92007582014-08-01 07:46:52 -0700121}
joshualitt261c3ad2015-04-27 09:16:57 -0700122
123void SKPBench::drawMPDPicture() {
Mike Reed2319b802019-11-15 10:39:50 -0500124 // TODO: remove me
joshualitt261c3ad2015-04-27 09:16:57 -0700125}
126
127void SKPBench::drawPicture() {
128 for (int j = 0; j < fTileRects.count(); ++j) {
Mike Reed1f607332020-05-21 12:11:27 -0400129 const SkMatrix trans = SkMatrix::Translate(-fTileRects[j].fLeft / fScale,
joshualitt261c3ad2015-04-27 09:16:57 -0700130 -fTileRects[j].fTop / fScale);
Hal Canary2db83612016-11-04 13:02:54 -0400131 fSurfaces[j]->getCanvas()->drawPicture(fPic.get(), &trans, nullptr);
joshualitt261c3ad2015-04-27 09:16:57 -0700132 }
133
134 for (int j = 0; j < fTileRects.count(); ++j) {
Robert Phillips2c21a112020-11-20 13:49:37 -0500135 fSurfaces[j]->flush();
joshualitt261c3ad2015-04-27 09:16:57 -0700136 }
137}
joshualitte45c81c2015-12-02 09:05:37 -0800138
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500139#include "src/gpu/GrGpu.h"
Robert Phillips2c21a112020-11-20 13:49:37 -0500140static void draw_pic_for_stats(SkCanvas* canvas,
141 GrDirectContext* dContext,
142 const SkPicture* picture,
143 SkTArray<SkString>* keys,
144 SkTArray<double>* values) {
145 dContext->priv().resetGpuStats();
146 dContext->priv().resetContextStats();
joshualitte45c81c2015-12-02 09:05:37 -0800147 canvas->drawPicture(picture);
Robert Phillips2c21a112020-11-20 13:49:37 -0500148 dContext->flush();
joshualitte45c81c2015-12-02 09:05:37 -0800149
Robert Phillips2c21a112020-11-20 13:49:37 -0500150 dContext->priv().dumpGpuStatsKeyValuePairs(keys, values);
151 dContext->priv().dumpCacheStatsKeyValuePairs(keys, values);
152 dContext->priv().dumpContextStatsKeyValuePairs(keys, values);
joshualitte45c81c2015-12-02 09:05:37 -0800153}
joshualitte45c81c2015-12-02 09:05:37 -0800154
155void SKPBench::getGpuStats(SkCanvas* canvas, SkTArray<SkString>* keys, SkTArray<double>* values) {
joshualitte45c81c2015-12-02 09:05:37 -0800156 // we do a special single draw and then dump the key / value pairs
Robert Phillipsf0288102020-07-06 13:45:34 -0400157 auto direct = canvas->recordingContext() ? canvas->recordingContext()->asDirectContext()
158 : nullptr;
159 if (!direct) {
joshualitte45c81c2015-12-02 09:05:37 -0800160 return;
161 }
162
163 // TODO refactor this out if we want to test other subclasses of skpbench
Robert Phillipsf0288102020-07-06 13:45:34 -0400164 direct->flushAndSubmit();
165 direct->freeGpuResources();
166 direct->resetContext();
167 direct->priv().getGpu()->resetShaderCacheForTesting();
168 draw_pic_for_stats(canvas, direct, fPic.get(), keys, values);
joshualitte45c81c2015-12-02 09:05:37 -0800169}
Chris Dalton5dfb3f42021-04-30 17:16:12 -0600170
171bool SKPBench::getDMSAAStats(GrRecordingContext* rContext) {
172 if (!rContext || !rContext->asDirectContext()) {
173 return false;
174 }
175 // Clear the current DMSAA stats then do a single tiled draw that resets them to the specific
176 // values for our SKP.
177 rContext->asDirectContext()->flushAndSubmit();
178 rContext->priv().dmsaaStats() = {};
179 this->drawPicture(); // Draw tiled for DMSAA stats.
180 rContext->asDirectContext()->flush();
181 return true;
182}