OSDN Git Service

Merge "Merge WebKit at Chromium 9.0.597.83: Trivial merge by git." into honeycomb
[android-x86/external-webkit.git] / WebCore / dom / CanvasSurface.cpp
1 /*
2  * Copyright (C) 2010 Google Inc. All rights reserved.
3  * Copyright (C) 2010 Torch Mobile (Beijing) Co. Ltd. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
15  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
18  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 #include "config.h"
28 #include "CanvasSurface.h"
29
30 #include "AffineTransform.h"
31 #include "ExceptionCode.h"
32 #include "FloatRect.h"
33 #include "GraphicsContext.h"
34 #include "HTMLCanvasElement.h"
35 #include "ImageBuffer.h"
36 #include "MIMETypeRegistry.h"
37
38 namespace WebCore {
39
40 // These values come from the WhatWG spec.
41 const int CanvasSurface::DefaultWidth = 300;
42 const int CanvasSurface::DefaultHeight = 150;
43
44 // Firefox limits width/height to 32767 pixels, but slows down dramatically before it
45 // reaches that limit. We limit by area instead, giving us larger maximum dimensions,
46 // in exchange for a smaller maximum canvas size.
47 const float CanvasSurface::MaxCanvasArea = 32768 * 8192; // Maximum canvas area in CSS pixels
48
49 CanvasSurface::CanvasSurface(float pageScaleFactor)
50     : m_size(DefaultWidth, DefaultHeight)
51 #if PLATFORM(ANDROID)
52     /* In Android we capture the drawing into a displayList, and then replay
53      * that list at various scale factors (sometimes zoomed out, other times
54      * zoomed in for "normal" reading, yet other times at arbitrary zoom values
55      * based on the user's choice). In all of these cases, we do not re-record
56      * the displayList, hence it is usually harmful to perform any pre-rounding,
57      * since we just don't know the actual drawing resolution at record time.
58      */
59     // TODO - may be better to move the ifdef to the call site of this
60     // constructor
61     , m_pageScaleFactor(1.0f)
62 #else
63     , m_pageScaleFactor(pageScaleFactor)
64 #endif
65     , m_originClean(true)
66     , m_hasCreatedImageBuffer(false)
67 {
68 }
69
70 CanvasSurface::~CanvasSurface()
71 {
72 }
73
74 void CanvasSurface::setSurfaceSize(const IntSize& size)
75 {
76     m_size = size;
77     m_hasCreatedImageBuffer = false;
78     m_imageBuffer.clear();
79 }
80
81 String CanvasSurface::toDataURL(const String& mimeType, const double* quality, ExceptionCode& ec)
82 {
83     if (!m_originClean) {
84         ec = SECURITY_ERR;
85         return String();
86     }
87
88     if (m_size.isEmpty() || !buffer())
89         return String("data:,");
90
91     String lowercaseMimeType = mimeType.lower();
92
93     // FIXME: Make isSupportedImageMIMETypeForEncoding threadsafe (to allow this method to be used on a worker thread).
94     if (mimeType.isNull() || !MIMETypeRegistry::isSupportedImageMIMETypeForEncoding(lowercaseMimeType))
95         return buffer()->toDataURL("image/png");
96
97     return buffer()->toDataURL(lowercaseMimeType, quality);
98 }
99
100 void CanvasSurface::willDraw(const FloatRect&)
101 {
102     if (m_imageBuffer)
103         m_imageBuffer->clearImage();
104 }
105
106 IntRect CanvasSurface::convertLogicalToDevice(const FloatRect& logicalRect) const
107 {
108     return IntRect(convertLogicalToDevice(logicalRect.location()), convertLogicalToDevice(logicalRect.size()));
109 }
110
111 IntSize CanvasSurface::convertLogicalToDevice(const FloatSize& logicalSize) const
112 {
113     float wf = ceilf(logicalSize.width() * m_pageScaleFactor);
114     float hf = ceilf(logicalSize.height() * m_pageScaleFactor);
115
116     if (!(wf >= 1 && hf >= 1 && wf * hf <= MaxCanvasArea))
117         return IntSize();
118
119     return IntSize(static_cast<unsigned>(wf), static_cast<unsigned>(hf));
120 }
121
122 IntPoint CanvasSurface::convertLogicalToDevice(const FloatPoint& logicalPos) const
123 {
124     float xf = logicalPos.x() * m_pageScaleFactor;
125     float yf = logicalPos.y() * m_pageScaleFactor;
126
127     return IntPoint(static_cast<unsigned>(xf), static_cast<unsigned>(yf));
128 }
129
130 void CanvasSurface::createImageBuffer() const
131 {
132     ASSERT(!m_imageBuffer);
133
134     m_hasCreatedImageBuffer = true;
135
136     FloatSize unscaledSize(width(), height());
137     IntSize size = convertLogicalToDevice(unscaledSize);
138     if (!size.width() || !size.height())
139         return;
140
141     m_imageBuffer = ImageBuffer::create(size);
142     // The convertLogicalToDevice MaxCanvasArea check should prevent common cases
143     // where ImageBuffer::create() returns 0, however we could still be low on memory.
144     if (!m_imageBuffer)
145         return;
146     m_imageBuffer->context()->scale(FloatSize(size.width() / unscaledSize.width(), size.height() / unscaledSize.height()));
147     m_imageBuffer->context()->setShadowsIgnoreTransforms(true);
148 }
149
150 GraphicsContext* CanvasSurface::drawingContext() const
151 {
152     return buffer() ? m_imageBuffer->context() : 0;
153 }
154
155 ImageBuffer* CanvasSurface::buffer() const
156 {
157     if (!m_hasCreatedImageBuffer)
158         createImageBuffer();
159     return m_imageBuffer.get();
160 }
161
162 AffineTransform CanvasSurface::baseTransform() const
163 {
164     ASSERT(m_hasCreatedImageBuffer);
165     FloatSize unscaledSize(width(), height());
166     IntSize size = convertLogicalToDevice(unscaledSize);
167     AffineTransform transform;
168     if (size.width() && size.height())
169         transform.scaleNonUniform(size.width() / unscaledSize.width(), size.height() / unscaledSize.height());
170     transform.multiply(m_imageBuffer->baseTransform());
171     return transform;
172 }
173
174 // FIXME: Everything below here relies on CanvasSurface really being
175 // a HTMLCanvasElement.
176 const SecurityOrigin& CanvasSurface::securityOrigin() const
177 {
178     return *(static_cast<const HTMLCanvasElement*>(this)->document()->securityOrigin());
179 }
180
181 RenderBox* CanvasSurface::renderBox() const
182 {
183     return static_cast<const HTMLCanvasElement*>(this)->renderBox();
184 }
185
186 RenderStyle* CanvasSurface::computedStyle()
187 {
188     return static_cast<HTMLCanvasElement*>(this)->computedStyle();
189 }
190
191 CSSStyleSelector* CanvasSurface::styleSelector()
192 {
193     return static_cast<HTMLCanvasElement*>(this)->document()->styleSelector();
194 }
195
196 } // namespace WebCore