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

Commit 2800296d authored by Peiyong Lin's avatar Peiyong Lin Committed by Android (Google) Code Review
Browse files

Merge "Implement setThreads APIs for PerformanceHintManager.Session."

parents b9067b1d 095de769
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -32519,6 +32519,7 @@ package android.os {
  public static class PerformanceHintManager.Session implements java.io.Closeable {
    method public void close();
    method public void reportActualWorkDuration(long);
    method public void setThreads(@NonNull int[]);
    method public void updateTargetWorkDuration(long);
  }
+1 −0
Original line number Diff line number Diff line
@@ -1861,6 +1861,7 @@ package android.os {
  }

  public static class PerformanceHintManager.Session implements java.io.Closeable {
    method @Nullable public int[] getThreadIds();
    method public void sendHint(int);
    field public static final int CPU_LOAD_DOWN = 1; // 0x1
    field public static final int CPU_LOAD_RESET = 2; // 0x2
+5 −2
Original line number Diff line number Diff line
@@ -30,4 +30,7 @@ interface IHintManager {
     * Get preferred rate limit in nano second.
     */
    long getHintSessionPreferredRate();

    void setHintSessionThreads(in IHintSession hintSession, in int[] tids);
    int[] getHintSessionThreadIds(in IHintSession hintSession);
}
+34 −0
Original line number Diff line number Diff line
@@ -222,16 +222,50 @@ public final class PerformanceHintManager {
                Reference.reachabilityFence(this);
            }
        }

        /**
         * Set a list of threads to the performance hint session. This operation will replace
         * the current list of threads with the given list of threads.
         * Note that this is not an oneway method.
         *
         * @param tids The list of threads to be associated with this session. They must be
         *     part of this app's thread group.
         *
         * @throws IllegalStateException if the hint session is not in the foreground.
         * @throws IllegalArgumentException if the thread id list is empty.
         * @throws SecurityException if any thread id doesn't belong to the application.
         */
        public void setThreads(@NonNull int[] tids) {
            if (mNativeSessionPtr == 0) {
                return;
            }
            if (tids.length == 0) {
                throw new IllegalArgumentException("Thread id list can't be empty.");
            }
            nativeSetThreads(mNativeSessionPtr, tids);
        }

        /**
         * Returns the list of thread ids.
         *
         * @hide
         */
        @TestApi
        public @Nullable int[] getThreadIds() {
            return nativeGetThreadIds(mNativeSessionPtr);
        }
    }

    private static native long nativeAcquireManager();
    private static native long nativeGetPreferredUpdateRateNanos(long nativeManagerPtr);
    private static native long nativeCreateSession(long nativeManagerPtr,
            int[] tids, long initialTargetWorkDurationNanos);
    private static native int[] nativeGetThreadIds(long nativeSessionPtr);
    private static native void nativeUpdateTargetWorkDuration(long nativeSessionPtr,
            long targetDurationNanos);
    private static native void nativeReportActualWorkDuration(long nativeSessionPtr,
            long actualDurationNanos);
    private static native void nativeCloseSession(long nativeSessionPtr);
    private static native void nativeSendHint(long nativeSessionPtr, int hint);
    private static native void nativeSetThreads(long nativeSessionPtr, int[] tids);
}
+58 −0
Original line number Diff line number Diff line
@@ -41,6 +41,8 @@ typedef void (*APH_updateTargetWorkDuration)(APerformanceHintSession*, int64_t);
typedef void (*APH_reportActualWorkDuration)(APerformanceHintSession*, int64_t);
typedef void (*APH_closeSession)(APerformanceHintSession* session);
typedef void (*APH_sendHint)(APerformanceHintSession*, int32_t);
typedef void (*APH_setThreads)(APerformanceHintSession*, const int32_t*, size_t);
typedef void (*APH_getThreadIds)(APerformanceHintSession*, int32_t* const, size_t* const);

bool gAPerformanceHintBindingInitialized = false;
APH_getManager gAPH_getManagerFn = nullptr;
@@ -50,6 +52,8 @@ APH_updateTargetWorkDuration gAPH_updateTargetWorkDurationFn = nullptr;
APH_reportActualWorkDuration gAPH_reportActualWorkDurationFn = nullptr;
APH_closeSession gAPH_closeSessionFn = nullptr;
APH_sendHint gAPH_sendHintFn = nullptr;
APH_setThreads gAPH_setThreadsFn = nullptr;
APH_getThreadIds gAPH_getThreadIdsFn = nullptr;

void ensureAPerformanceHintBindingInitialized() {
    if (gAPerformanceHintBindingInitialized) return;
@@ -95,6 +99,14 @@ void ensureAPerformanceHintBindingInitialized() {
                        "Failed to find required symbol "
                        "APerformanceHint_sendHint!");

    gAPH_setThreadsFn = (APH_setThreads)dlsym(handle_, "APerformanceHint_setThreads");
    LOG_ALWAYS_FATAL_IF(gAPH_setThreadsFn == nullptr,
                        "Failed to find required symbol APerformanceHint_setThreads!");

    gAPH_getThreadIdsFn = (APH_getThreadIds)dlsym(handle_, "APerformanceHint_getThreadIds");
    LOG_ALWAYS_FATAL_IF(gAPH_getThreadIdsFn == nullptr,
                        "Failed to find required symbol APerformanceHint_getThreadIds!");

    gAPerformanceHintBindingInitialized = true;
}

@@ -150,6 +162,50 @@ static void nativeSendHint(JNIEnv* env, jclass clazz, jlong nativeSessionPtr, ji
    gAPH_sendHintFn(reinterpret_cast<APerformanceHintSession*>(nativeSessionPtr), hint);
}

static void nativeSetThreads(JNIEnv* env, jclass clazz, jlong nativeSessionPtr, jintArray tids) {
    ensureAPerformanceHintBindingInitialized();

    if (tids == nullptr) {
        return;
    }
    ScopedIntArrayRO tidsArray(env, tids);
    std::vector<int32_t> tidsVector;
    tidsVector.reserve(tidsArray.size());
    for (size_t i = 0; i < tidsArray.size(); ++i) {
        tidsVector.push_back(static_cast<int32_t>(tidsArray[i]));
    }
    gAPH_setThreadsFn(reinterpret_cast<APerformanceHintSession*>(nativeSessionPtr),
                      tidsVector.data(), tidsVector.size());
}

// This call should only be used for validation in tests only. This call will initiate two IPC
// calls, the first one is used to determined the size of the thread ids list, the second one
// is used to return the actual list.
static jintArray nativeGetThreadIds(JNIEnv* env, jclass clazz, jlong nativeSessionPtr) {
    ensureAPerformanceHintBindingInitialized();
    size_t size = 0;
    gAPH_getThreadIdsFn(reinterpret_cast<APerformanceHintSession*>(nativeSessionPtr), nullptr,
                        &size);
    if (size == 0) {
        jintArray jintArr = env->NewIntArray(0);
        return jintArr;
    }
    std::vector<int32_t> tidsVector(size);
    gAPH_getThreadIdsFn(reinterpret_cast<APerformanceHintSession*>(nativeSessionPtr),
                        tidsVector.data(), &size);
    jintArray jintArr = env->NewIntArray(size);
    if (jintArr == nullptr) {
        jniThrowException(env, "java/lang/OutOfMemoryError", nullptr);
        return nullptr;
    }
    jint* threadIds = env->GetIntArrayElements(jintArr, 0);
    for (int i = 0; i < size; ++i) {
        threadIds[i] = tidsVector[i];
    }
    env->ReleaseIntArrayElements(jintArr, threadIds, 0);
    return jintArr;
}

static const JNINativeMethod gPerformanceHintMethods[] = {
        {"nativeAcquireManager", "()J", (void*)nativeAcquireManager},
        {"nativeGetPreferredUpdateRateNanos", "(J)J", (void*)nativeGetPreferredUpdateRateNanos},
@@ -158,6 +214,8 @@ static const JNINativeMethod gPerformanceHintMethods[] = {
        {"nativeReportActualWorkDuration", "(JJ)V", (void*)nativeReportActualWorkDuration},
        {"nativeCloseSession", "(J)V", (void*)nativeCloseSession},
        {"nativeSendHint", "(JI)V", (void*)nativeSendHint},
        {"nativeSetThreads", "(J[I)V", (void*)nativeSetThreads},
        {"nativeGetThreadIds", "(J)[I", (void*)nativeGetThreadIds},
};

int register_android_os_PerformanceHintManager(JNIEnv* env) {
Loading