Adrian Roos | 4d18a2e | 2017-12-19 19:08:05 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 com.android.server.wm; |
| 18 | |
| 19 | import static android.view.SurfaceControl.HIDDEN; |
| 20 | |
Adrian Roos | 7af9d97 | 2018-11-30 15:26:27 +0100 | [diff] [blame] | 21 | import android.graphics.Point; |
Adrian Roos | 4d18a2e | 2017-12-19 19:08:05 +0100 | [diff] [blame] | 22 | import android.graphics.Rect; |
Riddle Hsu | 192fe76 | 2019-01-15 23:41:57 +0800 | [diff] [blame] | 23 | import android.os.Binder; |
| 24 | import android.os.Process; |
| 25 | import android.view.InputChannel; |
| 26 | import android.view.InputEventReceiver; |
| 27 | import android.view.InputWindowHandle; |
Adrian Roos | 4d18a2e | 2017-12-19 19:08:05 +0100 | [diff] [blame] | 28 | import android.view.SurfaceControl; |
Riddle Hsu | 192fe76 | 2019-01-15 23:41:57 +0800 | [diff] [blame] | 29 | import android.view.WindowManager; |
| 30 | |
| 31 | import com.android.server.UiThread; |
Adrian Roos | 4d18a2e | 2017-12-19 19:08:05 +0100 | [diff] [blame] | 32 | |
| 33 | import java.util.function.Supplier; |
| 34 | |
| 35 | /** |
| 36 | * Manages a set of {@link SurfaceControl}s to draw a black letterbox between an |
| 37 | * outer rect and an inner rect. |
| 38 | */ |
| 39 | public class Letterbox { |
| 40 | |
| 41 | private static final Rect EMPTY_RECT = new Rect(); |
Adrian Roos | 7af9d97 | 2018-11-30 15:26:27 +0100 | [diff] [blame] | 42 | private static final Point ZERO_POINT = new Point(0, 0); |
Adrian Roos | 4d18a2e | 2017-12-19 19:08:05 +0100 | [diff] [blame] | 43 | |
| 44 | private final Supplier<SurfaceControl.Builder> mFactory; |
| 45 | private final Rect mOuter = new Rect(); |
| 46 | private final Rect mInner = new Rect(); |
| 47 | private final LetterboxSurface mTop = new LetterboxSurface("top"); |
| 48 | private final LetterboxSurface mLeft = new LetterboxSurface("left"); |
| 49 | private final LetterboxSurface mBottom = new LetterboxSurface("bottom"); |
| 50 | private final LetterboxSurface mRight = new LetterboxSurface("right"); |
Riddle Hsu | 192fe76 | 2019-01-15 23:41:57 +0800 | [diff] [blame] | 51 | private final LetterboxSurface[] mSurfaces = { mLeft, mTop, mRight, mBottom }; |
Adrian Roos | 4d18a2e | 2017-12-19 19:08:05 +0100 | [diff] [blame] | 52 | |
| 53 | /** |
| 54 | * Constructs a Letterbox. |
| 55 | * |
| 56 | * @param surfaceControlFactory a factory for creating the managed {@link SurfaceControl}s |
| 57 | */ |
| 58 | public Letterbox(Supplier<SurfaceControl.Builder> surfaceControlFactory) { |
| 59 | mFactory = surfaceControlFactory; |
| 60 | } |
| 61 | |
| 62 | /** |
Adrian Roos | 23df3a3 | 2018-03-15 15:41:13 +0100 | [diff] [blame] | 63 | * Lays out the letterbox, such that the area between the outer and inner |
Adrian Roos | 4d18a2e | 2017-12-19 19:08:05 +0100 | [diff] [blame] | 64 | * frames will be covered by black color surfaces. |
| 65 | * |
Adrian Roos | 23df3a3 | 2018-03-15 15:41:13 +0100 | [diff] [blame] | 66 | * The caller must use {@link #applySurfaceChanges} to apply the new layout to the surface. |
Adrian Roos | 4d18a2e | 2017-12-19 19:08:05 +0100 | [diff] [blame] | 67 | * @param outer the outer frame of the letterbox (this frame will be black, except the area |
Adrian Roos | 7af9d97 | 2018-11-30 15:26:27 +0100 | [diff] [blame] | 68 | * that intersects with the {code inner} frame), in global coordinates |
| 69 | * @param inner the inner frame of the letterbox (this frame will be clear), in global |
| 70 | * coordinates |
| 71 | * @param surfaceOrigin the origin of the surface factory in global coordinates |
Adrian Roos | 4d18a2e | 2017-12-19 19:08:05 +0100 | [diff] [blame] | 72 | */ |
Adrian Roos | 7af9d97 | 2018-11-30 15:26:27 +0100 | [diff] [blame] | 73 | public void layout(Rect outer, Rect inner, Point surfaceOrigin) { |
Adrian Roos | 4d18a2e | 2017-12-19 19:08:05 +0100 | [diff] [blame] | 74 | mOuter.set(outer); |
| 75 | mInner.set(inner); |
| 76 | |
Adrian Roos | 73f21b1 | 2019-01-18 13:59:54 +0100 | [diff] [blame] | 77 | mTop.layout(outer.left, outer.top, inner.right, inner.top, surfaceOrigin); |
| 78 | mLeft.layout(outer.left, inner.top, inner.left, outer.bottom, surfaceOrigin); |
| 79 | mBottom.layout(inner.left, inner.bottom, outer.right, outer.bottom, surfaceOrigin); |
| 80 | mRight.layout(inner.right, outer.top, outer.right, inner.bottom, surfaceOrigin); |
Adrian Roos | 4d18a2e | 2017-12-19 19:08:05 +0100 | [diff] [blame] | 81 | } |
| 82 | |
Adrian Roos | 23df3a3 | 2018-03-15 15:41:13 +0100 | [diff] [blame] | 83 | |
Adrian Roos | 4d18a2e | 2017-12-19 19:08:05 +0100 | [diff] [blame] | 84 | /** |
Adrian Roos | 20e0789 | 2018-02-23 19:12:01 +0100 | [diff] [blame] | 85 | * Gets the insets between the outer and inner rects. |
| 86 | */ |
| 87 | public Rect getInsets() { |
| 88 | return new Rect( |
| 89 | mLeft.getWidth(), |
| 90 | mTop.getHeight(), |
| 91 | mRight.getWidth(), |
| 92 | mBottom.getHeight()); |
| 93 | } |
| 94 | |
| 95 | /** |
Adrian Roos | 23df3a3 | 2018-03-15 15:41:13 +0100 | [diff] [blame] | 96 | * Returns true if any part of the letterbox overlaps with the given {@code rect}. |
| 97 | */ |
| 98 | public boolean isOverlappingWith(Rect rect) { |
Riddle Hsu | 192fe76 | 2019-01-15 23:41:57 +0800 | [diff] [blame] | 99 | for (LetterboxSurface surface : mSurfaces) { |
| 100 | if (surface.isOverlappingWith(rect)) { |
| 101 | return true; |
| 102 | } |
| 103 | } |
| 104 | return false; |
Adrian Roos | 23df3a3 | 2018-03-15 15:41:13 +0100 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | /** |
Adrian Roos | 4d18a2e | 2017-12-19 19:08:05 +0100 | [diff] [blame] | 108 | * Hides the letterbox. |
| 109 | * |
Adrian Roos | 23df3a3 | 2018-03-15 15:41:13 +0100 | [diff] [blame] | 110 | * The caller must use {@link #applySurfaceChanges} to apply the new layout to the surface. |
Adrian Roos | 4d18a2e | 2017-12-19 19:08:05 +0100 | [diff] [blame] | 111 | */ |
Adrian Roos | 23df3a3 | 2018-03-15 15:41:13 +0100 | [diff] [blame] | 112 | public void hide() { |
Adrian Roos | 7af9d97 | 2018-11-30 15:26:27 +0100 | [diff] [blame] | 113 | layout(EMPTY_RECT, EMPTY_RECT, ZERO_POINT); |
Adrian Roos | 4d18a2e | 2017-12-19 19:08:05 +0100 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Destroys the managed {@link SurfaceControl}s. |
| 118 | */ |
| 119 | public void destroy() { |
| 120 | mOuter.setEmpty(); |
| 121 | mInner.setEmpty(); |
| 122 | |
Riddle Hsu | 192fe76 | 2019-01-15 23:41:57 +0800 | [diff] [blame] | 123 | for (LetterboxSurface surface : mSurfaces) { |
| 124 | surface.remove(); |
| 125 | } |
Adrian Roos | 4d18a2e | 2017-12-19 19:08:05 +0100 | [diff] [blame] | 126 | } |
| 127 | |
Adrian Roos | 23df3a3 | 2018-03-15 15:41:13 +0100 | [diff] [blame] | 128 | /** Returns whether a call to {@link #applySurfaceChanges} would change the surface. */ |
| 129 | public boolean needsApplySurfaceChanges() { |
Riddle Hsu | 192fe76 | 2019-01-15 23:41:57 +0800 | [diff] [blame] | 130 | for (LetterboxSurface surface : mSurfaces) { |
| 131 | if (surface.needsApplySurfaceChanges()) { |
| 132 | return true; |
| 133 | } |
| 134 | } |
| 135 | return false; |
Adrian Roos | 23df3a3 | 2018-03-15 15:41:13 +0100 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | public void applySurfaceChanges(SurfaceControl.Transaction t) { |
Riddle Hsu | 192fe76 | 2019-01-15 23:41:57 +0800 | [diff] [blame] | 139 | for (LetterboxSurface surface : mSurfaces) { |
| 140 | surface.applySurfaceChanges(t); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | /** Enables touches to slide into other neighboring surfaces. */ |
| 145 | void attachInput(WindowState win) { |
| 146 | for (LetterboxSurface surface : mSurfaces) { |
| 147 | surface.attachInput(win); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | void onMovedToDisplay(int displayId) { |
| 152 | for (LetterboxSurface surface : mSurfaces) { |
| 153 | if (surface.mInputInterceptor != null) { |
| 154 | surface.mInputInterceptor.mWindowHandle.displayId = displayId; |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | private static class InputInterceptor { |
| 160 | final InputChannel mServerChannel; |
| 161 | final InputChannel mClientChannel; |
| 162 | final InputWindowHandle mWindowHandle; |
| 163 | final InputEventReceiver mInputEventReceiver; |
| 164 | final WindowManagerService mWmService; |
| 165 | |
| 166 | InputInterceptor(String namePrefix, WindowState win) { |
| 167 | mWmService = win.mWmService; |
| 168 | final String name = namePrefix + (win.mAppToken != null ? win.mAppToken : win); |
| 169 | final InputChannel[] channels = InputChannel.openInputChannelPair(name); |
| 170 | mServerChannel = channels[0]; |
| 171 | mClientChannel = channels[1]; |
| 172 | mInputEventReceiver = new SimpleInputReceiver(mClientChannel); |
| 173 | |
| 174 | final Binder token = new Binder(); |
| 175 | mWmService.mInputManager.registerInputChannel(mServerChannel, token); |
| 176 | |
| 177 | mWindowHandle = new InputWindowHandle(null /* inputApplicationHandle */, |
| 178 | null /* clientWindow */, win.getDisplayId()); |
| 179 | mWindowHandle.name = name; |
| 180 | mWindowHandle.token = token; |
| 181 | mWindowHandle.layoutParamsFlags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
| 182 | | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
| 183 | | WindowManager.LayoutParams.FLAG_SPLIT_TOUCH |
| 184 | | WindowManager.LayoutParams.FLAG_SLIPPERY; |
| 185 | mWindowHandle.layoutParamsType = WindowManager.LayoutParams.TYPE_INPUT_CONSUMER; |
| 186 | mWindowHandle.dispatchingTimeoutNanos = |
| 187 | WindowManagerService.DEFAULT_INPUT_DISPATCHING_TIMEOUT_NANOS; |
| 188 | mWindowHandle.visible = true; |
| 189 | mWindowHandle.ownerPid = Process.myPid(); |
| 190 | mWindowHandle.ownerUid = Process.myUid(); |
| 191 | mWindowHandle.scaleFactor = 1.0f; |
| 192 | } |
| 193 | |
| 194 | void updateTouchableRegion(Rect frame) { |
| 195 | mWindowHandle.touchableRegion.set(frame); |
| 196 | mWindowHandle.touchableRegion.translate(-frame.left, -frame.top); |
| 197 | } |
| 198 | |
| 199 | void dispose() { |
| 200 | mWmService.mInputManager.unregisterInputChannel(mServerChannel); |
| 201 | mInputEventReceiver.dispose(); |
| 202 | mServerChannel.dispose(); |
| 203 | mClientChannel.dispose(); |
| 204 | } |
| 205 | |
| 206 | private static class SimpleInputReceiver extends InputEventReceiver { |
| 207 | SimpleInputReceiver(InputChannel inputChannel) { |
| 208 | super(inputChannel, UiThread.getHandler().getLooper()); |
| 209 | } |
| 210 | } |
Adrian Roos | 23df3a3 | 2018-03-15 15:41:13 +0100 | [diff] [blame] | 211 | } |
| 212 | |
Adrian Roos | 4d18a2e | 2017-12-19 19:08:05 +0100 | [diff] [blame] | 213 | private class LetterboxSurface { |
| 214 | |
| 215 | private final String mType; |
| 216 | private SurfaceControl mSurface; |
| 217 | |
Adrian Roos | 73f21b1 | 2019-01-18 13:59:54 +0100 | [diff] [blame] | 218 | private final Rect mSurfaceFrameRelative = new Rect(); |
| 219 | private final Rect mLayoutFrameGlobal = new Rect(); |
| 220 | private final Rect mLayoutFrameRelative = new Rect(); |
Adrian Roos | 4d18a2e | 2017-12-19 19:08:05 +0100 | [diff] [blame] | 221 | |
Riddle Hsu | 192fe76 | 2019-01-15 23:41:57 +0800 | [diff] [blame] | 222 | private InputInterceptor mInputInterceptor; |
| 223 | |
Adrian Roos | 4d18a2e | 2017-12-19 19:08:05 +0100 | [diff] [blame] | 224 | public LetterboxSurface(String type) { |
| 225 | mType = type; |
| 226 | } |
| 227 | |
Adrian Roos | 73f21b1 | 2019-01-18 13:59:54 +0100 | [diff] [blame] | 228 | public void layout(int left, int top, int right, int bottom, Point surfaceOrigin) { |
| 229 | mLayoutFrameGlobal.set(left, top, right, bottom); |
| 230 | mLayoutFrameRelative.set(mLayoutFrameGlobal); |
| 231 | mLayoutFrameRelative.offset(-surfaceOrigin.x, -surfaceOrigin.y); |
Adrian Roos | 4d18a2e | 2017-12-19 19:08:05 +0100 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | private void createSurface() { |
| 235 | mSurface = mFactory.get().setName("Letterbox - " + mType) |
Chavi Weingarten | 6ef9cc6 | 2019-02-07 16:28:45 +0000 | [diff] [blame] | 236 | .setFlags(HIDDEN).setColorLayer().build(); |
Adrian Roos | 4d18a2e | 2017-12-19 19:08:05 +0100 | [diff] [blame] | 237 | mSurface.setLayer(-1); |
| 238 | mSurface.setColor(new float[]{0, 0, 0}); |
Peiyong Lin | f4f0f64 | 2019-03-01 14:36:05 -0800 | [diff] [blame] | 239 | mSurface.setColorSpaceAgnostic(true); |
Adrian Roos | 4d18a2e | 2017-12-19 19:08:05 +0100 | [diff] [blame] | 240 | } |
| 241 | |
Riddle Hsu | 192fe76 | 2019-01-15 23:41:57 +0800 | [diff] [blame] | 242 | void attachInput(WindowState win) { |
| 243 | if (mInputInterceptor != null) { |
| 244 | mInputInterceptor.dispose(); |
| 245 | } |
| 246 | mInputInterceptor = new InputInterceptor("Letterbox_" + mType + "_", win); |
| 247 | } |
| 248 | |
Robert Carr | 5ea304d | 2019-02-04 16:04:55 -0800 | [diff] [blame] | 249 | public void remove() { |
Adrian Roos | 4d18a2e | 2017-12-19 19:08:05 +0100 | [diff] [blame] | 250 | if (mSurface != null) { |
Robert Carr | 5ea304d | 2019-02-04 16:04:55 -0800 | [diff] [blame] | 251 | mSurface.remove(); |
Adrian Roos | 4d18a2e | 2017-12-19 19:08:05 +0100 | [diff] [blame] | 252 | mSurface = null; |
| 253 | } |
Riddle Hsu | 192fe76 | 2019-01-15 23:41:57 +0800 | [diff] [blame] | 254 | if (mInputInterceptor != null) { |
| 255 | mInputInterceptor.dispose(); |
| 256 | mInputInterceptor = null; |
| 257 | } |
Adrian Roos | 4d18a2e | 2017-12-19 19:08:05 +0100 | [diff] [blame] | 258 | } |
Adrian Roos | 20e0789 | 2018-02-23 19:12:01 +0100 | [diff] [blame] | 259 | |
| 260 | public int getWidth() { |
Adrian Roos | 73f21b1 | 2019-01-18 13:59:54 +0100 | [diff] [blame] | 261 | return Math.max(0, mLayoutFrameGlobal.width()); |
Adrian Roos | 20e0789 | 2018-02-23 19:12:01 +0100 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | public int getHeight() { |
Adrian Roos | 73f21b1 | 2019-01-18 13:59:54 +0100 | [diff] [blame] | 265 | return Math.max(0, mLayoutFrameGlobal.height()); |
Adrian Roos | 23df3a3 | 2018-03-15 15:41:13 +0100 | [diff] [blame] | 266 | } |
| 267 | |
Adrian Roos | 73f21b1 | 2019-01-18 13:59:54 +0100 | [diff] [blame] | 268 | /** |
| 269 | * Returns if the given {@code rect} overlaps with this letterbox piece. |
| 270 | * @param rect the area to check for overlap in global coordinates |
| 271 | */ |
Adrian Roos | 23df3a3 | 2018-03-15 15:41:13 +0100 | [diff] [blame] | 272 | public boolean isOverlappingWith(Rect rect) { |
Adrian Roos | 73f21b1 | 2019-01-18 13:59:54 +0100 | [diff] [blame] | 273 | if (mLayoutFrameGlobal.isEmpty()) { |
Adrian Roos | 23df3a3 | 2018-03-15 15:41:13 +0100 | [diff] [blame] | 274 | return false; |
| 275 | } |
Adrian Roos | 73f21b1 | 2019-01-18 13:59:54 +0100 | [diff] [blame] | 276 | return Rect.intersects(rect, mLayoutFrameGlobal); |
Adrian Roos | 23df3a3 | 2018-03-15 15:41:13 +0100 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | public void applySurfaceChanges(SurfaceControl.Transaction t) { |
Adrian Roos | 73f21b1 | 2019-01-18 13:59:54 +0100 | [diff] [blame] | 280 | if (mSurfaceFrameRelative.equals(mLayoutFrameRelative)) { |
Adrian Roos | 23df3a3 | 2018-03-15 15:41:13 +0100 | [diff] [blame] | 281 | // Nothing changed. |
| 282 | return; |
| 283 | } |
Adrian Roos | 73f21b1 | 2019-01-18 13:59:54 +0100 | [diff] [blame] | 284 | mSurfaceFrameRelative.set(mLayoutFrameRelative); |
| 285 | if (!mSurfaceFrameRelative.isEmpty()) { |
Adrian Roos | 23df3a3 | 2018-03-15 15:41:13 +0100 | [diff] [blame] | 286 | if (mSurface == null) { |
| 287 | createSurface(); |
| 288 | } |
Adrian Roos | 73f21b1 | 2019-01-18 13:59:54 +0100 | [diff] [blame] | 289 | t.setPosition(mSurface, mSurfaceFrameRelative.left, mSurfaceFrameRelative.top); |
| 290 | t.setWindowCrop(mSurface, mSurfaceFrameRelative.width(), |
| 291 | mSurfaceFrameRelative.height()); |
Riddle Hsu | 192fe76 | 2019-01-15 23:41:57 +0800 | [diff] [blame] | 292 | if (mInputInterceptor != null) { |
| 293 | mInputInterceptor.updateTouchableRegion(mSurfaceFrameRelative); |
| 294 | t.setInputWindowInfo(mSurface, mInputInterceptor.mWindowHandle); |
| 295 | } |
Adrian Roos | 23df3a3 | 2018-03-15 15:41:13 +0100 | [diff] [blame] | 296 | t.show(mSurface); |
| 297 | } else if (mSurface != null) { |
| 298 | t.hide(mSurface); |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | public boolean needsApplySurfaceChanges() { |
Adrian Roos | 73f21b1 | 2019-01-18 13:59:54 +0100 | [diff] [blame] | 303 | return !mSurfaceFrameRelative.equals(mLayoutFrameRelative); |
Adrian Roos | 20e0789 | 2018-02-23 19:12:01 +0100 | [diff] [blame] | 304 | } |
Adrian Roos | 4d18a2e | 2017-12-19 19:08:05 +0100 | [diff] [blame] | 305 | } |
| 306 | } |