blob: 764119685d902cb9806b756dc518e1325ee276f7 [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
8#include "SKPBench.h"
mtkleinc7f7f462014-10-21 12:29:25 -07009#include "SkCommandLineFlags.h"
robertphillips5b693772014-11-21 06:19:36 -080010#include "SkMultiPictureDraw.h"
11#include "SkSurface.h"
mtkleinc7f7f462014-10-21 12:29:25 -070012
cdaltone6d20242015-10-26 13:45:29 -070013#include "GrContext.h"
Robert Phillipsf35fd8d2018-01-22 10:48:15 -050014#include "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.
17DEFINE_int32(CPUbenchTileW, 256, "Tile width used for CPU SKP playback.");
18DEFINE_int32(CPUbenchTileH, 256, "Tile height used for CPU SKP playback.");
19
20DEFINE_int32(GPUbenchTileW, 1600, "Tile width used for GPU SKP playback.");
21DEFINE_int32(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
137 mpd.draw();
138
139 for (int j = 0; j < fTileRects.count(); ++j) {
140 fSurfaces[j]->getCanvas()->flush();
141 }
142}
143
144void SKPBench::drawPicture() {
145 for (int j = 0; j < fTileRects.count(); ++j) {
146 const SkMatrix trans = SkMatrix::MakeTrans(-fTileRects[j].fLeft / fScale,
147 -fTileRects[j].fTop / fScale);
Hal Canary2db83612016-11-04 13:02:54 -0400148 fSurfaces[j]->getCanvas()->drawPicture(fPic.get(), &trans, nullptr);
joshualitt261c3ad2015-04-27 09:16:57 -0700149 }
150
151 for (int j = 0; j < fTileRects.count(); ++j) {
152 fSurfaces[j]->getCanvas()->flush();
153 }
154}
joshualitte45c81c2015-12-02 09:05:37 -0800155
joshualitt8fd844f2015-12-02 13:36:47 -0800156#include "GrGpu.h"
joshualitte45c81c2015-12-02 09:05:37 -0800157static void draw_pic_for_stats(SkCanvas* canvas, GrContext* context, const SkPicture* picture,
158 SkTArray<SkString>* keys, SkTArray<double>* values,
159 const char* tag) {
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500160 context->contextPriv().resetGpuStats();
joshualitte45c81c2015-12-02 09:05:37 -0800161 canvas->drawPicture(picture);
162 canvas->flush();
163
164 int offset = keys->count();
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500165 context->contextPriv().dumpGpuStatsKeyValuePairs(keys, values);
166 context->contextPriv().dumpCacheStatsKeyValuePairs(keys, values);
joshualitte45c81c2015-12-02 09:05:37 -0800167
168 // append tag, but only to new tags
169 for (int i = offset; i < keys->count(); i++, offset++) {
170 (*keys)[i].appendf("_%s", tag);
171 }
172}
joshualitte45c81c2015-12-02 09:05:37 -0800173
174void SKPBench::getGpuStats(SkCanvas* canvas, SkTArray<SkString>* keys, SkTArray<double>* values) {
joshualitte45c81c2015-12-02 09:05:37 -0800175 // we do a special single draw and then dump the key / value pairs
176 GrContext* context = canvas->getGrContext();
177 if (!context) {
178 return;
179 }
180
181 // TODO refactor this out if we want to test other subclasses of skpbench
182 context->flush();
183 context->freeGpuResources();
184 context->resetContext();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500185 context->contextPriv().getGpu()->resetShaderCacheForTesting();
Hal Canary2db83612016-11-04 13:02:54 -0400186 draw_pic_for_stats(canvas, context, fPic.get(), keys, values, "first_frame");
joshualitte45c81c2015-12-02 09:05:37 -0800187
188 // draw second frame
Hal Canary2db83612016-11-04 13:02:54 -0400189 draw_pic_for_stats(canvas, context, fPic.get(), keys, values, "second_frame");
joshualitte45c81c2015-12-02 09:05:37 -0800190}