2010-05-18 04:04:22 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim:set ts=2 sw=2 sts=2 et cindent: */
|
2012-05-21 11:12:37 +00:00
|
|
|
/* 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/. */
|
2013-01-04 05:16:14 +00:00
|
|
|
#if !defined(mozilla_dom_HTMLCanvasElement_h)
|
|
|
|
#define mozilla_dom_HTMLCanvasElement_h
|
2010-05-18 04:04:22 +00:00
|
|
|
|
2013-05-29 20:43:41 +00:00
|
|
|
#include "mozilla/Attributes.h"
|
2010-05-18 04:04:22 +00:00
|
|
|
#include "nsIDOMHTMLCanvasElement.h"
|
|
|
|
#include "nsGenericHTMLElement.h"
|
|
|
|
#include "nsGkAtoms.h"
|
|
|
|
#include "nsSize.h"
|
2012-07-27 14:03:27 +00:00
|
|
|
#include "nsError.h"
|
2010-05-18 04:04:22 +00:00
|
|
|
|
2013-05-29 13:32:30 +00:00
|
|
|
#include "mozilla/gfx/Rect.h"
|
2010-05-18 04:04:22 +00:00
|
|
|
|
2012-06-06 07:42:47 +00:00
|
|
|
class nsICanvasRenderingContextInternal;
|
2012-08-31 17:45:45 +00:00
|
|
|
class nsITimerCallback;
|
2010-09-18 01:14:59 +00:00
|
|
|
|
2012-07-31 00:42:26 +00:00
|
|
|
namespace mozilla {
|
2012-11-04 08:05:25 +00:00
|
|
|
|
2012-07-31 00:42:26 +00:00
|
|
|
namespace layers {
|
|
|
|
class CanvasLayer;
|
|
|
|
class LayerManager;
|
|
|
|
}
|
2014-04-17 05:29:52 +00:00
|
|
|
namespace gfx {
|
|
|
|
class SourceSurface;
|
|
|
|
}
|
2012-11-04 08:05:25 +00:00
|
|
|
|
2013-01-04 05:16:14 +00:00
|
|
|
namespace dom {
|
|
|
|
|
2014-10-08 16:15:23 +00:00
|
|
|
class File;
|
2013-09-11 22:35:51 +00:00
|
|
|
class FileCallback;
|
2013-01-04 05:16:14 +00:00
|
|
|
class HTMLCanvasPrintState;
|
2013-09-11 22:35:51 +00:00
|
|
|
class PrintCallback;
|
2012-07-31 00:42:26 +00:00
|
|
|
|
2015-01-25 22:22:07 +00:00
|
|
|
enum class CanvasContextType : uint8_t {
|
2014-11-10 23:16:50 +00:00
|
|
|
Canvas2D,
|
|
|
|
WebGL1,
|
|
|
|
WebGL2
|
2015-01-25 22:22:07 +00:00
|
|
|
};
|
2014-11-10 23:16:50 +00:00
|
|
|
|
2015-03-21 16:28:04 +00:00
|
|
|
class HTMLCanvasElement final : public nsGenericHTMLElement,
|
2015-03-27 18:52:19 +00:00
|
|
|
public nsIDOMHTMLCanvasElement
|
2010-05-18 04:04:22 +00:00
|
|
|
{
|
2013-04-13 07:05:40 +00:00
|
|
|
enum {
|
|
|
|
DEFAULT_CANVAS_WIDTH = 300,
|
|
|
|
DEFAULT_CANVAS_HEIGHT = 150
|
|
|
|
};
|
|
|
|
|
2013-01-04 05:16:14 +00:00
|
|
|
typedef layers::CanvasLayer CanvasLayer;
|
|
|
|
typedef layers::LayerManager LayerManager;
|
2010-05-18 04:04:22 +00:00
|
|
|
|
|
|
|
public:
|
2014-08-05 13:19:51 +00:00
|
|
|
explicit HTMLCanvasElement(already_AddRefed<mozilla::dom::NodeInfo>& aNodeInfo);
|
2010-05-18 04:04:22 +00:00
|
|
|
|
2013-01-04 05:16:14 +00:00
|
|
|
NS_IMPL_FROMCONTENT_HTML_WITH_TAG(HTMLCanvasElement, canvas)
|
2012-09-26 14:17:51 +00:00
|
|
|
|
2010-05-18 04:04:22 +00:00
|
|
|
// nsISupports
|
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
|
|
|
|
|
|
|
// nsIDOMHTMLCanvasElement
|
|
|
|
NS_DECL_NSIDOMHTMLCANVASELEMENT
|
|
|
|
|
2010-06-15 21:38:05 +00:00
|
|
|
// CC
|
2013-01-04 05:16:14 +00:00
|
|
|
NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(HTMLCanvasElement,
|
2011-06-21 21:12:50 +00:00
|
|
|
nsGenericHTMLElement)
|
2010-06-15 21:38:05 +00:00
|
|
|
|
2013-04-13 07:05:40 +00:00
|
|
|
// WebIDL
|
|
|
|
uint32_t Height()
|
|
|
|
{
|
|
|
|
return GetUnsignedIntAttr(nsGkAtoms::height, DEFAULT_CANVAS_HEIGHT);
|
|
|
|
}
|
|
|
|
void SetHeight(uint32_t aHeight, ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
SetUnsignedIntAttr(nsGkAtoms::height, aHeight, aRv);
|
|
|
|
}
|
|
|
|
uint32_t Width()
|
|
|
|
{
|
|
|
|
return GetUnsignedIntAttr(nsGkAtoms::width, DEFAULT_CANVAS_WIDTH);
|
|
|
|
}
|
|
|
|
void SetWidth(uint32_t aWidth, ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
SetUnsignedIntAttr(nsGkAtoms::width, aWidth, aRv);
|
|
|
|
}
|
|
|
|
already_AddRefed<nsISupports>
|
|
|
|
GetContext(JSContext* aCx, const nsAString& aContextId,
|
2013-06-07 18:42:12 +00:00
|
|
|
JS::Handle<JS::Value> aContextOptions,
|
|
|
|
ErrorResult& aRv);
|
2013-04-13 07:05:51 +00:00
|
|
|
void ToDataURL(JSContext* aCx, const nsAString& aType,
|
2014-02-19 15:13:38 +00:00
|
|
|
JS::Handle<JS::Value> aParams,
|
2013-05-16 16:36:54 +00:00
|
|
|
nsAString& aDataURL, ErrorResult& aRv)
|
2013-04-13 07:05:40 +00:00
|
|
|
{
|
2014-02-19 15:13:38 +00:00
|
|
|
aRv = ToDataURL(aType, aParams, aCx, aDataURL);
|
2013-04-13 07:05:40 +00:00
|
|
|
}
|
2013-07-24 11:17:00 +00:00
|
|
|
void ToBlob(JSContext* aCx,
|
2013-09-11 22:35:51 +00:00
|
|
|
FileCallback& aCallback,
|
2013-07-24 11:17:00 +00:00
|
|
|
const nsAString& aType,
|
2014-02-19 15:13:38 +00:00
|
|
|
JS::Handle<JS::Value> aParams,
|
2013-09-11 22:35:51 +00:00
|
|
|
ErrorResult& aRv);
|
2013-04-13 07:05:40 +00:00
|
|
|
|
|
|
|
bool MozOpaque() const
|
|
|
|
{
|
|
|
|
return GetBoolAttr(nsGkAtoms::moz_opaque);
|
|
|
|
}
|
|
|
|
void SetMozOpaque(bool aValue, ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
SetHTMLBoolAttr(nsGkAtoms::moz_opaque, aValue, aRv);
|
|
|
|
}
|
2014-10-08 16:15:23 +00:00
|
|
|
already_AddRefed<File> MozGetAsFile(const nsAString& aName,
|
|
|
|
const nsAString& aType,
|
|
|
|
ErrorResult& aRv);
|
2013-04-13 07:05:40 +00:00
|
|
|
already_AddRefed<nsISupports> MozGetIPCContext(const nsAString& aContextId,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsISupports> context;
|
|
|
|
aRv = MozGetIPCContext(aContextId, getter_AddRefs(context));
|
|
|
|
return context.forget();
|
|
|
|
}
|
|
|
|
void MozFetchAsStream(nsIInputStreamCallback* aCallback,
|
|
|
|
const nsAString& aType, ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
aRv = MozFetchAsStream(aCallback, aType);
|
|
|
|
}
|
2013-09-11 22:35:51 +00:00
|
|
|
PrintCallback* GetMozPrintCallback() const;
|
|
|
|
void SetMozPrintCallback(PrintCallback* aCallback);
|
2013-04-13 07:05:40 +00:00
|
|
|
|
2010-05-18 04:04:22 +00:00
|
|
|
/**
|
|
|
|
* Get the size in pixels of this canvas element
|
|
|
|
*/
|
|
|
|
nsIntSize GetSize();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine whether the canvas is write-only.
|
|
|
|
*/
|
2011-09-29 06:19:26 +00:00
|
|
|
bool IsWriteOnly();
|
2010-05-18 04:04:22 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Force the canvas to be write-only.
|
|
|
|
*/
|
|
|
|
void SetWriteOnly();
|
|
|
|
|
2011-03-27 23:59:47 +00:00
|
|
|
/**
|
|
|
|
* Notify that some canvas content has changed and the window may
|
|
|
|
* need to be updated. aDamageRect is in canvas coordinates.
|
|
|
|
*/
|
2012-11-04 08:05:25 +00:00
|
|
|
void InvalidateCanvasContent(const mozilla::gfx::Rect* aDamageRect);
|
2010-05-18 04:04:22 +00:00
|
|
|
/*
|
2011-03-27 23:59:47 +00:00
|
|
|
* Notify that we need to repaint the entire canvas, including updating of
|
|
|
|
* the layer tree.
|
2010-05-18 04:04:22 +00:00
|
|
|
*/
|
2011-03-27 23:59:47 +00:00
|
|
|
void InvalidateCanvas();
|
2010-05-18 04:04:22 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Get the number of contexts in this canvas, and request a context at
|
|
|
|
* an index.
|
|
|
|
*/
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t CountContexts ();
|
|
|
|
nsICanvasRenderingContextInternal *GetContextAtIndex (int32_t index);
|
2010-05-18 04:04:22 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns true if the canvas context content is guaranteed to be opaque
|
|
|
|
* across its entire area.
|
|
|
|
*/
|
2011-09-29 06:19:26 +00:00
|
|
|
bool GetIsOpaque();
|
2010-05-18 04:04:22 +00:00
|
|
|
|
2014-04-17 05:29:52 +00:00
|
|
|
virtual TemporaryRef<gfx::SourceSurface> GetSurfaceSnapshot(bool* aPremultAlpha = nullptr);
|
2010-05-18 04:04:22 +00:00
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
virtual bool ParseAttribute(int32_t aNamespaceID,
|
2010-05-18 04:04:22 +00:00
|
|
|
nsIAtom* aAttribute,
|
|
|
|
const nsAString& aValue,
|
2015-03-21 16:28:04 +00:00
|
|
|
nsAttrValue& aResult) override;
|
|
|
|
nsChangeHint GetAttributeChangeHint(const nsIAtom* aAttribute, int32_t aModType) const override;
|
2010-05-18 04:04:22 +00:00
|
|
|
|
|
|
|
// SetAttr override. C++ is stupid, so have to override both
|
|
|
|
// overloaded methods.
|
2012-08-22 15:56:38 +00:00
|
|
|
nsresult SetAttr(int32_t aNameSpaceID, nsIAtom* aName,
|
2011-09-29 06:19:26 +00:00
|
|
|
const nsAString& aValue, bool aNotify)
|
2010-05-18 04:04:22 +00:00
|
|
|
{
|
2012-07-30 14:20:58 +00:00
|
|
|
return SetAttr(aNameSpaceID, aName, nullptr, aValue, aNotify);
|
2010-05-18 04:04:22 +00:00
|
|
|
}
|
2012-08-22 15:56:38 +00:00
|
|
|
virtual nsresult SetAttr(int32_t aNameSpaceID, nsIAtom* aName,
|
2010-05-18 04:04:22 +00:00
|
|
|
nsIAtom* aPrefix, const nsAString& aValue,
|
2015-03-21 16:28:04 +00:00
|
|
|
bool aNotify) override;
|
2013-09-30 21:02:40 +00:00
|
|
|
|
|
|
|
virtual nsresult UnsetAttr(int32_t aNameSpaceID, nsIAtom* aName,
|
2015-03-21 16:28:04 +00:00
|
|
|
bool aNotify) override;
|
2013-09-30 21:02:40 +00:00
|
|
|
|
2015-03-21 16:28:04 +00:00
|
|
|
virtual nsresult Clone(mozilla::dom::NodeInfo *aNodeInfo, nsINode **aResult) const override;
|
2012-11-14 22:10:08 +00:00
|
|
|
nsresult CopyInnerTo(mozilla::dom::Element* aDest);
|
2010-05-18 04:04:22 +00:00
|
|
|
|
2015-03-21 16:28:04 +00:00
|
|
|
virtual nsresult PreHandleEvent(mozilla::EventChainPreVisitor& aVisitor) override;
|
2014-05-20 04:54:00 +00:00
|
|
|
|
2010-05-18 04:04:22 +00:00
|
|
|
/*
|
|
|
|
* Helpers called by various users of Canvas
|
|
|
|
*/
|
|
|
|
|
2011-03-27 23:59:47 +00:00
|
|
|
already_AddRefed<CanvasLayer> GetCanvasLayer(nsDisplayListBuilder* aBuilder,
|
|
|
|
CanvasLayer *aOldLayer,
|
2010-07-15 21:07:46 +00:00
|
|
|
LayerManager *aManager);
|
2011-07-08 10:42:21 +00:00
|
|
|
// Should return true if the canvas layer should always be marked inactive.
|
|
|
|
// We should return true here if we can't do accelerated compositing with
|
|
|
|
// a non-BasicCanvasLayer.
|
2011-09-29 06:19:26 +00:00
|
|
|
bool ShouldForceInactiveLayer(LayerManager *aManager);
|
2010-05-18 04:04:22 +00:00
|
|
|
|
2011-03-27 23:59:47 +00:00
|
|
|
// Call this whenever we need future changes to the canvas
|
|
|
|
// to trigger fresh invalidation requests. This needs to be called
|
|
|
|
// whenever we render the canvas contents to the screen, or whenever we
|
|
|
|
// take a snapshot of the canvas that needs to be "live" (e.g. -moz-element).
|
2010-05-18 04:04:22 +00:00
|
|
|
void MarkContextClean();
|
|
|
|
|
2013-04-13 07:05:33 +00:00
|
|
|
nsresult GetContext(const nsAString& aContextId, nsISupports** aContext);
|
|
|
|
|
2010-05-18 04:04:22 +00:00
|
|
|
protected:
|
2014-07-08 21:23:16 +00:00
|
|
|
virtual ~HTMLCanvasElement();
|
|
|
|
|
2015-03-21 16:28:04 +00:00
|
|
|
virtual JSObject* WrapNode(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
|
2013-04-13 07:05:51 +00:00
|
|
|
|
2010-05-18 04:04:22 +00:00
|
|
|
nsIntSize GetWidthHeight();
|
|
|
|
|
2013-06-07 18:42:12 +00:00
|
|
|
nsresult UpdateContext(JSContext* aCx, JS::Handle<JS::Value> options);
|
2013-07-24 11:17:00 +00:00
|
|
|
nsresult ParseParams(JSContext* aCx,
|
|
|
|
const nsAString& aType,
|
|
|
|
const JS::Value& aEncoderOptions,
|
|
|
|
nsAString& aParams,
|
|
|
|
bool* usingCustomParseOptions);
|
2013-10-17 02:55:08 +00:00
|
|
|
nsresult ExtractData(nsAString& aType,
|
2010-09-18 01:14:59 +00:00
|
|
|
const nsAString& aOptions,
|
2013-10-17 02:55:08 +00:00
|
|
|
nsIInputStream** aStream);
|
2013-04-13 07:05:22 +00:00
|
|
|
nsresult ToDataURLImpl(JSContext* aCx,
|
|
|
|
const nsAString& aMimeType,
|
|
|
|
const JS::Value& aEncoderOptions,
|
2010-05-18 04:04:22 +00:00
|
|
|
nsAString& aDataURL);
|
2010-09-18 01:14:59 +00:00
|
|
|
nsresult MozGetAsFileImpl(const nsAString& aName,
|
|
|
|
const nsAString& aType,
|
|
|
|
nsIDOMFile** aResult);
|
2012-08-31 17:45:45 +00:00
|
|
|
void CallPrintCallback();
|
2010-05-18 04:04:22 +00:00
|
|
|
|
2014-11-10 23:16:50 +00:00
|
|
|
CanvasContextType mCurrentContextType;
|
2013-01-04 05:16:14 +00:00
|
|
|
nsRefPtr<HTMLCanvasElement> mOriginalCanvas;
|
2013-09-11 22:35:51 +00:00
|
|
|
nsRefPtr<PrintCallback> mPrintCallback;
|
2010-05-18 04:04:22 +00:00
|
|
|
nsCOMPtr<nsICanvasRenderingContextInternal> mCurrentContext;
|
2013-09-11 22:35:51 +00:00
|
|
|
nsRefPtr<HTMLCanvasPrintState> mPrintState;
|
2013-04-13 07:03:57 +00:00
|
|
|
|
2010-05-18 04:04:22 +00:00
|
|
|
public:
|
|
|
|
// Record whether this canvas should be write-only or not.
|
|
|
|
// We set this when script paints an image from a different origin.
|
|
|
|
// We also transitively set it when script paints a canvas which
|
|
|
|
// is itself write-only.
|
2011-09-29 06:19:26 +00:00
|
|
|
bool mWriteOnly;
|
2012-08-31 17:45:45 +00:00
|
|
|
|
|
|
|
bool IsPrintCallbackDone();
|
|
|
|
|
|
|
|
void HandlePrintCallback(nsPresContext::nsPresContextType aType);
|
|
|
|
|
|
|
|
nsresult DispatchPrintCallback(nsITimerCallback* aCallback);
|
|
|
|
|
|
|
|
void ResetPrintCallback();
|
|
|
|
|
2013-01-04 05:16:14 +00:00
|
|
|
HTMLCanvasElement* GetOriginalCanvas();
|
2010-05-18 04:04:22 +00:00
|
|
|
};
|
|
|
|
|
2015-03-21 16:28:04 +00:00
|
|
|
class HTMLCanvasPrintState final : public nsWrapperCache
|
2013-09-11 22:35:51 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
HTMLCanvasPrintState(HTMLCanvasElement* aCanvas,
|
|
|
|
nsICanvasRenderingContextInternal* aContext,
|
|
|
|
nsITimerCallback* aCallback);
|
|
|
|
|
|
|
|
nsISupports* Context() const;
|
|
|
|
|
|
|
|
void Done();
|
|
|
|
|
|
|
|
void NotifyDone();
|
|
|
|
|
|
|
|
bool mIsDone;
|
|
|
|
|
|
|
|
NS_INLINE_DECL_CYCLE_COLLECTING_NATIVE_REFCOUNTING(HTMLCanvasPrintState)
|
|
|
|
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_NATIVE_CLASS(HTMLCanvasPrintState)
|
|
|
|
|
2015-03-21 16:28:04 +00:00
|
|
|
virtual JSObject* WrapObject(JSContext *cx, JS::Handle<JSObject*> aGivenProto) override;
|
2013-09-11 22:35:51 +00:00
|
|
|
|
|
|
|
HTMLCanvasElement* GetParentObject()
|
|
|
|
{
|
|
|
|
return mCanvas;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
~HTMLCanvasPrintState();
|
|
|
|
bool mPendingNotify;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
nsRefPtr<HTMLCanvasElement> mCanvas;
|
|
|
|
nsCOMPtr<nsICanvasRenderingContextInternal> mContext;
|
|
|
|
nsCOMPtr<nsITimerCallback> mCallback;
|
|
|
|
};
|
|
|
|
|
2013-01-04 05:16:14 +00:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif /* mozilla_dom_HTMLCanvasElement_h */
|