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 | |
mtklein | 6838d85 | 2014-10-29 14:15:10 -0700 | [diff] [blame] | 13 | DEFINE_int32(benchTile, 256, "Tile dimension used for SKP playback."); |
mtklein | 9200758 | 2014-08-01 07:46:52 -0700 | [diff] [blame] | 14 | |
robertphillips | 5b69377 | 2014-11-21 06:19:36 -0800 | [diff] [blame^] | 15 | SKPBench::SKPBench(const char* name, const SkPicture* pic, const SkIRect& clip, SkScalar scale, |
| 16 | bool useMultiPictureDraw) |
mtklein | 9200758 | 2014-08-01 07:46:52 -0700 | [diff] [blame] | 17 | : fPic(SkRef(pic)) |
| 18 | , fClip(clip) |
mtklein | 9628905 | 2014-09-10 12:05:59 -0700 | [diff] [blame] | 19 | , fScale(scale) |
robertphillips | 5b69377 | 2014-11-21 06:19:36 -0800 | [diff] [blame^] | 20 | , fName(name) |
| 21 | , fUseMultiPictureDraw(useMultiPictureDraw) { |
mtklein | 9628905 | 2014-09-10 12:05:59 -0700 | [diff] [blame] | 22 | fUniqueName.printf("%s_%.2g", name, scale); // Scale makes this unqiue for skiaperf.com traces. |
robertphillips | 5b69377 | 2014-11-21 06:19:36 -0800 | [diff] [blame^] | 23 | if (useMultiPictureDraw) { |
| 24 | fUniqueName.append("_mpd"); |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | SKPBench::~SKPBench() { |
| 29 | for (int i = 0; i < fSurfaces.count(); ++i) { |
| 30 | fSurfaces[i]->unref(); |
| 31 | } |
mtklein | 9200758 | 2014-08-01 07:46:52 -0700 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | const char* SKPBench::onGetName() { |
| 35 | return fName.c_str(); |
| 36 | } |
| 37 | |
mtklein | 9628905 | 2014-09-10 12:05:59 -0700 | [diff] [blame] | 38 | const char* SKPBench::onGetUniqueName() { |
| 39 | return fUniqueName.c_str(); |
| 40 | } |
| 41 | |
robertphillips | 5b69377 | 2014-11-21 06:19:36 -0800 | [diff] [blame^] | 42 | void SKPBench::onPerCanvasPreDraw(SkCanvas* canvas) { |
| 43 | if (!fUseMultiPictureDraw) { |
| 44 | return; |
| 45 | } |
| 46 | |
| 47 | SkIRect bounds; |
| 48 | SkAssertResult(canvas->getClipDeviceBounds(&bounds)); |
| 49 | |
| 50 | int xTiles = SkScalarCeilToInt(bounds.width() / SkIntToScalar(FLAGS_benchTile)); |
| 51 | int yTiles = SkScalarCeilToInt(bounds.height() / SkIntToScalar(FLAGS_benchTile)); |
| 52 | |
| 53 | fSurfaces.setReserve(xTiles * yTiles); |
| 54 | fTileRects.setReserve(xTiles * yTiles); |
| 55 | |
| 56 | SkImageInfo ii = canvas->imageInfo().makeWH(FLAGS_benchTile, FLAGS_benchTile); |
| 57 | |
| 58 | for (int y = bounds.fTop; y < bounds.fBottom; y += FLAGS_benchTile) { |
| 59 | for (int x = bounds.fLeft; x < bounds.fRight; x += FLAGS_benchTile) { |
| 60 | *fTileRects.append() = SkIRect::MakeXYWH(x, y, FLAGS_benchTile, FLAGS_benchTile); |
| 61 | *fSurfaces.push() = canvas->newSurface(ii); |
| 62 | fSurfaces.top()->getCanvas()->setMatrix(canvas->getTotalMatrix()); |
| 63 | fSurfaces.top()->getCanvas()->scale(fScale, fScale); |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | void SKPBench::onPerCanvasPostDraw(SkCanvas* canvas) { |
| 69 | if (!fUseMultiPictureDraw) { |
| 70 | return; |
| 71 | } |
| 72 | |
| 73 | // Draw the last set of tiles into the master canvas in case we're |
| 74 | // saving the images |
| 75 | for (int i = 0; i < fTileRects.count(); ++i) { |
| 76 | canvas->drawImage(fSurfaces[i]->newImageSnapshot(), |
| 77 | SkIntToScalar(fTileRects[i].fLeft), SkIntToScalar(fTileRects[i].fTop)); |
| 78 | SkSafeSetNull(fSurfaces[i]); |
| 79 | } |
| 80 | |
| 81 | fSurfaces.rewind(); |
| 82 | fTileRects.rewind(); |
| 83 | } |
| 84 | |
mtklein | 9200758 | 2014-08-01 07:46:52 -0700 | [diff] [blame] | 85 | bool SKPBench::isSuitableFor(Backend backend) { |
| 86 | return backend != kNonRendering_Backend; |
| 87 | } |
| 88 | |
| 89 | SkIPoint SKPBench::onGetSize() { |
| 90 | return SkIPoint::Make(fClip.width(), fClip.height()); |
| 91 | } |
| 92 | |
| 93 | void SKPBench::onDraw(const int loops, SkCanvas* canvas) { |
robertphillips | 5b69377 | 2014-11-21 06:19:36 -0800 | [diff] [blame^] | 94 | if (fUseMultiPictureDraw) { |
| 95 | for (int i = 0; i < loops; i++) { |
| 96 | SkMultiPictureDraw mpd; |
mtklein | c7f7f46 | 2014-10-21 12:29:25 -0700 | [diff] [blame] | 97 | |
robertphillips | 5b69377 | 2014-11-21 06:19:36 -0800 | [diff] [blame^] | 98 | for (int i = 0; i < fTileRects.count(); ++i) { |
| 99 | SkMatrix trans; |
| 100 | trans.setTranslate(-fTileRects[i].fLeft/fScale, |
| 101 | -fTileRects[i].fTop/fScale); |
| 102 | mpd.add(fSurfaces[i]->getCanvas(), fPic, &trans); |
| 103 | } |
mtklein | c7f7f46 | 2014-10-21 12:29:25 -0700 | [diff] [blame] | 104 | |
robertphillips | 5b69377 | 2014-11-21 06:19:36 -0800 | [diff] [blame^] | 105 | mpd.draw(); |
| 106 | |
| 107 | for (int i = 0; i < fTileRects.count(); ++i) { |
| 108 | fSurfaces[i]->getCanvas()->flush(); |
mtklein | c7f7f46 | 2014-10-21 12:29:25 -0700 | [diff] [blame] | 109 | } |
mtklein | 9200758 | 2014-08-01 07:46:52 -0700 | [diff] [blame] | 110 | } |
robertphillips | 5b69377 | 2014-11-21 06:19:36 -0800 | [diff] [blame^] | 111 | } else { |
| 112 | SkIRect bounds; |
| 113 | SkAssertResult(canvas->getClipDeviceBounds(&bounds)); |
| 114 | |
| 115 | SkAutoCanvasRestore overall(canvas, true/*save now*/); |
| 116 | canvas->scale(fScale, fScale); |
| 117 | |
| 118 | for (int i = 0; i < loops; i++) { |
| 119 | for (int y = bounds.fTop; y < bounds.fBottom; y += FLAGS_benchTile) { |
| 120 | for (int x = bounds.fLeft; x < bounds.fRight; x += FLAGS_benchTile) { |
| 121 | SkAutoCanvasRestore perTile(canvas, true/*save now*/); |
| 122 | canvas->clipRect(SkRect::Make( |
| 123 | SkIRect::MakeXYWH(x, y, FLAGS_benchTile, FLAGS_benchTile))); |
| 124 | fPic->playback(canvas); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | canvas->flush(); |
| 129 | } |
mtklein | c7f7f46 | 2014-10-21 12:29:25 -0700 | [diff] [blame] | 130 | } |
mtklein | 9200758 | 2014-08-01 07:46:52 -0700 | [diff] [blame] | 131 | } |