Home | History | Annotate | Download | only in bench
      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 "Benchmark.h"
      9 #include "SkCanvas.h"
     10 #include "SkPaint.h"
     11 #include "SkRandom.h"
     12 #include "SkShader.h"
     13 #include "SkString.h"
     14 #include "SkVertices.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 
     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:
     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:
     80     const char* onGetName() override { return fName.c_str(); }
     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 
     95 ///////////////////////////////////////////////////////////////////////////////
     96 
     97 DEF_BENCH(return new VertBench();)
     98