2015-01-26 15:45:53 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2014 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "bench/Benchmark.h"
|
|
|
|
#include "include/core/SkBitmap.h"
|
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkImageFilter.h"
|
|
|
|
#include "include/effects/SkColorFilterImageFilter.h"
|
|
|
|
#include "include/effects/SkColorMatrixFilter.h"
|
|
|
|
#include "include/effects/SkGradientShader.h"
|
|
|
|
#include "include/effects/SkTableColorFilter.h"
|
2015-01-26 15:45:53 +00:00
|
|
|
|
|
|
|
// Chains several matrix color filters image filter or several
|
|
|
|
// table filter image filters and draws a bitmap.
|
|
|
|
// This bench shows an improvement in performance and memory
|
|
|
|
// when collapsing matrices or tables is implemented since all
|
|
|
|
// the passes are collapsed in one.
|
|
|
|
|
|
|
|
class BaseImageFilterCollapseBench : public Benchmark {
|
|
|
|
public:
|
2016-04-05 18:50:42 +00:00
|
|
|
BaseImageFilterCollapseBench() {}
|
2015-01-26 15:45:53 +00:00
|
|
|
|
|
|
|
protected:
|
2016-03-22 17:17:23 +00:00
|
|
|
void doPreDraw(sk_sp<SkColorFilter> colorFilters[], int nFilters) {
|
2016-04-05 18:50:42 +00:00
|
|
|
SkASSERT(!fImageFilter);
|
|
|
|
|
2015-01-26 15:45:53 +00:00
|
|
|
// Create a chain of ImageFilters from colorFilters
|
|
|
|
for(int i = nFilters; i --> 0;) {
|
2016-04-05 18:50:42 +00:00
|
|
|
fImageFilter = SkColorFilterImageFilter::Make(colorFilters[i], fImageFilter);
|
2015-01-26 15:45:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-01 16:43:39 +00:00
|
|
|
void onDraw(int loops, SkCanvas* canvas) override {
|
2015-01-26 15:45:53 +00:00
|
|
|
makeBitmap();
|
|
|
|
|
|
|
|
for(int i = 0; i < loops; i++) {
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setImageFilter(fImageFilter);
|
|
|
|
canvas->drawBitmap(fBitmap, 0, 0, &paint);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2016-04-05 18:50:42 +00:00
|
|
|
sk_sp<SkImageFilter> fImageFilter;
|
2015-01-26 15:45:53 +00:00
|
|
|
SkBitmap fBitmap;
|
|
|
|
|
|
|
|
void makeBitmap() {
|
|
|
|
int W = 400;
|
|
|
|
int H = 400;
|
|
|
|
fBitmap.allocN32Pixels(W, H);
|
|
|
|
fBitmap.eraseColor(SK_ColorTRANSPARENT);
|
|
|
|
|
|
|
|
SkCanvas canvas(fBitmap);
|
|
|
|
SkPaint paint;
|
|
|
|
SkPoint pts[] = { {0, 0}, {SkIntToScalar(W), SkIntToScalar(H)} };
|
|
|
|
SkColor colors[] = {
|
|
|
|
SK_ColorBLACK, SK_ColorGREEN, SK_ColorCYAN,
|
|
|
|
SK_ColorRED, 0, SK_ColorBLUE, SK_ColorWHITE
|
|
|
|
};
|
2016-03-14 19:22:10 +00:00
|
|
|
paint.setShader(SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors),
|
2019-04-03 14:27:45 +00:00
|
|
|
SkTileMode::kClamp));
|
2015-01-26 15:45:53 +00:00
|
|
|
canvas.drawPaint(paint);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class TableCollapseBench: public BaseImageFilterCollapseBench {
|
|
|
|
protected:
|
2016-03-22 17:17:23 +00:00
|
|
|
const char* onGetName() override {
|
2015-01-26 15:45:53 +00:00
|
|
|
return "image_filter_collapse_table";
|
|
|
|
}
|
|
|
|
|
2016-03-22 17:17:23 +00:00
|
|
|
void onDelayedSetup() override {
|
2015-01-26 15:45:53 +00:00
|
|
|
for (int i = 0; i < 256; ++i) {
|
|
|
|
int n = i >> 5;
|
|
|
|
table1[i] = (n << 5) | (n << 2) | (n >> 1);
|
|
|
|
|
|
|
|
table2[i] = i * i / 255;
|
|
|
|
|
|
|
|
float fi = i / 255.0f;
|
|
|
|
table3[i] = static_cast<uint8_t>(sqrtf(fi) * 255);
|
|
|
|
}
|
|
|
|
|
2016-03-22 17:17:23 +00:00
|
|
|
sk_sp<SkColorFilter> colorFilters[] = {
|
|
|
|
SkTableColorFilter::Make(table1),
|
|
|
|
SkTableColorFilter::Make(table2),
|
|
|
|
SkTableColorFilter::Make(table3),
|
2015-01-26 15:45:53 +00:00
|
|
|
};
|
|
|
|
|
2016-04-05 18:50:42 +00:00
|
|
|
this->doPreDraw(colorFilters, SK_ARRAY_COUNT(colorFilters));
|
2015-01-26 15:45:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
uint8_t table1[256], table2[256], table3[256];
|
|
|
|
};
|
|
|
|
|
2016-03-22 17:17:23 +00:00
|
|
|
static sk_sp<SkColorFilter> make_brightness(float amount) {
|
2019-04-30 16:18:54 +00:00
|
|
|
SkScalar matrix[20] = { 1, 0, 0, 0, amount,
|
|
|
|
0, 1, 0, 0, amount,
|
|
|
|
0, 0, 1, 0, amount,
|
2015-01-26 15:45:53 +00:00
|
|
|
0, 0, 0, 1, 0 };
|
2019-04-30 16:18:54 +00:00
|
|
|
return SkColorFilters::Matrix(matrix);
|
2015-01-26 15:45:53 +00:00
|
|
|
}
|
|
|
|
|
2016-03-22 17:17:23 +00:00
|
|
|
static sk_sp<SkColorFilter> make_grayscale() {
|
2019-04-30 16:18:54 +00:00
|
|
|
float matrix[20];
|
|
|
|
memset(matrix, 0, 20 * sizeof(float));
|
2015-01-26 15:45:53 +00:00
|
|
|
matrix[0] = matrix[5] = matrix[10] = 0.2126f;
|
|
|
|
matrix[1] = matrix[6] = matrix[11] = 0.7152f;
|
|
|
|
matrix[2] = matrix[7] = matrix[12] = 0.0722f;
|
|
|
|
matrix[18] = 1.0f;
|
2019-04-30 16:18:54 +00:00
|
|
|
return SkColorFilters::Matrix(matrix);
|
2015-01-26 15:45:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class MatrixCollapseBench: public BaseImageFilterCollapseBench {
|
|
|
|
protected:
|
2016-03-22 17:17:23 +00:00
|
|
|
const char* onGetName() override {
|
2015-01-26 15:45:53 +00:00
|
|
|
return "image_filter_collapse_matrix";
|
|
|
|
}
|
|
|
|
|
2016-03-22 17:17:23 +00:00
|
|
|
void onDelayedSetup() override {
|
|
|
|
sk_sp<SkColorFilter> colorFilters[] = {
|
2015-01-26 15:45:53 +00:00
|
|
|
make_brightness(0.1f),
|
|
|
|
make_grayscale(),
|
|
|
|
make_brightness(-0.1f),
|
|
|
|
};
|
|
|
|
|
2016-04-05 18:50:42 +00:00
|
|
|
this->doPreDraw(colorFilters, SK_ARRAY_COUNT(colorFilters));
|
2015-01-26 15:45:53 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
DEF_BENCH(return new TableCollapseBench;)
|
|
|
|
DEF_BENCH(return new MatrixCollapseBench;)
|