180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru/*
380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * Copyright 2011 Google Inc.
480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru *
580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * Use of this source code is governed by a BSD-style license that can be
680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * found in the LICENSE file.
780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru */
880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "SkBenchmark.h"
980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "SkCanvas.h"
1080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "SkColorShader.h"
1180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "SkFontHost.h"
1280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "SkPaint.h"
1380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "SkRandom.h"
1480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "SkString.h"
1580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "SkTemplates.h"
1680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
1780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#define STR     "Hamburgefons"
1880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
1980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruenum FontQuality {
2080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    kBW,
2180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    kAA,
2280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    kLCD
2380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru};
2480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
2580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querustatic const char* fontQualityName(const SkPaint& paint) {
2680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    if (!paint.isAntiAlias()) {
2780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        return "BW";
2880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
2980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    if (paint.isLCDRenderText()) {
3080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        return "LCD";
3180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
3280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    return "AA";
3380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
3480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
3580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruclass ShaderMaskBench : public SkBenchmark {
3680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SkPaint     fPaint;
3780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SkString    fText;
3880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SkString    fName;
3980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    FontQuality fFQ;
4080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    enum { N = SkBENCHLOOP(500) };
4180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querupublic:
4280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    ShaderMaskBench(void* param, bool isOpaque, FontQuality fq) : INHERITED(param) {
4380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        fFQ = fq;
4480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        fText.set(STR);
4580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
4680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        fPaint.setAntiAlias(kBW != fq);
4780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        fPaint.setLCDRenderText(kLCD == fq);
4880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        fPaint.setAlpha(isOpaque ? 0xFF : 0x80);
4980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        fPaint.setShader(new SkColorShader)->unref();
5080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
5180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
5280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruprotected:
5380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    virtual const char* onGetName() {
5480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        fName.printf("shadermask");
5580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        fName.appendf("_%s", fontQualityName(fPaint));
5680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        fName.appendf("_%02X", fPaint.getAlpha());
5780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        return fName.c_str();
5880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
5980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
6080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    virtual void onDraw(SkCanvas* canvas) {
6180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        const SkIPoint dim = this->getSize();
6280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        SkRandom rand;
6380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
6480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        SkPaint paint(fPaint);
6580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        this->setupPaint(&paint);
6680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        // explicitly need these
6780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        paint.setAlpha(fPaint.getAlpha());
6880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        paint.setAntiAlias(kBW != fFQ);
6980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        paint.setLCDRenderText(kLCD == fFQ);
7080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
7180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        const SkScalar x0 = SkIntToScalar(-10);
7280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        const SkScalar y0 = SkIntToScalar(-10);
7380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
7480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        paint.setTextSize(SkIntToScalar(12));
7580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        for (int i = 0; i < N; i++) {
7680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            SkScalar x = x0 + rand.nextUScalar1() * dim.fX;
7780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            SkScalar y = y0 + rand.nextUScalar1() * dim.fY;
7880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            canvas->drawText(fText.c_str(), fText.size(), x, y, paint);
7980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        }
8080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
8180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        paint.setTextSize(SkIntToScalar(48));
8280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        for (int i = 0; i < N/4; i++) {
8380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            SkScalar x = x0 + rand.nextUScalar1() * dim.fX;
8480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            SkScalar y = y0 + rand.nextUScalar1() * dim.fY;
8580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            canvas->drawText(fText.c_str(), fText.size(), x, y, paint);
8680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        }
8780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
8880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
8980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruprivate:
9080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    typedef SkBenchmark INHERITED;
9180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru};
9280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
9380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru///////////////////////////////////////////////////////////////////////////////
9480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
9580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querustatic SkBenchmark* Fact00(void* p) { return new ShaderMaskBench(p, true,  kBW); }
9680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querustatic SkBenchmark* Fact01(void* p) { return new ShaderMaskBench(p, false, kBW); }
9780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querustatic SkBenchmark* Fact10(void* p) { return new ShaderMaskBench(p, true,  kAA); }
9880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querustatic SkBenchmark* Fact11(void* p) { return new ShaderMaskBench(p, false, kAA); }
9980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querustatic SkBenchmark* Fact20(void* p) { return new ShaderMaskBench(p, true,  kLCD); }
10080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querustatic SkBenchmark* Fact21(void* p) { return new ShaderMaskBench(p, false, kLCD); }
10180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
10280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querustatic BenchRegistry gReg00(Fact00);
10380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querustatic BenchRegistry gReg01(Fact01);
10480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querustatic BenchRegistry gReg10(Fact10);
10580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querustatic BenchRegistry gReg11(Fact11);
10680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querustatic BenchRegistry gReg20(Fact20);
10780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querustatic BenchRegistry gReg21(Fact21);
108