OLD | NEW |
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 "SkBlurImageFilter.h" | 9 #include "SkBlurImageFilter.h" |
9 | |
10 #include "SkAutoPixmapStorage.h" | |
11 #include "SkColorPriv.h" | 10 #include "SkColorPriv.h" |
| 11 #include "SkDevice.h" |
12 #include "SkGpuBlurUtils.h" | 12 #include "SkGpuBlurUtils.h" |
13 #include "SkOpts.h" | 13 #include "SkOpts.h" |
14 #include "SkReadBuffer.h" | 14 #include "SkReadBuffer.h" |
15 #include "SkSpecialImage.h" | |
16 #include "SkWriteBuffer.h" | 15 #include "SkWriteBuffer.h" |
17 | |
18 #if SK_SUPPORT_GPU | 16 #if SK_SUPPORT_GPU |
19 #include "GrContext.h" | 17 #include "GrContext.h" |
20 #include "SkGr.h" | 18 #include "SkGr.h" |
21 #endif | 19 #endif |
22 | 20 |
23 // This rather arbitrary-looking value results in a maximum box blur kernel size | 21 // This rather arbitrary-looking value results in a maximum box blur kernel size |
24 // of 1000 pixels on the raster path, which matches the WebKit and Firefox | 22 // of 1000 pixels on the raster path, which matches the WebKit and Firefox |
25 // implementations. Since the GPU path does not compute a box blur, putting | 23 // implementations. Since the GPU path does not compute a box blur, putting |
26 // the limit on sigma ensures consistent behaviour between the GPU and | 24 // the limit on sigma ensures consistent behaviour between the GPU and |
27 // raster paths. | 25 // raster paths. |
(...skipping 20 matching lines...) Expand all Loading... |
48 SkScalar sigmaY = buffer.readScalar(); | 46 SkScalar sigmaY = buffer.readScalar(); |
49 return Create(sigmaX, sigmaY, common.getInput(0), &common.cropRect()); | 47 return Create(sigmaX, sigmaY, common.getInput(0), &common.cropRect()); |
50 } | 48 } |
51 | 49 |
52 void SkBlurImageFilter::flatten(SkWriteBuffer& buffer) const { | 50 void SkBlurImageFilter::flatten(SkWriteBuffer& buffer) const { |
53 this->INHERITED::flatten(buffer); | 51 this->INHERITED::flatten(buffer); |
54 buffer.writeScalar(fSigma.fWidth); | 52 buffer.writeScalar(fSigma.fWidth); |
55 buffer.writeScalar(fSigma.fHeight); | 53 buffer.writeScalar(fSigma.fHeight); |
56 } | 54 } |
57 | 55 |
58 static void get_box3_params(SkScalar s, int *kernelSize, int* kernelSize3, int *
lowOffset, | 56 static void getBox3Params(SkScalar s, int *kernelSize, int* kernelSize3, int *lo
wOffset, |
59 int *highOffset) | 57 int *highOffset) |
60 { | 58 { |
61 float pi = SkScalarToFloat(SK_ScalarPI); | 59 float pi = SkScalarToFloat(SK_ScalarPI); |
62 int d = static_cast<int>(floorf(SkScalarToFloat(s) * 3.0f * sqrtf(2.0f * pi)
/ 4.0f + 0.5f)); | 60 int d = static_cast<int>(floorf(SkScalarToFloat(s) * 3.0f * sqrtf(2.0f * pi)
/ 4.0f + 0.5f)); |
63 *kernelSize = d; | 61 *kernelSize = d; |
64 if (d % 2 == 1) { | 62 if (d % 2 == 1) { |
65 *lowOffset = *highOffset = (d - 1) / 2; | 63 *lowOffset = *highOffset = (d - 1) / 2; |
66 *kernelSize3 = d; | 64 *kernelSize3 = d; |
67 } else { | 65 } else { |
68 *highOffset = d / 2; | 66 *highOffset = d / 2; |
69 *lowOffset = *highOffset - 1; | 67 *lowOffset = *highOffset - 1; |
70 *kernelSize3 = d + 1; | 68 *kernelSize3 = d + 1; |
71 } | 69 } |
72 } | 70 } |
73 | 71 |
74 SkSpecialImage* SkBlurImageFilter::onFilterImage(SkSpecialImage* source, const C
ontext& ctx, | 72 bool SkBlurImageFilter::onFilterImageDeprecated(Proxy* proxy, |
75 SkIPoint* offset) const { | 73 const SkBitmap& source, const Co
ntext& ctx, |
76 SkIPoint inputOffset = SkIPoint::Make(0, 0); | 74 SkBitmap* dst, SkIPoint* offset)
const { |
77 | 75 SkBitmap src = source; |
78 SkAutoTUnref<SkSpecialImage> input(this->filterInput(0, source, ctx, &inputO
ffset)); | 76 SkIPoint srcOffset = SkIPoint::Make(0, 0); |
79 if (!input) { | 77 if (!this->filterInputDeprecated(0, proxy, source, ctx, &src, &srcOffset)) { |
80 return nullptr; | 78 return false; |
81 } | 79 } |
82 | 80 |
83 SkIRect inputBounds = SkIRect::MakeXYWH(inputOffset.fX, inputOffset.fY, | 81 if (src.colorType() != kN32_SkColorType) { |
84 input->width(), input->height()); | 82 return false; |
85 | |
86 SkIRect dstBounds; | |
87 if (!this->applyCropRect(this->mapContext(ctx), inputBounds, &dstBounds)) { | |
88 return nullptr; | |
89 } | |
90 if (!inputBounds.intersect(dstBounds)) { | |
91 return nullptr; | |
92 } | 83 } |
93 | 84 |
94 const SkVector sigma = map_sigma(fSigma, ctx.ctm()); | 85 SkIRect srcBounds = src.bounds(); |
| 86 srcBounds.offset(srcOffset); |
| 87 SkIRect dstBounds; |
| 88 if (!this->applyCropRect(this->mapContext(ctx), srcBounds, &dstBounds)) { |
| 89 return false; |
| 90 } |
| 91 if (!srcBounds.intersect(dstBounds)) { |
| 92 return false; |
| 93 } |
95 | 94 |
96 #if SK_SUPPORT_GPU | 95 SkVector sigma = map_sigma(fSigma, ctx.ctm()); |
97 if (input->peekTexture()) { | |
98 if (0 == sigma.x() && 0 == sigma.y()) { | |
99 offset->fX = inputBounds.x(); | |
100 offset->fY = inputBounds.y(); | |
101 return input->makeSubset(inputBounds.makeOffset(-inputOffset.x(), | |
102 -inputOffset.y())).r
elease(); | |
103 } | |
104 | |
105 GrTexture* inputTexture = input->peekTexture(); | |
106 | |
107 offset->fX = dstBounds.fLeft; | |
108 offset->fY = dstBounds.fTop; | |
109 inputBounds.offset(-inputOffset); | |
110 dstBounds.offset(-inputOffset); | |
111 SkRect inputBoundsF(SkRect::Make(inputBounds)); | |
112 SkAutoTUnref<GrTexture> tex(SkGpuBlurUtils::GaussianBlur(inputTexture->g
etContext(), | |
113 inputTexture, | |
114 false, | |
115 SkRect::Make(ds
tBounds), | |
116 &inputBoundsF, | |
117 sigma.x(), | |
118 sigma.y())); | |
119 if (!tex) { | |
120 return nullptr; | |
121 } | |
122 | |
123 return SkSpecialImage::MakeFromGpu(source->internal_getProxy(), | |
124 SkIRect::MakeWH(dstBounds.width(), ds
tBounds.height()), | |
125 kNeedNewImageUniqueID_SpecialImage, | |
126 tex).release(); | |
127 } | |
128 #endif | |
129 | 96 |
130 int kernelSizeX, kernelSizeX3, lowOffsetX, highOffsetX; | 97 int kernelSizeX, kernelSizeX3, lowOffsetX, highOffsetX; |
131 int kernelSizeY, kernelSizeY3, lowOffsetY, highOffsetY; | 98 int kernelSizeY, kernelSizeY3, lowOffsetY, highOffsetY; |
132 get_box3_params(sigma.x(), &kernelSizeX, &kernelSizeX3, &lowOffsetX, &highOf
fsetX); | 99 getBox3Params(sigma.x(), &kernelSizeX, &kernelSizeX3, &lowOffsetX, &highOffs
etX); |
133 get_box3_params(sigma.y(), &kernelSizeY, &kernelSizeY3, &lowOffsetY, &highOf
fsetY); | 100 getBox3Params(sigma.y(), &kernelSizeY, &kernelSizeY3, &lowOffsetY, &highOffs
etY); |
134 | 101 |
135 if (kernelSizeX < 0 || kernelSizeY < 0) { | 102 if (kernelSizeX < 0 || kernelSizeY < 0) { |
136 return nullptr; | 103 return false; |
137 } | 104 } |
138 | 105 |
139 if (kernelSizeX == 0 && kernelSizeY == 0) { | 106 if (kernelSizeX == 0 && kernelSizeY == 0) { |
140 offset->fX = inputBounds.x(); | 107 src.extractSubset(dst, srcBounds.makeOffset(-srcOffset.x(), -srcOffset.y
())); |
141 offset->fY = inputBounds.y(); | 108 offset->fX = srcBounds.x(); |
142 return input->makeSubset(inputBounds.makeOffset(-inputOffset.x(), | 109 offset->fY = srcBounds.y(); |
143 -inputOffset.y())).relea
se(); | 110 return true; |
144 } | 111 } |
145 | 112 |
146 SkPixmap inputPixmap; | 113 SkAutoLockPixels alp(src); |
147 | 114 if (!src.getPixels()) { |
148 if (!input->peekPixels(&inputPixmap)) { | 115 return false; |
149 return nullptr; | |
150 } | 116 } |
151 | 117 |
152 if (inputPixmap.colorType() != kN32_SkColorType) { | 118 SkAutoTUnref<SkBaseDevice> device(proxy->createDevice(dstBounds.width(), dst
Bounds.height())); |
153 return nullptr; | 119 if (!device) { |
| 120 return false; |
154 } | 121 } |
| 122 *dst = device->accessBitmap(false); |
| 123 SkAutoLockPixels alp_dst(*dst); |
155 | 124 |
156 SkImageInfo info = SkImageInfo::Make(dstBounds.width(), dstBounds.height(), | 125 SkAutoTUnref<SkBaseDevice> tempDevice(proxy->createDevice(dst->width(), dst-
>height())); |
157 inputPixmap.colorType(), inputPixmap.al
phaType()); | 126 if (!tempDevice) { |
158 | 127 return false; |
159 SkBitmap tmp, dst; | |
160 if (!tmp.tryAllocPixels(info) || !dst.tryAllocPixels(info)) { | |
161 return nullptr; | |
162 } | 128 } |
| 129 SkBitmap temp = tempDevice->accessBitmap(false); |
| 130 SkAutoLockPixels alpTemp(temp); |
163 | 131 |
164 offset->fX = dstBounds.fLeft; | 132 offset->fX = dstBounds.fLeft; |
165 offset->fY = dstBounds.fTop; | 133 offset->fY = dstBounds.fTop; |
166 SkPMColor* t = tmp.getAddr32(0, 0); | 134 SkPMColor* t = temp.getAddr32(0, 0); |
167 SkPMColor* d = dst.getAddr32(0, 0); | 135 SkPMColor* d = dst->getAddr32(0, 0); |
168 int w = dstBounds.width(), h = dstBounds.height(); | 136 int w = dstBounds.width(), h = dstBounds.height(); |
169 const SkPMColor* s = inputPixmap.addr32(inputBounds.x() - inputOffset.x(), | 137 const SkPMColor* s = src.getAddr32(srcBounds.x() - srcOffset.x(), srcBounds.
y() - srcOffset.y()); |
170 inputBounds.y() - inputOffset.y()); | 138 srcBounds.offset(-dstBounds.x(), -dstBounds.y()); |
171 inputBounds.offset(-dstBounds.x(), -dstBounds.y()); | |
172 dstBounds.offset(-dstBounds.x(), -dstBounds.y()); | 139 dstBounds.offset(-dstBounds.x(), -dstBounds.y()); |
173 SkIRect inputBoundsT = SkIRect::MakeLTRB(inputBounds.top(), inputBounds.left
(), | 140 SkIRect srcBoundsT = SkIRect::MakeLTRB(srcBounds.top(), srcBounds.left(), sr
cBounds.bottom(), srcBounds.right()); |
174 inputBounds.bottom(), inputBounds.r
ight()); | |
175 SkIRect dstBoundsT = SkIRect::MakeWH(dstBounds.height(), dstBounds.width()); | 141 SkIRect dstBoundsT = SkIRect::MakeWH(dstBounds.height(), dstBounds.width()); |
176 int sw = int(inputPixmap.rowBytes() >> 2); | 142 int sw = src.rowBytesAsPixels(); |
177 | 143 |
178 /** | 144 /** |
179 * | 145 * |
180 * In order to make memory accesses cache-friendly, we reorder the passes to | 146 * In order to make memory accesses cache-friendly, we reorder the passes to |
181 * use contiguous memory reads wherever possible. | 147 * use contiguous memory reads wherever possible. |
182 * | 148 * |
183 * For example, the 6 passes of the X-and-Y blur case are rewritten as | 149 * For example, the 6 passes of the X-and-Y blur case are rewritten as |
184 * follows. Instead of 3 passes in X and 3 passes in Y, we perform | 150 * follows. Instead of 3 passes in X and 3 passes in Y, we perform |
185 * 2 passes in X, 1 pass in X transposed to Y on write, 2 passes in X, | 151 * 2 passes in X, 1 pass in X transposed to Y on write, 2 passes in X, |
186 * then 1 pass in X transposed to Y on write. | 152 * then 1 pass in X transposed to Y on write. |
187 * | 153 * |
188 * +----+ +----+ +----+ +---+ +---+ +---+
+----+ | 154 * +----+ +----+ +----+ +---+ +---+ +---+
+----+ |
189 * + AB + ----> | AB | ----> | AB | -----> | A | ----> | A | ----> | A | ---
--> | AB | | 155 * + AB + ----> | AB | ----> | AB | -----> | A | ----> | A | ----> | A | ---
--> | AB | |
190 * +----+ blurX +----+ blurX +----+ blurXY | B | blurX | B | blurX | B | blu
rXY +----+ | 156 * +----+ blurX +----+ blurX +----+ blurXY | B | blurX | B | blurX | B | blu
rXY +----+ |
191 * +---+ +---+ +---+ | 157 * +---+ +---+ +---+ |
192 * | 158 * |
193 * In this way, two of the y-blurs become x-blurs applied to transposed | 159 * In this way, two of the y-blurs become x-blurs applied to transposed |
194 * images, and all memory reads are contiguous. | 160 * images, and all memory reads are contiguous. |
195 */ | 161 */ |
196 if (kernelSizeX > 0 && kernelSizeY > 0) { | 162 if (kernelSizeX > 0 && kernelSizeY > 0) { |
197 SkOpts::box_blur_xx(s, sw, inputBounds, t, kernelSizeX, lowOffsetX,
highOffsetX, w, h); | 163 SkOpts::box_blur_xx(s, sw, srcBounds, t, kernelSizeX, lowOffsetX, hi
ghOffsetX, w, h); |
198 SkOpts::box_blur_xx(t, w, dstBounds, d, kernelSizeX, highOffsetX,
lowOffsetX, w, h); | 164 SkOpts::box_blur_xx(t, w, dstBounds, d, kernelSizeX, highOffsetX, lo
wOffsetX, w, h); |
199 SkOpts::box_blur_xy(d, w, dstBounds, t, kernelSizeX3, highOffsetX,
highOffsetX, w, h); | 165 SkOpts::box_blur_xy(d, w, dstBounds, t, kernelSizeX3, highOffsetX, hi
ghOffsetX, w, h); |
200 SkOpts::box_blur_xx(t, h, dstBoundsT, d, kernelSizeY, lowOffsetY,
highOffsetY, h, w); | 166 SkOpts::box_blur_xx(t, h, dstBoundsT, d, kernelSizeY, lowOffsetY, hi
ghOffsetY, h, w); |
201 SkOpts::box_blur_xx(d, h, dstBoundsT, t, kernelSizeY, highOffsetY,
lowOffsetY, h, w); | 167 SkOpts::box_blur_xx(d, h, dstBoundsT, t, kernelSizeY, highOffsetY, lo
wOffsetY, h, w); |
202 SkOpts::box_blur_xy(t, h, dstBoundsT, d, kernelSizeY3, highOffsetY,
highOffsetY, h, w); | 168 SkOpts::box_blur_xy(t, h, dstBoundsT, d, kernelSizeY3, highOffsetY, hi
ghOffsetY, h, w); |
203 } else if (kernelSizeX > 0) { | 169 } else if (kernelSizeX > 0) { |
204 SkOpts::box_blur_xx(s, sw, inputBounds, d, kernelSizeX, lowOffsetX,
highOffsetX, w, h); | 170 SkOpts::box_blur_xx(s, sw, srcBounds, d, kernelSizeX, lowOffsetX, hi
ghOffsetX, w, h); |
205 SkOpts::box_blur_xx(d, w, dstBounds, t, kernelSizeX, highOffsetX,
lowOffsetX, w, h); | 171 SkOpts::box_blur_xx(d, w, dstBounds, t, kernelSizeX, highOffsetX, lo
wOffsetX, w, h); |
206 SkOpts::box_blur_xx(t, w, dstBounds, d, kernelSizeX3, highOffsetX,
highOffsetX, w, h); | 172 SkOpts::box_blur_xx(t, w, dstBounds, d, kernelSizeX3, highOffsetX, hi
ghOffsetX, w, h); |
207 } else if (kernelSizeY > 0) { | 173 } else if (kernelSizeY > 0) { |
208 SkOpts::box_blur_yx(s, sw, inputBoundsT, d, kernelSizeY, lowOffsetY,
highOffsetY, h, w); | 174 SkOpts::box_blur_yx(s, sw, srcBoundsT, d, kernelSizeY, lowOffsetY, hi
ghOffsetY, h, w); |
209 SkOpts::box_blur_xx(d, h, dstBoundsT, t, kernelSizeY, highOffsetY,
lowOffsetY, h, w); | 175 SkOpts::box_blur_xx(d, h, dstBoundsT, t, kernelSizeY, highOffsetY, lo
wOffsetY, h, w); |
210 SkOpts::box_blur_xy(t, h, dstBoundsT, d, kernelSizeY3, highOffsetY,
highOffsetY, h, w); | 176 SkOpts::box_blur_xy(t, h, dstBoundsT, d, kernelSizeY3, highOffsetY, hi
ghOffsetY, h, w); |
211 } | 177 } |
212 | 178 return true; |
213 return SkSpecialImage::MakeFromRaster(source->internal_getProxy(), | |
214 SkIRect::MakeWH(dstBounds.width(), | |
215 dstBounds.height()), | |
216 dst).release(); | |
217 } | 179 } |
218 | 180 |
219 | 181 |
220 SkRect SkBlurImageFilter::computeFastBounds(const SkRect& src) const { | 182 SkRect SkBlurImageFilter::computeFastBounds(const SkRect& src) const { |
221 SkRect bounds = this->getInput(0) ? this->getInput(0)->computeFastBounds(src
) : src; | 183 SkRect bounds = this->getInput(0) ? this->getInput(0)->computeFastBounds(src
) : src; |
222 bounds.outset(SkScalarMul(fSigma.width(), SkIntToScalar(3)), | 184 bounds.outset(SkScalarMul(fSigma.width(), SkIntToScalar(3)), |
223 SkScalarMul(fSigma.height(), SkIntToScalar(3))); | 185 SkScalarMul(fSigma.height(), SkIntToScalar(3))); |
224 return bounds; | 186 return bounds; |
225 } | 187 } |
226 | 188 |
227 SkIRect SkBlurImageFilter::onFilterNodeBounds(const SkIRect& src, const SkMatrix
& ctm, | 189 SkIRect SkBlurImageFilter::onFilterNodeBounds(const SkIRect& src, const SkMatrix
& ctm, |
228 MapDirection) const { | 190 MapDirection) const { |
229 SkVector sigma = map_sigma(fSigma, ctm); | 191 SkVector sigma = map_sigma(fSigma, ctm); |
230 return src.makeOutset(SkScalarCeilToInt(SkScalarMul(sigma.x(), SkIntToScalar
(3))), | 192 return src.makeOutset(SkScalarCeilToInt(SkScalarMul(sigma.x(), SkIntToScalar
(3))), |
231 SkScalarCeilToInt(SkScalarMul(sigma.y(), SkIntToScalar
(3)))); | 193 SkScalarCeilToInt(SkScalarMul(sigma.y(), SkIntToScalar
(3)))); |
232 } | 194 } |
233 | 195 |
| 196 bool SkBlurImageFilter::filterImageGPUDeprecated(Proxy* proxy, const SkBitmap& s
rc, |
| 197 const Context& ctx, |
| 198 SkBitmap* result, SkIPoint* off
set) const { |
| 199 #if SK_SUPPORT_GPU |
| 200 SkBitmap input = src; |
| 201 SkIPoint srcOffset = SkIPoint::Make(0, 0); |
| 202 if (!this->filterInputGPUDeprecated(0, proxy, src, ctx, &input, &srcOffset))
{ |
| 203 return false; |
| 204 } |
| 205 SkIRect srcBounds = input.bounds(); |
| 206 srcBounds.offset(srcOffset); |
| 207 SkIRect dstBounds; |
| 208 if (!this->applyCropRect(this->mapContext(ctx), srcBounds, &dstBounds)) { |
| 209 return false; |
| 210 } |
| 211 if (!srcBounds.intersect(dstBounds)) { |
| 212 return false; |
| 213 } |
| 214 SkVector sigma = map_sigma(fSigma, ctx.ctm()); |
| 215 if (sigma.x() == 0 && sigma.y() == 0) { |
| 216 input.extractSubset(result, srcBounds.makeOffset(-srcOffset.x(), -srcOff
set.y())); |
| 217 offset->fX = srcBounds.x(); |
| 218 offset->fY = srcBounds.y(); |
| 219 return true; |
| 220 } |
| 221 offset->fX = dstBounds.fLeft; |
| 222 offset->fY = dstBounds.fTop; |
| 223 srcBounds.offset(-srcOffset); |
| 224 dstBounds.offset(-srcOffset); |
| 225 SkRect srcBoundsF(SkRect::Make(srcBounds)); |
| 226 GrTexture* inputTexture = input.getTexture(); |
| 227 SkAutoTUnref<GrTexture> tex(SkGpuBlurUtils::GaussianBlur(inputTexture->getCo
ntext(), |
| 228 inputTexture, |
| 229 false, |
| 230 SkRect::Make(dstBou
nds), |
| 231 &srcBoundsF, |
| 232 sigma.x(), |
| 233 sigma.y())); |
| 234 if (!tex) { |
| 235 return false; |
| 236 } |
| 237 GrWrapTextureInBitmap(tex, dstBounds.width(), dstBounds.height(), false, res
ult); |
| 238 return true; |
| 239 #else |
| 240 SkDEBUGFAIL("Should not call in GPU-less build"); |
| 241 return false; |
| 242 #endif |
| 243 } |
| 244 |
234 #ifndef SK_IGNORE_TO_STRING | 245 #ifndef SK_IGNORE_TO_STRING |
235 void SkBlurImageFilter::toString(SkString* str) const { | 246 void SkBlurImageFilter::toString(SkString* str) const { |
236 str->appendf("SkBlurImageFilter: ("); | 247 str->appendf("SkBlurImageFilter: ("); |
237 str->appendf("sigma: (%f, %f) input (", fSigma.fWidth, fSigma.fHeight); | 248 str->appendf("sigma: (%f, %f) input (", fSigma.fWidth, fSigma.fHeight); |
238 | 249 |
239 if (this->getInput(0)) { | 250 if (this->getInput(0)) { |
240 this->getInput(0)->toString(str); | 251 this->getInput(0)->toString(str); |
241 } | 252 } |
242 | 253 |
243 str->append("))"); | 254 str->append("))"); |
244 } | 255 } |
245 #endif | 256 #endif |
OLD | NEW |