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 | #include "GrContext.h" |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 14 | #include "GrContextPriv.h" |
cdalton | e6d2024 | 2015-10-26 13:45:29 -0700 | [diff] [blame] | 15 | |
mtklein | bf9e600 | 2015-06-16 10:41:27 -0700 | [diff] [blame] | 16 | // These CPU tile sizes are not good per se, but they are similar to what Chrome uses. |
| 17 | DEFINE_int32(CPUbenchTileW, 256, "Tile width used for CPU SKP playback."); |
| 18 | DEFINE_int32(CPUbenchTileH, 256, "Tile height used for CPU SKP playback."); |
| 19 | |
| 20 | DEFINE_int32(GPUbenchTileW, 1600, "Tile width used for GPU SKP playback."); |
| 21 | DEFINE_int32(GPUbenchTileH, 512, "Tile height used for GPU SKP playback."); |
mtklein | 9200758 | 2014-08-01 07:46:52 -0700 | [diff] [blame] | 22 | |
robertphillips | 5b69377 | 2014-11-21 06:19:36 -0800 | [diff] [blame] | 23 | SKPBench::SKPBench(const char* name, const SkPicture* pic, const SkIRect& clip, SkScalar scale, |
cdalton | b402296 | 2015-06-25 10:51:56 -0700 | [diff] [blame] | 24 | bool useMultiPictureDraw, bool doLooping) |
mtklein | 9200758 | 2014-08-01 07:46:52 -0700 | [diff] [blame] | 25 | : fPic(SkRef(pic)) |
| 26 | , fClip(clip) |
mtklein | 9628905 | 2014-09-10 12:05:59 -0700 | [diff] [blame] | 27 | , fScale(scale) |
robertphillips | 5b69377 | 2014-11-21 06:19:36 -0800 | [diff] [blame] | 28 | , fName(name) |
cdalton | b402296 | 2015-06-25 10:51:56 -0700 | [diff] [blame] | 29 | , fUseMultiPictureDraw(useMultiPictureDraw) |
| 30 | , fDoLooping(doLooping) { |
tfarina | 0004e7d | 2015-01-26 06:47:55 -0800 | [diff] [blame] | 31 | 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] | 32 | if (useMultiPictureDraw) { |
| 33 | fUniqueName.append("_mpd"); |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | SKPBench::~SKPBench() { |
| 38 | for (int i = 0; i < fSurfaces.count(); ++i) { |
| 39 | fSurfaces[i]->unref(); |
| 40 | } |
mtklein | 9200758 | 2014-08-01 07:46:52 -0700 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | const char* SKPBench::onGetName() { |
| 44 | return fName.c_str(); |
| 45 | } |
| 46 | |
mtklein | 9628905 | 2014-09-10 12:05:59 -0700 | [diff] [blame] | 47 | const char* SKPBench::onGetUniqueName() { |
| 48 | return fUniqueName.c_str(); |
| 49 | } |
| 50 | |
robertphillips | 5b69377 | 2014-11-21 06:19:36 -0800 | [diff] [blame] | 51 | void SKPBench::onPerCanvasPreDraw(SkCanvas* canvas) { |
Mike Reed | 918e144 | 2017-01-23 11:39:45 -0500 | [diff] [blame] | 52 | SkIRect bounds = canvas->getDeviceClipBounds(); |
| 53 | SkAssertResult(!bounds.isEmpty()); |
robertphillips | 5b69377 | 2014-11-21 06:19:36 -0800 | [diff] [blame] | 54 | |
mtklein | bf9e600 | 2015-06-16 10:41:27 -0700 | [diff] [blame] | 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()); |
bsalomon | cc4d667 | 2015-03-05 13:42:27 -0800 | [diff] [blame] | 61 | |
| 62 | int xTiles = SkScalarCeilToInt(bounds.width() / SkIntToScalar(tileW)); |
| 63 | int yTiles = SkScalarCeilToInt(bounds.height() / SkIntToScalar(tileH)); |
robertphillips | 5b69377 | 2014-11-21 06:19:36 -0800 | [diff] [blame] | 64 | |
Ben Wagner | 9ec70c6 | 2018-07-12 13:30:47 -0400 | [diff] [blame] | 65 | fSurfaces.reserve(xTiles * yTiles); |
robertphillips | 5b69377 | 2014-11-21 06:19:36 -0800 | [diff] [blame] | 66 | fTileRects.setReserve(xTiles * yTiles); |
| 67 | |
bsalomon | cc4d667 | 2015-03-05 13:42:27 -0800 | [diff] [blame] | 68 | SkImageInfo ii = canvas->imageInfo().makeWH(tileW, tileH); |
robertphillips | 5b69377 | 2014-11-21 06:19:36 -0800 | [diff] [blame] | 69 | |
bsalomon | cc4d667 | 2015-03-05 13:42:27 -0800 | [diff] [blame] | 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); |
robertphillips | 63242d7 | 2014-12-04 08:31:02 -0800 | [diff] [blame] | 73 | *fTileRects.append() = tileRect; |
Ben Wagner | 9ec70c6 | 2018-07-12 13:30:47 -0400 | [diff] [blame] | 74 | fSurfaces.emplace_back(canvas->makeSurface(ii)); |
robertphillips | 63242d7 | 2014-12-04 08:31:02 -0800 | [diff] [blame] | 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)); |
Ben Wagner | 9ec70c6 | 2018-07-12 13:30:47 -0400 | [diff] [blame] | 80 | fSurfaces.back()->getCanvas()->clipRect(clip); |
robertphillips | 63242d7 | 2014-12-04 08:31:02 -0800 | [diff] [blame] | 81 | |
Ben Wagner | 9ec70c6 | 2018-07-12 13:30:47 -0400 | [diff] [blame] | 82 | fSurfaces.back()->getCanvas()->setMatrix(canvas->getTotalMatrix()); |
| 83 | fSurfaces.back()->getCanvas()->scale(fScale, fScale); |
robertphillips | 5b69377 | 2014-11-21 06:19:36 -0800 | [diff] [blame] | 84 | } |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | void SKPBench::onPerCanvasPostDraw(SkCanvas* canvas) { |
robertphillips | 5b69377 | 2014-11-21 06:19:36 -0800 | [diff] [blame] | 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) { |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 92 | sk_sp<SkImage> image(fSurfaces[i]->makeImageSnapshot()); |
robertphillips | 186a08e | 2014-11-21 06:53:00 -0800 | [diff] [blame] | 93 | canvas->drawImage(image, |
robertphillips | 5b69377 | 2014-11-21 06:19:36 -0800 | [diff] [blame] | 94 | SkIntToScalar(fTileRects[i].fLeft), SkIntToScalar(fTileRects[i].fTop)); |
robertphillips | 5b69377 | 2014-11-21 06:19:36 -0800 | [diff] [blame] | 95 | } |
| 96 | |
Ben Wagner | 9ec70c6 | 2018-07-12 13:30:47 -0400 | [diff] [blame] | 97 | fSurfaces.reset(); |
robertphillips | 5b69377 | 2014-11-21 06:19:36 -0800 | [diff] [blame] | 98 | fTileRects.rewind(); |
| 99 | } |
| 100 | |
mtklein | 9200758 | 2014-08-01 07:46:52 -0700 | [diff] [blame] | 101 | bool SKPBench::isSuitableFor(Backend backend) { |
| 102 | return backend != kNonRendering_Backend; |
| 103 | } |
| 104 | |
| 105 | SkIPoint SKPBench::onGetSize() { |
| 106 | return SkIPoint::Make(fClip.width(), fClip.height()); |
| 107 | } |
| 108 | |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 109 | void SKPBench::onDraw(int loops, SkCanvas* canvas) { |
cdalton | b402296 | 2015-06-25 10:51:56 -0700 | [diff] [blame] | 110 | SkASSERT(fDoLooping || 1 == loops); |
cdalton | 31c45bb | 2016-02-22 08:08:25 -0800 | [diff] [blame] | 111 | while (1) { |
| 112 | if (fUseMultiPictureDraw) { |
joshualitt | 261c3ad | 2015-04-27 09:16:57 -0700 | [diff] [blame] | 113 | this->drawMPDPicture(); |
cdalton | 31c45bb | 2016-02-22 08:08:25 -0800 | [diff] [blame] | 114 | } else { |
joshualitt | 261c3ad | 2015-04-27 09:16:57 -0700 | [diff] [blame] | 115 | this->drawPicture(); |
robertphillips | 5b69377 | 2014-11-21 06:19:36 -0800 | [diff] [blame] | 116 | } |
cdalton | 31c45bb | 2016-02-22 08:08:25 -0800 | [diff] [blame] | 117 | if (0 == --loops) { |
| 118 | break; |
| 119 | } |
Brian Salomon | 09d994e | 2016-12-21 11:14:46 -0500 | [diff] [blame] | 120 | // Ensure the GrContext doesn't combine ops across draw loops. |
cdalton | 31c45bb | 2016-02-22 08:08:25 -0800 | [diff] [blame] | 121 | if (GrContext* context = canvas->getGrContext()) { |
| 122 | context->flush(); |
| 123 | } |
cdalton | 31c45bb | 2016-02-22 08:08:25 -0800 | [diff] [blame] | 124 | } |
mtklein | 9200758 | 2014-08-01 07:46:52 -0700 | [diff] [blame] | 125 | } |
joshualitt | 261c3ad | 2015-04-27 09:16:57 -0700 | [diff] [blame] | 126 | |
| 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); |
Hal Canary | 2db8361 | 2016-11-04 13:02:54 -0400 | [diff] [blame] | 134 | mpd.add(fSurfaces[j]->getCanvas(), fPic.get(), &trans); |
joshualitt | 261c3ad | 2015-04-27 09:16:57 -0700 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | mpd.draw(); |
| 138 | |
| 139 | for (int j = 0; j < fTileRects.count(); ++j) { |
| 140 | fSurfaces[j]->getCanvas()->flush(); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | void SKPBench::drawPicture() { |
| 145 | for (int j = 0; j < fTileRects.count(); ++j) { |
| 146 | const SkMatrix trans = SkMatrix::MakeTrans(-fTileRects[j].fLeft / fScale, |
| 147 | -fTileRects[j].fTop / fScale); |
Hal Canary | 2db8361 | 2016-11-04 13:02:54 -0400 | [diff] [blame] | 148 | fSurfaces[j]->getCanvas()->drawPicture(fPic.get(), &trans, nullptr); |
joshualitt | 261c3ad | 2015-04-27 09:16:57 -0700 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | for (int j = 0; j < fTileRects.count(); ++j) { |
| 152 | fSurfaces[j]->getCanvas()->flush(); |
| 153 | } |
| 154 | } |
joshualitt | e45c81c | 2015-12-02 09:05:37 -0800 | [diff] [blame] | 155 | |
joshualitt | 8fd844f | 2015-12-02 13:36:47 -0800 | [diff] [blame] | 156 | #include "GrGpu.h" |
joshualitt | e45c81c | 2015-12-02 09:05:37 -0800 | [diff] [blame] | 157 | static void draw_pic_for_stats(SkCanvas* canvas, GrContext* context, const SkPicture* picture, |
| 158 | SkTArray<SkString>* keys, SkTArray<double>* values, |
| 159 | const char* tag) { |
Robert Phillips | 0c4b7b1 | 2018-03-06 08:20:37 -0500 | [diff] [blame] | 160 | context->contextPriv().resetGpuStats(); |
joshualitt | e45c81c | 2015-12-02 09:05:37 -0800 | [diff] [blame] | 161 | canvas->drawPicture(picture); |
| 162 | canvas->flush(); |
| 163 | |
| 164 | int offset = keys->count(); |
Robert Phillips | 0c4b7b1 | 2018-03-06 08:20:37 -0500 | [diff] [blame] | 165 | context->contextPriv().dumpGpuStatsKeyValuePairs(keys, values); |
| 166 | context->contextPriv().dumpCacheStatsKeyValuePairs(keys, values); |
joshualitt | e45c81c | 2015-12-02 09:05:37 -0800 | [diff] [blame] | 167 | |
| 168 | // append tag, but only to new tags |
| 169 | for (int i = offset; i < keys->count(); i++, offset++) { |
| 170 | (*keys)[i].appendf("_%s", tag); |
| 171 | } |
| 172 | } |
joshualitt | e45c81c | 2015-12-02 09:05:37 -0800 | [diff] [blame] | 173 | |
| 174 | void SKPBench::getGpuStats(SkCanvas* canvas, SkTArray<SkString>* keys, SkTArray<double>* values) { |
joshualitt | e45c81c | 2015-12-02 09:05:37 -0800 | [diff] [blame] | 175 | // we do a special single draw and then dump the key / value pairs |
| 176 | GrContext* context = canvas->getGrContext(); |
| 177 | if (!context) { |
| 178 | return; |
| 179 | } |
| 180 | |
| 181 | // TODO refactor this out if we want to test other subclasses of skpbench |
| 182 | context->flush(); |
| 183 | context->freeGpuResources(); |
| 184 | context->resetContext(); |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 185 | context->contextPriv().getGpu()->resetShaderCacheForTesting(); |
Hal Canary | 2db8361 | 2016-11-04 13:02:54 -0400 | [diff] [blame] | 186 | draw_pic_for_stats(canvas, context, fPic.get(), keys, values, "first_frame"); |
joshualitt | e45c81c | 2015-12-02 09:05:37 -0800 | [diff] [blame] | 187 | |
| 188 | // draw second frame |
Hal Canary | 2db8361 | 2016-11-04 13:02:54 -0400 | [diff] [blame] | 189 | draw_pic_for_stats(canvas, context, fPic.get(), keys, values, "second_frame"); |
joshualitt | e45c81c | 2015-12-02 09:05:37 -0800 | [diff] [blame] | 190 | } |