mtklein | 9200758 | 2014-08-01 07:46:52 -0700 | [diff] [blame] | 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" |
mtklein | c7f7f46 | 2014-10-21 12:29:25 -0700 | [diff] [blame] | 9 | #include "SkCommandLineFlags.h" |
robertphillips | 5b69377 | 2014-11-21 06:19:36 -0800 | [diff] [blame] | 10 | #include "SkMultiPictureDraw.h" |
| 11 | #include "SkSurface.h" |
mtklein | c7f7f46 | 2014-10-21 12:29:25 -0700 | [diff] [blame] | 12 | |
cdalton | e6d2024 | 2015-10-26 13:45:29 -0700 | [diff] [blame] | 13 | #if SK_SUPPORT_GPU |
| 14 | #include "GrContext.h" |
| 15 | #endif |
| 16 | |
mtklein | bf9e600 | 2015-06-16 10:41:27 -0700 | [diff] [blame] | 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."); |
mtklein | 9200758 | 2014-08-01 07:46:52 -0700 | [diff] [blame] | 23 | |
robertphillips | 5b69377 | 2014-11-21 06:19:36 -0800 | [diff] [blame] | 24 | SKPBench::SKPBench(const char* name, const SkPicture* pic, const SkIRect& clip, SkScalar scale, |
cdalton | b402296 | 2015-06-25 10:51:56 -0700 | [diff] [blame] | 25 | bool useMultiPictureDraw, bool doLooping) |
mtklein | 9200758 | 2014-08-01 07:46:52 -0700 | [diff] [blame] | 26 | : fPic(SkRef(pic)) |
| 27 | , fClip(clip) |
mtklein | 9628905 | 2014-09-10 12:05:59 -0700 | [diff] [blame] | 28 | , fScale(scale) |
robertphillips | 5b69377 | 2014-11-21 06:19:36 -0800 | [diff] [blame] | 29 | , fName(name) |
cdalton | b402296 | 2015-06-25 10:51:56 -0700 | [diff] [blame] | 30 | , fUseMultiPictureDraw(useMultiPictureDraw) |
| 31 | , fDoLooping(doLooping) { |
tfarina | 0004e7d | 2015-01-26 06:47:55 -0800 | [diff] [blame] | 32 | fUniqueName.printf("%s_%.2g", name, scale); // Scale makes this unqiue for perf.skia.org traces. |
robertphillips | 5b69377 | 2014-11-21 06:19:36 -0800 | [diff] [blame] | 33 | if (useMultiPictureDraw) { |
| 34 | fUniqueName.append("_mpd"); |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | SKPBench::~SKPBench() { |
| 39 | for (int i = 0; i < fSurfaces.count(); ++i) { |
| 40 | fSurfaces[i]->unref(); |
| 41 | } |
mtklein | 9200758 | 2014-08-01 07:46:52 -0700 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | const char* SKPBench::onGetName() { |
| 45 | return fName.c_str(); |
| 46 | } |
| 47 | |
mtklein | 9628905 | 2014-09-10 12:05:59 -0700 | [diff] [blame] | 48 | const char* SKPBench::onGetUniqueName() { |
| 49 | return fUniqueName.c_str(); |
| 50 | } |
| 51 | |
robertphillips | 5b69377 | 2014-11-21 06:19:36 -0800 | [diff] [blame] | 52 | void SKPBench::onPerCanvasPreDraw(SkCanvas* canvas) { |
Mike Reed | 918e144 | 2017-01-23 11:39:45 -0500 | [diff] [blame] | 53 | SkIRect bounds = canvas->getDeviceClipBounds(); |
| 54 | SkAssertResult(!bounds.isEmpty()); |
robertphillips | 5b69377 | 2014-11-21 06:19:36 -0800 | [diff] [blame] | 55 | |
mtklein | bf9e600 | 2015-06-16 10:41:27 -0700 | [diff] [blame] | 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()); |
bsalomon | cc4d667 | 2015-03-05 13:42:27 -0800 | [diff] [blame] | 62 | |
| 63 | int xTiles = SkScalarCeilToInt(bounds.width() / SkIntToScalar(tileW)); |
| 64 | int yTiles = SkScalarCeilToInt(bounds.height() / SkIntToScalar(tileH)); |
robertphillips | 5b69377 | 2014-11-21 06:19:36 -0800 | [diff] [blame] | 65 | |
| 66 | fSurfaces.setReserve(xTiles * yTiles); |
| 67 | fTileRects.setReserve(xTiles * yTiles); |
| 68 | |
bsalomon | cc4d667 | 2015-03-05 13:42:27 -0800 | [diff] [blame] | 69 | SkImageInfo ii = canvas->imageInfo().makeWH(tileW, tileH); |
robertphillips | 5b69377 | 2014-11-21 06:19:36 -0800 | [diff] [blame] | 70 | |
bsalomon | cc4d667 | 2015-03-05 13:42:27 -0800 | [diff] [blame] | 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); |
robertphillips | 63242d7 | 2014-12-04 08:31:02 -0800 | [diff] [blame] | 74 | *fTileRects.append() = tileRect; |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 75 | *fSurfaces.push() = canvas->makeSurface(ii).release(); |
robertphillips | 63242d7 | 2014-12-04 08:31:02 -0800 | [diff] [blame] | 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 | |
robertphillips | 5b69377 | 2014-11-21 06:19:36 -0800 | [diff] [blame] | 83 | fSurfaces.top()->getCanvas()->setMatrix(canvas->getTotalMatrix()); |
| 84 | fSurfaces.top()->getCanvas()->scale(fScale, fScale); |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | void SKPBench::onPerCanvasPostDraw(SkCanvas* canvas) { |
robertphillips | 5b69377 | 2014-11-21 06:19:36 -0800 | [diff] [blame] | 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) { |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 93 | sk_sp<SkImage> image(fSurfaces[i]->makeImageSnapshot()); |
robertphillips | 186a08e | 2014-11-21 06:53:00 -0800 | [diff] [blame] | 94 | canvas->drawImage(image, |
robertphillips | 5b69377 | 2014-11-21 06:19:36 -0800 | [diff] [blame] | 95 | SkIntToScalar(fTileRects[i].fLeft), SkIntToScalar(fTileRects[i].fTop)); |
| 96 | SkSafeSetNull(fSurfaces[i]); |
| 97 | } |
| 98 | |
| 99 | fSurfaces.rewind(); |
| 100 | fTileRects.rewind(); |
| 101 | } |
| 102 | |
mtklein | 9200758 | 2014-08-01 07:46:52 -0700 | [diff] [blame] | 103 | bool SKPBench::isSuitableFor(Backend backend) { |
| 104 | return backend != kNonRendering_Backend; |
| 105 | } |
| 106 | |
| 107 | SkIPoint SKPBench::onGetSize() { |
| 108 | return SkIPoint::Make(fClip.width(), fClip.height()); |
| 109 | } |
| 110 | |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 111 | void SKPBench::onDraw(int loops, SkCanvas* canvas) { |
cdalton | b402296 | 2015-06-25 10:51:56 -0700 | [diff] [blame] | 112 | SkASSERT(fDoLooping || 1 == loops); |
cdalton | 31c45bb | 2016-02-22 08:08:25 -0800 | [diff] [blame] | 113 | while (1) { |
| 114 | if (fUseMultiPictureDraw) { |
joshualitt | 261c3ad | 2015-04-27 09:16:57 -0700 | [diff] [blame] | 115 | this->drawMPDPicture(); |
cdalton | 31c45bb | 2016-02-22 08:08:25 -0800 | [diff] [blame] | 116 | } else { |
joshualitt | 261c3ad | 2015-04-27 09:16:57 -0700 | [diff] [blame] | 117 | this->drawPicture(); |
robertphillips | 5b69377 | 2014-11-21 06:19:36 -0800 | [diff] [blame] | 118 | } |
cdalton | 31c45bb | 2016-02-22 08:08:25 -0800 | [diff] [blame] | 119 | if (0 == --loops) { |
| 120 | break; |
| 121 | } |
cdalton | e6d2024 | 2015-10-26 13:45:29 -0700 | [diff] [blame] | 122 | #if SK_SUPPORT_GPU |
Brian Salomon | 09d994e | 2016-12-21 11:14:46 -0500 | [diff] [blame] | 123 | // Ensure the GrContext doesn't combine ops across draw loops. |
cdalton | 31c45bb | 2016-02-22 08:08:25 -0800 | [diff] [blame] | 124 | if (GrContext* context = canvas->getGrContext()) { |
| 125 | context->flush(); |
| 126 | } |
cdalton | e6d2024 | 2015-10-26 13:45:29 -0700 | [diff] [blame] | 127 | #endif |
cdalton | 31c45bb | 2016-02-22 08:08:25 -0800 | [diff] [blame] | 128 | } |
mtklein | 9200758 | 2014-08-01 07:46:52 -0700 | [diff] [blame] | 129 | } |
joshualitt | 261c3ad | 2015-04-27 09:16:57 -0700 | [diff] [blame] | 130 | |
| 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); |
Hal Canary | 2db8361 | 2016-11-04 13:02:54 -0400 | [diff] [blame] | 138 | mpd.add(fSurfaces[j]->getCanvas(), fPic.get(), &trans); |
joshualitt | 261c3ad | 2015-04-27 09:16:57 -0700 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | mpd.draw(); |
| 142 | |
| 143 | for (int j = 0; j < fTileRects.count(); ++j) { |
| 144 | fSurfaces[j]->getCanvas()->flush(); |
| 145 | } |
| 146 | } |
| 147 | |
| 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); |
Hal Canary | 2db8361 | 2016-11-04 13:02:54 -0400 | [diff] [blame] | 152 | fSurfaces[j]->getCanvas()->drawPicture(fPic.get(), &trans, nullptr); |
joshualitt | 261c3ad | 2015-04-27 09:16:57 -0700 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | for (int j = 0; j < fTileRects.count(); ++j) { |
| 156 | fSurfaces[j]->getCanvas()->flush(); |
| 157 | } |
| 158 | } |
joshualitt | e45c81c | 2015-12-02 09:05:37 -0800 | [diff] [blame] | 159 | |
| 160 | #if SK_SUPPORT_GPU |
joshualitt | 8fd844f | 2015-12-02 13:36:47 -0800 | [diff] [blame] | 161 | #include "GrGpu.h" |
joshualitt | e45c81c | 2015-12-02 09:05:37 -0800 | [diff] [blame] | 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); |
joshualitt | dc5685a | 2015-12-02 14:08:25 -0800 | [diff] [blame] | 171 | context->dumpCacheStatsKeyValuePairs(keys, values); |
joshualitt | e45c81c | 2015-12-02 09:05:37 -0800 | [diff] [blame] | 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 | |
| 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(); |
joshualitt | 8fd844f | 2015-12-02 13:36:47 -0800 | [diff] [blame] | 192 | context->getGpu()->resetShaderCacheForTesting(); |
Hal Canary | 2db8361 | 2016-11-04 13:02:54 -0400 | [diff] [blame] | 193 | draw_pic_for_stats(canvas, context, fPic.get(), keys, values, "first_frame"); |
joshualitt | e45c81c | 2015-12-02 09:05:37 -0800 | [diff] [blame] | 194 | |
| 195 | // draw second frame |
Hal Canary | 2db8361 | 2016-11-04 13:02:54 -0400 | [diff] [blame] | 196 | draw_pic_for_stats(canvas, context, fPic.get(), keys, values, "second_frame"); |
joshualitt | e45c81c | 2015-12-02 09:05:37 -0800 | [diff] [blame] | 197 | |
| 198 | #endif |
| 199 | } |