Chromium Code Reviews| Index: Source/core/html/HTMLCanvasElement.cpp |
| diff --git a/Source/core/html/HTMLCanvasElement.cpp b/Source/core/html/HTMLCanvasElement.cpp |
| index 6dc6f92d1556336a741cd0d237d79c7bb972ab68..69be989aea15b9af3bcf8911cb6e6040afebac81 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> |
| @@ -510,7 +511,7 @@ ImageData* HTMLCanvasElement::toImageData(SourceDrawingBuffer sourceBuffer) cons |
| return imageData; |
| } |
| -String HTMLCanvasElement::toDataURLInternal(const String& mimeType, const double* quality, SourceDrawingBuffer sourceBuffer) const |
| +String HTMLCanvasElement::toDataURLInternal(const String& mimeType, const double& quality, SourceDrawingBuffer sourceBuffer) const |
| { |
| if (!isPaintable()) |
| return String("data:,"); |
| @@ -529,52 +530,73 @@ String HTMLCanvasElement::toDataURL(const String& mimeType, const ScriptValue& q |
| exceptionState.throwSecurityError("Tainted canvases may not be exported."); |
| return String(); |
| } |
| - double quality; |
| - double* qualityPtr = nullptr; |
| + double quality = -1.0; |
|
Justin Novosad
2015/09/21 14:12:38
Use a named constant instead of a literal here. Th
|
| if (!qualityArgument.isEmpty()) { |
| v8::Local<v8::Value> v8Value = qualityArgument.v8Value(); |
| if (v8Value->IsNumber()) { |
| quality = v8Value.As<v8::Number>()->Value(); |
| - qualityPtr = &quality; |
| } |
| } |
| - return toDataURLInternal(mimeType, qualityPtr, BackBuffer); |
| + return toDataURLInternal(mimeType, quality, 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, double quality) |
| +{ |
| + // Create an OwnPtr for this worker thread to hold the encoded image vector |
|
Justin Novosad
2015/09/21 14:12:38
"worker" is a loaded word, don't use it here. Also
|
| + OwnPtr<Vector<char>> encodedImage(adoptPtr(new Vector<char>())); |
| + |
| + // Perform image encoding |
|
Justin Novosad
2015/09/21 14:12:38
Useless comment
|
| + 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 |
|
Justin Novosad
2015/09/21 14:12:38
Useless comment.
|
| + 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; |
| } |
| - double quality; |
| - double* qualityPtr = nullptr; |
| + double quality = -1.0; |
|
Justin Novosad
2015/09/21 14:12:38
use a constant
|
| if (!qualityArgument.isEmpty()) { |
| v8::Local<v8::Value> v8Value = qualityArgument.v8Value(); |
| if (v8Value->IsNumber()) { |
| quality = v8Value.As<v8::Number>()->Value(); |
| - qualityPtr = &quality; |
| } |
| } |
| String encodingMimeType = toEncodingMimeType(mimeType); |
| ImageData* imageData = toImageData(BackBuffer); |
| + // ImageData object will be disposed on leaving stack scope; only imageData->data and imageData->size are passed to m_thread |
| 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 until completion of encoding |
| + RefPtr<DOMUint8ClampedArray> imageDataRef(imageData->data()); |
| - Platform::current()->mainThread()->taskRunner()->postTask(FROM_HERE, bind(&FileCallback::handleEvent, callback, resultBlob)); |
| + if (!m_thread) { |
| + m_thread = adoptPtr(Platform::current()->createThread("Async toBlob")); |
| + } |
| + m_thread->taskRunner()->postTask(FROM_HERE, new Task(threadSafeBind(&HTMLCanvasElement::encodeImageAsync, AllowCrossThreadAccess(this), AllowCrossThreadAccess(imageDataRef.release().leakRef()), imageData->size(), AllowCrossThreadAccess(callback), encodingMimeType, quality))); |
| } |
| SecurityOrigin* HTMLCanvasElement::securityOrigin() const |