djsollen@google.com | dde718c | 2012-05-30 16:50:11 +0000 | [diff] [blame^] | 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 | #include "SkBenchmark.h" |
| 8 | #include "SkCanvas.h" |
| 9 | #include "SkColor.h" |
| 10 | #include "SkPaint.h" |
| 11 | #include "SkPicture.h" |
| 12 | #include "SkPoint.h" |
| 13 | #include "SkRect.h" |
| 14 | #include "SkString.h" |
| 15 | |
| 16 | class PictureRecordBench : public SkBenchmark { |
| 17 | public: |
| 18 | PictureRecordBench(void* param, const char name[]) : INHERITED(param) { |
| 19 | fName.printf("picture_record_%s", name); |
| 20 | fPictureWidth = SkIntToScalar(PICTURE_WIDTH); |
| 21 | fPictureHeight = SkIntToScalar(PICTURE_HEIGHT); |
| 22 | } |
| 23 | |
| 24 | enum { |
| 25 | N = SkBENCHLOOP(25), // number of times to create the picture |
| 26 | PICTURE_WIDTH = 1000, |
| 27 | PICTURE_HEIGHT = 4000, |
| 28 | }; |
| 29 | protected: |
| 30 | virtual const char* onGetName() { |
| 31 | return fName.c_str(); |
| 32 | } |
| 33 | |
| 34 | virtual void onDraw(SkCanvas* canvas) { |
| 35 | |
| 36 | for (int i = 0; i < N; i++) { |
| 37 | |
| 38 | SkPicture picture; |
| 39 | |
| 40 | SkCanvas* pCanvas = picture.beginRecording(PICTURE_WIDTH, PICTURE_HEIGHT); |
| 41 | recordCanvas(pCanvas); |
| 42 | |
| 43 | // we don't need to draw the picture as the endRecording step will |
| 44 | // do the work of transferring the recorded content into a playback |
| 45 | // object. |
| 46 | picture.endRecording(); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | virtual void recordCanvas(SkCanvas* canvas) = 0; |
| 51 | |
| 52 | SkString fName; |
| 53 | SkScalar fPictureWidth; |
| 54 | SkScalar fPictureHeight; |
| 55 | SkScalar fTextSize; |
| 56 | private: |
| 57 | typedef SkBenchmark INHERITED; |
| 58 | }; |
| 59 | |
| 60 | /* |
| 61 | * An SkPicture has internal dictionaries to store bitmaps, matrices, paints, |
| 62 | * and regions. This bench populates those dictionaries to test the speed of |
| 63 | * reading and writing to those particular dictionary data structures. |
| 64 | */ |
| 65 | class DictionaryRecordBench : public PictureRecordBench { |
| 66 | public: |
| 67 | DictionaryRecordBench(void* param) |
| 68 | : INHERITED(param, "dictionaries") { } |
| 69 | |
| 70 | enum { |
| 71 | M = SkBENCHLOOP(100), // number of elements in each dictionary |
| 72 | }; |
| 73 | protected: |
| 74 | virtual void recordCanvas(SkCanvas* canvas) { |
| 75 | |
| 76 | const SkPoint translateDelta = getTranslateDelta(); |
| 77 | |
| 78 | for (int i = 0; i < M; i++) { |
| 79 | |
| 80 | SkColor color = SK_ColorYELLOW + (i % 255); |
| 81 | SkIRect rect = SkIRect::MakeWH(i,i); |
| 82 | |
| 83 | canvas->save(); |
| 84 | |
| 85 | // set the clip to the given region |
| 86 | SkRegion region; |
| 87 | region.setRect(rect); |
| 88 | canvas->clipRegion(region); |
| 89 | |
| 90 | // fill the clip with a color |
| 91 | SkPaint paint; |
| 92 | paint.setColor(color); |
| 93 | canvas->drawPaint(paint); |
| 94 | |
| 95 | // set a matrix on the canvas |
| 96 | SkMatrix matrix; |
| 97 | matrix.setRotate(SkIntToScalar(i % 360)); |
| 98 | canvas->setMatrix(matrix); |
| 99 | |
| 100 | // create a simple bitmap |
| 101 | SkBitmap bitmap; |
| 102 | bitmap.setConfig(SkBitmap::kRGB_565_Config, 10, 10); |
| 103 | bitmap.allocPixels(); |
| 104 | |
| 105 | // draw a single color into the bitmap |
| 106 | SkCanvas bitmapCanvas(bitmap); |
| 107 | bitmapCanvas.drawColor(SkColorSetA(color, i % 255)); |
| 108 | |
| 109 | // draw the bitmap onto the canvas |
| 110 | canvas->drawBitmapMatrix(bitmap, matrix); |
| 111 | |
| 112 | canvas->restore(); |
| 113 | canvas->translate(translateDelta.fX, translateDelta.fY); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | SkPoint getTranslateDelta() { |
| 118 | SkIPoint canvasSize = onGetSize(); |
| 119 | return SkPoint::Make(SkIntToScalar((PICTURE_WIDTH - canvasSize.fX)/M), |
| 120 | SkIntToScalar((PICTURE_HEIGHT- canvasSize.fY)/M)); |
| 121 | } |
| 122 | private: |
| 123 | typedef PictureRecordBench INHERITED; |
| 124 | }; |
| 125 | |
| 126 | /////////////////////////////////////////////////////////////////////////////// |
| 127 | |
| 128 | static SkBenchmark* Fact0(void* p) { return new DictionaryRecordBench(p); } |
| 129 | |
| 130 | static BenchRegistry gReg0(Fact0); |