• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2011 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/SkShader.h"
12 #include "include/core/SkString.h"
13 #include "include/core/SkVertices.h"
14 #include "include/utils/SkRandom.h"
15 
16 enum VertFlags {
17     kColors_VertFlag,
18     kTexture_VertFlag,
19 };
20 
21 class VertBench : public Benchmark {
22     SkString fName;
23     enum {
24         W = 640,
25         H = 480,
26         ROW = 20,
27         COL = 20,
28         PTS = (ROW + 1) * (COL + 1),
29         IDX = ROW * COL * 6,
30     };
31 
32     SkPoint fPts[PTS];
33     SkColor fColors[PTS];
34     uint16_t fIdx[IDX];
35 
load_2_tris(uint16_t idx[],int x,int y,int rb)36     static void load_2_tris(uint16_t idx[], int x, int y, int rb) {
37         int n = y * rb + x;
38         idx[0] = n; idx[1] = n + 1; idx[2] = rb + n + 1;
39         idx[3] = n; idx[4] = rb + n + 1; idx[5] = n + rb;
40     }
41 
42 public:
VertBench()43     VertBench() {
44         const SkScalar dx = SkIntToScalar(W) / COL;
45         const SkScalar dy = SkIntToScalar(H) / COL;
46 
47         SkPoint* pts = fPts;
48         uint16_t* idx = fIdx;
49 
50         SkScalar yy = 0;
51         for (int y = 0; y <= ROW; y++) {
52             SkScalar xx = 0;
53             for (int x = 0; x <= COL; ++x) {
54                 pts->set(xx, yy);
55                 pts += 1;
56                 xx += dx;
57 
58                 if (x < COL && y < ROW) {
59                     load_2_tris(idx, x, y, COL + 1);
60                     for (int i = 0; i < 6; i++) {
61                         SkASSERT(idx[i] < PTS);
62                     }
63                     idx += 6;
64                 }
65             }
66             yy += dy;
67         }
68         SkASSERT(PTS == pts - fPts);
69         SkASSERT(IDX == idx - fIdx);
70 
71         SkRandom rand;
72         for (int i = 0; i < PTS; ++i) {
73             fColors[i] = rand.nextU() | (0xFF << 24);
74         }
75 
76         fName.set("verts");
77     }
78 
79 protected:
onGetName()80     const char* onGetName() override { return fName.c_str(); }
onDraw(int loops,SkCanvas * canvas)81     void onDraw(int loops, SkCanvas* canvas) override {
82         SkPaint paint;
83         this->setupPaint(&paint);
84 
85         auto verts = SkVertices::MakeCopy(SkVertices::kTriangles_VertexMode, PTS,
86                                           fPts, nullptr, fColors, IDX, fIdx);
87         for (int i = 0; i < loops; i++) {
88             canvas->drawVertices(verts, SkBlendMode::kModulate, paint);
89         }
90     }
91 private:
92     typedef Benchmark INHERITED;
93 };
94 DEF_BENCH(return new VertBench();)
95 
96 /////////////////////////////////////////////////////////////////////////////////////////////////
97 
98 #include "include/core/SkRSXform.h"
99 #include "include/utils/SkRandom.h"
100 #include "tools/Resources.h"
101 
102 enum AtlasFlags {
103     kColors_Flag = 1 << 0,
104     kRotate_Flag = 1 << 1,
105 };
106 
107 class AtlasBench : public Benchmark {
108     unsigned fFlags;
109     SkString fName;
110     enum {
111         W = 640,
112         H = 480,
113         N = 10*1000,
114     };
115 
116     sk_sp<SkImage>  fAtlas;
117     SkRSXform       fXforms[N];
118     SkRect          fRects[N];
119     SkColor         fColors[N];
120 
121 public:
AtlasBench(unsigned flags)122     AtlasBench(unsigned flags) : fFlags(flags) {
123         fName.printf("drawAtlas");
124         if (flags & kColors_Flag) {
125             fName.append("_colors");
126         }
127         if (flags & kRotate_Flag) {
128             fName.append("_rotated");
129         }
130     }
~AtlasBench()131     ~AtlasBench() override {}
132 
133 protected:
onGetName()134     const char* onGetName() override { return fName.c_str(); }
onDelayedSetup()135     void onDelayedSetup() override {
136         fAtlas = GetResourceAsImage("images/mandrill_256.png");
137         if (fAtlas) {
138             fAtlas = fAtlas->makeRasterImage();
139         }
140 
141         const SkScalar imageW = fAtlas->width();
142         const SkScalar imageH = fAtlas->height();
143         SkScalar scos = 1;
144         SkScalar ssin = 0;
145         if (fFlags & kRotate_Flag) {
146             scos = 0.866025403784439f;  // sqrt(3)/2
147             ssin = 0.5f;
148         }
149 
150         SkRandom rand;
151         for (int i = 0; i < N; ++i) {
152             fRects[i] = SkRect::MakeXYWH(rand.nextF() * (imageW - 8),
153                                          rand.nextF() * (imageH - 8), 8, 8);
154             fColors[i] = rand.nextU() | 0xFF000000;
155             fXforms[i] = SkRSXform::Make(scos, ssin, rand.nextF() * W, rand.nextF() * H);
156         }
157     }
onDraw(int loops,SkCanvas * canvas)158     void onDraw(int loops, SkCanvas* canvas) override {
159         const SkRect* cullRect = nullptr;
160         const SkPaint* paintPtr = nullptr;
161         const SkColor* colors = nullptr;
162         if (fFlags & kColors_Flag) {
163             colors = fColors;
164         }
165         for (int i = 0; i < loops; i++) {
166             canvas->drawAtlas(fAtlas, fXforms, fRects, colors, N, SkBlendMode::kModulate,
167                               cullRect, paintPtr);
168         }
169     }
170 private:
171     typedef Benchmark INHERITED;
172 };
173 //DEF_BENCH(return new AtlasBench(0);)
174 //DEF_BENCH(return new AtlasBench(kColors_Flag);)
175 DEF_BENCH(return new AtlasBench(0);)
176 DEF_BENCH(return new AtlasBench(kRotate_Flag);)
177 DEF_BENCH(return new AtlasBench(kColors_Flag);)
178 DEF_BENCH(return new AtlasBench(kColors_Flag | kRotate_Flag);)
179 
180