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

Commit f32adf44 authored by John Reck's avatar John Reck
Browse files

Clean up ApplyStyle JNI

Bug: 32573798

Mark input uint32_t[] as const. Use Read-only JNI
array access for input as it's faster than critical access.

Use non-movable arrays for TypedArray so that the address can
be resolved and stored, avoiding the need to do JNI array
access for the output.

Indicies is always non-null, so remove the optional checks.

Eliminate unused return value.

Benchmark results:
twelveKeyInflate 4963us -> 4713us
simpleViewInflate 73us -> 60us

Test: Device boots, benchmarks show faster

Change-Id: Ic3bde5aee31407d8903913f97f2218daf074499a
parent 0730df73
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -815,9 +815,9 @@ public final class AssetManager implements AutoCloseable {

    /*package*/ static final int STYLE_DENSITY = 5;
    @FastNative
    /*package*/ native static final boolean applyStyle(long theme,
    /*package*/ native static final void applyStyle(long theme,
            int defStyleAttr, int defStyleRes, long xmlParser,
            int[] inAttrs, int[] outValues, int[] outIndices);
            int[] inAttrs, int length, long outValuesAddress, long outIndicesAddress);
    @FastNative
    /*package*/ native static final boolean resolveAttrs(long theme,
            int defStyleAttr, int defStyleRes, int[] inValues,
+1 −1
Original line number Diff line number Diff line
@@ -1126,7 +1126,7 @@ public class ResourcesImpl {
                final XmlBlock.Parser parser = (XmlBlock.Parser) set;
                AssetManager.applyStyle(mTheme, defStyleAttr, defStyleRes,
                        parser != null ? parser.mParseState : 0,
                        attrs, array.mData, array.mIndices);
                        attrs, attrs.length, array.mDataAddress, array.mIndicesAddress);
                array.mTheme = wrapper;
                array.mXml = parser;

+28 −24
Original line number Diff line number Diff line
@@ -30,6 +30,8 @@ import android.util.TypedValue;

import com.android.internal.util.XmlUtils;

import dalvik.system.VMRuntime;

import java.util.Arrays;

/**
@@ -44,30 +46,19 @@ import java.util.Arrays;
public class TypedArray {

    static TypedArray obtain(Resources res, int len) {
        final TypedArray attrs = res.mTypedArrayPool.acquire();
        if (attrs != null) {
            attrs.mLength = len;
            attrs.mRecycled = false;
        TypedArray attrs = res.mTypedArrayPool.acquire();
        if (attrs == null) {
            attrs = new TypedArray(res);
        }

        attrs.mRecycled = false;
        // Reset the assets, which may have changed due to configuration changes
        // or further resource loading.
        attrs.mAssets = res.getAssets();

            final int fullLen = len * AssetManager.STYLE_NUM_ENTRIES;
            if (attrs.mData.length >= fullLen) {
                return attrs;
            }

            attrs.mData = new int[fullLen];
            attrs.mIndices = new int[1 + len];
        attrs.resize(len);
        return attrs;
    }

        return new TypedArray(res,
                new int[len*AssetManager.STYLE_NUM_ENTRIES],
                new int[1+len], len);
    }

    private final Resources mResources;
    private final DisplayMetrics mMetrics;
    private AssetManager mAssets;
@@ -77,10 +68,25 @@ public class TypedArray {
    /*package*/ XmlBlock.Parser mXml;
    /*package*/ Resources.Theme mTheme;
    /*package*/ int[] mData;
    /*package*/ long mDataAddress;
    /*package*/ int[] mIndices;
    /*package*/ long mIndicesAddress;
    /*package*/ int mLength;
    /*package*/ TypedValue mValue = new TypedValue();

    private void resize(int len) {
        mLength = len;
        final int dataLen = len * AssetManager.STYLE_NUM_ENTRIES;
        final int indicesLen = len + 1;
        final VMRuntime runtime = VMRuntime.getRuntime();
        if (mData == null || mData.length < dataLen) {
            mData = (int[]) runtime.newNonMovableArray(int.class, dataLen);
            mDataAddress = runtime.addressOf(mData);
            mIndices = (int[]) runtime.newNonMovableArray(int.class, indicesLen);
            mIndicesAddress = runtime.addressOf(mIndices);
        }
    }

    /**
     * Returns the number of values in this array.
     *
@@ -1217,13 +1223,11 @@ public class TypedArray {
        return mAssets.getPooledStringForCookie(cookie, data[index+AssetManager.STYLE_DATA]);
    }

    /*package*/ TypedArray(Resources resources, int[] data, int[] indices, int len) {
    /** @hide */
    protected TypedArray(Resources resources) {
        mResources = resources;
        mMetrics = mResources.getDisplayMetrics();
        mAssets = mResources.getAssets();
        mData = data;
        mIndices = indices;
        mLength = len;
    }

    @Override
+10 −60
Original line number Diff line number Diff line
@@ -1179,67 +1179,17 @@ static jboolean android_content_AssetManager_resolveAttrs(JNIEnv* env, jobject c
    return result ? JNI_TRUE : JNI_FALSE;
}

static jboolean android_content_AssetManager_applyStyle(JNIEnv* env, jobject clazz,
                                                        jlong themeToken,
                                                        jint defStyleAttr,
                                                        jint defStyleRes,
                                                        jlong xmlParserToken,
                                                        jintArray attrs,
                                                        jintArray outValues,
                                                        jintArray outIndices)
{
    if (themeToken == 0) {
        jniThrowNullPointerException(env, "theme token");
        return JNI_FALSE;
    }
    if (attrs == NULL) {
        jniThrowNullPointerException(env, "attrs");
        return JNI_FALSE;
    }
    if (outValues == NULL) {
        jniThrowNullPointerException(env, "out values");
        return JNI_FALSE;
    }

    const jsize NI = env->GetArrayLength(attrs);
    const jsize NV = env->GetArrayLength(outValues);
    if (NV < (NI*STYLE_NUM_ENTRIES)) {
        jniThrowException(env, "java/lang/IndexOutOfBoundsException", "out values too small");
        return JNI_FALSE;
    }

    jint* src = (jint*)env->GetPrimitiveArrayCritical(attrs, 0);
    if (src == NULL) {
        return JNI_FALSE;
    }

    jint* baseDest = (jint*)env->GetPrimitiveArrayCritical(outValues, 0);
    if (baseDest == NULL) {
        env->ReleasePrimitiveArrayCritical(attrs, src, 0);
        return JNI_FALSE;
    }

    jint* indices = NULL;
    if (outIndices != NULL) {
        if (env->GetArrayLength(outIndices) > NI) {
            indices = (jint*)env->GetPrimitiveArrayCritical(outIndices, 0);
        }
    }

static void android_content_AssetManager_applyStyle(JNIEnv* env, jobject, jlong themeToken,
        jint defStyleAttr, jint defStyleRes, jlong xmlParserToken, jintArray attrsObj, jint length,
        jlong outValuesAddress, jlong outIndicesAddress) {
    jint* attrs = env->GetIntArrayElements(attrsObj, 0);
    ResTable::Theme* theme = reinterpret_cast<ResTable::Theme*>(themeToken);
    ResXMLParser* xmlParser = reinterpret_cast<ResXMLParser*>(xmlParserToken);
    bool result = ApplyStyle(theme, xmlParser,
                             defStyleAttr, defStyleRes,
                             (uint32_t*) src, NI,
                             (uint32_t*) baseDest,
                             (uint32_t*) indices);

    if (indices != NULL) {
        env->ReleasePrimitiveArrayCritical(outIndices, indices, 0);
    }
    env->ReleasePrimitiveArrayCritical(outValues, baseDest, 0);
    env->ReleasePrimitiveArrayCritical(attrs, src, 0);
    return result ? JNI_TRUE : JNI_FALSE;
    uint32_t* outValues = reinterpret_cast<uint32_t*>(static_cast<uintptr_t>(outValuesAddress));
    uint32_t* outIndices = reinterpret_cast<uint32_t*>(static_cast<uintptr_t>(outIndicesAddress));
    ApplyStyle(theme, xmlParser, defStyleAttr, defStyleRes,
            reinterpret_cast<const uint32_t*>(attrs), length, outValues, outIndices);
    env->ReleaseIntArrayElements(attrsObj, attrs, JNI_ABORT);
}

static jboolean android_content_AssetManager_retrieveAttributes(JNIEnv* env, jobject clazz,
@@ -1795,7 +1745,7 @@ static const JNINativeMethod gAssetManagerMethods[] = {
    { "dumpTheme", "(JILjava/lang/String;Ljava/lang/String;)V",
        (void*) android_content_AssetManager_dumpTheme },
    // @FastNative
    { "applyStyle","(JIIJ[I[I[I)Z",
    { "applyStyle","(JIIJ[IIJJ)V",
        (void*) android_content_AssetManager_applyStyle },
    // @FastNative
    { "resolveAttrs","(JII[I[I[I[I)Z",
+5 −8
Original line number Diff line number Diff line
@@ -193,9 +193,9 @@ bool ResolveAttrs(ResTable::Theme* theme, uint32_t def_style_attr, uint32_t def_
  return true;
}

bool ApplyStyle(ResTable::Theme* theme, ResXMLParser* xml_parser, uint32_t def_style_attr,
                uint32_t def_style_res, uint32_t* attrs, size_t attrs_length, uint32_t* out_values,
                uint32_t* out_indices) {
void ApplyStyle(ResTable::Theme* theme, ResXMLParser* xml_parser, uint32_t def_style_attr,
                uint32_t def_style_res, const uint32_t* attrs, size_t attrs_length,
                uint32_t* out_values, uint32_t* out_indices) {
  if (kDebugStyles) {
    ALOGI("APPLY STYLE: theme=0x%p defStyleAttr=0x%x defStyleRes=0x%x xml=0x%p", theme,
          def_style_attr, def_style_res, xml_parser);
@@ -376,7 +376,7 @@ bool ApplyStyle(ResTable::Theme* theme, ResXMLParser* xml_parser, uint32_t def_s
    out_values[STYLE_CHANGING_CONFIGURATIONS] = type_set_flags;
    out_values[STYLE_DENSITY] = config.density;

    if (out_indices != NULL && value.dataType != Res_value::TYPE_NULL) {
    if (value.dataType != Res_value::TYPE_NULL) {
      indices_idx++;
      out_indices[indices_idx] = ii;
    }
@@ -386,11 +386,8 @@ bool ApplyStyle(ResTable::Theme* theme, ResXMLParser* xml_parser, uint32_t def_s

  res.unlock();

  if (out_indices != NULL) {
  out_indices[0] = indices_idx;
}
  return true;
}

bool RetrieveAttributes(const ResTable* res, ResXMLParser* xml_parser, uint32_t* attrs,
                        size_t attrs_length, uint32_t* out_values, uint32_t* out_indices) {
Loading