summaryrefslogtreecommitdiff
path: root/services/core/java/com/android/server/wm/SurfaceControlWithBackground.java
blob: d3c2c80e6ec17b029f7299a5605162b0408b8f3d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License
 */

package com.android.server.wm;

import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.graphics.Region;
import android.os.IBinder;
import android.os.Parcel;
import android.view.Surface;
import android.view.Surface.OutOfResourcesException;
import android.view.SurfaceControl;
import android.view.SurfaceSession;

/**
 * SurfaceControl extension that has background sized to match its container.
 */
class SurfaceControlWithBackground extends SurfaceControl {
    // SurfaceControl that holds the background behind opaque letterboxed app windows.
    private SurfaceControl mBackgroundControl;

    // Flags that define whether the background should be shown.
    private boolean mOpaque;
    private boolean mVisible;

    // Way to communicate with corresponding window.
    private WindowSurfaceController mWindowSurfaceController;

    // Rect to hold task bounds when computing metrics for background.
    private Rect mTmpContainerRect = new Rect();

    // Last metrics applied to the main SurfaceControl.
    private float mLastWidth, mLastHeight;
    private float mLastDsDx = 1, mLastDsDy = 1;
    private float mLastX, mLastY;

    public SurfaceControlWithBackground(SurfaceControlWithBackground other) {
        super(other);
        mBackgroundControl = other.mBackgroundControl;
        mOpaque = other.mOpaque;
        mVisible = other.mVisible;
        mWindowSurfaceController = other.mWindowSurfaceController;
    }

    public SurfaceControlWithBackground(SurfaceSession s, String name, int w, int h, int format,
            int flags, int windowType, int ownerUid,
            WindowSurfaceController windowSurfaceController) throws OutOfResourcesException {
        super(s, name, w, h, format, flags, windowType, ownerUid);

        // We should only show background when the window is letterboxed in a task.
        if (!windowSurfaceController.mAnimator.mWin.isLetterboxedAppWindow()) {
            return;
        }
        mWindowSurfaceController = windowSurfaceController;
        mLastWidth = w;
        mLastHeight = h;
        mOpaque = (flags & SurfaceControl.OPAQUE) != 0;
        mWindowSurfaceController.getContainerRect(mTmpContainerRect);
        mBackgroundControl = new SurfaceControl(s, "Background for - " + name,
                mTmpContainerRect.width(), mTmpContainerRect.height(), PixelFormat.OPAQUE,
                flags | SurfaceControl.FX_SURFACE_DIM);
    }

    @Override
    public void setAlpha(float alpha) {
        super.setAlpha(alpha);

        if (mBackgroundControl == null) {
            return;
        }
        mBackgroundControl.setAlpha(alpha);
    }

    @Override
    public void setLayer(int zorder) {
        super.setLayer(zorder);

        if (mBackgroundControl == null) {
            return;
        }
        // TODO: Use setRelativeLayer(Integer.MIN_VALUE) when it's fixed.
        mBackgroundControl.setLayer(zorder - 1);
    }

    @Override
    public void setPosition(float x, float y) {
        super.setPosition(x, y);

        if (mBackgroundControl == null) {
            return;
        }
        mLastX = x;
        mLastY = y;
        updateBgPosition();
    }

    private void updateBgPosition() {
        mWindowSurfaceController.getContainerRect(mTmpContainerRect);
        final Rect winFrame = mWindowSurfaceController.mAnimator.mWin.mFrame;
        final float offsetX = (mTmpContainerRect.left - winFrame.left) * mLastDsDx;
        final float offsetY = (mTmpContainerRect.top - winFrame.top) * mLastDsDy;
        mBackgroundControl.setPosition(mLastX + offsetX, mLastY + offsetY);
    }

    @Override
    public void setSize(int w, int h) {
        super.setSize(w, h);

        if (mBackgroundControl == null) {
            return;
        }
        mLastWidth = w;
        mLastHeight = h;
        mWindowSurfaceController.getContainerRect(mTmpContainerRect);
        mBackgroundControl.setSize(mTmpContainerRect.width(), mTmpContainerRect.height());
    }

    @Override
    public void setWindowCrop(Rect crop) {
        super.setWindowCrop(crop);

        if (mBackgroundControl == null) {
            return;
        }
        if (crop.width() < mLastWidth || crop.height() < mLastHeight) {
            // We're animating and cropping window, compute the appropriate crop for background.
            calculateBgCrop(crop);
            mBackgroundControl.setWindowCrop(mTmpContainerRect);
        } else {
            // When not animating just set crop to container rect.
            mWindowSurfaceController.getContainerRect(mTmpContainerRect);
            mBackgroundControl.setWindowCrop(mTmpContainerRect);
        }
    }

    @Override
    public void setFinalCrop(Rect crop) {
        super.setFinalCrop(crop);

        if (mBackgroundControl == null) {
            return;
        }
        if (crop.width() < mLastWidth || crop.height() < mLastHeight) {
            // We're animating and cropping window, compute the appropriate crop for background.
            calculateBgCrop(crop);
            mBackgroundControl.setFinalCrop(mTmpContainerRect);
        } else {
            // When not animating just set crop to container rect.
            mWindowSurfaceController.getContainerRect(mTmpContainerRect);
            mBackgroundControl.setFinalCrop(mTmpContainerRect);
        }
    }

    /** Compute background crop based on current animation progress for main surface control. */
    private void calculateBgCrop(Rect crop) {
        // Track overall progress of animation by computing cropped portion of status bar.
        final Rect contentInsets = mWindowSurfaceController.mAnimator.mWin.mContentInsets;
        float d = contentInsets.top == 0 ? 0 : (float) crop.top / contentInsets.top;
        if (d > 1.f) {
            // We're running expand animation from launcher, won't compute custom bg crop here.
            mTmpContainerRect.set(crop);
            return;
        }

        // Compute additional offset for the background when app window is positioned not at (0,0).
        // E.g. landscape with navigation bar on the left.
        final Rect winFrame = mWindowSurfaceController.mAnimator.mWin.mFrame;
        final int offsetX = (int) (winFrame.left * mLastDsDx * d + 0.5);
        final int offsetY = (int) (winFrame.top * mLastDsDy * d + 0.5);

        // Compute new scaled width and height for background that will depend on current animation
        // progress. Those consist of current crop rect for the main surface + scaled areas outside
        // of letterboxed area.
        // TODO: Because the progress is computed with low precision we're getting smaller values
        // for background width/height then screen size at the end of the animation. Will round when
        // the value is smaller then some empiric epsilon. However, this should be fixed by
        // computing correct frames for letterboxed windows in WindowState.
        d = d < 0.025f ? 0 : d;
        mWindowSurfaceController.getContainerRect(mTmpContainerRect);
        final int backgroundWidth =
                (int) (crop.width() + (mTmpContainerRect.width() - mLastWidth) * (1 - d) + 0.5);
        final int backgroundHeight =
                (int) (crop.height() + (mTmpContainerRect.height() - mLastHeight) * (1 - d) + 0.5);

        mTmpContainerRect.set(crop);
        // Make sure that part of background to left/top is visible and scaled.
        mTmpContainerRect.offset(offsetX, offsetY);
        // Set correct width/height, so that area to right/bottom is cropped properly.
        mTmpContainerRect.right = mTmpContainerRect.left + backgroundWidth;
        mTmpContainerRect.bottom = mTmpContainerRect.top + backgroundHeight;
    }

    @Override
    public void setLayerStack(int layerStack) {
        super.setLayerStack(layerStack);

        if (mBackgroundControl == null) {
            return;
        }
        mBackgroundControl.setLayerStack(layerStack);
    }

    @Override
    public void setOpaque(boolean isOpaque) {
        super.setOpaque(isOpaque);
        mOpaque = isOpaque;
        updateBackgroundVisibility();
    }

    @Override
    public void setSecure(boolean isSecure) {
        super.setSecure(isSecure);
    }

    @Override
    public void setMatrix(float dsdx, float dtdx, float dtdy, float dsdy) {
        super.setMatrix(dsdx, dtdx, dtdy, dsdy);

        if (mBackgroundControl == null) {
            return;
        }
        mBackgroundControl.setMatrix(dsdx, dtdx, dtdy, dsdy);
        mLastDsDx = dsdx;
        mLastDsDy = dsdy;
        updateBgPosition();
    }

    @Override
    public void hide() {
        super.hide();
        mVisible = false;
        updateBackgroundVisibility();
    }

    @Override
    public void show() {
        super.show();
        mVisible = true;
        updateBackgroundVisibility();
    }

    @Override
    public void destroy() {
        super.destroy();

        if (mBackgroundControl == null) {
            return;
        }
        mBackgroundControl.destroy();
    }

    @Override
    public void release() {
        super.release();

        if (mBackgroundControl == null) {
            return;
        }
        mBackgroundControl.release();
    }

    @Override
    public void setTransparentRegionHint(Region region) {
        super.setTransparentRegionHint(region);

        if (mBackgroundControl == null) {
            return;
        }
        mBackgroundControl.setTransparentRegionHint(region);
    }

    @Override
    public void deferTransactionUntil(IBinder handle, long frame) {
        super.deferTransactionUntil(handle, frame);

        if (mBackgroundControl == null) {
            return;
        }
        mBackgroundControl.deferTransactionUntil(handle, frame);
    }

    @Override
    public void deferTransactionUntil(Surface barrier, long frame) {
        super.deferTransactionUntil(barrier, frame);

        if (mBackgroundControl == null) {
            return;
        }
        mBackgroundControl.deferTransactionUntil(barrier, frame);
    }

    private void updateBackgroundVisibility() {
        if (mBackgroundControl == null) {
            return;
        }
        if (mOpaque && mVisible) {
            mBackgroundControl.show();
        } else {
            mBackgroundControl.hide();
        }
    }
}