OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright 2012 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 "SkBenchmark.h" |
| 9 #include "SkCanvas.h" |
| 10 #include "SkPaint.h" |
| 11 #include "SkRandom.h" |
| 12 #include "SkString.h" |
| 13 |
| 14 |
| 15 // This bench simulates the calls Skia sees from various HTML5 canvas |
| 16 // game bench marks |
| 17 class GameBench : public SkBenchmark { |
| 18 public: |
| 19 enum Type { |
| 20 kScale_Type, |
| 21 kTranslate_Type, |
| 22 kRotate_Type |
| 23 }; |
| 24 |
| 25 GameBench(void* param, Type type, bool partialClear) |
| 26 : INHERITED(param) |
| 27 , fType(type) |
| 28 , fPartialClear(partialClear) |
| 29 , fName("game") |
| 30 , fNumSaved(0) |
| 31 , fInitialized(false) { |
| 32 |
| 33 switch (fType) { |
| 34 case kScale_Type: |
| 35 fName.append("_scale"); |
| 36 break; |
| 37 case kTranslate_Type: |
| 38 fName.append("_trans"); |
| 39 break; |
| 40 case kRotate_Type: |
| 41 fName.append("_rot"); |
| 42 break; |
| 43 }; |
| 44 |
| 45 if (partialClear) { |
| 46 fName.append("_partial"); |
| 47 } else { |
| 48 fName.append("_full"); |
| 49 } |
| 50 |
| 51 // It's HTML 5 canvas, so always AA |
| 52 fName.append("_aa"); |
| 53 } |
| 54 |
| 55 protected: |
| 56 virtual const char* onGetName() SK_OVERRIDE { |
| 57 return fName.c_str();; |
| 58 } |
| 59 |
| 60 virtual void onPreDraw() SK_OVERRIDE { |
| 61 if (!fInitialized) { |
| 62 this->makeCheckerboard(); |
| 63 fInitialized = true; |
| 64 } |
| 65 } |
| 66 |
| 67 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
| 68 static SkMWCRandom scaleRand; |
| 69 static SkMWCRandom transRand; |
| 70 static SkMWCRandom rotRand; |
| 71 |
| 72 SkPaint clearPaint; |
| 73 clearPaint.setColor(0xFF000000); |
| 74 clearPaint.setAntiAlias(true); |
| 75 |
| 76 SkISize size = canvas->getDeviceSize(); |
| 77 |
| 78 SkScalar maxTransX, maxTransY; |
| 79 |
| 80 if (kScale_Type == fType) { |
| 81 maxTransX = size.fWidth - (1.5f * kCheckerboardWidth); |
| 82 maxTransY = size.fHeight - (1.5f * kCheckerboardHeight); |
| 83 } else if (kTranslate_Type == fType) { |
| 84 maxTransX = SkIntToScalar(size.fWidth - kCheckerboardWidth); |
| 85 maxTransY = SkIntToScalar(size.fHeight - kCheckerboardHeight); |
| 86 } else { |
| 87 SkASSERT(kRotate_Type == fType); |
| 88 // Yes, some rotations will be off the top and left sides |
| 89 maxTransX = size.fWidth - SK_ScalarSqrt2 * kCheckerboardHeight; |
| 90 maxTransY = size.fHeight - SK_ScalarSqrt2 * kCheckerboardHeight; |
| 91 } |
| 92 |
| 93 SkMatrix mat; |
| 94 SkIRect src = { 0, 0, kCheckerboardWidth, kCheckerboardHeight }; |
| 95 SkRect dst = { 0, 0, kCheckerboardWidth, kCheckerboardHeight }; |
| 96 SkRect clearRect = { -1.0f, -1.0f, |
| 97 kCheckerboardWidth+1.0f, kCheckerboardHeight+1.0f }
; |
| 98 |
| 99 SkPaint p; |
| 100 p.setColor(0xFF000000); |
| 101 p.setFilterBitmap(true); |
| 102 |
| 103 for (int i = 0; i < kNumRects; ++i, ++fNumSaved) { |
| 104 |
| 105 if (0 == i % kNumBeforeClear) { |
| 106 if (fPartialClear) { |
| 107 for (int j = 0; j < fNumSaved; ++j) { |
| 108 canvas->setMatrix(SkMatrix::I()); |
| 109 mat.setTranslate(fSaved[j][0], fSaved[j][1]); |
| 110 |
| 111 if (kScale_Type == fType) { |
| 112 mat.preScale(fSaved[j][2], fSaved[j][2]); |
| 113 } else if (kRotate_Type == fType) { |
| 114 mat.preRotate(fSaved[j][2]); |
| 115 } |
| 116 |
| 117 canvas->concat(mat); |
| 118 canvas->drawRect(clearRect, clearPaint); |
| 119 } |
| 120 } else { |
| 121 canvas->clear(0xFF000000); |
| 122 } |
| 123 |
| 124 fNumSaved = 0; |
| 125 } |
| 126 |
| 127 SkASSERT(fNumSaved < kNumBeforeClear); |
| 128 |
| 129 canvas->setMatrix(SkMatrix::I()); |
| 130 |
| 131 fSaved[fNumSaved][0] = transRand.nextRangeScalar(0.0f, maxTransX); |
| 132 fSaved[fNumSaved][1] = transRand.nextRangeScalar(0.0f, maxTransY); |
| 133 |
| 134 mat.setTranslate(fSaved[fNumSaved][0], fSaved[fNumSaved][1]); |
| 135 |
| 136 if (kScale_Type == fType) { |
| 137 fSaved[fNumSaved][2] = scaleRand.nextRangeScalar(0.5f, 1.5f); |
| 138 mat.preScale(fSaved[fNumSaved][2], fSaved[fNumSaved][2]); |
| 139 } else if (kRotate_Type == fType) { |
| 140 fSaved[fNumSaved][2] = rotRand.nextRangeScalar(0.0f, 360.0f); |
| 141 mat.preRotate(fSaved[fNumSaved][2]); |
| 142 } |
| 143 |
| 144 canvas->concat(mat); |
| 145 canvas->drawBitmapRect(fCheckerboard, &src, dst, &p); |
| 146 } |
| 147 } |
| 148 |
| 149 private: |
| 150 static const int kCheckerboardWidth = 64; |
| 151 static const int kCheckerboardHeight = 128; |
| 152 #ifdef SK_DEBUG |
| 153 static const int kNumRects = 100; |
| 154 static const int kNumBeforeClear = 10; |
| 155 #else |
| 156 static const int kNumRects = 5000; |
| 157 static const int kNumBeforeClear = 300; |
| 158 #endif |
| 159 |
| 160 |
| 161 Type fType; |
| 162 bool fPartialClear; |
| 163 SkString fName; |
| 164 int fNumSaved; // num draws stored in 'fSaved' |
| 165 bool fInitialized; |
| 166 |
| 167 // 0 & 1 are always x & y translate. 2 is either scale or rotate. |
| 168 SkScalar fSaved[kNumBeforeClear][3]; |
| 169 SkBitmap fCheckerboard; |
| 170 |
| 171 // Note: the resulting checker board has transparency |
| 172 void makeCheckerboard() { |
| 173 static unsigned int kCheckSize = 16; |
| 174 |
| 175 fCheckerboard.setConfig(SkBitmap::kARGB_8888_Config, |
| 176 kCheckerboardWidth, kCheckerboardHeight); |
| 177 fCheckerboard.allocPixels(); |
| 178 SkAutoLockPixels lock(fCheckerboard); |
| 179 for (unsigned int y = 0; y < kCheckerboardHeight; ++y) { |
| 180 int even = (y / kCheckSize) % 2; |
| 181 |
| 182 SkPMColor* scanline = fCheckerboard.getAddr32(0, y); |
| 183 |
| 184 for (unsigned int x = 0; x < kCheckerboardWidth; ++x) { |
| 185 if (even == (x / kCheckSize) % 2) { |
| 186 *scanline++ = 0xFFFF0000; |
| 187 } else { |
| 188 *scanline++ = 0x00000000; |
| 189 } |
| 190 } |
| 191 } |
| 192 } |
| 193 |
| 194 typedef SkBenchmark INHERITED; |
| 195 }; |
| 196 |
| 197 // Partial clear |
| 198 DEF_BENCH( return SkNEW_ARGS(GameBench, (p, GameBench::kScale_Type, false)); ) |
| 199 DEF_BENCH( return SkNEW_ARGS(GameBench, (p, GameBench::kTranslate_Type, false));
) |
| 200 DEF_BENCH( return SkNEW_ARGS(GameBench, (p, GameBench::kRotate_Type, false)); ) |
| 201 |
| 202 // Full clear |
| 203 DEF_BENCH( return SkNEW_ARGS(GameBench, (p, GameBench::kScale_Type, true)); ) |
| 204 DEF_BENCH( return SkNEW_ARGS(GameBench, (p, GameBench::kTranslate_Type, true));
) |
| 205 DEF_BENCH( return SkNEW_ARGS(GameBench, (p, GameBench::kRotate_Type, true)); ) |
| 206 |
OLD | NEW |