• Home
  • 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 "bench/SKPBench.h"
9 #include "include/core/SkMultiPictureDraw.h"
10 #include "include/core/SkSurface.h"
11 #include "tools/flags/CommandLineFlags.h"
12 
13 #include "include/gpu/GrContext.h"
14 #include "src/gpu/GrContextPriv.h"
15 
16 // These CPU tile sizes are not good per se, but they are similar to what Chrome uses.
17 static DEFINE_int(CPUbenchTileW, 256, "Tile width  used for CPU SKP playback.");
18 static DEFINE_int(CPUbenchTileH, 256, "Tile height used for CPU SKP playback.");
19 
20 static DEFINE_int(GPUbenchTileW, 1600, "Tile width  used for GPU SKP playback.");
21 static DEFINE_int(GPUbenchTileH, 512, "Tile height used for GPU SKP playback.");
22 
SKPBench(const char * name,const SkPicture * pic,const SkIRect & clip,SkScalar scale,bool useMultiPictureDraw,bool doLooping)23 SKPBench::SKPBench(const char* name, const SkPicture* pic, const SkIRect& clip, SkScalar scale,
24                    bool useMultiPictureDraw, bool doLooping)
25     : fPic(SkRef(pic))
26     , fClip(clip)
27     , fScale(scale)
28     , fName(name)
29     , fUseMultiPictureDraw(useMultiPictureDraw)
30     , fDoLooping(doLooping) {
31     fUniqueName.printf("%s_%.2g", name, scale);  // Scale makes this unqiue for perf.skia.org traces.
32     if (useMultiPictureDraw) {
33         fUniqueName.append("_mpd");
34     }
35 }
36 
~SKPBench()37 SKPBench::~SKPBench() {
38     for (int i = 0; i < fSurfaces.count(); ++i) {
39         fSurfaces[i]->unref();
40     }
41 }
42 
onGetName()43 const char* SKPBench::onGetName() {
44     return fName.c_str();
45 }
46 
onGetUniqueName()47 const char* SKPBench::onGetUniqueName() {
48     return fUniqueName.c_str();
49 }
50 
onPerCanvasPreDraw(SkCanvas * canvas)51 void SKPBench::onPerCanvasPreDraw(SkCanvas* canvas) {
52     SkIRect bounds = canvas->getDeviceClipBounds();
53     SkAssertResult(!bounds.isEmpty());
54 
55     const bool gpu = canvas->getGrContext() != nullptr;
56     int tileW = gpu ? FLAGS_GPUbenchTileW : FLAGS_CPUbenchTileW,
57         tileH = gpu ? FLAGS_GPUbenchTileH : FLAGS_CPUbenchTileH;
58 
59     tileW = SkTMin(tileW, bounds.width());
60     tileH = SkTMin(tileH, bounds.height());
61 
62     int xTiles = SkScalarCeilToInt(bounds.width()  / SkIntToScalar(tileW));
63     int yTiles = SkScalarCeilToInt(bounds.height() / SkIntToScalar(tileH));
64 
65     fSurfaces.reserve(xTiles * yTiles);
66     fTileRects.setReserve(xTiles * yTiles);
67 
68     SkImageInfo ii = canvas->imageInfo().makeWH(tileW, tileH);
69 
70     for (int y = bounds.fTop; y < bounds.fBottom; y += tileH) {
71         for (int x = bounds.fLeft; x < bounds.fRight; x += tileW) {
72             const SkIRect tileRect = SkIRect::MakeXYWH(x, y, tileW, tileH);
73             *fTileRects.append() = tileRect;
74             fSurfaces.emplace_back(canvas->makeSurface(ii));
75 
76             // Never want the contents of a tile to include stuff the parent
77             // canvas clips out
78             SkRect clip = SkRect::Make(bounds);
79             clip.offset(-SkIntToScalar(tileRect.fLeft), -SkIntToScalar(tileRect.fTop));
80             fSurfaces.back()->getCanvas()->clipRect(clip);
81 
82             fSurfaces.back()->getCanvas()->setMatrix(canvas->getTotalMatrix());
83             fSurfaces.back()->getCanvas()->scale(fScale, fScale);
84         }
85     }
86 }
87 
onPerCanvasPostDraw(SkCanvas * canvas)88 void SKPBench::onPerCanvasPostDraw(SkCanvas* canvas) {
89     // Draw the last set of tiles into the master canvas in case we're
90     // saving the images
91     for (int i = 0; i < fTileRects.count(); ++i) {
92         sk_sp<SkImage> image(fSurfaces[i]->makeImageSnapshot());
93         canvas->drawImage(image,
94                           SkIntToScalar(fTileRects[i].fLeft), SkIntToScalar(fTileRects[i].fTop));
95     }
96 
97     fSurfaces.reset();
98     fTileRects.rewind();
99 }
100 
isSuitableFor(Backend backend)101 bool SKPBench::isSuitableFor(Backend backend) {
102     return backend != kNonRendering_Backend;
103 }
104 
onGetSize()105 SkIPoint SKPBench::onGetSize() {
106     return SkIPoint::Make(fClip.width(), fClip.height());
107 }
108 
onDraw(int loops,SkCanvas * canvas)109 void SKPBench::onDraw(int loops, SkCanvas* canvas) {
110     SkASSERT(fDoLooping || 1 == loops);
111     while (1) {
112         if (fUseMultiPictureDraw) {
113             this->drawMPDPicture();
114         } else {
115             this->drawPicture();
116         }
117         if (0 == --loops) {
118             break;
119         }
120         // Ensure the GrContext doesn't combine ops across draw loops.
121         if (GrContext* context = canvas->getGrContext()) {
122             context->flush();
123         }
124     }
125 }
126 
drawMPDPicture()127 void SKPBench::drawMPDPicture() {
128     SkMultiPictureDraw mpd;
129 
130     for (int j = 0; j < fTileRects.count(); ++j) {
131         SkMatrix trans;
132         trans.setTranslate(-fTileRects[j].fLeft/fScale,
133                            -fTileRects[j].fTop/fScale);
134         mpd.add(fSurfaces[j]->getCanvas(), fPic.get(), &trans);
135     }
136 
137     // We flush after each picture to more closely model how Chrome rasterizes tiles.
138     mpd.draw(/*flush = */ true);
139 }
140 
drawPicture()141 void SKPBench::drawPicture() {
142     for (int j = 0; j < fTileRects.count(); ++j) {
143         const SkMatrix trans = SkMatrix::MakeTrans(-fTileRects[j].fLeft / fScale,
144                                                    -fTileRects[j].fTop / fScale);
145         fSurfaces[j]->getCanvas()->drawPicture(fPic.get(), &trans, nullptr);
146     }
147 
148     for (int j = 0; j < fTileRects.count(); ++j) {
149         fSurfaces[j]->getCanvas()->flush();
150     }
151 }
152 
153 #include "src/gpu/GrGpu.h"
draw_pic_for_stats(SkCanvas * canvas,GrContext * context,const SkPicture * picture,SkTArray<SkString> * keys,SkTArray<double> * values)154 static void draw_pic_for_stats(SkCanvas* canvas, GrContext* context, const SkPicture* picture,
155                                SkTArray<SkString>* keys, SkTArray<double>* values) {
156     context->priv().resetGpuStats();
157     canvas->drawPicture(picture);
158     canvas->flush();
159 
160     context->priv().dumpGpuStatsKeyValuePairs(keys, values);
161     context->priv().dumpCacheStatsKeyValuePairs(keys, values);
162 }
163 
getGpuStats(SkCanvas * canvas,SkTArray<SkString> * keys,SkTArray<double> * values)164 void SKPBench::getGpuStats(SkCanvas* canvas, SkTArray<SkString>* keys, SkTArray<double>* values) {
165     // we do a special single draw and then dump the key / value pairs
166     GrContext* context = canvas->getGrContext();
167     if (!context) {
168         return;
169     }
170 
171     // TODO refactor this out if we want to test other subclasses of skpbench
172     context->flush();
173     context->freeGpuResources();
174     context->resetContext();
175     context->priv().getGpu()->resetShaderCacheForTesting();
176     draw_pic_for_stats(canvas, context, fPic.get(), keys, values);
177 }
178