Chromium Code Reviews| 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; | |
| 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 } |
| (...skipping 412 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 503 ASSERT(m_context->is2d()); | 508 ASSERT(m_context->is2d()); |
| 504 RefPtr<SkImage> snapshot = buffer()->newSkImageSnapshot(); | 509 RefPtr<SkImage> snapshot = buffer()->newSkImageSnapshot(); |
| 505 if (snapshot) { | 510 if (snapshot) { |
| 506 SkImageInfo imageInfo = SkImageInfo::Make(width(), height(), kRGBA_8888_ SkColorType, kUnpremul_SkAlphaType); | 511 SkImageInfo imageInfo = SkImageInfo::Make(width(), height(), kRGBA_8888_ SkColorType, kUnpremul_SkAlphaType); |
| 507 snapshot->readPixels(imageInfo, imageData->data()->data(), imageInfo.min RowBytes(), 0, 0); | 512 snapshot->readPixels(imageInfo, imageData->data()->data(), imageInfo.min RowBytes(), 0, 0); |
| 508 } | 513 } |
| 509 | 514 |
| 510 return imageData; | 515 return imageData; |
| 511 } | 516 } |
| 512 | 517 |
| 513 String HTMLCanvasElement::toDataURLInternal(const String& mimeType, const double * quality, SourceDrawingBuffer sourceBuffer) const | 518 String HTMLCanvasElement::toDataURLInternal(const String& mimeType, const double & quality, SourceDrawingBuffer sourceBuffer) const |
| 514 { | 519 { |
| 515 if (!isPaintable()) | 520 if (!isPaintable()) |
| 516 return String("data:,"); | 521 return String("data:,"); |
| 517 | 522 |
| 518 String encodingMimeType = toEncodingMimeType(mimeType); | 523 String encodingMimeType = toEncodingMimeType(mimeType); |
| 519 | 524 |
| 520 ImageData* imageData = toImageData(sourceBuffer); | 525 ImageData* imageData = toImageData(sourceBuffer); |
| 521 ScopedDisposal<ImageData> disposer(imageData); | 526 ScopedDisposal<ImageData> disposer(imageData); |
| 522 | 527 |
| 523 return ImageDataBuffer(imageData->size(), imageData->data()->data()).toDataU RL(encodingMimeType, quality); | 528 return ImageDataBuffer(imageData->size(), imageData->data()->data()).toDataU RL(encodingMimeType, quality); |
| 524 } | 529 } |
| 525 | 530 |
| 526 String HTMLCanvasElement::toDataURL(const String& mimeType, const ScriptValue& q ualityArgument, ExceptionState& exceptionState) const | 531 String HTMLCanvasElement::toDataURL(const String& mimeType, const ScriptValue& q ualityArgument, ExceptionState& exceptionState) const |
| 527 { | 532 { |
| 528 if (!originClean()) { | 533 if (!originClean()) { |
| 529 exceptionState.throwSecurityError("Tainted canvases may not be exported. "); | 534 exceptionState.throwSecurityError("Tainted canvases may not be exported. "); |
| 530 return String(); | 535 return String(); |
| 531 } | 536 } |
| 532 double quality; | 537 double quality = InvalidQualityValue; |
| 533 double* qualityPtr = nullptr; | |
| 534 if (!qualityArgument.isEmpty()) { | 538 if (!qualityArgument.isEmpty()) { |
| 535 v8::Local<v8::Value> v8Value = qualityArgument.v8Value(); | 539 v8::Local<v8::Value> v8Value = qualityArgument.v8Value(); |
| 536 if (v8Value->IsNumber()) { | 540 if (v8Value->IsNumber()) { |
| 537 quality = v8Value.As<v8::Number>()->Value(); | 541 quality = v8Value.As<v8::Number>()->Value(); |
| 538 qualityPtr = &quality; | |
| 539 } | 542 } |
| 540 } | 543 } |
| 541 return toDataURLInternal(mimeType, qualityPtr, BackBuffer); | 544 return toDataURLInternal(mimeType, quality, BackBuffer); |
| 542 } | 545 } |
| 543 | 546 |
| 544 void HTMLCanvasElement::toBlob(FileCallback* callback, const String& mimeType, c onst ScriptValue& qualityArgument, ExceptionState& exceptionState) const | 547 void HTMLCanvasElement::encodeImageAsync(DOMUint8ClampedArray* imageData, IntSiz e imageSize, FileCallback* callback, const String& mimeType, double quality) |
| 548 { | |
| 549 OwnPtr<Vector<char>> encodedImage(adoptPtr(new Vector<char>())); | |
| 550 | |
| 551 if (!ImageDataBuffer(imageSize, imageData->data()).encodeImage(mimeType, qua lity, encodedImage.get())) { | |
|
Justin Novosad
2015/09/21 16:51:56
Do we know that all of the encoders are thread saf
| |
| 552 Platform::current()->mainThread()->taskRunner()->postTask(FROM_HERE, bin d(&FileCallback::handleEvent, callback, nullptr)); | |
| 553 } else { | |
| 554 Platform::current()->mainThread()->taskRunner()->postTask(FROM_HERE, thr eadSafeBind(&HTMLCanvasElement::createBlobAndCall, AllowCrossThreadAccess(this), encodedImage.release(), mimeType, AllowCrossThreadAccess(callback))); | |
| 555 } | |
| 556 } | |
| 557 | |
| 558 void HTMLCanvasElement::createBlobAndCall(PassOwnPtr<Vector<char>> encodedImage, const String& mimeType, FileCallback* callback) | |
| 559 { | |
| 560 // The main thread takes ownership of encoded image vector | |
| 561 OwnPtr<Vector<char>> enc(encodedImage); | |
| 562 | |
| 563 File* resultBlob = File::create(enc->data(), enc->size(), mimeType); | |
| 564 Platform::current()->mainThread()->taskRunner()->postTask(FROM_HERE, bind(&F ileCallback::handleEvent, callback, resultBlob)); | |
| 565 } | |
| 566 | |
| 567 void HTMLCanvasElement::toBlob(FileCallback* callback, const String& mimeType, c onst ScriptValue& qualityArgument, ExceptionState& exceptionState) | |
| 545 { | 568 { |
| 546 if (!originClean()) { | 569 if (!originClean()) { |
| 547 exceptionState.throwSecurityError("Tainted canvases may not be exported. "); | 570 exceptionState.throwSecurityError("Tainted canvases may not be exported. "); |
| 548 return; | 571 return; |
| 549 } | 572 } |
| 550 | 573 |
| 551 File* resultBlob = nullptr; | |
| 552 if (!isPaintable()) { | 574 if (!isPaintable()) { |
| 553 // If the canvas element's bitmap has no pixels | 575 // If the canvas element's bitmap has no pixels |
| 576 Platform::current()->mainThread()->taskRunner()->postTask(FROM_HERE, bin d(&FileCallback::handleEvent, callback, nullptr)); | |
| 554 return; | 577 return; |
| 555 } | 578 } |
| 556 | 579 |
| 557 double quality; | 580 double quality = InvalidQualityValue; |
| 558 double* qualityPtr = nullptr; | |
| 559 if (!qualityArgument.isEmpty()) { | 581 if (!qualityArgument.isEmpty()) { |
| 560 v8::Local<v8::Value> v8Value = qualityArgument.v8Value(); | 582 v8::Local<v8::Value> v8Value = qualityArgument.v8Value(); |
| 561 if (v8Value->IsNumber()) { | 583 if (v8Value->IsNumber()) { |
| 562 quality = v8Value.As<v8::Number>()->Value(); | 584 quality = v8Value.As<v8::Number>()->Value(); |
| 563 qualityPtr = &quality; | |
| 564 } | 585 } |
| 565 } | 586 } |
| 566 | 587 |
| 567 String encodingMimeType = toEncodingMimeType(mimeType); | 588 String encodingMimeType = toEncodingMimeType(mimeType); |
| 568 | 589 |
| 569 ImageData* imageData = toImageData(BackBuffer); | 590 ImageData* imageData = toImageData(BackBuffer); |
| 591 // ImageData object will be disposed on leaving stack scope; only imageData- >data and imageData->size are passed to m_thread | |
| 570 ScopedDisposal<ImageData> disposer(imageData); | 592 ScopedDisposal<ImageData> disposer(imageData); |
| 571 | 593 |
| 572 // Perform image encoding | 594 // Add a ref to keep image data alive until completion of encoding |
| 573 Vector<char> encodedImage; | 595 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 | 596 |
| 577 Platform::current()->mainThread()->taskRunner()->postTask(FROM_HERE, bind(&F ileCallback::handleEvent, callback, resultBlob)); | 597 if (!m_thread) { |
| 598 m_thread = adoptPtr(Platform::current()->createThread("Async toBlob")); | |
|
Justin Novosad
2015/09/21 16:51:55
These threads are created on a per canvas basis, w
| |
| 599 } | |
| 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, quality))); | |
| 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 Loading... | |
| 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 |
| OLD | NEW |