Bug 801176 - part1-v8: Modify webidl for 2d canvas. r=smaug, roc
authorvincentliu <vliu@mozilla.com>
Fri, 04 Mar 2016 15:22:16 +0800
changeset 336832 2022afa9aef252c1140990422be98f7c3c9bdd8f
parent 336831 7c6642a759eebe9ae74c9cef9f67e55d9477b994
child 336833 134a8c56335a92fdf3461ecb489a7a4b1dfcaf1c
push id12189
push usercku@mozilla.com
push dateFri, 04 Mar 2016 07:52:22 +0000
reviewerssmaug, roc
bugs801176
milestone47.0a1
Bug 801176 - part1-v8: Modify webidl for 2d canvas. r=smaug, roc --- dom/canvas/CanvasRenderingContext2D.cpp | 38 +++++++++++++++ dom/canvas/CanvasRenderingContext2D.h | 6 +++ dom/webidl/CanvasRenderingContext2D.webidl | 76 ++++++++++++++++++------------ dom/webidl/HTMLCanvasElement.webidl | 2 + dom/webidl/WebGLRenderingContext.webidl | 2 +- dom/workers/WorkerPrefs.h | 1 + 6 files changed, 95 insertions(+), 30 deletions(-)
dom/canvas/CanvasRenderingContext2D.cpp
dom/canvas/CanvasRenderingContext2D.h
dom/webidl/CanvasRenderingContext2D.webidl
dom/webidl/HTMLCanvasElement.webidl
dom/webidl/WebGLRenderingContext.webidl
dom/workers/WorkerPrefs.h
--- a/dom/canvas/CanvasRenderingContext2D.cpp
+++ b/dom/canvas/CanvasRenderingContext2D.cpp
@@ -101,16 +101,17 @@
 #include "nsWrapperCacheInlines.h"
 #include "mozilla/dom/CanvasRenderingContext2DBinding.h"
 #include "mozilla/dom/CanvasPath.h"
 #include "mozilla/dom/HTMLImageElement.h"
 #include "mozilla/dom/HTMLVideoElement.h"
 #include "mozilla/dom/SVGMatrix.h"
 #include "mozilla/dom/TextMetrics.h"
 #include "mozilla/dom/SVGMatrix.h"
+#include "mozilla/dom/WorkerPrivate.h"
 #include "mozilla/FloatingPoint.h"
 #include "nsGlobalWindow.h"
 #include "GLContext.h"
 #include "GLContextProvider.h"
 #include "SVGContentUtils.h"
 #include "SVGImageContext.h"
 #include "nsIScreenManager.h"
 #include "nsFilterInstance.h"
@@ -3451,16 +3452,53 @@ CanvasRenderingContext2D::GetHitRegionRe
 
       return true;
     }
   }
 
   return false;
 }
 
+/* static */ bool
+CanvasRenderingContext2D::PrefCanvasPathEnabled(JSContext* aCx, JSObject* aObj)
+{
+  if (NS_IsMainThread()) {
+   return Preferences::GetBool("canvas.path.enabled");
+  } else {
+   workers::WorkerPrivate* workerPrivate = workers::GetWorkerPrivateFromContext(aCx);
+   MOZ_ASSERT(workerPrivate);
+   return (workerPrivate->CanvasPathEnabled() && workerPrivate->OffscreenCanvasEnabled());
+  }
+}
+
+void
+CanvasRenderingContext2D::GetCanvas(Nullable<dom::OwningHTMLCanvasElementOrOffscreenCanvas>& aRetval)
+{
+    if (mCanvasElement) {
+      MOZ_RELEASE_ASSERT(!mOffscreenCanvas);
+      if (mCanvasElement->IsInNativeAnonymousSubtree()) {
+        aRetval.SetNull();
+      } else {
+        aRetval.SetValue().SetAsHTMLCanvasElement() = mCanvasElement;
+      }
+    } else if (mOffscreenCanvas) {
+      aRetval.SetValue().SetAsOffscreenCanvas() = mOffscreenCanvas;
+    } else {
+      aRetval.SetNull();
+    }
+}
+
+void
+CanvasRenderingContext2D::Commit()
+{
+    if (mOffscreenCanvas) {
+      mOffscreenCanvas->CommitFrameToCompositor();
+    }
+}
+
 /**
  * Used for nsBidiPresUtils::ProcessText
  */
 struct MOZ_STACK_CLASS CanvasBidiProcessor : public nsBidiPresUtils::BidiProcessor
 {
   CanvasBidiProcessor()
     : nsBidiPresUtils::BidiProcessor()
   {
--- a/dom/canvas/CanvasRenderingContext2D.h
+++ b/dom/canvas/CanvasRenderingContext2D.h
@@ -41,16 +41,17 @@ namespace dom {
 class HTMLImageElementOrHTMLCanvasElementOrHTMLVideoElementOrImageBitmap;
 typedef HTMLImageElementOrHTMLCanvasElementOrHTMLVideoElementOrImageBitmap CanvasImageSource;
 class ImageData;
 class StringOrCanvasGradientOrCanvasPattern;
 class OwningStringOrCanvasGradientOrCanvasPattern;
 class TextMetrics;
 class CanvasFilterChainObserver;
 class CanvasPath;
+class OwningHTMLCanvasElementOrOffscreenCanvas;
 
 extern const mozilla::gfx::Float SIGMA_MAX;
 
 template<typename T> class Optional;
 
 struct CanvasBidiProcessor;
 class CanvasRenderingContext2DUserData;
 class CanvasDrawObserver;
@@ -74,16 +75,20 @@ public:
     if (mCanvasElement->IsInNativeAnonymousSubtree()) {
       return nullptr;
     }
 
     // corresponds to changes to the old bindings made in bug 745025
     return mCanvasElement->GetOriginalCanvas();
   }
 
+  void GetCanvas(Nullable<dom::OwningHTMLCanvasElementOrOffscreenCanvas>& aRetval);
+
+  void Commit();
+
   void Save();
   void Restore();
   void Scale(double aX, double aY, mozilla::ErrorResult& aError);
   void Rotate(double aAngle, mozilla::ErrorResult& aError);
   void Translate(double aX, double aY, mozilla::ErrorResult& aError);
   void Transform(double aM11, double aM12, double aM21, double aM22, double aDx,
                  double aDy, mozilla::ErrorResult& aError);
   void SetTransform(double aM11, double aM12, double aM21, double aM22, double aDx,
@@ -538,16 +543,17 @@ public:
 
   // Given a point, return hit region ID if it exists
   nsString GetHitRegion(const mozilla::gfx::Point& aPoint) override;
 
 
   // return true and fills in the bound rect if element has a hit region.
   bool GetHitRegionRect(Element* aElement, nsRect& aRect) override;
 
+  static bool PrefCanvasPathEnabled(JSContext* aCx, JSObject* aObj);
 protected:
   nsresult GetImageDataArray(JSContext* aCx, int32_t aX, int32_t aY,
                              uint32_t aWidth, uint32_t aHeight,
                              JSObject** aRetval);
 
   nsresult PutImageData_explicit(int32_t aX, int32_t aY, uint32_t aW, uint32_t aH,
                                  dom::Uint8ClampedArray* aArray,
                                  bool aHasDirtyRect, int32_t aDirtyX, int32_t aDirtyY,
--- a/dom/webidl/CanvasRenderingContext2D.webidl
+++ b/dom/webidl/CanvasRenderingContext2D.webidl
@@ -26,21 +26,25 @@ dictionary HitRegionOptions {
   Element? control = null;
 };
 
 typedef (HTMLImageElement or
          HTMLCanvasElement or
          HTMLVideoElement or
          ImageBitmap) CanvasImageSource;
 
+[Exposed=(Window,Worker),
+ Func="mozilla::dom::OffscreenCanvas::PrefEnabledOnWorkerThread"]
 interface CanvasRenderingContext2D {
 
   // back-reference to the canvas.  Might be null if we're not
   // associated with a canvas.
-  readonly attribute HTMLCanvasElement? canvas;
+  readonly attribute CanvasObj? canvas;
+
+  void commit();
 
   // state
   void save(); // push state on state stack
   void restore(); // pop state stack and restore state
 
   // transformations (default transform is the identity matrix)
 // NOT IMPLEMENTED           attribute SVGMatrix currentTransform;
   [Throws, LenientFloat]
@@ -56,16 +60,19 @@ interface CanvasRenderingContext2D {
   [Throws]
   void resetTransform();
 
   // compositing
            attribute unrestricted double globalAlpha; // (default 1.0)
            [Throws]
            attribute DOMString globalCompositeOperation; // (default source-over)
 
+  // drawing images
+// NOT IMPLEMENTED           attribute boolean imageSmoothingEnabled; // (default true)
+
   // colors and styles (see also the CanvasDrawingStyles interface)
            attribute (DOMString or CanvasGradient or CanvasPattern) strokeStyle; // (default black)
            attribute (DOMString or CanvasGradient or CanvasPattern) fillStyle; // (default black)
   [NewObject]
   CanvasGradient createLinearGradient(double x0, double y0, double x1, double y1);
   [NewObject, Throws]
   CanvasGradient createRadialGradient(double x0, double y0, double r0, double x1, double y1, double r1);
   [NewObject, Throws]
@@ -75,97 +82,103 @@ interface CanvasRenderingContext2D {
            [LenientFloat]
            attribute double shadowOffsetX; // (default 0)
            [LenientFloat]
            attribute double shadowOffsetY; // (default 0)
            [LenientFloat]
            attribute double shadowBlur; // (default 0)
            attribute DOMString shadowColor; // (default transparent black)
 
-  [Pref="canvas.filters.enabled", SetterThrows]
-  attribute DOMString filter; // (default empty string = no filter)
-
   // rects
   [LenientFloat]
   void clearRect(double x, double y, double w, double h);
   [LenientFloat]
   void fillRect(double x, double y, double w, double h);
   [LenientFloat]
   void strokeRect(double x, double y, double w, double h);
 
   // path API (see also CanvasPathMethods)
   void beginPath();
   void fill(optional CanvasWindingRule winding = "nonzero");
   void fill(Path2D path, optional CanvasWindingRule winding = "nonzero");
   void stroke();
   void stroke(Path2D path);
-  [Pref="canvas.focusring.enabled", Throws] void drawFocusIfNeeded(Element element);
-// NOT IMPLEMENTED  void drawSystemFocusRing(Path path, HTMLElement element);
-  [Pref="canvas.customfocusring.enabled"] boolean drawCustomFocusRing(Element element);
-// NOT IMPLEMENTED  boolean drawCustomFocusRing(Path path, HTMLElement element);
+  [Exposed=Window, Pref="canvas.focusring.enabled", Throws] void drawFocusIfNeeded(Element element);
 // NOT IMPLEMENTED  void scrollPathIntoView();
 // NOT IMPLEMENTED  void scrollPathIntoView(Path path);
   void clip(optional CanvasWindingRule winding = "nonzero");
   void clip(Path2D path, optional CanvasWindingRule winding = "nonzero");
 // NOT IMPLEMENTED  void resetClip();
   boolean isPointInPath(unrestricted double x, unrestricted double y, optional CanvasWindingRule winding = "nonzero");
   boolean isPointInPath(Path2D path, unrestricted double x, unrestricted double y, optional CanvasWindingRule winding = "nonzero");
   boolean isPointInStroke(double x, double y);
   boolean isPointInStroke(Path2D path, unrestricted double x, unrestricted double y);
 
   // text (see also the CanvasDrawingStyles interface)
-  [Throws, LenientFloat]
+  [Exposed=Window, Throws, LenientFloat]
   void fillText(DOMString text, double x, double y, optional double maxWidth);
-  [Throws, LenientFloat]
+  [Exposed=Window, Throws, LenientFloat]
   void strokeText(DOMString text, double x, double y, optional double maxWidth);
-  [NewObject, Throws]
+  [Exposed=Window, NewObject, Throws]
   TextMetrics measureText(DOMString text);
 
-  // drawing images
-// NOT IMPLEMENTED           attribute boolean imageSmoothingEnabled; // (default true)
-
   [Throws, LenientFloat]
   void drawImage(CanvasImageSource image, double dx, double dy);
   [Throws, LenientFloat]
   void drawImage(CanvasImageSource image, double dx, double dy, double dw, double dh);
   [Throws, LenientFloat]
   void drawImage(CanvasImageSource image, double sx, double sy, double sw, double sh, double dx, double dy, double dw, double dh);
 
   // hit regions
-  [Pref="canvas.hitregions.enabled", Throws] void addHitRegion(optional HitRegionOptions options);
-  [Pref="canvas.hitregions.enabled"] void removeHitRegion(DOMString id);
-  [Pref="canvas.hitregions.enabled"] void clearHitRegions();
+  [Exposed=Window, Pref="canvas.hitregions.enabled", Throws] void addHitRegion(optional HitRegionOptions options);
+  [Exposed=Window, Pref="canvas.hitregions.enabled"] void removeHitRegion(DOMString id);
+  [Exposed=Window, Pref="canvas.hitregions.enabled"] void clearHitRegions();
 
   // pixel manipulation
   [NewObject, Throws]
   ImageData createImageData(double sw, double sh);
   [NewObject, Throws]
   ImageData createImageData(ImageData imagedata);
   [NewObject, Throws]
   ImageData getImageData(double sx, double sy, double sw, double sh);
   [Throws]
   void putImageData(ImageData imagedata, double dx, double dy);
   [Throws]
   void putImageData(ImageData imagedata, double dx, double dy, double dirtyX, double dirtyY, double dirtyWidth, double dirtyHeight);
+};
+CanvasRenderingContext2D implements CanvasDrawingStyles;
+CanvasRenderingContext2D implements CanvasPathMethods;
+
+[Exposed=(Window,Worker)]
+partial interface CanvasRenderingContext2D {
+  [Throws]
+  attribute any mozDash; /* default |null| */
+
+  [LenientFloat]
+  attribute double mozDashOffset; /* default 0.0 */
+};
+
+[Exposed=Window]
+partial interface CanvasRenderingContext2D {
+  [Pref="canvas.filters.enabled", SetterThrows]
+  attribute DOMString filter; // (default empty string = no filter)
+
+// NOT IMPLEMENTED  void drawSystemFocusRing(Path path, HTMLElement element);
+  [Pref="canvas.customfocusring.enabled"] boolean drawCustomFocusRing(Element element);
+// NOT IMPLEMENTED  boolean drawCustomFocusRing(Path path, HTMLElement element);
 
   // Mozilla-specific stuff
   // FIXME Bug 768048 mozCurrentTransform/mozCurrentTransformInverse should return a WebIDL array.
   [Throws]
   attribute object mozCurrentTransform; // [ m11, m12, m21, m22, dx, dy ], i.e. row major
   [Throws]
   attribute object mozCurrentTransformInverse;
 
   attribute DOMString mozFillRule; /* "evenodd", "nonzero" (default) */
 
-  [Throws]
-  attribute any mozDash; /* default |null| */
-
-  [LenientFloat]
-  attribute double mozDashOffset; /* default 0.0 */
-
   [SetterThrows]
   attribute DOMString mozTextStyle;
 
   // image smoothing mode -- if disabled, images won't be smoothed
   // if scaled.
   attribute boolean mozImageSmoothingEnabled;
 
   // Show the caret if appropriate when drawing
@@ -244,43 +257,43 @@ interface CanvasRenderingContext2D {
 
   /**
    * This causes a context that is currently using a hardware-accelerated
    * backend to fallback to a software one. All state should be preserved.
    */
   [ChromeOnly]
   void demote();
 };
-CanvasRenderingContext2D implements CanvasDrawingStyles;
-CanvasRenderingContext2D implements CanvasPathMethods;
 
-[NoInterfaceObject]
+[NoInterfaceObject, Exposed=(Window,Worker)]
 interface CanvasDrawingStyles {
   // line caps/joins
            [LenientFloat]
            attribute double lineWidth; // (default 1)
            attribute DOMString lineCap; // "butt", "round", "square" (default "butt")
            [GetterThrows]
            attribute DOMString lineJoin; // "round", "bevel", "miter" (default "miter")
            [LenientFloat]
            attribute double miterLimit; // (default 10)
 
   // dashed lines
     [LenientFloat, Throws] void setLineDash(sequence<double> segments); // default empty
     sequence<double> getLineDash();
     [LenientFloat] attribute double lineDashOffset;
 
   // text
-           [SetterThrows]
+           [Exposed=Window, SetterThrows]
            attribute DOMString font; // (default 10px sans-serif)
+           [Exposed=Window]
            attribute DOMString textAlign; // "start", "end", "left", "right", "center" (default: "start")
+           [Exposed=Window]
            attribute DOMString textBaseline; // "top", "hanging", "middle", "alphabetic", "ideographic", "bottom" (default: "alphabetic")
 };
 
-[NoInterfaceObject]
+[NoInterfaceObject, Exposed=(Window,Worker)]
 interface CanvasPathMethods {
   // shared path API methods
   void closePath();
   [LenientFloat]
   void moveTo(double x, double y);
   [LenientFloat]
   void lineTo(double x, double y);
   [LenientFloat]
@@ -296,23 +309,27 @@ interface CanvasPathMethods {
   [LenientFloat]
   void rect(double x, double y, double w, double h);
 
   [Throws, LenientFloat]
   void arc(double x, double y, double radius, double startAngle, double endAngle, optional boolean anticlockwise = false); 
 // NOT IMPLEMENTED  [LenientFloat] void ellipse(double x, double y, double radiusX, double radiusY, double rotation, double startAngle, double endAngle, boolean anticlockwise);
 };
 
+[Exposed=(Window,Worker),
+ Func="mozilla::dom::OffscreenCanvas::PrefEnabledOnWorkerThread"]
 interface CanvasGradient {
   // opaque object
   [Throws]
   // addColorStop should take a double
   void addColorStop(float offset, DOMString color);
 };
 
+[Exposed=(Window,Worker),
+ Func="mozilla::dom::OffscreenCanvas::PrefEnabledOnWorkerThread"]
 interface CanvasPattern {
   // opaque object
   // [Throws, LenientFloat] - could not do this overload because of bug 1020975
   // void setTransform(double a, double b, double c, double d, double e, double f);
 
   // No throw necessary here - SVGMatrix is always good.
   void setTransform(SVGMatrix matrix);
 };
@@ -337,17 +354,18 @@ interface TextMetrics {
   readonly attribute double emHeightDescent;
   readonly attribute double hangingBaseline;
   readonly attribute double alphabeticBaseline;
   readonly attribute double ideographicBaseline;
   */
 
 };
 
-[Pref="canvas.path.enabled",
+[Exposed=(Window,Worker),
+ Func="CanvasRenderingContext2D::PrefCanvasPathEnabled",
  Constructor,
  Constructor(Path2D other),
  Constructor(DOMString pathString)]
 interface Path2D
 {
   void addPath(Path2D path, optional SVGMatrix transformation);
 };
 Path2D implements CanvasPathMethods;
--- a/dom/webidl/HTMLCanvasElement.webidl
+++ b/dom/webidl/HTMLCanvasElement.webidl
@@ -58,11 +58,13 @@ interface MozCanvasPrintState
 {
   // A canvas rendering context.
   readonly attribute nsISupports context;
 
   // To be called when rendering to the context is done.
   void done();
 };
 
+typedef (HTMLCanvasElement or OffscreenCanvas) CanvasObj;
+
 callback PrintCallback = void(MozCanvasPrintState ctx);
 
 callback FileCallback = void(Blob file);
--- a/dom/webidl/WebGLRenderingContext.webidl
+++ b/dom/webidl/WebGLRenderingContext.webidl
@@ -519,17 +519,17 @@ interface WebGLRenderingContext {
     /* WebGL-specific enums */
     const GLenum UNPACK_FLIP_Y_WEBGL            = 0x9240;
     const GLenum UNPACK_PREMULTIPLY_ALPHA_WEBGL = 0x9241;
     const GLenum CONTEXT_LOST_WEBGL             = 0x9242;
     const GLenum UNPACK_COLORSPACE_CONVERSION_WEBGL = 0x9243;
     const GLenum BROWSER_DEFAULT_WEBGL          = 0x9244;
 
     // The canvas might actually be null in some cases, apparently.
-    readonly attribute (HTMLCanvasElement or OffscreenCanvas)? canvas;
+    readonly attribute CanvasObj? canvas;
     readonly attribute GLsizei drawingBufferWidth;
     readonly attribute GLsizei drawingBufferHeight;
 
     [WebGLHandlesContextLoss] WebGLContextAttributes? getContextAttributes();
     [WebGLHandlesContextLoss] boolean isContextLost();
 
     sequence<DOMString>? getSupportedExtensions();
 
--- a/dom/workers/WorkerPrefs.h
+++ b/dom/workers/WorkerPrefs.h
@@ -31,16 +31,17 @@ WORKER_SIMPLE_PREF("dom.webnotifications
 WORKER_SIMPLE_PREF("dom.webnotifications.serviceworker.enabled", DOMServiceWorkerNotificationEnabled, DOM_SERVICEWORKERNOTIFICATION)
 WORKER_SIMPLE_PREF("dom.serviceWorkers.enabled", ServiceWorkersEnabled, SERVICEWORKERS_ENABLED)
 WORKER_SIMPLE_PREF("dom.serviceWorkers.testing.enabled", ServiceWorkersTestingEnabled, SERVICEWORKERS_TESTING_ENABLED)
 WORKER_SIMPLE_PREF("dom.serviceWorkers.openWindow.enabled", OpenWindowEnabled, OPEN_WINDOW_ENABLED)
 WORKER_SIMPLE_PREF("dom.push.enabled", PushEnabled, PUSH_ENABLED)
 WORKER_SIMPLE_PREF("dom.requestcache.enabled", RequestCacheEnabled, REQUESTCACHE_ENABLED)
 WORKER_SIMPLE_PREF("dom.requestcontext.enabled", RequestContextEnabled, REQUESTCONTEXT_ENABLED)
 WORKER_SIMPLE_PREF("gfx.offscreencanvas.enabled", OffscreenCanvasEnabled, OFFSCREENCANVAS_ENABLED)
+WORKER_SIMPLE_PREF("canvas.path.enabled", CanvasPathEnabled, CANVASPATH_ENABLED)
 WORKER_PREF("dom.workers.latestJSVersion", JSVersionChanged)
 WORKER_PREF("intl.accept_languages", PrefLanguagesChanged)
 WORKER_PREF("general.appname.override", AppNameOverrideChanged)
 WORKER_PREF("general.appversion.override", AppVersionOverrideChanged)
 WORKER_PREF("general.platform.override", PlatformOverrideChanged)
 #ifdef JS_GC_ZEAL
 WORKER_PREF("dom.workers.options.gcZeal", LoadGCZealOptions)
 #endif