Flutter Engine
The Flutter Engine
ImageFilterCollapse.cpp
Go to the documentation of this file.
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"
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
24public:
26
27protected:
28 void doPreDraw(sk_sp<SkColorFilter> colorFilters[], int nFilters) {
29 SkASSERT(!fImageFilter);
30
31 // Create a chain of ImageFilters from colorFilters
32 for(int i = nFilters; i --> 0;) {
33 fImageFilter = SkImageFilters::ColorFilter(colorFilters[i], fImageFilter);
34 }
35 }
36
37 void onDraw(int loops, SkCanvas* canvas) override {
38 makeBitmap();
39
40 for(int i = 0; i < loops; i++) {
42 paint.setImageFilter(fImageFilter);
43 canvas->drawImage(fImage, 0, 0, SkSamplingOptions(), &paint);
44 }
45 }
46
47private:
48 sk_sp<SkImageFilter> fImageFilter;
49 sk_sp<SkImage> fImage;
50
51 void makeBitmap() {
52 int W = 400;
53 int H = 400;
55
57 SkPoint pts[] = { {0, 0}, {SkIntToScalar(W), SkIntToScalar(H)} };
58 SkColor colors[] = {
61 };
64 surf->getCanvas()->drawPaint(paint);
65 fImage = surf->makeImageSnapshot(); // shader->makeImage()
66 }
67};
68
70protected:
71 const char* onGetName() override {
72 return "image_filter_collapse_table";
73 }
74
75 void onDelayedSetup() override {
76 for (int i = 0; i < 256; ++i) {
77 int n = i >> 5;
78 table1[i] = (n << 5) | (n << 2) | (n >> 1);
79
80 table2[i] = i * i / 255;
81
82 float fi = i / 255.0f;
83 table3[i] = static_cast<uint8_t>(sqrtf(fi) * 255);
84 }
85
86 sk_sp<SkColorFilter> colorFilters[] = {
90 };
91
92 this->doPreDraw(colorFilters, std::size(colorFilters));
93 }
94
95private:
96 uint8_t table1[256], table2[256], table3[256];
97};
98
100 SkScalar matrix[20] = { 1, 0, 0, 0, amount,
101 0, 1, 0, 0, amount,
102 0, 0, 1, 0, amount,
103 0, 0, 0, 1, 0 };
105}
106
108 float matrix[20];
109 memset(matrix, 0, 20 * sizeof(float));
110 matrix[0] = matrix[5] = matrix[10] = 0.2126f;
111 matrix[1] = matrix[6] = matrix[11] = 0.7152f;
112 matrix[2] = matrix[7] = matrix[12] = 0.0722f;
113 matrix[18] = 1.0f;
115}
116
118protected:
119 const char* onGetName() override {
120 return "image_filter_collapse_matrix";
121 }
122
123 void onDelayedSetup() override {
124 sk_sp<SkColorFilter> colorFilters[] = {
125 make_brightness(0.1f),
127 make_brightness(-0.1f),
128 };
129
130 this->doPreDraw(colorFilters, std::size(colorFilters));
131 }
132};
133
134DEF_BENCH(return new TableCollapseBench;)
#define DEF_BENCH(code)
Definition: Benchmark.h:20
static sk_sp< SkColorFilter > make_brightness(float amount)
static sk_sp< SkColorFilter > make_grayscale()
#define SkASSERT(cond)
Definition: SkAssert.h:116
uint32_t SkColor
Definition: SkColor.h:37
constexpr SkColor SK_ColorCYAN
Definition: SkColor.h:143
constexpr SkColor SK_ColorBLUE
Definition: SkColor.h:135
constexpr SkColor SK_ColorRED
Definition: SkColor.h:126
constexpr SkColor SK_ColorBLACK
Definition: SkColor.h:103
constexpr SkColor SK_ColorGREEN
Definition: SkColor.h:131
constexpr SkColor SK_ColorWHITE
Definition: SkColor.h:122
#define SkIntToScalar(x)
Definition: SkScalar.h:57
#define W
Definition: aaa.cpp:17
void doPreDraw(sk_sp< SkColorFilter > colorFilters[], int nFilters)
void onDraw(int loops, SkCanvas *canvas) override
const char * onGetName() override
void onDelayedSetup() override
void drawImage(const SkImage *image, SkScalar left, SkScalar top)
Definition: SkCanvas.h:1528
static sk_sp< SkColorFilter > Table(const uint8_t table[256])
static sk_sp< SkColorFilter > Matrix(const SkColorMatrix &)
static sk_sp< SkShader > MakeLinear(const SkPoint pts[2], const SkColor colors[], const SkScalar pos[], int count, SkTileMode mode, uint32_t flags=0, const SkMatrix *localMatrix=nullptr)
static sk_sp< SkImageFilter > ColorFilter(sk_sp< SkColorFilter > cf, sk_sp< SkImageFilter > input, const CropRect &cropRect={})
const char * onGetName() override
void onDelayedSetup() override
const Paint & paint
Definition: color_source.cc:38
float SkScalar
Definition: extension.cpp:12
unsigned useCenter Optional< SkMatrix > matrix
Definition: SkRecords.h:258
PODArray< SkColor > colors
Definition: SkRecords.h:276
SK_API sk_sp< SkSurface > Raster(const SkImageInfo &imageInfo, size_t rowBytes, const SkSurfaceProps *surfaceProps)
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive keep the shell running after the Dart script has completed enable serial On low power devices with low core running concurrent GC tasks on threads can cause them to contend with the UI thread which could potentially lead to jank This option turns off all concurrent GC activities domain network JSON encoded network policy per domain This overrides the DisallowInsecureConnections switch Embedder can specify whether to allow or disallow insecure connections at a domain level old gen heap size
Definition: switches.h:259
SkSamplingOptions(SkFilterMode::kLinear))
Definition: SkMD5.cpp:130
static SkImageInfo MakeN32Premul(int width, int height)