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

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

Issue 1657773002: Fix zero-sized blur with crop rect. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Fixes per review comments Created 4 years, 10 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 | « gm/imagefilterscropped.cpp ('k') | 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"
11 #include "SkDevice.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 "SkWriteBuffer.h" 15 #include "SkWriteBuffer.h"
16 #if SK_SUPPORT_GPU 16 #if SK_SUPPORT_GPU
17 #include "GrContext.h" 17 #include "GrContext.h"
18 #include "SkGr.h" 18 #include "SkGr.h"
19 #endif 19 #endif
20 20
21 // 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
22 // 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
23 // 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
24 // the limit on sigma ensures consistent behaviour between the GPU and 24 // the limit on sigma ensures consistent behaviour between the GPU and
25 // raster paths. 25 // raster paths.
26 #define MAX_SIGMA SkIntToScalar(532) 26 #define MAX_SIGMA SkIntToScalar(532)
27 27
28 static SkVector mapSigma(const SkSize& localSigma, const SkMatrix& ctm) { 28 static SkVector map_sigma(const SkSize& localSigma, const SkMatrix& ctm) {
29 SkVector sigma = SkVector::Make(localSigma.width(), localSigma.height()); 29 SkVector sigma = SkVector::Make(localSigma.width(), localSigma.height());
30 ctm.mapVectors(&sigma, 1); 30 ctm.mapVectors(&sigma, 1);
31 sigma.fX = SkMinScalar(SkScalarAbs(sigma.fX), MAX_SIGMA); 31 sigma.fX = SkMinScalar(SkScalarAbs(sigma.fX), MAX_SIGMA);
32 sigma.fY = SkMinScalar(SkScalarAbs(sigma.fY), MAX_SIGMA); 32 sigma.fY = SkMinScalar(SkScalarAbs(sigma.fY), MAX_SIGMA);
33 return sigma; 33 return sigma;
34 } 34 }
35 35
36 SkBlurImageFilter::SkBlurImageFilter(SkScalar sigmaX, 36 SkBlurImageFilter::SkBlurImageFilter(SkScalar sigmaX,
37 SkScalar sigmaY, 37 SkScalar sigmaY,
38 SkImageFilter* input, 38 SkImageFilter* input,
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
83 } 83 }
84 84
85 SkIRect srcBounds, dstBounds; 85 SkIRect srcBounds, dstBounds;
86 if (!this->applyCropRect(this->mapContext(ctx), src, srcOffset, &dstBounds, &srcBounds)) { 86 if (!this->applyCropRect(this->mapContext(ctx), src, srcOffset, &dstBounds, &srcBounds)) {
87 return false; 87 return false;
88 } 88 }
89 if (!srcBounds.intersect(dstBounds)) { 89 if (!srcBounds.intersect(dstBounds)) {
90 return false; 90 return false;
91 } 91 }
92 92
93 SkAutoLockPixels alp(src); 93 SkVector sigma = map_sigma(fSigma, ctx.ctm());
94 if (!src.getPixels()) {
95 return false;
96 }
97
98 SkAutoTUnref<SkBaseDevice> device(proxy->createDevice(dstBounds.width(), dst Bounds.height()));
99 if (!device) {
100 return false;
101 }
102 *dst = device->accessBitmap(false);
103 SkAutoLockPixels alp_dst(*dst);
104
105 SkVector sigma = mapSigma(fSigma, ctx.ctm());
106 94
107 int kernelSizeX, kernelSizeX3, lowOffsetX, highOffsetX; 95 int kernelSizeX, kernelSizeX3, lowOffsetX, highOffsetX;
108 int kernelSizeY, kernelSizeY3, lowOffsetY, highOffsetY; 96 int kernelSizeY, kernelSizeY3, lowOffsetY, highOffsetY;
109 getBox3Params(sigma.x(), &kernelSizeX, &kernelSizeX3, &lowOffsetX, &highOffs etX); 97 getBox3Params(sigma.x(), &kernelSizeX, &kernelSizeX3, &lowOffsetX, &highOffs etX);
110 getBox3Params(sigma.y(), &kernelSizeY, &kernelSizeY3, &lowOffsetY, &highOffs etY); 98 getBox3Params(sigma.y(), &kernelSizeY, &kernelSizeY3, &lowOffsetY, &highOffs etY);
111 99
112 if (kernelSizeX < 0 || kernelSizeY < 0) { 100 if (kernelSizeX < 0 || kernelSizeY < 0) {
113 return false; 101 return false;
114 } 102 }
115 103
116 if (kernelSizeX == 0 && kernelSizeY == 0) { 104 if (kernelSizeX == 0 && kernelSizeY == 0) {
117 src.copyTo(dst, dst->colorType()); 105 src.extractSubset(dst, srcBounds);
118 offset->fX = dstBounds.x() + srcOffset.x(); 106 offset->fX = srcBounds.x();
119 offset->fY = dstBounds.y() + srcOffset.y(); 107 offset->fY = srcBounds.y();
120 return true; 108 return true;
121 } 109 }
122 110
111 SkAutoLockPixels alp(src);
112 if (!src.getPixels()) {
113 return false;
114 }
115
116 SkAutoTUnref<SkBaseDevice> device(proxy->createDevice(dstBounds.width(), dst Bounds.height()));
117 if (!device) {
118 return false;
119 }
120 *dst = device->accessBitmap(false);
121 SkAutoLockPixels alp_dst(*dst);
122
123 SkAutoTUnref<SkBaseDevice> tempDevice(proxy->createDevice(dst->width(), dst- >height())); 123 SkAutoTUnref<SkBaseDevice> tempDevice(proxy->createDevice(dst->width(), dst- >height()));
124 if (!tempDevice) { 124 if (!tempDevice) {
125 return false; 125 return false;
126 } 126 }
127 SkBitmap temp = tempDevice->accessBitmap(false); 127 SkBitmap temp = tempDevice->accessBitmap(false);
128 SkAutoLockPixels alpTemp(temp); 128 SkAutoLockPixels alpTemp(temp);
129 129
130 offset->fX = dstBounds.fLeft; 130 offset->fX = dstBounds.fLeft;
131 offset->fY = dstBounds.fTop; 131 offset->fY = dstBounds.fTop;
132 SkPMColor* t = temp.getAddr32(0, 0); 132 SkPMColor* t = temp.getAddr32(0, 0);
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
184 *dst = src; 184 *dst = src;
185 } 185 }
186 186
187 dst->outset(SkScalarMul(fSigma.width(), SkIntToScalar(3)), 187 dst->outset(SkScalarMul(fSigma.width(), SkIntToScalar(3)),
188 SkScalarMul(fSigma.height(), SkIntToScalar(3))); 188 SkScalarMul(fSigma.height(), SkIntToScalar(3)));
189 } 189 }
190 190
191 void SkBlurImageFilter::onFilterNodeBounds(const SkIRect& src, const SkMatrix& c tm, 191 void SkBlurImageFilter::onFilterNodeBounds(const SkIRect& src, const SkMatrix& c tm,
192 SkIRect* dst, MapDirection) const { 192 SkIRect* dst, MapDirection) const {
193 *dst = src; 193 *dst = src;
194 SkVector sigma = mapSigma(fSigma, ctm); 194 SkVector sigma = map_sigma(fSigma, ctm);
195 dst->outset(SkScalarCeilToInt(SkScalarMul(sigma.x(), SkIntToScalar(3))), 195 dst->outset(SkScalarCeilToInt(SkScalarMul(sigma.x(), SkIntToScalar(3))),
196 SkScalarCeilToInt(SkScalarMul(sigma.y(), SkIntToScalar(3)))); 196 SkScalarCeilToInt(SkScalarMul(sigma.y(), SkIntToScalar(3))));
197 } 197 }
198 198
199 bool SkBlurImageFilter::filterImageGPU(Proxy* proxy, const SkBitmap& src, const Context& ctx, 199 bool SkBlurImageFilter::filterImageGPU(Proxy* proxy, const SkBitmap& src, const Context& ctx,
200 SkBitmap* result, SkIPoint* offset) const { 200 SkBitmap* result, SkIPoint* offset) const {
201 #if SK_SUPPORT_GPU 201 #if SK_SUPPORT_GPU
202 SkBitmap input = src; 202 SkBitmap input = src;
203 SkIPoint srcOffset = SkIPoint::Make(0, 0); 203 SkIPoint srcOffset = SkIPoint::Make(0, 0);
204 if (!this->filterInputGPU(0, proxy, src, ctx, &input, &srcOffset)) { 204 if (!this->filterInputGPU(0, proxy, src, ctx, &input, &srcOffset)) {
205 return false; 205 return false;
206 } 206 }
207 SkIRect srcBounds, dstBounds; 207 SkIRect srcBounds, dstBounds;
208 if (!this->applyCropRect(this->mapContext(ctx), input, srcOffset, &dstBounds , &srcBounds)) { 208 if (!this->applyCropRect(this->mapContext(ctx), input, srcOffset, &dstBounds , &srcBounds)) {
209 return false; 209 return false;
210 } 210 }
211 if (!srcBounds.intersect(dstBounds)) { 211 if (!srcBounds.intersect(dstBounds)) {
212 return false; 212 return false;
213 } 213 }
214 GrTexture* source = input.getTexture(); 214 SkVector sigma = map_sigma(fSigma, ctx.ctm());
215 SkVector sigma = mapSigma(fSigma, ctx.ctm()); 215 if (sigma.x() == 0 && sigma.y() == 0) {
216 input.extractSubset(result, srcBounds);
217 offset->fX = srcBounds.x();
218 offset->fY = srcBounds.y();
219 return true;
220 }
216 offset->fX = dstBounds.fLeft; 221 offset->fX = dstBounds.fLeft;
217 offset->fY = dstBounds.fTop; 222 offset->fY = dstBounds.fTop;
218 srcBounds.offset(-srcOffset); 223 srcBounds.offset(-srcOffset);
219 dstBounds.offset(-srcOffset); 224 dstBounds.offset(-srcOffset);
220 SkRect srcBoundsF(SkRect::Make(srcBounds)); 225 SkRect srcBoundsF(SkRect::Make(srcBounds));
221 SkAutoTUnref<GrTexture> tex(SkGpuBlurUtils::GaussianBlur(source->getContext( ), 226 GrTexture* inputTexture = input.getTexture();
222 source, 227 SkAutoTUnref<GrTexture> tex(SkGpuBlurUtils::GaussianBlur(inputTexture->getCo ntext(),
228 inputTexture,
223 false, 229 false,
224 SkRect::Make(dstBou nds), 230 SkRect::Make(dstBou nds),
225 &srcBoundsF, 231 &srcBoundsF,
226 sigma.x(), 232 sigma.x(),
227 sigma.y())); 233 sigma.y()));
228 if (!tex) { 234 if (!tex) {
229 return false; 235 return false;
230 } 236 }
231 GrWrapTextureInBitmap(tex, dstBounds.width(), dstBounds.height(), false, res ult); 237 GrWrapTextureInBitmap(tex, dstBounds.width(), dstBounds.height(), false, res ult);
232 return true; 238 return true;
233 #else 239 #else
234 SkDEBUGFAIL("Should not call in GPU-less build"); 240 SkDEBUGFAIL("Should not call in GPU-less build");
235 return false; 241 return false;
236 #endif 242 #endif
237 } 243 }
238 244
239 #ifndef SK_IGNORE_TO_STRING 245 #ifndef SK_IGNORE_TO_STRING
240 void SkBlurImageFilter::toString(SkString* str) const { 246 void SkBlurImageFilter::toString(SkString* str) const {
241 str->appendf("SkBlurImageFilter: ("); 247 str->appendf("SkBlurImageFilter: (");
242 str->appendf("sigma: (%f, %f) input (", fSigma.fWidth, fSigma.fHeight); 248 str->appendf("sigma: (%f, %f) input (", fSigma.fWidth, fSigma.fHeight);
243 249
244 if (this->getInput(0)) { 250 if (this->getInput(0)) {
245 this->getInput(0)->toString(str); 251 this->getInput(0)->toString(str);
246 } 252 }
247 253
248 str->append("))"); 254 str->append("))");
249 } 255 }
250 #endif 256 #endif
OLDNEW
« no previous file with comments | « gm/imagefilterscropped.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698