当前位置:网站首页>Viewrootimpl and windowmanagerservice notes

Viewrootimpl and windowmanagerservice notes

2022-07-05 20:53:00 Full stack programmer webmaster

Hello everyone , I meet you again , I'm the king of the whole stack , I've prepared for you today Idea Registration code .

1、 Of each form ViewRootImpl There is one. mWindowAttributes Form properties , This attribute in WindowManagerGlobal.updateViewLayout()->ViewRootImpl.setView() and WindowManagerGlobal.updateViewLayout->ViewRootImpl.setLayoutParams() Assignment in . At the same time ViewRootImpl.mWindowAttributesChanged It will also be set to true Indicates that the form properties have changed . When form properties change .surfaceChanged It will also be set to true

 if (mWindowAttributesChanged) {
            mWindowAttributesChanged = false;
            surfaceChanged = true;
            params = lp;
        }

When surfaceChanged Set to true when , The following code will call

if (surfaceChanged) {
                        mSurfaceHolderCallback.surfaceChanged(mSurfaceHolder,
                                lp.format, mWidth, mHeight);
                        SurfaceHolder.Callback callbacks[] = mSurfaceHolder.getCallbacks();
                        if (callbacks != null) {
                            for (SurfaceHolder.Callback c : callbacks) {
                                c.surfaceChanged(mSurfaceHolder, lp.format,
                                        mWidth, mHeight);
                            }
                        }
                    }

Call callback function , Indicates the current form Surface There are updates .

2、 stay WMS in Stackbox、TaskStack、Task、AppWindowToken The relationship between :

3、relayoutWindow() Function for visible wallpaper 、 typewriting 、activity Wait for the window to be handled as follows :

            if (viewVisibility == View.VISIBLE &&
                    (win.mAppToken == null || !win.mAppToken.clientHidden)) {
                toBeDisplayed = !win.isVisibleLw();
                if (win.mExiting) {
                    winAnimator.cancelExitAnimationForNextAnimationLocked();
                    win.mExiting = false;
                }
                if (win.mDestroying) {
                    win.mDestroying = false;
                    mDestroySurface.remove(win);
                }
                if (oldVisibility == View.GONE) {
                    winAnimator.mEnterAnimationPending = true;
                }
                if (toBeDisplayed) {
                    if (win.isDrawnLw() && okToDisplay()) {
                        winAnimator.applyEnterAnimationLocked();
                    }
                    if ((win.mAttrs.flags
                            & WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON) != 0) {
                        if (DEBUG_VISIBILITY) Slog.v(TAG,
                                "Relayout window turning screen on: " + win);
                        win.mTurnOnScreen = true;
                    }
                    if (win.isConfigChanged()) {
                        if (DEBUG_CONFIGURATION) Slog.i(TAG, "Window " + win
                                + " visible with new config: " + mCurConfiguration);
                        outConfig.setTo(mCurConfiguration);
                    }
                }
                if ((attrChanges&WindowManager.LayoutParams.FORMAT_CHANGED) != 0) {
                    // To change the format, we need to re-build the surface.
                    winAnimator.destroySurfaceLocked();
                    toBeDisplayed = true;
                    surfaceChanged = true;
                }
                try {
                    if (!win.mHasSurface) {
                        surfaceChanged = true;
                    }
                    SurfaceControl surfaceControl = winAnimator.createSurfaceLocked();
                    if (surfaceControl != null) {
                        outSurface.copyFrom(surfaceControl);
                        if (SHOW_TRANSACTIONS) Slog.i(TAG,
                                "  OUT SURFACE " + outSurface + ": copied");
                    } else {
                        // For some reason there isn't a surface.  Clear the
                        // caller's object so they see the same state.
                        outSurface.release();
                    }
                } catch (Exception e) {
                    mInputMonitor.updateInputWindowsLw(true /*force*/);

                    Slog.w(TAG, "Exception thrown when creating surface for client "
                             + client + " (" + win.mAttrs.getTitle() + ")",
                             e);
                    Binder.restoreCallingIdentity(origId);
                    return 0;
                }
                if (toBeDisplayed) {
                    focusMayChange = isDefaultDisplay;
                }
                if (win.mAttrs.type == TYPE_INPUT_METHOD
                        && mInputMethodWindow == null) {
                    mInputMethodWindow = win;
                    imMayMove = true;
                }
                if (win.mAttrs.type == TYPE_BASE_APPLICATION
                        && win.mAppToken != null
                        && win.mAppToken.startingWindow != null) {
                    // Special handling of starting window over the base
                    // window of the app: propagate lock screen flags to it,
                    // to provide the correct semantics while starting.
                    final int mask =
                        WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
                        | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
                        | WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON;
                    WindowManager.LayoutParams sa = win.mAppToken.startingWindow.mAttrs;
                    sa.flags = (sa.flags&~mask) | (win.mAttrs.flags&mask);
                }
            }

Assume that the current form is exiting (win.mExiting==true), Just call cancelExitAnimationForNextAnimationLocked() Cancel exit animation . Suppose the form is being destroyed (win.mDestroying). Just change the form from mDestroySurface Remove ,mDestroySurface What is saved in is to be destroyed surface Form . Suppose a state on the form is invisible , That is, from invisible to visible ,winAnimator.mEnterAnimationPending = true; Indicates waiting to enter the animation ;

    void applyEnterAnimationLocked() {
        final int transit;
        if (mEnterAnimationPending) {
            mEnterAnimationPending = false;
            transit = WindowManagerPolicy.TRANSIT_ENTER;
        } else {
            transit = WindowManagerPolicy.TRANSIT_SHOW;
        }
        applyAnimationLocked(transit, true);
        //TODO (multidisplay): Magnification is supported only for the default display.
        if (mService.mDisplayMagnifier != null
                && mWin.getDisplayId() == Display.DEFAULT_DISPLAY) {
            mService.mDisplayMagnifier.onWindowTransitionLocked(mWin, transit);
        }
    }

mEnterAnimationPending =true when . call applyAnimationLocked() The parameter passed in by the function is WindowManagerPolicy.TRANSIT_ENTER Indicates that the form enters the animation .

    boolean applyAnimationLocked(int transit, boolean isEntrance) {
        if (mLocalAnimating && mAnimationIsEntrance == isEntrance) {
            // If we are trying to apply an animation, but already running
            // an animation of the same type, then just leave that one alone.
            return true;
        }

        // Only apply an animation if the display isn't frozen.  If it is
        // frozen, there is no reason to animate and it can cause strange
        // artifacts when we unfreeze the display if some different animation
        // is running.
        if (mService.okToDisplay()) {
            int anim = mPolicy.selectAnimationLw(mWin, transit);
            int attr = -1;
            Animation a = null;
            if (anim != 0) {
                a = anim != -1 ? AnimationUtils.loadAnimation(mContext, anim) : null;
            } else {
                switch (transit) {
                    case WindowManagerPolicy.TRANSIT_ENTER:
                        attr = com.android.internal.R.styleable.WindowAnimation_windowEnterAnimation;
                        break;
                    case WindowManagerPolicy.TRANSIT_EXIT:
                        attr = com.android.internal.R.styleable.WindowAnimation_windowExitAnimation;
                        break;
                    case WindowManagerPolicy.TRANSIT_SHOW:
                        attr = com.android.internal.R.styleable.WindowAnimation_windowShowAnimation;
                        break;
                    case WindowManagerPolicy.TRANSIT_HIDE:
                        attr = com.android.internal.R.styleable.WindowAnimation_windowHideAnimation;
                        break;
                }
                if (attr >= 0) {
                    a = mService.mAppTransition.loadAnimation(mWin.mAttrs, attr);
                }
            }
            if (WindowManagerService.DEBUG_ANIM) Slog.v(TAG,
                    "applyAnimation: win=" + this
                    + " anim=" + anim + " attr=0x" + Integer.toHexString(attr)
                    + " a=" + a
                    + " transit=" + transit
                    + " isEntrance=" + isEntrance + " Callers " + Debug.getCallers(3));
            if (a != null) {
                if (WindowManagerService.DEBUG_ANIM) {
                    RuntimeException e = null;
                    if (!WindowManagerService.HIDE_STACK_CRAWLS) {
                        e = new RuntimeException();
                        e.fillInStackTrace();
                    }
                    Slog.v(TAG, "Loaded animation " + a + " for " + this, e);
                }
                setAnimation(a);
                mAnimationIsEntrance = isEntrance;
            }
        } else {
            clearAnimation();
        }

        return mAnimation != null;
    }

applyAnimationLocked() Function , For the status bar 、 The navigation bar ( Three virtual keys )selectAnimationLw() Return to the corresponding animation resources id, For general forms, return 0. For general application forms , Yes “ Window entry ” Animation 、“ Form exit ” Animation 、“ Form display ” Animation 、“ The form is hidden ” Animation . Find the corresponding form animation resources . And then call mService.mAppTransition.loadAnimation(mWin.mAttrs, attr) Load animation .

Call after loading successfully setAnimation(a) To animate the current form .mAppTransition It's a AppTransition Class object ,AppTransition Is a transition animation state management class , There is one in this class mAppTransitionState Variable . This variable is specially used to save the current transition animation state ;AppTransition Class has two functions to create animation , One is to create enlarged animation createScaleUpAnimationLocked, One is to create a zoom out animation createThumbnailAnimationLocked().

    Animation loadAnimation(WindowManager.LayoutParams lp, int animAttr) {
        int anim = 0;
        Context context = mContext;
        if (animAttr >= 0) {
            AttributeCache.Entry ent = getCachedAnimations(lp);
            if (ent != null) {
                context = ent.context;
                anim = ent.array.getResourceId(animAttr, 0);
            }
        }
        if (anim != 0) {
            return AnimationUtils.loadAnimation(context, anim);
        }
        return null;
    }

This function calls the animation tool class AnimationUtils.loadAnimation() function . Parameters anim Animation resources id,AnimationUtils.loadAnimation() Called in createAnimationFromXml(context, parser); Static functions ,parser It's a XmlResourceParser object , Animation resources id Save in this object .createAnimationFromXml() From the name can be seen from xml The file parses out a Animation come out

    private static Animation createAnimationFromXml(Context c, XmlPullParser parser,
            AnimationSet parent, AttributeSet attrs) throws XmlPullParserException, IOException {

        Animation anim = null;

        // Make sure we are on a start tag.
        int type;
        int depth = parser.getDepth();

        while (((type=parser.next()) != XmlPullParser.END_TAG || parser.getDepth() > depth)
               && type != XmlPullParser.END_DOCUMENT) {

            if (type != XmlPullParser.START_TAG) {
                continue;
            }

            String  name = parser.getName();

            if (name.equals("set")) {
                anim = new AnimationSet(c, attrs);
                createAnimationFromXml(c, parser, (AnimationSet)anim, attrs);
            } else if (name.equals("alpha")) {
                anim = new AlphaAnimation(c, attrs);
            } else if (name.equals("scale")) {
                anim = new ScaleAnimation(c, attrs);
            }  else if (name.equals("rotate")) {
                anim = new RotateAnimation(c, attrs);
            }  else if (name.equals("translate")) {
                anim = new TranslateAnimation(c, attrs);
            } else {
                throw new RuntimeException("Unknown animation name: " + parser.getName());
            }

            if (parent != null) {
                parent.addAnimation(anim);
            }
        }

        return anim;

    }

while The loop follows a certain format ( Animation format ) analysis xml, According to different animations, create corresponding Animation object , Then return .

about AnimationSet Animation ( Animation combination mechanism ). Recursively call createAnimationFromXml() function , Save animations in a queue , Finally, return to the first animation . See from the source code android Provides composite animation 、 Gradient animation 、 Zoom animation 、 Rotate animation 、 Transition animation ( Moving animation effect . Picture browsing sliding effect ).

go back to applyAnimationLocked() Function . from xml The file parses a corresponding Animation after , Call again setAnimation(a)

public void setAnimation(Animation anim) {

        if (localLOGV) Slog.v(TAG, "Setting animation in " + this + ": " + anim);
        mAnimating = false;
        mLocalAnimating = false;
        mAnimation = anim;
        mAnimation.restrictDuration(WindowManagerService.MAX_ANIMATION_DURATION);
        mAnimation.scaleCurrentDuration(mService.mWindowAnimationScale);
        // Start out animation gone if window is gone, or visible if window is visible.
        mTransformation.clear();
        mTransformation.setAlpha(mLastHidden ? 0 : 1);        mHasLocalTransformation = true;    }

This function is very easy, Set up mAnimating、mLocalAnimating by false, Take what you got in the last step Animation Object to save to WindowStateAnimator.mAnimation in .

3、WindowManagerService in Configuration Class object mCurConfiguration The current configuration information is saved in .setNewConfiguration() Responsible for updating this object .setNewConfiguration() By ActivityManagerService.updateConfigurationLocked() call .AMS call WMS Of updateConfigurationLocked() Function configuration The object is AMS Medium mConfiguration, that AMS And WMS Keep the same configuration Configuration information .

Copyright notice : This article is an original blog article , Blog , Without consent , Shall not be reproduced .

Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/117641.html Link to the original text :https://javaforall.cn

原网站

版权声明
本文为[Full stack programmer webmaster]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/186/202207052046419873.html