<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[277574] trunk</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.webkit.org/projects/webkit/changeset/277574">277574</a></dd>
<dt>Author</dt> <dd>weinig@apple.com</dd>
<dt>Date</dt> <dd>2021-05-16 15:00:30 -0700 (Sun, 16 May 2021)</dd>
</dl>

<h3>Log Message</h3>
<pre>Support serializing ImageData object colorSpace property in SerializedScriptValue
https://bugs.webkit.org/show_bug.cgi?id=225854

Reviewed by Chris Dumez.

Source/WebCore:

Test: storage/indexeddb/structured-clone-image-data-display-p3.html

Update SerializedScriptValue schema to version 8 and add serialization
of the colorSpace property.

* bindings/js/SerializedScriptValue.cpp:
(WebCore::PredefinedColorSpaceTag):
Add copy of the PredefinedColorSpace values that are guaranteed to
stay consistent forever.

(WebCore::CloneSerializer::dumpIfTerminal):
Add encoding of the colorSpace when serializing ImageData.

(WebCore::CloneSerializer::write):
(WebCore::CloneDeserializer::read):
Add coders for PredefinedColorSpaceTag.

(WebCore::CloneDeserializer::getJSValue):
Replace multiple overloads with one perfect forwarding overload which does
what we want and works with r-values.

(WebCore::CloneDeserializer::readImageBitmap):
Use auto in two places.

(WebCore::CloneDeserializer::readTerminal):
Add decoding of the colorSpace when deserializing ImageData. For prior versions,
the colorSpace defaults to sRGB, which was the only color space supported.

LayoutTests:

Updates existing tests for new serialization version (version 8) and support
for serializing color space in ImageData.

To avoid multiple result files due to lack of support for display-p3, testing
that the display-p3 color space is serialized correctly was put into its own
test that is skipped on non-supporting platforms.

* platform/glib/TestExpectations:
* platform/win/TestExpectations:
Skip structured-clone-image-data-display-p3.html on platforms that don't support
display-p3 color spaces.

* fast/storage/serialized-script-value.html:
Bump version to version 8.

* storage/indexeddb/resources/structured-clone.js:
* storage/indexeddb/structured-clone-expected.txt:
Add tests for ImageData colorSpace round tripping.

* storage/indexeddb/structured-clone-image-data-display-p3-expected.txt: Added.
* storage/indexeddb/structured-clone-image-data-display-p3.html: Added.
Add test for ImageData display-p3 colorSpace round tripping as its own test
to allow targetted skipping by non-supporting platforms.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkLayoutTestsChangeLog">trunk/LayoutTests/ChangeLog</a></li>
<li><a href="#trunkLayoutTestsfaststorageserializedscriptvaluehtml">trunk/LayoutTests/fast/storage/serialized-script-value.html</a></li>
<li><a href="#trunkLayoutTestsplatformglibTestExpectations">trunk/LayoutTests/platform/glib/TestExpectations</a></li>
<li><a href="#trunkLayoutTestsplatformwinTestExpectations">trunk/LayoutTests/platform/win/TestExpectations</a></li>
<li><a href="#trunkLayoutTestsstorageindexeddbresourcesstructuredclonejs">trunk/LayoutTests/storage/indexeddb/resources/structured-clone.js</a></li>
<li><a href="#trunkLayoutTestsstorageindexeddbstructuredcloneexpectedtxt">trunk/LayoutTests/storage/indexeddb/structured-clone-expected.txt</a></li>
<li><a href="#trunkSourceWebCoreChangeLog">trunk/Source/WebCore/ChangeLog</a></li>
<li><a href="#trunkSourceWebCorebindingsjsSerializedScriptValuecpp">trunk/Source/WebCore/bindings/js/SerializedScriptValue.cpp</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#trunkLayoutTestsstorageindexeddbstructuredcloneimagedatadisplayp3expectedtxt">trunk/LayoutTests/storage/indexeddb/structured-clone-image-data-display-p3-expected.txt</a></li>
<li><a href="#trunkLayoutTestsstorageindexeddbstructuredcloneimagedatadisplayp3html">trunk/LayoutTests/storage/indexeddb/structured-clone-image-data-display-p3.html</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkLayoutTestsChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/LayoutTests/ChangeLog (277573 => 277574)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/ChangeLog      2021-05-16 20:32:30 UTC (rev 277573)
+++ trunk/LayoutTests/ChangeLog 2021-05-16 22:00:30 UTC (rev 277574)
</span><span class="lines">@@ -1,5 +1,36 @@
</span><span class="cx"> 2021-05-16  Sam Weinig  <weinig@apple.com>
</span><span class="cx"> 
</span><ins>+        Support serializing ImageData object colorSpace property in SerializedScriptValue
+        https://bugs.webkit.org/show_bug.cgi?id=225854
+
+        Reviewed by Chris Dumez.
+
+        Updates existing tests for new serialization version (version 8) and support
+        for serializing color space in ImageData.
+
+        To avoid multiple result files due to lack of support for display-p3, testing
+        that the display-p3 color space is serialized correctly was put into its own
+        test that is skipped on non-supporting platforms.
+
+        * platform/glib/TestExpectations:
+        * platform/win/TestExpectations:
+        Skip structured-clone-image-data-display-p3.html on platforms that don't support
+        display-p3 color spaces.
+
+        * fast/storage/serialized-script-value.html:
+        Bump version to version 8.
+
+        * storage/indexeddb/resources/structured-clone.js:
+        * storage/indexeddb/structured-clone-expected.txt:
+        Add tests for ImageData colorSpace round tripping.
+
+        * storage/indexeddb/structured-clone-image-data-display-p3-expected.txt: Added.
+        * storage/indexeddb/structured-clone-image-data-display-p3.html: Added.
+        Add test for ImageData display-p3 colorSpace round tripping as its own test
+        to allow targetted skipping by non-supporting platforms.
+
+2021-05-16  Sam Weinig  <weinig@apple.com>
+
</ins><span class="cx">         Add support for creating/accessing/setting non-sRGB ImageData via canvas
</span><span class="cx">         https://bugs.webkit.org/show_bug.cgi?id=225841
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkLayoutTestsfaststorageserializedscriptvaluehtml"></a>
<div class="modfile"><h4>Modified: trunk/LayoutTests/fast/storage/serialized-script-value.html (277573 => 277574)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/fast/storage/serialized-script-value.html      2021-05-16 20:32:30 UTC (rev 277573)
+++ trunk/LayoutTests/fast/storage/serialized-script-value.html 2021-05-16 22:00:30 UTC (rev 277574)
</span><span class="lines">@@ -6,7 +6,7 @@
</span><span class="cx">     <body>
</span><span class="cx">         <script>
</span><span class="cx"> 
</span><del>-const currentVersion = 0x07;
</del><ins>+const currentVersion = 0x08;
</ins><span class="cx"> 
</span><span class="cx"> // Here's a little Q&D helper for future adventurers needing to rebaseline this.
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkLayoutTestsplatformglibTestExpectations"></a>
<div class="modfile"><h4>Modified: trunk/LayoutTests/platform/glib/TestExpectations (277573 => 277574)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/platform/glib/TestExpectations 2021-05-16 20:32:30 UTC (rev 277573)
+++ trunk/LayoutTests/platform/glib/TestExpectations    2021-05-16 22:00:30 UTC (rev 277574)
</span><span class="lines">@@ -2357,10 +2357,13 @@
</span><span class="cx"> # It was a flaky crash until r275516, but it's still a timeout flaky.
</span><span class="cx"> webkit.org/b/224112 media/video-as-img-output-pts.html [ Timeout Pass ]
</span><span class="cx"> 
</span><del>-webkit.org/b/225423 fast/canvas/canvas-color-space-display-p3.html [ ImageOnlyFailure ]
-webkit.org/b/225423 fast/canvas/canvas-color-space-display-p3-ImageData.html [ Failure ]
</del><span class="cx"> webkit.org/b/225423 fast/canvas/canvas-composite-text-alpha.html [ Failure ]
</span><span class="cx"> 
</span><ins>+# Non-sRGB color spaces are not currently supported by the glib ports.
+fast/canvas/canvas-color-space-display-p3.html [ Skip ]
+fast/canvas/canvas-color-space-display-p3-ImageData.html [ Skip ]
+storage/indexeddb/structured-clone-image-data-display-p3.html [ Skip ]
+
</ins><span class="cx"> # Failing after r277073.
</span><span class="cx"> webkit.org/b/225462 imported/w3c/web-platform-tests/css/css-backgrounds/background-repeat-space-10.html [ ImageOnlyFailure ]
</span><span class="cx"> webkit.org/b/225462 imported/w3c/web-platform-tests/css/css-backgrounds/background-repeat-space-1a.html [ ImageOnlyFailure ]
</span></span></pre></div>
<a id="trunkLayoutTestsplatformwinTestExpectations"></a>
<div class="modfile"><h4>Modified: trunk/LayoutTests/platform/win/TestExpectations (277573 => 277574)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/platform/win/TestExpectations  2021-05-16 20:32:30 UTC (rev 277573)
+++ trunk/LayoutTests/platform/win/TestExpectations     2021-05-16 22:00:30 UTC (rev 277574)
</span><span class="lines">@@ -4635,6 +4635,7 @@
</span><span class="cx"> 
</span><span class="cx"> fast/text/line-break-with-locale.html [ ImageOnlyFailure ]
</span><span class="cx"> 
</span><del>-# Non-SRGB color spaces are not currently supported by the Windows port.
</del><ins>+# Non-sRGB color spaces are not currently supported by the Windows port.
</ins><span class="cx"> fast/canvas/canvas-color-space-display-p3.html [ Skip ]
</span><span class="cx"> fast/canvas/canvas-color-space-display-p3-ImageData.html [ Skip ]
</span><ins>+storage/indexeddb/structured-clone-image-data-display-p3.html [ Skip ]
</ins></span></pre></div>
<a id="trunkLayoutTestsstorageindexeddbresourcesstructuredclonejs"></a>
<div class="modfile"><h4>Modified: trunk/LayoutTests/storage/indexeddb/resources/structured-clone.js (277573 => 277574)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/storage/indexeddb/resources/structured-clone.js        2021-05-16 20:32:30 UTC (rev 277573)
+++ trunk/LayoutTests/storage/indexeddb/resources/structured-clone.js   2021-05-16 22:00:30 UTC (rev 277574)
</span><span class="lines">@@ -312,29 +312,38 @@
</span><span class="cx"> function testImageData(callback)
</span><span class="cx"> {
</span><span class="cx">     debug("Testing ImageData");
</span><del>-    evalAndLog("canvas = document.createElement('canvas')");
-    evalAndLog("canvas.width = 8");
-    evalAndLog("canvas.height = 8");
-    evalAndLog("test_data = canvas.getContext('2d').getImageData(0, 0, 8, 8)");
</del><ins>+    function testImageData(colorSpace, callback) {
+        debug(`Testing ImageData: { colorSpace: ${colorSpace} }`);
+        evalAndLog("canvas = document.createElement('canvas')");
+        evalAndLog("canvas.width = 8");
+        evalAndLog("canvas.height = 8");
+        evalAndLog(`test_data = canvas.getContext('2d').getImageData(0, 0, 8, 8, { colorSpace: ${colorSpace} })`);
</ins><span class="cx"> 
</span><del>-    for (var i = 0; i < 256; ++i) {
-        test_data.data[i] = i;
</del><ins>+        for (var i = 0; i < 256; ++i) {
+            test_data.data[i] = i;
+        }
+
+        testValue(test_data, function(result) {
+            self.result = result;
+            shouldBeTrue("test_data !== result");
+            shouldBeEqualToString("Object.prototype.toString.call(result)", "[object ImageData]");
+            shouldBe("result.width", "test_data.width");
+            shouldBe("result.height", "test_data.height");
+            shouldBe("result.data.length", "test_data.data.length");
+            shouldBe("result.colorSpace", "test_data.colorSpace");
+            if (arrayCompare(test_data.data, result.data)) {
+                testPassed("result data matches");
+            } else {
+                testFailed("result data doesn't match");
+            }
+            callback();
+        });
</ins><span class="cx">     }
</span><span class="cx"> 
</span><del>-    testValue(test_data, function(result) {
-        self.result = result;
-        shouldBeTrue("test_data !== result");
-        shouldBeEqualToString("Object.prototype.toString.call(result)", "[object ImageData]");
-        shouldBe("result.width", "test_data.width");
-        shouldBe("result.height", "test_data.height");
-        shouldBe("result.data.length", "test_data.data.length");
-        if (arrayCompare(test_data.data, result.data)) {
-            testPassed("result data matches");
-        } else {
-            testFailed("result data doesn't match");
-        }
-        callback();
-    });
</del><ins>+    forEachWithCallback(testImageData, [
+        `"srgb"`,
+        `undefined`
+    ], callback);
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> function readBlobAsText(blob, callback)
</span></span></pre></div>
<a id="trunkLayoutTestsstorageindexeddbstructuredcloneexpectedtxt"></a>
<div class="modfile"><h4>Modified: trunk/LayoutTests/storage/indexeddb/structured-clone-expected.txt (277573 => 277574)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/storage/indexeddb/structured-clone-expected.txt        2021-05-16 20:32:30 UTC (rev 277573)
+++ trunk/LayoutTests/storage/indexeddb/structured-clone-expected.txt   2021-05-16 22:00:30 UTC (rev 277574)
</span><span class="lines">@@ -583,10 +583,11 @@
</span><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx"> Testing ImageData
</span><ins>+Testing ImageData: { colorSpace: "srgb" }
</ins><span class="cx"> canvas = document.createElement('canvas')
</span><span class="cx"> canvas.width = 8
</span><span class="cx"> canvas.height = 8
</span><del>-test_data = canvas.getContext('2d').getImageData(0, 0, 8, 8)
</del><ins>+test_data = canvas.getContext('2d').getImageData(0, 0, 8, 8, { colorSpace: "srgb" })
</ins><span class="cx"> transaction = db.transaction('storeName', 'readwrite')
</span><span class="cx"> store = transaction.objectStore('storeName')
</span><span class="cx"> store.put(value, 'key')
</span><span class="lines">@@ -596,7 +597,24 @@
</span><span class="cx"> PASS result.width is test_data.width
</span><span class="cx"> PASS result.height is test_data.height
</span><span class="cx"> PASS result.data.length is test_data.data.length
</span><ins>+PASS result.colorSpace is test_data.colorSpace
</ins><span class="cx"> PASS result data matches
</span><ins>+Testing ImageData: { colorSpace: undefined }
+canvas = document.createElement('canvas')
+canvas.width = 8
+canvas.height = 8
+test_data = canvas.getContext('2d').getImageData(0, 0, 8, 8, { colorSpace: undefined })
+transaction = db.transaction('storeName', 'readwrite')
+store = transaction.objectStore('storeName')
+store.put(value, 'key')
+store.get('key')
+PASS test_data !== result is true
+PASS Object.prototype.toString.call(result) is "[object ImageData]"
+PASS result.width is test_data.width
+PASS result.height is test_data.height
+PASS result.data.length is test_data.data.length
+PASS result.colorSpace is test_data.colorSpace
+PASS result data matches
</ins><span class="cx"> 
</span><span class="cx"> Testing Blob
</span><span class="cx"> PASS FileReader != null is true
</span></span></pre></div>
<a id="trunkLayoutTestsstorageindexeddbstructuredcloneimagedatadisplayp3expectedtxt"></a>
<div class="addfile"><h4>Added: trunk/LayoutTests/storage/indexeddb/structured-clone-image-data-display-p3-expected.txt (0 => 277574)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/storage/indexeddb/structured-clone-image-data-display-p3-expected.txt                          (rev 0)
+++ trunk/LayoutTests/storage/indexeddb/structured-clone-image-data-display-p3-expected.txt     2021-05-16 22:00:30 UTC (rev 277574)
</span><span class="lines">@@ -0,0 +1,33 @@
</span><ins>+Test structured clone of ImageData with a display-p3 colorSpace.
+
+On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE".
+
+
+indexedDB = self.indexedDB || self.webkitIndexedDB || self.mozIndexedDB || self.msIndexedDB || self.OIndexedDB;
+
+indexedDB.deleteDatabase(dbname)
+indexedDB.open(dbname)
+store = db.createObjectStore('storeName')
+This index is not used, but evaluating key path on each put() call will exercise (de)serialization:
+store.createIndex('indexName', 'dummyKeyPath')
+
+Testing ImageData: { colorSpace: "display-p3" }
+canvas = document.createElement('canvas')
+canvas.width = 8
+canvas.height = 8
+test_data = canvas.getContext('2d').getImageData(0, 0, 8, 8, { colorSpace: "display-p3" })
+transaction = db.transaction('storeName', 'readwrite')
+store = transaction.objectStore('storeName')
+store.put(value, 'key')
+store.get('key')
+PASS test_data !== result is true
+PASS Object.prototype.toString.call(result) is "[object ImageData]"
+PASS result.width is test_data.width
+PASS result.height is test_data.height
+PASS result.data.length is test_data.data.length
+PASS result.colorSpace is test_data.colorSpace
+PASS result data matches
+PASS successfullyParsed is true
+
+TEST COMPLETE
+
</ins></span></pre></div>
<a id="trunkLayoutTestsstorageindexeddbstructuredcloneimagedatadisplayp3html"></a>
<div class="addfile"><h4>Added: trunk/LayoutTests/storage/indexeddb/structured-clone-image-data-display-p3.html (0 => 277574)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/storage/indexeddb/structured-clone-image-data-display-p3.html                          (rev 0)
+++ trunk/LayoutTests/storage/indexeddb/structured-clone-image-data-display-p3.html     2021-05-16 22:00:30 UTC (rev 277574)
</span><span class="lines">@@ -0,0 +1,93 @@
</span><ins>+<html>
+<head>
+<script src="../../resources/js-test.js"></script>
+<script src="resources/shared.js"></script>
+</head>
+<body>
+<script>
+    description("Test structured clone of ImageData with a display-p3 colorSpace.");
+
+    indexedDBTest(prepareDatabase, startTests);
+
+    function prepareDatabase()
+    {
+        db = event.target.result;
+        evalAndLog("store = db.createObjectStore('storeName')");
+        debug("This index is not used, but evaluating key path on each put() call will exercise (de)serialization:");
+        evalAndLog("store.createIndex('indexName', 'dummyKeyPath')");
+    }
+    
+    function testValue(value, callback)
+    {
+        // One transaction per test, since some tests require asynchronous
+        // operations to verify the result (e.g. via FileReader)
+        evalAndLog("transaction = db.transaction('storeName', 'readwrite')");
+        transaction.onerror = unexpectedErrorCallback;
+        transaction.onabort = unexpectedAbortCallback;
+        evalAndLog("store = transaction.objectStore('storeName')");
+
+        self.value = value;
+        request = evalAndLog("store.put(value, 'key')");
+        request.onerror = unexpectedErrorCallback;
+        request.onsuccess = function(e) {
+            request = evalAndLog("store.get('key')");
+            request.onerror = unexpectedErrorCallback;
+            request.onsuccess = function(e) {
+                callback(request.result);
+            };
+        };
+    }
+
+    // Identity testing, sensitive to NaN and -0
+    function is(x, y) {
+        if (x === y) {
+            return x !== 0 || 1 / x === 1 / y;
+        }
+        return x !== x && y !== y;
+    }
+
+    function arrayCompare(a, b) {
+        if (a.length !== b.length) {
+            return false;
+        }
+        for (var i = 0; i < a.length; ++i) {
+            if (!is(a[i], b[i])) {
+                return false;
+            }
+        }
+        return true;
+    }
+
+    function startTests()
+    {
+        debug("");
+        debug(`Testing ImageData: { colorSpace: "display-p3" }`);
+        evalAndLog("canvas = document.createElement('canvas')");
+        evalAndLog("canvas.width = 8");
+        evalAndLog("canvas.height = 8");
+        evalAndLog(`test_data = canvas.getContext('2d').getImageData(0, 0, 8, 8, { colorSpace: "display-p3" })`);
+    
+        for (var i = 0; i < 256; ++i) {
+            test_data.data[i] = i;
+        }
+
+        testValue(test_data, function(result) {
+            self.result = result;
+            shouldBeTrue("test_data !== result");
+            shouldBeEqualToString("Object.prototype.toString.call(result)", "[object ImageData]");
+            shouldBe("result.width", "test_data.width");
+            shouldBe("result.height", "test_data.height");
+            shouldBe("result.data.length", "test_data.data.length");
+            shouldBe("result.colorSpace", "test_data.colorSpace");
+            if (arrayCompare(test_data.data, result.data)) {
+                testPassed("result data matches");
+            } else {
+                testFailed("result data doesn't match");
+            }
+            
+            finishJSTest();
+        });
+    }
+</script>
+</body>
+</html>
</ins></span></pre></div>
<a id="trunkSourceWebCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/ChangeLog (277573 => 277574)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/ChangeLog   2021-05-16 20:32:30 UTC (rev 277573)
+++ trunk/Source/WebCore/ChangeLog      2021-05-16 22:00:30 UTC (rev 277574)
</span><span class="lines">@@ -1,3 +1,38 @@
</span><ins>+2021-05-16  Sam Weinig  <weinig@apple.com>
+
+        Support serializing ImageData object colorSpace property in SerializedScriptValue
+        https://bugs.webkit.org/show_bug.cgi?id=225854
+
+        Reviewed by Chris Dumez.
+
+        Test: storage/indexeddb/structured-clone-image-data-display-p3.html
+
+        Update SerializedScriptValue schema to version 8 and add serialization
+        of the colorSpace property.
+
+        * bindings/js/SerializedScriptValue.cpp:
+        (WebCore::PredefinedColorSpaceTag):
+        Add copy of the PredefinedColorSpace values that are guaranteed to
+        stay consistent forever.
+
+        (WebCore::CloneSerializer::dumpIfTerminal):
+        Add encoding of the colorSpace when serializing ImageData.
+
+        (WebCore::CloneSerializer::write):
+        (WebCore::CloneDeserializer::read):
+        Add coders for PredefinedColorSpaceTag.
+
+        (WebCore::CloneDeserializer::getJSValue):
+        Replace multiple overloads with one perfect forwarding overload which does
+        what we want and works with r-values.
+
+        (WebCore::CloneDeserializer::readImageBitmap):
+        Use auto in two places.
+
+        (WebCore::CloneDeserializer::readTerminal):
+        Add decoding of the colorSpace when deserializing ImageData. For prior versions,
+        the colorSpace defaults to sRGB, which was the only color space supported.
+
</ins><span class="cx"> 2021-05-14  Jer Noble  <jer.noble@apple.com>
</span><span class="cx"> 
</span><span class="cx">         MediaSessionCoordinator survives page reload/navigation
</span></span></pre></div>
<a id="trunkSourceWebCorebindingsjsSerializedScriptValuecpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/bindings/js/SerializedScriptValue.cpp (277573 => 277574)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/bindings/js/SerializedScriptValue.cpp       2021-05-16 20:32:30 UTC (rev 277573)
+++ trunk/Source/WebCore/bindings/js/SerializedScriptValue.cpp  2021-05-16 22:00:30 UTC (rev 277574)
</span><span class="lines">@@ -233,9 +233,15 @@
</span><span class="cx">     default:
</span><span class="cx">         return 0;
</span><span class="cx">     }
</span><del>-
</del><span class="cx"> }
</span><span class="cx"> 
</span><ins>+enum class PredefinedColorSpaceTag : uint8_t {
+    SRGB = 0
+#if ENABLE(DESTINATION_COLOR_SPACE_DISPLAY_P3)
+    , DisplayP3 = 1
+#endif
+};
+
</ins><span class="cx"> #if ENABLE(WEB_CRYPTO)
</span><span class="cx"> 
</span><span class="cx"> const uint32_t currentKeyFormatVersion = 1;
</span><span class="lines">@@ -314,8 +320,9 @@
</span><span class="cx">  * Version 5. added support for Map and Set types.
</span><span class="cx">  * Version 6. added support for 8-bit strings.
</span><span class="cx">  * Version 7. added support for File's lastModified attribute.
</span><ins>+ * Version 8. added support for ImageData's colorSpace attribute.
</ins><span class="cx">  */
</span><del>-static const unsigned CurrentVersion = 7;
</del><ins>+static const unsigned CurrentVersion = 8;
</ins><span class="cx"> static const unsigned TerminatorTag = 0xFFFFFFFF;
</span><span class="cx"> static const unsigned StringPoolTag = 0xFFFFFFFE;
</span><span class="cx"> static const unsigned NonIndexPropertiesTag = 0xFFFFFFFD;
</span><span class="lines">@@ -423,7 +430,7 @@
</span><span class="cx">  *    FileListTag <length:uint32_t>(<file:FileData>){length}
</span><span class="cx">  *
</span><span class="cx">  * ImageData :-
</span><del>- *    ImageDataTag <width:int32_t><height:int32_t><length:uint32_t><data:uint8_t{length}>
</del><ins>+ *    ImageDataTag <width:int32_t> <height:int32_t> <length:uint32_t> <data:uint8_t{length}> <colorSpace:PredefinedColorSpaceTag>
</ins><span class="cx">  *
</span><span class="cx">  * Blob :-
</span><span class="cx">  *    BlobTag <url:StringData><type:StringData><size:long long>
</span><span class="lines">@@ -1229,6 +1236,7 @@
</span><span class="cx">                 write(data->height());
</span><span class="cx">                 write(data->data().length());
</span><span class="cx">                 write(data->data().data(), data->data().length());
</span><ins>+                write(data->colorSpace());
</ins><span class="cx">                 return true;
</span><span class="cx">             }
</span><span class="cx">             if (auto* regExp = jsDynamicCast<RegExpObject*>(vm, obj)) {
</span><span class="lines">@@ -1558,6 +1566,20 @@
</span><span class="cx">         write(static_cast<double>(file.lastModifiedOverride().valueOr(-1)));
</span><span class="cx">     }
</span><span class="cx"> 
</span><ins>+    void write(PredefinedColorSpace colorSpace)
+    {
+        switch (colorSpace) {
+        case PredefinedColorSpace::SRGB:
+            writeLittleEndian<uint8_t>(m_buffer, static_cast<uint8_t>(PredefinedColorSpaceTag::SRGB));
+            break;
+#if ENABLE(DESTINATION_COLOR_SPACE_DISPLAY_P3)
+        case PredefinedColorSpace::DisplayP3:
+            writeLittleEndian<uint8_t>(m_buffer, static_cast<uint8_t>(PredefinedColorSpaceTag::DisplayP3));
+            break;
+#endif
+        }
+    }
+
</ins><span class="cx"> #if ENABLE(WEB_CRYPTO)
</span><span class="cx">     void write(CryptoAlgorithmIdentifier algorithm)
</span><span class="cx">     {
</span><span class="lines">@@ -2537,6 +2559,26 @@
</span><span class="cx">         return true;
</span><span class="cx">     }
</span><span class="cx"> 
</span><ins>+    bool read(PredefinedColorSpace& result)
+    {
+        uint8_t tag;
+        if (!read(tag))
+            return false;
+
+        switch (static_cast<PredefinedColorSpaceTag>(tag)) {
+        case PredefinedColorSpaceTag::SRGB:
+            result = PredefinedColorSpace::SRGB;
+            return true;
+#if ENABLE(DESTINATION_COLOR_SPACE_DISPLAY_P3)
+        case PredefinedColorSpaceTag::DisplayP3:
+            result = PredefinedColorSpace::DisplayP3;
+            return true;
+#endif
+        default:
+            return false;
+        }
+    }
+
</ins><span class="cx"> #if ENABLE(WEB_CRYPTO)
</span><span class="cx">     bool read(CryptoAlgorithmIdentifier& result)
</span><span class="cx">     {
</span><span class="lines">@@ -2864,18 +2906,12 @@
</span><span class="cx"> #endif
</span><span class="cx"> 
</span><span class="cx">     template<class T>
</span><del>-    JSValue getJSValue(T* nativeObj)
</del><ins>+    JSValue getJSValue(T&& nativeObj)
</ins><span class="cx">     {
</span><del>-        return toJS(m_lexicalGlobalObject, jsCast<JSDOMGlobalObject*>(m_globalObject), nativeObj);
</del><ins>+        return toJS(m_lexicalGlobalObject, jsCast<JSDOMGlobalObject*>(m_globalObject), std::forward<T>(nativeObj));
</ins><span class="cx">     }
</span><span class="cx"> 
</span><span class="cx">     template<class T>
</span><del>-    JSValue getJSValue(T& nativeObj)
-    {
-        return toJS(m_lexicalGlobalObject, jsCast<JSDOMGlobalObject*>(m_globalObject), nativeObj);
-    }
-
-    template<class T>
</del><span class="cx">     JSValue readDOMPoint()
</span><span class="cx">     {
</span><span class="cx">         double x;
</span><span class="lines">@@ -3141,8 +3177,8 @@
</span><span class="cx">             return JSValue();
</span><span class="cx">         }
</span><span class="cx"> 
</span><del>-        IntSize logicalSize = IntSize(logicalWidth, logicalHeight);
-        IntSize imageDataSize = logicalSize;
</del><ins>+        auto logicalSize = IntSize(logicalWidth, logicalHeight);
+        auto imageDataSize = logicalSize;
</ins><span class="cx">         imageDataSize.scale(resolutionScale);
</span><span class="cx"> 
</span><span class="cx">         auto buffer = ImageBitmap::createImageBuffer(*scriptExecutionContextFromExecState(m_lexicalGlobalObject), logicalSize, RenderingMode::Unaccelerated, resolutionScale);
</span><span class="lines">@@ -3374,23 +3410,33 @@
</span><span class="cx">                 fail();
</span><span class="cx">                 return JSValue();
</span><span class="cx">             }
</span><del>-            if (!m_isDOMGlobalObject) {
-                m_ptr += length;
-                return jsNull();
</del><ins>+            auto bufferStart = m_ptr;
+            m_ptr += length;
+
+            auto resultColorSpace = PredefinedColorSpace::SRGB;
+            if (m_version > 7) {
+                if (!read(resultColorSpace))
+                    return JSValue();
</ins><span class="cx">             }
</span><del>-            IntSize imageSize(width, height);
-            RELEASE_ASSERT(!length || (imageSize.area() * 4).unsafeGet() <= length);
-            auto result = ImageData::create(imageSize);
-            if (!result) {
</del><ins>+
+            if (length && (IntSize(width, height).area() * 4).unsafeGet() != length) {
</ins><span class="cx">                 fail();
</span><span class="cx">                 return JSValue();
</span><span class="cx">             }
</span><ins>+
+            if (!m_isDOMGlobalObject)
+                return jsNull();
+
+            auto result = ImageData::createUninitialized(width, height, resultColorSpace);
+            if (result.hasException()) {
+                fail();
+                return JSValue();
+            }
</ins><span class="cx">             if (length)
</span><del>-                memcpy(result->data().data(), m_ptr, length);
</del><ins>+                memcpy(result.returnValue()->data().data(), bufferStart, length);
</ins><span class="cx">             else
</span><del>-                result->data().zeroFill();
-            m_ptr += length;
-            return getJSValue(result.get());
</del><ins>+                result.returnValue()->data().zeroFill();
+            return getJSValue(result.releaseReturnValue());
</ins><span class="cx">         }
</span><span class="cx">         case BlobTag: {
</span><span class="cx">             CachedStringRef url;
</span></span></pre>
</div>
</div>

</body>
</html>