sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +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 | */ |
reed | aa4c7a7 | 2015-04-02 20:31:17 -0700 | [diff] [blame] | 7 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "bench/Benchmark.h" |
| 9 | #include "include/core/SkCanvas.h" |
| 10 | #include "include/effects/SkColorFilterImageFilter.h" |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 11 | |
Mike Reed | d93ee53 | 2019-05-20 12:30:37 -0400 | [diff] [blame] | 12 | // Just need an interesting filter, nothing to special about colormatrix |
| 13 | static sk_sp<SkColorFilter> make_grayscale() { |
Mike Reed | e869a1e | 2019-04-30 12:18:54 -0400 | [diff] [blame] | 14 | float matrix[20]; |
| 15 | memset(matrix, 0, 20 * sizeof(float)); |
robertphillips | 5605b56 | 2016-04-05 11:50:42 -0700 | [diff] [blame] | 16 | matrix[0] = matrix[5] = matrix[10] = 0.2126f; |
| 17 | matrix[1] = matrix[6] = matrix[11] = 0.7152f; |
| 18 | matrix[2] = matrix[7] = matrix[12] = 0.0722f; |
| 19 | matrix[18] = 1.0f; |
Mike Reed | d93ee53 | 2019-05-20 12:30:37 -0400 | [diff] [blame] | 20 | return SkColorFilters::Matrix(matrix); |
robertphillips | 5605b56 | 2016-04-05 11:50:42 -0700 | [diff] [blame] | 21 | } |
| 22 | |
Mike Reed | d93ee53 | 2019-05-20 12:30:37 -0400 | [diff] [blame] | 23 | /** |
| 24 | * Different ways to draw the same thing (a red rect) |
| 25 | * All of their timings should be about the same |
| 26 | * (we allow for slight overhead to figure out that we can undo the presence of the filters) |
| 27 | */ |
| 28 | class FilteredRectBench : public Benchmark { |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 29 | public: |
Mike Reed | d93ee53 | 2019-05-20 12:30:37 -0400 | [diff] [blame] | 30 | enum Type { |
| 31 | kNoFilter_Type, |
| 32 | kColorFilter_Type, |
| 33 | kImageFilter_Type, |
| 34 | }; |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 35 | |
Mike Reed | d93ee53 | 2019-05-20 12:30:37 -0400 | [diff] [blame] | 36 | FilteredRectBench(Type t) : fType(t) { |
| 37 | static const char* suffix[] = { "nofilter", "colorfilter", "imagefilter" }; |
| 38 | fName.printf("filteredrect_%s", suffix[t]); |
| 39 | fPaint.setColor(SK_ColorRED); |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 40 | } |
| 41 | |
Mike Reed | d93ee53 | 2019-05-20 12:30:37 -0400 | [diff] [blame] | 42 | protected: |
| 43 | const char* onGetName() override { |
| 44 | return fName.c_str(); |
| 45 | } |
| 46 | |
| 47 | void onDelayedSetup() override { |
| 48 | switch (fType) { |
| 49 | case kNoFilter_Type: |
| 50 | break; |
| 51 | case kColorFilter_Type: |
| 52 | fPaint.setColorFilter(make_grayscale()); |
| 53 | break; |
| 54 | case kImageFilter_Type: |
| 55 | fPaint.setImageFilter(SkColorFilterImageFilter::Make(make_grayscale(), nullptr)); |
| 56 | break; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | void onDraw(int loops, SkCanvas* canvas) override { |
| 61 | const SkRect r = { 0, 0, 256, 256 }; |
| 62 | for (int i = 0; i < loops; ++i) { |
| 63 | canvas->drawRect(r, fPaint); |
| 64 | } |
| 65 | } |
reed | aa4c7a7 | 2015-04-02 20:31:17 -0700 | [diff] [blame] | 66 | |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 67 | private: |
Mike Reed | d93ee53 | 2019-05-20 12:30:37 -0400 | [diff] [blame] | 68 | SkPaint fPaint; |
| 69 | SkString fName; |
| 70 | Type fType; |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 71 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 72 | typedef Benchmark INHERITED; |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 73 | }; |
| 74 | |
Mike Reed | d93ee53 | 2019-05-20 12:30:37 -0400 | [diff] [blame] | 75 | DEF_BENCH( return new FilteredRectBench(FilteredRectBench::kNoFilter_Type); ) |
| 76 | DEF_BENCH( return new FilteredRectBench(FilteredRectBench::kColorFilter_Type); ) |
| 77 | DEF_BENCH( return new FilteredRectBench(FilteredRectBench::kImageFilter_Type); ) |