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/. */
|
|
|
|
|
|
|
|
#ifndef MOZILLA_DOM_OFFSCREENCANVAS_H_
|
|
|
|
#define MOZILLA_DOM_OFFSCREENCANVAS_H_
|
|
|
|
|
2017-07-05 18:22:00 +03:00
|
|
|
#include "gfxTypes.h"
|
2015-10-12 06:21:03 +03:00
|
|
|
#include "mozilla/DOMEventTargetHelper.h"
|
2018-01-08 16:05:05 +03:00
|
|
|
#include "mozilla/dom/DOMPrefs.h"
|
2015-10-12 06:21:03 +03:00
|
|
|
#include "mozilla/layers/LayersTypes.h"
|
2015-10-18 08:24:48 +03:00
|
|
|
#include "mozilla/RefPtr.h"
|
2015-10-12 06:21:03 +03:00
|
|
|
#include "CanvasRenderingContextHelper.h"
|
|
|
|
#include "nsCycleCollectionParticipant.h"
|
|
|
|
|
|
|
|
struct JSContext;
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
class ErrorResult;
|
|
|
|
|
|
|
|
namespace layers {
|
|
|
|
class AsyncCanvasRenderer;
|
|
|
|
class CanvasClient;
|
|
|
|
} // namespace layers
|
|
|
|
|
|
|
|
namespace dom {
|
2015-12-18 09:52:17 +03:00
|
|
|
class Blob;
|
2015-12-18 09:52:17 +03:00
|
|
|
class ImageBitmap;
|
2015-10-12 06:21:03 +03:00
|
|
|
|
|
|
|
// This is helper class for transferring OffscreenCanvas to worker thread.
|
|
|
|
// Because OffscreenCanvas is not thread-safe. So we cannot pass Offscreen-
|
|
|
|
// Canvas to worker thread directly. Thus, we create this helper class and
|
|
|
|
// store necessary data in it then pass it to worker thread.
|
|
|
|
struct OffscreenCanvasCloneData final
|
|
|
|
{
|
|
|
|
OffscreenCanvasCloneData(layers::AsyncCanvasRenderer* aRenderer,
|
|
|
|
uint32_t aWidth, uint32_t aHeight,
|
2015-10-12 06:21:03 +03:00
|
|
|
layers::LayersBackend aCompositorBackend,
|
2015-12-18 09:52:17 +03:00
|
|
|
bool aNeutered, bool aIsWriteOnly);
|
2015-10-12 06:21:03 +03:00
|
|
|
~OffscreenCanvasCloneData();
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<layers::AsyncCanvasRenderer> mRenderer;
|
2015-10-12 06:21:03 +03:00
|
|
|
uint32_t mWidth;
|
|
|
|
uint32_t mHeight;
|
2015-10-12 06:21:03 +03:00
|
|
|
layers::LayersBackend mCompositorBackendType;
|
2015-10-12 06:21:03 +03:00
|
|
|
bool mNeutered;
|
2015-12-18 09:52:17 +03:00
|
|
|
bool mIsWriteOnly;
|
2015-10-12 06:21:03 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
class OffscreenCanvas final : public DOMEventTargetHelper
|
|
|
|
, public CanvasRenderingContextHelper
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
|
|
|
NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(OffscreenCanvas, DOMEventTargetHelper)
|
|
|
|
|
2015-12-18 09:52:17 +03:00
|
|
|
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
|
|
|
nsCOMPtr<nsIGlobalObject> GetParentObject() const { return GetOwnerGlobal(); }
|
2015-10-12 06:21:03 +03:00
|
|
|
|
|
|
|
virtual JSObject* WrapObject(JSContext* aCx,
|
|
|
|
JS::Handle<JSObject*> aGivenProto) override;
|
|
|
|
|
2015-12-18 09:52:17 +03:00
|
|
|
static already_AddRefed<OffscreenCanvas>
|
|
|
|
Constructor(const GlobalObject& aGlobal,
|
|
|
|
uint32_t aWidth,
|
|
|
|
uint32_t aHeight,
|
|
|
|
ErrorResult& aRv);
|
|
|
|
|
2015-10-12 06:21:03 +03:00
|
|
|
void ClearResources();
|
|
|
|
|
2015-10-12 06:21:03 +03:00
|
|
|
uint32_t Width() const
|
|
|
|
{
|
|
|
|
return mWidth;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t Height() const
|
|
|
|
{
|
|
|
|
return mHeight;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetWidth(uint32_t aWidth, ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
if (mNeutered) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mWidth != aWidth) {
|
|
|
|
mWidth = aWidth;
|
|
|
|
CanvasAttrChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetHeight(uint32_t aHeight, ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
if (mNeutered) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mHeight != aHeight) {
|
|
|
|
mHeight = aHeight;
|
|
|
|
CanvasAttrChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-18 09:52:17 +03:00
|
|
|
already_AddRefed<ImageBitmap>
|
2017-10-12 21:14:54 +03:00
|
|
|
TransferToImageBitmap(ErrorResult& aRv);
|
2015-12-18 09:52:17 +03:00
|
|
|
|
2015-12-18 09:52:17 +03:00
|
|
|
already_AddRefed<Promise>
|
|
|
|
ToBlob(JSContext* aCx,
|
|
|
|
const nsAString& aType,
|
|
|
|
JS::Handle<JS::Value> aParams,
|
|
|
|
ErrorResult& aRv);
|
|
|
|
|
2015-10-12 06:21:03 +03:00
|
|
|
nsICanvasRenderingContextInternal* GetContext() const
|
|
|
|
{
|
|
|
|
return mCurrentContext;
|
|
|
|
}
|
|
|
|
|
2017-04-12 12:55:23 +03:00
|
|
|
already_AddRefed<gfx::SourceSurface> GetSurfaceSnapshot(gfxAlphaType* aOutAlphaType = nullptr);
|
2015-12-18 09:52:17 +03:00
|
|
|
|
2015-10-12 06:21:03 +03:00
|
|
|
static already_AddRefed<OffscreenCanvas>
|
2015-12-18 09:52:17 +03:00
|
|
|
CreateFromCloneData(nsIGlobalObject* aGlobal, OffscreenCanvasCloneData* aData);
|
2015-10-12 06:21:03 +03:00
|
|
|
|
|
|
|
// Return true on main-thread, and return gfx.offscreencanvas.enabled
|
|
|
|
// on worker thread.
|
|
|
|
static bool PrefEnabledOnWorkerThread(JSContext* aCx, JSObject* aObj);
|
|
|
|
|
|
|
|
OffscreenCanvasCloneData* ToCloneData();
|
|
|
|
|
|
|
|
void CommitFrameToCompositor();
|
|
|
|
|
|
|
|
virtual bool GetOpaqueAttr() override
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual nsIntSize GetWidthHeight() override
|
|
|
|
{
|
|
|
|
return nsIntSize(mWidth, mHeight);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual already_AddRefed<nsICanvasRenderingContextInternal>
|
|
|
|
CreateContext(CanvasContextType aContextType) override;
|
|
|
|
|
|
|
|
virtual already_AddRefed<nsISupports>
|
|
|
|
GetContext(JSContext* aCx,
|
|
|
|
const nsAString& aContextId,
|
|
|
|
JS::Handle<JS::Value> aContextOptions,
|
|
|
|
ErrorResult& aRv) override;
|
|
|
|
|
|
|
|
void SetNeutered()
|
|
|
|
{
|
|
|
|
mNeutered = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsNeutered() const
|
|
|
|
{
|
|
|
|
return mNeutered;
|
|
|
|
}
|
|
|
|
|
2015-12-18 09:52:17 +03:00
|
|
|
void SetWriteOnly()
|
|
|
|
{
|
|
|
|
mIsWriteOnly = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsWriteOnly() const
|
|
|
|
{
|
|
|
|
return mIsWriteOnly;
|
|
|
|
}
|
|
|
|
|
2015-10-12 06:21:03 +03:00
|
|
|
layers::LayersBackend GetCompositorBackendType() const
|
|
|
|
{
|
|
|
|
return mCompositorBackendType;
|
|
|
|
}
|
|
|
|
|
2015-10-12 06:21:03 +03:00
|
|
|
private:
|
|
|
|
~OffscreenCanvas();
|
|
|
|
|
2015-12-18 09:52:17 +03:00
|
|
|
nsCOMPtr<nsIGlobalObject> GetGlobalObject();
|
|
|
|
|
2015-10-12 06:21:03 +03:00
|
|
|
void CanvasAttrChanged()
|
|
|
|
{
|
|
|
|
mAttrDirty = true;
|
2015-10-18 14:14:22 +03:00
|
|
|
ErrorResult dummy;
|
|
|
|
UpdateContext(nullptr, JS::NullHandleValue, dummy);
|
2015-10-12 06:21:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool mAttrDirty;
|
|
|
|
bool mNeutered;
|
2015-12-18 09:52:17 +03:00
|
|
|
bool mIsWriteOnly;
|
2015-10-12 06:21:03 +03:00
|
|
|
|
|
|
|
uint32_t mWidth;
|
|
|
|
uint32_t mHeight;
|
|
|
|
|
2015-10-12 06:21:03 +03:00
|
|
|
layers::LayersBackend mCompositorBackendType;
|
|
|
|
|
2016-09-03 02:08:49 +03:00
|
|
|
RefPtr<layers::CanvasClient> mCanvasClient;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<layers::AsyncCanvasRenderer> mCanvasRenderer;
|
2015-10-12 06:21:03 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif // MOZILLA_DOM_OFFSCREENCANVAS_H_
|