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#include "config.h"
27#include "OffscreenCanvas.h"
28
29#include "CanvasRenderingContext.h"
30#include "ImageBitmap.h"
31#include "WebGLRenderingContext.h"
32#include <wtf/IsoMallocInlines.h>
33
34namespace WebCore {
35
36WTF_MAKE_ISO_ALLOCATED_IMPL(OffscreenCanvas);
37
38Ref<OffscreenCanvas> OffscreenCanvas::create(ScriptExecutionContext& context, unsigned width, unsigned height)
39{
40 return adoptRef(*new OffscreenCanvas(context, width, height));
41}
42
43OffscreenCanvas::OffscreenCanvas(ScriptExecutionContext& context, unsigned width, unsigned height)
44 : ContextDestructionObserver(&context)
45 , m_size(width, height)
46{
47}
48
49OffscreenCanvas::~OffscreenCanvas()
50{
51 notifyObserversCanvasDestroyed();
52
53 m_context = nullptr;
54}
55
56unsigned OffscreenCanvas::width() const
57{
58 return m_size.width();
59}
60
61void OffscreenCanvas::setWidth(unsigned newWidth)
62{
63 return m_size.setWidth(newWidth);
64}
65
66unsigned OffscreenCanvas::height() const
67{
68 return m_size.height();
69}
70
71void OffscreenCanvas::setHeight(unsigned newHeight)
72{
73 return m_size.setHeight(newHeight);
74}
75
76const IntSize& OffscreenCanvas::size() const
77{
78 return m_size;
79}
80
81void OffscreenCanvas::setSize(const IntSize& newSize)
82{
83 m_size = newSize;
84}
85
86#if ENABLE(WEBGL)
87ExceptionOr<OffscreenRenderingContext> OffscreenCanvas::getContext(JSC::ExecState& state, RenderingContextType contextType, Vector<JSC::Strong<JSC::Unknown>>&& arguments)
88{
89 if (m_context && contextType == RenderingContextType::Webgl)
90 return { RefPtr<WebGLRenderingContext> { &downcast<WebGLRenderingContext>(*m_context) } };
91
92 if (contextType == RenderingContextType::Webgl) {
93 auto scope = DECLARE_THROW_SCOPE(state.vm());
94 auto attributes = convert<IDLDictionary<WebGLContextAttributes>>(state, !arguments.isEmpty() ? arguments[0].get() : JSC::jsUndefined());
95 RETURN_IF_EXCEPTION(scope, Exception { ExistingExceptionError });
96
97 m_context = WebGLRenderingContextBase::create(*this, attributes, "webgl");
98 if (!m_context)
99 return { nullptr };
100
101 return { RefPtr<WebGLRenderingContext> { &downcast<WebGLRenderingContext>(*m_context) } };
102 }
103
104 return { nullptr };
105}
106#endif
107
108RefPtr<ImageBitmap> OffscreenCanvas::transferToImageBitmap()
109{
110 if (!m_context)
111 return nullptr;
112
113#if ENABLE(WEBGL)
114 if (!is<WebGLRenderingContext>(*m_context))
115 return nullptr;
116
117 auto webGLContext = &downcast<WebGLRenderingContext>(*m_context);
118
119 // FIXME: We're supposed to create an ImageBitmap using the backing
120 // store from this canvas (or its context), but for now we'll just
121 // create a new bitmap and paint into it.
122
123 auto imageBitmap = ImageBitmap::create(m_size);
124 if (!imageBitmap->buffer())
125 return nullptr;
126
127 auto* gc3d = webGLContext->graphicsContext3D();
128 gc3d->paintRenderingResultsToCanvas(imageBitmap->buffer());
129
130 // FIXME: The transfer algorithm requires that the canvas effectively
131 // creates a new backing store. Since we're not doing that yet, we
132 // need to erase what's there.
133
134 GC3Dfloat clearColor[4];
135 gc3d->getFloatv(GraphicsContext3D::COLOR_CLEAR_VALUE, clearColor);
136 gc3d->clearColor(0, 0, 0, 0);
137 gc3d->clear(GraphicsContext3D::COLOR_BUFFER_BIT | GraphicsContext3D::DEPTH_BUFFER_BIT | GraphicsContext3D::STENCIL_BUFFER_BIT);
138 gc3d->clearColor(clearColor[0], clearColor[1], clearColor[2], clearColor[3]);
139
140 return imageBitmap;
141#else
142 return nullptr;
143#endif
144}
145
146}
147