From aa485308704c2fae384a589158e6c3f802d75ccb Mon Sep 17 00:00:00 2001 From: Samuel Freilich Date: Tue, 19 May 2026 11:13:36 -0700 Subject: [PATCH] Simplify JNI interface to avoid some unnecessary calls PiperOrigin-RevId: 917929265 --- ink/jni/internal/jni_jvm_interface.cc | 26 +---------------- ink/jni/internal/jni_jvm_interface.h | 3 -- .../internal/jni/stroke_input_batch_native.cc | 19 ++++++------ .../internal/jni/stroke_input_batch_native.h | 2 +- .../internal/jni/stroke_input_jni_helper.cc | 29 ++++--------------- .../jni/stroke_input_native_helper.cc | 4 --- .../internal/jni/stroke_input_native_helper.h | 4 --- 7 files changed, 17 insertions(+), 70 deletions(-) diff --git a/ink/jni/internal/jni_jvm_interface.cc b/ink/jni/internal/jni_jvm_interface.cc index 0eff7b5b..766e28d5 100644 --- a/ink/jni/internal/jni_jvm_interface.cc +++ b/ink/jni/internal/jni_jvm_interface.cc @@ -56,9 +56,6 @@ static jclass class_color_callbacks = nullptr; static jmethodID method_color_callbacks_compose_color_long_from_components = nullptr; -static jclass class_input_tool_type = nullptr; -static jmethodID method_input_tool_type_from = nullptr; - static jclass class_stroke_input = nullptr; static jmethodID method_stroke_input_update = nullptr; @@ -128,9 +125,6 @@ void UnloadJvmInterface(JNIEnv* env) { DeleteCachedClass(env, class_color_callbacks); method_color_callbacks_compose_color_long_from_components = nullptr; - DeleteCachedClass(env, class_input_tool_type); - method_input_tool_type_from = nullptr; - DeleteCachedClass(env, class_stroke_input); method_stroke_input_update = nullptr; } @@ -319,23 +313,6 @@ jmethodID MethodColorCallbacksComposeColorLongFromComponents(JNIEnv* env) { return method_color_callbacks_compose_color_long_from_components; } -jclass ClassInputToolType(JNIEnv* env) { - if (class_input_tool_type == nullptr) { - class_input_tool_type = - FindAndCacheClass(env, "androidx/ink/brush/InputToolType"); - } - return class_input_tool_type; -} - -jmethodID MethodInputToolTypeFromInt(JNIEnv* env) { - if (method_input_tool_type_from == nullptr) { - method_input_tool_type_from = - GetStaticMethodId(env, ClassInputToolType(env), "fromInt", - "(I)Landroidx/ink/brush/InputToolType;"); - } - return method_input_tool_type_from; -} - jclass ClassStrokeInput(JNIEnv* env) { if (class_stroke_input == nullptr) { class_stroke_input = @@ -347,8 +324,7 @@ jclass ClassStrokeInput(JNIEnv* env) { jmethodID MethodStrokeInputUpdate(JNIEnv* env) { if (method_stroke_input_update == nullptr) { method_stroke_input_update = - GetMethodId(env, ClassStrokeInput(env), "update", - "(FFJLandroidx/ink/brush/InputToolType;FFFF)V"); + GetMethodId(env, ClassStrokeInput(env), "update", "(FFJIFFFF)V"); } return method_stroke_input_update; } diff --git a/ink/jni/internal/jni_jvm_interface.h b/ink/jni/internal/jni_jvm_interface.h index a46bd769..4ccd4409 100644 --- a/ink/jni/internal/jni_jvm_interface.h +++ b/ink/jni/internal/jni_jvm_interface.h @@ -68,9 +68,6 @@ jmethodID MethodMutableParallelogramSetCenterDimensionsRotationInDegreesAndSkew( jclass ClassColorCallbacks(JNIEnv* env); jmethodID MethodColorCallbacksComposeColorLongFromComponents(JNIEnv* env); -jclass ClassInputToolType(JNIEnv* env); -jmethodID MethodInputToolTypeFromInt(JNIEnv* env); - jclass ClassStrokeInput(JNIEnv* env); jmethodID MethodStrokeInputUpdate(JNIEnv* env); diff --git a/ink/strokes/internal/jni/stroke_input_batch_native.cc b/ink/strokes/internal/jni/stroke_input_batch_native.cc index 082e0777..a2934f01 100644 --- a/ink/strokes/internal/jni/stroke_input_batch_native.cc +++ b/ink/strokes/internal/jni/stroke_input_batch_native.cc @@ -52,16 +52,15 @@ int StrokeInputBatchNative_getSize(int64_t native_pointer) { StrokeInputBatchNative_Input StrokeInputBatchNative_getStrokeInput( int64_t native_pointer, int index) { StrokeInput input = CastToStrokeInputBatch(native_pointer).Get(index); - return StrokeInputBatchNative_Input{ - .tool_type = ToolTypeToInt(input.tool_type), - .x = input.position.x, - .y = input.position.y, - .elapsed_time_millis = - static_cast(input.elapsed_time.ToMillis()), - .stroke_unit_length_cm = input.stroke_unit_length.ToCentimeters(), - .pressure = input.pressure, - .tilt_radians = input.tilt.ValueInRadians(), - .orientation_radians = input.orientation.ValueInRadians()}; + return {.tool_type_int = ToolTypeToInt(input.tool_type), + .x = input.position.x, + .y = input.position.y, + .elapsed_time_millis = + static_cast(input.elapsed_time.ToMillis()), + .stroke_unit_length_cm = input.stroke_unit_length.ToCentimeters(), + .pressure = input.pressure, + .tilt_radians = input.tilt.ValueInRadians(), + .orientation_radians = input.orientation.ValueInRadians()}; } int64_t StrokeInputBatchNative_getDurationMillis(int64_t native_pointer) { diff --git a/ink/strokes/internal/jni/stroke_input_batch_native.h b/ink/strokes/internal/jni/stroke_input_batch_native.h index a070b8e7..35dcfa95 100644 --- a/ink/strokes/internal/jni/stroke_input_batch_native.h +++ b/ink/strokes/internal/jni/stroke_input_batch_native.h @@ -23,7 +23,7 @@ extern "C" { #endif typedef struct { - int tool_type; + int tool_type_int; float x; float y; int64_t elapsed_time_millis; diff --git a/ink/strokes/internal/jni/stroke_input_jni_helper.cc b/ink/strokes/internal/jni/stroke_input_jni_helper.cc index 4b873b53..f0882764 100644 --- a/ink/strokes/internal/jni/stroke_input_jni_helper.cc +++ b/ink/strokes/internal/jni/stroke_input_jni_helper.cc @@ -23,28 +23,14 @@ namespace ink::jni { -namespace { - -using ::ink::native::IntToToolType; using ::ink::native::ToolTypeToInt; -jobject ToolTypeToJObjectOrThrow(JNIEnv* env, StrokeInput::ToolType tool_type) { - return env->CallStaticObjectMethod(ClassInputToolType(env), - MethodInputToolTypeFromInt(env), - ToolTypeToInt(tool_type)); -} - -} // namespace - void UpdateJStrokeInputOrThrow(JNIEnv* env, const StrokeInput& input_in, jobject j_input_out) { - jobject j_inputtooltype = ToolTypeToJObjectOrThrow(env, input_in.tool_type); - if (env->ExceptionCheck()) return; - - jlong elapsed_time_millis = input_in.elapsed_time.ToMillis(); env->CallVoidMethod( j_input_out, MethodStrokeInputUpdate(env), input_in.position.x, - input_in.position.y, elapsed_time_millis, j_inputtooltype, + input_in.position.y, static_cast(input_in.elapsed_time.ToMillis()), + ToolTypeToInt(input_in.tool_type), input_in.stroke_unit_length.ToCentimeters(), input_in.pressure, input_in.tilt.ValueInRadians(), input_in.orientation.ValueInRadians()); } @@ -52,14 +38,11 @@ void UpdateJStrokeInputOrThrow(JNIEnv* env, const StrokeInput& input_in, void UpdateJStrokeInputOrThrow(JNIEnv* env, const StrokeInputBatchNative_Input& input_in, jobject j_input_out) { - jobject j_inputtooltype = - ToolTypeToJObjectOrThrow(env, IntToToolType(input_in.tool_type)); - if (env->ExceptionCheck()) return; - env->CallVoidMethod(j_input_out, MethodStrokeInputUpdate(env), input_in.x, - input_in.y, input_in.elapsed_time_millis, j_inputtooltype, - input_in.stroke_unit_length_cm, input_in.pressure, - input_in.tilt_radians, input_in.orientation_radians); + input_in.y, input_in.elapsed_time_millis, + input_in.tool_type_int, input_in.stroke_unit_length_cm, + input_in.pressure, input_in.tilt_radians, + input_in.orientation_radians); } } // namespace ink::jni diff --git a/ink/strokes/internal/jni/stroke_input_native_helper.cc b/ink/strokes/internal/jni/stroke_input_native_helper.cc index f7bae3cb..c0e9caf6 100644 --- a/ink/strokes/internal/jni/stroke_input_native_helper.cc +++ b/ink/strokes/internal/jni/stroke_input_native_helper.cc @@ -21,10 +21,6 @@ namespace ink::native { // Convert an int to an StrokeInput::ToolType enum. // // This should match the enum in InputToolType.kt. -StrokeInput::ToolType IntToToolType(int val) { - return static_cast(val); -} - int ToolTypeToInt(StrokeInput::ToolType type) { switch (type) { case StrokeInput::ToolType::kMouse: diff --git a/ink/strokes/internal/jni/stroke_input_native_helper.h b/ink/strokes/internal/jni/stroke_input_native_helper.h index 7a5e858b..5a5d8e20 100644 --- a/ink/strokes/internal/jni/stroke_input_native_helper.h +++ b/ink/strokes/internal/jni/stroke_input_native_helper.h @@ -19,10 +19,6 @@ namespace ink::native { -// Converts Kotlin jint representation of InputToolType enum to C++ -// StrokeInput::ToolType enum. -StrokeInput::ToolType IntToToolType(int val); - // Converts C++ StrokeInput::ToolType enum to Kotlin int representation of // InputToolType enum. int ToolTypeToInt(StrokeInput::ToolType type);