abort drawing if srcRect is outside of the bitmap bounds
authorreed@google.com <reed@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>
Thu, 17 Mar 2011 17:48:04 +0000 (17:48 +0000)
committerreed@google.com <reed@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>
Thu, 17 Mar 2011 17:48:04 +0000 (17:48 +0000)
git-svn-id: http://skia.googlecode.com/svn/trunk@951 2bbb7eff-a529-9590-31e7-b0007b416f81

src/core/SkCanvas.cpp
tests/DrawBitmapRectTest.cpp [new file with mode: 0644]
tests/tests_files.mk

index a31623e..368bdfd 100644 (file)
@@ -1305,7 +1305,9 @@ void SkCanvas::drawBitmapRect(const SkBitmap& bitmap, const SkIRect* src,
     SkIRect tmpISrc;
     if (src) {
         tmpISrc.set(0, 0, bitmap.width(), bitmap.height());
-        tmpISrc.intersect(*src);
+        if (!tmpISrc.intersect(*src)) {
+            return;
+        }
         src = &tmpISrc;
     }
     this->internalDrawBitmap(*bitmapPtr, src, matrix, paint);
diff --git a/tests/DrawBitmapRectTest.cpp b/tests/DrawBitmapRectTest.cpp
new file mode 100644 (file)
index 0000000..fc382ce
--- /dev/null
@@ -0,0 +1,47 @@
+#include "Test.h"
+#include "SkBitmap.h"
+#include "SkCanvas.h"
+
+static bool check_for_all_zeros(const SkBitmap& bm) {
+    SkAutoLockPixels alp(bm);
+
+    size_t count = bm.width() * bm.bytesPerPixel();
+    for (int y = 0; y < bm.height(); y++) {
+        const uint8_t* ptr = reinterpret_cast<const uint8_t*>(bm.getAddr(0, y));
+        for (size_t i = 0; i < count; i++) {
+            if (ptr[i]) {
+                return false;
+            }
+        }
+    }
+    return true;
+}
+
+static const int gWidth = 256;
+static const int gHeight = 256;
+
+static void create(SkBitmap* bm, SkBitmap::Config config, SkColor color) {
+    bm->setConfig(config, gWidth, gHeight);
+    bm->allocPixels();
+    bm->eraseColor(color);
+}
+
+static void TestDrawBitmapRect(skiatest::Reporter* reporter) {
+    SkBitmap src, dst;
+
+    create(&src, SkBitmap::kARGB_8888_Config, 0xFFFFFFFF);
+    create(&dst, SkBitmap::kARGB_8888_Config, 0);
+
+    SkCanvas canvas(dst);
+
+    SkIRect srcR = { gWidth, 0, gWidth + 16, 16 };
+    SkRect  dstR = { 0, 0, SkIntToScalar(16), SkIntToScalar(16) };
+
+    canvas.drawBitmapRect(src, &srcR, dstR, NULL);
+
+    // ensure that we draw nothing if srcR does not intersect the bitmap
+    REPORTER_ASSERT(reporter, check_for_all_zeros(dst));
+}
+
+#include "TestClassDef.h"
+DEFINE_TESTCLASS("DrawBitmapRect", TestDrawBitmapRectClass, TestDrawBitmapRect)
index 5280502..cee5cfe 100644 (file)
@@ -6,6 +6,7 @@ SOURCE := \
     ClipStackTest.cpp \
     ClipperTest.cpp \
     DequeTest.cpp \
+    DrawBitmapRectTest.cpp \
     FillPathTest.cpp \
     FlateTest.cpp \
     GeometryTest.cpp \