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