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 #include "SkBenchmark.h"
      8 #include "SkBitmapDevice.h"
      9 #include "SkCanvas.h"
     10 #include "SkMagnifierImageFilter.h"
     11 #include "SkRandom.h"
     12 
     13 #define FILTER_WIDTH_SMALL  32
     14 #define FILTER_HEIGHT_SMALL 32
     15 #define FILTER_WIDTH_LARGE  256
     16 #define FILTER_HEIGHT_LARGE 256
     17 
     18 class MagnifierBench : public SkBenchmark {
     19 public:
     20     MagnifierBench(bool small) :
     21         fIsSmall(small), fInitialized(false) {
     22     }
     23 
     24 protected:
     25     virtual const char* onGetName() SK_OVERRIDE {
     26         return fIsSmall ? "magnifier_small" : "magnifier_large";
     27     }
     28 
     29     virtual void onPreDraw() SK_OVERRIDE {
     30         if (!fInitialized) {
     31             make_checkerboard();
     32             fInitialized = true;
     33         }
     34     }
     35 
     36     virtual void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE {
     37         const int w = fIsSmall ? FILTER_WIDTH_SMALL : FILTER_WIDTH_LARGE;
     38         const int h = fIsSmall ? FILTER_HEIGHT_SMALL : FILTER_HEIGHT_LARGE;
     39         SkPaint paint;
     40         paint.setImageFilter(
     41             new SkMagnifierImageFilter(
     42                 SkRect::MakeXYWH(SkIntToScalar(w / 4),
     43                                  SkIntToScalar(h / 4),
     44                                  SkIntToScalar(w / 2),
     45                                  SkIntToScalar(h / 2)), 100))->unref();
     46 
     47         for (int i = 0; i < loops; i++) {
     48             canvas->drawBitmap(fCheckerboard, 0, 0, &paint);
     49         }
     50     }
     51 
     52 private:
     53     void make_checkerboard() {
     54         const int w = fIsSmall ? FILTER_WIDTH_SMALL : FILTER_WIDTH_LARGE;
     55         const int h = fIsSmall ? FILTER_HEIGHT_LARGE : FILTER_HEIGHT_LARGE;
     56         fCheckerboard.setConfig(SkBitmap::kARGB_8888_Config, w, h);
     57         fCheckerboard.allocPixels();
     58         SkBitmapDevice device(fCheckerboard);
     59         SkCanvas canvas(&device);
     60         canvas.clear(0x00000000);
     61         SkPaint darkPaint;
     62         darkPaint.setColor(0xFF804020);
     63         SkPaint lightPaint;
     64         lightPaint.setColor(0xFF244484);
     65         for (int y = 0; y < h; y += 16) {
     66             for (int x = 0; x < w; x += 16) {
     67                 canvas.save();
     68                 canvas.translate(SkIntToScalar(x), SkIntToScalar(y));
     69                 canvas.drawRect(SkRect::MakeXYWH(0, 0, 8, 8), darkPaint);
     70                 canvas.drawRect(SkRect::MakeXYWH(8, 0, 8, 8), lightPaint);
     71                 canvas.drawRect(SkRect::MakeXYWH(0, 8, 8, 8), lightPaint);
     72                 canvas.drawRect(SkRect::MakeXYWH(8, 8, 8, 8), darkPaint);
     73                 canvas.restore();
     74             }
     75         }
     76     }
     77 
     78     bool fIsSmall;
     79     bool fInitialized;
     80     SkBitmap fCheckerboard;
     81     typedef SkBenchmark INHERITED;
     82 };
     83 
     84 ///////////////////////////////////////////////////////////////////////////////
     85 
     86 DEF_BENCH( return new MagnifierBench(true); )
     87 DEF_BENCH( return new MagnifierBench(false); )
     88