blob: 6ba25a53248ded7d85670ae6c7a893e6c9d141d6 [file] [log] [blame]
Jeff Brownfa25bf52012-07-23 19:26:30 -07001/*
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
17package com.android.server.display;
18
Jeff Brownbd6e1502012-08-28 03:27:37 -070019import android.content.Context;
20import android.os.Handler;
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -070021import android.view.Display;
Jeff Brownbd6e1502012-08-28 03:27:37 -070022
23import java.io.PrintWriter;
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -070024import java.util.concurrent.atomic.AtomicInteger;
Jeff Brownbd6e1502012-08-28 03:27:37 -070025
Jeff Brownfa25bf52012-07-23 19:26:30 -070026/**
Jeff Brown848c2dc2012-08-19 20:18:08 -070027 * 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.
Jeff Brownfa25bf52012-07-23 19:26:30 -070029 * <p>
30 * For now, all display adapters are registered in the system server but
31 * in principle it could be done from other processes.
Jeff Brownbd6e1502012-08-28 03:27:37 -070032 * </p><p>
Jeff Brown4ed8fe72012-08-30 18:18:29 -070033 * Display adapters are guarded by the {@link DisplayManagerService.SyncRoot} lock.
Jeff Brownfa25bf52012-07-23 19:26:30 -070034 * </p>
35 */
Jeff Brown4ed8fe72012-08-30 18:18:29 -070036abstract class DisplayAdapter {
37 private final DisplayManagerService.SyncRoot mSyncRoot;
Jeff Brownbd6e1502012-08-28 03:27:37 -070038 private final Context mContext;
Jeff Brownbd6e1502012-08-28 03:27:37 -070039 private final Handler mHandler;
Jeff Brown4ed8fe72012-08-30 18:18:29 -070040 private final Listener mListener;
41 private final String mName;
Jeff Brownbd6e1502012-08-28 03:27:37 -070042
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
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -070047 /**
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
Jeff Brown66692502012-10-18 16:13:44 -070052 // Called with SyncRoot lock held.
Jeff Brown4ed8fe72012-08-30 18:18:29 -070053 public DisplayAdapter(DisplayManagerService.SyncRoot syncRoot,
54 Context context, Handler handler, Listener listener, String name) {
55 mSyncRoot = syncRoot;
Jeff Brownbd6e1502012-08-28 03:27:37 -070056 mContext = context;
Jeff Brown4ed8fe72012-08-30 18:18:29 -070057 mHandler = handler;
58 mListener = listener;
Jeff Brownbd6e1502012-08-28 03:27:37 -070059 mName = name;
Jeff Brownbd6e1502012-08-28 03:27:37 -070060 }
61
Jeff Brown4ed8fe72012-08-30 18:18:29 -070062 /**
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 */
66 public final DisplayManagerService.SyncRoot getSyncRoot() {
67 return mSyncRoot;
68 }
69
70 /**
71 * Gets the display adapter's context.
72 */
Jeff Brownbd6e1502012-08-28 03:27:37 -070073 public final Context getContext() {
74 return mContext;
75 }
76
Jeff Brown4ed8fe72012-08-30 18:18:29 -070077 /**
78 * Gets a handler that the display adapter may use to post asynchronous messages.
79 */
Jeff Brownbd6e1502012-08-28 03:27:37 -070080 public final Handler getHandler() {
81 return mHandler;
82 }
83
Jeff Brownfa25bf52012-07-23 19:26:30 -070084 /**
Jeff Brown848c2dc2012-08-19 20:18:08 -070085 * Gets the display adapter name for debugging purposes.
Jeff Brownfa25bf52012-07-23 19:26:30 -070086 */
Jeff Brownbd6e1502012-08-28 03:27:37 -070087 public final String getName() {
88 return mName;
89 }
Jeff Brownfa25bf52012-07-23 19:26:30 -070090
Jeff Brown848c2dc2012-08-19 20:18:08 -070091 /**
92 * Registers the display adapter with the display manager.
Jeff Brown848c2dc2012-08-19 20:18:08 -070093 *
Jeff Brown4ed8fe72012-08-30 18:18:29 -070094 * 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.
Jeff Brown848c2dc2012-08-19 20:18:08 -070097 */
Jeff Brown4ed8fe72012-08-30 18:18:29 -070098 public void registerLocked() {
Jeff Brownbd6e1502012-08-28 03:27:37 -070099 }
100
101 /**
102 * Dumps the local state of the display adapter.
103 */
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700104 public void dumpLocked(PrintWriter pw) {
Jeff Brownbd6e1502012-08-28 03:27:37 -0700105 }
106
107 /**
108 * Sends a display device event to the display adapter listener asynchronously.
109 */
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700110 protected final void sendDisplayDeviceEventLocked(
111 final DisplayDevice device, final int event) {
Jeff Brownbd6e1502012-08-28 03:27:37 -0700112 mHandler.post(new Runnable() {
113 @Override
114 public void run() {
115 mListener.onDisplayDeviceEvent(device, event);
116 }
117 });
118 }
Jeff Brown848c2dc2012-08-19 20:18:08 -0700119
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700120 /**
121 * Sends a request to perform traversals.
122 */
123 protected final void sendTraversalRequestLocked() {
124 mHandler.post(new Runnable() {
125 @Override
126 public void run() {
127 mListener.onTraversalRequested();
128 }
129 });
130 }
131
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -0700132 public static Display.Mode createMode(int width, int height, float refreshRate) {
133 return new Display.Mode(
134 NEXT_DISPLAY_MODE_ID.getAndIncrement(), width, height, refreshRate);
135 }
136
Jeff Brown848c2dc2012-08-19 20:18:08 -0700137 public interface Listener {
Jeff Brownbd6e1502012-08-28 03:27:37 -0700138 public void onDisplayDeviceEvent(DisplayDevice device, int event);
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700139 public void onTraversalRequested();
Jeff Brown848c2dc2012-08-19 20:18:08 -0700140 }
Jeff Brownfa25bf52012-07-23 19:26:30 -0700141}