Commit f0fc0049 authored by jamesr@google.com's avatar jamesr@google.com

2010-07-09 James Robinson <jamesr@chromium.org>

        Reviewed by Darin Adler.

        Removes CanvasSurface and moves all of its functionality to HTMLCanvasElement.
        https://bugs.webkit.org/show_bug.cgi?id=42005

        http://trac.webkit.org/changeset/55201 introduced a new base class for HTMLCanvasElement called CanvasSurface.
        The intention was that this would allow for code sharing with the then-proposed OffscreenCanvas.  However,
        there is no OffscreenCanvas and there's unlikely to be one soon.  Additionally CanvasSurface breaks
        encapsulation pretty badly by doing "static_cast<HTMLCanvasElement* const>(this)".  Until an abstraction is
        really needed we should just use HTMLCanvasElement when we want to talk about a canvas.

        This patch moves all of CanvasSurface's functionality back up to HTMLCanvasElement and reorders the header
        to be a bit more logical.

        Refactor with no behavior change, thus no new tests.

        * CMakeLists.txt:
        * GNUmakefile.am:
        * WebCore.gypi:
        * WebCore.pro:
        * WebCore.vcproj/WebCore.vcproj:
        * WebCore.xcodeproj/project.pbxproj:
        * dom/CanvasSurface.cpp: Removed.
        * dom/CanvasSurface.h: Removed.
        * html/HTMLCanvasElement.cpp:
        (WebCore::HTMLCanvasElement::HTMLCanvasElement):
        (WebCore::HTMLCanvasElement::willDraw):
        (WebCore::HTMLCanvasElement::setSurfaceSize):
        (WebCore::HTMLCanvasElement::toDataURL):
        (WebCore::HTMLCanvasElement::convertLogicalToDevice):
        (WebCore::HTMLCanvasElement::securityOrigin):
        (WebCore::HTMLCanvasElement::styleSelector):
        (WebCore::HTMLCanvasElement::createImageBuffer):
        (WebCore::HTMLCanvasElement::drawingContext):
        (WebCore::HTMLCanvasElement::buffer):
        (WebCore::HTMLCanvasElement::baseTransform):
        * html/HTMLCanvasElement.h:
        (WebCore::HTMLCanvasElement::setObserver):
        (WebCore::HTMLCanvasElement::width):
        (WebCore::HTMLCanvasElement::height):
        (WebCore::HTMLCanvasElement::size):
        (WebCore::HTMLCanvasElement::toDataURL):
        (WebCore::HTMLCanvasElement::setOriginTainted):
        (WebCore::HTMLCanvasElement::originClean):
        (WebCore::HTMLCanvasElement::hasCreatedImageBuffer):


git-svn-id: http://svn.webkit.org/repository/webkit/trunk@63025 268f45cc-cd09-0410-ab3c-d52691b4dbfc
parent 2cb5aac7
......@@ -758,7 +758,6 @@ SET(WebCore_SOURCES
dom/Attribute.cpp
dom/BeforeTextInsertedEvent.cpp
dom/BeforeUnloadEvent.cpp
dom/CanvasSurface.cpp
dom/CDATASection.cpp
dom/CharacterData.cpp
dom/CheckedRadioButtons.cpp
......
2010-07-09 James Robinson <jamesr@chromium.org>
Reviewed by Darin Adler.
Removes CanvasSurface and moves all of its functionality to HTMLCanvasElement.
https://bugs.webkit.org/show_bug.cgi?id=42005
http://trac.webkit.org/changeset/55201 introduced a new base class for HTMLCanvasElement called CanvasSurface.
The intention was that this would allow for code sharing with the then-proposed OffscreenCanvas. However,
there is no OffscreenCanvas and there's unlikely to be one soon. Additionally CanvasSurface breaks
encapsulation pretty badly by doing "static_cast<HTMLCanvasElement* const>(this)". Until an abstraction is
really needed we should just use HTMLCanvasElement when we want to talk about a canvas.
This patch moves all of CanvasSurface's functionality back up to HTMLCanvasElement and reorders the header
to be a bit more logical.
Refactor with no behavior change, thus no new tests.
* CMakeLists.txt:
* GNUmakefile.am:
* WebCore.gypi:
* WebCore.pro:
* WebCore.vcproj/WebCore.vcproj:
* WebCore.xcodeproj/project.pbxproj:
* dom/CanvasSurface.cpp: Removed.
* dom/CanvasSurface.h: Removed.
* html/HTMLCanvasElement.cpp:
(WebCore::HTMLCanvasElement::HTMLCanvasElement):
(WebCore::HTMLCanvasElement::willDraw):
(WebCore::HTMLCanvasElement::setSurfaceSize):
(WebCore::HTMLCanvasElement::toDataURL):
(WebCore::HTMLCanvasElement::convertLogicalToDevice):
(WebCore::HTMLCanvasElement::securityOrigin):
(WebCore::HTMLCanvasElement::styleSelector):
(WebCore::HTMLCanvasElement::createImageBuffer):
(WebCore::HTMLCanvasElement::drawingContext):
(WebCore::HTMLCanvasElement::buffer):
(WebCore::HTMLCanvasElement::baseTransform):
* html/HTMLCanvasElement.h:
(WebCore::HTMLCanvasElement::setObserver):
(WebCore::HTMLCanvasElement::width):
(WebCore::HTMLCanvasElement::height):
(WebCore::HTMLCanvasElement::size):
(WebCore::HTMLCanvasElement::toDataURL):
(WebCore::HTMLCanvasElement::setOriginTainted):
(WebCore::HTMLCanvasElement::originClean):
(WebCore::HTMLCanvasElement::hasCreatedImageBuffer):
2010-07-09 Simon Fraser <simon.fraser@apple.com>
Reviewed by Darin Adler.
......@@ -755,8 +755,6 @@ webcore_sources += \
WebCore/dom/CDATASection.h \
WebCore/dom/CSSMappedAttributeDeclaration.cpp \
WebCore/dom/CSSMappedAttributeDeclaration.h \
WebCore/dom/CanvasSurface.cpp \
WebCore/dom/CanvasSurface.h \
WebCore/dom/CharacterData.cpp \
WebCore/dom/CharacterData.h \
WebCore/dom/CheckedRadioButtons.cpp \
......
......@@ -1076,8 +1076,6 @@
'dom/CDATASection.h',
'dom/CSSMappedAttributeDeclaration.cpp',
'dom/CSSMappedAttributeDeclaration.h',
'dom/CanvasSurface.cpp',
'dom/CanvasSurface.h',
'dom/CharacterData.cpp',
'dom/CharacterData.h',
'dom/CheckedRadioButtons.cpp',
......
......@@ -447,7 +447,6 @@ SOURCES += \
dom/BeforeTextInsertedEvent.cpp \
dom/BeforeUnloadEvent.cpp \
dom/CDATASection.cpp \
dom/CanvasSurface.cpp \
dom/CharacterData.cpp \
dom/CheckedRadioButtons.cpp \
dom/ChildNodeList.cpp \
......
......@@ -32236,14 +32236,6 @@
RelativePath="..\dom\BeforeUnloadEvent.h"
>
</File>
<File
RelativePath="..\dom\CanvasSurface.cpp"
>
</File>
<File
RelativePath="..\dom\CanvasSurface.h"
>
</File>
<File
RelativePath="..\dom\CDATASection.cpp"
>
......@@ -205,8 +205,6 @@
0B90561E0F257E930095FF6A /* ThreadableLoader.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 0B90561D0F257E930095FF6A /* ThreadableLoader.cpp */; };
0B9056F80F2685F30095FF6A /* WorkerThreadableLoader.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 0B9056F60F2685F30095FF6A /* WorkerThreadableLoader.cpp */; };
0B9056F90F2685F30095FF6A /* WorkerThreadableLoader.h in Headers */ = {isa = PBXBuildFile; fileRef = 0B9056F70F2685F30095FF6A /* WorkerThreadableLoader.h */; settings = {ATTRIBUTES = (); }; };
0BC2C7771134A8FC000B2F61 /* CanvasSurface.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 0BC2C7751134A8FC000B2F61 /* CanvasSurface.cpp */; };
0BC2C7781134A8FC000B2F61 /* CanvasSurface.h in Headers */ = {isa = PBXBuildFile; fileRef = 0BC2C7761134A8FC000B2F61 /* CanvasSurface.h */; };
0BE030A20F3112FB003C1A46 /* RenderLineBoxList.h in Headers */ = {isa = PBXBuildFile; fileRef = 0BE030A10F3112FB003C1A46 /* RenderLineBoxList.h */; settings = {ATTRIBUTES = (Private, ); }; };
0C3F1F5A10C8871200D72CE1 /* WebGLUniformLocation.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 0C3F1F5710C8871200D72CE1 /* WebGLUniformLocation.cpp */; };
0C3F1F5B10C8871200D72CE1 /* WebGLUniformLocation.h in Headers */ = {isa = PBXBuildFile; fileRef = 0C3F1F5810C8871200D72CE1 /* WebGLUniformLocation.h */; };
......@@ -5810,8 +5808,6 @@
0B90561D0F257E930095FF6A /* ThreadableLoader.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ThreadableLoader.cpp; sourceTree = "<group>"; };
0B9056F60F2685F30095FF6A /* WorkerThreadableLoader.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = WorkerThreadableLoader.cpp; sourceTree = "<group>"; };
0B9056F70F2685F30095FF6A /* WorkerThreadableLoader.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WorkerThreadableLoader.h; sourceTree = "<group>"; };
0BC2C7751134A8FC000B2F61 /* CanvasSurface.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CanvasSurface.cpp; sourceTree = "<group>"; };
0BC2C7761134A8FC000B2F61 /* CanvasSurface.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CanvasSurface.h; sourceTree = "<group>"; };
0BE030A10F3112FB003C1A46 /* RenderLineBoxList.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RenderLineBoxList.h; sourceTree = "<group>"; };
0C3F1F5710C8871200D72CE1 /* WebGLUniformLocation.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = WebGLUniformLocation.cpp; path = canvas/WebGLUniformLocation.cpp; sourceTree = "<group>"; };
0C3F1F5810C8871200D72CE1 /* WebGLUniformLocation.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = WebGLUniformLocation.h; path = canvas/WebGLUniformLocation.h; sourceTree = "<group>"; };
......@@ -16794,8 +16790,6 @@
AB23A32609BBA7D00067CC53 /* BeforeTextInsertedEvent.h */,
85031B260A44EFC700F992E0 /* BeforeUnloadEvent.cpp */,
85031B270A44EFC700F992E0 /* BeforeUnloadEvent.h */,
0BC2C7751134A8FC000B2F61 /* CanvasSurface.cpp */,
0BC2C7761134A8FC000B2F61 /* CanvasSurface.h */,
6550B693099DF0270090D781 /* CDATASection.cpp */,
6550B694099DF0270090D781 /* CDATASection.h */,
85089CC90A98C2AB00A275AA /* CDATASection.idl */,
......@@ -17203,7 +17197,6 @@
49C7B9DD1042D32F0009D447 /* CanvasRenderingContext.h in Headers */,
49484FCB102CF23C00187DD3 /* CanvasRenderingContext2D.h in Headers */,
49484FCE102CF23C00187DD3 /* CanvasStyle.h in Headers */,
0BC2C7781134A8FC000B2F61 /* CanvasSurface.h in Headers */,
6550B69E099DF0270090D781 /* CDATASection.h in Headers */,
514185EE0CD65F0400763C99 /* ChangeVersionWrapper.h in Headers */,
6550B6A0099DF0270090D781 /* CharacterData.h in Headers */,
......@@ -20122,7 +20115,6 @@
49C7B9DC1042D32F0009D447 /* CanvasRenderingContext.cpp in Sources */,
49484FCA102CF23C00187DD3 /* CanvasRenderingContext2D.cpp in Sources */,
49484FCD102CF23C00187DD3 /* CanvasStyle.cpp in Sources */,
0BC2C7771134A8FC000B2F61 /* CanvasSurface.cpp in Sources */,
6550B69D099DF0270090D781 /* CDATASection.cpp in Sources */,
514185EF0CD65F0400763C99 /* ChangeVersionWrapper.cpp in Sources */,
6550B69F099DF0270090D781 /* CharacterData.cpp in Sources */,
/*
* Copyright (C) 2010 Google Inc. All rights reserved.
* Copyright (C) 2010 Torch Mobile (Beijing) Co. Ltd. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "CanvasSurface.h"
#include "AffineTransform.h"
#include "ExceptionCode.h"
#include "FloatRect.h"
#include "GraphicsContext.h"
#include "HTMLCanvasElement.h"
#include "ImageBuffer.h"
#include "MIMETypeRegistry.h"
namespace WebCore {
// These values come from the WhatWG spec.
const int CanvasSurface::DefaultWidth = 300;
const int CanvasSurface::DefaultHeight = 150;
// Firefox limits width/height to 32767 pixels, but slows down dramatically before it
// reaches that limit. We limit by area instead, giving us larger maximum dimensions,
// in exchange for a smaller maximum canvas size.
const float CanvasSurface::MaxCanvasArea = 32768 * 8192; // Maximum canvas area in CSS pixels
CanvasSurface::CanvasSurface(float pageScaleFactor)
: m_size(DefaultWidth, DefaultHeight)
, m_pageScaleFactor(pageScaleFactor)
, m_originClean(true)
, m_hasCreatedImageBuffer(false)
{
}
CanvasSurface::~CanvasSurface()
{
}
void CanvasSurface::setSurfaceSize(const IntSize& size)
{
m_size = size;
m_hasCreatedImageBuffer = false;
m_imageBuffer.clear();
}
String CanvasSurface::toDataURL(const String& mimeType, const double* quality, ExceptionCode& ec)
{
if (!m_originClean) {
ec = SECURITY_ERR;
return String();
}
if (m_size.isEmpty() || !buffer())
return String("data:,");
String lowercaseMimeType = mimeType.lower();
// FIXME: Make isSupportedImageMIMETypeForEncoding threadsafe (to allow this method to be used on a worker thread).
if (mimeType.isNull() || !MIMETypeRegistry::isSupportedImageMIMETypeForEncoding(lowercaseMimeType))
return buffer()->toDataURL("image/png");
return buffer()->toDataURL(lowercaseMimeType, quality);
}
void CanvasSurface::willDraw(const FloatRect&)
{
if (m_imageBuffer)
m_imageBuffer->clearImage();
}
IntRect CanvasSurface::convertLogicalToDevice(const FloatRect& logicalRect) const
{
return IntRect(convertLogicalToDevice(logicalRect.location()), convertLogicalToDevice(logicalRect.size()));
}
IntSize CanvasSurface::convertLogicalToDevice(const FloatSize& logicalSize) const
{
float wf = ceilf(logicalSize.width() * m_pageScaleFactor);
float hf = ceilf(logicalSize.height() * m_pageScaleFactor);
if (!(wf >= 1 && hf >= 1 && wf * hf <= MaxCanvasArea))
return IntSize();
return IntSize(static_cast<unsigned>(wf), static_cast<unsigned>(hf));
}
IntPoint CanvasSurface::convertLogicalToDevice(const FloatPoint& logicalPos) const
{
float xf = logicalPos.x() * m_pageScaleFactor;
float yf = logicalPos.y() * m_pageScaleFactor;
return IntPoint(static_cast<unsigned>(xf), static_cast<unsigned>(yf));
}
void CanvasSurface::createImageBuffer() const
{
ASSERT(!m_imageBuffer);
m_hasCreatedImageBuffer = true;
FloatSize unscaledSize(width(), height());
IntSize size = convertLogicalToDevice(unscaledSize);
if (!size.width() || !size.height())
return;
m_imageBuffer = ImageBuffer::create(size);
// The convertLogicalToDevice MaxCanvasArea check should prevent common cases
// where ImageBuffer::create() returns 0, however we could still be low on memory.
if (!m_imageBuffer)
return;
m_imageBuffer->context()->scale(FloatSize(size.width() / unscaledSize.width(), size.height() / unscaledSize.height()));
m_imageBuffer->context()->setShadowsIgnoreTransforms(true);
}
GraphicsContext* CanvasSurface::drawingContext() const
{
return buffer() ? m_imageBuffer->context() : 0;
}
ImageBuffer* CanvasSurface::buffer() const
{
if (!m_hasCreatedImageBuffer)
createImageBuffer();
return m_imageBuffer.get();
}
AffineTransform CanvasSurface::baseTransform() const
{
ASSERT(m_hasCreatedImageBuffer);
FloatSize unscaledSize(width(), height());
IntSize size = convertLogicalToDevice(unscaledSize);
AffineTransform transform;
if (size.width() && size.height())
transform.scaleNonUniform(size.width() / unscaledSize.width(), size.height() / unscaledSize.height());
transform.multiply(m_imageBuffer->baseTransform());
return transform;
}
// FIXME: Everything below here relies on CanvasSurface really being
// a HTMLCanvasElement.
const SecurityOrigin& CanvasSurface::securityOrigin() const
{
return *(static_cast<const HTMLCanvasElement*>(this)->document()->securityOrigin());
}
RenderBox* CanvasSurface::renderBox() const
{
return static_cast<const HTMLCanvasElement*>(this)->renderBox();
}
RenderStyle* CanvasSurface::computedStyle()
{
return static_cast<HTMLCanvasElement*>(this)->computedStyle();
}
CSSStyleSelector* CanvasSurface::styleSelector()
{
return static_cast<HTMLCanvasElement*>(this)->document()->styleSelector();
}
} // namespace WebCore
/*
* Copyright (C) 2010 Google Inc. All rights reserved.
* Copyright (C) 2010 Torch Mobile (Beijing) Co. Ltd. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef CanvasSurface_h
#define CanvasSurface_h
#include "AffineTransform.h"
#include "IntSize.h"
#include "PlatformString.h"
#include <wtf/OwnPtr.h>
#include <wtf/Noncopyable.h>
namespace WebCore {
class AffineTransform;
class FloatPoint;
class FloatRect;
class FloatSize;
class GraphicsContext;
class ImageBuffer;
class IntPoint;
class CSSStyleSelector;
class RenderBox;
class RenderStyle;
class SecurityOrigin;
typedef int ExceptionCode;
class CanvasSurface : public Noncopyable {
public:
CanvasSurface(float pageScaleFactor);
virtual ~CanvasSurface();
int width() const { return m_size.width(); }
int height() const { return m_size.height(); }
String toDataURL(const String& mimeType, const double* quality, ExceptionCode&);
String toDataURL(const String& mimeType, ExceptionCode& ec) { return toDataURL(mimeType, 0, ec); }
const IntSize& size() const { return m_size; }
virtual void willDraw(const FloatRect&);
GraphicsContext* drawingContext() const;
ImageBuffer* buffer() const;
IntRect convertLogicalToDevice(const FloatRect&) const;
IntSize convertLogicalToDevice(const FloatSize&) const;
IntPoint convertLogicalToDevice(const FloatPoint&) const;
void setOriginTainted() { m_originClean = false; }
bool originClean() const { return m_originClean; }
AffineTransform baseTransform() const;
const SecurityOrigin& securityOrigin() const;
RenderBox* renderBox() const;
RenderStyle* computedStyle();
CSSStyleSelector* styleSelector();
protected:
void setSurfaceSize(const IntSize&);
bool hasCreatedImageBuffer() const { return m_hasCreatedImageBuffer; }
static const int DefaultWidth;
static const int DefaultHeight;
private:
void createImageBuffer() const;
static const float MaxCanvasArea;
IntSize m_size;
float m_pageScaleFactor;
bool m_originClean;
// m_createdImageBuffer means we tried to malloc the buffer. We didn't necessarily get it.
mutable bool m_hasCreatedImageBuffer;
mutable OwnPtr<ImageBuffer> m_imageBuffer;
};
} // namespace WebCore
#endif
......@@ -40,6 +40,7 @@
#include "GraphicsContext.h"
#include "HTMLNames.h"
#include "ImageBuffer.h"
#include "MIMETypeRegistry.h"
#include "Page.h"
#include "RenderHTMLCanvas.h"
#include "Settings.h"
......@@ -55,11 +56,23 @@ namespace WebCore {
using namespace HTMLNames;
// These values come from the WhatWG spec.
static const int DefaultWidth = 300;
static const int DefaultHeight = 150;
// Firefox limits width/height to 32767 pixels, but slows down dramatically before it
// reaches that limit. We limit by area instead, giving us larger maximum dimensions,
// in exchange for a smaller maximum canvas size.
static const float MaxCanvasArea = 32768 * 8192; // Maximum canvas area in CSS pixels
HTMLCanvasElement::HTMLCanvasElement(const QualifiedName& tagName, Document* document)
: HTMLElement(tagName, document)
, CanvasSurface(document->frame() ? document->frame()->page()->chrome()->scaleFactor() : 1)
, m_observer(0)
, m_size(DefaultWidth, DefaultHeight)
, m_ignoreReset(false)
, m_pageScaleFactor(document->frame() ? document->frame()->page()->chrome()->scaleFactor() : 1)
, m_originClean(true)
, m_hasCreatedImageBuffer(false)
{
ASSERT(hasTagName(canvasTag));
}
......@@ -186,7 +199,8 @@ CanvasRenderingContext* HTMLCanvasElement::getContext(const String& type, Canvas
void HTMLCanvasElement::willDraw(const FloatRect& rect)
{
CanvasSurface::willDraw(rect);
if (m_imageBuffer)
m_imageBuffer->clearImage();
if (RenderBox* ro = renderBox()) {
FloatRect destRect = ro->contentBoxRect();
......@@ -290,4 +304,108 @@ void HTMLCanvasElement::recalcStyle(StyleChange change)
}
}
void HTMLCanvasElement::setSurfaceSize(const IntSize& size)
{
m_size = size;
m_hasCreatedImageBuffer = false;
m_imageBuffer.clear();
}
String HTMLCanvasElement::toDataURL(const String& mimeType, const double* quality, ExceptionCode& ec)
{
if (!m_originClean) {
ec = SECURITY_ERR;
return String();
}
if (m_size.isEmpty() || !buffer())
return String("data:,");
String lowercaseMimeType = mimeType.lower();
// FIXME: Make isSupportedImageMIMETypeForEncoding threadsafe (to allow this method to be used on a worker thread).
if (mimeType.isNull() || !MIMETypeRegistry::isSupportedImageMIMETypeForEncoding(lowercaseMimeType))
return buffer()->toDataURL("image/png");
return buffer()->toDataURL(lowercaseMimeType, quality);
}
IntRect HTMLCanvasElement::convertLogicalToDevice(const FloatRect& logicalRect) const
{
return IntRect(convertLogicalToDevice(logicalRect.location()), convertLogicalToDevice(logicalRect.size()));
}
IntSize HTMLCanvasElement::convertLogicalToDevice(const FloatSize& logicalSize) const
{
float wf = ceilf(logicalSize.width() * m_pageScaleFactor);
float hf = ceilf(logicalSize.height() * m_pageScaleFactor);
if (!(wf >= 1 && hf >= 1 && wf * hf <= MaxCanvasArea))
return IntSize();
return IntSize(static_cast<unsigned>(wf), static_cast<unsigned>(hf));
}
IntPoint HTMLCanvasElement::convertLogicalToDevice(const FloatPoint& logicalPos) const
{
float xf = logicalPos.x() * m_pageScaleFactor;
float yf = logicalPos.y() * m_pageScaleFactor;
return IntPoint(static_cast<unsigned>(xf), static_cast<unsigned>(yf));
}
const SecurityOrigin& HTMLCanvasElement::securityOrigin() const
{
return *document()->securityOrigin();
}
CSSStyleSelector* HTMLCanvasElement::styleSelector()
{
return document()->styleSelector();
}
void HTMLCanvasElement::createImageBuffer() const
{
ASSERT(!m_imageBuffer);
m_hasCreatedImageBuffer = true;
FloatSize unscaledSize(width(), height());
IntSize size = convertLogicalToDevice(unscaledSize);
if (!size.width() || !size.height())
return;
m_imageBuffer = ImageBuffer::create(size);
// The convertLogicalToDevice MaxCanvasArea check should prevent common cases
// where ImageBuffer::create() returns 0, however we could still be low on memory.
if (!m_imageBuffer)
return;
m_imageBuffer->context()->scale(FloatSize(size.width() / unscaledSize.width(), size.height() / unscaledSize.height()));
m_imageBuffer->context()->setShadowsIgnoreTransforms(true);
}
GraphicsContext* HTMLCanvasElement::drawingContext() const
{
return buffer() ? m_imageBuffer->context() : 0;
}
ImageBuffer* HTMLCanvasElement::buffer() const
{
if (!m_hasCreatedImageBuffer)
createImageBuffer();
return m_imageBuffer.get();
}
AffineTransform HTMLCanvasElement::baseTransform() const
{
ASSERT(m_hasCreatedImageBuffer);
FloatSize unscaledSize(width(), height());
IntSize size = convertLogicalToDevice(unscaledSize);
AffineTransform transform;
if (size.width() && size.height())
transform.scaleNonUniform(size.width() / unscaledSize.width(), size.height() / unscaledSize.height());
transform.multiply(m_imageBuffer->baseTransform());
return transform;
}
}
......@@ -28,7 +28,6 @@
#ifndef HTMLCanvasElement_h
#define HTMLCanvasElement_h
#include "CanvasSurface.h"
#include "FloatRect.h"
#include "HTMLElement.h"
#include "IntSize.h"
......@@ -43,6 +42,7 @@ class CanvasContextAttributes;
class CanvasRenderingContext;
class GraphicsContext;
class HTMLCanvasElement;
class ImageBuffer;
class IntSize;
class CanvasObserver {
......@@ -54,17 +54,23 @@ public:
virtual void canvasDestroyed(HTMLCanvasElement*) = 0;
};
class HTMLCanvasElement : public HTMLElement, public CanvasSurface {
class HTMLCanvasElement : public HTMLElement {
public:
static PassRefPtr<HTMLCanvasElement> create(Document*);
static PassRefPtr<HTMLCanvasElement> create(const QualifiedName&, Document*);
virtual ~HTMLCanvasElement();
void setObserver(CanvasObserver* observer) { m_observer = observer; }
// Attributes and functions exposed to script
int width() const { return size().width(); }
int height() const { return size().height(); }
const IntSize& size() const { return m_size; }
void setWidth(int);
void setHeight(int);
CanvasRenderingContext* getContext(const String&, CanvasContextAttributes* attributes = 0);
void setSize(const IntSize& newSize)
{
if (newSize == size())
......@@ -76,16 +82,33 @@ public:
reset();
}
virtual void willDraw(const FloatRect&);
CanvasRenderingContext* getContext(const String&, CanvasContextAttributes* attributes = 0);
String toDataURL(const String& mimeType, const double* quality, ExceptionCode&);