OffscreenCanvas.cpp [plain text]
#include "config.h"
#include "OffscreenCanvas.h"
#include "ImageBitmap.h"
#include "WebGLRenderingContext.h"
namespace WebCore {
Ref<OffscreenCanvas> OffscreenCanvas::create(ScriptExecutionContext& context, unsigned width, unsigned height)
{
return adoptRef(*new OffscreenCanvas(context, width, height));
}
OffscreenCanvas::OffscreenCanvas(ScriptExecutionContext& context, unsigned width, unsigned height)
: CanvasBase(&context)
, m_size(width, height)
{
}
OffscreenCanvas::~OffscreenCanvas() = default;
unsigned OffscreenCanvas::width() const
{
return m_size.width();
}
void OffscreenCanvas::setWidth(unsigned newWidth)
{
return m_size.setWidth(newWidth);
}
unsigned OffscreenCanvas::height() const
{
return m_size.height();
}
void OffscreenCanvas::setHeight(unsigned newHeight)
{
return m_size.setHeight(newHeight);
}
const IntSize& OffscreenCanvas::size() const
{
return m_size;
}
void OffscreenCanvas::setSize(const IntSize& newSize)
{
m_size = newSize;
}
#if ENABLE(WEBGL)
ExceptionOr<OffscreenRenderingContext> OffscreenCanvas::getContext(JSC::ExecState& state, RenderingContextType contextType, Vector<JSC::Strong<JSC::Unknown>>&& arguments)
{
if (m_context && contextType == RenderingContextType::Webgl)
return { RefPtr<WebGLRenderingContext> { &downcast<WebGLRenderingContext>(*m_context) } };
if (contextType == RenderingContextType::Webgl) {
auto scope = DECLARE_THROW_SCOPE(state.vm());
auto attributes = convert<IDLDictionary<WebGLContextAttributes>>(state, !arguments.isEmpty() ? arguments[0].get() : JSC::jsUndefined());
RETURN_IF_EXCEPTION(scope, Exception { ExistingExceptionError });
m_context = WebGLRenderingContextBase::create(*this, attributes, "webgl");
if (!m_context)
return { nullptr };
return { RefPtr<WebGLRenderingContext> { &downcast<WebGLRenderingContext>(*m_context) } };
}
return { nullptr };
}
#endif
RefPtr<ImageBitmap> OffscreenCanvas::transferToImageBitmap()
{
if (!m_context)
return nullptr;
#if ENABLE(WEBGL)
if (!is<WebGLRenderingContext>(*m_context))
return nullptr;
auto webGLContext = &downcast<WebGLRenderingContext>(*m_context);
auto imageBitmap = ImageBitmap::create(m_size);
if (!imageBitmap->buffer())
return nullptr;
auto* gc3d = webGLContext->graphicsContext3D();
gc3d->paintRenderingResultsToCanvas(imageBitmap->buffer());
GC3Dfloat clearColor[4];
gc3d->getFloatv(GraphicsContext3D::COLOR_CLEAR_VALUE, clearColor);
gc3d->clearColor(0, 0, 0, 0);
gc3d->clear(GraphicsContext3D::COLOR_BUFFER_BIT | GraphicsContext3D::DEPTH_BUFFER_BIT | GraphicsContext3D::STENCIL_BUFFER_BIT);
gc3d->clearColor(clearColor[0], clearColor[1], clearColor[2], clearColor[3]);
return WTFMove(imageBitmap);
#else
return nullptr;
#endif
}
}