blob: 44261335daa3bb750b7891b8acd224b3426caff9 [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
mtkleinbf9e6002015-06-16 10:41:27 -070013// These CPU tile sizes are not good per se, but they are similar to what Chrome uses.
14DEFINE_int32(CPUbenchTileW, 256, "Tile width used for CPU SKP playback.");
15DEFINE_int32(CPUbenchTileH, 256, "Tile height used for CPU SKP playback.");
16
17DEFINE_int32(GPUbenchTileW, 1600, "Tile width used for GPU SKP playback.");
18DEFINE_int32(GPUbenchTileH, 512, "Tile height used for GPU SKP playback.");
mtklein92007582014-08-01 07:46:52 -070019
robertphillips5b693772014-11-21 06:19:36 -080020SKPBench::SKPBench(const char* name, const SkPicture* pic, const SkIRect& clip, SkScalar scale,
cdaltonb4022962015-06-25 10:51:56 -070021 bool useMultiPictureDraw, bool doLooping)
mtklein92007582014-08-01 07:46:52 -070022 : fPic(SkRef(pic))
23 , fClip(clip)
mtklein96289052014-09-10 12:05:59 -070024 , fScale(scale)
robertphillips5b693772014-11-21 06:19:36 -080025 , fName(name)
cdaltonb4022962015-06-25 10:51:56 -070026 , fUseMultiPictureDraw(useMultiPictureDraw)
27 , fDoLooping(doLooping) {
tfarina0004e7d2015-01-26 06:47:55 -080028 fUniqueName.printf("%s_%.2g", name, scale); // Scale makes this unqiue for perf.skia.org traces.
robertphillips5b693772014-11-21 06:19:36 -080029 if (useMultiPictureDraw) {
30 fUniqueName.append("_mpd");
31 }
32}
33
34SKPBench::~SKPBench() {
35 for (int i = 0; i < fSurfaces.count(); ++i) {
36 fSurfaces[i]->unref();
37 }
mtklein92007582014-08-01 07:46:52 -070038}
39
40const char* SKPBench::onGetName() {
41 return fName.c_str();
42}
43
mtklein96289052014-09-10 12:05:59 -070044const char* SKPBench::onGetUniqueName() {
45 return fUniqueName.c_str();
46}
47
robertphillips5b693772014-11-21 06:19:36 -080048void SKPBench::onPerCanvasPreDraw(SkCanvas* canvas) {
robertphillips5b693772014-11-21 06:19:36 -080049 SkIRect bounds;
50 SkAssertResult(canvas->getClipDeviceBounds(&bounds));
51
mtkleinbf9e6002015-06-16 10:41:27 -070052 const bool gpu = canvas->getGrContext() != nullptr;
53 int tileW = gpu ? FLAGS_GPUbenchTileW : FLAGS_CPUbenchTileW,
54 tileH = gpu ? FLAGS_GPUbenchTileH : FLAGS_CPUbenchTileH;
55
56 tileW = SkTMin(tileW, bounds.width());
57 tileH = SkTMin(tileH, bounds.height());
bsalomoncc4d6672015-03-05 13:42:27 -080058
59 int xTiles = SkScalarCeilToInt(bounds.width() / SkIntToScalar(tileW));
60 int yTiles = SkScalarCeilToInt(bounds.height() / SkIntToScalar(tileH));
robertphillips5b693772014-11-21 06:19:36 -080061
62 fSurfaces.setReserve(xTiles * yTiles);
63 fTileRects.setReserve(xTiles * yTiles);
64
bsalomoncc4d6672015-03-05 13:42:27 -080065 SkImageInfo ii = canvas->imageInfo().makeWH(tileW, tileH);
robertphillips5b693772014-11-21 06:19:36 -080066
bsalomoncc4d6672015-03-05 13:42:27 -080067 for (int y = bounds.fTop; y < bounds.fBottom; y += tileH) {
68 for (int x = bounds.fLeft; x < bounds.fRight; x += tileW) {
69 const SkIRect tileRect = SkIRect::MakeXYWH(x, y, tileW, tileH);
robertphillips63242d72014-12-04 08:31:02 -080070 *fTileRects.append() = tileRect;
robertphillips5b693772014-11-21 06:19:36 -080071 *fSurfaces.push() = canvas->newSurface(ii);
robertphillips63242d72014-12-04 08:31:02 -080072
73 // Never want the contents of a tile to include stuff the parent
74 // canvas clips out
75 SkRect clip = SkRect::Make(bounds);
76 clip.offset(-SkIntToScalar(tileRect.fLeft), -SkIntToScalar(tileRect.fTop));
77 fSurfaces.top()->getCanvas()->clipRect(clip);
78
robertphillips5b693772014-11-21 06:19:36 -080079 fSurfaces.top()->getCanvas()->setMatrix(canvas->getTotalMatrix());
80 fSurfaces.top()->getCanvas()->scale(fScale, fScale);
81 }
82 }
83}
84
85void SKPBench::onPerCanvasPostDraw(SkCanvas* canvas) {
robertphillips5b693772014-11-21 06:19:36 -080086 // Draw the last set of tiles into the master canvas in case we're
87 // saving the images
88 for (int i = 0; i < fTileRects.count(); ++i) {
robertphillips186a08e2014-11-21 06:53:00 -080089 SkAutoTUnref<SkImage> image(fSurfaces[i]->newImageSnapshot());
90 canvas->drawImage(image,
robertphillips5b693772014-11-21 06:19:36 -080091 SkIntToScalar(fTileRects[i].fLeft), SkIntToScalar(fTileRects[i].fTop));
92 SkSafeSetNull(fSurfaces[i]);
93 }
94
95 fSurfaces.rewind();
96 fTileRects.rewind();
97}
98
mtklein92007582014-08-01 07:46:52 -070099bool SKPBench::isSuitableFor(Backend backend) {
100 return backend != kNonRendering_Backend;
101}
102
103SkIPoint SKPBench::onGetSize() {
104 return SkIPoint::Make(fClip.width(), fClip.height());
105}
106
mtkleina1ebeb22015-10-01 09:43:39 -0700107void SKPBench::onDraw(int loops, SkCanvas* canvas) {
cdaltonb4022962015-06-25 10:51:56 -0700108 SkASSERT(fDoLooping || 1 == loops);
robertphillips5b693772014-11-21 06:19:36 -0800109 if (fUseMultiPictureDraw) {
110 for (int i = 0; i < loops; i++) {
joshualitt261c3ad2015-04-27 09:16:57 -0700111 this->drawMPDPicture();
mtklein92007582014-08-01 07:46:52 -0700112 }
robertphillips5b693772014-11-21 06:19:36 -0800113 } else {
robertphillips5b693772014-11-21 06:19:36 -0800114 for (int i = 0; i < loops; i++) {
joshualitt261c3ad2015-04-27 09:16:57 -0700115 this->drawPicture();
robertphillips5b693772014-11-21 06:19:36 -0800116 }
mtkleinc7f7f462014-10-21 12:29:25 -0700117 }
mtklein92007582014-08-01 07:46:52 -0700118}
joshualitt261c3ad2015-04-27 09:16:57 -0700119
120void SKPBench::drawMPDPicture() {
121 SkMultiPictureDraw mpd;
122
123 for (int j = 0; j < fTileRects.count(); ++j) {
124 SkMatrix trans;
125 trans.setTranslate(-fTileRects[j].fLeft/fScale,
126 -fTileRects[j].fTop/fScale);
127 mpd.add(fSurfaces[j]->getCanvas(), fPic, &trans);
128 }
129
130 mpd.draw();
131
132 for (int j = 0; j < fTileRects.count(); ++j) {
133 fSurfaces[j]->getCanvas()->flush();
134 }
135}
136
137void SKPBench::drawPicture() {
138 for (int j = 0; j < fTileRects.count(); ++j) {
139 const SkMatrix trans = SkMatrix::MakeTrans(-fTileRects[j].fLeft / fScale,
140 -fTileRects[j].fTop / fScale);
halcanary96fcdcc2015-08-27 07:41:13 -0700141 fSurfaces[j]->getCanvas()->drawPicture(fPic, &trans, nullptr);
joshualitt261c3ad2015-04-27 09:16:57 -0700142 }
143
144 for (int j = 0; j < fTileRects.count(); ++j) {
145 fSurfaces[j]->getCanvas()->flush();
146 }
147}