blob: bef974ae10817772e197c509e2071d130d39f123 [file] [log] [blame]
Jorim Jaggi988f6682017-11-17 17:46:43 +01001/*
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
17package com.android.server.wm;
18
19import static com.android.server.wm.WindowManagerDebugConfig.SHOW_TRANSACTIONS;
20import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
21import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
22import static com.android.server.wm.WindowManagerService.MAX_ANIMATION_DURATION;
Yi Jin6c6e9ca2018-03-20 16:53:35 -070023import static com.android.server.wm.AppWindowThumbnailProto.HEIGHT;
24import static com.android.server.wm.AppWindowThumbnailProto.SURFACE_ANIMATOR;
25import static com.android.server.wm.AppWindowThumbnailProto.WIDTH;
Jorim Jaggi988f6682017-11-17 17:46:43 +010026
27import android.graphics.GraphicBuffer;
28import android.graphics.PixelFormat;
29import android.graphics.Point;
30import android.os.Binder;
31import android.util.Slog;
Vishnu Nair04ab4392018-01-10 11:00:06 -080032import android.util.proto.ProtoOutputStream;
Jorim Jaggi988f6682017-11-17 17:46:43 +010033import android.view.Surface;
34import android.view.SurfaceControl;
35import android.view.SurfaceControl.Builder;
36import android.view.SurfaceControl.Transaction;
37import android.view.animation.Animation;
38
39import com.android.server.wm.SurfaceAnimator.Animatable;
40
41/**
42 * Represents a surface that is displayed over an {@link AppWindowToken}
43 */
44class 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 Weingartenb736e322018-02-23 00:27:54 +000056 mSurfaceAnimator = new SurfaceAnimator(this, this::onAnimationFinished, appToken.mService);
Jorim Jaggi988f6682017-11-17 17:46:43 +010057 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.chenff674ee2018-11-06 21:03:00 +080072 .setBufferLayer()
Jorim Jaggi988f6682017-11-17 17:46:43 +010073 .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 Mak64b8d562017-12-28 17:44:02 +000092 startAnimation(t, anim, null /* position */);
93 }
94
95 void startAnimation(Transaction t, Animation anim, Point position) {
Jorim Jaggi988f6682017-11-17 17:46:43 +010096 anim.restrictDuration(MAX_ANIMATION_DURATION);
97 anim.scaleCurrentDuration(mAppToken.mService.getTransitionAnimationScaleLocked());
chaviw23012112017-12-20 15:29:04 -080098 mSurfaceAnimator.startAnimation(t, new LocalAnimationAdapter(
Tony Mak64b8d562017-12-28 17:44:02 +000099 new WindowAnimationSpec(anim, position,
chaviw23012112017-12-20 15:29:04 -0800100 mAppToken.mService.mAppTransition.canSkipFirstFrame()),
101 mAppToken.mService.mSurfaceAnimationRunner), false /* hidden */);
Jorim Jaggi988f6682017-11-17 17:46:43 +0100102 }
103
104 private void onAnimationFinished() {
105 }
106
107 void setShowing(Transaction pendingTransaction, boolean show) {
Jorim Jaggi2e3c31d2017-11-20 19:49:00 +0100108 // TODO: Not needed anymore once thumbnail is attached to the app.
Jorim Jaggi988f6682017-11-17 17:46:43 +0100109 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 Nair04ab4392018-01-10 11:00:06 -0800121 /**
122 * Write to a protocol buffer output stream. Protocol buffer message definition is at {@link
Yi Jin6c6e9ca2018-03-20 16:53:35 -0700123 * com.android.server.wm.AppWindowThumbnailProto}.
Vishnu Nair04ab4392018-01-10 11:00:06 -0800124 *
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 Jaggi988f6682017-11-17 17:46:43 +0100137 @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 Jaggi988f6682017-11-17 17:46:43 +0100148 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 Jaggi2e3c31d2017-11-20 19:49:00 +0100154
155 // TODO: Once attached to app token, we don't need to hide it immediately if thumbnail
156 // became visible.
Jorim Jaggi988f6682017-11-17 17:46:43 +0100157 t.hide(mSurfaceControl);
158 }
159
160 @Override
161 public Builder makeAnimationLeash() {
Jorim Jaggi596a1992017-12-29 14:48:02 +0100162 return mAppToken.makeSurface();
Jorim Jaggi988f6682017-11-17 17:46:43 +0100163 }
164
165 @Override
166 public SurfaceControl getSurfaceControl() {
167 return mSurfaceControl;
168 }
169
170 @Override
Jorim Jaggi596a1992017-12-29 14:48:02 +0100171 public SurfaceControl getAnimationLeashParent() {
172 return mAppToken.getAppAnimationLayer();
173 }
174
175 @Override
Jorim Jaggi988f6682017-11-17 17:46:43 +0100176 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}