1 package com.genymobile.scrcpy.wrappers;
2 
3 import android.annotation.SuppressLint;
4 import android.graphics.Rect;
5 import android.os.IBinder;
6 import android.view.Surface;
7 
8 @SuppressLint("PrivateApi")
9 public final class SurfaceControl {
10 
11     private static final Class<?> CLASS;
12 
13     // see <https://android.googlesource.com/platform/frameworks/base.git/+/pie-release-2/core/java/android/view/SurfaceControl.java#305>
14     public static final int POWER_MODE_OFF = 0;
15     public static final int POWER_MODE_NORMAL = 2;
16 
17     static {
18         try {
19             CLASS = Class.forName("android.view.SurfaceControl");
20         } catch (ClassNotFoundException e) {
21             throw new AssertionError(e);
22         }
23     }
24 
SurfaceControl()25     private SurfaceControl() {
26         // only static methods
27     }
28 
openTransaction()29     public static void openTransaction() {
30         try {
31             CLASS.getMethod("openTransaction").invoke(null);
32         } catch (Exception e) {
33             throw new AssertionError(e);
34         }
35     }
36 
closeTransaction()37     public static void closeTransaction() {
38         try {
39             CLASS.getMethod("closeTransaction").invoke(null);
40         } catch (Exception e) {
41             throw new AssertionError(e);
42         }
43     }
44 
setDisplayProjection(IBinder displayToken, int orientation, Rect layerStackRect, Rect displayRect)45     public static void setDisplayProjection(IBinder displayToken, int orientation, Rect layerStackRect, Rect displayRect) {
46         try {
47             CLASS.getMethod("setDisplayProjection", IBinder.class, int.class, Rect.class, Rect.class)
48                     .invoke(null, displayToken, orientation, layerStackRect, displayRect);
49         } catch (Exception e) {
50             throw new AssertionError(e);
51         }
52     }
53 
setDisplayLayerStack(IBinder displayToken, int layerStack)54     public static void setDisplayLayerStack(IBinder displayToken, int layerStack) {
55         try {
56             CLASS.getMethod("setDisplayLayerStack", IBinder.class, int.class).invoke(null, displayToken, layerStack);
57         } catch (Exception e) {
58             throw new AssertionError(e);
59         }
60     }
61 
setDisplaySurface(IBinder displayToken, Surface surface)62     public static void setDisplaySurface(IBinder displayToken, Surface surface) {
63         try {
64             CLASS.getMethod("setDisplaySurface", IBinder.class, Surface.class).invoke(null, displayToken, surface);
65         } catch (Exception e) {
66             throw new AssertionError(e);
67         }
68     }
69 
createDisplay(String name, boolean secure)70     public static IBinder createDisplay(String name, boolean secure) {
71         try {
72             return (IBinder) CLASS.getMethod("createDisplay", String.class, boolean.class).invoke(null, name, secure);
73         } catch (Exception e) {
74             throw new AssertionError(e);
75         }
76     }
77 
getBuiltInDisplay(int builtInDisplayId)78     public static IBinder getBuiltInDisplay(int builtInDisplayId) {
79         try {
80             return (IBinder) CLASS.getMethod("getBuiltInDisplay", int.class).invoke(null, builtInDisplayId);
81         } catch (Exception e) {
82             throw new AssertionError(e);
83         }
84     }
85 
setDisplayPowerMode(IBinder displayToken, int mode)86     public static void setDisplayPowerMode(IBinder displayToken, int mode) {
87         try {
88             CLASS.getMethod("setDisplayPowerMode", IBinder.class, int.class).invoke(null, displayToken, mode);
89         } catch (Exception e) {
90             throw new AssertionError(e);
91         }
92     }
93 
destroyDisplay(IBinder displayToken)94     public static void destroyDisplay(IBinder displayToken) {
95         try {
96             CLASS.getMethod("destroyDisplay", IBinder.class).invoke(null, displayToken);
97         } catch (Exception e) {
98             throw new AssertionError(e);
99         }
100     }
101 }
102