Skip to content

Commit 20c73fa

Browse files
authored
chore: Return unique_ptr from TransformOperationInterpolator interpolate method (#8342)
## Summary As the title states, this PR changes the return type of the `interpolate` method declared on the `TransformOperationInterpolator` class to the `std::unique_ptr` from `folly::dynamic`. This is necessary to add support for SVG transforms handling were we have to create a single matrix from interpolated operation and can't convert transforms array to `folly::dynamic::array` like we do in plain transforms.
1 parent 9cbe664 commit 20c73fa

File tree

3 files changed

+57
-55
lines changed

3 files changed

+57
-55
lines changed

packages/react-native-reanimated/Common/cpp/reanimated/CSS/interpolation/transforms/TransformOperationInterpolator.cpp

Lines changed: 41 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,13 @@ std::shared_ptr<TransformOperation> TransformInterpolator::resolveOperation(
1313
return operation;
1414
}
1515

16-
folly::dynamic
16+
TransformOperationInterpolator<PerspectiveOperation>::
17+
TransformOperationInterpolator(
18+
const std::shared_ptr<PerspectiveOperation> &defaultOperation)
19+
: TransformOperationInterpolatorBase<PerspectiveOperation>(
20+
defaultOperation) {}
21+
22+
std::unique_ptr<TransformOperation>
1723
TransformOperationInterpolator<PerspectiveOperation>::interpolate(
1824
double progress,
1925
const std::shared_ptr<TransformOperation> &from,
@@ -24,16 +30,16 @@ TransformOperationInterpolator<PerspectiveOperation>::interpolate(
2430
const auto &toValue =
2531
std::static_pointer_cast<PerspectiveOperation>(to)->value;
2632

27-
if (fromValue.value == 0)
28-
return from->toDynamic();
29-
if (toValue.value == 0)
30-
return to->toDynamic();
31-
32-
return PerspectiveOperation(fromValue.interpolate(progress, toValue))
33-
.toDynamic();
33+
return std::make_unique<PerspectiveOperation>(
34+
fromValue.interpolate(progress, toValue));
3435
}
3536

36-
folly::dynamic TransformOperationInterpolator<MatrixOperation>::interpolate(
37+
TransformOperationInterpolator<MatrixOperation>::TransformOperationInterpolator(
38+
const std::shared_ptr<MatrixOperation> &defaultOperation)
39+
: TransformOperationInterpolatorBase<MatrixOperation>(defaultOperation) {}
40+
41+
std::unique_ptr<TransformOperation>
42+
TransformOperationInterpolator<MatrixOperation>::interpolate(
3743
double progress,
3844
const std::shared_ptr<TransformOperation> &from,
3945
const std::shared_ptr<TransformOperation> &to,
@@ -43,21 +49,18 @@ folly::dynamic TransformOperationInterpolator<MatrixOperation>::interpolate(
4349
const auto fromMatrix = matrixFromOperation(from, shouldBe3D, context);
4450
const auto toMatrix = matrixFromOperation(to, shouldBe3D, context);
4551

46-
folly::dynamic value;
47-
4852
if (shouldBe3D) {
49-
value = interpolateMatrix<TransformMatrix3D>(progress, fromMatrix, toMatrix)
50-
.toDynamic();
51-
} else {
52-
// Unfortunately 2D matrices aren't handled properly in RN, so we have to
53-
// convert them to 3D
54-
// see the issue: https://github.com/facebook/react-native/issues/53639
55-
const auto result2D =
56-
interpolateMatrix<TransformMatrix2D>(progress, fromMatrix, toMatrix);
57-
value = TransformMatrix3D::from2D(result2D).toDynamic();
53+
return std::make_unique<MatrixOperation>(
54+
interpolateMatrix<TransformMatrix3D>(progress, fromMatrix, toMatrix));
5855
}
5956

60-
return folly::dynamic::object(from->getOperationName(), value);
57+
const auto result2D =
58+
interpolateMatrix<TransformMatrix2D>(progress, fromMatrix, toMatrix);
59+
60+
// Unfortunately 2D matrices aren't handled properly in RN, so we have to
61+
// convert them to 3D
62+
// see the issue: https://github.com/facebook/react-native/issues/53639
63+
return std::make_unique<MatrixOperation>(TransformMatrix3D::from2D(result2D));
6164
}
6265

6366
template <typename MatrixType>
@@ -139,21 +142,23 @@ template TransformMatrix3D TransformOperationInterpolator<MatrixOperation>::
139142
const TransformMatrix::Shared &) const;
140143

141144
// Template implementations
142-
template <typename OperationType>
143-
TransformOperationInterpolator<OperationType>::TransformOperationInterpolator(
144-
std::shared_ptr<OperationType> defaultOperation)
145-
: TransformOperationInterpolatorBase<OperationType>(defaultOperation) {}
145+
template <typename TOperation>
146+
TransformOperationInterpolator<TOperation>::TransformOperationInterpolator(
147+
const std::shared_ptr<TOperation> &defaultOperation)
148+
: TransformOperationInterpolatorBase<TOperation>(defaultOperation) {}
146149

147-
template <typename OperationType>
148-
folly::dynamic TransformOperationInterpolator<OperationType>::interpolate(
150+
template <typename TOperation>
151+
std::unique_ptr<TransformOperation>
152+
TransformOperationInterpolator<TOperation>::interpolate(
149153
double progress,
150154
const std::shared_ptr<TransformOperation> &from,
151155
const std::shared_ptr<TransformOperation> &to,
152156
const TransformInterpolationContext &context) const {
153-
const auto &fromOp = *std::static_pointer_cast<OperationType>(from);
154-
const auto &toOp = *std::static_pointer_cast<OperationType>(to);
155-
return OperationType(fromOp.value.interpolate(progress, toOp.value))
156-
.toDynamic();
157+
const auto &fromOp = *std::static_pointer_cast<TOperation>(from);
158+
const auto &toOp = *std::static_pointer_cast<TOperation>(to);
159+
160+
return std::make_unique<TOperation>(
161+
fromOp.value.interpolate(progress, toOp.value));
157162
}
158163

159164
template <ResolvableTransformOp TOperation>
@@ -164,17 +169,17 @@ TransformOperationInterpolator<TOperation>::TransformOperationInterpolator(
164169
config_(std::move(config)) {}
165170

166171
template <ResolvableTransformOp TOperation>
167-
folly::dynamic TransformOperationInterpolator<TOperation>::interpolate(
172+
std::unique_ptr<TransformOperation>
173+
TransformOperationInterpolator<TOperation>::interpolate(
168174
double progress,
169175
const std::shared_ptr<TransformOperation> &from,
170176
const std::shared_ptr<TransformOperation> &to,
171177
const TransformInterpolationContext &context) const {
172178
const auto &fromOp = *std::static_pointer_cast<TOperation>(from);
173179
const auto &toOp = *std::static_pointer_cast<TOperation>(to);
174-
return TOperation(
175-
fromOp.value.interpolate(
176-
progress, toOp.value, getResolvableValueContext(context)))
177-
.toDynamic();
180+
181+
return std::make_unique<TOperation>(fromOp.value.interpolate(
182+
progress, toOp.value, getResolvableValueContext(context)));
178183
}
179184

180185
template <ResolvableTransformOp TOperation>

packages/react-native-reanimated/Common/cpp/reanimated/CSS/interpolation/transforms/TransformOperationInterpolator.h

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class TransformInterpolator {
3333
virtual ~TransformInterpolator() = default;
3434

3535
virtual std::shared_ptr<TransformOperation> getDefaultOperation() const = 0;
36-
virtual folly::dynamic interpolate(
36+
virtual std::unique_ptr<TransformOperation> interpolate(
3737
double progress,
3838
const std::shared_ptr<TransformOperation> &from,
3939
const std::shared_ptr<TransformOperation> &to,
@@ -47,30 +47,30 @@ using TransformOperationInterpolators = TransformInterpolator::Interpolators;
4747
using TransformInterpolationContext = TransformInterpolator::UpdateContext;
4848

4949
// Base class with common functionality
50-
template <typename OperationType>
50+
template <typename TOperation>
5151
class TransformOperationInterpolatorBase : public TransformInterpolator {
5252
public:
5353
TransformOperationInterpolatorBase(
54-
std::shared_ptr<OperationType> defaultOperation)
54+
std::shared_ptr<TOperation> defaultOperation)
5555
: defaultOperation_(defaultOperation) {}
5656

5757
std::shared_ptr<TransformOperation> getDefaultOperation() const override {
5858
return defaultOperation_;
5959
}
6060

6161
protected:
62-
std::shared_ptr<OperationType> defaultOperation_;
62+
std::shared_ptr<TOperation> defaultOperation_;
6363
};
6464

6565
// Base implementation for simple operations
66-
template <typename OperationType>
66+
template <typename TOperation>
6767
class TransformOperationInterpolator
68-
: public TransformOperationInterpolatorBase<OperationType> {
68+
: public TransformOperationInterpolatorBase<TOperation> {
6969
public:
7070
TransformOperationInterpolator(
71-
std::shared_ptr<OperationType> defaultOperation);
71+
const std::shared_ptr<TOperation> &defaultOperation);
7272

73-
folly::dynamic interpolate(
73+
std::unique_ptr<TransformOperation> interpolate(
7474
double progress,
7575
const std::shared_ptr<TransformOperation> &from,
7676
const std::shared_ptr<TransformOperation> &to,
@@ -83,11 +83,9 @@ class TransformOperationInterpolator<PerspectiveOperation>
8383
: public TransformOperationInterpolatorBase<PerspectiveOperation> {
8484
public:
8585
TransformOperationInterpolator(
86-
std::shared_ptr<PerspectiveOperation> defaultOperation)
87-
: TransformOperationInterpolatorBase<PerspectiveOperation>(
88-
defaultOperation) {}
86+
const std::shared_ptr<PerspectiveOperation> &defaultOperation);
8987

90-
folly::dynamic interpolate(
88+
std::unique_ptr<TransformOperation> interpolate(
9189
double progress,
9290
const std::shared_ptr<TransformOperation> &from,
9391
const std::shared_ptr<TransformOperation> &to,
@@ -100,10 +98,9 @@ class TransformOperationInterpolator<MatrixOperation>
10098
: public TransformOperationInterpolatorBase<MatrixOperation> {
10199
public:
102100
TransformOperationInterpolator(
103-
std::shared_ptr<MatrixOperation> defaultOperation)
104-
: TransformOperationInterpolatorBase<MatrixOperation>(defaultOperation) {}
101+
const std::shared_ptr<MatrixOperation> &defaultOperation);
105102

106-
folly::dynamic interpolate(
103+
std::unique_ptr<TransformOperation> interpolate(
107104
double progress,
108105
const std::shared_ptr<TransformOperation> &from,
109106
const std::shared_ptr<TransformOperation> &to,
@@ -131,7 +128,7 @@ class TransformOperationInterpolator<TOperation>
131128
const std::shared_ptr<TOperation> &defaultOperation,
132129
ResolvableValueInterpolatorConfig config);
133130

134-
folly::dynamic interpolate(
131+
std::unique_ptr<TransformOperation> interpolate(
135132
double progress,
136133
const std::shared_ptr<TransformOperation> &from,
137134
const std::shared_ptr<TransformOperation> &to,

packages/react-native-reanimated/Common/cpp/reanimated/CSS/interpolation/transforms/TransformsStyleInterpolator.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ folly::dynamic TransformsStyleInterpolator::interpolateOperations(
372372
const double keyframeProgress,
373373
const TransformOperations &fromOperations,
374374
const TransformOperations &toOperations) const {
375-
auto result = folly::dynamic::array();
375+
TransformOperations result;
376376
result.reserve(fromOperations.size());
377377

378378
const auto transformUpdateContext = TransformInterpolationContext{
@@ -384,15 +384,15 @@ folly::dynamic TransformsStyleInterpolator::interpolateOperations(
384384

385385
// fromOperation and toOperation have the same type
386386
const auto &interpolator = interpolators_->at(fromOperation->type);
387-
result.push_back(interpolator->interpolate(
387+
result.emplace_back(interpolator->interpolate(
388388
keyframeProgress, fromOperation, toOperation, transformUpdateContext));
389389
}
390390

391391
if (result.empty()) {
392392
return folly::dynamic();
393393
}
394394

395-
return result;
395+
return convertOperationsToDynamic(result);
396396
}
397397

398398
folly::dynamic TransformsStyleInterpolator::convertOperationsToDynamic(

0 commit comments

Comments
 (0)