Home | History | Annotate | Download | only in bench
      1 /*
      2  * Copyright 2011 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 #include "Benchmark.h"
      8 #include "SkBitmap.h"
      9 #include "SkCanvas.h"
     10 #include "SkColorPriv.h"
     11 #include "SkPaint.h"
     12 #include "SkRandom.h"
     13 #include "SkString.h"
     14 
     15 static void draw_into_bitmap(const SkBitmap& bm) {
     16     const int w = bm.width();
     17     const int h = bm.height();
     18 
     19     SkCanvas canvas(bm);
     20     SkPaint p;
     21     p.setAntiAlias(true);
     22     p.setColor(SK_ColorRED);
     23     canvas.drawCircle(SkIntToScalar(w)/2, SkIntToScalar(h)/2,
     24                       SkIntToScalar(SkMin32(w, h))*3/8, p);
     25 
     26     SkRect r;
     27     r.set(0, 0, SkIntToScalar(w), SkIntToScalar(h));
     28     p.setStyle(SkPaint::kStroke_Style);
     29     p.setStrokeWidth(SkIntToScalar(4));
     30     p.setColor(SK_ColorBLUE);
     31     canvas.drawRect(r, p);
     32 }
     33 
     34 /*  Variants for bitmaprect
     35     src : entire bitmap, subset, fractional subset
     36     dst : same size as src, diff size
     37     paint : filter-p
     38  */
     39 
     40 class BitmapRectBench : public Benchmark {
     41     SkBitmap                fBitmap;
     42     bool                    fSlightMatrix;
     43     uint8_t                 fAlpha;
     44     SkFilterQuality         fFilterQuality;
     45     SkString                fName;
     46     SkRect                  fSrcR, fDstR;
     47 
     48     static const int kWidth = 128;
     49     static const int kHeight = 128;
     50 public:
     51     BitmapRectBench(U8CPU alpha, SkFilterQuality filterQuality,
     52                     bool slightMatrix)  {
     53         fAlpha = SkToU8(alpha);
     54         fFilterQuality = filterQuality;
     55         fSlightMatrix = slightMatrix;
     56 
     57         fBitmap.setInfo(SkImageInfo::MakeN32Premul(kWidth, kHeight));
     58     }
     59 
     60 protected:
     61     const char* onGetName() override {
     62         fName.printf("bitmaprect_%02X_%sfilter_%s",
     63                      fAlpha,
     64                      kNone_SkFilterQuality == fFilterQuality ? "no" : "",
     65                      fSlightMatrix ? "trans" : "identity");
     66         return fName.c_str();
     67     }
     68 
     69     void onDelayedSetup() override {
     70         fBitmap.allocPixels();
     71         fBitmap.setAlphaType(kOpaque_SkAlphaType);
     72         fBitmap.eraseColor(SK_ColorBLACK);
     73         draw_into_bitmap(fBitmap);
     74 
     75         fSrcR.iset(0, 0, kWidth, kHeight);
     76         fDstR.iset(0, 0, kWidth, kHeight);
     77 
     78         if (fSlightMatrix) {
     79             // want fractional translate
     80             fDstR.offset(SK_Scalar1 / 3, SK_Scalar1 * 5 / 7);
     81             // want enough to create a scale matrix, but not enough to scare
     82             // off our sniffer which tries to see if the matrix is "effectively"
     83             // translate-only.
     84             fDstR.fRight += SK_Scalar1 / (kWidth * 60);
     85         }
     86     }
     87 
     88 
     89     void onDraw(int loops, SkCanvas* canvas) override {
     90         SkRandom rand;
     91 
     92         SkPaint paint;
     93         this->setupPaint(&paint);
     94         paint.setFilterQuality(fFilterQuality);
     95         paint.setAlpha(fAlpha);
     96 
     97         for (int i = 0; i < loops; i++) {
     98             canvas->drawBitmapRect(fBitmap, fSrcR, fDstR, &paint,
     99                                    SkCanvas::kStrict_SrcRectConstraint);
    100         }
    101     }
    102 
    103 private:
    104     typedef Benchmark INHERITED;
    105 };
    106 
    107 DEF_BENCH(return new BitmapRectBench(0xFF, kNone_SkFilterQuality, false))
    108 DEF_BENCH(return new BitmapRectBench(0x80, kNone_SkFilterQuality, false))
    109 DEF_BENCH(return new BitmapRectBench(0xFF, kLow_SkFilterQuality, false))
    110 DEF_BENCH(return new BitmapRectBench(0x80, kLow_SkFilterQuality, false))
    111 
    112 DEF_BENCH(return new BitmapRectBench(0xFF, kNone_SkFilterQuality, true))
    113 DEF_BENCH(return new BitmapRectBench(0xFF, kLow_SkFilterQuality, true))
    114