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 android.car.builtin.view;
18 
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 import android.annotation.SystemApi;
22 import android.app.Activity;
23 import android.car.builtin.util.Slogf;
24 import android.os.RemoteException;
25 import android.util.Log;
26 import android.view.SurfaceControl;
27 import android.view.ViewRootImpl;
28 import android.view.Window;
29 import android.view.WindowManagerGlobal;
30 
31 /**
32  * Provides access to {@code android.view.SurfaceControl} calls.
33  * @hide
34  */
35 @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
36 public final class SurfaceControlHelper {
37     private static final String TAG = SurfaceControlHelper.class.getSimpleName();
SurfaceControlHelper()38     private SurfaceControlHelper() {
39         throw new UnsupportedOperationException();
40     }
41 
42     /**
43      * Creates a mirrored hierarchy for the mirror of {@link SurfaceControl}.
44      *
45      * <p>For the detail, see {@link SurfaceControl#mirrorSurface(SurfaceControl)}
46      */
47     @NonNull
mirrorSurface(@onNull SurfaceControl mirrorOf)48     public static SurfaceControl mirrorSurface(@NonNull SurfaceControl mirrorOf) {
49         return SurfaceControl.mirrorSurface(mirrorOf);
50     }
51 
52     /**
53      * Creates a mirrored Surface for the given Display.
54      */
55     @Nullable
mirrorDisplay(int displayId)56     public static SurfaceControl mirrorDisplay(int displayId) {
57         try {
58             SurfaceControl outSurfaceControl = new SurfaceControl();
59             boolean success = WindowManagerGlobal.getWindowManagerService().mirrorDisplay(displayId,
60                     outSurfaceControl);
61             return success ? outSurfaceControl : null;
62         } catch (RemoteException e) {
63             Log.e(TAG, "Unable to reach window manager", e);
64         }
65         return null;
66     }
67 
68     /**
69      * See {@link SurfaceControl(SurfaceControl)}}.
70      */
copy(SurfaceControl source)71     public static SurfaceControl copy(SurfaceControl source) {
72         return new SurfaceControl(source, SurfaceControlHelper.class.getSimpleName());
73     }
74 
getViewRootImpl(Activity activity)75     private static ViewRootImpl getViewRootImpl(Activity activity) {
76         Window window = activity.getWindow();
77         if (window == null) {
78             Slogf.e(TAG, "Window is not ready yet: %s", activity.getComponentName());
79             return null;
80         }
81         ViewRootImpl viewRoot = window.getDecorView().getViewRootImpl();
82         if (viewRoot == null) {
83             Slogf.e(TAG, "ViewRoot is not attached to Window yet: %s",
84                     activity.getComponentName());
85             return null;
86         }
87         return viewRoot;
88     }
89 
90     /**
91      * Gets {@link SurfaceControl} of given Activity.
92      *
93      * Note: SurfaceControl is not available during {@code onCreate} and {@code onResume}.
94      * You can access it from {@link Activity#onAttachedToWindow()}.
95      */
getSurfaceControl(@onNull Activity activity)96     @Nullable public static SurfaceControl getSurfaceControl(@NonNull Activity activity) {
97         ViewRootImpl viewRoot = getViewRootImpl(activity);
98         if (viewRoot == null) {
99             return null;
100         }
101         SurfaceControl surface = viewRoot.getSurfaceControl();
102         if (surface == null) {
103             Slogf.e(TAG, "Surface is not prepared yet");
104             return null;
105         }
106         return surface;
107     }
108 }
109