Web Inspector: support `console.screenshot` with ImageData and ImageBitmap
authordrousso@apple.com <drousso@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 25 Jul 2019 02:25:05 +0000 (02:25 +0000)
committerdrousso@apple.com <drousso@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 25 Jul 2019 02:25:05 +0000 (02:25 +0000)
https://bugs.webkit.org/show_bug.cgi?id=199304

Reviewed by Joseph Pecoraro.

Source/WebCore:

Test: inspector/console/console-screenshot.html

* page/PageConsoleClient.cpp:
(WebCore::PageConsoleClient::screenshot):

LayoutTests:

* inspector/console/console-screenshot.html:
* inspector/console/console-screenshot-expected.txt:

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@247812 268f45cc-cd09-0410-ab3c-d52691b4dbfc

LayoutTests/ChangeLog
LayoutTests/inspector/console/console-screenshot-expected.txt
LayoutTests/inspector/console/console-screenshot.html
Source/WebCore/ChangeLog
Source/WebCore/page/PageConsoleClient.cpp

index c4847e4..8da9cfb 100644 (file)
@@ -1,3 +1,13 @@
+2019-07-24  Devin Rousso  <drousso@apple.com>
+
+        Web Inspector: support `console.screenshot` with ImageData and ImageBitmap
+        https://bugs.webkit.org/show_bug.cgi?id=199304
+
+        Reviewed by Joseph Pecoraro.
+
+        * inspector/console/console-screenshot.html:
+        * inspector/console/console-screenshot-expected.txt:
+
 2019-07-24  Youenn Fablet  <youenn@apple.com>
 
         Disable speculative loading if cache is not to be used for the load
index 0fc00f0..7f22f50 100644 (file)
@@ -1,6 +1,8 @@
 CONSOLE MESSAGE: [object HTMLDivElement]
 CONSOLE MESSAGE: [object HTMLDivElement]
 CONSOLE MESSAGE: [object HTMLDivElement]
+CONSOLE MESSAGE: [object ImageData]
+CONSOLE MESSAGE: [object ImageBitmap]
 CONSOLE MESSAGE: [object CanvasRenderingContext2D]
 CONSOLE MESSAGE: Viewport
 CONSOLE MESSAGE: Viewport
@@ -23,6 +25,18 @@ PASS: The image height should be 2px.
 -- Running test case: console.screenshot.Node.DetachedNonScreenshotable
 PASS: Could not capture screenshot
 
+-- Running test case: console.screenshot.ImageData
+PASS: The added message should be an image.
+PASS: The image should not be empty.
+PASS: The image width should be 2px.
+PASS: The image height should be 2px.
+
+-- Running test case: console.screenshot.ImageBitmap
+PASS: The added message should be an image.
+PASS: The image should not be empty.
+PASS: The image width should be 2px.
+PASS: The image height should be 2px.
+
 -- Running test case: console.screenshot.CanvasRenderingContext2D
 PASS: The added message should be an image.
 PASS: The image should not be empty.
index f34714b..53b4928 100644 (file)
@@ -11,6 +11,16 @@ function createDetachedTest()
     return div;
 }
 
+function testImageBitmap() {
+    // 2x2 red square
+    let image = document.createElement("img");
+    image.src = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAYAAABytg0kAAAAAXNSR0IArs4c6QAAABNJREFUCB1j/M/AAEQMDEwgAgQAHxcCAmtAm/sAAAAASUVORK5CYII=";
+    image.addEventListener("load", async (event) => {
+        let imageBitmap = await createImageBitmap(image);
+        console.screenshot(imageBitmap);
+    });
+}
+
 function createCanvas2DTest() {
     let canvas = document.createElement("canvas");
     canvas.width = 2;
@@ -83,6 +93,30 @@ function test()
     });
 
     addTest({
+        name: "console.screenshot.ImageData",
+        expression: `console.screenshot(new ImageData(2, 2))`,
+        async imageMessageAddedCallback(message) {
+            InspectorTest.expectNotEqual(message.messageText, "data:", "The image should not be empty.");
+
+            let img = await WI.ImageUtilities.promisifyLoad(message.messageText);
+            InspectorTest.expectEqual(img.width, 2, "The image width should be 2px.");
+            InspectorTest.expectEqual(img.height, 2, "The image height should be 2px.");
+        },
+    });
+
+    addTest({
+        name: "console.screenshot.ImageBitmap",
+        expression: `testImageBitmap()`,
+        async imageMessageAddedCallback(message) {
+            InspectorTest.expectNotEqual(message.messageText, "data:", "The image should not be empty.");
+
+            let img = await WI.ImageUtilities.promisifyLoad(message.messageText);
+            InspectorTest.expectEqual(img.width, 2, "The image width should be 2px.");
+            InspectorTest.expectEqual(img.height, 2, "The image height should be 2px.");
+        },
+    });
+
+    addTest({
         name: "console.screenshot.CanvasRenderingContext2D",
         expression: `console.screenshot(createCanvas2DTest())`,
         async imageMessageAddedCallback(message) {
index b89b57c..70d0c77 100644 (file)
@@ -1,5 +1,17 @@
 2019-07-24  Devin Rousso  <drousso@apple.com>
 
+        Web Inspector: support `console.screenshot` with ImageData and ImageBitmap
+        https://bugs.webkit.org/show_bug.cgi?id=199304
+
+        Reviewed by Joseph Pecoraro.
+
+        Test: inspector/console/console-screenshot.html
+
+        * page/PageConsoleClient.cpp:
+        (WebCore::PageConsoleClient::screenshot):
+
+2019-07-24  Devin Rousso  <drousso@apple.com>
+
         Web Inspector: REGRESSION: Overlay: highlighting a ::before/::after shows the node info of the parent instead
         https://bugs.webkit.org/show_bug.cgi?id=200051
 
index 0d3c2cb..8067b63 100644 (file)
 #include "Frame.h"
 #include "FrameSnapshotting.h"
 #include "HTMLCanvasElement.h"
+#include "ImageBitmap.h"
 #include "ImageBitmapRenderingContext.h"
 #include "ImageBuffer.h"
+#include "ImageData.h"
 #include "InspectorController.h"
 #include "InspectorInstrumentation.h"
 #include "IntRect.h"
 #include "JSCanvasRenderingContext2D.h"
 #include "JSExecState.h"
 #include "JSHTMLCanvasElement.h"
+#include "JSImageBitmap.h"
 #include "JSImageBitmapRenderingContext.h"
+#include "JSImageData.h"
 #include "JSNode.h"
 #include "JSOffscreenCanvas.h"
 #include "Node.h"
@@ -284,6 +288,22 @@ void PageConsoleClient::screenshot(JSC::ExecState* state, Ref<ScriptArguments>&&
                 if (auto snapshot = WebCore::snapshotNode(m_page.mainFrame(), *node))
                     dataURL = snapshot->toDataURL("image/png"_s, WTF::nullopt, PreserveResolution::Yes);
             }
+        } else if (auto* imageData = JSImageData::toWrapped(state->vm(), possibleTarget)) {
+            target = possibleTarget;
+            if (UNLIKELY(InspectorInstrumentation::hasFrontends())) {
+                auto sourceSize = imageData->size();
+                if (auto imageBuffer = ImageBuffer::create(sourceSize, RenderingMode::Unaccelerated)) {
+                    IntRect sourceRect(IntPoint(), sourceSize);
+                    imageBuffer->putByteArray(*imageData->data(), AlphaPremultiplication::Unpremultiplied, sourceSize, sourceRect, IntPoint());
+                    dataURL = imageBuffer->toDataURL("image/png"_s, WTF::nullopt, PreserveResolution::Yes);
+                }
+            }
+        } else if (auto* imageBitmap = JSImageBitmap::toWrapped(state->vm(), possibleTarget)) {
+            target = possibleTarget;
+            if (UNLIKELY(InspectorInstrumentation::hasFrontends())) {
+                if (auto* imageBuffer = imageBitmap->buffer())
+                    dataURL = imageBuffer->toDataURL("image/png"_s, WTF::nullopt, PreserveResolution::Yes);
+            }
         } else if (auto* context = canvasRenderingContext(state->vm(), possibleTarget)) {
             auto& canvas = context->canvasBase();
             if (is<HTMLCanvasElement>(canvas)) {