blob: ce27f06dd04c4414d2105d41d43f0d52dd227920 [file] [log] [blame]
cwallezc12b74d2015-01-26 07:45:53 -08001/*
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 "Benchmark.h"
9#include "SkBitmap.h"
10#include "SkCanvas.h"
11#include "SkColorFilterImageFilter.h"
12#include "SkColorMatrixFilter.h"
13#include "SkGradientShader.h"
14#include "SkImageFilter.h"
15#include "SkTableColorFilter.h"
16
17// Chains several matrix color filters image filter or several
18// table filter image filters and draws a bitmap.
19// This bench shows an improvement in performance and memory
20// when collapsing matrices or tables is implemented since all
21// the passes are collapsed in one.
22
23class BaseImageFilterCollapseBench : public Benchmark {
24public:
robertphillips5605b562016-04-05 11:50:42 -070025 BaseImageFilterCollapseBench() {}
cwallezc12b74d2015-01-26 07:45:53 -080026
27protected:
reedd053ce92016-03-22 10:17:23 -070028 void doPreDraw(sk_sp<SkColorFilter> colorFilters[], int nFilters) {
robertphillips5605b562016-04-05 11:50:42 -070029 SkASSERT(!fImageFilter);
30
cwallezc12b74d2015-01-26 07:45:53 -080031 // Create a chain of ImageFilters from colorFilters
cwallezc12b74d2015-01-26 07:45:53 -080032 for(int i = nFilters; i --> 0;) {
robertphillips5605b562016-04-05 11:50:42 -070033 fImageFilter = SkColorFilterImageFilter::Make(colorFilters[i], fImageFilter);
cwallezc12b74d2015-01-26 07:45:53 -080034 }
35 }
36
mtkleina1ebeb22015-10-01 09:43:39 -070037 void onDraw(int loops, SkCanvas* canvas) override {
cwallezc12b74d2015-01-26 07:45:53 -080038 makeBitmap();
39
40 for(int i = 0; i < loops; i++) {
41 SkPaint paint;
42 paint.setImageFilter(fImageFilter);
43 canvas->drawBitmap(fBitmap, 0, 0, &paint);
44 }
45 }
46
47private:
robertphillips5605b562016-04-05 11:50:42 -070048 sk_sp<SkImageFilter> fImageFilter;
cwallezc12b74d2015-01-26 07:45:53 -080049 SkBitmap fBitmap;
50
51 void makeBitmap() {
52 int W = 400;
53 int H = 400;
54 fBitmap.allocN32Pixels(W, H);
55 fBitmap.eraseColor(SK_ColorTRANSPARENT);
56
57 SkCanvas canvas(fBitmap);
58 SkPaint paint;
59 SkPoint pts[] = { {0, 0}, {SkIntToScalar(W), SkIntToScalar(H)} };
60 SkColor colors[] = {
61 SK_ColorBLACK, SK_ColorGREEN, SK_ColorCYAN,
62 SK_ColorRED, 0, SK_ColorBLUE, SK_ColorWHITE
63 };
reedc6f28f72016-03-14 12:22:10 -070064 paint.setShader(SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors),
65 SkShader::kClamp_TileMode));
cwallezc12b74d2015-01-26 07:45:53 -080066 canvas.drawPaint(paint);
67 }
68};
69
70class TableCollapseBench: public BaseImageFilterCollapseBench {
cwallezc12b74d2015-01-26 07:45:53 -080071protected:
reedd053ce92016-03-22 10:17:23 -070072 const char* onGetName() override {
cwallezc12b74d2015-01-26 07:45:53 -080073 return "image_filter_collapse_table";
74 }
75
reedd053ce92016-03-22 10:17:23 -070076 void onDelayedSetup() override {
cwallezc12b74d2015-01-26 07:45:53 -080077 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
reedd053ce92016-03-22 10:17:23 -070087 sk_sp<SkColorFilter> colorFilters[] = {
88 SkTableColorFilter::Make(table1),
89 SkTableColorFilter::Make(table2),
90 SkTableColorFilter::Make(table3),
cwallezc12b74d2015-01-26 07:45:53 -080091 };
92
robertphillips5605b562016-04-05 11:50:42 -070093 this->doPreDraw(colorFilters, SK_ARRAY_COUNT(colorFilters));
cwallezc12b74d2015-01-26 07:45:53 -080094 }
95
96private:
97 uint8_t table1[256], table2[256], table3[256];
98};
99
reedd053ce92016-03-22 10:17:23 -0700100static sk_sp<SkColorFilter> make_brightness(float amount) {
Mike Reeddf85c382017-02-14 10:59:19 -0500101 SkScalar amount255 = amount * 255;
cwallezc12b74d2015-01-26 07:45:53 -0800102 SkScalar matrix[20] = { 1, 0, 0, 0, amount255,
103 0, 1, 0, 0, amount255,
104 0, 0, 1, 0, amount255,
105 0, 0, 0, 1, 0 };
reedd053ce92016-03-22 10:17:23 -0700106 return SkColorFilter::MakeMatrixFilterRowMajor255(matrix);
cwallezc12b74d2015-01-26 07:45:53 -0800107}
108
reedd053ce92016-03-22 10:17:23 -0700109static sk_sp<SkColorFilter> make_grayscale() {
cwallezc12b74d2015-01-26 07:45:53 -0800110 SkScalar matrix[20];
111 memset(matrix, 0, 20 * sizeof(SkScalar));
112 matrix[0] = matrix[5] = matrix[10] = 0.2126f;
113 matrix[1] = matrix[6] = matrix[11] = 0.7152f;
114 matrix[2] = matrix[7] = matrix[12] = 0.0722f;
115 matrix[18] = 1.0f;
reedd053ce92016-03-22 10:17:23 -0700116 return SkColorFilter::MakeMatrixFilterRowMajor255(matrix);
cwallezc12b74d2015-01-26 07:45:53 -0800117}
118
119class MatrixCollapseBench: public BaseImageFilterCollapseBench {
cwallezc12b74d2015-01-26 07:45:53 -0800120protected:
reedd053ce92016-03-22 10:17:23 -0700121 const char* onGetName() override {
cwallezc12b74d2015-01-26 07:45:53 -0800122 return "image_filter_collapse_matrix";
123 }
124
reedd053ce92016-03-22 10:17:23 -0700125 void onDelayedSetup() override {
126 sk_sp<SkColorFilter> colorFilters[] = {
cwallezc12b74d2015-01-26 07:45:53 -0800127 make_brightness(0.1f),
128 make_grayscale(),
129 make_brightness(-0.1f),
130 };
131
robertphillips5605b562016-04-05 11:50:42 -0700132 this->doPreDraw(colorFilters, SK_ARRAY_COUNT(colorFilters));
cwallezc12b74d2015-01-26 07:45:53 -0800133 }
134};
135
136DEF_BENCH(return new TableCollapseBench;)
137DEF_BENCH(return new MatrixCollapseBench;)