blob: d7ba2bedc2308b6f07fcf5754ce4bce21e0f0aae [file] [log] [blame]
herbcc49e592016-05-17 09:57:34 -07001/*
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 <tuple>
9
10#include "Benchmark.h"
11#include "Resources.h"
12#include "SkCpu.h"
13#include "SkImage.h"
14#include "SkImage_Base.h"
15#include "SkNx.h"
16#include "SkOpts.h"
herb2edf0c62016-07-12 15:00:46 -070017#include "SkPM4fPriv.h"
herbcc49e592016-05-17 09:57:34 -070018#include "SkString.h"
19
20#define INNER_LOOPS 10
21
mtklein0c902472016-07-20 18:10:07 -070022static inline void brute_srcover_srgb_srgb_1(uint32_t* dst, uint32_t src) {
23 auto d = Sk4f_fromS32(*dst),
24 s = Sk4f_fromS32( src);
25 *dst = Sk4f_toS32(s + d * (1.0f - s[3]));
26}
27
28static inline void srcover_srgb_srgb_1(uint32_t* dst, uint32_t src) {
29 if (src >= 0xFF000000) {
30 *dst = src;
31 return;
32 }
33 brute_srcover_srgb_srgb_1(dst, src);
34}
35
herb2edf0c62016-07-12 15:00:46 -070036static void brute_force_srcover_srgb_srgb(
37 uint32_t* dst, const uint32_t* const src, int ndst, const int nsrc) {
38 while (ndst > 0) {
39 int n = SkTMin(ndst, nsrc);
40
41 for (int i = 0; i < n; i++) {
mtklein0c902472016-07-20 18:10:07 -070042 brute_srcover_srgb_srgb_1(dst++, src[i]);
43 }
44 ndst -= n;
45 }
46}
47
48static void trivial_srcover_srgb_srgb(
49 uint32_t* dst, const uint32_t* const src, int ndst, const int nsrc) {
50 while (ndst > 0) {
51 int n = SkTMin(ndst, nsrc);
52
53 for (int i = 0; i < n; i++) {
54 srcover_srgb_srgb_1(dst++, src[i]);
herb2edf0c62016-07-12 15:00:46 -070055 }
56 ndst -= n;
57 }
58}
59
60static void best_non_simd_srcover_srgb_srgb(
61 uint32_t* dst, const uint32_t* const src, int ndst, const int nsrc) {
62 uint64_t* ddst = reinterpret_cast<uint64_t*>(dst);
63
64 auto srcover_srgb_srgb_2 = [](uint32_t* dst, const uint32_t* src) {
mtklein0c902472016-07-20 18:10:07 -070065 srcover_srgb_srgb_1(dst++, *src++);
66 srcover_srgb_srgb_1(dst, *src);
herb2edf0c62016-07-12 15:00:46 -070067 };
68
69 while (ndst >0) {
70 int count = SkTMin(ndst, nsrc);
71 ndst -= count;
72 const uint64_t* dsrc = reinterpret_cast<const uint64_t*>(src);
73 const uint64_t* end = dsrc + (count >> 1);
74 do {
75 if ((~*dsrc & 0xFF000000FF000000) == 0) {
76 do {
77 *ddst++ = *dsrc++;
78 } while (dsrc < end && (~*dsrc & 0xFF000000FF000000) == 0);
79 } else if ((*dsrc & 0xFF000000FF000000) == 0) {
80 do {
81 dsrc++;
82 ddst++;
83 } while (dsrc < end && (*dsrc & 0xFF000000FF000000) == 0);
84 } else {
85 srcover_srgb_srgb_2(reinterpret_cast<uint32_t*>(ddst++),
86 reinterpret_cast<const uint32_t*>(dsrc++));
87 }
88 } while (dsrc < end);
89
90 if ((count & 1) != 0) {
Mike Klein849f5022016-09-26 13:19:12 -040091 uint32_t s1;
92 memcpy(&s1, dsrc, 4);
93 srcover_srgb_srgb_1(reinterpret_cast<uint32_t*>(ddst), s1);
herb2edf0c62016-07-12 15:00:46 -070094 }
95 }
96}
97
herbcc49e592016-05-17 09:57:34 -070098class SrcOverVSkOptsBruteForce {
99public:
100 static SkString Name() { return SkString{"VSkOptsBruteForce"}; }
herb2edf0c62016-07-12 15:00:46 -0700101 static void BlendN(uint32_t* dst, const uint32_t* src, int count) {
102 brute_force_srcover_srgb_srgb(dst, src, count, count);
herbcc49e592016-05-17 09:57:34 -0700103 }
104};
105
herbcc49e592016-05-17 09:57:34 -0700106class SrcOverVSkOptsTrivial {
107public:
108 static SkString Name() { return SkString{"VSkOptsTrivial"}; }
herb2edf0c62016-07-12 15:00:46 -0700109 static void BlendN(uint32_t* dst, const uint32_t* src, int count) {
110 trivial_srcover_srgb_srgb(dst, src, count, count);
herbcc49e592016-05-17 09:57:34 -0700111 }
112};
113
herbcc49e592016-05-17 09:57:34 -0700114class SrcOverVSkOptsNonSimdCore {
115public:
116 static SkString Name() { return SkString{"VSkOptsNonSimdCore"}; }
herb2edf0c62016-07-12 15:00:46 -0700117 static void BlendN(uint32_t* dst, const uint32_t* src, int count) {
118 best_non_simd_srcover_srgb_srgb(dst, src, count, count);
herbcc49e592016-05-17 09:57:34 -0700119 }
120};
121
herbcc49e592016-05-17 09:57:34 -0700122class SrcOverVSkOptsDefault {
123public:
124 static SkString Name() { return SkString{"VSkOptsDefault"}; }
herb2edf0c62016-07-12 15:00:46 -0700125 static void BlendN(uint32_t* dst, const uint32_t* src, int count) {
mtklein0358a6a2016-07-13 08:02:20 -0700126 SkOpts::srcover_srgb_srgb(dst, src, count, count);
herbcc49e592016-05-17 09:57:34 -0700127 }
128};
129
130///////////////////////////////////////////////////////////////////////////////////////////////////
131
132template <typename Blender>
133class LinearSrcOverBench : public Benchmark {
134public:
bsalomon809f2582016-05-20 08:54:23 -0700135 LinearSrcOverBench(const char* fileName) : fFileName(fileName) {
herb2edf0c62016-07-12 15:00:46 -0700136 fName = "LinearSrcOver_";
herbcc49e592016-05-17 09:57:34 -0700137 fName.append(fileName);
138 fName.append(Blender::Name());
herbcc49e592016-05-17 09:57:34 -0700139 }
140
141protected:
mtklein0358a6a2016-07-13 08:02:20 -0700142 bool isSuitableFor(Backend backend) override { return backend == kNonRendering_Backend; }
herbcc49e592016-05-17 09:57:34 -0700143 const char* onGetName() override { return fName.c_str(); }
bsalomon809f2582016-05-20 08:54:23 -0700144
145 void onPreDraw(SkCanvas*) override {
146 if (!fPixmap.addr()) {
147 sk_sp<SkImage> image = GetResourceAsImage(fFileName.c_str());
148 SkBitmap bm;
Brian Osman61624f02016-12-09 14:51:59 -0500149 SkColorSpace* legacyColorSpace = nullptr;
150 if (!as_IB(image)->getROPixels(&bm, legacyColorSpace)) {
bsalomon809f2582016-05-20 08:54:23 -0700151 SkFAIL("Could not read resource");
152 }
153 bm.peekPixels(&fPixmap);
154 fCount = fPixmap.rowBytesAsPixels();
155 fDst.reset(fCount);
herb2edf0c62016-07-12 15:00:46 -0700156 sk_bzero(fDst.get(), fPixmap.rowBytes());
bsalomon809f2582016-05-20 08:54:23 -0700157 }
158 }
159
herbcc49e592016-05-17 09:57:34 -0700160 void onDraw(int loops, SkCanvas*) override {
161 SkASSERT(fPixmap.colorType() == kN32_SkColorType);
162
163 const int width = fPixmap.rowBytesAsPixels();
164
165 for (int i = 0; i < loops * INNER_LOOPS; ++i) {
166 const uint32_t* src = fPixmap.addr32();
167 for (int y = 0; y < fPixmap.height(); y++) {
herb2edf0c62016-07-12 15:00:46 -0700168 Blender::BlendN(fDst.get(), src, width);
herbcc49e592016-05-17 09:57:34 -0700169 src += width;
170 }
171 }
172 }
173
174 void onPostDraw(SkCanvas*) override {
175 // Make sure the compiler does not optimize away the operation.
176 volatile uint32_t v = 0;
177 for (int i = 0; i < fCount; i++) {
178 v ^= fDst[i];
179 }
180 }
181
182private:
183 int fCount;
184 SkAutoTArray<uint32_t> fDst;
bsalomon809f2582016-05-20 08:54:23 -0700185 SkString fFileName;
herbcc49e592016-05-17 09:57:34 -0700186 SkString fName;
187 SkPixmap fPixmap;
188
189 typedef Benchmark INHERITED;
190};
191
mtklein0358a6a2016-07-13 08:02:20 -0700192#define BENCHES(fileName) \
193 DEF_BENCH( return new LinearSrcOverBench<SrcOverVSkOptsBruteForce>(fileName); ) \
194 DEF_BENCH( return new LinearSrcOverBench<SrcOverVSkOptsTrivial>(fileName); ) \
195 DEF_BENCH( return new LinearSrcOverBench<SrcOverVSkOptsNonSimdCore>(fileName); ) \
196 DEF_BENCH( return new LinearSrcOverBench<SrcOverVSkOptsDefault>(fileName); )
herbcc49e592016-05-17 09:57:34 -0700197
198BENCHES("yellow_rose.png")
199BENCHES("baby_tux.png")
200BENCHES("plane.png")
201BENCHES("mandrill_512.png")
202BENCHES("iconstrip.png")