| 1 | /* |
| 2 | * Copyright (C) 2004-2018 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 | #pragma once |
| 29 | |
| 30 | #include "CanvasBase.h" |
| 31 | #include "FloatRect.h" |
| 32 | #include "HTMLElement.h" |
| 33 | #include "ImageBitmapRenderingContextSettings.h" |
| 34 | #include "IntSize.h" |
| 35 | #include <memory> |
| 36 | #include <wtf/Forward.h> |
| 37 | |
| 38 | #if ENABLE(WEBGL) |
| 39 | #include "WebGLContextAttributes.h" |
| 40 | #endif |
| 41 | |
| 42 | namespace WebCore { |
| 43 | |
| 44 | class BlobCallback; |
| 45 | class CanvasRenderingContext2D; |
| 46 | class GraphicsContext; |
| 47 | class GraphicsContextStateSaver; |
| 48 | class Image; |
| 49 | class ImageBuffer; |
| 50 | class ImageData; |
| 51 | class MediaSample; |
| 52 | class MediaStream; |
| 53 | class WebGLRenderingContextBase; |
| 54 | class GPUCanvasContext; |
| 55 | struct UncachedString; |
| 56 | |
| 57 | namespace DisplayList { |
| 58 | using AsTextFlags = unsigned; |
| 59 | } |
| 60 | |
| 61 | class HTMLCanvasElement final : public HTMLElement, public CanvasBase { |
| 62 | WTF_MAKE_ISO_ALLOCATED(HTMLCanvasElement); |
| 63 | public: |
| 64 | static Ref<HTMLCanvasElement> create(Document&); |
| 65 | static Ref<HTMLCanvasElement> create(const QualifiedName&, Document&); |
| 66 | virtual ~HTMLCanvasElement(); |
| 67 | |
| 68 | unsigned width() const final { return size().width(); } |
| 69 | unsigned height() const final { return size().height(); } |
| 70 | |
| 71 | WEBCORE_EXPORT ExceptionOr<void> setWidth(unsigned); |
| 72 | WEBCORE_EXPORT ExceptionOr<void> setHeight(unsigned); |
| 73 | |
| 74 | const IntSize& size() const final { return m_size; } |
| 75 | |
| 76 | void setSize(const IntSize& newSize) override |
| 77 | { |
| 78 | if (newSize == size()) |
| 79 | return; |
| 80 | m_ignoreReset = true; |
| 81 | setWidth(newSize.width()); |
| 82 | setHeight(newSize.height()); |
| 83 | m_ignoreReset = false; |
| 84 | reset(); |
| 85 | } |
| 86 | |
| 87 | ExceptionOr<Optional<RenderingContext>> getContext(JSC::ExecState&, const String& contextId, Vector<JSC::Strong<JSC::Unknown>>&& arguments); |
| 88 | |
| 89 | CanvasRenderingContext* getContext(const String&); |
| 90 | |
| 91 | static bool is2dType(const String&); |
| 92 | CanvasRenderingContext2D* createContext2d(const String& type); |
| 93 | CanvasRenderingContext2D* getContext2d(const String&); |
| 94 | |
| 95 | #if ENABLE(WEBGL) |
| 96 | static bool isWebGLType(const String&); |
| 97 | WebGLRenderingContextBase* createContextWebGL(const String&, WebGLContextAttributes&& = { }); |
| 98 | WebGLRenderingContextBase* getContextWebGL(const String&, WebGLContextAttributes&& = { }); |
| 99 | #endif |
| 100 | #if ENABLE(WEBGPU) |
| 101 | static bool isWebGPUType(const String&); |
| 102 | GPUCanvasContext* createContextWebGPU(const String&); |
| 103 | GPUCanvasContext* getContextWebGPU(const String&); |
| 104 | #endif |
| 105 | |
| 106 | static bool isBitmapRendererType(const String&); |
| 107 | ImageBitmapRenderingContext* createContextBitmapRenderer(const String&, ImageBitmapRenderingContextSettings&& = { }); |
| 108 | ImageBitmapRenderingContext* getContextBitmapRenderer(const String&, ImageBitmapRenderingContextSettings&& = { }); |
| 109 | |
| 110 | WEBCORE_EXPORT ExceptionOr<UncachedString> toDataURL(const String& mimeType, JSC::JSValue quality); |
| 111 | WEBCORE_EXPORT ExceptionOr<UncachedString> toDataURL(const String& mimeType); |
| 112 | ExceptionOr<void> toBlob(ScriptExecutionContext&, Ref<BlobCallback>&&, const String& mimeType, JSC::JSValue quality); |
| 113 | |
| 114 | // Used for rendering |
| 115 | void didDraw(const FloatRect&) final; |
| 116 | |
| 117 | void paint(GraphicsContext&, const LayoutRect&); |
| 118 | |
| 119 | GraphicsContext* drawingContext() const final; |
| 120 | GraphicsContext* existingDrawingContext() const final; |
| 121 | |
| 122 | #if ENABLE(MEDIA_STREAM) |
| 123 | RefPtr<MediaSample> toMediaSample(); |
| 124 | ExceptionOr<Ref<MediaStream>> captureStream(ScriptExecutionContext&, Optional<double>&& frameRequestRate); |
| 125 | #endif |
| 126 | |
| 127 | ImageBuffer* buffer() const; |
| 128 | Image* copiedImage() const final; |
| 129 | void clearCopiedImage(); |
| 130 | RefPtr<ImageData> getImageData(); |
| 131 | void makePresentationCopy(); |
| 132 | void clearPresentationCopy(); |
| 133 | |
| 134 | SecurityOrigin* securityOrigin() const final; |
| 135 | |
| 136 | AffineTransform baseTransform() const final; |
| 137 | |
| 138 | void makeRenderingResultsAvailable() final; |
| 139 | bool hasCreatedImageBuffer() const { return m_hasCreatedImageBuffer; } |
| 140 | |
| 141 | bool shouldAccelerate(const IntSize&) const; |
| 142 | |
| 143 | WEBCORE_EXPORT void setUsesDisplayListDrawing(bool); |
| 144 | WEBCORE_EXPORT void setTracksDisplayListReplay(bool); |
| 145 | WEBCORE_EXPORT String displayListAsText(DisplayList::AsTextFlags) const; |
| 146 | WEBCORE_EXPORT String replayDisplayListAsText(DisplayList::AsTextFlags) const; |
| 147 | |
| 148 | size_t memoryCost() const; |
| 149 | size_t externalMemoryCost() const; |
| 150 | |
| 151 | // FIXME: Only some canvas rendering contexts need an ImageBuffer. |
| 152 | // It would be better to have the contexts own the buffers. |
| 153 | void setImageBufferAndMarkDirty(std::unique_ptr<ImageBuffer>&&); |
| 154 | |
| 155 | private: |
| 156 | HTMLCanvasElement(const QualifiedName&, Document&); |
| 157 | |
| 158 | bool isHTMLCanvasElement() const final { return true; } |
| 159 | |
| 160 | void parseAttribute(const QualifiedName&, const AtomicString&) final; |
| 161 | RenderPtr<RenderElement> createElementRenderer(RenderStyle&&, const RenderTreePosition&) final; |
| 162 | |
| 163 | bool canContainRangeEndPoint() const final; |
| 164 | bool canStartSelection() const final; |
| 165 | |
| 166 | void reset(); |
| 167 | |
| 168 | void createImageBuffer() const; |
| 169 | void clearImageBuffer() const; |
| 170 | |
| 171 | void setSurfaceSize(const IntSize&); |
| 172 | void setImageBuffer(std::unique_ptr<ImageBuffer>&&) const; |
| 173 | void releaseImageBufferAndContext(); |
| 174 | |
| 175 | bool paintsIntoCanvasBuffer() const; |
| 176 | |
| 177 | bool isGPUBased() const; |
| 178 | |
| 179 | void refCanvasBase() final { HTMLElement::ref(); } |
| 180 | void derefCanvasBase() final { HTMLElement::deref(); } |
| 181 | |
| 182 | ScriptExecutionContext* canvasBaseScriptExecutionContext() const final { return HTMLElement::scriptExecutionContext(); } |
| 183 | |
| 184 | FloatRect m_dirtyRect; |
| 185 | mutable IntSize m_size; |
| 186 | |
| 187 | bool m_ignoreReset { false }; |
| 188 | |
| 189 | bool m_usesDisplayListDrawing { false }; |
| 190 | bool m_tracksDisplayListReplay { false }; |
| 191 | |
| 192 | mutable Lock m_imageBufferAssignmentLock; |
| 193 | |
| 194 | // m_createdImageBuffer means we tried to malloc the buffer. We didn't necessarily get it. |
| 195 | mutable bool m_hasCreatedImageBuffer { false }; |
| 196 | mutable bool m_didClearImageBuffer { false }; |
| 197 | mutable std::unique_ptr<ImageBuffer> m_imageBuffer; |
| 198 | mutable std::unique_ptr<GraphicsContextStateSaver> m_contextStateSaver; |
| 199 | |
| 200 | mutable RefPtr<Image> m_presentedImage; |
| 201 | mutable RefPtr<Image> m_copiedImage; // FIXME: This is temporary for platforms that have to copy the image buffer to render (and for CSSCanvasValue). |
| 202 | }; |
| 203 | |
| 204 | } // namespace WebCore |
| 205 | |
| 206 | namespace WTF { |
| 207 | template<typename ArgType> class TypeCastTraits<const WebCore::HTMLCanvasElement, ArgType, false /* isBaseType */> { |
| 208 | public: |
| 209 | static bool isOfType(ArgType& node) { return checkTagName(node); } |
| 210 | private: |
| 211 | static bool checkTagName(const WebCore::CanvasBase& base) { return base.isHTMLCanvasElement(); } |
| 212 | static bool checkTagName(const WebCore::HTMLElement& element) { return element.hasTagName(WebCore::HTMLNames::canvasTag); } |
| 213 | static bool checkTagName(const WebCore::Node& node) { return node.hasTagName(WebCore::HTMLNames::canvasTag); } |
| 214 | static bool checkTagName(const WebCore::EventTarget& target) { return is<WebCore::Node>(target) && checkTagName(downcast<WebCore::Node>(target)); } |
| 215 | }; |
| 216 | } |
| 217 | |
| 218 | |