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