2015-10-12 06:21:03 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim:set ts=2 sw=2 sts=2 et cindent: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "OffscreenCanvas.h"
|
|
|
|
|
2018-01-08 16:05:05 +03:00
|
|
|
#include "mozilla/dom/DOMPrefs.h"
|
2015-10-12 06:21:03 +03:00
|
|
|
#include "mozilla/dom/OffscreenCanvasBinding.h"
|
|
|
|
#include "mozilla/dom/WorkerPrivate.h"
|
2015-12-18 09:52:17 +03:00
|
|
|
#include "mozilla/dom/WorkerScope.h"
|
2015-10-12 06:21:03 +03:00
|
|
|
#include "mozilla/layers/AsyncCanvasRenderer.h"
|
|
|
|
#include "mozilla/layers/CanvasClient.h"
|
|
|
|
#include "mozilla/layers/ImageBridgeChild.h"
|
|
|
|
#include "mozilla/Telemetry.h"
|
|
|
|
#include "CanvasRenderingContext2D.h"
|
|
|
|
#include "CanvasUtils.h"
|
|
|
|
#include "GLScreenBuffer.h"
|
|
|
|
#include "WebGL1Context.h"
|
|
|
|
#include "WebGL2Context.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
|
|
|
OffscreenCanvasCloneData::OffscreenCanvasCloneData(
|
|
|
|
layers::AsyncCanvasRenderer* aRenderer, uint32_t aWidth, uint32_t aHeight,
|
2015-10-12 06:21:03 +03:00
|
|
|
layers::LayersBackend aCompositorBackend, bool aNeutered, bool aIsWriteOnly)
|
2015-10-12 06:21:03 +03:00
|
|
|
: mRenderer(aRenderer),
|
|
|
|
mWidth(aWidth),
|
|
|
|
mHeight(aHeight),
|
2015-10-12 06:21:03 +03:00
|
|
|
mCompositorBackendType(aCompositorBackend),
|
2015-10-12 06:21:03 +03:00
|
|
|
mNeutered(aNeutered),
|
2015-12-18 09:52:17 +03:00
|
|
|
mIsWriteOnly(aIsWriteOnly) {}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-10-12 06:21:03 +03:00
|
|
|
OffscreenCanvasCloneData::~OffscreenCanvasCloneData() {}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-12-18 09:52:17 +03:00
|
|
|
OffscreenCanvas::OffscreenCanvas(nsIGlobalObject* aGlobal, uint32_t aWidth,
|
2015-10-12 06:21:03 +03:00
|
|
|
uint32_t aHeight,
|
2015-10-12 06:21:03 +03:00
|
|
|
layers::LayersBackend aCompositorBackend,
|
2015-10-12 06:21:03 +03:00
|
|
|
layers::AsyncCanvasRenderer* aRenderer)
|
2015-12-18 09:52:17 +03:00
|
|
|
: DOMEventTargetHelper(aGlobal),
|
|
|
|
mAttrDirty(false),
|
2015-10-12 06:21:03 +03:00
|
|
|
mNeutered(false),
|
2015-12-18 09:52:17 +03:00
|
|
|
mIsWriteOnly(false),
|
2015-10-12 06:21:03 +03:00
|
|
|
mWidth(aWidth),
|
|
|
|
mHeight(aHeight),
|
2015-10-12 06:21:03 +03:00
|
|
|
mCompositorBackendType(aCompositorBackend),
|
2015-10-12 06:21:03 +03:00
|
|
|
mCanvasRenderer(aRenderer) {}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-10-12 06:21:03 +03:00
|
|
|
OffscreenCanvas::~OffscreenCanvas() { ClearResources(); }
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-10-12 06:21:03 +03:00
|
|
|
JSObject* OffscreenCanvas::WrapObject(JSContext* aCx,
|
|
|
|
JS::Handle<JSObject*> aGivenProto) {
|
2018-06-26 00:20:54 +03:00
|
|
|
return OffscreenCanvas_Binding::Wrap(aCx, this, aGivenProto);
|
2015-10-12 06:21:03 +03:00
|
|
|
}
|
|
|
|
|
2015-12-18 09:52:17 +03:00
|
|
|
/* static */ already_AddRefed<OffscreenCanvas> OffscreenCanvas::Constructor(
|
|
|
|
const GlobalObject& aGlobal, uint32_t aWidth, uint32_t aHeight,
|
|
|
|
ErrorResult& aRv) {
|
2015-12-18 09:52:17 +03:00
|
|
|
nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(aGlobal.GetAsSupports());
|
2015-12-18 09:52:17 +03:00
|
|
|
RefPtr<OffscreenCanvas> offscreenCanvas = new OffscreenCanvas(
|
|
|
|
global, aWidth, aHeight, layers::LayersBackend::LAYERS_NONE, nullptr);
|
|
|
|
return offscreenCanvas.forget();
|
|
|
|
}
|
|
|
|
|
2015-10-12 06:21:03 +03:00
|
|
|
void OffscreenCanvas::ClearResources() {
|
|
|
|
if (mCanvasClient) {
|
|
|
|
mCanvasClient->Clear();
|
|
|
|
|
|
|
|
if (mCanvasRenderer) {
|
2017-06-01 23:43:00 +03:00
|
|
|
nsCOMPtr<nsISerialEventTarget> activeTarget =
|
|
|
|
mCanvasRenderer->GetActiveEventTarget();
|
2017-06-01 23:42:05 +03:00
|
|
|
MOZ_RELEASE_ASSERT(activeTarget,
|
|
|
|
"GFX: failed to get active event target.");
|
2016-03-05 18:20:47 +03:00
|
|
|
bool current;
|
2017-06-01 23:42:05 +03:00
|
|
|
activeTarget->IsOnCurrentThread(¤t);
|
2016-06-06 18:17:23 +03:00
|
|
|
MOZ_RELEASE_ASSERT(current, "GFX: active thread is not current thread.");
|
2015-10-12 06:21:03 +03:00
|
|
|
mCanvasRenderer->SetCanvasClient(nullptr);
|
|
|
|
mCanvasRenderer->mContext = nullptr;
|
|
|
|
mCanvasRenderer->mGLContext = nullptr;
|
2017-06-01 23:43:00 +03:00
|
|
|
mCanvasRenderer->ResetActiveEventTarget();
|
2015-10-12 06:21:03 +03:00
|
|
|
}
|
2016-09-03 02:08:49 +03:00
|
|
|
|
|
|
|
mCanvasClient = nullptr;
|
2015-10-12 06:21:03 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-12 06:21:03 +03:00
|
|
|
already_AddRefed<nsISupports> OffscreenCanvas::GetContext(
|
|
|
|
JSContext* aCx, const nsAString& aContextId,
|
|
|
|
JS::Handle<JS::Value> aContextOptions, ErrorResult& aRv) {
|
|
|
|
if (mNeutered) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-03-05 18:20:47 +03:00
|
|
|
// We only support WebGL in workers for now
|
2015-10-12 06:21:03 +03:00
|
|
|
CanvasContextType contextType;
|
|
|
|
if (!CanvasUtils::GetCanvasContextType(aContextId, &contextType)) {
|
|
|
|
aRv.Throw(NS_ERROR_NOT_IMPLEMENTED);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(contextType == CanvasContextType::WebGL1 ||
|
2015-12-18 09:52:17 +03:00
|
|
|
contextType == CanvasContextType::WebGL2 ||
|
|
|
|
contextType == CanvasContextType::ImageBitmap)) {
|
2015-10-12 06:21:03 +03:00
|
|
|
aRv.Throw(NS_ERROR_NOT_IMPLEMENTED);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<nsISupports> result = CanvasRenderingContextHelper::GetContext(
|
|
|
|
aCx, aContextId, aContextOptions, aRv);
|
|
|
|
|
2015-10-12 06:21:03 +03:00
|
|
|
if (!mCurrentContext) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2015-10-12 06:21:03 +03:00
|
|
|
|
2016-03-05 18:20:47 +03:00
|
|
|
if (mCanvasRenderer) {
|
2015-12-18 09:52:17 +03:00
|
|
|
if (contextType == CanvasContextType::WebGL1 ||
|
|
|
|
contextType == CanvasContextType::WebGL2) {
|
|
|
|
WebGLContext* webGL = static_cast<WebGLContext*>(mCurrentContext.get());
|
|
|
|
gl::GLContext* gl = webGL->GL();
|
2016-03-05 18:20:47 +03:00
|
|
|
mCanvasRenderer->mContext = mCurrentContext;
|
2017-06-01 23:43:00 +03:00
|
|
|
mCanvasRenderer->SetActiveEventTarget();
|
2015-12-18 09:52:17 +03:00
|
|
|
mCanvasRenderer->mGLContext = gl;
|
|
|
|
mCanvasRenderer->SetIsAlphaPremultiplied(webGL->IsPremultAlpha() ||
|
|
|
|
!gl->Caps().alpha);
|
|
|
|
|
2016-09-14 02:30:57 +03:00
|
|
|
if (RefPtr<ImageBridgeChild> imageBridge =
|
|
|
|
ImageBridgeChild::GetSingleton()) {
|
2016-03-05 18:20:47 +03:00
|
|
|
TextureFlags flags = TextureFlags::ORIGIN_BOTTOM_LEFT;
|
2016-09-14 02:30:57 +03:00
|
|
|
mCanvasClient = imageBridge->CreateCanvasClient(
|
|
|
|
CanvasClient::CanvasClientTypeShSurf, flags);
|
2016-03-05 18:20:47 +03:00
|
|
|
mCanvasRenderer->SetCanvasClient(mCanvasClient);
|
2016-03-04 10:22:39 +03:00
|
|
|
|
2016-03-05 18:20:47 +03:00
|
|
|
gl::GLScreenBuffer* screen = gl->Screen();
|
|
|
|
gl::SurfaceCaps caps = screen->mCaps;
|
|
|
|
auto forwarder = mCanvasClient->GetForwarder();
|
2015-12-18 09:52:17 +03:00
|
|
|
|
2016-03-05 18:20:47 +03:00
|
|
|
UniquePtr<gl::SurfaceFactory> factory =
|
|
|
|
gl::GLScreenBuffer::CreateFactory(gl, caps, forwarder, flags);
|
2015-12-18 09:52:17 +03:00
|
|
|
|
2018-05-30 22:15:35 +03:00
|
|
|
if (factory) screen->Morph(std::move(factory));
|
2016-03-05 18:20:47 +03:00
|
|
|
}
|
2015-10-12 06:21:03 +03:00
|
|
|
}
|
2015-10-12 06:21:03 +03:00
|
|
|
}
|
|
|
|
|
2018-09-21 23:45:49 +03:00
|
|
|
return result.forget();
|
2015-10-12 06:21:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<nsICanvasRenderingContextInternal>
|
|
|
|
OffscreenCanvas::CreateContext(CanvasContextType aContextType) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsICanvasRenderingContextInternal> ret =
|
2015-10-12 06:21:03 +03:00
|
|
|
CanvasRenderingContextHelper::CreateContext(aContextType);
|
|
|
|
|
|
|
|
ret->SetOffscreenCanvas(this);
|
|
|
|
return ret.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
void OffscreenCanvas::CommitFrameToCompositor() {
|
2015-12-18 09:52:17 +03:00
|
|
|
if (!mCanvasRenderer) {
|
|
|
|
// This offscreen canvas doesn't associate to any HTML canvas element.
|
|
|
|
// So, just bail out.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-10-12 06:21:03 +03:00
|
|
|
// The attributes has changed, we have to notify main
|
|
|
|
// thread to change canvas size.
|
|
|
|
if (mAttrDirty) {
|
|
|
|
if (mCanvasRenderer) {
|
|
|
|
mCanvasRenderer->SetWidth(mWidth);
|
|
|
|
mCanvasRenderer->SetHeight(mHeight);
|
|
|
|
mCanvasRenderer->NotifyElementAboutAttributesChanged();
|
|
|
|
}
|
|
|
|
mAttrDirty = false;
|
|
|
|
}
|
|
|
|
|
2016-03-05 18:20:47 +03:00
|
|
|
if (mCurrentContext) {
|
|
|
|
static_cast<WebGLContext*>(mCurrentContext.get())->PresentScreenBuffer();
|
|
|
|
}
|
2015-10-12 06:21:03 +03:00
|
|
|
|
2016-03-05 18:20:47 +03:00
|
|
|
if (mCanvasRenderer && mCanvasRenderer->mGLContext) {
|
2015-10-12 06:21:03 +03:00
|
|
|
mCanvasRenderer->NotifyElementAboutInvalidation();
|
2015-10-12 06:21:03 +03:00
|
|
|
ImageBridgeChild::GetSingleton()->UpdateAsyncCanvasRenderer(
|
|
|
|
mCanvasRenderer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
OffscreenCanvasCloneData* OffscreenCanvas::ToCloneData() {
|
2015-10-12 06:21:03 +03:00
|
|
|
return new OffscreenCanvasCloneData(mCanvasRenderer, mWidth, mHeight,
|
2015-12-18 09:52:17 +03:00
|
|
|
mCompositorBackendType, mNeutered,
|
|
|
|
mIsWriteOnly);
|
2015-10-12 06:21:03 +03:00
|
|
|
}
|
|
|
|
|
2017-10-12 21:14:54 +03:00
|
|
|
already_AddRefed<ImageBitmap> OffscreenCanvas::TransferToImageBitmap(
|
|
|
|
ErrorResult& aRv) {
|
2016-05-01 21:29:23 +03:00
|
|
|
nsCOMPtr<nsIGlobalObject> globalObject = GetGlobalObject();
|
2017-10-12 21:14:54 +03:00
|
|
|
RefPtr<ImageBitmap> result =
|
|
|
|
ImageBitmap::CreateFromOffscreenCanvas(globalObject, *this, aRv);
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2015-12-18 09:52:17 +03:00
|
|
|
|
2017-12-19 05:30:56 +03:00
|
|
|
// TODO: Clear the content?
|
2015-12-18 09:52:17 +03:00
|
|
|
return result.forget();
|
|
|
|
}
|
|
|
|
|
2015-12-18 09:52:17 +03:00
|
|
|
already_AddRefed<Promise> OffscreenCanvas::ToBlob(JSContext* aCx,
|
|
|
|
const nsAString& aType,
|
|
|
|
JS::Handle<JS::Value> aParams,
|
|
|
|
ErrorResult& aRv) {
|
|
|
|
// do a trust check if this is a write-only canvas
|
|
|
|
if (mIsWriteOnly) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_SECURITY_ERR);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIGlobalObject> global = GetGlobalObject();
|
|
|
|
|
|
|
|
RefPtr<Promise> promise = Promise::Create(global, aRv);
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Encoder callback when encoding is complete.
|
|
|
|
class EncodeCallback : public EncodeCompleteCallback {
|
|
|
|
public:
|
|
|
|
EncodeCallback(nsIGlobalObject* aGlobal, Promise* aPromise)
|
|
|
|
: mGlobal(aGlobal), mPromise(aPromise) {}
|
|
|
|
|
|
|
|
// This is called on main thread.
|
2017-11-06 06:37:28 +03:00
|
|
|
nsresult ReceiveBlob(already_AddRefed<Blob> aBlob) override {
|
2015-12-18 09:52:17 +03:00
|
|
|
RefPtr<Blob> blob = aBlob;
|
|
|
|
|
|
|
|
if (mPromise) {
|
|
|
|
RefPtr<Blob> newBlob = Blob::Create(mGlobal, blob->Impl());
|
|
|
|
mPromise->MaybeResolve(newBlob);
|
|
|
|
}
|
|
|
|
|
|
|
|
mGlobal = nullptr;
|
|
|
|
mPromise = nullptr;
|
|
|
|
|
2017-09-12 12:46:51 +03:00
|
|
|
return NS_OK;
|
2015-12-18 09:52:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIGlobalObject> mGlobal;
|
|
|
|
RefPtr<Promise> mPromise;
|
|
|
|
};
|
|
|
|
|
|
|
|
RefPtr<EncodeCompleteCallback> callback = new EncodeCallback(global, promise);
|
|
|
|
|
2017-08-22 09:23:41 +03:00
|
|
|
// TODO: Can we obtain the context and document here somehow
|
|
|
|
// so that we can decide when usePlaceholder should be true/false?
|
|
|
|
// See https://trac.torproject.org/18599
|
|
|
|
// For now, we always return a placeholder if fingerprinting resistance is on.
|
|
|
|
bool usePlaceholder = nsContentUtils::ShouldResistFingerprinting();
|
|
|
|
CanvasRenderingContextHelper::ToBlob(aCx, global, callback, aType, aParams,
|
|
|
|
usePlaceholder, aRv);
|
2015-12-18 09:52:17 +03:00
|
|
|
|
|
|
|
return promise.forget();
|
|
|
|
}
|
|
|
|
|
2015-12-18 09:52:17 +03:00
|
|
|
already_AddRefed<gfx::SourceSurface> OffscreenCanvas::GetSurfaceSnapshot(
|
2017-04-12 12:55:23 +03:00
|
|
|
gfxAlphaType* const aOutAlphaType) {
|
2015-12-18 09:52:17 +03:00
|
|
|
if (!mCurrentContext) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2017-04-12 12:55:23 +03:00
|
|
|
return mCurrentContext->GetSurfaceSnapshot(aOutAlphaType);
|
2015-12-18 09:52:17 +03:00
|
|
|
}
|
2015-12-18 09:52:17 +03:00
|
|
|
|
|
|
|
nsCOMPtr<nsIGlobalObject> OffscreenCanvas::GetGlobalObject() {
|
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
return GetParentObject();
|
|
|
|
}
|
|
|
|
|
2018-01-31 10:24:08 +03:00
|
|
|
dom::WorkerPrivate* workerPrivate = dom::GetCurrentThreadWorkerPrivate();
|
2015-12-18 09:52:17 +03:00
|
|
|
return workerPrivate->GlobalScope();
|
|
|
|
}
|
|
|
|
|
2015-10-12 06:21:03 +03:00
|
|
|
/* static */ already_AddRefed<OffscreenCanvas>
|
2015-12-18 09:52:17 +03:00
|
|
|
OffscreenCanvas::CreateFromCloneData(nsIGlobalObject* aGlobal,
|
|
|
|
OffscreenCanvasCloneData* aData) {
|
2015-10-12 06:21:03 +03:00
|
|
|
MOZ_ASSERT(aData);
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<OffscreenCanvas> wc =
|
2015-12-18 09:52:17 +03:00
|
|
|
new OffscreenCanvas(aGlobal, aData->mWidth, aData->mHeight,
|
2015-10-12 06:21:03 +03:00
|
|
|
aData->mCompositorBackendType, aData->mRenderer);
|
2015-10-12 06:21:03 +03:00
|
|
|
if (aData->mNeutered) {
|
|
|
|
wc->SetNeutered();
|
|
|
|
}
|
|
|
|
return wc.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ bool OffscreenCanvas::PrefEnabledOnWorkerThread(JSContext* aCx,
|
|
|
|
JSObject* aObj) {
|
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-09-10 21:36:16 +03:00
|
|
|
return DOMPrefs::gfx_offscreencanvas_enabled(aCx, aObj);
|
2015-10-12 06:21:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_INHERITED(OffscreenCanvas, DOMEventTargetHelper,
|
|
|
|
mCurrentContext)
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF_INHERITED(OffscreenCanvas, DOMEventTargetHelper)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(OffscreenCanvas, DOMEventTargetHelper)
|
|
|
|
|
2017-08-30 02:02:48 +03:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(OffscreenCanvas)
|
2015-10-12 06:21:03 +03:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
|
|
|
NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper)
|
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|