Adding a bit of rebustness to SkRectShaderImageFilter
authorsugoi@google.com <sugoi@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>
Thu, 18 Apr 2013 14:13:10 +0000 (14:13 +0000)
committersugoi@google.com <sugoi@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>
Thu, 18 Apr 2013 14:13:10 +0000 (14:13 +0000)
Since SkRectShaderImageFilter is an image filter, I added logic so that it can actually use the size of an input image as the rect if an empty rect is provided as an argument. I also added logic to prevent creating an empty device.

Review URL: https://codereview.chromium.org/14093008

git-svn-id: http://skia.googlecode.com/svn/trunk@8739 2bbb7eff-a529-9590-31e7-b0007b416f81

src/effects/SkRectShaderImageFilter.cpp

index f5466cc..aee0e63 100644 (file)
@@ -43,16 +43,25 @@ SkRectShaderImageFilter::~SkRectShaderImageFilter() {
 }
 
 bool SkRectShaderImageFilter::onFilterImage(Proxy* proxy,
-                                        const SkBitmap& source,
-                                        const SkMatrix& matrix,
-                                        SkBitmap* result,
-                                        SkIPoint* loc) {
-    SkAutoTUnref<SkDevice> device(proxy->createDevice(SkScalarCeilToInt(fRect.width()),
-                                                      SkScalarCeilToInt(fRect.height())));
+                                            const SkBitmap& source,
+                                            const SkMatrix&,
+                                            SkBitmap* result,
+                                            SkIPoint*) {
+    SkRect rect(fRect);
+    if (rect.isEmpty()) {
+        rect = SkRect::MakeWH(source.width(), source.height());
+    }
+
+    if (rect.isEmpty()) {
+        return false;
+    }
+
+    SkAutoTUnref<SkDevice> device(proxy->createDevice(SkScalarCeilToInt(rect.width()),
+                                                      SkScalarCeilToInt(rect.height())));
     SkCanvas canvas(device.get());
     SkPaint paint;
     paint.setShader(fShader);
-    canvas.drawRect(fRect, paint);
+    canvas.drawRect(rect, paint);
     *result = device.get()->accessBitmap(false);
     return true;
 }