blob: db55b45f435601bad64d2c371273549a4c6e7f9a [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);
robertphillips5b693772014-11-21 06:19:36 -0800113 if (fUseMultiPictureDraw) {
114 for (int i = 0; i < loops; i++) {
joshualitt261c3ad2015-04-27 09:16:57 -0700115 this->drawMPDPicture();
mtklein92007582014-08-01 07:46:52 -0700116 }
robertphillips5b693772014-11-21 06:19:36 -0800117 } else {
robertphillips5b693772014-11-21 06:19:36 -0800118 for (int i = 0; i < loops; i++) {
joshualitt261c3ad2015-04-27 09:16:57 -0700119 this->drawPicture();
robertphillips5b693772014-11-21 06:19:36 -0800120 }
mtkleinc7f7f462014-10-21 12:29:25 -0700121 }
cdaltone6d20242015-10-26 13:45:29 -0700122#if SK_SUPPORT_GPU
123 // Ensure the GrContext doesn't batch across draw loops.
124 if (GrContext* context = canvas->getGrContext()) {
125 context->flush();
126 }
127#endif
mtklein92007582014-08-01 07:46:52 -0700128}
joshualitt261c3ad2015-04-27 09:16:57 -0700129
130void SKPBench::drawMPDPicture() {
131 SkMultiPictureDraw mpd;
132
133 for (int j = 0; j < fTileRects.count(); ++j) {
134 SkMatrix trans;
135 trans.setTranslate(-fTileRects[j].fLeft/fScale,
136 -fTileRects[j].fTop/fScale);
137 mpd.add(fSurfaces[j]->getCanvas(), fPic, &trans);
138 }
139
140 mpd.draw();
141
142 for (int j = 0; j < fTileRects.count(); ++j) {
143 fSurfaces[j]->getCanvas()->flush();
144 }
145}
146
147void SKPBench::drawPicture() {
148 for (int j = 0; j < fTileRects.count(); ++j) {
149 const SkMatrix trans = SkMatrix::MakeTrans(-fTileRects[j].fLeft / fScale,
150 -fTileRects[j].fTop / fScale);
halcanary96fcdcc2015-08-27 07:41:13 -0700151 fSurfaces[j]->getCanvas()->drawPicture(fPic, &trans, nullptr);
joshualitt261c3ad2015-04-27 09:16:57 -0700152 }
153
154 for (int j = 0; j < fTileRects.count(); ++j) {
155 fSurfaces[j]->getCanvas()->flush();
156 }
157}
joshualitte45c81c2015-12-02 09:05:37 -0800158
159#if SK_SUPPORT_GPU
joshualitt8fd844f2015-12-02 13:36:47 -0800160#include "GrGpu.h"
joshualitte45c81c2015-12-02 09:05:37 -0800161static void draw_pic_for_stats(SkCanvas* canvas, GrContext* context, const SkPicture* picture,
162 SkTArray<SkString>* keys, SkTArray<double>* values,
163 const char* tag) {
164 context->resetGpuStats();
165 canvas->drawPicture(picture);
166 canvas->flush();
167
168 int offset = keys->count();
169 context->dumpGpuStatsKeyValuePairs(keys, values);
joshualittdc5685a2015-12-02 14:08:25 -0800170 context->dumpCacheStatsKeyValuePairs(keys, values);
joshualitte45c81c2015-12-02 09:05:37 -0800171
172 // append tag, but only to new tags
173 for (int i = offset; i < keys->count(); i++, offset++) {
174 (*keys)[i].appendf("_%s", tag);
175 }
176}
177#endif
178
179void SKPBench::getGpuStats(SkCanvas* canvas, SkTArray<SkString>* keys, SkTArray<double>* values) {
180#if SK_SUPPORT_GPU
181 // we do a special single draw and then dump the key / value pairs
182 GrContext* context = canvas->getGrContext();
183 if (!context) {
184 return;
185 }
186
187 // TODO refactor this out if we want to test other subclasses of skpbench
188 context->flush();
189 context->freeGpuResources();
190 context->resetContext();
joshualitt8fd844f2015-12-02 13:36:47 -0800191 context->getGpu()->resetShaderCacheForTesting();
joshualitte45c81c2015-12-02 09:05:37 -0800192 draw_pic_for_stats(canvas, context, fPic, keys, values, "first_frame");
193
194 // draw second frame
195 draw_pic_for_stats(canvas, context, fPic, keys, values, "second_frame");
196
197#endif
198}