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

Side by Side Diff: src/effects/SkBlurImageFilter.cpp

Issue 59133006: Implement the y-transpose optimization in image (RGBA) blurs. This gives ~38% performance improvem… (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: Remove spurious returns Created 7 years, 1 month 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 | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | 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 2011 The Android Open Source Project 2 * Copyright 2011 The Android Open Source Project
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 "SkBitmap.h" 8 #include "SkBitmap.h"
9 #include "SkBlurImageFilter.h" 9 #include "SkBlurImageFilter.h"
10 #include "SkColorPriv.h" 10 #include "SkColorPriv.h"
(...skipping 21 matching lines...) Expand all
32 : INHERITED(input, cropRect), fSigma(SkSize::Make(sigmaX, sigmaY)) { 32 : INHERITED(input, cropRect), fSigma(SkSize::Make(sigmaX, sigmaY)) {
33 SkASSERT(sigmaX >= 0 && sigmaY >= 0); 33 SkASSERT(sigmaX >= 0 && sigmaY >= 0);
34 } 34 }
35 35
36 void SkBlurImageFilter::flatten(SkFlattenableWriteBuffer& buffer) const { 36 void SkBlurImageFilter::flatten(SkFlattenableWriteBuffer& buffer) const {
37 this->INHERITED::flatten(buffer); 37 this->INHERITED::flatten(buffer);
38 buffer.writeScalar(fSigma.fWidth); 38 buffer.writeScalar(fSigma.fWidth);
39 buffer.writeScalar(fSigma.fHeight); 39 buffer.writeScalar(fSigma.fHeight);
40 } 40 }
41 41
42 static void boxBlurX(const SkBitmap& src, SkBitmap* dst, int kernelSize, 42 enum BlurDirection {
43 int leftOffset, int rightOffset, const SkIRect& bounds) 43 kX, kY
44 };
45
mtklein 2013/11/06 13:51:52 Maybe add a nice big block comment here with some
Stephen White 2013/11/06 15:09:59 Done.
46 template<BlurDirection srcDirection, BlurDirection dstDirection>
47 static void boxBlur(const SkPMColor* src, int srcStride, SkPMColor* dst, int ker nelSize,
48 int leftOffset, int rightOffset, int width, int height)
44 { 49 {
45 int width = bounds.width(), height = bounds.height();
46 int rightBorder = SkMin32(rightOffset + 1, width); 50 int rightBorder = SkMin32(rightOffset + 1, width);
51 int srcStrideX = srcDirection == kX ? 1 : srcStride;
52 int dstStrideX = dstDirection == kX ? 1 : height;
53 int srcStrideY = srcDirection == kX ? srcStride : 1;
54 int dstStrideY = dstDirection == kX ? width : 1;
47 #ifndef SK_DISABLE_BLUR_DIVISION_OPTIMIZATION 55 #ifndef SK_DISABLE_BLUR_DIVISION_OPTIMIZATION
48 uint32_t scale = (1 << 24) / kernelSize; 56 uint32_t scale = (1 << 24) / kernelSize;
49 uint32_t half = 1 << 23; 57 uint32_t half = 1 << 23;
50 #endif 58 #endif
51 for (int y = 0; y < height; ++y) { 59 for (int y = 0; y < height; ++y) {
52 int sumA = 0, sumR = 0, sumG = 0, sumB = 0; 60 int sumA = 0, sumR = 0, sumG = 0, sumB = 0;
53 SkPMColor* p = src.getAddr32(bounds.fLeft, y + bounds.fTop); 61 const SkPMColor* p = src;
54 for (int i = 0; i < rightBorder; ++i) { 62 for (int i = 0; i < rightBorder; ++i) {
55 sumA += SkGetPackedA32(*p); 63 sumA += SkGetPackedA32(*p);
56 sumR += SkGetPackedR32(*p); 64 sumR += SkGetPackedR32(*p);
57 sumG += SkGetPackedG32(*p); 65 sumG += SkGetPackedG32(*p);
58 sumB += SkGetPackedB32(*p); 66 sumB += SkGetPackedB32(*p);
59 p++; 67 p += srcStrideX;
60 } 68 }
61 69
62 const SkColor* sptr = src.getAddr32(bounds.fLeft, bounds.fTop + y); 70 const SkPMColor* sptr = src;
63 SkColor* dptr = dst->getAddr32(0, y); 71 SkColor* dptr = dst;
64 for (int x = 0; x < width; ++x) { 72 for (int x = 0; x < width; ++x) {
65 #ifndef SK_DISABLE_BLUR_DIVISION_OPTIMIZATION 73 #ifndef SK_DISABLE_BLUR_DIVISION_OPTIMIZATION
66 *dptr = SkPackARGB32((sumA * scale + half) >> 24, 74 *dptr = SkPackARGB32((sumA * scale + half) >> 24,
67 (sumR * scale + half) >> 24, 75 (sumR * scale + half) >> 24,
68 (sumG * scale + half) >> 24, 76 (sumG * scale + half) >> 24,
69 (sumB * scale + half) >> 24); 77 (sumB * scale + half) >> 24);
70 #else 78 #else
71 *dptr = SkPackARGB32(sumA / kernelSize, 79 *dptr = SkPackARGB32(sumA / kernelSize,
72 sumR / kernelSize, 80 sumR / kernelSize,
73 sumG / kernelSize, 81 sumG / kernelSize,
74 sumB / kernelSize); 82 sumB / kernelSize);
75 #endif 83 #endif
76 if (x >= leftOffset) { 84 if (x >= leftOffset) {
77 SkColor l = *(sptr - leftOffset); 85 SkColor l = *(sptr - leftOffset * srcStrideX);
78 sumA -= SkGetPackedA32(l); 86 sumA -= SkGetPackedA32(l);
79 sumR -= SkGetPackedR32(l); 87 sumR -= SkGetPackedR32(l);
80 sumG -= SkGetPackedG32(l); 88 sumG -= SkGetPackedG32(l);
81 sumB -= SkGetPackedB32(l); 89 sumB -= SkGetPackedB32(l);
82 } 90 }
83 if (x + rightOffset + 1 < width) { 91 if (x + rightOffset + 1 < width) {
84 SkColor r = *(sptr + rightOffset + 1); 92 SkColor r = *(sptr + (rightOffset + 1) * srcStrideX);
85 sumA += SkGetPackedA32(r); 93 sumA += SkGetPackedA32(r);
86 sumR += SkGetPackedR32(r); 94 sumR += SkGetPackedR32(r);
87 sumG += SkGetPackedG32(r); 95 sumG += SkGetPackedG32(r);
88 sumB += SkGetPackedB32(r); 96 sumB += SkGetPackedB32(r);
89 } 97 }
90 sptr++; 98 sptr += srcStrideX;
91 dptr++; 99 if (srcDirection == kY) {
mtklein 2013/11/06 13:51:52 Should be fine to remove this prefetch entirely no
Stephen White 2013/11/06 15:09:59 Actually, it'll still be used in the Y-only path (
100 SK_PREFETCH(sptr + (rightOffset + 1) * srcStrideX);
101 }
102 dptr += dstStrideX;
92 } 103 }
104 src += srcStrideY;
105 dst += dstStrideY;
93 } 106 }
94 } 107 }
95 108
96 static void boxBlurY(const SkBitmap& src, SkBitmap* dst, int kernelSize, 109 static void boxBlurX(const SkPMColor* src, int srcStride, SkPMColor* dst, int ke rnelSize,
mtklein 2013/11/06 13:51:52 These are going to want to get renames, eh? X see
Stephen White 2013/11/06 15:09:59 We'll still need boxBlurY(), and boxBlurTransposed
mtklein 2013/11/06 15:18:56 Maybe just get rid of these intermediates and call
97 int topOffset, int bottomOffset, const SkIRect& bounds) 110 int leftOffset, int rightOffset, int width, int height)
98 { 111 {
99 int width = bounds.width(), height = bounds.height(); 112 boxBlur<kX, kX>(src, srcStride, dst, kernelSize, leftOffset, rightOffset, wi dth, height);
100 int bottomBorder = SkMin32(bottomOffset + 1, height); 113 }
101 int srcStride = src.rowBytesAsPixels(); 114
102 int dstStride = dst->rowBytesAsPixels();
103 #ifndef SK_DISABLE_BLUR_DIVISION_OPTIMIZATION 115 #ifndef SK_DISABLE_BLUR_DIVISION_OPTIMIZATION
104 uint32_t scale = (1 << 24) / kernelSize; 116 static void boxBlurXY(const SkPMColor* src, int srcStride, SkPMColor* dst, int k ernelSize,
105 uint32_t half = 1 << 23; 117 int leftOffset, int rightOffset, int width, int he ight)
118 {
119 boxBlur<kX, kY>(src, srcStride, dst, kernelSize, leftOffset, rightOffset, wi dth, height);
120 }
106 #endif 121 #endif
107 for (int x = 0; x < width; ++x) {
108 int sumA = 0, sumR = 0, sumG = 0, sumB = 0;
109 SkColor* p = src.getAddr32(bounds.fLeft + x, bounds.fTop);
110 for (int i = 0; i < bottomBorder; ++i) {
111 sumA += SkGetPackedA32(*p);
112 sumR += SkGetPackedR32(*p);
113 sumG += SkGetPackedG32(*p);
114 sumB += SkGetPackedB32(*p);
115 p += srcStride;
116 }
117 122
118 const SkColor* sptr = src.getAddr32(bounds.fLeft + x, bounds.fTop); 123 static void boxBlurY(const SkPMColor* src, int srcStride, SkPMColor* dst, int ke rnelSize,
119 SkColor* dptr = dst->getAddr32(x, 0); 124 int topOffset, int bottomOffset, int width, int height)
120 for (int y = 0; y < height; ++y) { 125 {
121 #ifndef SK_DISABLE_BLUR_DIVISION_OPTIMIZATION 126 boxBlur<kY, kY>(src, srcStride, dst, kernelSize, topOffset, bottomOffset, wi dth, height);
122 *dptr = SkPackARGB32((sumA * scale + half) >> 24,
123 (sumR * scale + half) >> 24,
124 (sumG * scale + half) >> 24,
125 (sumB * scale + half) >> 24);
126 #else
127 *dptr = SkPackARGB32(sumA / kernelSize,
128 sumR / kernelSize,
129 sumG / kernelSize,
130 sumB / kernelSize);
131 #endif
132 if (y >= topOffset) {
133 SkColor l = *(sptr - topOffset * srcStride);
134 sumA -= SkGetPackedA32(l);
135 sumR -= SkGetPackedR32(l);
136 sumG -= SkGetPackedG32(l);
137 sumB -= SkGetPackedB32(l);
138 }
139 if (y + bottomOffset + 1 < height) {
140 SkColor r = *(sptr + (bottomOffset + 1) * srcStride);
141 sumA += SkGetPackedA32(r);
142 sumR += SkGetPackedR32(r);
143 sumG += SkGetPackedG32(r);
144 sumB += SkGetPackedB32(r);
145 }
146 sptr += srcStride;
147 // The next leading pixel seems to be too hard to predict. Hint the fetch.
148 SK_PREFETCH(sptr + (bottomOffset + 1) * srcStride);
149 dptr += dstStride;
150 }
151 }
152 } 127 }
153 128
154 static void getBox3Params(SkScalar s, int *kernelSize, int* kernelSize3, int *lo wOffset, 129 static void getBox3Params(SkScalar s, int *kernelSize, int* kernelSize3, int *lo wOffset,
155 int *highOffset) 130 int *highOffset)
156 { 131 {
157 float pi = SkScalarToFloat(SK_ScalarPI); 132 float pi = SkScalarToFloat(SK_ScalarPI);
158 int d = static_cast<int>(floorf(SkScalarToFloat(s) * 3.0f * sqrtf(2.0f * pi) / 4.0f + 0.5f)); 133 int d = static_cast<int>(floorf(SkScalarToFloat(s) * 3.0f * sqrtf(2.0f * pi) / 4.0f + 0.5f));
159 *kernelSize = d; 134 *kernelSize = d;
160 if (d % 2 == 1) { 135 if (d % 2 == 1) {
161 *lowOffset = *highOffset = (d - 1) / 2; 136 *lowOffset = *highOffset = (d - 1) / 2;
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
206 src.copyTo(dst, dst->config()); 181 src.copyTo(dst, dst->config());
207 return true; 182 return true;
208 } 183 }
209 184
210 SkBitmap temp; 185 SkBitmap temp;
211 temp.setConfig(dst->config(), dst->width(), dst->height()); 186 temp.setConfig(dst->config(), dst->width(), dst->height());
212 if (!temp.allocPixels()) { 187 if (!temp.allocPixels()) {
213 return false; 188 return false;
214 } 189 }
215 190
191 const SkPMColor* s = src.getAddr32(srcBounds.left(), srcBounds.top());
192 SkPMColor* t = temp.getAddr32(0, 0);
193 SkPMColor* d = dst->getAddr32(0, 0);
194 int w = dstBounds.width(), h = dstBounds.height();
195 int sw = src.rowBytesAsPixels();
216 if (kernelSizeX > 0 && kernelSizeY > 0) { 196 if (kernelSizeX > 0 && kernelSizeY > 0) {
217 boxBlurX(src, &temp, kernelSizeX, lowOffsetX, highOffsetX, srcBounds) ; 197 #ifndef SK_DISABLE_BLUR_DIVISION_OPTIMIZATION
218 boxBlurY(temp, dst, kernelSizeY, lowOffsetY, highOffsetY, dstBounds) ; 198 boxBlurX(s, sw, t, kernelSizeX, lowOffsetX, highOffsetX, w, h);
219 boxBlurX(*dst, &temp, kernelSizeX, highOffsetX, lowOffsetX, dstBounds); 199 boxBlurX(t, w, d, kernelSizeX, highOffsetX, lowOffsetX, w, h);
220 boxBlurY(temp, dst, kernelSizeY, highOffsetY, lowOffsetY, dstBounds); 200 boxBlurXY(d, w, t, kernelSizeX3, highOffsetX, highOffsetX, w, h);
221 boxBlurX(*dst, &temp, kernelSizeX3, highOffsetX, highOffsetX, dstBounds) ; 201 boxBlurX(t, h, d, kernelSizeY, lowOffsetY, highOffsetY, h, w);
222 boxBlurY(temp, dst, kernelSizeY3, highOffsetY, highOffsetY, dstBounds) ; 202 boxBlurX(d, h, t, kernelSizeY, highOffsetY, lowOffsetY, h, w);
203 boxBlurXY(t, h, d, kernelSizeY3, highOffsetY, highOffsetY, h, w);
204 #else
205 boxBlurX(s, sw, t, kernelSizeX, lowOffsetX, highOffsetX, w, h);
206 boxBlurY(t, w, d, kernelSizeY, lowOffsetY, highOffsetY, h, w);
207 boxBlurX(d, w, t, kernelSizeX, highOffsetX, lowOffsetX, w, h);
208 boxBlurY(t, w, d, kernelSizeY, highOffsetY, lowOffsetY, h, w);
209 boxBlurX(d, w, t, kernelSizeX3, highOffsetX, highOffsetX, w, h);
210 boxBlurY(t, w, d, kernelSizeY3, highOffsetY, highOffsetY, h, w);
211 #endif
223 } else if (kernelSizeX > 0) { 212 } else if (kernelSizeX > 0) {
224 boxBlurX(src, dst, kernelSizeX, lowOffsetX, highOffsetX, srcBounds) ; 213 boxBlurX(s, sw, d, kernelSizeX, lowOffsetX, highOffsetX, w, h);
225 boxBlurX(*dst, &temp, kernelSizeX, highOffsetX, lowOffsetX, dstBounds); 214 boxBlurX(d, w, t, kernelSizeX, highOffsetX, lowOffsetX, w, h);
226 boxBlurX(temp, dst, kernelSizeX3, highOffsetX, highOffsetX, dstBounds) ; 215 boxBlurX(t, w, d, kernelSizeX3, highOffsetX, highOffsetX, w, h);
227 } else if (kernelSizeY > 0) { 216 } else if (kernelSizeY > 0) {
228 boxBlurY(src, dst, kernelSizeY, lowOffsetY, highOffsetY, srcBounds) ; 217 boxBlurY(s, sw, d, kernelSizeY, lowOffsetY, highOffsetY, h, w);
229 boxBlurY(*dst, &temp, kernelSizeY, highOffsetY, lowOffsetY, dstBounds); 218 boxBlurY(d, w, t, kernelSizeY, highOffsetY, lowOffsetY, h, w);
230 boxBlurY(temp, dst, kernelSizeY3, highOffsetY, highOffsetY, dstBounds) ; 219 boxBlurY(t, w, d, kernelSizeY3, highOffsetY, highOffsetY, h, w);
231 } 220 }
232 offset->fX += srcBounds.fLeft; 221 offset->fX += srcBounds.fLeft;
233 offset->fY += srcBounds.fTop; 222 offset->fY += srcBounds.fTop;
234 return true; 223 return true;
235 } 224 }
236 225
237 bool SkBlurImageFilter::filterImageGPU(Proxy* proxy, const SkBitmap& src, const SkMatrix& ctm, 226 bool SkBlurImageFilter::filterImageGPU(Proxy* proxy, const SkBitmap& src, const SkMatrix& ctm,
238 SkBitmap* result, SkIPoint* offset) { 227 SkBitmap* result, SkIPoint* offset) {
239 #if SK_SUPPORT_GPU 228 #if SK_SUPPORT_GPU
240 SkBitmap input; 229 SkBitmap input;
(...skipping 14 matching lines...) Expand all
255 fSigma.width(), 244 fSigma.width(),
256 fSigma.height())); 245 fSigma.height()));
257 offset->fX += rect.fLeft; 246 offset->fX += rect.fLeft;
258 offset->fY += rect.fTop; 247 offset->fY += rect.fTop;
259 return SkImageFilterUtils::WrapTexture(tex, rect.width(), rect.height(), res ult); 248 return SkImageFilterUtils::WrapTexture(tex, rect.width(), rect.height(), res ult);
260 #else 249 #else
261 SkDEBUGFAIL("Should not call in GPU-less build"); 250 SkDEBUGFAIL("Should not call in GPU-less build");
262 return false; 251 return false;
263 #endif 252 #endif
264 } 253 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698