SKPBench.cpp revision 9a0f629973ee97ea10f5e3c36a802eca4270d7e0
1/*
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"
9#include "SkCommandLineFlags.h"
10#include "SkMultiPictureDraw.h"
11#include "SkSurface.h"
12
13DEFINE_int32(benchTileW, 1600, "Tile width  used for SKP playback.");
14DEFINE_int32(benchTileH, 512, "Tile height used for SKP playback.");
15
16SKPBench::SKPBench(const char* name, const SkPicture* pic, const SkIRect& clip, SkScalar scale,
17                   bool useMultiPictureDraw)
18    : fPic(SkRef(pic))
19    , fClip(clip)
20    , fScale(scale)
21    , fName(name)
22    , fUseMultiPictureDraw(useMultiPictureDraw) {
23    fUniqueName.printf("%s_%.2g", name, scale);  // Scale makes this unqiue for perf.skia.org traces.
24    if (useMultiPictureDraw) {
25        fUniqueName.append("_mpd");
26    }
27}
28
29SKPBench::~SKPBench() {
30    for (int i = 0; i < fSurfaces.count(); ++i) {
31        fSurfaces[i]->unref();
32    }
33}
34
35const char* SKPBench::onGetName() {
36    return fName.c_str();
37}
38
39const char* SKPBench::onGetUniqueName() {
40    return fUniqueName.c_str();
41}
42
43void SKPBench::onPerCanvasPreDraw(SkCanvas* canvas) {
44    SkIRect bounds;
45    SkAssertResult(canvas->getClipDeviceBounds(&bounds));
46
47    int tileW = SkTMin(FLAGS_benchTileW, bounds.width());
48    int tileH = SkTMin(FLAGS_benchTileH, bounds.height());
49
50    int xTiles = SkScalarCeilToInt(bounds.width()  / SkIntToScalar(tileW));
51    int yTiles = SkScalarCeilToInt(bounds.height() / SkIntToScalar(tileH));
52
53    fSurfaces.setReserve(xTiles * yTiles);
54    fTileRects.setReserve(xTiles * yTiles);
55
56    SkImageInfo ii = canvas->imageInfo().makeWH(tileW, tileH);
57
58    for (int y = bounds.fTop; y < bounds.fBottom; y += tileH) {
59        for (int x = bounds.fLeft; x < bounds.fRight; x += tileW) {
60            const SkIRect tileRect = SkIRect::MakeXYWH(x, y, tileW, tileH);
61            *fTileRects.append() = tileRect;
62            *fSurfaces.push() = canvas->newSurface(ii);
63
64            // Never want the contents of a tile to include stuff the parent
65            // canvas clips out
66            SkRect clip = SkRect::Make(bounds);
67            clip.offset(-SkIntToScalar(tileRect.fLeft), -SkIntToScalar(tileRect.fTop));
68            fSurfaces.top()->getCanvas()->clipRect(clip);
69
70            fSurfaces.top()->getCanvas()->setMatrix(canvas->getTotalMatrix());
71            fSurfaces.top()->getCanvas()->scale(fScale, fScale);
72        }
73    }
74}
75
76void SKPBench::onPerCanvasPostDraw(SkCanvas* canvas) {
77    // Draw the last set of tiles into the master canvas in case we're
78    // saving the images
79    for (int i = 0; i < fTileRects.count(); ++i) {
80        SkAutoTUnref<SkImage> image(fSurfaces[i]->newImageSnapshot());
81        canvas->drawImage(image,
82                          SkIntToScalar(fTileRects[i].fLeft), SkIntToScalar(fTileRects[i].fTop));
83        SkSafeSetNull(fSurfaces[i]);
84    }
85
86    fSurfaces.rewind();
87    fTileRects.rewind();
88}
89
90bool SKPBench::isSuitableFor(Backend backend) {
91    return backend != kNonRendering_Backend;
92}
93
94SkIPoint SKPBench::onGetSize() {
95    return SkIPoint::Make(fClip.width(), fClip.height());
96}
97
98void SKPBench::onDraw(const int loops, SkCanvas* canvas) {
99    if (fUseMultiPictureDraw) {
100        for (int i = 0; i < loops; i++) {
101            SkMultiPictureDraw mpd;
102
103            for (int j = 0; j < fTileRects.count(); ++j) {
104                SkMatrix trans;
105                trans.setTranslate(-fTileRects[j].fLeft/fScale,
106                                   -fTileRects[j].fTop/fScale);
107                mpd.add(fSurfaces[j]->getCanvas(), fPic, &trans);
108            }
109
110            mpd.draw();
111
112            for (int j = 0; j < fTileRects.count(); ++j) {
113                fSurfaces[j]->getCanvas()->flush();
114            }
115        }
116    } else {
117        for (int i = 0; i < loops; i++) {
118            for (int j = 0; j < fTileRects.count(); ++j) {
119                SkMatrix trans;
120                trans.setTranslate(-fTileRects[j].fLeft / fScale,
121                                   -fTileRects[j].fTop / fScale);
122                fSurfaces[j]->getCanvas()->drawPicture(fPic, &trans, NULL);
123            }
124
125            for (int j = 0; j < fTileRects.count(); ++j) {
126                fSurfaces[j]->getCanvas()->flush();
127            }
128        }
129    }
130}
131