• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2012 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.server.display;
18 
19 import android.content.Context;
20 import android.os.Handler;
21 import android.view.Display;
22 
23 import com.android.server.display.feature.DisplayManagerFlags;
24 
25 import java.io.PrintWriter;
26 import java.util.concurrent.atomic.AtomicInteger;
27 
28 /**
29  * A display adapter makes zero or more display devices available to the system
30  * and provides facilities for discovering when displays are connected or disconnected.
31  * <p>
32  * For now, all display adapters are registered in the system server but
33  * in principle it could be done from other processes.
34  * </p><p>
35  * Display adapters are guarded by the {@link DisplayManagerService.SyncRoot} lock.
36  * </p>
37  */
38 abstract class DisplayAdapter {
39     private final DisplayManagerService.SyncRoot mSyncRoot;
40     private final Context mContext;
41     private final Handler mHandler;
42     private final Listener mListener;
43     private final String mName;
44     private final DisplayManagerFlags mFeatureFlags;
45 
46     public static final int DISPLAY_DEVICE_EVENT_ADDED = 1;
47     public static final int DISPLAY_DEVICE_EVENT_CHANGED = 2;
48     public static final int DISPLAY_DEVICE_EVENT_REMOVED = 3;
49 
50     /**
51      * Used to generate globally unique display mode ids.
52      */
53     private static final AtomicInteger NEXT_DISPLAY_MODE_ID = new AtomicInteger(1);  // 0 = no mode.
54 
55     // Called with SyncRoot lock held.
DisplayAdapter(DisplayManagerService.SyncRoot syncRoot, Context context, Handler handler, Listener listener, String name, DisplayManagerFlags featureFlags)56     DisplayAdapter(DisplayManagerService.SyncRoot syncRoot, Context context, Handler handler,
57             Listener listener, String name, DisplayManagerFlags featureFlags) {
58         mSyncRoot = syncRoot;
59         mContext = context;
60         mHandler = handler;
61         mListener = listener;
62         mName = name;
63         mFeatureFlags = featureFlags;
64     }
65 
66     /**
67      * Gets the object that the display adapter should synchronize on when handling
68      * calls that come in from outside of the display manager service.
69      */
getSyncRoot()70     public final DisplayManagerService.SyncRoot getSyncRoot() {
71         return mSyncRoot;
72     }
73 
74     /**
75      * Gets the display adapter's context.
76      */
getContext()77     public final Context getContext() {
78         return mContext;
79     }
80 
81     /**
82      * Gets a handler that the display adapter may use to post asynchronous messages.
83      */
getHandler()84     public final Handler getHandler() {
85         return mHandler;
86     }
87 
88     /**
89      * Gets the display adapter name for debugging purposes.
90      */
getName()91     public final String getName() {
92         return mName;
93     }
94 
getFeatureFlags()95     public final DisplayManagerFlags getFeatureFlags() {
96         return mFeatureFlags;
97     }
98 
99     /**
100      * Registers the display adapter with the display manager.
101      *
102      * The display adapter should register any built-in display devices as soon as possible.
103      * The boot process will wait for the default display to be registered.
104      * Other display devices can be registered dynamically later.
105      */
registerLocked()106     public void registerLocked() {
107     }
108 
109     /**
110      * Dumps the local state of the display adapter.
111      */
dumpLocked(PrintWriter pw)112     public void dumpLocked(PrintWriter pw) {
113     }
114 
115     /**
116      * Sends a display device event to the display adapter listener asynchronously.
117      */
sendDisplayDeviceEventLocked( final DisplayDevice device, final int event)118     protected final void sendDisplayDeviceEventLocked(
119             final DisplayDevice device, final int event) {
120         mHandler.post(() -> mListener.onDisplayDeviceEvent(device, event));
121     }
122 
123     /**
124      * Sends a request to perform traversals.
125      */
sendTraversalRequestLocked()126     protected final void sendTraversalRequestLocked() {
127         mHandler.post(() -> mListener.onTraversalRequested());
128     }
129 
createMode(int width, int height, float refreshRate)130     public static Display.Mode createMode(int width, int height, float refreshRate) {
131         return createMode(width, height, refreshRate, refreshRate, new float[0], new int[0]);
132     }
133 
createMode(int width, int height, float refreshRate, float vsyncRate, float[] alternativeRefreshRates, @Display.HdrCapabilities.HdrType int[] supportedHdrTypes)134     public static Display.Mode createMode(int width, int height, float refreshRate, float vsyncRate,
135             float[] alternativeRefreshRates,
136             @Display.HdrCapabilities.HdrType int[] supportedHdrTypes) {
137         return new Display.Mode(NEXT_DISPLAY_MODE_ID.getAndIncrement(), width, height, refreshRate,
138                 vsyncRate, /* isSynthetic= */ false, alternativeRefreshRates, supportedHdrTypes);
139     }
140 
141     public interface Listener {
onDisplayDeviceEvent(DisplayDevice device, int event)142         void onDisplayDeviceEvent(DisplayDevice device, int event);
onTraversalRequested()143         void onTraversalRequested();
144     }
145 }
146