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. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#pragma once
27
28#include "AffineTransform.h"
29#include "CanvasBase.h"
30#include "EventTarget.h"
31#include "ExceptionOr.h"
32#include "IntSize.h"
33#include "JSDOMPromiseDeferred.h"
34#include "ScriptWrappable.h"
35#include <wtf/Forward.h>
36#include <wtf/RefCounted.h>
37#include <wtf/text/WTFString.h>
38
39namespace WebCore {
40
41class ImageBitmap;
42class WebGLRenderingContext;
43
44#if ENABLE(WEBGL)
45using OffscreenRenderingContext = RefPtr<WebGLRenderingContext>;
46#endif
47
48class OffscreenCanvas final : public RefCounted<OffscreenCanvas>, public CanvasBase, public EventTargetWithInlineData, private ContextDestructionObserver {
49 WTF_MAKE_ISO_ALLOCATED(OffscreenCanvas);
50public:
51
52 struct ImageEncodeOptions {
53 String type = "image/png";
54 double quality = 1.0;
55 };
56
57 enum class RenderingContextType {
58 _2d,
59 Webgl
60 };
61
62 static Ref<OffscreenCanvas> create(ScriptExecutionContext&, unsigned width, unsigned height);
63 virtual ~OffscreenCanvas();
64
65 unsigned width() const final;
66 void setWidth(unsigned);
67 unsigned height() const final;
68 void setHeight(unsigned);
69
70 const IntSize& size() const final;
71 void setSize(const IntSize&) final;
72
73#if ENABLE(WEBGL)
74 ExceptionOr<OffscreenRenderingContext> getContext(JSC::ExecState&, RenderingContextType, Vector<JSC::Strong<JSC::Unknown>>&& arguments);
75#endif
76 RefPtr<ImageBitmap> transferToImageBitmap();
77 // void convertToBlob(ImageEncodeOptions options);
78
79 GraphicsContext* drawingContext() const final { return nullptr; }
80 GraphicsContext* existingDrawingContext() const final { return nullptr; }
81
82 void makeRenderingResultsAvailable() final { }
83 void didDraw(const FloatRect&) final { }
84
85 AffineTransform baseTransform() const final { return { }; }
86 Image* copiedImage() const final { return nullptr; }
87
88 using RefCounted::ref;
89 using RefCounted::deref;
90
91private:
92
93 OffscreenCanvas(ScriptExecutionContext&, unsigned width, unsigned height);
94
95 bool isOffscreenCanvas() const final { return true; }
96
97 ScriptExecutionContext* scriptExecutionContext() const final { return ContextDestructionObserver::scriptExecutionContext(); }
98 ScriptExecutionContext* canvasBaseScriptExecutionContext() const final { return ContextDestructionObserver::scriptExecutionContext(); }
99
100 EventTargetInterface eventTargetInterface() const final { return OffscreenCanvasEventTargetInterfaceType; }
101 void refEventTarget() final { ref(); }
102 void derefEventTarget() final { deref(); }
103
104 void refCanvasBase() final { ref(); }
105 void derefCanvasBase() final { deref(); }
106
107 IntSize m_size;
108};
109
110}
111
112SPECIALIZE_TYPE_TRAITS_CANVAS(WebCore::OffscreenCanvas, isOffscreenCanvas())
113