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

Commit bed7ef2e authored by Tiger's avatar Tiger
Browse files

Store the source ID in InsetsFrameProvider

This CL stores the source ID but not the owner, the index, and the type
in InsetsFrameProvider. In this way, we write less info to the parcel.
Also, we won't use the BinderProxy to generate the hash code for the ID,
but will use the Binder object directly. Because the hash code obatined
from the BinderProxy might change.

Fix: 272230840
Fix: 279476217
Test: atest InsetsSourceTest WindowContainerTests
Change-Id: I0c6434ff19c33e4bb939dee76f49ccb3322735d2
parent 1e9f13da
Loading
Loading
Loading
Loading
+26 −33
Original line number Diff line number Diff line
@@ -62,9 +62,7 @@ public class InsetsFrameProvider implements Parcelable {
     */
    public static final int SOURCE_ARBITRARY_RECTANGLE = 3;

    private final IBinder mOwner;
    private final int mIndex;
    private final @InsetsType int mType;
    private final int mId;

    /**
     * The selection of the starting rectangle to be converted into source frame.
@@ -122,30 +120,30 @@ public class InsetsFrameProvider implements Parcelable {
     * @param type the {@link InsetsType}.
     * @see InsetsSource#createId(Object, int, int)
     */
    public InsetsFrameProvider(IBinder owner, @IntRange(from = 0, to = 2047) int index,
    public InsetsFrameProvider(Object owner, @IntRange(from = 0, to = 2047) int index,
            @InsetsType int type) {
        if (index < 0 || index >= 2048) {
            throw new IllegalArgumentException();
        mId = InsetsSource.createId(owner, index, type);
    }

        // This throws IllegalArgumentException if the type is not valid.
        WindowInsets.Type.indexOf(type);

        mOwner = owner;
        mIndex = index;
        mType = type;
    }

    public IBinder getOwner() {
        return mOwner;
    /**
     * Returns an unique integer which identifies the insets source.
     */
    public int getId() {
        return mId;
    }

    /**
     * Returns the index specified in {@link #InsetsFrameProvider(IBinder, int, int)}.
     */
    public int getIndex() {
        return mIndex;
        return InsetsSource.getIndex(mId);
    }

    /**
     * Returns the {@link InsetsType} specified in {@link #InsetsFrameProvider(IBinder, int, int)}.
     */
    public int getType() {
        return mType;
        return InsetsSource.getType(mId);
    }

    public InsetsFrameProvider setSource(int source) {
@@ -211,9 +209,9 @@ public class InsetsFrameProvider implements Parcelable {
    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("InsetsFrameProvider: {");
        sb.append("owner=").append(mOwner);
        sb.append(", index=").append(mIndex);
        sb.append(", type=").append(WindowInsets.Type.toString(mType));
        sb.append("id=#").append(Integer.toHexString(mId));
        sb.append(", index=").append(getIndex());
        sb.append(", type=").append(WindowInsets.Type.toString(getType()));
        sb.append(", source=").append(sourceToString(mSource));
        sb.append(", flags=[").append(InsetsSource.flagsToString(mFlags)).append("]");
        if (mInsetsSize != null) {
@@ -244,9 +242,7 @@ public class InsetsFrameProvider implements Parcelable {
    }

    public InsetsFrameProvider(Parcel in) {
        mOwner = in.readStrongBinder();
        mIndex = in.readInt();
        mType = in.readInt();
        mId = in.readInt();
        mSource = in.readInt();
        mFlags = in.readInt();
        mInsetsSize = in.readTypedObject(Insets.CREATOR);
@@ -256,9 +252,7 @@ public class InsetsFrameProvider implements Parcelable {

    @Override
    public void writeToParcel(Parcel out, int flags) {
        out.writeStrongBinder(mOwner);
        out.writeInt(mIndex);
        out.writeInt(mType);
        out.writeInt(mId);
        out.writeInt(mSource);
        out.writeInt(mFlags);
        out.writeTypedObject(mInsetsSize, flags);
@@ -267,7 +261,7 @@ public class InsetsFrameProvider implements Parcelable {
    }

    public boolean idEquals(InsetsFrameProvider o) {
        return Objects.equals(mOwner, o.mOwner) && mIndex == o.mIndex && mType == o.mType;
        return mId == o.mId;
    }

    @Override
@@ -279,8 +273,7 @@ public class InsetsFrameProvider implements Parcelable {
            return false;
        }
        final InsetsFrameProvider other = (InsetsFrameProvider) o;
        return Objects.equals(mOwner, other.mOwner) && mIndex == other.mIndex
                && mType == other.mType && mSource == other.mSource && mFlags == other.mFlags
        return mId == other.mId && mSource == other.mSource && mFlags == other.mFlags
                && Objects.equals(mInsetsSize, other.mInsetsSize)
                && Arrays.equals(mInsetsSizeOverrides, other.mInsetsSizeOverrides)
                && Objects.equals(mArbitraryRectangle, other.mArbitraryRectangle);
@@ -288,7 +281,7 @@ public class InsetsFrameProvider implements Parcelable {

    @Override
    public int hashCode() {
        return Objects.hash(mOwner, mIndex, mType, mSource, mFlags, mInsetsSize,
        return Objects.hash(mId, mSource, mFlags, mInsetsSize,
                Arrays.hashCode(mInsetsSizeOverrides), mArbitraryRectangle);
    }

@@ -319,7 +312,7 @@ public class InsetsFrameProvider implements Parcelable {

        protected InsetsSizeOverride(Parcel in) {
            mWindowType = in.readInt();
            mInsetsSize = in.readParcelable(null, Insets.class);
            mInsetsSize = in.readTypedObject(Insets.CREATOR);
        }

        public InsetsSizeOverride(int windowType, Insets insetsSize) {
@@ -354,7 +347,7 @@ public class InsetsFrameProvider implements Parcelable {
        @Override
        public void writeToParcel(Parcel out, int flags) {
            out.writeInt(mWindowType);
            out.writeParcelable(mInsetsSize, flags);
            out.writeTypedObject(mInsetsSize, flags);
        }

        @Override
+20 −2
Original line number Diff line number Diff line
@@ -271,7 +271,7 @@ public class InsetsSource implements Parcelable {
     * @param index An owner may have multiple sources with the same type. For example, the system
     *              server might have multiple display cutout sources. This is used to identify
     *              which one is which. The value must be in a range of [0, 2047].
     * @param type The {@link WindowInsets.Type.InsetsType type} of the source.
     * @param type The {@link InsetsType type} of the source.
     * @return a unique integer as the identifier.
     */
    public static int createId(Object owner, @IntRange(from = 0, to = 2047) int index,
@@ -282,11 +282,29 @@ public class InsetsSource implements Parcelable {
        // owner takes top 16 bits;
        // index takes 11 bits since the 6th bit;
        // type takes bottom 5 bits.
        return (((owner != null ? owner.hashCode() : 1) % (1 << 16)) << 16)
        return ((System.identityHashCode(owner) % (1 << 16)) << 16)
                + (index << 5)
                + WindowInsets.Type.indexOf(type);
    }

    /**
     * Gets the index from the ID.
     *
     * @see #createId(Object, int, int)
     */
    public static int getIndex(int id) {
        return (id % (1 << 16)) >> 5;
    }

    /**
     * Gets the {@link InsetsType} from the ID.
     *
     * @see #createId(Object, int, int)
     */
    public static int getType(int id) {
        return 1 << (id % 32);
    }

    public static String flagsToString(@Flags int flags) {
        final StringJoiner joiner = new StringJoiner(" ");
        if ((flags & FLAG_SUPPRESS_SCRIM) != 0) {
+20 −0
Original line number Diff line number Diff line
@@ -227,5 +227,25 @@ public class InsetsSourceTest {
        assertEquals(numTotalSources, sources.size());
    }

    @Test
    public void testGetIndex() {
        for (int index = 0; index < 2048; index++) {
            for (int type = FIRST; type <= LAST; type = type << 1) {
                final int id = InsetsSource.createId(null, index, type);
                assertEquals(index, InsetsSource.getIndex(id));
            }
        }
    }

    @Test
    public void testGetType() {
        for (int index = 0; index < 2048; index++) {
            for (int type = FIRST; type <= LAST; type = type << 1) {
                final int id = InsetsSource.createId(null, index, type);
                assertEquals(type, InsetsSource.getType(id));
            }
        }
    }

    // Parcel and equals already tested via InsetsStateTest
}
+3 −5
Original line number Diff line number Diff line
@@ -1095,11 +1095,9 @@ public class DisplayPolicy {
                } else {
                    overrideProviders = null;
                }
                final @InsetsType int type = provider.getType();
                final int id = InsetsSource.createId(
                        provider.getOwner(), provider.getIndex(), type);
                mDisplayContent.getInsetsStateController().getOrCreateSourceProvider(id, type)
                        .setWindowContainer(win, frameProvider, overrideProviders);
                mDisplayContent.getInsetsStateController().getOrCreateSourceProvider(
                        provider.getId(), provider.getType()).setWindowContainer(
                                win, frameProvider, overrideProviders);
                mInsetsSourceWindowsExceptIme.add(win);
            }
        }
+2 −5
Original line number Diff line number Diff line
@@ -311,16 +311,13 @@ class InsetsPolicy {
            state.removeSource(ID_IME);
        } else if (attrs.providedInsets != null) {
            for (InsetsFrameProvider provider : attrs.providedInsets) {
                final int id = InsetsSource.createId(
                        provider.getOwner(), provider.getIndex(), provider.getType());
                final @InsetsType int type = provider.getType();
                if ((type & WindowInsets.Type.systemBars()) == 0) {
                if ((provider.getType() & WindowInsets.Type.systemBars()) == 0) {
                    continue;
                }
                if (state == originalState) {
                    state = new InsetsState(state);
                }
                state.removeSource(id);
                state.removeSource(provider.getId());
            }
        }

Loading