blob: 560375a00b96d7399269647431cb27d0e80b59cc [file] [log] [blame]
sugoi@google.com580a1722013-04-23 14:20:45 +00001/*
2 * Copyright 2013 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 */
reedaa4c7a72015-04-02 20:31:17 -07007
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "bench/Benchmark.h"
9#include "include/core/SkCanvas.h"
10#include "include/effects/SkColorFilterImageFilter.h"
sugoi@google.com580a1722013-04-23 14:20:45 +000011
Mike Reedd93ee532019-05-20 12:30:37 -040012// Just need an interesting filter, nothing to special about colormatrix
13static sk_sp<SkColorFilter> make_grayscale() {
Mike Reede869a1e2019-04-30 12:18:54 -040014 float matrix[20];
15 memset(matrix, 0, 20 * sizeof(float));
robertphillips5605b562016-04-05 11:50:42 -070016 matrix[0] = matrix[5] = matrix[10] = 0.2126f;
17 matrix[1] = matrix[6] = matrix[11] = 0.7152f;
18 matrix[2] = matrix[7] = matrix[12] = 0.0722f;
19 matrix[18] = 1.0f;
Mike Reedd93ee532019-05-20 12:30:37 -040020 return SkColorFilters::Matrix(matrix);
robertphillips5605b562016-04-05 11:50:42 -070021}
22
Mike Reedd93ee532019-05-20 12:30:37 -040023/**
24 * Different ways to draw the same thing (a red rect)
25 * All of their timings should be about the same
26 * (we allow for slight overhead to figure out that we can undo the presence of the filters)
27 */
28class FilteredRectBench : public Benchmark {
sugoi@google.com580a1722013-04-23 14:20:45 +000029public:
Mike Reedd93ee532019-05-20 12:30:37 -040030 enum Type {
31 kNoFilter_Type,
32 kColorFilter_Type,
33 kImageFilter_Type,
34 };
sugoi@google.com580a1722013-04-23 14:20:45 +000035
Mike Reedd93ee532019-05-20 12:30:37 -040036 FilteredRectBench(Type t) : fType(t) {
37 static const char* suffix[] = { "nofilter", "colorfilter", "imagefilter" };
38 fName.printf("filteredrect_%s", suffix[t]);
39 fPaint.setColor(SK_ColorRED);
sugoi@google.com580a1722013-04-23 14:20:45 +000040 }
41
Mike Reedd93ee532019-05-20 12:30:37 -040042protected:
43 const char* onGetName() override {
44 return fName.c_str();
45 }
46
47 void onDelayedSetup() override {
48 switch (fType) {
49 case kNoFilter_Type:
50 break;
51 case kColorFilter_Type:
52 fPaint.setColorFilter(make_grayscale());
53 break;
54 case kImageFilter_Type:
55 fPaint.setImageFilter(SkColorFilterImageFilter::Make(make_grayscale(), nullptr));
56 break;
57 }
58 }
59
60 void onDraw(int loops, SkCanvas* canvas) override {
61 const SkRect r = { 0, 0, 256, 256 };
62 for (int i = 0; i < loops; ++i) {
63 canvas->drawRect(r, fPaint);
64 }
65 }
reedaa4c7a72015-04-02 20:31:17 -070066
sugoi@google.com580a1722013-04-23 14:20:45 +000067private:
Mike Reedd93ee532019-05-20 12:30:37 -040068 SkPaint fPaint;
69 SkString fName;
70 Type fType;
sugoi@google.com580a1722013-04-23 14:20:45 +000071
tfarinaf168b862014-06-19 12:32:29 -070072 typedef Benchmark INHERITED;
sugoi@google.com580a1722013-04-23 14:20:45 +000073};
74
Mike Reedd93ee532019-05-20 12:30:37 -040075DEF_BENCH( return new FilteredRectBench(FilteredRectBench::kNoFilter_Type); )
76DEF_BENCH( return new FilteredRectBench(FilteredRectBench::kColorFilter_Type); )
77DEF_BENCH( return new FilteredRectBench(FilteredRectBench::kImageFilter_Type); )