Home | History | Annotate | Download | only in bench
      1 
      2 /*
      3  * Copyright 2013 Google Inc.
      4  *
      5  * Use of this source code is governed by a BSD-style license that can be
      6  * found in the LICENSE file.
      7  */
      8 
      9 #include "SkBenchmark.h"
     10 #include "SkBitmap.h"
     11 #include "SkCanvas.h"
     12 #include "SkShader.h"
     13 #include "SkString.h"
     14 
     15 static void create_gradient(SkBitmap* bm) {
     16     SkASSERT(1 == bm->width());
     17     const int height = bm->height();
     18 
     19     float deltaB = 255.0f / height;
     20     float blue = 255.0f;
     21 
     22     SkAutoLockPixels lock(*bm);
     23     for (int y = 0; y < height; y++) {
     24         *bm->getAddr32(0, y) = SkColorSetRGB(0, 0, (U8CPU) blue);
     25         blue -= deltaB;
     26     }
     27 }
     28 
     29 // Test out the special case of a tiled 1xN texture. Test out opacity,
     30 // filtering and the different tiling modes
     31 class ConstXTileBench : public SkBenchmark {
     32     SkPaint             fPaint;
     33     SkString            fName;
     34     bool                fDoFilter;
     35     bool                fDoTrans;
     36     bool                fDoScale;
     37     enum { N = SkBENCHLOOP(20) };
     38     static const int kWidth = 1;
     39     static const int kHeight = 300;
     40 
     41 public:
     42     ConstXTileBench(void* param,
     43                     SkShader::TileMode xTile,
     44                     SkShader::TileMode yTile,
     45                     bool doFilter,
     46                     bool doTrans,
     47                     bool doScale)
     48         : INHERITED(param)
     49         , fDoFilter(doFilter)
     50         , fDoTrans(doTrans)
     51         , fDoScale(doScale) {
     52         SkBitmap bm;
     53 
     54         bm.setConfig(SkBitmap::kARGB_8888_Config, kWidth, kHeight);
     55 
     56         bm.allocPixels();
     57         bm.eraseColor(SK_ColorWHITE);
     58         bm.setIsOpaque(true);
     59 
     60         create_gradient(&bm);
     61 
     62         SkShader* s = SkShader::CreateBitmapShader(bm, xTile, yTile);
     63         fPaint.setShader(s)->unref();
     64 
     65         fName.printf("constXTile_");
     66 
     67         static const char* gTileModeStr[SkShader::kTileModeCount] = { "C", "R", "M" };
     68         fName.append(gTileModeStr[xTile]);
     69         fName.append(gTileModeStr[yTile]);
     70 
     71         if (doFilter) {
     72             fName.append("_filter");
     73         }
     74 
     75         if (doTrans) {
     76             fName.append("_trans");
     77         }
     78 
     79         if (doScale) {
     80             fName.append("_scale");
     81         }
     82     }
     83 
     84 protected:
     85     virtual const char* onGetName() {
     86         return fName.c_str();
     87     }
     88 
     89     virtual void onDraw(SkCanvas* canvas) {
     90         SkPaint paint(fPaint);
     91         this->setupPaint(&paint);
     92         paint.setFilterBitmap(fDoFilter);
     93         if (fDoTrans) {
     94             paint.setColor(SkColorSetARGBMacro(0x80, 0xFF, 0xFF, 0xFF));
     95         }
     96 
     97         SkRect r;
     98 
     99         if (fDoScale) {
    100             r = SkRect::MakeWH(SkIntToScalar(2 * 640), SkIntToScalar(2 * 480));
    101             canvas->scale(SK_ScalarHalf, SK_ScalarHalf);
    102         } else {
    103             r = SkRect::MakeWH(SkIntToScalar(640), SkIntToScalar(480));
    104         }
    105 
    106         SkPaint bgPaint;
    107         bgPaint.setColor(SK_ColorWHITE);
    108 
    109         for (int i = 0; i < N; i++) {
    110             if (fDoTrans) {
    111                 canvas->drawRect(r, bgPaint);
    112             }
    113 
    114             canvas->drawRect(r, paint);
    115         }
    116     }
    117 
    118 private:
    119     typedef SkBenchmark INHERITED;
    120 };
    121 
    122 DEF_BENCH(return new ConstXTileBench(p, SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode, false, false, true))
    123 DEF_BENCH(return new ConstXTileBench(p, SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, false, false, false))
    124 DEF_BENCH(return new ConstXTileBench(p, SkShader::kMirror_TileMode, SkShader::kMirror_TileMode, false, false, true))
    125 
    126 DEF_BENCH(return new ConstXTileBench(p, SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode, true, false, false))
    127 DEF_BENCH(return new ConstXTileBench(p, SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, true, false, true))
    128 DEF_BENCH(return new ConstXTileBench(p, SkShader::kMirror_TileMode, SkShader::kMirror_TileMode, true, false, false))
    129 
    130 DEF_BENCH(return new ConstXTileBench(p, SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode, false, true, true))
    131 DEF_BENCH(return new ConstXTileBench(p, SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, false, true, false))
    132 DEF_BENCH(return new ConstXTileBench(p, SkShader::kMirror_TileMode, SkShader::kMirror_TileMode, false, true, true))
    133 
    134 DEF_BENCH(return new ConstXTileBench(p, SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode, true, true, false))
    135 DEF_BENCH(return new ConstXTileBench(p, SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, true, true, true))
    136 DEF_BENCH(return new ConstXTileBench(p, SkShader::kMirror_TileMode, SkShader::kMirror_TileMode, true, true, false))
    137