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) { |
robertphillips | 5b69377 | 2014-11-21 06:19:36 -0800 | [diff] [blame] | 53 | SkIRect bounds; |
| 54 | SkAssertResult(canvas->getClipDeviceBounds(&bounds)); |
| 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; |
robertphillips | 5b69377 | 2014-11-21 06:19:36 -0800 | [diff] [blame] | 75 | *fSurfaces.push() = canvas->newSurface(ii); |
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) { |
robertphillips | 186a08e | 2014-11-21 06:53:00 -0800 | [diff] [blame] | 93 | SkAutoTUnref<SkImage> image(fSurfaces[i]->newImageSnapshot()); |
| 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); |
robertphillips | 5b69377 | 2014-11-21 06:19:36 -0800 | [diff] [blame] | 113 | if (fUseMultiPictureDraw) { |
| 114 | for (int i = 0; i < loops; i++) { |
joshualitt | 261c3ad | 2015-04-27 09:16:57 -0700 | [diff] [blame] | 115 | this->drawMPDPicture(); |
mtklein | 9200758 | 2014-08-01 07:46:52 -0700 | [diff] [blame] | 116 | } |
robertphillips | 5b69377 | 2014-11-21 06:19:36 -0800 | [diff] [blame] | 117 | } else { |
robertphillips | 5b69377 | 2014-11-21 06:19:36 -0800 | [diff] [blame] | 118 | for (int i = 0; i < loops; i++) { |
joshualitt | 261c3ad | 2015-04-27 09:16:57 -0700 | [diff] [blame] | 119 | this->drawPicture(); |
robertphillips | 5b69377 | 2014-11-21 06:19:36 -0800 | [diff] [blame] | 120 | } |
mtklein | c7f7f46 | 2014-10-21 12:29:25 -0700 | [diff] [blame] | 121 | } |
cdalton | e6d2024 | 2015-10-26 13:45:29 -0700 | [diff] [blame] | 122 | #if SK_SUPPORT_GPU |
| 123 | // Ensure the GrContext doesn't batch across draw loops. |
| 124 | if (GrContext* context = canvas->getGrContext()) { |
| 125 | context->flush(); |
| 126 | } |
| 127 | #endif |
mtklein | 9200758 | 2014-08-01 07:46:52 -0700 | [diff] [blame] | 128 | } |
joshualitt | 261c3ad | 2015-04-27 09:16:57 -0700 | [diff] [blame] | 129 | |
| 130 | void SKPBench::drawMPDPicture() { |
| 131 | SkMultiPictureDraw mpd; |
| 132 | |
| 133 | for (int j = 0; j < fTileRects.count(); ++j) { |
| 134 | SkMatrix trans; |
| 135 | trans.setTranslate(-fTileRects[j].fLeft/fScale, |
| 136 | -fTileRects[j].fTop/fScale); |
| 137 | mpd.add(fSurfaces[j]->getCanvas(), fPic, &trans); |
| 138 | } |
| 139 | |
| 140 | mpd.draw(); |
| 141 | |
| 142 | for (int j = 0; j < fTileRects.count(); ++j) { |
| 143 | fSurfaces[j]->getCanvas()->flush(); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | void SKPBench::drawPicture() { |
| 148 | for (int j = 0; j < fTileRects.count(); ++j) { |
| 149 | const SkMatrix trans = SkMatrix::MakeTrans(-fTileRects[j].fLeft / fScale, |
| 150 | -fTileRects[j].fTop / fScale); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 151 | fSurfaces[j]->getCanvas()->drawPicture(fPic, &trans, nullptr); |
joshualitt | 261c3ad | 2015-04-27 09:16:57 -0700 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | for (int j = 0; j < fTileRects.count(); ++j) { |
| 155 | fSurfaces[j]->getCanvas()->flush(); |
| 156 | } |
| 157 | } |
joshualitt | e45c81c | 2015-12-02 09:05:37 -0800 | [diff] [blame] | 158 | |
| 159 | #if SK_SUPPORT_GPU |
joshualitt | 8fd844f | 2015-12-02 13:36:47 -0800 | [diff] [blame] | 160 | #include "GrGpu.h" |
joshualitt | e45c81c | 2015-12-02 09:05:37 -0800 | [diff] [blame] | 161 | static void draw_pic_for_stats(SkCanvas* canvas, GrContext* context, const SkPicture* picture, |
| 162 | SkTArray<SkString>* keys, SkTArray<double>* values, |
| 163 | const char* tag) { |
| 164 | context->resetGpuStats(); |
| 165 | canvas->drawPicture(picture); |
| 166 | canvas->flush(); |
| 167 | |
| 168 | int offset = keys->count(); |
| 169 | context->dumpGpuStatsKeyValuePairs(keys, values); |
joshualitt | dc5685a | 2015-12-02 14:08:25 -0800 | [diff] [blame] | 170 | context->dumpCacheStatsKeyValuePairs(keys, values); |
joshualitt | e45c81c | 2015-12-02 09:05:37 -0800 | [diff] [blame] | 171 | |
| 172 | // append tag, but only to new tags |
| 173 | for (int i = offset; i < keys->count(); i++, offset++) { |
| 174 | (*keys)[i].appendf("_%s", tag); |
| 175 | } |
| 176 | } |
| 177 | #endif |
| 178 | |
| 179 | void SKPBench::getGpuStats(SkCanvas* canvas, SkTArray<SkString>* keys, SkTArray<double>* values) { |
| 180 | #if SK_SUPPORT_GPU |
| 181 | // we do a special single draw and then dump the key / value pairs |
| 182 | GrContext* context = canvas->getGrContext(); |
| 183 | if (!context) { |
| 184 | return; |
| 185 | } |
| 186 | |
| 187 | // TODO refactor this out if we want to test other subclasses of skpbench |
| 188 | context->flush(); |
| 189 | context->freeGpuResources(); |
| 190 | context->resetContext(); |
joshualitt | 8fd844f | 2015-12-02 13:36:47 -0800 | [diff] [blame] | 191 | context->getGpu()->resetShaderCacheForTesting(); |
joshualitt | e45c81c | 2015-12-02 09:05:37 -0800 | [diff] [blame] | 192 | draw_pic_for_stats(canvas, context, fPic, keys, values, "first_frame"); |
| 193 | |
| 194 | // draw second frame |
| 195 | draw_pic_for_stats(canvas, context, fPic, keys, values, "second_frame"); |
| 196 | |
| 197 | #endif |
| 198 | } |