1 /* 2 * Copyright 2011 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 #include <memory> 8 9 #include "bench/Benchmark.h" 10 #include "include/core/SkCanvas.h" 11 #include "include/core/SkColor.h" 12 #include "include/core/SkPaint.h" 13 #include "include/core/SkPicture.h" 14 #include "include/core/SkPictureRecorder.h" 15 #include "include/core/SkPoint.h" 16 #include "include/core/SkRect.h" 17 #include "include/core/SkString.h" 18 #include "include/utils/SkRandom.h" 19 20 // This is designed to emulate about 4 screens of textual content 21 22 /////////////////////////////////////////////////////////////////////////////// 23 24 // Chrome draws into small tiles with impl-side painting. 25 // This benchmark measures the relative performance of our bounding-box hierarchies, 26 // both when querying tiles perfectly and when not. 27 enum BBH { kNone, kRTree }; 28 enum Mode { kTiled, kRandom }; 29 class TiledPlaybackBench : public Benchmark { 30 public: TiledPlaybackBench(BBH bbh,Mode mode)31 TiledPlaybackBench(BBH bbh, Mode mode) : fBBH(bbh), fMode(mode), fName("tiled_playback") { 32 switch (fBBH) { 33 case kNone: fName.append("_none" ); break; 34 case kRTree: fName.append("_rtree" ); break; 35 } 36 switch (fMode) { 37 case kTiled: fName.append("_tiled" ); break; 38 case kRandom: fName.append("_random"); break; 39 } 40 } 41 onGetName()42 const char* onGetName() override { return fName.c_str(); } onGetSize()43 SkIPoint onGetSize() override { return SkIPoint::Make(1024,1024); } 44 onDelayedSetup()45 void onDelayedSetup() override { 46 std::unique_ptr<SkBBHFactory> factory; 47 switch (fBBH) { 48 case kNone: break; 49 case kRTree: factory = std::make_unique<SkRTreeFactory>(); break; 50 } 51 52 SkPictureRecorder recorder; 53 SkCanvas* canvas = recorder.beginRecording(1024, 1024, factory.get()); 54 SkRandom rand; 55 for (int i = 0; i < 10000; i++) { 56 SkScalar x = rand.nextRangeScalar(0, 1024), 57 y = rand.nextRangeScalar(0, 1024), 58 w = rand.nextRangeScalar(0, 128), 59 h = rand.nextRangeScalar(0, 128); 60 SkPaint paint; 61 paint.setColor(rand.nextU()); 62 paint.setAlpha(0xFF); 63 canvas->drawRect(SkRect::MakeXYWH(x,y,w,h), paint); 64 } 65 fPic = recorder.finishRecordingAsPicture(); 66 } 67 onDraw(int loops,SkCanvas * canvas)68 void onDraw(int loops, SkCanvas* canvas) override { 69 for (int i = 0; i < loops; i++) { 70 // This inner loop guarantees we make the same choices for all bench variants. 71 SkRandom rand; 72 for (int j = 0; j < 10; j++) { 73 SkScalar x = 0, y = 0; 74 switch (fMode) { 75 case kTiled: x = SkScalar(256 * rand.nextULessThan(4)); 76 y = SkScalar(256 * rand.nextULessThan(4)); 77 break; 78 case kRandom: x = rand.nextRangeScalar(0, 768); 79 y = rand.nextRangeScalar(0, 768); 80 break; 81 } 82 SkAutoCanvasRestore ar(canvas, true/*save now*/); 83 canvas->clipRect(SkRect::MakeXYWH(x,y,256,256)); 84 fPic->playback(canvas); 85 } 86 } 87 } 88 89 private: 90 BBH fBBH; 91 Mode fMode; 92 SkString fName; 93 sk_sp<SkPicture> fPic; 94 }; 95 96 DEF_BENCH( return new TiledPlaybackBench(kNone, kRandom); ) 97 DEF_BENCH( return new TiledPlaybackBench(kNone, kTiled ); ) 98 DEF_BENCH( return new TiledPlaybackBench(kRTree, kRandom); ) 99 DEF_BENCH( return new TiledPlaybackBench(kRTree, kTiled ); ) 100