1/*
2 * Copyright (C) 2009, 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 "CanvasBase.h"
29#include "GraphicsLayer.h"
30#include "ScriptWrappable.h"
31#include <wtf/Forward.h>
32#include <wtf/IsoMalloc.h>
33#include <wtf/Noncopyable.h>
34#include <wtf/text/StringHash.h>
35
36namespace WebCore {
37
38class CanvasPattern;
39class HTMLCanvasElement;
40class HTMLImageElement;
41class HTMLVideoElement;
42class ImageBitmap;
43class TypedOMCSSImageValue;
44class WebGLObject;
45
46class CanvasRenderingContext : public ScriptWrappable {
47 WTF_MAKE_NONCOPYABLE(CanvasRenderingContext);
48 WTF_MAKE_ISO_ALLOCATED(CanvasRenderingContext);
49public:
50 virtual ~CanvasRenderingContext();
51
52 static HashSet<CanvasRenderingContext*>& instances(const LockHolder&);
53 static Lock& instancesMutex();
54
55 void ref();
56 WEBCORE_EXPORT void deref();
57
58 CanvasBase& canvasBase() const { return m_canvas; }
59
60 virtual bool is2d() const { return false; }
61 virtual bool isWebGL1() const { return false; }
62 virtual bool isWebGL2() const { return false; }
63 bool isWebGL() const { return isWebGL1() || isWebGL2(); }
64#if ENABLE(WEBGPU)
65 virtual bool isWebGPU() const { return false; }
66#endif
67 virtual bool isGPUBased() const { return false; }
68 virtual bool isAccelerated() const { return false; }
69 virtual bool isBitmapRenderer() const { return false; }
70 virtual bool isPlaceholder() const { return false; }
71 virtual bool isOffscreen2d() const { return false; }
72 virtual bool isPaint() const { return false; }
73
74 virtual void paintRenderingResultsToCanvas() {}
75 virtual PlatformLayer* platformLayer() const { return 0; }
76
77 bool callTracingActive() const { return m_callTracingActive; }
78 void setCallTracingActive(bool callTracingActive) { m_callTracingActive = callTracingActive; }
79
80protected:
81 explicit CanvasRenderingContext(CanvasBase&);
82 bool wouldTaintOrigin(const CanvasPattern*);
83 bool wouldTaintOrigin(const CanvasBase*);
84 bool wouldTaintOrigin(const HTMLImageElement*);
85 bool wouldTaintOrigin(const HTMLVideoElement*);
86 bool wouldTaintOrigin(const ImageBitmap*);
87 bool wouldTaintOrigin(const URL&);
88
89 template<class T> void checkOrigin(const T* arg)
90 {
91 if (wouldTaintOrigin(arg))
92 m_canvas.setOriginTainted();
93 }
94 void checkOrigin(const URL&);
95 void checkOrigin(const TypedOMCSSImageValue&);
96
97 bool m_callTracingActive { false };
98
99private:
100 CanvasBase& m_canvas;
101};
102
103} // namespace WebCore
104
105#define SPECIALIZE_TYPE_TRAITS_CANVASRENDERINGCONTEXT(ToValueTypeName, predicate) \
106SPECIALIZE_TYPE_TRAITS_BEGIN(ToValueTypeName) \
107 static bool isType(const WebCore::CanvasRenderingContext& context) { return context.predicate; } \
108SPECIALIZE_TYPE_TRAITS_END()
109