aboutsummaryrefslogtreecommitdiff
path: root/src/javascript/jsc/bindings/webcore/OffscreenCanvas.h
blob: 464a22275b7db1cc1dcabf0ffe879ce10258279a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#pragma once

#include "root.h"

#include "ContextDestructionObserver.h"
#include "EventTarget.h"
#include "ExceptionOr.h"
#include "IDLTypes.h"
// #include "ImageBuffer.h"
// #include "ImageBufferPipe.h"
// #include "IntSize.h"
#include "ScriptWrappable.h"
#include <wtf/FixedVector.h>
#include <wtf/Forward.h>
#include <wtf/RefCounted.h>
#include <wtf/ThreadSafeRefCounted.h>
#include <wtf/WeakPtr.h>
#include <wtf/text/WTFString.h>

namespace WebCore {
class OffscreenCanvasRenderingContext2D;
class OffscreenCanvas : public RefCounted<OffscreenCanvas>, public EventTargetWithInlineData, private ContextDestructionObserver {
    WTF_MAKE_ISO_ALLOCATED(OffscreenCanvas);

public:
    struct ImageEncodeOptions {
        String type = "image/png";
        double quality = 1.0;
    };

    enum class RenderingContextType {
        _2d,
        Webgl,
        Webgl2
    };
    using OffscreenRenderingContext = std::variant<
#if ENABLE(WEBGL)
        RefPtr<WebGLRenderingContext>,
#endif
#if ENABLE(WEBGL2)
        RefPtr<WebGL2RenderingContext>,
#endif
        RefPtr<OffscreenCanvasRenderingContext2D>>;

    inline unsigned width()
    {
        return m_width;
    }
    inline unsigned height()
    {
        return m_height;
    }
    void setWidth(unsigned dimension)
    {
        m_width = dimension;
    }
    void setHeight(unsigned dimension)
    {
        m_height = dimension;
    }

    ExceptionOr<std::optional<OffscreenRenderingContext>> getContext(JSC::JSGlobalObject&, RenderingContextType);

    using RefCounted::deref;
    using RefCounted::ref;

    static Ref<OffscreenCanvas> create(ScriptExecutionContext&, unsigned width, unsigned height);

    ~OffscreenCanvas();

private:
    OffscreenCanvas(ScriptExecutionContext& ctx, unsigned width, unsigned height);

    bool isOffscreenCanvas() const { return true; }

    ScriptExecutionContext* scriptExecutionContext() const { return ContextDestructionObserver::scriptExecutionContext(); }
    ScriptExecutionContext* canvasBaseScriptExecutionContext() const { return ContextDestructionObserver::scriptExecutionContext(); }

    EventTargetInterface eventTargetInterface() const { return OffscreenCanvasEventTargetInterfaceType; }
    void refEventTarget() { ref(); }
    void derefEventTarget() { deref(); }

    void refCanvasBase() { ref(); }
    void derefCanvasBase() { deref(); }

    // void setSize(const IntSize&) final;
    unsigned m_width;
    unsigned m_height;
};

}