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