react-native-macos 0.0.0-78b696cee → 0.0.0-7c6fa2fb1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (197) hide show
  1. package/Libraries/Animated/AnimatedEvent.js +23 -4
  2. package/Libraries/Animated/NativeAnimatedHelper.js +1 -1
  3. package/Libraries/Components/DatePicker/DatePickerIOS.ios.js +3 -6
  4. package/Libraries/Components/DrawerAndroid/DrawerLayoutAndroid.android.js +4 -7
  5. package/Libraries/Components/ScrollView/ScrollView.js +8 -5
  6. package/Libraries/Components/ScrollView/ScrollViewStickyHeader.js +268 -252
  7. package/Libraries/Core/ExceptionsManager.js +3 -1
  8. package/Libraries/Core/ReactNativeVersion.js +1 -1
  9. package/Libraries/Core/setUpBatchedBridge.js +1 -1
  10. package/Libraries/Core/setUpGlobals.js +2 -0
  11. package/Libraries/Core/setUpTimers.js +2 -2
  12. package/Libraries/Image/Image.ios.js +6 -0
  13. package/Libraries/LogBox/UI/LogBoxImages/alert-triangle.png +0 -0
  14. package/Libraries/LogBox/UI/LogBoxImages/chevron-left.png +0 -0
  15. package/Libraries/LogBox/UI/LogBoxImages/chevron-right.png +0 -0
  16. package/Libraries/LogBox/UI/LogBoxImages/close.png +0 -0
  17. package/Libraries/LogBox/UI/LogBoxImages/loader.png +0 -0
  18. package/Libraries/NativeAnimation/Nodes/RCTPropsAnimatedNode.m +0 -3
  19. package/Libraries/NativeAnimation/RCTNativeAnimatedModule.mm +6 -3
  20. package/Libraries/NativeAnimation/RCTNativeAnimatedTurboModule.h +1 -2
  21. package/Libraries/NativeAnimation/RCTNativeAnimatedTurboModule.mm +2 -1
  22. package/Libraries/NewAppScreen/components/logo.png +0 -0
  23. package/Libraries/Pressability/Pressability.js +1 -0
  24. package/Libraries/Pressability/PressabilityPerformanceEventEmitter.js +1 -0
  25. package/Libraries/Renderer/REVISION +1 -1
  26. package/Libraries/Renderer/implementations/ReactFabric-dev.fb.js +238 -47
  27. package/Libraries/Renderer/implementations/ReactFabric-prod.fb.js +84 -18
  28. package/Libraries/Renderer/implementations/ReactFabric-profiling.fb.js +84 -18
  29. package/Libraries/Renderer/implementations/ReactNativeRenderer-dev.fb.js +238 -47
  30. package/Libraries/Renderer/implementations/ReactNativeRenderer-prod.fb.js +84 -18
  31. package/Libraries/Renderer/implementations/ReactNativeRenderer-profiling.fb.js +84 -18
  32. package/Libraries/StyleSheet/normalizeColor.js +2 -2
  33. package/Libraries/Text/TextProps.js +1 -7
  34. package/Libraries/TurboModule/TurboModuleRegistry.js +1 -1
  35. package/Libraries/Utilities/HMRClient.js +1 -1
  36. package/Libraries/WebSocket/RCTSRWebSocket.m +23 -0
  37. package/React/Base/RCTBridge+Private.h +8 -0
  38. package/React/Base/RCTBridge.h +4 -0
  39. package/React/Base/RCTBridge.m +17 -0
  40. package/React/Base/RCTBridgeModule.h +1 -1
  41. package/React/Base/RCTBundleURLProvider.h +1 -1
  42. package/React/Base/RCTBundleURLProvider.mm +2 -2
  43. package/React/Base/RCTDefines.h +12 -3
  44. package/React/Base/RCTEventDispatcherProtocol.h +2 -0
  45. package/React/Base/RCTRootView.h +15 -0
  46. package/React/Base/RCTRootView.m +15 -0
  47. package/React/Base/RCTVersion.m +1 -1
  48. package/React/Base/Surface/SurfaceHostingView/RCTSurfaceHostingProxyRootView.h +11 -0
  49. package/React/Base/Surface/SurfaceHostingView/RCTSurfaceHostingProxyRootView.mm +35 -1
  50. package/React/CoreModules/RCTDevMenu.mm +4 -4
  51. package/React/CoreModules/RCTDevSettings.mm +6 -6
  52. package/React/CoreModules/RCTDevSplitBundleLoader.mm +1 -1
  53. package/React/CoreModules/RCTDeviceInfo.mm +4 -4
  54. package/React/CoreModules/RCTEventDispatcher.mm +5 -0
  55. package/React/CoreModules/RCTPerfMonitor.mm +9 -4
  56. package/React/CxxBridge/RCTCxxBridge.mm +18 -1
  57. package/React/FBReactNativeSpec/FBReactNativeSpec.podspec +11 -3
  58. package/React/Fabric/Mounting/ComponentViews/ScrollView/RCTScrollViewComponentView.mm +9 -0
  59. package/React/Fabric/Mounting/RCTMountingManager.mm +15 -1
  60. package/React/Fabric/RCTScheduler.mm +10 -3
  61. package/React/Modules/RCTUIManager.m +19 -10
  62. package/React/Views/RCTFont.mm +58 -47
  63. package/React/Views/RCTViewManager.m +3 -2
  64. package/ReactAndroid/build.gradle +12 -6
  65. package/ReactAndroid/gradle.properties +1 -1
  66. package/ReactAndroid/src/androidTest/java/com/facebook/react/tests/BUCK +0 -1
  67. package/ReactAndroid/src/androidTest/js/TestBundle.js +0 -7
  68. package/ReactAndroid/src/main/java/com/facebook/react/BUCK +0 -1
  69. package/ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManager.java +24 -6
  70. package/ReactAndroid/src/main/java/com/facebook/react/bridge/CatalystInstance.java +4 -0
  71. package/ReactAndroid/src/main/java/com/facebook/react/bridge/CatalystInstanceImpl.java +4 -0
  72. package/ReactAndroid/src/main/java/com/facebook/react/bridge/ReactCxxErrorHandler.java +39 -0
  73. package/ReactAndroid/src/main/java/com/facebook/react/bridge/RuntimeScheduler.java +21 -0
  74. package/ReactAndroid/src/main/java/com/facebook/react/config/ReactFeatureFlags.java +6 -0
  75. package/ReactAndroid/src/main/java/com/facebook/react/fabric/Binding.java +5 -0
  76. package/ReactAndroid/src/main/java/com/facebook/react/fabric/FabricJSIModuleProvider.java +1 -0
  77. package/ReactAndroid/src/main/java/com/facebook/react/fabric/jni/Android.mk +2 -1
  78. package/ReactAndroid/src/main/java/com/facebook/react/fabric/jni/Binding.cpp +14 -0
  79. package/ReactAndroid/src/main/java/com/facebook/react/fabric/jni/Binding.h +2 -0
  80. package/ReactAndroid/src/main/java/com/facebook/react/fabric/mounting/MountItemDispatcher.java +9 -1
  81. package/ReactAndroid/src/main/java/com/facebook/react/fabric/mounting/SurfaceMountingManager.java +19 -0
  82. package/ReactAndroid/src/main/java/com/facebook/react/modules/core/{JavaScriptTimerManager.java → JavaScriptTimerExecutor.java} +1 -1
  83. package/ReactAndroid/src/main/java/com/facebook/react/modules/core/JavaTimerManager.java +7 -7
  84. package/ReactAndroid/src/main/java/com/facebook/react/modules/core/TimingModule.java +2 -2
  85. package/ReactAndroid/src/main/java/com/facebook/react/modules/i18nmanager/I18nManagerModule.java +7 -1
  86. package/ReactAndroid/src/main/java/com/facebook/react/modules/intent/IntentModule.java +23 -19
  87. package/ReactAndroid/src/main/java/com/facebook/react/modules/network/ForwardingCookieHandler.java +1 -4
  88. package/ReactAndroid/src/main/java/com/facebook/react/modules/systeminfo/ReactNativeVersion.java +1 -1
  89. package/ReactAndroid/src/main/java/com/facebook/react/shell/BUCK +0 -1
  90. package/ReactAndroid/src/main/java/com/facebook/react/shell/MainReactPackage.java +0 -5
  91. package/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextAnchorViewManager.java +0 -4
  92. package/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextShadowNode.java +16 -0
  93. package/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextView.java +8 -0
  94. package/ReactAndroid/src/main/java/com/facebook/react/views/text/TextAttributeProps.java +20 -1
  95. package/ReactAndroid/src/main/java/com/facebook/react/views/text/TextLayoutManager.java +25 -6
  96. package/ReactAndroid/src/main/java/com/facebook/react/views/text/TextLayoutManagerMapBuffer.java +25 -5
  97. package/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditTextInputConnectionWrapper.java +3 -0
  98. package/ReactAndroid/src/main/jni/react/jni/Android.mk +3 -2
  99. package/ReactAndroid/src/main/jni/react/jni/BUCK +2 -0
  100. package/ReactAndroid/src/main/jni/react/jni/CatalystInstanceImpl.cpp +29 -0
  101. package/ReactAndroid/src/main/jni/react/jni/CatalystInstanceImpl.h +4 -0
  102. package/ReactAndroid/src/main/jni/react/jni/JReactCxxErrorHandler.cpp +18 -0
  103. package/ReactAndroid/src/main/jni/react/jni/JReactCxxErrorHandler.h +25 -0
  104. package/ReactAndroid/src/main/jni/react/jni/JRuntimeScheduler.cpp +22 -0
  105. package/ReactAndroid/src/main/jni/react/jni/JRuntimeScheduler.h +30 -0
  106. package/ReactAndroid/src/main/third-party/android/androidx/BUCK +6 -6
  107. package/ReactCommon/React-Fabric.podspec +9 -0
  108. package/ReactCommon/cxxreact/ReactNativeVersion.h +1 -1
  109. package/ReactCommon/hermes/inspector/tools/msggen/package.json +7 -4
  110. package/ReactCommon/hermes/inspector/tools/msggen/yarn.lock +13 -10
  111. package/ReactCommon/react/nativemodule/core/platform/ios/RCTTurboModuleManager.mm +14 -31
  112. package/ReactCommon/react/renderer/animations/BUCK +1 -0
  113. package/ReactCommon/react/renderer/animations/LayoutAnimationCallbackWrapper.h +34 -0
  114. package/ReactCommon/react/renderer/animations/LayoutAnimationDriver.cpp +5 -26
  115. package/ReactCommon/react/renderer/animations/LayoutAnimationDriver.h +3 -13
  116. package/ReactCommon/react/renderer/animations/LayoutAnimationKeyFrameManager.cpp +1399 -1622
  117. package/ReactCommon/react/renderer/animations/LayoutAnimationKeyFrameManager.h +76 -244
  118. package/ReactCommon/react/renderer/animations/conversions.h +212 -0
  119. package/ReactCommon/react/renderer/animations/primitives.h +104 -0
  120. package/ReactCommon/react/renderer/animations/utils.cpp +71 -0
  121. package/ReactCommon/react/renderer/animations/utils.h +80 -0
  122. package/ReactCommon/react/renderer/attributedstring/AttributedString.h +0 -1
  123. package/ReactCommon/react/renderer/attributedstring/ParagraphAttributes.cpp +7 -3
  124. package/ReactCommon/react/renderer/attributedstring/ParagraphAttributes.h +12 -2
  125. package/ReactCommon/react/renderer/attributedstring/TextAttributes.h +1 -1
  126. package/ReactCommon/react/renderer/attributedstring/conversions.h +58 -3
  127. package/ReactCommon/react/renderer/attributedstring/primitives.h +18 -1
  128. package/ReactCommon/react/renderer/components/text/ParagraphComponentDescriptor.h +1 -17
  129. package/ReactCommon/react/renderer/components/view/Android.mk +1 -1
  130. package/ReactCommon/react/renderer/components/view/ViewComponentDescriptor.h +0 -23
  131. package/ReactCommon/react/renderer/components/view/ViewPropsInterpolation.h +2 -3
  132. package/ReactCommon/react/renderer/components/view/ViewShadowNode.cpp +1 -0
  133. package/ReactCommon/react/renderer/components/view/ViewShadowNode.h +1 -1
  134. package/ReactCommon/react/renderer/components/view/accessibilityPropsConversions.h +4 -0
  135. package/ReactCommon/react/renderer/core/ConcreteComponentDescriptor.h +14 -6
  136. package/ReactCommon/react/renderer/core/ConcreteShadowNode.h +1 -0
  137. package/ReactCommon/react/renderer/core/Props.h +1 -0
  138. package/ReactCommon/react/renderer/core/PropsParserContext.h +3 -2
  139. package/ReactCommon/react/renderer/core/RawPropsKey.cpp +10 -2
  140. package/ReactCommon/react/renderer/core/ReactPrimitives.h +0 -2
  141. package/ReactCommon/react/renderer/core/ShadowNodeFamily.h +1 -0
  142. package/ReactCommon/react/renderer/graphics/Android.mk +1 -1
  143. package/ReactCommon/react/renderer/graphics/Transform.cpp +1 -1
  144. package/ReactCommon/react/renderer/graphics/Transform.h +1 -1
  145. package/ReactCommon/react/renderer/mounting/Android.mk +2 -1
  146. package/ReactCommon/react/renderer/mounting/MountingOverrideDelegate.h +1 -1
  147. package/ReactCommon/react/renderer/mounting/ShadowViewMutation.cpp +12 -0
  148. package/ReactCommon/react/renderer/mounting/ShadowViewMutation.h +10 -1
  149. package/ReactCommon/react/renderer/runtimescheduler/BUCK +0 -1
  150. package/ReactCommon/react/renderer/runtimescheduler/RuntimeScheduler.cpp +4 -2
  151. package/ReactCommon/react/renderer/runtimescheduler/RuntimeScheduler.h +4 -2
  152. package/ReactCommon/react/renderer/runtimescheduler/RuntimeSchedulerBinding.cpp +4 -3
  153. package/ReactCommon/react/renderer/runtimescheduler/RuntimeSchedulerBinding.h +4 -2
  154. package/ReactCommon/react/renderer/runtimescheduler/RuntimeSchedulerClock.h +4 -2
  155. package/ReactCommon/react/renderer/runtimescheduler/SchedulerPriority.h +4 -3
  156. package/ReactCommon/react/renderer/runtimescheduler/Task.cpp +4 -2
  157. package/ReactCommon/react/renderer/runtimescheduler/Task.h +4 -2
  158. package/ReactCommon/react/renderer/runtimescheduler/primitives.h +4 -2
  159. package/ReactCommon/react/renderer/scheduler/SurfaceHandler.h +4 -4
  160. package/ReactCommon/react/renderer/textlayoutmanager/platform/ios/RCTAttributedTextUtils.h +2 -0
  161. package/ReactCommon/react/renderer/textlayoutmanager/platform/ios/RCTAttributedTextUtils.mm +19 -0
  162. package/ReactCommon/react/renderer/timeline/BUCK +6 -6
  163. package/ReactCommon/react/renderer/uimanager/UIManagerAnimationDelegate.h +1 -1
  164. package/ReactCommon/react/test_utils/BUCK +3 -0
  165. package/ReactCommon/react/test_utils/MockSurfaceHandler.h +25 -0
  166. package/ReactCommon/react/utils/ContextContainer.h +0 -27
  167. package/flow/global.js +45 -0
  168. package/index.js +15 -10
  169. package/package.json +4 -4
  170. package/react.gradle +1 -1
  171. package/scripts/generate-specs-cli.js +18 -0
  172. package/scripts/ios-configure-glog.sh +4 -0
  173. package/scripts/react_native_pods.rb +156 -37
  174. package/template/_flowconfig +1 -1
  175. package/template/android/gradle/wrapper/gradle-wrapper.properties +1 -1
  176. package/template/ios/HelloWorld.xcodeproj/project.pbxproj +0 -2
  177. package/template/package.json +1 -1
  178. package/Libraries/Components/DatePickerAndroid/DatePickerAndroid.android.js +0 -87
  179. package/Libraries/Components/DatePickerAndroid/DatePickerAndroid.ios.js +0 -30
  180. package/Libraries/Components/DatePickerAndroid/DatePickerAndroidTypes.js +0 -30
  181. package/ReactAndroid/src/androidTest/java/com/facebook/react/tests/DatePickerDialogTestCase.java +0 -172
  182. package/ReactAndroid/src/androidTest/js/DatePickerDialogTestModule.js +0 -45
  183. package/ReactAndroid/src/main/java/com/facebook/react/modules/datepicker/BUCK +0 -27
  184. package/ReactAndroid/src/main/java/com/facebook/react/modules/datepicker/DatePickerDialogFragment.java +0 -137
  185. package/ReactAndroid/src/main/java/com/facebook/react/modules/datepicker/DatePickerDialogModule.java +0 -153
  186. package/ReactAndroid/src/main/java/com/facebook/react/modules/datepicker/DatePickerMode.java +0 -15
  187. package/ReactAndroid/src/main/java/com/facebook/react/modules/datepicker/DismissableDatePickerDialog.java +0 -117
  188. package/ReactAndroid/src/main/java/com/facebook/react/runtimescheduler/BUCK +0 -20
  189. package/ReactAndroid/src/main/java/com/facebook/react/runtimescheduler/RuntimeSchedulerManager.java +0 -39
  190. package/ReactAndroid/src/main/java/com/facebook/react/runtimescheduler/jni/Android.mk +0 -34
  191. package/ReactAndroid/src/main/java/com/facebook/react/runtimescheduler/jni/BUCK +0 -35
  192. package/ReactAndroid/src/main/java/com/facebook/react/runtimescheduler/jni/OnLoad.cpp +0 -15
  193. package/ReactAndroid/src/main/java/com/facebook/react/runtimescheduler/jni/RuntimeSchedulerManager.cpp +0 -46
  194. package/ReactAndroid/src/main/java/com/facebook/react/runtimescheduler/jni/RuntimeSchedulerManager.h +0 -40
  195. package/scripts/generate-specs.sh +0 -99
  196. package/template/_editorconfig +0 -3
  197. package/template/_gitattributes +0 -3
@@ -8,25 +8,24 @@
8
8
  #include "LayoutAnimationKeyFrameManager.h"
9
9
 
10
10
  #include <algorithm>
11
- #include <chrono>
12
11
 
13
12
  #include <react/debug/flags.h>
14
13
  #include <react/debug/react_native_assert.h>
15
14
 
15
+ #include <logger/react_native_log.h>
16
+ #include <react/renderer/animations/conversions.h>
17
+ #include <react/renderer/animations/utils.h>
16
18
  #include <react/renderer/componentregistry/ComponentDescriptorFactory.h>
19
+ #include <react/renderer/components/image/ImageProps.h>
17
20
  #include <react/renderer/components/view/ViewProps.h>
18
21
  #include <react/renderer/core/ComponentDescriptor.h>
19
22
  #include <react/renderer/core/LayoutMetrics.h>
20
- #include <react/renderer/core/LayoutableShadowNode.h>
21
23
  #include <react/renderer/core/Props.h>
22
24
  #include <react/renderer/core/PropsParserContext.h>
23
25
  #include <react/renderer/core/RawValue.h>
24
26
  #include <react/renderer/mounting/MountingCoordinator.h>
25
- #include <react/renderer/mounting/ShadowViewMutation.h>
26
-
27
- #include <react/renderer/mounting/Differentiator.h>
28
- #include <react/renderer/mounting/ShadowTreeRevision.h>
29
27
  #include <react/renderer/mounting/ShadowView.h>
28
+ #include <react/renderer/mounting/ShadowViewMutation.h>
30
29
 
31
30
  #include <glog/logging.h>
32
31
 
@@ -74,6 +73,7 @@ void PrintMutationInstruction(
74
73
  return stream;
75
74
  }(LOG(ERROR));
76
75
  }
76
+
77
77
  void PrintMutationInstructionRelative(
78
78
  std::string message,
79
79
  ShadowViewMutation const &mutation,
@@ -85,199 +85,25 @@ void PrintMutationInstructionRelative(
85
85
  }
86
86
  #endif
87
87
 
88
- static better::optional<AnimationType> parseAnimationType(std::string param) {
89
- if (param == "spring") {
90
- return AnimationType::Spring;
91
- }
92
- if (param == "linear") {
93
- return AnimationType::Linear;
94
- }
95
- if (param == "easeInEaseOut") {
96
- return AnimationType::EaseInEaseOut;
97
- }
98
- if (param == "easeIn") {
99
- return AnimationType::EaseIn;
100
- }
101
- if (param == "easeOut") {
102
- return AnimationType::EaseOut;
103
- }
104
- if (param == "keyboard") {
105
- return AnimationType::Keyboard;
106
- }
107
-
108
- LOG(ERROR) << "Error parsing animation type: " << param;
109
- return {};
110
- }
111
-
112
- static better::optional<AnimationProperty> parseAnimationProperty(
113
- std::string param) {
114
- if (param == "opacity") {
115
- return AnimationProperty::Opacity;
116
- }
117
- if (param == "scaleX") {
118
- return AnimationProperty::ScaleX;
119
- }
120
- if (param == "scaleY") {
121
- return AnimationProperty::ScaleY;
122
- }
123
- if (param == "scaleXY") {
124
- return AnimationProperty::ScaleXY;
125
- }
126
-
127
- LOG(ERROR) << "Error parsing animation property: " << param;
128
- return {};
129
- }
130
-
131
- static better::optional<AnimationConfig> parseAnimationConfig(
132
- folly::dynamic const &config,
133
- double defaultDuration,
134
- bool parsePropertyType) {
135
- if (config.empty() || !config.isObject()) {
136
- return AnimationConfig{
137
- AnimationType::Linear,
138
- AnimationProperty::NotApplicable,
139
- defaultDuration,
140
- 0,
141
- 0,
142
- 0};
143
- }
144
-
145
- auto const typeIt = config.find("type");
146
- if (typeIt == config.items().end()) {
147
- LOG(ERROR) << "Error parsing animation config: could not find field `type`";
148
- return {};
149
- }
150
- auto const animationTypeParam = typeIt->second;
151
- if (animationTypeParam.empty() || !animationTypeParam.isString()) {
152
- LOG(ERROR)
153
- << "Error parsing animation config: could not unwrap field `type`";
154
- return {};
155
- }
156
- const auto animationType = parseAnimationType(animationTypeParam.asString());
157
- if (!animationType) {
158
- LOG(ERROR)
159
- << "Error parsing animation config: could not parse field `type`";
160
- return {};
161
- }
162
-
163
- AnimationProperty animationProperty = AnimationProperty::NotApplicable;
164
- if (parsePropertyType) {
165
- auto const propertyIt = config.find("property");
166
- if (propertyIt == config.items().end()) {
167
- LOG(ERROR)
168
- << "Error parsing animation config: could not find field `property`";
169
- return {};
170
- }
171
- auto const animationPropertyParam = propertyIt->second;
172
- if (animationPropertyParam.empty() || !animationPropertyParam.isString()) {
173
- LOG(ERROR)
174
- << "Error parsing animation config: could not unwrap field `property`";
175
- return {};
176
- }
177
- const auto animationPropertyParsed =
178
- parseAnimationProperty(animationPropertyParam.asString());
179
- if (!animationPropertyParsed) {
180
- LOG(ERROR)
181
- << "Error parsing animation config: could not parse field `property`";
182
- return {};
183
- }
184
- animationProperty = *animationPropertyParsed;
185
- }
186
-
187
- double duration = defaultDuration;
188
- double delay = 0;
189
- double springDamping = 0.5;
190
- double initialVelocity = 0;
191
-
192
- auto const durationIt = config.find("duration");
193
- if (durationIt != config.items().end()) {
194
- if (durationIt->second.isDouble()) {
195
- duration = durationIt->second.asDouble();
196
- } else {
197
- LOG(ERROR)
198
- << "Error parsing animation config: field `duration` must be a number";
199
- return {};
200
- }
201
- }
202
-
203
- auto const delayIt = config.find("delay");
204
- if (delayIt != config.items().end()) {
205
- if (delayIt->second.isDouble()) {
206
- delay = delayIt->second.asDouble();
207
- } else {
208
- LOG(ERROR)
209
- << "Error parsing animation config: field `delay` must be a number";
210
- return {};
211
- }
212
- }
213
-
214
- auto const springDampingIt = config.find("springDamping");
215
- if (springDampingIt != config.items().end() &&
216
- springDampingIt->second.isDouble()) {
217
- if (springDampingIt->second.isDouble()) {
218
- springDamping = springDampingIt->second.asDouble();
219
- } else {
220
- LOG(ERROR)
221
- << "Error parsing animation config: field `springDamping` must be a number";
222
- return {};
223
- }
224
- }
225
-
226
- auto const initialVelocityIt = config.find("initialVelocity");
227
- if (initialVelocityIt != config.items().end()) {
228
- if (initialVelocityIt->second.isDouble()) {
229
- initialVelocity = initialVelocityIt->second.asDouble();
230
- } else {
231
- LOG(ERROR)
232
- << "Error parsing animation config: field `initialVelocity` must be a number";
233
- return {};
234
- }
235
- }
236
-
237
- return better::optional<AnimationConfig>(AnimationConfig{
238
- *animationType,
239
- animationProperty,
240
- duration,
241
- delay,
242
- springDamping,
243
- initialVelocity});
88
+ static inline float
89
+ interpolateFloats(float coefficient, float oldValue, float newValue) {
90
+ return oldValue + (newValue - oldValue) * coefficient;
244
91
  }
245
92
 
246
- // Parse animation config from JS
247
- static better::optional<LayoutAnimationConfig> parseLayoutAnimationConfig(
248
- folly::dynamic const &config) {
249
- if (config.empty() || !config.isObject()) {
250
- return {};
251
- }
252
-
253
- const auto durationIt = config.find("duration");
254
- if (durationIt == config.items().end() || !durationIt->second.isDouble()) {
255
- return {};
256
- }
257
- const double duration = durationIt->second.asDouble();
258
-
259
- const auto createConfigIt = config.find("create");
260
- const auto createConfig = createConfigIt == config.items().end()
261
- ? better::optional<AnimationConfig>(AnimationConfig{})
262
- : parseAnimationConfig(createConfigIt->second, duration, true);
263
-
264
- const auto updateConfigIt = config.find("update");
265
- const auto updateConfig = updateConfigIt == config.items().end()
266
- ? better::optional<AnimationConfig>(AnimationConfig{})
267
- : parseAnimationConfig(updateConfigIt->second, duration, false);
93
+ #pragma mark -
268
94
 
269
- const auto deleteConfigIt = config.find("delete");
270
- const auto deleteConfig = deleteConfigIt == config.items().end()
271
- ? better::optional<AnimationConfig>(AnimationConfig{})
272
- : parseAnimationConfig(deleteConfigIt->second, duration, true);
95
+ LayoutAnimationKeyFrameManager::LayoutAnimationKeyFrameManager(
96
+ RuntimeExecutor runtimeExecutor,
97
+ LayoutAnimationStatusDelegate *delegate)
98
+ : runtimeExecutor_(runtimeExecutor),
99
+ layoutAnimationStatusDelegate_(delegate),
100
+ now_([]() {
101
+ return std::chrono::duration_cast<std::chrono::milliseconds>(
102
+ std::chrono::high_resolution_clock::now().time_since_epoch())
103
+ .count();
104
+ }) {}
273
105
 
274
- if (!createConfig || !updateConfig || !deleteConfig) {
275
- return {};
276
- }
277
-
278
- return better::optional<LayoutAnimationConfig>(LayoutAnimationConfig{
279
- duration, *createConfig, *updateConfig, *deleteConfig});
280
- }
106
+ #pragma mark UIManagerAnimationDelegate methods
281
107
 
282
108
  /**
283
109
  * Globally configure next LayoutAnimation.
@@ -323,24 +149,9 @@ void LayoutAnimationKeyFrameManager::uiManagerDidConfigureNextLayoutAnimation(
323
149
  }
324
150
  }
325
151
 
326
- void LayoutAnimationKeyFrameManager::uiManagerDidConfigureNextLayoutAnimation(
327
- LayoutAnimation layoutAnimation) const {
328
- currentAnimation_ = better::optional<LayoutAnimation>{layoutAnimation};
329
- }
330
-
331
- void LayoutAnimationKeyFrameManager::setLayoutAnimationStatusDelegate(
332
- LayoutAnimationStatusDelegate *delegate) const {
333
- std::lock_guard<std::mutex> lock(layoutAnimationStatusDelegateMutex_);
334
- layoutAnimationStatusDelegate_ = delegate;
335
- }
336
-
337
- bool LayoutAnimationKeyFrameManager::shouldOverridePullTransaction() const {
338
- return shouldAnimateFrame();
339
- }
340
-
341
- void LayoutAnimationKeyFrameManager::stopSurface(SurfaceId surfaceId) {
342
- std::lock_guard<std::mutex> lock(surfaceIdsToStopMutex_);
343
- surfaceIdsToStop_.push_back(surfaceId);
152
+ void LayoutAnimationKeyFrameManager::setComponentDescriptorRegistry(
153
+ const SharedComponentDescriptorRegistry &componentDescriptorRegistry) {
154
+ componentDescriptorRegistry_ = componentDescriptorRegistry;
344
155
  }
345
156
 
346
157
  bool LayoutAnimationKeyFrameManager::shouldAnimateFrame() const {
@@ -348,1567 +159,1533 @@ bool LayoutAnimationKeyFrameManager::shouldAnimateFrame() const {
348
159
  return currentAnimation_ || !inflightAnimations_.empty();
349
160
  }
350
161
 
351
- static inline const float
352
- interpolateFloats(float coefficient, float oldValue, float newValue) {
353
- return oldValue + (newValue - oldValue) * coefficient;
162
+ void LayoutAnimationKeyFrameManager::stopSurface(SurfaceId surfaceId) {
163
+ std::lock_guard<std::mutex> lock(surfaceIdsToStopMutex_);
164
+ surfaceIdsToStop_.insert(surfaceId);
354
165
  }
355
166
 
356
- std::pair<double, double>
357
- LayoutAnimationKeyFrameManager::calculateAnimationProgress(
358
- uint64_t now,
359
- const LayoutAnimation &animation,
360
- const AnimationConfig &mutationConfig) const {
361
- if (mutationConfig.animationType == AnimationType::None) {
362
- return {1, 1};
363
- }
364
-
365
- uint64_t startTime = animation.startTime;
366
- uint64_t delay = mutationConfig.delay;
367
- uint64_t endTime = startTime + delay + mutationConfig.duration;
368
-
369
- static const float PI = 3.14159265358979323846;
370
-
371
- if (now >= endTime) {
372
- return {1, 1};
373
- }
374
- if (now < startTime + delay) {
375
- return {0, 0};
376
- }
167
+ #pragma mark - MountingOverrideDelegate methods
377
168
 
378
- double linearTimeProgression = 1 -
379
- (double)(endTime - delay - now) / (double)(endTime - animation.startTime);
380
-
381
- if (mutationConfig.animationType == AnimationType::Linear) {
382
- return {linearTimeProgression, linearTimeProgression};
383
- } else if (mutationConfig.animationType == AnimationType::EaseIn) {
384
- // This is an accelerator-style interpolator.
385
- // In the future, this parameter (2.0) could be adjusted. This has been the
386
- // default for Classic RN forever.
387
- return {linearTimeProgression, pow(linearTimeProgression, 2.0)};
388
- } else if (mutationConfig.animationType == AnimationType::EaseOut) {
389
- // This is an decelerator-style interpolator.
390
- // In the future, this parameter (2.0) could be adjusted. This has been the
391
- // default for Classic RN forever.
392
- return {linearTimeProgression, 1.0 - pow(1 - linearTimeProgression, 2.0)};
393
- } else if (mutationConfig.animationType == AnimationType::EaseInEaseOut) {
394
- // This is a combination of accelerate+decelerate.
395
- // The animation starts and ends slowly, and speeds up in the middle.
396
- return {
397
- linearTimeProgression,
398
- cos((linearTimeProgression + 1.0) * PI) / 2 + 0.5};
399
- } else if (mutationConfig.animationType == AnimationType::Spring) {
400
- // Using mSpringDamping in this equation is not really the exact
401
- // mathematical springDamping, but a good approximation We need to replace
402
- // this equation with the right Factor that accounts for damping and
403
- // friction
404
- double damping = mutationConfig.springDamping;
405
- return {
406
- linearTimeProgression,
407
- (1 +
408
- pow(2, -10 * linearTimeProgression) *
409
- sin((linearTimeProgression - damping / 4) * PI * 2 / damping))};
410
- } else {
411
- return {linearTimeProgression, linearTimeProgression};
412
- }
169
+ bool LayoutAnimationKeyFrameManager::shouldOverridePullTransaction() const {
170
+ return shouldAnimateFrame();
413
171
  }
414
172
 
415
- void LayoutAnimationKeyFrameManager::
416
- adjustImmediateMutationIndicesForDelayedMutations(
417
- SurfaceId surfaceId,
418
- ShadowViewMutation &mutation,
419
- bool skipLastAnimation,
420
- bool lastAnimationOnly) const {
421
- bool isRemoveMutation = mutation.type == ShadowViewMutation::Type::Remove;
422
- react_native_assert(
423
- isRemoveMutation || mutation.type == ShadowViewMutation::Type::Insert);
424
-
425
- // TODO: turn all of this into a lambda and share code?
426
- if (mutation.mutatedViewIsVirtual()) {
427
- PrintMutationInstruction(
428
- "[IndexAdjustment] Not calling adjustImmediateMutationIndicesForDelayedMutations, is virtual, for:",
429
- mutation);
430
- return;
431
- }
432
-
433
- PrintMutationInstruction(
434
- "[IndexAdjustment] Calling adjustImmediateMutationIndicesForDelayedMutations for:",
435
- mutation);
436
-
437
- // First, collect all final mutations that could impact this immediate
438
- // mutation.
439
- std::vector<ShadowViewMutation *> candidateMutations{};
440
-
441
- for (auto inflightAnimationIt =
442
- inflightAnimations_.rbegin() + (skipLastAnimation ? 1 : 0);
443
- inflightAnimationIt != inflightAnimations_.rend();
444
- inflightAnimationIt++) {
445
- auto &inflightAnimation = *inflightAnimationIt;
446
- if (inflightAnimation.surfaceId != surfaceId) {
447
- continue;
448
- }
449
- if (inflightAnimation.completed) {
450
- continue;
451
- }
173
+ better::optional<MountingTransaction>
174
+ LayoutAnimationKeyFrameManager::pullTransaction(
175
+ SurfaceId surfaceId,
176
+ MountingTransaction::Number transactionNumber,
177
+ TransactionTelemetry const &telemetry,
178
+ ShadowViewMutationList mutations) const {
179
+ simulateImagePropsMemoryAccess(mutations);
180
+ // Current time in milliseconds
181
+ uint64_t now = now_();
452
182
 
453
- for (auto it = inflightAnimation.keyFrames.begin();
454
- it != inflightAnimation.keyFrames.end();
455
- it++) {
456
- auto &animatedKeyFrame = *it;
183
+ bool inflightAnimationsExistInitially = !inflightAnimations_.empty();
184
+ deleteAnimationsForStoppedSurfaces();
457
185
 
458
- if (animatedKeyFrame.invalidated) {
459
- continue;
186
+ if (!mutations.empty()) {
187
+ #ifdef RN_SHADOW_TREE_INTROSPECTION
188
+ {
189
+ std::stringstream ss(getDebugDescription(mutations, {}));
190
+ std::string to;
191
+ while (std::getline(ss, to, '\n')) {
192
+ LOG(ERROR)
193
+ << "LayoutAnimationKeyFrameManager.cpp: got mutation list: Line: "
194
+ << to;
460
195
  }
196
+ };
197
+ #endif
461
198
 
462
- // Detect if they're in the same view hierarchy, but not equivalent
463
- // We've already detected direct conflicts and removed them.
464
- if (animatedKeyFrame.parentView.tag != mutation.parentShadowView.tag) {
199
+ // DEBUG ONLY: list existing inflight animations
200
+ #ifdef LAYOUT_ANIMATION_VERBOSE_LOGGING
201
+ LOG(ERROR) << "BEGINNING DISPLAYING ONGOING inflightAnimations_!";
202
+ int i = 0;
203
+ int j = 0;
204
+ for (auto const &inflightAnimation : inflightAnimations_) {
205
+ i++;
206
+ j = 0;
207
+ if (inflightAnimation.completed) {
465
208
  continue;
466
209
  }
467
-
468
- for (auto &delayedMutation : animatedKeyFrame.finalMutationsForKeyFrame) {
469
- if (delayedMutation.type != ShadowViewMutation::Type::Remove) {
470
- continue;
471
- }
472
- if (delayedMutation.mutatedViewIsVirtual()) {
210
+ for (auto &keyframe : inflightAnimation.keyFrames) {
211
+ j++;
212
+ if (keyframe.invalidated) {
473
213
  continue;
474
214
  }
475
- if (delayedMutation.oldChildShadowView.tag ==
476
- (isRemoveMutation ? mutation.oldChildShadowView.tag
477
- : mutation.newChildShadowView.tag)) {
478
- continue;
215
+ for (const auto &finalMutationForKeyFrame :
216
+ keyframe.finalMutationsForKeyFrame) {
217
+ if (finalMutationForKeyFrame.mutatedViewIsVirtual()) {
218
+ std::string msg = "Animation " + std::to_string(i) + " keyframe " +
219
+ std::to_string(j) + ": Final Animation";
220
+ PrintMutationInstruction(msg, finalMutationForKeyFrame);
221
+ } else {
222
+ LOG(ERROR) << "Animation " << i << " keyframe " << j
223
+ << ": on tag: [" << keyframe.viewStart.tag << "]";
224
+ }
479
225
  }
480
-
481
- PrintMutationInstructionRelative(
482
- "[IndexAdjustment] adjustImmediateMutationIndicesForDelayedMutations CANDIDATE for:",
483
- mutation,
484
- delayedMutation);
485
- candidateMutations.push_back(&delayedMutation);
486
226
  }
487
227
  }
228
+ LOG(ERROR) << "BEGINNING DONE DISPLAYING ONGOING inflightAnimations_!";
229
+ #endif
488
230
 
489
- if (lastAnimationOnly) {
490
- break;
231
+ // Stub PropsParserContext used for cloneProps.
232
+ // This is/should be safe because cloning doesn't actually need to
233
+ // parse props, and just copies them; therefore there should be no
234
+ // need to actually use anything in the PropsParserContext.
235
+ // If this ever changes, the LayoutAnimations API will need to change
236
+ // to pass in a real PropsParserContext.
237
+ ContextContainer contextContainer{};
238
+ PropsParserContext propsParserContext{surfaceId, contextContainer};
239
+
240
+ // What to do if we detect a conflict? Get current value and make
241
+ // that the baseline of the next animation. Scale the remaining time
242
+ // in the animation
243
+ // Types of conflicts and how we handle them:
244
+ // Update -> update: remove the previous update, make it the baseline of the
245
+ // next update (with current progress) Update -> remove: same, with final
246
+ // mutation being a remove Insert -> update: treat as update->update Insert
247
+ // -> remove: same, as update->remove Remove -> update/insert: not possible
248
+ // We just collect pairs here of <Mutation, AnimationConfig> and delete them
249
+ // from active animations. If another animation is queued up from the
250
+ // current mutations then these deleted mutations will serve as the baseline
251
+ // for the next animation. If not, the current mutations are executed
252
+ // immediately without issues.
253
+ std::vector<AnimationKeyFrame> conflictingAnimations{};
254
+ getAndEraseConflictingAnimations(
255
+ surfaceId, mutations, conflictingAnimations);
256
+
257
+ // Are we animating this list of mutations?
258
+ better::optional<LayoutAnimation> currentAnimation{};
259
+ {
260
+ std::lock_guard<std::mutex> lock(currentAnimationMutex_);
261
+ if (currentAnimation_) {
262
+ currentAnimation = std::move(currentAnimation_);
263
+ currentAnimation_.reset();
264
+ }
491
265
  }
492
- }
493
266
 
494
- // While the mutation keeps being affected, keep checking. We use the vector
495
- // so we only perform one adjustment per delayed mutation. See comments at
496
- // bottom of adjustDelayedMutationIndicesForMutation for further explanation.
497
- bool changed = true;
498
- int adjustedDelta = 0;
499
- while (changed) {
500
- changed = false;
501
- candidateMutations.erase(
502
- std::remove_if(
503
- candidateMutations.begin(),
504
- candidateMutations.end(),
505
- [&changed, &mutation, &adjustedDelta, &isRemoveMutation](
506
- ShadowViewMutation *candidateMutation) {
507
- bool indexConflicts =
508
- (candidateMutation->index < mutation.index ||
509
- (isRemoveMutation &&
510
- candidateMutation->index == mutation.index));
511
- if (indexConflicts) {
512
- mutation.index++;
513
- adjustedDelta++;
514
- changed = true;
515
- PrintMutationInstructionRelative(
516
- "[IndexAdjustment] adjustImmediateMutationIndicesForDelayedMutations: Adjusting mutation UPWARD",
517
- mutation,
518
- *candidateMutation);
519
- return true;
520
- }
521
- return false;
522
- }),
523
- candidateMutations.end());
524
- }
525
- }
526
-
527
- void LayoutAnimationKeyFrameManager::adjustDelayedMutationIndicesForMutation(
528
- SurfaceId surfaceId,
529
- ShadowViewMutation const &mutation,
530
- bool skipLastAnimation) const {
531
- bool isRemoveMutation = mutation.type == ShadowViewMutation::Type::Remove;
532
- bool isInsertMutation = mutation.type == ShadowViewMutation::Type::Insert;
533
- auto tag = isRemoveMutation ? mutation.oldChildShadowView.tag
534
- : mutation.newChildShadowView.tag;
535
- react_native_assert(isRemoveMutation || isInsertMutation);
267
+ if (currentAnimation.hasValue()) {
268
+ LayoutAnimation animation = std::move(currentAnimation).value();
269
+ animation.surfaceId = surfaceId;
270
+ animation.startTime = now;
536
271
 
537
- if (mutation.mutatedViewIsVirtual()) {
538
- PrintMutationInstruction(
539
- "[IndexAdjustment] Not calling adjustDelayedMutationIndicesForMutation, is virtual, for:",
540
- mutation);
541
- return;
542
- }
272
+ // Pre-process list to:
273
+ // Catch remove+reinsert (reorders)
274
+ // Catch delete+create (reparenting) (this should be optimized away at
275
+ // the diffing level eventually?)
276
+ // TODO: to prevent this step we could tag Remove/Insert mutations as
277
+ // being moves on the Differ level, since we know that there? We could use
278
+ // TinyMap here, but it's not exposed by Differentiator (yet).
279
+ better::set<Tag> insertedTags;
280
+ better::set<Tag> deletedTags;
281
+ better::set<Tag> reparentedTags; // tags that are deleted and recreated
282
+ std::unordered_map<Tag, ShadowViewMutation> movedTags;
283
+ for (const auto &mutation : mutations) {
284
+ if (mutation.type == ShadowViewMutation::Type::Insert) {
285
+ insertedTags.insert(mutation.newChildShadowView.tag);
286
+ }
287
+ if (mutation.type == ShadowViewMutation::Type::Delete) {
288
+ deletedTags.insert(mutation.oldChildShadowView.tag);
289
+ }
290
+ if (mutation.type == ShadowViewMutation::Type::Create) {
291
+ if (deletedTags.find(mutation.newChildShadowView.tag) !=
292
+ deletedTags.end()) {
293
+ reparentedTags.insert(mutation.newChildShadowView.tag);
294
+ }
295
+ }
296
+ }
543
297
 
544
- // First, collect all final mutations that could impact this immediate
545
- // mutation.
546
- std::vector<ShadowViewMutation *> candidateMutations{};
298
+ // Process mutations list into operations that can be sent to platform
299
+ // immediately, and those that need to be animated Deletions, removals,
300
+ // updates are delayed and animated. Creations and insertions are sent to
301
+ // platform and then "animated in" with opacity updates. Upon completion,
302
+ // removals and deletions are sent to platform
303
+ ShadowViewMutation::List immediateMutations;
547
304
 
548
- for (auto inflightAnimationIt =
549
- inflightAnimations_.rbegin() + (skipLastAnimation ? 1 : 0);
550
- inflightAnimationIt != inflightAnimations_.rend();
551
- inflightAnimationIt++) {
552
- auto &inflightAnimation = *inflightAnimationIt;
305
+ // Remove operations that are actually moves should be copied to
306
+ // "immediate mutations". The corresponding "insert" will also be executed
307
+ // immediately and animated as an update.
308
+ std::vector<AnimationKeyFrame> keyFramesToAnimate;
309
+ auto const layoutAnimationConfig = animation.layoutAnimationConfig;
310
+ for (auto const &mutation : mutations) {
311
+ ShadowView baselineShadowView =
312
+ (mutation.type == ShadowViewMutation::Type::Delete ||
313
+ mutation.type == ShadowViewMutation::Type::Remove ||
314
+ mutation.type == ShadowViewMutation::Type::Update
315
+ ? mutation.oldChildShadowView
316
+ : mutation.newChildShadowView);
317
+ react_native_assert(baselineShadowView.tag > 0);
318
+ bool haveComponentDescriptor =
319
+ hasComponentDescriptorForShadowView(baselineShadowView);
553
320
 
554
- if (inflightAnimation.surfaceId != surfaceId) {
555
- continue;
556
- }
557
- if (inflightAnimation.completed) {
558
- continue;
559
- }
321
+ // Immediately execute any mutations on a root node
322
+ if (baselineShadowView.traits.check(
323
+ ShadowNodeTraits::Trait::RootNodeKind)) {
324
+ immediateMutations.push_back(mutation);
325
+ continue;
326
+ }
560
327
 
561
- for (auto it = inflightAnimation.keyFrames.begin();
562
- it != inflightAnimation.keyFrames.end();
563
- it++) {
564
- auto &animatedKeyFrame = *it;
328
+ better::optional<ShadowViewMutation> executeMutationImmediately{};
565
329
 
566
- if (animatedKeyFrame.invalidated) {
567
- continue;
568
- }
330
+ bool isRemoveReinserted =
331
+ mutation.type == ShadowViewMutation::Type::Remove &&
332
+ insertedTags.find(mutation.oldChildShadowView.tag) !=
333
+ insertedTags.end();
569
334
 
570
- // Detect if they're in the same view hierarchy, but not equivalent
571
- // (We've already detected direct conflicts and handled them above)
572
- if (animatedKeyFrame.parentView.tag != mutation.parentShadowView.tag) {
573
- continue;
574
- }
335
+ // Reparenting can result in a node being removed, inserted (moved) and
336
+ // also deleted and created in the same frame, with the same props etc.
337
+ // This should eventually be optimized out of the diffing algorithm, but
338
+ // for now we detect reparenting and prevent the corresponding
339
+ // Delete/Create instructions from being animated.
340
+ bool isReparented =
341
+ reparentedTags.find(baselineShadowView.tag) != reparentedTags.end();
575
342
 
576
- for (auto &finalAnimationMutation :
577
- animatedKeyFrame.finalMutationsForKeyFrame) {
578
- if (finalAnimationMutation.oldChildShadowView.tag == tag) {
579
- continue;
343
+ if (isRemoveReinserted) {
344
+ movedTags.insert({mutation.oldChildShadowView.tag, mutation});
580
345
  }
581
346
 
582
- if (finalAnimationMutation.type != ShadowViewMutation::Type::Remove) {
583
- continue;
584
- }
585
- if (finalAnimationMutation.mutatedViewIsVirtual()) {
586
- continue;
347
+ // Inserts that follow a "remove" of the same tag should be treated as
348
+ // an update (move) animation.
349
+ bool wasInsertedTagRemoved = false;
350
+ auto movedIt = movedTags.end();
351
+ if (mutation.type == ShadowViewMutation::Type::Insert) {
352
+ // If this is a move, we actually don't want to copy this insert
353
+ // instruction to animated instructions - we want to
354
+ // generate an Update mutation for Remove+Insert pairs to animate
355
+ // the layout.
356
+ // The corresponding Remove and Insert instructions will instead
357
+ // be treated as "immediate" instructions.
358
+ movedIt = movedTags.find(mutation.newChildShadowView.tag);
359
+ wasInsertedTagRemoved = movedIt != movedTags.end();
587
360
  }
588
361
 
589
- PrintMutationInstructionRelative(
590
- "[IndexAdjustment] adjustDelayedMutationIndicesForMutation: CANDIDATE:",
591
- mutation,
592
- finalAnimationMutation);
593
- candidateMutations.push_back(&finalAnimationMutation);
594
- }
595
- }
596
- }
362
+ auto const &mutationConfig =
363
+ (mutation.type == ShadowViewMutation::Type::Delete ||
364
+ (mutation.type == ShadowViewMutation::Type::Remove &&
365
+ !wasInsertedTagRemoved)
366
+ ? layoutAnimationConfig.deleteConfig
367
+ : (mutation.type == ShadowViewMutation::Type::Insert &&
368
+ !wasInsertedTagRemoved
369
+ ? layoutAnimationConfig.createConfig
370
+ : layoutAnimationConfig.updateConfig));
371
+ bool haveConfiguration =
372
+ mutationConfig.animationType != AnimationType::None;
597
373
 
598
- // Because the finalAnimations are not sorted in any way, it is possible to
599
- // have some sequence like:
600
- // * DELAYED REMOVE 10 from {TAG}
601
- // * DELAYED REMOVE 9 from {TAG}
602
- // * ...
603
- // * DELAYED REMOVE 5 from {TAG}
604
- // with mutation: INSERT 6/REMOVE 6. This would cause the first few mutations
605
- // to *not* be adjusted, even though they would be impacted by mutation or
606
- // vice-versa after later adjustments are applied. Therefore, we just keep
607
- // recursing while there are any changes. This isn't great, but is good enough
608
- // for now until we change these data-structures.
609
- bool changed = true;
610
- while (changed) {
611
- changed = false;
612
- candidateMutations.erase(
613
- std::remove_if(
614
- candidateMutations.begin(),
615
- candidateMutations.end(),
616
- [&mutation, &isRemoveMutation, &isInsertMutation, &changed](
617
- ShadowViewMutation *candidateMutation) {
618
- if (isRemoveMutation &&
619
- mutation.index <= candidateMutation->index) {
620
- candidateMutation->index--;
621
- changed = true;
622
- PrintMutationInstructionRelative(
623
- "[IndexAdjustment] adjustDelayedMutationIndicesForMutation: Adjusting mutation DOWNWARD",
624
- mutation,
625
- *candidateMutation);
626
- return true;
627
- } else if (
628
- isInsertMutation &&
629
- mutation.index <= candidateMutation->index) {
630
- candidateMutation->index++;
631
- changed = true;
632
- PrintMutationInstructionRelative(
633
- "[IndexAdjustment] adjustDelayedMutationIndicesForMutation: Adjusting mutation UPWARD",
634
- mutation,
635
- *candidateMutation);
636
- return true;
637
- }
638
- return false;
639
- }),
640
- candidateMutations.end());
641
- }
642
- }
374
+ // Creates and inserts should also be executed immediately.
375
+ // Mutations that would otherwise be animated, but have no
376
+ // configuration, are also executed immediately.
377
+ if (isRemoveReinserted || !haveConfiguration || isReparented ||
378
+ mutation.type == ShadowViewMutation::Type::Create ||
379
+ mutation.type == ShadowViewMutation::Type::Insert) {
380
+ executeMutationImmediately = mutation;
643
381
 
644
- void LayoutAnimationKeyFrameManager::getAndEraseConflictingAnimations(
645
- SurfaceId surfaceId,
646
- ShadowViewMutationList const &mutations,
647
- std::vector<AnimationKeyFrame> &conflictingAnimations) const {
648
- ShadowViewMutationList localConflictingMutations{};
649
- for (auto const &mutation : mutations) {
650
- bool mutationIsCreateOrDelete =
651
- mutation.type == ShadowViewMutation::Type::Create ||
652
- mutation.type == ShadowViewMutation::Type::Delete;
653
- auto const &baselineShadowView =
654
- (mutation.type == ShadowViewMutation::Type::Insert ||
655
- mutation.type == ShadowViewMutation::Type::Create)
656
- ? mutation.newChildShadowView
657
- : mutation.oldChildShadowView;
658
- auto baselineTag = baselineShadowView.tag;
382
+ // It is possible, especially in the case of "moves", that we have a
383
+ // sequence of operations like:
384
+ // UPDATE X
385
+ // REMOVE X
386
+ // INSERT X
387
+ // In these cases, we will have queued up an animation for the UPDATE
388
+ // and delayed its execution; the REMOVE and INSERT will be executed
389
+ // first; and then the UPDATE will be animating to/from ShadowViews
390
+ // that are out-of-sync with what's on the mounting layer. Thus, for
391
+ // any UPDATE animations already queued up for this tag, we adjust the
392
+ // "previous" ShadowView.
393
+ if (mutation.type == ShadowViewMutation::Type::Insert) {
394
+ for (auto &keyframe : keyFramesToAnimate) {
395
+ if (keyframe.tag == baselineShadowView.tag) {
396
+ // If there's already an animation queued up, followed by this
397
+ // Insert, it *must* be an Update mutation animation. Other
398
+ // sequences should not be possible.
399
+ react_native_assert(
400
+ keyframe.type == AnimationConfigurationType::Update);
659
401
 
660
- for (auto &inflightAnimation : inflightAnimations_) {
661
- if (inflightAnimation.surfaceId != surfaceId) {
662
- continue;
663
- }
664
- if (inflightAnimation.completed) {
665
- continue;
666
- }
402
+ // The mutation is an "insert", so it must have a
403
+ // "newChildShadowView"
404
+ react_native_assert(mutation.newChildShadowView.tag > 0);
667
405
 
668
- for (auto it = inflightAnimation.keyFrames.begin();
669
- it != inflightAnimation.keyFrames.end();) {
670
- auto &animatedKeyFrame = *it;
671
-
672
- if (animatedKeyFrame.invalidated) {
673
- continue;
674
- }
675
-
676
- // A conflict is when either: the animated node itself is mutated
677
- // directly; or, the parent of the node is created or deleted. In cases
678
- // of reparenting - say, the parent is deleted but the node was moved to
679
- // a different parent first - the reparenting (remove/insert) conflict
680
- // will be detected before we process the parent DELETE.
681
- // Parent deletion is important because deleting a parent recursively
682
- // deletes all children. If we previously deferred deletion of a child,
683
- // we need to force deletion/removal to happen immediately.
684
- bool conflicting = animatedKeyFrame.tag == baselineTag ||
685
- (mutationIsCreateOrDelete &&
686
- animatedKeyFrame.parentView.tag == baselineTag &&
687
- animatedKeyFrame.parentView.tag != 0);
688
-
689
- // Conflicting animation detected: if we're mutating a tag under
690
- // animation, or deleting the parent of a tag under animation, or
691
- // reparenting.
692
- if (conflicting) {
693
- animatedKeyFrame.invalidated = true;
694
-
695
- // We construct a list of all conflicting animations, whether or not
696
- // they have a "final mutation" to execute. This is important with,
697
- // for example, "insert" mutations where the final update needs to set
698
- // opacity to "1", even if there's no final ShadowNode update.
699
- // TODO: don't animate virtual views in the first place?
700
- bool isVirtual = false;
701
- for (const auto &finalMutationForKeyFrame :
702
- animatedKeyFrame.finalMutationsForKeyFrame) {
703
- isVirtual =
704
- isVirtual || finalMutationForKeyFrame.mutatedViewIsVirtual();
406
+ // Those asserts don't run in prod. If there's some edge-case
407
+ // that we haven't caught yet, we'd crash in debug; make sure we
408
+ // don't mutate the prevView in prod.
409
+ if (keyframe.type == AnimationConfigurationType::Update &&
410
+ mutation.newChildShadowView.tag > 0) {
411
+ keyframe.viewPrev = mutation.newChildShadowView;
412
+ }
413
+ }
414
+ }
415
+ } else if (mutation.type == ShadowViewMutation::Type::Remove) {
416
+ for (auto &keyframe : keyFramesToAnimate) {
417
+ if (keyframe.tag == baselineShadowView.tag) {
418
+ // If there's already an animation queued up, followed by this
419
+ // Insert, it *must* be an Update mutation animation. Other
420
+ // sequences should not be possible.
421
+ react_native_assert(
422
+ keyframe.type == AnimationConfigurationType::Update);
705
423
 
706
- #ifdef LAYOUT_ANIMATION_VERBOSE_LOGGING
707
- PrintMutationInstructionRelative(
708
- "Found mutation that conflicts with existing in-flight animation:",
709
- mutation,
710
- finalMutationForKeyFrame);
711
- #endif
712
- }
424
+ // The mutation is a "remove", so it must have a
425
+ // "oldChildShadowView"
426
+ react_native_assert(mutation.oldChildShadowView.tag > 0);
713
427
 
714
- conflictingAnimations.push_back(animatedKeyFrame);
715
- for (const auto &finalMutationForKeyFrame :
716
- animatedKeyFrame.finalMutationsForKeyFrame) {
717
- if (!isVirtual ||
718
- finalMutationForKeyFrame.type ==
719
- ShadowViewMutation::Type::Delete) {
720
- localConflictingMutations.push_back(finalMutationForKeyFrame);
428
+ // Those asserts don't run in prod. If there's some edge-case
429
+ // that we haven't caught yet, we'd crash in debug; make sure we
430
+ // don't mutate the prevView in prod.
431
+ // Since normally the UPDATE would have been executed first and
432
+ // now it's deferred, we need to change the `oldChildShadowView`
433
+ // that is being referenced by the REMOVE mutation.
434
+ if (keyframe.type == AnimationConfigurationType::Update &&
435
+ mutation.oldChildShadowView.tag > 0) {
436
+ executeMutationImmediately =
437
+ ShadowViewMutation::RemoveMutation(
438
+ mutation.parentShadowView,
439
+ keyframe.viewPrev,
440
+ mutation.index);
441
+ }
442
+ }
721
443
  }
722
444
  }
723
-
724
- // Delete from existing animation
725
- it = inflightAnimation.keyFrames.erase(it);
726
- } else {
727
- it++;
728
445
  }
729
- }
730
- }
731
- }
732
446
 
733
- // Recurse, in case conflicting mutations conflict with other existing
734
- // animations
735
- if (!localConflictingMutations.empty()) {
736
- getAndEraseConflictingAnimations(
737
- surfaceId, localConflictingMutations, conflictingAnimations);
738
- }
739
- }
447
+ // Deletes, non-move inserts, updates get animated
448
+ if (!wasInsertedTagRemoved && !isRemoveReinserted && !isReparented &&
449
+ haveConfiguration &&
450
+ mutation.type != ShadowViewMutation::Type::Create) {
451
+ ShadowView viewStart = ShadowView(
452
+ mutation.type == ShadowViewMutation::Type::Insert
453
+ ? mutation.newChildShadowView
454
+ : mutation.oldChildShadowView);
455
+ react_native_assert(viewStart.tag > 0);
456
+ ShadowView viewFinal = ShadowView(
457
+ mutation.type == ShadowViewMutation::Type::Update
458
+ ? mutation.newChildShadowView
459
+ : viewStart);
460
+ react_native_assert(viewFinal.tag > 0);
461
+ ShadowView parent = mutation.parentShadowView;
462
+ react_native_assert(
463
+ parent.tag > 0 ||
464
+ mutation.type == ShadowViewMutation::Type::Update ||
465
+ mutation.type == ShadowViewMutation::Type::Delete);
466
+ Tag tag = viewStart.tag;
740
467
 
741
- void LayoutAnimationKeyFrameManager::setClockNow(
742
- std::function<uint64_t()> now) {
743
- now_ = now;
744
- }
468
+ AnimationKeyFrame keyFrame{};
469
+ if (mutation.type == ShadowViewMutation::Type::Insert) {
470
+ if (mutationConfig.animationProperty ==
471
+ AnimationProperty::Opacity &&
472
+ haveComponentDescriptor) {
473
+ auto props =
474
+ getComponentDescriptorForShadowView(baselineShadowView)
475
+ .cloneProps(propsParserContext, viewStart.props, {});
745
476
 
746
- better::optional<MountingTransaction>
747
- LayoutAnimationKeyFrameManager::pullTransaction(
748
- SurfaceId surfaceId,
749
- MountingTransaction::Number transactionNumber,
750
- TransactionTelemetry const &telemetry,
751
- ShadowViewMutationList mutations) const {
752
- // Current time in milliseconds
753
- uint64_t now = now_();
477
+ if (baselineShadowView.traits.check(
478
+ ShadowNodeTraits::Trait::ViewKind)) {
479
+ auto const &viewProps =
480
+ *std::static_pointer_cast<ViewProps const>(props);
481
+ const_cast<ViewProps &>(viewProps).opacity = 0;
482
+ }
754
483
 
755
- bool inflightAnimationsExistInitially = !inflightAnimations_.empty();
484
+ react_native_assert(props != nullptr);
485
+ if (props != nullptr) {
486
+ viewStart.props = props;
487
+ }
488
+ }
489
+ bool isScaleX =
490
+ mutationConfig.animationProperty == AnimationProperty::ScaleX ||
491
+ mutationConfig.animationProperty == AnimationProperty::ScaleXY;
492
+ bool isScaleY =
493
+ mutationConfig.animationProperty == AnimationProperty::ScaleY ||
494
+ mutationConfig.animationProperty == AnimationProperty::ScaleXY;
495
+ if ((isScaleX || isScaleY) && haveComponentDescriptor) {
496
+ auto props =
497
+ getComponentDescriptorForShadowView(baselineShadowView)
498
+ .cloneProps(propsParserContext, viewStart.props, {});
499
+ if (baselineShadowView.traits.check(
500
+ ShadowNodeTraits::Trait::ViewKind)) {
501
+ auto const &viewProps =
502
+ *std::static_pointer_cast<ViewProps const>(props);
503
+ const_cast<ViewProps &>(viewProps).transform =
504
+ Transform::Scale(isScaleX ? 0 : 1, isScaleY ? 0 : 1, 1);
505
+ }
756
506
 
757
- // Execute stopSurface on any ongoing animations
758
- if (inflightAnimationsExistInitially) {
759
- std::vector<SurfaceId> surfaceIdsToStop{};
760
- {
761
- std::lock_guard<std::mutex> lock(surfaceIdsToStopMutex_);
762
- surfaceIdsToStop = surfaceIdsToStop_;
763
- surfaceIdsToStop_ = {};
764
- }
507
+ react_native_assert(props != nullptr);
508
+ if (props != nullptr) {
509
+ viewStart.props = props;
510
+ }
511
+ }
765
512
 
766
- for (auto it = inflightAnimations_.begin();
767
- it != inflightAnimations_.end();) {
768
- const auto &animation = *it;
513
+ PrintMutationInstruction(
514
+ "Setting up animation KeyFrame for INSERT mutation (Create animation)",
515
+ mutation);
769
516
 
770
- if (std::find(
771
- surfaceIdsToStop.begin(),
772
- surfaceIdsToStop.end(),
773
- animation.surfaceId) != surfaceIdsToStop.end()) {
774
- #ifdef LAYOUT_ANIMATION_VERBOSE_LOGGING
775
- LOG(ERROR)
776
- << "LayoutAnimations: stopping animation due to stopSurface on "
777
- << surfaceId;
517
+ keyFrame = AnimationKeyFrame{
518
+ /* .finalMutationsForKeyFrame = */ {},
519
+ /* .type = */ AnimationConfigurationType::Create,
520
+ /* .tag = */ tag,
521
+ /* .parentView = */ parent,
522
+ /* .viewStart = */ viewStart,
523
+ /* .viewEnd = */ viewFinal,
524
+ /* .viewPrev = */ baselineShadowView,
525
+ /* .initialProgress = */ 0};
526
+ } else if (mutation.type == ShadowViewMutation::Type::Delete) {
527
+ // This is just for assertion purposes.
528
+ // The NDEBUG check here is to satisfy the compiler in certain environments
529
+ // complaining about correspondingRemoveIt being unused.
530
+ #ifdef REACT_NATIVE_DEBUG
531
+ #ifndef NDEBUG
532
+ // This block is temporarily disabled to fix some internal builds.
533
+ // In some build configurations, we get a compiler error that
534
+ // `correspondingRemoveIt` is unused.
535
+ /* Tag deleteTag = mutation.oldChildShadowView.tag;
536
+ auto correspondingRemoveIt = std::find_if(
537
+ mutations.begin(),
538
+ mutations.end(),
539
+ [&deleteTag](auto &mutation) {
540
+ return mutation.type == ShadowViewMutation::Type::Remove &&
541
+ mutation.oldChildShadowView.tag == deleteTag;
542
+ });
543
+ react_native_assert(correspondingRemoveIt != mutations.end());
544
+ */
778
545
  #endif
779
- it = inflightAnimations_.erase(it);
780
- } else {
781
- it++;
782
- }
783
- }
784
- }
785
-
786
- if (!mutations.empty()) {
787
- #ifdef RN_SHADOW_TREE_INTROSPECTION
788
- {
789
- std::stringstream ss(getDebugDescription(mutations, {}));
790
- std::string to;
791
- while (std::getline(ss, to, '\n')) {
792
- LOG(ERROR)
793
- << "LayoutAnimationKeyFrameManager.cpp: got mutation list: Line: "
794
- << to;
795
- }
796
- };
797
546
  #endif
547
+ continue;
548
+ } else if (mutation.type == ShadowViewMutation::Type::Update) {
549
+ viewFinal = ShadowView(mutation.newChildShadowView);
798
550
 
799
- // DEBUG ONLY: list existing inflight animations
800
- #ifdef LAYOUT_ANIMATION_VERBOSE_LOGGING
801
- LOG(ERROR) << "BEGINNING DISPLAYING ONGOING inflightAnimations_!";
802
- int i = 0;
803
- int j = 0;
804
- for (auto const &inflightAnimation : inflightAnimations_) {
805
- i++;
806
- j = 0;
807
- if (inflightAnimation.completed) {
808
- continue;
809
- }
810
- for (auto &keyframe : inflightAnimation.keyFrames) {
811
- j++;
812
- if (keyframe.invalidated) {
813
- continue;
814
- }
815
- for (const auto &finalMutationForKeyFrame :
816
- keyframe.finalMutationsForKeyFrame) {
817
- if (finalMutationForKeyFrame.mutatedViewIsVirtual()) {
818
- std::string msg = "Animation " + std::to_string(i) + " keyframe " +
819
- std::to_string(j) + ": Final Animation";
820
- PrintMutationInstruction(msg, finalMutationForKeyFrame);
551
+ PrintMutationInstruction(
552
+ "Setting up animation KeyFrame for UPDATE mutation (Update animation)",
553
+ mutation);
554
+
555
+ keyFrame = AnimationKeyFrame{
556
+ /* .finalMutationsForKeyFrame = */ {mutation},
557
+ /* .type = */ AnimationConfigurationType::Update,
558
+ /* .tag = */ tag,
559
+ /* .parentView = */ parent,
560
+ /* .viewStart = */ viewStart,
561
+ /* .viewEnd = */ viewFinal,
562
+ /* .viewPrev = */ baselineShadowView,
563
+ /* .initialProgress = */ 0};
821
564
  } else {
822
- LOG(ERROR) << "Animation " << i << " keyframe " << j
823
- << ": on tag: [" << keyframe.viewStart.tag << "]";
824
- }
825
- }
826
- }
827
- }
828
- LOG(ERROR) << "BEGINNING DONE DISPLAYING ONGOING inflightAnimations_!";
829
- #endif
565
+ // This should just be "Remove" instructions that are not animated
566
+ // (either this is a "move", or there's a corresponding "Delete"
567
+ // that is animated).
568
+ react_native_assert(
569
+ mutation.type == ShadowViewMutation::Type::Remove);
830
570
 
831
- // Stub PropsParserContext used for cloneProps.
832
- // This is/should be safe because cloning doesn't actually need to
833
- // parse props, and just copies them; therefore there should be no
834
- // need to actually use anything in the PropsParserContext.
835
- // If this ever changes, the LayoutAnimations API will need to change
836
- // to pass in a real PropsParserContext.
837
- ContextContainer contextContainer{};
838
- PropsParserContext propsParserContext{surfaceId, contextContainer};
571
+ Tag removeTag = mutation.oldChildShadowView.tag;
572
+ auto correspondingInsertIt = std::find_if(
573
+ mutations.begin(),
574
+ mutations.end(),
575
+ [&removeTag](auto &mutation) {
576
+ return mutation.type == ShadowViewMutation::Type::Insert &&
577
+ mutation.newChildShadowView.tag == removeTag;
578
+ });
579
+ if (correspondingInsertIt == mutations.end()) {
580
+ // This is a REMOVE not paired with an INSERT (move), so it must
581
+ // be paired with a DELETE.
582
+ auto correspondingDeleteIt = std::find_if(
583
+ mutations.begin(),
584
+ mutations.end(),
585
+ [&removeTag](auto &mutation) {
586
+ return mutation.type == ShadowViewMutation::Type::Delete &&
587
+ mutation.oldChildShadowView.tag == removeTag;
588
+ });
589
+ react_native_assert(correspondingDeleteIt != mutations.end());
839
590
 
840
- // What to do if we detect a conflict? Get current value and make
841
- // that the baseline of the next animation. Scale the remaining time
842
- // in the animation
843
- // Types of conflicts and how we handle them:
844
- // Update -> update: remove the previous update, make it the baseline of the
845
- // next update (with current progress) Update -> remove: same, with final
846
- // mutation being a remove Insert -> update: treat as update->update Insert
847
- // -> remove: same, as update->remove Remove -> update/insert: not possible
848
- // We just collect pairs here of <Mutation, AnimationConfig> and delete them
849
- // from active animations. If another animation is queued up from the
850
- // current mutations then these deleted mutations will serve as the baseline
851
- // for the next animation. If not, the current mutations are executed
852
- // immediately without issues.
853
- std::vector<AnimationKeyFrame> conflictingAnimations{};
854
- getAndEraseConflictingAnimations(
855
- surfaceId, mutations, conflictingAnimations);
591
+ auto deleteMutation = *correspondingDeleteIt;
856
592
 
857
- // Are we animating this list of mutations?
858
- better::optional<LayoutAnimation> currentAnimation{};
859
- {
860
- std::lock_guard<std::mutex> lock(currentAnimationMutex_);
861
- if (currentAnimation_) {
862
- currentAnimation = std::move(currentAnimation_);
863
- currentAnimation_ = {};
864
- }
865
- }
593
+ if (mutationConfig.animationProperty ==
594
+ AnimationProperty::Opacity &&
595
+ haveComponentDescriptor) {
596
+ auto props =
597
+ getComponentDescriptorForShadowView(baselineShadowView)
598
+ .cloneProps(propsParserContext, viewFinal.props, {});
866
599
 
867
- if (currentAnimation) {
868
- LayoutAnimation animation = std::move(currentAnimation.value());
869
- currentAnimation = {};
870
- animation.surfaceId = surfaceId;
871
- animation.startTime = now;
600
+ if (baselineShadowView.traits.check(
601
+ ShadowNodeTraits::Trait::ViewKind)) {
602
+ auto const &viewProps =
603
+ *std::static_pointer_cast<ViewProps const>(props);
604
+ const_cast<ViewProps &>(viewProps).opacity = 0;
605
+ }
872
606
 
873
- // Pre-process list to:
874
- // Catch remove+reinsert (reorders)
875
- // Catch delete+create (reparenting) (this should be optimized away at
876
- // the diffing level eventually?)
877
- // TODO: to prevent this step we could tag Remove/Insert mutations as
878
- // being moves on the Differ level, since we know that there? We could use
879
- // TinyMap here, but it's not exposed by Differentiator (yet).
880
- std::vector<Tag> insertedTags;
881
- std::vector<Tag> deletedTags;
882
- std::vector<Tag> reparentedTags; // tags that are deleted and recreated
883
- std::unordered_map<Tag, ShadowViewMutation> movedTags;
884
- for (const auto &mutation : mutations) {
885
- if (mutation.type == ShadowViewMutation::Type::Insert) {
886
- insertedTags.push_back(mutation.newChildShadowView.tag);
887
- }
888
- if (mutation.type == ShadowViewMutation::Type::Delete) {
889
- deletedTags.push_back(mutation.oldChildShadowView.tag);
890
- }
891
- if (mutation.type == ShadowViewMutation::Type::Create) {
892
- if (std::find(
893
- deletedTags.begin(),
894
- deletedTags.end(),
895
- mutation.newChildShadowView.tag) != deletedTags.end()) {
896
- reparentedTags.push_back(mutation.newChildShadowView.tag);
897
- }
898
- }
899
- }
607
+ react_native_assert(props != nullptr);
608
+ if (props != nullptr) {
609
+ viewFinal.props = props;
610
+ }
611
+ }
612
+ bool isScaleX = mutationConfig.animationProperty ==
613
+ AnimationProperty::ScaleX ||
614
+ mutationConfig.animationProperty ==
615
+ AnimationProperty::ScaleXY;
616
+ bool isScaleY = mutationConfig.animationProperty ==
617
+ AnimationProperty::ScaleY ||
618
+ mutationConfig.animationProperty ==
619
+ AnimationProperty::ScaleXY;
620
+ if ((isScaleX || isScaleY) && haveComponentDescriptor) {
621
+ auto props =
622
+ getComponentDescriptorForShadowView(baselineShadowView)
623
+ .cloneProps(propsParserContext, viewFinal.props, {});
900
624
 
901
- // Process mutations list into operations that can be sent to platform
902
- // immediately, and those that need to be animated Deletions, removals,
903
- // updates are delayed and animated. Creations and insertions are sent to
904
- // platform and then "animated in" with opacity updates. Upon completion,
905
- // removals and deletions are sent to platform
906
- ShadowViewMutation::List immediateMutations;
625
+ if (baselineShadowView.traits.check(
626
+ ShadowNodeTraits::Trait::ViewKind)) {
627
+ auto const &viewProps =
628
+ *std::static_pointer_cast<ViewProps const>(props);
629
+ const_cast<ViewProps &>(viewProps).transform =
630
+ Transform::Scale(isScaleX ? 0 : 1, isScaleY ? 0 : 1, 1);
631
+ }
907
632
 
908
- // Remove operations that are actually moves should be copied to
909
- // "immediate mutations". The corresponding "insert" will also be executed
910
- // immediately and animated as an update.
911
- std::vector<AnimationKeyFrame> keyFramesToAnimate;
912
- std::vector<AnimationKeyFrame> movesToAnimate;
913
- auto const layoutAnimationConfig = animation.layoutAnimationConfig;
914
- for (auto const &mutation : mutations) {
915
- ShadowView baselineShadowView =
916
- (mutation.type == ShadowViewMutation::Type::Delete ||
917
- mutation.type == ShadowViewMutation::Type::Remove ||
918
- mutation.type == ShadowViewMutation::Type::Update
919
- ? mutation.oldChildShadowView
920
- : mutation.newChildShadowView);
921
- react_native_assert(baselineShadowView.tag > 0);
922
- bool haveComponentDescriptor =
923
- hasComponentDescriptorForShadowView(baselineShadowView);
633
+ react_native_assert(props != nullptr);
634
+ if (props != nullptr) {
635
+ viewFinal.props = props;
636
+ }
637
+ }
924
638
 
925
- // Immediately execute any mutations on a root node
926
- if (baselineShadowView.traits.check(
927
- ShadowNodeTraits::Trait::RootNodeKind)) {
928
- immediateMutations.push_back(mutation);
929
- continue;
930
- }
639
+ PrintMutationInstruction(
640
+ "Setting up animation KeyFrame for REMOVE mutation (Delete animation)",
641
+ mutation);
931
642
 
932
- better::optional<ShadowViewMutation> executeMutationImmediately{};
643
+ keyFrame = AnimationKeyFrame{
644
+ /* .finalMutationsForKeyFrame */ {mutation, deleteMutation},
645
+ /* .type */ AnimationConfigurationType::Delete,
646
+ /* .tag */ tag,
647
+ /* .parentView */ parent,
648
+ /* .viewStart */ viewStart,
649
+ /* .viewEnd */ viewFinal,
650
+ /* .viewPrev */ baselineShadowView,
651
+ /* .initialProgress */ 0};
652
+ } else {
653
+ PrintMutationInstruction(
654
+ "Executing Remove Immediately, due to reordering operation",
655
+ mutation);
656
+ immediateMutations.push_back(mutation);
657
+ continue;
658
+ }
659
+ }
933
660
 
934
- bool isRemoveReinserted =
935
- mutation.type == ShadowViewMutation::Type::Remove &&
936
- std::find(
937
- insertedTags.begin(),
938
- insertedTags.end(),
939
- mutation.oldChildShadowView.tag) != insertedTags.end();
661
+ // Handle conflicting animations
662
+ for (auto &conflictingKeyFrame : conflictingAnimations) {
663
+ auto const &conflictingMutationBaselineShadowView =
664
+ conflictingKeyFrame.viewStart;
940
665
 
941
- // Reparenting can result in a node being removed, inserted (moved) and
942
- // also deleted and created in the same frame, with the same props etc.
943
- // This should eventually be optimized out of the diffing algorithm, but
944
- // for now we detect reparenting and prevent the corresponding
945
- // Delete/Create instructions from being animated.
946
- bool isReparented = std::find(
947
- reparentedTags.begin(),
948
- reparentedTags.end(),
949
- baselineShadowView.tag) != reparentedTags.end();
666
+ // We've found a conflict.
667
+ if (conflictingMutationBaselineShadowView.tag == tag) {
668
+ conflictingKeyFrame.generateFinalSyntheticMutations = false;
950
669
 
951
- if (isRemoveReinserted) {
952
- movedTags.insert({mutation.oldChildShadowView.tag, mutation});
953
- }
670
+ // Do NOT update viewStart for a CREATE animation.
671
+ if (keyFrame.type == AnimationConfigurationType::Create) {
672
+ break;
673
+ }
954
674
 
955
- // Inserts that follow a "remove" of the same tag should be treated as
956
- // an update (move) animation.
957
- bool wasInsertedTagRemoved = false;
958
- auto movedIt = movedTags.end();
959
- if (mutation.type == ShadowViewMutation::Type::Insert) {
960
- // If this is a move, we actually don't want to copy this insert
961
- // instruction to animated instructions - we want to
962
- // generate an Update mutation for Remove+Insert pairs to animate
963
- // the layout.
964
- // The corresponding Remove and Insert instructions will instead
965
- // be treated as "immediate" instructions.
966
- movedIt = movedTags.find(mutation.newChildShadowView.tag);
967
- wasInsertedTagRemoved = movedIt != movedTags.end();
675
+ #ifdef LAYOUT_ANIMATION_VERBOSE_LOGGING
676
+ LOG(ERROR)
677
+ << "Due to conflict, replacing 'viewStart' of animated keyframe: ["
678
+ << conflictingKeyFrame.viewPrev.tag << "] with ##"
679
+ << std::hash<ShadowView>{}(conflictingKeyFrame.viewPrev);
680
+ #endif
681
+ // Pick a Prop or layout property, depending on the current
682
+ // animation configuration. Figure out how much progress we've
683
+ // already made in the current animation, and start the animation
684
+ // from this point.
685
+ keyFrame.viewPrev = conflictingKeyFrame.viewPrev;
686
+ keyFrame.viewStart = conflictingKeyFrame.viewPrev;
687
+ react_native_assert(keyFrame.viewStart.tag > 0);
688
+ keyFrame.initialProgress = 0;
689
+
690
+ // We're guaranteed that a tag only has one animation associated
691
+ // with it, so we can break here. If we support multiple
692
+ // animations and animation curves over the same tag in the
693
+ // future, this will need to be modified to support that.
694
+ break;
695
+ }
696
+ }
697
+
698
+ #ifdef LAYOUT_ANIMATION_VERBOSE_LOGGING
699
+ LOG(ERROR) << "Checking validity of keyframe: ["
700
+ << keyFrame.viewStart.tag << "] [" << keyFrame.viewEnd.tag
701
+ << "] [" << keyFrame.viewPrev.tag
702
+ << "] animation type: " << (int)keyFrame.type;
703
+ #endif
704
+ react_native_assert(keyFrame.viewStart.tag > 0);
705
+ react_native_assert(keyFrame.viewEnd.tag > 0);
706
+ react_native_assert(keyFrame.viewPrev.tag > 0);
707
+ keyFramesToAnimate.push_back(keyFrame);
968
708
  }
969
709
 
970
- auto const &mutationConfig =
971
- (mutation.type == ShadowViewMutation::Type::Delete ||
972
- (mutation.type == ShadowViewMutation::Type::Remove &&
973
- !wasInsertedTagRemoved)
974
- ? layoutAnimationConfig.deleteConfig
975
- : (mutation.type == ShadowViewMutation::Type::Insert &&
976
- !wasInsertedTagRemoved
977
- ? layoutAnimationConfig.createConfig
978
- : layoutAnimationConfig.updateConfig));
979
- bool haveConfiguration =
980
- mutationConfig.animationType != AnimationType::None;
710
+ if (executeMutationImmediately.hasValue()) {
711
+ PrintMutationInstruction(
712
+ "Queue Up For Immediate Execution", *executeMutationImmediately);
713
+ immediateMutations.push_back(*executeMutationImmediately);
714
+ }
715
+ }
981
716
 
982
- if (wasInsertedTagRemoved && haveConfiguration) {
983
- movesToAnimate.push_back(AnimationKeyFrame{
984
- {},
985
- AnimationConfigurationType::Update,
986
- mutation.newChildShadowView.tag,
987
- mutation.parentShadowView,
988
- movedIt->second.oldChildShadowView,
989
- mutation.newChildShadowView});
717
+ #ifdef RN_SHADOW_TREE_INTROSPECTION
718
+ #ifdef LAYOUT_ANIMATION_VERBOSE_LOGGING
719
+ {
720
+ int idx = 0;
721
+ for (auto &mutation : immediateMutations) {
722
+ PrintMutationInstruction(
723
+ std::string("IMMEDIATE list: ") + std::to_string(idx) + "/" +
724
+ std::to_string(immediateMutations.size()),
725
+ mutation);
726
+ idx++;
990
727
  }
728
+ }
991
729
 
992
- // Creates and inserts should also be executed immediately.
993
- // Mutations that would otherwise be animated, but have no
994
- // configuration, are also executed immediately.
995
- if (isRemoveReinserted || !haveConfiguration || isReparented ||
996
- mutation.type == ShadowViewMutation::Type::Create ||
997
- mutation.type == ShadowViewMutation::Type::Insert) {
998
- executeMutationImmediately = mutation;
730
+ {
731
+ int idx = 0;
732
+ for (const auto &keyframe : keyFramesToAnimate) {
733
+ for (const auto &finalMutationForKeyFrame :
734
+ keyframe.finalMutationsForKeyFrame) {
735
+ PrintMutationInstruction(
736
+ std::string("FINAL list: ") + std::to_string(idx) + "/" +
737
+ std::to_string(keyFramesToAnimate.size()),
738
+ finalMutationForKeyFrame);
739
+ }
740
+ idx++;
741
+ }
742
+ }
743
+ #endif
744
+ #endif
999
745
 
1000
- // It is possible, especially in the case of "moves", that we have a
1001
- // sequence of operations like:
1002
- // UPDATE X
1003
- // REMOVE X
1004
- // INSERT X
1005
- // In these cases, we will have queued up an animation for the UPDATE
1006
- // and delayed its execution; the REMOVE and INSERT will be executed
1007
- // first; and then the UPDATE will be animating to/from ShadowViews
1008
- // that are out-of-sync with what's on the mounting layer. Thus, for
1009
- // any UPDATE animations already queued up for this tag, we adjust the
1010
- // "previous" ShadowView.
1011
- if (mutation.type == ShadowViewMutation::Type::Insert) {
1012
- for (auto &keyframe : keyFramesToAnimate) {
1013
- if (keyframe.tag == baselineShadowView.tag) {
1014
- // If there's already an animation queued up, followed by this
1015
- // Insert, it *must* be an Update mutation animation. Other
1016
- // sequences should not be possible.
1017
- react_native_assert(
1018
- keyframe.type == AnimationConfigurationType::Update);
1019
-
1020
- // The mutation is an "insert", so it must have a
1021
- // "newChildShadowView"
1022
- react_native_assert(mutation.newChildShadowView.tag > 0);
1023
-
1024
- // Those asserts don't run in prod. If there's some edge-case
1025
- // that we haven't caught yet, we'd crash in debug; make sure we
1026
- // don't mutate the prevView in prod.
1027
- if (keyframe.type == AnimationConfigurationType::Update &&
1028
- mutation.newChildShadowView.tag > 0) {
1029
- keyframe.viewPrev = mutation.newChildShadowView;
1030
- }
1031
- }
1032
- }
1033
- } else if (mutation.type == ShadowViewMutation::Type::Remove) {
1034
- for (auto &keyframe : keyFramesToAnimate) {
1035
- if (keyframe.tag == baselineShadowView.tag) {
1036
- // If there's already an animation queued up, followed by this
1037
- // Insert, it *must* be an Update mutation animation. Other
1038
- // sequences should not be possible.
1039
- react_native_assert(
1040
- keyframe.type == AnimationConfigurationType::Update);
746
+ auto finalConflictingMutations = ShadowViewMutationList{};
747
+ for (auto &keyFrame : conflictingAnimations) {
748
+ // Special-case: if we have some (1) ongoing UPDATE animation,
749
+ // (2) it conflicted with a new MOVE operation (REMOVE+INSERT)
750
+ // without another corresponding UPDATE, we should re-queue the
751
+ // keyframe so that its position/props don't suddenly "jump".
752
+ if (keyFrame.type == AnimationConfigurationType::Update) {
753
+ auto movedIt = movedTags.find(keyFrame.tag);
754
+ if (movedIt != movedTags.end()) {
755
+ auto newKeyFrameForUpdate = std::find_if(
756
+ keyFramesToAnimate.begin(),
757
+ keyFramesToAnimate.end(),
758
+ [&](auto const &newKeyFrame) {
759
+ return newKeyFrame.type ==
760
+ AnimationConfigurationType::Update &&
761
+ newKeyFrame.tag == keyFrame.tag;
762
+ });
763
+ if (newKeyFrameForUpdate == keyFramesToAnimate.end()) {
764
+ keyFrame.invalidated = false;
1041
765
 
1042
- // The mutation is a "remove", so it must have a
1043
- // "oldChildShadowView"
1044
- react_native_assert(mutation.oldChildShadowView.tag > 0);
766
+ // The animation will continue from the current position - we
767
+ // restart viewStart to make sure there are no sudden jumps
768
+ keyFrame.viewStart = keyFrame.viewPrev;
1045
769
 
1046
- // Those asserts don't run in prod. If there's some edge-case
1047
- // that we haven't caught yet, we'd crash in debug; make sure we
1048
- // don't mutate the prevView in prod.
1049
- // Since normally the UPDATE would have been executed first and
1050
- // now it's deferred, we need to change the `oldChildShadowView`
1051
- // that is being referenced by the REMOVE mutation.
1052
- if (keyframe.type == AnimationConfigurationType::Update &&
1053
- mutation.oldChildShadowView.tag > 0) {
1054
- executeMutationImmediately = ShadowViewMutation{
1055
- mutation.type,
1056
- mutation.parentShadowView,
1057
- keyframe.viewPrev,
1058
- {},
1059
- mutation.index};
770
+ // Find the insert mutation that conflicted with this update
771
+ for (auto &mutation : immediateMutations) {
772
+ if (mutation.newChildShadowView.tag == keyFrame.tag &&
773
+ (mutation.type == ShadowViewMutation::Insert ||
774
+ mutation.type == ShadowViewMutation::Create)) {
775
+ keyFrame.viewPrev = mutation.newChildShadowView;
776
+ keyFrame.viewEnd = mutation.newChildShadowView;
1060
777
  }
1061
778
  }
779
+ keyFramesToAnimate.push_back(keyFrame);
780
+ continue;
1062
781
  }
1063
782
  }
1064
783
  }
1065
784
 
1066
- // Deletes, non-move inserts, updates get animated
1067
- if (!wasInsertedTagRemoved && !isRemoveReinserted && !isReparented &&
1068
- haveConfiguration &&
1069
- mutation.type != ShadowViewMutation::Type::Create) {
1070
- ShadowView viewStart = ShadowView(
1071
- mutation.type == ShadowViewMutation::Type::Insert
1072
- ? mutation.newChildShadowView
1073
- : mutation.oldChildShadowView);
1074
- react_native_assert(viewStart.tag > 0);
1075
- ShadowView viewFinal = ShadowView(
1076
- mutation.type == ShadowViewMutation::Type::Update
1077
- ? mutation.newChildShadowView
1078
- : viewStart);
1079
- react_native_assert(viewFinal.tag > 0);
1080
- ShadowView parent = mutation.parentShadowView;
1081
- react_native_assert(
1082
- parent.tag > 0 ||
1083
- mutation.type == ShadowViewMutation::Type::Update ||
1084
- mutation.type == ShadowViewMutation::Type::Delete);
1085
- Tag tag = viewStart.tag;
1086
-
1087
- AnimationKeyFrame keyFrame{};
1088
- if (mutation.type == ShadowViewMutation::Type::Insert) {
1089
- if (mutationConfig.animationProperty ==
1090
- AnimationProperty::Opacity &&
1091
- haveComponentDescriptor) {
1092
- auto props =
1093
- getComponentDescriptorForShadowView(baselineShadowView)
1094
- .cloneProps(propsParserContext, viewStart.props, {});
785
+ // If the "final" mutation is already accounted for, by previously
786
+ // setting the correct "viewPrev" of the next conflicting animation, we
787
+ // don't want to queue up any final UPDATE mutations here.
788
+ bool shouldGenerateSyntheticMutations =
789
+ keyFrame.generateFinalSyntheticMutations;
790
+ auto numFinalMutations = keyFrame.finalMutationsForKeyFrame.size();
791
+ bool onlyMutationIsUpdate =
792
+ (numFinalMutations == 1 &&
793
+ keyFrame.finalMutationsForKeyFrame[0].type ==
794
+ ShadowViewMutation::Update);
795
+ if (!shouldGenerateSyntheticMutations &&
796
+ (numFinalMutations == 0 || onlyMutationIsUpdate)) {
797
+ continue;
798
+ }
1095
799
 
1096
- // Dynamic cast, because - we don't know the type of this
1097
- // ShadowNode, it could be Image or Text or something else with
1098
- // different base props.
1099
- const auto viewProps =
1100
- dynamic_cast<const ViewProps *>(props.get());
1101
- if (viewProps != nullptr) {
1102
- const_cast<ViewProps *>(viewProps)->opacity = 0;
1103
- }
800
+ queueFinalMutationsForCompletedKeyFrame(
801
+ keyFrame,
802
+ finalConflictingMutations,
803
+ true,
804
+ "KeyFrameManager: Finished Conflicting Keyframe");
805
+ }
1104
806
 
1105
- react_native_assert(props != nullptr);
1106
- if (props != nullptr) {
1107
- viewStart.props = props;
1108
- }
1109
- }
1110
- bool isScaleX =
1111
- mutationConfig.animationProperty == AnimationProperty::ScaleX ||
1112
- mutationConfig.animationProperty == AnimationProperty::ScaleXY;
1113
- bool isScaleY =
1114
- mutationConfig.animationProperty == AnimationProperty::ScaleY ||
1115
- mutationConfig.animationProperty == AnimationProperty::ScaleXY;
1116
- if ((isScaleX || isScaleY) && haveComponentDescriptor) {
1117
- auto props =
1118
- getComponentDescriptorForShadowView(baselineShadowView)
1119
- .cloneProps(propsParserContext, viewStart.props, {});
807
+ // Make sure that all operations execute in the proper order, since
808
+ // conflicting animations are not sorted in any reasonable way.
809
+ std::stable_sort(
810
+ finalConflictingMutations.begin(),
811
+ finalConflictingMutations.end(),
812
+ &shouldFirstComeBeforeSecondMutation);
1120
813
 
1121
- // Dynamic cast, because - we don't know the type of this
1122
- // ShadowNode, it could be Image or Text or something else with
1123
- // different base props.
1124
- const auto viewProps =
1125
- dynamic_cast<const ViewProps *>(props.get());
1126
- if (viewProps != nullptr) {
1127
- const_cast<ViewProps *>(viewProps)->transform =
1128
- Transform::Scale(isScaleX ? 0 : 1, isScaleY ? 0 : 1, 1);
1129
- }
814
+ std::stable_sort(
815
+ immediateMutations.begin(),
816
+ immediateMutations.end(),
817
+ &shouldFirstComeBeforeSecondRemovesOnly);
1130
818
 
1131
- react_native_assert(props != nullptr);
1132
- if (props != nullptr) {
1133
- viewStart.props = props;
1134
- }
1135
- }
819
+ animation.keyFrames = keyFramesToAnimate;
820
+ inflightAnimations_.push_back(std::move(animation));
1136
821
 
1137
- PrintMutationInstruction(
1138
- "Setting up animation KeyFrame for INSERT mutation (Create animation)",
1139
- mutation);
822
+ // At this point, we have the following information and knowledge graph:
823
+ // Knowledge Graph:
824
+ // [ImmediateMutations] -> assumes [FinalConflicting], [FrameDelayed],
825
+ // [Delayed] already executed [FrameDelayed] -> assumes
826
+ // [FinalConflicting], [Delayed] already executed [FinalConflicting] ->
827
+ // is adjusted based on [Delayed], no dependency on [FinalConflicting],
828
+ // [FrameDelayed] [Delayed] -> assumes [FinalConflicting],
829
+ // [ImmediateMutations] not executed yet
1140
830
 
1141
- keyFrame = AnimationKeyFrame{
1142
- {},
1143
- AnimationConfigurationType::Create,
1144
- tag,
1145
- parent,
1146
- viewStart,
1147
- viewFinal,
1148
- baselineShadowView,
1149
- 0};
1150
- } else if (mutation.type == ShadowViewMutation::Type::Delete) {
1151
- // This is just for assertion purposes.
1152
- // The NDEBUG check here is to satisfy the compiler in certain environments
1153
- // complaining about correspondingRemoveIt being unused.
1154
- #ifdef REACT_NATIVE_DEBUG
1155
- #ifndef NDEBUG
1156
- // This block is temporarily disabled to fix some internal builds.
1157
- // In some build configurations, we get a compiler error that
1158
- // `correspondingRemoveIt` is unused.
1159
- /* Tag deleteTag = mutation.oldChildShadowView.tag;
1160
- auto correspondingRemoveIt = std::find_if(
1161
- mutations.begin(),
1162
- mutations.end(),
1163
- [&deleteTag](auto &mutation) {
1164
- return mutation.type == ShadowViewMutation::Type::Remove &&
1165
- mutation.oldChildShadowView.tag == deleteTag;
1166
- });
1167
- react_native_assert(correspondingRemoveIt != mutations.end());
1168
- */
1169
- #endif
831
+ // Adjust [Delayed] based on [FinalConflicting]
832
+ // Knowledge Graph:
833
+ // [ImmediateMutations] -> assumes [FinalConflicting], [FrameDelayed],
834
+ // [Delayed] already executed [FrameDelayed] -> assumes
835
+ // [FinalConflicting], [Delayed] already executed [FinalConflicting] ->
836
+ // is adjusted based on [Delayed], no dependency on [FinalConflicting],
837
+ // [FrameDelayed] [Delayed] -> adjusted for [FinalConflicting]; assumes
838
+ // [ImmediateMutations] not executed yet
839
+ #ifdef LAYOUT_ANIMATION_VERBOSE_LOGGING
840
+ LOG(ERROR) << "Adjust [Delayed] based on [FinalConflicting]";
1170
841
  #endif
1171
- continue;
1172
- } else if (mutation.type == ShadowViewMutation::Type::Update) {
1173
- viewFinal = ShadowView(mutation.newChildShadowView);
1174
-
1175
- PrintMutationInstruction(
1176
- "Setting up animation KeyFrame for UPDATE mutation (Update animation)",
1177
- mutation);
842
+ for (auto &mutation : finalConflictingMutations) {
843
+ if (mutation.type == ShadowViewMutation::Type::Insert ||
844
+ mutation.type == ShadowViewMutation::Type::Remove) {
845
+ adjustDelayedMutationIndicesForMutation(surfaceId, mutation, true);
846
+ }
847
+ }
1178
848
 
1179
- keyFrame = AnimationKeyFrame{
1180
- {mutation},
1181
- AnimationConfigurationType::Update,
1182
- tag,
1183
- parent,
1184
- viewStart,
1185
- viewFinal,
1186
- baselineShadowView,
1187
- 0};
1188
- } else {
1189
- // This should just be "Remove" instructions that are not animated
1190
- // (either this is a "move", or there's a corresponding "Delete"
1191
- // that is animated).
1192
- react_native_assert(
1193
- mutation.type == ShadowViewMutation::Type::Remove);
849
+ // Adjust [FrameDelayed] based on [Delayed]
850
+ // Knowledge Graph:
851
+ // [ImmediateExecutions] -> assumes [FinalConflicting], [Delayed],
852
+ // [FrameDelayed] already executed [FrameDelayed] -> adjusted for
853
+ // [Delayed]; assumes [FinalConflicting] already executed
854
+ // [FinalConflicting] -> is adjusted based on [Delayed], no dependency
855
+ // on [FinalConflicting], [FrameDelayed] [Delayed] -> adjusted for
856
+ // [FinalConflicting]; assumes [ImmediateExecutions] not executed yet
857
+ #ifdef LAYOUT_ANIMATION_VERBOSE_LOGGING
858
+ LOG(ERROR) << "Adjust [FrameDelayed] based on [Delayed]";
859
+ #endif
860
+ for (auto &keyframe : inflightAnimations_.back().keyFrames) {
861
+ for (auto &finalMutation : keyframe.finalMutationsForKeyFrame) {
862
+ if (finalMutation.type == ShadowViewMutation::Type::Insert ||
863
+ finalMutation.type == ShadowViewMutation::Type::Remove) {
864
+ // When adjusting, skip adjusting against last animation - because
865
+ // all `mutation`s here come from the last animation, so we can't
866
+ // adjust a batch against itself.
867
+ adjustImmediateMutationIndicesForDelayedMutations(
868
+ surfaceId, finalMutation, true);
869
+ }
870
+ }
871
+ }
1194
872
 
1195
- Tag removeTag = mutation.oldChildShadowView.tag;
1196
- auto correspondingInsertIt = std::find_if(
1197
- mutations.begin(),
1198
- mutations.end(),
1199
- [&removeTag](auto &mutation) {
1200
- return mutation.type == ShadowViewMutation::Type::Insert &&
1201
- mutation.newChildShadowView.tag == removeTag;
1202
- });
1203
- if (correspondingInsertIt == mutations.end()) {
1204
- // This is a REMOVE not paired with an INSERT (move), so it must
1205
- // be paired with a DELETE.
1206
- auto correspondingDeleteIt = std::find_if(
1207
- mutations.begin(),
1208
- mutations.end(),
1209
- [&removeTag](auto &mutation) {
1210
- return mutation.type == ShadowViewMutation::Type::Delete &&
1211
- mutation.oldChildShadowView.tag == removeTag;
1212
- });
1213
- react_native_assert(correspondingDeleteIt != mutations.end());
1214
-
1215
- auto deleteMutation = *correspondingDeleteIt;
1216
-
1217
- if (mutationConfig.animationProperty ==
1218
- AnimationProperty::Opacity &&
1219
- haveComponentDescriptor) {
1220
- auto props =
1221
- getComponentDescriptorForShadowView(baselineShadowView)
1222
- .cloneProps(propsParserContext, viewFinal.props, {});
1223
-
1224
- // Dynamic cast, because - we don't know the type of this
1225
- // ShadowNode, it could be Image or Text or something else with
1226
- // different base props.
1227
- const auto viewProps =
1228
- dynamic_cast<const ViewProps *>(props.get());
1229
- if (viewProps != nullptr) {
1230
- const_cast<ViewProps *>(viewProps)->opacity = 0;
1231
- }
1232
-
1233
- react_native_assert(props != nullptr);
1234
- if (props != nullptr) {
1235
- viewFinal.props = props;
1236
- }
1237
- }
1238
- bool isScaleX = mutationConfig.animationProperty ==
1239
- AnimationProperty::ScaleX ||
1240
- mutationConfig.animationProperty ==
1241
- AnimationProperty::ScaleXY;
1242
- bool isScaleY = mutationConfig.animationProperty ==
1243
- AnimationProperty::ScaleY ||
1244
- mutationConfig.animationProperty ==
1245
- AnimationProperty::ScaleXY;
1246
- if ((isScaleX || isScaleY) && haveComponentDescriptor) {
1247
- auto props =
1248
- getComponentDescriptorForShadowView(baselineShadowView)
1249
- .cloneProps(propsParserContext, viewFinal.props, {});
1250
-
1251
- // Dynamic cast, because - we don't know the type of this
1252
- // ShadowNode, it could be Image or Text or something else with
1253
- // different base props.
1254
- const auto viewProps =
1255
- dynamic_cast<const ViewProps *>(props.get());
1256
- if (viewProps != nullptr) {
1257
- const_cast<ViewProps *>(viewProps)->transform =
1258
- Transform::Scale(isScaleX ? 0 : 1, isScaleY ? 0 : 1, 1);
1259
- }
1260
-
1261
- react_native_assert(props != nullptr);
1262
- if (props != nullptr) {
1263
- viewFinal.props = props;
1264
- }
1265
- }
1266
-
1267
- PrintMutationInstruction(
1268
- "Setting up animation KeyFrame for REMOVE mutation (Delete animation)",
1269
- mutation);
1270
-
1271
- keyFrame = AnimationKeyFrame{
1272
- {mutation, deleteMutation},
1273
- AnimationConfigurationType::Delete,
1274
- tag,
1275
- parent,
1276
- viewStart,
1277
- viewFinal,
1278
- baselineShadowView,
1279
- 0};
1280
- } else {
1281
- PrintMutationInstruction(
1282
- "Executing Remove Immediately, due to reordering operation",
1283
- mutation);
1284
- immediateMutations.push_back(mutation);
1285
- continue;
1286
- }
1287
- }
1288
-
1289
- // Handle conflicting animations
1290
- for (auto &conflictingKeyFrame : conflictingAnimations) {
1291
- auto const &conflictingMutationBaselineShadowView =
1292
- conflictingKeyFrame.viewStart;
1293
-
1294
- // We've found a conflict.
1295
- if (conflictingMutationBaselineShadowView.tag == tag) {
1296
- conflictingKeyFrame.generateFinalSyntheticMutations = false;
1297
-
1298
- // Do NOT update viewStart for a CREATE animation.
1299
- if (keyFrame.type == AnimationConfigurationType::Create) {
1300
- break;
1301
- }
873
+ // Adjust [ImmediateExecutions] based on [Delayed]
874
+ // Knowledge Graph:
875
+ // [ImmediateExecutions] -> adjusted for [FrameDelayed], [Delayed];
876
+ // assumes [FinalConflicting] already executed [FrameDelayed] ->
877
+ // adjusted for [Delayed]; assumes [FinalConflicting] already executed
878
+ // [FinalConflicting] -> is adjusted based on [Delayed], no dependency
879
+ // on [FinalConflicting], [FrameDelayed] [Delayed] -> adjusted for
880
+ // [FinalConflicting]; assumes [ImmediateExecutions] not executed yet
881
+ //
882
+ // THEN,
883
+ // Adjust [Delayed] based on [ImmediateExecutions] and
884
+ // [FinalConflicting] Knowledge Graph: [ImmediateExecutions] -> adjusted
885
+ // for [FrameDelayed], [Delayed]; assumes [FinalConflicting] already
886
+ // executed [FrameDelayed] -> adjusted for [Delayed]; assumes
887
+ // [FinalConflicting] already executed [FinalConflicting] -> is adjusted
888
+ // based on [Delayed], no dependency on [FinalConflicting],
889
+ // [FrameDelayed] [Delayed] -> adjusted for [FinalConflicting],
890
+ // [ImmediateExecutions]
891
+ //
892
+ // We do these in the same loop because each immediate execution is
893
+ // impacted by each delayed mutation, and also can impact each delayed
894
+ // mutation, and these effects compound.
895
+ #ifdef LAYOUT_ANIMATION_VERBOSE_LOGGING
896
+ LOG(ERROR)
897
+ << "Adjust each [ImmediateExecution] based on [Delayed] and [Delayed] based on each [ImmediateExecution]";
898
+ #endif
899
+ for (auto &mutation : immediateMutations) {
900
+ // Note: when adjusting [ImmediateExecutions] based on [FrameDelayed],
901
+ // we need only adjust Inserts. Since inserts are executed
902
+ // highest-index-first, lower indices being delayed does not impact
903
+ // the higher-index removals; and conversely, higher indices being
904
+ // delayed cannot impact lower index removal, regardless of order.
905
+ if (mutation.type == ShadowViewMutation::Type::Insert ||
906
+ mutation.type == ShadowViewMutation::Type::Remove) {
907
+ adjustImmediateMutationIndicesForDelayedMutations(
908
+ surfaceId,
909
+ mutation,
910
+ mutation.type == ShadowViewMutation::Type::Remove);
911
+ // Here we need to adjust both Delayed and FrameDelayed mutations.
912
+ // Delayed Removes can be impacted by non-delayed Inserts from the
913
+ // same frame.
914
+ adjustDelayedMutationIndicesForMutation(surfaceId, mutation);
915
+ }
916
+ }
1302
917
 
918
+ // If the knowledge graph progression above is correct, it is now safe
919
+ // to execute finalConflictingMutations and immediateMutations in that
920
+ // order, and to queue the delayed animations from this frame.
921
+ //
922
+ // Execute the conflicting, delayed operations immediately. Any UPDATE
923
+ // operations that smoothly transition into another animation will be
924
+ // overridden by generated UPDATE operations at the end of the list, and
925
+ // we want any REMOVE or DELETE operations to execute immediately.
926
+ // Additionally, this should allow us to avoid performing index
927
+ // adjustment between this list of conflicting animations and the batch
928
+ // we're about to execute.
929
+ finalConflictingMutations.insert(
930
+ finalConflictingMutations.end(),
931
+ immediateMutations.begin(),
932
+ immediateMutations.end());
933
+ mutations = finalConflictingMutations;
934
+ } /* if (currentAnimation) */
935
+ else {
936
+ // If there's no "next" animation, make sure we queue up "final"
937
+ // operations from all ongoing, conflicting animations.
1303
938
  #ifdef LAYOUT_ANIMATION_VERBOSE_LOGGING
1304
- LOG(ERROR)
1305
- << "Due to conflict, replacing 'viewStart' of animated keyframe: ["
1306
- << conflictingKeyFrame.viewPrev.tag << "] with ##"
1307
- << std::hash<ShadowView>{}(conflictingKeyFrame.viewPrev);
939
+ LOG(ERROR) << "No Animation: Queue up final conflicting animations";
1308
940
  #endif
1309
- // Pick a Prop or layout property, depending on the current
1310
- // animation configuration. Figure out how much progress we've
1311
- // already made in the current animation, and start the animation
1312
- // from this point.
1313
- keyFrame.viewPrev = conflictingKeyFrame.viewPrev;
1314
- keyFrame.viewStart = conflictingKeyFrame.viewPrev;
1315
- react_native_assert(keyFrame.viewStart.tag > 0);
1316
- keyFrame.initialProgress = 0;
941
+ ShadowViewMutationList finalMutationsForConflictingAnimations{};
942
+ for (auto const &keyFrame : conflictingAnimations) {
943
+ queueFinalMutationsForCompletedKeyFrame(
944
+ keyFrame,
945
+ finalMutationsForConflictingAnimations,
946
+ true,
947
+ "Conflict with non-animated mutation");
948
+ }
1317
949
 
1318
- // We're guaranteed that a tag only has one animation associated
1319
- // with it, so we can break here. If we support multiple
1320
- // animations and animation curves over the same tag in the
1321
- // future, this will need to be modified to support that.
1322
- break;
1323
- }
1324
- }
950
+ // Make sure that all operations execute in the proper order.
951
+ // REMOVE operations with highest indices must operate first.
952
+ std::stable_sort(
953
+ finalMutationsForConflictingAnimations.begin(),
954
+ finalMutationsForConflictingAnimations.end(),
955
+ &shouldFirstComeBeforeSecondMutation);
1325
956
 
1326
957
  #ifdef LAYOUT_ANIMATION_VERBOSE_LOGGING
1327
- LOG(ERROR) << "Checking validity of keyframe: ["
1328
- << keyFrame.viewStart.tag << "] [" << keyFrame.viewEnd.tag
1329
- << "] [" << keyFrame.viewPrev.tag
1330
- << "] animation type: " << (int)keyFrame.type;
958
+ LOG(ERROR)
959
+ << "No Animation: Adjust delayed mutations based on all finalMutationsForConflictingAnimations";
1331
960
  #endif
1332
- react_native_assert(keyFrame.viewStart.tag > 0);
1333
- react_native_assert(keyFrame.viewEnd.tag > 0);
1334
- react_native_assert(keyFrame.viewPrev.tag > 0);
1335
- keyFramesToAnimate.push_back(keyFrame);
1336
- }
1337
-
1338
- if (executeMutationImmediately.hasValue()) {
1339
- PrintMutationInstruction(
1340
- "Queue Up For Immediate Execution", *executeMutationImmediately);
1341
- immediateMutations.push_back(*executeMutationImmediately);
961
+ for (auto const &mutation : finalMutationsForConflictingAnimations) {
962
+ if (mutation.type == ShadowViewMutation::Type::Remove ||
963
+ mutation.type == ShadowViewMutation::Type::Insert) {
964
+ adjustDelayedMutationIndicesForMutation(surfaceId, mutation);
1342
965
  }
1343
966
  }
1344
967
 
1345
- #ifdef RN_SHADOW_TREE_INTROSPECTION
968
+ // The ShadowTree layer doesn't realize that certain operations have
969
+ // been delayed, so we must adjust all Remove and Insert operations
970
+ // based on what else has been deferred, whether we are executing this
971
+ // immediately or later.
1346
972
  #ifdef LAYOUT_ANIMATION_VERBOSE_LOGGING
1347
- {
1348
- int idx = 0;
1349
- for (auto &mutation : immediateMutations) {
1350
- PrintMutationInstruction(
1351
- std::string("IMMEDIATE list: ") + std::to_string(idx) + "/" +
1352
- std::to_string(immediateMutations.size()),
1353
- mutation);
1354
- idx++;
973
+ LOG(ERROR)
974
+ << "No Animation: Adjust mutations based on remaining delayed mutations / adjust delayed, based on each";
975
+ #endif
976
+ for (auto &mutation : mutations) {
977
+ if (mutation.type == ShadowViewMutation::Type::Remove ||
978
+ mutation.type == ShadowViewMutation::Type::Insert) {
979
+ adjustImmediateMutationIndicesForDelayedMutations(
980
+ surfaceId, mutation);
981
+ adjustDelayedMutationIndicesForMutation(surfaceId, mutation);
1355
982
  }
1356
983
  }
1357
984
 
1358
- {
1359
- int idx = 0;
1360
- for (const auto &keyframe : keyFramesToAnimate) {
1361
- for (const auto &finalMutationForKeyFrame :
1362
- keyframe.finalMutationsForKeyFrame) {
1363
- PrintMutationInstruction(
1364
- std::string("FINAL list: ") + std::to_string(idx) + "/" +
1365
- std::to_string(keyFramesToAnimate.size()),
1366
- finalMutationForKeyFrame);
1367
- }
1368
- idx++;
1369
- }
1370
- }
1371
- #endif
1372
- #endif
985
+ // Append mutations to this list and swap - so that the final
986
+ // conflicting mutations happen before any other mutations
987
+ finalMutationsForConflictingAnimations.insert(
988
+ finalMutationsForConflictingAnimations.end(),
989
+ mutations.begin(),
990
+ mutations.end());
991
+ mutations = finalMutationsForConflictingAnimations;
992
+ }
993
+ } // if (mutations)
1373
994
 
1374
- auto finalConflictingMutations = ShadowViewMutationList{};
1375
- for (auto &keyFrame : conflictingAnimations) {
1376
- // Special-case: if we have some (1) ongoing UPDATE animation,
1377
- // (2) it conflicted with a new MOVE operation (REMOVE+INSERT)
1378
- // without another corresponding UPDATE, we should re-queue the
1379
- // keyframe so that its position/props don't suddenly "jump".
1380
- if (keyFrame.type == AnimationConfigurationType::Update) {
1381
- auto movedIt = movedTags.find(keyFrame.tag);
1382
- if (movedIt != movedTags.end()) {
1383
- auto newKeyFrameForUpdate = std::find_if(
1384
- keyFramesToAnimate.begin(),
1385
- keyFramesToAnimate.end(),
1386
- [&](auto const &newKeyFrame) {
1387
- return newKeyFrame.type ==
1388
- AnimationConfigurationType::Update &&
1389
- newKeyFrame.tag == keyFrame.tag;
1390
- });
1391
- if (newKeyFrameForUpdate == keyFramesToAnimate.end()) {
1392
- keyFrame.invalidated = false;
995
+ // We never commit a different root or modify anything -
996
+ // we just send additional mutations to the mounting layer until the
997
+ // animations are finished and the mounting layer (view) represents exactly
998
+ // what is in the most recent shadow tree
999
+ // Add animation mutations to the end of our existing mutations list in this
1000
+ // function.
1001
+ ShadowViewMutationList mutationsForAnimation{};
1002
+ animationMutationsForFrame(surfaceId, mutationsForAnimation, now);
1393
1003
 
1394
- // The animation will continue from the current position - we
1395
- // restart viewStart to make sure there are no sudden jumps
1396
- keyFrame.viewStart = keyFrame.viewPrev;
1004
+ // If any delayed removes were executed, update remaining delayed keyframes
1005
+ #ifdef LAYOUT_ANIMATION_VERBOSE_LOGGING
1006
+ LOG(ERROR)
1007
+ << "Adjust all delayed mutations based on final mutations generated by animation driver";
1008
+ #endif
1009
+ for (auto const &mutation : mutationsForAnimation) {
1010
+ if (mutation.type == ShadowViewMutation::Type::Remove) {
1011
+ adjustDelayedMutationIndicesForMutation(surfaceId, mutation);
1012
+ }
1013
+ }
1397
1014
 
1398
- // Find the insert mutation that conflicted with this update
1399
- for (auto &mutation : immediateMutations) {
1400
- if (mutation.newChildShadowView.tag == keyFrame.tag &&
1401
- (mutation.type == ShadowViewMutation::Insert ||
1402
- mutation.type == ShadowViewMutation::Create)) {
1403
- keyFrame.viewPrev = mutation.newChildShadowView;
1404
- keyFrame.viewEnd = mutation.newChildShadowView;
1405
- }
1406
- }
1407
- keyFramesToAnimate.push_back(keyFrame);
1408
- continue;
1409
- }
1410
- }
1015
+ mutations.insert(
1016
+ mutations.end(),
1017
+ mutationsForAnimation.begin(),
1018
+ mutationsForAnimation.end());
1019
+
1020
+ // DEBUG ONLY: list existing inflight animations
1021
+ #ifdef LAYOUT_ANIMATION_VERBOSE_LOGGING
1022
+ LOG(ERROR) << "FINISHING DISPLAYING ONGOING inflightAnimations_!";
1023
+ int i = 0;
1024
+ int j = 0;
1025
+ for (auto const &inflightAnimation : inflightAnimations_) {
1026
+ i++;
1027
+ j = 0;
1028
+ if (inflightAnimation.completed) {
1029
+ continue;
1030
+ }
1031
+ for (auto &keyframe : inflightAnimation.keyFrames) {
1032
+ j++;
1033
+ if (keyframe.invalidated) {
1034
+ continue;
1035
+ }
1036
+ for (auto const &finalMutation : keyframe.finalMutationsForKeyFrame) {
1037
+ if (!finalMutation.mutatedViewIsVirtual()) {
1038
+ std::string msg = "Animation " + std::to_string(i) + " keyframe " +
1039
+ std::to_string(j) + ": Final Animation";
1040
+ PrintMutationInstruction(msg, finalMutation);
1411
1041
  }
1042
+ }
1043
+ }
1044
+ }
1045
+ LOG(ERROR) << "FINISHING DONE DISPLAYING ONGOING inflightAnimations_!";
1046
+ #endif
1412
1047
 
1413
- // If the "final" mutation is already accounted for, by previously
1414
- // setting the correct "viewPrev" of the next conflicting animation, we
1415
- // don't want to queue up any final UPDATE mutations here.
1416
- bool shouldGenerateSyntheticMutations =
1417
- keyFrame.generateFinalSyntheticMutations;
1418
- bool numFinalMutations = keyFrame.finalMutationsForKeyFrame.size();
1419
- bool onlyMutationIsUpdate =
1420
- (numFinalMutations == 1 &&
1421
- keyFrame.finalMutationsForKeyFrame[0].type ==
1422
- ShadowViewMutation::Update);
1423
- if (!shouldGenerateSyntheticMutations &&
1424
- (numFinalMutations == 0 || onlyMutationIsUpdate)) {
1048
+ // Signal to delegate if all animations are complete, or if we were not
1049
+ // animating anything and now some animation exists.
1050
+ if (inflightAnimationsExistInitially && inflightAnimations_.empty()) {
1051
+ std::lock_guard<std::mutex> lock(layoutAnimationStatusDelegateMutex_);
1052
+ if (layoutAnimationStatusDelegate_ != nullptr) {
1053
+ layoutAnimationStatusDelegate_->onAllAnimationsComplete();
1054
+ }
1055
+ } else if (
1056
+ !inflightAnimationsExistInitially && !inflightAnimations_.empty()) {
1057
+ std::lock_guard<std::mutex> lock(layoutAnimationStatusDelegateMutex_);
1058
+ if (layoutAnimationStatusDelegate_ != nullptr) {
1059
+ layoutAnimationStatusDelegate_->onAnimationStarted();
1060
+ }
1061
+ }
1062
+
1063
+ simulateImagePropsMemoryAccess(mutations);
1064
+
1065
+ return MountingTransaction{
1066
+ surfaceId, transactionNumber, std::move(mutations), telemetry};
1067
+ }
1068
+
1069
+ void LayoutAnimationKeyFrameManager::uiManagerDidConfigureNextLayoutAnimation(
1070
+ LayoutAnimation layoutAnimation) const {
1071
+ currentAnimation_ = better::optional<LayoutAnimation>{layoutAnimation};
1072
+ }
1073
+
1074
+ void LayoutAnimationKeyFrameManager::setLayoutAnimationStatusDelegate(
1075
+ LayoutAnimationStatusDelegate *delegate) const {
1076
+ std::lock_guard<std::mutex> lock(layoutAnimationStatusDelegateMutex_);
1077
+ layoutAnimationStatusDelegate_ = delegate;
1078
+ }
1079
+
1080
+ void LayoutAnimationKeyFrameManager::setClockNow(
1081
+ std::function<uint64_t()> now) {
1082
+ now_ = now;
1083
+ }
1084
+
1085
+ void LayoutAnimationKeyFrameManager::enableSkipInvalidatedKeyFrames() {
1086
+ skipInvalidatedKeyFrames_ = true;
1087
+ }
1088
+
1089
+ void LayoutAnimationKeyFrameManager::enableCrashOnMissingComponentDescriptor() {
1090
+ crashOnMissingComponentDescriptor_ = true;
1091
+ }
1092
+
1093
+ void LayoutAnimationKeyFrameManager::enableSimulateImagePropsMemoryAccess() {
1094
+ simulateImagePropsMemoryAccess_ = true;
1095
+ }
1096
+
1097
+ #pragma mark - Protected
1098
+
1099
+ bool LayoutAnimationKeyFrameManager::hasComponentDescriptorForShadowView(
1100
+ ShadowView const &shadowView) const {
1101
+ auto hasComponentDescriptor =
1102
+ componentDescriptorRegistry_->hasComponentDescriptorAt(
1103
+ shadowView.componentHandle);
1104
+
1105
+ if (crashOnMissingComponentDescriptor_ && !hasComponentDescriptor) {
1106
+ LOG(FATAL) << "Component descriptor with handle: "
1107
+ << shadowView.componentHandle
1108
+ << " doesn't exist. The component name: "
1109
+ << shadowView.componentName;
1110
+ }
1111
+
1112
+ return hasComponentDescriptor;
1113
+ }
1114
+
1115
+ ComponentDescriptor const &
1116
+ LayoutAnimationKeyFrameManager::getComponentDescriptorForShadowView(
1117
+ ShadowView const &shadowView) const {
1118
+ return componentDescriptorRegistry_->at(shadowView.componentHandle);
1119
+ }
1120
+
1121
+ ShadowView LayoutAnimationKeyFrameManager::createInterpolatedShadowView(
1122
+ double progress,
1123
+ ShadowView const &startingView,
1124
+ ShadowView const &finalView) const {
1125
+ react_native_assert(startingView.tag > 0);
1126
+ react_native_assert(finalView.tag > 0);
1127
+ if (!hasComponentDescriptorForShadowView(startingView)) {
1128
+ LOG(ERROR) << "No ComponentDescriptor for ShadowView being animated: ["
1129
+ << startingView.tag << "]";
1130
+ react_native_assert(false);
1131
+ return finalView;
1132
+ }
1133
+
1134
+ // Stub PropsParserContext used for interpolateProps.
1135
+ // This is/should be safe because interpolating doesn't actually need to
1136
+ // parse props, and just copies them; therefore there should be no
1137
+ // need to actually use anything in the PropsParserContext.
1138
+ // If this ever changes, the LayoutAnimations API will need to change
1139
+ // to pass in a real PropsParserContext.
1140
+ ContextContainer contextContainer{};
1141
+ PropsParserContext propsParserContext{-1, contextContainer};
1142
+
1143
+ ComponentDescriptor const &componentDescriptor =
1144
+ getComponentDescriptorForShadowView(startingView);
1145
+
1146
+ // Base the mutated view on the finalView, so that the following stay
1147
+ // consistent:
1148
+ // - state
1149
+ // - eventEmitter
1150
+ // For now, we do not allow interpolation of state. And we probably never
1151
+ // will, so make sure we always keep the mounting layer consistent with the
1152
+ // "final" state.
1153
+ auto mutatedShadowView = ShadowView(finalView);
1154
+ react_native_assert(mutatedShadowView.tag > 0);
1155
+
1156
+ react_native_assert(startingView.props != nullptr);
1157
+ react_native_assert(finalView.props != nullptr);
1158
+ if (startingView.props == nullptr || finalView.props == nullptr) {
1159
+ return finalView;
1160
+ }
1161
+
1162
+ // Animate opacity or scale/transform
1163
+ mutatedShadowView.props = componentDescriptor.interpolateProps(
1164
+ propsParserContext, progress, startingView.props, finalView.props);
1165
+ react_native_assert(mutatedShadowView.props != nullptr);
1166
+ if (mutatedShadowView.props == nullptr) {
1167
+ return finalView;
1168
+ }
1169
+
1170
+ // Interpolate LayoutMetrics
1171
+ LayoutMetrics const &finalLayoutMetrics = finalView.layoutMetrics;
1172
+ LayoutMetrics const &baselineLayoutMetrics = startingView.layoutMetrics;
1173
+ LayoutMetrics interpolatedLayoutMetrics = finalLayoutMetrics;
1174
+ interpolatedLayoutMetrics.frame.origin.x = interpolateFloats(
1175
+ progress,
1176
+ baselineLayoutMetrics.frame.origin.x,
1177
+ finalLayoutMetrics.frame.origin.x);
1178
+ interpolatedLayoutMetrics.frame.origin.y = interpolateFloats(
1179
+ progress,
1180
+ baselineLayoutMetrics.frame.origin.y,
1181
+ finalLayoutMetrics.frame.origin.y);
1182
+ interpolatedLayoutMetrics.frame.size.width = interpolateFloats(
1183
+ progress,
1184
+ baselineLayoutMetrics.frame.size.width,
1185
+ finalLayoutMetrics.frame.size.width);
1186
+ interpolatedLayoutMetrics.frame.size.height = interpolateFloats(
1187
+ progress,
1188
+ baselineLayoutMetrics.frame.size.height,
1189
+ finalLayoutMetrics.frame.size.height);
1190
+ mutatedShadowView.layoutMetrics = interpolatedLayoutMetrics;
1191
+
1192
+ return mutatedShadowView;
1193
+ }
1194
+
1195
+ void LayoutAnimationKeyFrameManager::callCallback(
1196
+ LayoutAnimationCallbackWrapper const &callback) const {
1197
+ runtimeExecutor_(
1198
+ [callback](jsi::Runtime &runtime) { callback.call(runtime); });
1199
+ }
1200
+
1201
+ void LayoutAnimationKeyFrameManager::queueFinalMutationsForCompletedKeyFrame(
1202
+ AnimationKeyFrame const &keyframe,
1203
+ ShadowViewMutation::List &mutationsList,
1204
+ bool interrupted,
1205
+ std::string logPrefix) const {
1206
+ if (skipInvalidatedKeyFrames_ && keyframe.invalidated) {
1207
+ return;
1208
+ }
1209
+ if (keyframe.finalMutationsForKeyFrame.size() > 0) {
1210
+ // TODO: modularize this segment, it is repeated 2x in KeyFrameManager
1211
+ // as well.
1212
+ ShadowView prev = keyframe.viewPrev;
1213
+ for (auto const &finalMutation : keyframe.finalMutationsForKeyFrame) {
1214
+ PrintMutationInstruction(
1215
+ logPrefix + ": Queuing up Final Mutation:", finalMutation);
1216
+ // Copy so that if something else mutates the inflight animations,
1217
+ // it won't change this mutation after this point.
1218
+ switch (finalMutation.type) {
1219
+ // For CREATE/INSERT this will contain CREATE, INSERT in that order.
1220
+ // For REMOVE/DELETE, same.
1221
+ case ShadowViewMutation::Type::Create:
1222
+ mutationsList.push_back(ShadowViewMutation::CreateMutation(
1223
+ finalMutation.newChildShadowView));
1224
+ break;
1225
+ case ShadowViewMutation::Type::Delete:
1226
+ mutationsList.push_back(ShadowViewMutation::DeleteMutation(prev));
1227
+ break;
1228
+ case ShadowViewMutation::Type::Insert:
1229
+ mutationsList.push_back(ShadowViewMutation::InsertMutation(
1230
+ finalMutation.parentShadowView,
1231
+ finalMutation.newChildShadowView,
1232
+ finalMutation.index));
1233
+ break;
1234
+ case ShadowViewMutation::Type::Remove:
1235
+ mutationsList.push_back(ShadowViewMutation::RemoveMutation(
1236
+ finalMutation.parentShadowView, prev, finalMutation.index));
1237
+ break;
1238
+ case ShadowViewMutation::Type::Update:
1239
+ mutationsList.push_back(ShadowViewMutation::UpdateMutation(
1240
+ prev, finalMutation.newChildShadowView));
1241
+ break;
1242
+ }
1243
+ if (finalMutation.newChildShadowView.tag > 0) {
1244
+ prev = finalMutation.newChildShadowView;
1245
+ }
1246
+ }
1247
+ } else {
1248
+ // If there's no final mutation associated, create a mutation that
1249
+ // corresponds to the animation being 100% complete. This is
1250
+ // important for, for example, INSERT mutations being animated from
1251
+ // opacity 0 to 1. If the animation is interrupted we must force the
1252
+ // View to be at opacity 1. For Android - since it passes along only
1253
+ // deltas, not an entire bag of props - generate an "animation"
1254
+ // frame corresponding to a final update for this view. Only then,
1255
+ // generate an update that will cause the ShadowTree to be
1256
+ // consistent with the Mounting layer by passing viewEnd,
1257
+ // unmodified, to the mounting layer. This helps with, for example,
1258
+ // opacity animations.
1259
+ // This is necessary for INSERT (create) and UPDATE (update) mutations, but
1260
+ // not REMOVE/DELETE mutations ("delete" animations).
1261
+ if (interrupted) {
1262
+ auto mutatedShadowView =
1263
+ createInterpolatedShadowView(1, keyframe.viewStart, keyframe.viewEnd);
1264
+ auto generatedPenultimateMutation = ShadowViewMutation::UpdateMutation(
1265
+ keyframe.viewPrev, mutatedShadowView);
1266
+ react_native_assert(
1267
+ generatedPenultimateMutation.oldChildShadowView.tag > 0);
1268
+ react_native_assert(
1269
+ generatedPenultimateMutation.newChildShadowView.tag > 0);
1270
+ PrintMutationInstruction(
1271
+ "Queueing up penultimate mutation instruction - synthetic",
1272
+ generatedPenultimateMutation);
1273
+ mutationsList.push_back(generatedPenultimateMutation);
1274
+
1275
+ auto generatedMutation = ShadowViewMutation::UpdateMutation(
1276
+ mutatedShadowView, keyframe.viewEnd);
1277
+ react_native_assert(generatedMutation.oldChildShadowView.tag > 0);
1278
+ react_native_assert(generatedMutation.newChildShadowView.tag > 0);
1279
+ PrintMutationInstruction(
1280
+ "Queueing up final mutation instruction - synthetic",
1281
+ generatedMutation);
1282
+ mutationsList.push_back(generatedMutation);
1283
+ } else {
1284
+ auto mutation = ShadowViewMutation::UpdateMutation(
1285
+ keyframe.viewPrev, keyframe.viewEnd);
1286
+ PrintMutationInstruction(
1287
+ logPrefix +
1288
+ "Animation Complete: Queuing up Final Synthetic Mutation:",
1289
+ mutation);
1290
+ react_native_assert(mutation.oldChildShadowView.tag > 0);
1291
+ react_native_assert(mutation.newChildShadowView.tag > 0);
1292
+ mutationsList.push_back(std::move(mutation));
1293
+ }
1294
+ }
1295
+ }
1296
+
1297
+ #pragma mark - Private
1298
+
1299
+ void LayoutAnimationKeyFrameManager::
1300
+ adjustImmediateMutationIndicesForDelayedMutations(
1301
+ SurfaceId surfaceId,
1302
+ ShadowViewMutation &mutation,
1303
+ bool skipLastAnimation,
1304
+ bool lastAnimationOnly) const {
1305
+ bool isRemoveMutation = mutation.type == ShadowViewMutation::Type::Remove;
1306
+ react_native_assert(
1307
+ isRemoveMutation || mutation.type == ShadowViewMutation::Type::Insert);
1308
+
1309
+ // TODO: turn all of this into a lambda and share code?
1310
+ if (mutation.mutatedViewIsVirtual()) {
1311
+ PrintMutationInstruction(
1312
+ "[IndexAdjustment] Not calling adjustImmediateMutationIndicesForDelayedMutations, is virtual, for:",
1313
+ mutation);
1314
+ return;
1315
+ }
1316
+
1317
+ PrintMutationInstruction(
1318
+ "[IndexAdjustment] Calling adjustImmediateMutationIndicesForDelayedMutations for:",
1319
+ mutation);
1320
+
1321
+ // First, collect all final mutations that could impact this immediate
1322
+ // mutation.
1323
+ std::vector<ShadowViewMutation *> candidateMutations{};
1324
+
1325
+ for (auto inflightAnimationIt =
1326
+ inflightAnimations_.rbegin() + (skipLastAnimation ? 1 : 0);
1327
+ inflightAnimationIt != inflightAnimations_.rend();
1328
+ inflightAnimationIt++) {
1329
+ auto &inflightAnimation = *inflightAnimationIt;
1330
+ if (inflightAnimation.surfaceId != surfaceId) {
1331
+ continue;
1332
+ }
1333
+ if (inflightAnimation.completed) {
1334
+ continue;
1335
+ }
1336
+
1337
+ for (auto it = inflightAnimation.keyFrames.begin();
1338
+ it != inflightAnimation.keyFrames.end();
1339
+ it++) {
1340
+ auto &animatedKeyFrame = *it;
1341
+
1342
+ if (animatedKeyFrame.invalidated) {
1343
+ continue;
1344
+ }
1345
+
1346
+ // Detect if they're in the same view hierarchy, but not equivalent
1347
+ // We've already detected direct conflicts and removed them.
1348
+ if (animatedKeyFrame.parentView.tag != mutation.parentShadowView.tag) {
1349
+ continue;
1350
+ }
1351
+
1352
+ for (auto &delayedMutation : animatedKeyFrame.finalMutationsForKeyFrame) {
1353
+ if (delayedMutation.type != ShadowViewMutation::Type::Remove) {
1354
+ continue;
1355
+ }
1356
+ if (delayedMutation.mutatedViewIsVirtual()) {
1357
+ continue;
1358
+ }
1359
+ if (delayedMutation.oldChildShadowView.tag ==
1360
+ (isRemoveMutation ? mutation.oldChildShadowView.tag
1361
+ : mutation.newChildShadowView.tag)) {
1425
1362
  continue;
1426
1363
  }
1427
1364
 
1428
- queueFinalMutationsForCompletedKeyFrame(
1429
- keyFrame,
1430
- finalConflictingMutations,
1431
- true,
1432
- "KeyFrameManager: Finished Conflicting Keyframe");
1365
+ PrintMutationInstructionRelative(
1366
+ "[IndexAdjustment] adjustImmediateMutationIndicesForDelayedMutations CANDIDATE for:",
1367
+ mutation,
1368
+ delayedMutation);
1369
+ candidateMutations.push_back(&delayedMutation);
1433
1370
  }
1371
+ }
1434
1372
 
1435
- // Make sure that all operations execute in the proper order, since
1436
- // conflicting animations are not sorted in any reasonable way.
1437
- std::stable_sort(
1438
- finalConflictingMutations.begin(),
1439
- finalConflictingMutations.end(),
1440
- &shouldFirstComeBeforeSecondMutation);
1373
+ if (lastAnimationOnly) {
1374
+ break;
1375
+ }
1376
+ }
1441
1377
 
1442
- std::stable_sort(
1443
- immediateMutations.begin(),
1444
- immediateMutations.end(),
1445
- &shouldFirstComeBeforeSecondRemovesOnly);
1378
+ // While the mutation keeps being affected, keep checking. We use the vector
1379
+ // so we only perform one adjustment per delayed mutation. See comments at
1380
+ // bottom of adjustDelayedMutationIndicesForMutation for further explanation.
1381
+ bool changed = true;
1382
+ int adjustedDelta = 0;
1383
+ while (changed) {
1384
+ changed = false;
1385
+ candidateMutations.erase(
1386
+ std::remove_if(
1387
+ candidateMutations.begin(),
1388
+ candidateMutations.end(),
1389
+ [&changed, &mutation, &adjustedDelta, &isRemoveMutation](
1390
+ ShadowViewMutation *candidateMutation) {
1391
+ bool indexConflicts =
1392
+ (candidateMutation->index < mutation.index ||
1393
+ (isRemoveMutation &&
1394
+ candidateMutation->index == mutation.index));
1395
+ if (indexConflicts) {
1396
+ mutation.index++;
1397
+ adjustedDelta++;
1398
+ changed = true;
1399
+ PrintMutationInstructionRelative(
1400
+ "[IndexAdjustment] adjustImmediateMutationIndicesForDelayedMutations: Adjusting mutation UPWARD",
1401
+ mutation,
1402
+ *candidateMutation);
1403
+ return true;
1404
+ }
1405
+ return false;
1406
+ }),
1407
+ candidateMutations.end());
1408
+ }
1409
+ }
1446
1410
 
1447
- animation.keyFrames = keyFramesToAnimate;
1448
- inflightAnimations_.push_back(std::move(animation));
1411
+ void LayoutAnimationKeyFrameManager::adjustDelayedMutationIndicesForMutation(
1412
+ SurfaceId surfaceId,
1413
+ ShadowViewMutation const &mutation,
1414
+ bool skipLastAnimation) const {
1415
+ bool isRemoveMutation = mutation.type == ShadowViewMutation::Type::Remove;
1416
+ bool isInsertMutation = mutation.type == ShadowViewMutation::Type::Insert;
1417
+ auto tag = isRemoveMutation ? mutation.oldChildShadowView.tag
1418
+ : mutation.newChildShadowView.tag;
1419
+ react_native_assert(isRemoveMutation || isInsertMutation);
1449
1420
 
1450
- // At this point, we have the following information and knowledge graph:
1451
- // Knowledge Graph:
1452
- // [ImmediateMutations] -> assumes [FinalConflicting], [FrameDelayed],
1453
- // [Delayed] already executed [FrameDelayed] -> assumes
1454
- // [FinalConflicting], [Delayed] already executed [FinalConflicting] ->
1455
- // is adjusted based on [Delayed], no dependency on [FinalConflicting],
1456
- // [FrameDelayed] [Delayed] -> assumes [FinalConflicting],
1457
- // [ImmediateMutations] not executed yet
1421
+ if (mutation.mutatedViewIsVirtual()) {
1422
+ PrintMutationInstruction(
1423
+ "[IndexAdjustment] Not calling adjustDelayedMutationIndicesForMutation, is virtual, for:",
1424
+ mutation);
1425
+ return;
1426
+ }
1458
1427
 
1459
- // Adjust [Delayed] based on [FinalConflicting]
1460
- // Knowledge Graph:
1461
- // [ImmediateMutations] -> assumes [FinalConflicting], [FrameDelayed],
1462
- // [Delayed] already executed [FrameDelayed] -> assumes
1463
- // [FinalConflicting], [Delayed] already executed [FinalConflicting] ->
1464
- // is adjusted based on [Delayed], no dependency on [FinalConflicting],
1465
- // [FrameDelayed] [Delayed] -> adjusted for [FinalConflicting]; assumes
1466
- // [ImmediateMutations] not executed yet
1467
- #ifdef LAYOUT_ANIMATION_VERBOSE_LOGGING
1468
- LOG(ERROR) << "Adjust [Delayed] based on [FinalConflicting]";
1469
- #endif
1470
- for (auto &mutation : finalConflictingMutations) {
1471
- if (mutation.type == ShadowViewMutation::Type::Insert ||
1472
- mutation.type == ShadowViewMutation::Type::Remove) {
1473
- adjustDelayedMutationIndicesForMutation(surfaceId, mutation, true);
1474
- }
1428
+ // First, collect all final mutations that could impact this immediate
1429
+ // mutation.
1430
+ std::vector<ShadowViewMutation *> candidateMutations{};
1431
+
1432
+ for (auto inflightAnimationIt =
1433
+ inflightAnimations_.rbegin() + (skipLastAnimation ? 1 : 0);
1434
+ inflightAnimationIt != inflightAnimations_.rend();
1435
+ inflightAnimationIt++) {
1436
+ auto &inflightAnimation = *inflightAnimationIt;
1437
+
1438
+ if (inflightAnimation.surfaceId != surfaceId) {
1439
+ continue;
1440
+ }
1441
+ if (inflightAnimation.completed) {
1442
+ continue;
1443
+ }
1444
+
1445
+ for (auto it = inflightAnimation.keyFrames.begin();
1446
+ it != inflightAnimation.keyFrames.end();
1447
+ it++) {
1448
+ auto &animatedKeyFrame = *it;
1449
+
1450
+ if (animatedKeyFrame.invalidated) {
1451
+ continue;
1452
+ }
1453
+
1454
+ // Detect if they're in the same view hierarchy, but not equivalent
1455
+ // (We've already detected direct conflicts and handled them above)
1456
+ if (animatedKeyFrame.parentView.tag != mutation.parentShadowView.tag) {
1457
+ continue;
1475
1458
  }
1476
1459
 
1477
- // Adjust [FrameDelayed] based on [Delayed]
1478
- // Knowledge Graph:
1479
- // [ImmediateExecutions] -> assumes [FinalConflicting], [Delayed],
1480
- // [FrameDelayed] already executed [FrameDelayed] -> adjusted for
1481
- // [Delayed]; assumes [FinalConflicting] already executed
1482
- // [FinalConflicting] -> is adjusted based on [Delayed], no dependency
1483
- // on [FinalConflicting], [FrameDelayed] [Delayed] -> adjusted for
1484
- // [FinalConflicting]; assumes [ImmediateExecutions] not executed yet
1485
- #ifdef LAYOUT_ANIMATION_VERBOSE_LOGGING
1486
- LOG(ERROR) << "Adjust [FrameDelayed] based on [Delayed]";
1487
- #endif
1488
- for (auto &keyframe : inflightAnimations_.back().keyFrames) {
1489
- for (auto &finalMutation : keyframe.finalMutationsForKeyFrame) {
1490
- if (finalMutation.type == ShadowViewMutation::Type::Insert ||
1491
- finalMutation.type == ShadowViewMutation::Type::Remove) {
1492
- // When adjusting, skip adjusting against last animation - because
1493
- // all `mutation`s here come from the last animation, so we can't
1494
- // adjust a batch against itself.
1495
- adjustImmediateMutationIndicesForDelayedMutations(
1496
- surfaceId, finalMutation, true);
1497
- }
1460
+ for (auto &finalAnimationMutation :
1461
+ animatedKeyFrame.finalMutationsForKeyFrame) {
1462
+ if (finalAnimationMutation.oldChildShadowView.tag == tag) {
1463
+ continue;
1498
1464
  }
1499
- }
1500
1465
 
1501
- // Adjust [ImmediateExecutions] based on [Delayed]
1502
- // Knowledge Graph:
1503
- // [ImmediateExecutions] -> adjusted for [FrameDelayed], [Delayed];
1504
- // assumes [FinalConflicting] already executed [FrameDelayed] ->
1505
- // adjusted for [Delayed]; assumes [FinalConflicting] already executed
1506
- // [FinalConflicting] -> is adjusted based on [Delayed], no dependency
1507
- // on [FinalConflicting], [FrameDelayed] [Delayed] -> adjusted for
1508
- // [FinalConflicting]; assumes [ImmediateExecutions] not executed yet
1509
- //
1510
- // THEN,
1511
- // Adjust [Delayed] based on [ImmediateExecutions] and
1512
- // [FinalConflicting] Knowledge Graph: [ImmediateExecutions] -> adjusted
1513
- // for [FrameDelayed], [Delayed]; assumes [FinalConflicting] already
1514
- // executed [FrameDelayed] -> adjusted for [Delayed]; assumes
1515
- // [FinalConflicting] already executed [FinalConflicting] -> is adjusted
1516
- // based on [Delayed], no dependency on [FinalConflicting],
1517
- // [FrameDelayed] [Delayed] -> adjusted for [FinalConflicting],
1518
- // [ImmediateExecutions]
1519
- //
1520
- // We do these in the same loop because each immediate execution is
1521
- // impacted by each delayed mutation, and also can impact each delayed
1522
- // mutation, and these effects compound.
1523
- #ifdef LAYOUT_ANIMATION_VERBOSE_LOGGING
1524
- LOG(ERROR)
1525
- << "Adjust each [ImmediateExecution] based on [Delayed] and [Delayed] based on each [ImmediateExecution]";
1526
- #endif
1527
- for (auto &mutation : immediateMutations) {
1528
- // Note: when adjusting [ImmediateExecutions] based on [FrameDelayed],
1529
- // we need only adjust Inserts. Since inserts are executed
1530
- // highest-index-first, lower indices being delayed does not impact
1531
- // the higher-index removals; and conversely, higher indices being
1532
- // delayed cannot impact lower index removal, regardless of order.
1533
- if (mutation.type == ShadowViewMutation::Type::Insert ||
1534
- mutation.type == ShadowViewMutation::Type::Remove) {
1535
- adjustImmediateMutationIndicesForDelayedMutations(
1536
- surfaceId,
1537
- mutation,
1538
- mutation.type == ShadowViewMutation::Type::Remove);
1539
- // Here we need to adjust both Delayed and FrameDelayed mutations.
1540
- // Delayed Removes can be impacted by non-delayed Inserts from the
1541
- // same frame.
1542
- adjustDelayedMutationIndicesForMutation(surfaceId, mutation);
1466
+ if (finalAnimationMutation.type != ShadowViewMutation::Type::Remove) {
1467
+ continue;
1468
+ }
1469
+ if (finalAnimationMutation.mutatedViewIsVirtual()) {
1470
+ continue;
1543
1471
  }
1544
- }
1545
1472
 
1546
- // If the knowledge graph progression above is correct, it is now safe
1547
- // to execute finalConflictingMutations and immediateMutations in that
1548
- // order, and to queue the delayed animations from this frame.
1549
- //
1550
- // Execute the conflicting, delayed operations immediately. Any UPDATE
1551
- // operations that smoothly transition into another animation will be
1552
- // overridden by generated UPDATE operations at the end of the list, and
1553
- // we want any REMOVE or DELETE operations to execute immediately.
1554
- // Additionally, this should allow us to avoid performing index
1555
- // adjustment between this list of conflicting animations and the batch
1556
- // we're about to execute.
1557
- finalConflictingMutations.insert(
1558
- finalConflictingMutations.end(),
1559
- immediateMutations.begin(),
1560
- immediateMutations.end());
1561
- mutations = finalConflictingMutations;
1562
- } /* if (currentAnimation) */
1563
- else {
1564
- // If there's no "next" animation, make sure we queue up "final"
1565
- // operations from all ongoing, conflicting animations.
1566
- #ifdef LAYOUT_ANIMATION_VERBOSE_LOGGING
1567
- LOG(ERROR) << "No Animation: Queue up final conflicting animations";
1568
- #endif
1569
- ShadowViewMutationList finalMutationsForConflictingAnimations{};
1570
- for (auto const &keyFrame : conflictingAnimations) {
1571
- queueFinalMutationsForCompletedKeyFrame(
1572
- keyFrame,
1573
- finalMutationsForConflictingAnimations,
1574
- true,
1575
- "Conflict with non-animated mutation");
1473
+ PrintMutationInstructionRelative(
1474
+ "[IndexAdjustment] adjustDelayedMutationIndicesForMutation: CANDIDATE:",
1475
+ mutation,
1476
+ finalAnimationMutation);
1477
+ candidateMutations.push_back(&finalAnimationMutation);
1576
1478
  }
1479
+ }
1480
+ }
1577
1481
 
1578
- // Make sure that all operations execute in the proper order.
1579
- // REMOVE operations with highest indices must operate first.
1580
- std::stable_sort(
1581
- finalMutationsForConflictingAnimations.begin(),
1582
- finalMutationsForConflictingAnimations.end(),
1583
- &shouldFirstComeBeforeSecondMutation);
1482
+ // Because the finalAnimations are not sorted in any way, it is possible to
1483
+ // have some sequence like:
1484
+ // * DELAYED REMOVE 10 from {TAG}
1485
+ // * DELAYED REMOVE 9 from {TAG}
1486
+ // * ...
1487
+ // * DELAYED REMOVE 5 from {TAG}
1488
+ // with mutation: INSERT 6/REMOVE 6. This would cause the first few mutations
1489
+ // to *not* be adjusted, even though they would be impacted by mutation or
1490
+ // vice-versa after later adjustments are applied. Therefore, we just keep
1491
+ // recursing while there are any changes. This isn't great, but is good enough
1492
+ // for now until we change these data-structures.
1493
+ bool changed = true;
1494
+ while (changed) {
1495
+ changed = false;
1496
+ candidateMutations.erase(
1497
+ std::remove_if(
1498
+ candidateMutations.begin(),
1499
+ candidateMutations.end(),
1500
+ [&mutation, &isRemoveMutation, &isInsertMutation, &changed](
1501
+ ShadowViewMutation *candidateMutation) {
1502
+ if (isRemoveMutation &&
1503
+ mutation.index <= candidateMutation->index) {
1504
+ candidateMutation->index--;
1505
+ changed = true;
1506
+ PrintMutationInstructionRelative(
1507
+ "[IndexAdjustment] adjustDelayedMutationIndicesForMutation: Adjusting mutation DOWNWARD",
1508
+ mutation,
1509
+ *candidateMutation);
1510
+ return true;
1511
+ } else if (
1512
+ isInsertMutation &&
1513
+ mutation.index <= candidateMutation->index) {
1514
+ candidateMutation->index++;
1515
+ changed = true;
1516
+ PrintMutationInstructionRelative(
1517
+ "[IndexAdjustment] adjustDelayedMutationIndicesForMutation: Adjusting mutation UPWARD",
1518
+ mutation,
1519
+ *candidateMutation);
1520
+ return true;
1521
+ }
1522
+ return false;
1523
+ }),
1524
+ candidateMutations.end());
1525
+ }
1526
+ }
1584
1527
 
1585
- #ifdef LAYOUT_ANIMATION_VERBOSE_LOGGING
1586
- LOG(ERROR)
1587
- << "No Animation: Adjust delayed mutations based on all finalMutationsForConflictingAnimations";
1588
- #endif
1589
- for (auto const &mutation : finalMutationsForConflictingAnimations) {
1590
- if (mutation.type == ShadowViewMutation::Type::Remove ||
1591
- mutation.type == ShadowViewMutation::Type::Insert) {
1592
- adjustDelayedMutationIndicesForMutation(surfaceId, mutation);
1593
- }
1528
+ void LayoutAnimationKeyFrameManager::getAndEraseConflictingAnimations(
1529
+ SurfaceId surfaceId,
1530
+ ShadowViewMutationList const &mutations,
1531
+ std::vector<AnimationKeyFrame> &conflictingAnimations) const {
1532
+ ShadowViewMutationList localConflictingMutations{};
1533
+ for (auto const &mutation : mutations) {
1534
+ bool mutationIsCreateOrDelete =
1535
+ mutation.type == ShadowViewMutation::Type::Create ||
1536
+ mutation.type == ShadowViewMutation::Type::Delete;
1537
+ auto const &baselineShadowView =
1538
+ (mutation.type == ShadowViewMutation::Type::Insert ||
1539
+ mutation.type == ShadowViewMutation::Type::Create)
1540
+ ? mutation.newChildShadowView
1541
+ : mutation.oldChildShadowView;
1542
+ auto baselineTag = baselineShadowView.tag;
1543
+
1544
+ for (auto &inflightAnimation : inflightAnimations_) {
1545
+ if (inflightAnimation.surfaceId != surfaceId) {
1546
+ continue;
1547
+ }
1548
+ if (inflightAnimation.completed) {
1549
+ continue;
1594
1550
  }
1595
1551
 
1596
- // The ShadowTree layer doesn't realize that certain operations have
1597
- // been delayed, so we must adjust all Remove and Insert operations
1598
- // based on what else has been deferred, whether we are executing this
1599
- // immediately or later.
1600
- #ifdef LAYOUT_ANIMATION_VERBOSE_LOGGING
1601
- LOG(ERROR)
1602
- << "No Animation: Adjust mutations based on remaining delayed mutations / adjust delayed, based on each";
1603
- #endif
1604
- for (auto &mutation : mutations) {
1605
- if (mutation.type == ShadowViewMutation::Type::Remove ||
1606
- mutation.type == ShadowViewMutation::Type::Insert) {
1607
- adjustImmediateMutationIndicesForDelayedMutations(
1608
- surfaceId, mutation);
1609
- adjustDelayedMutationIndicesForMutation(surfaceId, mutation);
1552
+ for (auto it = inflightAnimation.keyFrames.begin();
1553
+ it != inflightAnimation.keyFrames.end();) {
1554
+ auto &animatedKeyFrame = *it;
1555
+
1556
+ if (animatedKeyFrame.invalidated) {
1557
+ continue;
1610
1558
  }
1611
- }
1612
1559
 
1613
- // Append mutations to this list and swap - so that the final
1614
- // conflicting mutations happen before any other mutations
1615
- finalMutationsForConflictingAnimations.insert(
1616
- finalMutationsForConflictingAnimations.end(),
1617
- mutations.begin(),
1618
- mutations.end());
1619
- mutations = finalMutationsForConflictingAnimations;
1620
- }
1621
- } // if (mutations)
1560
+ // A conflict is when either: the animated node itself is mutated
1561
+ // directly; or, the parent of the node is created or deleted. In cases
1562
+ // of reparenting - say, the parent is deleted but the node was moved to
1563
+ // a different parent first - the reparenting (remove/insert) conflict
1564
+ // will be detected before we process the parent DELETE.
1565
+ // Parent deletion is important because deleting a parent recursively
1566
+ // deletes all children. If we previously deferred deletion of a child,
1567
+ // we need to force deletion/removal to happen immediately.
1568
+ bool conflicting = animatedKeyFrame.tag == baselineTag ||
1569
+ (mutationIsCreateOrDelete &&
1570
+ animatedKeyFrame.parentView.tag == baselineTag &&
1571
+ animatedKeyFrame.parentView.tag != 0);
1622
1572
 
1623
- // We never commit a different root or modify anything -
1624
- // we just send additional mutations to the mounting layer until the
1625
- // animations are finished and the mounting layer (view) represents exactly
1626
- // what is in the most recent shadow tree
1627
- // Add animation mutations to the end of our existing mutations list in this
1628
- // function.
1629
- ShadowViewMutationList mutationsForAnimation{};
1630
- animationMutationsForFrame(surfaceId, mutationsForAnimation, now);
1573
+ // Conflicting animation detected: if we're mutating a tag under
1574
+ // animation, or deleting the parent of a tag under animation, or
1575
+ // reparenting.
1576
+ if (conflicting) {
1577
+ animatedKeyFrame.invalidated = true;
1578
+
1579
+ // We construct a list of all conflicting animations, whether or not
1580
+ // they have a "final mutation" to execute. This is important with,
1581
+ // for example, "insert" mutations where the final update needs to set
1582
+ // opacity to "1", even if there's no final ShadowNode update.
1583
+ // TODO: don't animate virtual views in the first place?
1584
+ bool isVirtual = false;
1585
+ for (const auto &finalMutationForKeyFrame :
1586
+ animatedKeyFrame.finalMutationsForKeyFrame) {
1587
+ isVirtual =
1588
+ isVirtual || finalMutationForKeyFrame.mutatedViewIsVirtual();
1631
1589
 
1632
- // If any delayed removes were executed, update remaining delayed keyframes
1633
1590
  #ifdef LAYOUT_ANIMATION_VERBOSE_LOGGING
1634
- LOG(ERROR)
1635
- << "Adjust all delayed mutations based on final mutations generated by animation driver";
1591
+ PrintMutationInstructionRelative(
1592
+ "Found mutation that conflicts with existing in-flight animation:",
1593
+ mutation,
1594
+ finalMutationForKeyFrame);
1636
1595
  #endif
1637
- for (auto const &mutation : mutationsForAnimation) {
1638
- if (mutation.type == ShadowViewMutation::Type::Remove) {
1639
- adjustDelayedMutationIndicesForMutation(surfaceId, mutation);
1640
- }
1641
- }
1596
+ }
1642
1597
 
1643
- mutations.insert(
1644
- mutations.end(),
1645
- mutationsForAnimation.begin(),
1646
- mutationsForAnimation.end());
1598
+ conflictingAnimations.push_back(animatedKeyFrame);
1599
+ for (const auto &finalMutationForKeyFrame :
1600
+ animatedKeyFrame.finalMutationsForKeyFrame) {
1601
+ if (!isVirtual ||
1602
+ finalMutationForKeyFrame.type ==
1603
+ ShadowViewMutation::Type::Delete) {
1604
+ localConflictingMutations.push_back(finalMutationForKeyFrame);
1605
+ }
1606
+ }
1647
1607
 
1648
- // DEBUG ONLY: list existing inflight animations
1649
- #ifdef LAYOUT_ANIMATION_VERBOSE_LOGGING
1650
- LOG(ERROR) << "FINISHING DISPLAYING ONGOING inflightAnimations_!";
1651
- int i = 0;
1652
- int j = 0;
1653
- for (auto const &inflightAnimation : inflightAnimations_) {
1654
- i++;
1655
- j = 0;
1656
- if (inflightAnimation.completed) {
1657
- continue;
1658
- }
1659
- for (auto &keyframe : inflightAnimation.keyFrames) {
1660
- j++;
1661
- if (keyframe.invalidated) {
1662
- continue;
1663
- }
1664
- for (auto const &finalMutation : keyframe.finalMutationsForKeyFrame) {
1665
- if (!finalMutation.mutatedViewIsVirtual()) {
1666
- std::string msg = "Animation " + std::to_string(i) + " keyframe " +
1667
- std::to_string(j) + ": Final Animation";
1668
- PrintMutationInstruction(msg, finalMutation);
1608
+ // Delete from existing animation
1609
+ it = inflightAnimation.keyFrames.erase(it);
1610
+ } else {
1611
+ it++;
1669
1612
  }
1670
1613
  }
1671
1614
  }
1672
1615
  }
1673
- LOG(ERROR) << "FINISHING DONE DISPLAYING ONGOING inflightAnimations_!";
1674
- #endif
1675
1616
 
1676
- // Signal to delegate if all animations are complete, or if we were not
1677
- // animating anything and now some animation exists.
1678
- if (inflightAnimationsExistInitially && inflightAnimations_.empty()) {
1679
- std::lock_guard<std::mutex> lock(layoutAnimationStatusDelegateMutex_);
1680
- if (layoutAnimationStatusDelegate_ != nullptr) {
1681
- layoutAnimationStatusDelegate_->onAllAnimationsComplete();
1682
- }
1683
- } else if (
1684
- !inflightAnimationsExistInitially && !inflightAnimations_.empty()) {
1685
- std::lock_guard<std::mutex> lock(layoutAnimationStatusDelegateMutex_);
1686
- if (layoutAnimationStatusDelegate_ != nullptr) {
1687
- layoutAnimationStatusDelegate_->onAnimationStarted();
1688
- }
1617
+ // Recurse, in case conflicting mutations conflict with other existing
1618
+ // animations
1619
+ if (!localConflictingMutations.empty()) {
1620
+ getAndEraseConflictingAnimations(
1621
+ surfaceId, localConflictingMutations, conflictingAnimations);
1689
1622
  }
1690
-
1691
- return MountingTransaction{
1692
- surfaceId, transactionNumber, std::move(mutations), telemetry};
1693
- }
1694
-
1695
- bool LayoutAnimationKeyFrameManager::hasComponentDescriptorForShadowView(
1696
- ShadowView const &shadowView) const {
1697
- return componentDescriptorRegistry_->hasComponentDescriptorAt(
1698
- shadowView.componentHandle);
1699
- }
1700
-
1701
- ComponentDescriptor const &
1702
- LayoutAnimationKeyFrameManager::getComponentDescriptorForShadowView(
1703
- ShadowView const &shadowView) const {
1704
- return componentDescriptorRegistry_->at(shadowView.componentHandle);
1705
1623
  }
1706
1624
 
1707
- void LayoutAnimationKeyFrameManager::setComponentDescriptorRegistry(
1708
- const SharedComponentDescriptorRegistry &componentDescriptorRegistry) {
1709
- componentDescriptorRegistry_ = componentDescriptorRegistry;
1710
- }
1711
-
1712
- /**
1713
- * Given a `progress` between 0 and 1, a mutation and LayoutAnimation config,
1714
- * return a ShadowView with mutated props and/or LayoutMetrics.
1715
- *
1716
- * @param progress
1717
- * @param layoutAnimation
1718
- * @param animatedMutation
1719
- * @return
1720
- */
1721
- ShadowView LayoutAnimationKeyFrameManager::createInterpolatedShadowView(
1722
- double progress,
1723
- ShadowView startingView,
1724
- ShadowView finalView) const {
1725
- react_native_assert(startingView.tag > 0);
1726
- react_native_assert(finalView.tag > 0);
1727
- if (!hasComponentDescriptorForShadowView(startingView)) {
1728
- LOG(ERROR) << "No ComponentDescriptor for ShadowView being animated: ["
1729
- << startingView.tag << "]";
1730
- react_native_assert(false);
1731
- return finalView;
1732
- }
1733
-
1734
- // Stub PropsParserContext used for interpolateProps.
1735
- // This is/should be safe because interpolating doesn't actually need to
1736
- // parse props, and just copies them; therefore there should be no
1737
- // need to actually use anything in the PropsParserContext.
1738
- // If this ever changes, the LayoutAnimations API will need to change
1739
- // to pass in a real PropsParserContext.
1740
- ContextContainer contextContainer{};
1741
- PropsParserContext propsParserContext{-1, contextContainer};
1742
-
1743
- ComponentDescriptor const &componentDescriptor =
1744
- getComponentDescriptorForShadowView(startingView);
1745
-
1746
- // Base the mutated view on the finalView, so that the following stay
1747
- // consistent:
1748
- // - state
1749
- // - eventEmitter
1750
- // For now, we do not allow interpolation of state. And we probably never
1751
- // will, so make sure we always keep the mounting layer consistent with the
1752
- // "final" state.
1753
- auto mutatedShadowView = ShadowView(finalView);
1754
- react_native_assert(mutatedShadowView.tag > 0);
1755
-
1756
- react_native_assert(startingView.props != nullptr);
1757
- react_native_assert(finalView.props != nullptr);
1758
- if (startingView.props == nullptr || finalView.props == nullptr) {
1759
- return finalView;
1760
- }
1761
-
1762
- // Animate opacity or scale/transform
1763
- mutatedShadowView.props = componentDescriptor.interpolateProps(
1764
- propsParserContext, progress, startingView.props, finalView.props);
1765
- react_native_assert(mutatedShadowView.props != nullptr);
1766
- if (mutatedShadowView.props == nullptr) {
1767
- return finalView;
1768
- }
1625
+ void LayoutAnimationKeyFrameManager::deleteAnimationsForStoppedSurfaces()
1626
+ const {
1627
+ bool inflightAnimationsExistInitially = !inflightAnimations_.empty();
1769
1628
 
1770
- // Interpolate LayoutMetrics
1771
- LayoutMetrics const &finalLayoutMetrics = finalView.layoutMetrics;
1772
- LayoutMetrics const &baselineLayoutMetrics = startingView.layoutMetrics;
1773
- LayoutMetrics interpolatedLayoutMetrics = finalLayoutMetrics;
1774
- interpolatedLayoutMetrics.frame.origin.x = interpolateFloats(
1775
- progress,
1776
- baselineLayoutMetrics.frame.origin.x,
1777
- finalLayoutMetrics.frame.origin.x);
1778
- interpolatedLayoutMetrics.frame.origin.y = interpolateFloats(
1779
- progress,
1780
- baselineLayoutMetrics.frame.origin.y,
1781
- finalLayoutMetrics.frame.origin.y);
1782
- interpolatedLayoutMetrics.frame.size.width = interpolateFloats(
1783
- progress,
1784
- baselineLayoutMetrics.frame.size.width,
1785
- finalLayoutMetrics.frame.size.width);
1786
- interpolatedLayoutMetrics.frame.size.height = interpolateFloats(
1787
- progress,
1788
- baselineLayoutMetrics.frame.size.height,
1789
- finalLayoutMetrics.frame.size.height);
1790
- mutatedShadowView.layoutMetrics = interpolatedLayoutMetrics;
1629
+ // Execute stopSurface on any ongoing animations
1630
+ if (inflightAnimationsExistInitially) {
1631
+ better::set<SurfaceId> surfaceIdsToStop{};
1632
+ {
1633
+ std::lock_guard<std::mutex> lock(surfaceIdsToStopMutex_);
1634
+ surfaceIdsToStop = surfaceIdsToStop_;
1635
+ surfaceIdsToStop_.clear();
1636
+ }
1791
1637
 
1792
- return mutatedShadowView;
1793
- }
1638
+ for (auto it = inflightAnimations_.begin();
1639
+ it != inflightAnimations_.end();) {
1640
+ const auto &animation = *it;
1794
1641
 
1795
- void LayoutAnimationKeyFrameManager::queueFinalMutationsForCompletedKeyFrame(
1796
- AnimationKeyFrame const &keyframe,
1797
- ShadowViewMutation::List &mutationsList,
1798
- bool interrupted,
1799
- std::string logPrefix) const {
1800
- if (keyframe.finalMutationsForKeyFrame.size() > 0) {
1801
- // TODO: modularize this segment, it is repeated 2x in KeyFrameManager
1802
- // as well.
1803
- ShadowView prev = keyframe.viewPrev;
1804
- for (auto const &finalMutation : keyframe.finalMutationsForKeyFrame) {
1805
- PrintMutationInstruction(
1806
- logPrefix + ": Queuing up Final Mutation:", finalMutation);
1807
- // Copy so that if something else mutates the inflight animations,
1808
- // it won't change this mutation after this point.
1809
- auto mutation = ShadowViewMutation{
1810
- finalMutation.type,
1811
- finalMutation.parentShadowView,
1812
- prev,
1813
- finalMutation.newChildShadowView,
1814
- finalMutation.index};
1815
- react_native_assert(mutation.oldChildShadowView.tag > 0);
1816
- react_native_assert(
1817
- mutation.newChildShadowView.tag > 0 ||
1818
- finalMutation.type == ShadowViewMutation::Remove ||
1819
- finalMutation.type == ShadowViewMutation::Delete);
1820
- mutationsList.push_back(mutation);
1821
- if (finalMutation.newChildShadowView.tag > 0) {
1822
- prev = finalMutation.newChildShadowView;
1642
+ #ifdef LAYOUT_ANIMATION_VERBOSE_LOGGING
1643
+ LOG(ERROR)
1644
+ << "LayoutAnimations: stopping animation due to stopSurface on "
1645
+ << surfaceId;
1646
+ #endif
1647
+ if (surfaceIdsToStop.find(animation.surfaceId) !=
1648
+ surfaceIdsToStop.end()) {
1649
+ it = inflightAnimations_.erase(it);
1650
+ } else {
1651
+ it++;
1823
1652
  }
1824
1653
  }
1825
- } else {
1826
- // If there's no final mutation associated, create a mutation that
1827
- // corresponds to the animation being 100% complete. This is
1828
- // important for, for example, INSERT mutations being animated from
1829
- // opacity 0 to 1. If the animation is interrupted we must force the
1830
- // View to be at opacity 1. For Android - since it passes along only
1831
- // deltas, not an entire bag of props - generate an "animation"
1832
- // frame corresponding to a final update for this view. Only then,
1833
- // generate an update that will cause the ShadowTree to be
1834
- // consistent with the Mounting layer by passing viewEnd,
1835
- // unmodified, to the mounting layer. This helps with, for example,
1836
- // opacity animations.
1837
- // This is necessary for INSERT (create) and UPDATE (update) mutations, but
1838
- // not REMOVE/DELETE mutations ("delete" animations).
1839
- if (interrupted) {
1840
- auto mutatedShadowView =
1841
- createInterpolatedShadowView(1, keyframe.viewStart, keyframe.viewEnd);
1842
- auto generatedPenultimateMutation = ShadowViewMutation::UpdateMutation(
1843
- keyframe.viewPrev, mutatedShadowView);
1844
- react_native_assert(
1845
- generatedPenultimateMutation.oldChildShadowView.tag > 0);
1846
- react_native_assert(
1847
- generatedPenultimateMutation.newChildShadowView.tag > 0);
1848
- PrintMutationInstruction(
1849
- "Queueing up penultimate mutation instruction - synthetic",
1850
- generatedPenultimateMutation);
1851
- mutationsList.push_back(generatedPenultimateMutation);
1852
-
1853
- auto generatedMutation = ShadowViewMutation::UpdateMutation(
1854
- mutatedShadowView, keyframe.viewEnd);
1855
- react_native_assert(generatedMutation.oldChildShadowView.tag > 0);
1856
- react_native_assert(generatedMutation.newChildShadowView.tag > 0);
1857
- PrintMutationInstruction(
1858
- "Queueing up final mutation instruction - synthetic",
1859
- generatedMutation);
1860
- mutationsList.push_back(generatedMutation);
1861
- } else {
1862
- auto mutation = ShadowViewMutation{
1863
- ShadowViewMutation::Type::Update,
1864
- keyframe.parentView,
1865
- keyframe.viewPrev,
1866
- keyframe.viewEnd,
1867
- -1};
1868
- PrintMutationInstruction(
1869
- logPrefix +
1870
- "Animation Complete: Queuing up Final Synthetic Mutation:",
1871
- mutation);
1872
- react_native_assert(mutation.oldChildShadowView.tag > 0);
1873
- react_native_assert(mutation.newChildShadowView.tag > 0);
1874
- mutationsList.push_back(mutation);
1875
- }
1876
1654
  }
1877
1655
  }
1878
1656
 
1879
- void LayoutAnimationKeyFrameManager::callCallback(
1880
- const LayoutAnimationCallbackWrapper &callback) const {
1881
- if (callback.readyForCleanup()) {
1657
+ void LayoutAnimationKeyFrameManager::simulateImagePropsMemoryAccess(
1658
+ ShadowViewMutationList const &mutations) const {
1659
+ if (!simulateImagePropsMemoryAccess_) {
1882
1660
  return;
1883
1661
  }
1884
-
1885
- // Callbacks can only be called once. Replace the callsite with an empty
1886
- // CallbackWrapper. We use a unique_ptr to avoid copying into the vector.
1887
- std::unique_ptr<LayoutAnimationCallbackWrapper> copiedCallback(
1888
- std::make_unique<LayoutAnimationCallbackWrapper>(callback));
1889
-
1890
- // Call the callback that is being retained in the vector
1891
- copiedCallback->call(runtimeExecutor_);
1892
-
1893
- // Protect with a mutex: this can be called on failure callbacks in the JS
1894
- // thread and success callbacks on the UI thread
1895
- {
1896
- std::lock_guard<std::mutex> lock(callbackWrappersPendingMutex_);
1897
-
1898
- // Clean any stale data in the retention vector
1899
- callbackWrappersPending_.erase(
1900
- std::remove_if(
1901
- callbackWrappersPending_.begin(),
1902
- callbackWrappersPending_.end(),
1903
- [](const std::unique_ptr<LayoutAnimationCallbackWrapper> &wrapper) {
1904
- return wrapper->readyForCleanup();
1905
- }),
1906
- callbackWrappersPending_.end());
1907
-
1908
- // Hold onto a reference to the callback, only while
1909
- // LayoutAnimationKeyFrameManager is alive and the callback hasn't
1910
- // completed yet.
1911
- callbackWrappersPending_.push_back(std::move(copiedCallback));
1662
+ for (auto const &mutation : mutations) {
1663
+ if (mutation.type != ShadowViewMutation::Type::Insert) {
1664
+ continue;
1665
+ }
1666
+ if (strcmp(mutation.newChildShadowView.componentName, "Image") == 0) {
1667
+ auto const &imageProps = *std::static_pointer_cast<ImageProps const>(
1668
+ mutation.newChildShadowView.props);
1669
+ int temp = 0;
1670
+ switch (imageProps.resizeMode) {
1671
+ case ImageResizeMode::Cover:
1672
+ temp = 1;
1673
+ break;
1674
+ case ImageResizeMode::Contain:
1675
+ temp = 2;
1676
+ break;
1677
+ case ImageResizeMode::Stretch:
1678
+ temp = 3;
1679
+ break;
1680
+ case ImageResizeMode::Center:
1681
+ temp = 4;
1682
+ break;
1683
+ case ImageResizeMode::Repeat:
1684
+ temp = 5;
1685
+ break;
1686
+ }
1687
+ (void)temp;
1688
+ }
1912
1689
  }
1913
1690
  }
1914
1691