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

Side by Side 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 unified diff | Download patch
« no previous file with comments | « Source/core/html/HTMLCanvasElement.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2004, 2006, 2007 Apple Inc. All rights reserved. 2 * Copyright (C) 2004, 2006, 2007 Apple Inc. All rights reserved.
3 * Copyright (C) 2007 Alp Toker <alp@atoker.com> 3 * Copyright (C) 2007 Alp Toker <alp@atoker.com>
4 * Copyright (C) 2010 Torch Mobile (Beijing) Co. Ltd. All rights reserved. 4 * Copyright (C) 2010 Torch Mobile (Beijing) Co. Ltd. All rights reserved.
5 * 5 *
6 * Redistribution and use in source and binary forms, with or without 6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions 7 * modification, are permitted provided that the following conditions
8 * are met: 8 * are met:
9 * 1. Redistributions of source code must retain the above copyright 9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer. 10 * notice, this list of conditions and the following disclaimer.
(...skipping 28 matching lines...) Expand all
39 #include "core/frame/Settings.h" 39 #include "core/frame/Settings.h"
40 #include "core/html/ImageData.h" 40 #include "core/html/ImageData.h"
41 #include "core/html/canvas/CanvasContextCreationAttributes.h" 41 #include "core/html/canvas/CanvasContextCreationAttributes.h"
42 #include "core/html/canvas/CanvasFontCache.h" 42 #include "core/html/canvas/CanvasFontCache.h"
43 #include "core/html/canvas/CanvasRenderingContext.h" 43 #include "core/html/canvas/CanvasRenderingContext.h"
44 #include "core/html/canvas/CanvasRenderingContextFactory.h" 44 #include "core/html/canvas/CanvasRenderingContextFactory.h"
45 #include "core/layout/LayoutHTMLCanvas.h" 45 #include "core/layout/LayoutHTMLCanvas.h"
46 #include "core/paint/DeprecatedPaintLayer.h" 46 #include "core/paint/DeprecatedPaintLayer.h"
47 #include "platform/MIMETypeRegistry.h" 47 #include "platform/MIMETypeRegistry.h"
48 #include "platform/RuntimeEnabledFeatures.h" 48 #include "platform/RuntimeEnabledFeatures.h"
49 #include "platform/Task.h"
50 #include "platform/ThreadSafeFunctional.h"
49 #include "platform/graphics/Canvas2DImageBufferSurface.h" 51 #include "platform/graphics/Canvas2DImageBufferSurface.h"
50 #include "platform/graphics/ExpensiveCanvasHeuristicParameters.h" 52 #include "platform/graphics/ExpensiveCanvasHeuristicParameters.h"
51 #include "platform/graphics/ImageBuffer.h" 53 #include "platform/graphics/ImageBuffer.h"
52 #include "platform/graphics/RecordingImageBufferSurface.h" 54 #include "platform/graphics/RecordingImageBufferSurface.h"
53 #include "platform/graphics/StaticBitmapImage.h" 55 #include "platform/graphics/StaticBitmapImage.h"
54 #include "platform/graphics/UnacceleratedImageBufferSurface.h" 56 #include "platform/graphics/UnacceleratedImageBufferSurface.h"
55 #include "platform/graphics/gpu/AcceleratedImageBufferSurface.h" 57 #include "platform/graphics/gpu/AcceleratedImageBufferSurface.h"
56 #include "platform/transforms/AffineTransform.h" 58 #include "platform/transforms/AffineTransform.h"
57 #include "public/platform/Platform.h" 59 #include "public/platform/Platform.h"
58 #include "public/platform/WebTraceLocation.h" 60 #include "public/platform/WebTraceLocation.h"
59 #include "wtf/Functional.h"
60 #include <math.h> 61 #include <math.h>
61 #include <v8.h> 62 #include <v8.h>
62 63
63 namespace blink { 64 namespace blink {
64 65
65 using namespace HTMLNames; 66 using namespace HTMLNames;
66 67
67 namespace { 68 namespace {
68 69
69 // These values come from the WhatWG spec. 70 // These values come from the WhatWG spec.
(...skipping 464 matching lines...) Expand 10 before | Expand all | Expand 10 after
534 if (!qualityArgument.isEmpty()) { 535 if (!qualityArgument.isEmpty()) {
535 v8::Local<v8::Value> v8Value = qualityArgument.v8Value(); 536 v8::Local<v8::Value> v8Value = qualityArgument.v8Value();
536 if (v8Value->IsNumber()) { 537 if (v8Value->IsNumber()) {
537 quality = v8Value.As<v8::Number>()->Value(); 538 quality = v8Value.As<v8::Number>()->Value();
538 qualityPtr = &quality; 539 qualityPtr = &quality;
539 } 540 }
540 } 541 }
541 return toDataURLInternal(mimeType, qualityPtr, BackBuffer); 542 return toDataURLInternal(mimeType, qualityPtr, BackBuffer);
542 } 543 }
543 544
544 void HTMLCanvasElement::toBlob(FileCallback* callback, const String& mimeType, c onst ScriptValue& qualityArgument, ExceptionState& exceptionState) const 545 void HTMLCanvasElement::encodeImageAsync(DOMUint8ClampedArray* imageData, IntSiz e imageSize, FileCallback* callback, const String& mimeType, const double* quali ty)
546 {
547 // Create an OwnPtr for this worker thread to hold the encoded image vector
548 OwnPtr<Vector<char>> encodedImage(adoptPtr(new Vector<char>()));
549
550 // Perform image encoding
551 if (!ImageDataBuffer(imageSize, imageData->data()).encodeImage(mimeType, qua lity, encodedImage.get())) {
552 Platform::current()->mainThread()->taskRunner()->postTask(FROM_HERE, bin d(&FileCallback::handleEvent, callback, nullptr));
553 } else {
554 // Pass the pointer of encoded image vector to the main thread
555 Platform::current()->mainThread()->taskRunner()->postTask(FROM_HERE, thr eadSafeBind(&HTMLCanvasElement::createBlobAndCall, AllowCrossThreadAccess(this), encodedImage.release(), mimeType, AllowCrossThreadAccess(callback)));
556 }
557 }
558
559 void HTMLCanvasElement::createBlobAndCall(PassOwnPtr<Vector<char>> encodedImage, const String& mimeType, FileCallback* callback)
560 {
561 // The main thread takes ownership of encoded image vector
562 OwnPtr<Vector<char>> enc(encodedImage);
563
564 File* resultBlob = File::create(enc->data(), enc->size(), mimeType);
565 Platform::current()->mainThread()->taskRunner()->postTask(FROM_HERE, bind(&F ileCallback::handleEvent, callback, resultBlob));
566 }
567
568 void HTMLCanvasElement::toBlob(FileCallback* callback, const String& mimeType, c onst ScriptValue& qualityArgument, ExceptionState& exceptionState)
545 { 569 {
546 if (!originClean()) { 570 if (!originClean()) {
547 exceptionState.throwSecurityError("Tainted canvases may not be exported. "); 571 exceptionState.throwSecurityError("Tainted canvases may not be exported. ");
548 return; 572 return;
549 } 573 }
550 574
551 File* resultBlob = nullptr;
552 if (!isPaintable()) { 575 if (!isPaintable()) {
553 // If the canvas element's bitmap has no pixels 576 // If the canvas element's bitmap has no pixels
577 Platform::current()->mainThread()->taskRunner()->postTask(FROM_HERE, bin d(&FileCallback::handleEvent, callback, nullptr));
554 return; 578 return;
555 } 579 }
556 580
557 double quality; 581 double quality;
558 double* qualityPtr = nullptr; 582 double* qualityPtr = nullptr;
559 if (!qualityArgument.isEmpty()) { 583 if (!qualityArgument.isEmpty()) {
560 v8::Local<v8::Value> v8Value = qualityArgument.v8Value(); 584 v8::Local<v8::Value> v8Value = qualityArgument.v8Value();
561 if (v8Value->IsNumber()) { 585 if (v8Value->IsNumber()) {
562 quality = v8Value.As<v8::Number>()->Value(); 586 quality = v8Value.As<v8::Number>()->Value();
563 qualityPtr = &quality; 587 qualityPtr = &quality;
564 } 588 }
565 } 589 }
566 590
567 String encodingMimeType = toEncodingMimeType(mimeType); 591 String encodingMimeType = toEncodingMimeType(mimeType);
568 592
569 ImageData* imageData = toImageData(BackBuffer); 593 ImageData* imageData = toImageData(BackBuffer);
570 ScopedDisposal<ImageData> disposer(imageData); 594 ScopedDisposal<ImageData> disposer(imageData);
571 595
572 // Perform image encoding 596 // Add a ref to keep image data alive untio completion of encoding
Justin Novosad 2015/09/10 15:04:17 "until"
573 Vector<char> encodedImage; 597 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
574 ImageDataBuffer(imageData->size(), imageData->data()->data()).encodeImage(en codingMimeType, qualityPtr, &encodedImage);
575 resultBlob = File::create(encodedImage.data(), encodedImage.size(), encoding MimeType);
576 598
577 Platform::current()->mainThread()->taskRunner()->postTask(FROM_HERE, bind(&F ileCallback::handleEvent, callback, resultBlob)); 599 OwnPtr<WebThread> m_thread = adoptPtr(Platform::current()->createThread("Asy nc 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
600 m_thread->taskRunner()->postTask(FROM_HERE, new Task(threadSafeBind(&HTMLCan vasElement::encodeImageAsync, AllowCrossThreadAccess(this), AllowCrossThreadAcce ss(imageDataRef.release().leakRef()), imageData->size(), AllowCrossThreadAccess( callback), encodingMimeType, AllowCrossThreadAccess(qualityPtr))));
578 } 601 }
579 602
580 SecurityOrigin* HTMLCanvasElement::securityOrigin() const 603 SecurityOrigin* HTMLCanvasElement::securityOrigin() const
581 { 604 {
582 return document().securityOrigin(); 605 return document().securityOrigin();
583 } 606 }
584 607
585 bool HTMLCanvasElement::originClean() const 608 bool HTMLCanvasElement::originClean() const
586 { 609 {
587 if (document().settings() && document().settings()->disableReadingFromCanvas ()) 610 if (document().settings() && document().settings()->disableReadingFromCanvas ())
(...skipping 355 matching lines...) Expand 10 before | Expand all | Expand 10 after
943 { 966 {
944 return FloatSize(width(), height()); 967 return FloatSize(width(), height());
945 } 968 }
946 969
947 bool HTMLCanvasElement::isOpaque() const 970 bool HTMLCanvasElement::isOpaque() const
948 { 971 {
949 return m_context && !m_context->hasAlpha(); 972 return m_context && !m_context->hasAlpha();
950 } 973 }
951 974
952 } // blink 975 } // blink
OLDNEW
« 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