blob: ad92f81f4dde45eef9eb3c5a3aa45bff49da3d59 [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())
72 .build();
73
74 if (SHOW_TRANSACTIONS) {
75 Slog.i(TAG, " THUMBNAIL " + mSurfaceControl + ": CREATE");
76 }
77
78 // Transfer the thumbnail to the surface
79 Surface drawSurface = new Surface();
80 drawSurface.copyFrom(mSurfaceControl);
81 drawSurface.attachAndQueueBuffer(thumbnailHeader);
82 drawSurface.release();
83 t.show(mSurfaceControl);
84
85 // We parent the thumbnail to the task, and just place it on top of anything else in the
86 // task.
87 t.setLayer(mSurfaceControl, Integer.MAX_VALUE);
88 }
89
90 void startAnimation(Transaction t, Animation anim) {
Tony Mak64b8d562017-12-28 17:44:02 +000091 startAnimation(t, anim, null /* position */);
92 }
93
94 void startAnimation(Transaction t, Animation anim, Point position) {
Jorim Jaggi988f6682017-11-17 17:46:43 +010095 anim.restrictDuration(MAX_ANIMATION_DURATION);
96 anim.scaleCurrentDuration(mAppToken.mService.getTransitionAnimationScaleLocked());
chaviw23012112017-12-20 15:29:04 -080097 mSurfaceAnimator.startAnimation(t, new LocalAnimationAdapter(
Tony Mak64b8d562017-12-28 17:44:02 +000098 new WindowAnimationSpec(anim, position,
chaviw23012112017-12-20 15:29:04 -080099 mAppToken.mService.mAppTransition.canSkipFirstFrame()),
100 mAppToken.mService.mSurfaceAnimationRunner), false /* hidden */);
Jorim Jaggi988f6682017-11-17 17:46:43 +0100101 }
102
103 private void onAnimationFinished() {
104 }
105
106 void setShowing(Transaction pendingTransaction, boolean show) {
Jorim Jaggi2e3c31d2017-11-20 19:49:00 +0100107 // TODO: Not needed anymore once thumbnail is attached to the app.
Jorim Jaggi988f6682017-11-17 17:46:43 +0100108 if (show) {
109 pendingTransaction.show(mSurfaceControl);
110 } else {
111 pendingTransaction.hide(mSurfaceControl);
112 }
113 }
114
115 void destroy() {
116 mSurfaceAnimator.cancelAnimation();
117 mSurfaceControl.destroy();
118 }
119
Vishnu Nair04ab4392018-01-10 11:00:06 -0800120 /**
121 * Write to a protocol buffer output stream. Protocol buffer message definition is at {@link
Yi Jin6c6e9ca2018-03-20 16:53:35 -0700122 * com.android.server.wm.AppWindowThumbnailProto}.
Vishnu Nair04ab4392018-01-10 11:00:06 -0800123 *
124 * @param proto Stream to write the AppWindowThumbnail object to.
125 * @param fieldId Field Id of the AppWindowThumbnail as defined in the parent message.
126 * @hide
127 */
128 void writeToProto(ProtoOutputStream proto, long fieldId) {
129 final long token = proto.start(fieldId);
130 proto.write(WIDTH, mWidth);
131 proto.write(HEIGHT, mHeight);
132 mSurfaceAnimator.writeToProto(proto, SURFACE_ANIMATOR);
133 proto.end(token);
134 }
135
Jorim Jaggi988f6682017-11-17 17:46:43 +0100136 @Override
137 public Transaction getPendingTransaction() {
138 return mAppToken.getPendingTransaction();
139 }
140
141 @Override
142 public void commitPendingTransaction() {
143 mAppToken.commitPendingTransaction();
144 }
145
146 @Override
Jorim Jaggi988f6682017-11-17 17:46:43 +0100147 public void onAnimationLeashCreated(Transaction t, SurfaceControl leash) {
148 t.setLayer(leash, Integer.MAX_VALUE);
149 }
150
151 @Override
152 public void onAnimationLeashDestroyed(Transaction t) {
Jorim Jaggi2e3c31d2017-11-20 19:49:00 +0100153
154 // TODO: Once attached to app token, we don't need to hide it immediately if thumbnail
155 // became visible.
Jorim Jaggi988f6682017-11-17 17:46:43 +0100156 t.hide(mSurfaceControl);
157 }
158
159 @Override
160 public Builder makeAnimationLeash() {
Jorim Jaggi596a1992017-12-29 14:48:02 +0100161 return mAppToken.makeSurface();
Jorim Jaggi988f6682017-11-17 17:46:43 +0100162 }
163
164 @Override
165 public SurfaceControl getSurfaceControl() {
166 return mSurfaceControl;
167 }
168
169 @Override
Jorim Jaggi596a1992017-12-29 14:48:02 +0100170 public SurfaceControl getAnimationLeashParent() {
171 return mAppToken.getAppAnimationLayer();
172 }
173
174 @Override
Jorim Jaggi988f6682017-11-17 17:46:43 +0100175 public SurfaceControl getParentSurfaceControl() {
176 return mAppToken.getParentSurfaceControl();
177 }
178
179 @Override
180 public int getSurfaceWidth() {
181 return mWidth;
182 }
183
184 @Override
185 public int getSurfaceHeight() {
186 return mHeight;
187 }
188}