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 "platform/geometry/FloatRect.h" 33 #include "platform/geometry/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 ImageBufferSurface; 49 class IntSize; 50 51 class CanvasObserver { 52 public: 53 virtual ~CanvasObserver() { } 54 55 virtual void canvasChanged(HTMLCanvasElement*, const FloatRect& changedRect) = 0; 56 virtual void canvasResized(HTMLCanvasElement*) = 0; 57 virtual void canvasDestroyed(HTMLCanvasElement*) = 0; 58 }; 59 60 class HTMLCanvasElement FINAL : public HTMLElement { 61 public: 62 static PassRefPtr<HTMLCanvasElement> create(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()) 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& exceptionState) { return toDataURL(mimeType, 0, exceptionState); } 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 void ensureUnacceleratedImageBuffer(); 108 ImageBuffer* buffer() const; 109 Image* copiedImage() const; 110 void clearCopiedImage(); 111 PassRefPtr<ImageData> getImageData(); 112 void makePresentationCopy(); 113 void clearPresentationCopy(); 114 115 SecurityOrigin* securityOrigin() const; 116 void setOriginTainted() { m_originClean = false; } 117 bool originClean() const { return m_originClean; } 118 119 AffineTransform baseTransform() const; 120 121 bool is3D() const; 122 123 bool hasImageBuffer() const { return m_imageBuffer.get(); } 124 125 bool shouldAccelerate(const IntSize&) const; 126 127 InsertionNotificationRequest insertedInto(ContainerNode*) OVERRIDE; 128 129 private: 130 explicit HTMLCanvasElement(Document&); 131 132 virtual void parseAttribute(const QualifiedName&, const AtomicString&) OVERRIDE; 133 virtual RenderObject* createRenderer(RenderStyle*); 134 virtual bool areAuthorShadowsAllowed() const OVERRIDE { return false; } 135 136 void reset(); 137 138 PassOwnPtr<ImageBufferSurface> createImageBufferSurface(const IntSize& deviceSize, int* msaaSampleCount); 139 void createImageBuffer(); 140 void clearImageBuffer(); 141 142 void setSurfaceSize(const IntSize&); 143 144 bool paintsIntoCanvasBuffer() const; 145 146 void setExternallyAllocatedMemory(intptr_t); 147 148 HashSet<CanvasObserver*> m_observers; 149 150 IntSize m_size; 151 152 OwnPtr<CanvasRenderingContext> m_context; 153 154 bool m_rendererIsCanvas; 155 156 bool m_ignoreReset; 157 bool m_accelerationDisabled; 158 FloatRect m_dirtyRect; 159 160 intptr_t m_externallyAllocatedMemory; 161 162 bool m_originClean; 163 164 // It prevents HTMLCanvasElement::buffer() from continuously re-attempting to allocate an imageBuffer 165 // after the first attempt failed. 166 mutable bool m_didFailToCreateImageBuffer; 167 mutable bool m_didClearImageBuffer; 168 OwnPtr<ImageBuffer> m_imageBuffer; 169 mutable OwnPtr<GraphicsContextStateSaver> m_contextStateSaver; 170 171 mutable RefPtr<Image> m_presentedImage; 172 mutable RefPtr<Image> m_copiedImage; // FIXME: This is temporary for platforms that have to copy the image buffer to render (and for CSSCanvasValue). 173 }; 174 175 DEFINE_NODE_TYPE_CASTS(HTMLCanvasElement, hasTagName(HTMLNames::canvasTag)); 176 177 } //namespace 178 179 #endif 180