2016-01-11 02:28:35 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2009-09-10 23:03:36 +04:00
|
|
|
|
|
|
|
/* Utilities for animation of computed style values */
|
|
|
|
|
2017-02-14 22:23:11 +03:00
|
|
|
#include "mozilla/StyleAnimationValue.h"
|
|
|
|
|
2013-12-09 06:52:54 +04:00
|
|
|
#include "mozilla/ArrayUtils.h"
|
2012-08-27 05:58:23 +04:00
|
|
|
#include "mozilla/MathAlgorithms.h"
|
2018-03-29 14:15:46 +03:00
|
|
|
#include "mozilla/ServoStyleSet.h"
|
2016-09-13 05:48:45 +03:00
|
|
|
#include "mozilla/Tuple.h"
|
2016-02-17 03:19:26 +03:00
|
|
|
#include "mozilla/UniquePtr.h"
|
2016-06-07 23:10:18 +03:00
|
|
|
#include "nsAutoPtr.h"
|
2009-09-10 23:03:36 +04:00
|
|
|
#include "nsCOMArray.h"
|
|
|
|
#include "nsString.h"
|
2018-03-22 21:20:41 +03:00
|
|
|
#include "mozilla/ComputedStyle.h"
|
2009-09-10 23:03:36 +04:00
|
|
|
#include "nsComputedDOMStyle.h"
|
2016-02-17 23:37:00 +03:00
|
|
|
#include "nsCSSPseudoElements.h"
|
2011-03-28 20:51:59 +04:00
|
|
|
#include "mozilla/dom/Element.h"
|
2012-10-22 17:53:31 +04:00
|
|
|
#include "mozilla/FloatingPoint.h"
|
2012-10-26 17:32:10 +04:00
|
|
|
#include "mozilla/Likely.h"
|
2016-10-18 07:29:03 +03:00
|
|
|
#include "mozilla/ServoBindings.h" // RawServoDeclarationBlock
|
2017-12-01 12:35:47 +03:00
|
|
|
#include "mozilla/ServoCSSParser.h"
|
2011-07-23 02:28:07 +04:00
|
|
|
#include "gfxMatrix.h"
|
2011-09-27 01:53:33 +04:00
|
|
|
#include "gfxQuaternion.h"
|
2019-01-02 16:05:23 +03:00
|
|
|
#include "mozilla/dom/Document.h"
|
2015-07-20 05:30:40 +03:00
|
|
|
#include "nsIFrame.h"
|
2014-08-29 22:47:30 +04:00
|
|
|
#include "gfx2DGlue.h"
|
2018-03-22 21:20:41 +03:00
|
|
|
#include "mozilla/ComputedStyleInlines.h"
|
2009-09-10 23:03:36 +04:00
|
|
|
|
2011-10-11 09:50:08 +04:00
|
|
|
using namespace mozilla;
|
2016-09-16 08:36:39 +03:00
|
|
|
using namespace mozilla::css;
|
2014-08-29 22:47:30 +04:00
|
|
|
using namespace mozilla::gfx;
|
2016-10-04 10:00:31 +03:00
|
|
|
using nsStyleTransformMatrix::Decompose2DMatrix;
|
|
|
|
using nsStyleTransformMatrix::Decompose3DMatrix;
|
2016-10-12 07:36:58 +03:00
|
|
|
using nsStyleTransformMatrix::ShearType;
|
2010-06-29 02:49:35 +04:00
|
|
|
|
2018-03-20 20:04:19 +03:00
|
|
|
static already_AddRefed<nsCSSValue::Array> AppendFunction(
|
|
|
|
nsCSSKeyword aTransformFunction) {
|
|
|
|
uint32_t nargs;
|
|
|
|
switch (aTransformFunction) {
|
|
|
|
case eCSSKeyword_matrix3d:
|
|
|
|
nargs = 16;
|
2009-10-29 06:22:27 +03:00
|
|
|
break;
|
2018-03-20 20:04:19 +03:00
|
|
|
case eCSSKeyword_matrix:
|
|
|
|
nargs = 6;
|
2016-04-19 01:04:20 +03:00
|
|
|
break;
|
2018-03-20 20:04:19 +03:00
|
|
|
case eCSSKeyword_rotate3d:
|
|
|
|
nargs = 4;
|
2013-12-12 06:09:44 +04:00
|
|
|
break;
|
2018-03-20 20:04:19 +03:00
|
|
|
case eCSSKeyword_interpolatematrix:
|
|
|
|
case eCSSKeyword_accumulatematrix:
|
|
|
|
case eCSSKeyword_translate3d:
|
|
|
|
case eCSSKeyword_scale3d:
|
|
|
|
nargs = 3;
|
2009-12-22 00:46:25 +03:00
|
|
|
break;
|
2018-03-20 20:04:19 +03:00
|
|
|
case eCSSKeyword_translate:
|
|
|
|
case eCSSKeyword_skew:
|
|
|
|
case eCSSKeyword_scale:
|
|
|
|
nargs = 2;
|
2009-12-10 20:26:27 +03:00
|
|
|
break;
|
2018-03-20 20:04:19 +03:00
|
|
|
default:
|
|
|
|
NS_ERROR("must be a transform function");
|
|
|
|
MOZ_FALLTHROUGH;
|
|
|
|
case eCSSKeyword_translatex:
|
|
|
|
case eCSSKeyword_translatey:
|
|
|
|
case eCSSKeyword_translatez:
|
|
|
|
case eCSSKeyword_scalex:
|
|
|
|
case eCSSKeyword_scaley:
|
|
|
|
case eCSSKeyword_scalez:
|
|
|
|
case eCSSKeyword_skewx:
|
|
|
|
case eCSSKeyword_skewy:
|
|
|
|
case eCSSKeyword_rotate:
|
|
|
|
case eCSSKeyword_rotatex:
|
|
|
|
case eCSSKeyword_rotatey:
|
|
|
|
case eCSSKeyword_rotatez:
|
|
|
|
case eCSSKeyword_perspective:
|
|
|
|
nargs = 1;
|
2016-09-16 08:30:35 +03:00
|
|
|
break;
|
2009-10-20 15:46:16 +04:00
|
|
|
}
|
|
|
|
|
2018-03-20 20:04:19 +03:00
|
|
|
RefPtr<nsCSSValue::Array> arr = nsCSSValue::Array::Create(nargs + 1);
|
|
|
|
arr->Item(0).SetIntValue(aTransformFunction, eCSSUnit_Enumerated);
|
2009-12-22 00:46:25 +03:00
|
|
|
|
2018-03-20 20:04:19 +03:00
|
|
|
return arr.forget();
|
2009-10-20 15:46:16 +04:00
|
|
|
}
|
|
|
|
|
2017-03-31 12:46:37 +03:00
|
|
|
// AnimationValue Implementation
|
|
|
|
|
|
|
|
bool AnimationValue::operator==(const AnimationValue& aOther) const {
|
2017-03-31 06:04:08 +03:00
|
|
|
if (mServo && aOther.mServo) {
|
2017-03-31 12:46:37 +03:00
|
|
|
return Servo_AnimationValue_DeepEqual(mServo, aOther.mServo);
|
|
|
|
}
|
2018-02-01 07:04:04 +03:00
|
|
|
if (!mServo && !aOther.mServo) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2017-03-31 12:46:37 +03:00
|
|
|
}
|
|
|
|
|
2017-03-09 07:33:15 +03:00
|
|
|
bool AnimationValue::operator!=(const AnimationValue& aOther) const {
|
|
|
|
return !operator==(aOther);
|
|
|
|
}
|
|
|
|
|
2017-03-31 12:46:37 +03:00
|
|
|
float AnimationValue::GetOpacity() const {
|
2018-02-01 07:04:04 +03:00
|
|
|
MOZ_ASSERT(mServo);
|
2018-05-03 19:41:17 +03:00
|
|
|
return Servo_AnimationValue_GetOpacity(mServo);
|
2017-03-31 12:46:37 +03:00
|
|
|
}
|
|
|
|
|
2018-11-28 03:58:46 +03:00
|
|
|
nscolor AnimationValue::GetColor(nscolor aForegroundColor) const {
|
|
|
|
MOZ_ASSERT(mServo);
|
|
|
|
return Servo_AnimationValue_GetColor(mServo, aForegroundColor);
|
|
|
|
}
|
|
|
|
|
2017-09-25 09:25:43 +03:00
|
|
|
already_AddRefed<const nsCSSValueSharedList> AnimationValue::GetTransformList()
|
|
|
|
const {
|
2018-02-01 07:04:04 +03:00
|
|
|
MOZ_ASSERT(mServo);
|
2017-09-25 09:25:43 +03:00
|
|
|
|
|
|
|
RefPtr<nsCSSValueSharedList> transform;
|
2018-05-03 19:41:17 +03:00
|
|
|
Servo_AnimationValue_GetTransform(mServo, &transform);
|
2017-09-25 09:25:43 +03:00
|
|
|
return transform.forget();
|
|
|
|
}
|
|
|
|
|
2017-03-31 12:46:37 +03:00
|
|
|
Size AnimationValue::GetScaleValue(const nsIFrame* aFrame) const {
|
2018-02-01 07:04:04 +03:00
|
|
|
MOZ_ASSERT(mServo);
|
2018-05-03 19:41:17 +03:00
|
|
|
RefPtr<nsCSSValueSharedList> list;
|
|
|
|
Servo_AnimationValue_GetTransform(mServo, &list);
|
|
|
|
return nsStyleTransformMatrix::GetScaleValue(list, aFrame);
|
2017-03-31 12:46:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void AnimationValue::SerializeSpecifiedValue(nsCSSPropertyID aProperty,
|
|
|
|
nsAString& aString) const {
|
2018-02-01 07:04:04 +03:00
|
|
|
MOZ_ASSERT(mServo);
|
2018-05-03 19:41:17 +03:00
|
|
|
Servo_AnimationValue_Serialize(mServo, aProperty, &aString);
|
2017-03-31 12:46:37 +03:00
|
|
|
}
|
2017-04-12 11:26:59 +03:00
|
|
|
|
|
|
|
bool AnimationValue::IsInterpolableWith(nsCSSPropertyID aProperty,
|
|
|
|
const AnimationValue& aToValue) const {
|
|
|
|
if (IsNull() || aToValue.IsNull()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-02-01 07:04:04 +03:00
|
|
|
MOZ_ASSERT(mServo);
|
|
|
|
MOZ_ASSERT(aToValue.mServo);
|
2018-05-03 19:41:17 +03:00
|
|
|
return Servo_AnimationValues_IsInterpolable(mServo, aToValue.mServo);
|
2017-04-12 11:26:59 +03:00
|
|
|
}
|
2017-04-26 15:05:04 +03:00
|
|
|
|
|
|
|
double AnimationValue::ComputeDistance(nsCSSPropertyID aProperty,
|
|
|
|
const AnimationValue& aOther,
|
2018-03-22 21:20:41 +03:00
|
|
|
ComputedStyle* aComputedStyle) const {
|
2017-04-26 15:05:04 +03:00
|
|
|
if (IsNull() || aOther.IsNull()) {
|
|
|
|
return 0.0;
|
|
|
|
}
|
|
|
|
|
2018-02-01 07:04:04 +03:00
|
|
|
MOZ_ASSERT(mServo);
|
|
|
|
MOZ_ASSERT(aOther.mServo);
|
2017-04-26 15:05:04 +03:00
|
|
|
|
2018-05-03 19:41:17 +03:00
|
|
|
double distance =
|
|
|
|
Servo_AnimationValues_ComputeDistance(mServo, aOther.mServo);
|
|
|
|
return distance < 0.0 ? 0.0 : distance;
|
2017-04-26 15:05:04 +03:00
|
|
|
}
|
2017-05-03 06:15:27 +03:00
|
|
|
|
|
|
|
/* static */ AnimationValue AnimationValue::FromString(
|
|
|
|
nsCSSPropertyID aProperty, const nsAString& aValue, Element* aElement) {
|
|
|
|
MOZ_ASSERT(aElement);
|
|
|
|
|
|
|
|
AnimationValue result;
|
|
|
|
|
2019-01-02 16:05:23 +03:00
|
|
|
nsCOMPtr<Document> doc = aElement->GetComposedDoc();
|
2017-05-03 06:15:27 +03:00
|
|
|
if (!doc) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPresShell> shell = doc->GetShell();
|
|
|
|
if (!shell) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-03-22 21:20:41 +03:00
|
|
|
// GetComputedStyle() flushes style, so we shouldn't assume that any
|
2017-05-03 06:15:27 +03:00
|
|
|
// non-owning references we have are still valid.
|
2018-03-22 16:49:21 +03:00
|
|
|
RefPtr<ComputedStyle> computedStyle =
|
2018-03-22 21:20:41 +03:00
|
|
|
nsComputedDOMStyle::GetComputedStyle(aElement, nullptr);
|
2018-03-22 16:49:21 +03:00
|
|
|
MOZ_ASSERT(computedStyle);
|
2017-05-03 06:15:27 +03:00
|
|
|
|
2018-03-22 21:20:41 +03:00
|
|
|
RefPtr<RawServoDeclarationBlock> declarations = ServoCSSParser::ParseProperty(
|
|
|
|
aProperty, aValue, ServoCSSParser::GetParsingEnvironment(doc));
|
2017-05-03 06:15:27 +03:00
|
|
|
|
2018-03-22 21:20:41 +03:00
|
|
|
if (!declarations) {
|
2017-05-03 06:15:27 +03:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-03-29 14:15:46 +03:00
|
|
|
result.mServo = shell->StyleSet()->ComputeAnimationValue(
|
|
|
|
aElement, declarations, computedStyle);
|
2018-03-22 21:20:41 +03:00
|
|
|
return result;
|
2017-05-03 06:15:27 +03:00
|
|
|
}
|
2017-10-27 22:04:52 +03:00
|
|
|
|
2018-03-28 18:34:34 +03:00
|
|
|
/* static */ AnimationValue AnimationValue::Opacity(float aOpacity) {
|
2017-10-27 22:04:52 +03:00
|
|
|
AnimationValue result;
|
2018-03-28 18:34:34 +03:00
|
|
|
result.mServo = Servo_AnimationValue_Opacity(aOpacity).Consume();
|
2017-10-27 22:04:52 +03:00
|
|
|
return result;
|
|
|
|
}
|
2017-10-27 22:06:30 +03:00
|
|
|
|
|
|
|
/* static */ AnimationValue AnimationValue::Transform(
|
2018-03-28 18:34:34 +03:00
|
|
|
nsCSSValueSharedList& aList) {
|
2017-10-27 22:06:30 +03:00
|
|
|
AnimationValue result;
|
2018-03-28 18:34:34 +03:00
|
|
|
result.mServo = Servo_AnimationValue_Transform(aList).Consume();
|
2017-10-27 22:06:30 +03:00
|
|
|
return result;
|
|
|
|
}
|
2017-10-17 08:11:19 +03:00
|
|
|
|
|
|
|
/* static */ already_AddRefed<nsCSSValue::Array>
|
|
|
|
AnimationValue::AppendTransformFunction(nsCSSKeyword aTransformFunction,
|
|
|
|
nsCSSValueList**& aListTail) {
|
|
|
|
RefPtr<nsCSSValue::Array> arr = AppendFunction(aTransformFunction);
|
|
|
|
nsCSSValueList* item = new nsCSSValueList;
|
|
|
|
item->mValue.SetArrayValue(arr, eCSSUnit_Function);
|
|
|
|
|
|
|
|
*aListTail = item;
|
|
|
|
aListTail = &item->mNext;
|
|
|
|
|
|
|
|
return arr.forget();
|
|
|
|
}
|