Home | History | Annotate | Download | only in bench
      1 
      2 /*
      3  * Copyright 2011 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 #include "SkBenchmark.h"
      9 #include "SkBitmap.h"
     10 #include "SkPaint.h"
     11 #include "SkCanvas.h"
     12 #include "SkColorPriv.h"
     13 #include "SkRandom.h"
     14 #include "SkString.h"
     15 
     16 static const char* gTileName[] = {
     17     "clamp", "repeat", "mirror"
     18 };
     19 
     20 static const char* gConfigName[] = {
     21     "ERROR", "a1", "a8", "index8", "565", "4444", "8888"
     22 };
     23 
     24 static void drawIntoBitmap(const SkBitmap& bm) {
     25     const int w = bm.width();
     26     const int h = bm.height();
     27 
     28     SkCanvas canvas(bm);
     29     SkPaint p;
     30     p.setAntiAlias(true);
     31     p.setColor(SK_ColorRED);
     32     canvas.drawCircle(SkIntToScalar(w)/2, SkIntToScalar(h)/2,
     33                       SkIntToScalar(SkMin32(w, h))*3/8, p);
     34 
     35     SkRect r;
     36     r.set(0, 0, SkIntToScalar(w), SkIntToScalar(h));
     37     p.setStyle(SkPaint::kStroke_Style);
     38     p.setStrokeWidth(SkIntToScalar(4));
     39     p.setColor(SK_ColorBLUE);
     40     canvas.drawRect(r, p);
     41 }
     42 
     43 static int conv6ToByte(int x) {
     44     return x * 0xFF / 5;
     45 }
     46 
     47 static int convByteTo6(int x) {
     48     return x * 5 / 255;
     49 }
     50 
     51 static uint8_t compute666Index(SkPMColor c) {
     52     int r = SkGetPackedR32(c);
     53     int g = SkGetPackedG32(c);
     54     int b = SkGetPackedB32(c);
     55 
     56     return convByteTo6(r) * 36 + convByteTo6(g) * 6 + convByteTo6(b);
     57 }
     58 
     59 static void convertToIndex666(const SkBitmap& src, SkBitmap* dst) {
     60     SkColorTable* ctable = new SkColorTable(216);
     61     SkPMColor* colors = ctable->lockColors();
     62     // rrr ggg bbb
     63     for (int r = 0; r < 6; r++) {
     64         int rr = conv6ToByte(r);
     65         for (int g = 0; g < 6; g++) {
     66             int gg = conv6ToByte(g);
     67             for (int b = 0; b < 6; b++) {
     68                 int bb = conv6ToByte(b);
     69                 *colors++ = SkPreMultiplyARGB(0xFF, rr, gg, bb);
     70             }
     71         }
     72     }
     73     ctable->unlockColors(true);
     74     dst->setConfig(SkBitmap::kIndex8_Config, src.width(), src.height());
     75     dst->allocPixels(ctable);
     76     ctable->unref();
     77 
     78     SkAutoLockPixels alps(src);
     79     SkAutoLockPixels alpd(*dst);
     80 
     81     for (int y = 0; y < src.height(); y++) {
     82         const SkPMColor* srcP = src.getAddr32(0, y);
     83         uint8_t* dstP = dst->getAddr8(0, y);
     84         for (int x = src.width() - 1; x >= 0; --x) {
     85             *dstP++ = compute666Index(*srcP++);
     86         }
     87     }
     88 }
     89 
     90 /*  Variants for bitmaps
     91 
     92     - src depth (32 w+w/o alpha), 565, 4444, index, a8
     93     - paint options: filtering, dither, alpha
     94     - matrix options: translate, scale, rotate, persp
     95     - tiling: none, repeat, mirror, clamp
     96 
     97  */
     98 
     99 class BitmapBench : public SkBenchmark {
    100     SkBitmap    fBitmap;
    101     SkPaint     fPaint;
    102     bool        fIsOpaque;
    103     bool        fForceUpdate; //bitmap marked as dirty before each draw. forces bitmap to be updated on device cache
    104     int         fTileX, fTileY; // -1 means don't use shader
    105     SkString    fName;
    106     enum { N = SkBENCHLOOP(300) };
    107 public:
    108     BitmapBench(void* param, bool isOpaque, SkBitmap::Config c,
    109                 bool forceUpdate = false, bool bitmapVolatile = false,
    110                 int tx = -1, int ty = -1)
    111         : INHERITED(param), fIsOpaque(isOpaque), fForceUpdate(forceUpdate), fTileX(tx), fTileY(ty) {
    112         const int w = 128;
    113         const int h = 128;
    114         SkBitmap bm;
    115 
    116         if (SkBitmap::kIndex8_Config == c) {
    117             bm.setConfig(SkBitmap::kARGB_8888_Config, w, h);
    118         } else {
    119             bm.setConfig(c, w, h);
    120         }
    121         bm.allocPixels();
    122         bm.eraseColor(isOpaque ? SK_ColorBLACK : 0);
    123 
    124         drawIntoBitmap(bm);
    125 
    126         if (SkBitmap::kIndex8_Config == c) {
    127             convertToIndex666(bm, &fBitmap);
    128         } else {
    129             fBitmap = bm;
    130         }
    131 
    132         if (fBitmap.getColorTable()) {
    133             fBitmap.getColorTable()->setIsOpaque(isOpaque);
    134         }
    135         fBitmap.setIsOpaque(isOpaque);
    136         fBitmap.setIsVolatile(bitmapVolatile);
    137     }
    138 
    139 protected:
    140     virtual const char* onGetName() {
    141         fName.set("bitmap");
    142         if (fTileX >= 0) {
    143             fName.appendf("_%s", gTileName[fTileX]);
    144             if (fTileY != fTileX) {
    145                 fName.appendf("_%s", gTileName[fTileY]);
    146             }
    147         }
    148         fName.appendf("_%s%s", gConfigName[fBitmap.config()],
    149                       fIsOpaque ? "" : "_A");
    150         if (fForceUpdate)
    151             fName.append("_update");
    152         if (fBitmap.isVolatile())
    153             fName.append("_volatile");
    154 
    155         return fName.c_str();
    156     }
    157 
    158     virtual void onDraw(SkCanvas* canvas) {
    159         SkIPoint dim = this->getSize();
    160         SkRandom rand;
    161 
    162         SkPaint paint(fPaint);
    163         this->setupPaint(&paint);
    164 
    165         const SkBitmap& bitmap = fBitmap;
    166         const SkScalar x0 = SkIntToScalar(-bitmap.width() / 2);
    167         const SkScalar y0 = SkIntToScalar(-bitmap.height() / 2);
    168 
    169         for (int i = 0; i < N; i++) {
    170             SkScalar x = x0 + rand.nextUScalar1() * dim.fX;
    171             SkScalar y = y0 + rand.nextUScalar1() * dim.fY;
    172 
    173             if (fForceUpdate)
    174                 bitmap.notifyPixelsChanged();
    175 
    176             canvas->drawBitmap(bitmap, x, y, &paint);
    177         }
    178     }
    179 
    180 private:
    181     typedef SkBenchmark INHERITED;
    182 };
    183 
    184 /** Explicitly invoke some filter types to improve coverage of acceleration
    185     procs. */
    186 
    187 class FilterBitmapBench : public BitmapBench {
    188     bool        fScale;
    189     bool        fRotate;
    190     bool        fFilter;
    191     SkString    fFullName;
    192     enum { N = SkBENCHLOOP(300) };
    193 public:
    194     FilterBitmapBench(void* param, bool isOpaque, SkBitmap::Config c,
    195                 bool forceUpdate = false, bool bitmapVolatile = false,
    196                 int tx = -1, int ty = -1, bool addScale = false,
    197                 bool addRotate = false, bool addFilter = false)
    198         : INHERITED(param, isOpaque, c, forceUpdate, bitmapVolatile, tx, ty)
    199         , fScale(addScale), fRotate(addRotate), fFilter(addFilter) {
    200 
    201     }
    202 
    203 protected:
    204     virtual const char* onGetName() {
    205         fFullName.set(INHERITED::onGetName());
    206         if (fScale)
    207             fFullName.append("_scale");
    208         if (fRotate)
    209             fFullName.append("_rotate");
    210         if (fFilter)
    211             fFullName.append("_filter");
    212 
    213         return fFullName.c_str();
    214     }
    215 
    216     virtual void onDraw(SkCanvas* canvas) {
    217         SkISize dim = canvas->getDeviceSize();
    218         if (fScale) {
    219             const SkScalar x = SkIntToScalar(dim.fWidth) / 2;
    220             const SkScalar y = SkIntToScalar(dim.fHeight) / 2;
    221 
    222             canvas->translate(x, y);
    223             // just enough so we can't take the sprite case
    224             canvas->scale(SK_Scalar1 * 99/100, SK_Scalar1 * 99/100);
    225             canvas->translate(-x, -y);
    226         }
    227         if (fRotate) {
    228             const SkScalar x = SkIntToScalar(dim.fWidth) / 2;
    229             const SkScalar y = SkIntToScalar(dim.fHeight) / 2;
    230 
    231             canvas->translate(x, y);
    232             canvas->rotate(SkIntToScalar(35));
    233             canvas->translate(-x, -y);
    234         }
    235 
    236         this->setForceFilter(fFilter);
    237         INHERITED::onDraw(canvas);
    238     }
    239 
    240 private:
    241     typedef BitmapBench INHERITED;
    242 };
    243 
    244 static SkBenchmark* Fact0(void* p) { return new BitmapBench(p, false, SkBitmap::kARGB_8888_Config); }
    245 static SkBenchmark* Fact1(void* p) { return new BitmapBench(p, true, SkBitmap::kARGB_8888_Config); }
    246 static SkBenchmark* Fact2(void* p) { return new BitmapBench(p, true, SkBitmap::kRGB_565_Config); }
    247 static SkBenchmark* Fact3(void* p) { return new BitmapBench(p, false, SkBitmap::kARGB_4444_Config); }
    248 static SkBenchmark* Fact4(void* p) { return new BitmapBench(p, true, SkBitmap::kARGB_4444_Config); }
    249 static SkBenchmark* Fact5(void* p) { return new BitmapBench(p, false, SkBitmap::kIndex8_Config); }
    250 static SkBenchmark* Fact6(void* p) { return new BitmapBench(p, true, SkBitmap::kIndex8_Config); }
    251 static SkBenchmark* Fact7(void* p) { return new BitmapBench(p, true, SkBitmap::kARGB_8888_Config, true, true); }
    252 static SkBenchmark* Fact8(void* p) { return new BitmapBench(p, true, SkBitmap::kARGB_8888_Config, true, false); }
    253 
    254 // scale filter -> S32_opaque_D32_filter_DX_{SSE2,SSSE3} and Fact9 is also for S32_D16_filter_DX_SSE2
    255 static SkBenchmark* Fact9(void* p) { return new FilterBitmapBench(p, false, SkBitmap::kARGB_8888_Config, false, false, -1, -1, true, false, true); }
    256 static SkBenchmark* Fact10(void* p) { return new FilterBitmapBench(p, true, SkBitmap::kARGB_8888_Config, false, false, -1, -1, true, false, true); }
    257 static SkBenchmark* Fact11(void* p) { return new FilterBitmapBench(p, true, SkBitmap::kARGB_8888_Config, true, true, -1, -1, true, false, true); }
    258 static SkBenchmark* Fact12(void* p) { return new FilterBitmapBench(p, true, SkBitmap::kARGB_8888_Config, true, false, -1, -1, true, false, true); }
    259 
    260 // scale rotate filter -> S32_opaque_D32_filter_DXDY_{SSE2,SSSE3}
    261 static SkBenchmark* Fact13(void* p) { return new FilterBitmapBench(p, false, SkBitmap::kARGB_8888_Config, false, false, -1, -1, true, true, true); }
    262 static SkBenchmark* Fact14(void* p) { return new FilterBitmapBench(p, true, SkBitmap::kARGB_8888_Config, false, false, -1, -1, true, true, true); }
    263 static SkBenchmark* Fact15(void* p) { return new FilterBitmapBench(p, true, SkBitmap::kARGB_8888_Config, true, true, -1, -1, true, true, true); }
    264 static SkBenchmark* Fact16(void* p) { return new FilterBitmapBench(p, true, SkBitmap::kARGB_8888_Config, true, false, -1, -1, true, true, true); }
    265 
    266 static BenchRegistry gReg0(Fact0);
    267 static BenchRegistry gReg1(Fact1);
    268 static BenchRegistry gReg2(Fact2);
    269 static BenchRegistry gReg3(Fact3);
    270 static BenchRegistry gReg4(Fact4);
    271 static BenchRegistry gReg5(Fact5);
    272 static BenchRegistry gReg6(Fact6);
    273 static BenchRegistry gReg7(Fact7);
    274 static BenchRegistry gReg8(Fact8);
    275 
    276 static BenchRegistry gReg9(Fact9);
    277 static BenchRegistry gReg10(Fact10);
    278 static BenchRegistry gReg11(Fact11);
    279 static BenchRegistry gReg12(Fact12);
    280 
    281 static BenchRegistry gReg13(Fact13);
    282 static BenchRegistry gReg14(Fact14);
    283 static BenchRegistry gReg15(Fact15);
    284 static BenchRegistry gReg16(Fact16);
    285