android graphic(12)—display上层相关概念、关系

http://blog.csdn.net/lewif/article/details/50827430

目录(?)[+]


涉及的java

DisplayManagerService

Manages attached displays.

The DisplayManagerService manages theglobal lifecycle of displays,

decides how to configure logicaldisplays based on the physical display devices currently

attached, sends notifications to thesystem and to applications when the state

changes, and so on.

The display manager service relies on acollection of DisplayAdapter components,

for discovering and configuring physicaldisplay devices attached to the system.

There are separate display adapters foreach manner that devices are attached:

one display adapter for built-in localdisplays, one for simulated non-functional

displays when the system is headless,one for simulated overlay displays used for

development, one for wifi displays, etc.

Display adapters are only weakly coupledto the display manager service.

Display adapters communicate changes indisplay device state to the display manager

service asynchronously via aDisplayAdapter.Listener registered

by the display manager service.  This separation of concerns is important for

two main reasons.  First, it neatly encapsulates theresponsibilities of these

two classes: display adapters handleindividual display devices whereas

the display manager service handles theglobal state.  Second, it eliminates

the potential for deadlocks resultingfrom asynchronous display device discovery.

DisplayAdapter

A display adapter makes zero or moredisplay devices available to the system

and provides facilities for discoveringwhen displays are connected or disconnected.

For now, all display adapters areregistered in the system server but

in principle it could be done from otherprocesses.

现在支持的4Adapter,分别对应不同类型的display

1.LocalDisplayAdapter

A display adapter for the local displaysmanaged by Surface Flinger

2.WifiDisplayAdapter

Connects to Wifi displays that implementthe Miracast protocol.

This class is responsible for connectingto Wifi displays and mediating

the interactions between Media Server,Surface Flinger and the Display Manager Service.

3.VirtualDisplayAdapter

4.OverlayDisplayAdapter

DisplayDevice

 Represents a physical display device such asthe built-in display an external monitor, or a WiFi display.

WifiDisplayDevice

VirtualDisplayDevice

OverlayDisplayDevice

LocalDisplayDevice

DisplayManagerGlobal

Manager communication with the displaymanager service on behalf of an application process.

DisplayManager

Manages the properties of attacheddisplays.

LogicalDisplay

Describes how a logical display isconfigured.

At this time, we only support logicaldisplays that are coupled to a particular

primary display device from which thelogical display derives its basic properties

such as its size, density and refreshrate.

A logical display may be mirrored ontomultiple display devices in addition to its

primary display device.  Note that the contents of a logical displaymay not

always be visible, even on its primarydisplay device, such as in the case where

the primary display device is currentlymirroring content from a different

logical display.

Note: The display manager architecturedoes not actually require logical displays

to be associated with any individualdisplay device.  Logical displays and

display devices are orthogonalconcepts.  Some mapping will existbetween

logical displays and display devices butit can be many-to-many and

and some might have no relation at all.

Display

Provides information about the size anddensity of a logical display.

DisplayContent

Utility class for keeping track of theWindowStates and other pertinent contents of a particular Display.

DisplayInfo

Describes the characteristics of aparticular logical display.

类之间的关系

以添加系统built in display为例,下面是各个类之间的关系图,

其中,LocalDisplayDevice中的mPhys是向surface flinger获取的display的硬件相关属性,而mDisplayTokensurfacefinger中为display创建的new BBinder对应的代理对象。

默认屏幕的上层初始化分析

1. 
systemserver.Java

   /*--------------systemserver.java---------------------------*/

   // 专门为window manager创建了一个handler thread

   // Create a handler thread just for the window manager to enjoy.

       HandlerThread wmHandlerThread = new HandlerThread("WindowManager");

       wmHandlerThread.start();

       //创建一个window managerHandler(looperwmHandlerThread线程的)

       Handler wmHandler = new Handler(wmHandlerThread.getLooper());

       wmHandler.post(new Runnable(){

            @Override

            public void run() {

                //Looper.myLooper().setMessageLogging(new LogPrinter(

                //        android.util.Log.DEBUG, TAG, android.util.Log.LOG_ID_SYSTEM));

                android.os.Process.setThreadPriority(

                        android.os.Process.THREAD_PRIORITY_DISPLAY);

                android.os.Process.setCanSelfBackground(false);

 

                // For debug builds, log eventloop stalls to dropbox for analysis.

                if (StrictMode.conditionallyEnableDebugLogging()) {

                    Slog.i(TAG, "Enabled StrictMode logging for WM Looper");

                }

            }

       });

   /*--------------systemserver.java---------------------------*/

   // DisplayManagerService display = null;

   // 新建DisplayManagerService服务

       Slog.i(TAG, "Display Manager");

            display =new DisplayManagerService(context,wmHandler);

            ServiceManager.addService(Context.DISPLAY_SERVICE,display, true);

        //需要等待第一个display初始化完成后,才继续进行,否则一直循环

       if (!display.waitForDefaultDisplay()) {

                reportWtf("Timeoutwaiting for default display to be initialized.",

                        new Throwable());

            }

   /*--------------DisplayManagerService.java---------------------------*/

   // mSyncRoot wait,直到mLogicalDisplays.get(Display.DEFAULT_DISPLAY)不为null

   // 即有地方添加了默认displayLogicalDisplay

   /**

     * Pauses theboot process to wait for the first display to be initialized.

     */

   publicbooleanwaitForDefaultDisplay() {

       synchronized (mSyncRoot) {

            long timeout = SystemClock.uptimeMillis() + WAIT_FOR_DEFAULT_DISPLAY_TIMEOUT;

           while (mLogicalDisplays.get(Display.DEFAULT_DISPLAY) == null) {

                long delay = timeout - SystemClock.uptimeMillis();

                if (delay <= 0) {

                    returnfalse;

                }

                if (DEBUG) {

                    Slog.d(TAG, "waitForDefaultDisplay:waiting, timeout=" + delay);

                }

                try {

                    mSyncRoot.wait(delay);

                } catch (InterruptedException ex) {

                }

            }

       }

       returntrue;

   }

  /*--------------DisplayManagerService.java---------------------------*/

  publicDisplayManagerService(Context context, Handler mainHandler) {

       mContext = context;

       mHeadless = SystemProperties.get(SYSTEM_HEADLESS).equals("1");

   //新建个DisplayManagerHandler

       mHandler = newDisplayManagerHandler(mainHandler.getLooper());

       mUiHandler = UiThread.getHandler();

       //新建个DisplayAdapterListener

       mDisplayAdapterListener = new DisplayAdapterListener();

       //persist.demo.singledisplay是只创建默认displaylogical display

       mSingleDisplayDemoMode = SystemProperties.getBoolean("persist.demo.singledisplay", false);

 

       mHandler.sendEmptyMessage(MSG_REGISTER_DEFAULT_DISPLAY_ADAPTER);

   }

DisplayManagerHandler的消息处理函数,

  /*--------------DisplayManagerService.java---------------------------*/

privatefinalclassDisplayManagerHandlerextendsHandler {

       publicDisplayManagerHandler(Looper looper) {

            super(looper, null, true/*async*/);

       }

 

       @Override

       publicvoidhandleMessage(Message msg) {

            switch (msg.what) {

                case MSG_REGISTER_DEFAULT_DISPLAY_ADAPTER:

                   registerDefaultDisplayAdapter();

                    break;

 

                case MSG_REGISTER_ADDITIONAL_DISPLAY_ADAPTERS:

                   registerAdditionalDisplayAdapters();

                    break;

 

                case MSG_DELIVER_DISPLAY_EVENT:

                   deliverDisplayEvent(msg.arg1, msg.arg2);

                    break;

 

                case MSG_REQUEST_TRAVERSAL:

                   mWindowManagerFuncs.requestTraversal();

                    break;

 

                case MSG_UPDATE_VIEWPORT: {

                    synchronized (mSyncRoot) {

                        mTempDefaultViewport.copyFrom(mDefaultViewport);

                       mTempExternalTouchViewport.copyFrom(mExternalTouchViewport);

                    }

                   mInputManagerFuncs.setDisplayViewports(

                            mTempDefaultViewport,mTempExternalTouchViewport);

                    break;

                }

            }

       }

   }

DisplayAdapterListener类,

     /*--------------DisplayManagerService.java---------------------------*/

   privatefinalclassDisplayAdapterListenerimplementsDisplayAdapter.Listener {

       @Override

       publicvoidonDisplayDeviceEvent(DisplayDevicedevice, int event) {

            switch (event) {

                case DisplayAdapter.DISPLAY_DEVICE_EVENT_ADDED:

                   handleDisplayDeviceAdded(device);

                    break;

 

                case DisplayAdapter.DISPLAY_DEVICE_EVENT_CHANGED:

                   handleDisplayDeviceChanged(device);

                    break;

 

                case DisplayAdapter.DISPLAY_DEVICE_EVENT_REMOVED:

                    handleDisplayDeviceRemoved(device);

                    break;

            }

       }

 

       @Override

       publicvoidonTraversalRequested() {

            synchronized (mSyncRoot) {

                scheduleTraversalLocked(false);

            }

       }

   }

   mHeadless = SystemProperties.get(SYSTEM_HEADLESS).equals("1");

/* ro.config.headless

   系统中判断ro.config.headless的总共有几个地方

   aSurfaceControl.java在构造函数中判断如果是headless设备则抛出异常,说明headless的设备不应该构造任何显示画面

   b、在SystemUI中判断headlesstrue的情况下不启动WallpaperManagerServiceSystemUIService

   c、在PhoneWindowManagersystemReady中判断headlesstrue的情况下不起动KeyguardServiceDelegate,不显示启动提示消息,屏蔽滑盖(lid)状态,屏蔽一些按键

   dDisplayManagerService在创建默认显示设备的时候(registerDefaultDisplayAdapter)判断headlesstrue的情况下创建的是HeadlessDisplayAdapter而非LocalDisplayAdapter,前者并没有通过SurfaceFlinger.getBuiltInDisplay获取一个对应的DisplayDevice,也就是说上层的默认显示设备是空

   eActivityManagerServicestartHomeActivityLockedheadlesstrue的情况下不启动HomeActivity,不能启动Activity,不能重启挂掉的进程(即使是persistent),不能更新屏幕配置。

*/

mHandlerMSG_REGISTER_DEFAULT_DISPLAY_ADAPTER处理函数为,

/*--------------DisplayManagerService.java---------------------------*/

   privatevoidregisterDefaultDisplayAdapter() {

       // Register default display adapter.

       synchronized (mSyncRoot) {

            if (mHeadless) {

                registerDisplayAdapterLocked(new HeadlessDisplayAdapter(

                        mSyncRoot, mContext,mHandler, mDisplayAdapterListener));

            } else {

                //走这个分支

                registerDisplayAdapterLocked(new LocalDisplayAdapter(

                       mSyncRoot, mContext,mHandler, mDisplayAdapterListener));

            }

       }

   }

LocalDisplayAdapter构造函数的mHandlermDisplayAdapterListener分别为DisplayManagerHandlerDisplayAdapterListener

   /*--------------LocalDisplayAdapter.java---------------------------*/

   /* class LocalDisplayAdapter extends DisplayAdapter */

   // Called with SyncRoot lock held.

    publicLocalDisplayAdapter(DisplayManagerService.SyncRoot syncRoot,

            Contextcontext, Handlerhandler, Listener listener) {

       super(syncRoot, context, handler, listener, TAG);

   }

   /*--------------DisplayAdapter.java---------------------------*/

  // LocalDisplayAdapter DisplayAdapter的子类

  public DisplayAdapter(DisplayManagerService.SyncRoot syncRoot,

            Context context, Handlerhandler, Listenerlistener, String name) {

        mSyncRoot =syncRoot;

       mContext = context;

       mHandler = handler;

       mListener = listener;

       mName = name;

   }

/*--------------DisplayManagerService.java---------------------------*/

//    private finalArrayList<DisplayAdapter> mDisplayAdapters = newArrayList<DisplayAdapter>();

       privatevoidregisterDisplayAdapterLocked(DisplayAdapteradapter) {

       mDisplayAdapters.add(adapter);

       adapter.registerLocked();

   }

LocalDisplayAdapter.registerLocked()

/*--------------LocalDisplayAdapter.java---------------------------*/      

       privatestaticfinalint[] BUILT_IN_DISPLAY_IDS_TO_SCAN = newint[] {

           SurfaceControl.BUILT_IN_DISPLAY_ID_MAIN,

           SurfaceControl.BUILT_IN_DISPLAY_ID_HDMI,

   };

 

   // LocalDisplayAdapter.registerLocked

       publicvoidregisterLocked() {

       super.registerLocked();

 

       mHotplugReceiver = new HotplugDisplayEventReceiver(getHandler().getLooper());

   //对默认屏幕和HDMI调用tryConnectDisplayLocked

       for (int builtInDisplayId : BUILT_IN_DISPLAY_IDS_TO_SCAN) {

            tryConnectDisplayLocked(builtInDisplayId);

       }

   }

调用tryConnectDisplayLocked(),这里主要是和底层framework去交互,获取底层注册的displays的相关信息。

     /*--------------LocalDisplayAdapter.java---------------------------*/

     // LocalDisplayAdapter.mDevices

     // private final SparseArray<LocalDisplayDevice> mDevices =newSparseArray<LocalDisplayDevice>();

     privatevoidtryConnectDisplayLocked(int builtInDisplayId) {

     //获取surface flinger中的new BBinder对应的client IBinder

       IBinder displayToken =SurfaceControl.getBuiltInDisplay(builtInDisplayId);

       //获取display的硬件属性,新建LocalDisplayDevice

       if (displayToken != null && SurfaceControl.getDisplayInfo(displayToken, mTempPhys)) {

            LocalDisplayDevice device =mDevices.get(builtInDisplayId);

            if (device == null) {

                // Display wasadded.

                device = new LocalDisplayDevice(displayToken, builtInDisplayId, mTempPhys);

                mDevices.put(builtInDisplayId,device);

                //DisplayManagerServiceDisplayManagerHandler发消息

               sendDisplayDeviceEventLocked(device, DISPLAY_DEVICE_EVENT_ADDED);

            } elseif(device.updatePhysicalDisplayInfoLocked(mTempPhys)) {

                // Displayproperties changed.

               sendDisplayDeviceEventLocked(device, DISPLAY_DEVICE_EVENT_CHANGED);

            }

       } else {

            // The display isno longer available. Ignore the attempt to add it.

            // If it wasconnected but has already been disconnected, we'll get a

            // disconnect eventthat will remove it from mDevices.

       }

   }

   /*--------------SurfaceControl.java---------------------------*/

   //SurfaceControl.java,都是static,类函数

   //通过id获取display token

   publicstatic IBinder getBuiltInDisplay(int builtInDisplayId) {

       return nativeGetBuiltInDisplay(builtInDisplayId);

   }

       //通过display token,获取display硬件属性

       publicstaticbooleangetDisplayInfo(IBinder displayToken, SurfaceControl.PhysicalDisplayInfo outInfo) {

       if (displayToken == null) {

            thrownew IllegalArgumentException("displayTokenmust not be null");

       }

       if (outInfo == null) {

            thrownew IllegalArgumentException("outInfomust not be null");

       }

       return nativeGetDisplayInfo(displayToken,outInfo);

   }

其中,nativeGetBuiltInDisplay最终会调用surfaceflinger中的getBuiltInDisplay,通过Binder传回代理对象。

/*--------------SurfaceFlinger.cpp---------------------------*/

sp<IBinder> SurfaceFlinger::getBuiltInDisplay(int32_tid) {

   if (uint32_t(id) >= DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {

       ALOGE("getDefaultDisplay: id=%d is not a valid default display id", id);

       returnNULL;

   }

   return mBuiltinDisplays[id];

}

nativeGetDisplayInfo—>SurfaceFlinger::getDisplayInfo

/*--------------SurfaceFlinger.cpp---------------------------*/

status_t SurfaceFlinger::getDisplayInfo(const sp<IBinder>& display, DisplayInfo* info) {

   int32_t type = NAME_NOT_FOUND;

   for (int i=0 ;i<DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES ; i++) {

       if (display == mBuiltinDisplays[i]) {

            type = i;

            break;

       }

   }

 

   if (type < 0) {

       return type;

   }

 

    const HWComposer& hwc(getHwComposer());

   float xdpi = hwc.getDpiX(type);

   float ydpi = hwc.getDpiY(type);

 

   // TODO: Not sure if display density should handled by SF any longer

   class Density {

       staticint getDensityFromProperty(charconst* propName) {

            char property[PROPERTY_VALUE_MAX];

            int density = 0;

            if (property_get(propName, property, NULL) > 0) {

                density = atoi(property);

            }

            return density;

       }

   public:

       staticint getEmuDensity() {

            return getDensityFromProperty("qemu.sf.lcd_density"); }

       staticint getBuildDensity()  {

            return getDensityFromProperty("ro.sf.lcd_density"); }

   };

 

   if (type == DisplayDevice::DISPLAY_PRIMARY) {

       // The density of the device is provided by a build property

       float density = Density::getBuildDensity() / 160.0f;

       if (density == 0) {

            // the builddoesn't provide a density -- this is wrong!

            // use xdpi instead

            ALOGE("ro.sf.lcd_densitymust be defined as a build property");

            density = xdpi / 160.0f;

       }

       if (Density::getEmuDensity()) {

            // if"qemu.sf.lcd_density" is specified, it overrides everything

            xdpi = ydpi = density =Density::getEmuDensity();

            density /= 160.0f;

       }

       info->density = density;

 

       // TODO: this needs to go away (currently needed only by webkit)

       sp<const DisplayDevice>hw(getDefaultDisplayDevice());

       info->orientation = hw->getOrientation();

   } else {

       // TODO: where should this value come from?

       staticconstint TV_DENSITY = 213;

       info->density = TV_DENSITY / 160.0f;

       info->orientation = 0;

   }

 

   info->w = hwc.getWidth(type);

   info->h = hwc.getHeight(type);

   info->xdpi = xdpi;

   info->ydpi = ydpi;

   info->fps = float(1e9 / hwc.getRefreshPeriod(type));

 

   // All non-virtual displays are currently considered secure.

   info->secure = true;

 

   return NO_ERROR;

}

创建完LocalDisplayDevice,给DisplayManagerServiceDisplayManagerHandler发消息,

   /*--------------DisplayAdapter.java---------------------------*/

   /**

     * Sends adisplay device event to the display adapter listener asynchronously.

     */

   protectedfinalvoidsendDisplayDeviceEventLocked(

            final DisplayDevice device, finalint event) {

       mHandler.post(new Runnable() {

            @Override

            publicvoidrun() {

                mListener.onDisplayDeviceEvent(device,event);

            }

       });

   }

LocalDisplayAdapter构造函数的mHandlermDisplayAdapterListener分别为DisplayManagerHandlerDisplayAdapterListener,进而会去调用,

   /*--------------DisplayManagerService.java---------------------------*/

       privatevoidhandleDisplayDeviceAdded(DisplayDevicedevice) {

       synchronized (mSyncRoot) {

           handleDisplayDeviceAddedLocked(device);

       }

   }

   /*--------------DisplayManagerService.java---------------------------*/

   //输入为LocalDisplayDevice

   //    private finalArrayList<DisplayDevice> mDisplayDevices = newArrayList<DisplayDevice>();

   privatevoidhandleDisplayDeviceAddedLocked(DisplayDevicedevice) {

       if (mDisplayDevices.contains(device)) {

            Slog.w(TAG, "Attemptedto add already added display device: "

                    +device.getDisplayDeviceInfoLocked());

            return;

       }

 

       Slog.i(TAG, "Display device added: " + device.getDisplayDeviceInfoLocked());

   // List of all currently connected display devices.

   //LocalDisplayDevice添加到mDisplayDevices

       mDisplayDevices.add(device);

       //为一个物理display添加一个logical display

       addLogicalDisplayLocked(device);

       updateDisplayBlankingLocked(device);

       scheduleTraversalLocked(false);

   }

为物理屏幕创建一个logical display

/*--------------DisplayManagerService.java---------------------------*/

// Adds a new logical display based on the given displaydevice.

   // Sends notifications if needed.

   privatevoidaddLogicalDisplayLocked(DisplayDevicedevice) {

       DisplayDeviceInfo deviceInfo = device.getDisplayDeviceInfoLocked();

       boolean isDefault = (deviceInfo.flags

                &DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY) != 0;

       //主屏,同时mLogicalDisplays包含了这个device

       if (isDefault && mLogicalDisplays.get(Display.DEFAULT_DISPLAY)!= null) {

            Slog.w(TAG, "Ignoringattempt to add a second default display: " + deviceInfo);

            isDefault = false;

       }

       //不是主屏,但是mSingleDisplayDemoMode为真,即设置了单屏模式

       //这时候不会去为这个物理display创建新的logical display,如果

       //hdmi,这时候会mirror主屏的内容

       //看来逻辑display就是和显示的内容有很大的关系

       if (!isDefault &&mSingleDisplayDemoMode) {

            Slog.i(TAG, "Notcreating a logical display for a secondary display "

                    + " becausesingle display demo mode is enabled: " + deviceInfo);

            return;

       }

 

       finalint displayId = assignDisplayIdLocked(isDefault);

       // layerStack 就是displayId id,主屏0hdmi 1

       finalint layerStack = assignLayerStackLocked(displayId);

 

       //新建LogicalDisplay

       LogicalDisplay display = new LogicalDisplay(displayId, layerStack, device);

       display.updateLocked(mDisplayDevices);

       if (!display.isValidLocked()) {

           // This shouldnever happen currently.

            Slog.w(TAG, "Ignoringdisplay device because the logical display "

                    + "created fromit was not considered valid: " + deviceInfo);

            return;

       }

 

       mLogicalDisplays.put(displayId, display);

 

       //如果添加的是built-in display,需要mSyncRoot.notifyAll()

       //因为systemserver.java中调用了waitForDefaultDisplay,这时候systemserver可以继续运行了

       // Wake up waitForDefaultDisplay.

       if (isDefault) {

            mSyncRoot.notifyAll();

       }

   //mHandler发消息去处理

       sendDisplayEventLocked(displayId,DisplayManagerGlobal.EVENT_DISPLAY_ADDED);

   }

   /*--------------LogicalDisplay.java---------------------------*/

   publicLogicalDisplay(int displayId, int layerStack, DisplayDevice primaryDisplayDevice) {

       mDisplayId = displayId;

       mLayerStack = layerStack;

       mPrimaryDisplayDevice = primaryDisplayDevice;

   }

进而去调用mHandlerdeliverDisplayEvent,先不去管mCallbacks是在哪里注册的,进而会去调用mTempCallbacks.get(i).notifyDisplayEventAsync(displayId, event);

  /*--------------DisplayManagerService.java---------------------------*/

  privatevoiddeliverDisplayEvent(int displayId, intevent) {

       if (DEBUG) {

            Slog.d(TAG, "Deliveringdisplay event: displayId="

                    + displayId + ",event=" + event);

       }

 

       // Grab the lock and copy the callbacks.

       final int count;

       synchronized (mSyncRoot) {

       //这里的mCallbacks是哪里注册的??

            count = mCallbacks.size();

            mTempCallbacks.clear();

            for (int i = 0; i < count; i++) {

               mTempCallbacks.add(mCallbacks.valueAt(i));

            }

       }

 

       // After releasing the lock, send the notifications out.

       for (int i = 0; i < count; i++) {

            mTempCallbacks.get(i).notifyDisplayEventAsync(displayId, event);

       }

       mTempCallbacks.clear();

   }

2. 
mCallbacks
的由来, 
上面在添加了默认displayLogicalDisplay后,会去调用下面代码,使得systemserver.javawaitForDefaultDisplay()中返回。

   /*--------------systemserver.java---------------------------*/

       if (isDefault) {

            mSyncRoot.notifyAll();

       }

进而,systemserver.javainitAndLoop() 会去注册WindowManagerService

   /*--------------systemserver.java---------------------------*/

            wm = WindowManagerService.main(context, power, display, inputManager,

                    wmHandler, factoryTest !=SystemServer.FACTORY_TEST_LOW_LEVEL,

                    !firstBoot, onlyCore);

下面分析下,WindowManagerService的构造函数中做了什么和display相关的,

   /*--------------WindowManagerService.java---------------------------*/

/*

class WindowManagerService extends IWindowManager.Stub

        implementsWatchdog.Monitor, WindowManagerPolicy.WindowManagerFuncs,

               DisplayManagerService.WindowManagerFuncs, DisplayManager.DisplayListener

*/

   //WindowManagerService实现了DisplayManager.DisplayListener接口

 

   privateWindowManagerService(Context context, PowerManagerService pm,

            DisplayManagerServicedisplayManager, InputManagerService inputManager,

            boolean haveInputMethods, boolean showBootMsgs, boolean onlyCore) {

 

   mDisplayManagerService = displayManager;

   //新建个DisplayManager

   mDisplayManager =(DisplayManager)context.getSystemService(Context.DISPLAY_SERVICE);

   //由于WindowManagerService实现了DisplayManager.DisplayListener接口,

   //WindowManagerService注册到DisplayManager

   mDisplayManager.registerDisplayListener(this, null);   

 

   }

   /*--------------DisplayManager.java---------------------------*/

       publicDisplayManager(Context context) {

       mContext = context;

       mGlobal = DisplayManagerGlobal.getInstance();

   }      

/*--------------DisplayManagerGlobal.java---------------------------*/

 

// class DisplayManagerService extendsIDisplayManager.Stub

  publicstatic DisplayManagerGlobal getInstance() {

       synchronized (DisplayManagerGlobal.class) {

            if (sInstance == null) {

                IBinder b =ServiceManager.getService(Context.DISPLAY_SERVICE);

                if (b != null) {

               //输入参数是DisplayManagerService 的代理

                    sInstance = new DisplayManagerGlobal(IDisplayManager.Stub.asInterface(b));

                }

            }

            return sInstance;

       }

   }

 

publicfinalclassDisplayManagerGlobal {

 

   //mDmDisplayManagerService 的代理,用来和DisplayManagerService打交道

   privatefinal IDisplayManager mDm;

 

       privateDisplayManagerGlobal(IDisplayManager dm) {

       mDm = dm;

   }

 

}          

接着将WindowManagerService注册到DisplayManager中,

   /*--------------DisplayManager.java---------------------------*/

       //输入参数为WindowManagerService,null   

       publicvoidregisterDisplayListener(DisplayListenerlistener, Handler handler) {

       mGlobal.registerDisplayListener(listener, handler);

   }      

   /*--------------DisplayManagerGlobal.java---------------------------*/

       //DisplayManagerGlobal

       publicvoidregisterDisplayListener(DisplayListenerlistener, Handler handler) {

       if (listener == null) {

            thrownew IllegalArgumentException("listenermust not be null");

       }

  // private final ArrayList<DisplayListenerDelegate>mDisplayListeners =

   //        newArrayList<DisplayListenerDelegate>();

       synchronized (mLock) {

            int index = findDisplayListenerLocked(listener);

            if (index < 0) {

             //新建一个display listener的代理者

                mDisplayListeners.add(new DisplayListenerDelegate(listener, handler));

             //DisplayManagerGlobal DisplayManagerService 联系起来

            registerCallbackIfNeededLocked();

            }

       }

   }

 

 publicDisplayListenerDelegate(DisplayListenerlistener, Handler handler) {

            super(handler != null ?handler.getLooper() : Looper.myLooper(), null, true/*async*/);

            mListener = listener;

       }      

/*--------------DisplayManagerGlobal.java---------------------------*/

//     DisplayManagerGlobal DisplayManagerService 联系起来

       privatevoidregisterCallbackIfNeededLocked() {

       if (mCallback == null) {

            mCallback = new DisplayManagerCallback();

            try {

                //调用DisplayManagerService registerCallback

                //DisplayManagerGlobal中的DisplayManagerCallback注册到DisplayManagerService

               mDm.registerCallback(mCallback);

            } catch (RemoteException ex) {

                Log.e(TAG, "Failed toregister callback with display manager service.", ex);

                mCallback = null;

           }

       }

   }  

privatefinalclassDisplayManagerCallbackextendsIDisplayManagerCallback.Stub {

       @Override

       publicvoidonDisplayEvent(int displayId, int event) {

            if (DEBUG) {

                Log.d(TAG, "onDisplayEvent:displayId=" + displayId + ",event=" + event);

            }

            handleDisplayEvent(displayId,event);

       }

   }  

DisplayManagerService中调用registerCallback,将DisplayManagerCallback保存到mCallbacks中,

   /*--------------DisplayManagerService.java---------------------------*/

  @Override// Binder call

   publicvoidregisterCallback(IDisplayManagerCallbackcallback) {

       if (callback == null) {

            thrownew IllegalArgumentException("listenermust not be null");

       }

 

       synchronized (mSyncRoot) {

            int callingPid = Binder.getCallingPid();

            if(mCallbacks.get(callingPid) != null) {

                thrownew SecurityException("Thecalling process has already "

                        + "registeredan IDisplayManagerCallback.");

            }

 

            CallbackRecord record = new CallbackRecord(callingPid, callback);

            try {

                IBinder binder =callback.asBinder();

                binder.linkToDeath(record, 0);

            } catch (RemoteException ex) {

                // give up

                thrownew RuntimeException(ex);

            }

       //保存的是CallbackRecord

            mCallbacks.put(callingPid, record);

       }

   }

   /*--------------DisplayManagerService.java---------------------------*/

   privatefinalclassCallbackRecordimplementsDeathRecipient {

       publicfinalint mPid;

       privatefinal IDisplayManagerCallback mCallback;

 

       publicboolean mWifiDisplayScanRequested;

 

       publicCallbackRecord(int pid, IDisplayManagerCallback callback){

            mPid = pid;

            mCallback = callback;

       }

 

       @Override

       publicvoidbinderDied() {

            if (DEBUG) {

                Slog.d(TAG, "Displaylistener for pid " + mPid + "died.");

            }

            onCallbackDied(this);

       }

 

       publicvoidnotifyDisplayEventAsync(int displayId, int event) {

            try {

                mCallback.onDisplayEvent(displayId,event);

            } catch (RemoteException ex) {

                Slog.w(TAG, "Failed tonotify process "

                        + mPid + " thatdisplays changed, assuming it died.", ex);

                binderDied();

            }

       }

   }

3. 
回到最开始的deliverDisplayEvent函数,会调用CallbackRecordnotifyDisplayEventAsync(displayId,event),进而mCallback.onDisplayEvent(displayId, event),进而调用DisplayManagerGlobalhandleDisplayEvent(displayId, event)

/*--------------DisplayManagerService.java---------------------------*/

 privatevoiddeliverDisplayEvent(int displayId, intevent) {

       if (DEBUG) {

            Slog.d(TAG, "Deliveringdisplay event: displayId="

                    + displayId + ",event=" + event);

       }

 

       // Grab the lock and copy the callbacks.

       final int count;

       synchronized (mSyncRoot) {

       //这里的mCallbacks是哪里注册的??

            count = mCallbacks.size();

            mTempCallbacks.clear();

            for (int i = 0; i < count; i++) {

                mTempCallbacks.add(mCallbacks.valueAt(i));

            }

       }

 

       // After releasing the lock, send the notifications out.

       for (int i = 0; i < count; i++) {

            mTempCallbacks.get(i).notifyDisplayEventAsync(displayId, event);

       }

       mTempCallbacks.clear();

   }

handleDisplayEvent中的mDisplayListeners就是前面注册的DisplayListenerDelegate,其中的listenerhandler分别为WindowManagerServicenull

 

/*--------------DisplayManagerGlobal.java---------------------------*/

   privatevoidhandleDisplayEvent(int displayId, intevent) {

       synchronized (mLock) {

            if (USE_CACHE) {

               mDisplayInfoCache.remove(displayId);

 

                if (event == EVENT_DISPLAY_ADDED || event == EVENT_DISPLAY_REMOVED) {

                    mDisplayIdCache = null;

                }

            }

 

            final int numListeners = mDisplayListeners.size();

            for (int i = 0; i < numListeners; i++) {

                mDisplayListeners.get(i).sendDisplayEvent(displayId, event);

            }

       }

   }

调用DisplayListenerDelegate sendDisplayEvent,进而调用mListener.onDisplayAdded(msg.arg1);,即WindowManagerServiceonDisplayAdded

 

/*--------------DisplayManagerGlobal.java---------------------------*/

 privatestaticfinalclassDisplayListenerDelegateextendsHandler {

       publicfinal DisplayListener mListener;

 

       publicDisplayListenerDelegate(DisplayListener listener, Handler handler) {

            super(handler != null ?handler.getLooper() : Looper.myLooper(), null, true/*async*/);

            mListener = listener;

       }

 

       publicvoidsendDisplayEvent(int displayId, int event) {

            Message msg = obtainMessage(event,displayId, 0);

            sendMessage(msg);

       }

 

       publicvoidclearEvents() {

            removeCallbacksAndMessages(null);

       }

 

       @Override

       publicvoidhandleMessage(Message msg) {

            switch (msg.what) {

                case EVENT_DISPLAY_ADDED:

                   mListener.onDisplayAdded(msg.arg1);

                    break;

                case EVENT_DISPLAY_CHANGED:

                    mListener.onDisplayChanged(msg.arg1);

                    break;

                case EVENT_DISPLAY_REMOVED:

                   mListener.onDisplayRemoved(msg.arg1);

                    break;

            }

       }

   }

}

转到WindowManagerService中,

   /*--------------WindowManagerService.java---------------------------*/

   @Override

   publicvoidonDisplayAdded(int displayId) {

       mH.sendMessage(mH.obtainMessage(H.DO_DISPLAY_ADDED, displayId, 0));

   }

   /*--------------WindowManagerService.java---------------------------*/

                    case DO_DISPLAY_ADDED:

                    synchronized (mWindowMap) {

                       handleDisplayAddedLocked(msg.arg1);

                    }

                    break;

 

       privatevoidhandleDisplayAddedLocked(int displayId) {

       final Display display =mDisplayManager.getDisplay(displayId);

       if (display != null) {

           createDisplayContentLocked(display);

            displayReady(displayId);

       }

   }

调用DisplayManagergetDisplay

   /*--------------DisplayManager.java---------------------------*/

       public Display getDisplay(int displayId) {

       synchronized (mLock) {

            return getOrCreateDisplayLocked(displayId, false/*assumeValid*/);

       }

   }

   /*--------------DisplayManager.java---------------------------*/

   //  private finalSparseArray<Display> mDisplays = new SparseArray<Display>();

       private Display getOrCreateDisplayLocked(int displayId, boolean assumeValid) {

       Display display = mDisplays.get(displayId);

       if (display == null) {

            display =mGlobal.getCompatibleDisplay(displayId,

                   mContext.getDisplayAdjustments(displayId));

            if (display != null) {

                mDisplays.put(displayId,display);

            }

       } elseif (!assumeValid && !display.isValid()) {

            display = null;

       }

       return display;

   }

   /*--------------DisplayManagerGlobal.java---------------------------*/

 

   public Display getCompatibleDisplay(int displayId, DisplayAdjustments daj) {

       DisplayInfo displayInfo = getDisplayInfo(displayId);

       if (displayInfo == null) {

            returnnull;

       }

       returnnew Display(this, displayId, displayInfo, daj);

   }

/*--------------DisplayManagerGlobal.java---------------------------*/

//mDm DisplayManagerService

    public DisplayInfo getDisplayInfo(int displayId) {

       try {

            synchronized (mLock) {

               DisplayInfo info;

                if (USE_CACHE) {

                    info = mDisplayInfoCache.get(displayId);

                    if (info != null) {

                        return info;

                    }

                }

       //调用DisplayManagerServicegetDisplayInfo,获取显示器信息

                info =mDm.getDisplayInfo(displayId);

                if (info == null) {

                    returnnull;

                }

 

                if (USE_CACHE) {

                    mDisplayInfoCache.put(displayId,info);

                }

                //前面已经注册过了,mCallback不为Null

               registerCallbackIfNeededLocked();

 

                if (DEBUG) {

                    Log.d(TAG, "getDisplayInfo:displayId=" + displayId + ", info=" + info);

                }

                return info;

            }

       } catch (RemoteException ex) {

            Log.e(TAG, "Could not getdisplay information from display manager.", ex);

            returnnull;

       }

   }

   /*--------------DisplayManagerGlobal.java---------------------------*/

   //已经注册过了,mCallback不为Null

       privatevoidregisterCallbackIfNeededLocked() {

       if (mCallback == null) {

            mCallback = new DisplayManagerCallback();

            try {

               mDm.registerCallback(mCallback);

            } catch (RemoteException ex) {

                Log.e(TAG, "Failed toregister callback with display manager service.", ex);

                mCallback = null;

            }

       }

   }

   /*--------------WindowManagerService.java---------------------------*/

   //Display已经创建,创建display content

   publicvoidcreateDisplayContentLocked(final Display display) {

       if (display == null) {

            thrownew IllegalArgumentException("getDisplayContent:display must not be null");

       }

       getDisplayContentLocked(display.getDisplayId());

   }

       /** All DisplayContents in the world, kept here */

   // SparseArray<DisplayContent> mDisplayContents = newSparseArray<DisplayContent>(2);

 

       public DisplayContent getDisplayContentLocked(finalint displayId) {

       DisplayContent displayContent = mDisplayContents.get(displayId);

       if (displayContent == null) {

            final Display display = mDisplayManager.getDisplay(displayId);

            // 走到这个分支,创建displayContent

            if (display != null) {

                displayContent =newDisplayContentLocked(display);

            }

       }

       return displayContent;

   }

新建一个DisplayContent,保存到WindowManagerServicemDisplayContents(displayId, displayContent)

   private DisplayContent newDisplayContentLocked(final Display display) {

       DisplayContent displayContent = new DisplayContent(display, this);

       final int displayId = display.getDisplayId();

       mDisplayContents.put(displayId, displayContent);

 

       DisplayInfo displayInfo = displayContent.getDisplayInfo();

       final Rect rect = new Rect();

       mDisplaySettings.getOverscanLocked(displayInfo.name, rect);

       synchronized (displayContent.mDisplaySizeLock) {

            displayInfo.overscanLeft = rect.left;

            displayInfo.overscanTop = rect.top;

            displayInfo.overscanRight = rect.right;

            displayInfo.overscanBottom = rect.bottom;

            mDisplayManagerService.setDisplayInfoOverrideFromWindowManager(

                    displayId, displayInfo);

       }

       configureDisplayPolicyLocked(displayContent);

 

       // TODO: Create an input channel for each display with touch capability.

       if (displayId == Display.DEFAULT_DISPLAY) {

            displayContent.mTapDetector = new StackTapPointerEventListener(this, displayContent);

           registerPointerEventListener(displayContent.mTapDetector);

       }

 

       return displayContent;

   }

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值