1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
|
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/gfx/geometry/transform_operations.h"
#include <stddef.h>
#include <algorithm>
#include <utility>
#include "base/numerics/angle_conversions.h"
#include "ui/gfx/geometry/box_f.h"
#include "ui/gfx/geometry/transform_util.h"
#include "ui/gfx/geometry/vector3d_f.h"
namespace gfx {
TransformOperations::TransformOperations() = default;
TransformOperations::TransformOperations(const TransformOperations& other) {
operations_ = other.operations_;
}
TransformOperations::~TransformOperations() = default;
TransformOperations& TransformOperations::operator=(
const TransformOperations& other) {
operations_ = other.operations_;
return *this;
}
Transform TransformOperations::Apply() const {
return ApplyRemaining(0);
}
Transform TransformOperations::ApplyRemaining(size_t start) const {
Transform to_return;
for (size_t i = start; i < operations_.size(); i++) {
to_return.PreConcat(operations_[i].matrix);
}
return to_return;
}
// TODO(crbug.com/41431421): Consolidate blink and cc implementations of
// transform interpolation.
TransformOperations TransformOperations::Blend(const TransformOperations& from,
SkScalar progress) const {
TransformOperations to_return;
if (!BlendInternal(from, progress, &to_return)) {
// If the matrices cannot be blended, fallback to discrete animation logic.
// See https://drafts.csswg.org/css-transforms/#matrix-interpolation
to_return = progress < 0.5 ? from : *this;
}
return to_return;
}
bool TransformOperations::BlendedBoundsForBox(const BoxF& box,
const TransformOperations& from,
SkScalar min_progress,
SkScalar max_progress,
BoxF* bounds) const {
*bounds = box;
bool from_identity = from.IsIdentity();
bool to_identity = IsIdentity();
if (from_identity && to_identity)
return true;
if (!MatchesTypes(from))
return false;
size_t num_operations = std::max(from_identity ? 0 : from.operations_.size(),
to_identity ? 0 : operations_.size());
// Because we are squashing all of the matrices together when applying
// them to the animation, we must apply them in reverse order when
// not squashing them.
for (size_t i = 0; i < num_operations; ++i) {
size_t operation_index = num_operations - 1 - i;
BoxF bounds_for_operation;
const TransformOperation* from_op =
from_identity ? nullptr : &from.operations_[operation_index];
const TransformOperation* to_op =
to_identity ? nullptr : &operations_[operation_index];
if (!TransformOperation::BlendedBoundsForBox(*bounds, from_op, to_op,
min_progress, max_progress,
&bounds_for_operation)) {
return false;
}
*bounds = bounds_for_operation;
}
return true;
}
bool TransformOperations::PreservesAxisAlignment() const {
for (auto& operation : operations_) {
switch (operation.type) {
case TransformOperation::TRANSFORM_OPERATION_IDENTITY:
case TransformOperation::TRANSFORM_OPERATION_TRANSLATE:
case TransformOperation::TRANSFORM_OPERATION_SCALE:
continue;
case TransformOperation::TRANSFORM_OPERATION_MATRIX:
if (!operation.matrix.IsIdentity() &&
!operation.matrix.IsScaleOrTranslation())
return false;
continue;
case TransformOperation::TRANSFORM_OPERATION_ROTATE:
case TransformOperation::TRANSFORM_OPERATION_SKEWX:
case TransformOperation::TRANSFORM_OPERATION_SKEWY:
case TransformOperation::TRANSFORM_OPERATION_SKEW:
case TransformOperation::TRANSFORM_OPERATION_PERSPECTIVE:
return false;
}
}
return true;
}
bool TransformOperations::IsTranslation() const {
for (auto& operation : operations_) {
switch (operation.type) {
case TransformOperation::TRANSFORM_OPERATION_IDENTITY:
case TransformOperation::TRANSFORM_OPERATION_TRANSLATE:
continue;
case TransformOperation::TRANSFORM_OPERATION_MATRIX:
if (!operation.matrix.IsIdentityOrTranslation())
return false;
continue;
case TransformOperation::TRANSFORM_OPERATION_ROTATE:
case TransformOperation::TRANSFORM_OPERATION_SCALE:
case TransformOperation::TRANSFORM_OPERATION_SKEWX:
case TransformOperation::TRANSFORM_OPERATION_SKEWY:
case TransformOperation::TRANSFORM_OPERATION_SKEW:
case TransformOperation::TRANSFORM_OPERATION_PERSPECTIVE:
return false;
}
}
return true;
}
static SkScalar TanDegrees(double degrees) {
return SkDoubleToScalar(std::tan(base::DegToRad(degrees)));
}
bool TransformOperations::ScaleComponent(SkScalar* scale) const {
SkScalar operations_scale = 1.f;
for (auto& operation : operations_) {
switch (operation.type) {
case TransformOperation::TRANSFORM_OPERATION_IDENTITY:
case TransformOperation::TRANSFORM_OPERATION_TRANSLATE:
case TransformOperation::TRANSFORM_OPERATION_ROTATE:
continue;
case TransformOperation::TRANSFORM_OPERATION_MATRIX: {
if (operation.matrix.HasPerspective())
return false;
Vector2dF scale_components =
ComputeTransform2dScaleComponents(operation.matrix, 1.f);
operations_scale *=
std::max(scale_components.x(), scale_components.y());
break;
}
case TransformOperation::TRANSFORM_OPERATION_SKEWX:
case TransformOperation::TRANSFORM_OPERATION_SKEWY:
case TransformOperation::TRANSFORM_OPERATION_SKEW: {
SkScalar x_component = TanDegrees(operation.skew.x);
SkScalar y_component = TanDegrees(operation.skew.y);
SkScalar x_scale = std::sqrt(x_component * x_component + 1);
SkScalar y_scale = std::sqrt(y_component * y_component + 1);
operations_scale *= std::max(x_scale, y_scale);
break;
}
case TransformOperation::TRANSFORM_OPERATION_PERSPECTIVE:
return false;
case TransformOperation::TRANSFORM_OPERATION_SCALE:
operations_scale *= std::max(
std::abs(operation.scale.x),
std::max(std::abs(operation.scale.y), std::abs(operation.scale.z)));
}
}
*scale = operations_scale;
return true;
}
bool TransformOperations::MatchesTypes(const TransformOperations& other) const {
if (operations_.size() == 0 || other.operations_.size() == 0)
return true;
if (operations_.size() != other.operations_.size())
return false;
for (size_t i = 0; i < operations_.size(); ++i) {
if (operations_[i].type != other.operations_[i].type)
return false;
}
return true;
}
size_t TransformOperations::MatchingPrefixLength(
const TransformOperations& other) const {
size_t num_operations =
std::min(operations_.size(), other.operations_.size());
for (size_t i = 0; i < num_operations; ++i) {
if (operations_[i].type != other.operations_[i].type) {
// Remaining operations in each operations list require matrix/matrix3d
// interpolation.
return i;
}
}
// If the operations match to the length of the shorter list, then pad its
// length with the matching identity operations.
// https://drafts.csswg.org/css-transforms/#transform-function-lists
return std::max(operations_.size(), other.operations_.size());
}
bool TransformOperations::CanBlendWith(const TransformOperations& other) const {
TransformOperations dummy;
return BlendInternal(other, 0.5, &dummy);
}
void TransformOperations::AppendTranslate(SkScalar x, SkScalar y, SkScalar z) {
TransformOperation to_add;
to_add.matrix.Translate3d(x, y, z);
to_add.type = TransformOperation::TRANSFORM_OPERATION_TRANSLATE;
to_add.translate.x = x;
to_add.translate.y = y;
to_add.translate.z = z;
operations_.push_back(to_add);
decomposed_transforms_.clear();
}
void TransformOperations::AppendRotate(SkScalar x,
SkScalar y,
SkScalar z,
SkScalar degrees) {
TransformOperation to_add;
to_add.type = TransformOperation::TRANSFORM_OPERATION_ROTATE;
to_add.rotate.axis.x = x;
to_add.rotate.axis.y = y;
to_add.rotate.axis.z = z;
to_add.rotate.angle = degrees;
to_add.Bake();
operations_.push_back(to_add);
decomposed_transforms_.clear();
}
void TransformOperations::AppendScale(SkScalar x, SkScalar y, SkScalar z) {
TransformOperation to_add;
to_add.type = TransformOperation::TRANSFORM_OPERATION_SCALE;
to_add.scale.x = x;
to_add.scale.y = y;
to_add.scale.z = z;
to_add.Bake();
operations_.push_back(to_add);
decomposed_transforms_.clear();
}
void TransformOperations::AppendSkewX(SkScalar x) {
TransformOperation to_add;
to_add.type = TransformOperation::TRANSFORM_OPERATION_SKEWX;
to_add.skew.x = x;
to_add.skew.y = 0;
to_add.Bake();
operations_.push_back(to_add);
decomposed_transforms_.clear();
}
void TransformOperations::AppendSkewY(SkScalar y) {
TransformOperation to_add;
to_add.type = TransformOperation::TRANSFORM_OPERATION_SKEWY;
to_add.skew.x = 0;
to_add.skew.y = y;
to_add.Bake();
operations_.push_back(to_add);
decomposed_transforms_.clear();
}
void TransformOperations::AppendSkew(SkScalar x, SkScalar y) {
TransformOperation to_add;
to_add.type = TransformOperation::TRANSFORM_OPERATION_SKEW;
to_add.skew.x = x;
to_add.skew.y = y;
to_add.Bake();
operations_.push_back(to_add);
decomposed_transforms_.clear();
}
void TransformOperations::AppendPerspective(std::optional<SkScalar> depth) {
TransformOperation to_add;
to_add.type = TransformOperation::TRANSFORM_OPERATION_PERSPECTIVE;
if (depth) {
DCHECK_GE(*depth, 1.0f);
to_add.perspective_m43 = -1.0f / *depth;
} else {
to_add.perspective_m43 = 0.0f;
}
to_add.Bake();
operations_.push_back(to_add);
decomposed_transforms_.clear();
}
void TransformOperations::AppendMatrix(const Transform& matrix) {
TransformOperation to_add;
to_add.matrix = matrix;
to_add.type = TransformOperation::TRANSFORM_OPERATION_MATRIX;
operations_.push_back(to_add);
decomposed_transforms_.clear();
}
void TransformOperations::AppendIdentity() {
operations_.emplace_back();
}
void TransformOperations::Append(const TransformOperation& operation) {
operations_.push_back(operation);
decomposed_transforms_.clear();
}
bool TransformOperations::IsIdentity() const {
for (auto& operation : operations_) {
if (!operation.IsIdentity())
return false;
}
return true;
}
bool TransformOperations::ApproximatelyEqual(const TransformOperations& other,
SkScalar tolerance) const {
if (size() != other.size())
return false;
for (size_t i = 0; i < operations_.size(); ++i) {
if (!operations_[i].ApproximatelyEqual(other.operations_[i], tolerance))
return false;
}
return true;
}
bool TransformOperations::BlendInternal(const TransformOperations& from,
SkScalar progress,
TransformOperations* result) const {
bool from_identity = from.IsIdentity();
bool to_identity = IsIdentity();
if (from_identity && to_identity)
return true;
size_t matching_prefix_length = MatchingPrefixLength(from);
size_t from_size = from_identity ? 0 : from.operations_.size();
size_t to_size = to_identity ? 0 : operations_.size();
size_t num_operations = std::max(from_size, to_size);
for (size_t i = 0; i < matching_prefix_length; ++i) {
TransformOperation blended;
if (!TransformOperation::BlendTransformOperations(
i >= from_size ? nullptr : &from.operations_[i],
i >= to_size ? nullptr : &operations_[i], progress, &blended)) {
return false;
}
result->Append(blended);
}
if (matching_prefix_length < num_operations) {
if (!ComputeDecomposedTransform(matching_prefix_length) ||
!from.ComputeDecomposedTransform(matching_prefix_length)) {
return false;
}
DecomposedTransform matrix_transform = BlendDecomposedTransforms(
*decomposed_transforms_[matching_prefix_length].get(),
*from.decomposed_transforms_[matching_prefix_length].get(), progress);
result->AppendMatrix(Transform::Compose(matrix_transform));
}
return true;
}
bool TransformOperations::ComputeDecomposedTransform(
size_t start_offset) const {
auto it = decomposed_transforms_.find(start_offset);
if (it == decomposed_transforms_.end()) {
Transform transform = ApplyRemaining(start_offset);
if (std::optional<DecomposedTransform> decomp = transform.Decompose()) {
decomposed_transforms_[start_offset] =
std::make_unique<DecomposedTransform>(*decomp);
} else {
return false;
}
}
return true;
}
} // namespace gfx
|