OLD | NEW |
---|---|
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 Loading... | |
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. |
70 const int DefaultWidth = 300; | 71 const int DefaultWidth = 300; |
71 const int DefaultHeight = 150; | 72 const int DefaultHeight = 150; |
72 | 73 |
73 // Firefox limits width/height to 32767 pixels, but slows down dramatically befo re it | 74 // Firefox limits width/height to 32767 pixels, but slows down dramatically befo re it |
74 // reaches that limit. We limit by area instead, giving us larger maximum dimens ions, | 75 // reaches that limit. We limit by area instead, giving us larger maximum dimens ions, |
75 // in exchange for a smaller maximum canvas size. | 76 // in exchange for a smaller maximum canvas size. |
76 const int MaxCanvasArea = 32768 * 8192; // Maximum canvas area in CSS pixels | 77 const int MaxCanvasArea = 32768 * 8192; // Maximum canvas area in CSS pixels |
77 | 78 |
78 // In Skia, we will also limit width/height to 32767. | 79 // In Skia, we will also limit width/height to 32767. |
79 const int MaxSkiaDim = 32767; // Maximum width/height in CSS pixels. | 80 const int MaxSkiaDim = 32767; // Maximum width/height in CSS pixels. |
80 | 81 |
82 // A default value of quality argument for toDataURL and toBlob | |
83 // It is in an invalid range (outside 0.0 - 1.0) so that it will not be misinter preted as a user-input value | |
84 const int InvalidQualityValue = -1.0; | |
Justin Novosad
2015/09/23 20:32:32
I would call this Undefined instead of Invalid. "I
| |
85 | |
81 bool canCreateImageBuffer(const IntSize& size) | 86 bool canCreateImageBuffer(const IntSize& size) |
82 { | 87 { |
83 if (size.isEmpty()) | 88 if (size.isEmpty()) |
84 return false; | 89 return false; |
85 if (size.width() * size.height() > MaxCanvasArea) | 90 if (size.width() * size.height() > MaxCanvasArea) |
86 return false; | 91 return false; |
87 if (size.width() > MaxSkiaDim || size.height() > MaxSkiaDim) | 92 if (size.width() > MaxSkiaDim || size.height() > MaxSkiaDim) |
88 return false; | 93 return false; |
89 return true; | 94 return true; |
90 } | 95 } |
91 | 96 |
92 PassRefPtr<Image> createTransparentImage(const IntSize& size) | 97 PassRefPtr<Image> createTransparentImage(const IntSize& size) |
93 { | 98 { |
94 ASSERT(canCreateImageBuffer(size)); | 99 ASSERT(canCreateImageBuffer(size)); |
95 RefPtr<SkSurface> surface = adoptRef(SkSurface::NewRasterN32Premul(size.widt h(), size.height())); | 100 RefPtr<SkSurface> surface = adoptRef(SkSurface::NewRasterN32Premul(size.widt h(), size.height())); |
96 surface->getCanvas()->clear(SK_ColorTRANSPARENT); | 101 surface->getCanvas()->clear(SK_ColorTRANSPARENT); |
97 return StaticBitmapImage::create(adoptRef(surface->newImageSnapshot())); | 102 return StaticBitmapImage::create(adoptRef(surface->newImageSnapshot())); |
98 } | 103 } |
99 | 104 |
100 } // namespace | 105 } // namespace |
101 | 106 |
107 static WebThread* s_thread = 0; | |
Justin Novosad
2015/09/23 20:32:32
You should make these statics members of HTMLCanva
| |
108 static int numCanvasInstances = 0; | |
Justin Novosad
2015/09/23 20:32:32
missing s_
| |
109 | |
102 DEFINE_EMPTY_DESTRUCTOR_WILL_BE_REMOVED(CanvasObserver); | 110 DEFINE_EMPTY_DESTRUCTOR_WILL_BE_REMOVED(CanvasObserver); |
103 | 111 |
104 inline HTMLCanvasElement::HTMLCanvasElement(Document& document) | 112 inline HTMLCanvasElement::HTMLCanvasElement(Document& document) |
105 : HTMLElement(canvasTag, document) | 113 : HTMLElement(canvasTag, document) |
106 , DocumentVisibilityObserver(document) | 114 , DocumentVisibilityObserver(document) |
107 , m_size(DefaultWidth, DefaultHeight) | 115 , m_size(DefaultWidth, DefaultHeight) |
108 , m_ignoreReset(false) | 116 , m_ignoreReset(false) |
109 , m_accelerationDisabled(false) | 117 , m_accelerationDisabled(false) |
110 , m_externallyAllocatedMemory(0) | 118 , m_externallyAllocatedMemory(0) |
111 , m_originClean(true) | 119 , m_originClean(true) |
112 , m_didFailToCreateImageBuffer(false) | 120 , m_didFailToCreateImageBuffer(false) |
113 , m_imageBufferIsClear(false) | 121 , m_imageBufferIsClear(false) |
114 { | 122 { |
115 setHasCustomStyleCallbacks(); | 123 setHasCustomStyleCallbacks(); |
124 numCanvasInstances++; | |
116 } | 125 } |
117 | 126 |
118 DEFINE_NODE_FACTORY(HTMLCanvasElement) | 127 DEFINE_NODE_FACTORY(HTMLCanvasElement) |
119 | 128 |
120 HTMLCanvasElement::~HTMLCanvasElement() | 129 HTMLCanvasElement::~HTMLCanvasElement() |
121 { | 130 { |
122 v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(-m_external lyAllocatedMemory); | 131 v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(-m_external lyAllocatedMemory); |
132 numCanvasInstances--; | |
133 if (numCanvasInstances == 0 && !!s_thread) | |
134 s_thread = 0; | |
Justin Novosad
2015/09/23 20:32:32
This is a memory leak. You should make s_thread an
| |
123 #if !ENABLE(OILPAN) | 135 #if !ENABLE(OILPAN) |
124 for (CanvasObserver* canvasObserver : m_observers) | 136 for (CanvasObserver* canvasObserver : m_observers) |
125 canvasObserver->canvasDestroyed(this); | 137 canvasObserver->canvasDestroyed(this); |
126 // Ensure these go away before the ImageBuffer. | 138 // Ensure these go away before the ImageBuffer. |
127 m_context.clear(); | 139 m_context.clear(); |
128 #endif | 140 #endif |
129 } | 141 } |
130 | 142 |
131 void HTMLCanvasElement::parseAttribute(const QualifiedName& name, const AtomicSt ring& value) | 143 void HTMLCanvasElement::parseAttribute(const QualifiedName& name, const AtomicSt ring& value) |
132 { | 144 { |
(...skipping 370 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
503 ASSERT(m_context->is2d()); | 515 ASSERT(m_context->is2d()); |
504 RefPtr<SkImage> snapshot = buffer()->newSkImageSnapshot(); | 516 RefPtr<SkImage> snapshot = buffer()->newSkImageSnapshot(); |
505 if (snapshot) { | 517 if (snapshot) { |
506 SkImageInfo imageInfo = SkImageInfo::Make(width(), height(), kRGBA_8888_ SkColorType, kUnpremul_SkAlphaType); | 518 SkImageInfo imageInfo = SkImageInfo::Make(width(), height(), kRGBA_8888_ SkColorType, kUnpremul_SkAlphaType); |
507 snapshot->readPixels(imageInfo, imageData->data()->data(), imageInfo.min RowBytes(), 0, 0); | 519 snapshot->readPixels(imageInfo, imageData->data()->data(), imageInfo.min RowBytes(), 0, 0); |
508 } | 520 } |
509 | 521 |
510 return imageData; | 522 return imageData; |
511 } | 523 } |
512 | 524 |
513 String HTMLCanvasElement::toDataURLInternal(const String& mimeType, const double * quality, SourceDrawingBuffer sourceBuffer) const | 525 String HTMLCanvasElement::toDataURLInternal(const String& mimeType, const double & quality, SourceDrawingBuffer sourceBuffer) const |
514 { | 526 { |
515 if (!isPaintable()) | 527 if (!isPaintable()) |
516 return String("data:,"); | 528 return String("data:,"); |
517 | 529 |
518 String encodingMimeType = toEncodingMimeType(mimeType); | 530 String encodingMimeType = toEncodingMimeType(mimeType); |
519 | 531 |
520 ImageData* imageData = toImageData(sourceBuffer); | 532 ImageData* imageData = toImageData(sourceBuffer); |
521 ScopedDisposal<ImageData> disposer(imageData); | 533 ScopedDisposal<ImageData> disposer(imageData); |
522 | 534 |
523 return ImageDataBuffer(imageData->size(), imageData->data()->data()).toDataU RL(encodingMimeType, quality); | 535 return ImageDataBuffer(imageData->size(), imageData->data()->data()).toDataU RL(encodingMimeType, quality); |
524 } | 536 } |
525 | 537 |
526 String HTMLCanvasElement::toDataURL(const String& mimeType, const ScriptValue& q ualityArgument, ExceptionState& exceptionState) const | 538 String HTMLCanvasElement::toDataURL(const String& mimeType, const ScriptValue& q ualityArgument, ExceptionState& exceptionState) const |
527 { | 539 { |
528 if (!originClean()) { | 540 if (!originClean()) { |
529 exceptionState.throwSecurityError("Tainted canvases may not be exported. "); | 541 exceptionState.throwSecurityError("Tainted canvases may not be exported. "); |
530 return String(); | 542 return String(); |
531 } | 543 } |
532 double quality; | 544 double quality = InvalidQualityValue; |
533 double* qualityPtr = nullptr; | |
534 if (!qualityArgument.isEmpty()) { | 545 if (!qualityArgument.isEmpty()) { |
535 v8::Local<v8::Value> v8Value = qualityArgument.v8Value(); | 546 v8::Local<v8::Value> v8Value = qualityArgument.v8Value(); |
536 if (v8Value->IsNumber()) { | 547 if (v8Value->IsNumber()) { |
537 quality = v8Value.As<v8::Number>()->Value(); | 548 quality = v8Value.As<v8::Number>()->Value(); |
538 qualityPtr = &quality; | |
539 } | 549 } |
540 } | 550 } |
541 return toDataURLInternal(mimeType, qualityPtr, BackBuffer); | 551 return toDataURLInternal(mimeType, quality, BackBuffer); |
542 } | 552 } |
543 | 553 |
544 void HTMLCanvasElement::toBlob(FileCallback* callback, const String& mimeType, c onst ScriptValue& qualityArgument, ExceptionState& exceptionState) const | 554 void HTMLCanvasElement::encodeImageAsync(DOMUint8ClampedArray* imageData, IntSiz e imageSize, FileCallback* callback, const String& mimeType, double quality) |
555 { | |
556 OwnPtr<Vector<char>> encodedImage(adoptPtr(new Vector<char>())); | |
557 | |
558 if (!ImageDataBuffer(imageSize, imageData->data()).encodeImage(mimeType, qua lity, encodedImage.get())) { | |
559 Platform::current()->mainThread()->taskRunner()->postTask(FROM_HERE, bin d(&FileCallback::handleEvent, callback, nullptr)); | |
560 } else { | |
561 Platform::current()->mainThread()->taskRunner()->postTask(FROM_HERE, thr eadSafeBind(&HTMLCanvasElement::createBlobAndCall, AllowCrossThreadAccess(this), encodedImage.release(), mimeType, AllowCrossThreadAccess(callback))); | |
562 } | |
563 } | |
564 | |
565 void HTMLCanvasElement::createBlobAndCall(PassOwnPtr<Vector<char>> encodedImage, const String& mimeType, FileCallback* callback) | |
566 { | |
567 // The main thread takes ownership of encoded image vector | |
568 OwnPtr<Vector<char>> enc(encodedImage); | |
569 | |
570 File* resultBlob = File::create(enc->data(), enc->size(), mimeType); | |
571 Platform::current()->mainThread()->taskRunner()->postTask(FROM_HERE, bind(&F ileCallback::handleEvent, callback, resultBlob)); | |
572 } | |
573 | |
574 void HTMLCanvasElement::toBlob(FileCallback* callback, const String& mimeType, c onst ScriptValue& qualityArgument, ExceptionState& exceptionState) | |
545 { | 575 { |
546 if (!originClean()) { | 576 if (!originClean()) { |
547 exceptionState.throwSecurityError("Tainted canvases may not be exported. "); | 577 exceptionState.throwSecurityError("Tainted canvases may not be exported. "); |
548 return; | 578 return; |
549 } | 579 } |
550 | 580 |
551 File* resultBlob = nullptr; | |
552 if (!isPaintable()) { | 581 if (!isPaintable()) { |
553 // If the canvas element's bitmap has no pixels | 582 // If the canvas element's bitmap has no pixels |
583 Platform::current()->mainThread()->taskRunner()->postTask(FROM_HERE, bin d(&FileCallback::handleEvent, callback, nullptr)); | |
554 return; | 584 return; |
555 } | 585 } |
556 | 586 |
557 double quality; | 587 double quality = InvalidQualityValue; |
558 double* qualityPtr = nullptr; | |
559 if (!qualityArgument.isEmpty()) { | 588 if (!qualityArgument.isEmpty()) { |
560 v8::Local<v8::Value> v8Value = qualityArgument.v8Value(); | 589 v8::Local<v8::Value> v8Value = qualityArgument.v8Value(); |
561 if (v8Value->IsNumber()) { | 590 if (v8Value->IsNumber()) { |
562 quality = v8Value.As<v8::Number>()->Value(); | 591 quality = v8Value.As<v8::Number>()->Value(); |
563 qualityPtr = &quality; | |
564 } | 592 } |
565 } | 593 } |
566 | 594 |
567 String encodingMimeType = toEncodingMimeType(mimeType); | 595 String encodingMimeType = toEncodingMimeType(mimeType); |
568 | 596 |
569 ImageData* imageData = toImageData(BackBuffer); | 597 ImageData* imageData = toImageData(BackBuffer); |
598 // ImageData object will be disposed on leaving stack scope; only imageData- >data and imageData->size are passed to s_thread | |
Justin Novosad
2015/09/23 20:32:32
This comment describes poorly what is happening. T
| |
570 ScopedDisposal<ImageData> disposer(imageData); | 599 ScopedDisposal<ImageData> disposer(imageData); |
571 | 600 |
572 // Perform image encoding | 601 // Add a ref to keep image data alive until completion of encoding |
573 Vector<char> encodedImage; | 602 RefPtr<DOMUint8ClampedArray> imageDataRef(imageData->data()); |
574 ImageDataBuffer(imageData->size(), imageData->data()->data()).encodeImage(en codingMimeType, qualityPtr, &encodedImage); | |
575 resultBlob = File::create(encodedImage.data(), encodedImage.size(), encoding MimeType); | |
576 | 603 |
577 Platform::current()->mainThread()->taskRunner()->postTask(FROM_HERE, bind(&F ileCallback::handleEvent, callback, resultBlob)); | 604 if (!s_thread) { |
605 s_thread = Platform::current()->createThread("Async toBlob"); | |
606 } | |
607 s_thread->taskRunner()->postTask(FROM_HERE, new Task(threadSafeBind(&HTMLCan vasElement::encodeImageAsync, AllowCrossThreadAccess(this), AllowCrossThreadAcce ss(imageDataRef.release().leakRef()), imageData->size(), AllowCrossThreadAccess( callback), encodingMimeType, quality))); | |
Justin Novosad
2015/09/23 20:32:32
This is unsafe. We cannot guarantee that 'this' wi
| |
578 } | 608 } |
579 | 609 |
580 SecurityOrigin* HTMLCanvasElement::securityOrigin() const | 610 SecurityOrigin* HTMLCanvasElement::securityOrigin() const |
581 { | 611 { |
582 return document().securityOrigin(); | 612 return document().securityOrigin(); |
583 } | 613 } |
584 | 614 |
585 bool HTMLCanvasElement::originClean() const | 615 bool HTMLCanvasElement::originClean() const |
586 { | 616 { |
587 if (document().settings() && document().settings()->disableReadingFromCanvas ()) | 617 if (document().settings() && document().settings()->disableReadingFromCanvas ()) |
(...skipping 355 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
943 { | 973 { |
944 return FloatSize(width(), height()); | 974 return FloatSize(width(), height()); |
945 } | 975 } |
946 | 976 |
947 bool HTMLCanvasElement::isOpaque() const | 977 bool HTMLCanvasElement::isOpaque() const |
948 { | 978 { |
949 return m_context && !m_context->hasAlpha(); | 979 return m_context && !m_context->hasAlpha(); |
950 } | 980 } |
951 | 981 |
952 } // blink | 982 } // blink |
OLD | NEW |