Add support for canvas.toBlob
authoraestes@apple.com <aestes@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Sat, 4 Mar 2017 02:16:58 +0000 (02:16 +0000)
committeraestes@apple.com <aestes@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Sat, 4 Mar 2017 02:16:58 +0000 (02:16 +0000)
https://bugs.webkit.org/show_bug.cgi?id=148878
<rdar://problem/22590406>

Reviewed by Tim Horton.

LayoutTests/imported/w3c:

* web-platform-tests/html/semantics/embedded-content/the-canvas-element/toBlob.jpeg-expected.txt:
* web-platform-tests/html/semantics/embedded-content/the-canvas-element/toBlob.png-expected.txt:

Source/WebCore:

These tests now pass:
imported/w3c/web-platform-tests/html/semantics/embedded-content/the-canvas-element/toBlob.jpeg.html
imported/w3c/web-platform-tests/html/semantics/embedded-content/the-canvas-element/toBlob.png.html

* CMakeLists.txt:
* DerivedSources.make:
* WebCore.xcodeproj/project.pbxproj:
* fileapi/BlobCallback.cpp: Added.
(WebCore::BlobCallback::scheduleCallback):
* fileapi/BlobCallback.h: Added.
(WebCore::BlobCallback::~BlobCallback):
* fileapi/BlobCallback.idl: Added.
* html/HTMLCanvasElement.cpp:
(WebCore::HTMLCanvasElement::toBlob):
* html/HTMLCanvasElement.h:
* html/HTMLCanvasElement.idl:
* platform/graphics/ImageBuffer.h:
* platform/graphics/cairo/ImageBufferCairo.cpp:
(WebCore::writeFunction):
(WebCore::encodeImage):
(WebCore::ImageBuffer::toDataURL):
(WebCore::ImageBuffer::toData):
* platform/graphics/cg/ImageBufferCG.cpp:
(WebCore::data):
(WebCore::ImageBuffer::toDataURL):
(WebCore::ImageBuffer::toData):
(WebCore::ImageBuffer::toCGImage):
(WebCore::cgImage):
(WebCore::dataURL):
* platform/graphics/gtk/ImageBufferGtk.cpp:
(WebCore::ImageBuffer::toDataURL):
(WebCore::ImageBuffer::toData):
* platform/graphics/win/ImageBufferDirect2D.cpp:
(WebCore::ImageBuffer::toData):

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

18 files changed:
LayoutTests/imported/w3c/ChangeLog
LayoutTests/imported/w3c/web-platform-tests/html/semantics/embedded-content/the-canvas-element/toBlob.jpeg-expected.txt
LayoutTests/imported/w3c/web-platform-tests/html/semantics/embedded-content/the-canvas-element/toBlob.png-expected.txt
Source/WebCore/CMakeLists.txt
Source/WebCore/ChangeLog
Source/WebCore/DerivedSources.make
Source/WebCore/WebCore.xcodeproj/project.pbxproj
Source/WebCore/fileapi/BlobCallback.cpp [new file with mode: 0644]
Source/WebCore/fileapi/BlobCallback.h [new file with mode: 0644]
Source/WebCore/fileapi/BlobCallback.idl [new file with mode: 0644]
Source/WebCore/html/HTMLCanvasElement.cpp
Source/WebCore/html/HTMLCanvasElement.h
Source/WebCore/html/HTMLCanvasElement.idl
Source/WebCore/platform/graphics/ImageBuffer.h
Source/WebCore/platform/graphics/cairo/ImageBufferCairo.cpp
Source/WebCore/platform/graphics/cg/ImageBufferCG.cpp
Source/WebCore/platform/graphics/gtk/ImageBufferGtk.cpp
Source/WebCore/platform/graphics/win/ImageBufferDirect2D.cpp

index 33d7d9e..3817f5a 100644 (file)
@@ -1,3 +1,14 @@
+2017-03-03  Andy Estes  <aestes@apple.com>
+
+        Add support for canvas.toBlob
+        https://bugs.webkit.org/show_bug.cgi?id=148878
+        <rdar://problem/22590406>
+
+        Reviewed by Tim Horton.
+
+        * web-platform-tests/html/semantics/embedded-content/the-canvas-element/toBlob.jpeg-expected.txt:
+        * web-platform-tests/html/semantics/embedded-content/the-canvas-element/toBlob.png-expected.txt:
+
 2017-03-03  Ryan Haddad  <ryanhaddad@apple.com>
 
         Unreviewed, rolling out r213344.
index a101947..c98a528 100644 (file)
@@ -1,5 +1,3 @@
 
-FAIL toBlob with image/jpeg returns a JPEG Blob canvas.toBlob is not a function. (In 'canvas.toBlob(this.step_func_done(function(data) {
-            assert_equals(data.type, "image/jpeg");
-        }), 'image/jpeg')', 'canvas.toBlob' is undefined)
+PASS toBlob with image/jpeg returns a JPEG Blob 
 
index c9fd1ea..9b196fe 100644 (file)
@@ -1,5 +1,3 @@
 
-FAIL toBlob with image/png returns a PNG Blob canvas.toBlob is not a function. (In 'canvas.toBlob(this.step_func_done(function(data) {
-            assert_equals(data.type, "image/png");
-        }), 'image/png')', 'canvas.toBlob' is undefined)
+PASS toBlob with image/png returns a PNG Blob 
 
index c83cf30..0c35ba5 100644 (file)
@@ -475,6 +475,7 @@ set(WebCore_NON_SVG_IDL_FILES
     dom/XMLDocument.idl
 
     fileapi/Blob.idl
+    fileapi/BlobCallback.idl
     fileapi/BlobLineEndings.idl
     fileapi/BlobPropertyBag.idl
     fileapi/File.idl
@@ -1605,6 +1606,7 @@ set(WebCore_SOURCES
     fileapi/AsyncFileStream.cpp
     fileapi/Blob.cpp
     fileapi/BlobBuilder.cpp
+    fileapi/BlobCallback.cpp
     fileapi/BlobURL.cpp
     fileapi/File.cpp
     fileapi/FileException.cpp
index 0ca1789..d43ad79 100644 (file)
@@ -1,3 +1,46 @@
+2017-03-03  Andy Estes  <aestes@apple.com>
+
+        Add support for canvas.toBlob
+        https://bugs.webkit.org/show_bug.cgi?id=148878
+        <rdar://problem/22590406>
+
+        Reviewed by Tim Horton.
+
+        These tests now pass:
+        imported/w3c/web-platform-tests/html/semantics/embedded-content/the-canvas-element/toBlob.jpeg.html
+        imported/w3c/web-platform-tests/html/semantics/embedded-content/the-canvas-element/toBlob.png.html
+
+        * CMakeLists.txt:
+        * DerivedSources.make:
+        * WebCore.xcodeproj/project.pbxproj:
+        * fileapi/BlobCallback.cpp: Added.
+        (WebCore::BlobCallback::scheduleCallback):
+        * fileapi/BlobCallback.h: Added.
+        (WebCore::BlobCallback::~BlobCallback):
+        * fileapi/BlobCallback.idl: Added.
+        * html/HTMLCanvasElement.cpp:
+        (WebCore::HTMLCanvasElement::toBlob):
+        * html/HTMLCanvasElement.h:
+        * html/HTMLCanvasElement.idl:
+        * platform/graphics/ImageBuffer.h:
+        * platform/graphics/cairo/ImageBufferCairo.cpp:
+        (WebCore::writeFunction):
+        (WebCore::encodeImage):
+        (WebCore::ImageBuffer::toDataURL):
+        (WebCore::ImageBuffer::toData):
+        * platform/graphics/cg/ImageBufferCG.cpp:
+        (WebCore::data):
+        (WebCore::ImageBuffer::toDataURL):
+        (WebCore::ImageBuffer::toData):
+        (WebCore::ImageBuffer::toCGImage):
+        (WebCore::cgImage):
+        (WebCore::dataURL):
+        * platform/graphics/gtk/ImageBufferGtk.cpp:
+        (WebCore::ImageBuffer::toDataURL):
+        (WebCore::ImageBuffer::toData):
+        * platform/graphics/win/ImageBufferDirect2D.cpp:
+        (WebCore::ImageBuffer::toData):
+
 2017-03-03  Dean Jackson  <dino@apple.com>
 
         Add WebGPU compile flag and experimental feature flag
index 8a57ca7..d9275af 100644 (file)
@@ -404,6 +404,7 @@ JS_BINDING_IDLS = \
     $(WebCore)/dom/WheelEvent.idl \
     $(WebCore)/dom/XMLDocument.idl \
     $(WebCore)/fileapi/Blob.idl \
+    $(WebCore)/fileapi/BlobCallback.idl \
     $(WebCore)/fileapi/BlobLineEndings.idl \
     $(WebCore)/fileapi/BlobPropertyBag.idl \
     $(WebCore)/fileapi/File.idl \
index f7ad2ff..198bf80 100644 (file)
                A149786E1ABAF33800CEF7E4 /* ContentFilter.cpp in Sources */ = {isa = PBXBuildFile; fileRef = A149786C1ABAF33800CEF7E4 /* ContentFilter.cpp */; };
                A149786F1ABAF33800CEF7E4 /* ContentFilter.h in Headers */ = {isa = PBXBuildFile; fileRef = A149786D1ABAF33800CEF7E4 /* ContentFilter.h */; };
                A14978711ABAF3A500CEF7E4 /* PlatformContentFilter.h in Headers */ = {isa = PBXBuildFile; fileRef = A14978701ABAF3A500CEF7E4 /* PlatformContentFilter.h */; };
+               A15D75151E68F7C400A35FBC /* BlobCallback.cpp in Sources */ = {isa = PBXBuildFile; fileRef = A15D75121E68F7B100A35FBC /* BlobCallback.cpp */; };
+               A15D75161E68F7C800A35FBC /* BlobCallback.h in Headers */ = {isa = PBXBuildFile; fileRef = A15D75131E68F7B100A35FBC /* BlobCallback.h */; };
+               A15D751A1E68F89E00A35FBC /* JSBlobCallback.cpp in Sources */ = {isa = PBXBuildFile; fileRef = A15D75171E68F83600A35FBC /* JSBlobCallback.cpp */; };
+               A15D751B1E68F8A300A35FBC /* JSBlobCallback.h in Headers */ = {isa = PBXBuildFile; fileRef = A15D75181E68F83600A35FBC /* JSBlobCallback.h */; };
                A15E31F31E0CB0B2004B371C /* QuickLook.mm in Sources */ = {isa = PBXBuildFile; fileRef = A15E31F21E0CB0AA004B371C /* QuickLook.mm */; };
                A15E31F41E0CB0B5004B371C /* QuickLook.h in Headers */ = {isa = PBXBuildFile; fileRef = A15E31F11E0CB0AA004B371C /* QuickLook.h */; settings = {ATTRIBUTES = (Private, ); }; };
                A15E6BF11E212A6A0080AF34 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = A15E6BF01E212A6A0080AF34 /* Foundation.framework */; };
                A149786C1ABAF33800CEF7E4 /* ContentFilter.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ContentFilter.cpp; sourceTree = "<group>"; };
                A149786D1ABAF33800CEF7E4 /* ContentFilter.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ContentFilter.h; sourceTree = "<group>"; };
                A14978701ABAF3A500CEF7E4 /* PlatformContentFilter.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PlatformContentFilter.h; sourceTree = "<group>"; };
+               A15D75121E68F7B100A35FBC /* BlobCallback.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = BlobCallback.cpp; sourceTree = "<group>"; };
+               A15D75131E68F7B100A35FBC /* BlobCallback.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BlobCallback.h; sourceTree = "<group>"; };
+               A15D75141E68F7B100A35FBC /* BlobCallback.idl */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = BlobCallback.idl; sourceTree = "<group>"; };
+               A15D75171E68F83600A35FBC /* JSBlobCallback.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = JSBlobCallback.cpp; sourceTree = "<group>"; };
+               A15D75181E68F83600A35FBC /* JSBlobCallback.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JSBlobCallback.h; sourceTree = "<group>"; };
                A15E31F11E0CB0AA004B371C /* QuickLook.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = QuickLook.h; sourceTree = "<group>"; };
                A15E31F21E0CB0AA004B371C /* QuickLook.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = QuickLook.mm; sourceTree = "<group>"; };
                A15E6BF01E212A6A0080AF34 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; };
                        children = (
                                2E2D99CB10E2BBDA00496337 /* JSBlob.cpp */,
                                2E2D99CC10E2BBDA00496337 /* JSBlob.h */,
+                               A15D75171E68F83600A35FBC /* JSBlobCallback.cpp */,
+                               A15D75181E68F83600A35FBC /* JSBlobCallback.h */,
                                7C77C3D91DEF86D700A50BFA /* JSBlobLineEndings.cpp */,
                                7C77C3DA1DEF86D700A50BFA /* JSBlobLineEndings.h */,
                                7C77C3D21DEF178E00A50BFA /* JSBlobPropertyBag.cpp */,
                                14A98E05136386A200C9FED2 /* Blob.idl */,
                                976D6C5C122B8A3D001FD1F7 /* BlobBuilder.cpp */,
                                976D6C5D122B8A3D001FD1F7 /* BlobBuilder.h */,
+                               A15D75121E68F7B100A35FBC /* BlobCallback.cpp */,
+                               A15D75131E68F7B100A35FBC /* BlobCallback.h */,
+                               A15D75141E68F7B100A35FBC /* BlobCallback.idl */,
                                7C77C3D61DEF850A00A50BFA /* BlobLineEndings.h */,
                                7C77C3D81DEF854000A50BFA /* BlobLineEndings.idl */,
                                7C77C3D01DEE472400A50BFA /* BlobPropertyBag.h */,
                                1AE2ABAB0A1CE90500B42B25 /* JSHTMLPreElement.h in Headers */,
                                A4226E5C1163D695008B8397 /* JSHTMLProgressElement.h in Headers */,
                                1AE2AEC80A1D297B00B42B25 /* JSHTMLQuoteElement.h in Headers */,
+                               A15D751B1E68F8A300A35FBC /* JSBlobCallback.h in Headers */,
                                1AE2ABAD0A1CE90500B42B25 /* JSHTMLScriptElement.h in Headers */,
                                E1E6EEA80B628DB3005F2F70 /* JSHTMLSelectElement.h in Headers */,
                                AB4CB4EB0B8BDA3D009F40B0 /* JSHTMLSelectElementCustom.h in Headers */,
                                FEE1811416C319E800084849 /* SQLTransactionBackend.h in Headers */,
                                97BC6A521505F081001B74AC /* SQLTransactionCallback.h in Headers */,
                                97BC6A571505F081001B74AC /* SQLTransactionCoordinator.h in Headers */,
+                               A15D75161E68F7C800A35FBC /* BlobCallback.h in Headers */,
                                97BC6A581505F081001B74AC /* SQLTransactionErrorCallback.h in Headers */,
                                FE36FD1816C7826500F887C1 /* SQLTransactionState.h in Headers */,
                                FE36FD1716C7826500F887C1 /* SQLTransactionStateMachine.h in Headers */,
                                B2FA3E080AB75A6F000E5AC4 /* JSSVGTitleElement.cpp in Sources */,
                                B2FA3E0A0AB75A6F000E5AC4 /* JSSVGTransform.cpp in Sources */,
                                B2FA3E0C0AB75A6F000E5AC4 /* JSSVGTransformList.cpp in Sources */,
+                               A15D75151E68F7C400A35FBC /* BlobCallback.cpp in Sources */,
                                B2FA3E0E0AB75A6F000E5AC4 /* JSSVGTRefElement.cpp in Sources */,
                                B2FA3E100AB75A6F000E5AC4 /* JSSVGTSpanElement.cpp in Sources */,
                                B2FA3E120AB75A6F000E5AC4 /* JSSVGUnitTypes.cpp in Sources */,
                                444D4E230F708B2E003158E0 /* WebCoreURLResponseIOS.mm in Sources */,
                                93F19B0708245E59001E9ABC /* WebCoreView.m in Sources */,
                                A5E616731894581F009ADF50 /* WebDebuggerAgent.cpp in Sources */,
+                               A15D751A1E68F89E00A35FBC /* JSBlobCallback.cpp in Sources */,
                                FE0D84EB1048436E001A179E /* WebEvent.mm in Sources */,
                                225A16B60D5C11E900090295 /* WebEventRegion.mm in Sources */,
                                D3F3D36D1A69B7DC0059FC2B /* WebGL2RenderingContext.cpp in Sources */,
diff --git a/Source/WebCore/fileapi/BlobCallback.cpp b/Source/WebCore/fileapi/BlobCallback.cpp
new file mode 100644 (file)
index 0000000..b9a4063
--- /dev/null
@@ -0,0 +1,41 @@
+/*
+ * Copyright (C) 2017 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "config.h"
+#include "BlobCallback.h"
+
+#include "Blob.h"
+#include "ScriptExecutionContext.h"
+
+namespace WebCore {
+
+void BlobCallback::scheduleCallback(ScriptExecutionContext& context, RefPtr<Blob>&& blob)
+{
+    context.postTask([this, protectedThis = makeRef(*this), blob = WTFMove(blob)](ScriptExecutionContext&) {
+        handleEvent(blob.get());
+    });
+}
+
+} // namespace WebCore
diff --git a/Source/WebCore/fileapi/BlobCallback.h b/Source/WebCore/fileapi/BlobCallback.h
new file mode 100644 (file)
index 0000000..23c6fc2
--- /dev/null
@@ -0,0 +1,44 @@
+/*
+ * Copyright (C) 2017 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#pragma once
+
+#include <wtf/RefCounted.h>
+#include <wtf/RefPtr.h>
+
+namespace WebCore {
+
+class Blob;
+class ScriptExecutionContext;
+
+class BlobCallback : public RefCounted<BlobCallback> {
+public:
+    virtual ~BlobCallback() { }
+    virtual bool handleEvent(Blob*) = 0;
+
+    void scheduleCallback(ScriptExecutionContext&, RefPtr<Blob>&&);
+};
+
+} // namespace WebCore
diff --git a/Source/WebCore/fileapi/BlobCallback.idl b/Source/WebCore/fileapi/BlobCallback.idl
new file mode 100644 (file)
index 0000000..27b5ede
--- /dev/null
@@ -0,0 +1,26 @@
+/*
+ * Copyright (C) 2017 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+callback BlobCallback = void (Blob? blob);
index 7d7d391..4d23fc6 100644 (file)
@@ -28,6 +28,8 @@
 #include "config.h"
 #include "HTMLCanvasElement.h"
 
+#include "Blob.h"
+#include "BlobCallback.h"
 #include "CanvasGradient.h"
 #include "CanvasPattern.h"
 #include "CanvasRenderingContext2D.h"
@@ -485,6 +487,36 @@ ExceptionOr<String> HTMLCanvasElement::toDataURL(const String& mimeType, std::op
     return buffer()->toDataURL(encodingMIMEType, quality);
 }
 
+ExceptionOr<void> HTMLCanvasElement::toBlob(ScriptExecutionContext& context, Ref<BlobCallback>&& callback, const String& mimeType, JSC::JSValue qualityValue)
+{
+    if (!m_originClean)
+        return Exception { SECURITY_ERR };
+
+    if (m_size.isEmpty() || !buffer()) {
+        callback->scheduleCallback(context, nullptr);
+        return { };
+    }
+
+    String encodingMIMEType = toEncodingMimeType(mimeType);
+    std::optional<double> quality;
+    if (qualityValue.isNumber())
+        quality = qualityValue.toNumber(context.execState());
+
+#if USE(CG)
+    if (auto imageData = getImageData()) {
+        Vector<uint8_t> blobData = data(*imageData, encodingMIMEType, quality);
+        callback->scheduleCallback(context, Blob::create(WTFMove(blobData), encodingMIMEType));
+        return { };
+    }
+#endif
+
+    makeRenderingResultsAvailable();
+
+    Vector<uint8_t> blobData = buffer()->toData(encodingMIMEType, quality);
+    callback->scheduleCallback(context, Blob::create(WTFMove(blobData), encodingMIMEType));
+    return { };
+}
+
 RefPtr<ImageData> HTMLCanvasElement::getImageData()
 {
 #if ENABLE(WEBGL)
index 3b79114..23c1446 100644 (file)
@@ -39,6 +39,7 @@
 
 namespace WebCore {
 
+class BlobCallback;
 class CanvasRenderingContext;
 class GraphicsContext;
 class GraphicsContextStateSaver;
@@ -101,6 +102,7 @@ public:
     static String toEncodingMimeType(const String& mimeType);
     WEBCORE_EXPORT ExceptionOr<String> toDataURL(const String& mimeType, std::optional<double> quality);
     ExceptionOr<String> toDataURL(const String& mimeType) { return toDataURL(mimeType, std::nullopt); }
+    ExceptionOr<void> toBlob(ScriptExecutionContext&, Ref<BlobCallback>&&, const String& mimeType, JSC::JSValue qualityValue);
 
     // Used for rendering
     void didDraw(const FloatRect&);
index 9a7a4a7..b9b8cf9 100644 (file)
@@ -37,4 +37,5 @@ typedef (CanvasRenderingContext2D or WebGLRenderingContextBase) RenderingContext
     [Custom] RenderingContext? getContext(DOMString contextId, any... arguments);
 
     [Custom, MayThrowException] DOMString toDataURL(optional DOMString? type);
+    [CallWith=ScriptExecutionContext, MayThrowException] void toBlob(BlobCallback callback, optional DOMString type, optional any quality);
 };
index 397beee..606a154 100644 (file)
@@ -112,6 +112,7 @@ public:
     void convertToLuminanceMask();
     
     String toDataURL(const String& mimeType, std::optional<double> quality = std::nullopt, CoordinateSystem = LogicalCoordinateSystem) const;
+    Vector<uint8_t> toData(const String& mimeType, std::optional<double> quality = std::nullopt) const;
 #if !USE(CG)
     AffineTransform baseTransform() const { return AffineTransform(); }
     void transformColorSpace(ColorSpace srcColorSpace, ColorSpace dstColorSpace);
@@ -174,6 +175,7 @@ private:
     WEBCORE_EXPORT ImageBuffer(const FloatSize&, float resolutionScale, ColorSpace, RenderingMode, bool& success);
 #if USE(CG)
     ImageBuffer(const FloatSize&, float resolutionScale, CGColorSpaceRef, RenderingMode, bool& success);
+    RetainPtr<CGImageRef> toCGImage(const String& mimeType) const;
 #elif USE(DIRECT2D)
     ImageBuffer(const FloatSize&, float resolutionScale, ColorSpace, RenderingMode, const GraphicsContext*, bool& success);
 #endif
@@ -181,6 +183,7 @@ private:
 
 #if USE(CG)
 String dataURL(const ImageData&, const String& mimeType, std::optional<double> quality);
+Vector<uint8_t> data(const ImageData&, const String& mimeType, std::optional<double> quality);
 #endif
 
 } // namespace WebCore
index c978d78..00eb877 100644 (file)
@@ -539,12 +539,12 @@ void ImageBuffer::putByteArray(Multiply multiplied, Uint8ClampedArray* source, c
 #if !PLATFORM(GTK)
 static cairo_status_t writeFunction(void* output, const unsigned char* data, unsigned int length)
 {
-    if (!reinterpret_cast<Vector<unsigned char>*>(output)->tryAppend(data, length))
+    if (!reinterpret_cast<Vector<uint8_t>*>(output)->tryAppend(data, length))
         return CAIRO_STATUS_WRITE_ERROR;
     return CAIRO_STATUS_SUCCESS;
 }
 
-static bool encodeImage(cairo_surface_t* image, const String& mimeType, Vector<char>* output)
+static bool encodeImage(cairo_surface_t* image, const String& mimeType, Vector<uint8_t>* output)
 {
     ASSERT_UNUSED(mimeType, mimeType == "image/png"); // Only PNG output is supported for now.
 
@@ -553,19 +553,29 @@ static bool encodeImage(cairo_surface_t* image, const String& mimeType, Vector<c
 
 String ImageBuffer::toDataURL(const String& mimeType, std::optional<double>, CoordinateSystem) const
 {
+    Vector<uint8_t> encodedImage = toData(mimeType, quality);
+    if (encodedImage.isEmpty())
+        return "data:,";
+
+    Vector<char> base64Data;
+    base64Encode(encodedImage.data(), encodedImage.size(), base64Data);
+
+    return "data:" + mimeType + ";base64," + base64Data;
+}
+
+Vector<uint8_t> ImageBuffer::toData(const String& mimeType, std::optional<double> quality) const
+{
     ASSERT(MIMETypeRegistry::isSupportedImageMIMETypeForEncoding(mimeType));
 
     cairo_surface_t* image = cairo_get_target(context().platformContext()->cr());
 
-    Vector<char> encodedImage;
+    Vector<uint8_t> encodedImage;
     if (!image || !encodeImage(image, mimeType, &encodedImage))
-        return "data:,";
-
-    Vector<char> base64Data;
-    base64Encode(encodedImage, base64Data);
+        return { };
 
-    return "data:" + mimeType + ";base64," + base64Data;
+    return encodedImage;
 }
+
 #endif
 
 #if ENABLE(ACCELERATED_2D_CANVAS) && !USE(COORDINATED_GRAPHICS_THREADED)
index f6c544c..0a4bcc2 100644 (file)
@@ -498,8 +498,36 @@ static String dataURL(CGImageRef image, const String& mimeType, std::optional<do
     return "data:" + mimeType + ";base64," + base64Data;
 }
 
+static Vector<uint8_t> data(CGImageRef image, const String& mimeType, std::optional<double> quality)
+{
+    auto uti = utiFromMIMEType(mimeType);
+    ASSERT(uti);
+
+    auto cfData = adoptCF(CFDataCreateMutable(kCFAllocatorDefault, 0));
+    if (!encodeImage(image, uti.get(), quality, cfData.get()))
+        return { };
+
+    Vector<uint8_t> data;
+    data.append(CFDataGetBytePtr(cfData.get()), CFDataGetLength(cfData.get()));
+    return data;
+}
+
 String ImageBuffer::toDataURL(const String& mimeType, std::optional<double> quality, CoordinateSystem) const
 {
+    if (auto image = toCGImage(mimeType))
+        return dataURL(image.get(), mimeType, quality);
+    return ASCIILiteral("data:,");
+}
+
+Vector<uint8_t> ImageBuffer::toData(const String& mimeType, std::optional<double> quality) const
+{
+    if (auto image = toCGImage(mimeType))
+        return data(image.get(), mimeType, quality);
+    return { };
+}
+
+RetainPtr<CGImageRef> ImageBuffer::toCGImage(const String& mimeType) const
+{
     ASSERT(MIMETypeRegistry::isSupportedImageMIMETypeForEncoding(mimeType));
 
     if (context().isAcceleratedContext())
@@ -508,21 +536,24 @@ String ImageBuffer::toDataURL(const String& mimeType, std::optional<double> qual
     auto uti = utiFromMIMEType(mimeType);
     ASSERT(uti);
 
-    RefPtr<Uint8ClampedArray> premultipliedData;
     RetainPtr<CGImageRef> image;
 
     if (CFEqual(uti.get(), jpegUTI())) {
         // JPEGs don't have an alpha channel, so we have to manually composite on top of black.
-        premultipliedData = getPremultipliedImageData(IntRect(IntPoint(0, 0), logicalSize()));
+        // premultipliedData is leaked here, then adopted in the releaseData lambda passed to CGDataProviderCreateWithData().
+        Uint8ClampedArray* premultipliedData = getPremultipliedImageData(IntRect(IntPoint(0, 0), logicalSize())).leakRef();
         if (!premultipliedData)
-            return ASCIILiteral("data:,");
+            return nullptr;
 
-        auto dataProvider = adoptCF(CGDataProviderCreateWithData(0, premultipliedData->data(), 4 * logicalSize().width() * logicalSize().height(), 0));
+        size_t dataSize = 4 * logicalSize().width() * logicalSize().height();
+        auto dataProvider = adoptCF(CGDataProviderCreateWithData(premultipliedData, premultipliedData->data(), dataSize, [](void* info, const void*, size_t) {
+            ASSERT(info);
+            adoptRef(reinterpret_cast<Uint8ClampedArray*>(info));
+        }));
         if (!dataProvider)
-            return ASCIILiteral("data:,");
+            return nullptr;
 
-        image = adoptCF(CGImageCreate(logicalSize().width(), logicalSize().height(), 8, 32, 4 * logicalSize().width(),
-            sRGBColorSpaceRef(), kCGBitmapByteOrderDefault | kCGImageAlphaNoneSkipLast, dataProvider.get(), 0, false, kCGRenderingIntentDefault));
+        image = adoptCF(CGImageCreate(logicalSize().width(), logicalSize().height(), 8, 32, 4 * logicalSize().width(), sRGBColorSpaceRef(), kCGBitmapByteOrderDefault | kCGImageAlphaNoneSkipLast, dataProvider.get(), 0, false, kCGRenderingIntentDefault));
     } else if (m_resolutionScale == 1) {
         image = copyNativeImage(CopyBackingStore);
         image = createCroppedImageIfNecessary(image.get(), internalSize());
@@ -536,10 +567,10 @@ String ImageBuffer::toDataURL(const String& mimeType, std::optional<double> qual
         image = adoptCF(CGBitmapContextCreateImage(context.get()));
     }
 
-    return dataURL(image.get(), mimeType, quality);
+    return image;
 }
 
-String dataURL(const ImageData& source, const String& mimeType, std::optional<double> quality)
+static RetainPtr<CGImageRef> cgImage(const ImageData& source, const String& mimeType)
 {
     ASSERT(MIMETypeRegistry::isSupportedImageMIMETypeForEncoding(mimeType));
 
@@ -554,7 +585,7 @@ String dataURL(const ImageData& source, const String& mimeType, std::optional<do
         // JPEGs don't have an alpha channel, so we have to manually composite on top of black.
         size_t size = 4 * source.width() * source.height();
         if (!premultipliedData.tryReserveCapacity(size))
-            return ASCIILiteral("data:,");
+            return nullptr;
 
         premultipliedData.resize(size);
         unsigned char *buffer = premultipliedData.data();
@@ -577,11 +608,24 @@ String dataURL(const ImageData& source, const String& mimeType, std::optional<do
 
     auto dataProvider = adoptCF(CGDataProviderCreateWithData(0, data, 4 * source.width() * source.height(), 0));
     if (!dataProvider)
-        return ASCIILiteral("data:,");
+        return nullptr;
+
+    auto image = adoptCF(CGImageCreate(source.width(), source.height(), 8, 32, 4 * source.width(), sRGBColorSpaceRef(), kCGBitmapByteOrderDefault | dataAlphaInfo, dataProvider.get(), 0, false, kCGRenderingIntentDefault));
+    return image;
+}
+
+String dataURL(const ImageData& source, const String& mimeType, std::optional<double> quality)
+{
+    if (auto image = cgImage(source, mimeType))
+        return dataURL(image.get(), mimeType, quality);
+    return ASCIILiteral("data:,");
+}
 
-    auto image = adoptCF(CGImageCreate(source.width(), source.height(), 8, 32, 4 * source.width(),
-        sRGBColorSpaceRef(), kCGBitmapByteOrderDefault | dataAlphaInfo, dataProvider.get(), 0, false, kCGRenderingIntentDefault));
-    return dataURL(image.get(), mimeType, quality);
+Vector<uint8_t> data(const ImageData& source, const String& mimeType, std::optional<double> quality)
+{
+    if (auto image = cgImage(source, mimeType))
+        return data(image.get(), mimeType, quality);
+    return { };
 }
 
 } // namespace WebCore
index b2fe2a9..8cf0de5 100644 (file)
@@ -77,17 +77,28 @@ static bool encodeImage(cairo_surface_t* surface, const String& mimeType, std::o
 
 String ImageBuffer::toDataURL(const String& mimeType, std::optional<double> quality, CoordinateSystem) const
 {
+    Vector<uint8_t> imageData = toData(mimeType, quality);
+    if (imageData.isEmpty())
+        return "data:,";
+
+    Vector<char> base64Data;
+    base64Encode(imageData.data(), imageData.size(), base64Data);
+
+    return "data:" + mimeType + ";base64," + base64Data;
+}
+
+Vector<uint8_t> ImageBuffer::toData(const String& mimeType, std::optional<double> quality) const
+{
     ASSERT(MIMETypeRegistry::isSupportedImageMIMETypeForEncoding(mimeType));
 
     GUniqueOutPtr<gchar> buffer;
     gsize bufferSize;
     if (!encodeImage(m_data.m_surface.get(), mimeType, quality, buffer, bufferSize))
-        return "data:,";
+        return { };
 
-    Vector<char> base64Data;
-    base64Encode(buffer.get(), bufferSize, base64Data);
-
-    return "data:" + mimeType + ";base64," + base64Data;
+    Vector<uint8_t> imageData;
+    imageData.append(buffer.get(), bufferSize);
+    return imageData;
 }
 
 }
index 9ca153b..99d860c 100644 (file)
@@ -260,6 +260,12 @@ String ImageBuffer::toDataURL(const String&, std::optional<double>, CoordinateSy
     return ASCIILiteral("data:,");
 }
 
+Vector<uint8_t> ImageBuffer::toData(const String& mimeType, std::optional<double> quality) const
+{
+    notImplemented();
+    return { };
+}
+
 String ImageDataToDataURL(const ImageData& source, const String& mimeType, const double* quality)
 {
     notImplemented();