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 "SkImageSource.h"
     11 #include "SkMergeImageFilter.h"
     12 #include "SkSurface.h"
     13 
     14 #define FILTER_WIDTH_SMALL  SkIntToScalar(32)
     15 #define FILTER_HEIGHT_SMALL SkIntToScalar(32)
     16 #define FILTER_WIDTH_LARGE  SkIntToScalar(256)
     17 #define FILTER_HEIGHT_LARGE SkIntToScalar(256)
     18 
     19 class MergeBench : public Benchmark {
     20 public:
     21     MergeBench(bool small) : fIsSmall(small), fInitialized(false) { }
     22 
     23 protected:
     24     const char* onGetName() override {
     25         return fIsSmall ? "merge_small" : "merge_large";
     26     }
     27 
     28     void onDelayedSetup() override {
     29         if (!fInitialized) {
     30             make_bitmap();
     31             make_checkerboard();
     32             fInitialized = true;
     33         }
     34     }
     35 
     36     void onDraw(int loops, SkCanvas* canvas) override {
     37         SkRect r = fIsSmall ? SkRect::MakeWH(FILTER_WIDTH_SMALL, FILTER_HEIGHT_SMALL) :
     38                               SkRect::MakeWH(FILTER_WIDTH_LARGE, FILTER_HEIGHT_LARGE);
     39         SkPaint paint;
     40         paint.setImageFilter(mergeBitmaps())->unref();
     41         for (int i = 0; i < loops; i++) {
     42             canvas->drawRect(r, paint);
     43         }
     44     }
     45 
     46 private:
     47     SkImageFilter* mergeBitmaps() {
     48         SkAutoTUnref<SkImageFilter> first(SkImageSource::Create(fCheckerboard));
     49         SkAutoTUnref<SkImageFilter> second(SkImageSource::Create(fImage));
     50         return SkMergeImageFilter::Create(first, second);
     51     }
     52 
     53     void make_bitmap() {
     54         SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterN32Premul(80, 80));
     55         surface->getCanvas()->clear(0x00000000);
     56         SkPaint paint;
     57         paint.setAntiAlias(true);
     58         paint.setColor(0xFF884422);
     59         paint.setTextSize(SkIntToScalar(96));
     60         const char* str = "g";
     61         surface->getCanvas()->drawText(str, strlen(str), 15, 55, paint);
     62         fImage.reset(surface->newImageSnapshot());
     63     }
     64 
     65     void make_checkerboard() {
     66         SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterN32Premul(80, 80));
     67         SkCanvas* canvas = surface->getCanvas();
     68         canvas->clear(0x00000000);
     69         SkPaint darkPaint;
     70         darkPaint.setColor(0xFF804020);
     71         SkPaint lightPaint;
     72         lightPaint.setColor(0xFF244484);
     73         for (int y = 0; y < 80; y += 16) {
     74             for (int x = 0; x < 80; x += 16) {
     75                 canvas->save();
     76                 canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
     77                 canvas->drawRect(SkRect::MakeXYWH(0, 0, 8, 8), darkPaint);
     78                 canvas->drawRect(SkRect::MakeXYWH(8, 0, 8, 8), lightPaint);
     79                 canvas->drawRect(SkRect::MakeXYWH(0, 8, 8, 8), lightPaint);
     80                 canvas->drawRect(SkRect::MakeXYWH(8, 8, 8, 8), darkPaint);
     81                 canvas->restore();
     82             }
     83         }
     84 
     85         fCheckerboard.reset(surface->newImageSnapshot());
     86     }
     87 
     88     bool fIsSmall;
     89     bool fInitialized;
     90     SkAutoTUnref<SkImage> fImage, fCheckerboard;
     91 
     92     typedef Benchmark INHERITED;
     93 };
     94 
     95 ///////////////////////////////////////////////////////////////////////////////
     96 
     97 DEF_BENCH( return new MergeBench(true); )
     98 DEF_BENCH( return new MergeBench(false); )
     99