Home | History | Annotate | Download | only in bench
      1 /*
      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  */
      7 
      8 #include "Benchmark.h"
      9 #include "SkBitmap.h"
     10 #include "SkBlurImageFilter.h"
     11 #include "SkOffsetImageFilter.h"
     12 #include "SkCanvas.h"
     13 #include "SkPaint.h"
     14 #include "SkRandom.h"
     15 #include "SkShader.h"
     16 #include "SkString.h"
     17 
     18 #define FILTER_WIDTH_SMALL  32
     19 #define FILTER_HEIGHT_SMALL 32
     20 #define FILTER_WIDTH_LARGE  256
     21 #define FILTER_HEIGHT_LARGE 256
     22 #define BLUR_SIGMA_MINI     0.5f
     23 #define BLUR_SIGMA_SMALL    1.0f
     24 #define BLUR_SIGMA_LARGE    10.0f
     25 #define BLUR_SIGMA_HUGE     80.0f
     26 
     27 
     28 // When 'cropped' is set we apply a cropRect to the blurImageFilter. The crop rect is an inset of
     29 // the source's natural dimensions. This is intended to exercise blurring a larger source bitmap
     30 // to a smaller destination bitmap.
     31 
     32 // When 'expanded' is set we apply a cropRect to the input of the blurImageFilter (a noOp
     33 // offsetImageFilter). The crop rect in this case is an inset of the source's natural dimensions.
     34 // An additional crop rect is applied to the blurImageFilter that is just the natural dimensions
     35 // of the source (not inset). This is intended to exercise blurring a smaller source bitmap to a
     36 // larger destination.
     37 
     38 static SkBitmap make_checkerboard(int width, int height) {
     39     SkBitmap bm;
     40     bm.allocN32Pixels(width, height);
     41     SkCanvas canvas(bm);
     42     canvas.clear(0x00000000);
     43     SkPaint darkPaint;
     44     darkPaint.setColor(0xFF804020);
     45     SkPaint lightPaint;
     46     lightPaint.setColor(0xFF244484);
     47     for (int y = 0; y < height; y += 16) {
     48         for (int x = 0; x < width; x += 16) {
     49             canvas.save();
     50             canvas.translate(SkIntToScalar(x), SkIntToScalar(y));
     51             canvas.drawRect(SkRect::MakeXYWH(0, 0, 8, 8), darkPaint);
     52             canvas.drawRect(SkRect::MakeXYWH(8, 0, 8, 8), lightPaint);
     53             canvas.drawRect(SkRect::MakeXYWH(0, 8, 8, 8), lightPaint);
     54             canvas.drawRect(SkRect::MakeXYWH(8, 8, 8, 8), darkPaint);
     55             canvas.restore();
     56         }
     57     }
     58 
     59     return bm;
     60 }
     61 
     62 class BlurImageFilterBench : public Benchmark {
     63 public:
     64     BlurImageFilterBench(SkScalar sigmaX, SkScalar sigmaY,  bool small, bool cropped,
     65                          bool expanded)
     66       : fIsSmall(small)
     67       , fIsCropped(cropped)
     68       , fIsExpanded(expanded)
     69       , fInitialized(false)
     70       , fSigmaX(sigmaX)
     71       , fSigmaY(sigmaY) {
     72         fName.printf("blur_image_filter_%s%s%s_%.2f_%.2f",
     73             fIsSmall ? "small" : "large",
     74             fIsCropped ? "_cropped" : "",
     75             fIsExpanded ? "_expanded" : "",
     76             SkScalarToFloat(sigmaX), SkScalarToFloat(sigmaY));
     77         SkASSERT(!fIsExpanded || fIsCropped); // never want expansion w/o cropping
     78     }
     79 
     80 protected:
     81     const char* onGetName() override {
     82         return fName.c_str();
     83     }
     84 
     85     void onDelayedSetup() override {
     86         if (!fInitialized) {
     87             fCheckerboard = make_checkerboard(fIsSmall ? FILTER_WIDTH_SMALL : FILTER_WIDTH_LARGE,
     88                                               fIsSmall ? FILTER_HEIGHT_SMALL : FILTER_HEIGHT_LARGE);
     89             fInitialized = true;
     90         }
     91     }
     92 
     93     void onDraw(int loops, SkCanvas* canvas) override {
     94         static const SkScalar kX = 0;
     95         static const SkScalar kY = 0;
     96         const SkRect bmpRect = SkRect::MakeXYWH(kX, kY,
     97                                                 SkIntToScalar(fCheckerboard.width()),
     98                                                 SkIntToScalar(fCheckerboard.height()));
     99         const SkImageFilter::CropRect cropRect(bmpRect.makeInset(10.f, 10.f));
    100         const SkImageFilter::CropRect cropRectLarge(bmpRect);
    101 
    102         sk_sp<SkImageFilter> input = fIsExpanded
    103                                         ? SkOffsetImageFilter::Make(0, 0, nullptr, &cropRect)
    104                                         : nullptr;
    105 
    106         const SkImageFilter::CropRect* crop =
    107             fIsExpanded ? &cropRectLarge : fIsCropped ? &cropRect : nullptr;
    108         SkPaint paint;
    109         paint.setImageFilter(SkBlurImageFilter::Make(fSigmaX, fSigmaY, std::move(input), crop));
    110 
    111         for (int i = 0; i < loops; i++) {
    112             canvas->drawBitmap(fCheckerboard, kX, kY, &paint);
    113         }
    114     }
    115 
    116 private:
    117 
    118     SkString fName;
    119     bool fIsSmall;
    120     bool fIsCropped;
    121     bool fIsExpanded;
    122     bool fInitialized;
    123     SkBitmap fCheckerboard;
    124     SkScalar fSigmaX, fSigmaY;
    125     typedef Benchmark INHERITED;
    126 };
    127 
    128 DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_LARGE, 0, false, false, false);)
    129 DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_SMALL, 0, false, false, false);)
    130 DEF_BENCH(return new BlurImageFilterBench(0, BLUR_SIGMA_LARGE, false, false, false);)
    131 DEF_BENCH(return new BlurImageFilterBench(0, BLUR_SIGMA_SMALL, false, false, false);)
    132 DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_MINI, BLUR_SIGMA_MINI, true, false, false);)
    133 DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_MINI, BLUR_SIGMA_MINI, false, false, false);)
    134 DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_SMALL, BLUR_SIGMA_SMALL, true, false, false);)
    135 DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_SMALL, BLUR_SIGMA_SMALL, false, false, false);)
    136 DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_LARGE, BLUR_SIGMA_LARGE, true, false, false);)
    137 DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_LARGE, BLUR_SIGMA_LARGE, false, false, false);)
    138 DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_HUGE, BLUR_SIGMA_HUGE, true, false, false);)
    139 DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_HUGE, BLUR_SIGMA_HUGE, false, false, false);)
    140 
    141 DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_LARGE, 0, false, true, false);)
    142 DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_SMALL, 0, false, true, false);)
    143 DEF_BENCH(return new BlurImageFilterBench(0, BLUR_SIGMA_LARGE, false, true, false);)
    144 DEF_BENCH(return new BlurImageFilterBench(0, BLUR_SIGMA_SMALL, false, true, false);)
    145 DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_MINI, BLUR_SIGMA_MINI, true, true, false);)
    146 DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_MINI, BLUR_SIGMA_MINI, false, true, false);)
    147 DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_SMALL, BLUR_SIGMA_SMALL, true, true, false);)
    148 DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_SMALL, BLUR_SIGMA_SMALL, false, true, false);)
    149 DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_LARGE, BLUR_SIGMA_LARGE, true, true, false);)
    150 DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_LARGE, BLUR_SIGMA_LARGE, false, true, false);)
    151 DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_HUGE, BLUR_SIGMA_HUGE, true, true, false);)
    152 DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_HUGE, BLUR_SIGMA_HUGE, false, true, false);)
    153 
    154 DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_LARGE, 0, false, true, true);)
    155 DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_SMALL, 0, false, true, true);)
    156 DEF_BENCH(return new BlurImageFilterBench(0, BLUR_SIGMA_LARGE, false, true, true);)
    157 DEF_BENCH(return new BlurImageFilterBench(0, BLUR_SIGMA_SMALL, false, true, true);)
    158 DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_MINI, BLUR_SIGMA_MINI, true, true, true);)
    159 DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_MINI, BLUR_SIGMA_MINI, false, true, true);)
    160 DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_SMALL, BLUR_SIGMA_SMALL, true, true, true);)
    161 DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_SMALL, BLUR_SIGMA_SMALL, false, true, true);)
    162 DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_LARGE, BLUR_SIGMA_LARGE, true, true, true);)
    163 DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_LARGE, BLUR_SIGMA_LARGE, false, true, true);)
    164 DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_HUGE, BLUR_SIGMA_HUGE, true, true, true);)
    165 DEF_BENCH(return new BlurImageFilterBench(BLUR_SIGMA_HUGE, BLUR_SIGMA_HUGE, false, true, true);)
    166