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