Robert Carr | 9d431e1 | 2018-12-17 13:11:48 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.view; |
| 18 | |
Vishnu Nair | 5cf25319 | 2019-11-07 15:33:20 -0800 | [diff] [blame] | 19 | import android.annotation.NonNull; |
| 20 | import android.annotation.Nullable; |
Robert Carr | 8833ae8 | 2019-09-16 13:20:02 -0700 | [diff] [blame] | 21 | import android.annotation.TestApi; |
Vishnu Nair | 5cf25319 | 2019-11-07 15:33:20 -0800 | [diff] [blame] | 22 | import android.content.Context; |
Robert Carr | 87f5d2c | 2020-01-19 17:27:00 -0800 | [diff] [blame] | 23 | import android.graphics.PixelFormat; |
Vishnu Nair | 5cf25319 | 2019-11-07 15:33:20 -0800 | [diff] [blame] | 24 | import android.os.IBinder; |
Robert Carr | 87f5d2c | 2020-01-19 17:27:00 -0800 | [diff] [blame] | 25 | import android.os.Parcel; |
| 26 | import android.os.Parcelable; |
Jackal Guo | ac234d6 | 2020-02-03 15:05:43 +0800 | [diff] [blame] | 27 | import android.view.accessibility.IAccessibilityEmbeddedConnection; |
Robert Carr | 8833ae8 | 2019-09-16 13:20:02 -0700 | [diff] [blame] | 28 | |
Robert Carr | 271d9c7 | 2020-03-12 12:39:24 -0700 | [diff] [blame] | 29 | import java.util.Objects; |
| 30 | |
Robert Carr | 9d431e1 | 2018-12-17 13:11:48 -0800 | [diff] [blame] | 31 | /** |
Robert Carr | 87f5d2c | 2020-01-19 17:27:00 -0800 | [diff] [blame] | 32 | * Utility class for adding a View hierarchy to a {@link SurfaceControl}. The View hierarchy |
| 33 | * will render in to a root SurfaceControl, and receive input based on the SurfaceControl's |
| 34 | * placement on-screen. The primary usage of this class is to embed a View hierarchy from |
| 35 | * one process in to another. After the SurfaceControlViewHost has been set up in the embedded |
| 36 | * content provider, we can send the {@link SurfaceControlViewHost.SurfacePackage} |
| 37 | * to the host process. The host process can then attach the hierarchy to a SurfaceView within |
| 38 | * its own by calling |
| 39 | * {@link SurfaceView#setChildSurfacePackage}. |
Robert Carr | 9d431e1 | 2018-12-17 13:11:48 -0800 | [diff] [blame] | 40 | */ |
Robert Carr | 59b1888 | 2019-12-18 00:38:40 -0800 | [diff] [blame] | 41 | public class SurfaceControlViewHost { |
Evan Rosky | cff7ebb | 2020-04-22 16:54:49 -0700 | [diff] [blame] | 42 | private final ViewRootImpl mViewRoot; |
Vishnu Nair | 5cf25319 | 2019-11-07 15:33:20 -0800 | [diff] [blame] | 43 | private WindowlessWindowManager mWm; |
Evan Rosky | 3f09bb3 | 2019-10-09 19:27:52 -0700 | [diff] [blame] | 44 | |
Robert Carr | 59b1888 | 2019-12-18 00:38:40 -0800 | [diff] [blame] | 45 | private SurfaceControl mSurfaceControl; |
Jackal Guo | ac234d6 | 2020-02-03 15:05:43 +0800 | [diff] [blame] | 46 | private IAccessibilityEmbeddedConnection mAccessibilityEmbeddedConnection; |
Robert Carr | 59b1888 | 2019-12-18 00:38:40 -0800 | [diff] [blame] | 47 | |
| 48 | /** |
Robert Carr | 87f5d2c | 2020-01-19 17:27:00 -0800 | [diff] [blame] | 49 | * Package encapsulating a Surface hierarchy which contains interactive view |
| 50 | * elements. It's expected to get this object from |
| 51 | * {@link SurfaceControlViewHost#getSurfacePackage} afterwards it can be embedded within |
| 52 | * a SurfaceView by calling {@link SurfaceView#setChildSurfacePackage}. |
Robert Carr | 20c2396 | 2020-04-10 15:23:42 -0700 | [diff] [blame] | 53 | * |
| 54 | * Note that each {@link SurfacePackage} must be released by calling |
| 55 | * {@link SurfacePackage#release}. However, if you use the recommended flow, |
| 56 | * the framework will automatically handle the lifetime for you. |
| 57 | * |
| 58 | * 1. When sending the package to the remote process, return it from an AIDL method |
| 59 | * or manually use FLAG_WRITE_RETURN_VALUE in writeToParcel. This will automatically |
| 60 | * release the package in the local process. |
| 61 | * 2. In the remote process, consume the package using SurfaceView. This way the |
| 62 | * SurfaceView will take over the lifetime and call {@link SurfacePackage#release} |
| 63 | * for the user. |
| 64 | * |
| 65 | * One final note: The {@link SurfacePackage} lifetime is totally de-coupled |
| 66 | * from the lifetime of the underlying {@link SurfaceControlViewHost}. Regardless |
| 67 | * of the lifetime of the package the user should still call |
| 68 | * {@link SurfaceControlViewHost#release} when finished. |
Robert Carr | 59b1888 | 2019-12-18 00:38:40 -0800 | [diff] [blame] | 69 | */ |
Robert Carr | 87f5d2c | 2020-01-19 17:27:00 -0800 | [diff] [blame] | 70 | public static final class SurfacePackage implements Parcelable { |
Robert Carr | d9c9c0c | 2020-03-17 11:31:55 -0700 | [diff] [blame] | 71 | private SurfaceControl mSurfaceControl; |
Jackal Guo | ac234d6 | 2020-02-03 15:05:43 +0800 | [diff] [blame] | 72 | private final IAccessibilityEmbeddedConnection mAccessibilityEmbeddedConnection; |
Robert Carr | 59b1888 | 2019-12-18 00:38:40 -0800 | [diff] [blame] | 73 | |
Jackal Guo | ac234d6 | 2020-02-03 15:05:43 +0800 | [diff] [blame] | 74 | SurfacePackage(SurfaceControl sc, IAccessibilityEmbeddedConnection connection) { |
Robert Carr | 59b1888 | 2019-12-18 00:38:40 -0800 | [diff] [blame] | 75 | mSurfaceControl = sc; |
Jackal Guo | ac234d6 | 2020-02-03 15:05:43 +0800 | [diff] [blame] | 76 | mAccessibilityEmbeddedConnection = connection; |
Robert Carr | 59b1888 | 2019-12-18 00:38:40 -0800 | [diff] [blame] | 77 | } |
| 78 | |
Robert Carr | 87f5d2c | 2020-01-19 17:27:00 -0800 | [diff] [blame] | 79 | private SurfacePackage(Parcel in) { |
| 80 | mSurfaceControl = new SurfaceControl(); |
| 81 | mSurfaceControl.readFromParcel(in); |
Jackal Guo | ac234d6 | 2020-02-03 15:05:43 +0800 | [diff] [blame] | 82 | mAccessibilityEmbeddedConnection = IAccessibilityEmbeddedConnection.Stub.asInterface( |
| 83 | in.readStrongBinder()); |
Robert Carr | 87f5d2c | 2020-01-19 17:27:00 -0800 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Use {@link SurfaceView#setChildSurfacePackage} or manually fix |
| 88 | * accessibility (see SurfaceView implementation). |
| 89 | * @hide |
| 90 | */ |
Robert Carr | 59b1888 | 2019-12-18 00:38:40 -0800 | [diff] [blame] | 91 | public @NonNull SurfaceControl getSurfaceControl() { |
| 92 | return mSurfaceControl; |
| 93 | } |
Robert Carr | 87f5d2c | 2020-01-19 17:27:00 -0800 | [diff] [blame] | 94 | |
Jackal Guo | ac234d6 | 2020-02-03 15:05:43 +0800 | [diff] [blame] | 95 | /** |
| 96 | * Gets an accessibility embedded connection interface for this SurfaceControlViewHost. |
| 97 | * |
| 98 | * @return {@link IAccessibilityEmbeddedConnection} interface. |
| 99 | * @hide |
| 100 | */ |
| 101 | public IAccessibilityEmbeddedConnection getAccessibilityEmbeddedConnection() { |
| 102 | return mAccessibilityEmbeddedConnection; |
| 103 | } |
| 104 | |
Robert Carr | 87f5d2c | 2020-01-19 17:27:00 -0800 | [diff] [blame] | 105 | @Override |
| 106 | public int describeContents() { |
| 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | @Override |
| 111 | public void writeToParcel(@NonNull Parcel out, int flags) { |
| 112 | mSurfaceControl.writeToParcel(out, flags); |
Jackal Guo | ac234d6 | 2020-02-03 15:05:43 +0800 | [diff] [blame] | 113 | out.writeStrongBinder(mAccessibilityEmbeddedConnection.asBinder()); |
Robert Carr | 87f5d2c | 2020-01-19 17:27:00 -0800 | [diff] [blame] | 114 | } |
| 115 | |
Robert Carr | d9c9c0c | 2020-03-17 11:31:55 -0700 | [diff] [blame] | 116 | /** |
Robert Carr | 5af7d62 | 2020-03-17 12:04:20 -0700 | [diff] [blame] | 117 | * Release the {@link SurfaceControl} associated with this package. |
| 118 | * It's not necessary to call this if you pass the package to |
| 119 | * {@link SurfaceView#setChildSurfacePackage} as {@link SurfaceView} will |
| 120 | * take ownership in that case. |
Robert Carr | d9c9c0c | 2020-03-17 11:31:55 -0700 | [diff] [blame] | 121 | */ |
| 122 | public void release() { |
| 123 | if (mSurfaceControl != null) { |
| 124 | mSurfaceControl.release(); |
| 125 | } |
| 126 | mSurfaceControl = null; |
| 127 | } |
| 128 | |
Robert Carr | 87f5d2c | 2020-01-19 17:27:00 -0800 | [diff] [blame] | 129 | public static final @NonNull Creator<SurfacePackage> CREATOR |
| 130 | = new Creator<SurfacePackage>() { |
| 131 | public SurfacePackage createFromParcel(Parcel in) { |
| 132 | return new SurfacePackage(in); |
| 133 | } |
| 134 | public SurfacePackage[] newArray(int size) { |
| 135 | return new SurfacePackage[size]; |
| 136 | } |
| 137 | }; |
Robert Carr | 59b1888 | 2019-12-18 00:38:40 -0800 | [diff] [blame] | 138 | } |
| 139 | |
Evan Rosky | 3f09bb3 | 2019-10-09 19:27:52 -0700 | [diff] [blame] | 140 | /** @hide */ |
Robert Carr | 59b1888 | 2019-12-18 00:38:40 -0800 | [diff] [blame] | 141 | public SurfaceControlViewHost(@NonNull Context c, @NonNull Display d, |
Evan Rosky | 3f09bb3 | 2019-10-09 19:27:52 -0700 | [diff] [blame] | 142 | @NonNull WindowlessWindowManager wwm) { |
Tony Huang | 89d580c | 2020-03-26 14:10:26 +0800 | [diff] [blame] | 143 | this(c, d, wwm, false /* useSfChoreographer */); |
| 144 | } |
| 145 | |
| 146 | /** @hide */ |
| 147 | public SurfaceControlViewHost(@NonNull Context c, @NonNull Display d, |
| 148 | @NonNull WindowlessWindowManager wwm, boolean useSfChoreographer) { |
Evan Rosky | 3f09bb3 | 2019-10-09 19:27:52 -0700 | [diff] [blame] | 149 | mWm = wwm; |
Tony Huang | 89d580c | 2020-03-26 14:10:26 +0800 | [diff] [blame] | 150 | mViewRoot = new ViewRootImpl(c, d, mWm, useSfChoreographer); |
Robert Carr | 6d16c8c | 2020-02-21 12:04:31 -0800 | [diff] [blame] | 151 | mViewRoot.forceDisableBLAST(); |
Jackal Guo | ac234d6 | 2020-02-03 15:05:43 +0800 | [diff] [blame] | 152 | mAccessibilityEmbeddedConnection = mViewRoot.getAccessibilityEmbeddedConnection(); |
Evan Rosky | 3f09bb3 | 2019-10-09 19:27:52 -0700 | [diff] [blame] | 153 | } |
| 154 | |
Robert Carr | 87f5d2c | 2020-01-19 17:27:00 -0800 | [diff] [blame] | 155 | /** |
| 156 | * Construct a new SurfaceControlViewHost. The root Surface will be |
| 157 | * allocated internally and is accessible via getSurfacePackage(). |
| 158 | * |
| 159 | * The {@param hostToken} parameter, primarily used for ANR reporting, |
| 160 | * must be obtained from whomever will be hosting the embedded hierarchy. |
| 161 | * It's accessible from {@link SurfaceView#getHostToken}. |
| 162 | * |
| 163 | * @param context The Context object for your activity or application. |
| 164 | * @param display The Display the hierarchy will be placed on. |
| 165 | * @param hostToken The host token, as discussed above. |
| 166 | */ |
| 167 | public SurfaceControlViewHost(@NonNull Context context, @NonNull Display display, |
| 168 | @Nullable IBinder hostToken) { |
Robert Carr | 59b1888 | 2019-12-18 00:38:40 -0800 | [diff] [blame] | 169 | mSurfaceControl = new SurfaceControl.Builder() |
| 170 | .setContainerLayer() |
| 171 | .setName("SurfaceControlViewHost") |
| 172 | .build(); |
Robert Carr | 87f5d2c | 2020-01-19 17:27:00 -0800 | [diff] [blame] | 173 | mWm = new WindowlessWindowManager(context.getResources().getConfiguration(), |
| 174 | mSurfaceControl, hostToken); |
| 175 | mViewRoot = new ViewRootImpl(context, display, mWm); |
Robert Carr | 6d16c8c | 2020-02-21 12:04:31 -0800 | [diff] [blame] | 176 | mViewRoot.forceDisableBLAST(); |
Jackal Guo | ac234d6 | 2020-02-03 15:05:43 +0800 | [diff] [blame] | 177 | mAccessibilityEmbeddedConnection = mViewRoot.getAccessibilityEmbeddedConnection(); |
Robert Carr | 9d431e1 | 2018-12-17 13:11:48 -0800 | [diff] [blame] | 178 | } |
| 179 | |
Robert Carr | 87f5d2c | 2020-01-19 17:27:00 -0800 | [diff] [blame] | 180 | /** |
| 181 | * Return a SurfacePackage for the root SurfaceControl of the embedded hierarchy. |
| 182 | * Rather than be directly reparented using {@link SurfaceControl.Transaction} this |
| 183 | * SurfacePackage should be passed to {@link SurfaceView#setChildSurfacePackage} |
| 184 | * which will not only reparent the Surface, but ensure the accessibility hierarchies |
| 185 | * are linked. |
| 186 | */ |
Robert Carr | 59b1888 | 2019-12-18 00:38:40 -0800 | [diff] [blame] | 187 | public @Nullable SurfacePackage getSurfacePackage() { |
Jackal Guo | ac234d6 | 2020-02-03 15:05:43 +0800 | [diff] [blame] | 188 | if (mSurfaceControl != null && mAccessibilityEmbeddedConnection != null) { |
| 189 | return new SurfacePackage(mSurfaceControl, mAccessibilityEmbeddedConnection); |
Robert Carr | 59b1888 | 2019-12-18 00:38:40 -0800 | [diff] [blame] | 190 | } else { |
| 191 | return null; |
| 192 | } |
| 193 | } |
| 194 | |
Robert Carr | 87f5d2c | 2020-01-19 17:27:00 -0800 | [diff] [blame] | 195 | /** |
| 196 | * @hide |
| 197 | */ |
Evan Rosky | 680377e | 2020-01-10 19:12:10 -0800 | [diff] [blame] | 198 | @TestApi |
Robert Carr | 271d9c7 | 2020-03-12 12:39:24 -0700 | [diff] [blame] | 199 | public void setView(@NonNull View view, @NonNull WindowManager.LayoutParams attrs) { |
| 200 | Objects.requireNonNull(view); |
Robert Carr | 9d431e1 | 2018-12-17 13:11:48 -0800 | [diff] [blame] | 201 | mViewRoot.setView(view, attrs, null); |
| 202 | } |
Robert Carr | df28946 | 2019-09-16 13:20:02 -0700 | [diff] [blame] | 203 | |
Robert Carr | 87f5d2c | 2020-01-19 17:27:00 -0800 | [diff] [blame] | 204 | /** |
| 205 | * Set the root view of the SurfaceControlViewHost. This view will render in to |
| 206 | * the SurfaceControl, and receive input based on the SurfaceControls positioning on |
| 207 | * screen. It will be laid as if it were in a window of the passed in width and height. |
| 208 | * |
| 209 | * @param view The View to add |
| 210 | * @param width The width to layout the View within, in pixels. |
| 211 | * @param height The height to layout the View within, in pixels. |
| 212 | */ |
Robert Carr | 271d9c7 | 2020-03-12 12:39:24 -0700 | [diff] [blame] | 213 | public void setView(@NonNull View view, int width, int height) { |
Robert Carr | 87f5d2c | 2020-01-19 17:27:00 -0800 | [diff] [blame] | 214 | final WindowManager.LayoutParams lp = |
| 215 | new WindowManager.LayoutParams(width, height, |
| 216 | WindowManager.LayoutParams.TYPE_APPLICATION, 0, PixelFormat.TRANSPARENT); |
Robert Carr | 4d5e630 | 2020-03-23 12:47:06 -0700 | [diff] [blame] | 217 | lp.flags |= WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED; |
Robert Carr | 271d9c7 | 2020-03-12 12:39:24 -0700 | [diff] [blame] | 218 | setView(view, lp); |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * @return The view passed to setView, or null if none has been passed. |
| 223 | */ |
| 224 | public @Nullable View getView() { |
| 225 | return mViewRoot.getView(); |
Robert Carr | 87f5d2c | 2020-01-19 17:27:00 -0800 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | /** |
Evan Rosky | cff7ebb | 2020-04-22 16:54:49 -0700 | [diff] [blame] | 229 | * @return the ViewRootImpl wrapped by this host. |
| 230 | * @hide |
| 231 | */ |
| 232 | public IWindow getWindowToken() { |
| 233 | return mViewRoot.mWindow; |
| 234 | } |
| 235 | |
| 236 | /** |
Evan Rosky | 1283728 | 2020-04-27 19:12:25 -0700 | [diff] [blame] | 237 | * @return the WindowlessWindowManager instance that this host is attached to. |
| 238 | * @hide |
| 239 | */ |
| 240 | public @NonNull WindowlessWindowManager getWindowlessWM() { |
| 241 | return mWm; |
| 242 | } |
| 243 | |
| 244 | /** |
Robert Carr | 87f5d2c | 2020-01-19 17:27:00 -0800 | [diff] [blame] | 245 | * @hide |
| 246 | */ |
Evan Rosky | 680377e | 2020-01-10 19:12:10 -0800 | [diff] [blame] | 247 | @TestApi |
Robert Carr | df28946 | 2019-09-16 13:20:02 -0700 | [diff] [blame] | 248 | public void relayout(WindowManager.LayoutParams attrs) { |
| 249 | mViewRoot.setLayoutParams(attrs, false); |
| 250 | mViewRoot.setReportNextDraw(); |
| 251 | mWm.setCompletionCallback(mViewRoot.mWindow.asBinder(), (SurfaceControl.Transaction t) -> { |
| 252 | t.apply(); |
| 253 | }); |
| 254 | } |
Vishnu Nair | 5cf25319 | 2019-11-07 15:33:20 -0800 | [diff] [blame] | 255 | |
Robert Carr | 87f5d2c | 2020-01-19 17:27:00 -0800 | [diff] [blame] | 256 | /** |
| 257 | * Modify the size of the root view. |
| 258 | * |
| 259 | * @param width Width in pixels |
| 260 | * @param height Height in pixels |
| 261 | */ |
| 262 | public void relayout(int width, int height) { |
| 263 | final WindowManager.LayoutParams lp = |
| 264 | new WindowManager.LayoutParams(width, height, |
| 265 | WindowManager.LayoutParams.TYPE_APPLICATION, 0, PixelFormat.TRANSPARENT); |
Robert Carr | 7fdf5d1 | 2020-02-27 12:11:16 -0800 | [diff] [blame] | 266 | relayout(lp); |
Robert Carr | 87f5d2c | 2020-01-19 17:27:00 -0800 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | /** |
| 270 | * Trigger the tear down of the embedded view hierarchy and release the SurfaceControl. |
| 271 | * This will result in onDispatchedFromWindow being dispatched to the embedded view hierarchy |
| 272 | * and render the object unusable. |
| 273 | */ |
| 274 | public void release() { |
Robert Carr | 8644050 | 2020-05-04 11:23:29 -0700 | [diff] [blame] | 275 | // ViewRoot will release mSurfaceControl for us. |
Robert Carr | bb7964d | 2020-03-18 14:24:38 -0700 | [diff] [blame] | 276 | mViewRoot.die(false /* immediate */); |
Vishnu Nair | 5cf25319 | 2019-11-07 15:33:20 -0800 | [diff] [blame] | 277 | } |
Evan Rosky | 3f09bb3 | 2019-10-09 19:27:52 -0700 | [diff] [blame] | 278 | |
| 279 | /** |
| 280 | * Tell this viewroot to clean itself up. |
| 281 | * @hide |
| 282 | */ |
| 283 | public void die() { |
| 284 | mViewRoot.die(false /* immediate */); |
| 285 | } |
Robert Carr | 9d431e1 | 2018-12-17 13:11:48 -0800 | [diff] [blame] | 286 | } |