Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 /* | |
| 2 * Copyright 2014 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 */ | |
| 7 | |
| 8 #include "Benchmark.h" | |
| 9 #include "SkBitmap.h" | |
| 10 #include "SkCanvas.h" | |
| 11 #include "SkColorFilterImageFilter.h" | |
| 12 #include "SkColorMatrixFilter.h" | |
| 13 #include "SkGradientShader.h" | |
| 14 #include "SkImageFilter.h" | |
| 15 #include "SkTableColorFilter.h" | |
| 16 | |
| 17 // Chains several matrix color filter imager filter or several | |
| 18 // table filter image filters and draws a bitmap. | |
| 19 // This bench shows an improvement in performance and memory | |
| 20 // when collapsing matrices or tables is implemented since all | |
| 21 // the passes are collapsed in one. | |
| 22 | |
| 23 class BaseImageFilterCollapseBench : public Benchmark { | |
| 24 public: | |
| 25 BaseImageFilterCollapseBench() {} | |
| 26 | |
| 27 protected: | |
| 28 void doDraw(int loops, SkCanvas* canvas, SkColorFilter* colorFilters[], int nFilters) { | |
| 29 makeBitmap(); | |
| 30 | |
| 31 SkImageFilter* imageFilter = NULL; | |
| 32 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.
| |
| 33 SkImageFilter* filter = SkColorFilterImageFilter::Create(colorFilter s[i], | |
| 34 imageFilter , NULL, 0); | |
| 35 | |
| 36 if (imageFilter) { | |
| 37 imageFilter->unref(); | |
| 38 } | |
| 39 imageFilter = filter; | |
| 40 } | |
| 41 | |
| 42 for(int i = 0; i < loops; i++) { | |
| 43 SkPaint paint; | |
| 44 paint.setImageFilter(imageFilter); | |
| 45 canvas->drawBitmap(bm, 0, 0, &paint); | |
| 46 } | |
| 47 | |
| 48 imageFilter->unref(); | |
| 49 } | |
| 50 | |
| 51 private: | |
| 52 SkBitmap bm; | |
| 53 | |
| 54 void makeBitmap() { | |
| 55 int W = 400; | |
| 56 int H = 400; | |
| 57 bm.allocN32Pixels(W, H); | |
| 58 bm.eraseColor(SK_ColorTRANSPARENT); | |
| 59 | |
| 60 SkCanvas canvas(bm); | |
| 61 SkPaint paint; | |
| 62 SkPoint pts[] = { {0, 0}, {SkIntToScalar(W), SkIntToScalar(H)} }; | |
| 63 SkColor colors[] = { | |
| 64 SK_ColorBLACK, SK_ColorGREEN, SK_ColorCYAN, | |
| 65 SK_ColorRED, 0, SK_ColorBLUE, SK_ColorWHITE | |
| 66 }; | |
| 67 SkShader* s = SkGradientShader::CreateLinear(pts, colors, NULL, SK_ARRAY _COUNT(colors), | |
| 68 SkShader::kClamp_TileMode); | |
| 69 paint.setShader(s)->unref(); | |
| 70 canvas.drawPaint(paint); | |
| 71 } | |
| 72 }; | |
| 73 | |
| 74 class TableCollapseBench: public BaseImageFilterCollapseBench { | |
| 75 public: | |
| 76 virtual ~TableCollapseBench() {} | |
| 77 | |
| 78 protected: | |
| 79 virtual const char* onGetName() SK_OVERRIDE { | |
| 80 return "image_filter_collapse_table"; | |
| 81 } | |
| 82 | |
| 83 virtual void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE { | |
| 84 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.
| |
| 85 | |
| 86 for (int i = 0; i < 256; ++i) { | |
| 87 int n = i >> 5; | |
| 88 table1[i] = (n << 5) | (n << 2) | (n >> 1); | |
| 89 | |
| 90 table2[i] = i * i / 255; | |
| 91 | |
| 92 float fi = i / 255.0f; | |
| 93 table3[i] = static_cast<uint8_t>(sqrtf(fi) * 255); | |
| 94 } | |
| 95 | |
| 96 SkColorFilter* colorFilters[] = { | |
| 97 SkTableColorFilter::Create(table1), | |
| 98 SkTableColorFilter::Create(table2), | |
| 99 SkTableColorFilter::Create(table3), | |
| 100 }; | |
| 101 | |
| 102 doDraw(loops, canvas, colorFilters, SK_ARRAY_COUNT(colorFilters)); | |
| 103 | |
| 104 for(unsigned i = 0; i < SK_ARRAY_COUNT(colorFilters); i++) { | |
| 105 colorFilters[i]->unref(); | |
| 106 } | |
| 107 } | |
| 108 }; | |
| 109 | |
| 110 static SkColorFilter* make_brightness(float amount) { | |
| 111 SkScalar amount255 = SkScalarMul(amount, SkIntToScalar(255)); | |
| 112 SkScalar matrix[20] = { 1, 0, 0, 0, amount255, | |
| 113 0, 1, 0, 0, amount255, | |
| 114 0, 0, 1, 0, amount255, | |
| 115 0, 0, 0, 1, 0 }; | |
| 116 return SkColorMatrixFilter::Create(matrix); | |
| 117 } | |
| 118 | |
| 119 static SkColorFilter* make_grayscale() { | |
| 120 SkScalar matrix[20]; | |
| 121 memset(matrix, 0, 20 * sizeof(SkScalar)); | |
| 122 matrix[0] = matrix[5] = matrix[10] = 0.2126f; | |
| 123 matrix[1] = matrix[6] = matrix[11] = 0.7152f; | |
| 124 matrix[2] = matrix[7] = matrix[12] = 0.0722f; | |
| 125 matrix[18] = 1.0f; | |
| 126 return SkColorMatrixFilter::Create(matrix); | |
| 127 } | |
| 128 | |
| 129 class MatrixCollapseBench: public BaseImageFilterCollapseBench { | |
| 130 public: | |
| 131 virtual ~MatrixCollapseBench() {} | |
| 132 | |
| 133 protected: | |
| 134 virtual const char* onGetName() SK_OVERRIDE { | |
| 135 return "matrix_filter_collapse_table"; | |
| 136 } | |
| 137 | |
| 138 virtual void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE { | |
| 139 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.
| |
| 140 make_brightness(0.1f), | |
| 141 make_grayscale(), | |
| 142 make_brightness(-0.1f), | |
| 143 }; | |
| 144 | |
| 145 doDraw(loops, canvas, colorFilters, SK_ARRAY_COUNT(colorFilters)); | |
| 146 | |
| 147 for(unsigned i = 0; i < SK_ARRAY_COUNT(colorFilters); i++) { | |
| 148 colorFilters[i]->unref(); | |
| 149 } | |
| 150 } | |
| 151 }; | |
| 152 | |
| 153 DEF_BENCH(return new TableCollapseBench;) | |
| 154 DEF_BENCH(return new MatrixCollapseBench;) | |
| OLD | NEW |