blob: 854317434e397b90f3d02fdc78459816fe3f2c7c [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"
9#include "include/core/SkMultiPictureDraw.h"
10#include "include/core/SkSurface.h"
11#include "tools/flags/CommandLineFlags.h"
mtkleinc7f7f462014-10-21 12:29:25 -070012
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "include/gpu/GrContext.h"
14#include "src/gpu/GrContextPriv.h"
cdaltone6d20242015-10-26 13:45:29 -070015
mtkleinbf9e6002015-06-16 10:41:27 -070016// These CPU tile sizes are not good per se, but they are similar to what Chrome uses.
Mike Klein5b3f3432019-03-21 11:42:21 -050017static DEFINE_int(CPUbenchTileW, 256, "Tile width used for CPU SKP playback.");
18static DEFINE_int(CPUbenchTileH, 256, "Tile height used for CPU SKP playback.");
mtkleinbf9e6002015-06-16 10:41:27 -070019
Mike Klein5b3f3432019-03-21 11:42:21 -050020static DEFINE_int(GPUbenchTileW, 1600, "Tile width used for GPU SKP playback.");
21static DEFINE_int(GPUbenchTileH, 512, "Tile height used for GPU SKP playback.");
mtklein92007582014-08-01 07:46:52 -070022
robertphillips5b693772014-11-21 06:19:36 -080023SKPBench::SKPBench(const char* name, const SkPicture* pic, const SkIRect& clip, SkScalar scale,
cdaltonb4022962015-06-25 10:51:56 -070024 bool useMultiPictureDraw, bool doLooping)
mtklein92007582014-08-01 07:46:52 -070025 : fPic(SkRef(pic))
26 , fClip(clip)
mtklein96289052014-09-10 12:05:59 -070027 , fScale(scale)
robertphillips5b693772014-11-21 06:19:36 -080028 , fName(name)
cdaltonb4022962015-06-25 10:51:56 -070029 , fUseMultiPictureDraw(useMultiPictureDraw)
30 , fDoLooping(doLooping) {
tfarina0004e7d2015-01-26 06:47:55 -080031 fUniqueName.printf("%s_%.2g", name, scale); // Scale makes this unqiue for perf.skia.org traces.
robertphillips5b693772014-11-21 06:19:36 -080032 if (useMultiPictureDraw) {
33 fUniqueName.append("_mpd");
34 }
35}
36
37SKPBench::~SKPBench() {
38 for (int i = 0; i < fSurfaces.count(); ++i) {
39 fSurfaces[i]->unref();
40 }
mtklein92007582014-08-01 07:46:52 -070041}
42
43const char* SKPBench::onGetName() {
44 return fName.c_str();
45}
46
mtklein96289052014-09-10 12:05:59 -070047const char* SKPBench::onGetUniqueName() {
48 return fUniqueName.c_str();
49}
50
robertphillips5b693772014-11-21 06:19:36 -080051void SKPBench::onPerCanvasPreDraw(SkCanvas* canvas) {
Mike Reed918e1442017-01-23 11:39:45 -050052 SkIRect bounds = canvas->getDeviceClipBounds();
53 SkAssertResult(!bounds.isEmpty());
robertphillips5b693772014-11-21 06:19:36 -080054
mtkleinbf9e6002015-06-16 10:41:27 -070055 const bool gpu = canvas->getGrContext() != nullptr;
56 int tileW = gpu ? FLAGS_GPUbenchTileW : FLAGS_CPUbenchTileW,
57 tileH = gpu ? FLAGS_GPUbenchTileH : FLAGS_CPUbenchTileH;
58
59 tileW = SkTMin(tileW, bounds.width());
60 tileH = SkTMin(tileH, bounds.height());
bsalomoncc4d6672015-03-05 13:42:27 -080061
62 int xTiles = SkScalarCeilToInt(bounds.width() / SkIntToScalar(tileW));
63 int yTiles = SkScalarCeilToInt(bounds.height() / SkIntToScalar(tileH));
robertphillips5b693772014-11-21 06:19:36 -080064
Ben Wagner9ec70c62018-07-12 13:30:47 -040065 fSurfaces.reserve(xTiles * yTiles);
robertphillips5b693772014-11-21 06:19:36 -080066 fTileRects.setReserve(xTiles * yTiles);
67
bsalomoncc4d6672015-03-05 13:42:27 -080068 SkImageInfo ii = canvas->imageInfo().makeWH(tileW, tileH);
robertphillips5b693772014-11-21 06:19:36 -080069
bsalomoncc4d6672015-03-05 13:42:27 -080070 for (int y = bounds.fTop; y < bounds.fBottom; y += tileH) {
71 for (int x = bounds.fLeft; x < bounds.fRight; x += tileW) {
72 const SkIRect tileRect = SkIRect::MakeXYWH(x, y, tileW, tileH);
robertphillips63242d72014-12-04 08:31:02 -080073 *fTileRects.append() = tileRect;
Ben Wagner9ec70c62018-07-12 13:30:47 -040074 fSurfaces.emplace_back(canvas->makeSurface(ii));
robertphillips63242d72014-12-04 08:31:02 -080075
76 // Never want the contents of a tile to include stuff the parent
77 // canvas clips out
78 SkRect clip = SkRect::Make(bounds);
79 clip.offset(-SkIntToScalar(tileRect.fLeft), -SkIntToScalar(tileRect.fTop));
Ben Wagner9ec70c62018-07-12 13:30:47 -040080 fSurfaces.back()->getCanvas()->clipRect(clip);
robertphillips63242d72014-12-04 08:31:02 -080081
Ben Wagner9ec70c62018-07-12 13:30:47 -040082 fSurfaces.back()->getCanvas()->setMatrix(canvas->getTotalMatrix());
83 fSurfaces.back()->getCanvas()->scale(fScale, fScale);
robertphillips5b693772014-11-21 06:19:36 -080084 }
85 }
86}
87
88void SKPBench::onPerCanvasPostDraw(SkCanvas* canvas) {
robertphillips5b693772014-11-21 06:19:36 -080089 // Draw the last set of tiles into the master canvas in case we're
90 // saving the images
91 for (int i = 0; i < fTileRects.count(); ++i) {
reed9ce9d672016-03-17 10:51:11 -070092 sk_sp<SkImage> image(fSurfaces[i]->makeImageSnapshot());
robertphillips186a08e2014-11-21 06:53:00 -080093 canvas->drawImage(image,
robertphillips5b693772014-11-21 06:19:36 -080094 SkIntToScalar(fTileRects[i].fLeft), SkIntToScalar(fTileRects[i].fTop));
robertphillips5b693772014-11-21 06:19:36 -080095 }
96
Ben Wagner9ec70c62018-07-12 13:30:47 -040097 fSurfaces.reset();
robertphillips5b693772014-11-21 06:19:36 -080098 fTileRects.rewind();
99}
100
mtklein92007582014-08-01 07:46:52 -0700101bool SKPBench::isSuitableFor(Backend backend) {
102 return backend != kNonRendering_Backend;
103}
104
105SkIPoint SKPBench::onGetSize() {
106 return SkIPoint::Make(fClip.width(), fClip.height());
107}
108
mtkleina1ebeb22015-10-01 09:43:39 -0700109void SKPBench::onDraw(int loops, SkCanvas* canvas) {
cdaltonb4022962015-06-25 10:51:56 -0700110 SkASSERT(fDoLooping || 1 == loops);
cdalton31c45bb2016-02-22 08:08:25 -0800111 while (1) {
112 if (fUseMultiPictureDraw) {
joshualitt261c3ad2015-04-27 09:16:57 -0700113 this->drawMPDPicture();
cdalton31c45bb2016-02-22 08:08:25 -0800114 } else {
joshualitt261c3ad2015-04-27 09:16:57 -0700115 this->drawPicture();
robertphillips5b693772014-11-21 06:19:36 -0800116 }
cdalton31c45bb2016-02-22 08:08:25 -0800117 if (0 == --loops) {
118 break;
119 }
Brian Salomon09d994e2016-12-21 11:14:46 -0500120 // Ensure the GrContext doesn't combine ops across draw loops.
cdalton31c45bb2016-02-22 08:08:25 -0800121 if (GrContext* context = canvas->getGrContext()) {
122 context->flush();
123 }
cdalton31c45bb2016-02-22 08:08:25 -0800124 }
mtklein92007582014-08-01 07:46:52 -0700125}
joshualitt261c3ad2015-04-27 09:16:57 -0700126
127void SKPBench::drawMPDPicture() {
128 SkMultiPictureDraw mpd;
129
130 for (int j = 0; j < fTileRects.count(); ++j) {
131 SkMatrix trans;
132 trans.setTranslate(-fTileRects[j].fLeft/fScale,
133 -fTileRects[j].fTop/fScale);
Hal Canary2db83612016-11-04 13:02:54 -0400134 mpd.add(fSurfaces[j]->getCanvas(), fPic.get(), &trans);
joshualitt261c3ad2015-04-27 09:16:57 -0700135 }
136
Brian Salomondd791212018-10-31 12:45:12 -0400137 // We flush after each picture to more closely model how Chrome rasterizes tiles.
138 mpd.draw(/*flush = */ true);
joshualitt261c3ad2015-04-27 09:16:57 -0700139}
140
141void SKPBench::drawPicture() {
142 for (int j = 0; j < fTileRects.count(); ++j) {
143 const SkMatrix trans = SkMatrix::MakeTrans(-fTileRects[j].fLeft / fScale,
144 -fTileRects[j].fTop / fScale);
Hal Canary2db83612016-11-04 13:02:54 -0400145 fSurfaces[j]->getCanvas()->drawPicture(fPic.get(), &trans, nullptr);
joshualitt261c3ad2015-04-27 09:16:57 -0700146 }
147
148 for (int j = 0; j < fTileRects.count(); ++j) {
149 fSurfaces[j]->getCanvas()->flush();
150 }
151}
joshualitte45c81c2015-12-02 09:05:37 -0800152
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500153#include "src/gpu/GrGpu.h"
joshualitte45c81c2015-12-02 09:05:37 -0800154static void draw_pic_for_stats(SkCanvas* canvas, GrContext* context, const SkPicture* picture,
Brian Osmanf2ae6af2019-03-25 13:32:28 -0400155 SkTArray<SkString>* keys, SkTArray<double>* values) {
Robert Phillips9da87e02019-02-04 13:26:26 -0500156 context->priv().resetGpuStats();
joshualitte45c81c2015-12-02 09:05:37 -0800157 canvas->drawPicture(picture);
158 canvas->flush();
159
Robert Phillips9da87e02019-02-04 13:26:26 -0500160 context->priv().dumpGpuStatsKeyValuePairs(keys, values);
161 context->priv().dumpCacheStatsKeyValuePairs(keys, values);
joshualitte45c81c2015-12-02 09:05:37 -0800162}
joshualitte45c81c2015-12-02 09:05:37 -0800163
164void SKPBench::getGpuStats(SkCanvas* canvas, SkTArray<SkString>* keys, SkTArray<double>* values) {
joshualitte45c81c2015-12-02 09:05:37 -0800165 // we do a special single draw and then dump the key / value pairs
166 GrContext* context = canvas->getGrContext();
167 if (!context) {
168 return;
169 }
170
171 // TODO refactor this out if we want to test other subclasses of skpbench
172 context->flush();
173 context->freeGpuResources();
174 context->resetContext();
Robert Phillips9da87e02019-02-04 13:26:26 -0500175 context->priv().getGpu()->resetShaderCacheForTesting();
Brian Osmanf2ae6af2019-03-25 13:32:28 -0400176 draw_pic_for_stats(canvas, context, fPic.get(), keys, values);
joshualitte45c81c2015-12-02 09:05:37 -0800177}