skia2/bench/ImageFilterCollapse.cpp
reed d053ce9c54 Reland of [2] of "switch colorfilters to sk_sp (patchset #11 id:200001 of https://codereview.chromium.o… (patchset #1 id:1 of https://codereview.chromium.org/1821103004/ )
Reason for revert:
guard has now landed in chrome

Original issue's description:
> Revert of Revert[2] of "switch colorfilters to sk_sp (patchset #11 id:200001 of https://codereview.chromium.o… (patchset #3 id:40001 of https://codereview.chromium.org/1825073002/ )
>
> Reason for revert:
> CreateModeFilter not compiling
>
> Original issue's description:
> > Revert[2] of "switch colorfilters to sk_sp (patchset #11 id:200001 of https://codereview.chromium.org/1822623002/ )"
> >
> > Fixed legacy withColorFilter to call new(er) make method
> >
> > This reverts commit 1eb81db650.
> >
> > BUG=skia:
> > GOLD_TRYBOT_URL= https://gold.skia.org/search2?unt=true&query=source_type%3Dgm&master=false&issue=1825073002
> >
> > TBR=
> >
> > Committed: https://skia.googlesource.com/skia/+/4c9776b046dd5e9e46e2d1ce35154855c8fcb381
>
> TBR=
> # Skipping CQ checks because original CL landed less than 1 days ago.
> NOPRESUBMIT=true
> NOTREECHECKS=true
> NOTRY=true
> BUG=skia:
>
> Committed: https://skia.googlesource.com/skia/+/d6889293dd0942f27f9593f679722c956831f2c4

TBR=
# Not skipping CQ checks because original CL landed more than 1 days ago.
BUG=skia:

Review URL: https://codereview.chromium.org/1827433002
2016-03-22 10:17:23 -07:00

143 lines
4.3 KiB
C++

/*
* 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 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:
BaseImageFilterCollapseBench(): fImageFilter(nullptr) {}
~BaseImageFilterCollapseBench() {
SkSafeUnref(fImageFilter);
}
protected:
void doPreDraw(sk_sp<SkColorFilter> colorFilters[], int nFilters) {
// Create a chain of ImageFilters from colorFilters
fImageFilter = nullptr;
for(int i = nFilters; i --> 0;) {
SkAutoTUnref<SkImageFilter> filter(
SkColorFilterImageFilter::Create(colorFilters[i].get(), fImageFilter, nullptr)
);
SkRefCnt_SafeAssign(fImageFilter, filter.get());
}
}
void onDraw(int loops, SkCanvas* canvas) override {
makeBitmap();
for(int i = 0; i < loops; i++) {
SkPaint paint;
paint.setImageFilter(fImageFilter);
canvas->drawBitmap(fBitmap, 0, 0, &paint);
}
}
private:
SkImageFilter* fImageFilter;
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
};
paint.setShader(SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors),
SkShader::kClamp_TileMode));
canvas.drawPaint(paint);
}
};
class TableCollapseBench: public BaseImageFilterCollapseBench {
protected:
const char* onGetName() override {
return "image_filter_collapse_table";
}
void onDelayedSetup() override {
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);
}
sk_sp<SkColorFilter> colorFilters[] = {
SkTableColorFilter::Make(table1),
SkTableColorFilter::Make(table2),
SkTableColorFilter::Make(table3),
};
doPreDraw(colorFilters, SK_ARRAY_COUNT(colorFilters));
}
private:
uint8_t table1[256], table2[256], table3[256];
};
static sk_sp<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 SkColorFilter::MakeMatrixFilterRowMajor255(matrix);
}
static sk_sp<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 SkColorFilter::MakeMatrixFilterRowMajor255(matrix);
}
class MatrixCollapseBench: public BaseImageFilterCollapseBench {
protected:
const char* onGetName() override {
return "image_filter_collapse_matrix";
}
void onDelayedSetup() override {
sk_sp<SkColorFilter> colorFilters[] = {
make_brightness(0.1f),
make_grayscale(),
make_brightness(-0.1f),
};
doPreDraw(colorFilters, SK_ARRAY_COUNT(colorFilters));
}
};
DEF_BENCH(return new TableCollapseBench;)
DEF_BENCH(return new MatrixCollapseBench;)