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

Commit 9d431e1c authored by Robert Carr's avatar Robert Carr Committed by Tony Mak
Browse files

Enable views to be placed in windowless surfaces.

For purposes of parcelling SurfaceControl to embed view hierarchies across
processes. We also want these Surfaces to receive input, but we can't
let the clients call setInputWindowInfo directly (or we could have issues
with clients stealing focus, etc...) and so we provide a method
blessInputSurface which has the WM configure a surface for input with
a minimal and safe set of parameters.

Test: WindowlessWmTests
Bug: 111373437
Bug: 134365580
Change-Id: I45fde62ba9b810e783d62c4dd5442abd038734d5
parent 482c2c99
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -309,4 +309,10 @@ interface IWindowSession {
     * Called when the system gesture exclusion has changed.
     */
    void reportSystemGestureExclusionChanged(IWindow window, in List<Rect> exclusionRects);

    /**
    * Request the server to call setInputWindowInfo on a given Surface, and return
    * an input channel where the client can receive input.
    */
    void blessInputSurface(int displayId, in SurfaceControl surface, out InputChannel outInputChannel);
}
+9 −0
Original line number Diff line number Diff line
@@ -55,6 +55,7 @@ public final class InputChannel implements Parcelable {
    private static native InputChannel[] nativeOpenInputChannelPair(String name);

    private native void nativeDispose(boolean finalized);
    private native void nativeRelease();
    private native void nativeTransferTo(InputChannel other);
    private native void nativeReadFromParcel(Parcel parcel);
    private native void nativeWriteToParcel(Parcel parcel);
@@ -119,6 +120,14 @@ public final class InputChannel implements Parcelable {
        nativeDispose(false);
    }

    /**
     * Release the Java objects hold over the native InputChannel. If other references
     * still exist in native-land, then the channel may continue to exist.
     */
    public void release() {
        nativeRelease();
    }

    /**
     * Transfers ownership of the internal state of the input channel to another
     * instance and invalidates this instance.  This is used to pass an input channel
+5 −2
Original line number Diff line number Diff line
@@ -609,10 +609,13 @@ public final class ViewRootImpl implements ViewParent,
    }

    private String mTag = TAG;

    public ViewRootImpl(Context context, Display display) {
        this(context, display, WindowManagerGlobal.getWindowSession());
    }

    public ViewRootImpl(Context context, Display display, IWindowSession session) {
        mContext = context;
        mWindowSession = WindowManagerGlobal.getWindowSession();
        mWindowSession = session;
        mDisplay = display;
        mBasePackageName = context.getBasePackageName();
        mThread = Thread.currentThread();
+41 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.view;

import android.content.res.Resources;
import android.content.Context;
import android.view.SurfaceControl;
import android.view.View;

/**
 * Utility class for adding a view hierarchy to a SurfaceControl.
 *
 * See WindowlessWmTest for example usage.
 * @hide
 */
public class WindowlessViewRoot {
    ViewRootImpl mViewRoot;
    WindowlessWindowManager mWm;
    public WindowlessViewRoot(Context c, Display d, SurfaceControl rootSurface) {
        mWm = new WindowlessWindowManager(c.getResources().getConfiguration(), rootSurface);
        mViewRoot = new ViewRootImpl(c, d, mWm);
    }

    public void addView(View view, WindowManager.LayoutParams attrs) {
        mViewRoot.setView(view, attrs, null);
    }
}
+109 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.view;

import android.content.res.Configuration;
import android.graphics.Rect;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.MergedConfiguration;
import android.util.Log;
import android.view.IWindowSession;
import android.view.SurfaceControl;
import android.view.SurfaceSession;

import java.util.HashMap;

/**
* A simplistic implementation of IWindowSession. Rather than managing Surfaces
* as children of the display, it manages Surfaces as children of a given root.
*
* By parcelling the root surface, the app can offer another app content for embedding.
* @hide
*/
class WindowlessWindowManager extends IWindowSession.Default {
    private final static String TAG = "WindowlessWindowManager";

    /**
     * Used to store SurfaceControl we've built for clients to
     * reconfigure them if relayout is called.
     */
    final HashMap<IBinder, SurfaceControl> mScForWindow = new HashMap<IBinder, SurfaceControl>();
    final SurfaceSession mSurfaceSession = new SurfaceSession();
    final SurfaceControl mRootSurface;
    final Configuration mConfiguration;
    IWindowSession mRealWm;

    private int mForceHeight = -1;
    private int mForceWidth = -1;

    WindowlessWindowManager(Configuration c, SurfaceControl rootSurface) {
        mRootSurface = rootSurface;
        mConfiguration = new Configuration(c);
        mRealWm = WindowManagerGlobal.getWindowSession();
    }

    public int addToDisplay(IWindow window, int seq, WindowManager.LayoutParams attrs,
            int viewVisibility, int displayId, Rect outFrame, Rect outContentInsets,
            Rect outStableInsets, Rect outOutsets,
            DisplayCutout.ParcelableWrapper outDisplayCutout, InputChannel outInputChannel,
            InsetsState outInsetsState) {
        final SurfaceControl.Builder b = new SurfaceControl.Builder(mSurfaceSession)
            .setParent(mRootSurface)
            .setName(attrs.getTitle().toString());
        final SurfaceControl sc = b.build();
        synchronized (this) {
            mScForWindow.put(window.asBinder(), sc);
        }

        if ((attrs.inputFeatures &
                WindowManager.LayoutParams.INPUT_FEATURE_NO_INPUT_CHANNEL) == 0) {
            try {
                mRealWm.blessInputSurface(displayId, sc, outInputChannel);
            } catch (RemoteException e) {
                Log.e(TAG, "Failed to bless surface: " + e);
            }
        }

        return WindowManagerGlobal.ADD_OKAY | WindowManagerGlobal.ADD_FLAG_APP_VISIBLE;
    }

    @Override
    public int relayout(IWindow window, int seq, WindowManager.LayoutParams attrs,
            int requestedWidth, int requestedHeight, int viewFlags, int flags, long frameNumber,
            Rect outFrame, Rect outOverscanInsets, Rect outContentInsets, Rect outVisibleInsets,
            Rect outStableInsets, Rect outsets, Rect outBackdropFrame,
            DisplayCutout.ParcelableWrapper cutout, MergedConfiguration mergedConfiguration,
            SurfaceControl outSurfaceControl, InsetsState outInsetsState) {
        SurfaceControl sc = null;
        synchronized (this) {
            sc = mScForWindow.get(window.asBinder());
        }
        if (sc == null) {
            throw new IllegalArgumentException(
                    "Invalid window token (never added or removed already)");
        }
        SurfaceControl.Transaction t = new SurfaceControl.Transaction();
        t.show(sc).setBufferSize(sc, requestedWidth, requestedHeight).apply();
        outSurfaceControl.copyFrom(sc);
        outFrame.set(0, 0, requestedWidth, requestedHeight);

        mergedConfiguration.setConfiguration(mConfiguration, mConfiguration);

        return 0;
    }
}
Loading