• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2016 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 "bench/Benchmark.h"
9 #include "include/core/SkCanvas.h"
10 #include "include/core/SkPaint.h"
11 #include "include/core/SkString.h"
12 #include "include/effects/SkImageFilters.h"
13 
14 #define WIDTH 512
15 #define HEIGHT 512
16 
17 // This bench exercises SkTileImageFilter drawn from a 50x50 source to
18 // a 512x512 destination. It is drawn using a single rect, or "tiled"
19 // rendering (using 32x32, 64x64 tiles). Tiled rendering is currently an order
20 // of magnitude slower, since SkTileImageFilter does not clip the
21 // source or destination rects.
22 
23 class TileImageFilterBench : public Benchmark {
24 public:
TileImageFilterBench(int tileSize)25     TileImageFilterBench(int tileSize) : fTileSize(tileSize) {
26         if (tileSize > 0) {
27             fName.printf("tile_image_filter_tiled_%d", tileSize);
28         } else {
29             fName.printf("tile_image_filter");
30         }
31     }
32 
33 protected:
onGetName()34     const char* onGetName() override {
35         return fName.c_str();
36     }
37 
onDraw(int loops,SkCanvas * canvas)38     void onDraw(int loops, SkCanvas* canvas) override {
39         SkPaint paint;
40         paint.setImageFilter(SkImageFilters::Tile(
41                 SkRect::MakeWH(50, 50), SkRect::MakeWH(WIDTH, HEIGHT), nullptr));
42 
43         for (int i = 0; i < loops; i++) {
44             if (fTileSize > 0) {
45                 for (int y = 0; y < HEIGHT; y += fTileSize) {
46                     for (int x = 0; x < WIDTH; x += fTileSize) {
47                         canvas->save();
48                         SkIRect clipIRect = SkIRect::MakeXYWH(x, y, fTileSize, fTileSize);
49                         canvas->clipRect(SkRect::Make(clipIRect));
50                         canvas->drawRect(SkRect::MakeWH(WIDTH, HEIGHT), paint);
51                         canvas->restore();
52                     }
53                 }
54             } else {
55                 canvas->drawRect(SkRect::MakeWH(WIDTH, HEIGHT), paint);
56             }
57         }
58     }
59 
60 private:
61     SkString fName;
62     // Note: this is the tile size used for tiled rendering, not for the size
63     // of the SkTileImageFilter source rect.
64     int fTileSize;
65     using INHERITED = Benchmark;
66 };
67 
68 DEF_BENCH(return new TileImageFilterBench(0);)
69 DEF_BENCH(return new TileImageFilterBench(32);)
70 DEF_BENCH(return new TileImageFilterBench(64);)
71