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