cwallez | c12b74d | 2015-01-26 07:45:53 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "bench/Benchmark.h" |
| 9 | #include "include/core/SkBitmap.h" |
| 10 | #include "include/core/SkCanvas.h" |
| 11 | #include "include/core/SkImageFilter.h" |
Mike Reed | 069e484 | 2021-01-24 11:39:58 -0500 | [diff] [blame] | 12 | #include "include/core/SkSurface.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #include "include/effects/SkColorMatrixFilter.h" |
| 14 | #include "include/effects/SkGradientShader.h" |
Michael Ludwig | 2300318 | 2019-08-05 11:25:23 -0400 | [diff] [blame] | 15 | #include "include/effects/SkImageFilters.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 16 | #include "include/effects/SkTableColorFilter.h" |
cwallez | c12b74d | 2015-01-26 07:45:53 -0800 | [diff] [blame] | 17 | |
| 18 | // Chains several matrix color filters image filter or several |
| 19 | // table filter image filters and draws a bitmap. |
| 20 | // This bench shows an improvement in performance and memory |
| 21 | // when collapsing matrices or tables is implemented since all |
| 22 | // the passes are collapsed in one. |
| 23 | |
| 24 | class BaseImageFilterCollapseBench : public Benchmark { |
| 25 | public: |
robertphillips | 5605b56 | 2016-04-05 11:50:42 -0700 | [diff] [blame] | 26 | BaseImageFilterCollapseBench() {} |
cwallez | c12b74d | 2015-01-26 07:45:53 -0800 | [diff] [blame] | 27 | |
| 28 | protected: |
reed | d053ce9 | 2016-03-22 10:17:23 -0700 | [diff] [blame] | 29 | void doPreDraw(sk_sp<SkColorFilter> colorFilters[], int nFilters) { |
robertphillips | 5605b56 | 2016-04-05 11:50:42 -0700 | [diff] [blame] | 30 | SkASSERT(!fImageFilter); |
| 31 | |
cwallez | c12b74d | 2015-01-26 07:45:53 -0800 | [diff] [blame] | 32 | // Create a chain of ImageFilters from colorFilters |
cwallez | c12b74d | 2015-01-26 07:45:53 -0800 | [diff] [blame] | 33 | for(int i = nFilters; i --> 0;) { |
Michael Ludwig | 2300318 | 2019-08-05 11:25:23 -0400 | [diff] [blame] | 34 | fImageFilter = SkImageFilters::ColorFilter(colorFilters[i], fImageFilter); |
cwallez | c12b74d | 2015-01-26 07:45:53 -0800 | [diff] [blame] | 35 | } |
| 36 | } |
| 37 | |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 38 | void onDraw(int loops, SkCanvas* canvas) override { |
cwallez | c12b74d | 2015-01-26 07:45:53 -0800 | [diff] [blame] | 39 | makeBitmap(); |
| 40 | |
| 41 | for(int i = 0; i < loops; i++) { |
| 42 | SkPaint paint; |
| 43 | paint.setImageFilter(fImageFilter); |
Mike Reed | 069e484 | 2021-01-24 11:39:58 -0500 | [diff] [blame] | 44 | canvas->drawImage(fImage, 0, 0, SkSamplingOptions(), &paint); |
cwallez | c12b74d | 2015-01-26 07:45:53 -0800 | [diff] [blame] | 45 | } |
| 46 | } |
| 47 | |
| 48 | private: |
robertphillips | 5605b56 | 2016-04-05 11:50:42 -0700 | [diff] [blame] | 49 | sk_sp<SkImageFilter> fImageFilter; |
Mike Reed | 069e484 | 2021-01-24 11:39:58 -0500 | [diff] [blame] | 50 | sk_sp<SkImage> fImage; |
cwallez | c12b74d | 2015-01-26 07:45:53 -0800 | [diff] [blame] | 51 | |
| 52 | void makeBitmap() { |
| 53 | int W = 400; |
| 54 | int H = 400; |
Mike Reed | 069e484 | 2021-01-24 11:39:58 -0500 | [diff] [blame] | 55 | auto surf = SkSurface::MakeRasterN32Premul(W, H); |
cwallez | c12b74d | 2015-01-26 07:45:53 -0800 | [diff] [blame] | 56 | |
cwallez | c12b74d | 2015-01-26 07:45:53 -0800 | [diff] [blame] | 57 | SkPaint paint; |
| 58 | SkPoint pts[] = { {0, 0}, {SkIntToScalar(W), SkIntToScalar(H)} }; |
| 59 | SkColor colors[] = { |
| 60 | SK_ColorBLACK, SK_ColorGREEN, SK_ColorCYAN, |
| 61 | SK_ColorRED, 0, SK_ColorBLUE, SK_ColorWHITE |
| 62 | }; |
reed | c6f28f7 | 2016-03-14 12:22:10 -0700 | [diff] [blame] | 63 | paint.setShader(SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors), |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 64 | SkTileMode::kClamp)); |
Mike Reed | 069e484 | 2021-01-24 11:39:58 -0500 | [diff] [blame] | 65 | surf->getCanvas()->drawPaint(paint); |
| 66 | fImage = surf->makeImageSnapshot(); // shader->makeImage() |
cwallez | c12b74d | 2015-01-26 07:45:53 -0800 | [diff] [blame] | 67 | } |
| 68 | }; |
| 69 | |
| 70 | class TableCollapseBench: public BaseImageFilterCollapseBench { |
cwallez | c12b74d | 2015-01-26 07:45:53 -0800 | [diff] [blame] | 71 | protected: |
reed | d053ce9 | 2016-03-22 10:17:23 -0700 | [diff] [blame] | 72 | const char* onGetName() override { |
cwallez | c12b74d | 2015-01-26 07:45:53 -0800 | [diff] [blame] | 73 | return "image_filter_collapse_table"; |
| 74 | } |
| 75 | |
reed | d053ce9 | 2016-03-22 10:17:23 -0700 | [diff] [blame] | 76 | void onDelayedSetup() override { |
cwallez | c12b74d | 2015-01-26 07:45:53 -0800 | [diff] [blame] | 77 | for (int i = 0; i < 256; ++i) { |
| 78 | int n = i >> 5; |
| 79 | table1[i] = (n << 5) | (n << 2) | (n >> 1); |
| 80 | |
| 81 | table2[i] = i * i / 255; |
| 82 | |
| 83 | float fi = i / 255.0f; |
| 84 | table3[i] = static_cast<uint8_t>(sqrtf(fi) * 255); |
| 85 | } |
| 86 | |
reed | d053ce9 | 2016-03-22 10:17:23 -0700 | [diff] [blame] | 87 | sk_sp<SkColorFilter> colorFilters[] = { |
| 88 | SkTableColorFilter::Make(table1), |
| 89 | SkTableColorFilter::Make(table2), |
| 90 | SkTableColorFilter::Make(table3), |
cwallez | c12b74d | 2015-01-26 07:45:53 -0800 | [diff] [blame] | 91 | }; |
| 92 | |
robertphillips | 5605b56 | 2016-04-05 11:50:42 -0700 | [diff] [blame] | 93 | this->doPreDraw(colorFilters, SK_ARRAY_COUNT(colorFilters)); |
cwallez | c12b74d | 2015-01-26 07:45:53 -0800 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | private: |
| 97 | uint8_t table1[256], table2[256], table3[256]; |
| 98 | }; |
| 99 | |
reed | d053ce9 | 2016-03-22 10:17:23 -0700 | [diff] [blame] | 100 | static sk_sp<SkColorFilter> make_brightness(float amount) { |
Mike Reed | e869a1e | 2019-04-30 12:18:54 -0400 | [diff] [blame] | 101 | SkScalar matrix[20] = { 1, 0, 0, 0, amount, |
| 102 | 0, 1, 0, 0, amount, |
| 103 | 0, 0, 1, 0, amount, |
cwallez | c12b74d | 2015-01-26 07:45:53 -0800 | [diff] [blame] | 104 | 0, 0, 0, 1, 0 }; |
Mike Reed | e869a1e | 2019-04-30 12:18:54 -0400 | [diff] [blame] | 105 | return SkColorFilters::Matrix(matrix); |
cwallez | c12b74d | 2015-01-26 07:45:53 -0800 | [diff] [blame] | 106 | } |
| 107 | |
reed | d053ce9 | 2016-03-22 10:17:23 -0700 | [diff] [blame] | 108 | static sk_sp<SkColorFilter> make_grayscale() { |
Mike Reed | e869a1e | 2019-04-30 12:18:54 -0400 | [diff] [blame] | 109 | float matrix[20]; |
| 110 | memset(matrix, 0, 20 * sizeof(float)); |
cwallez | c12b74d | 2015-01-26 07:45:53 -0800 | [diff] [blame] | 111 | matrix[0] = matrix[5] = matrix[10] = 0.2126f; |
| 112 | matrix[1] = matrix[6] = matrix[11] = 0.7152f; |
| 113 | matrix[2] = matrix[7] = matrix[12] = 0.0722f; |
| 114 | matrix[18] = 1.0f; |
Mike Reed | e869a1e | 2019-04-30 12:18:54 -0400 | [diff] [blame] | 115 | return SkColorFilters::Matrix(matrix); |
cwallez | c12b74d | 2015-01-26 07:45:53 -0800 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | class MatrixCollapseBench: public BaseImageFilterCollapseBench { |
cwallez | c12b74d | 2015-01-26 07:45:53 -0800 | [diff] [blame] | 119 | protected: |
reed | d053ce9 | 2016-03-22 10:17:23 -0700 | [diff] [blame] | 120 | const char* onGetName() override { |
cwallez | c12b74d | 2015-01-26 07:45:53 -0800 | [diff] [blame] | 121 | return "image_filter_collapse_matrix"; |
| 122 | } |
| 123 | |
reed | d053ce9 | 2016-03-22 10:17:23 -0700 | [diff] [blame] | 124 | void onDelayedSetup() override { |
| 125 | sk_sp<SkColorFilter> colorFilters[] = { |
cwallez | c12b74d | 2015-01-26 07:45:53 -0800 | [diff] [blame] | 126 | make_brightness(0.1f), |
| 127 | make_grayscale(), |
| 128 | make_brightness(-0.1f), |
| 129 | }; |
| 130 | |
robertphillips | 5605b56 | 2016-04-05 11:50:42 -0700 | [diff] [blame] | 131 | this->doPreDraw(colorFilters, SK_ARRAY_COUNT(colorFilters)); |
cwallez | c12b74d | 2015-01-26 07:45:53 -0800 | [diff] [blame] | 132 | } |
| 133 | }; |
| 134 | |
| 135 | DEF_BENCH(return new TableCollapseBench;) |
| 136 | DEF_BENCH(return new MatrixCollapseBench;) |