2012-11-15 01:30:54 -08:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
* 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 "CanvasRenderingContext2D.h"
|
|
|
|
|
2013-01-29 09:51:55 -08:00
|
|
|
#include "nsXULElement.h"
|
2012-11-15 01:30:54 -08:00
|
|
|
|
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
#include "nsMathUtils.h"
|
2014-08-29 07:04:34 -07:00
|
|
|
#include "SVGImageContext.h"
|
2012-11-15 01:30:54 -08:00
|
|
|
|
|
|
|
#include "nsContentUtils.h"
|
|
|
|
|
|
|
|
#include "nsIDocument.h"
|
2013-01-03 21:16:14 -08:00
|
|
|
#include "mozilla/dom/HTMLCanvasElement.h"
|
2012-11-15 01:30:54 -08:00
|
|
|
#include "nsSVGEffects.h"
|
|
|
|
#include "nsPresContext.h"
|
|
|
|
#include "nsIPresShell.h"
|
|
|
|
|
|
|
|
#include "nsIInterfaceRequestorUtils.h"
|
|
|
|
#include "nsIFrame.h"
|
|
|
|
#include "nsError.h"
|
|
|
|
|
|
|
|
#include "nsCSSParser.h"
|
|
|
|
#include "mozilla/css/StyleRule.h"
|
|
|
|
#include "mozilla/css/Declaration.h"
|
|
|
|
#include "nsComputedDOMStyle.h"
|
|
|
|
#include "nsStyleSet.h"
|
|
|
|
|
|
|
|
#include "nsPrintfCString.h"
|
|
|
|
|
|
|
|
#include "nsReadableUtils.h"
|
|
|
|
|
|
|
|
#include "nsColor.h"
|
|
|
|
#include "nsGfxCIID.h"
|
|
|
|
#include "nsIDocShell.h"
|
|
|
|
#include "nsIDOMWindow.h"
|
|
|
|
#include "nsPIDOMWindow.h"
|
|
|
|
#include "nsDisplayList.h"
|
2013-11-04 11:52:24 -08:00
|
|
|
#include "nsFocusManager.h"
|
2012-11-15 01:30:54 -08:00
|
|
|
|
|
|
|
#include "nsTArray.h"
|
|
|
|
|
2013-10-16 19:55:08 -07:00
|
|
|
#include "ImageEncoder.h"
|
2014-08-22 13:12:38 -07:00
|
|
|
#include "ImageRegion.h"
|
2013-05-15 12:25:47 -07:00
|
|
|
|
2012-11-15 01:30:54 -08:00
|
|
|
#include "gfxContext.h"
|
|
|
|
#include "gfxASurface.h"
|
|
|
|
#include "gfxImageSurface.h"
|
|
|
|
#include "gfxPlatform.h"
|
|
|
|
#include "gfxFont.h"
|
|
|
|
#include "gfxBlur.h"
|
|
|
|
#include "gfxUtils.h"
|
|
|
|
|
|
|
|
#include "nsFrameLoader.h"
|
|
|
|
#include "nsBidi.h"
|
|
|
|
#include "nsBidiPresUtils.h"
|
|
|
|
#include "Layers.h"
|
|
|
|
#include "CanvasUtils.h"
|
|
|
|
#include "nsIMemoryReporter.h"
|
|
|
|
#include "nsStyleUtil.h"
|
|
|
|
#include "CanvasImageCache.h"
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
#include "jsapi.h"
|
|
|
|
#include "jsfriendapi.h"
|
|
|
|
|
2013-08-14 00:00:52 -07:00
|
|
|
#include "mozilla/Alignment.h"
|
2012-11-15 01:30:54 -08:00
|
|
|
#include "mozilla/Assertions.h"
|
|
|
|
#include "mozilla/CheckedInt.h"
|
2014-06-03 18:31:43 -07:00
|
|
|
#include "mozilla/DebugOnly.h"
|
2012-11-15 01:30:54 -08:00
|
|
|
#include "mozilla/dom/ContentParent.h"
|
|
|
|
#include "mozilla/dom/ImageData.h"
|
|
|
|
#include "mozilla/dom/PBrowserParent.h"
|
2014-04-10 11:57:42 -07:00
|
|
|
#include "mozilla/dom/ToJSValue.h"
|
2012-11-15 01:30:54 -08:00
|
|
|
#include "mozilla/dom/TypedArray.h"
|
2014-02-26 17:20:15 -08:00
|
|
|
#include "mozilla/Endian.h"
|
2012-11-15 01:30:54 -08:00
|
|
|
#include "mozilla/gfx/2D.h"
|
|
|
|
#include "mozilla/gfx/PathHelpers.h"
|
2013-11-03 08:28:30 -08:00
|
|
|
#include "mozilla/gfx/DataSurfaceHelpers.h"
|
2012-11-15 01:30:54 -08:00
|
|
|
#include "mozilla/ipc/DocumentRendererParent.h"
|
|
|
|
#include "mozilla/ipc/PDocumentRendererParent.h"
|
2013-02-15 19:55:36 -08:00
|
|
|
#include "mozilla/MathAlgorithms.h"
|
2012-11-15 01:30:54 -08:00
|
|
|
#include "mozilla/Preferences.h"
|
|
|
|
#include "mozilla/Telemetry.h"
|
|
|
|
#include "mozilla/unused.h"
|
|
|
|
#include "nsCCUncollectableMarker.h"
|
|
|
|
#include "nsWrapperCacheInlines.h"
|
2013-07-26 11:25:54 -07:00
|
|
|
#include "mozilla/dom/CanvasRenderingContext2DBinding.h"
|
2012-12-31 09:25:46 -08:00
|
|
|
#include "mozilla/dom/HTMLImageElement.h"
|
2013-03-19 05:27:35 -07:00
|
|
|
#include "mozilla/dom/HTMLVideoElement.h"
|
2014-08-25 06:03:00 -07:00
|
|
|
#include "mozilla/dom/SVGMatrix.h"
|
2013-04-19 01:49:22 -07:00
|
|
|
#include "mozilla/dom/TextMetrics.h"
|
2013-07-26 11:25:54 -07:00
|
|
|
#include "mozilla/dom/UnionTypes.h"
|
2014-06-12 07:54:43 -07:00
|
|
|
#include "mozilla/dom/SVGMatrix.h"
|
2013-10-08 08:51:42 -07:00
|
|
|
#include "nsGlobalWindow.h"
|
2013-12-15 13:40:20 -08:00
|
|
|
#include "GLContext.h"
|
|
|
|
#include "GLContextProvider.h"
|
2014-03-24 06:31:19 -07:00
|
|
|
#include "SVGContentUtils.h"
|
2014-08-22 13:12:38 -07:00
|
|
|
#include "SVGImageContext.h"
|
2014-03-27 20:48:22 -07:00
|
|
|
#include "nsIScreenManager.h"
|
2012-11-15 01:30:54 -08:00
|
|
|
|
2013-06-16 19:07:18 -07:00
|
|
|
#undef free // apparently defined by some windows header, clashing with a free()
|
|
|
|
// method in SkTypes.h
|
2014-04-15 12:07:42 -07:00
|
|
|
#ifdef USE_SKIA
|
2014-03-05 13:49:37 -08:00
|
|
|
#include "SkiaGLGlue.h"
|
|
|
|
#include "SurfaceStream.h"
|
2012-11-30 15:58:00 -08:00
|
|
|
#include "SurfaceTypes.h"
|
2014-04-15 12:07:42 -07:00
|
|
|
#endif
|
2014-03-05 13:49:37 -08:00
|
|
|
|
2013-06-16 19:07:18 -07:00
|
|
|
using mozilla::gl::GLContext;
|
2014-03-05 13:49:37 -08:00
|
|
|
using mozilla::gl::SkiaGLGlue;
|
2013-06-16 19:07:18 -07:00
|
|
|
using mozilla::gl::GLContextProvider;
|
2012-11-30 15:58:00 -08:00
|
|
|
|
2012-11-15 01:30:54 -08:00
|
|
|
#ifdef XP_WIN
|
|
|
|
#include "gfxWindowsPlatform.h"
|
|
|
|
#endif
|
|
|
|
|
2013-06-11 05:21:48 -07:00
|
|
|
#ifdef MOZ_WIDGET_GONK
|
|
|
|
#include "mozilla/layers/ShadowLayers.h"
|
|
|
|
#endif
|
|
|
|
|
2012-11-15 01:30:54 -08:00
|
|
|
// windows.h (included by chromium code) defines this, in its infinite wisdom
|
|
|
|
#undef DrawText
|
|
|
|
|
|
|
|
using namespace mozilla;
|
|
|
|
using namespace mozilla::CanvasUtils;
|
|
|
|
using namespace mozilla::css;
|
|
|
|
using namespace mozilla::gfx;
|
2014-08-22 13:12:38 -07:00
|
|
|
using namespace mozilla::image;
|
2012-11-15 01:30:54 -08:00
|
|
|
using namespace mozilla::ipc;
|
|
|
|
using namespace mozilla::layers;
|
|
|
|
|
|
|
|
namespace mgfx = mozilla::gfx;
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
|
|
|
// Cap sigma to avoid overly large temp surfaces.
|
|
|
|
const Float SIGMA_MAX = 100;
|
|
|
|
|
|
|
|
/* Memory reporter stuff */
|
|
|
|
static int64_t gCanvasAzureMemoryUsed = 0;
|
|
|
|
|
|
|
|
// This is KIND_OTHER because it's not always clear where in memory the pixels
|
|
|
|
// of a canvas are stored. Furthermore, this memory will be tracked by the
|
|
|
|
// underlying surface implementations. See bug 655638 for details.
|
2013-12-07 22:09:10 -08:00
|
|
|
class Canvas2dPixelsReporter MOZ_FINAL : public nsIMemoryReporter
|
2013-01-17 16:45:11 -08:00
|
|
|
{
|
2014-06-26 06:30:49 -07:00
|
|
|
~Canvas2dPixelsReporter() {}
|
2013-12-07 22:09:10 -08:00
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
|
|
|
NS_IMETHOD CollectReports(nsIHandleReportCallback* aHandleReport,
|
2014-05-20 23:06:54 -07:00
|
|
|
nsISupports* aData, bool aAnonymize)
|
2013-12-07 22:09:10 -08:00
|
|
|
{
|
|
|
|
return MOZ_COLLECT_REPORT(
|
|
|
|
"canvas-2d-pixels", KIND_OTHER, UNITS_BYTES,
|
|
|
|
gCanvasAzureMemoryUsed,
|
|
|
|
"Memory used by 2D canvases. Each canvas requires "
|
|
|
|
"(width * height * 4) bytes.");
|
|
|
|
}
|
2013-01-17 16:45:11 -08:00
|
|
|
};
|
2012-11-15 01:30:54 -08:00
|
|
|
|
2014-04-27 00:06:00 -07:00
|
|
|
NS_IMPL_ISUPPORTS(Canvas2dPixelsReporter, nsIMemoryReporter)
|
2013-12-07 22:09:10 -08:00
|
|
|
|
2012-11-15 01:30:54 -08:00
|
|
|
class CanvasRadialGradient : public CanvasGradient
|
|
|
|
{
|
|
|
|
public:
|
2013-04-26 07:55:56 -07:00
|
|
|
CanvasRadialGradient(CanvasRenderingContext2D* aContext,
|
|
|
|
const Point &aBeginOrigin, Float aBeginRadius,
|
2012-11-15 01:30:54 -08:00
|
|
|
const Point &aEndOrigin, Float aEndRadius)
|
2014-04-25 19:34:07 -07:00
|
|
|
: CanvasGradient(aContext, Type::RADIAL)
|
2012-11-15 01:30:54 -08:00
|
|
|
, mCenter1(aBeginOrigin)
|
|
|
|
, mCenter2(aEndOrigin)
|
|
|
|
, mRadius1(aBeginRadius)
|
|
|
|
, mRadius2(aEndRadius)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Point mCenter1;
|
|
|
|
Point mCenter2;
|
|
|
|
Float mRadius1;
|
|
|
|
Float mRadius2;
|
|
|
|
};
|
|
|
|
|
|
|
|
class CanvasLinearGradient : public CanvasGradient
|
|
|
|
{
|
|
|
|
public:
|
2013-04-26 07:55:56 -07:00
|
|
|
CanvasLinearGradient(CanvasRenderingContext2D* aContext,
|
|
|
|
const Point &aBegin, const Point &aEnd)
|
2014-04-25 19:34:07 -07:00
|
|
|
: CanvasGradient(aContext, Type::LINEAR)
|
2012-11-15 01:30:54 -08:00
|
|
|
, mBegin(aBegin)
|
|
|
|
, mEnd(aEnd)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
friend class CanvasGeneralPattern;
|
|
|
|
|
|
|
|
// Beginning of linear gradient.
|
|
|
|
Point mBegin;
|
|
|
|
// End of linear gradient.
|
|
|
|
Point mEnd;
|
|
|
|
};
|
|
|
|
|
|
|
|
// This class is named 'GeneralCanvasPattern' instead of just
|
|
|
|
// 'GeneralPattern' to keep Windows PGO builds from confusing the
|
|
|
|
// GeneralPattern class in gfxContext.cpp with this one.
|
|
|
|
|
|
|
|
class CanvasGeneralPattern
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef CanvasRenderingContext2D::Style Style;
|
|
|
|
typedef CanvasRenderingContext2D::ContextState ContextState;
|
|
|
|
|
|
|
|
CanvasGeneralPattern() : mPattern(nullptr) {}
|
|
|
|
~CanvasGeneralPattern()
|
|
|
|
{
|
|
|
|
if (mPattern) {
|
|
|
|
mPattern->~Pattern();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Pattern& ForStyle(CanvasRenderingContext2D *aCtx,
|
|
|
|
Style aStyle,
|
|
|
|
DrawTarget *aRT)
|
|
|
|
{
|
|
|
|
// This should only be called once or the mPattern destructor will
|
|
|
|
// not be executed.
|
|
|
|
NS_ASSERTION(!mPattern, "ForStyle() should only be called once on CanvasGeneralPattern!");
|
|
|
|
|
|
|
|
const ContextState &state = aCtx->CurrentState();
|
|
|
|
|
|
|
|
if (state.StyleIsColor(aStyle)) {
|
|
|
|
mPattern = new (mColorPattern.addr()) ColorPattern(Color::FromABGR(state.colorStyles[aStyle]));
|
|
|
|
} else if (state.gradientStyles[aStyle] &&
|
2014-04-25 19:34:07 -07:00
|
|
|
state.gradientStyles[aStyle]->GetType() == CanvasGradient::Type::LINEAR) {
|
2012-11-15 01:30:54 -08:00
|
|
|
CanvasLinearGradient *gradient =
|
|
|
|
static_cast<CanvasLinearGradient*>(state.gradientStyles[aStyle].get());
|
|
|
|
|
|
|
|
mPattern = new (mLinearGradientPattern.addr())
|
|
|
|
LinearGradientPattern(gradient->mBegin, gradient->mEnd,
|
|
|
|
gradient->GetGradientStopsForTarget(aRT));
|
|
|
|
} else if (state.gradientStyles[aStyle] &&
|
2014-04-25 19:34:07 -07:00
|
|
|
state.gradientStyles[aStyle]->GetType() == CanvasGradient::Type::RADIAL) {
|
2012-11-15 01:30:54 -08:00
|
|
|
CanvasRadialGradient *gradient =
|
|
|
|
static_cast<CanvasRadialGradient*>(state.gradientStyles[aStyle].get());
|
|
|
|
|
|
|
|
mPattern = new (mRadialGradientPattern.addr())
|
|
|
|
RadialGradientPattern(gradient->mCenter1, gradient->mCenter2, gradient->mRadius1,
|
|
|
|
gradient->mRadius2, gradient->GetGradientStopsForTarget(aRT));
|
|
|
|
} else if (state.patternStyles[aStyle]) {
|
|
|
|
if (aCtx->mCanvasElement) {
|
|
|
|
CanvasUtils::DoDrawImageSecurityCheck(aCtx->mCanvasElement,
|
|
|
|
state.patternStyles[aStyle]->mPrincipal,
|
|
|
|
state.patternStyles[aStyle]->mForceWriteOnly,
|
|
|
|
state.patternStyles[aStyle]->mCORSUsed);
|
|
|
|
}
|
|
|
|
|
|
|
|
ExtendMode mode;
|
2014-04-25 19:34:07 -07:00
|
|
|
if (state.patternStyles[aStyle]->mRepeat == CanvasPattern::RepeatMode::NOREPEAT) {
|
2014-01-10 11:06:17 -08:00
|
|
|
mode = ExtendMode::CLAMP;
|
2012-11-15 01:30:54 -08:00
|
|
|
} else {
|
2014-01-10 11:06:17 -08:00
|
|
|
mode = ExtendMode::REPEAT;
|
2012-11-15 01:30:54 -08:00
|
|
|
}
|
|
|
|
mPattern = new (mSurfacePattern.addr())
|
2014-06-12 07:54:43 -07:00
|
|
|
SurfacePattern(state.patternStyles[aStyle]->mSurface, mode,
|
|
|
|
state.patternStyles[aStyle]->mTransform);
|
2012-11-15 01:30:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return *mPattern;
|
|
|
|
}
|
|
|
|
|
|
|
|
union {
|
|
|
|
AlignedStorage2<ColorPattern> mColorPattern;
|
|
|
|
AlignedStorage2<LinearGradientPattern> mLinearGradientPattern;
|
|
|
|
AlignedStorage2<RadialGradientPattern> mRadialGradientPattern;
|
|
|
|
AlignedStorage2<SurfacePattern> mSurfacePattern;
|
|
|
|
};
|
|
|
|
Pattern *mPattern;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* This is an RAII based class that can be used as a drawtarget for
|
|
|
|
* operations that need a shadow drawn. It will automatically provide a
|
|
|
|
* temporary target when needed, and if so blend it back with a shadow.
|
|
|
|
*
|
|
|
|
* aBounds specifies the bounds of the drawing operation that will be
|
|
|
|
* drawn to the target, it is given in device space! This function will
|
2013-04-02 18:14:24 -07:00
|
|
|
* change aBounds to incorporate shadow bounds. If this is nullptr the drawing
|
2012-11-15 01:30:54 -08:00
|
|
|
* operation will be assumed to cover an infinite rect.
|
|
|
|
*/
|
|
|
|
class AdjustedTarget
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef CanvasRenderingContext2D::ContextState ContextState;
|
|
|
|
|
2014-09-01 15:26:43 -07:00
|
|
|
explicit AdjustedTarget(CanvasRenderingContext2D* ctx,
|
2012-11-15 01:30:54 -08:00
|
|
|
mgfx::Rect *aBounds = nullptr)
|
|
|
|
: mCtx(nullptr)
|
|
|
|
{
|
|
|
|
if (!ctx->NeedToDrawShadow()) {
|
|
|
|
mTarget = ctx->mTarget;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mCtx = ctx;
|
|
|
|
|
|
|
|
const ContextState &state = mCtx->CurrentState();
|
|
|
|
|
|
|
|
mSigma = state.shadowBlur / 2.0f;
|
|
|
|
|
|
|
|
if (mSigma > SIGMA_MAX) {
|
|
|
|
mSigma = SIGMA_MAX;
|
|
|
|
}
|
|
|
|
|
|
|
|
Matrix transform = mCtx->mTarget->GetTransform();
|
|
|
|
|
|
|
|
mTempRect = mgfx::Rect(0, 0, ctx->mWidth, ctx->mHeight);
|
|
|
|
|
|
|
|
static const gfxFloat GAUSSIAN_SCALE_FACTOR = (3 * sqrt(2 * M_PI) / 4) * 1.5;
|
|
|
|
int32_t blurRadius = (int32_t) floor(mSigma * GAUSSIAN_SCALE_FACTOR + 0.5);
|
|
|
|
|
|
|
|
// We need to enlarge and possibly offset our temporary surface
|
|
|
|
// so that things outside of the canvas may cast shadows.
|
2013-03-06 00:05:55 -08:00
|
|
|
mTempRect.Inflate(Margin(blurRadius + std::max<Float>(state.shadowOffset.y, 0),
|
2013-01-15 04:22:03 -08:00
|
|
|
blurRadius + std::max<Float>(-state.shadowOffset.x, 0),
|
2013-03-06 00:05:55 -08:00
|
|
|
blurRadius + std::max<Float>(-state.shadowOffset.y, 0),
|
|
|
|
blurRadius + std::max<Float>(state.shadowOffset.x, 0)));
|
2012-11-15 01:30:54 -08:00
|
|
|
|
|
|
|
if (aBounds) {
|
|
|
|
// We actually include the bounds of the shadow blur, this makes it
|
|
|
|
// easier to execute the actual blur on hardware, and shouldn't affect
|
|
|
|
// the amount of pixels that need to be touched.
|
|
|
|
aBounds->Inflate(Margin(blurRadius, blurRadius,
|
|
|
|
blurRadius, blurRadius));
|
|
|
|
mTempRect = mTempRect.Intersect(*aBounds);
|
|
|
|
}
|
|
|
|
|
|
|
|
mTempRect.ScaleRoundOut(1.0f);
|
|
|
|
|
|
|
|
transform._31 -= mTempRect.x;
|
|
|
|
transform._32 -= mTempRect.y;
|
|
|
|
|
|
|
|
mTarget =
|
|
|
|
mCtx->mTarget->CreateShadowDrawTarget(IntSize(int32_t(mTempRect.width), int32_t(mTempRect.height)),
|
2014-01-10 11:06:16 -08:00
|
|
|
SurfaceFormat::B8G8R8A8, mSigma);
|
2012-11-15 01:30:54 -08:00
|
|
|
|
|
|
|
if (!mTarget) {
|
|
|
|
// XXX - Deal with the situation where our temp size is too big to
|
|
|
|
// fit in a texture.
|
|
|
|
mTarget = ctx->mTarget;
|
|
|
|
mCtx = nullptr;
|
|
|
|
} else {
|
|
|
|
mTarget->SetTransform(transform);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
~AdjustedTarget()
|
|
|
|
{
|
|
|
|
if (!mCtx) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<SourceSurface> snapshot = mTarget->Snapshot();
|
|
|
|
|
|
|
|
mCtx->mTarget->DrawSurfaceWithShadow(snapshot, mTempRect.TopLeft(),
|
|
|
|
Color::FromABGR(mCtx->CurrentState().shadowColor),
|
|
|
|
mCtx->CurrentState().shadowOffset, mSigma,
|
|
|
|
mCtx->CurrentState().op);
|
|
|
|
}
|
|
|
|
|
2014-07-11 13:45:22 -07:00
|
|
|
operator DrawTarget*()
|
2014-01-12 16:29:48 -08:00
|
|
|
{
|
|
|
|
return mTarget;
|
|
|
|
}
|
|
|
|
|
2012-11-15 01:30:54 -08:00
|
|
|
DrawTarget* operator->()
|
|
|
|
{
|
|
|
|
return mTarget;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
RefPtr<DrawTarget> mTarget;
|
|
|
|
CanvasRenderingContext2D *mCtx;
|
|
|
|
Float mSigma;
|
|
|
|
mgfx::Rect mTempRect;
|
|
|
|
};
|
|
|
|
|
2014-06-12 07:54:43 -07:00
|
|
|
void
|
|
|
|
CanvasPattern::SetTransform(SVGMatrix& aMatrix)
|
|
|
|
{
|
|
|
|
mTransform = ToMatrix(aMatrix.GetMatrix());
|
|
|
|
}
|
|
|
|
|
2013-04-26 07:55:54 -07:00
|
|
|
void
|
|
|
|
CanvasGradient::AddColorStop(float offset, const nsAString& colorstr, ErrorResult& rv)
|
2012-11-15 01:30:54 -08:00
|
|
|
{
|
2013-04-26 07:55:54 -07:00
|
|
|
if (offset < 0.0 || offset > 1.0) {
|
|
|
|
rv.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
|
|
|
|
return;
|
2012-11-15 01:30:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
nsCSSValue value;
|
|
|
|
nsCSSParser parser;
|
|
|
|
if (!parser.ParseColorString(colorstr, nullptr, 0, value)) {
|
2013-04-26 07:55:54 -07:00
|
|
|
rv.Throw(NS_ERROR_DOM_SYNTAX_ERR);
|
|
|
|
return;
|
2012-11-15 01:30:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
nscolor color;
|
|
|
|
if (!nsRuleNode::ComputeColor(value, nullptr, nullptr, color)) {
|
2013-04-26 07:55:54 -07:00
|
|
|
rv.Throw(NS_ERROR_DOM_SYNTAX_ERR);
|
|
|
|
return;
|
2012-11-15 01:30:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
mStops = nullptr;
|
|
|
|
|
|
|
|
GradientStop newStop;
|
|
|
|
|
|
|
|
newStop.offset = offset;
|
|
|
|
newStop.color = Color::FromABGR(color);
|
|
|
|
|
|
|
|
mRawStops.AppendElement(newStop);
|
|
|
|
}
|
|
|
|
|
2013-07-26 11:25:56 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_ROOT_NATIVE(CanvasGradient, AddRef)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNROOT_NATIVE(CanvasGradient, Release)
|
2012-11-15 01:30:54 -08:00
|
|
|
|
2014-04-29 01:57:00 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(CanvasGradient, mContext)
|
2013-04-26 07:55:56 -07:00
|
|
|
|
2013-07-26 11:25:56 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_ROOT_NATIVE(CanvasPattern, AddRef)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNROOT_NATIVE(CanvasPattern, Release)
|
2012-11-15 01:30:54 -08:00
|
|
|
|
2014-04-29 01:57:00 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(CanvasPattern, mContext)
|
2013-04-26 07:55:37 -07:00
|
|
|
|
2012-11-15 01:30:54 -08:00
|
|
|
class CanvasRenderingContext2DUserData : public LayerUserData {
|
|
|
|
public:
|
2014-09-01 15:26:43 -07:00
|
|
|
explicit CanvasRenderingContext2DUserData(CanvasRenderingContext2D *aContext)
|
2012-11-15 01:30:54 -08:00
|
|
|
: mContext(aContext)
|
|
|
|
{
|
|
|
|
aContext->mUserDatas.AppendElement(this);
|
|
|
|
}
|
|
|
|
~CanvasRenderingContext2DUserData()
|
|
|
|
{
|
|
|
|
if (mContext) {
|
|
|
|
mContext->mUserDatas.RemoveElement(this);
|
|
|
|
}
|
|
|
|
}
|
2012-11-30 15:58:00 -08:00
|
|
|
|
|
|
|
static void PreTransactionCallback(void* aData)
|
|
|
|
{
|
|
|
|
CanvasRenderingContext2DUserData* self =
|
|
|
|
static_cast<CanvasRenderingContext2DUserData*>(aData);
|
|
|
|
CanvasRenderingContext2D* context = self->mContext;
|
2014-03-05 13:49:37 -08:00
|
|
|
if (!context || !context->mStream || !context->mTarget)
|
2014-02-14 15:48:05 -08:00
|
|
|
return;
|
|
|
|
|
2014-03-05 13:49:37 -08:00
|
|
|
// Since SkiaGL default to store drawing command until flush
|
|
|
|
// We will have to flush it before present.
|
|
|
|
context->mTarget->Flush();
|
2012-11-30 15:58:00 -08:00
|
|
|
}
|
|
|
|
|
2012-11-15 01:30:54 -08:00
|
|
|
static void DidTransactionCallback(void* aData)
|
|
|
|
{
|
|
|
|
CanvasRenderingContext2DUserData* self =
|
|
|
|
static_cast<CanvasRenderingContext2DUserData*>(aData);
|
|
|
|
if (self->mContext) {
|
|
|
|
self->mContext->MarkContextClean();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bool IsForContext(CanvasRenderingContext2D *aContext)
|
|
|
|
{
|
|
|
|
return mContext == aContext;
|
|
|
|
}
|
|
|
|
void Forget()
|
|
|
|
{
|
|
|
|
mContext = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
CanvasRenderingContext2D *mContext;
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(CanvasRenderingContext2D)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(CanvasRenderingContext2D)
|
|
|
|
|
2013-08-01 18:29:05 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(CanvasRenderingContext2D)
|
|
|
|
|
2013-04-26 07:55:37 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(CanvasRenderingContext2D)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mCanvasElement)
|
|
|
|
for (uint32_t i = 0; i < tmp->mStyleStack.Length(); i++) {
|
2014-04-25 19:34:07 -07:00
|
|
|
ImplCycleCollectionUnlink(tmp->mStyleStack[i].patternStyles[Style::STROKE]);
|
|
|
|
ImplCycleCollectionUnlink(tmp->mStyleStack[i].patternStyles[Style::FILL]);
|
|
|
|
ImplCycleCollectionUnlink(tmp->mStyleStack[i].gradientStyles[Style::STROKE]);
|
|
|
|
ImplCycleCollectionUnlink(tmp->mStyleStack[i].gradientStyles[Style::FILL]);
|
2013-04-26 07:55:37 -07:00
|
|
|
}
|
2014-05-19 21:54:00 -07:00
|
|
|
for (size_t x = 0 ; x < tmp->mHitRegionsOptions.Length(); x++) {
|
|
|
|
RegionInfo& info = tmp->mHitRegionsOptions[x];
|
|
|
|
if (info.mElement) {
|
|
|
|
ImplCycleCollectionUnlink(info.mElement);
|
|
|
|
}
|
|
|
|
}
|
2013-04-26 07:55:37 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(CanvasRenderingContext2D)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mCanvasElement)
|
|
|
|
for (uint32_t i = 0; i < tmp->mStyleStack.Length(); i++) {
|
2014-04-25 19:34:07 -07:00
|
|
|
ImplCycleCollectionTraverse(cb, tmp->mStyleStack[i].patternStyles[Style::STROKE], "Stroke CanvasPattern");
|
|
|
|
ImplCycleCollectionTraverse(cb, tmp->mStyleStack[i].patternStyles[Style::FILL], "Fill CanvasPattern");
|
|
|
|
ImplCycleCollectionTraverse(cb, tmp->mStyleStack[i].gradientStyles[Style::STROKE], "Stroke CanvasGradient");
|
|
|
|
ImplCycleCollectionTraverse(cb, tmp->mStyleStack[i].gradientStyles[Style::FILL], "Fill CanvasGradient");
|
2013-04-26 07:55:37 -07:00
|
|
|
}
|
2014-05-19 21:54:00 -07:00
|
|
|
for (size_t x = 0 ; x < tmp->mHitRegionsOptions.Length(); x++) {
|
|
|
|
RegionInfo& info = tmp->mHitRegionsOptions[x];
|
|
|
|
if (info.mElement) {
|
|
|
|
ImplCycleCollectionTraverse(cb, info.mElement, "Hit region fallback element");
|
|
|
|
}
|
|
|
|
}
|
2013-04-26 07:55:37 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_SCRIPT_OBJECTS
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_WRAPPERCACHE(CanvasRenderingContext2D)
|
2012-11-15 01:30:54 -08:00
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_BEGIN(CanvasRenderingContext2D)
|
|
|
|
if (nsCCUncollectableMarker::sGeneration && tmp->IsBlack()) {
|
|
|
|
dom::Element* canvasElement = tmp->mCanvasElement;
|
|
|
|
if (canvasElement) {
|
|
|
|
if (canvasElement->IsPurple()) {
|
|
|
|
canvasElement->RemovePurple();
|
|
|
|
}
|
|
|
|
dom::Element::MarkNodeChildren(canvasElement);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_IN_CC_BEGIN(CanvasRenderingContext2D)
|
|
|
|
return nsCCUncollectableMarker::sGeneration && tmp->IsBlack();
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_IN_CC_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_THIS_BEGIN(CanvasRenderingContext2D)
|
|
|
|
return nsCCUncollectableMarker::sGeneration && tmp->IsBlack();
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_THIS_END
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(CanvasRenderingContext2D)
|
|
|
|
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsICanvasRenderingContextInternal)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
|
|
|
/**
|
|
|
|
** CanvasRenderingContext2D impl
|
|
|
|
**/
|
|
|
|
|
|
|
|
|
|
|
|
// Initialize our static variables.
|
|
|
|
uint32_t CanvasRenderingContext2D::sNumLivingContexts = 0;
|
|
|
|
DrawTarget* CanvasRenderingContext2D::sErrorTarget = nullptr;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
CanvasRenderingContext2D::CanvasRenderingContext2D()
|
2014-09-02 16:26:36 -07:00
|
|
|
: mRenderingMode(RenderingMode::OpenGLBackendMode)
|
2014-07-01 08:20:07 -07:00
|
|
|
// these are the default values from the Canvas spec
|
2014-07-25 14:16:51 -07:00
|
|
|
, mWidth(0), mHeight(0)
|
2014-07-01 08:20:07 -07:00
|
|
|
, mZero(false), mOpaque(false)
|
|
|
|
, mResetLayer(true)
|
2012-11-15 01:30:54 -08:00
|
|
|
, mIPC(false)
|
2014-03-05 13:49:37 -08:00
|
|
|
, mStream(nullptr)
|
2012-11-15 01:30:54 -08:00
|
|
|
, mIsEntireFrameInvalid(false)
|
|
|
|
, mPredictManyRedrawCalls(false), mPathTransformWillUpdate(false)
|
|
|
|
, mInvalidateCount(0)
|
|
|
|
{
|
|
|
|
sNumLivingContexts++;
|
|
|
|
SetIsDOMBinding();
|
2014-09-02 16:26:36 -07:00
|
|
|
|
|
|
|
// The default is to use OpenGL mode
|
|
|
|
if (!gfxPlatform::GetPlatform()->UseAcceleratedSkiaCanvas()) {
|
|
|
|
mRenderingMode = RenderingMode::SoftwareBackendMode;
|
|
|
|
}
|
|
|
|
|
2012-11-15 01:30:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
CanvasRenderingContext2D::~CanvasRenderingContext2D()
|
|
|
|
{
|
2014-09-09 18:40:51 -07:00
|
|
|
RemovePostRefreshObserver();
|
2012-11-15 01:30:54 -08:00
|
|
|
Reset();
|
|
|
|
// Drop references from all CanvasRenderingContext2DUserData to this context
|
|
|
|
for (uint32_t i = 0; i < mUserDatas.Length(); ++i) {
|
|
|
|
mUserDatas[i]->Forget();
|
|
|
|
}
|
|
|
|
sNumLivingContexts--;
|
|
|
|
if (!sNumLivingContexts) {
|
|
|
|
NS_IF_RELEASE(sErrorTarget);
|
|
|
|
}
|
2013-06-28 19:58:40 -07:00
|
|
|
|
2013-08-23 06:52:32 -07:00
|
|
|
RemoveDemotableContext(this);
|
2012-11-15 01:30:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
JSObject*
|
2014-04-08 15:27:18 -07:00
|
|
|
CanvasRenderingContext2D::WrapObject(JSContext *cx)
|
2012-11-15 01:30:54 -08:00
|
|
|
{
|
Bug 991742 part 6. Remove the "aScope" argument of binding Wrap() methods. r=bholley
This patch was mostly generated with this command:
find . -name "*.h" -o -name "*.cpp" | xargs sed -e 's/Binding::Wrap(aCx, aScope, this/Binding::Wrap(aCx, this/' -e 's/Binding_workers::Wrap(aCx, aScope, this/Binding_workers::Wrap(aCx, this/' -e 's/Binding::Wrap(cx, scope, this/Binding::Wrap(cx, this/' -i ""
plus a few manual fixes to dom/bindings/Codegen.py, js/xpconnect/src/event_impl_gen.py, and a few C++ files that were not caught in the search-and-replace above.
2014-04-08 15:27:17 -07:00
|
|
|
return CanvasRenderingContext2DBinding::Wrap(cx, this);
|
2012-11-15 01:30:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
CanvasRenderingContext2D::ParseColor(const nsAString& aString,
|
|
|
|
nscolor* aColor)
|
|
|
|
{
|
|
|
|
nsIDocument* document = mCanvasElement
|
|
|
|
? mCanvasElement->OwnerDoc()
|
|
|
|
: nullptr;
|
|
|
|
|
|
|
|
// Pass the CSS Loader object to the parser, to allow parser error
|
|
|
|
// reports to include the outer window ID.
|
|
|
|
nsCSSParser parser(document ? document->CSSLoader() : nullptr);
|
|
|
|
nsCSSValue value;
|
|
|
|
if (!parser.ParseColorString(aString, nullptr, 0, value)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-09-15 16:35:48 -07:00
|
|
|
if (value.IsNumericColorUnit()) {
|
2012-11-15 01:30:54 -08:00
|
|
|
// if we already have a color we can just use it directly
|
|
|
|
*aColor = value.GetColorValue();
|
|
|
|
} else {
|
|
|
|
// otherwise resolve it
|
|
|
|
nsIPresShell* presShell = GetPresShell();
|
|
|
|
nsRefPtr<nsStyleContext> parentContext;
|
|
|
|
if (mCanvasElement && mCanvasElement->IsInDoc()) {
|
|
|
|
// Inherit from the canvas element.
|
|
|
|
parentContext = nsComputedDOMStyle::GetStyleContextForElement(
|
|
|
|
mCanvasElement, nullptr, presShell);
|
|
|
|
}
|
|
|
|
|
|
|
|
unused << nsRuleNode::ComputeColor(
|
|
|
|
value, presShell ? presShell->GetPresContext() : nullptr, parentContext,
|
|
|
|
*aColor);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
CanvasRenderingContext2D::Reset()
|
|
|
|
{
|
|
|
|
if (mCanvasElement) {
|
|
|
|
mCanvasElement->InvalidateCanvas();
|
|
|
|
}
|
|
|
|
|
|
|
|
// only do this for non-docshell created contexts,
|
|
|
|
// since those are the ones that we created a surface for
|
|
|
|
if (mTarget && IsTargetValid() && !mDocShell) {
|
|
|
|
gCanvasAzureMemoryUsed -= mWidth * mHeight * 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
mTarget = nullptr;
|
2014-03-05 13:49:37 -08:00
|
|
|
mStream = nullptr;
|
2012-11-15 01:30:54 -08:00
|
|
|
|
2014-02-25 05:53:42 -08:00
|
|
|
// reset hit regions
|
2014-05-18 16:43:00 -07:00
|
|
|
mHitRegionsOptions.ClearAndRetainStorage();
|
2014-02-25 05:53:42 -08:00
|
|
|
|
2012-11-15 01:30:54 -08:00
|
|
|
// Since the target changes the backing texture will change, and this will
|
|
|
|
// no longer be valid.
|
|
|
|
mIsEntireFrameInvalid = false;
|
|
|
|
mPredictManyRedrawCalls = false;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CanvasRenderingContext2D::SetStyleFromString(const nsAString& str,
|
|
|
|
Style whichStyle)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!str.IsVoid());
|
|
|
|
|
|
|
|
nscolor color;
|
|
|
|
if (!ParseColor(str, &color)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
CurrentState().SetColorStyle(whichStyle, color);
|
|
|
|
}
|
|
|
|
|
2013-07-26 11:25:54 -07:00
|
|
|
void
|
2013-09-17 08:16:02 -07:00
|
|
|
CanvasRenderingContext2D::GetStyleAsUnion(OwningStringOrCanvasGradientOrCanvasPattern& aValue,
|
2013-07-26 11:25:54 -07:00
|
|
|
Style aWhichStyle)
|
2012-11-15 01:30:54 -08:00
|
|
|
{
|
|
|
|
const ContextState &state = CurrentState();
|
|
|
|
if (state.patternStyles[aWhichStyle]) {
|
2013-07-26 11:25:54 -07:00
|
|
|
aValue.SetAsCanvasPattern() = state.patternStyles[aWhichStyle];
|
2012-11-15 01:30:54 -08:00
|
|
|
} else if (state.gradientStyles[aWhichStyle]) {
|
2013-07-26 11:25:54 -07:00
|
|
|
aValue.SetAsCanvasGradient() = state.gradientStyles[aWhichStyle];
|
2012-11-15 01:30:54 -08:00
|
|
|
} else {
|
2013-07-26 11:25:54 -07:00
|
|
|
StyleColorToString(state.colorStyles[aWhichStyle], aValue.SetAsString());
|
2012-11-15 01:30:54 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
void
|
|
|
|
CanvasRenderingContext2D::StyleColorToString(const nscolor& aColor, nsAString& aStr)
|
|
|
|
{
|
|
|
|
// We can't reuse the normal CSS color stringification code,
|
|
|
|
// because the spec calls for a different algorithm for canvas.
|
|
|
|
if (NS_GET_A(aColor) == 255) {
|
|
|
|
CopyUTF8toUTF16(nsPrintfCString("#%02x%02x%02x",
|
|
|
|
NS_GET_R(aColor),
|
|
|
|
NS_GET_G(aColor),
|
|
|
|
NS_GET_B(aColor)),
|
|
|
|
aStr);
|
|
|
|
} else {
|
|
|
|
CopyUTF8toUTF16(nsPrintfCString("rgba(%d, %d, %d, ",
|
|
|
|
NS_GET_R(aColor),
|
|
|
|
NS_GET_G(aColor),
|
|
|
|
NS_GET_B(aColor)),
|
|
|
|
aStr);
|
|
|
|
aStr.AppendFloat(nsStyleUtil::ColorComponentToFloat(NS_GET_A(aColor)));
|
|
|
|
aStr.Append(')');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
CanvasRenderingContext2D::Redraw()
|
|
|
|
{
|
|
|
|
if (mIsEntireFrameInvalid) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
mIsEntireFrameInvalid = true;
|
|
|
|
|
|
|
|
if (!mCanvasElement) {
|
|
|
|
NS_ASSERTION(mDocShell, "Redraw with no canvas element or docshell!");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsSVGEffects::InvalidateDirectRenderingObservers(mCanvasElement);
|
|
|
|
|
|
|
|
mCanvasElement->InvalidateCanvasContent(nullptr);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CanvasRenderingContext2D::Redraw(const mgfx::Rect &r)
|
|
|
|
{
|
|
|
|
++mInvalidateCount;
|
|
|
|
|
|
|
|
if (mIsEntireFrameInvalid) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mPredictManyRedrawCalls ||
|
|
|
|
mInvalidateCount > kCanvasMaxInvalidateCount) {
|
|
|
|
Redraw();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mCanvasElement) {
|
|
|
|
NS_ASSERTION(mDocShell, "Redraw with no canvas element or docshell!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsSVGEffects::InvalidateDirectRenderingObservers(mCanvasElement);
|
|
|
|
|
|
|
|
mCanvasElement->InvalidateCanvasContent(&r);
|
|
|
|
}
|
|
|
|
|
2014-09-09 18:40:51 -07:00
|
|
|
void
|
|
|
|
CanvasRenderingContext2D::DidRefresh()
|
|
|
|
{
|
|
|
|
if (mStream && mStream->GLContext()) {
|
|
|
|
mStream->GLContext()->FlushIfHeavyGLCallsSinceLastFlush();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-15 01:30:54 -08:00
|
|
|
void
|
|
|
|
CanvasRenderingContext2D::RedrawUser(const gfxRect& r)
|
|
|
|
{
|
|
|
|
if (mIsEntireFrameInvalid) {
|
|
|
|
++mInvalidateCount;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mgfx::Rect newr =
|
|
|
|
mTarget->GetTransform().TransformBounds(ToRect(r));
|
|
|
|
Redraw(newr);
|
|
|
|
}
|
|
|
|
|
2014-09-02 16:26:36 -07:00
|
|
|
bool CanvasRenderingContext2D::SwitchRenderingMode(RenderingMode aRenderingMode)
|
2013-06-28 19:58:40 -07:00
|
|
|
{
|
2014-09-02 16:26:36 -07:00
|
|
|
if (!IsTargetValid() || mRenderingMode == aRenderingMode) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-08-23 06:52:32 -07:00
|
|
|
|
2013-06-28 19:58:40 -07:00
|
|
|
RefPtr<SourceSurface> snapshot = mTarget->Snapshot();
|
|
|
|
RefPtr<DrawTarget> oldTarget = mTarget;
|
|
|
|
mTarget = nullptr;
|
2014-03-05 13:49:37 -08:00
|
|
|
mStream = nullptr;
|
2013-08-07 07:07:17 -07:00
|
|
|
mResetLayer = true;
|
2013-06-28 19:58:40 -07:00
|
|
|
|
2014-09-02 16:26:36 -07:00
|
|
|
// Recreate target using the new rendering mode
|
|
|
|
RenderingMode attemptedMode = EnsureTarget(aRenderingMode);
|
2013-06-28 19:58:40 -07:00
|
|
|
if (!IsTargetValid())
|
2014-09-02 16:26:36 -07:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// We succeeded, so update mRenderingMode to reflect reality
|
|
|
|
mRenderingMode = attemptedMode;
|
2013-06-28 19:58:40 -07:00
|
|
|
|
2013-08-23 06:52:32 -07:00
|
|
|
// Restore the content from the old DrawTarget
|
2013-06-28 19:58:40 -07:00
|
|
|
mgfx::Rect r(0, 0, mWidth, mHeight);
|
|
|
|
mTarget->DrawSurface(snapshot, r, r);
|
2013-08-23 06:52:32 -07:00
|
|
|
|
|
|
|
// Restore the clips and transform
|
|
|
|
for (uint32_t i = 0; i < CurrentState().clipsPushed.size(); i++) {
|
|
|
|
mTarget->PushClip(CurrentState().clipsPushed[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
mTarget->SetTransform(oldTarget->GetTransform());
|
2014-09-02 16:26:36 -07:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CanvasRenderingContext2D::Demote()
|
|
|
|
{
|
|
|
|
if (SwitchRenderingMode(RenderingMode::SoftwareBackendMode)) {
|
|
|
|
RemoveDemotableContext(this);
|
|
|
|
}
|
2013-06-28 19:58:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<CanvasRenderingContext2D*>&
|
|
|
|
CanvasRenderingContext2D::DemotableContexts()
|
|
|
|
{
|
|
|
|
static std::vector<CanvasRenderingContext2D*> contexts;
|
|
|
|
return contexts;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CanvasRenderingContext2D::DemoteOldestContextIfNecessary()
|
|
|
|
{
|
2014-03-05 13:49:37 -08:00
|
|
|
const size_t kMaxContexts = 64;
|
2013-06-28 19:58:40 -07:00
|
|
|
|
|
|
|
std::vector<CanvasRenderingContext2D*>& contexts = DemotableContexts();
|
|
|
|
if (contexts.size() < kMaxContexts)
|
|
|
|
return;
|
|
|
|
|
|
|
|
CanvasRenderingContext2D* oldest = contexts.front();
|
2014-09-02 16:26:36 -07:00
|
|
|
if (oldest->SwitchRenderingMode(RenderingMode::SoftwareBackendMode)) {
|
|
|
|
RemoveDemotableContext(oldest);
|
|
|
|
}
|
2013-06-28 19:58:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CanvasRenderingContext2D::AddDemotableContext(CanvasRenderingContext2D* context)
|
|
|
|
{
|
2013-08-09 14:19:21 -07:00
|
|
|
std::vector<CanvasRenderingContext2D*>::iterator iter = std::find(DemotableContexts().begin(), DemotableContexts().end(), context);
|
|
|
|
if (iter != DemotableContexts().end())
|
|
|
|
return;
|
|
|
|
|
2013-06-28 19:58:40 -07:00
|
|
|
DemotableContexts().push_back(context);
|
|
|
|
}
|
|
|
|
|
2013-08-23 06:52:32 -07:00
|
|
|
void
|
|
|
|
CanvasRenderingContext2D::RemoveDemotableContext(CanvasRenderingContext2D* context)
|
|
|
|
{
|
|
|
|
std::vector<CanvasRenderingContext2D*>::iterator iter = std::find(DemotableContexts().begin(), DemotableContexts().end(), context);
|
|
|
|
if (iter != DemotableContexts().end())
|
|
|
|
DemotableContexts().erase(iter);
|
|
|
|
}
|
|
|
|
|
2013-08-05 13:46:31 -07:00
|
|
|
bool
|
2014-04-29 19:19:48 -07:00
|
|
|
CanvasRenderingContext2D::CheckSizeForSkiaGL(IntSize size) {
|
2014-03-27 20:48:22 -07:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2013-09-06 05:02:04 -07:00
|
|
|
int minsize = Preferences::GetInt("gfx.canvas.min-size-for-skia-gl", 128);
|
2014-03-27 20:48:22 -07:00
|
|
|
if (size.width < minsize || size.height < minsize) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Maximum pref allows 3 different options:
|
|
|
|
// 0 means unlimited size
|
|
|
|
// > 0 means use value as an absolute threshold
|
|
|
|
// < 0 means use the number of screen pixels as a threshold
|
|
|
|
int maxsize = Preferences::GetInt("gfx.canvas.max-size-for-skia-gl", 0);
|
|
|
|
|
|
|
|
// unlimited max size
|
|
|
|
if (!maxsize) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// absolute max size threshold
|
|
|
|
if (maxsize > 0) {
|
|
|
|
return size.width <= maxsize && size.height <= maxsize;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cache the number of pixels on the primary screen
|
|
|
|
static int32_t gScreenPixels = -1;
|
|
|
|
if (gScreenPixels < 0) {
|
2014-04-29 19:19:48 -07:00
|
|
|
// Default to historical mobile screen size of 980x480. In addition,
|
|
|
|
// allow skia use up to this size even if the screen is smaller. A lot
|
|
|
|
// content expects this size to work well.
|
|
|
|
gScreenPixels = 980 * 480;
|
|
|
|
|
2014-03-27 20:48:22 -07:00
|
|
|
nsCOMPtr<nsIScreenManager> screenManager =
|
|
|
|
do_GetService("@mozilla.org/gfx/screenmanager;1");
|
|
|
|
if (screenManager) {
|
|
|
|
nsCOMPtr<nsIScreen> primaryScreen;
|
|
|
|
screenManager->GetPrimaryScreen(getter_AddRefs(primaryScreen));
|
|
|
|
if (primaryScreen) {
|
|
|
|
int32_t x, y, width, height;
|
|
|
|
primaryScreen->GetRect(&x, &y, &width, &height);
|
|
|
|
|
2014-04-29 19:19:48 -07:00
|
|
|
gScreenPixels = std::max(gScreenPixels, width * height);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// On high DPI devices the screen pixels may be scaled up. Make
|
|
|
|
// sure to apply that scaling here as well if we are hooked up
|
|
|
|
// to a widget.
|
|
|
|
static double gDefaultScale = 0.0;
|
|
|
|
if (gDefaultScale < 1.0) {
|
|
|
|
nsIPresShell* ps = GetPresShell();
|
|
|
|
if (ps) {
|
|
|
|
nsIFrame* frame = ps->GetRootFrame();
|
|
|
|
if (frame) {
|
|
|
|
nsIWidget* widget = frame->GetNearestWidget();
|
|
|
|
if (widget) {
|
|
|
|
gDefaultScale = widget->GetDefaultScale().scale;
|
|
|
|
}
|
2014-03-27 20:48:22 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-01 07:38:07 -07:00
|
|
|
double scale = gDefaultScale > 0 ? gDefaultScale : 1.0;
|
|
|
|
int32_t threshold = ceil(scale * scale * gScreenPixels);
|
2014-04-29 19:19:48 -07:00
|
|
|
|
2014-03-27 20:48:22 -07:00
|
|
|
// screen size acts as max threshold
|
2014-04-29 19:19:48 -07:00
|
|
|
return threshold < 0 || (size.width * size.height) <= threshold;
|
2013-08-05 13:46:31 -07:00
|
|
|
}
|
|
|
|
|
2014-09-02 16:26:36 -07:00
|
|
|
CanvasRenderingContext2D::RenderingMode
|
|
|
|
CanvasRenderingContext2D::EnsureTarget(RenderingMode aRenderingMode)
|
2012-11-15 01:30:54 -08:00
|
|
|
{
|
2014-09-02 16:26:36 -07:00
|
|
|
// This would make no sense, so make sure we don't get ourselves in a mess
|
|
|
|
MOZ_ASSERT(mRenderingMode != RenderingMode::DefaultBackendMode);
|
|
|
|
|
|
|
|
RenderingMode mode = (aRenderingMode == RenderingMode::DefaultBackendMode) ? mRenderingMode : aRenderingMode;
|
|
|
|
|
|
|
|
if (mTarget && mode == mRenderingMode) {
|
|
|
|
return mRenderingMode;
|
2012-11-15 01:30:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check that the dimensions are sane
|
|
|
|
IntSize size(mWidth, mHeight);
|
|
|
|
if (size.width <= 0xFFFF && size.height <= 0xFFFF &&
|
|
|
|
size.width >= 0 && size.height >= 0) {
|
|
|
|
SurfaceFormat format = GetSurfaceFormat();
|
|
|
|
nsIDocument* ownerDoc = nullptr;
|
|
|
|
if (mCanvasElement) {
|
|
|
|
ownerDoc = mCanvasElement->OwnerDoc();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<LayerManager> layerManager = nullptr;
|
|
|
|
|
|
|
|
if (ownerDoc) {
|
|
|
|
layerManager =
|
|
|
|
nsContentUtils::PersistentLayerManagerForDocument(ownerDoc);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (layerManager) {
|
2014-09-02 16:26:36 -07:00
|
|
|
if (mode == RenderingMode::OpenGLBackendMode && CheckSizeForSkiaGL(size)) {
|
2013-06-28 12:59:44 -07:00
|
|
|
DemoteOldestContextIfNecessary();
|
|
|
|
|
2014-03-05 13:49:37 -08:00
|
|
|
SkiaGLGlue* glue = gfxPlatform::GetPlatform()->GetSkiaGLGlue();
|
2013-06-28 12:59:44 -07:00
|
|
|
|
2014-04-15 12:07:42 -07:00
|
|
|
#if USE_SKIA
|
2014-03-24 07:13:55 -07:00
|
|
|
if (glue && glue->GetGrContext() && glue->GetGLContext()) {
|
2014-03-05 13:49:37 -08:00
|
|
|
mTarget = Factory::CreateDrawTargetSkiaWithGrContext(glue->GetGrContext(), size, format);
|
|
|
|
if (mTarget) {
|
2014-07-11 15:10:49 -07:00
|
|
|
mStream = gl::SurfaceStream::CreateForType(gl::SurfaceStreamType::TripleBuffer,
|
|
|
|
glue->GetGLContext());
|
2014-03-05 13:49:37 -08:00
|
|
|
AddDemotableContext(this);
|
|
|
|
} else {
|
|
|
|
printf_stderr("Failed to create a SkiaGL DrawTarget, falling back to software\n");
|
2014-09-02 16:26:36 -07:00
|
|
|
mode = RenderingMode::SoftwareBackendMode;
|
2014-03-05 13:49:37 -08:00
|
|
|
}
|
2014-02-14 15:48:05 -08:00
|
|
|
}
|
2014-04-15 12:07:42 -07:00
|
|
|
#endif
|
2014-03-05 13:49:37 -08:00
|
|
|
if (!mTarget) {
|
2013-06-28 12:59:44 -07:00
|
|
|
mTarget = layerManager->CreateDrawTarget(size, format);
|
|
|
|
}
|
2014-09-10 14:04:00 -07:00
|
|
|
} else {
|
2014-03-05 13:49:37 -08:00
|
|
|
mTarget = layerManager->CreateDrawTarget(size, format);
|
2014-09-02 16:26:36 -07:00
|
|
|
mode = RenderingMode::SoftwareBackendMode;
|
2014-09-10 14:04:00 -07:00
|
|
|
}
|
2012-11-15 01:30:54 -08:00
|
|
|
} else {
|
2014-03-05 13:49:37 -08:00
|
|
|
mTarget = gfxPlatform::GetPlatform()->CreateOffscreenCanvasDrawTarget(size, format);
|
2014-09-02 16:26:36 -07:00
|
|
|
mode = RenderingMode::SoftwareBackendMode;
|
2012-11-15 01:30:54 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mTarget) {
|
2013-01-14 16:26:47 -08:00
|
|
|
static bool registered = false;
|
|
|
|
if (!registered) {
|
|
|
|
registered = true;
|
2013-11-06 21:35:30 -08:00
|
|
|
RegisterStrongMemoryReporter(new Canvas2dPixelsReporter());
|
2012-11-15 01:30:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
gCanvasAzureMemoryUsed += mWidth * mHeight * 4;
|
|
|
|
JSContext* context = nsContentUtils::GetCurrentJSContext();
|
|
|
|
if (context) {
|
|
|
|
JS_updateMallocCounter(context, mWidth * mHeight * 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
mTarget->ClearRect(mgfx::Rect(Point(0, 0), Size(mWidth, mHeight)));
|
2014-07-10 11:12:26 -07:00
|
|
|
if (mTarget->GetBackendType() == mgfx::BackendType::CAIRO) {
|
|
|
|
// Cairo doesn't play well with huge clips. When given a very big clip it
|
|
|
|
// will try to allocate big mask surface without taking the target
|
|
|
|
// size into account which can cause OOM. See bug 1034593.
|
|
|
|
// This limits the clip extents to the size of the canvas.
|
|
|
|
// A fix in Cairo would probably be preferable, but requires somewhat
|
|
|
|
// invasive changes.
|
|
|
|
mTarget->PushClipRect(mgfx::Rect(Point(0, 0), Size(mWidth, mHeight)));
|
|
|
|
}
|
2012-11-15 01:30:54 -08:00
|
|
|
// Force a full layer transaction since we didn't have a layer before
|
|
|
|
// and now we might need one.
|
|
|
|
if (mCanvasElement) {
|
|
|
|
mCanvasElement->InvalidateCanvas();
|
|
|
|
}
|
|
|
|
// Calling Redraw() tells our invalidation machinery that the entire
|
|
|
|
// canvas is already invalid, which can speed up future drawing.
|
|
|
|
Redraw();
|
|
|
|
} else {
|
|
|
|
EnsureErrorTarget();
|
|
|
|
mTarget = sErrorTarget;
|
|
|
|
}
|
2014-09-02 16:26:36 -07:00
|
|
|
|
|
|
|
return mode;
|
2012-11-15 01:30:54 -08:00
|
|
|
}
|
|
|
|
|
2013-09-30 14:02:40 -07:00
|
|
|
#ifdef DEBUG
|
|
|
|
int32_t
|
|
|
|
CanvasRenderingContext2D::GetWidth() const
|
|
|
|
{
|
|
|
|
return mWidth;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t
|
|
|
|
CanvasRenderingContext2D::GetHeight() const
|
|
|
|
{
|
|
|
|
return mHeight;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-11-15 01:30:54 -08:00
|
|
|
NS_IMETHODIMP
|
|
|
|
CanvasRenderingContext2D::SetDimensions(int32_t width, int32_t height)
|
|
|
|
{
|
|
|
|
ClearTarget();
|
|
|
|
|
2013-10-09 14:22:11 -07:00
|
|
|
// Zero sized surfaces can cause problems.
|
|
|
|
mZero = false;
|
|
|
|
if (height == 0) {
|
|
|
|
height = 1;
|
|
|
|
mZero = true;
|
|
|
|
}
|
|
|
|
if (width == 0) {
|
|
|
|
width = 1;
|
2012-11-15 01:30:54 -08:00
|
|
|
mZero = true;
|
|
|
|
}
|
2013-10-09 14:22:11 -07:00
|
|
|
mWidth = width;
|
|
|
|
mHeight = height;
|
2012-11-15 01:30:54 -08:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CanvasRenderingContext2D::ClearTarget()
|
|
|
|
{
|
|
|
|
Reset();
|
|
|
|
|
|
|
|
mResetLayer = true;
|
|
|
|
|
|
|
|
// set up the initial canvas defaults
|
|
|
|
mStyleStack.Clear();
|
|
|
|
mPathBuilder = nullptr;
|
|
|
|
mPath = nullptr;
|
|
|
|
mDSPathBuilder = nullptr;
|
|
|
|
|
|
|
|
ContextState *state = mStyleStack.AppendElement();
|
|
|
|
state->globalAlpha = 1.0;
|
|
|
|
|
2014-04-25 19:34:07 -07:00
|
|
|
state->colorStyles[Style::FILL] = NS_RGB(0,0,0);
|
|
|
|
state->colorStyles[Style::STROKE] = NS_RGB(0,0,0);
|
2012-11-15 01:30:54 -08:00
|
|
|
state->shadowColor = NS_RGBA(0,0,0,0);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
CanvasRenderingContext2D::InitializeWithSurface(nsIDocShell *shell,
|
|
|
|
gfxASurface *surface,
|
|
|
|
int32_t width,
|
|
|
|
int32_t height)
|
|
|
|
{
|
2014-09-09 18:40:51 -07:00
|
|
|
RemovePostRefreshObserver();
|
2012-11-15 01:30:54 -08:00
|
|
|
mDocShell = shell;
|
2014-09-09 18:40:51 -07:00
|
|
|
AddPostRefreshObserverIfNecessary();
|
2012-11-15 01:30:54 -08:00
|
|
|
|
|
|
|
SetDimensions(width, height);
|
|
|
|
mTarget = gfxPlatform::GetPlatform()->
|
|
|
|
CreateDrawTargetForSurface(surface, IntSize(width, height));
|
2013-06-16 19:07:18 -07:00
|
|
|
|
2012-11-15 01:30:54 -08:00
|
|
|
if (!mTarget) {
|
|
|
|
EnsureErrorTarget();
|
|
|
|
mTarget = sErrorTarget;
|
|
|
|
}
|
|
|
|
|
2014-07-10 11:12:26 -07:00
|
|
|
if (mTarget->GetBackendType() == mgfx::BackendType::CAIRO) {
|
|
|
|
// Cf comment in EnsureTarget
|
|
|
|
mTarget->PushClipRect(mgfx::Rect(Point(0, 0), Size(mWidth, mHeight)));
|
|
|
|
}
|
|
|
|
|
2012-11-15 01:30:54 -08:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
CanvasRenderingContext2D::SetIsOpaque(bool isOpaque)
|
|
|
|
{
|
|
|
|
if (isOpaque != mOpaque) {
|
|
|
|
mOpaque = isOpaque;
|
|
|
|
ClearTarget();
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
CanvasRenderingContext2D::SetIsIPC(bool isIPC)
|
|
|
|
{
|
|
|
|
if (isIPC != mIPC) {
|
|
|
|
mIPC = isIPC;
|
|
|
|
ClearTarget();
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-12-20 13:50:48 -08:00
|
|
|
NS_IMETHODIMP
|
|
|
|
CanvasRenderingContext2D::SetContextOptions(JSContext* aCx, JS::Handle<JS::Value> aOptions)
|
|
|
|
{
|
|
|
|
if (aOptions.isNullOrUndefined()) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-09-02 16:26:36 -07:00
|
|
|
// This shouldn't be called before drawing starts, so there should be no drawtarget yet
|
|
|
|
MOZ_ASSERT(!mTarget);
|
|
|
|
|
2013-12-20 13:50:48 -08:00
|
|
|
ContextAttributes2D attributes;
|
|
|
|
NS_ENSURE_TRUE(attributes.Init(aCx, aOptions), NS_ERROR_UNEXPECTED);
|
|
|
|
|
|
|
|
if (Preferences::GetBool("gfx.canvas.willReadFrequently.enable", false)) {
|
|
|
|
// Use software when there is going to be a lot of readback
|
2014-09-02 16:26:36 -07:00
|
|
|
if (attributes.mWillReadFrequently) {
|
|
|
|
mRenderingMode = RenderingMode::SoftwareBackendMode;
|
|
|
|
}
|
2013-12-20 13:50:48 -08:00
|
|
|
}
|
|
|
|
|
2014-03-16 12:34:14 -07:00
|
|
|
if (!attributes.mAlpha) {
|
|
|
|
SetIsOpaque(true);
|
|
|
|
}
|
|
|
|
|
2013-12-20 13:50:48 -08:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-10-16 19:55:08 -07:00
|
|
|
void
|
|
|
|
CanvasRenderingContext2D::GetImageBuffer(uint8_t** aImageBuffer,
|
|
|
|
int32_t* aFormat)
|
2012-11-15 01:30:54 -08:00
|
|
|
{
|
2013-10-16 19:55:08 -07:00
|
|
|
*aImageBuffer = nullptr;
|
|
|
|
*aFormat = 0;
|
2013-05-15 12:25:47 -07:00
|
|
|
|
2013-11-03 08:28:30 -08:00
|
|
|
EnsureTarget();
|
|
|
|
RefPtr<SourceSurface> snapshot = mTarget->Snapshot();
|
|
|
|
if (!snapshot) {
|
2013-10-16 19:55:08 -07:00
|
|
|
return;
|
2012-11-15 01:30:54 -08:00
|
|
|
}
|
|
|
|
|
2013-11-03 08:28:30 -08:00
|
|
|
RefPtr<DataSourceSurface> data = snapshot->GetDataSurface();
|
2013-11-20 20:27:33 -08:00
|
|
|
if (!data || data->GetSize() != IntSize(mWidth, mHeight)) {
|
2013-10-16 19:55:08 -07:00
|
|
|
return;
|
2012-11-15 01:30:54 -08:00
|
|
|
}
|
|
|
|
|
2013-11-03 08:28:30 -08:00
|
|
|
*aImageBuffer = SurfaceToPackedBGRA(data);
|
2013-10-16 19:55:08 -07:00
|
|
|
*aFormat = imgIEncoder::INPUT_FORMAT_HOSTARGB;
|
|
|
|
}
|
|
|
|
|
2014-05-19 21:54:00 -07:00
|
|
|
nsString CanvasRenderingContext2D::GetHitRegion(const mozilla::gfx::Point& aPoint)
|
|
|
|
{
|
|
|
|
for (size_t x = 0 ; x < mHitRegionsOptions.Length(); x++) {
|
|
|
|
RegionInfo& info = mHitRegionsOptions[x];
|
|
|
|
if (info.mPath->ContainsPoint(aPoint, Matrix())) {
|
|
|
|
return info.mId;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nsString();
|
|
|
|
}
|
|
|
|
|
2013-10-16 19:55:08 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
CanvasRenderingContext2D::GetInputStream(const char *aMimeType,
|
2014-01-04 07:02:17 -08:00
|
|
|
const char16_t *aEncoderOptions,
|
2013-10-16 19:55:08 -07:00
|
|
|
nsIInputStream **aStream)
|
|
|
|
{
|
|
|
|
nsCString enccid("@mozilla.org/image/encoder;2?type=");
|
|
|
|
enccid += aMimeType;
|
|
|
|
nsCOMPtr<imgIEncoder> encoder = do_CreateInstance(enccid.get());
|
|
|
|
if (!encoder) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2012-11-15 01:30:54 -08:00
|
|
|
|
2013-11-20 16:55:53 -08:00
|
|
|
nsAutoArrayPtr<uint8_t> imageBuffer;
|
|
|
|
int32_t format = 0;
|
|
|
|
GetImageBuffer(getter_Transfers(imageBuffer), &format);
|
|
|
|
if (!imageBuffer) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2013-10-16 19:55:08 -07:00
|
|
|
return ImageEncoder::GetInputStream(mWidth, mHeight, imageBuffer, format,
|
|
|
|
encoder, aEncoderOptions, aStream);
|
2012-11-15 01:30:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
SurfaceFormat
|
|
|
|
CanvasRenderingContext2D::GetSurfaceFormat() const
|
|
|
|
{
|
2014-01-10 11:06:16 -08:00
|
|
|
return mOpaque ? SurfaceFormat::B8G8R8X8 : SurfaceFormat::B8G8R8A8;
|
2012-11-15 01:30:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// state
|
|
|
|
//
|
|
|
|
|
|
|
|
void
|
|
|
|
CanvasRenderingContext2D::Save()
|
|
|
|
{
|
|
|
|
EnsureTarget();
|
|
|
|
mStyleStack[mStyleStack.Length() - 1].transform = mTarget->GetTransform();
|
|
|
|
mStyleStack.SetCapacity(mStyleStack.Length() + 1);
|
|
|
|
mStyleStack.AppendElement(CurrentState());
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CanvasRenderingContext2D::Restore()
|
|
|
|
{
|
|
|
|
if (mStyleStack.Length() - 1 == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
TransformWillUpdate();
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < CurrentState().clipsPushed.size(); i++) {
|
|
|
|
mTarget->PopClip();
|
|
|
|
}
|
|
|
|
|
|
|
|
mStyleStack.RemoveElementAt(mStyleStack.Length() - 1);
|
|
|
|
|
|
|
|
mTarget->SetTransform(CurrentState().transform);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// transformations
|
|
|
|
//
|
|
|
|
|
|
|
|
void
|
|
|
|
CanvasRenderingContext2D::Scale(double x, double y, ErrorResult& error)
|
|
|
|
{
|
|
|
|
TransformWillUpdate();
|
|
|
|
if (!IsTargetValid()) {
|
|
|
|
error.Throw(NS_ERROR_FAILURE);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Matrix newMatrix = mTarget->GetTransform();
|
2014-09-10 10:29:35 -07:00
|
|
|
mTarget->SetTransform(newMatrix.PreScale(x, y));
|
2012-11-15 01:30:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CanvasRenderingContext2D::Rotate(double angle, ErrorResult& error)
|
|
|
|
{
|
|
|
|
TransformWillUpdate();
|
|
|
|
if (!IsTargetValid()) {
|
|
|
|
error.Throw(NS_ERROR_FAILURE);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Matrix rotation = Matrix::Rotation(angle);
|
|
|
|
mTarget->SetTransform(rotation * mTarget->GetTransform());
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CanvasRenderingContext2D::Translate(double x, double y, ErrorResult& error)
|
|
|
|
{
|
|
|
|
TransformWillUpdate();
|
|
|
|
if (!IsTargetValid()) {
|
|
|
|
error.Throw(NS_ERROR_FAILURE);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-09-10 10:29:35 -07:00
|
|
|
mTarget->SetTransform(Matrix(mTarget->GetTransform()).PreTranslate(x, y));
|
2012-11-15 01:30:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CanvasRenderingContext2D::Transform(double m11, double m12, double m21,
|
|
|
|
double m22, double dx, double dy,
|
|
|
|
ErrorResult& error)
|
|
|
|
{
|
|
|
|
TransformWillUpdate();
|
|
|
|
if (!IsTargetValid()) {
|
|
|
|
error.Throw(NS_ERROR_FAILURE);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Matrix matrix(m11, m12, m21, m22, dx, dy);
|
|
|
|
mTarget->SetTransform(matrix * mTarget->GetTransform());
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CanvasRenderingContext2D::SetTransform(double m11, double m12,
|
|
|
|
double m21, double m22,
|
|
|
|
double dx, double dy,
|
|
|
|
ErrorResult& error)
|
|
|
|
{
|
|
|
|
TransformWillUpdate();
|
|
|
|
if (!IsTargetValid()) {
|
|
|
|
error.Throw(NS_ERROR_FAILURE);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Matrix matrix(m11, m12, m21, m22, dx, dy);
|
|
|
|
mTarget->SetTransform(matrix);
|
|
|
|
}
|
|
|
|
|
2014-06-11 13:26:52 -07:00
|
|
|
static void
|
|
|
|
MatrixToJSObject(JSContext* cx, const Matrix& matrix,
|
|
|
|
JS::MutableHandle<JSObject*> result, ErrorResult& error)
|
2012-11-15 01:30:54 -08:00
|
|
|
{
|
2014-04-10 11:57:42 -07:00
|
|
|
double elts[6] = { matrix._11, matrix._12,
|
|
|
|
matrix._21, matrix._22,
|
|
|
|
matrix._31, matrix._32 };
|
2012-11-15 01:30:54 -08:00
|
|
|
|
|
|
|
// XXX Should we enter GetWrapper()'s compartment?
|
2014-04-10 11:57:42 -07:00
|
|
|
JS::Rooted<JS::Value> val(cx);
|
|
|
|
if (!ToJSValue(cx, elts, &val)) {
|
2012-11-15 01:30:54 -08:00
|
|
|
error.Throw(NS_ERROR_OUT_OF_MEMORY);
|
2014-06-11 13:26:52 -07:00
|
|
|
} else {
|
|
|
|
result.set(&val.toObject());
|
2012-11-15 01:30:54 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-06 05:30:50 -07:00
|
|
|
static bool
|
|
|
|
ObjectToMatrix(JSContext* cx, JS::Handle<JSObject*> obj, Matrix& matrix,
|
|
|
|
ErrorResult& error)
|
2012-11-15 01:30:54 -08:00
|
|
|
{
|
|
|
|
uint32_t length;
|
2013-05-06 05:30:50 -07:00
|
|
|
if (!JS_GetArrayLength(cx, obj, &length) || length != 6) {
|
2012-11-15 01:30:54 -08:00
|
|
|
// Not an array-like thing or wrong size
|
|
|
|
error.Throw(NS_ERROR_INVALID_ARG);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Float* elts[] = { &matrix._11, &matrix._12, &matrix._21, &matrix._22,
|
|
|
|
&matrix._31, &matrix._32 };
|
|
|
|
for (uint32_t i = 0; i < 6; ++i) {
|
2013-05-06 05:30:50 -07:00
|
|
|
JS::Rooted<JS::Value> elt(cx);
|
2012-11-15 01:30:54 -08:00
|
|
|
double d;
|
2013-08-05 06:02:47 -07:00
|
|
|
if (!JS_GetElement(cx, obj, i, &elt)) {
|
2012-11-15 01:30:54 -08:00
|
|
|
error.Throw(NS_ERROR_FAILURE);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!CoerceDouble(elt, &d)) {
|
|
|
|
error.Throw(NS_ERROR_INVALID_ARG);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!FloatValidate(d)) {
|
|
|
|
// This is weird, but it's the behavior of SetTransform()
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
*elts[i] = Float(d);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CanvasRenderingContext2D::SetMozCurrentTransform(JSContext* cx,
|
2013-05-16 09:36:55 -07:00
|
|
|
JS::Handle<JSObject*> currentTransform,
|
2012-11-15 01:30:54 -08:00
|
|
|
ErrorResult& error)
|
|
|
|
{
|
|
|
|
EnsureTarget();
|
|
|
|
if (!IsTargetValid()) {
|
|
|
|
error.Throw(NS_ERROR_FAILURE);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Matrix newCTM;
|
|
|
|
if (ObjectToMatrix(cx, currentTransform, newCTM, error)) {
|
|
|
|
mTarget->SetTransform(newCTM);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-11 13:26:52 -07:00
|
|
|
void
|
2012-11-15 01:30:54 -08:00
|
|
|
CanvasRenderingContext2D::GetMozCurrentTransform(JSContext* cx,
|
2014-06-11 13:26:52 -07:00
|
|
|
JS::MutableHandle<JSObject*> result,
|
2012-11-15 01:30:54 -08:00
|
|
|
ErrorResult& error) const
|
|
|
|
{
|
2014-06-11 13:26:52 -07:00
|
|
|
MatrixToJSObject(cx, mTarget ? mTarget->GetTransform() : Matrix(),
|
|
|
|
result, error);
|
2012-11-15 01:30:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CanvasRenderingContext2D::SetMozCurrentTransformInverse(JSContext* cx,
|
2013-05-16 09:36:55 -07:00
|
|
|
JS::Handle<JSObject*> currentTransform,
|
2012-11-15 01:30:54 -08:00
|
|
|
ErrorResult& error)
|
|
|
|
{
|
|
|
|
EnsureTarget();
|
|
|
|
if (!IsTargetValid()) {
|
|
|
|
error.Throw(NS_ERROR_FAILURE);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Matrix newCTMInverse;
|
|
|
|
if (ObjectToMatrix(cx, currentTransform, newCTMInverse, error)) {
|
|
|
|
// XXX ERRMSG we need to report an error to developers here! (bug 329026)
|
|
|
|
if (newCTMInverse.Invert()) {
|
|
|
|
mTarget->SetTransform(newCTMInverse);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-11 13:26:52 -07:00
|
|
|
void
|
2012-11-15 01:30:54 -08:00
|
|
|
CanvasRenderingContext2D::GetMozCurrentTransformInverse(JSContext* cx,
|
2014-06-11 13:26:52 -07:00
|
|
|
JS::MutableHandle<JSObject*> result,
|
2012-11-15 01:30:54 -08:00
|
|
|
ErrorResult& error) const
|
|
|
|
{
|
|
|
|
if (!mTarget) {
|
2014-06-11 13:26:52 -07:00
|
|
|
MatrixToJSObject(cx, Matrix(), result, error);
|
|
|
|
return;
|
2012-11-15 01:30:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
Matrix ctm = mTarget->GetTransform();
|
|
|
|
|
|
|
|
if (!ctm.Invert()) {
|
2014-04-27 19:38:31 -07:00
|
|
|
double NaN = JS_GetNaNValue(cx).toDouble();
|
2012-11-15 01:30:54 -08:00
|
|
|
ctm = Matrix(NaN, NaN, NaN, NaN, NaN, NaN);
|
|
|
|
}
|
|
|
|
|
2014-06-11 13:26:52 -07:00
|
|
|
MatrixToJSObject(cx, ctm, result, error);
|
2012-11-15 01:30:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// colors
|
|
|
|
//
|
|
|
|
|
|
|
|
void
|
2013-07-26 11:25:54 -07:00
|
|
|
CanvasRenderingContext2D::SetStyleFromUnion(const StringOrCanvasGradientOrCanvasPattern& value,
|
|
|
|
Style whichStyle)
|
2012-11-15 01:30:54 -08:00
|
|
|
{
|
2013-07-26 11:25:54 -07:00
|
|
|
if (value.IsString()) {
|
|
|
|
SetStyleFromString(value.GetAsString(), whichStyle);
|
2012-11-15 01:30:54 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-07-26 11:25:54 -07:00
|
|
|
if (value.IsCanvasGradient()) {
|
|
|
|
SetStyleFromGradient(value.GetAsCanvasGradient(), whichStyle);
|
|
|
|
return;
|
2012-11-15 01:30:54 -08:00
|
|
|
}
|
|
|
|
|
2013-07-26 11:25:54 -07:00
|
|
|
if (value.IsCanvasPattern()) {
|
|
|
|
SetStyleFromPattern(value.GetAsCanvasPattern(), whichStyle);
|
|
|
|
return;
|
2012-11-15 01:30:54 -08:00
|
|
|
}
|
|
|
|
|
2014-04-20 22:00:33 -07:00
|
|
|
MOZ_ASSERT_UNREACHABLE("Invalid union value");
|
2012-11-15 01:30:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CanvasRenderingContext2D::SetFillRule(const nsAString& aString)
|
|
|
|
{
|
|
|
|
FillRule rule;
|
|
|
|
|
|
|
|
if (aString.EqualsLiteral("evenodd"))
|
2014-01-10 11:06:17 -08:00
|
|
|
rule = FillRule::FILL_EVEN_ODD;
|
2012-11-15 01:30:54 -08:00
|
|
|
else if (aString.EqualsLiteral("nonzero"))
|
2014-01-10 11:06:17 -08:00
|
|
|
rule = FillRule::FILL_WINDING;
|
2012-11-15 01:30:54 -08:00
|
|
|
else
|
|
|
|
return;
|
|
|
|
|
|
|
|
CurrentState().fillRule = rule;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CanvasRenderingContext2D::GetFillRule(nsAString& aString)
|
|
|
|
{
|
|
|
|
switch (CurrentState().fillRule) {
|
2014-01-10 11:06:17 -08:00
|
|
|
case FillRule::FILL_WINDING:
|
2012-11-15 01:30:54 -08:00
|
|
|
aString.AssignLiteral("nonzero"); break;
|
2014-01-10 11:06:17 -08:00
|
|
|
case FillRule::FILL_EVEN_ODD:
|
2012-11-15 01:30:54 -08:00
|
|
|
aString.AssignLiteral("evenodd"); break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//
|
|
|
|
// gradients and patterns
|
|
|
|
//
|
2013-04-26 07:55:54 -07:00
|
|
|
already_AddRefed<CanvasGradient>
|
|
|
|
CanvasRenderingContext2D::CreateLinearGradient(double x0, double y0, double x1, double y1)
|
2012-11-15 01:30:54 -08:00
|
|
|
{
|
2013-04-26 07:55:54 -07:00
|
|
|
nsRefPtr<CanvasGradient> grad =
|
2013-04-26 07:55:56 -07:00
|
|
|
new CanvasLinearGradient(this, Point(x0, y0), Point(x1, y1));
|
2012-11-15 01:30:54 -08:00
|
|
|
|
|
|
|
return grad.forget();
|
|
|
|
}
|
|
|
|
|
2013-04-26 07:55:54 -07:00
|
|
|
already_AddRefed<CanvasGradient>
|
2012-11-15 01:30:54 -08:00
|
|
|
CanvasRenderingContext2D::CreateRadialGradient(double x0, double y0, double r0,
|
|
|
|
double x1, double y1, double r1,
|
|
|
|
ErrorResult& aError)
|
|
|
|
{
|
|
|
|
if (r0 < 0.0 || r1 < 0.0) {
|
|
|
|
aError.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2013-04-26 07:55:54 -07:00
|
|
|
nsRefPtr<CanvasGradient> grad =
|
2013-04-26 07:55:56 -07:00
|
|
|
new CanvasRadialGradient(this, Point(x0, y0), r0, Point(x1, y1), r1);
|
2012-11-15 01:30:54 -08:00
|
|
|
|
|
|
|
return grad.forget();
|
|
|
|
}
|
|
|
|
|
2013-04-19 01:49:22 -07:00
|
|
|
already_AddRefed<CanvasPattern>
|
2012-11-15 01:30:54 -08:00
|
|
|
CanvasRenderingContext2D::CreatePattern(const HTMLImageOrCanvasOrVideoElement& element,
|
|
|
|
const nsAString& repeat,
|
|
|
|
ErrorResult& error)
|
|
|
|
{
|
|
|
|
CanvasPattern::RepeatMode repeatMode =
|
2014-04-25 19:34:07 -07:00
|
|
|
CanvasPattern::RepeatMode::NOREPEAT;
|
2012-11-15 01:30:54 -08:00
|
|
|
|
|
|
|
if (repeat.IsEmpty() || repeat.EqualsLiteral("repeat")) {
|
2014-04-25 19:34:07 -07:00
|
|
|
repeatMode = CanvasPattern::RepeatMode::REPEAT;
|
2012-11-15 01:30:54 -08:00
|
|
|
} else if (repeat.EqualsLiteral("repeat-x")) {
|
2014-04-25 19:34:07 -07:00
|
|
|
repeatMode = CanvasPattern::RepeatMode::REPEATX;
|
2012-11-15 01:30:54 -08:00
|
|
|
} else if (repeat.EqualsLiteral("repeat-y")) {
|
2014-04-25 19:34:07 -07:00
|
|
|
repeatMode = CanvasPattern::RepeatMode::REPEATY;
|
2012-11-15 01:30:54 -08:00
|
|
|
} else if (repeat.EqualsLiteral("no-repeat")) {
|
2014-04-25 19:34:07 -07:00
|
|
|
repeatMode = CanvasPattern::RepeatMode::NOREPEAT;
|
2012-11-15 01:30:54 -08:00
|
|
|
} else {
|
|
|
|
error.Throw(NS_ERROR_DOM_SYNTAX_ERR);
|
2013-04-02 18:14:24 -07:00
|
|
|
return nullptr;
|
2012-11-15 01:30:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
Element* htmlElement;
|
|
|
|
if (element.IsHTMLCanvasElement()) {
|
2013-04-13 00:05:51 -07:00
|
|
|
HTMLCanvasElement* canvas = &element.GetAsHTMLCanvasElement();
|
2012-11-15 01:30:54 -08:00
|
|
|
htmlElement = canvas;
|
|
|
|
|
|
|
|
nsIntSize size = canvas->GetSize();
|
|
|
|
if (size.width == 0 || size.height == 0) {
|
|
|
|
error.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
|
2013-04-02 18:14:24 -07:00
|
|
|
return nullptr;
|
2012-11-15 01:30:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Special case for Canvas, which could be an Azure canvas!
|
|
|
|
nsICanvasRenderingContextInternal *srcCanvas = canvas->GetContextAtIndex(0);
|
|
|
|
if (srcCanvas) {
|
|
|
|
// This might not be an Azure canvas!
|
|
|
|
RefPtr<SourceSurface> srcSurf = srcCanvas->GetSurfaceSnapshot();
|
|
|
|
|
|
|
|
nsRefPtr<CanvasPattern> pat =
|
2013-04-26 07:55:37 -07:00
|
|
|
new CanvasPattern(this, srcSurf, repeatMode, htmlElement->NodePrincipal(), canvas->IsWriteOnly(), false);
|
2012-11-15 01:30:54 -08:00
|
|
|
|
|
|
|
return pat.forget();
|
|
|
|
}
|
|
|
|
} else if (element.IsHTMLImageElement()) {
|
2014-05-01 14:00:00 -07:00
|
|
|
HTMLImageElement* img = &element.GetAsHTMLImageElement();
|
|
|
|
if (img->IntrinsicState().HasState(NS_EVENT_STATE_BROKEN)) {
|
|
|
|
error.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
htmlElement = img;
|
2012-11-15 01:30:54 -08:00
|
|
|
} else {
|
2013-03-19 05:28:34 -07:00
|
|
|
htmlElement = &element.GetAsHTMLVideoElement();
|
2012-11-15 01:30:54 -08:00
|
|
|
}
|
|
|
|
|
2013-12-11 13:05:27 -08:00
|
|
|
EnsureTarget();
|
|
|
|
|
2012-11-15 01:30:54 -08:00
|
|
|
// The canvas spec says that createPattern should use the first frame
|
|
|
|
// of animated images
|
|
|
|
nsLayoutUtils::SurfaceFromElementResult res =
|
|
|
|
nsLayoutUtils::SurfaceFromElement(htmlElement,
|
2013-12-11 13:05:27 -08:00
|
|
|
nsLayoutUtils::SFE_WANT_FIRST_FRAME, mTarget);
|
2012-11-15 01:30:54 -08:00
|
|
|
|
2013-12-11 13:05:27 -08:00
|
|
|
if (!res.mSourceSurface) {
|
2013-05-01 19:04:08 -07:00
|
|
|
error.Throw(NS_ERROR_NOT_AVAILABLE);
|
2013-04-02 18:14:24 -07:00
|
|
|
return nullptr;
|
2012-11-15 01:30:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<CanvasPattern> pat =
|
2013-12-11 13:05:27 -08:00
|
|
|
new CanvasPattern(this, res.mSourceSurface, repeatMode, res.mPrincipal,
|
2012-11-15 01:30:54 -08:00
|
|
|
res.mIsWriteOnly, res.mCORSUsed);
|
|
|
|
|
|
|
|
return pat.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// shadows
|
|
|
|
//
|
|
|
|
void
|
|
|
|
CanvasRenderingContext2D::SetShadowColor(const nsAString& shadowColor)
|
|
|
|
{
|
|
|
|
nscolor color;
|
|
|
|
if (!ParseColor(shadowColor, &color)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
CurrentState().shadowColor = color;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// rects
|
|
|
|
//
|
|
|
|
|
|
|
|
void
|
|
|
|
CanvasRenderingContext2D::ClearRect(double x, double y, double w,
|
|
|
|
double h)
|
|
|
|
{
|
2012-11-27 12:32:05 -08:00
|
|
|
if (!mTarget) {
|
2012-11-15 01:30:54 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mTarget->ClearRect(mgfx::Rect(x, y, w, h));
|
|
|
|
|
|
|
|
RedrawUser(gfxRect(x, y, w, h));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CanvasRenderingContext2D::FillRect(double x, double y, double w,
|
|
|
|
double h)
|
|
|
|
{
|
|
|
|
const ContextState &state = CurrentState();
|
|
|
|
|
2014-04-25 19:34:07 -07:00
|
|
|
if (state.patternStyles[Style::FILL]) {
|
2012-11-15 01:30:54 -08:00
|
|
|
CanvasPattern::RepeatMode repeat =
|
2014-04-25 19:34:07 -07:00
|
|
|
state.patternStyles[Style::FILL]->mRepeat;
|
2012-11-15 01:30:54 -08:00
|
|
|
// In the FillRect case repeat modes are easy to deal with.
|
2014-04-25 19:34:07 -07:00
|
|
|
bool limitx = repeat == CanvasPattern::RepeatMode::NOREPEAT || repeat == CanvasPattern::RepeatMode::REPEATY;
|
|
|
|
bool limity = repeat == CanvasPattern::RepeatMode::NOREPEAT || repeat == CanvasPattern::RepeatMode::REPEATX;
|
2012-11-15 01:30:54 -08:00
|
|
|
|
|
|
|
IntSize patternSize =
|
2014-04-25 19:34:07 -07:00
|
|
|
state.patternStyles[Style::FILL]->mSurface->GetSize();
|
2012-11-15 01:30:54 -08:00
|
|
|
|
|
|
|
// We always need to execute painting for non-over operators, even if
|
|
|
|
// we end up with w/h = 0.
|
|
|
|
if (limitx) {
|
|
|
|
if (x < 0) {
|
|
|
|
w += x;
|
|
|
|
if (w < 0) {
|
|
|
|
w = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
x = 0;
|
|
|
|
}
|
|
|
|
if (x + w > patternSize.width) {
|
|
|
|
w = patternSize.width - x;
|
|
|
|
if (w < 0) {
|
|
|
|
w = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (limity) {
|
|
|
|
if (y < 0) {
|
|
|
|
h += y;
|
|
|
|
if (h < 0) {
|
|
|
|
h = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
y = 0;
|
|
|
|
}
|
|
|
|
if (y + h > patternSize.height) {
|
|
|
|
h = patternSize.height - y;
|
|
|
|
if (h < 0) {
|
|
|
|
h = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mgfx::Rect bounds;
|
|
|
|
|
|
|
|
EnsureTarget();
|
|
|
|
if (NeedToDrawShadow()) {
|
|
|
|
bounds = mgfx::Rect(x, y, w, h);
|
|
|
|
bounds = mTarget->GetTransform().TransformBounds(bounds);
|
|
|
|
}
|
|
|
|
|
|
|
|
AdjustedTarget(this, bounds.IsEmpty() ? nullptr : &bounds)->
|
|
|
|
FillRect(mgfx::Rect(x, y, w, h),
|
2014-04-25 19:34:07 -07:00
|
|
|
CanvasGeneralPattern().ForStyle(this, Style::FILL, mTarget),
|
2012-11-15 01:30:54 -08:00
|
|
|
DrawOptions(state.globalAlpha, UsedOperation()));
|
|
|
|
|
|
|
|
RedrawUser(gfxRect(x, y, w, h));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CanvasRenderingContext2D::StrokeRect(double x, double y, double w,
|
|
|
|
double h)
|
|
|
|
{
|
|
|
|
const ContextState &state = CurrentState();
|
|
|
|
|
|
|
|
mgfx::Rect bounds;
|
|
|
|
|
|
|
|
if (!w && !h) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
EnsureTarget();
|
|
|
|
if (!IsTargetValid()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NeedToDrawShadow()) {
|
|
|
|
bounds = mgfx::Rect(x - state.lineWidth / 2.0f, y - state.lineWidth / 2.0f,
|
|
|
|
w + state.lineWidth, h + state.lineWidth);
|
|
|
|
bounds = mTarget->GetTransform().TransformBounds(bounds);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!h) {
|
2014-01-10 11:06:17 -08:00
|
|
|
CapStyle cap = CapStyle::BUTT;
|
|
|
|
if (state.lineJoin == JoinStyle::ROUND) {
|
|
|
|
cap = CapStyle::ROUND;
|
2012-11-15 01:30:54 -08:00
|
|
|
}
|
|
|
|
AdjustedTarget(this, bounds.IsEmpty() ? nullptr : &bounds)->
|
|
|
|
StrokeLine(Point(x, y), Point(x + w, y),
|
2014-04-25 19:34:07 -07:00
|
|
|
CanvasGeneralPattern().ForStyle(this, Style::STROKE, mTarget),
|
2012-11-15 01:30:54 -08:00
|
|
|
StrokeOptions(state.lineWidth, state.lineJoin,
|
|
|
|
cap, state.miterLimit,
|
|
|
|
state.dash.Length(),
|
|
|
|
state.dash.Elements(),
|
|
|
|
state.dashOffset),
|
|
|
|
DrawOptions(state.globalAlpha, UsedOperation()));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!w) {
|
2014-01-10 11:06:17 -08:00
|
|
|
CapStyle cap = CapStyle::BUTT;
|
|
|
|
if (state.lineJoin == JoinStyle::ROUND) {
|
|
|
|
cap = CapStyle::ROUND;
|
2012-11-15 01:30:54 -08:00
|
|
|
}
|
|
|
|
AdjustedTarget(this, bounds.IsEmpty() ? nullptr : &bounds)->
|
|
|
|
StrokeLine(Point(x, y), Point(x, y + h),
|
2014-04-25 19:34:07 -07:00
|
|
|
CanvasGeneralPattern().ForStyle(this, Style::STROKE, mTarget),
|
2012-11-15 01:30:54 -08:00
|
|
|
StrokeOptions(state.lineWidth, state.lineJoin,
|
|
|
|
cap, state.miterLimit,
|
|
|
|
state.dash.Length(),
|
|
|
|
state.dash.Elements(),
|
|
|
|
state.dashOffset),
|
|
|
|
DrawOptions(state.globalAlpha, UsedOperation()));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
AdjustedTarget(this, bounds.IsEmpty() ? nullptr : &bounds)->
|
|
|
|
StrokeRect(mgfx::Rect(x, y, w, h),
|
2014-04-25 19:34:07 -07:00
|
|
|
CanvasGeneralPattern().ForStyle(this, Style::STROKE, mTarget),
|
2012-11-15 01:30:54 -08:00
|
|
|
StrokeOptions(state.lineWidth, state.lineJoin,
|
|
|
|
state.lineCap, state.miterLimit,
|
|
|
|
state.dash.Length(),
|
|
|
|
state.dash.Elements(),
|
|
|
|
state.dashOffset),
|
|
|
|
DrawOptions(state.globalAlpha, UsedOperation()));
|
|
|
|
|
|
|
|
Redraw();
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// path bits
|
|
|
|
//
|
|
|
|
|
|
|
|
void
|
|
|
|
CanvasRenderingContext2D::BeginPath()
|
|
|
|
{
|
|
|
|
mPath = nullptr;
|
|
|
|
mPathBuilder = nullptr;
|
|
|
|
mDSPathBuilder = nullptr;
|
|
|
|
mPathTransformWillUpdate = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-01-16 18:55:43 -08:00
|
|
|
CanvasRenderingContext2D::Fill(const CanvasWindingRule& winding)
|
2012-11-15 01:30:54 -08:00
|
|
|
{
|
2013-01-16 18:55:43 -08:00
|
|
|
EnsureUserSpacePath(winding);
|
2012-11-15 01:30:54 -08:00
|
|
|
|
|
|
|
if (!mPath) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mgfx::Rect bounds;
|
|
|
|
|
|
|
|
if (NeedToDrawShadow()) {
|
|
|
|
bounds = mPath->GetBounds(mTarget->GetTransform());
|
|
|
|
}
|
|
|
|
|
|
|
|
AdjustedTarget(this, bounds.IsEmpty() ? nullptr : &bounds)->
|
2014-04-25 19:34:07 -07:00
|
|
|
Fill(mPath, CanvasGeneralPattern().ForStyle(this, Style::FILL, mTarget),
|
2012-11-15 01:30:54 -08:00
|
|
|
DrawOptions(CurrentState().globalAlpha, UsedOperation()));
|
|
|
|
|
|
|
|
Redraw();
|
|
|
|
}
|
|
|
|
|
2014-03-18 05:03:06 -07:00
|
|
|
void CanvasRenderingContext2D::Fill(const CanvasPath& path, const CanvasWindingRule& winding)
|
|
|
|
{
|
|
|
|
EnsureTarget();
|
|
|
|
|
|
|
|
RefPtr<gfx::Path> gfxpath = path.GetPath(winding, mTarget);
|
|
|
|
|
|
|
|
if (!gfxpath) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mgfx::Rect bounds;
|
|
|
|
|
|
|
|
if (NeedToDrawShadow()) {
|
|
|
|
bounds = gfxpath->GetBounds(mTarget->GetTransform());
|
|
|
|
}
|
|
|
|
|
|
|
|
AdjustedTarget(this, bounds.IsEmpty() ? nullptr : &bounds)->
|
2014-04-25 19:34:07 -07:00
|
|
|
Fill(gfxpath, CanvasGeneralPattern().ForStyle(this, Style::FILL, mTarget),
|
2014-03-18 05:03:06 -07:00
|
|
|
DrawOptions(CurrentState().globalAlpha, UsedOperation()));
|
|
|
|
|
|
|
|
Redraw();
|
|
|
|
}
|
|
|
|
|
2012-11-15 01:30:54 -08:00
|
|
|
void
|
|
|
|
CanvasRenderingContext2D::Stroke()
|
|
|
|
{
|
|
|
|
EnsureUserSpacePath();
|
|
|
|
|
|
|
|
if (!mPath) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ContextState &state = CurrentState();
|
|
|
|
|
|
|
|
StrokeOptions strokeOptions(state.lineWidth, state.lineJoin,
|
|
|
|
state.lineCap, state.miterLimit,
|
|
|
|
state.dash.Length(), state.dash.Elements(),
|
|
|
|
state.dashOffset);
|
|
|
|
|
|
|
|
mgfx::Rect bounds;
|
|
|
|
if (NeedToDrawShadow()) {
|
|
|
|
bounds =
|
|
|
|
mPath->GetStrokedBounds(strokeOptions, mTarget->GetTransform());
|
|
|
|
}
|
|
|
|
|
|
|
|
AdjustedTarget(this, bounds.IsEmpty() ? nullptr : &bounds)->
|
2014-04-25 19:34:07 -07:00
|
|
|
Stroke(mPath, CanvasGeneralPattern().ForStyle(this, Style::STROKE, mTarget),
|
2012-11-15 01:30:54 -08:00
|
|
|
strokeOptions, DrawOptions(state.globalAlpha, UsedOperation()));
|
|
|
|
|
|
|
|
Redraw();
|
|
|
|
}
|
|
|
|
|
2014-03-18 05:03:06 -07:00
|
|
|
void
|
|
|
|
CanvasRenderingContext2D::Stroke(const CanvasPath& path)
|
|
|
|
{
|
|
|
|
EnsureTarget();
|
|
|
|
|
|
|
|
RefPtr<gfx::Path> gfxpath = path.GetPath(CanvasWindingRule::Nonzero, mTarget);
|
|
|
|
|
|
|
|
if (!gfxpath) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ContextState &state = CurrentState();
|
|
|
|
|
|
|
|
StrokeOptions strokeOptions(state.lineWidth, state.lineJoin,
|
|
|
|
state.lineCap, state.miterLimit,
|
|
|
|
state.dash.Length(), state.dash.Elements(),
|
|
|
|
state.dashOffset);
|
|
|
|
|
|
|
|
mgfx::Rect bounds;
|
|
|
|
if (NeedToDrawShadow()) {
|
|
|
|
bounds =
|
|
|
|
gfxpath->GetStrokedBounds(strokeOptions, mTarget->GetTransform());
|
|
|
|
}
|
|
|
|
|
|
|
|
AdjustedTarget(this, bounds.IsEmpty() ? nullptr : &bounds)->
|
2014-04-25 19:34:07 -07:00
|
|
|
Stroke(gfxpath, CanvasGeneralPattern().ForStyle(this, Style::STROKE, mTarget),
|
2014-03-18 05:03:06 -07:00
|
|
|
strokeOptions, DrawOptions(state.globalAlpha, UsedOperation()));
|
|
|
|
|
|
|
|
Redraw();
|
|
|
|
}
|
|
|
|
|
2014-01-15 06:50:31 -08:00
|
|
|
void CanvasRenderingContext2D::DrawFocusIfNeeded(mozilla::dom::Element& aElement)
|
2013-11-04 11:52:24 -08:00
|
|
|
{
|
|
|
|
EnsureUserSpacePath();
|
|
|
|
|
|
|
|
if (!mPath) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(DrawCustomFocusRing(aElement)) {
|
|
|
|
Save();
|
|
|
|
|
|
|
|
// set state to conforming focus state
|
|
|
|
ContextState& state = CurrentState();
|
|
|
|
state.globalAlpha = 1.0;
|
|
|
|
state.shadowBlur = 0;
|
|
|
|
state.shadowOffset.x = 0;
|
|
|
|
state.shadowOffset.y = 0;
|
2014-01-10 11:06:17 -08:00
|
|
|
state.op = mozilla::gfx::CompositionOp::OP_OVER;
|
2013-11-04 11:52:24 -08:00
|
|
|
|
2014-01-10 11:06:17 -08:00
|
|
|
state.lineCap = CapStyle::BUTT;
|
|
|
|
state.lineJoin = mozilla::gfx::JoinStyle::MITER_OR_BEVEL;
|
2013-11-04 11:52:24 -08:00
|
|
|
state.lineWidth = 1;
|
|
|
|
CurrentState().dash.Clear();
|
|
|
|
|
|
|
|
// color and style of the rings is the same as for image maps
|
|
|
|
// set the background focus color
|
2014-04-25 19:34:07 -07:00
|
|
|
CurrentState().SetColorStyle(Style::STROKE, NS_RGBA(255, 255, 255, 255));
|
2013-11-04 11:52:24 -08:00
|
|
|
// draw the focus ring
|
|
|
|
Stroke();
|
|
|
|
|
|
|
|
// set dashing for foreground
|
|
|
|
FallibleTArray<mozilla::gfx::Float>& dash = CurrentState().dash;
|
|
|
|
dash.AppendElement(1);
|
|
|
|
dash.AppendElement(1);
|
|
|
|
|
|
|
|
// set the foreground focus color
|
2014-04-25 19:34:07 -07:00
|
|
|
CurrentState().SetColorStyle(Style::STROKE, NS_RGBA(0,0,0, 255));
|
2013-11-04 11:52:24 -08:00
|
|
|
// draw the focus ring
|
|
|
|
Stroke();
|
|
|
|
|
|
|
|
Restore();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CanvasRenderingContext2D::DrawCustomFocusRing(mozilla::dom::Element& aElement)
|
|
|
|
{
|
|
|
|
EnsureUserSpacePath();
|
|
|
|
|
|
|
|
HTMLCanvasElement* canvas = GetCanvas();
|
|
|
|
|
|
|
|
if (!canvas|| !nsContentUtils::ContentIsDescendantOf(&aElement, canvas)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIFocusManager* fm = nsFocusManager::GetFocusManager();
|
|
|
|
if (fm) {
|
|
|
|
// check that the element i focused
|
|
|
|
nsCOMPtr<nsIDOMElement> focusedElement;
|
|
|
|
fm->GetFocusedElement(getter_AddRefs(focusedElement));
|
|
|
|
if (SameCOMIdentity(aElement.AsDOMNode(), focusedElement)) {
|
2014-05-02 00:45:00 -07:00
|
|
|
nsPIDOMWindow *window = aElement.OwnerDoc()->GetWindow();
|
|
|
|
if (window) {
|
|
|
|
return window->ShouldShowFocusRing();
|
|
|
|
}
|
2013-11-04 11:52:24 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-11-15 01:30:54 -08:00
|
|
|
void
|
2013-01-16 18:55:43 -08:00
|
|
|
CanvasRenderingContext2D::Clip(const CanvasWindingRule& winding)
|
2012-11-15 01:30:54 -08:00
|
|
|
{
|
2013-01-16 18:55:43 -08:00
|
|
|
EnsureUserSpacePath(winding);
|
2012-11-15 01:30:54 -08:00
|
|
|
|
|
|
|
if (!mPath) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mTarget->PushClip(mPath);
|
|
|
|
CurrentState().clipsPushed.push_back(mPath);
|
|
|
|
}
|
|
|
|
|
2014-03-18 05:03:06 -07:00
|
|
|
void
|
|
|
|
CanvasRenderingContext2D::Clip(const CanvasPath& path, const CanvasWindingRule& winding)
|
|
|
|
{
|
|
|
|
EnsureTarget();
|
|
|
|
|
|
|
|
RefPtr<gfx::Path> gfxpath = path.GetPath(winding, mTarget);
|
|
|
|
|
|
|
|
if (!gfxpath) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mTarget->PushClip(gfxpath);
|
|
|
|
CurrentState().clipsPushed.push_back(gfxpath);
|
|
|
|
}
|
|
|
|
|
2012-11-15 01:30:54 -08:00
|
|
|
void
|
|
|
|
CanvasRenderingContext2D::ArcTo(double x1, double y1, double x2,
|
|
|
|
double y2, double radius,
|
|
|
|
ErrorResult& error)
|
|
|
|
{
|
|
|
|
if (radius < 0) {
|
|
|
|
error.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
EnsureWritablePath();
|
|
|
|
|
|
|
|
// Current point in user space!
|
|
|
|
Point p0;
|
|
|
|
if (mPathBuilder) {
|
|
|
|
p0 = mPathBuilder->CurrentPoint();
|
|
|
|
} else {
|
|
|
|
Matrix invTransform = mTarget->GetTransform();
|
|
|
|
if (!invTransform.Invert()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
p0 = invTransform * mDSPathBuilder->CurrentPoint();
|
|
|
|
}
|
|
|
|
|
|
|
|
Point p1(x1, y1);
|
|
|
|
Point p2(x2, y2);
|
|
|
|
|
|
|
|
// Execute these calculations in double precision to avoid cumulative
|
|
|
|
// rounding errors.
|
|
|
|
double dir, a2, b2, c2, cosx, sinx, d, anx, any,
|
|
|
|
bnx, bny, x3, y3, x4, y4, cx, cy, angle0, angle1;
|
|
|
|
bool anticlockwise;
|
|
|
|
|
|
|
|
if (p0 == p1 || p1 == p2 || radius == 0) {
|
|
|
|
LineTo(p1.x, p1.y);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for colinearity
|
2014-08-28 09:45:48 -07:00
|
|
|
dir = (p2.x - p1.x) * (p0.y - p1.y) + (p2.y - p1.y) * (p1.x - p0.x);
|
2012-11-15 01:30:54 -08:00
|
|
|
if (dir == 0) {
|
|
|
|
LineTo(p1.x, p1.y);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// XXX - Math for this code was already available from the non-azure code
|
|
|
|
// and would be well tested. Perhaps converting to bezier directly might
|
|
|
|
// be more efficient longer run.
|
|
|
|
a2 = (p0.x-x1)*(p0.x-x1) + (p0.y-y1)*(p0.y-y1);
|
|
|
|
b2 = (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2);
|
|
|
|
c2 = (p0.x-x2)*(p0.x-x2) + (p0.y-y2)*(p0.y-y2);
|
|
|
|
cosx = (a2+b2-c2)/(2*sqrt(a2*b2));
|
|
|
|
|
|
|
|
sinx = sqrt(1 - cosx*cosx);
|
|
|
|
d = radius / ((1 - cosx) / sinx);
|
|
|
|
|
|
|
|
anx = (x1-p0.x) / sqrt(a2);
|
|
|
|
any = (y1-p0.y) / sqrt(a2);
|
|
|
|
bnx = (x1-x2) / sqrt(b2);
|
|
|
|
bny = (y1-y2) / sqrt(b2);
|
|
|
|
x3 = x1 - anx*d;
|
|
|
|
y3 = y1 - any*d;
|
|
|
|
x4 = x1 - bnx*d;
|
|
|
|
y4 = y1 - bny*d;
|
|
|
|
anticlockwise = (dir < 0);
|
|
|
|
cx = x3 + any*radius*(anticlockwise ? 1 : -1);
|
|
|
|
cy = y3 - anx*radius*(anticlockwise ? 1 : -1);
|
|
|
|
angle0 = atan2((y3-cy), (x3-cx));
|
|
|
|
angle1 = atan2((y4-cy), (x4-cx));
|
|
|
|
|
|
|
|
|
|
|
|
LineTo(x3, y3);
|
|
|
|
|
|
|
|
Arc(cx, cy, radius, angle0, angle1, anticlockwise, error);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CanvasRenderingContext2D::Arc(double x, double y, double r,
|
|
|
|
double startAngle, double endAngle,
|
|
|
|
bool anticlockwise, ErrorResult& error)
|
|
|
|
{
|
|
|
|
if (r < 0.0) {
|
|
|
|
error.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
EnsureWritablePath();
|
|
|
|
|
2013-12-13 04:14:36 -08:00
|
|
|
ArcToBezier(this, Point(x, y), Size(r, r), startAngle, endAngle, anticlockwise);
|
2012-11-15 01:30:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CanvasRenderingContext2D::Rect(double x, double y, double w, double h)
|
|
|
|
{
|
|
|
|
EnsureWritablePath();
|
|
|
|
|
|
|
|
if (mPathBuilder) {
|
|
|
|
mPathBuilder->MoveTo(Point(x, y));
|
|
|
|
mPathBuilder->LineTo(Point(x + w, y));
|
|
|
|
mPathBuilder->LineTo(Point(x + w, y + h));
|
|
|
|
mPathBuilder->LineTo(Point(x, y + h));
|
|
|
|
mPathBuilder->Close();
|
|
|
|
} else {
|
|
|
|
mDSPathBuilder->MoveTo(mTarget->GetTransform() * Point(x, y));
|
|
|
|
mDSPathBuilder->LineTo(mTarget->GetTransform() * Point(x + w, y));
|
|
|
|
mDSPathBuilder->LineTo(mTarget->GetTransform() * Point(x + w, y + h));
|
|
|
|
mDSPathBuilder->LineTo(mTarget->GetTransform() * Point(x, y + h));
|
|
|
|
mDSPathBuilder->Close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CanvasRenderingContext2D::EnsureWritablePath()
|
|
|
|
{
|
|
|
|
if (mDSPathBuilder) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
FillRule fillRule = CurrentState().fillRule;
|
|
|
|
|
|
|
|
if (mPathBuilder) {
|
|
|
|
if (mPathTransformWillUpdate) {
|
|
|
|
mPath = mPathBuilder->Finish();
|
|
|
|
mDSPathBuilder =
|
|
|
|
mPath->TransformedCopyToBuilder(mPathToDS, fillRule);
|
|
|
|
mPath = nullptr;
|
|
|
|
mPathBuilder = nullptr;
|
|
|
|
mPathTransformWillUpdate = false;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
EnsureTarget();
|
|
|
|
if (!mPath) {
|
|
|
|
NS_ASSERTION(!mPathTransformWillUpdate, "mPathTransformWillUpdate should be false, if all paths are null");
|
|
|
|
mPathBuilder = mTarget->CreatePathBuilder(fillRule);
|
|
|
|
} else if (!mPathTransformWillUpdate) {
|
|
|
|
mPathBuilder = mPath->CopyToBuilder(fillRule);
|
|
|
|
} else {
|
|
|
|
mDSPathBuilder =
|
|
|
|
mPath->TransformedCopyToBuilder(mPathToDS, fillRule);
|
|
|
|
mPathTransformWillUpdate = false;
|
2012-11-23 11:02:36 -08:00
|
|
|
mPath = nullptr;
|
2012-11-15 01:30:54 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-01-16 18:55:43 -08:00
|
|
|
CanvasRenderingContext2D::EnsureUserSpacePath(const CanvasWindingRule& winding)
|
2012-11-15 01:30:54 -08:00
|
|
|
{
|
|
|
|
FillRule fillRule = CurrentState().fillRule;
|
2013-05-06 12:28:13 -07:00
|
|
|
if(winding == CanvasWindingRule::Evenodd)
|
2014-01-10 11:06:17 -08:00
|
|
|
fillRule = FillRule::FILL_EVEN_ODD;
|
2012-11-15 01:30:54 -08:00
|
|
|
|
|
|
|
if (!mPath && !mPathBuilder && !mDSPathBuilder) {
|
|
|
|
EnsureTarget();
|
|
|
|
mPathBuilder = mTarget->CreatePathBuilder(fillRule);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mPathBuilder) {
|
|
|
|
mPath = mPathBuilder->Finish();
|
|
|
|
mPathBuilder = nullptr;
|
|
|
|
}
|
|
|
|
|
2012-11-29 11:37:49 -08:00
|
|
|
if (mPath &&
|
2012-11-15 01:30:54 -08:00
|
|
|
mPathTransformWillUpdate) {
|
|
|
|
mDSPathBuilder =
|
|
|
|
mPath->TransformedCopyToBuilder(mPathToDS, fillRule);
|
|
|
|
mPath = nullptr;
|
|
|
|
mPathTransformWillUpdate = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mDSPathBuilder) {
|
|
|
|
RefPtr<Path> dsPath;
|
|
|
|
dsPath = mDSPathBuilder->Finish();
|
|
|
|
mDSPathBuilder = nullptr;
|
|
|
|
|
|
|
|
Matrix inverse = mTarget->GetTransform();
|
|
|
|
if (!inverse.Invert()) {
|
|
|
|
NS_WARNING("Could not invert transform");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mPathBuilder =
|
|
|
|
dsPath->TransformedCopyToBuilder(inverse, fillRule);
|
|
|
|
mPath = mPathBuilder->Finish();
|
|
|
|
mPathBuilder = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mPath && mPath->GetFillRule() != fillRule) {
|
|
|
|
mPathBuilder = mPath->CopyToBuilder(fillRule);
|
|
|
|
mPath = mPathBuilder->Finish();
|
2013-11-08 19:24:24 -08:00
|
|
|
mPathBuilder = nullptr;
|
2012-11-15 01:30:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_ASSERTION(mPath, "mPath should exist");
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CanvasRenderingContext2D::TransformWillUpdate()
|
|
|
|
{
|
|
|
|
EnsureTarget();
|
|
|
|
|
|
|
|
// Store the matrix that would transform the current path to device
|
|
|
|
// space.
|
|
|
|
if (mPath || mPathBuilder) {
|
|
|
|
if (!mPathTransformWillUpdate) {
|
|
|
|
// If the transform has already been updated, but a device space builder
|
|
|
|
// has not been created yet mPathToDS contains the right transform to
|
|
|
|
// transform the current mPath into device space.
|
|
|
|
// We should leave it alone.
|
|
|
|
mPathToDS = mTarget->GetTransform();
|
|
|
|
}
|
|
|
|
mPathTransformWillUpdate = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// text
|
|
|
|
//
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper function for SetFont that creates a style rule for the given font.
|
|
|
|
* @param aFont The CSS font string
|
|
|
|
* @param aNode The canvas element
|
|
|
|
* @param aResult Pointer in which to place the new style rule.
|
|
|
|
* @remark Assumes all pointer arguments are non-null.
|
|
|
|
*/
|
|
|
|
static nsresult
|
|
|
|
CreateFontStyleRule(const nsAString& aFont,
|
|
|
|
nsINode* aNode,
|
|
|
|
StyleRule** aResult)
|
|
|
|
{
|
|
|
|
nsRefPtr<StyleRule> rule;
|
|
|
|
bool changed;
|
|
|
|
|
|
|
|
nsIPrincipal* principal = aNode->NodePrincipal();
|
|
|
|
nsIDocument* document = aNode->OwnerDoc();
|
|
|
|
|
|
|
|
nsIURI* docURL = document->GetDocumentURI();
|
|
|
|
nsIURI* baseURL = document->GetDocBaseURI();
|
|
|
|
|
|
|
|
// Pass the CSS Loader object to the parser, to allow parser error reports
|
|
|
|
// to include the outer window ID.
|
|
|
|
nsCSSParser parser(document->CSSLoader());
|
|
|
|
|
|
|
|
nsresult rv = parser.ParseStyleAttribute(EmptyString(), docURL, baseURL,
|
|
|
|
principal, getter_AddRefs(rule));
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = parser.ParseProperty(eCSSProperty_font, aFont, docURL, baseURL,
|
|
|
|
principal, rule->GetDeclaration(), &changed,
|
|
|
|
false);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
rv = parser.ParseProperty(eCSSProperty_line_height,
|
|
|
|
NS_LITERAL_STRING("normal"), docURL, baseURL,
|
|
|
|
principal, rule->GetDeclaration(), &changed,
|
|
|
|
false);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
rule->RuleMatched();
|
|
|
|
|
|
|
|
rule.forget(aResult);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CanvasRenderingContext2D::SetFont(const nsAString& font,
|
|
|
|
ErrorResult& error)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* If font is defined with relative units (e.g. ems) and the parent
|
|
|
|
* style context changes in between calls, setting the font to the
|
|
|
|
* same value as previous could result in a different computed value,
|
|
|
|
* so we cannot have the optimization where we check if the new font
|
|
|
|
* string is equal to the old one.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (!mCanvasElement && !mDocShell) {
|
|
|
|
NS_WARNING("Canvas element must be non-null or a docshell must be provided");
|
|
|
|
error.Throw(NS_ERROR_FAILURE);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIPresShell* presShell = GetPresShell();
|
|
|
|
if (!presShell) {
|
|
|
|
error.Throw(NS_ERROR_FAILURE);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
nsIDocument* document = presShell->GetDocument();
|
|
|
|
|
|
|
|
nsRefPtr<css::StyleRule> rule;
|
|
|
|
error = CreateFontStyleRule(font, document, getter_AddRefs(rule));
|
|
|
|
|
|
|
|
if (error.Failed()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
css::Declaration *declaration = rule->GetDeclaration();
|
|
|
|
// The easiest way to see whether we got a syntax error or whether
|
|
|
|
// we got 'inherit' or 'initial' is to look at font-size-adjust,
|
|
|
|
// which the shorthand resets to either 'none' or
|
|
|
|
// '-moz-system-font'.
|
|
|
|
// We know the declaration is not !important, so we can use
|
|
|
|
// GetNormalBlock().
|
|
|
|
const nsCSSValue *fsaVal =
|
|
|
|
declaration->GetNormalBlock()->ValueFor(eCSSProperty_font_size_adjust);
|
|
|
|
if (!fsaVal || (fsaVal->GetUnit() != eCSSUnit_None &&
|
|
|
|
fsaVal->GetUnit() != eCSSUnit_System_Font)) {
|
|
|
|
// We got an all-property value or a syntax error. The spec says
|
|
|
|
// this value must be ignored.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsTArray< nsCOMPtr<nsIStyleRule> > rules;
|
|
|
|
rules.AppendElement(rule);
|
|
|
|
|
|
|
|
nsStyleSet* styleSet = presShell->StyleSet();
|
|
|
|
|
|
|
|
// have to get a parent style context for inherit-like relative
|
|
|
|
// values (2em, bolder, etc.)
|
|
|
|
nsRefPtr<nsStyleContext> parentContext;
|
|
|
|
|
|
|
|
if (mCanvasElement && mCanvasElement->IsInDoc()) {
|
|
|
|
// inherit from the canvas element
|
|
|
|
parentContext = nsComputedDOMStyle::GetStyleContextForElement(
|
|
|
|
mCanvasElement,
|
|
|
|
nullptr,
|
|
|
|
presShell);
|
|
|
|
} else {
|
|
|
|
// otherwise inherit from default (10px sans-serif)
|
|
|
|
nsRefPtr<css::StyleRule> parentRule;
|
|
|
|
error = CreateFontStyleRule(NS_LITERAL_STRING("10px sans-serif"),
|
|
|
|
document,
|
|
|
|
getter_AddRefs(parentRule));
|
|
|
|
|
|
|
|
if (error.Failed()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsTArray< nsCOMPtr<nsIStyleRule> > parentRules;
|
|
|
|
parentRules.AppendElement(parentRule);
|
|
|
|
parentContext = styleSet->ResolveStyleForRules(nullptr, parentRules);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!parentContext) {
|
|
|
|
error.Throw(NS_ERROR_FAILURE);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-06-28 20:28:50 -07:00
|
|
|
// add a rule to prevent text zoom from affecting the style
|
|
|
|
rules.AppendElement(new nsDisableTextZoomStyleRule);
|
|
|
|
|
2012-11-15 01:30:54 -08:00
|
|
|
nsRefPtr<nsStyleContext> sc =
|
|
|
|
styleSet->ResolveStyleForRules(parentContext, rules);
|
|
|
|
if (!sc) {
|
|
|
|
error.Throw(NS_ERROR_FAILURE);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-02-16 13:51:02 -08:00
|
|
|
const nsStyleFont* fontStyle = sc->StyleFont();
|
2012-11-15 01:30:54 -08:00
|
|
|
|
|
|
|
NS_ASSERTION(fontStyle, "Could not obtain font style");
|
|
|
|
|
2013-02-16 13:51:02 -08:00
|
|
|
nsIAtom* language = sc->StyleFont()->mLanguage;
|
2012-11-15 01:30:54 -08:00
|
|
|
if (!language) {
|
|
|
|
language = presShell->GetPresContext()->GetLanguageFromCharset();
|
|
|
|
}
|
|
|
|
|
|
|
|
// use CSS pixels instead of dev pixels to avoid being affected by page zoom
|
|
|
|
const uint32_t aupcp = nsPresContext::AppUnitsPerCSSPixel();
|
|
|
|
|
|
|
|
bool printerFont = (presShell->GetPresContext()->Type() == nsPresContext::eContext_PrintPreview ||
|
|
|
|
presShell->GetPresContext()->Type() == nsPresContext::eContext_Print);
|
|
|
|
|
2013-06-28 20:28:50 -07:00
|
|
|
// Purposely ignore the font size that respects the user's minimum
|
|
|
|
// font preference (fontStyle->mFont.size) in favor of the computed
|
|
|
|
// size (fontStyle->mSize). See
|
|
|
|
// https://bugzilla.mozilla.org/show_bug.cgi?id=698652.
|
|
|
|
MOZ_ASSERT(!fontStyle->mAllowZoom,
|
|
|
|
"expected text zoom to be disabled on this nsStyleFont");
|
2012-11-15 01:30:54 -08:00
|
|
|
gfxFontStyle style(fontStyle->mFont.style,
|
|
|
|
fontStyle->mFont.weight,
|
|
|
|
fontStyle->mFont.stretch,
|
2013-06-28 20:28:50 -07:00
|
|
|
NSAppUnitsToFloatPixels(fontStyle->mSize, float(aupcp)),
|
2012-11-15 01:30:54 -08:00
|
|
|
language,
|
|
|
|
fontStyle->mFont.sizeAdjust,
|
|
|
|
fontStyle->mFont.systemFont,
|
|
|
|
printerFont,
|
2014-06-19 00:08:55 -07:00
|
|
|
fontStyle->mFont.synthesis & NS_FONT_SYNTHESIS_WEIGHT,
|
|
|
|
fontStyle->mFont.synthesis & NS_FONT_SYNTHESIS_STYLE,
|
2012-11-15 01:30:54 -08:00
|
|
|
fontStyle->mFont.languageOverride);
|
|
|
|
|
|
|
|
fontStyle->mFont.AddFontFeaturesToStyle(&style);
|
|
|
|
|
2013-11-24 20:59:56 -08:00
|
|
|
nsPresContext *c = presShell->GetPresContext();
|
2012-11-15 01:30:54 -08:00
|
|
|
CurrentState().fontGroup =
|
2014-06-05 23:09:23 -07:00
|
|
|
gfxPlatform::GetPlatform()->CreateFontGroup(fontStyle->mFont.fontlist,
|
2012-11-15 01:30:54 -08:00
|
|
|
&style,
|
2013-11-24 20:59:56 -08:00
|
|
|
c->GetUserFontSet());
|
2012-11-15 01:30:54 -08:00
|
|
|
NS_ASSERTION(CurrentState().fontGroup, "Could not get font group");
|
2013-11-24 20:59:56 -08:00
|
|
|
CurrentState().fontGroup->SetTextPerfMetrics(c->GetTextPerfMetrics());
|
2012-11-15 01:30:54 -08:00
|
|
|
|
|
|
|
// The font getter is required to be reserialized based on what we
|
|
|
|
// parsed (including having line-height removed). (Older drafts of
|
|
|
|
// the spec required font sizes be converted to pixels, but that no
|
|
|
|
// longer seems to be required.)
|
|
|
|
declaration->GetValue(eCSSProperty_font, CurrentState().font);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CanvasRenderingContext2D::SetTextAlign(const nsAString& ta)
|
|
|
|
{
|
|
|
|
if (ta.EqualsLiteral("start"))
|
2014-04-25 19:34:07 -07:00
|
|
|
CurrentState().textAlign = TextAlign::START;
|
2012-11-15 01:30:54 -08:00
|
|
|
else if (ta.EqualsLiteral("end"))
|
2014-04-25 19:34:07 -07:00
|
|
|
CurrentState().textAlign = TextAlign::END;
|
2012-11-15 01:30:54 -08:00
|
|
|
else if (ta.EqualsLiteral("left"))
|
2014-04-25 19:34:07 -07:00
|
|
|
CurrentState().textAlign = TextAlign::LEFT;
|
2012-11-15 01:30:54 -08:00
|
|
|
else if (ta.EqualsLiteral("right"))
|
2014-04-25 19:34:07 -07:00
|
|
|
CurrentState().textAlign = TextAlign::RIGHT;
|
2012-11-15 01:30:54 -08:00
|
|
|
else if (ta.EqualsLiteral("center"))
|
2014-04-25 19:34:07 -07:00
|
|
|
CurrentState().textAlign = TextAlign::CENTER;
|
2012-11-15 01:30:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CanvasRenderingContext2D::GetTextAlign(nsAString& ta)
|
|
|
|
{
|
|
|
|
switch (CurrentState().textAlign)
|
|
|
|
{
|
2014-04-25 19:34:07 -07:00
|
|
|
case TextAlign::START:
|
2012-11-15 01:30:54 -08:00
|
|
|
ta.AssignLiteral("start");
|
|
|
|
break;
|
2014-04-25 19:34:07 -07:00
|
|
|
case TextAlign::END:
|
2012-11-15 01:30:54 -08:00
|
|
|
ta.AssignLiteral("end");
|
|
|
|
break;
|
2014-04-25 19:34:07 -07:00
|
|
|
case TextAlign::LEFT:
|
2012-11-15 01:30:54 -08:00
|
|
|
ta.AssignLiteral("left");
|
|
|
|
break;
|
2014-04-25 19:34:07 -07:00
|
|
|
case TextAlign::RIGHT:
|
2012-11-15 01:30:54 -08:00
|
|
|
ta.AssignLiteral("right");
|
|
|
|
break;
|
2014-04-25 19:34:07 -07:00
|
|
|
case TextAlign::CENTER:
|
2012-11-15 01:30:54 -08:00
|
|
|
ta.AssignLiteral("center");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CanvasRenderingContext2D::SetTextBaseline(const nsAString& tb)
|
|
|
|
{
|
|
|
|
if (tb.EqualsLiteral("top"))
|
2014-04-25 19:34:07 -07:00
|
|
|
CurrentState().textBaseline = TextBaseline::TOP;
|
2012-11-15 01:30:54 -08:00
|
|
|
else if (tb.EqualsLiteral("hanging"))
|
2014-04-25 19:34:07 -07:00
|
|
|
CurrentState().textBaseline = TextBaseline::HANGING;
|
2012-11-15 01:30:54 -08:00
|
|
|
else if (tb.EqualsLiteral("middle"))
|
2014-04-25 19:34:07 -07:00
|
|
|
CurrentState().textBaseline = TextBaseline::MIDDLE;
|
2012-11-15 01:30:54 -08:00
|
|
|
else if (tb.EqualsLiteral("alphabetic"))
|
2014-04-25 19:34:07 -07:00
|
|
|
CurrentState().textBaseline = TextBaseline::ALPHABETIC;
|
2012-11-15 01:30:54 -08:00
|
|
|
else if (tb.EqualsLiteral("ideographic"))
|
2014-04-25 19:34:07 -07:00
|
|
|
CurrentState().textBaseline = TextBaseline::IDEOGRAPHIC;
|
2012-11-15 01:30:54 -08:00
|
|
|
else if (tb.EqualsLiteral("bottom"))
|
2014-04-25 19:34:07 -07:00
|
|
|
CurrentState().textBaseline = TextBaseline::BOTTOM;
|
2012-11-15 01:30:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CanvasRenderingContext2D::GetTextBaseline(nsAString& tb)
|
|
|
|
{
|
|
|
|
switch (CurrentState().textBaseline)
|
|
|
|
{
|
2014-04-25 19:34:07 -07:00
|
|
|
case TextBaseline::TOP:
|
2012-11-15 01:30:54 -08:00
|
|
|
tb.AssignLiteral("top");
|
|
|
|
break;
|
2014-04-25 19:34:07 -07:00
|
|
|
case TextBaseline::HANGING:
|
2012-11-15 01:30:54 -08:00
|
|
|
tb.AssignLiteral("hanging");
|
|
|
|
break;
|
2014-04-25 19:34:07 -07:00
|
|
|
case TextBaseline::MIDDLE:
|
2012-11-15 01:30:54 -08:00
|
|
|
tb.AssignLiteral("middle");
|
|
|
|
break;
|
2014-04-25 19:34:07 -07:00
|
|
|
case TextBaseline::ALPHABETIC:
|
2012-11-15 01:30:54 -08:00
|
|
|
tb.AssignLiteral("alphabetic");
|
|
|
|
break;
|
2014-04-25 19:34:07 -07:00
|
|
|
case TextBaseline::IDEOGRAPHIC:
|
2012-11-15 01:30:54 -08:00
|
|
|
tb.AssignLiteral("ideographic");
|
|
|
|
break;
|
2014-04-25 19:34:07 -07:00
|
|
|
case TextBaseline::BOTTOM:
|
2012-11-15 01:30:54 -08:00
|
|
|
tb.AssignLiteral("bottom");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Helper function that replaces the whitespace characters in a string
|
|
|
|
* with U+0020 SPACE. The whitespace characters are defined as U+0020 SPACE,
|
|
|
|
* U+0009 CHARACTER TABULATION (tab), U+000A LINE FEED (LF), U+000B LINE
|
|
|
|
* TABULATION, U+000C FORM FEED (FF), and U+000D CARRIAGE RETURN (CR).
|
|
|
|
* @param str The string whose whitespace characters to replace.
|
|
|
|
*/
|
|
|
|
static inline void
|
|
|
|
TextReplaceWhitespaceCharacters(nsAutoString& str)
|
|
|
|
{
|
2014-01-04 07:02:17 -08:00
|
|
|
str.ReplaceChar("\x09\x0A\x0B\x0C\x0D", char16_t(' '));
|
2012-11-15 01:30:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CanvasRenderingContext2D::FillText(const nsAString& text, double x,
|
|
|
|
double y,
|
|
|
|
const Optional<double>& maxWidth,
|
|
|
|
ErrorResult& error)
|
|
|
|
{
|
2014-04-25 19:34:07 -07:00
|
|
|
error = DrawOrMeasureText(text, x, y, maxWidth, TextDrawOperation::FILL, nullptr);
|
2012-11-15 01:30:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CanvasRenderingContext2D::StrokeText(const nsAString& text, double x,
|
|
|
|
double y,
|
|
|
|
const Optional<double>& maxWidth,
|
|
|
|
ErrorResult& error)
|
|
|
|
{
|
2014-04-25 19:34:07 -07:00
|
|
|
error = DrawOrMeasureText(text, x, y, maxWidth, TextDrawOperation::STROKE, nullptr);
|
2012-11-15 01:30:54 -08:00
|
|
|
}
|
|
|
|
|
2013-04-19 01:49:22 -07:00
|
|
|
TextMetrics*
|
2012-11-15 01:30:54 -08:00
|
|
|
CanvasRenderingContext2D::MeasureText(const nsAString& rawText,
|
|
|
|
ErrorResult& error)
|
|
|
|
{
|
|
|
|
float width;
|
|
|
|
Optional<double> maxWidth;
|
2014-04-25 19:34:07 -07:00
|
|
|
error = DrawOrMeasureText(rawText, 0, 0, maxWidth, TextDrawOperation::MEASURE, &width);
|
2012-11-15 01:30:54 -08:00
|
|
|
if (error.Failed()) {
|
2013-04-02 18:14:24 -07:00
|
|
|
return nullptr;
|
2012-11-15 01:30:54 -08:00
|
|
|
}
|
|
|
|
|
2013-04-19 01:49:22 -07:00
|
|
|
return new TextMetrics(width);
|
2012-11-15 01:30:54 -08:00
|
|
|
}
|
|
|
|
|
2014-02-21 05:35:36 -08:00
|
|
|
void
|
|
|
|
CanvasRenderingContext2D::AddHitRegion(const HitRegionOptions& options, ErrorResult& error)
|
|
|
|
{
|
2014-02-21 05:35:58 -08:00
|
|
|
// check if the path is valid
|
|
|
|
EnsureUserSpacePath(CanvasWindingRule::Nonzero);
|
|
|
|
if(!mPath) {
|
|
|
|
error.Throw(NS_ERROR_DOM_NOT_SUPPORTED_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get the bounds of the current path. They are relative to the canvas
|
|
|
|
mgfx::Rect bounds(mPath->GetBounds(mTarget->GetTransform()));
|
2014-02-25 05:53:42 -08:00
|
|
|
if ((bounds.width == 0) || (bounds.height == 0) || !bounds.IsFinite()) {
|
|
|
|
// The specified region has no pixels.
|
|
|
|
error.Throw(NS_ERROR_DOM_NOT_SUPPORTED_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-05-18 16:43:00 -07:00
|
|
|
// remove old hit region first
|
|
|
|
RemoveHitRegion(options.mId);
|
|
|
|
|
|
|
|
if (options.mControl) {
|
|
|
|
// also remove regions with this control
|
2014-05-19 21:54:00 -07:00
|
|
|
for (size_t x = 0; x < mHitRegionsOptions.Length(); x++) {
|
2014-05-18 16:43:00 -07:00
|
|
|
RegionInfo& info = mHitRegionsOptions[x];
|
|
|
|
if (info.mElement == options.mControl) {
|
|
|
|
mHitRegionsOptions.RemoveElementAt(x);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-02-25 13:47:29 -08:00
|
|
|
#ifdef ACCESSIBILITY
|
2014-05-18 16:43:00 -07:00
|
|
|
options.mControl->SetProperty(nsGkAtoms::hitregion, new bool(true),
|
|
|
|
nsINode::DeleteProperty<bool>);
|
2014-05-19 07:36:40 -07:00
|
|
|
#endif
|
|
|
|
}
|
2014-07-11 13:45:22 -07:00
|
|
|
|
2014-05-18 16:43:00 -07:00
|
|
|
// finally, add the region to the list
|
|
|
|
RegionInfo info;
|
|
|
|
info.mId = options.mId;
|
|
|
|
info.mElement = options.mControl;
|
|
|
|
RefPtr<PathBuilder> pathBuilder = mPath->TransformedCopyToBuilder(mTarget->GetTransform());
|
|
|
|
info.mPath = pathBuilder->Finish();
|
|
|
|
|
|
|
|
mHitRegionsOptions.InsertElementAt(0, info);
|
2014-02-21 05:35:36 -08:00
|
|
|
}
|
2014-02-21 05:35:14 -08:00
|
|
|
|
2014-02-21 05:35:36 -08:00
|
|
|
void
|
|
|
|
CanvasRenderingContext2D::RemoveHitRegion(const nsAString& id)
|
|
|
|
{
|
2014-05-18 16:43:00 -07:00
|
|
|
if (id.Length() == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-05-19 21:54:00 -07:00
|
|
|
for (size_t x = 0; x < mHitRegionsOptions.Length(); x++) {
|
2014-05-18 16:43:00 -07:00
|
|
|
RegionInfo& info = mHitRegionsOptions[x];
|
|
|
|
if (info.mId == id) {
|
|
|
|
mHitRegionsOptions.RemoveElementAt(x);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2014-05-18 10:43:00 -07:00
|
|
|
}
|
2014-05-18 16:43:00 -07:00
|
|
|
}
|
2014-05-18 10:43:00 -07:00
|
|
|
|
2014-05-18 16:43:00 -07:00
|
|
|
bool
|
|
|
|
CanvasRenderingContext2D::GetHitRegionRect(Element* aElement, nsRect& aRect)
|
|
|
|
{
|
|
|
|
for (unsigned int x = 0; x < mHitRegionsOptions.Length(); x++) {
|
|
|
|
RegionInfo& info = mHitRegionsOptions[x];
|
|
|
|
if (info.mElement == aElement) {
|
|
|
|
mgfx::Rect bounds(info.mPath->GetBounds());
|
|
|
|
gfxRect rect(bounds.x, bounds.y, bounds.width, bounds.height);
|
|
|
|
aRect = nsLayoutUtils::RoundGfxRectToAppRect(rect, AppUnitsPerCSSPixel());
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2014-02-21 05:35:36 -08:00
|
|
|
}
|
2014-02-21 05:35:14 -08:00
|
|
|
|
2012-11-15 01:30:54 -08:00
|
|
|
/**
|
|
|
|
* Used for nsBidiPresUtils::ProcessText
|
|
|
|
*/
|
2013-04-11 20:20:09 -07:00
|
|
|
struct MOZ_STACK_CLASS CanvasBidiProcessor : public nsBidiPresUtils::BidiProcessor
|
2012-11-15 01:30:54 -08:00
|
|
|
{
|
|
|
|
typedef CanvasRenderingContext2D::ContextState ContextState;
|
|
|
|
|
2014-01-04 07:02:17 -08:00
|
|
|
virtual void SetText(const char16_t* text, int32_t length, nsBidiDirection direction)
|
2012-11-15 01:30:54 -08:00
|
|
|
{
|
|
|
|
mFontgrp->UpdateFontList(); // ensure user font generation is current
|
|
|
|
mTextRun = mFontgrp->MakeTextRun(text,
|
|
|
|
length,
|
|
|
|
mThebes,
|
|
|
|
mAppUnitsPerDevPixel,
|
|
|
|
direction==NSBIDI_RTL ? gfxTextRunFactory::TEXT_IS_RTL : 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual nscoord GetWidth()
|
|
|
|
{
|
|
|
|
gfxTextRun::Metrics textRunMetrics = mTextRun->MeasureText(0,
|
|
|
|
mTextRun->GetLength(),
|
|
|
|
mDoMeasureBoundingBox ?
|
|
|
|
gfxFont::TIGHT_INK_EXTENTS :
|
|
|
|
gfxFont::LOOSE_INK_EXTENTS,
|
|
|
|
mThebes,
|
|
|
|
nullptr);
|
|
|
|
|
|
|
|
// this only measures the height; the total width is gotten from the
|
|
|
|
// the return value of ProcessText.
|
|
|
|
if (mDoMeasureBoundingBox) {
|
|
|
|
textRunMetrics.mBoundingBox.Scale(1.0 / mAppUnitsPerDevPixel);
|
|
|
|
mBoundingBox = mBoundingBox.Union(textRunMetrics.mBoundingBox);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NSToCoordRound(textRunMetrics.mAdvanceWidth);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void DrawText(nscoord xOffset, nscoord width)
|
|
|
|
{
|
|
|
|
gfxPoint point = mPt;
|
|
|
|
point.x += xOffset;
|
|
|
|
|
|
|
|
// offset is given in terms of left side of string
|
|
|
|
if (mTextRun->IsRightToLeft()) {
|
|
|
|
// Bug 581092 - don't use rounded pixel width to advance to
|
|
|
|
// right-hand end of run, because this will cause different
|
|
|
|
// glyph positioning for LTR vs RTL drawing of the same
|
|
|
|
// glyph string on OS X and DWrite where textrun widths may
|
|
|
|
// involve fractional pixels.
|
|
|
|
gfxTextRun::Metrics textRunMetrics =
|
|
|
|
mTextRun->MeasureText(0,
|
|
|
|
mTextRun->GetLength(),
|
|
|
|
mDoMeasureBoundingBox ?
|
|
|
|
gfxFont::TIGHT_INK_EXTENTS :
|
|
|
|
gfxFont::LOOSE_INK_EXTENTS,
|
|
|
|
mThebes,
|
|
|
|
nullptr);
|
|
|
|
point.x += textRunMetrics.mAdvanceWidth;
|
|
|
|
// old code was:
|
|
|
|
// point.x += width * mAppUnitsPerDevPixel;
|
|
|
|
// TODO: restore this if/when we move to fractional coords
|
|
|
|
// throughout the text layout process
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t numRuns;
|
|
|
|
const gfxTextRun::GlyphRun *runs = mTextRun->GetGlyphRuns(&numRuns);
|
2013-02-19 13:46:27 -08:00
|
|
|
const int32_t appUnitsPerDevUnit = mAppUnitsPerDevPixel;
|
2012-11-15 01:30:54 -08:00
|
|
|
const double devUnitsPerAppUnit = 1.0/double(appUnitsPerDevUnit);
|
|
|
|
Point baselineOrigin =
|
|
|
|
Point(point.x * devUnitsPerAppUnit, point.y * devUnitsPerAppUnit);
|
|
|
|
|
|
|
|
float advanceSum = 0;
|
|
|
|
|
|
|
|
mCtx->EnsureTarget();
|
|
|
|
for (uint32_t c = 0; c < numRuns; c++) {
|
|
|
|
gfxFont *font = runs[c].mFont;
|
|
|
|
uint32_t endRun = 0;
|
|
|
|
if (c + 1 < numRuns) {
|
|
|
|
endRun = runs[c + 1].mCharacterOffset;
|
|
|
|
} else {
|
|
|
|
endRun = mTextRun->GetLength();
|
|
|
|
}
|
|
|
|
|
|
|
|
const gfxTextRun::CompressedGlyph *glyphs = mTextRun->GetCharacterGlyphs();
|
|
|
|
|
|
|
|
RefPtr<ScaledFont> scaledFont =
|
|
|
|
gfxPlatform::GetPlatform()->GetScaledFontForFont(mCtx->mTarget, font);
|
|
|
|
|
|
|
|
if (!scaledFont) {
|
|
|
|
// This can occur when something switched DirectWrite off.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-10-13 09:23:20 -07:00
|
|
|
RefPtr<GlyphRenderingOptions> renderingOptions = font->GetGlyphRenderingOptions();
|
|
|
|
|
2012-11-15 01:30:54 -08:00
|
|
|
GlyphBuffer buffer;
|
|
|
|
|
|
|
|
std::vector<Glyph> glyphBuf;
|
|
|
|
|
|
|
|
for (uint32_t i = runs[c].mCharacterOffset; i < endRun; i++) {
|
|
|
|
Glyph newGlyph;
|
|
|
|
if (glyphs[i].IsSimpleGlyph()) {
|
|
|
|
newGlyph.mIndex = glyphs[i].GetSimpleGlyph();
|
|
|
|
if (mTextRun->IsRightToLeft()) {
|
|
|
|
newGlyph.mPosition.x = baselineOrigin.x - advanceSum -
|
|
|
|
glyphs[i].GetSimpleAdvance() * devUnitsPerAppUnit;
|
|
|
|
} else {
|
|
|
|
newGlyph.mPosition.x = baselineOrigin.x + advanceSum;
|
|
|
|
}
|
|
|
|
newGlyph.mPosition.y = baselineOrigin.y;
|
|
|
|
advanceSum += glyphs[i].GetSimpleAdvance() * devUnitsPerAppUnit;
|
|
|
|
glyphBuf.push_back(newGlyph);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!glyphs[i].GetGlyphCount()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
gfxTextRun::DetailedGlyph *detailedGlyphs =
|
|
|
|
mTextRun->GetDetailedGlyphs(i);
|
|
|
|
|
|
|
|
if (glyphs[i].IsMissing()) {
|
2012-11-03 09:17:17 -07:00
|
|
|
newGlyph.mIndex = 0;
|
|
|
|
if (mTextRun->IsRightToLeft()) {
|
|
|
|
newGlyph.mPosition.x = baselineOrigin.x - advanceSum -
|
|
|
|
detailedGlyphs[0].mAdvance * devUnitsPerAppUnit;
|
|
|
|
} else {
|
|
|
|
newGlyph.mPosition.x = baselineOrigin.x + advanceSum;
|
|
|
|
}
|
|
|
|
newGlyph.mPosition.y = baselineOrigin.y;
|
2013-02-08 08:52:55 -08:00
|
|
|
advanceSum += detailedGlyphs[0].mAdvance * devUnitsPerAppUnit;
|
2012-11-03 09:17:17 -07:00
|
|
|
glyphBuf.push_back(newGlyph);
|
2012-11-15 01:30:54 -08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (uint32_t c = 0; c < glyphs[i].GetGlyphCount(); c++) {
|
|
|
|
newGlyph.mIndex = detailedGlyphs[c].mGlyphID;
|
|
|
|
if (mTextRun->IsRightToLeft()) {
|
|
|
|
newGlyph.mPosition.x = baselineOrigin.x + detailedGlyphs[c].mXOffset * devUnitsPerAppUnit -
|
|
|
|
advanceSum - detailedGlyphs[c].mAdvance * devUnitsPerAppUnit;
|
|
|
|
} else {
|
|
|
|
newGlyph.mPosition.x = baselineOrigin.x + detailedGlyphs[c].mXOffset * devUnitsPerAppUnit + advanceSum;
|
|
|
|
}
|
|
|
|
newGlyph.mPosition.y = baselineOrigin.y + detailedGlyphs[c].mYOffset * devUnitsPerAppUnit;
|
|
|
|
glyphBuf.push_back(newGlyph);
|
|
|
|
advanceSum += detailedGlyphs[c].mAdvance * devUnitsPerAppUnit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!glyphBuf.size()) {
|
|
|
|
// This may happen for glyph runs for a 0 size font.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer.mGlyphs = &glyphBuf.front();
|
|
|
|
buffer.mNumGlyphs = glyphBuf.size();
|
|
|
|
|
|
|
|
Rect bounds = mCtx->mTarget->GetTransform().
|
|
|
|
TransformBounds(Rect(mBoundingBox.x, mBoundingBox.y,
|
|
|
|
mBoundingBox.width, mBoundingBox.height));
|
2014-04-25 19:34:07 -07:00
|
|
|
if (mOp == CanvasRenderingContext2D::TextDrawOperation::FILL) {
|
2012-11-15 01:30:54 -08:00
|
|
|
AdjustedTarget(mCtx, &bounds)->
|
|
|
|
FillGlyphs(scaledFont, buffer,
|
|
|
|
CanvasGeneralPattern().
|
2014-04-25 19:34:07 -07:00
|
|
|
ForStyle(mCtx, CanvasRenderingContext2D::Style::FILL, mCtx->mTarget),
|
2013-10-13 09:23:20 -07:00
|
|
|
DrawOptions(mState->globalAlpha, mCtx->UsedOperation()),
|
|
|
|
renderingOptions);
|
2014-04-25 19:34:07 -07:00
|
|
|
} else if (mOp == CanvasRenderingContext2D::TextDrawOperation::STROKE) {
|
2013-04-30 11:26:28 -07:00
|
|
|
// stroke glyphs one at a time to avoid poor CoreGraphics performance
|
|
|
|
// when stroking a path with a very large number of points
|
|
|
|
buffer.mGlyphs = &glyphBuf.front();
|
|
|
|
buffer.mNumGlyphs = 1;
|
2012-11-15 01:30:54 -08:00
|
|
|
const ContextState& state = *mState;
|
2013-04-30 11:26:28 -07:00
|
|
|
AdjustedTarget target(mCtx, &bounds);
|
|
|
|
const StrokeOptions strokeOpts(state.lineWidth, state.lineJoin,
|
|
|
|
state.lineCap, state.miterLimit,
|
|
|
|
state.dash.Length(),
|
|
|
|
state.dash.Elements(),
|
|
|
|
state.dashOffset);
|
|
|
|
CanvasGeneralPattern cgp;
|
|
|
|
const Pattern& patForStyle
|
2014-04-25 19:34:07 -07:00
|
|
|
(cgp.ForStyle(mCtx, CanvasRenderingContext2D::Style::STROKE, mCtx->mTarget));
|
2013-04-30 11:26:28 -07:00
|
|
|
const DrawOptions drawOpts(state.globalAlpha, mCtx->UsedOperation());
|
|
|
|
|
|
|
|
for (unsigned i = glyphBuf.size(); i > 0; --i) {
|
|
|
|
RefPtr<Path> path = scaledFont->GetPathForGlyphs(buffer, mCtx->mTarget);
|
|
|
|
target->Stroke(path, patForStyle, strokeOpts, drawOpts);
|
|
|
|
buffer.mGlyphs++;
|
|
|
|
}
|
2012-11-15 01:30:54 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// current text run
|
|
|
|
nsAutoPtr<gfxTextRun> mTextRun;
|
|
|
|
|
|
|
|
// pointer to a screen reference context used to measure text and such
|
|
|
|
nsRefPtr<gfxContext> mThebes;
|
|
|
|
|
|
|
|
// Pointer to the draw target we should fill our text to
|
|
|
|
CanvasRenderingContext2D *mCtx;
|
|
|
|
|
|
|
|
// position of the left side of the string, alphabetic baseline
|
|
|
|
gfxPoint mPt;
|
|
|
|
|
|
|
|
// current font
|
|
|
|
gfxFontGroup* mFontgrp;
|
|
|
|
|
|
|
|
// dev pixel conversion factor
|
2013-02-19 13:46:27 -08:00
|
|
|
int32_t mAppUnitsPerDevPixel;
|
2012-11-15 01:30:54 -08:00
|
|
|
|
|
|
|
// operation (fill or stroke)
|
|
|
|
CanvasRenderingContext2D::TextDrawOperation mOp;
|
|
|
|
|
|
|
|
// context state
|
|
|
|
ContextState *mState;
|
|
|
|
|
|
|
|
// union of bounding boxes of all runs, needed for shadows
|
|
|
|
gfxRect mBoundingBox;
|
|
|
|
|
|
|
|
// true iff the bounding box should be measured
|
|
|
|
bool mDoMeasureBoundingBox;
|
|
|
|
};
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
CanvasRenderingContext2D::DrawOrMeasureText(const nsAString& aRawText,
|
|
|
|
float aX,
|
|
|
|
float aY,
|
|
|
|
const Optional<double>& aMaxWidth,
|
|
|
|
TextDrawOperation aOp,
|
|
|
|
float* aWidth)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
// spec isn't clear on what should happen if aMaxWidth <= 0, so
|
|
|
|
// treat it as an invalid argument
|
|
|
|
// technically, 0 should be an invalid value as well, but 0 is the default
|
|
|
|
// arg, and there is no way to tell if the default was used
|
|
|
|
if (aMaxWidth.WasPassed() && aMaxWidth.Value() < 0)
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
|
|
|
|
if (!mCanvasElement && !mDocShell) {
|
|
|
|
NS_WARNING("Canvas element must be non-null or a docshell must be provided");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPresShell> presShell = GetPresShell();
|
|
|
|
if (!presShell)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
nsIDocument* document = presShell->GetDocument();
|
|
|
|
|
|
|
|
// replace all the whitespace characters with U+0020 SPACE
|
|
|
|
nsAutoString textToDraw(aRawText);
|
|
|
|
TextReplaceWhitespaceCharacters(textToDraw);
|
|
|
|
|
|
|
|
// for now, default to ltr if not in doc
|
|
|
|
bool isRTL = false;
|
|
|
|
|
|
|
|
if (mCanvasElement && mCanvasElement->IsInDoc()) {
|
|
|
|
// try to find the closest context
|
|
|
|
nsRefPtr<nsStyleContext> canvasStyle =
|
|
|
|
nsComputedDOMStyle::GetStyleContextForElement(mCanvasElement,
|
|
|
|
nullptr,
|
|
|
|
presShell);
|
|
|
|
if (!canvasStyle) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2013-02-16 13:51:02 -08:00
|
|
|
isRTL = canvasStyle->StyleVisibility()->mDirection ==
|
2012-11-15 01:30:54 -08:00
|
|
|
NS_STYLE_DIRECTION_RTL;
|
|
|
|
} else {
|
|
|
|
isRTL = GET_BIDI_OPTION_DIRECTION(document->GetBidiOptions()) == IBMBIDI_TEXTDIRECTION_RTL;
|
|
|
|
}
|
|
|
|
|
|
|
|
gfxFontGroup* currentFontStyle = GetCurrentFontStyle();
|
|
|
|
NS_ASSERTION(currentFontStyle, "font group is null");
|
|
|
|
|
2013-12-19 12:21:11 -08:00
|
|
|
// ensure user font set is up to date
|
|
|
|
currentFontStyle->
|
|
|
|
SetUserFontSet(presShell->GetPresContext()->GetUserFontSet());
|
|
|
|
|
2012-11-15 01:30:54 -08:00
|
|
|
if (currentFontStyle->GetStyle()->size == 0.0F) {
|
|
|
|
if (aWidth) {
|
|
|
|
*aWidth = 0;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ContextState &state = CurrentState();
|
|
|
|
|
|
|
|
// This is only needed to know if we can know the drawing bounding box easily.
|
2012-12-18 08:37:14 -08:00
|
|
|
bool doDrawShadow = NeedToDrawShadow();
|
2012-11-15 01:30:54 -08:00
|
|
|
|
|
|
|
CanvasBidiProcessor processor;
|
|
|
|
|
|
|
|
GetAppUnitsValues(&processor.mAppUnitsPerDevPixel, nullptr);
|
|
|
|
processor.mPt = gfxPoint(aX, aY);
|
|
|
|
processor.mThebes =
|
2013-10-14 17:29:15 -07:00
|
|
|
new gfxContext(gfxPlatform::GetPlatform()->ScreenReferenceDrawTarget());
|
2012-11-15 01:30:54 -08:00
|
|
|
|
|
|
|
// If we don't have a target then we don't have a transform. A target won't
|
|
|
|
// be needed in the case where we're measuring the text size. This allows
|
|
|
|
// to avoid creating a target if it's only being used to measure text sizes.
|
|
|
|
if (mTarget) {
|
|
|
|
Matrix matrix = mTarget->GetTransform();
|
|
|
|
processor.mThebes->SetMatrix(gfxMatrix(matrix._11, matrix._12, matrix._21, matrix._22, matrix._31, matrix._32));
|
|
|
|
}
|
|
|
|
processor.mCtx = this;
|
|
|
|
processor.mOp = aOp;
|
|
|
|
processor.mBoundingBox = gfxRect(0, 0, 0, 0);
|
|
|
|
processor.mDoMeasureBoundingBox = doDrawShadow || !mIsEntireFrameInvalid;
|
|
|
|
processor.mState = &CurrentState();
|
|
|
|
processor.mFontgrp = currentFontStyle;
|
|
|
|
|
|
|
|
nscoord totalWidthCoord;
|
|
|
|
|
|
|
|
// calls bidi algo twice since it needs the full text width and the
|
|
|
|
// bounding boxes before rendering anything
|
|
|
|
nsBidi bidiEngine;
|
|
|
|
rv = nsBidiPresUtils::ProcessText(textToDraw.get(),
|
|
|
|
textToDraw.Length(),
|
|
|
|
isRTL ? NSBIDI_RTL : NSBIDI_LTR,
|
|
|
|
presShell->GetPresContext(),
|
|
|
|
processor,
|
|
|
|
nsBidiPresUtils::MODE_MEASURE,
|
|
|
|
nullptr,
|
|
|
|
0,
|
|
|
|
&totalWidthCoord,
|
|
|
|
&bidiEngine);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
float totalWidth = float(totalWidthCoord) / processor.mAppUnitsPerDevPixel;
|
|
|
|
if (aWidth) {
|
|
|
|
*aWidth = totalWidth;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if only measuring, don't need to do any more work
|
2014-04-25 19:34:07 -07:00
|
|
|
if (aOp==TextDrawOperation::MEASURE) {
|
2012-11-15 01:30:54 -08:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// offset pt.x based on text align
|
|
|
|
gfxFloat anchorX;
|
|
|
|
|
2014-04-25 19:34:07 -07:00
|
|
|
if (state.textAlign == TextAlign::CENTER) {
|
2012-11-15 01:30:54 -08:00
|
|
|
anchorX = .5;
|
2014-04-25 19:34:07 -07:00
|
|
|
} else if (state.textAlign == TextAlign::LEFT ||
|
|
|
|
(!isRTL && state.textAlign == TextAlign::START) ||
|
|
|
|
(isRTL && state.textAlign == TextAlign::END)) {
|
2012-11-15 01:30:54 -08:00
|
|
|
anchorX = 0;
|
|
|
|
} else {
|
|
|
|
anchorX = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
processor.mPt.x -= anchorX * totalWidth;
|
|
|
|
|
|
|
|
// offset pt.y based on text baseline
|
|
|
|
processor.mFontgrp->UpdateFontList(); // ensure user font generation is current
|
|
|
|
NS_ASSERTION(processor.mFontgrp->FontListLength()>0, "font group contains no fonts");
|
|
|
|
const gfxFont::Metrics& fontMetrics = processor.mFontgrp->GetFontAt(0)->GetMetrics();
|
|
|
|
|
|
|
|
gfxFloat anchorY;
|
|
|
|
|
|
|
|
switch (state.textBaseline)
|
|
|
|
{
|
2014-04-25 19:34:07 -07:00
|
|
|
case TextBaseline::HANGING:
|
2012-11-15 01:30:54 -08:00
|
|
|
// fall through; best we can do with the information available
|
2014-04-25 19:34:07 -07:00
|
|
|
case TextBaseline::TOP:
|
2012-11-15 01:30:54 -08:00
|
|
|
anchorY = fontMetrics.emAscent;
|
|
|
|
break;
|
2014-04-25 19:34:07 -07:00
|
|
|
case TextBaseline::MIDDLE:
|
2012-11-15 01:30:54 -08:00
|
|
|
anchorY = (fontMetrics.emAscent - fontMetrics.emDescent) * .5f;
|
|
|
|
break;
|
2014-04-25 19:34:07 -07:00
|
|
|
case TextBaseline::IDEOGRAPHIC:
|
2012-11-15 01:30:54 -08:00
|
|
|
// fall through; best we can do with the information available
|
2014-04-25 19:34:07 -07:00
|
|
|
case TextBaseline::ALPHABETIC:
|
2012-11-15 01:30:54 -08:00
|
|
|
anchorY = 0;
|
|
|
|
break;
|
2014-04-25 19:34:07 -07:00
|
|
|
case TextBaseline::BOTTOM:
|
2012-11-15 01:30:54 -08:00
|
|
|
anchorY = -fontMetrics.emDescent;
|
|
|
|
break;
|
|
|
|
default:
|
2013-06-28 18:38:32 -07:00
|
|
|
MOZ_CRASH("unexpected TextBaseline");
|
2012-11-15 01:30:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
processor.mPt.y += anchorY;
|
|
|
|
|
|
|
|
// correct bounding box to get it to be the correct size/position
|
|
|
|
processor.mBoundingBox.width = totalWidth;
|
|
|
|
processor.mBoundingBox.MoveBy(processor.mPt);
|
|
|
|
|
|
|
|
processor.mPt.x *= processor.mAppUnitsPerDevPixel;
|
|
|
|
processor.mPt.y *= processor.mAppUnitsPerDevPixel;
|
|
|
|
|
|
|
|
EnsureTarget();
|
|
|
|
Matrix oldTransform = mTarget->GetTransform();
|
|
|
|
// if text is over aMaxWidth, then scale the text horizontally such that its
|
|
|
|
// width is precisely aMaxWidth
|
|
|
|
if (aMaxWidth.WasPassed() && aMaxWidth.Value() > 0 &&
|
|
|
|
totalWidth > aMaxWidth.Value()) {
|
|
|
|
Matrix newTransform = oldTransform;
|
|
|
|
|
|
|
|
// Translate so that the anchor point is at 0,0, then scale and then
|
|
|
|
// translate back.
|
2014-09-10 10:29:35 -07:00
|
|
|
newTransform.PreTranslate(aX, 0);
|
|
|
|
newTransform.PreScale(aMaxWidth.Value() / totalWidth, 1);
|
|
|
|
newTransform.PreTranslate(-aX, 0);
|
2012-11-15 01:30:54 -08:00
|
|
|
/* we do this to avoid an ICE in the android compiler */
|
|
|
|
Matrix androidCompilerBug = newTransform;
|
|
|
|
mTarget->SetTransform(androidCompilerBug);
|
|
|
|
}
|
|
|
|
|
|
|
|
// save the previous bounding box
|
|
|
|
gfxRect boundingBox = processor.mBoundingBox;
|
|
|
|
|
|
|
|
// don't ever need to measure the bounding box twice
|
|
|
|
processor.mDoMeasureBoundingBox = false;
|
|
|
|
|
|
|
|
rv = nsBidiPresUtils::ProcessText(textToDraw.get(),
|
|
|
|
textToDraw.Length(),
|
|
|
|
isRTL ? NSBIDI_RTL : NSBIDI_LTR,
|
|
|
|
presShell->GetPresContext(),
|
|
|
|
processor,
|
|
|
|
nsBidiPresUtils::MODE_DRAW,
|
|
|
|
nullptr,
|
|
|
|
0,
|
|
|
|
nullptr,
|
|
|
|
&bidiEngine);
|
|
|
|
|
|
|
|
|
|
|
|
mTarget->SetTransform(oldTransform);
|
|
|
|
|
2014-04-25 19:34:07 -07:00
|
|
|
if (aOp == CanvasRenderingContext2D::TextDrawOperation::FILL &&
|
2012-11-15 01:30:54 -08:00
|
|
|
!doDrawShadow) {
|
|
|
|
RedrawUser(boundingBox);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
Redraw();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
gfxFontGroup *CanvasRenderingContext2D::GetCurrentFontStyle()
|
|
|
|
{
|
|
|
|
// use lazy initilization for the font group since it's rather expensive
|
|
|
|
if (!CurrentState().fontGroup) {
|
|
|
|
ErrorResult err;
|
2013-05-30 15:34:53 -07:00
|
|
|
NS_NAMED_LITERAL_STRING(kDefaultFontStyle, "10px sans-serif");
|
|
|
|
static float kDefaultFontSize = 10.0;
|
2012-11-15 01:30:54 -08:00
|
|
|
SetFont(kDefaultFontStyle, err);
|
|
|
|
if (err.Failed()) {
|
|
|
|
gfxFontStyle style;
|
|
|
|
style.size = kDefaultFontSize;
|
|
|
|
CurrentState().fontGroup =
|
2014-06-05 23:09:23 -07:00
|
|
|
gfxPlatform::GetPlatform()->CreateFontGroup(FontFamilyList(eFamily_sans_serif),
|
2012-11-15 01:30:54 -08:00
|
|
|
&style,
|
|
|
|
nullptr);
|
|
|
|
if (CurrentState().fontGroup) {
|
|
|
|
CurrentState().font = kDefaultFontStyle;
|
2013-11-24 20:59:56 -08:00
|
|
|
|
|
|
|
nsIPresShell* presShell = GetPresShell();
|
|
|
|
if (presShell) {
|
|
|
|
CurrentState().fontGroup->SetTextPerfMetrics(
|
|
|
|
presShell->GetPresContext()->GetTextPerfMetrics());
|
|
|
|
}
|
2012-11-15 01:30:54 -08:00
|
|
|
} else {
|
|
|
|
NS_ERROR("Default canvas font is invalid");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return CurrentState().fontGroup;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// line caps/joins
|
|
|
|
//
|
|
|
|
|
|
|
|
void
|
|
|
|
CanvasRenderingContext2D::SetLineCap(const nsAString& capstyle)
|
|
|
|
{
|
|
|
|
CapStyle cap;
|
|
|
|
|
|
|
|
if (capstyle.EqualsLiteral("butt")) {
|
2014-01-10 11:06:17 -08:00
|
|
|
cap = CapStyle::BUTT;
|
2012-11-15 01:30:54 -08:00
|
|
|
} else if (capstyle.EqualsLiteral("round")) {
|
2014-01-10 11:06:17 -08:00
|
|
|
cap = CapStyle::ROUND;
|
2012-11-15 01:30:54 -08:00
|
|
|
} else if (capstyle.EqualsLiteral("square")) {
|
2014-01-10 11:06:17 -08:00
|
|
|
cap = CapStyle::SQUARE;
|
2012-11-15 01:30:54 -08:00
|
|
|
} else {
|
|
|
|
// XXX ERRMSG we need to report an error to developers here! (bug 329026)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
CurrentState().lineCap = cap;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CanvasRenderingContext2D::GetLineCap(nsAString& capstyle)
|
|
|
|
{
|
|
|
|
switch (CurrentState().lineCap) {
|
2014-01-10 11:06:17 -08:00
|
|
|
case CapStyle::BUTT:
|
2012-11-15 01:30:54 -08:00
|
|
|
capstyle.AssignLiteral("butt");
|
|
|
|
break;
|
2014-01-10 11:06:17 -08:00
|
|
|
case CapStyle::ROUND:
|
2012-11-15 01:30:54 -08:00
|
|
|
capstyle.AssignLiteral("round");
|
|
|
|
break;
|
2014-01-10 11:06:17 -08:00
|
|
|
case CapStyle::SQUARE:
|
2012-11-15 01:30:54 -08:00
|
|
|
capstyle.AssignLiteral("square");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CanvasRenderingContext2D::SetLineJoin(const nsAString& joinstyle)
|
|
|
|
{
|
|
|
|
JoinStyle j;
|
|
|
|
|
|
|
|
if (joinstyle.EqualsLiteral("round")) {
|
2014-01-10 11:06:17 -08:00
|
|
|
j = JoinStyle::ROUND;
|
2012-11-15 01:30:54 -08:00
|
|
|
} else if (joinstyle.EqualsLiteral("bevel")) {
|
2014-01-10 11:06:17 -08:00
|
|
|
j = JoinStyle::BEVEL;
|
2012-11-15 01:30:54 -08:00
|
|
|
} else if (joinstyle.EqualsLiteral("miter")) {
|
2014-01-10 11:06:17 -08:00
|
|
|
j = JoinStyle::MITER_OR_BEVEL;
|
2012-11-15 01:30:54 -08:00
|
|
|
} else {
|
|
|
|
// XXX ERRMSG we need to report an error to developers here! (bug 329026)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
CurrentState().lineJoin = j;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CanvasRenderingContext2D::GetLineJoin(nsAString& joinstyle, ErrorResult& error)
|
|
|
|
{
|
|
|
|
switch (CurrentState().lineJoin) {
|
2014-01-10 11:06:17 -08:00
|
|
|
case JoinStyle::ROUND:
|
2012-11-15 01:30:54 -08:00
|
|
|
joinstyle.AssignLiteral("round");
|
|
|
|
break;
|
2014-01-10 11:06:17 -08:00
|
|
|
case JoinStyle::BEVEL:
|
2012-11-15 01:30:54 -08:00
|
|
|
joinstyle.AssignLiteral("bevel");
|
|
|
|
break;
|
2014-01-10 11:06:17 -08:00
|
|
|
case JoinStyle::MITER_OR_BEVEL:
|
2012-11-15 01:30:54 -08:00
|
|
|
joinstyle.AssignLiteral("miter");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
error.Throw(NS_ERROR_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CanvasRenderingContext2D::SetMozDash(JSContext* cx,
|
|
|
|
const JS::Value& mozDash,
|
|
|
|
ErrorResult& error)
|
|
|
|
{
|
|
|
|
FallibleTArray<Float> dash;
|
|
|
|
error = JSValToDashArray(cx, mozDash, dash);
|
|
|
|
if (!error.Failed()) {
|
|
|
|
ContextState& state = CurrentState();
|
|
|
|
state.dash = dash;
|
|
|
|
if (state.dash.IsEmpty()) {
|
|
|
|
state.dashOffset = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-11 13:26:52 -07:00
|
|
|
void
|
|
|
|
CanvasRenderingContext2D::GetMozDash(JSContext* cx,
|
|
|
|
JS::MutableHandle<JS::Value> retval,
|
|
|
|
ErrorResult& error)
|
2012-11-15 01:30:54 -08:00
|
|
|
{
|
2014-06-11 13:26:52 -07:00
|
|
|
DashArrayToJSVal(CurrentState().dash, cx, retval, error);
|
2012-11-15 01:30:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CanvasRenderingContext2D::SetMozDashOffset(double mozDashOffset)
|
|
|
|
{
|
|
|
|
ContextState& state = CurrentState();
|
|
|
|
if (!state.dash.IsEmpty()) {
|
|
|
|
state.dashOffset = mozDashOffset;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-16 05:27:59 -07:00
|
|
|
void
|
2014-01-22 11:37:10 -08:00
|
|
|
CanvasRenderingContext2D::SetLineDash(const Sequence<double>& aSegments)
|
|
|
|
{
|
2014-05-22 17:07:24 -07:00
|
|
|
FallibleTArray<mozilla::gfx::Float> dash;
|
2013-10-16 05:27:59 -07:00
|
|
|
|
2013-10-27 17:53:45 -07:00
|
|
|
for (uint32_t x = 0; x < aSegments.Length(); x++) {
|
2014-05-22 17:07:24 -07:00
|
|
|
if (aSegments[x] < 0.0) {
|
|
|
|
// Pattern elements must be finite "numbers" >= 0, with "finite"
|
|
|
|
// taken care of by WebIDL
|
|
|
|
return;
|
|
|
|
}
|
2013-10-27 17:53:45 -07:00
|
|
|
dash.AppendElement(aSegments[x]);
|
2013-10-16 05:27:59 -07:00
|
|
|
}
|
2013-10-27 17:53:45 -07:00
|
|
|
if (aSegments.Length() % 2) { // If the number of elements is odd, concatenate again
|
|
|
|
for (uint32_t x = 0; x < aSegments.Length(); x++) {
|
|
|
|
dash.AppendElement(aSegments[x]);
|
2013-10-16 05:27:59 -07:00
|
|
|
}
|
|
|
|
}
|
2014-05-22 17:07:24 -07:00
|
|
|
|
|
|
|
CurrentState().dash = dash;
|
2013-10-16 05:27:59 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-10-27 17:53:45 -07:00
|
|
|
CanvasRenderingContext2D::GetLineDash(nsTArray<double>& aSegments) const {
|
2013-10-16 05:27:59 -07:00
|
|
|
const FallibleTArray<mozilla::gfx::Float>& dash = CurrentState().dash;
|
2013-10-27 17:53:45 -07:00
|
|
|
aSegments.Clear();
|
2013-10-16 05:27:59 -07:00
|
|
|
|
2013-10-27 17:53:45 -07:00
|
|
|
for (uint32_t x = 0; x < dash.Length(); x++) {
|
|
|
|
aSegments.AppendElement(dash[x]);
|
2013-10-16 05:27:59 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CanvasRenderingContext2D::SetLineDashOffset(double mOffset) {
|
|
|
|
CurrentState().dashOffset = mOffset;
|
|
|
|
}
|
|
|
|
|
|
|
|
double
|
|
|
|
CanvasRenderingContext2D::LineDashOffset() const {
|
|
|
|
return CurrentState().dashOffset;
|
|
|
|
}
|
|
|
|
|
2012-11-15 01:30:54 -08:00
|
|
|
bool
|
2013-01-16 18:55:43 -08:00
|
|
|
CanvasRenderingContext2D::IsPointInPath(double x, double y, const CanvasWindingRule& winding)
|
2012-11-15 01:30:54 -08:00
|
|
|
{
|
|
|
|
if (!FloatValidate(x,y)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-01-16 18:55:43 -08:00
|
|
|
EnsureUserSpacePath(winding);
|
2012-11-15 01:30:54 -08:00
|
|
|
if (!mPath) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-01-16 18:55:43 -08:00
|
|
|
|
2012-11-15 01:30:54 -08:00
|
|
|
if (mPathTransformWillUpdate) {
|
|
|
|
return mPath->ContainsPoint(Point(x, y), mPathToDS);
|
|
|
|
}
|
2013-01-16 18:55:43 -08:00
|
|
|
|
2012-11-15 01:30:54 -08:00
|
|
|
return mPath->ContainsPoint(Point(x, y), mTarget->GetTransform());
|
|
|
|
}
|
|
|
|
|
2014-03-19 10:42:38 -07:00
|
|
|
bool CanvasRenderingContext2D::IsPointInPath(const CanvasPath& mPath, double x, double y, const CanvasWindingRule& mWinding)
|
|
|
|
{
|
|
|
|
if (!FloatValidate(x,y)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
EnsureTarget();
|
|
|
|
RefPtr<gfx::Path> tempPath = mPath.GetPath(mWinding, mTarget);
|
|
|
|
|
|
|
|
return tempPath->ContainsPoint(Point(x, y), mTarget->GetTransform());
|
|
|
|
}
|
|
|
|
|
2012-11-15 01:30:54 -08:00
|
|
|
bool
|
2012-11-22 05:37:42 -08:00
|
|
|
CanvasRenderingContext2D::IsPointInStroke(double x, double y)
|
2012-11-15 01:30:54 -08:00
|
|
|
{
|
|
|
|
if (!FloatValidate(x,y)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-11-29 11:37:49 -08:00
|
|
|
EnsureUserSpacePath();
|
2012-11-15 01:30:54 -08:00
|
|
|
if (!mPath) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ContextState &state = CurrentState();
|
|
|
|
|
|
|
|
StrokeOptions strokeOptions(state.lineWidth,
|
|
|
|
state.lineJoin,
|
|
|
|
state.lineCap,
|
|
|
|
state.miterLimit,
|
|
|
|
state.dash.Length(),
|
|
|
|
state.dash.Elements(),
|
|
|
|
state.dashOffset);
|
|
|
|
|
|
|
|
if (mPathTransformWillUpdate) {
|
|
|
|
return mPath->StrokeContainsPoint(strokeOptions, Point(x, y), mPathToDS);
|
|
|
|
}
|
|
|
|
return mPath->StrokeContainsPoint(strokeOptions, Point(x, y), mTarget->GetTransform());
|
|
|
|
}
|
|
|
|
|
2014-03-19 10:42:38 -07:00
|
|
|
bool CanvasRenderingContext2D::IsPointInStroke(const CanvasPath& mPath, double x, double y)
|
|
|
|
{
|
|
|
|
if (!FloatValidate(x,y)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
EnsureTarget();
|
|
|
|
RefPtr<gfx::Path> tempPath = mPath.GetPath(CanvasWindingRule::Nonzero, mTarget);
|
|
|
|
|
|
|
|
const ContextState &state = CurrentState();
|
|
|
|
|
|
|
|
StrokeOptions strokeOptions(state.lineWidth,
|
|
|
|
state.lineJoin,
|
|
|
|
state.lineCap,
|
|
|
|
state.miterLimit,
|
|
|
|
state.dash.Length(),
|
|
|
|
state.dash.Elements(),
|
|
|
|
state.dashOffset);
|
|
|
|
|
|
|
|
return tempPath->StrokeContainsPoint(strokeOptions, Point(x, y), mTarget->GetTransform());
|
|
|
|
}
|
|
|
|
|
2012-11-15 01:30:54 -08:00
|
|
|
//
|
|
|
|
// image
|
|
|
|
//
|
|
|
|
|
|
|
|
// drawImage(in HTMLImageElement image, in float dx, in float dy);
|
|
|
|
// -- render image from 0,0 at dx,dy top-left coords
|
|
|
|
// drawImage(in HTMLImageElement image, in float dx, in float dy, in float sw, in float sh);
|
|
|
|
// -- render image from 0,0 at dx,dy top-left coords clipping it to sw,sh
|
|
|
|
// drawImage(in HTMLImageElement image, in float sx, in float sy, in float sw, in float sh, in float dx, in float dy, in float dw, in float dh);
|
|
|
|
// -- render the region defined by (sx,sy,sw,wh) in image-local space into the region (dx,dy,dw,dh) on the canvas
|
|
|
|
|
|
|
|
// If only dx and dy are passed in then optional_argc should be 0. If only
|
|
|
|
// dx, dy, dw and dh are passed in then optional_argc should be 2. The only
|
|
|
|
// other valid value for optional_argc is 6 if sx, sy, sw, sh, dx, dy, dw and dh
|
|
|
|
// are all passed in.
|
|
|
|
|
|
|
|
void
|
|
|
|
CanvasRenderingContext2D::DrawImage(const HTMLImageOrCanvasOrVideoElement& image,
|
|
|
|
double sx, double sy, double sw,
|
|
|
|
double sh, double dx, double dy,
|
|
|
|
double dw, double dh,
|
|
|
|
uint8_t optional_argc,
|
|
|
|
ErrorResult& error)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(optional_argc == 0 || optional_argc == 2 || optional_argc == 6);
|
|
|
|
|
|
|
|
RefPtr<SourceSurface> srcSurf;
|
|
|
|
gfxIntSize imgSize;
|
|
|
|
|
|
|
|
Element* element;
|
|
|
|
|
|
|
|
EnsureTarget();
|
|
|
|
if (image.IsHTMLCanvasElement()) {
|
2013-04-13 00:05:51 -07:00
|
|
|
HTMLCanvasElement* canvas = &image.GetAsHTMLCanvasElement();
|
2012-11-15 01:30:54 -08:00
|
|
|
element = canvas;
|
2014-04-16 23:43:35 -07:00
|
|
|
nsIntSize size = canvas->GetSize();
|
|
|
|
if (size.width == 0 || size.height == 0) {
|
|
|
|
error.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
|
|
|
|
return;
|
|
|
|
}
|
2012-11-15 01:30:54 -08:00
|
|
|
} else {
|
|
|
|
if (image.IsHTMLImageElement()) {
|
2013-01-02 09:28:14 -08:00
|
|
|
HTMLImageElement* img = &image.GetAsHTMLImageElement();
|
2012-11-15 01:30:54 -08:00
|
|
|
element = img;
|
|
|
|
} else {
|
2013-03-19 05:28:34 -07:00
|
|
|
HTMLVideoElement* video = &image.GetAsHTMLVideoElement();
|
2012-11-15 01:30:54 -08:00
|
|
|
element = video;
|
|
|
|
}
|
|
|
|
|
2013-12-11 13:05:27 -08:00
|
|
|
srcSurf =
|
2012-11-15 01:30:54 -08:00
|
|
|
CanvasImageCache::Lookup(element, mCanvasElement, &imgSize);
|
|
|
|
}
|
|
|
|
|
2014-01-12 16:29:48 -08:00
|
|
|
nsLayoutUtils::DirectDrawInfo drawInfo;
|
|
|
|
|
2012-11-15 01:30:54 -08:00
|
|
|
if (!srcSurf) {
|
|
|
|
// The canvas spec says that drawImage should draw the first frame
|
2014-01-12 16:29:48 -08:00
|
|
|
// of animated images. We also don't want to rasterize vector images.
|
|
|
|
uint32_t sfeFlags = nsLayoutUtils::SFE_WANT_FIRST_FRAME |
|
|
|
|
nsLayoutUtils::SFE_NO_RASTERIZING_VECTORS;
|
2012-11-15 01:30:54 -08:00
|
|
|
nsLayoutUtils::SurfaceFromElementResult res =
|
2013-12-11 13:05:27 -08:00
|
|
|
nsLayoutUtils::SurfaceFromElement(element, sfeFlags, mTarget);
|
2012-11-15 01:30:54 -08:00
|
|
|
|
2014-01-12 16:29:48 -08:00
|
|
|
if (!res.mSourceSurface && !res.mDrawInfo.mImgContainer) {
|
2012-11-15 01:30:54 -08:00
|
|
|
// Spec says to silently do nothing if the element is still loading.
|
|
|
|
if (!res.mIsStillLoading) {
|
|
|
|
error.Throw(NS_ERROR_NOT_AVAILABLE);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
imgSize = res.mSize;
|
|
|
|
|
2013-11-17 21:26:46 -08:00
|
|
|
// Scale sw/sh based on aspect ratio
|
|
|
|
if (image.IsHTMLVideoElement()) {
|
|
|
|
HTMLVideoElement* video = &image.GetAsHTMLVideoElement();
|
|
|
|
int32_t displayWidth = video->VideoWidth();
|
|
|
|
int32_t displayHeight = video->VideoHeight();
|
|
|
|
sw *= (double)imgSize.width / (double)displayWidth;
|
|
|
|
sh *= (double)imgSize.height / (double)displayHeight;
|
|
|
|
}
|
|
|
|
|
2012-11-15 01:30:54 -08:00
|
|
|
if (mCanvasElement) {
|
|
|
|
CanvasUtils::DoDrawImageSecurityCheck(mCanvasElement,
|
|
|
|
res.mPrincipal, res.mIsWriteOnly,
|
|
|
|
res.mCORSUsed);
|
|
|
|
}
|
|
|
|
|
2014-01-12 16:29:48 -08:00
|
|
|
if (res.mSourceSurface) {
|
|
|
|
if (res.mImageRequest) {
|
|
|
|
CanvasImageCache::NotifyDrawImage(element, mCanvasElement, res.mImageRequest,
|
|
|
|
res.mSourceSurface, imgSize);
|
|
|
|
}
|
2013-07-15 12:50:26 -07:00
|
|
|
|
2014-01-12 16:29:48 -08:00
|
|
|
srcSurf = res.mSourceSurface;
|
|
|
|
} else {
|
|
|
|
drawInfo = res.mDrawInfo;
|
|
|
|
}
|
2012-11-15 01:30:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (optional_argc == 0) {
|
|
|
|
sx = sy = 0.0;
|
|
|
|
dw = sw = (double) imgSize.width;
|
|
|
|
dh = sh = (double) imgSize.height;
|
|
|
|
} else if (optional_argc == 2) {
|
|
|
|
sx = sy = 0.0;
|
|
|
|
sw = (double) imgSize.width;
|
|
|
|
sh = (double) imgSize.height;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sw == 0.0 || sh == 0.0) {
|
|
|
|
error.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dw == 0.0 || dh == 0.0) {
|
|
|
|
// not really failure, but nothing to do --
|
|
|
|
// and noone likes a divide-by-zero
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sx < 0.0 || sy < 0.0 ||
|
|
|
|
sw < 0.0 || sw > (double) imgSize.width ||
|
|
|
|
sh < 0.0 || sh > (double) imgSize.height ||
|
|
|
|
dw < 0.0 || dh < 0.0) {
|
|
|
|
// XXX - Unresolved spec issues here, for now return error.
|
|
|
|
error.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Filter filter;
|
|
|
|
|
|
|
|
if (CurrentState().imageSmoothingEnabled)
|
2014-01-10 11:06:17 -08:00
|
|
|
filter = mgfx::Filter::LINEAR;
|
2012-11-15 01:30:54 -08:00
|
|
|
else
|
2014-01-10 11:06:17 -08:00
|
|
|
filter = mgfx::Filter::POINT;
|
2012-11-15 01:30:54 -08:00
|
|
|
|
|
|
|
mgfx::Rect bounds;
|
|
|
|
|
|
|
|
if (NeedToDrawShadow()) {
|
|
|
|
bounds = mgfx::Rect(dx, dy, dw, dh);
|
|
|
|
bounds = mTarget->GetTransform().TransformBounds(bounds);
|
|
|
|
}
|
|
|
|
|
2014-01-12 16:29:48 -08:00
|
|
|
if (srcSurf) {
|
|
|
|
AdjustedTarget(this, bounds.IsEmpty() ? nullptr : &bounds)->
|
|
|
|
DrawSurface(srcSurf,
|
|
|
|
mgfx::Rect(dx, dy, dw, dh),
|
|
|
|
mgfx::Rect(sx, sy, sw, sh),
|
|
|
|
DrawSurfaceOptions(filter),
|
|
|
|
DrawOptions(CurrentState().globalAlpha, UsedOperation()));
|
|
|
|
} else {
|
2014-08-22 13:12:38 -07:00
|
|
|
DrawDirectlyToCanvas(drawInfo, &bounds,
|
|
|
|
mgfx::Rect(dx, dy, dw, dh),
|
|
|
|
mgfx::Rect(sx, sy, sw, sh),
|
|
|
|
imgSize);
|
2014-01-12 16:29:48 -08:00
|
|
|
}
|
2012-11-15 01:30:54 -08:00
|
|
|
|
|
|
|
RedrawUser(gfxRect(dx, dy, dw, dh));
|
|
|
|
}
|
|
|
|
|
2014-01-12 16:29:48 -08:00
|
|
|
void
|
|
|
|
CanvasRenderingContext2D::DrawDirectlyToCanvas(
|
|
|
|
const nsLayoutUtils::DirectDrawInfo& image,
|
2014-08-22 13:12:38 -07:00
|
|
|
mgfx::Rect* bounds,
|
|
|
|
mgfx::Rect dest,
|
|
|
|
mgfx::Rect src,
|
|
|
|
gfxIntSize imgSize)
|
2014-01-12 16:29:48 -08:00
|
|
|
{
|
2014-08-22 13:12:38 -07:00
|
|
|
MOZ_ASSERT(src.width > 0 && src.height > 0,
|
|
|
|
"Need positive source width and height");
|
2014-01-12 16:29:48 -08:00
|
|
|
|
2014-08-22 13:12:38 -07:00
|
|
|
gfxMatrix contextMatrix;
|
2014-01-12 16:29:48 -08:00
|
|
|
AdjustedTarget tempTarget(this, bounds->IsEmpty() ? nullptr: bounds);
|
|
|
|
|
2014-08-22 13:12:38 -07:00
|
|
|
// Get any existing transforms on the context, including transformations used
|
|
|
|
// for context shadow.
|
2014-01-12 16:29:48 -08:00
|
|
|
if (tempTarget) {
|
|
|
|
Matrix matrix = tempTarget->GetTransform();
|
|
|
|
contextMatrix = gfxMatrix(matrix._11, matrix._12, matrix._21,
|
|
|
|
matrix._22, matrix._31, matrix._32);
|
|
|
|
}
|
2014-08-22 13:12:38 -07:00
|
|
|
gfxSize contextScale(contextMatrix.ScaleFactors(true));
|
2014-01-12 16:29:48 -08:00
|
|
|
|
2014-08-22 13:12:38 -07:00
|
|
|
// Scale the dest rect to include the context scale.
|
|
|
|
dest.Scale(contextScale.width, contextScale.height);
|
|
|
|
|
|
|
|
// Scale the image size to the dest rect, and adjust the source rect to match.
|
|
|
|
gfxSize scale(dest.width / src.width, dest.height / src.height);
|
|
|
|
nsIntSize scaledImageSize(std::ceil(imgSize.width * scale.width),
|
|
|
|
std::ceil(imgSize.height * scale.height));
|
|
|
|
src.Scale(scale.width, scale.height);
|
2014-01-12 16:29:48 -08:00
|
|
|
|
|
|
|
nsRefPtr<gfxContext> context = new gfxContext(tempTarget);
|
|
|
|
context->SetMatrix(contextMatrix);
|
2014-08-22 13:12:38 -07:00
|
|
|
context->Scale(1.0 / contextScale.width, 1.0 / contextScale.height);
|
|
|
|
context->Translate(gfxPoint(dest.x - src.x, dest.y - src.y));
|
2014-07-11 13:45:22 -07:00
|
|
|
|
2014-08-22 13:12:38 -07:00
|
|
|
// FLAG_CLAMP is added for increased performance, since we never tile here.
|
2014-01-12 16:29:48 -08:00
|
|
|
uint32_t modifiedFlags = image.mDrawingFlags | imgIContainer::FLAG_CLAMP;
|
|
|
|
|
2014-08-29 07:04:34 -07:00
|
|
|
SVGImageContext svgContext(scaledImageSize, Nothing(), CurrentState().globalAlpha);
|
|
|
|
|
2014-01-12 16:29:48 -08:00
|
|
|
nsresult rv = image.mImgContainer->
|
2014-08-22 13:12:38 -07:00
|
|
|
Draw(context, scaledImageSize,
|
|
|
|
ImageRegion::Create(gfxRect(src.x, src.y, src.width, src.height)),
|
|
|
|
image.mWhichFrame, GraphicsFilter::FILTER_GOOD,
|
2014-08-29 07:04:34 -07:00
|
|
|
Some(svgContext), modifiedFlags);
|
2014-01-12 16:29:48 -08:00
|
|
|
|
|
|
|
NS_ENSURE_SUCCESS_VOID(rv);
|
|
|
|
}
|
|
|
|
|
2012-11-15 01:30:54 -08:00
|
|
|
void
|
|
|
|
CanvasRenderingContext2D::SetGlobalCompositeOperation(const nsAString& op,
|
|
|
|
ErrorResult& error)
|
|
|
|
{
|
|
|
|
CompositionOp comp_op;
|
|
|
|
|
|
|
|
#define CANVAS_OP_TO_GFX_OP(cvsop, op2d) \
|
|
|
|
if (op.EqualsLiteral(cvsop)) \
|
2014-01-10 11:06:17 -08:00
|
|
|
comp_op = CompositionOp::OP_##op2d;
|
2012-11-15 01:30:54 -08:00
|
|
|
|
|
|
|
CANVAS_OP_TO_GFX_OP("copy", SOURCE)
|
|
|
|
else CANVAS_OP_TO_GFX_OP("source-atop", ATOP)
|
|
|
|
else CANVAS_OP_TO_GFX_OP("source-in", IN)
|
|
|
|
else CANVAS_OP_TO_GFX_OP("source-out", OUT)
|
|
|
|
else CANVAS_OP_TO_GFX_OP("source-over", OVER)
|
|
|
|
else CANVAS_OP_TO_GFX_OP("destination-in", DEST_IN)
|
|
|
|
else CANVAS_OP_TO_GFX_OP("destination-out", DEST_OUT)
|
|
|
|
else CANVAS_OP_TO_GFX_OP("destination-over", DEST_OVER)
|
|
|
|
else CANVAS_OP_TO_GFX_OP("destination-atop", DEST_ATOP)
|
|
|
|
else CANVAS_OP_TO_GFX_OP("lighter", ADD)
|
|
|
|
else CANVAS_OP_TO_GFX_OP("xor", XOR)
|
2012-12-12 12:28:17 -08:00
|
|
|
else CANVAS_OP_TO_GFX_OP("multiply", MULTIPLY)
|
|
|
|
else CANVAS_OP_TO_GFX_OP("screen", SCREEN)
|
|
|
|
else CANVAS_OP_TO_GFX_OP("overlay", OVERLAY)
|
|
|
|
else CANVAS_OP_TO_GFX_OP("darken", DARKEN)
|
|
|
|
else CANVAS_OP_TO_GFX_OP("lighten", LIGHTEN)
|
|
|
|
else CANVAS_OP_TO_GFX_OP("color-dodge", COLOR_DODGE)
|
|
|
|
else CANVAS_OP_TO_GFX_OP("color-burn", COLOR_BURN)
|
|
|
|
else CANVAS_OP_TO_GFX_OP("hard-light", HARD_LIGHT)
|
|
|
|
else CANVAS_OP_TO_GFX_OP("soft-light", SOFT_LIGHT)
|
|
|
|
else CANVAS_OP_TO_GFX_OP("difference", DIFFERENCE)
|
|
|
|
else CANVAS_OP_TO_GFX_OP("exclusion", EXCLUSION)
|
|
|
|
else CANVAS_OP_TO_GFX_OP("hue", HUE)
|
|
|
|
else CANVAS_OP_TO_GFX_OP("saturation", SATURATION)
|
|
|
|
else CANVAS_OP_TO_GFX_OP("color", COLOR)
|
|
|
|
else CANVAS_OP_TO_GFX_OP("luminosity", LUMINOSITY)
|
2012-11-15 01:30:54 -08:00
|
|
|
// XXX ERRMSG we need to report an error to developers here! (bug 329026)
|
|
|
|
else return;
|
|
|
|
|
|
|
|
#undef CANVAS_OP_TO_GFX_OP
|
|
|
|
CurrentState().op = comp_op;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CanvasRenderingContext2D::GetGlobalCompositeOperation(nsAString& op,
|
|
|
|
ErrorResult& error)
|
|
|
|
{
|
|
|
|
CompositionOp comp_op = CurrentState().op;
|
|
|
|
|
|
|
|
#define CANVAS_OP_TO_GFX_OP(cvsop, op2d) \
|
2014-01-10 11:06:17 -08:00
|
|
|
if (comp_op == CompositionOp::OP_##op2d) \
|
2012-11-15 01:30:54 -08:00
|
|
|
op.AssignLiteral(cvsop);
|
|
|
|
|
|
|
|
CANVAS_OP_TO_GFX_OP("copy", SOURCE)
|
|
|
|
else CANVAS_OP_TO_GFX_OP("destination-atop", DEST_ATOP)
|
|
|
|
else CANVAS_OP_TO_GFX_OP("destination-in", DEST_IN)
|
|
|
|
else CANVAS_OP_TO_GFX_OP("destination-out", DEST_OUT)
|
|
|
|
else CANVAS_OP_TO_GFX_OP("destination-over", DEST_OVER)
|
|
|
|
else CANVAS_OP_TO_GFX_OP("lighter", ADD)
|
|
|
|
else CANVAS_OP_TO_GFX_OP("source-atop", ATOP)
|
|
|
|
else CANVAS_OP_TO_GFX_OP("source-in", IN)
|
|
|
|
else CANVAS_OP_TO_GFX_OP("source-out", OUT)
|
|
|
|
else CANVAS_OP_TO_GFX_OP("source-over", OVER)
|
|
|
|
else CANVAS_OP_TO_GFX_OP("xor", XOR)
|
2012-12-12 12:28:17 -08:00
|
|
|
else CANVAS_OP_TO_GFX_OP("multiply", MULTIPLY)
|
|
|
|
else CANVAS_OP_TO_GFX_OP("screen", SCREEN)
|
|
|
|
else CANVAS_OP_TO_GFX_OP("overlay", OVERLAY)
|
|
|
|
else CANVAS_OP_TO_GFX_OP("darken", DARKEN)
|
|
|
|
else CANVAS_OP_TO_GFX_OP("lighten", LIGHTEN)
|
|
|
|
else CANVAS_OP_TO_GFX_OP("color-dodge", COLOR_DODGE)
|
|
|
|
else CANVAS_OP_TO_GFX_OP("color-burn", COLOR_BURN)
|
|
|
|
else CANVAS_OP_TO_GFX_OP("hard-light", HARD_LIGHT)
|
|
|
|
else CANVAS_OP_TO_GFX_OP("soft-light", SOFT_LIGHT)
|
|
|
|
else CANVAS_OP_TO_GFX_OP("difference", DIFFERENCE)
|
|
|
|
else CANVAS_OP_TO_GFX_OP("exclusion", EXCLUSION)
|
|
|
|
else CANVAS_OP_TO_GFX_OP("hue", HUE)
|
|
|
|
else CANVAS_OP_TO_GFX_OP("saturation", SATURATION)
|
|
|
|
else CANVAS_OP_TO_GFX_OP("color", COLOR)
|
|
|
|
else CANVAS_OP_TO_GFX_OP("luminosity", LUMINOSITY)
|
2012-11-15 01:30:54 -08:00
|
|
|
else {
|
|
|
|
error.Throw(NS_ERROR_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef CANVAS_OP_TO_GFX_OP
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-10-08 08:51:42 -07:00
|
|
|
CanvasRenderingContext2D::DrawWindow(nsGlobalWindow& window, double x,
|
2012-11-15 01:30:54 -08:00
|
|
|
double y, double w, double h,
|
|
|
|
const nsAString& bgColor,
|
|
|
|
uint32_t flags, ErrorResult& error)
|
|
|
|
{
|
|
|
|
// protect against too-large surfaces that will cause allocation
|
|
|
|
// or overflow issues
|
|
|
|
if (!gfxASurface::CheckSurfaceSize(gfxIntSize(int32_t(w), int32_t(h)),
|
|
|
|
0xffff)) {
|
|
|
|
error.Throw(NS_ERROR_FAILURE);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
EnsureTarget();
|
|
|
|
// We can't allow web apps to call this until we fix at least the
|
|
|
|
// following potential security issues:
|
|
|
|
// -- rendering cross-domain IFRAMEs and then extracting the results
|
|
|
|
// -- rendering the user's theme and then extracting the results
|
|
|
|
// -- rendering native anonymous content (e.g., file input paths;
|
|
|
|
// scrollbars should be allowed)
|
|
|
|
if (!nsContentUtils::IsCallerChrome()) {
|
|
|
|
// not permitted to use DrawWindow
|
|
|
|
// XXX ERRMSG we need to report an error to developers here! (bug 329026)
|
|
|
|
error.Throw(NS_ERROR_DOM_SECURITY_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Flush layout updates
|
|
|
|
if (!(flags & nsIDOMCanvasRenderingContext2D::DRAWWINDOW_DO_NOT_FLUSH)) {
|
2013-10-08 08:51:42 -07:00
|
|
|
nsContentUtils::FlushLayoutForTree(&window);
|
2012-11-15 01:30:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<nsPresContext> presContext;
|
2013-10-08 08:51:42 -07:00
|
|
|
nsIDocShell* docshell = window.GetDocShell();
|
|
|
|
if (docshell) {
|
|
|
|
docshell->GetPresContext(getter_AddRefs(presContext));
|
2012-11-15 01:30:54 -08:00
|
|
|
}
|
|
|
|
if (!presContext) {
|
|
|
|
error.Throw(NS_ERROR_FAILURE);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nscolor backgroundColor;
|
|
|
|
if (!ParseColor(bgColor, &backgroundColor)) {
|
|
|
|
error.Throw(NS_ERROR_FAILURE);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRect r(nsPresContext::CSSPixelsToAppUnits((float)x),
|
|
|
|
nsPresContext::CSSPixelsToAppUnits((float)y),
|
|
|
|
nsPresContext::CSSPixelsToAppUnits((float)w),
|
|
|
|
nsPresContext::CSSPixelsToAppUnits((float)h));
|
|
|
|
uint32_t renderDocFlags = (nsIPresShell::RENDER_IGNORE_VIEWPORT_SCROLLING |
|
|
|
|
nsIPresShell::RENDER_DOCUMENT_RELATIVE);
|
|
|
|
if (flags & nsIDOMCanvasRenderingContext2D::DRAWWINDOW_DRAW_CARET) {
|
|
|
|
renderDocFlags |= nsIPresShell::RENDER_CARET;
|
|
|
|
}
|
|
|
|
if (flags & nsIDOMCanvasRenderingContext2D::DRAWWINDOW_DRAW_VIEW) {
|
|
|
|
renderDocFlags &= ~(nsIPresShell::RENDER_IGNORE_VIEWPORT_SCROLLING |
|
|
|
|
nsIPresShell::RENDER_DOCUMENT_RELATIVE);
|
|
|
|
}
|
|
|
|
if (flags & nsIDOMCanvasRenderingContext2D::DRAWWINDOW_USE_WIDGET_LAYERS) {
|
|
|
|
renderDocFlags |= nsIPresShell::RENDER_USE_WIDGET_LAYERS;
|
|
|
|
}
|
|
|
|
if (flags & nsIDOMCanvasRenderingContext2D::DRAWWINDOW_ASYNC_DECODE_IMAGES) {
|
|
|
|
renderDocFlags |= nsIPresShell::RENDER_ASYNC_DECODE_IMAGES;
|
|
|
|
}
|
2013-06-24 23:01:38 -07:00
|
|
|
if (flags & nsIDOMCanvasRenderingContext2D::DRAWWINDOW_DO_NOT_FLUSH) {
|
|
|
|
renderDocFlags |= nsIPresShell::RENDER_DRAWWINDOW_NOT_FLUSHING;
|
|
|
|
}
|
2012-11-15 01:30:54 -08:00
|
|
|
|
|
|
|
// gfxContext-over-Azure may modify the DrawTarget's transform, so
|
|
|
|
// save and restore it
|
|
|
|
Matrix matrix = mTarget->GetTransform();
|
2013-07-15 21:31:00 -07:00
|
|
|
double sw = matrix._11 * w;
|
|
|
|
double sh = matrix._22 * h;
|
2013-11-03 17:47:17 -08:00
|
|
|
if (!sw || !sh) {
|
|
|
|
return;
|
|
|
|
}
|
2012-11-15 01:30:54 -08:00
|
|
|
nsRefPtr<gfxContext> thebes;
|
2013-10-14 20:23:20 -07:00
|
|
|
RefPtr<DrawTarget> drawDT;
|
2014-07-02 10:27:25 -07:00
|
|
|
// Rendering directly is faster and can be done if mTarget supports Azure
|
|
|
|
// and does not need alpha blending.
|
|
|
|
if (gfxPlatform::GetPlatform()->SupportsAzureContentForDrawTarget(mTarget) &&
|
|
|
|
GlobalAlpha() == 1.0f)
|
|
|
|
{
|
2012-11-15 01:30:54 -08:00
|
|
|
thebes = new gfxContext(mTarget);
|
2013-06-06 07:58:45 -07:00
|
|
|
thebes->SetMatrix(gfxMatrix(matrix._11, matrix._12, matrix._21,
|
|
|
|
matrix._22, matrix._31, matrix._32));
|
2014-03-18 19:36:58 -07:00
|
|
|
} else {
|
2013-10-14 20:23:20 -07:00
|
|
|
drawDT =
|
|
|
|
gfxPlatform::GetPlatform()->CreateOffscreenContentDrawTarget(IntSize(ceil(sw), ceil(sh)),
|
2014-01-10 11:06:16 -08:00
|
|
|
SurfaceFormat::B8G8R8A8);
|
2013-10-14 20:23:20 -07:00
|
|
|
if (!drawDT) {
|
|
|
|
error.Throw(NS_ERROR_FAILURE);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
thebes = new gfxContext(drawDT);
|
|
|
|
thebes->Scale(matrix._11, matrix._22);
|
2012-11-15 01:30:54 -08:00
|
|
|
}
|
2013-06-06 07:58:45 -07:00
|
|
|
|
2012-11-15 01:30:54 -08:00
|
|
|
nsCOMPtr<nsIPresShell> shell = presContext->PresShell();
|
|
|
|
unused << shell->RenderDocument(r, renderDocFlags, backgroundColor, thebes);
|
2014-04-14 11:11:59 -07:00
|
|
|
if (drawDT) {
|
|
|
|
RefPtr<SourceSurface> snapshot = drawDT->Snapshot();
|
|
|
|
RefPtr<DataSourceSurface> data = snapshot->GetDataSurface();
|
|
|
|
|
|
|
|
RefPtr<SourceSurface> source =
|
|
|
|
mTarget->CreateSourceSurfaceFromData(data->GetData(),
|
|
|
|
data->GetSize(),
|
|
|
|
data->Stride(),
|
|
|
|
data->GetFormat());
|
2013-11-03 17:47:17 -08:00
|
|
|
|
|
|
|
if (!source) {
|
|
|
|
error.Throw(NS_ERROR_FAILURE);
|
|
|
|
return;
|
|
|
|
}
|
2013-06-12 06:03:52 -07:00
|
|
|
|
2013-07-15 21:31:00 -07:00
|
|
|
mgfx::Rect destRect(0, 0, w, h);
|
|
|
|
mgfx::Rect sourceRect(0, 0, sw, sh);
|
2013-10-14 20:23:20 -07:00
|
|
|
mTarget->DrawSurface(source, destRect, sourceRect,
|
2014-01-10 11:06:17 -08:00
|
|
|
DrawSurfaceOptions(mgfx::Filter::POINT),
|
2014-07-02 10:27:25 -07:00
|
|
|
DrawOptions(GlobalAlpha(), CompositionOp::OP_OVER,
|
2014-04-14 11:12:00 -07:00
|
|
|
AntialiasMode::NONE));
|
2013-06-06 07:58:45 -07:00
|
|
|
mTarget->Flush();
|
|
|
|
} else {
|
|
|
|
mTarget->SetTransform(matrix);
|
|
|
|
}
|
2012-11-15 01:30:54 -08:00
|
|
|
|
|
|
|
// note that x and y are coordinates in the document that
|
|
|
|
// we're drawing; x and y are drawn to 0,0 in current user
|
|
|
|
// space.
|
|
|
|
RedrawUser(gfxRect(0, 0, w, h));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-01-29 09:51:55 -08:00
|
|
|
CanvasRenderingContext2D::AsyncDrawXULElement(nsXULElement& elem,
|
2012-11-15 01:30:54 -08:00
|
|
|
double x, double y,
|
|
|
|
double w, double h,
|
|
|
|
const nsAString& bgColor,
|
|
|
|
uint32_t flags,
|
|
|
|
ErrorResult& error)
|
|
|
|
{
|
|
|
|
// We can't allow web apps to call this until we fix at least the
|
|
|
|
// following potential security issues:
|
|
|
|
// -- rendering cross-domain IFRAMEs and then extracting the results
|
|
|
|
// -- rendering the user's theme and then extracting the results
|
|
|
|
// -- rendering native anonymous content (e.g., file input paths;
|
|
|
|
// scrollbars should be allowed)
|
|
|
|
if (!nsContentUtils::IsCallerChrome()) {
|
|
|
|
// not permitted to use DrawWindow
|
|
|
|
// XXX ERRMSG we need to report an error to developers here! (bug 329026)
|
|
|
|
error.Throw(NS_ERROR_DOM_SECURITY_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
2013-01-29 09:51:55 -08:00
|
|
|
nsCOMPtr<nsIFrameLoaderOwner> loaderOwner = do_QueryInterface(&elem);
|
2012-11-15 01:30:54 -08:00
|
|
|
if (!loaderOwner) {
|
|
|
|
error.Throw(NS_ERROR_FAILURE);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<nsFrameLoader> frameloader = loaderOwner->GetFrameLoader();
|
|
|
|
if (!frameloader) {
|
|
|
|
error.Throw(NS_ERROR_FAILURE);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
PBrowserParent *child = frameloader->GetRemoteBrowser();
|
|
|
|
if (!child) {
|
2014-01-09 18:03:47 -08:00
|
|
|
nsIDocShell* docShell = frameLoader->GetExistingDocShell();
|
|
|
|
if (!docShell) {
|
|
|
|
error.Throw(NS_ERROR_FAILURE);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMWindow> window = docShell->GetWindow();
|
2012-11-15 01:30:54 -08:00
|
|
|
if (!window) {
|
|
|
|
error.Throw(NS_ERROR_FAILURE);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return DrawWindow(window, x, y, w, h, bgColor, flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
// protect against too-large surfaces that will cause allocation
|
|
|
|
// or overflow issues
|
|
|
|
if (!gfxASurface::CheckSurfaceSize(gfxIntSize(w, h), 0xffff)) {
|
|
|
|
error.Throw(NS_ERROR_FAILURE);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool flush =
|
|
|
|
(flags & nsIDOMCanvasRenderingContext2D::DRAWWINDOW_DO_NOT_FLUSH) == 0;
|
|
|
|
|
|
|
|
uint32_t renderDocFlags = nsIPresShell::RENDER_IGNORE_VIEWPORT_SCROLLING;
|
|
|
|
if (flags & nsIDOMCanvasRenderingContext2D::DRAWWINDOW_DRAW_CARET) {
|
|
|
|
renderDocFlags |= nsIPresShell::RENDER_CARET;
|
|
|
|
}
|
|
|
|
if (flags & nsIDOMCanvasRenderingContext2D::DRAWWINDOW_DRAW_VIEW) {
|
|
|
|
renderDocFlags &= ~nsIPresShell::RENDER_IGNORE_VIEWPORT_SCROLLING;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRect rect(nsPresContext::CSSPixelsToAppUnits(x),
|
|
|
|
nsPresContext::CSSPixelsToAppUnits(y),
|
|
|
|
nsPresContext::CSSPixelsToAppUnits(w),
|
|
|
|
nsPresContext::CSSPixelsToAppUnits(h));
|
|
|
|
if (mIPC) {
|
|
|
|
PDocumentRendererParent *pdocrender =
|
|
|
|
child->SendPDocumentRendererConstructor(rect,
|
|
|
|
mThebes->CurrentMatrix(),
|
|
|
|
nsString(aBGColor),
|
|
|
|
renderDocFlags, flush,
|
|
|
|
nsIntSize(mWidth, mHeight));
|
|
|
|
if (!pdocrender)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
DocumentRendererParent *docrender =
|
|
|
|
static_cast<DocumentRendererParent *>(pdocrender);
|
|
|
|
|
|
|
|
docrender->SetCanvasContext(this, mThebes);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// device pixel getting/setting
|
|
|
|
//
|
|
|
|
|
|
|
|
already_AddRefed<ImageData>
|
|
|
|
CanvasRenderingContext2D::GetImageData(JSContext* aCx, double aSx,
|
|
|
|
double aSy, double aSw,
|
|
|
|
double aSh, ErrorResult& error)
|
|
|
|
{
|
|
|
|
EnsureTarget();
|
|
|
|
if (!IsTargetValid()) {
|
|
|
|
error.Throw(NS_ERROR_FAILURE);
|
2013-04-02 18:14:24 -07:00
|
|
|
return nullptr;
|
2012-11-15 01:30:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!mCanvasElement && !mDocShell) {
|
|
|
|
NS_ERROR("No canvas element and no docshell in GetImageData!!!");
|
|
|
|
error.Throw(NS_ERROR_DOM_SECURITY_ERR);
|
2013-04-02 18:14:24 -07:00
|
|
|
return nullptr;
|
2012-11-15 01:30:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check only if we have a canvas element; if we were created with a docshell,
|
|
|
|
// then it's special internal use.
|
|
|
|
if (mCanvasElement && mCanvasElement->IsWriteOnly() &&
|
|
|
|
!nsContentUtils::IsCallerChrome())
|
|
|
|
{
|
|
|
|
// XXX ERRMSG we need to report an error to developers here! (bug 329026)
|
|
|
|
error.Throw(NS_ERROR_DOM_SECURITY_ERR);
|
2013-04-02 18:14:24 -07:00
|
|
|
return nullptr;
|
2012-11-15 01:30:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!NS_finite(aSx) || !NS_finite(aSy) ||
|
|
|
|
!NS_finite(aSw) || !NS_finite(aSh)) {
|
|
|
|
error.Throw(NS_ERROR_DOM_NOT_SUPPORTED_ERR);
|
2013-04-02 18:14:24 -07:00
|
|
|
return nullptr;
|
2012-11-15 01:30:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!aSw || !aSh) {
|
|
|
|
error.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
|
2013-04-02 18:14:24 -07:00
|
|
|
return nullptr;
|
2012-11-15 01:30:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
int32_t x = JS_DoubleToInt32(aSx);
|
|
|
|
int32_t y = JS_DoubleToInt32(aSy);
|
|
|
|
int32_t wi = JS_DoubleToInt32(aSw);
|
|
|
|
int32_t hi = JS_DoubleToInt32(aSh);
|
|
|
|
|
|
|
|
// Handle negative width and height by flipping the rectangle over in the
|
|
|
|
// relevant direction.
|
|
|
|
uint32_t w, h;
|
|
|
|
if (aSw < 0) {
|
|
|
|
w = -wi;
|
|
|
|
x -= w;
|
|
|
|
} else {
|
|
|
|
w = wi;
|
|
|
|
}
|
|
|
|
if (aSh < 0) {
|
|
|
|
h = -hi;
|
|
|
|
y -= h;
|
|
|
|
} else {
|
|
|
|
h = hi;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (w == 0) {
|
|
|
|
w = 1;
|
|
|
|
}
|
|
|
|
if (h == 0) {
|
|
|
|
h = 1;
|
|
|
|
}
|
|
|
|
|
2013-05-06 05:30:50 -07:00
|
|
|
JS::Rooted<JSObject*> array(aCx);
|
|
|
|
error = GetImageDataArray(aCx, x, y, w, h, array.address());
|
2012-11-15 01:30:54 -08:00
|
|
|
if (error.Failed()) {
|
2013-04-02 18:14:24 -07:00
|
|
|
return nullptr;
|
2012-11-15 01:30:54 -08:00
|
|
|
}
|
|
|
|
MOZ_ASSERT(array);
|
|
|
|
|
|
|
|
nsRefPtr<ImageData> imageData = new ImageData(w, h, *array);
|
|
|
|
return imageData.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
CanvasRenderingContext2D::GetImageDataArray(JSContext* aCx,
|
|
|
|
int32_t aX,
|
|
|
|
int32_t aY,
|
|
|
|
uint32_t aWidth,
|
|
|
|
uint32_t aHeight,
|
|
|
|
JSObject** aRetval)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aWidth && aHeight);
|
|
|
|
|
|
|
|
CheckedInt<uint32_t> len = CheckedInt<uint32_t>(aWidth) * aHeight * 4;
|
|
|
|
if (!len.isValid()) {
|
|
|
|
return NS_ERROR_DOM_INDEX_SIZE_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
CheckedInt<int32_t> rightMost = CheckedInt<int32_t>(aX) + aWidth;
|
|
|
|
CheckedInt<int32_t> bottomMost = CheckedInt<int32_t>(aY) + aHeight;
|
|
|
|
|
|
|
|
if (!rightMost.isValid() || !bottomMost.isValid()) {
|
|
|
|
return NS_ERROR_DOM_SYNTAX_ERR;
|
|
|
|
}
|
|
|
|
|
2013-02-11 20:56:28 -08:00
|
|
|
IntRect srcRect(0, 0, mWidth, mHeight);
|
|
|
|
IntRect destRect(aX, aY, aWidth, aHeight);
|
|
|
|
IntRect srcReadRect = srcRect.Intersect(destRect);
|
|
|
|
RefPtr<DataSourceSurface> readback;
|
|
|
|
if (!srcReadRect.IsEmpty() && !mZero) {
|
|
|
|
RefPtr<SourceSurface> snapshot = mTarget->Snapshot();
|
|
|
|
if (snapshot) {
|
|
|
|
readback = snapshot->GetDataSurface();
|
|
|
|
}
|
|
|
|
if (!readback || !readback->GetData()) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-02 02:12:45 -07:00
|
|
|
JS::Rooted<JSObject*> darray(aCx, JS_NewUint8ClampedArray(aCx, len.value()));
|
2012-11-15 01:30:54 -08:00
|
|
|
if (!darray) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mZero) {
|
|
|
|
*aRetval = darray;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t* data = JS_GetUint8ClampedArrayData(darray);
|
|
|
|
|
|
|
|
IntRect dstWriteRect = srcReadRect;
|
|
|
|
dstWriteRect.MoveBy(-aX, -aY);
|
|
|
|
|
|
|
|
uint8_t* src = data;
|
|
|
|
uint32_t srcStride = aWidth * 4;
|
2013-02-11 20:56:28 -08:00
|
|
|
if (readback) {
|
|
|
|
srcStride = readback->Stride();
|
|
|
|
src = readback->GetData() + srcReadRect.y * srcStride + srcReadRect.x * 4;
|
2012-11-15 01:30:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE! dst is the same as src, and this relies on reading
|
|
|
|
// from src and advancing that ptr before writing to dst.
|
|
|
|
// NOTE! I'm not sure that it is, I think this comment might have been
|
|
|
|
// inherited from Thebes canvas and is no longer true
|
|
|
|
uint8_t* dst = data + dstWriteRect.y * (aWidth * 4) + dstWriteRect.x * 4;
|
|
|
|
|
2014-03-16 12:34:14 -07:00
|
|
|
if (mOpaque) {
|
|
|
|
for (int32_t j = 0; j < dstWriteRect.height; ++j) {
|
|
|
|
for (int32_t i = 0; i < dstWriteRect.width; ++i) {
|
|
|
|
// XXX Is there some useful swizzle MMX we can use here?
|
|
|
|
#if MOZ_LITTLE_ENDIAN
|
|
|
|
uint8_t b = *src++;
|
|
|
|
uint8_t g = *src++;
|
|
|
|
uint8_t r = *src++;
|
|
|
|
src++;
|
|
|
|
#else
|
|
|
|
src++;
|
|
|
|
uint8_t r = *src++;
|
|
|
|
uint8_t g = *src++;
|
|
|
|
uint8_t b = *src++;
|
|
|
|
#endif
|
|
|
|
*dst++ = r;
|
|
|
|
*dst++ = g;
|
|
|
|
*dst++ = b;
|
|
|
|
*dst++ = 255;
|
|
|
|
}
|
|
|
|
src += srcStride - (dstWriteRect.width * 4);
|
|
|
|
dst += (aWidth * 4) - (dstWriteRect.width * 4);
|
|
|
|
}
|
|
|
|
} else
|
2012-11-15 01:30:54 -08:00
|
|
|
for (int32_t j = 0; j < dstWriteRect.height; ++j) {
|
|
|
|
for (int32_t i = 0; i < dstWriteRect.width; ++i) {
|
|
|
|
// XXX Is there some useful swizzle MMX we can use here?
|
2014-02-26 17:20:15 -08:00
|
|
|
#if MOZ_LITTLE_ENDIAN
|
2012-11-15 01:30:54 -08:00
|
|
|
uint8_t b = *src++;
|
|
|
|
uint8_t g = *src++;
|
|
|
|
uint8_t r = *src++;
|
|
|
|
uint8_t a = *src++;
|
|
|
|
#else
|
|
|
|
uint8_t a = *src++;
|
|
|
|
uint8_t r = *src++;
|
|
|
|
uint8_t g = *src++;
|
|
|
|
uint8_t b = *src++;
|
|
|
|
#endif
|
|
|
|
// Convert to non-premultiplied color
|
2012-12-16 16:20:01 -08:00
|
|
|
*dst++ = gfxUtils::sUnpremultiplyTable[a * 256 + r];
|
|
|
|
*dst++ = gfxUtils::sUnpremultiplyTable[a * 256 + g];
|
|
|
|
*dst++ = gfxUtils::sUnpremultiplyTable[a * 256 + b];
|
2012-11-15 01:30:54 -08:00
|
|
|
*dst++ = a;
|
|
|
|
}
|
|
|
|
src += srcStride - (dstWriteRect.width * 4);
|
|
|
|
dst += (aWidth * 4) - (dstWriteRect.width * 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
*aRetval = darray;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CanvasRenderingContext2D::EnsureErrorTarget()
|
|
|
|
{
|
|
|
|
if (sErrorTarget) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-01-10 11:06:16 -08:00
|
|
|
RefPtr<DrawTarget> errorTarget = gfxPlatform::GetPlatform()->CreateOffscreenCanvasDrawTarget(IntSize(1, 1), SurfaceFormat::B8G8R8A8);
|
2013-10-11 06:16:43 -07:00
|
|
|
MOZ_ASSERT(errorTarget, "Failed to allocate the error target!");
|
2012-11-15 01:30:54 -08:00
|
|
|
|
|
|
|
sErrorTarget = errorTarget;
|
|
|
|
NS_ADDREF(sErrorTarget);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CanvasRenderingContext2D::FillRuleChanged()
|
|
|
|
{
|
|
|
|
if (mPath) {
|
|
|
|
mPathBuilder = mPath->CopyToBuilder(CurrentState().fillRule);
|
|
|
|
mPath = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-11-15 11:54:34 -08:00
|
|
|
CanvasRenderingContext2D::PutImageData(ImageData& imageData, double dx,
|
2012-11-15 01:30:54 -08:00
|
|
|
double dy, ErrorResult& error)
|
|
|
|
{
|
2014-06-03 18:31:43 -07:00
|
|
|
dom::Uint8ClampedArray arr;
|
|
|
|
DebugOnly<bool> inited = arr.Init(imageData.GetDataObject());
|
|
|
|
MOZ_ASSERT(inited);
|
2012-11-15 01:30:54 -08:00
|
|
|
|
|
|
|
error = PutImageData_explicit(JS_DoubleToInt32(dx), JS_DoubleToInt32(dy),
|
|
|
|
imageData.Width(), imageData.Height(),
|
Bug 999651, bug 995679, bug 1009952, bug 1011007, bug 991981. r=sfink, r=shu, r=jandem, r=jdm, r=luke, r=bbouvier, r=nmatsakis, r=bz, r=ehsan, r=jgilbert, r=smaug, r=sicking, r=terrence, r=bholley, r=bent, r=efaust, r=jorendorff
2014-05-27 14:32:41 -07:00
|
|
|
&arr, false, 0, 0, 0, 0);
|
2012-11-15 01:30:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-11-15 11:54:34 -08:00
|
|
|
CanvasRenderingContext2D::PutImageData(ImageData& imageData, double dx,
|
2012-11-15 01:30:54 -08:00
|
|
|
double dy, double dirtyX,
|
|
|
|
double dirtyY, double dirtyWidth,
|
|
|
|
double dirtyHeight,
|
|
|
|
ErrorResult& error)
|
|
|
|
{
|
2014-06-03 18:31:43 -07:00
|
|
|
dom::Uint8ClampedArray arr;
|
|
|
|
DebugOnly<bool> inited = arr.Init(imageData.GetDataObject());
|
|
|
|
MOZ_ASSERT(inited);
|
2012-11-15 01:30:54 -08:00
|
|
|
|
|
|
|
error = PutImageData_explicit(JS_DoubleToInt32(dx), JS_DoubleToInt32(dy),
|
|
|
|
imageData.Width(), imageData.Height(),
|
Bug 999651, bug 995679, bug 1009952, bug 1011007, bug 991981. r=sfink, r=shu, r=jandem, r=jdm, r=luke, r=bbouvier, r=nmatsakis, r=bz, r=ehsan, r=jgilbert, r=smaug, r=sicking, r=terrence, r=bholley, r=bent, r=efaust, r=jorendorff
2014-05-27 14:32:41 -07:00
|
|
|
&arr, true,
|
2012-11-15 01:30:54 -08:00
|
|
|
JS_DoubleToInt32(dirtyX),
|
|
|
|
JS_DoubleToInt32(dirtyY),
|
|
|
|
JS_DoubleToInt32(dirtyWidth),
|
|
|
|
JS_DoubleToInt32(dirtyHeight));
|
|
|
|
}
|
|
|
|
|
|
|
|
// void putImageData (in ImageData d, in float x, in float y);
|
|
|
|
// void putImageData (in ImageData d, in double x, in double y, in double dirtyX, in double dirtyY, in double dirtyWidth, in double dirtyHeight);
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
CanvasRenderingContext2D::PutImageData_explicit(int32_t x, int32_t y, uint32_t w, uint32_t h,
|
Bug 999651, bug 995679, bug 1009952, bug 1011007, bug 991981. r=sfink, r=shu, r=jandem, r=jdm, r=luke, r=bbouvier, r=nmatsakis, r=bz, r=ehsan, r=jgilbert, r=smaug, r=sicking, r=terrence, r=bholley, r=bent, r=efaust, r=jorendorff
2014-05-27 14:32:41 -07:00
|
|
|
dom::Uint8ClampedArray* aArray,
|
2012-11-15 01:30:54 -08:00
|
|
|
bool hasDirtyRect, int32_t dirtyX, int32_t dirtyY,
|
|
|
|
int32_t dirtyWidth, int32_t dirtyHeight)
|
|
|
|
{
|
|
|
|
if (w == 0 || h == 0) {
|
2014-06-05 11:19:22 -07:00
|
|
|
return NS_ERROR_DOM_INVALID_STATE_ERR;
|
2012-11-15 01:30:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
IntRect dirtyRect;
|
|
|
|
IntRect imageDataRect(0, 0, w, h);
|
|
|
|
|
|
|
|
if (hasDirtyRect) {
|
|
|
|
// fix up negative dimensions
|
|
|
|
if (dirtyWidth < 0) {
|
|
|
|
NS_ENSURE_TRUE(dirtyWidth != INT_MIN, NS_ERROR_DOM_INDEX_SIZE_ERR);
|
|
|
|
|
|
|
|
CheckedInt32 checkedDirtyX = CheckedInt32(dirtyX) + dirtyWidth;
|
|
|
|
|
|
|
|
if (!checkedDirtyX.isValid())
|
|
|
|
return NS_ERROR_DOM_INDEX_SIZE_ERR;
|
|
|
|
|
|
|
|
dirtyX = checkedDirtyX.value();
|
|
|
|
dirtyWidth = -dirtyWidth;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dirtyHeight < 0) {
|
|
|
|
NS_ENSURE_TRUE(dirtyHeight != INT_MIN, NS_ERROR_DOM_INDEX_SIZE_ERR);
|
|
|
|
|
|
|
|
CheckedInt32 checkedDirtyY = CheckedInt32(dirtyY) + dirtyHeight;
|
|
|
|
|
|
|
|
if (!checkedDirtyY.isValid())
|
|
|
|
return NS_ERROR_DOM_INDEX_SIZE_ERR;
|
|
|
|
|
|
|
|
dirtyY = checkedDirtyY.value();
|
|
|
|
dirtyHeight = -dirtyHeight;
|
|
|
|
}
|
|
|
|
|
|
|
|
// bound the dirty rect within the imageData rectangle
|
|
|
|
dirtyRect = imageDataRect.Intersect(IntRect(dirtyX, dirtyY, dirtyWidth, dirtyHeight));
|
|
|
|
|
|
|
|
if (dirtyRect.Width() <= 0 || dirtyRect.Height() <= 0)
|
|
|
|
return NS_OK;
|
|
|
|
} else {
|
|
|
|
dirtyRect = imageDataRect;
|
|
|
|
}
|
|
|
|
|
|
|
|
dirtyRect.MoveBy(IntPoint(x, y));
|
|
|
|
dirtyRect = IntRect(0, 0, mWidth, mHeight).Intersect(dirtyRect);
|
|
|
|
|
|
|
|
if (dirtyRect.Width() <= 0 || dirtyRect.Height() <= 0) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
Bug 999651, bug 995679, bug 1009952, bug 1011007, bug 991981. r=sfink, r=shu, r=jandem, r=jdm, r=luke, r=bbouvier, r=nmatsakis, r=bz, r=ehsan, r=jgilbert, r=smaug, r=sicking, r=terrence, r=bholley, r=bent, r=efaust, r=jorendorff
2014-05-27 14:32:41 -07:00
|
|
|
aArray->ComputeLengthAndData();
|
|
|
|
|
|
|
|
uint32_t dataLen = aArray->Length();
|
|
|
|
|
2012-11-15 01:30:54 -08:00
|
|
|
uint32_t len = w * h * 4;
|
Bug 999651, bug 995679, bug 1009952, bug 1011007, bug 991981. r=sfink, r=shu, r=jandem, r=jdm, r=luke, r=bbouvier, r=nmatsakis, r=bz, r=ehsan, r=jgilbert, r=smaug, r=sicking, r=terrence, r=bholley, r=bent, r=efaust, r=jorendorff
2014-05-27 14:32:41 -07:00
|
|
|
if (dataLen != len) {
|
2014-06-05 11:19:22 -07:00
|
|
|
return NS_ERROR_DOM_INVALID_STATE_ERR;
|
2012-11-15 01:30:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<gfxImageSurface> imgsurf = new gfxImageSurface(gfxIntSize(w, h),
|
2014-01-23 10:26:40 -08:00
|
|
|
gfxImageFormat::ARGB32,
|
2012-11-15 01:30:54 -08:00
|
|
|
false);
|
|
|
|
if (!imgsurf || imgsurf->CairoStatus()) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
Bug 999651, bug 995679, bug 1009952, bug 1011007, bug 991981. r=sfink, r=shu, r=jandem, r=jdm, r=luke, r=bbouvier, r=nmatsakis, r=bz, r=ehsan, r=jgilbert, r=smaug, r=sicking, r=terrence, r=bholley, r=bent, r=efaust, r=jorendorff
2014-05-27 14:32:41 -07:00
|
|
|
uint8_t *src = aArray->Data();
|
2012-11-15 01:30:54 -08:00
|
|
|
uint8_t *dst = imgsurf->Data();
|
|
|
|
|
|
|
|
for (uint32_t j = 0; j < h; j++) {
|
|
|
|
for (uint32_t i = 0; i < w; i++) {
|
|
|
|
uint8_t r = *src++;
|
|
|
|
uint8_t g = *src++;
|
|
|
|
uint8_t b = *src++;
|
|
|
|
uint8_t a = *src++;
|
|
|
|
|
|
|
|
// Convert to premultiplied color (losslessly if the input came from getImageData)
|
2014-02-26 17:20:15 -08:00
|
|
|
#if MOZ_LITTLE_ENDIAN
|
2012-12-16 16:20:01 -08:00
|
|
|
*dst++ = gfxUtils::sPremultiplyTable[a * 256 + b];
|
|
|
|
*dst++ = gfxUtils::sPremultiplyTable[a * 256 + g];
|
|
|
|
*dst++ = gfxUtils::sPremultiplyTable[a * 256 + r];
|
2012-11-15 01:30:54 -08:00
|
|
|
*dst++ = a;
|
|
|
|
#else
|
|
|
|
*dst++ = a;
|
2012-12-16 16:20:01 -08:00
|
|
|
*dst++ = gfxUtils::sPremultiplyTable[a * 256 + r];
|
|
|
|
*dst++ = gfxUtils::sPremultiplyTable[a * 256 + g];
|
|
|
|
*dst++ = gfxUtils::sPremultiplyTable[a * 256 + b];
|
2012-11-15 01:30:54 -08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
EnsureTarget();
|
|
|
|
if (!IsTargetValid()) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<SourceSurface> sourceSurface =
|
2014-01-10 11:06:16 -08:00
|
|
|
mTarget->CreateSourceSurfaceFromData(imgsurf->Data(), IntSize(w, h), imgsurf->Stride(), SurfaceFormat::B8G8R8A8);
|
2012-11-15 01:30:54 -08:00
|
|
|
|
2013-01-17 05:38:38 -08:00
|
|
|
// In certain scenarios, requesting larger than 8k image fails. Bug 803568
|
|
|
|
// covers the details of how to run into it, but the full detailed
|
|
|
|
// investigation hasn't been done to determine the underlying cause. We
|
|
|
|
// will just handle the failure to allocate the surface to avoid a crash.
|
|
|
|
if (!sourceSurface) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2012-11-15 01:30:54 -08:00
|
|
|
|
|
|
|
mTarget->CopySurface(sourceSurface,
|
|
|
|
IntRect(dirtyRect.x - x, dirtyRect.y - y,
|
|
|
|
dirtyRect.width, dirtyRect.height),
|
|
|
|
IntPoint(dirtyRect.x, dirtyRect.y));
|
|
|
|
|
|
|
|
Redraw(mgfx::Rect(dirtyRect.x, dirtyRect.y, dirtyRect.width, dirtyRect.height));
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static already_AddRefed<ImageData>
|
|
|
|
CreateImageData(JSContext* cx, CanvasRenderingContext2D* context,
|
|
|
|
uint32_t w, uint32_t h, ErrorResult& error)
|
|
|
|
{
|
|
|
|
if (w == 0)
|
|
|
|
w = 1;
|
|
|
|
if (h == 0)
|
|
|
|
h = 1;
|
|
|
|
|
|
|
|
CheckedInt<uint32_t> len = CheckedInt<uint32_t>(w) * h * 4;
|
|
|
|
if (!len.isValid()) {
|
|
|
|
error.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
|
2013-04-02 18:14:24 -07:00
|
|
|
return nullptr;
|
2012-11-15 01:30:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create the fast typed array; it's initialized to 0 by default.
|
|
|
|
JSObject* darray = Uint8ClampedArray::Create(cx, context, len.value());
|
|
|
|
if (!darray) {
|
|
|
|
error.Throw(NS_ERROR_OUT_OF_MEMORY);
|
2013-04-02 18:14:24 -07:00
|
|
|
return nullptr;
|
2012-11-15 01:30:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<mozilla::dom::ImageData> imageData =
|
|
|
|
new mozilla::dom::ImageData(w, h, *darray);
|
|
|
|
return imageData.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<ImageData>
|
|
|
|
CanvasRenderingContext2D::CreateImageData(JSContext* cx, double sw,
|
|
|
|
double sh, ErrorResult& error)
|
|
|
|
{
|
|
|
|
if (!sw || !sh) {
|
|
|
|
error.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
|
2013-04-02 18:14:24 -07:00
|
|
|
return nullptr;
|
2012-11-15 01:30:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
int32_t wi = JS_DoubleToInt32(sw);
|
|
|
|
int32_t hi = JS_DoubleToInt32(sh);
|
|
|
|
|
2013-03-05 15:43:53 -08:00
|
|
|
uint32_t w = Abs(wi);
|
|
|
|
uint32_t h = Abs(hi);
|
2012-11-15 01:30:54 -08:00
|
|
|
return mozilla::dom::CreateImageData(cx, this, w, h, error);
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<ImageData>
|
|
|
|
CanvasRenderingContext2D::CreateImageData(JSContext* cx,
|
|
|
|
ImageData& imagedata,
|
|
|
|
ErrorResult& error)
|
|
|
|
{
|
|
|
|
return mozilla::dom::CreateImageData(cx, this, imagedata.Width(),
|
|
|
|
imagedata.Height(), error);
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint8_t g2DContextLayerUserData;
|
|
|
|
|
|
|
|
already_AddRefed<CanvasLayer>
|
|
|
|
CanvasRenderingContext2D::GetCanvasLayer(nsDisplayListBuilder* aBuilder,
|
|
|
|
CanvasLayer *aOldLayer,
|
|
|
|
LayerManager *aManager)
|
|
|
|
{
|
2014-07-15 23:15:39 -07:00
|
|
|
if (mOpaque) {
|
|
|
|
// If we're opaque then make sure we have a surface so we paint black
|
|
|
|
// instead of transparent.
|
|
|
|
EnsureTarget();
|
|
|
|
}
|
|
|
|
|
2012-11-15 01:30:54 -08:00
|
|
|
// Don't call EnsureTarget() ... if there isn't already a surface, then
|
|
|
|
// we have nothing to paint and there is no need to create a surface just
|
|
|
|
// to paint nothing. Also, EnsureTarget() can cause creation of a persistent
|
|
|
|
// layer manager which must NOT happen during a paint.
|
|
|
|
if (!mTarget || !IsTargetValid()) {
|
|
|
|
// No DidTransactionCallback will be received, so mark the context clean
|
|
|
|
// now so future invalidations will be dispatched.
|
|
|
|
MarkContextClean();
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
mTarget->Flush();
|
|
|
|
|
|
|
|
if (!mResetLayer && aOldLayer) {
|
|
|
|
CanvasRenderingContext2DUserData* userData =
|
|
|
|
static_cast<CanvasRenderingContext2DUserData*>(
|
|
|
|
aOldLayer->GetUserData(&g2DContextLayerUserData));
|
2013-12-04 14:46:20 -08:00
|
|
|
|
|
|
|
CanvasLayer::Data data;
|
2014-03-05 13:49:37 -08:00
|
|
|
if (mStream) {
|
2014-04-15 12:07:42 -07:00
|
|
|
#ifdef USE_SKIA
|
2014-03-05 13:49:37 -08:00
|
|
|
SkiaGLGlue* glue = gfxPlatform::GetPlatform()->GetSkiaGLGlue();
|
|
|
|
|
|
|
|
if (glue) {
|
|
|
|
data.mGLContext = glue->GetGLContext();
|
|
|
|
data.mStream = mStream.get();
|
|
|
|
}
|
2014-04-15 12:07:42 -07:00
|
|
|
#endif
|
2014-03-05 13:49:37 -08:00
|
|
|
} else {
|
|
|
|
data.mDrawTarget = mTarget;
|
|
|
|
}
|
|
|
|
|
2013-12-04 14:46:20 -08:00
|
|
|
if (userData && userData->IsForContext(this) && aOldLayer->IsDataValid(data)) {
|
2013-04-22 04:15:59 -07:00
|
|
|
nsRefPtr<CanvasLayer> ret = aOldLayer;
|
|
|
|
return ret.forget();
|
2012-11-15 01:30:54 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<CanvasLayer> canvasLayer = aManager->CreateCanvasLayer();
|
|
|
|
if (!canvasLayer) {
|
|
|
|
NS_WARNING("CreateCanvasLayer returned null!");
|
|
|
|
// No DidTransactionCallback will be received, so mark the context clean
|
|
|
|
// now so future invalidations will be dispatched.
|
|
|
|
MarkContextClean();
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
CanvasRenderingContext2DUserData *userData = nullptr;
|
|
|
|
// Make the layer tell us whenever a transaction finishes (including
|
|
|
|
// the current transaction), so we can clear our invalidation state and
|
|
|
|
// start invalidating again. We need to do this for all layers since
|
|
|
|
// callers of DrawWindow may be expecting to receive normal invalidation
|
|
|
|
// notifications after this paint.
|
|
|
|
|
|
|
|
// The layer will be destroyed when we tear down the presentation
|
|
|
|
// (at the latest), at which time this userData will be destroyed,
|
|
|
|
// releasing the reference to the element.
|
|
|
|
// The userData will receive DidTransactionCallbacks, which flush the
|
|
|
|
// the invalidation state to indicate that the canvas is up to date.
|
|
|
|
userData = new CanvasRenderingContext2DUserData(this);
|
|
|
|
canvasLayer->SetDidTransactionCallback(
|
|
|
|
CanvasRenderingContext2DUserData::DidTransactionCallback, userData);
|
|
|
|
canvasLayer->SetUserData(&g2DContextLayerUserData, userData);
|
|
|
|
|
|
|
|
CanvasLayer::Data data;
|
2014-03-05 13:49:37 -08:00
|
|
|
if (mStream) {
|
|
|
|
SkiaGLGlue* glue = gfxPlatform::GetPlatform()->GetSkiaGLGlue();
|
|
|
|
|
|
|
|
if (glue) {
|
|
|
|
canvasLayer->SetPreTransactionCallback(
|
|
|
|
CanvasRenderingContext2DUserData::PreTransactionCallback, userData);
|
2014-04-15 12:07:42 -07:00
|
|
|
#if USE_SKIA
|
2014-03-05 13:49:37 -08:00
|
|
|
data.mGLContext = glue->GetGLContext();
|
2014-04-15 12:07:42 -07:00
|
|
|
#endif
|
2014-03-05 13:49:37 -08:00
|
|
|
data.mStream = mStream.get();
|
|
|
|
data.mTexID = (uint32_t)((uintptr_t)mTarget->GetNativeSurface(NativeSurfaceType::OPENGL_TEXTURE));
|
|
|
|
}
|
|
|
|
} else {
|
2012-11-30 15:58:00 -08:00
|
|
|
data.mDrawTarget = mTarget;
|
|
|
|
}
|
2012-11-15 01:30:54 -08:00
|
|
|
|
|
|
|
data.mSize = nsIntSize(mWidth, mHeight);
|
2014-05-14 09:26:13 -07:00
|
|
|
data.mHasAlpha = !mOpaque;
|
2012-11-15 01:30:54 -08:00
|
|
|
|
|
|
|
canvasLayer->Initialize(data);
|
|
|
|
uint32_t flags = mOpaque ? Layer::CONTENT_OPAQUE : 0;
|
|
|
|
canvasLayer->SetContentFlags(flags);
|
|
|
|
canvasLayer->Updated();
|
|
|
|
|
|
|
|
mResetLayer = false;
|
|
|
|
|
|
|
|
return canvasLayer.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CanvasRenderingContext2D::MarkContextClean()
|
|
|
|
{
|
|
|
|
if (mInvalidateCount > 0) {
|
|
|
|
mPredictManyRedrawCalls = mInvalidateCount > kCanvasMaxInvalidateCount;
|
|
|
|
}
|
|
|
|
mIsEntireFrameInvalid = false;
|
|
|
|
mInvalidateCount = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
CanvasRenderingContext2D::ShouldForceInactiveLayer(LayerManager *aManager)
|
|
|
|
{
|
2013-12-31 01:06:12 -08:00
|
|
|
return !aManager->CanUseCanvasLayerForSize(IntSize(mWidth, mHeight));
|
2012-11-15 01:30:54 -08:00
|
|
|
}
|
|
|
|
|
2014-03-18 05:03:06 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_ROOT_NATIVE(CanvasPath, AddRef)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNROOT_NATIVE(CanvasPath, Release)
|
|
|
|
|
2014-05-13 13:22:52 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(CanvasPath, mParent)
|
2014-03-18 05:03:06 -07:00
|
|
|
|
2014-05-13 13:22:52 -07:00
|
|
|
CanvasPath::CanvasPath(nsISupports* aParent)
|
|
|
|
: mParent(aParent)
|
2014-03-18 05:03:06 -07:00
|
|
|
{
|
|
|
|
SetIsDOMBinding();
|
|
|
|
|
|
|
|
mPathBuilder = gfxPlatform::GetPlatform()->ScreenReferenceDrawTarget()->CreatePathBuilder();
|
|
|
|
}
|
|
|
|
|
2014-06-17 02:37:46 -07:00
|
|
|
CanvasPath::CanvasPath(nsISupports* aParent, TemporaryRef<PathBuilder> aPathBuilder)
|
2014-05-13 13:22:52 -07:00
|
|
|
: mParent(aParent), mPathBuilder(aPathBuilder)
|
2014-03-18 05:03:06 -07:00
|
|
|
{
|
|
|
|
SetIsDOMBinding();
|
|
|
|
|
|
|
|
if (!mPathBuilder) {
|
|
|
|
mPathBuilder = gfxPlatform::GetPlatform()->ScreenReferenceDrawTarget()->CreatePathBuilder();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
JSObject*
|
2014-04-08 15:27:18 -07:00
|
|
|
CanvasPath::WrapObject(JSContext* aCx)
|
2014-03-18 05:03:06 -07:00
|
|
|
{
|
Bug 991742 part 6. Remove the "aScope" argument of binding Wrap() methods. r=bholley
This patch was mostly generated with this command:
find . -name "*.h" -o -name "*.cpp" | xargs sed -e 's/Binding::Wrap(aCx, aScope, this/Binding::Wrap(aCx, this/' -e 's/Binding_workers::Wrap(aCx, aScope, this/Binding_workers::Wrap(aCx, this/' -e 's/Binding::Wrap(cx, scope, this/Binding::Wrap(cx, this/' -i ""
plus a few manual fixes to dom/bindings/Codegen.py, js/xpconnect/src/event_impl_gen.py, and a few C++ files that were not caught in the search-and-replace above.
2014-04-08 15:27:17 -07:00
|
|
|
return Path2DBinding::Wrap(aCx, this);
|
2014-03-18 05:03:06 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<CanvasPath>
|
|
|
|
CanvasPath::Constructor(const GlobalObject& aGlobal, ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
nsRefPtr<CanvasPath> path = new CanvasPath(aGlobal.GetAsSupports());
|
|
|
|
return path.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<CanvasPath>
|
|
|
|
CanvasPath::Constructor(const GlobalObject& aGlobal, CanvasPath& aCanvasPath, ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
RefPtr<gfx::Path> tempPath = aCanvasPath.GetPath(CanvasWindingRule::Nonzero,
|
|
|
|
gfxPlatform::GetPlatform()->ScreenReferenceDrawTarget());
|
|
|
|
|
|
|
|
nsRefPtr<CanvasPath> path = new CanvasPath(aGlobal.GetAsSupports(), tempPath->CopyToBuilder());
|
|
|
|
return path.forget();
|
|
|
|
}
|
|
|
|
|
2014-03-24 06:31:19 -07:00
|
|
|
already_AddRefed<CanvasPath>
|
|
|
|
CanvasPath::Constructor(const GlobalObject& aGlobal, const nsAString& aPathString, ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
RefPtr<gfx::Path> tempPath = SVGContentUtils::GetPath(aPathString);
|
|
|
|
if (!tempPath) {
|
|
|
|
return Constructor(aGlobal, aRv);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<CanvasPath> path = new CanvasPath(aGlobal.GetAsSupports(), tempPath->CopyToBuilder());
|
|
|
|
return path.forget();
|
|
|
|
}
|
|
|
|
|
2014-03-18 05:03:06 -07:00
|
|
|
void
|
|
|
|
CanvasPath::ClosePath()
|
|
|
|
{
|
2014-03-30 23:55:00 -07:00
|
|
|
EnsurePathBuilder();
|
|
|
|
|
2014-03-18 05:03:06 -07:00
|
|
|
mPathBuilder->Close();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CanvasPath::MoveTo(double x, double y)
|
|
|
|
{
|
2014-03-30 23:55:00 -07:00
|
|
|
EnsurePathBuilder();
|
|
|
|
|
2014-03-18 05:03:06 -07:00
|
|
|
mPathBuilder->MoveTo(Point(ToFloat(x), ToFloat(y)));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CanvasPath::LineTo(double x, double y)
|
|
|
|
{
|
2014-03-30 23:55:00 -07:00
|
|
|
EnsurePathBuilder();
|
|
|
|
|
2014-03-18 05:03:06 -07:00
|
|
|
mPathBuilder->LineTo(Point(ToFloat(x), ToFloat(y)));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CanvasPath::QuadraticCurveTo(double cpx, double cpy, double x, double y)
|
|
|
|
{
|
2014-03-30 23:55:00 -07:00
|
|
|
EnsurePathBuilder();
|
|
|
|
|
2014-03-18 05:03:06 -07:00
|
|
|
mPathBuilder->QuadraticBezierTo(gfx::Point(ToFloat(cpx), ToFloat(cpy)),
|
|
|
|
gfx::Point(ToFloat(x), ToFloat(y)));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CanvasPath::BezierCurveTo(double cp1x, double cp1y,
|
|
|
|
double cp2x, double cp2y,
|
|
|
|
double x, double y)
|
|
|
|
{
|
|
|
|
BezierTo(gfx::Point(ToFloat(cp1x), ToFloat(cp1y)),
|
|
|
|
gfx::Point(ToFloat(cp2x), ToFloat(cp2y)),
|
|
|
|
gfx::Point(ToFloat(x), ToFloat(y)));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CanvasPath::ArcTo(double x1, double y1, double x2, double y2, double radius,
|
|
|
|
ErrorResult& error)
|
|
|
|
{
|
|
|
|
if (radius < 0) {
|
|
|
|
error.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-05-12 11:40:00 -07:00
|
|
|
EnsurePathBuilder();
|
|
|
|
|
2014-03-18 05:03:06 -07:00
|
|
|
// Current point in user space!
|
|
|
|
Point p0 = mPathBuilder->CurrentPoint();
|
|
|
|
Point p1(x1, y1);
|
|
|
|
Point p2(x2, y2);
|
|
|
|
|
|
|
|
// Execute these calculations in double precision to avoid cumulative
|
|
|
|
// rounding errors.
|
|
|
|
double dir, a2, b2, c2, cosx, sinx, d, anx, any,
|
|
|
|
bnx, bny, x3, y3, x4, y4, cx, cy, angle0, angle1;
|
|
|
|
bool anticlockwise;
|
|
|
|
|
|
|
|
if (p0 == p1 || p1 == p2 || radius == 0) {
|
|
|
|
LineTo(p1.x, p1.y);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for colinearity
|
2014-08-28 09:45:48 -07:00
|
|
|
dir = (p2.x - p1.x) * (p0.y - p1.y) + (p2.y - p1.y) * (p1.x - p0.x);
|
2014-03-18 05:03:06 -07:00
|
|
|
if (dir == 0) {
|
|
|
|
LineTo(p1.x, p1.y);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// XXX - Math for this code was already available from the non-azure code
|
|
|
|
// and would be well tested. Perhaps converting to bezier directly might
|
|
|
|
// be more efficient longer run.
|
|
|
|
a2 = (p0.x-x1)*(p0.x-x1) + (p0.y-y1)*(p0.y-y1);
|
|
|
|
b2 = (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2);
|
|
|
|
c2 = (p0.x-x2)*(p0.x-x2) + (p0.y-y2)*(p0.y-y2);
|
|
|
|
cosx = (a2+b2-c2)/(2*sqrt(a2*b2));
|
|
|
|
|
|
|
|
sinx = sqrt(1 - cosx*cosx);
|
|
|
|
d = radius / ((1 - cosx) / sinx);
|
|
|
|
|
|
|
|
anx = (x1-p0.x) / sqrt(a2);
|
|
|
|
any = (y1-p0.y) / sqrt(a2);
|
|
|
|
bnx = (x1-x2) / sqrt(b2);
|
|
|
|
bny = (y1-y2) / sqrt(b2);
|
|
|
|
x3 = x1 - anx*d;
|
|
|
|
y3 = y1 - any*d;
|
|
|
|
x4 = x1 - bnx*d;
|
|
|
|
y4 = y1 - bny*d;
|
|
|
|
anticlockwise = (dir < 0);
|
|
|
|
cx = x3 + any*radius*(anticlockwise ? 1 : -1);
|
|
|
|
cy = y3 - anx*radius*(anticlockwise ? 1 : -1);
|
|
|
|
angle0 = atan2((y3-cy), (x3-cx));
|
|
|
|
angle1 = atan2((y4-cy), (x4-cx));
|
|
|
|
|
|
|
|
|
|
|
|
LineTo(x3, y3);
|
|
|
|
|
|
|
|
Arc(cx, cy, radius, angle0, angle1, anticlockwise, error);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CanvasPath::Rect(double x, double y, double w, double h)
|
|
|
|
{
|
|
|
|
MoveTo(x, y);
|
|
|
|
LineTo(x + w, y);
|
|
|
|
LineTo(x + w, y + h);
|
|
|
|
LineTo(x, y + h);
|
|
|
|
ClosePath();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CanvasPath::Arc(double x, double y, double radius,
|
|
|
|
double startAngle, double endAngle, bool anticlockwise,
|
|
|
|
ErrorResult& error)
|
|
|
|
{
|
|
|
|
if (radius < 0.0) {
|
|
|
|
error.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-05-12 11:40:00 -07:00
|
|
|
EnsurePathBuilder();
|
|
|
|
|
2014-03-18 05:03:06 -07:00
|
|
|
ArcToBezier(this, Point(x, y), Size(radius, radius), startAngle, endAngle, anticlockwise);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CanvasPath::LineTo(const gfx::Point& aPoint)
|
|
|
|
{
|
2014-03-30 23:55:00 -07:00
|
|
|
EnsurePathBuilder();
|
|
|
|
|
2014-03-18 05:03:06 -07:00
|
|
|
mPathBuilder->LineTo(aPoint);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CanvasPath::BezierTo(const gfx::Point& aCP1,
|
|
|
|
const gfx::Point& aCP2,
|
|
|
|
const gfx::Point& aCP3)
|
|
|
|
{
|
2014-03-30 23:55:00 -07:00
|
|
|
EnsurePathBuilder();
|
|
|
|
|
2014-03-18 05:03:06 -07:00
|
|
|
mPathBuilder->BezierTo(aCP1, aCP2, aCP3);
|
|
|
|
}
|
|
|
|
|
2014-08-25 06:03:00 -07:00
|
|
|
void
|
|
|
|
CanvasPath::AddPath(CanvasPath& aCanvasPath, const Optional<NonNull<SVGMatrix>>& aMatrix)
|
|
|
|
{
|
|
|
|
RefPtr<gfx::Path> tempPath = aCanvasPath.GetPath(CanvasWindingRule::Nonzero,
|
|
|
|
gfxPlatform::GetPlatform()->ScreenReferenceDrawTarget());
|
|
|
|
|
|
|
|
if (aMatrix.WasPassed()) {
|
|
|
|
const SVGMatrix& m = aMatrix.Value();
|
|
|
|
Matrix transform(m.A(), m.B(), m.C(), m.D(), m.E(), m.F());
|
|
|
|
|
|
|
|
if (!transform.IsIdentity()) {
|
|
|
|
RefPtr<PathBuilder> tempBuilder = tempPath->TransformedCopyToBuilder(transform, FillRule::FILL_WINDING);
|
|
|
|
tempPath = tempBuilder->Finish();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tempPath->StreamToSink(mPathBuilder);
|
|
|
|
}
|
|
|
|
|
2014-06-17 02:37:46 -07:00
|
|
|
TemporaryRef<gfx::Path>
|
|
|
|
CanvasPath::GetPath(const CanvasWindingRule& winding, const DrawTarget* aTarget) const
|
2014-03-18 05:03:06 -07:00
|
|
|
{
|
|
|
|
FillRule fillRule = FillRule::FILL_WINDING;
|
|
|
|
if (winding == CanvasWindingRule::Evenodd) {
|
|
|
|
fillRule = FillRule::FILL_EVEN_ODD;
|
|
|
|
}
|
|
|
|
|
2014-03-30 23:55:00 -07:00
|
|
|
if (mPath &&
|
2014-06-19 13:35:33 -07:00
|
|
|
(mPath->GetBackendType() == aTarget->GetBackendType()) &&
|
2014-03-30 23:55:00 -07:00
|
|
|
(mPath->GetFillRule() == fillRule)) {
|
|
|
|
return mPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mPath) {
|
|
|
|
// if there is no path, there must be a pathbuilder
|
|
|
|
MOZ_ASSERT(mPathBuilder);
|
|
|
|
mPath = mPathBuilder->Finish();
|
|
|
|
if (!mPath)
|
|
|
|
return mPath;
|
|
|
|
|
|
|
|
mPathBuilder = nullptr;
|
|
|
|
}
|
2014-03-18 05:03:06 -07:00
|
|
|
|
|
|
|
// retarget our backend if we're used with a different backend
|
2014-06-19 13:35:33 -07:00
|
|
|
if (mPath->GetBackendType() != aTarget->GetBackendType()) {
|
2014-06-17 02:37:46 -07:00
|
|
|
RefPtr<PathBuilder> tmpPathBuilder = aTarget->CreatePathBuilder(fillRule);
|
2014-03-30 23:55:00 -07:00
|
|
|
mPath->StreamToSink(tmpPathBuilder);
|
|
|
|
mPath = tmpPathBuilder->Finish();
|
|
|
|
} else if (mPath->GetFillRule() != fillRule) {
|
|
|
|
RefPtr<PathBuilder> tmpPathBuilder = mPath->CopyToBuilder(fillRule);
|
|
|
|
mPath = tmpPathBuilder->Finish();
|
2014-03-18 05:03:06 -07:00
|
|
|
}
|
|
|
|
|
2014-03-30 23:55:00 -07:00
|
|
|
return mPath;
|
|
|
|
}
|
2014-03-18 05:03:06 -07:00
|
|
|
|
2014-03-30 23:55:00 -07:00
|
|
|
void
|
|
|
|
CanvasPath::EnsurePathBuilder() const
|
|
|
|
{
|
|
|
|
if (mPathBuilder) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if there is not pathbuilder, there must be a path
|
|
|
|
MOZ_ASSERT(mPath);
|
|
|
|
mPathBuilder = mPath->CopyToBuilder();
|
|
|
|
mPath = nullptr;
|
2014-03-18 05:03:06 -07:00
|
|
|
}
|
|
|
|
|
2012-11-15 01:30:54 -08:00
|
|
|
}
|
|
|
|
}
|