Jorim Jaggi | 988f668 | 2017-11-17 17:46:43 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | |
Evan Rosky | 485df20 | 2018-12-06 14:11:12 -0800 | [diff] [blame] | 19 | import static android.view.SurfaceControl.METADATA_OWNER_UID; |
| 20 | import static android.view.SurfaceControl.METADATA_WINDOW_TYPE; |
| 21 | |
Vishnu Nair | e86bd98 | 2018-11-28 13:23:17 -0800 | [diff] [blame] | 22 | import static com.android.server.wm.AppWindowThumbnailProto.HEIGHT; |
| 23 | import static com.android.server.wm.AppWindowThumbnailProto.SURFACE_ANIMATOR; |
| 24 | import static com.android.server.wm.AppWindowThumbnailProto.WIDTH; |
Jorim Jaggi | 988f668 | 2017-11-17 17:46:43 +0100 | [diff] [blame] | 25 | import static com.android.server.wm.WindowManagerDebugConfig.SHOW_TRANSACTIONS; |
| 26 | import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME; |
| 27 | import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM; |
| 28 | import static com.android.server.wm.WindowManagerService.MAX_ANIMATION_DURATION; |
| 29 | |
| 30 | import android.graphics.GraphicBuffer; |
| 31 | import android.graphics.PixelFormat; |
| 32 | import android.graphics.Point; |
| 33 | import android.os.Binder; |
| 34 | import android.util.Slog; |
Vishnu Nair | 04ab439 | 2018-01-10 11:00:06 -0800 | [diff] [blame] | 35 | import android.util.proto.ProtoOutputStream; |
Jorim Jaggi | 988f668 | 2017-11-17 17:46:43 +0100 | [diff] [blame] | 36 | import android.view.Surface; |
| 37 | import android.view.SurfaceControl; |
| 38 | import android.view.SurfaceControl.Builder; |
| 39 | import android.view.SurfaceControl.Transaction; |
| 40 | import android.view.animation.Animation; |
| 41 | |
| 42 | import com.android.server.wm.SurfaceAnimator.Animatable; |
| 43 | |
| 44 | /** |
| 45 | * Represents a surface that is displayed over an {@link AppWindowToken} |
| 46 | */ |
| 47 | class AppWindowThumbnail implements Animatable { |
| 48 | |
| 49 | private static final String TAG = TAG_WITH_CLASS_NAME ? "AppWindowThumbnail" : TAG_WM; |
| 50 | |
| 51 | private final AppWindowToken mAppToken; |
| 52 | private final SurfaceControl mSurfaceControl; |
| 53 | private final SurfaceAnimator mSurfaceAnimator; |
| 54 | private final int mWidth; |
| 55 | private final int mHeight; |
Evan Rosky | 2289ba1 | 2018-11-19 18:28:18 -0800 | [diff] [blame] | 56 | private final boolean mRelative; |
Jorim Jaggi | 988f668 | 2017-11-17 17:46:43 +0100 | [diff] [blame] | 57 | |
| 58 | AppWindowThumbnail(Transaction t, AppWindowToken appToken, GraphicBuffer thumbnailHeader) { |
Evan Rosky | 2289ba1 | 2018-11-19 18:28:18 -0800 | [diff] [blame] | 59 | this(t, appToken, thumbnailHeader, false /* relative */); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * @param t Transaction to create the thumbnail in. |
| 64 | * @param appToken {@link AppWindowToken} to associate this thumbnail with. |
| 65 | * @param thumbnailHeader A thumbnail or placeholder for thumbnail to initialize with. |
| 66 | * @param relative Whether this thumbnail will be a child of appToken (and thus positioned |
| 67 | * relative to it) or not. |
| 68 | */ |
| 69 | AppWindowThumbnail(Transaction t, AppWindowToken appToken, GraphicBuffer thumbnailHeader, |
| 70 | boolean relative) { |
Jorim Jaggi | 988f668 | 2017-11-17 17:46:43 +0100 | [diff] [blame] | 71 | mAppToken = appToken; |
Evan Rosky | 2289ba1 | 2018-11-19 18:28:18 -0800 | [diff] [blame] | 72 | mRelative = relative; |
Wale Ogunwale | 8b19de9 | 2018-11-29 19:58:26 -0800 | [diff] [blame] | 73 | mSurfaceAnimator = |
| 74 | new SurfaceAnimator(this, this::onAnimationFinished, appToken.mWmService); |
Jorim Jaggi | 988f668 | 2017-11-17 17:46:43 +0100 | [diff] [blame] | 75 | mWidth = thumbnailHeader.getWidth(); |
| 76 | mHeight = thumbnailHeader.getHeight(); |
| 77 | |
| 78 | // Create a new surface for the thumbnail |
| 79 | WindowState window = appToken.findMainWindow(); |
| 80 | |
| 81 | // TODO: This should be attached as a child to the app token, once the thumbnail animations |
| 82 | // use relative coordinates. Once we start animating task we can also consider attaching |
| 83 | // this to the task. |
| 84 | mSurfaceControl = appToken.makeSurface() |
| 85 | .setName("thumbnail anim: " + appToken.toString()) |
Vishnu Nair | e86bd98 | 2018-11-28 13:23:17 -0800 | [diff] [blame] | 86 | .setBufferSize(mWidth, mHeight) |
Jorim Jaggi | 988f668 | 2017-11-17 17:46:43 +0100 | [diff] [blame] | 87 | .setFormat(PixelFormat.TRANSLUCENT) |
Evan Rosky | 485df20 | 2018-12-06 14:11:12 -0800 | [diff] [blame] | 88 | .setMetadata(METADATA_WINDOW_TYPE, appToken.windowType) |
| 89 | .setMetadata(METADATA_OWNER_UID, |
Jorim Jaggi | 988f668 | 2017-11-17 17:46:43 +0100 | [diff] [blame] | 90 | window != null ? window.mOwnerUid : Binder.getCallingUid()) |
| 91 | .build(); |
| 92 | |
| 93 | if (SHOW_TRANSACTIONS) { |
| 94 | Slog.i(TAG, " THUMBNAIL " + mSurfaceControl + ": CREATE"); |
| 95 | } |
| 96 | |
| 97 | // Transfer the thumbnail to the surface |
| 98 | Surface drawSurface = new Surface(); |
| 99 | drawSurface.copyFrom(mSurfaceControl); |
| 100 | drawSurface.attachAndQueueBuffer(thumbnailHeader); |
| 101 | drawSurface.release(); |
| 102 | t.show(mSurfaceControl); |
| 103 | |
| 104 | // We parent the thumbnail to the task, and just place it on top of anything else in the |
| 105 | // task. |
| 106 | t.setLayer(mSurfaceControl, Integer.MAX_VALUE); |
Evan Rosky | 2289ba1 | 2018-11-19 18:28:18 -0800 | [diff] [blame] | 107 | if (relative) { |
| 108 | t.reparent(mSurfaceControl, appToken.getSurfaceControl()); |
| 109 | } |
Jorim Jaggi | 988f668 | 2017-11-17 17:46:43 +0100 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | void startAnimation(Transaction t, Animation anim) { |
Tony Mak | 64b8d56 | 2017-12-28 17:44:02 +0000 | [diff] [blame] | 113 | startAnimation(t, anim, null /* position */); |
| 114 | } |
| 115 | |
| 116 | void startAnimation(Transaction t, Animation anim, Point position) { |
Jorim Jaggi | 988f668 | 2017-11-17 17:46:43 +0100 | [diff] [blame] | 117 | anim.restrictDuration(MAX_ANIMATION_DURATION); |
Wale Ogunwale | 8b19de9 | 2018-11-29 19:58:26 -0800 | [diff] [blame] | 118 | anim.scaleCurrentDuration(mAppToken.mWmService.getTransitionAnimationScaleLocked()); |
chaviw | 2301211 | 2017-12-20 15:29:04 -0800 | [diff] [blame] | 119 | mSurfaceAnimator.startAnimation(t, new LocalAnimationAdapter( |
Tony Mak | 64b8d56 | 2017-12-28 17:44:02 +0000 | [diff] [blame] | 120 | new WindowAnimationSpec(anim, position, |
lumark | 588a3e8 | 2018-07-20 18:53:54 +0800 | [diff] [blame] | 121 | mAppToken.getDisplayContent().mAppTransition.canSkipFirstFrame()), |
Wale Ogunwale | 8b19de9 | 2018-11-29 19:58:26 -0800 | [diff] [blame] | 122 | mAppToken.mWmService.mSurfaceAnimationRunner), false /* hidden */); |
Jorim Jaggi | 988f668 | 2017-11-17 17:46:43 +0100 | [diff] [blame] | 123 | } |
| 124 | |
Evan Rosky | 2289ba1 | 2018-11-19 18:28:18 -0800 | [diff] [blame] | 125 | /** |
| 126 | * Start animation with existing adapter. |
| 127 | */ |
| 128 | void startAnimation(Transaction t, AnimationAdapter anim, boolean hidden) { |
| 129 | mSurfaceAnimator.startAnimation(t, anim, hidden); |
| 130 | } |
| 131 | |
Jorim Jaggi | 988f668 | 2017-11-17 17:46:43 +0100 | [diff] [blame] | 132 | private void onAnimationFinished() { |
| 133 | } |
| 134 | |
| 135 | void setShowing(Transaction pendingTransaction, boolean show) { |
Jorim Jaggi | 2e3c31d | 2017-11-20 19:49:00 +0100 | [diff] [blame] | 136 | // TODO: Not needed anymore once thumbnail is attached to the app. |
Jorim Jaggi | 988f668 | 2017-11-17 17:46:43 +0100 | [diff] [blame] | 137 | if (show) { |
| 138 | pendingTransaction.show(mSurfaceControl); |
| 139 | } else { |
| 140 | pendingTransaction.hide(mSurfaceControl); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | void destroy() { |
| 145 | mSurfaceAnimator.cancelAnimation(); |
| 146 | mSurfaceControl.destroy(); |
| 147 | } |
| 148 | |
Vishnu Nair | 04ab439 | 2018-01-10 11:00:06 -0800 | [diff] [blame] | 149 | /** |
| 150 | * Write to a protocol buffer output stream. Protocol buffer message definition is at {@link |
Yi Jin | 6c6e9ca | 2018-03-20 16:53:35 -0700 | [diff] [blame] | 151 | * com.android.server.wm.AppWindowThumbnailProto}. |
Vishnu Nair | 04ab439 | 2018-01-10 11:00:06 -0800 | [diff] [blame] | 152 | * |
| 153 | * @param proto Stream to write the AppWindowThumbnail object to. |
| 154 | * @param fieldId Field Id of the AppWindowThumbnail as defined in the parent message. |
| 155 | * @hide |
| 156 | */ |
| 157 | void writeToProto(ProtoOutputStream proto, long fieldId) { |
| 158 | final long token = proto.start(fieldId); |
| 159 | proto.write(WIDTH, mWidth); |
| 160 | proto.write(HEIGHT, mHeight); |
| 161 | mSurfaceAnimator.writeToProto(proto, SURFACE_ANIMATOR); |
| 162 | proto.end(token); |
| 163 | } |
| 164 | |
Jorim Jaggi | 988f668 | 2017-11-17 17:46:43 +0100 | [diff] [blame] | 165 | @Override |
| 166 | public Transaction getPendingTransaction() { |
| 167 | return mAppToken.getPendingTransaction(); |
| 168 | } |
| 169 | |
| 170 | @Override |
| 171 | public void commitPendingTransaction() { |
| 172 | mAppToken.commitPendingTransaction(); |
| 173 | } |
| 174 | |
| 175 | @Override |
Jorim Jaggi | 988f668 | 2017-11-17 17:46:43 +0100 | [diff] [blame] | 176 | public void onAnimationLeashCreated(Transaction t, SurfaceControl leash) { |
| 177 | t.setLayer(leash, Integer.MAX_VALUE); |
Evan Rosky | 2289ba1 | 2018-11-19 18:28:18 -0800 | [diff] [blame] | 178 | if (mRelative) { |
| 179 | t.reparent(leash, mAppToken.getSurfaceControl()); |
| 180 | } |
Jorim Jaggi | 988f668 | 2017-11-17 17:46:43 +0100 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | @Override |
| 184 | public void onAnimationLeashDestroyed(Transaction t) { |
Jorim Jaggi | 2e3c31d | 2017-11-20 19:49:00 +0100 | [diff] [blame] | 185 | |
| 186 | // TODO: Once attached to app token, we don't need to hide it immediately if thumbnail |
| 187 | // became visible. |
Jorim Jaggi | 988f668 | 2017-11-17 17:46:43 +0100 | [diff] [blame] | 188 | t.hide(mSurfaceControl); |
| 189 | } |
| 190 | |
| 191 | @Override |
| 192 | public Builder makeAnimationLeash() { |
Jorim Jaggi | 596a199 | 2017-12-29 14:48:02 +0100 | [diff] [blame] | 193 | return mAppToken.makeSurface(); |
Jorim Jaggi | 988f668 | 2017-11-17 17:46:43 +0100 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | @Override |
| 197 | public SurfaceControl getSurfaceControl() { |
| 198 | return mSurfaceControl; |
| 199 | } |
| 200 | |
| 201 | @Override |
Jorim Jaggi | 596a199 | 2017-12-29 14:48:02 +0100 | [diff] [blame] | 202 | public SurfaceControl getAnimationLeashParent() { |
| 203 | return mAppToken.getAppAnimationLayer(); |
| 204 | } |
| 205 | |
| 206 | @Override |
Jorim Jaggi | 988f668 | 2017-11-17 17:46:43 +0100 | [diff] [blame] | 207 | public SurfaceControl getParentSurfaceControl() { |
| 208 | return mAppToken.getParentSurfaceControl(); |
| 209 | } |
| 210 | |
| 211 | @Override |
| 212 | public int getSurfaceWidth() { |
| 213 | return mWidth; |
| 214 | } |
| 215 | |
| 216 | @Override |
| 217 | public int getSurfaceHeight() { |
| 218 | return mHeight; |
| 219 | } |
| 220 | } |