Skip to content

Commit 3908ef3

Browse files
tomekzawclaude
andauthored
Build the scroll event payload in one JNI call (#59714)
* Build the scroll event payload in one JNI call Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Build the JNI event and transform helpers in reactnativejni ScrollEventHelper, TouchesHelper and TransformHelper are only referenced by reactnativejni's OnLoad.cpp, so they belong in that library rather than in reactnativejni_common. TransformHelper is what needed rrc_view, so that dependency moves with it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 11f37bd commit 3908ef3

10 files changed

Lines changed: 192 additions & 43 deletions

File tree

‎packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ScrollEvent.kt‎

Lines changed: 16 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,8 @@ package com.facebook.react.views.scroll
1010
import android.os.SystemClock
1111
import androidx.core.util.Pools.SynchronizedPool
1212
import com.facebook.infer.annotation.Assertions
13-
import com.facebook.react.bridge.Arguments
1413
import com.facebook.react.bridge.ReactSoftExceptionLogger
1514
import com.facebook.react.bridge.WritableMap
16-
import com.facebook.react.bridge.buildReadableMap
1715
import com.facebook.react.internal.featureflags.ReactNativeFeatureFlags
1816
import com.facebook.react.uimanager.PixelUtil.toDIPFromPixel
1917
import com.facebook.react.uimanager.common.ViewUtil
@@ -76,46 +74,24 @@ public class ScrollEvent private constructor() : Event<ScrollEvent>() {
7674
override fun canCoalesce(): Boolean = scrollEventType == ScrollEventType.SCROLL
7775

7876
override fun getEventData(): WritableMap {
79-
val contentInset = buildReadableMap {
80-
put("top", 0.0)
81-
put("bottom", 0.0)
82-
put("left", 0.0)
83-
put("right", 0.0)
84-
}
85-
86-
val contentOffset = buildReadableMap {
87-
put("x", toDIPFromPixel(scrollX).toDouble())
88-
put("y", toDIPFromPixel(scrollY).toDouble())
89-
}
90-
91-
val contentSize = buildReadableMap {
92-
put("width", toDIPFromPixel(contentWidth.toFloat()).toDouble())
93-
put("height", toDIPFromPixel(contentHeight.toFloat()).toDouble())
94-
}
95-
96-
val layoutMeasurement = buildReadableMap {
97-
put("width", toDIPFromPixel(scrollViewWidth.toFloat()).toDouble())
98-
put("height", toDIPFromPixel(scrollViewHeight.toFloat()).toDouble())
99-
}
100-
101-
val velocity = buildReadableMap {
102-
put("x", toDIPFromPixel(xVelocity).toDouble())
103-
put("y", toDIPFromPixel(yVelocity).toDouble())
104-
}
105-
106-
val event = Arguments.createMap()
107-
event.putMap("contentInset", contentInset)
108-
event.putMap("contentOffset", contentOffset)
109-
event.putMap("contentSize", contentSize)
110-
event.putMap("layoutMeasurement", layoutMeasurement)
111-
event.putMap("velocity", velocity)
112-
event.putInt("target", viewTag)
113-
event.putDouble("timestamp", timestamp.toDouble())
114-
event.putBoolean(
115-
"responderIgnoreScroll",
77+
val data = DoubleArray(ScrollEventHelper.FIELDS)
78+
data[ScrollEventHelper.CONTENT_OFFSET_X_INDEX] = toDIPFromPixel(scrollX).toDouble()
79+
data[ScrollEventHelper.CONTENT_OFFSET_Y_INDEX] = toDIPFromPixel(scrollY).toDouble()
80+
data[ScrollEventHelper.CONTENT_WIDTH_INDEX] = toDIPFromPixel(contentWidth.toFloat()).toDouble()
81+
data[ScrollEventHelper.CONTENT_HEIGHT_INDEX] =
82+
toDIPFromPixel(contentHeight.toFloat()).toDouble()
83+
data[ScrollEventHelper.LAYOUT_WIDTH_INDEX] =
84+
toDIPFromPixel(scrollViewWidth.toFloat()).toDouble()
85+
data[ScrollEventHelper.LAYOUT_HEIGHT_INDEX] =
86+
toDIPFromPixel(scrollViewHeight.toFloat()).toDouble()
87+
data[ScrollEventHelper.VELOCITY_X_INDEX] = toDIPFromPixel(xVelocity).toDouble()
88+
data[ScrollEventHelper.VELOCITY_Y_INDEX] = toDIPFromPixel(yVelocity).toDouble()
89+
data[ScrollEventHelper.TIMESTAMP_INDEX] = timestamp.toDouble()
90+
return ScrollEventHelper.nativeCreateScrollPayload(
91+
data,
92+
viewTag,
11693
!ReactNativeFeatureFlags.shouldTriggerResponderTransferOnScrollAndroid(),
11794
)
118-
return event
11995
}
12096

12197
public companion object {
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
package com.facebook.react.views.scroll
9+
10+
import com.facebook.proguard.annotations.DoNotStrip
11+
import com.facebook.react.bridge.ReactNativeJNISoLoader
12+
import com.facebook.react.bridge.WritableNativeMap
13+
14+
/** Builds the payload of a [ScrollEvent] in one native call. */
15+
@DoNotStrip
16+
internal object ScrollEventHelper {
17+
init {
18+
ReactNativeJNISoLoader.staticInit()
19+
}
20+
21+
// Keep in sync with the field indices in ScrollEventHelper.cpp.
22+
const val CONTENT_OFFSET_X_INDEX = 0
23+
const val CONTENT_OFFSET_Y_INDEX = 1
24+
const val CONTENT_WIDTH_INDEX = 2
25+
const val CONTENT_HEIGHT_INDEX = 3
26+
const val LAYOUT_WIDTH_INDEX = 4
27+
const val LAYOUT_HEIGHT_INDEX = 5
28+
const val VELOCITY_X_INDEX = 6
29+
const val VELOCITY_Y_INDEX = 7
30+
const val TIMESTAMP_INDEX = 8
31+
const val FIELDS = 9
32+
33+
/**
34+
* Creates the payload of a scroll event: `contentInset` (all zero), `contentOffset`,
35+
* `contentSize`, `layoutMeasurement`, `velocity`, `target`, `timestamp` and
36+
* `responderIgnoreScroll`.
37+
*
38+
* @param data [FIELDS] values, in DIPs, at the indices above.
39+
*/
40+
@DoNotStrip
41+
@JvmStatic
42+
external fun nativeCreateScrollPayload(
43+
data: DoubleArray,
44+
target: Int,
45+
responderIgnoreScroll: Boolean,
46+
): WritableNativeMap
47+
}

‎packages/react-native/ReactAndroid/src/main/jni/react/jni/CMakeLists.txt‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,13 @@ add_library(
3232
OnLoad-common.cpp
3333
ReadableNativeArray.cpp
3434
ReadableNativeMap.cpp
35-
TouchesHelper.cpp
36-
TransformHelper.cpp
3735
WritableNativeArray.cpp
3836
WritableNativeMap.cpp
3937
)
4038
target_merge_so(reactnativejni_common)
4139
target_include_directories(reactnativejni_common PUBLIC ../../)
4240

43-
target_link_libraries(reactnativejni_common fbjni folly_runtime react_bridging react_cxxreact rrc_view)
41+
target_link_libraries(reactnativejni_common fbjni folly_runtime react_bridging react_cxxreact)
4442
target_compile_reactnative_options(reactnativejni_common PRIVATE)
4543
target_compile_options(reactnativejni_common PRIVATE -Wno-unused-lambda-capture)
4644

@@ -67,6 +65,9 @@ add_library(
6765
ModuleRegistryBuilder.cpp
6866
OnLoad.cpp
6967
SafeReleaseJniRef.cpp
68+
ScrollEventHelper.cpp
69+
TouchesHelper.cpp
70+
TransformHelper.cpp
7071
)
7172
target_merge_so(reactnativejni)
7273

@@ -84,6 +85,7 @@ target_link_libraries(reactnativejni
8485
react_cxxstableapi
8586
react_renderer_runtimescheduler
8687
reactnativejni_common
88+
rrc_view
8789
runtimeexecutor
8890
yoga
8991
)

‎packages/react-native/ReactAndroid/src/main/jni/react/jni/OnLoad.cpp‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <fbjni/fbjni.h>
1212

1313
#include "InspectorNetworkRequestListener.h"
14+
#include "ScrollEventHelper.h"
1415
#include "TouchesHelper.h"
1516
#include "TransformHelper.h"
1617

@@ -35,6 +36,7 @@ extern "C" JNIEXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved) {
3536
FLAGS_minloglevel = 0;
3637
#endif
3738
InspectorNetworkRequestListener::registerNatives();
39+
ScrollEventHelper::registerNatives();
3840
TouchesHelper::registerNatives();
3941
TransformHelper::registerNatives();
4042
});
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
#include "ScrollEventHelper.h"
9+
10+
#include <stdexcept>
11+
12+
#include "WritableNativeMap.h"
13+
14+
using namespace facebook::jni;
15+
16+
namespace facebook::react {
17+
18+
namespace {
19+
20+
// Keep in sync with the field indices in ScrollEventHelper.kt.
21+
constexpr jint kContentOffsetXIndex = 0;
22+
constexpr jint kContentOffsetYIndex = 1;
23+
constexpr jint kContentWidthIndex = 2;
24+
constexpr jint kContentHeightIndex = 3;
25+
constexpr jint kLayoutWidthIndex = 4;
26+
constexpr jint kLayoutHeightIndex = 5;
27+
constexpr jint kVelocityXIndex = 6;
28+
constexpr jint kVelocityYIndex = 7;
29+
constexpr jint kTimestampIndex = 8;
30+
constexpr jint kFields = 9;
31+
32+
local_ref<WritableNativeMap::jhybridobject> createScrollPayload(
33+
alias_ref<jclass> /*unused*/,
34+
alias_ref<JArrayDouble> data,
35+
jint target,
36+
jboolean responderIgnoreScroll) {
37+
if (data->size() != kFields) {
38+
throw std::invalid_argument("Invalid scroll event data");
39+
}
40+
auto values = data->getRegion(0, kFields);
41+
const double* fields = values.get();
42+
43+
auto jPayload = WritableNativeMap::newObjectJavaArgs();
44+
auto* payload = jPayload->cthis();
45+
// NOLINTBEGIN(cppcoreguidelines-pro-bounds-pointer-arithmetic)
46+
WritableNativeMap contentInset(
47+
folly::dynamic::object("top", 0.0)("bottom", 0.0)("left", 0.0)(
48+
"right", 0.0));
49+
payload->putNativeMap("contentInset", &contentInset);
50+
WritableNativeMap contentOffset(
51+
folly::dynamic::object("x", fields[kContentOffsetXIndex])(
52+
"y", fields[kContentOffsetYIndex]));
53+
payload->putNativeMap("contentOffset", &contentOffset);
54+
WritableNativeMap contentSize(
55+
folly::dynamic::object("width", fields[kContentWidthIndex])(
56+
"height", fields[kContentHeightIndex]));
57+
payload->putNativeMap("contentSize", &contentSize);
58+
WritableNativeMap layoutMeasurement(
59+
folly::dynamic::object("width", fields[kLayoutWidthIndex])(
60+
"height", fields[kLayoutHeightIndex]));
61+
payload->putNativeMap("layoutMeasurement", &layoutMeasurement);
62+
WritableNativeMap velocity(
63+
folly::dynamic::object("x", fields[kVelocityXIndex])(
64+
"y", fields[kVelocityYIndex]));
65+
payload->putNativeMap("velocity", &velocity);
66+
payload->putInt("target", target);
67+
payload->putDouble("timestamp", fields[kTimestampIndex]);
68+
// NOLINTEND(cppcoreguidelines-pro-bounds-pointer-arithmetic)
69+
payload->putBoolean("responderIgnoreScroll", responderIgnoreScroll != 0u);
70+
return jPayload;
71+
}
72+
73+
} // namespace
74+
75+
void ScrollEventHelper::registerNatives() {
76+
javaClassLocal()->registerNatives({
77+
makeNativeMethod("nativeCreateScrollPayload", createScrollPayload),
78+
});
79+
}
80+
81+
} // namespace facebook::react
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
#pragma once
9+
10+
#include <fbjni/fbjni.h>
11+
12+
namespace facebook::react {
13+
14+
class ScrollEventHelper : public jni::JavaClass<ScrollEventHelper> {
15+
public:
16+
static auto constexpr *kJavaDescriptor = "Lcom/facebook/react/views/scroll/ScrollEventHelper;";
17+
18+
static void registerNatives();
19+
};
20+
21+
} // namespace facebook::react

‎scripts/cxx-api/api-snapshots/ReactAndroidDebugCxx.api‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2203,6 +2203,11 @@ class facebook::react::SafeAreaViewProps : public facebook::react::HostPlatformV
22032203
public virtual folly::dynamic getDiffProps(const facebook::react::Props* prevProps) const override;
22042204
}
22052205

2206+
class facebook::react::ScrollEventHelper : public facebook::jni::JavaClass<facebook::react::ScrollEventHelper> {
2207+
public static constexpr auto * kJavaDescriptor;
2208+
public static void registerNatives();
2209+
}
2210+
22062211
class facebook::react::Sealable {
22072212
public Sealable();
22082213
public Sealable(const facebook::react::Sealable& other);

‎scripts/cxx-api/api-snapshots/ReactAndroidFrameworksCxx.api‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3567,6 +3567,11 @@ class facebook::react::SchedulerDelegate {
35673567
public virtual ~SchedulerDelegate() noexcept = default;
35683568
}
35693569

3570+
class facebook::react::ScrollEventHelper : public facebook::jni::JavaClass<facebook::react::ScrollEventHelper> {
3571+
public static constexpr auto * kJavaDescriptor;
3572+
public static void registerNatives();
3573+
}
3574+
35703575
class facebook::react::ScrollViewEventEmitter : public facebook::react::BaseViewEventEmitter {
35713576
public using EndDragMetrics = facebook::react::ScrollEndDragEvent;
35723577
public using Metrics = facebook::react::ScrollEvent;

‎scripts/cxx-api/api-snapshots/ReactAndroidNewarchCxx.api‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2202,6 +2202,11 @@ class facebook::react::SafeAreaViewProps : public facebook::react::HostPlatformV
22022202
public virtual folly::dynamic getDiffProps(const facebook::react::Props* prevProps) const override;
22032203
}
22042204

2205+
class facebook::react::ScrollEventHelper : public facebook::jni::JavaClass<facebook::react::ScrollEventHelper> {
2206+
public static constexpr auto * kJavaDescriptor;
2207+
public static void registerNatives();
2208+
}
2209+
22052210
class facebook::react::Sealable {
22062211
public bool getSealed() const;
22072212
public void ensureUnsealed() const;

‎scripts/cxx-api/api-snapshots/ReactAndroidReleaseCxx.api‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2202,6 +2202,11 @@ class facebook::react::SafeAreaViewProps : public facebook::react::HostPlatformV
22022202
public virtual folly::dynamic getDiffProps(const facebook::react::Props* prevProps) const override;
22032203
}
22042204

2205+
class facebook::react::ScrollEventHelper : public facebook::jni::JavaClass<facebook::react::ScrollEventHelper> {
2206+
public static constexpr auto * kJavaDescriptor;
2207+
public static void registerNatives();
2208+
}
2209+
22052210
class facebook::react::Sealable {
22062211
public bool getSealed() const;
22072212
public void ensureUnsealed() const;

0 commit comments

Comments
 (0)