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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "bench/SKPBench.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 9 | #include "include/core/SkSurface.h" |
Robert Phillips | f028810 | 2020-07-06 13:45:34 -0400 | [diff] [blame] | 10 | #include "include/gpu/GrDirectContext.h" |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 11 | #include "src/gpu/GrDirectContextPriv.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 12 | #include "tools/flags/CommandLineFlags.h" |
mtklein | c7f7f46 | 2014-10-21 12:29:25 -0700 | [diff] [blame] | 13 | |
cdalton | e6d2024 | 2015-10-26 13:45:29 -0700 | [diff] [blame] | 14 | |
mtklein | bf9e600 | 2015-06-16 10:41:27 -0700 | [diff] [blame] | 15 | // These CPU tile sizes are not good per se, but they are similar to what Chrome uses. |
Mike Klein | 5b3f343 | 2019-03-21 11:42:21 -0500 | [diff] [blame] | 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."); |
mtklein | bf9e600 | 2015-06-16 10:41:27 -0700 | [diff] [blame] | 18 | |
Mike Klein | 5b3f343 | 2019-03-21 11:42:21 -0500 | [diff] [blame] | 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."); |
mtklein | 9200758 | 2014-08-01 07:46:52 -0700 | [diff] [blame] | 21 | |
robertphillips | 5b69377 | 2014-11-21 06:19:36 -0800 | [diff] [blame] | 22 | SKPBench::SKPBench(const char* name, const SkPicture* pic, const SkIRect& clip, SkScalar scale, |
Brian Salomon | 626349b | 2020-08-27 10:54:36 -0400 | [diff] [blame] | 23 | bool doLooping) |
mtklein | 9200758 | 2014-08-01 07:46:52 -0700 | [diff] [blame] | 24 | : fPic(SkRef(pic)) |
| 25 | , fClip(clip) |
mtklein | 9628905 | 2014-09-10 12:05:59 -0700 | [diff] [blame] | 26 | , fScale(scale) |
robertphillips | 5b69377 | 2014-11-21 06:19:36 -0800 | [diff] [blame] | 27 | , fName(name) |
cdalton | b402296 | 2015-06-25 10:51:56 -0700 | [diff] [blame] | 28 | , fDoLooping(doLooping) { |
tfarina | 0004e7d | 2015-01-26 06:47:55 -0800 | [diff] [blame] | 29 | 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] | 30 | } |
| 31 | |
| 32 | SKPBench::~SKPBench() { |
| 33 | for (int i = 0; i < fSurfaces.count(); ++i) { |
| 34 | fSurfaces[i]->unref(); |
| 35 | } |
mtklein | 9200758 | 2014-08-01 07:46:52 -0700 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | const char* SKPBench::onGetName() { |
| 39 | return fName.c_str(); |
| 40 | } |
| 41 | |
mtklein | 9628905 | 2014-09-10 12:05:59 -0700 | [diff] [blame] | 42 | const char* SKPBench::onGetUniqueName() { |
| 43 | return fUniqueName.c_str(); |
| 44 | } |
| 45 | |
robertphillips | 5b69377 | 2014-11-21 06:19:36 -0800 | [diff] [blame] | 46 | void SKPBench::onPerCanvasPreDraw(SkCanvas* canvas) { |
Mike Reed | 918e144 | 2017-01-23 11:39:45 -0500 | [diff] [blame] | 47 | SkIRect bounds = canvas->getDeviceClipBounds(); |
| 48 | SkAssertResult(!bounds.isEmpty()); |
robertphillips | 5b69377 | 2014-11-21 06:19:36 -0800 | [diff] [blame] | 49 | |
Robert Phillips | f028810 | 2020-07-06 13:45:34 -0400 | [diff] [blame] | 50 | const bool gpu = canvas->recordingContext() != nullptr; |
mtklein | bf9e600 | 2015-06-16 10:41:27 -0700 | [diff] [blame] | 51 | int tileW = gpu ? FLAGS_GPUbenchTileW : FLAGS_CPUbenchTileW, |
| 52 | tileH = gpu ? FLAGS_GPUbenchTileH : FLAGS_CPUbenchTileH; |
| 53 | |
Brian Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 54 | tileW = std::min(tileW, bounds.width()); |
| 55 | tileH = std::min(tileH, bounds.height()); |
bsalomon | cc4d667 | 2015-03-05 13:42:27 -0800 | [diff] [blame] | 56 | |
| 57 | int xTiles = SkScalarCeilToInt(bounds.width() / SkIntToScalar(tileW)); |
| 58 | int yTiles = SkScalarCeilToInt(bounds.height() / SkIntToScalar(tileH)); |
robertphillips | 5b69377 | 2014-11-21 06:19:36 -0800 | [diff] [blame] | 59 | |
John Stiles | f4bda74 | 2020-10-14 16:57:41 -0400 | [diff] [blame] | 60 | fSurfaces.reserve_back(xTiles * yTiles); |
robertphillips | 5b69377 | 2014-11-21 06:19:36 -0800 | [diff] [blame] | 61 | fTileRects.setReserve(xTiles * yTiles); |
| 62 | |
bsalomon | cc4d667 | 2015-03-05 13:42:27 -0800 | [diff] [blame] | 63 | SkImageInfo ii = canvas->imageInfo().makeWH(tileW, tileH); |
robertphillips | 5b69377 | 2014-11-21 06:19:36 -0800 | [diff] [blame] | 64 | |
bsalomon | cc4d667 | 2015-03-05 13:42:27 -0800 | [diff] [blame] | 65 | for (int y = bounds.fTop; y < bounds.fBottom; y += tileH) { |
| 66 | for (int x = bounds.fLeft; x < bounds.fRight; x += tileW) { |
| 67 | const SkIRect tileRect = SkIRect::MakeXYWH(x, y, tileW, tileH); |
robertphillips | 63242d7 | 2014-12-04 08:31:02 -0800 | [diff] [blame] | 68 | *fTileRects.append() = tileRect; |
Ben Wagner | 9ec70c6 | 2018-07-12 13:30:47 -0400 | [diff] [blame] | 69 | fSurfaces.emplace_back(canvas->makeSurface(ii)); |
robertphillips | 63242d7 | 2014-12-04 08:31:02 -0800 | [diff] [blame] | 70 | |
| 71 | // Never want the contents of a tile to include stuff the parent |
| 72 | // canvas clips out |
| 73 | SkRect clip = SkRect::Make(bounds); |
| 74 | clip.offset(-SkIntToScalar(tileRect.fLeft), -SkIntToScalar(tileRect.fTop)); |
Ben Wagner | 9ec70c6 | 2018-07-12 13:30:47 -0400 | [diff] [blame] | 75 | fSurfaces.back()->getCanvas()->clipRect(clip); |
robertphillips | 63242d7 | 2014-12-04 08:31:02 -0800 | [diff] [blame] | 76 | |
Mike Reed | 1a4140e | 2020-12-03 11:21:31 -0500 | [diff] [blame] | 77 | fSurfaces.back()->getCanvas()->setMatrix(canvas->getLocalToDevice()); |
Ben Wagner | 9ec70c6 | 2018-07-12 13:30:47 -0400 | [diff] [blame] | 78 | fSurfaces.back()->getCanvas()->scale(fScale, fScale); |
robertphillips | 5b69377 | 2014-11-21 06:19:36 -0800 | [diff] [blame] | 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | void SKPBench::onPerCanvasPostDraw(SkCanvas* canvas) { |
Leon Scroggins III | 1ff0706 | 2020-07-27 14:52:19 -0400 | [diff] [blame] | 84 | // Draw the last set of tiles into the main canvas in case we're |
robertphillips | 5b69377 | 2014-11-21 06:19:36 -0800 | [diff] [blame] | 85 | // saving the images |
| 86 | for (int i = 0; i < fTileRects.count(); ++i) { |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 87 | sk_sp<SkImage> image(fSurfaces[i]->makeImageSnapshot()); |
robertphillips | 186a08e | 2014-11-21 06:53:00 -0800 | [diff] [blame] | 88 | canvas->drawImage(image, |
robertphillips | 5b69377 | 2014-11-21 06:19:36 -0800 | [diff] [blame] | 89 | SkIntToScalar(fTileRects[i].fLeft), SkIntToScalar(fTileRects[i].fTop)); |
robertphillips | 5b69377 | 2014-11-21 06:19:36 -0800 | [diff] [blame] | 90 | } |
| 91 | |
Ben Wagner | 9ec70c6 | 2018-07-12 13:30:47 -0400 | [diff] [blame] | 92 | fSurfaces.reset(); |
robertphillips | 5b69377 | 2014-11-21 06:19:36 -0800 | [diff] [blame] | 93 | fTileRects.rewind(); |
| 94 | } |
| 95 | |
mtklein | 9200758 | 2014-08-01 07:46:52 -0700 | [diff] [blame] | 96 | bool SKPBench::isSuitableFor(Backend backend) { |
| 97 | return backend != kNonRendering_Backend; |
| 98 | } |
| 99 | |
| 100 | SkIPoint SKPBench::onGetSize() { |
| 101 | return SkIPoint::Make(fClip.width(), fClip.height()); |
| 102 | } |
| 103 | |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 104 | void SKPBench::onDraw(int loops, SkCanvas* canvas) { |
cdalton | b402296 | 2015-06-25 10:51:56 -0700 | [diff] [blame] | 105 | SkASSERT(fDoLooping || 1 == loops); |
cdalton | 31c45bb | 2016-02-22 08:08:25 -0800 | [diff] [blame] | 106 | while (1) { |
Brian Salomon | 626349b | 2020-08-27 10:54:36 -0400 | [diff] [blame] | 107 | this->drawPicture(); |
cdalton | 31c45bb | 2016-02-22 08:08:25 -0800 | [diff] [blame] | 108 | if (0 == --loops) { |
| 109 | break; |
| 110 | } |
Robert Phillips | f028810 | 2020-07-06 13:45:34 -0400 | [diff] [blame] | 111 | |
| 112 | auto direct = canvas->recordingContext() ? canvas->recordingContext()->asDirectContext() |
| 113 | : nullptr; |
Brian Salomon | 09d994e | 2016-12-21 11:14:46 -0500 | [diff] [blame] | 114 | // Ensure the GrContext doesn't combine ops across draw loops. |
Robert Phillips | f028810 | 2020-07-06 13:45:34 -0400 | [diff] [blame] | 115 | if (direct) { |
| 116 | direct->flushAndSubmit(); |
cdalton | 31c45bb | 2016-02-22 08:08:25 -0800 | [diff] [blame] | 117 | } |
cdalton | 31c45bb | 2016-02-22 08:08:25 -0800 | [diff] [blame] | 118 | } |
mtklein | 9200758 | 2014-08-01 07:46:52 -0700 | [diff] [blame] | 119 | } |
joshualitt | 261c3ad | 2015-04-27 09:16:57 -0700 | [diff] [blame] | 120 | |
| 121 | void SKPBench::drawMPDPicture() { |
Mike Reed | 2319b80 | 2019-11-15 10:39:50 -0500 | [diff] [blame] | 122 | // TODO: remove me |
joshualitt | 261c3ad | 2015-04-27 09:16:57 -0700 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | void SKPBench::drawPicture() { |
| 126 | for (int j = 0; j < fTileRects.count(); ++j) { |
Mike Reed | 1f60733 | 2020-05-21 12:11:27 -0400 | [diff] [blame] | 127 | const SkMatrix trans = SkMatrix::Translate(-fTileRects[j].fLeft / fScale, |
joshualitt | 261c3ad | 2015-04-27 09:16:57 -0700 | [diff] [blame] | 128 | -fTileRects[j].fTop / fScale); |
Hal Canary | 2db8361 | 2016-11-04 13:02:54 -0400 | [diff] [blame] | 129 | fSurfaces[j]->getCanvas()->drawPicture(fPic.get(), &trans, nullptr); |
joshualitt | 261c3ad | 2015-04-27 09:16:57 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | for (int j = 0; j < fTileRects.count(); ++j) { |
Robert Phillips | 2c21a11 | 2020-11-20 13:49:37 -0500 | [diff] [blame] | 133 | fSurfaces[j]->flush(); |
joshualitt | 261c3ad | 2015-04-27 09:16:57 -0700 | [diff] [blame] | 134 | } |
| 135 | } |
joshualitt | e45c81c | 2015-12-02 09:05:37 -0800 | [diff] [blame] | 136 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 137 | #include "src/gpu/GrGpu.h" |
Robert Phillips | 2c21a11 | 2020-11-20 13:49:37 -0500 | [diff] [blame] | 138 | static void draw_pic_for_stats(SkCanvas* canvas, |
| 139 | GrDirectContext* dContext, |
| 140 | const SkPicture* picture, |
| 141 | SkTArray<SkString>* keys, |
| 142 | SkTArray<double>* values) { |
| 143 | dContext->priv().resetGpuStats(); |
| 144 | dContext->priv().resetContextStats(); |
joshualitt | e45c81c | 2015-12-02 09:05:37 -0800 | [diff] [blame] | 145 | canvas->drawPicture(picture); |
Robert Phillips | 2c21a11 | 2020-11-20 13:49:37 -0500 | [diff] [blame] | 146 | dContext->flush(); |
joshualitt | e45c81c | 2015-12-02 09:05:37 -0800 | [diff] [blame] | 147 | |
Robert Phillips | 2c21a11 | 2020-11-20 13:49:37 -0500 | [diff] [blame] | 148 | dContext->priv().dumpGpuStatsKeyValuePairs(keys, values); |
| 149 | dContext->priv().dumpCacheStatsKeyValuePairs(keys, values); |
| 150 | dContext->priv().dumpContextStatsKeyValuePairs(keys, values); |
joshualitt | e45c81c | 2015-12-02 09:05:37 -0800 | [diff] [blame] | 151 | } |
joshualitt | e45c81c | 2015-12-02 09:05:37 -0800 | [diff] [blame] | 152 | |
| 153 | void SKPBench::getGpuStats(SkCanvas* canvas, SkTArray<SkString>* keys, SkTArray<double>* values) { |
joshualitt | e45c81c | 2015-12-02 09:05:37 -0800 | [diff] [blame] | 154 | // we do a special single draw and then dump the key / value pairs |
Robert Phillips | f028810 | 2020-07-06 13:45:34 -0400 | [diff] [blame] | 155 | auto direct = canvas->recordingContext() ? canvas->recordingContext()->asDirectContext() |
| 156 | : nullptr; |
| 157 | if (!direct) { |
joshualitt | e45c81c | 2015-12-02 09:05:37 -0800 | [diff] [blame] | 158 | return; |
| 159 | } |
| 160 | |
| 161 | // TODO refactor this out if we want to test other subclasses of skpbench |
Robert Phillips | f028810 | 2020-07-06 13:45:34 -0400 | [diff] [blame] | 162 | direct->flushAndSubmit(); |
| 163 | direct->freeGpuResources(); |
| 164 | direct->resetContext(); |
| 165 | direct->priv().getGpu()->resetShaderCacheForTesting(); |
| 166 | draw_pic_for_stats(canvas, direct, fPic.get(), keys, values); |
joshualitt | e45c81c | 2015-12-02 09:05:37 -0800 | [diff] [blame] | 167 | } |