reed@google.com | 3d407a1 | 2012-10-12 14:42:38 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2011 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
| 8 | #include "SkBenchmark.h" |
| 9 | #include "SkBitmap.h" |
| 10 | #include "SkPaint.h" |
| 11 | #include "SkCanvas.h" |
| 12 | #include "SkColorPriv.h" |
| 13 | #include "SkRandom.h" |
| 14 | #include "SkString.h" |
| 15 | |
| 16 | static void drawIntoBitmap(const SkBitmap& bm) { |
| 17 | const int w = bm.width(); |
| 18 | const int h = bm.height(); |
| 19 | |
| 20 | SkCanvas canvas(bm); |
| 21 | SkPaint p; |
| 22 | p.setAntiAlias(true); |
| 23 | p.setColor(SK_ColorRED); |
| 24 | canvas.drawCircle(SkIntToScalar(w)/2, SkIntToScalar(h)/2, |
| 25 | SkIntToScalar(SkMin32(w, h))*3/8, p); |
| 26 | |
| 27 | SkRect r; |
| 28 | r.set(0, 0, SkIntToScalar(w), SkIntToScalar(h)); |
| 29 | p.setStyle(SkPaint::kStroke_Style); |
| 30 | p.setStrokeWidth(SkIntToScalar(4)); |
| 31 | p.setColor(SK_ColorBLUE); |
| 32 | canvas.drawRect(r, p); |
| 33 | } |
| 34 | |
| 35 | /* Variants for bitmaprect |
| 36 | src : entire bitmap, subset, fractional subset |
| 37 | dst : same size as src, diff size |
| 38 | paint : filter-p |
| 39 | */ |
| 40 | |
| 41 | class BitmapRectBench : public SkBenchmark { |
| 42 | SkBitmap fBitmap; |
| 43 | bool fDoFilter; |
reed@google.com | 9318231 | 2013-01-15 20:21:19 +0000 | [diff] [blame] | 44 | bool fSlightMatrix; |
reed@google.com | b8b92ea | 2012-10-16 15:57:13 +0000 | [diff] [blame] | 45 | uint8_t fAlpha; |
reed@google.com | 3d407a1 | 2012-10-12 14:42:38 +0000 | [diff] [blame] | 46 | SkString fName; |
| 47 | SkRect fSrcR, fDstR; |
| 48 | enum { N = SkBENCHLOOP(300) }; |
| 49 | public: |
reed@google.com | 9318231 | 2013-01-15 20:21:19 +0000 | [diff] [blame] | 50 | BitmapRectBench(void* param, U8CPU alpha, bool doFilter, bool slightMatrix) : INHERITED(param) { |
reed@google.com | b8b92ea | 2012-10-16 15:57:13 +0000 | [diff] [blame] | 51 | fAlpha = SkToU8(alpha); |
| 52 | fDoFilter = doFilter; |
reed@google.com | 9318231 | 2013-01-15 20:21:19 +0000 | [diff] [blame] | 53 | fSlightMatrix = slightMatrix; |
reed@google.com | b8b92ea | 2012-10-16 15:57:13 +0000 | [diff] [blame] | 54 | |
reed@google.com | 3d407a1 | 2012-10-12 14:42:38 +0000 | [diff] [blame] | 55 | const int w = 128; |
| 56 | const int h = 128; |
| 57 | |
| 58 | fBitmap.setConfig(SkBitmap::kARGB_8888_Config, w, h); |
| 59 | fBitmap.allocPixels(); |
| 60 | fBitmap.setIsOpaque(true); |
| 61 | fBitmap.eraseColor(SK_ColorBLACK); |
| 62 | drawIntoBitmap(fBitmap); |
skia.committer@gmail.com | f57c01b | 2012-10-13 02:01:56 +0000 | [diff] [blame] | 63 | |
robertphillips@google.com | 93f0332 | 2012-12-03 17:35:19 +0000 | [diff] [blame] | 64 | fSrcR.iset(0, 0, w, h); |
| 65 | fDstR.iset(0, 0, w, h); |
reed@google.com | 9318231 | 2013-01-15 20:21:19 +0000 | [diff] [blame] | 66 | |
| 67 | if (slightMatrix) { |
| 68 | // want fractional translate |
| 69 | fDstR.offset(SK_Scalar1 / 3, SK_Scalar1 * 5 / 7); |
| 70 | // want enough to create a scale matrix, but not enough to scare |
| 71 | // off our sniffer which tries to see if the matrix is "effectively" |
| 72 | // translate-only. |
| 73 | fDstR.fRight += SK_Scalar1 / (w * 60); |
| 74 | } |
reed@google.com | 3d407a1 | 2012-10-12 14:42:38 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | protected: |
| 78 | virtual const char* onGetName() { |
reed@google.com | 9318231 | 2013-01-15 20:21:19 +0000 | [diff] [blame] | 79 | fName.printf("bitmaprect_%02X_%sfilter_%s", |
| 80 | fAlpha, fDoFilter ? "" : "no", |
| 81 | fSlightMatrix ? "trans" : "identity"); |
reed@google.com | 3d407a1 | 2012-10-12 14:42:38 +0000 | [diff] [blame] | 82 | return fName.c_str(); |
| 83 | } |
| 84 | |
| 85 | virtual void onDraw(SkCanvas* canvas) { |
reed@google.com | 3d407a1 | 2012-10-12 14:42:38 +0000 | [diff] [blame] | 86 | SkRandom rand; |
| 87 | |
| 88 | SkPaint paint; |
| 89 | this->setupPaint(&paint); |
| 90 | paint.setFilterBitmap(fDoFilter); |
reed@google.com | b8b92ea | 2012-10-16 15:57:13 +0000 | [diff] [blame] | 91 | paint.setAlpha(fAlpha); |
reed@google.com | 3d407a1 | 2012-10-12 14:42:38 +0000 | [diff] [blame] | 92 | |
| 93 | for (int i = 0; i < N; i++) { |
| 94 | canvas->drawBitmapRectToRect(fBitmap, &fSrcR, fDstR, &paint); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | private: |
| 99 | typedef SkBenchmark INHERITED; |
| 100 | }; |
| 101 | |
reed@google.com | 9318231 | 2013-01-15 20:21:19 +0000 | [diff] [blame] | 102 | DEF_BENCH(return new BitmapRectBench(p, 0xFF, false, false)) |
| 103 | DEF_BENCH(return new BitmapRectBench(p, 0x80, false, false)) |
| 104 | DEF_BENCH(return new BitmapRectBench(p, 0xFF, true, false)) |
| 105 | DEF_BENCH(return new BitmapRectBench(p, 0x80, true, false)) |
| 106 | |
| 107 | DEF_BENCH(return new BitmapRectBench(p, 0xFF, false, true)) |
| 108 | DEF_BENCH(return new BitmapRectBench(p, 0xFF, true, true)) |