SkRasterPipeline in SkArenaAlloc
[platform/upstream/libSkiaSharp.git] / bench / SkRasterPipelineBench.cpp
1 /*
2  * Copyright 2016 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 "SkOpts.h"
10 #include "SkRasterPipeline.h"
11
12 static const int N = 15;
13
14 static uint64_t dst[N];  // sRGB or F16
15 static uint32_t src[N];  // sRGB
16 static uint8_t mask[N];  // 8-bit linear
17
18 // We'll build up a somewhat realistic useful pipeline:
19 //   - load srgb src
20 //   - scale src by 8-bit mask
21 //   - load srgb/f16 dst
22 //   - src = srcover(dst, src)
23 //   - store src back as srgb/f16
24
25 template <bool kF16>
26 class SkRasterPipelineBench : public Benchmark {
27 public:
28     bool isSuitableFor(Backend backend) override { return backend == kNonRendering_Backend; }
29     const char* onGetName() override {
30         switch ((int)kF16) {
31             case 0: return "SkRasterPipeline_srgb";
32             case 1: return "SkRasterPipeline_f16";
33         }
34         return "whoops";
35     }
36
37     void onDraw(int loops, SkCanvas*) override {
38         void* mask_ctx = mask;
39         void*  src_ctx = src;
40         void*  dst_ctx = dst;
41
42         SkRasterPipeline_<256> p;
43         p.append(SkRasterPipeline::load_8888, &src_ctx);
44         p.append_from_srgb(kUnpremul_SkAlphaType);
45         p.append(SkRasterPipeline::scale_u8, &mask_ctx);
46         p.append(SkRasterPipeline::move_src_dst);
47         if (kF16) {
48             p.append(SkRasterPipeline::load_f16, &dst_ctx);
49         } else {
50             p.append(SkRasterPipeline::load_8888, &dst_ctx);
51             p.append_from_srgb(kPremul_SkAlphaType);
52         }
53         p.append(SkRasterPipeline::dstover);
54         if (kF16) {
55             p.append(SkRasterPipeline::store_f16, &dst_ctx);
56         } else {
57             p.append(SkRasterPipeline::to_srgb);
58             p.append(SkRasterPipeline::store_8888, &dst_ctx);
59         }
60
61         while (loops --> 0) {
62             p.run(0,N);
63         }
64     }
65 };
66 DEF_BENCH( return (new SkRasterPipelineBench< true>); )
67 DEF_BENCH( return (new SkRasterPipelineBench<false>); )
68
69 class SkRasterPipelineCompileVsRunBench : public Benchmark {
70 public:
71     explicit SkRasterPipelineCompileVsRunBench(bool compile) : fCompile(compile) {}
72     bool isSuitableFor(Backend backend) override { return backend == kNonRendering_Backend; }
73     const char* onGetName() override {
74         return fCompile ? "SkRasterPipeline_compile"
75                         : "SkRasterPipeline_run";
76     }
77
78     void onDraw(int loops, SkCanvas*) override {
79         void*  src_ctx = src;
80         void*  dst_ctx = dst;
81
82         SkRasterPipeline_<256> p;
83         p.append(SkRasterPipeline::load_8888, &dst_ctx);
84         p.append(SkRasterPipeline::move_src_dst);
85         p.append(SkRasterPipeline::load_8888, &src_ctx);
86         p.append(SkRasterPipeline::srcover);
87         p.append(SkRasterPipeline::store_8888, &dst_ctx);
88
89         if (fCompile) {
90             auto fn = p.compile();
91             while (loops --> 0) {
92                 fn(0,N);
93             }
94         } else {
95             while (loops --> 0) {
96                 p.run(0,N);
97             }
98         }
99     }
100 private:
101     bool fCompile;
102 };
103 DEF_BENCH( return (new SkRasterPipelineCompileVsRunBench(true )); )
104 DEF_BENCH( return (new SkRasterPipelineCompileVsRunBench(false)); )
105
106 static SkColorSpaceTransferFn gamma(float g) {
107     SkColorSpaceTransferFn fn = {0,0,0,0,0,0,0};
108     fn.fG = g;
109     fn.fA = 1;
110     return fn;
111 }
112
113 class SkRasterPipeline_2dot2 : public Benchmark {
114 public:
115     bool isSuitableFor(Backend backend) override { return backend == kNonRendering_Backend; }
116     const char* onGetName() override {
117         return "SkRasterPipeline_2dot2";
118     }
119
120     void onDraw(int loops, SkCanvas*) override {
121         SkColor4f c = { 1.0f, 1.0f, 1.0f, 1.0f };
122
123         SkColorSpaceTransferFn from_2dot2 = gamma(  2.2f),
124                                  to_2dot2 = gamma(1/2.2f);
125         SkRasterPipeline_<256> p;
126         p.append(SkRasterPipeline::constant_color, &c);
127         p.append(SkRasterPipeline::parametric_r, &from_2dot2);
128         p.append(SkRasterPipeline::parametric_g, &from_2dot2);
129         p.append(SkRasterPipeline::parametric_b, &from_2dot2);
130         p.append(SkRasterPipeline::parametric_r, &  to_2dot2);
131         p.append(SkRasterPipeline::parametric_g, &  to_2dot2);
132         p.append(SkRasterPipeline::parametric_b, &  to_2dot2);
133
134         while (loops --> 0) {
135             p.run(0,N);
136         }
137     }
138 };
139 DEF_BENCH( return (new SkRasterPipeline_2dot2); )
140
141 class SkRasterPipelineToSRGB : public Benchmark {
142 public:
143     bool isSuitableFor(Backend backend) override { return backend == kNonRendering_Backend; }
144     const char* onGetName() override {
145         return "SkRasterPipeline_to_srgb";
146     }
147
148     void onDraw(int loops, SkCanvas*) override {
149         SkRasterPipeline_<256> p;
150         p.append(SkRasterPipeline::to_srgb);
151
152         while (loops --> 0) {
153             p.run(0,N);
154         }
155     }
156 };
157 DEF_BENCH( return (new SkRasterPipelineToSRGB); )