1cb93a386Sopenharmony_ci/*
2cb93a386Sopenharmony_ci * Copyright 2012 Google Inc.
3cb93a386Sopenharmony_ci *
4cb93a386Sopenharmony_ci * Use of this source code is governed by a BSD-style license that can be
5cb93a386Sopenharmony_ci * found in the LICENSE file.
6cb93a386Sopenharmony_ci */
7cb93a386Sopenharmony_ci
8cb93a386Sopenharmony_ci#include "bench/Benchmark.h"
9cb93a386Sopenharmony_ci#include "include/core/SkCanvas.h"
10cb93a386Sopenharmony_ci#include "include/core/SkRect.h"
11cb93a386Sopenharmony_ci
12cb93a386Sopenharmony_cistatic const SkScalar kCellWidth = SkIntToScalar(20);
13cb93a386Sopenharmony_cistatic const SkScalar kCellHeight = SkIntToScalar(10);
14cb93a386Sopenharmony_ci
15cb93a386Sopenharmony_ci// This bench draws a table in the manner of Google spreadsheet and sahadan.com.
16cb93a386Sopenharmony_ci//           ____________ ___
17cb93a386Sopenharmony_ci//          |     1      | 2 |
18cb93a386Sopenharmony_ci//          |____________|___|
19cb93a386Sopenharmony_ci//          |     3      | 4 |
20cb93a386Sopenharmony_ci//          |____________|___|
21cb93a386Sopenharmony_ci//
22cb93a386Sopenharmony_ci// Areas 1-4 are first all draw white. Areas 3&4 are then drawn grey. Areas
23cb93a386Sopenharmony_ci// 2&4 are then drawn grey. Areas 2&3 are thus double drawn while area 4 is
24cb93a386Sopenharmony_ci// triple drawn.
25cb93a386Sopenharmony_ci// This trio of drawRects is then repeat for the next cell.
26cb93a386Sopenharmony_ciclass TableBench : public Benchmark {
27cb93a386Sopenharmony_cipublic:
28cb93a386Sopenharmony_ci    static const int kNumRows = 48;
29cb93a386Sopenharmony_ci    static const int kNumCols = 32;
30cb93a386Sopenharmony_ci
31cb93a386Sopenharmony_ciprotected:
32cb93a386Sopenharmony_ci    const char* onGetName() override {
33cb93a386Sopenharmony_ci        return "tablebench";
34cb93a386Sopenharmony_ci    }
35cb93a386Sopenharmony_ci
36cb93a386Sopenharmony_ci    void onDraw(int loops, SkCanvas* canvas) override {
37cb93a386Sopenharmony_ci        SkPaint cellPaint;
38cb93a386Sopenharmony_ci        cellPaint.setColor(0xFFFFFFF);
39cb93a386Sopenharmony_ci
40cb93a386Sopenharmony_ci        SkPaint borderPaint;
41cb93a386Sopenharmony_ci        borderPaint.setColor(0xFFCCCCCC);
42cb93a386Sopenharmony_ci
43cb93a386Sopenharmony_ci        for (int i = 0; i < loops; ++i) {
44cb93a386Sopenharmony_ci            for (int row = 0; row < kNumRows; ++row) {
45cb93a386Sopenharmony_ci                for (int col = 0; col < kNumCols; ++col) {
46cb93a386Sopenharmony_ci                    SkRect cell = SkRect::MakeLTRB(col * kCellWidth,
47cb93a386Sopenharmony_ci                                                   row * kCellHeight,
48cb93a386Sopenharmony_ci                                                   (col+1) * kCellWidth,
49cb93a386Sopenharmony_ci                                                   (row+1) * kCellHeight);
50cb93a386Sopenharmony_ci                    canvas->drawRect(cell, cellPaint);
51cb93a386Sopenharmony_ci
52cb93a386Sopenharmony_ci                    SkRect bottom = SkRect::MakeLTRB(col * kCellWidth,
53cb93a386Sopenharmony_ci                                                     row * kCellHeight + (kCellHeight-SK_Scalar1),
54cb93a386Sopenharmony_ci                                                     (col+1) * kCellWidth,
55cb93a386Sopenharmony_ci                                                     (row+1) * kCellHeight);
56cb93a386Sopenharmony_ci                    canvas->drawRect(bottom, borderPaint);
57cb93a386Sopenharmony_ci
58cb93a386Sopenharmony_ci                    SkRect right = SkRect::MakeLTRB(col * kCellWidth + (kCellWidth-SK_Scalar1),
59cb93a386Sopenharmony_ci                                                    row * kCellHeight,
60cb93a386Sopenharmony_ci                                                    (col+1) * kCellWidth,
61cb93a386Sopenharmony_ci                                                    (row+1) * kCellHeight);
62cb93a386Sopenharmony_ci                    canvas->drawRect(right, borderPaint);
63cb93a386Sopenharmony_ci                }
64cb93a386Sopenharmony_ci            }
65cb93a386Sopenharmony_ci        }
66cb93a386Sopenharmony_ci    }
67cb93a386Sopenharmony_ci
68cb93a386Sopenharmony_ciprivate:
69cb93a386Sopenharmony_ci    using INHERITED = Benchmark;
70cb93a386Sopenharmony_ci};
71cb93a386Sopenharmony_ci
72cb93a386Sopenharmony_ciDEF_BENCH( return new TableBench(); )
73