Add flag to check that F16Norm pixels are clamped properly

Bug: skia:
Change-Id: Ia0d4fd8d191b59683717b179a9613dc3e53df6cf
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/199724
Reviewed-by: Mike Klein <mtklein@google.com>
Commit-Queue: Brian Osman <brianosman@google.com>
diff --git a/dm/DM.cpp b/dm/DM.cpp
index 462033f..06e25e1 100644
--- a/dm/DM.cpp
+++ b/dm/DM.cpp
@@ -101,6 +101,8 @@
 
 DEFINE_bool(gdi, false, "On Windows, use GDI instead of DirectWrite for font rendering.");
 
+DEFINE_bool(checkF16, false, "Ensure that F16Norm pixels are clamped.");
+
 using namespace DM;
 using sk_gpu_test::GrContextFactory;
 using sk_gpu_test::GLTestContext;
@@ -1177,6 +1179,22 @@
                         }
                     }
                 }
+
+                SkPixmap pm;
+                if (FLAGS_checkF16 && bitmap.colorType() == kRGBA_F16Norm_SkColorType &&
+                        bitmap.peekPixels(&pm)) {
+                    bool unclamped = false;
+                    for (int y = 0; y < pm.height() && !unclamped; ++y)
+                    for (int x = 0; x < pm.width() && !unclamped; ++x) {
+                        Sk4f rgba = SkHalfToFloat_finite_ftz(*pm.addr64(x, y));
+                        float a = rgba[3];
+                        if (a > 1.0f || (rgba < 0.0f).anyTrue() || (rgba > a).anyTrue()) {
+                            SkDEBUGFAILF("F16Norm pixel [%d, %d] is unclamped: (%g, %g, %g, %g)\n",
+                                         x, y, rgba[0], rgba[1], rgba[2], rgba[3]);
+                            unclamped = true;
+                        }
+                    }
+                }
             });
         }
         done(task.sink.tag.c_str(), task.src.tag.c_str(), task.src.options.c_str(), name.c_str());