Debian Codegraph @

libs/WindowManager/Shell/src/com/android/wm/shell/pip/PipContentOverlay.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2022 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.wm.shell.pip;
18
19 import android.content.Context;
20 import android.content.res.TypedArray;
21 import android.graphics.Color;
22 import android.graphics.Rect;
23 import android.view.SurfaceControl;
24 import android.view.SurfaceSession;
25 import android.window.TaskSnapshot;
26
27 /**
28 * Represents the content overlay used during the entering PiP animation.
29 */
30 public abstract class PipContentOverlay {
31 protected SurfaceControl mLeash;
32
33 /** Attaches the internal {@link #mLeash} to the given parent leash. */
34 public abstract void attach(SurfaceControl.Transaction tx, SurfaceControl parentLeash);
35
36 /** Detaches the internal {@link #mLeash} from its parent by removing itself. */
37 public void detach(SurfaceControl.Transaction tx) {
38 if (mLeash != null && mLeash.isValid()) {
39 tx.remove(mLeash);
40 tx.apply();
41 }
42 }
43
44 /**
45 * Animates the internal {@link #mLeash} by a given fraction.
46 * @param atomicTx {@link SurfaceControl.Transaction} to operate, you should not explicitly
47 * call apply on this transaction, it should be applied on the caller side.
48 * @param fraction progress of the animation ranged from 0f to 1f.
49 */
50 public abstract void onAnimationUpdate(SurfaceControl.Transaction atomicTx, float fraction);
51
52 /**
53 * Callback when reaches the end of animation on the internal {@link #mLeash}.
54 * @param atomicTx {@link SurfaceControl.Transaction} to operate, you should not explicitly
55 * call apply on this transaction, it should be applied on the caller side.
56 * @param destinationBounds {@link Rect} of the final bounds.
57 */
58 public abstract void onAnimationEnd(SurfaceControl.Transaction atomicTx,
59 Rect destinationBounds);
60
61 /** A {@link PipContentOverlay} uses solid color. */
62 public static final class PipColorOverlay extends PipContentOverlay {
63 private final Context mContext;
64
65 public PipColorOverlay(Context context) {
66 mContext = context;
67 mLeash = new SurfaceControl.Builder(new SurfaceSession())
68 .setCallsite("PipAnimation")
69 .setName(PipColorOverlay.class.getSimpleName())
70 .setColorLayer()
71 .build();
72 }
73
74 @Override
75 public void attach(SurfaceControl.Transaction tx, SurfaceControl parentLeash) {
76 tx.show(mLeash);
77 tx.setLayer(mLeash, Integer.MAX_VALUE);
78 tx.setColor(mLeash, getContentOverlayColor(mContext));
79 tx.setAlpha(mLeash, 0f);
80 tx.reparent(mLeash, parentLeash);
81 tx.apply();
82 }
83
84 @Override
85 public void onAnimationUpdate(SurfaceControl.Transaction atomicTx, float fraction) {
86 atomicTx.setAlpha(mLeash, fraction < 0.5f ? 0 : (fraction - 0.5f) * 2);
87 }
88
89 @Override
90 public void onAnimationEnd(SurfaceControl.Transaction atomicTx, Rect destinationBounds) {
91 // Do nothing. Color overlay should be fully opaque by now.
92 }
93
94 private float[] getContentOverlayColor(Context context) {
95 final TypedArray ta = context.obtainStyledAttributes(new int[] {
96 android.R.attr.colorBackground });
97 try {
98 int colorAccent = ta.getColor(0, 0);
99 return new float[] {
100 Color.red(colorAccent) / 255f,
101 Color.green(colorAccent) / 255f,
102 Color.blue(colorAccent) / 255f };
103 } finally {
104 ta.recycle();
105 }
106 }
107 }
108
109 /** A {@link PipContentOverlay} uses {@link TaskSnapshot}. */
110 public static final class PipSnapshotOverlay extends PipContentOverlay {
111 private final TaskSnapshot mSnapshot;
112 private final Rect mSourceRectHint;
113
114 public PipSnapshotOverlay(TaskSnapshot snapshot, Rect sourceRectHint) {
115 mSnapshot = snapshot;
116 mSourceRectHint = new Rect(sourceRectHint);
117 mLeash = new SurfaceControl.Builder(new SurfaceSession())
118 .setCallsite("PipAnimation")
119 .setName(PipSnapshotOverlay.class.getSimpleName())
120 .build();
121 }
122
123 @Override
124 public void attach(SurfaceControl.Transaction tx, SurfaceControl parentLeash) {
125 final float taskSnapshotScaleX = (float) mSnapshot.getTaskSize().x
126 / mSnapshot.getHardwareBuffer().getWidth();
127 final float taskSnapshotScaleY = (float) mSnapshot.getTaskSize().y
128 / mSnapshot.getHardwareBuffer().getHeight();
129 tx.show(mLeash);
130 tx.setLayer(mLeash, Integer.MAX_VALUE);
131 tx.setBuffer(mLeash, mSnapshot.getHardwareBuffer());
132 // Relocate the content to parentLeash's coordinates.
133 tx.setPosition(mLeash, -mSourceRectHint.left, -mSourceRectHint.top);
134 tx.setScale(mLeash, taskSnapshotScaleX, taskSnapshotScaleY);
135 tx.reparent(mLeash, parentLeash);
136 tx.apply();
137 }
138
139 @Override
140 public void onAnimationUpdate(SurfaceControl.Transaction atomicTx, float fraction) {
141 // Do nothing. Keep the snapshot till animation ends.
142 }
143
144 @Override
145 public void onAnimationEnd(SurfaceControl.Transaction atomicTx, Rect destinationBounds) {
146 atomicTx.remove(mLeash);
147 }
148 }
149 }