1/* 2 * Copyright (C) 2004, 2006, 2009, 2010 Apple Inc. All rights reserved. 3 * Copyright (C) 2007 Alp Toker <alp@atoker.com> 4 * Copyright (C) 2010 Torch Mobile (Beijing) Co. Ltd. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY 16 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR 19 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 22 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 23 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28#ifndef HTMLCanvasElement_h 29#define HTMLCanvasElement_h 30 31#include "FloatRect.h" 32#include "HTMLElement.h" 33#include "IntSize.h" 34#include <wtf/Forward.h> 35 36#if PLATFORM(QT) 37#define DefaultInterpolationQuality InterpolationMedium 38#elif USE(CG) 39#define DefaultInterpolationQuality InterpolationLow 40#else 41#define DefaultInterpolationQuality InterpolationDefault 42#endif 43 44namespace WebCore { 45 46class CanvasContextAttributes; 47class CanvasRenderingContext; 48class GraphicsContext; 49class GraphicsContextStateSaver; 50class HTMLCanvasElement; 51class Image; 52class ImageData; 53class ImageBuffer; 54class IntSize; 55 56class CanvasObserver { 57public: 58 virtual ~CanvasObserver() { } 59 60 virtual void canvasChanged(HTMLCanvasElement*, const FloatRect& changedRect) = 0; 61 virtual void canvasResized(HTMLCanvasElement*) = 0; 62 virtual void canvasDestroyed(HTMLCanvasElement*) = 0; 63}; 64 65class HTMLCanvasElement FINAL : public HTMLElement { 66public: 67 static PassRefPtr<HTMLCanvasElement> create(Document*); 68 static PassRefPtr<HTMLCanvasElement> create(const QualifiedName&, Document*); 69 virtual ~HTMLCanvasElement(); 70 71 void addObserver(CanvasObserver*); 72 void removeObserver(CanvasObserver*); 73 74 // Attributes and functions exposed to script 75 int width() const { return size().width(); } 76 int height() const { return size().height(); } 77 78 const IntSize& size() const { return m_size; } 79 80 void setWidth(int); 81 void setHeight(int); 82 83 void setSize(const IntSize& newSize) 84 { 85 if (newSize == size() && targetDeviceScaleFactor() == m_deviceScaleFactor) 86 return; 87 m_ignoreReset = true; 88 setWidth(newSize.width()); 89 setHeight(newSize.height()); 90 m_ignoreReset = false; 91 reset(); 92 } 93 94 CanvasRenderingContext* getContext(const String&, CanvasContextAttributes* attributes = 0); 95 bool supportsContext(const String&, CanvasContextAttributes* = 0); 96 static bool is2dType(const String&); 97#if ENABLE(WEBGL) 98 static bool is3dType(const String&); 99#endif 100 101 static String toEncodingMimeType(const String& mimeType); 102 String toDataURL(const String& mimeType, const double* quality, ExceptionCode&); 103 String toDataURL(const String& mimeType, ExceptionCode& ec) { return toDataURL(mimeType, 0, ec); } 104 105 // Used for rendering 106 void didDraw(const FloatRect&); 107 void notifyObserversCanvasChanged(const FloatRect&); 108 109 void paint(GraphicsContext*, const LayoutRect&, bool useLowQualityScale = false); 110 111 GraphicsContext* drawingContext() const; 112 GraphicsContext* existingDrawingContext() const; 113 114 CanvasRenderingContext* renderingContext() const { return m_context.get(); } 115 116 ImageBuffer* buffer() const; 117 Image* copiedImage() const; 118 void clearCopiedImage(); 119 PassRefPtr<ImageData> getImageData(); 120 void makePresentationCopy(); 121 void clearPresentationCopy(); 122 123 FloatRect convertLogicalToDevice(const FloatRect&) const; 124 FloatSize convertLogicalToDevice(const FloatSize&) const; 125 126 FloatSize convertDeviceToLogical(const FloatSize&) const; 127 128 SecurityOrigin* securityOrigin() const; 129 void setOriginTainted() { m_originClean = false; } 130 bool originClean() const { return m_originClean; } 131 132 AffineTransform baseTransform() const; 133 134#if ENABLE(WEBGL) 135 bool is3D() const; 136#endif 137 138 void makeRenderingResultsAvailable(); 139 bool hasCreatedImageBuffer() const { return m_hasCreatedImageBuffer; } 140 141 bool shouldAccelerate(const IntSize&) const; 142 143 float deviceScaleFactor() const { return m_deviceScaleFactor; } 144 145private: 146 HTMLCanvasElement(const QualifiedName&, Document*); 147 148 virtual void parseAttribute(const QualifiedName&, const AtomicString&) OVERRIDE; 149 virtual RenderObject* createRenderer(RenderArena*, RenderStyle*); 150 virtual void attach(const AttachContext& = AttachContext()) OVERRIDE; 151 virtual bool areAuthorShadowsAllowed() const OVERRIDE; 152 153 virtual bool canContainRangeEndPoint() const OVERRIDE; 154 virtual bool canStartSelection() const OVERRIDE; 155 156 void reset(); 157 158 float targetDeviceScaleFactor() const; 159 160 void createImageBuffer() const; 161 void clearImageBuffer() const; 162 163 void setSurfaceSize(const IntSize&); 164 165 bool paintsIntoCanvasBuffer() const; 166 167 HashSet<CanvasObserver*> m_observers; 168 169 IntSize m_size; 170 171 OwnPtr<CanvasRenderingContext> m_context; 172 173 bool m_rendererIsCanvas; 174 175 bool m_ignoreReset; 176 FloatRect m_dirtyRect; 177 178 float m_deviceScaleFactor; 179 bool m_originClean; 180 181 // m_createdImageBuffer means we tried to malloc the buffer. We didn't necessarily get it. 182 mutable bool m_hasCreatedImageBuffer; 183 mutable bool m_didClearImageBuffer; 184 mutable OwnPtr<ImageBuffer> m_imageBuffer; 185 mutable OwnPtr<GraphicsContextStateSaver> m_contextStateSaver; 186 187 mutable RefPtr<Image> m_presentedImage; 188 mutable RefPtr<Image> m_copiedImage; // FIXME: This is temporary for platforms that have to copy the image buffer to render (and for CSSCanvasValue). 189}; 190 191} //namespace 192 193#endif 194