scroggo@google.com | 7b05659 | 2013-11-05 15:57:21 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2013 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 "SkBlurMask.h" |
| 10 | #include "SkBlurMaskFilter.h" |
| 11 | #include "SkCanvas.h" |
| 12 | #include "SkColorFilter.h" |
| 13 | #include "SkLayerDrawLooper.h" |
| 14 | #include "SkPaint.h" |
| 15 | #include "SkPath.h" |
| 16 | #include "SkPoint.h" |
| 17 | #include "SkRect.h" |
| 18 | #include "SkRRect.h" |
| 19 | #include "SkString.h" |
| 20 | #include "SkXfermode.h" |
| 21 | |
| 22 | class BlurRoundRectBench : public SkBenchmark { |
| 23 | public: |
| 24 | BlurRoundRectBench(int width, int height, |
| 25 | // X and Y radii for the upper left corner |
| 26 | int ulX, int ulY, |
| 27 | // X and Y radii for the upper right corner |
| 28 | int urX, int urY, |
| 29 | // X and Y radii for the lower right corner |
| 30 | int lrX, int lrY, |
| 31 | // X and Y radii for the lower left corner |
| 32 | int llX, int llY) |
| 33 | : fName("blurroundrect") |
| 34 | , fWidth(width) |
| 35 | , fHeight(height) { |
| 36 | fName.appendf("_WH[%ix%i]_UL[%ix%i]_UR[%ix%i]_LR[%ix%i]_LL[%ix%i]", |
| 37 | width, height, |
| 38 | ulX, ulY, |
| 39 | urX, urY, |
| 40 | lrX, lrY, |
| 41 | llX, llY); |
| 42 | fRadii[0].set(SkIntToScalar(ulX), SkIntToScalar(ulY)); |
| 43 | fRadii[1].set(SkIntToScalar(urX), SkIntToScalar(urY)); |
| 44 | fRadii[2].set(SkIntToScalar(lrX), SkIntToScalar(lrY)); |
| 45 | fRadii[3].set(SkIntToScalar(llX), SkIntToScalar(llY)); |
| 46 | } |
| 47 | |
| 48 | virtual const char* onGetName() SK_OVERRIDE { |
| 49 | return fName.c_str(); |
| 50 | } |
| 51 | |
| 52 | virtual SkIPoint onGetSize() SK_OVERRIDE { |
| 53 | return SkIPoint::Make(fWidth, fHeight); |
| 54 | } |
| 55 | |
| 56 | virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
| 57 | for (int i = 0; i < this->getLoops(); i++) { |
| 58 | SkLayerDrawLooper* looper = new SkLayerDrawLooper; |
| 59 | { |
| 60 | SkLayerDrawLooper::LayerInfo info; |
| 61 | info.fFlagsMask = 0; |
| 62 | info.fPaintBits = 40; |
| 63 | info.fColorMode = SkXfermode::kSrc_Mode; |
| 64 | info.fOffset = SkPoint::Make(SkIntToScalar(-1), SkIntToScalar(0)); |
| 65 | info.fPostTranslate = false; |
| 66 | SkPaint* paint = looper->addLayerOnTop(info); |
| 67 | SkMaskFilter* maskFilter = SkBlurMaskFilter::Create(SK_ScalarHalf, |
| 68 | SkBlurMaskFilter::kNormal_BlurStyle, |
| 69 | SkBlurMaskFilter::kHighQuality_BlurFlag); |
| 70 | paint->setMaskFilter(maskFilter)->unref(); |
| 71 | SkColorFilter* colorFilter = SkColorFilter::CreateModeFilter(4279308561, |
| 72 | SkXfermode::kSrcIn_Mode); |
| 73 | paint->setColorFilter(colorFilter)->unref(); |
| 74 | paint->setColor(4278190080); |
| 75 | } |
| 76 | { |
| 77 | SkLayerDrawLooper::LayerInfo info; |
| 78 | looper->addLayerOnTop(info); |
| 79 | } |
| 80 | SkPaint paint; |
| 81 | SkRect rect = SkRect::MakeWH(fWidth, fHeight); |
| 82 | canvas->drawRect(rect, paint); |
| 83 | |
| 84 | paint.setLooper(looper)->unref(); |
| 85 | paint.setColor(4293848814); |
| 86 | paint.setAntiAlias(true); |
| 87 | |
| 88 | SkRRect rrect; |
| 89 | rrect.setRectRadii(rect, fRadii); |
| 90 | canvas->drawRRect(rrect, paint); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | private: |
| 95 | SkString fName; |
| 96 | const int fWidth; |
| 97 | const int fHeight; |
| 98 | SkVector fRadii[4]; |
| 99 | typedef SkBenchmark INHERITED; |
| 100 | }; |
| 101 | |
| 102 | // Create one with dimensions/rounded corners based on the skp |
| 103 | DEF_BENCH(return new BlurRoundRectBench(600, 5514, 6, 6, 6, 6, 6, 6, 6, 6);) |
| 104 | // Same radii, much smaller rectangle |
| 105 | DEF_BENCH(return new BlurRoundRectBench(100, 100, 6, 6, 6, 6, 6, 6, 6, 6);) |
| 106 | // Rounded rect with two opposite corners with large radii, the other two |
| 107 | // small. |
| 108 | DEF_BENCH(return new BlurRoundRectBench(100, 100, 30, 30, 10, 10, 30, 30, 10, 10);) |
| 109 | DEF_BENCH(return new BlurRoundRectBench(100, 100, 90, 90, 90, 90, 90, 90, 90, 90);) |