blob: b4724195df27d84749dc7c3f4f32ed37e38710ae [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#if SK_SUPPORT_GPU
14#include "GrContext.h"
15#endif
16
mtkleinbf9e6002015-06-16 10:41:27 -070017// These CPU tile sizes are not good per se, but they are similar to what Chrome uses.
18DEFINE_int32(CPUbenchTileW, 256, "Tile width used for CPU SKP playback.");
19DEFINE_int32(CPUbenchTileH, 256, "Tile height used for CPU SKP playback.");
20
21DEFINE_int32(GPUbenchTileW, 1600, "Tile width used for GPU SKP playback.");
22DEFINE_int32(GPUbenchTileH, 512, "Tile height used for GPU SKP playback.");
mtklein92007582014-08-01 07:46:52 -070023
robertphillips5b693772014-11-21 06:19:36 -080024SKPBench::SKPBench(const char* name, const SkPicture* pic, const SkIRect& clip, SkScalar scale,
cdaltonb4022962015-06-25 10:51:56 -070025 bool useMultiPictureDraw, bool doLooping)
mtklein92007582014-08-01 07:46:52 -070026 : fPic(SkRef(pic))
27 , fClip(clip)
mtklein96289052014-09-10 12:05:59 -070028 , fScale(scale)
robertphillips5b693772014-11-21 06:19:36 -080029 , fName(name)
cdaltonb4022962015-06-25 10:51:56 -070030 , fUseMultiPictureDraw(useMultiPictureDraw)
31 , fDoLooping(doLooping) {
tfarina0004e7d2015-01-26 06:47:55 -080032 fUniqueName.printf("%s_%.2g", name, scale); // Scale makes this unqiue for perf.skia.org traces.
robertphillips5b693772014-11-21 06:19:36 -080033 if (useMultiPictureDraw) {
34 fUniqueName.append("_mpd");
35 }
36}
37
38SKPBench::~SKPBench() {
39 for (int i = 0; i < fSurfaces.count(); ++i) {
40 fSurfaces[i]->unref();
41 }
mtklein92007582014-08-01 07:46:52 -070042}
43
44const char* SKPBench::onGetName() {
45 return fName.c_str();
46}
47
mtklein96289052014-09-10 12:05:59 -070048const char* SKPBench::onGetUniqueName() {
49 return fUniqueName.c_str();
50}
51
robertphillips5b693772014-11-21 06:19:36 -080052void SKPBench::onPerCanvasPreDraw(SkCanvas* canvas) {
robertphillips5b693772014-11-21 06:19:36 -080053 SkIRect bounds;
54 SkAssertResult(canvas->getClipDeviceBounds(&bounds));
55
mtkleinbf9e6002015-06-16 10:41:27 -070056 const bool gpu = canvas->getGrContext() != nullptr;
57 int tileW = gpu ? FLAGS_GPUbenchTileW : FLAGS_CPUbenchTileW,
58 tileH = gpu ? FLAGS_GPUbenchTileH : FLAGS_CPUbenchTileH;
59
60 tileW = SkTMin(tileW, bounds.width());
61 tileH = SkTMin(tileH, bounds.height());
bsalomoncc4d6672015-03-05 13:42:27 -080062
63 int xTiles = SkScalarCeilToInt(bounds.width() / SkIntToScalar(tileW));
64 int yTiles = SkScalarCeilToInt(bounds.height() / SkIntToScalar(tileH));
robertphillips5b693772014-11-21 06:19:36 -080065
66 fSurfaces.setReserve(xTiles * yTiles);
67 fTileRects.setReserve(xTiles * yTiles);
68
bsalomoncc4d6672015-03-05 13:42:27 -080069 SkImageInfo ii = canvas->imageInfo().makeWH(tileW, tileH);
robertphillips5b693772014-11-21 06:19:36 -080070
bsalomoncc4d6672015-03-05 13:42:27 -080071 for (int y = bounds.fTop; y < bounds.fBottom; y += tileH) {
72 for (int x = bounds.fLeft; x < bounds.fRight; x += tileW) {
73 const SkIRect tileRect = SkIRect::MakeXYWH(x, y, tileW, tileH);
robertphillips63242d72014-12-04 08:31:02 -080074 *fTileRects.append() = tileRect;
robertphillips5b693772014-11-21 06:19:36 -080075 *fSurfaces.push() = canvas->newSurface(ii);
robertphillips63242d72014-12-04 08:31:02 -080076
77 // Never want the contents of a tile to include stuff the parent
78 // canvas clips out
79 SkRect clip = SkRect::Make(bounds);
80 clip.offset(-SkIntToScalar(tileRect.fLeft), -SkIntToScalar(tileRect.fTop));
81 fSurfaces.top()->getCanvas()->clipRect(clip);
82
robertphillips5b693772014-11-21 06:19:36 -080083 fSurfaces.top()->getCanvas()->setMatrix(canvas->getTotalMatrix());
84 fSurfaces.top()->getCanvas()->scale(fScale, fScale);
85 }
86 }
87}
88
89void SKPBench::onPerCanvasPostDraw(SkCanvas* canvas) {
robertphillips5b693772014-11-21 06:19:36 -080090 // Draw the last set of tiles into the master canvas in case we're
91 // saving the images
92 for (int i = 0; i < fTileRects.count(); ++i) {
robertphillips186a08e2014-11-21 06:53:00 -080093 SkAutoTUnref<SkImage> image(fSurfaces[i]->newImageSnapshot());
94 canvas->drawImage(image,
robertphillips5b693772014-11-21 06:19:36 -080095 SkIntToScalar(fTileRects[i].fLeft), SkIntToScalar(fTileRects[i].fTop));
96 SkSafeSetNull(fSurfaces[i]);
97 }
98
99 fSurfaces.rewind();
100 fTileRects.rewind();
101}
102
mtklein92007582014-08-01 07:46:52 -0700103bool SKPBench::isSuitableFor(Backend backend) {
104 return backend != kNonRendering_Backend;
105}
106
107SkIPoint SKPBench::onGetSize() {
108 return SkIPoint::Make(fClip.width(), fClip.height());
109}
110
mtkleina1ebeb22015-10-01 09:43:39 -0700111void SKPBench::onDraw(int loops, SkCanvas* canvas) {
cdaltonb4022962015-06-25 10:51:56 -0700112 SkASSERT(fDoLooping || 1 == loops);
cdalton31c45bb2016-02-22 08:08:25 -0800113 while (1) {
114 if (fUseMultiPictureDraw) {
joshualitt261c3ad2015-04-27 09:16:57 -0700115 this->drawMPDPicture();
cdalton31c45bb2016-02-22 08:08:25 -0800116 } else {
joshualitt261c3ad2015-04-27 09:16:57 -0700117 this->drawPicture();
robertphillips5b693772014-11-21 06:19:36 -0800118 }
cdalton31c45bb2016-02-22 08:08:25 -0800119 if (0 == --loops) {
120 break;
121 }
cdaltone6d20242015-10-26 13:45:29 -0700122#if SK_SUPPORT_GPU
cdalton31c45bb2016-02-22 08:08:25 -0800123 // Ensure the GrContext doesn't batch across draw loops.
124 if (GrContext* context = canvas->getGrContext()) {
125 context->flush();
126 }
cdaltone6d20242015-10-26 13:45:29 -0700127#endif
cdalton31c45bb2016-02-22 08:08:25 -0800128 }
mtklein92007582014-08-01 07:46:52 -0700129}
joshualitt261c3ad2015-04-27 09:16:57 -0700130
131void SKPBench::drawMPDPicture() {
132 SkMultiPictureDraw mpd;
133
134 for (int j = 0; j < fTileRects.count(); ++j) {
135 SkMatrix trans;
136 trans.setTranslate(-fTileRects[j].fLeft/fScale,
137 -fTileRects[j].fTop/fScale);
138 mpd.add(fSurfaces[j]->getCanvas(), fPic, &trans);
139 }
140
141 mpd.draw();
142
143 for (int j = 0; j < fTileRects.count(); ++j) {
144 fSurfaces[j]->getCanvas()->flush();
145 }
146}
147
148void SKPBench::drawPicture() {
149 for (int j = 0; j < fTileRects.count(); ++j) {
150 const SkMatrix trans = SkMatrix::MakeTrans(-fTileRects[j].fLeft / fScale,
151 -fTileRects[j].fTop / fScale);
halcanary96fcdcc2015-08-27 07:41:13 -0700152 fSurfaces[j]->getCanvas()->drawPicture(fPic, &trans, nullptr);
joshualitt261c3ad2015-04-27 09:16:57 -0700153 }
154
155 for (int j = 0; j < fTileRects.count(); ++j) {
156 fSurfaces[j]->getCanvas()->flush();
157 }
158}
joshualitte45c81c2015-12-02 09:05:37 -0800159
160#if SK_SUPPORT_GPU
joshualitt8fd844f2015-12-02 13:36:47 -0800161#include "GrGpu.h"
joshualitte45c81c2015-12-02 09:05:37 -0800162static void draw_pic_for_stats(SkCanvas* canvas, GrContext* context, const SkPicture* picture,
163 SkTArray<SkString>* keys, SkTArray<double>* values,
164 const char* tag) {
165 context->resetGpuStats();
166 canvas->drawPicture(picture);
167 canvas->flush();
168
169 int offset = keys->count();
170 context->dumpGpuStatsKeyValuePairs(keys, values);
joshualittdc5685a2015-12-02 14:08:25 -0800171 context->dumpCacheStatsKeyValuePairs(keys, values);
joshualitte45c81c2015-12-02 09:05:37 -0800172
173 // append tag, but only to new tags
174 for (int i = offset; i < keys->count(); i++, offset++) {
175 (*keys)[i].appendf("_%s", tag);
176 }
177}
178#endif
179
180void SKPBench::getGpuStats(SkCanvas* canvas, SkTArray<SkString>* keys, SkTArray<double>* values) {
181#if SK_SUPPORT_GPU
182 // we do a special single draw and then dump the key / value pairs
183 GrContext* context = canvas->getGrContext();
184 if (!context) {
185 return;
186 }
187
188 // TODO refactor this out if we want to test other subclasses of skpbench
189 context->flush();
190 context->freeGpuResources();
191 context->resetContext();
joshualitt8fd844f2015-12-02 13:36:47 -0800192 context->getGpu()->resetShaderCacheForTesting();
joshualitte45c81c2015-12-02 09:05:37 -0800193 draw_pic_for_stats(canvas, context, fPic, keys, values, "first_frame");
194
195 // draw second frame
196 draw_pic_for_stats(canvas, context, fPic, keys, values, "second_frame");
197
198#endif
199}