OLD | NEW |
(Empty) | |
| 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 "Benchmark.h" |
| 9 #include "SkCanvas.h" |
| 10 #include "SkColor.h" |
| 11 #include "SkNullCanvas.h" |
| 12 #include "SkPaint.h" |
| 13 #include "SkPicture.h" |
| 14 #include "SkPictureRecorder.h" |
| 15 #include "SkString.h" |
| 16 |
| 17 class PictureNesting : public Benchmark { |
| 18 public: |
| 19 PictureNesting(const char* name, int maxLevel, int maxPictureLevel) |
| 20 : fMaxLevel(maxLevel) |
| 21 , fMaxPictureLevel(maxPictureLevel) { |
| 22 |
| 23 fPaint.setColor(SK_ColorRED); |
| 24 fPaint.setAntiAlias(true); |
| 25 fPaint.setStyle(SkPaint::kStroke_Style); |
| 26 fName.printf("picture_nesting_%s_%d", name, |
| 27 this->sierpinsky(SkCreateNullCanvas(), 0, fPaint)); |
| 28 } |
| 29 |
| 30 protected: |
| 31 virtual const char* onGetName() SK_OVERRIDE { |
| 32 return fName.c_str(); |
| 33 } |
| 34 |
| 35 void doDraw(SkCanvas* canvas) { |
| 36 SkIPoint canvasSize = onGetSize(); |
| 37 canvas->save(); |
| 38 canvas->scale(SkIntToScalar(canvasSize.x()), SkIntToScalar(canvasSize.y(
))); |
| 39 |
| 40 this->sierpinsky(canvas, 0, fPaint); |
| 41 |
| 42 canvas->restore(); |
| 43 } |
| 44 |
| 45 int sierpinsky(SkCanvas* canvas, int lvl, const SkPaint& paint) { |
| 46 if (++lvl > fMaxLevel) { |
| 47 return 0; |
| 48 } |
| 49 |
| 50 int pics = 0; |
| 51 bool recordPicture = lvl <= fMaxPictureLevel; |
| 52 SkPictureRecorder recorder; |
| 53 SkCanvas* c = canvas; |
| 54 |
| 55 if (recordPicture) { |
| 56 c = recorder.beginRecording(1, 1); |
| 57 pics++; |
| 58 } |
| 59 |
| 60 c->drawLine(0.5, 0, 0, 1, paint); |
| 61 c->drawLine(0.5, 0, 1, 1, paint); |
| 62 c->drawLine(0, 1, 1, 1, paint); |
| 63 |
| 64 c->save(); |
| 65 c->scale(0.5, 0.5); |
| 66 |
| 67 c->translate(0, 1); |
| 68 pics += this->sierpinsky(c, lvl, paint); |
| 69 |
| 70 c->translate(1, 0); |
| 71 pics += this->sierpinsky(c, lvl, paint); |
| 72 |
| 73 c->translate(-0.5, -1); |
| 74 pics += this->sierpinsky(c, lvl, paint); |
| 75 c->restore(); |
| 76 |
| 77 if (recordPicture) { |
| 78 SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
| 79 canvas->drawPicture(picture); |
| 80 } |
| 81 |
| 82 return pics; |
| 83 } |
| 84 |
| 85 int fMaxLevel; |
| 86 int fMaxPictureLevel; |
| 87 |
| 88 private: |
| 89 SkString fName; |
| 90 SkPaint fPaint; |
| 91 |
| 92 typedef Benchmark INHERITED; |
| 93 }; |
| 94 |
| 95 class PictureNestingRecording : public PictureNesting { |
| 96 public: |
| 97 PictureNestingRecording(int maxLevel, int maxPictureLevel) |
| 98 : INHERITED("recording", maxLevel, maxPictureLevel) { |
| 99 } |
| 100 |
| 101 protected: |
| 102 virtual void onDraw(const int loops, SkCanvas* canvas) { |
| 103 SkIPoint canvasSize = onGetSize(); |
| 104 SkPictureRecorder recorder; |
| 105 |
| 106 for (int i = 0; i < loops; i++) { |
| 107 SkCanvas* c = recorder.beginRecording(SkIntToScalar(canvasSize.x()), |
| 108 SkIntToScalar(canvasSize.y()))
; |
| 109 this->doDraw(c); |
| 110 SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
| 111 } |
| 112 } |
| 113 |
| 114 private: |
| 115 typedef PictureNesting INHERITED; |
| 116 }; |
| 117 |
| 118 class PictureNestingPlayback : public PictureNesting { |
| 119 public: |
| 120 PictureNestingPlayback(int maxLevel, int maxPictureLevel) |
| 121 : INHERITED("playback", maxLevel, maxPictureLevel) { |
| 122 |
| 123 SkIPoint canvasSize = onGetSize(); |
| 124 SkPictureRecorder recorder; |
| 125 SkCanvas* c = recorder.beginRecording(SkIntToScalar(canvasSize.x()), |
| 126 SkIntToScalar(canvasSize.y())); |
| 127 |
| 128 this->doDraw(c); |
| 129 fPicture.reset(recorder.endRecording()); |
| 130 } |
| 131 |
| 132 protected: |
| 133 virtual void onDraw(const int loops, SkCanvas* canvas) { |
| 134 for (int i = 0; i < loops; i++) { |
| 135 canvas->drawPicture(fPicture); |
| 136 } |
| 137 } |
| 138 |
| 139 private: |
| 140 SkAutoTUnref<SkPicture> fPicture; |
| 141 |
| 142 typedef PictureNesting INHERITED; |
| 143 }; |
| 144 |
| 145 DEF_BENCH( return new PictureNestingRecording(8, 0); ) |
| 146 DEF_BENCH( return new PictureNestingRecording(8, 1); ) |
| 147 DEF_BENCH( return new PictureNestingRecording(8, 2); ) |
| 148 DEF_BENCH( return new PictureNestingRecording(8, 3); ) |
| 149 DEF_BENCH( return new PictureNestingRecording(8, 4); ) |
| 150 DEF_BENCH( return new PictureNestingRecording(8, 5); ) |
| 151 DEF_BENCH( return new PictureNestingRecording(8, 6); ) |
| 152 DEF_BENCH( return new PictureNestingRecording(8, 7); ) |
| 153 DEF_BENCH( return new PictureNestingRecording(8, 8); ) |
| 154 |
| 155 DEF_BENCH( return new PictureNestingPlayback(8, 0); ) |
| 156 DEF_BENCH( return new PictureNestingPlayback(8, 1); ) |
| 157 DEF_BENCH( return new PictureNestingPlayback(8, 2); ) |
| 158 DEF_BENCH( return new PictureNestingPlayback(8, 3); ) |
| 159 DEF_BENCH( return new PictureNestingPlayback(8, 4); ) |
| 160 DEF_BENCH( return new PictureNestingPlayback(8, 5); ) |
| 161 DEF_BENCH( return new PictureNestingPlayback(8, 6); ) |
| 162 DEF_BENCH( return new PictureNestingPlayback(8, 7); ) |
| 163 DEF_BENCH( return new PictureNestingPlayback(8, 8); ) |
OLD | NEW |