1/*
2 * Copyright (C) 2017 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#pragma once
27
28#include <wtf/HashSet.h>
29#include <wtf/TypeCasts.h>
30
31namespace WebCore {
32
33class AffineTransform;
34class CanvasBase;
35class CanvasRenderingContext;
36class Element;
37class GraphicsContext;
38class Image;
39class ImageBuffer;
40class IntSize;
41class FloatRect;
42class ScriptExecutionContext;
43class SecurityOrigin;
44
45class CanvasObserver {
46public:
47 virtual ~CanvasObserver() = default;
48
49 virtual bool isCanvasObserverProxy() const { return false; }
50
51 virtual void canvasChanged(CanvasBase&, const FloatRect& changedRect) = 0;
52 virtual void canvasResized(CanvasBase&) = 0;
53 virtual void canvasDestroyed(CanvasBase&) = 0;
54};
55
56class CanvasBase {
57public:
58 virtual ~CanvasBase();
59
60 virtual void refCanvasBase() = 0;
61 virtual void derefCanvasBase() = 0;
62
63 virtual bool isHTMLCanvasElement() const { return false; }
64 virtual bool isOffscreenCanvas() const { return false; }
65 virtual bool isCustomPaintCanvas() const { return false; }
66
67 virtual unsigned width() const = 0;
68 virtual unsigned height() const = 0;
69 virtual const IntSize& size() const = 0;
70 virtual void setSize(const IntSize&) = 0;
71
72 void setOriginClean() { m_originClean = true; }
73 void setOriginTainted() { m_originClean = false; }
74 bool originClean() const { return m_originClean; }
75
76 virtual SecurityOrigin* securityOrigin() const { return nullptr; }
77 ScriptExecutionContext* scriptExecutionContext() const { return canvasBaseScriptExecutionContext(); }
78
79 CanvasRenderingContext* renderingContext() const;
80
81 void addObserver(CanvasObserver&);
82 void removeObserver(CanvasObserver&);
83 void notifyObserversCanvasChanged(const FloatRect&);
84 void notifyObserversCanvasResized();
85 void notifyObserversCanvasDestroyed(); // Must be called in destruction before clearing m_context.
86
87 HashSet<Element*> cssCanvasClients() const;
88
89 virtual GraphicsContext* drawingContext() const = 0;
90 virtual GraphicsContext* existingDrawingContext() const = 0;
91
92 virtual void makeRenderingResultsAvailable() = 0;
93 virtual void didDraw(const FloatRect&) = 0;
94
95 virtual AffineTransform baseTransform() const = 0;
96 virtual Image* copiedImage() const = 0;
97
98 bool callTracingActive() const;
99
100protected:
101 CanvasBase();
102
103 virtual ScriptExecutionContext* canvasBaseScriptExecutionContext() const = 0;
104
105 std::unique_ptr<CanvasRenderingContext> m_context;
106
107private:
108 bool m_originClean { true };
109#ifndef NDEBUG
110 bool m_didNotifyObserversCanvasDestroyed { false };
111#endif
112 HashSet<CanvasObserver*> m_observers;
113};
114
115} // namespace WebCore
116
117#define SPECIALIZE_TYPE_TRAITS_CANVAS(ToValueTypeName, predicate) \
118SPECIALIZE_TYPE_TRAITS_BEGIN(ToValueTypeName) \
119static bool isType(const WebCore::CanvasBase& canvas) { return canvas.predicate; } \
120SPECIALIZE_TYPE_TRAITS_END()
121