Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(31)

Side by Side Diff: src/core/SkXfermodeF16.cpp

Issue 2159993003: Improve naive SkColorXform to half floats (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Prettier code Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/core/SkNx.h ('k') | src/effects/gradients/Sk4fGradientPriv.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2016 Google Inc. 2 * Copyright 2016 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #include "SkHalf.h" 8 #include "SkHalf.h"
9 #include "SkPM4fPriv.h" 9 #include "SkPM4fPriv.h"
10 #include "SkUtils.h" 10 #include "SkUtils.h"
11 #include "SkXfermode.h" 11 #include "SkXfermode.h"
12 12
13 static Sk4f lerp_by_coverage(const Sk4f& src, const Sk4f& dst, uint8_t srcCovera ge) { 13 static Sk4f lerp_by_coverage(const Sk4f& src, const Sk4f& dst, uint8_t srcCovera ge) {
14 return dst + (src - dst) * Sk4f(srcCoverage * (1/255.0f)); 14 return dst + (src - dst) * Sk4f(srcCoverage * (1/255.0f));
15 } 15 }
16 16
17 //////////////////////////////////////////////////////////////////////////////// /////////////////// 17 //////////////////////////////////////////////////////////////////////////////// ///////////////////
18 18
19 static void xfer_1(const SkXfermode* xfer, uint64_t dst[], const SkPM4f* src, in t count, 19 static void xfer_1(const SkXfermode* xfer, uint64_t dst[], const SkPM4f* src, in t count,
20 const SkAlpha aa[]) { 20 const SkAlpha aa[]) {
21 SkXfermodeProc4f proc = xfer->getProc4f(); 21 SkXfermodeProc4f proc = xfer->getProc4f();
22 SkPM4f d; 22 SkPM4f d;
23 if (aa) { 23 if (aa) {
24 for (int i = 0; i < count; ++i) { 24 for (int i = 0; i < count; ++i) {
25 Sk4f d4 = SkHalfToFloat_finite(dst[i]); 25 Sk4f d4 = SkHalfToFloat_finite(dst[i]);
26 d4.store(d.fVec); 26 d4.store(d.fVec);
27 Sk4f r4 = Sk4f::Load(proc(*src, d).fVec); 27 Sk4f r4 = Sk4f::Load(proc(*src, d).fVec);
28 dst[i] = SkFloatToHalf_finite(lerp_by_coverage(r4, d4, aa[i])); 28 SkFloatToHalf_finite(lerp_by_coverage(r4, d4, aa[i])).store(&dst[i]) ;
29 } 29 }
30 } else { 30 } else {
31 for (int i = 0; i < count; ++i) { 31 for (int i = 0; i < count; ++i) {
32 SkHalfToFloat_finite(dst[i]).store(d.fVec); 32 SkHalfToFloat_finite(dst[i]).store(d.fVec);
33 Sk4f r4 = Sk4f::Load(proc(*src, d).fVec); 33 Sk4f r4 = Sk4f::Load(proc(*src, d).fVec);
34 dst[i] = SkFloatToHalf_finite(r4); 34 SkFloatToHalf_finite(r4).store(&dst[i]);
35 } 35 }
36 } 36 }
37 } 37 }
38 38
39 static void xfer_n(const SkXfermode* xfer, uint64_t dst[], const SkPM4f src[], i nt count, 39 static void xfer_n(const SkXfermode* xfer, uint64_t dst[], const SkPM4f src[], i nt count,
40 const SkAlpha aa[]) { 40 const SkAlpha aa[]) {
41 SkXfermodeProc4f proc = xfer->getProc4f(); 41 SkXfermodeProc4f proc = xfer->getProc4f();
42 SkPM4f d; 42 SkPM4f d;
43 if (aa) { 43 if (aa) {
44 for (int i = 0; i < count; ++i) { 44 for (int i = 0; i < count; ++i) {
45 Sk4f d4 = SkHalfToFloat_finite(dst[i]); 45 Sk4f d4 = SkHalfToFloat_finite(dst[i]);
46 d4.store(d.fVec); 46 d4.store(d.fVec);
47 Sk4f r4 = Sk4f::Load(proc(src[i], d).fVec); 47 Sk4f r4 = Sk4f::Load(proc(src[i], d).fVec);
48 dst[i] = SkFloatToHalf_finite(lerp_by_coverage(r4, d4, aa[i])); 48 SkFloatToHalf_finite(lerp_by_coverage(r4, d4, aa[i])).store(&dst[i]) ;
49 } 49 }
50 } else { 50 } else {
51 for (int i = 0; i < count; ++i) { 51 for (int i = 0; i < count; ++i) {
52 SkHalfToFloat_finite(dst[i]).store(d.fVec); 52 SkHalfToFloat_finite(dst[i]).store(d.fVec);
53 Sk4f r4 = Sk4f::Load(proc(src[i], d).fVec); 53 Sk4f r4 = Sk4f::Load(proc(src[i], d).fVec);
54 dst[i] = SkFloatToHalf_finite(r4); 54 SkFloatToHalf_finite(r4).store(&dst[i]);
55 } 55 }
56 } 56 }
57 } 57 }
58 58
59 const SkXfermode::F16Proc gProcs_General[] = { xfer_n, xfer_n, xfer_1, xfer_1 }; 59 const SkXfermode::F16Proc gProcs_General[] = { xfer_n, xfer_n, xfer_1, xfer_1 };
60 60
61 //////////////////////////////////////////////////////////////////////////////// /////////////////// 61 //////////////////////////////////////////////////////////////////////////////// ///////////////////
62 62
63 static void clear(const SkXfermode*, uint64_t dst[], const SkPM4f*, int count, c onst SkAlpha aa[]) { 63 static void clear(const SkXfermode*, uint64_t dst[], const SkPM4f*, int count, c onst SkAlpha aa[]) {
64 if (aa) { 64 if (aa) {
65 for (int i = 0; i < count; ++i) { 65 for (int i = 0; i < count; ++i) {
66 if (aa[i]) { 66 if (aa[i]) {
67 const Sk4f d4 = SkHalfToFloat_finite(dst[i]); 67 const Sk4f d4 = SkHalfToFloat_finite(dst[i]);
68 dst[i] = SkFloatToHalf_finite(d4 * Sk4f((255 - aa[i]) * 1.0f/255 )); 68 SkFloatToHalf_finite(d4 * Sk4f((255 - aa[i]) * 1.0f/255)).store( &dst[i]);
69 } 69 }
70 } 70 }
71 } else { 71 } else {
72 sk_memset64(dst, 0, count); 72 sk_memset64(dst, 0, count);
73 } 73 }
74 } 74 }
75 75
76 const SkXfermode::F16Proc gProcs_Clear[] = { clear, clear, clear, clear }; 76 const SkXfermode::F16Proc gProcs_Clear[] = { clear, clear, clear, clear };
77 77
78 //////////////////////////////////////////////////////////////////////////////// /////////////////// 78 //////////////////////////////////////////////////////////////////////////////// ///////////////////
79 79
80 static void src_1(const SkXfermode*, uint64_t dst[], const SkPM4f* src, int coun t, 80 static void src_1(const SkXfermode*, uint64_t dst[], const SkPM4f* src, int coun t,
81 const SkAlpha aa[]) { 81 const SkAlpha aa[]) {
82 const Sk4f s4 = Sk4f::Load(src->fVec); 82 const Sk4f s4 = Sk4f::Load(src->fVec);
83 if (aa) { 83 if (aa) {
84 for (int i = 0; i < count; ++i) { 84 for (int i = 0; i < count; ++i) {
85 const Sk4f d4 = SkHalfToFloat_finite(dst[i]); 85 const Sk4f d4 = SkHalfToFloat_finite(dst[i]);
86 dst[i] = SkFloatToHalf_finite(lerp_by_coverage(s4, d4, aa[i])); 86 SkFloatToHalf_finite(lerp_by_coverage(s4, d4, aa[i])).store(&dst[i]) ;
87 } 87 }
88 } else { 88 } else {
89 sk_memset64(dst, SkFloatToHalf_finite(s4), count); 89 uint64_t s4h;
90 SkFloatToHalf_finite(s4).store(&s4h);
91 sk_memset64(dst, s4h, count);
90 } 92 }
91 } 93 }
92 94
93 static void src_n(const SkXfermode*, uint64_t dst[], const SkPM4f src[], int cou nt, 95 static void src_n(const SkXfermode*, uint64_t dst[], const SkPM4f src[], int cou nt,
94 const SkAlpha aa[]) { 96 const SkAlpha aa[]) {
95 if (aa) { 97 if (aa) {
96 for (int i = 0; i < count; ++i) { 98 for (int i = 0; i < count; ++i) {
97 const Sk4f s4 = Sk4f::Load(src[i].fVec); 99 const Sk4f s4 = Sk4f::Load(src[i].fVec);
98 const Sk4f d4 = SkHalfToFloat_finite(dst[i]); 100 const Sk4f d4 = SkHalfToFloat_finite(dst[i]);
99 dst[i] = SkFloatToHalf_finite(lerp_by_coverage(s4, d4, aa[i])); 101 SkFloatToHalf_finite(lerp_by_coverage(s4, d4, aa[i])).store(&dst[i]) ;
100 } 102 }
101 } else { 103 } else {
102 for (int i = 0; i < count; ++i) { 104 for (int i = 0; i < count; ++i) {
103 const Sk4f s4 = Sk4f::Load(src[i].fVec); 105 const Sk4f s4 = Sk4f::Load(src[i].fVec);
104 dst[i] = SkFloatToHalf_finite(s4); 106 SkFloatToHalf_finite(s4).store(&dst[i]);
105 } 107 }
106 } 108 }
107 } 109 }
108 110
109 const SkXfermode::F16Proc gProcs_Src[] = { src_n, src_n, src_1, src_1 }; 111 const SkXfermode::F16Proc gProcs_Src[] = { src_n, src_n, src_1, src_1 };
110 112
111 //////////////////////////////////////////////////////////////////////////////// /////////////////// 113 //////////////////////////////////////////////////////////////////////////////// ///////////////////
112 114
113 static void dst(const SkXfermode*, uint64_t*, const SkPM4f*, int count, const Sk Alpha[]) {} 115 static void dst(const SkXfermode*, uint64_t*, const SkPM4f*, int count, const Sk Alpha[]) {}
114 116
115 const SkXfermode::F16Proc gProcs_Dst[] = { dst, dst, dst, dst }; 117 const SkXfermode::F16Proc gProcs_Dst[] = { dst, dst, dst, dst };
116 118
117 //////////////////////////////////////////////////////////////////////////////// /////////////////// 119 //////////////////////////////////////////////////////////////////////////////// ///////////////////
118 120
119 static void srcover_1(const SkXfermode*, uint64_t dst[], const SkPM4f* src, int count, 121 static void srcover_1(const SkXfermode*, uint64_t dst[], const SkPM4f* src, int count,
120 const SkAlpha aa[]) { 122 const SkAlpha aa[]) {
121 const Sk4f s4 = Sk4f::Load(src->fVec); 123 const Sk4f s4 = Sk4f::Load(src->fVec);
122 const Sk4f dst_scale = Sk4f(1 - get_alpha(s4)); 124 const Sk4f dst_scale = Sk4f(1 - get_alpha(s4));
123 for (int i = 0; i < count; ++i) { 125 for (int i = 0; i < count; ++i) {
124 const Sk4f d4 = SkHalfToFloat_finite(dst[i]); 126 const Sk4f d4 = SkHalfToFloat_finite(dst[i]);
125 const Sk4f r4 = s4 + d4 * dst_scale; 127 const Sk4f r4 = s4 + d4 * dst_scale;
126 if (aa) { 128 if (aa) {
127 dst[i] = SkFloatToHalf_finite(lerp_by_coverage(r4, d4, aa[i])); 129 SkFloatToHalf_finite(lerp_by_coverage(r4, d4, aa[i])).store(&dst[i]) ;
128 } else { 130 } else {
129 dst[i] = SkFloatToHalf_finite(r4); 131 SkFloatToHalf_finite(r4).store(&dst[i]);
130 } 132 }
131 } 133 }
132 } 134 }
133 135
134 static void srcover_n(const SkXfermode*, uint64_t dst[], const SkPM4f src[], int count, 136 static void srcover_n(const SkXfermode*, uint64_t dst[], const SkPM4f src[], int count,
135 const SkAlpha aa[]) { 137 const SkAlpha aa[]) {
136 for (int i = 0; i < count; ++i) { 138 for (int i = 0; i < count; ++i) {
137 Sk4f s = Sk4f::Load(src+i), 139 Sk4f s = Sk4f::Load(src+i),
138 d = SkHalfToFloat_finite(dst[i]), 140 d = SkHalfToFloat_finite(dst[i]),
139 r = s + d*(1.0f - SkNx_shuffle<3,3,3,3>(s)); 141 r = s + d*(1.0f - SkNx_shuffle<3,3,3,3>(s));
140 if (aa) { 142 if (aa) {
141 r = lerp_by_coverage(r, d, aa[i]); 143 r = lerp_by_coverage(r, d, aa[i]);
142 } 144 }
143 dst[i] = SkFloatToHalf_finite(r); 145 SkFloatToHalf_finite(r).store(&dst[i]);
144 } 146 }
145 } 147 }
146 148
147 const SkXfermode::F16Proc gProcs_SrcOver[] = { srcover_n, src_n, srcover_1, src_ 1 }; 149 const SkXfermode::F16Proc gProcs_SrcOver[] = { srcover_n, src_n, srcover_1, src_ 1 };
148 150
149 //////////////////////////////////////////////////////////////////////////////// /////////////////// 151 //////////////////////////////////////////////////////////////////////////////// ///////////////////
150 152
151 static SkXfermode::F16Proc find_proc(SkXfermode::Mode mode, uint32_t flags) { 153 static SkXfermode::F16Proc find_proc(SkXfermode::Mode mode, uint32_t flags) {
152 SkASSERT(0 == (flags & ~3)); 154 SkASSERT(0 == (flags & ~3));
153 flags &= 3; 155 flags &= 3;
(...skipping 13 matching lines...) Expand all
167 SkASSERT(0 == (flags & ~3)); 169 SkASSERT(0 == (flags & ~3));
168 flags &= 3; 170 flags &= 3;
169 171
170 Mode mode; 172 Mode mode;
171 return this->asMode(&mode) ? find_proc(mode, flags) : gProcs_General[flags]; 173 return this->asMode(&mode) ? find_proc(mode, flags) : gProcs_General[flags];
172 } 174 }
173 175
174 SkXfermode::F16Proc SkXfermode::GetF16Proc(SkXfermode* xfer, uint32_t flags) { 176 SkXfermode::F16Proc SkXfermode::GetF16Proc(SkXfermode* xfer, uint32_t flags) {
175 return xfer ? xfer->onGetF16Proc(flags) : find_proc(SkXfermode::kSrcOver_Mod e, flags); 177 return xfer ? xfer->onGetF16Proc(flags) : find_proc(SkXfermode::kSrcOver_Mod e, flags);
176 } 178 }
OLDNEW
« no previous file with comments | « src/core/SkNx.h ('k') | src/effects/gradients/Sk4fGradientPriv.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698