Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit c7529cb8 authored by Prabir Pradhan's avatar Prabir Pradhan Committed by Android (Google) Code Review
Browse files

Merge "MotionEvent JNI: Use return values for pointer{Coords,Props}ToNative" into main

parents 9b42a49f aa52f73d
Loading
Loading
Loading
Loading
+50 −59
Original line number Diff line number Diff line
@@ -203,44 +203,31 @@ static bool validatePointerProperties(JNIEnv* env, jobject pointerPropertiesObj)
    return true;
}

static void pointerCoordsToNative(JNIEnv* env, jobject pointerCoordsObj,
                                  PointerCoords& outRawPointerCoords) {
    outRawPointerCoords.clear();
    outRawPointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X,
                                     env->GetFloatField(pointerCoordsObj,
                                                        gPointerCoordsClassInfo.x));
    outRawPointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y,
                                     env->GetFloatField(pointerCoordsObj,
                                                        gPointerCoordsClassInfo.y));
    outRawPointerCoords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE,
                                     env->GetFloatField(pointerCoordsObj,
                                                        gPointerCoordsClassInfo.pressure));
    outRawPointerCoords.setAxisValue(AMOTION_EVENT_AXIS_SIZE,
                                     env->GetFloatField(pointerCoordsObj,
                                                        gPointerCoordsClassInfo.size));
    outRawPointerCoords.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR,
                                     env->GetFloatField(pointerCoordsObj,
                                                        gPointerCoordsClassInfo.touchMajor));
    outRawPointerCoords.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR,
                                     env->GetFloatField(pointerCoordsObj,
                                                        gPointerCoordsClassInfo.touchMinor));
    outRawPointerCoords.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR,
                                     env->GetFloatField(pointerCoordsObj,
                                                        gPointerCoordsClassInfo.toolMajor));
    outRawPointerCoords.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR,
                                     env->GetFloatField(pointerCoordsObj,
                                                        gPointerCoordsClassInfo.toolMinor));
    outRawPointerCoords.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION,
                                     env->GetFloatField(pointerCoordsObj,
                                                        gPointerCoordsClassInfo.orientation));
    outRawPointerCoords.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X,
                                     env->GetFloatField(pointerCoordsObj,
                                                        gPointerCoordsClassInfo.relativeX));
    outRawPointerCoords.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y,
                                     env->GetFloatField(pointerCoordsObj,
                                                        gPointerCoordsClassInfo.relativeY));
    outRawPointerCoords.isResampled =
            env->GetBooleanField(pointerCoordsObj, gPointerCoordsClassInfo.isResampled);
static PointerCoords pointerCoordsToNative(JNIEnv* env, jobject pointerCoordsObj) {
    PointerCoords out{};
    out.setAxisValue(AMOTION_EVENT_AXIS_X,
                     env->GetFloatField(pointerCoordsObj, gPointerCoordsClassInfo.x));
    out.setAxisValue(AMOTION_EVENT_AXIS_Y,
                     env->GetFloatField(pointerCoordsObj, gPointerCoordsClassInfo.y));
    out.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE,
                     env->GetFloatField(pointerCoordsObj, gPointerCoordsClassInfo.pressure));
    out.setAxisValue(AMOTION_EVENT_AXIS_SIZE,
                     env->GetFloatField(pointerCoordsObj, gPointerCoordsClassInfo.size));
    out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR,
                     env->GetFloatField(pointerCoordsObj, gPointerCoordsClassInfo.touchMajor));
    out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR,
                     env->GetFloatField(pointerCoordsObj, gPointerCoordsClassInfo.touchMinor));
    out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR,
                     env->GetFloatField(pointerCoordsObj, gPointerCoordsClassInfo.toolMajor));
    out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR,
                     env->GetFloatField(pointerCoordsObj, gPointerCoordsClassInfo.toolMinor));
    out.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION,
                     env->GetFloatField(pointerCoordsObj, gPointerCoordsClassInfo.orientation));
    out.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X,
                     env->GetFloatField(pointerCoordsObj, gPointerCoordsClassInfo.relativeX));
    out.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y,
                     env->GetFloatField(pointerCoordsObj, gPointerCoordsClassInfo.relativeY));
    out.isResampled = env->GetBooleanField(pointerCoordsObj, gPointerCoordsClassInfo.isResampled);

    BitSet64 bits =
            BitSet64(env->GetLongField(pointerCoordsObj, gPointerCoordsClassInfo.mPackedAxisBits));
@@ -254,13 +241,14 @@ static void pointerCoordsToNative(JNIEnv* env, jobject pointerCoordsObj,
            uint32_t index = 0;
            do {
                uint32_t axis = bits.clearFirstMarkedBit();
                outRawPointerCoords.setAxisValue(axis, values[index++]);
                out.setAxisValue(axis, values[index++]);
            } while (!bits.isEmpty());

            env->ReleasePrimitiveArrayCritical(valuesArray, values, JNI_ABORT);
            env->DeleteLocalRef(valuesArray);
        }
    }
    return out;
}

static jfloatArray obtainPackedAxisValuesArray(JNIEnv* env, uint32_t minSize,
@@ -312,14 +300,13 @@ static void pointerCoordsFromNative(JNIEnv* env, const PointerCoords* rawPointer
    env->SetLongField(outPointerCoordsObj, gPointerCoordsClassInfo.mPackedAxisBits, outBits);
}

static void pointerPropertiesToNative(JNIEnv* env, jobject pointerPropertiesObj,
        PointerProperties* outPointerProperties) {
    outPointerProperties->clear();
    outPointerProperties->id = env->GetIntField(pointerPropertiesObj,
            gPointerPropertiesClassInfo.id);
    const int32_t toolType = env->GetIntField(pointerPropertiesObj,
            gPointerPropertiesClassInfo.toolType);
    outPointerProperties->toolType = static_cast<ToolType>(toolType);
static PointerProperties pointerPropertiesToNative(JNIEnv* env, jobject pointerPropertiesObj) {
    PointerProperties out{};
    out.id = env->GetIntField(pointerPropertiesObj, gPointerPropertiesClassInfo.id);
    const int32_t toolType =
            env->GetIntField(pointerPropertiesObj, gPointerPropertiesClassInfo.toolType);
    out.toolType = static_cast<ToolType>(toolType);
    return out;
}

static void pointerPropertiesFromNative(JNIEnv* env, const PointerProperties* pointerProperties,
@@ -356,15 +343,17 @@ static jlong android_view_MotionEvent_nativeInitialize(
    transform.set(xOffset, yOffset);
    const ui::Transform inverseTransform = transform.inverse();

    PointerProperties pointerProperties[pointerCount];
    PointerCoords rawPointerCoords[pointerCount];
    std::vector<PointerProperties> pointerProperties;
    pointerProperties.reserve(pointerCount);
    std::vector<PointerCoords> rawPointerCoords;
    rawPointerCoords.reserve(pointerCount);

    for (jint i = 0; i < pointerCount; i++) {
        jobject pointerPropertiesObj = env->GetObjectArrayElement(pointerPropertiesObjArray, i);
        if (!pointerPropertiesObj) {
            return 0;
        }
        pointerPropertiesToNative(env, pointerPropertiesObj, &pointerProperties[i]);
        pointerProperties.emplace_back(pointerPropertiesToNative(env, pointerPropertiesObj));
        env->DeleteLocalRef(pointerPropertiesObj);

        jobject pointerCoordsObj = env->GetObjectArrayElement(pointerCoordsObjArray, i);
@@ -372,13 +361,13 @@ static jlong android_view_MotionEvent_nativeInitialize(
            jniThrowNullPointerException(env, "pointerCoords");
            return 0;
        }
        pointerCoordsToNative(env, pointerCoordsObj, rawPointerCoords[i]);
        if (rawPointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION) != 0.f) {
        rawPointerCoords.emplace_back(pointerCoordsToNative(env, pointerCoordsObj));
        PointerCoords& coords = rawPointerCoords.back();
        if (coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION) != 0.f) {
            flags |= AMOTION_EVENT_PRIVATE_FLAG_SUPPORTS_ORIENTATION |
                    AMOTION_EVENT_PRIVATE_FLAG_SUPPORTS_DIRECTIONAL_ORIENTATION;
        }
        MotionEvent::calculateTransformedCoordsInPlace(rawPointerCoords[i], source, flags,
                                                       inverseTransform);
        MotionEvent::calculateTransformedCoordsInPlace(coords, source, flags, inverseTransform);
        env->DeleteLocalRef(pointerCoordsObj);
    }

@@ -388,7 +377,8 @@ static jlong android_view_MotionEvent_nativeInitialize(
                      static_cast<MotionClassification>(classification), transform, xPrecision,
                      yPrecision, AMOTION_EVENT_INVALID_CURSOR_POSITION,
                      AMOTION_EVENT_INVALID_CURSOR_POSITION, kIdentityTransform, downTimeNanos,
                      eventTimeNanos, pointerCount, pointerProperties, rawPointerCoords);
                      eventTimeNanos, pointerCount, pointerProperties.data(),
                      rawPointerCoords.data());

    return reinterpret_cast<jlong>(event.release());
}
@@ -410,7 +400,8 @@ static void android_view_MotionEvent_nativeAddBatch(JNIEnv* env, jclass clazz,

    const ui::Transform inverseTransform = event->getTransform().inverse();

    PointerCoords rawPointerCoords[pointerCount];
    std::vector<PointerCoords> rawPointerCoords;
    rawPointerCoords.reserve(pointerCount);

    for (size_t i = 0; i < pointerCount; i++) {
        jobject pointerCoordsObj = env->GetObjectArrayElement(pointerCoordsObjArray, i);
@@ -418,13 +409,13 @@ static void android_view_MotionEvent_nativeAddBatch(JNIEnv* env, jclass clazz,
            jniThrowNullPointerException(env, "pointerCoords");
            return;
        }
        pointerCoordsToNative(env, pointerCoordsObj, rawPointerCoords[i]);
        MotionEvent::calculateTransformedCoordsInPlace(rawPointerCoords[i], event->getSource(),
        rawPointerCoords.emplace_back(pointerCoordsToNative(env, pointerCoordsObj));
        MotionEvent::calculateTransformedCoordsInPlace(rawPointerCoords.back(), event->getSource(),
                                                       event->getFlags(), inverseTransform);
        env->DeleteLocalRef(pointerCoordsObj);
    }

    event->addSample(eventTimeNanos, rawPointerCoords);
    event->addSample(eventTimeNanos, rawPointerCoords.data());
    event->setMetaState(event->getMetaState() | metaState);
}