Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(929)

Unified Diff: Source/core/html/HTMLCanvasElement.cpp

Issue 1331443009: Implement Asynchronous image encoding for Canvas.toBlob (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « Source/core/html/HTMLCanvasElement.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: Source/core/html/HTMLCanvasElement.cpp
diff --git a/Source/core/html/HTMLCanvasElement.cpp b/Source/core/html/HTMLCanvasElement.cpp
index 6dc6f92d1556336a741cd0d237d79c7bb972ab68..f66da27c74a433d1b9082ffbbe31bc556af0a550 100644
--- a/Source/core/html/HTMLCanvasElement.cpp
+++ b/Source/core/html/HTMLCanvasElement.cpp
@@ -46,6 +46,8 @@
#include "core/paint/DeprecatedPaintLayer.h"
#include "platform/MIMETypeRegistry.h"
#include "platform/RuntimeEnabledFeatures.h"
+#include "platform/Task.h"
+#include "platform/ThreadSafeFunctional.h"
#include "platform/graphics/Canvas2DImageBufferSurface.h"
#include "platform/graphics/ExpensiveCanvasHeuristicParameters.h"
#include "platform/graphics/ImageBuffer.h"
@@ -56,7 +58,6 @@
#include "platform/transforms/AffineTransform.h"
#include "public/platform/Platform.h"
#include "public/platform/WebTraceLocation.h"
-#include "wtf/Functional.h"
#include <math.h>
#include <v8.h>
@@ -541,16 +542,39 @@ String HTMLCanvasElement::toDataURL(const String& mimeType, const ScriptValue& q
return toDataURLInternal(mimeType, qualityPtr, BackBuffer);
}
-void HTMLCanvasElement::toBlob(FileCallback* callback, const String& mimeType, const ScriptValue& qualityArgument, ExceptionState& exceptionState) const
+void HTMLCanvasElement::encodeImageAsync(DOMUint8ClampedArray* imageData, IntSize imageSize, FileCallback* callback, const String& mimeType, const double* quality)
+{
+ // Create an OwnPtr for this worker thread to hold the encoded image vector
+ OwnPtr<Vector<char>> encodedImage(adoptPtr(new Vector<char>()));
+
+ // Perform image encoding
+ if (!ImageDataBuffer(imageSize, imageData->data()).encodeImage(mimeType, quality, encodedImage.get())) {
+ Platform::current()->mainThread()->taskRunner()->postTask(FROM_HERE, bind(&FileCallback::handleEvent, callback, nullptr));
+ } else {
+ // Pass the pointer of encoded image vector to the main thread
+ Platform::current()->mainThread()->taskRunner()->postTask(FROM_HERE, threadSafeBind(&HTMLCanvasElement::createBlobAndCall, AllowCrossThreadAccess(this), encodedImage.release(), mimeType, AllowCrossThreadAccess(callback)));
+ }
+}
+
+void HTMLCanvasElement::createBlobAndCall(PassOwnPtr<Vector<char>> encodedImage, const String& mimeType, FileCallback* callback)
+{
+ // The main thread takes ownership of encoded image vector
+ OwnPtr<Vector<char>> enc(encodedImage);
+
+ File* resultBlob = File::create(enc->data(), enc->size(), mimeType);
+ Platform::current()->mainThread()->taskRunner()->postTask(FROM_HERE, bind(&FileCallback::handleEvent, callback, resultBlob));
+}
+
+void HTMLCanvasElement::toBlob(FileCallback* callback, const String& mimeType, const ScriptValue& qualityArgument, ExceptionState& exceptionState)
{
if (!originClean()) {
exceptionState.throwSecurityError("Tainted canvases may not be exported.");
return;
}
- File* resultBlob = nullptr;
if (!isPaintable()) {
// If the canvas element's bitmap has no pixels
+ Platform::current()->mainThread()->taskRunner()->postTask(FROM_HERE, bind(&FileCallback::handleEvent, callback, nullptr));
return;
}
@@ -569,12 +593,11 @@ void HTMLCanvasElement::toBlob(FileCallback* callback, const String& mimeType, c
ImageData* imageData = toImageData(BackBuffer);
ScopedDisposal<ImageData> disposer(imageData);
- // Perform image encoding
- Vector<char> encodedImage;
- ImageDataBuffer(imageData->size(), imageData->data()->data()).encodeImage(encodingMimeType, qualityPtr, &encodedImage);
- resultBlob = File::create(encodedImage.data(), encodedImage.size(), encodingMimeType);
+ // Add a ref to keep image data alive untio completion of encoding
Justin Novosad 2015/09/10 15:04:17 "until"
+ RefPtr<DOMUint8ClampedArray> imageDataRef(imageData->data());
Justin Novosad 2015/09/10 15:04:17 Isn't this incompatible with the use of ScopedDisp
xlai (Olivia) 2015/09/17 21:12:11 ImageData object itself can be disposed. I only ne
- Platform::current()->mainThread()->taskRunner()->postTask(FROM_HERE, bind(&FileCallback::handleEvent, callback, resultBlob));
+ OwnPtr<WebThread> m_thread = adoptPtr(Platform::current()->createThread("Async toBlob"));
Justin Novosad 2015/09/10 15:04:17 no "m_" on local variables. Spinning up a new thr
xlai (Olivia) 2015/09/17 21:12:10 Done. Moved m_thread to private member of HTMLCanv
+ m_thread->taskRunner()->postTask(FROM_HERE, new Task(threadSafeBind(&HTMLCanvasElement::encodeImageAsync, AllowCrossThreadAccess(this), AllowCrossThreadAccess(imageDataRef.release().leakRef()), imageData->size(), AllowCrossThreadAccess(callback), encodingMimeType, AllowCrossThreadAccess(qualityPtr))));
}
SecurityOrigin* HTMLCanvasElement::securityOrigin() const
« no previous file with comments | « Source/core/html/HTMLCanvasElement.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698