• Home
  • History
  • Annotate
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
13 DEFINE_int32(benchTileW, 1600, "Tile width  used for SKP playback.");
14 DEFINE_int32(benchTileH, 512, "Tile height used for SKP playback.");
15 
SKPBench(const char * name,const SkPicture * pic,const SkIRect & clip,SkScalar scale,bool useMultiPictureDraw)16 SKPBench::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 
~SKPBench()29 SKPBench::~SKPBench() {
30     for (int i = 0; i < fSurfaces.count(); ++i) {
31         fSurfaces[i]->unref();
32     }
33 }
34 
onGetName()35 const char* SKPBench::onGetName() {
36     return fName.c_str();
37 }
38 
onGetUniqueName()39 const char* SKPBench::onGetUniqueName() {
40     return fUniqueName.c_str();
41 }
42 
onPerCanvasPreDraw(SkCanvas * canvas)43 void 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 
onPerCanvasPostDraw(SkCanvas * canvas)76 void 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 
isSuitableFor(Backend backend)90 bool SKPBench::isSuitableFor(Backend backend) {
91     return backend != kNonRendering_Backend;
92 }
93 
onGetSize()94 SkIPoint SKPBench::onGetSize() {
95     return SkIPoint::Make(fClip.width(), fClip.height());
96 }
97 
onDraw(const int loops,SkCanvas * canvas)98 void SKPBench::onDraw(const int loops, SkCanvas* canvas) {
99     if (fUseMultiPictureDraw) {
100         for (int i = 0; i < loops; i++) {
101             this->drawMPDPicture();
102         }
103     } else {
104         for (int i = 0; i < loops; i++) {
105             this->drawPicture();
106         }
107     }
108 }
109 
drawMPDPicture()110 void SKPBench::drawMPDPicture() {
111     SkMultiPictureDraw mpd;
112 
113     for (int j = 0; j < fTileRects.count(); ++j) {
114         SkMatrix trans;
115         trans.setTranslate(-fTileRects[j].fLeft/fScale,
116                            -fTileRects[j].fTop/fScale);
117         mpd.add(fSurfaces[j]->getCanvas(), fPic, &trans);
118     }
119 
120     mpd.draw();
121 
122     for (int j = 0; j < fTileRects.count(); ++j) {
123         fSurfaces[j]->getCanvas()->flush();
124     }
125 }
126 
drawPicture()127 void SKPBench::drawPicture() {
128     for (int j = 0; j < fTileRects.count(); ++j) {
129         const SkMatrix trans = SkMatrix::MakeTrans(-fTileRects[j].fLeft / fScale,
130                                                    -fTileRects[j].fTop / fScale);
131         fSurfaces[j]->getCanvas()->drawPicture(fPic, &trans, NULL);
132     }
133 
134     for (int j = 0; j < fTileRects.count(); ++j) {
135         fSurfaces[j]->getCanvas()->flush();
136     }
137 }
138