1 /* 2 * Copyright (C) 2004, 2006, 2009, 2010 Apple Inc. All rights reserved. 3 * Copyright (C) 2007 Alp Toker <alp (at) 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 "core/html/HTMLElement.h" 32 #include "core/platform/graphics/FloatRect.h" 33 #include "core/platform/graphics/IntSize.h" 34 #include "wtf/Forward.h" 35 36 #define DefaultInterpolationQuality InterpolationMedium 37 38 namespace WebCore { 39 40 class CanvasContextAttributes; 41 class CanvasRenderingContext; 42 class GraphicsContext; 43 class GraphicsContextStateSaver; 44 class HTMLCanvasElement; 45 class Image; 46 class ImageData; 47 class ImageBuffer; 48 class IntSize; 49 50 class CanvasObserver { 51 public: 52 virtual ~CanvasObserver() { } 53 54 virtual void canvasChanged(HTMLCanvasElement*, const FloatRect& changedRect) = 0; 55 virtual void canvasResized(HTMLCanvasElement*) = 0; 56 virtual void canvasDestroyed(HTMLCanvasElement*) = 0; 57 }; 58 59 class HTMLCanvasElement FINAL : public HTMLElement { 60 public: 61 static PassRefPtr<HTMLCanvasElement> create(Document*); 62 static PassRefPtr<HTMLCanvasElement> create(const QualifiedName&, Document*); 63 virtual ~HTMLCanvasElement(); 64 65 void addObserver(CanvasObserver*); 66 void removeObserver(CanvasObserver*); 67 68 // Attributes and functions exposed to script 69 int width() const { return size().width(); } 70 int height() const { return size().height(); } 71 72 const IntSize& size() const { return m_size; } 73 74 void setWidth(int); 75 void setHeight(int); 76 void setAccelerationDisabled(bool accelerationDisabled) { m_accelerationDisabled = accelerationDisabled; } 77 bool accelerationDisabled() const { return m_accelerationDisabled; } 78 79 void setSize(const IntSize& newSize) 80 { 81 if (newSize == size() && m_deviceScaleFactor == 1) 82 return; 83 m_ignoreReset = true; 84 setWidth(newSize.width()); 85 setHeight(newSize.height()); 86 m_ignoreReset = false; 87 reset(); 88 } 89 90 CanvasRenderingContext* getContext(const String&, CanvasContextAttributes* attributes = 0); 91 92 static String toEncodingMimeType(const String& mimeType); 93 String toDataURL(const String& mimeType, const double* quality, ExceptionState&); 94 String toDataURL(const String& mimeType, ExceptionState& es) { return toDataURL(mimeType, 0, es); } 95 96 // Used for rendering 97 void didDraw(const FloatRect&); 98 void notifyObserversCanvasChanged(const FloatRect&); 99 100 void paint(GraphicsContext*, const LayoutRect&, bool useLowQualityScale = false); 101 102 GraphicsContext* drawingContext() const; 103 GraphicsContext* existingDrawingContext() const; 104 105 CanvasRenderingContext* renderingContext() const { return m_context.get(); } 106 107 ImageBuffer* buffer() const; 108 Image* copiedImage() const; 109 void clearCopiedImage(); 110 PassRefPtr<ImageData> getImageData(); 111 void makePresentationCopy(); 112 void clearPresentationCopy(); 113 114 FloatRect convertLogicalToDevice(const FloatRect&) const; 115 FloatSize convertLogicalToDevice(const FloatSize&) const; 116 117 FloatSize convertDeviceToLogical(const FloatSize&) const; 118 119 SecurityOrigin* securityOrigin() const; 120 void setOriginTainted() { m_originClean = false; } 121 bool originClean() const { return m_originClean; } 122 123 StyleResolver* styleResolver(); 124 125 AffineTransform baseTransform() const; 126 127 bool is3D() const; 128 129 void makeRenderingResultsAvailable(); 130 bool hasCreatedImageBuffer() const { return m_hasCreatedImageBuffer; } 131 132 bool shouldAccelerate(const IntSize&) const; 133 134 float deviceScaleFactor() const { return m_deviceScaleFactor; } 135 136 InsertionNotificationRequest insertedInto(ContainerNode*) OVERRIDE; 137 138 private: 139 HTMLCanvasElement(const QualifiedName&, Document*); 140 141 virtual void parseAttribute(const QualifiedName&, const AtomicString&) OVERRIDE; 142 virtual RenderObject* createRenderer(RenderStyle*); 143 virtual bool areAuthorShadowsAllowed() const OVERRIDE { return false; } 144 145 void reset(); 146 147 void createImageBuffer(); 148 void clearImageBuffer(); 149 150 void setSurfaceSize(const IntSize&); 151 152 bool paintsIntoCanvasBuffer() const; 153 154 void setExternallyAllocatedMemory(intptr_t); 155 156 HashSet<CanvasObserver*> m_observers; 157 158 IntSize m_size; 159 160 OwnPtr<CanvasRenderingContext> m_context; 161 162 bool m_rendererIsCanvas; 163 164 bool m_ignoreReset; 165 bool m_accelerationDisabled; 166 FloatRect m_dirtyRect; 167 168 intptr_t m_externallyAllocatedMemory; 169 170 float m_deviceScaleFactor; // FIXME: This is always 1 and should probable be deleted 171 bool m_originClean; 172 173 // m_createdImageBuffer means we tried to malloc the buffer. We didn't necessarily get it. 174 mutable bool m_hasCreatedImageBuffer; 175 mutable bool m_didClearImageBuffer; 176 OwnPtr<ImageBuffer> m_imageBuffer; 177 mutable OwnPtr<GraphicsContextStateSaver> m_contextStateSaver; 178 179 mutable RefPtr<Image> m_presentedImage; 180 mutable RefPtr<Image> m_copiedImage; // FIXME: This is temporary for platforms that have to copy the image buffer to render (and for CSSCanvasValue). 181 }; 182 183 inline HTMLCanvasElement* toHTMLCanvasElement(Node* node) 184 { 185 ASSERT_WITH_SECURITY_IMPLICATION(!node || node->hasTagName(HTMLNames::canvasTag)); 186 return static_cast<HTMLCanvasElement*>(node); 187 } 188 189 } //namespace 190 191 #endif 192