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 "SkCanvas.h"
     10 #include "SkDisplacementMapEffect.h"
     11 #include "SkImageSource.h"
     12 #include "SkSurface.h"
     13 
     14 #define FILTER_WIDTH_SMALL  32
     15 #define FILTER_HEIGHT_SMALL 32
     16 #define FILTER_WIDTH_LARGE  256
     17 #define FILTER_HEIGHT_LARGE 256
     18 
     19 class DisplacementBaseBench : public Benchmark {
     20 public:
     21     DisplacementBaseBench(bool small) :
     22         fInitialized(false), fIsSmall(small) {
     23     }
     24 
     25 protected:
     26     void onDelayedSetup() override {
     27         if (!fInitialized) {
     28             this->makeBitmap();
     29             this->makeCheckerboard();
     30             fInitialized = true;
     31         }
     32     }
     33 
     34     void makeBitmap() {
     35         const int w = this->isSmall() ? FILTER_WIDTH_SMALL : FILTER_WIDTH_LARGE;
     36         const int h = this->isSmall() ? FILTER_HEIGHT_LARGE : FILTER_HEIGHT_LARGE;
     37         fBitmap.allocN32Pixels(w, h);
     38         SkCanvas canvas(fBitmap);
     39         canvas.clear(0x00000000);
     40         SkPaint paint;
     41         paint.setAntiAlias(true);
     42         paint.setColor(0xFF884422);
     43         paint.setTextSize(SkIntToScalar(96));
     44         const char* str = "g";
     45         canvas.drawText(str, strlen(str), SkIntToScalar(15), SkIntToScalar(55), paint);
     46     }
     47 
     48     void makeCheckerboard() {
     49         const int w = this->isSmall() ? FILTER_WIDTH_SMALL : FILTER_WIDTH_LARGE;
     50         const int h = this->isSmall() ? FILTER_HEIGHT_LARGE : FILTER_HEIGHT_LARGE;
     51         SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterN32Premul(w, h));
     52         SkCanvas* canvas = surface->getCanvas();
     53         canvas->clear(0x00000000);
     54         SkPaint darkPaint;
     55         darkPaint.setColor(0xFF804020);
     56         SkPaint lightPaint;
     57         lightPaint.setColor(0xFF244484);
     58         for (int y = 0; y < h; y += 16) {
     59             for (int x = 0; x < w; x += 16) {
     60                 canvas->save();
     61                 canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
     62                 canvas->drawRect(SkRect::MakeXYWH(0, 0, 8, 8), darkPaint);
     63                 canvas->drawRect(SkRect::MakeXYWH(8, 0, 8, 8), lightPaint);
     64                 canvas->drawRect(SkRect::MakeXYWH(0, 8, 8, 8), lightPaint);
     65                 canvas->drawRect(SkRect::MakeXYWH(8, 8, 8, 8), darkPaint);
     66                 canvas->restore();
     67             }
     68         }
     69 
     70         fCheckerboard.reset(surface->newImageSnapshot());
     71     }
     72 
     73     void drawClippedBitmap(SkCanvas* canvas, int x, int y, const SkPaint& paint) {
     74         canvas->save();
     75         canvas->clipRect(SkRect::MakeXYWH(SkIntToScalar(x), SkIntToScalar(y),
     76                                           SkIntToScalar(fBitmap.width()),
     77                                           SkIntToScalar(fBitmap.height())));
     78         canvas->drawBitmap(fBitmap, SkIntToScalar(x), SkIntToScalar(y), &paint);
     79         canvas->restore();
     80     }
     81 
     82     inline bool isSmall() const { return fIsSmall; }
     83 
     84     SkBitmap fBitmap;
     85     SkAutoTUnref<SkImage> fCheckerboard;
     86 
     87 private:
     88     bool fInitialized;
     89     bool fIsSmall;
     90     typedef Benchmark INHERITED;
     91 };
     92 
     93 class DisplacementZeroBench : public DisplacementBaseBench {
     94 public:
     95     DisplacementZeroBench(bool small) : INHERITED(small) {
     96     }
     97 
     98 protected:
     99     const char* onGetName() override {
    100         return this->isSmall() ? "displacement_zero_small" : "displacement_zero_large";
    101     }
    102 
    103     void onDraw(int loops, SkCanvas* canvas) override {
    104         SkPaint paint;
    105         SkAutoTUnref<SkImageFilter> displ(SkImageSource::Create(fCheckerboard));
    106         // No displacement effect
    107         paint.setImageFilter(SkDisplacementMapEffect::Create(
    108             SkDisplacementMapEffect::kR_ChannelSelectorType,
    109             SkDisplacementMapEffect::kG_ChannelSelectorType, 0.0f, displ))->unref();
    110 
    111         for (int i = 0; i < loops; i++) {
    112             this->drawClippedBitmap(canvas, 0, 0, paint);
    113         }
    114     }
    115 
    116 private:
    117     typedef DisplacementBaseBench INHERITED;
    118 };
    119 
    120 class DisplacementAlphaBench : public DisplacementBaseBench {
    121 public:
    122     DisplacementAlphaBench(bool small) : INHERITED(small) {
    123     }
    124 
    125 protected:
    126     const char* onGetName() override {
    127         return isSmall() ? "displacement_alpha_small" : "displacement_alpha_large";
    128     }
    129 
    130     void onDraw(int loops, SkCanvas* canvas) override {
    131         SkPaint paint;
    132         SkAutoTUnref<SkImageFilter> displ(SkImageSource::Create(fCheckerboard));
    133         // Displacement, with 1 alpha component (which isn't pre-multiplied)
    134         paint.setImageFilter(SkDisplacementMapEffect::Create(
    135             SkDisplacementMapEffect::kB_ChannelSelectorType,
    136             SkDisplacementMapEffect::kA_ChannelSelectorType, 16.0f, displ))->unref();
    137         for (int i = 0; i < loops; i++) {
    138             drawClippedBitmap(canvas, 100, 0, paint);
    139         }
    140     }
    141 
    142 private:
    143     typedef DisplacementBaseBench INHERITED;
    144 };
    145 
    146 class DisplacementFullBench : public DisplacementBaseBench {
    147 public:
    148     DisplacementFullBench(bool small) : INHERITED(small) {
    149     }
    150 
    151 protected:
    152     const char* onGetName() override {
    153         return isSmall() ? "displacement_full_small" : "displacement_full_large";
    154     }
    155 
    156     void onDraw(int loops, SkCanvas* canvas) override {
    157         SkPaint paint;
    158         SkAutoTUnref<SkImageFilter> displ(SkImageSource::Create(fCheckerboard));
    159         // Displacement, with 2 non-alpha components
    160         paint.setImageFilter(SkDisplacementMapEffect::Create(
    161             SkDisplacementMapEffect::kR_ChannelSelectorType,
    162             SkDisplacementMapEffect::kB_ChannelSelectorType, 32.0f, displ))->unref();
    163         for (int i = 0; i < loops; ++i) {
    164             this->drawClippedBitmap(canvas, 200, 0, paint);
    165         }
    166     }
    167 
    168 private:
    169     typedef DisplacementBaseBench INHERITED;
    170 };
    171 
    172 ///////////////////////////////////////////////////////////////////////////////
    173 
    174 DEF_BENCH( return new DisplacementZeroBench(true); )
    175 DEF_BENCH( return new DisplacementAlphaBench(true); )
    176 DEF_BENCH( return new DisplacementFullBench(true); )
    177 DEF_BENCH( return new DisplacementZeroBench(false); )
    178 DEF_BENCH( return new DisplacementAlphaBench(false); )
    179 DEF_BENCH( return new DisplacementFullBench(false); )
    180