transform_operations.cc 13 KB

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