blob: 9fe11f8ffb5d93bbf81702ef5eb50b9920e9f68e [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"
10#include "tools/flags/CommandLineFlags.h"
mtkleinc7f7f462014-10-21 12:29:25 -070011
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "include/gpu/GrContext.h"
13#include "src/gpu/GrContextPriv.h"
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,
cdaltonb4022962015-06-25 10:51:56 -070023 bool useMultiPictureDraw, 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 , fUseMultiPictureDraw(useMultiPictureDraw)
29 , fDoLooping(doLooping) {
tfarina0004e7d2015-01-26 06:47:55 -080030 fUniqueName.printf("%s_%.2g", name, scale); // Scale makes this unqiue for perf.skia.org traces.
robertphillips5b693772014-11-21 06:19:36 -080031 if (useMultiPictureDraw) {
32 fUniqueName.append("_mpd");
33 }
34}
35
36SKPBench::~SKPBench() {
37 for (int i = 0; i < fSurfaces.count(); ++i) {
38 fSurfaces[i]->unref();
39 }
mtklein92007582014-08-01 07:46:52 -070040}
41
42const char* SKPBench::onGetName() {
43 return fName.c_str();
44}
45
mtklein96289052014-09-10 12:05:59 -070046const char* SKPBench::onGetUniqueName() {
47 return fUniqueName.c_str();
48}
49
robertphillips5b693772014-11-21 06:19:36 -080050void SKPBench::onPerCanvasPreDraw(SkCanvas* canvas) {
Mike Reed918e1442017-01-23 11:39:45 -050051 SkIRect bounds = canvas->getDeviceClipBounds();
52 SkAssertResult(!bounds.isEmpty());
robertphillips5b693772014-11-21 06:19:36 -080053
mtkleinbf9e6002015-06-16 10:41:27 -070054 const bool gpu = canvas->getGrContext() != nullptr;
55 int tileW = gpu ? FLAGS_GPUbenchTileW : FLAGS_CPUbenchTileW,
56 tileH = gpu ? FLAGS_GPUbenchTileH : FLAGS_CPUbenchTileH;
57
58 tileW = SkTMin(tileW, bounds.width());
59 tileH = SkTMin(tileH, bounds.height());
bsalomoncc4d6672015-03-05 13:42:27 -080060
61 int xTiles = SkScalarCeilToInt(bounds.width() / SkIntToScalar(tileW));
62 int yTiles = SkScalarCeilToInt(bounds.height() / SkIntToScalar(tileH));
robertphillips5b693772014-11-21 06:19:36 -080063
Ben Wagner9ec70c62018-07-12 13:30:47 -040064 fSurfaces.reserve(xTiles * yTiles);
robertphillips5b693772014-11-21 06:19:36 -080065 fTileRects.setReserve(xTiles * yTiles);
66
bsalomoncc4d6672015-03-05 13:42:27 -080067 SkImageInfo ii = canvas->imageInfo().makeWH(tileW, tileH);
robertphillips5b693772014-11-21 06:19:36 -080068
bsalomoncc4d6672015-03-05 13:42:27 -080069 for (int y = bounds.fTop; y < bounds.fBottom; y += tileH) {
70 for (int x = bounds.fLeft; x < bounds.fRight; x += tileW) {
71 const SkIRect tileRect = SkIRect::MakeXYWH(x, y, tileW, tileH);
robertphillips63242d72014-12-04 08:31:02 -080072 *fTileRects.append() = tileRect;
Ben Wagner9ec70c62018-07-12 13:30:47 -040073 fSurfaces.emplace_back(canvas->makeSurface(ii));
robertphillips63242d72014-12-04 08:31:02 -080074
75 // Never want the contents of a tile to include stuff the parent
76 // canvas clips out
77 SkRect clip = SkRect::Make(bounds);
78 clip.offset(-SkIntToScalar(tileRect.fLeft), -SkIntToScalar(tileRect.fTop));
Ben Wagner9ec70c62018-07-12 13:30:47 -040079 fSurfaces.back()->getCanvas()->clipRect(clip);
robertphillips63242d72014-12-04 08:31:02 -080080
Ben Wagner9ec70c62018-07-12 13:30:47 -040081 fSurfaces.back()->getCanvas()->setMatrix(canvas->getTotalMatrix());
82 fSurfaces.back()->getCanvas()->scale(fScale, fScale);
robertphillips5b693772014-11-21 06:19:36 -080083 }
84 }
85}
86
87void SKPBench::onPerCanvasPostDraw(SkCanvas* canvas) {
robertphillips5b693772014-11-21 06:19:36 -080088 // Draw the last set of tiles into the master canvas in case we're
89 // saving the images
90 for (int i = 0; i < fTileRects.count(); ++i) {
reed9ce9d672016-03-17 10:51:11 -070091 sk_sp<SkImage> image(fSurfaces[i]->makeImageSnapshot());
robertphillips186a08e2014-11-21 06:53:00 -080092 canvas->drawImage(image,
robertphillips5b693772014-11-21 06:19:36 -080093 SkIntToScalar(fTileRects[i].fLeft), SkIntToScalar(fTileRects[i].fTop));
robertphillips5b693772014-11-21 06:19:36 -080094 }
95
Ben Wagner9ec70c62018-07-12 13:30:47 -040096 fSurfaces.reset();
robertphillips5b693772014-11-21 06:19:36 -080097 fTileRects.rewind();
98}
99
mtklein92007582014-08-01 07:46:52 -0700100bool SKPBench::isSuitableFor(Backend backend) {
101 return backend != kNonRendering_Backend;
102}
103
104SkIPoint SKPBench::onGetSize() {
105 return SkIPoint::Make(fClip.width(), fClip.height());
106}
107
mtkleina1ebeb22015-10-01 09:43:39 -0700108void SKPBench::onDraw(int loops, SkCanvas* canvas) {
cdaltonb4022962015-06-25 10:51:56 -0700109 SkASSERT(fDoLooping || 1 == loops);
cdalton31c45bb2016-02-22 08:08:25 -0800110 while (1) {
111 if (fUseMultiPictureDraw) {
joshualitt261c3ad2015-04-27 09:16:57 -0700112 this->drawMPDPicture();
cdalton31c45bb2016-02-22 08:08:25 -0800113 } else {
joshualitt261c3ad2015-04-27 09:16:57 -0700114 this->drawPicture();
robertphillips5b693772014-11-21 06:19:36 -0800115 }
cdalton31c45bb2016-02-22 08:08:25 -0800116 if (0 == --loops) {
117 break;
118 }
Brian Salomon09d994e2016-12-21 11:14:46 -0500119 // Ensure the GrContext doesn't combine ops across draw loops.
cdalton31c45bb2016-02-22 08:08:25 -0800120 if (GrContext* context = canvas->getGrContext()) {
121 context->flush();
122 }
cdalton31c45bb2016-02-22 08:08:25 -0800123 }
mtklein92007582014-08-01 07:46:52 -0700124}
joshualitt261c3ad2015-04-27 09:16:57 -0700125
126void SKPBench::drawMPDPicture() {
Mike Reed2319b802019-11-15 10:39:50 -0500127 // TODO: remove me
joshualitt261c3ad2015-04-27 09:16:57 -0700128}
129
130void SKPBench::drawPicture() {
131 for (int j = 0; j < fTileRects.count(); ++j) {
132 const SkMatrix trans = SkMatrix::MakeTrans(-fTileRects[j].fLeft / fScale,
133 -fTileRects[j].fTop / fScale);
Hal Canary2db83612016-11-04 13:02:54 -0400134 fSurfaces[j]->getCanvas()->drawPicture(fPic.get(), &trans, nullptr);
joshualitt261c3ad2015-04-27 09:16:57 -0700135 }
136
137 for (int j = 0; j < fTileRects.count(); ++j) {
138 fSurfaces[j]->getCanvas()->flush();
139 }
140}
joshualitte45c81c2015-12-02 09:05:37 -0800141
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500142#include "src/gpu/GrGpu.h"
joshualitte45c81c2015-12-02 09:05:37 -0800143static void draw_pic_for_stats(SkCanvas* canvas, GrContext* context, const SkPicture* picture,
Brian Osmanf2ae6af2019-03-25 13:32:28 -0400144 SkTArray<SkString>* keys, SkTArray<double>* values) {
Robert Phillips9da87e02019-02-04 13:26:26 -0500145 context->priv().resetGpuStats();
joshualitte45c81c2015-12-02 09:05:37 -0800146 canvas->drawPicture(picture);
147 canvas->flush();
148
Robert Phillips9da87e02019-02-04 13:26:26 -0500149 context->priv().dumpGpuStatsKeyValuePairs(keys, values);
150 context->priv().dumpCacheStatsKeyValuePairs(keys, values);
joshualitte45c81c2015-12-02 09:05:37 -0800151}
joshualitte45c81c2015-12-02 09:05:37 -0800152
153void SKPBench::getGpuStats(SkCanvas* canvas, SkTArray<SkString>* keys, SkTArray<double>* values) {
joshualitte45c81c2015-12-02 09:05:37 -0800154 // we do a special single draw and then dump the key / value pairs
155 GrContext* context = canvas->getGrContext();
156 if (!context) {
157 return;
158 }
159
160 // TODO refactor this out if we want to test other subclasses of skpbench
161 context->flush();
162 context->freeGpuResources();
163 context->resetContext();
Robert Phillips9da87e02019-02-04 13:26:26 -0500164 context->priv().getGpu()->resetShaderCacheForTesting();
Brian Osmanf2ae6af2019-03-25 13:32:28 -0400165 draw_pic_for_stats(canvas, context, fPic.get(), keys, values);
joshualitte45c81c2015-12-02 09:05:37 -0800166}