blob: 757ee4655ed3d3bad777e7625941d04b96ab23b5 [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,
21 bool useMultiPictureDraw)
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)
26 , fUseMultiPictureDraw(useMultiPictureDraw) {
tfarina0004e7d2015-01-26 06:47:55 -080027 fUniqueName.printf("%s_%.2g", name, scale); // Scale makes this unqiue for perf.skia.org traces.
robertphillips5b693772014-11-21 06:19:36 -080028 if (useMultiPictureDraw) {
29 fUniqueName.append("_mpd");
30 }
31}
32
33SKPBench::~SKPBench() {
34 for (int i = 0; i < fSurfaces.count(); ++i) {
35 fSurfaces[i]->unref();
36 }
mtklein92007582014-08-01 07:46:52 -070037}
38
39const char* SKPBench::onGetName() {
40 return fName.c_str();
41}
42
mtklein96289052014-09-10 12:05:59 -070043const char* SKPBench::onGetUniqueName() {
44 return fUniqueName.c_str();
45}
46
robertphillips5b693772014-11-21 06:19:36 -080047void SKPBench::onPerCanvasPreDraw(SkCanvas* canvas) {
robertphillips5b693772014-11-21 06:19:36 -080048 SkIRect bounds;
49 SkAssertResult(canvas->getClipDeviceBounds(&bounds));
50
mtkleinbf9e6002015-06-16 10:41:27 -070051 const bool gpu = canvas->getGrContext() != nullptr;
52 int tileW = gpu ? FLAGS_GPUbenchTileW : FLAGS_CPUbenchTileW,
53 tileH = gpu ? FLAGS_GPUbenchTileH : FLAGS_CPUbenchTileH;
54
55 tileW = SkTMin(tileW, bounds.width());
56 tileH = SkTMin(tileH, bounds.height());
bsalomoncc4d6672015-03-05 13:42:27 -080057
58 int xTiles = SkScalarCeilToInt(bounds.width() / SkIntToScalar(tileW));
59 int yTiles = SkScalarCeilToInt(bounds.height() / SkIntToScalar(tileH));
robertphillips5b693772014-11-21 06:19:36 -080060
61 fSurfaces.setReserve(xTiles * yTiles);
62 fTileRects.setReserve(xTiles * yTiles);
63
bsalomoncc4d6672015-03-05 13:42:27 -080064 SkImageInfo ii = canvas->imageInfo().makeWH(tileW, tileH);
robertphillips5b693772014-11-21 06:19:36 -080065
bsalomoncc4d6672015-03-05 13:42:27 -080066 for (int y = bounds.fTop; y < bounds.fBottom; y += tileH) {
67 for (int x = bounds.fLeft; x < bounds.fRight; x += tileW) {
68 const SkIRect tileRect = SkIRect::MakeXYWH(x, y, tileW, tileH);
robertphillips63242d72014-12-04 08:31:02 -080069 *fTileRects.append() = tileRect;
robertphillips5b693772014-11-21 06:19:36 -080070 *fSurfaces.push() = canvas->newSurface(ii);
robertphillips63242d72014-12-04 08:31:02 -080071
72 // Never want the contents of a tile to include stuff the parent
73 // canvas clips out
74 SkRect clip = SkRect::Make(bounds);
75 clip.offset(-SkIntToScalar(tileRect.fLeft), -SkIntToScalar(tileRect.fTop));
76 fSurfaces.top()->getCanvas()->clipRect(clip);
77
robertphillips5b693772014-11-21 06:19:36 -080078 fSurfaces.top()->getCanvas()->setMatrix(canvas->getTotalMatrix());
79 fSurfaces.top()->getCanvas()->scale(fScale, fScale);
80 }
81 }
82}
83
84void SKPBench::onPerCanvasPostDraw(SkCanvas* canvas) {
robertphillips5b693772014-11-21 06:19:36 -080085 // Draw the last set of tiles into the master canvas in case we're
86 // saving the images
87 for (int i = 0; i < fTileRects.count(); ++i) {
robertphillips186a08e2014-11-21 06:53:00 -080088 SkAutoTUnref<SkImage> image(fSurfaces[i]->newImageSnapshot());
89 canvas->drawImage(image,
robertphillips5b693772014-11-21 06:19:36 -080090 SkIntToScalar(fTileRects[i].fLeft), SkIntToScalar(fTileRects[i].fTop));
91 SkSafeSetNull(fSurfaces[i]);
92 }
93
94 fSurfaces.rewind();
95 fTileRects.rewind();
96}
97
mtklein92007582014-08-01 07:46:52 -070098bool SKPBench::isSuitableFor(Backend backend) {
99 return backend != kNonRendering_Backend;
100}
101
102SkIPoint SKPBench::onGetSize() {
103 return SkIPoint::Make(fClip.width(), fClip.height());
104}
105
106void SKPBench::onDraw(const int loops, SkCanvas* canvas) {
robertphillips5b693772014-11-21 06:19:36 -0800107 if (fUseMultiPictureDraw) {
108 for (int i = 0; i < loops; i++) {
joshualitt261c3ad2015-04-27 09:16:57 -0700109 this->drawMPDPicture();
mtklein92007582014-08-01 07:46:52 -0700110 }
robertphillips5b693772014-11-21 06:19:36 -0800111 } else {
robertphillips5b693772014-11-21 06:19:36 -0800112 for (int i = 0; i < loops; i++) {
joshualitt261c3ad2015-04-27 09:16:57 -0700113 this->drawPicture();
robertphillips5b693772014-11-21 06:19:36 -0800114 }
mtkleinc7f7f462014-10-21 12:29:25 -0700115 }
mtklein92007582014-08-01 07:46:52 -0700116}
joshualitt261c3ad2015-04-27 09:16:57 -0700117
118void SKPBench::drawMPDPicture() {
119 SkMultiPictureDraw mpd;
120
121 for (int j = 0; j < fTileRects.count(); ++j) {
122 SkMatrix trans;
123 trans.setTranslate(-fTileRects[j].fLeft/fScale,
124 -fTileRects[j].fTop/fScale);
125 mpd.add(fSurfaces[j]->getCanvas(), fPic, &trans);
126 }
127
128 mpd.draw();
129
130 for (int j = 0; j < fTileRects.count(); ++j) {
131 fSurfaces[j]->getCanvas()->flush();
132 }
133}
134
135void SKPBench::drawPicture() {
136 for (int j = 0; j < fTileRects.count(); ++j) {
137 const SkMatrix trans = SkMatrix::MakeTrans(-fTileRects[j].fLeft / fScale,
138 -fTileRects[j].fTop / fScale);
139 fSurfaces[j]->getCanvas()->drawPicture(fPic, &trans, NULL);
140 }
141
142 for (int j = 0; j < fTileRects.count(); ++j) {
143 fSurfaces[j]->getCanvas()->flush();
144 }
145}