Index: tests/image-bitmap.cpp |
diff --git a/tests/image-bitmap.cpp b/tests/image-bitmap.cpp |
new file mode 100644 |
index 0000000000000000000000000000000000000000..fee5f0d7a6de92203fb7862059b42a2b1bf90613 |
--- /dev/null |
+++ b/tests/image-bitmap.cpp |
@@ -0,0 +1,75 @@ |
+/* |
+ * Copyright 2016 Google Inc. |
+ * |
+ * Use of this source code is governed by a BSD-style license that can be |
+ * found in the LICENSE file. |
+ */ |
+ |
+#include "Test.h" |
+ |
+#include "SkBitmap.h" |
+#include "SkImage.h" |
+ |
+namespace { |
+class BitmapKey { |
+public: |
+ BitmapKey() : fSubset(SkIRect::MakeEmpty()), fID(0) {} |
+ explicit BitmapKey(const SkBitmap& bm) |
+ : fSubset(bm.getSubset()), fID(bm.getGenerationID()) {} |
+ explicit BitmapKey(const SkImage* img) |
+ : fSubset(img->bounds()), fID(img->uniqueID()) {} |
+ explicit BitmapKey(const sk_sp<SkImage> img) |
+ : fSubset(img->bounds()), fID(img->uniqueID()) {} |
+ bool operator==(const BitmapKey& rhs) const { |
+ return fID == rhs.fID && fSubset == rhs.fSubset; |
+ } |
+ bool operator!=(const BitmapKey& rhs) const { return !(*this == rhs); } |
+ |
+private: |
+ SkIRect fSubset; |
+ uint32_t fID; |
+}; |
+} |
+static SkBitmap makebitmap() { |
+ SkBitmap bm; |
+ bm.allocN32Pixels(32, 64); |
+ bm.eraseColor(SK_ColorBLACK); |
+ bm.setImmutable(); |
+ return bm; |
+} |
+ |
reed1
2016/03/17 17:34:15
// Test that when we make an image from a subset o
hal.canary
2016/03/17 17:57:02
Done.
|
+DEF_TEST(ImageBitmapIdentity, r) { |
+ SkBitmap bm = makebitmap(); |
+ auto img = SkImage::MakeFromBitmap(bm); |
+ if (img) { |
reed1
2016/03/17 17:34:15
Not sure these runtime checks are needed, unless i
hal.canary
2016/03/17 17:57:02
Done.
|
+ REPORTER_ASSERT(r, BitmapKey(img) == BitmapKey(bm)); |
+ } else { |
+ ERRORF(r, "SkImage::MakeFromBitmap failed"); |
+ } |
+ SkBitmap a, b; |
+ if (!bm.extractSubset(&a, SkIRect::MakeXYWH(0, 0, 32, 32))) { |
+ ERRORF(r, "SkBitmap::extractSubset failed."); |
+ return; |
+ } |
+ REPORTER_ASSERT(r, a.dimensions() == SkISize::Make(32, 32)); |
+ if (!bm.extractSubset(&b, SkIRect::MakeXYWH(0, 32, 32, 32))) { |
+ ERRORF(r, "SkBitmap::extractSubset failed."); |
+ return; |
+ } |
+ REPORTER_ASSERT(r, b.dimensions() == SkISize::Make(32, 32)); |
+ auto imgA = SkImage::MakeFromBitmap(a); |
+ if (!imgA) { |
+ ERRORF(r, "SkImage::MakeFromBitmap failed."); |
+ return; |
+ } |
+ auto imgB = SkImage::MakeFromBitmap(b); |
+ if (!imgB) { |
+ ERRORF(r, "SkImage::MakeFromBitmap failed."); |
+ return; |
+ } |
+ REPORTER_ASSERT(r, imgA->uniqueID() != imgB->uniqueID()); |
+ REPORTER_ASSERT(r, BitmapKey(imgA) != BitmapKey(imgB)); |
+ REPORTER_ASSERT(r, BitmapKey(imgA) == BitmapKey(a)); |
+ REPORTER_ASSERT(r, BitmapKey(imgB) != BitmapKey(b)); |
+ REPORTER_ASSERT(r, BitmapKey(a) != BitmapKey(b)); |
+} |