Surfaceflinger Study (i)

Source: Internet
Author: User

2014 Time to study Surfaceflinger read source several times also did not make very clear, this post to blog inside to do a record.

Surfaceflinger is the most complex module in the Android system, a framework. Today, we mainly grasp the running flow, run activity from Activtythread.java and then go to activity setcontentview execution route.

When we open an application, we send a request via Activitymanagerservice to Zygote Fork a new process for running the new app. This process is implemented by socket communication.

The final execution of the Activtythread.main method starts a stand-alone message queue for the current apk work.

Basic structure diagram


We started from Activitythread.java.

Let's look at handlelaunchactivity running an activity, first of all instantiating an activity

Activity a = performlaunchactivity (R, Customintent); Look at the realization inside.

Instantiation of activity, instantiated through Instrumentation.java is actually very simple

        Activity activity = NULL;        try {            Java.lang.ClassLoader cl = R.packageinfo.getclassloader ();            Activity = minstrumentation.newactivity (                    cl, Component.getclassname (), r.intent);            Strictmode.incrementexpectedactivitycount (Activity.getclass ());            R.intent.setextrasclassloader (CL);            if (r.state! = null) {                r.state.setclassloader (CL);            }        } catch (Exception e) {            if (! Minstrumentation.onexception (activity, E)) {                throw new RuntimeException (                    "Unable to instantiate activity" + Component                    + ":" + e.tostring (), E);            }        }

Then the following

Executive Minstrumentation.callactivityoncreate (activity, r.state); That is, we programmed the entry function OnCreate method

    public void callactivityoncreate (activity activity, Bundle Icicle) {if (mwaitingactivities! = null) {                Synchronized (Msync) {final int N = Mwaitingactivities.size ();                    for (int i=0; i<n; i++) {final Activitywaiter aw = mwaitingactivities.get (i);                    Final Intent Intent = aw.intent;                        if (Intent.filterequals (Activity.getintent ())) {aw.activity = activity;                    Mmessagequeue.addidlehandler (New activitygoing (AW)); }}}} activity.performcreate (icicle);//Execute OnCreate if (mAc                Tivitymonitors! = null) {synchronized (Msync) {final int N = Mactivitymonitors.size ();                    for (int i=0; i<n; i++) {final activitymonitor am = Mactivitymonitors.get (i); Am.match (activity, activity, Activity.getintenT ()); }            }        }    }

Now that we execute to the OnCreate function method body, we will execute our Setcontentview method view layout load

Find the Setcontentview method of Activity.java directly

Inside is

GetWindow (). Setcontentview (view);

Then continue looking for the GetWindow () instance Object

Instantiate Mwindow = Policymanager.makenewwindow (this); Initialized in the Activity.attach method.

This is called in the Performlaunchactivity function.

                Activity.attach (AppContext, this, getinstrumentation (), R.token,                        r.ident, app, R.intent, R.activityinfo, title, R.parent,                        r.embeddedid, r.lastnonconfigurationinstances, config);

Continue, Policymanager is actually Com.android.internal.policy.impl.Policy class

    Public Window Makenewwindow (context context) {        return new Phonewindow (context);    }
Just look for Phonewindow.java, really do the view initialization in this place

    @Override public    void Setcontentview (view view, Viewgroup.layoutparams params) {        if (mcontentparent = = null) { C6/>installdecor ();        } else {            mcontentparent.removeallviews ();        }        Mcontentparent.addview (view, params);        Final Callback cb = Getcallback ();        if (CB! = null &&!isdestroyed ()) {            cb.oncontentchanged ();}    }


An important member was initialized in the Installdecor method Decorview

Decorview is the inner class of Phonewindow, which contains a lot of event handling and basic UI dimension calculations, headings and other processing

We initialize the Decorview and return to the front of the Threadactivity.java handleresumeactivity function body

Some important codes are as follows:

                R.window = R.activity.getwindow ();                View decor = R.window.getdecorview ();                Decor.setvisibility (view.invisible);                Viewmanager wm = A.getwindowmanager ();                Windowmanager.layoutparams L = r.window.getattributes ();                A.mdecor = Decor;                L.type = WindowManager.LayoutParams.TYPE_BASE_APPLICATION;                L.softinputmode |= forwardbit;                if (a.mvisiblefromclient) {                    a.mwindowadded = true;                    Wm.addview (decor, L);                }

The point is wm.addview to submit the Decorview to the WMS service.

Initialization of WindowManager in the activity Attach method Mwindowmanager = Mwindow.getwindowmanager ();

That's Windowmanagerimpl.java.

The actual is private final windowmanagerglobal Mglobal = Windowmanagerglobal.getinstance (); Call

And then to Windowmanagerglobal.java AddView, the most important object is Viewrootimpl.java.

Instantiation of

root = new Viewrootimpl (View.getcontext (), display);

Root.setview (view, Wparams, Panelparentview); This is the point.

This function code is very complex, mainly has the following several function comparison core

Execute first

    public void Requestlayout () {        checkthread ();        Mlayoutrequested = true;        Scheduletraversals ();    }


Scheduletraversals (); A callback function is executed inside.


Final class Traversalrunnable implements Runnable {        @Override public        void Run () {            dotraversal ();        }    }

void Dotraversal () {        if (mtraversalscheduled) {            mtraversalscheduled = false;            Mhandler.getlooper (). Removesyncbarrier (mtraversalbarrier);            if (mprofile) {                debug.startmethodtracing ("Viewancestor");            }            Trace.tracebegin (Trace.trace_tag_view, "performtraversals");            try {                performtraversals ();            } finally {                trace.traceend (Trace.trace_tag_view);            }            if (mprofile) {                debug.stopmethodtracing ();                Mprofile = false;}}}    

The Performtraversals () method is more complex, and is recursive traversal of the view to the underlying inside including Msurface canvas initialization, callback, traversal, call iwindowsession to the underlying

Surface locks the canvas for painting. This place has been looked at many times is not very clear, involving more things

You need to perform the Relayoutwindow,performlayout function and then the PERFORMDRAW to paint the canvas and lock the canvas to create the surface object

Finally, the IPC communication with the Windowmanagerservice via the Iwindowsession interface transmits surface data to the underlying Surfaceflinger system for processing


Here is a summary of the process



The next time you will be introduced to the surface process of the native layer, please indicate thank you if you have any questions.








Surfaceflinger Study (i)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.