1 /*
2  * Copyright 2013 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 "SkAutoPixmapStorage.h"
10 #include "SkBitmap.h"
11 #include "SkCanvas.h"
12 #include "SkColorPriv.h"
13 #include "SkDraw.h"
14 #include "SkMatrix.h"
15 #include "SkPath.h"
16 #include "SkRasterClip.h"
17 
18 class DrawPathBench : public Benchmark {
19     SkPaint     fPaint;
20     SkString    fName;
21     SkPath      fPath;
22     SkRasterClip fRC;
23     SkAutoPixmapStorage fPixmap;
24     SkMatrix    fIdentity;
25     SkDraw      fDraw;
26     bool        fDrawCoverage;
27 public:
DrawPathBench(bool drawCoverage)28     DrawPathBench(bool drawCoverage) : fDrawCoverage(drawCoverage) {
29         fPaint.setAntiAlias(true);
30         fName.printf("draw_coverage_%s", drawCoverage ? "true" : "false");
31 
32         fPath.moveTo(0, 0);
33         fPath.quadTo(500, 0, 500, 500);
34         fPath.quadTo(250, 0, 0, 500);
35 
36         fPixmap.alloc(SkImageInfo::MakeA8(500, 500));
37         if (!drawCoverage) {
38             // drawPathCoverage() goes out of its way to work fine with an uninitialized
39             // dst buffer, even in "SrcOver" mode, but ordinary drawing sure doesn't.
40             fPixmap.erase(0);
41         }
42 
43         fIdentity.setIdentity();
44         fRC.setRect(fPath.getBounds().round());
45 
46         fDraw.fDst      = fPixmap;
47         fDraw.fMatrix   = &fIdentity;
48         fDraw.fRC       = &fRC;
49     }
50 
51 protected:
onGetName()52     const char* onGetName() override {
53         return fName.c_str();
54     }
55 
onDraw(int loops,SkCanvas * canvas)56     void onDraw(int loops, SkCanvas* canvas) override {
57         if (fDrawCoverage) {
58             for (int i = 0; i < loops; ++i) {
59                 fDraw.drawPathCoverage(fPath, fPaint);
60             }
61         } else {
62             for (int i = 0; i < loops; ++i) {
63                 fDraw.drawPath(fPath, fPaint);
64             }
65         }
66     }
67 
68 private:
69     typedef Benchmark INHERITED;
70 };
71 
72 ///////////////////////////////////////////////////////////////////////////////
73 
74 DEF_BENCH( return new DrawPathBench(false) )
75 DEF_BENCH( return new DrawPathBench(true) )
76