Chromium Code Reviews| Index: bench/ImageFilterCollapse.cpp |
| diff --git a/bench/ImageFilterCollapse.cpp b/bench/ImageFilterCollapse.cpp |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..b49bf54ef8bfd5d3d6d006175bbbab0c4d16acf0 |
| --- /dev/null |
| +++ b/bench/ImageFilterCollapse.cpp |
| @@ -0,0 +1,154 @@ |
| +/* |
| + * Copyright 2014 Google Inc. |
| + * |
| + * Use of this source code is governed by a BSD-style license that can be |
| + * found in the LICENSE file. |
| + */ |
| + |
| +#include "Benchmark.h" |
| +#include "SkBitmap.h" |
| +#include "SkCanvas.h" |
| +#include "SkColorFilterImageFilter.h" |
| +#include "SkColorMatrixFilter.h" |
| +#include "SkGradientShader.h" |
| +#include "SkImageFilter.h" |
| +#include "SkTableColorFilter.h" |
| + |
| +// Chains several matrix color filter imager 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: |
| + BaseImageFilterCollapseBench() {} |
| + |
| +protected: |
| + void doDraw(int loops, SkCanvas* canvas, SkColorFilter* colorFilters[], int nFilters) { |
| + makeBitmap(); |
| + |
| + SkImageFilter* imageFilter = NULL; |
| + for(int i = nFilters; i --> 0;) { |
|
Justin Novosad
2014/12/19 23:54:37
Add comment to explain you are creating a chain of
cwallez
2015/01/19 19:56:12
Done.
|
| + SkImageFilter* filter = SkColorFilterImageFilter::Create(colorFilters[i], |
| + imageFilter, NULL, 0); |
| + |
| + if (imageFilter) { |
| + imageFilter->unref(); |
| + } |
| + imageFilter = filter; |
| + } |
| + |
| + for(int i = 0; i < loops; i++) { |
| + SkPaint paint; |
| + paint.setImageFilter(imageFilter); |
| + canvas->drawBitmap(bm, 0, 0, &paint); |
| + } |
| + |
| + imageFilter->unref(); |
| + } |
| + |
| +private: |
| + SkBitmap bm; |
| + |
| + void makeBitmap() { |
| + int W = 400; |
| + int H = 400; |
| + bm.allocN32Pixels(W, H); |
| + bm.eraseColor(SK_ColorTRANSPARENT); |
| + |
| + SkCanvas canvas(bm); |
| + 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 |
| + }; |
| + SkShader* s = SkGradientShader::CreateLinear(pts, colors, NULL, SK_ARRAY_COUNT(colors), |
| + SkShader::kClamp_TileMode); |
| + paint.setShader(s)->unref(); |
| + canvas.drawPaint(paint); |
| + } |
| +}; |
| + |
| +class TableCollapseBench: public BaseImageFilterCollapseBench { |
| +public: |
| + virtual ~TableCollapseBench() {} |
| + |
| +protected: |
| + virtual const char* onGetName() SK_OVERRIDE { |
| + return "image_filter_collapse_table"; |
| + } |
| + |
| + virtual void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE { |
| + uint8_t table1[256], table2[256], table3[256]; |
|
Justin Novosad
2014/12/19 23:54:37
Only the things you really want to measure should
cwallez
2015/01/19 19:56:12
Done.
|
| + |
| + 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); |
| + } |
| + |
| + SkColorFilter* colorFilters[] = { |
| + SkTableColorFilter::Create(table1), |
| + SkTableColorFilter::Create(table2), |
| + SkTableColorFilter::Create(table3), |
| + }; |
| + |
| + doDraw(loops, canvas, colorFilters, SK_ARRAY_COUNT(colorFilters)); |
| + |
| + for(unsigned i = 0; i < SK_ARRAY_COUNT(colorFilters); i++) { |
| + colorFilters[i]->unref(); |
| + } |
| + } |
| +}; |
| + |
| +static SkColorFilter* make_brightness(float amount) { |
| + SkScalar amount255 = SkScalarMul(amount, SkIntToScalar(255)); |
| + SkScalar matrix[20] = { 1, 0, 0, 0, amount255, |
| + 0, 1, 0, 0, amount255, |
| + 0, 0, 1, 0, amount255, |
| + 0, 0, 0, 1, 0 }; |
| + return SkColorMatrixFilter::Create(matrix); |
| +} |
| + |
| +static SkColorFilter* make_grayscale() { |
| + SkScalar matrix[20]; |
| + memset(matrix, 0, 20 * sizeof(SkScalar)); |
| + 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; |
| + return SkColorMatrixFilter::Create(matrix); |
| +} |
| + |
| +class MatrixCollapseBench: public BaseImageFilterCollapseBench { |
| +public: |
| + virtual ~MatrixCollapseBench() {} |
| + |
| +protected: |
| + virtual const char* onGetName() SK_OVERRIDE { |
| + return "matrix_filter_collapse_table"; |
| + } |
| + |
| + virtual void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE { |
| + SkColorFilter* colorFilters[] = { |
|
Justin Novosad
2014/12/19 23:54:37
Same thing here: this initialization should not be
cwallez
2015/01/19 19:56:13
Done.
|
| + make_brightness(0.1f), |
| + make_grayscale(), |
| + make_brightness(-0.1f), |
| + }; |
| + |
| + doDraw(loops, canvas, colorFilters, SK_ARRAY_COUNT(colorFilters)); |
| + |
| + for(unsigned i = 0; i < SK_ARRAY_COUNT(colorFilters); i++) { |
| + colorFilters[i]->unref(); |
| + } |
| + } |
| +}; |
| + |
| +DEF_BENCH(return new TableCollapseBench;) |
| +DEF_BENCH(return new MatrixCollapseBench;) |