2 * Copyright (C) 2010 Google Inc. All rights reserved.
3 * Copyright (C) 2010 Torch Mobile (Beijing) Co. Ltd. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
28 #include "CanvasSurface.h"
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"
40 // These values come from the WhatWG spec.
41 const int CanvasSurface::DefaultWidth = 300;
42 const int CanvasSurface::DefaultHeight = 150;
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
49 CanvasSurface::CanvasSurface(float pageScaleFactor)
50 : m_size(DefaultWidth, DefaultHeight)
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.
59 // TODO - may be better to move the ifdef to the call site of this
61 , m_pageScaleFactor(1.0f)
63 , m_pageScaleFactor(pageScaleFactor)
66 , m_hasCreatedImageBuffer(false)
70 CanvasSurface::~CanvasSurface()
74 void CanvasSurface::setSurfaceSize(const IntSize& size)
77 m_hasCreatedImageBuffer = false;
78 m_imageBuffer.clear();
81 String CanvasSurface::toDataURL(const String& mimeType, const double* quality, ExceptionCode& ec)
88 if (m_size.isEmpty() || !buffer())
89 return String("data:,");
91 String lowercaseMimeType = mimeType.lower();
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");
97 return buffer()->toDataURL(lowercaseMimeType, quality);
100 void CanvasSurface::willDraw(const FloatRect&)
103 m_imageBuffer->clearImage();
106 IntRect CanvasSurface::convertLogicalToDevice(const FloatRect& logicalRect) const
108 return IntRect(convertLogicalToDevice(logicalRect.location()), convertLogicalToDevice(logicalRect.size()));
111 IntSize CanvasSurface::convertLogicalToDevice(const FloatSize& logicalSize) const
113 float wf = ceilf(logicalSize.width() * m_pageScaleFactor);
114 float hf = ceilf(logicalSize.height() * m_pageScaleFactor);
116 if (!(wf >= 1 && hf >= 1 && wf * hf <= MaxCanvasArea))
119 return IntSize(static_cast<unsigned>(wf), static_cast<unsigned>(hf));
122 IntPoint CanvasSurface::convertLogicalToDevice(const FloatPoint& logicalPos) const
124 float xf = logicalPos.x() * m_pageScaleFactor;
125 float yf = logicalPos.y() * m_pageScaleFactor;
127 return IntPoint(static_cast<unsigned>(xf), static_cast<unsigned>(yf));
130 void CanvasSurface::createImageBuffer() const
132 ASSERT(!m_imageBuffer);
134 m_hasCreatedImageBuffer = true;
136 FloatSize unscaledSize(width(), height());
137 IntSize size = convertLogicalToDevice(unscaledSize);
138 if (!size.width() || !size.height())
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.
146 m_imageBuffer->context()->scale(FloatSize(size.width() / unscaledSize.width(), size.height() / unscaledSize.height()));
147 m_imageBuffer->context()->setShadowsIgnoreTransforms(true);
150 GraphicsContext* CanvasSurface::drawingContext() const
152 return buffer() ? m_imageBuffer->context() : 0;
155 ImageBuffer* CanvasSurface::buffer() const
157 if (!m_hasCreatedImageBuffer)
159 return m_imageBuffer.get();
162 AffineTransform CanvasSurface::baseTransform() const
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());
174 // FIXME: Everything below here relies on CanvasSurface really being
175 // a HTMLCanvasElement.
176 const SecurityOrigin& CanvasSurface::securityOrigin() const
178 return *(static_cast<const HTMLCanvasElement*>(this)->document()->securityOrigin());
181 RenderBox* CanvasSurface::renderBox() const
183 return static_cast<const HTMLCanvasElement*>(this)->renderBox();
186 RenderStyle* CanvasSurface::computedStyle()
188 return static_cast<HTMLCanvasElement*>(this)->computedStyle();
191 CSSStyleSelector* CanvasSurface::styleSelector()
193 return static_cast<HTMLCanvasElement*>(this)->document()->styleSelector();
196 } // namespace WebCore