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

Commit fe33a3aa authored by Shuzhen Wang's avatar Shuzhen Wang
Browse files

Camera: Cache tag id to avoid repeatative lookup

Looking up tag id based on tag name for each key is potentially time
consuming. Instead, cache the id in the Key object so that only when the
first getTag() triggers a look-up. All subsequent calls doesn't do
look-up.

Bug: 144028609
Test: Run GCA and observe CameraMetadata CPU consumption
Change-Id: Iadd92e7e3b6cb4d610a6c9704ca2066291291f43
parent 6b7926d1
Loading
Loading
Loading
Loading
+40 −2
Original line number Diff line number Diff line
@@ -240,6 +240,11 @@ public class CameraMetadataNative implements Parcelable {
         *
         * <p>This value is looked up the first time, and cached subsequently.</p>
         *
         * <p>This function may be called without cacheTag() if this is not a vendor key.
         * If this is a vendor key, cacheTag() must be called first before getTag() can
         * be called. Otherwise, mVendorId could be default (Long.MAX_VALUE) and vendor
         * tag lookup could fail.</p>
         *
         * @return The tag numeric value corresponding to the string
         */
        @UnsupportedAppUsage
@@ -251,6 +256,27 @@ public class CameraMetadataNative implements Parcelable {
            return mTag;
        }

        /**
         * Whether this key's tag is cached.
         *
         * @hide
         */
        @UnsupportedAppUsage
        public final boolean hasTag() {
            return mHasTag;
        }

        /**
         * Cache this key's tag.
         *
         * @hide
         */
        @UnsupportedAppUsage
        public final void cacheTag(int tag) {
            mHasTag = true;
            mTag = tag;
        }

        /**
         * Get the raw class backing the type {@code T} for this key.
         *
@@ -523,7 +549,13 @@ public class CameraMetadataNative implements Parcelable {
    }

    private <T> T getBase(Key<T> key) {
        int tag = nativeGetTagFromKeyLocal(key.getName());
        int tag;
        if (key.hasTag()) {
            tag = key.getTag();
        } else {
            tag = nativeGetTagFromKeyLocal(key.getName());
            key.cacheTag(tag);
        }
        byte[] values = readValues(tag);
        if (values == null) {
            // If the key returns null, use the fallback key if exists.
@@ -1451,7 +1483,13 @@ public class CameraMetadataNative implements Parcelable {
    }

    private <T> void setBase(Key<T> key, T value) {
        int tag = nativeGetTagFromKeyLocal(key.getName());
        int tag;
        if (key.hasTag()) {
            tag = key.getTag();
        } else {
            tag = nativeGetTagFromKeyLocal(key.getName());
            key.cacheTag(tag);
        }
        if (value == null) {
            // Erase the entry
            writeValues(tag, /*src*/null);