Warn when an SkScalar is passed into SkIntToScalar() (converted twice)
http://codereview.appspot.com/4548051/
git-svn-id: http://skia.googlecode.com/svn/trunk@1405 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/gm/shadertext.cpp b/gm/shadertext.cpp
index 045c399..ea87823 100644
--- a/gm/shadertext.cpp
+++ b/gm/shadertext.cpp
@@ -11,8 +11,7 @@
bm->eraseColor(0);
SkCanvas canvas(*bm);
- int shorterDimensionAsInt = w < h ? w : h;
- SkScalar s = SkIntToScalar(shorterDimensionAsInt);
+ SkScalar s = SkIntToScalar(SkMin32(w, h));
SkPoint pts[] = { { 0, 0 }, { s, s } };
SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE };
SkScalar pos[] = { 0, SK_Scalar1/2, SK_Scalar1 };
diff --git a/include/core/SkScalar.h b/include/core/SkScalar.h
index ebe621b..5dbf684 100644
--- a/include/core/SkScalar.h
+++ b/include/core/SkScalar.h
@@ -66,9 +66,47 @@
int exponent = bits << 1 >> 24;
return exponent != 0xFF;
}
+#ifdef SK_DEBUG
+ /** SkIntToScalar(n) returns its integer argument as an SkScalar
+ *
+ * If we're compiling in DEBUG mode, and can thus afford some extra runtime
+ * cycles, check to make sure that the parameter passed in has not already
+ * been converted to SkScalar. (A double conversion like this is harmless
+ * for SK_SCALAR_IS_FLOAT, but for SK_SCALAR_IS_FIXED this causes trouble.)
+ *
+ * Note that we need all of these method signatures to properly handle the
+ * various types that we pass into SkIntToScalar() to date:
+ * int, size_t, U8CPU, etc., even though what we really mean is "anything
+ * but a float".
+ */
+ static inline float SkIntToScalar(signed int param) {
+ return (float)param;
+ }
+ static inline float SkIntToScalar(unsigned int param) {
+ return (float)param;
+ }
+ static inline float SkIntToScalar(signed long param) {
+ return (float)param;
+ }
+ static inline float SkIntToScalar(unsigned long param) {
+ return (float)param;
+ }
+ static inline float SkIntToScalar(float param) {
+ /* If the parameter passed into SkIntToScalar is a float,
+ * one of two things has happened:
+ * 1. the parameter was an SkScalar (which is typedef'd to float)
+ * 2. the parameter was a float instead of an int
+ *
+ * Either way, it's not good.
+ */
+ SkASSERT(!"looks like you passed an SkScalar into SkIntToScalar");
+ return (float)0;
+ }
+#else // not SK_DEBUG
/** SkIntToScalar(n) returns its integer argument as an SkScalar
*/
#define SkIntToScalar(n) ((float)(n))
+#endif // not SK_DEBUG
/** SkFixedToScalar(n) returns its SkFixed argument as an SkScalar
*/
#define SkFixedToScalar(x) SkFixedToFloat(x)
@@ -282,4 +320,3 @@
const SkScalar values[], int length);
#endif
-