blob: 487b52ca02b95dc461e8b70c5cb89498ebc32517 [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;
23
24import android.graphics.GraphicBuffer;
25import android.graphics.PixelFormat;
26import android.graphics.Point;
27import android.os.Binder;
28import android.util.Slog;
29import android.view.Surface;
30import android.view.SurfaceControl;
31import android.view.SurfaceControl.Builder;
32import android.view.SurfaceControl.Transaction;
33import android.view.animation.Animation;
34
35import com.android.server.wm.SurfaceAnimator.Animatable;
36
37/**
38 * Represents a surface that is displayed over an {@link AppWindowToken}
39 */
40class AppWindowThumbnail implements Animatable {
41
42 private static final String TAG = TAG_WITH_CLASS_NAME ? "AppWindowThumbnail" : TAG_WM;
43
44 private final AppWindowToken mAppToken;
45 private final SurfaceControl mSurfaceControl;
46 private final SurfaceAnimator mSurfaceAnimator;
47 private final int mWidth;
48 private final int mHeight;
49
50 AppWindowThumbnail(Transaction t, AppWindowToken appToken, GraphicBuffer thumbnailHeader) {
51 mAppToken = appToken;
Jorim Jaggi4130a682018-01-09 14:28:44 +010052 mSurfaceAnimator = new SurfaceAnimator(this, this::onAnimationFinished,
53 appToken.mService.mAnimator::addAfterPrepareSurfacesRunnable, appToken.mService);
Jorim Jaggi988f6682017-11-17 17:46:43 +010054 mWidth = thumbnailHeader.getWidth();
55 mHeight = thumbnailHeader.getHeight();
56
57 // Create a new surface for the thumbnail
58 WindowState window = appToken.findMainWindow();
59
60 // TODO: This should be attached as a child to the app token, once the thumbnail animations
61 // use relative coordinates. Once we start animating task we can also consider attaching
62 // this to the task.
63 mSurfaceControl = appToken.makeSurface()
64 .setName("thumbnail anim: " + appToken.toString())
65 .setSize(mWidth, mHeight)
66 .setFormat(PixelFormat.TRANSLUCENT)
67 .setMetadata(appToken.windowType,
68 window != null ? window.mOwnerUid : Binder.getCallingUid())
69 .build();
70
71 if (SHOW_TRANSACTIONS) {
72 Slog.i(TAG, " THUMBNAIL " + mSurfaceControl + ": CREATE");
73 }
74
75 // Transfer the thumbnail to the surface
76 Surface drawSurface = new Surface();
77 drawSurface.copyFrom(mSurfaceControl);
78 drawSurface.attachAndQueueBuffer(thumbnailHeader);
79 drawSurface.release();
80 t.show(mSurfaceControl);
81
82 // We parent the thumbnail to the task, and just place it on top of anything else in the
83 // task.
84 t.setLayer(mSurfaceControl, Integer.MAX_VALUE);
85 }
86
87 void startAnimation(Transaction t, Animation anim) {
Tony Mak64b8d562017-12-28 17:44:02 +000088 startAnimation(t, anim, null /* position */);
89 }
90
91 void startAnimation(Transaction t, Animation anim, Point position) {
Jorim Jaggi988f6682017-11-17 17:46:43 +010092 anim.restrictDuration(MAX_ANIMATION_DURATION);
93 anim.scaleCurrentDuration(mAppToken.mService.getTransitionAnimationScaleLocked());
chaviw23012112017-12-20 15:29:04 -080094 mSurfaceAnimator.startAnimation(t, new LocalAnimationAdapter(
Tony Mak64b8d562017-12-28 17:44:02 +000095 new WindowAnimationSpec(anim, position,
chaviw23012112017-12-20 15:29:04 -080096 mAppToken.mService.mAppTransition.canSkipFirstFrame()),
97 mAppToken.mService.mSurfaceAnimationRunner), false /* hidden */);
Jorim Jaggi988f6682017-11-17 17:46:43 +010098 }
99
100 private void onAnimationFinished() {
101 }
102
103 void setShowing(Transaction pendingTransaction, boolean show) {
Jorim Jaggi2e3c31d2017-11-20 19:49:00 +0100104 // TODO: Not needed anymore once thumbnail is attached to the app.
Jorim Jaggi988f6682017-11-17 17:46:43 +0100105 if (show) {
106 pendingTransaction.show(mSurfaceControl);
107 } else {
108 pendingTransaction.hide(mSurfaceControl);
109 }
110 }
111
112 void destroy() {
113 mSurfaceAnimator.cancelAnimation();
114 mSurfaceControl.destroy();
115 }
116
117 @Override
118 public Transaction getPendingTransaction() {
119 return mAppToken.getPendingTransaction();
120 }
121
122 @Override
123 public void commitPendingTransaction() {
124 mAppToken.commitPendingTransaction();
125 }
126
127 @Override
128 public void destroyAfterPendingTransaction(SurfaceControl surface) {
129 mAppToken.destroyAfterPendingTransaction(surface);
130 }
131
132 @Override
133 public void onAnimationLeashCreated(Transaction t, SurfaceControl leash) {
134 t.setLayer(leash, Integer.MAX_VALUE);
135 }
136
137 @Override
138 public void onAnimationLeashDestroyed(Transaction t) {
Jorim Jaggi2e3c31d2017-11-20 19:49:00 +0100139
140 // TODO: Once attached to app token, we don't need to hide it immediately if thumbnail
141 // became visible.
Jorim Jaggi988f6682017-11-17 17:46:43 +0100142 t.hide(mSurfaceControl);
143 }
144
145 @Override
146 public Builder makeAnimationLeash() {
Jorim Jaggi596a1992017-12-29 14:48:02 +0100147 return mAppToken.makeSurface();
Jorim Jaggi988f6682017-11-17 17:46:43 +0100148 }
149
150 @Override
151 public SurfaceControl getSurfaceControl() {
152 return mSurfaceControl;
153 }
154
155 @Override
Jorim Jaggi596a1992017-12-29 14:48:02 +0100156 public SurfaceControl getAnimationLeashParent() {
157 return mAppToken.getAppAnimationLayer();
158 }
159
160 @Override
Jorim Jaggi988f6682017-11-17 17:46:43 +0100161 public SurfaceControl getParentSurfaceControl() {
162 return mAppToken.getParentSurfaceControl();
163 }
164
165 @Override
166 public int getSurfaceWidth() {
167 return mWidth;
168 }
169
170 @Override
171 public int getSurfaceHeight() {
172 return mHeight;
173 }
174}