• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
8 #include "bench/Benchmark.h"
9 #include "include/core/SkBitmap.h"
10 #include "include/core/SkCanvas.h"
11 #include "include/core/SkImageFilter.h"
12 #include "include/core/SkSurface.h"
13 #include "include/effects/SkColorMatrixFilter.h"
14 #include "include/effects/SkGradientShader.h"
15 #include "include/effects/SkImageFilters.h"
16 #include "include/effects/SkTableColorFilter.h"
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:
BaseImageFilterCollapseBench()26     BaseImageFilterCollapseBench() {}
27 
28 protected:
doPreDraw(sk_sp<SkColorFilter> colorFilters[],int nFilters)29     void doPreDraw(sk_sp<SkColorFilter> colorFilters[], int nFilters) {
30         SkASSERT(!fImageFilter);
31 
32         // Create a chain of ImageFilters from colorFilters
33         for(int i = nFilters; i --> 0;) {
34             fImageFilter = SkImageFilters::ColorFilter(colorFilters[i], fImageFilter);
35         }
36     }
37 
onDraw(int loops,SkCanvas * canvas)38     void onDraw(int loops, SkCanvas* canvas) override {
39         makeBitmap();
40 
41         for(int i = 0; i < loops; i++) {
42             SkPaint paint;
43             paint.setImageFilter(fImageFilter);
44             canvas->drawImage(fImage, 0, 0, SkSamplingOptions(), &paint);
45         }
46     }
47 
48 private:
49     sk_sp<SkImageFilter> fImageFilter;
50     sk_sp<SkImage> fImage;
51 
makeBitmap()52     void makeBitmap() {
53         int W = 400;
54         int H = 400;
55         auto surf = SkSurface::MakeRasterN32Premul(W, H);
56 
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         };
63         paint.setShader(SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors),
64                                                      SkTileMode::kClamp));
65         surf->getCanvas()->drawPaint(paint);
66         fImage = surf->makeImageSnapshot();       // shader->makeImage()
67     }
68 };
69 
70 class TableCollapseBench: public BaseImageFilterCollapseBench {
71 protected:
onGetName()72     const char* onGetName() override {
73         return "image_filter_collapse_table";
74     }
75 
onDelayedSetup()76     void onDelayedSetup() override {
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 
87         sk_sp<SkColorFilter> colorFilters[] = {
88             SkTableColorFilter::Make(table1),
89             SkTableColorFilter::Make(table2),
90             SkTableColorFilter::Make(table3),
91         };
92 
93         this->doPreDraw(colorFilters, SK_ARRAY_COUNT(colorFilters));
94     }
95 
96 private:
97     uint8_t table1[256], table2[256], table3[256];
98 };
99 
make_brightness(float amount)100 static sk_sp<SkColorFilter> make_brightness(float amount) {
101     SkScalar matrix[20] = { 1, 0, 0, 0, amount,
102                             0, 1, 0, 0, amount,
103                             0, 0, 1, 0, amount,
104                             0, 0, 0, 1, 0 };
105     return SkColorFilters::Matrix(matrix);
106 }
107 
make_grayscale()108 static sk_sp<SkColorFilter> make_grayscale() {
109     float matrix[20];
110     memset(matrix, 0, 20 * sizeof(float));
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;
115     return SkColorFilters::Matrix(matrix);
116 }
117 
118 class MatrixCollapseBench: public BaseImageFilterCollapseBench {
119 protected:
onGetName()120     const char* onGetName() override {
121         return "image_filter_collapse_matrix";
122     }
123 
onDelayedSetup()124     void onDelayedSetup() override {
125         sk_sp<SkColorFilter> colorFilters[] = {
126             make_brightness(0.1f),
127             make_grayscale(),
128             make_brightness(-0.1f),
129         };
130 
131         this->doPreDraw(colorFilters, SK_ARRAY_COUNT(colorFilters));
132     }
133 };
134 
135 DEF_BENCH(return new TableCollapseBench;)
136 DEF_BENCH(return new MatrixCollapseBench;)
137