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 #if SK_SUPPORT_GPU
14 #include "GrContext.h"
15 #endif
16 
17 // These CPU tile sizes are not good per se, but they are similar to what Chrome uses.
18 DEFINE_int32(CPUbenchTileW, 256, "Tile width  used for CPU SKP playback.");
19 DEFINE_int32(CPUbenchTileH, 256, "Tile height used for CPU SKP playback.");
20 
21 DEFINE_int32(GPUbenchTileW, 1600, "Tile width  used for GPU SKP playback.");
22 DEFINE_int32(GPUbenchTileH, 512, "Tile height used for GPU SKP playback.");
23 
SKPBench(const char * name,const SkPicture * pic,const SkIRect & clip,SkScalar scale,bool useMultiPictureDraw,bool doLooping)24 SKPBench::SKPBench(const char* name, const SkPicture* pic, const SkIRect& clip, SkScalar scale,
25                    bool useMultiPictureDraw, bool doLooping)
26     : fPic(SkRef(pic))
27     , fClip(clip)
28     , fScale(scale)
29     , fName(name)
30     , fUseMultiPictureDraw(useMultiPictureDraw)
31     , fDoLooping(doLooping) {
32     fUniqueName.printf("%s_%.2g", name, scale);  // Scale makes this unqiue for perf.skia.org traces.
33     if (useMultiPictureDraw) {
34         fUniqueName.append("_mpd");
35     }
36 }
37 
~SKPBench()38 SKPBench::~SKPBench() {
39     for (int i = 0; i < fSurfaces.count(); ++i) {
40         fSurfaces[i]->unref();
41     }
42 }
43 
onGetName()44 const char* SKPBench::onGetName() {
45     return fName.c_str();
46 }
47 
onGetUniqueName()48 const char* SKPBench::onGetUniqueName() {
49     return fUniqueName.c_str();
50 }
51 
onPerCanvasPreDraw(SkCanvas * canvas)52 void SKPBench::onPerCanvasPreDraw(SkCanvas* canvas) {
53     SkIRect bounds;
54     SkAssertResult(canvas->getClipDeviceBounds(&bounds));
55 
56     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());
62 
63     int xTiles = SkScalarCeilToInt(bounds.width()  / SkIntToScalar(tileW));
64     int yTiles = SkScalarCeilToInt(bounds.height() / SkIntToScalar(tileH));
65 
66     fSurfaces.setReserve(xTiles * yTiles);
67     fTileRects.setReserve(xTiles * yTiles);
68 
69     SkImageInfo ii = canvas->imageInfo().makeWH(tileW, tileH);
70 
71     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);
74             *fTileRects.append() = tileRect;
75             *fSurfaces.push() = canvas->newSurface(ii);
76 
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 
83             fSurfaces.top()->getCanvas()->setMatrix(canvas->getTotalMatrix());
84             fSurfaces.top()->getCanvas()->scale(fScale, fScale);
85         }
86     }
87 }
88 
onPerCanvasPostDraw(SkCanvas * canvas)89 void SKPBench::onPerCanvasPostDraw(SkCanvas* canvas) {
90     // 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) {
93         SkAutoTUnref<SkImage> image(fSurfaces[i]->newImageSnapshot());
94         canvas->drawImage(image,
95                           SkIntToScalar(fTileRects[i].fLeft), SkIntToScalar(fTileRects[i].fTop));
96         SkSafeSetNull(fSurfaces[i]);
97     }
98 
99     fSurfaces.rewind();
100     fTileRects.rewind();
101 }
102 
isSuitableFor(Backend backend)103 bool SKPBench::isSuitableFor(Backend backend) {
104     return backend != kNonRendering_Backend;
105 }
106 
onGetSize()107 SkIPoint SKPBench::onGetSize() {
108     return SkIPoint::Make(fClip.width(), fClip.height());
109 }
110 
onDraw(int loops,SkCanvas * canvas)111 void SKPBench::onDraw(int loops, SkCanvas* canvas) {
112     SkASSERT(fDoLooping || 1 == loops);
113     while (1) {
114         if (fUseMultiPictureDraw) {
115             this->drawMPDPicture();
116         } else {
117             this->drawPicture();
118         }
119         if (0 == --loops) {
120             break;
121         }
122 #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
128     }
129 }
130 
drawMPDPicture()131 void SKPBench::drawMPDPicture() {
132     SkMultiPictureDraw mpd;
133 
134     for (int j = 0; j < fTileRects.count(); ++j) {
135         SkMatrix trans;
136         trans.setTranslate(-fTileRects[j].fLeft/fScale,
137                            -fTileRects[j].fTop/fScale);
138         mpd.add(fSurfaces[j]->getCanvas(), fPic, &trans);
139     }
140 
141     mpd.draw();
142 
143     for (int j = 0; j < fTileRects.count(); ++j) {
144         fSurfaces[j]->getCanvas()->flush();
145     }
146 }
147 
drawPicture()148 void SKPBench::drawPicture() {
149     for (int j = 0; j < fTileRects.count(); ++j) {
150         const SkMatrix trans = SkMatrix::MakeTrans(-fTileRects[j].fLeft / fScale,
151                                                    -fTileRects[j].fTop / fScale);
152         fSurfaces[j]->getCanvas()->drawPicture(fPic, &trans, nullptr);
153     }
154 
155     for (int j = 0; j < fTileRects.count(); ++j) {
156         fSurfaces[j]->getCanvas()->flush();
157     }
158 }
159 
160 #if SK_SUPPORT_GPU
161 #include "GrGpu.h"
draw_pic_for_stats(SkCanvas * canvas,GrContext * context,const SkPicture * picture,SkTArray<SkString> * keys,SkTArray<double> * values,const char * tag)162 static void draw_pic_for_stats(SkCanvas* canvas, GrContext* context, const SkPicture* picture,
163                                SkTArray<SkString>* keys, SkTArray<double>* values,
164                                const char* tag) {
165     context->resetGpuStats();
166     canvas->drawPicture(picture);
167     canvas->flush();
168 
169     int offset = keys->count();
170     context->dumpGpuStatsKeyValuePairs(keys, values);
171     context->dumpCacheStatsKeyValuePairs(keys, values);
172 
173     // append tag, but only to new tags
174     for (int i = offset; i < keys->count(); i++, offset++) {
175         (*keys)[i].appendf("_%s", tag);
176     }
177 }
178 #endif
179 
getGpuStats(SkCanvas * canvas,SkTArray<SkString> * keys,SkTArray<double> * values)180 void SKPBench::getGpuStats(SkCanvas* canvas, SkTArray<SkString>* keys, SkTArray<double>* values) {
181 #if SK_SUPPORT_GPU
182     // we do a special single draw and then dump the key / value pairs
183     GrContext* context = canvas->getGrContext();
184     if (!context) {
185         return;
186     }
187 
188     // TODO refactor this out if we want to test other subclasses of skpbench
189     context->flush();
190     context->freeGpuResources();
191     context->resetContext();
192     context->getGpu()->resetShaderCacheForTesting();
193     draw_pic_for_stats(canvas, context, fPic, keys, values, "first_frame");
194 
195     // draw second frame
196     draw_pic_for_stats(canvas, context, fPic, keys, values, "second_frame");
197 
198 #endif
199 }
200