add poly_tf to skcms_sRGB_profile

And a unit test that makes sure it's kept up to date,
and that skcms_OptimizeForSpeed() is a no-op when the
has_poly_tf bits are already set.

Change-Id: I5bf982084c312cafe59fb7a9d21918ccabec6f3d
Reviewed-on: https://skia-review.googlesource.com/123928
Commit-Queue: Brian Osman <brianosman@google.com>
Reviewed-by: Brian Osman <brianosman@google.com>
Auto-Submit: Mike Klein <mtklein@google.com>
diff --git a/src/ICCProfile.c b/src/ICCProfile.c
index 8448327..90ce6fc 100644
--- a/src/ICCProfile.c
+++ b/src/ICCProfile.c
@@ -806,6 +806,13 @@
         { 0.222488403f, 0.716873169f, 0.060607910f },
         { 0.013916016f, 0.097076416f, 0.714096069f },
     }},
+
+    .has_poly_tf = { true, true, true },
+    .poly_tf = {
+       {0.293833881617f, 0.704207003117f, (float)(1/12.92), 0.04045f},
+       {0.293833881617f, 0.704207003117f, (float)(1/12.92), 0.04045f},
+       {0.293833881617f, 0.704207003117f, (float)(1/12.92), 0.04045f},
+    },
 };
 
 const skcms_ICCProfile skcms_XYZD50_profile = {
diff --git a/src/PolyTF.c b/src/PolyTF.c
index f4d837b..f7f9a3f 100644
--- a/src/PolyTF.c
+++ b/src/PolyTF.c
@@ -150,6 +150,8 @@
 
 void skcms_OptimizeForSpeed(skcms_ICCProfile* profile) {
     for (int i = 0; profile->has_trc && i < 3; i++) {
-        profile->has_poly_tf[i] = fit_poly_tf(&profile->trc[i], &profile->poly_tf[i]);
+        if (!profile->has_poly_tf[i]) {
+             profile->has_poly_tf[i] = fit_poly_tf(&profile->trc[i], &profile->poly_tf[i]);
+        }
     }
 }
diff --git a/tests.c b/tests.c
index 9958215..6a3af00 100644
--- a/tests.c
+++ b/tests.c
@@ -1022,6 +1022,41 @@
     free(ptr);
 }
 
+static void test_sRGB_profile_has_poly_tf() {
+    // If we can find an skcms_PolyTF for anything, it'd better be sRGB.
+    skcms_ICCProfile srgb = skcms_sRGB_profile;
+
+    // First, test skcms_OptimizeForSpeed() is a no-op if the has_poly_tf bits are already set.
+    for (int i = 0; i < 3; i++) {
+        expect(srgb.has_poly_tf[i]);
+        srgb.poly_tf[i] = (skcms_PolyTF){0,0,0,0};
+    }
+    skcms_OptimizeForSpeed(&srgb);
+    for (int i = 0; i < 3; i++) {
+        expect(srgb.has_poly_tf[i]);
+        expect(srgb.poly_tf[i].A == 0);
+        expect(srgb.poly_tf[i].B == 0);
+        expect(srgb.poly_tf[i].C == 0);
+        expect(srgb.poly_tf[i].D == 0);
+    }
+
+    // Now clear the has-bits and re-optimize.
+    for (int i = 0; i < 3; i++) {
+        srgb.has_poly_tf[i] = false;
+    }
+    skcms_OptimizeForSpeed(&srgb);
+    for (int i = 0; i < 3; i++) {
+        expect(srgb.has_poly_tf[i]);
+        expect(srgb.poly_tf[i].A == 0.293833881617f);
+        expect(srgb.poly_tf[i].B == 0.704207003117f);
+        expect(srgb.poly_tf[i].C == 0.077399380505f);
+        expect(srgb.poly_tf[i].D == 0.040449999273f);
+    }
+
+    // Mostly a reminder to update skcms_sRGB_profile when skcms_OptimizeForSpeed() changes.
+    expect(0 == memcmp(&srgb, &skcms_sRGB_profile, sizeof(srgb)));
+}
+
 int main(int argc, char** argv) {
     bool regenTestData = false;
     for (int i = 1; i < argc; ++i) {
@@ -1048,6 +1083,7 @@
     test_TRC_Table16();
     test_Premul();
     test_EnsureUsableAsDestination();
+    test_sRGB_profile_has_poly_tf();
 #if 0
     test_CLUT();
 #endif