180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru/*
280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * Copyright 2012 Google Inc.
380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru *
480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * Use of this source code is governed by a BSD-style license that can be
580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * found in the LICENSE file.
680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru */
780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "SkBenchmark.h"
980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "SkCanvas.h"
1080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "SkRect.h"
1180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
1280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querustatic const SkScalar kCellWidth = SkIntToScalar(20);
1380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querustatic const SkScalar kCellHeight = SkIntToScalar(10);
1480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
1580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru// This bench draws a table in the manner of Google spreadsheet and sahadan.com.
1680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru//           ____________ ___
1780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru//          |     1      | 2 |
1880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru//          |____________|___|
1980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru//          |     3      | 4 |
2080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru//          |____________|___|
2180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru//
2280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru// Areas 1-4 are first all draw white. Areas 3&4 are then drawn grey. Areas
2380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru// 2&4 are then drawn grey. Areas 2&3 are thus double drawn while area 4 is
2480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru// triple drawn.
2580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru// This trio of drawRects is then repeat for the next cell.
2680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruclass TableBench : public SkBenchmark {
2780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querupublic:
2880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
2980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    static const int kNumIterations = SkBENCHLOOP(10);
3080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    static const int kNumRows = 48;
3180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    static const int kNumCols = 32;
3280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
3380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    TableBench(void* param)
3480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        : INHERITED(param) {
3580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
3680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
3780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruprotected:
3880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    virtual const char* onGetName() {
3980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        return "tablebench";
4080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
4180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
4280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    virtual void onDraw(SkCanvas* canvas) {
4380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
4480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        SkPaint cellPaint;
4580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        cellPaint.setColor(0xFFFFFFF);
4680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
4780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        SkPaint borderPaint;
4880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        borderPaint.setColor(0xFFCCCCCC);
4980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
5080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        for (int i = 0; i < kNumIterations; ++i) {
5180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            for (int row = 0; row < kNumRows; ++row) {
5280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                for (int col = 0; col < kNumCols; ++col) {
5380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                    SkRect cell = SkRect::MakeLTRB(col * kCellWidth,
5480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                                                   row * kCellHeight,
5580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                                                   (col+1) * kCellWidth,
5680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                                                   (row+1) * kCellHeight);
5780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                    canvas->drawRect(cell, cellPaint);
5880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
5980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                    SkRect bottom = SkRect::MakeLTRB(col * kCellWidth,
6080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                                                     row * kCellHeight + (kCellHeight-SK_Scalar1),
6180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                                                     (col+1) * kCellWidth,
6280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                                                     (row+1) * kCellHeight);
6380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                    canvas->drawRect(bottom, borderPaint);
6480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
6580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                    SkRect right = SkRect::MakeLTRB(col * kCellWidth + (kCellWidth-SK_Scalar1),
6680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                                                    row * kCellHeight,
6780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                                                    (col+1) * kCellWidth,
6880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                                                    (row+1) * kCellHeight);
6980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                    canvas->drawRect(right, borderPaint);
7080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                }
7180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            }
7280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        }
7380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
7480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
7580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruprivate:
7680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    typedef SkBenchmark INHERITED;
7780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru};
7880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
7980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querustatic SkBenchmark* gFactory(void* p) { return new TableBench(p); }
8080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
8180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querustatic BenchRegistry gRegistry(gFactory);
82