Rework colorfilter bench

Previous structure didn't measure anything but new/free cost. All of the
constructs turned into (eventually) drawRect + color.

New version also measures that, but in a clearer way, so we can examine
all the variants, and assume that they should all be about the same.

Change-Id: I1b4a0120a3e663ce1b9daa3c1e3e26ae6b397677
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/214687
Commit-Queue: Mike Reed <reed@google.com>
Reviewed-by: Robert Phillips <robertphillips@google.com>
This commit is contained in:
Mike Reed 2019-05-20 12:30:37 -04:00 committed by Skia Commit-Bot
parent dc2b98fd29
commit d93ee53aff

View File

@ -8,273 +8,70 @@
#include "bench/Benchmark.h" #include "bench/Benchmark.h"
#include "include/core/SkCanvas.h" #include "include/core/SkCanvas.h"
#include "include/effects/SkColorFilterImageFilter.h" #include "include/effects/SkColorFilterImageFilter.h"
#include "include/effects/SkColorMatrixFilter.h"
#define FILTER_WIDTH_SMALL SkIntToScalar(32) // Just need an interesting filter, nothing to special about colormatrix
#define FILTER_HEIGHT_SMALL SkIntToScalar(32) static sk_sp<SkColorFilter> make_grayscale() {
#define FILTER_WIDTH_LARGE SkIntToScalar(256)
#define FILTER_HEIGHT_LARGE SkIntToScalar(256)
static sk_sp<SkImageFilter> make_brightness(float amount, sk_sp<SkImageFilter> input) {
float matrix[20] = { 1, 0, 0, 0, amount,
0, 1, 0, 0, amount,
0, 0, 1, 0, amount,
0, 0, 0, 1, 0 };
sk_sp<SkColorFilter> filter(SkColorFilters::Matrix(matrix));
return SkColorFilterImageFilter::Make(std::move(filter), std::move(input));
}
static sk_sp<SkImageFilter> make_grayscale(sk_sp<SkImageFilter> input) {
float matrix[20]; float matrix[20];
memset(matrix, 0, 20 * sizeof(float)); memset(matrix, 0, 20 * sizeof(float));
matrix[0] = matrix[5] = matrix[10] = 0.2126f; matrix[0] = matrix[5] = matrix[10] = 0.2126f;
matrix[1] = matrix[6] = matrix[11] = 0.7152f; matrix[1] = matrix[6] = matrix[11] = 0.7152f;
matrix[2] = matrix[7] = matrix[12] = 0.0722f; matrix[2] = matrix[7] = matrix[12] = 0.0722f;
matrix[18] = 1.0f; matrix[18] = 1.0f;
sk_sp<SkColorFilter> filter(SkColorFilters::Matrix(matrix)); return SkColorFilters::Matrix(matrix);
return SkColorFilterImageFilter::Make(std::move(filter), std::move(input));
} }
static sk_sp<SkImageFilter> make_mode_blue(sk_sp<SkImageFilter> input) { /**
sk_sp<SkColorFilter> filter(SkColorFilters::Blend(SK_ColorBLUE, SkBlendMode::kSrcIn)); * Different ways to draw the same thing (a red rect)
return SkColorFilterImageFilter::Make(std::move(filter), std::move(input)); * All of their timings should be about the same
} * (we allow for slight overhead to figure out that we can undo the presence of the filters)
*/
class ColorFilterBaseBench : public Benchmark { class FilteredRectBench : public Benchmark {
public: public:
ColorFilterBaseBench(bool small) : fIsSmall(small) { } enum Type {
kNoFilter_Type,
kColorFilter_Type,
kImageFilter_Type,
};
protected: FilteredRectBench(Type t) : fType(t) {
SkRect getFilterRect() const { static const char* suffix[] = { "nofilter", "colorfilter", "imagefilter" };
return this->isSmall() ? SkRect::MakeWH(FILTER_WIDTH_SMALL, FILTER_HEIGHT_SMALL) : fName.printf("filteredrect_%s", suffix[t]);
SkRect::MakeWH(FILTER_WIDTH_LARGE, FILTER_HEIGHT_LARGE); fPaint.setColor(SK_ColorRED);
} }
inline bool isSmall() const { return fIsSmall; } protected:
const char* onGetName() override {
return fName.c_str();
}
void onDelayedSetup() override {
switch (fType) {
case kNoFilter_Type:
break;
case kColorFilter_Type:
fPaint.setColorFilter(make_grayscale());
break;
case kImageFilter_Type:
fPaint.setImageFilter(SkColorFilterImageFilter::Make(make_grayscale(), nullptr));
break;
}
}
void onDraw(int loops, SkCanvas* canvas) override {
const SkRect r = { 0, 0, 256, 256 };
for (int i = 0; i < loops; ++i) {
canvas->drawRect(r, fPaint);
}
}
private: private:
bool fIsSmall; SkPaint fPaint;
SkString fName;
Type fType;
typedef Benchmark INHERITED; typedef Benchmark INHERITED;
}; };
class ColorFilterDimBrightBench : public ColorFilterBaseBench { DEF_BENCH( return new FilteredRectBench(FilteredRectBench::kNoFilter_Type); )
DEF_BENCH( return new FilteredRectBench(FilteredRectBench::kColorFilter_Type); )
public: DEF_BENCH( return new FilteredRectBench(FilteredRectBench::kImageFilter_Type); )
ColorFilterDimBrightBench(bool small) : INHERITED(small) { }
protected:
const char* onGetName() override {
return this->isSmall() ? "colorfilter_dim_bright_small" : "colorfilter_dim_bright_large";
}
void onDraw(int loops, SkCanvas* canvas) override {
SkRect r = this->getFilterRect();
SkPaint paint;
paint.setColor(SK_ColorRED);
for (int i = 0; i < loops; i++) {
for (float brightness = -1.0f; brightness <= 1.0f; brightness += 0.4f) {
sk_sp<SkImageFilter> dim(make_brightness(-brightness, nullptr));
paint.setImageFilter(make_brightness(brightness, std::move(dim)));
canvas->drawRect(r, paint);
}
}
}
private:
typedef ColorFilterBaseBench INHERITED;
};
class ColorFilterBrightGrayBench : public ColorFilterBaseBench {
public:
ColorFilterBrightGrayBench(bool small) : INHERITED(small) { }
protected:
const char* onGetName() override {
return this->isSmall() ? "colorfilter_bright_gray_small" : "colorfilter_bright_gray_large";
}
void onDraw(int loops, SkCanvas* canvas) override {
SkRect r = this->getFilterRect();
SkPaint paint;
paint.setColor(SK_ColorRED);
for (int i = 0; i < loops; i++) {
sk_sp<SkImageFilter> brightness(make_brightness(0.9f, nullptr));
paint.setImageFilter(make_grayscale(std::move(brightness)));
canvas->drawRect(r, paint);
}
}
private:
typedef ColorFilterBaseBench INHERITED;
};
class ColorFilterGrayBrightBench : public ColorFilterBaseBench {
public:
ColorFilterGrayBrightBench(bool small) : INHERITED(small) { }
protected:
const char* onGetName() override {
return this->isSmall() ? "colorfilter_gray_bright_small" : "colorfilter_gray_bright_large";
}
void onDraw(int loops, SkCanvas* canvas) override {
SkRect r = this->getFilterRect();
SkPaint paint;
paint.setColor(SK_ColorRED);
for (int i = 0; i < loops; i++) {
sk_sp<SkImageFilter> grayscale(make_grayscale(nullptr));
paint.setImageFilter(make_brightness(0.9f, std::move(grayscale)));
canvas->drawRect(r, paint);
}
}
private:
typedef ColorFilterBaseBench INHERITED;
};
class ColorFilterBlueBrightBench : public ColorFilterBaseBench {
public:
ColorFilterBlueBrightBench(bool small) : INHERITED(small) { }
protected:
const char* onGetName() override {
return this->isSmall() ? "colorfilter_blue_bright_small" : "colorfilter_blue_bright_large";
}
void onDraw(int loops, SkCanvas* canvas) override {
SkRect r = this->getFilterRect();
SkPaint paint;
paint.setColor(SK_ColorRED);
for (int i = 0; i < loops; i++) {
sk_sp<SkImageFilter> blue(make_mode_blue(nullptr));
paint.setImageFilter(make_brightness(1.0f, std::move(blue)));
canvas->drawRect(r, paint);
}
}
private:
typedef ColorFilterBaseBench INHERITED;
};
class ColorFilterBrightBlueBench : public ColorFilterBaseBench {
public:
ColorFilterBrightBlueBench(bool small) : INHERITED(small) { }
protected:
const char* onGetName() override {
return this->isSmall() ? "colorfilter_bright_blue_small" : "colorfilter_bright_blue_large";
}
void onDraw(int loops, SkCanvas* canvas) override {
SkRect r = this->getFilterRect();
SkPaint paint;
paint.setColor(SK_ColorRED);
for (int i = 0; i < loops; i++) {
sk_sp<SkImageFilter> brightness(make_brightness(1.0f, nullptr));
paint.setImageFilter(make_mode_blue(std::move(brightness)));
canvas->drawRect(r, paint);
}
}
private:
typedef ColorFilterBaseBench INHERITED;
};
class ColorFilterBrightBench : public ColorFilterBaseBench {
public:
ColorFilterBrightBench(bool small) : INHERITED(small) { }
protected:
const char* onGetName() override {
return this->isSmall() ? "colorfilter_bright_small" : "colorfilter_bright_large";
}
void onDraw(int loops, SkCanvas* canvas) override {
SkRect r = this->getFilterRect();
SkPaint paint;
paint.setColor(SK_ColorRED);
for (int i = 0; i < loops; i++) {
paint.setImageFilter(make_brightness(1.0f, nullptr));
canvas->drawRect(r, paint);
}
}
private:
typedef ColorFilterBaseBench INHERITED;
};
class ColorFilterBlueBench : public ColorFilterBaseBench {
public:
ColorFilterBlueBench(bool small) : INHERITED(small) { }
protected:
const char* onGetName() override {
return this->isSmall() ? "colorfilter_blue_small" : "colorfilter_blue_large";
}
void onDraw(int loops, SkCanvas* canvas) override {
SkRect r = this->getFilterRect();
SkPaint paint;
paint.setColor(SK_ColorRED);
for (int i = 0; i < loops; i++) {
paint.setImageFilter(make_mode_blue(nullptr));
canvas->drawRect(r, paint);
}
}
private:
typedef ColorFilterBaseBench INHERITED;
};
class ColorFilterGrayBench : public ColorFilterBaseBench {
public:
ColorFilterGrayBench(bool small) : INHERITED(small) { }
protected:
const char* onGetName() override {
return this->isSmall() ? "colorfilter_gray_small" : "colorfilter_gray_large";
}
void onDraw(int loops, SkCanvas* canvas) override {
SkRect r = this->getFilterRect();
SkPaint paint;
paint.setColor(SK_ColorRED);
for (int i = 0; i < loops; i++) {
paint.setImageFilter(make_grayscale(nullptr));
canvas->drawRect(r, paint);
}
}
private:
typedef ColorFilterBaseBench INHERITED;
};
///////////////////////////////////////////////////////////////////////////////
DEF_BENCH( return new ColorFilterDimBrightBench(true); )
DEF_BENCH( return new ColorFilterBrightGrayBench(true); )
DEF_BENCH( return new ColorFilterGrayBrightBench(true); )
DEF_BENCH( return new ColorFilterBlueBrightBench(true); )
DEF_BENCH( return new ColorFilterBrightBlueBench(true); )
DEF_BENCH( return new ColorFilterBrightBench(true); )
DEF_BENCH( return new ColorFilterBlueBench(true); )
DEF_BENCH( return new ColorFilterGrayBench(true); )
DEF_BENCH( return new ColorFilterDimBrightBench(false); )
DEF_BENCH( return new ColorFilterBrightGrayBench(false); )
DEF_BENCH( return new ColorFilterGrayBrightBench(false); )
DEF_BENCH( return new ColorFilterBlueBrightBench(false); )
DEF_BENCH( return new ColorFilterBrightBlueBench(false); )
DEF_BENCH( return new ColorFilterBrightBench(false); )
DEF_BENCH( return new ColorFilterBlueBench(false); )
DEF_BENCH( return new ColorFilterGrayBench(false); )