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 static SkBenchmark* Fact0(void* p) { return new BitmapBench(p, false, SkBitmap::kARGB_8888_Config); }
    185 static SkBenchmark* Fact1(void* p) { return new BitmapBench(p, true, SkBitmap::kARGB_8888_Config); }
    186 static SkBenchmark* Fact2(void* p) { return new BitmapBench(p, true, SkBitmap::kRGB_565_Config); }
    187 static SkBenchmark* Fact3(void* p) { return new BitmapBench(p, false, SkBitmap::kARGB_4444_Config); }
    188 static SkBenchmark* Fact4(void* p) { return new BitmapBench(p, true, SkBitmap::kARGB_4444_Config); }
    189 static SkBenchmark* Fact5(void* p) { return new BitmapBench(p, false, SkBitmap::kIndex8_Config); }
    190 static SkBenchmark* Fact6(void* p) { return new BitmapBench(p, true, SkBitmap::kIndex8_Config); }
    191 static SkBenchmark* Fact7(void* p) { return new BitmapBench(p, true, SkBitmap::kARGB_8888_Config, true, true); }
    192 static SkBenchmark* Fact8(void* p) { return new BitmapBench(p, true, SkBitmap::kARGB_8888_Config, true, false); }
    193 
    194 static BenchRegistry gReg0(Fact0);
    195 static BenchRegistry gReg1(Fact1);
    196 static BenchRegistry gReg2(Fact2);
    197 static BenchRegistry gReg3(Fact3);
    198 static BenchRegistry gReg4(Fact4);
    199 static BenchRegistry gReg5(Fact5);
    200 static BenchRegistry gReg6(Fact6);
    201 static BenchRegistry gReg7(Fact7);
    202 static BenchRegistry gReg8(Fact8);
    203