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 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 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 <memory> 35#include <wtf/Forward.h> 36 37#if USE(CG) 38#define DefaultInterpolationQuality InterpolationLow 39#else 40#define DefaultInterpolationQuality InterpolationDefault 41#endif 42 43namespace WebCore { 44 45class CanvasContextAttributes; 46class CanvasRenderingContext; 47class GraphicsContext; 48class GraphicsContextStateSaver; 49class HTMLCanvasElement; 50class Image; 51class ImageData; 52class ImageBuffer; 53class IntSize; 54 55class CanvasObserver { 56public: 57 virtual ~CanvasObserver() { } 58 59 virtual void canvasChanged(HTMLCanvasElement&, const FloatRect& changedRect) = 0; 60 virtual void canvasResized(HTMLCanvasElement&) = 0; 61 virtual void canvasDestroyed(HTMLCanvasElement&) = 0; 62}; 63 64class HTMLCanvasElement final : public HTMLElement { 65public: 66 static PassRefPtr<HTMLCanvasElement> create(Document&); 67 static PassRefPtr<HTMLCanvasElement> create(const QualifiedName&, Document&); 68 virtual ~HTMLCanvasElement(); 69 70 void addObserver(CanvasObserver&); 71 void removeObserver(CanvasObserver&); 72 73 // Attributes and functions exposed to script 74 int width() const { return size().width(); } 75 int height() const { return size().height(); } 76 77 const IntSize& size() const { return m_size; } 78 79 void setWidth(int); 80 void setHeight(int); 81 82 void setSize(const IntSize& newSize) 83 { 84 if (newSize == size() && targetDeviceScaleFactor() == m_deviceScaleFactor) 85 return; 86 m_ignoreReset = true; 87 setWidth(newSize.width()); 88 setHeight(newSize.height()); 89 m_ignoreReset = false; 90 reset(); 91 } 92 93 CanvasRenderingContext* getContext(const String&, CanvasContextAttributes* attributes = 0); 94 bool probablySupportsContext(const String&, CanvasContextAttributes* = 0); 95 static bool is2dType(const String&); 96#if ENABLE(WEBGL) 97 static bool is3dType(const String&); 98#endif 99 100 static String toEncodingMimeType(const String& mimeType); 101 String toDataURL(const String& mimeType, const double* quality, ExceptionCode&); 102 String toDataURL(const String& mimeType, ExceptionCode& ec) { return toDataURL(mimeType, 0, ec); } 103 104 // Used for rendering 105 void didDraw(const FloatRect&); 106 void notifyObserversCanvasChanged(const FloatRect&); 107 108 void paint(GraphicsContext*, const LayoutRect&, bool useLowQualityScale = false); 109 110 GraphicsContext* drawingContext() const; 111 GraphicsContext* existingDrawingContext() const; 112 113 CanvasRenderingContext* renderingContext() const { return m_context.get(); } 114 115 ImageBuffer* buffer() const; 116 Image* copiedImage() const; 117 void clearCopiedImage(); 118 PassRefPtr<ImageData> getImageData(); 119 void makePresentationCopy(); 120 void clearPresentationCopy(); 121 122 FloatRect convertLogicalToDevice(const FloatRect&) const; 123 FloatSize convertLogicalToDevice(const FloatSize&) const; 124 125 FloatSize convertDeviceToLogical(const FloatSize&) const; 126 127 SecurityOrigin* securityOrigin() const; 128 void setOriginTainted() { m_originClean = false; } 129 bool originClean() const { return m_originClean; } 130 131 AffineTransform baseTransform() const; 132 133 void makeRenderingResultsAvailable(); 134 bool hasCreatedImageBuffer() const { return m_hasCreatedImageBuffer; } 135 136 bool shouldAccelerate(const IntSize&) const; 137 138 float deviceScaleFactor() const { return m_deviceScaleFactor; } 139 140private: 141 HTMLCanvasElement(const QualifiedName&, Document&); 142 143 virtual void parseAttribute(const QualifiedName&, const AtomicString&) override; 144 virtual RenderPtr<RenderElement> createElementRenderer(PassRef<RenderStyle>) override; 145 virtual void willAttachRenderers() override; 146 147 virtual bool canContainRangeEndPoint() const override; 148 virtual bool canStartSelection() const override; 149 150 void reset(); 151 152 float targetDeviceScaleFactor() const; 153 154 void createImageBuffer() const; 155 void clearImageBuffer() const; 156 157 void setSurfaceSize(const IntSize&); 158 159 bool paintsIntoCanvasBuffer() const; 160 161#if ENABLE(WEBGL) 162 bool is3D() const; 163#endif 164 165 HashSet<CanvasObserver*> m_observers; 166 167 IntSize m_size; 168 169 std::unique_ptr<CanvasRenderingContext> m_context; 170 171 bool m_rendererIsCanvas; 172 173 bool m_ignoreReset; 174 FloatRect m_dirtyRect; 175 176 float m_deviceScaleFactor; 177 bool m_originClean; 178 179 // m_createdImageBuffer means we tried to malloc the buffer. We didn't necessarily get it. 180 mutable bool m_hasCreatedImageBuffer; 181 mutable bool m_didClearImageBuffer; 182 mutable std::unique_ptr<ImageBuffer> m_imageBuffer; 183 mutable std::unique_ptr<GraphicsContextStateSaver> m_contextStateSaver; 184 185 mutable RefPtr<Image> m_presentedImage; 186 mutable RefPtr<Image> m_copiedImage; // FIXME: This is temporary for platforms that have to copy the image buffer to render (and for CSSCanvasValue). 187}; 188 189NODE_TYPE_CASTS(HTMLCanvasElement) 190 191} //namespace 192 193#endif 194