【framework】WindowContainer简介

1 前言

​ WindowContainer 继承自 ConfigurationContainer,是 WMS 家族的重要基类。ConfigurationContainer简介 中,已介绍 ConfigurationContainer。

img

​ 本文主要介绍 WindowContainer、SurfaceControl、SurfaceAnimator、WindowContainerController 及其相关类。

2 源码

2.1 SurfaceControl

​ 源码地址→/frameworks/base/core/java/android/view/SurfaceControl.java

(1)主要属性

private final String mName

//事务
static Transaction sGlobalTransaction

private int mWidth

private int mHeight

​ 说明:Transaction 实现了 Closeable 接口,是 SurfaceControl 的静态内部类,里面封装了一系列 native 方法,SurfaceControl 的各种操作,最终都会调用 Transaction 的相应方法。

(2)获取属性

public int getWidth()

public int getHeight()

(3)事务相关

//开启事务:sGlobalTransaction = new Transaction()
public static void openTransaction()

//关闭事务:sGlobalTransaction.apply(sync)
private static void closeTransaction(boolean sync)

(4)图层、位置、尺寸

//sGlobalTransaction.setLayer(this, zorder)
public void setLayer(int zorder)

//sGlobalTransaction.setPosition(this, x, y)
public void setPosition(float x, float y)

//sGlobalTransaction.setSize(this, w, h)
public void setSize(int w, int h)

(5)显示、隐藏

//sGlobalTransaction.show(this)
public void show()

//sGlobalTransaction.hide(this)
public void hide()

(6)透明度、颜色、阵点

//sGlobalTransaction.setAlpha(this, alpha)
public void setAlpha(float alpha)

//sGlobalTransaction.setColor(this, color)
public void setColor(float[] color)

//sGlobalTransaction.setMatrix(this, dsdx, dtdx, dtdy, dsdy)
public void setMatrix(float dsdx, float dtdx, float dtdy, float dsdy)

(7)Display 相关

//nativeCreateDisplay(name, secure)
public static IBinder createDisplay(String name, boolean secure) 

//sGlobalTransaction.setDisplaySurface(displayToken, surface)
public static void setDisplaySurface(IBinder displayToken, Surface surface)

//sGlobalTransaction.setDisplaySize(displayToken, width, height)
public static void setDisplaySize(IBinder displayToken, int width, int height)

public static SurfaceControl.PhysicalDisplayInfo[] getDisplayConfigs(IBinder displayToken)

(8)截屏

//nativeScreenshot(display, consumer, sourceCrop, width, height, minLayer, maxLayer, allLayers, useIdentityTransform)
private static void screenshot(IBinder display, Surface consumer, Rect sourceCrop, int width, int height, int minLayer, int maxLayer, boolean allLayers, boolean useIdentityTransform)

(9)内部类

SurfaceControl 的内部类主要包含 Builder、Transaction、PhysicalDisplayInfo,这里仅介绍 Builder。

public static class Builder {
	private SurfaceSession mSession;
	private int mFlags = HIDDEN;
	private int mWidth;
	private int mHeight;
	private int mFormat = PixelFormat.OPAQUE;
	private String mName;
	private SurfaceControl mParent;
	private int mWindowType = -1;
	private int mOwnerUid = -1;

	public SurfaceControl build() {
		...
		return new SurfaceControl(mSession, mName, mWidth, mHeight, mFormat, mFlags, mParent, mWindowType, mOwnerUid);
        //mNativeObject = nativeCreate(session, name, w, h, format, flags, parent != null ? parent.mNativeObject : 0, windowType, ownerUid)
	}
}

​ SurfaceSession 类如下。

public final class SurfaceSession {
    private long mNativeClient; // SurfaceComposerClient

    //创建与 SurfaceFlinger 的连接:mNativeClient = nativeCreate()
    public SurfaceSession()
    
    //创建与 SurfaceFlinger 的连接:mNativeClient = nativeCreateScoped(root.mNativeObject)
    public SurfaceSession(Surface root)

    //nativeDestroy(mNativeClient)
    protected void finalize()

    //nativeKill(mNativeClient)
    public void kill()
}

2.2 SurfaceAnimator

​ 源码地址→/frameworks/base/services/core/java/com/android/server/wm/SurfaceAnimator.java

(1)内部接口:OnAnimationFinishedCallback、Animatable

interface OnAnimationFinishedCallback {
    void onAnimationFinished(AnimationAdapter anim); //动画结束时调用
}

interface Animatable {
    //获取待提交的事务
    Transaction getPendingTransaction();
    //提交事务
    void commitPendingTransaction();
    //在leash创建时执行
    void onAnimationLeashCreated(Transaction t, SurfaceControl leash);
    //在leash销毁时执行
    void onAnimationLeashDestroyed(Transaction t);
    //生成leash
    SurfaceControl.Builder makeAnimationLeash();
    //获取Leash父节点
    SurfaceControl getAnimationLeashParent();
    //获取动画的SurfaceControl
    SurfaceControl getSurfaceControl();
    //获取动画Surface的父SurfaceControl
    SurfaceControl getParentSurfaceControl();
    //获取Surface宽度
    int getSurfaceWidth();
    //获取Surface高度
    int getSurfaceHeight();
    //动画结束时执行
    default boolean shouldDeferAnimationFinish(Runnable endDeferFinishCallback) {
        return false;
    }
}

(2)主要属性

private final WindowManagerService mService

private AnimationAdapter mAnimation

SurfaceControl mLeash

private final Animatable mAnimatable

private final OnAnimationFinishedCallback mInnerAnimationFinishedCallback

final Runnable mAnimationFinishedCallback

private boolean mAnimationStartDelayed

(3)主要方法

//mAnimation.startAnimation(mLeash, t, mInnerAnimationFinishedCallback)
void startAnimation(Transaction t, AnimationAdapter anim, boolean hidden)

//mAnimatable.commitPendingTransaction()
void endDelayingAnimationStart()

//t.setLayer(mLeash != null ? mLeash : mAnimatable.getSurfaceControl(), layer)
void setLayer(Transaction t, int layer)

//t.reparent(mLeash != null ? mLeash : mAnimatable.getSurfaceControl(), newParent.getHandle())
void reparent(Transaction t, SurfaceControl newParent)

//mService.scheduleAnimationLocked()
private void reset(Transaction t, boolean destroyLeash)

2.3 WindowContainerController

​ 源码地址→/frameworks/base/services/core/java/com/android/server/wm/WindowContainerController.java

(1)类定义

//E 为管理的 WindowContainer 类型
class WindowContainerController<E extends WindowContainer, I extends WindowContainerListener> implements ConfigurationContainerListener

(2)主要属性

//管理的 WindowContainer
E mContainer

final I mListener

(3)主要方法

//mContainer = container
void setContainer(E container)

//mContainer.setController(null)、mContainer = null
void removeContainer()

//mContainer.onOverrideConfigurationChanged(overrideConfiguration)
public void onOverrideConfigurationChanged(Configuration overrideConfiguration)

2.4 WindowContainer

​ 源码地址→/frameworks/base/services/core/java/com/android/server/wm/WindowContainer.java

WindowContainer 是 AppWindowToken、Task、TaskStack、DisplayContent 的基类,用于管理窗口配置。

(1)类定义

//E为子节点类型
class WindowContainer<E extends WindowContainer> extends ConfigurationContainer<E> implements Comparable<WindowContainer>, Animatable

(2)主要属性

//父节点
private WindowContainer<WindowContainer> mParent

//WindowList 继承 ArrayList,用于存储孩子节点
protected final WindowList<E> mChildren = new WindowList<E>()

protected int mOrientation = SCREEN_ORIENTATION_UNSPECIFIED

//SynchronizedPool 为同步对象池,通过 acquire() 方法获取对象,release() 方法释放对象,避免了反复创建对象,节省了系统资源,同时保证了线程安全
private final Pools.SynchronizedPool<ForAllWindowsConsumerWrapper> mConsumerWrapperPool = new Pools.SynchronizedPool<>(3)

//窗口容器控制者,每个 WindowContainer 与 WindowContainerController 一一对应
WindowContainerController mController

protected DisplayContent mDisplayContent

protected SurfaceControl mSurfaceControl

private SurfaceControl mLastRelativeToLayer = null

//当前窗口容器的图层序数
private int mLastLayer = 0

//事务
protected final Transaction mPendingTransaction

protected final SurfaceAnimator mSurfaceAnimator

protected final WindowManagerService mWmService

//当前窗口容器的位置(左上角位置)
protected final Point mLastSurfacePosition = new Point()

//以当前窗口容器为根节点的子树的叶子节点数
private int mTreeWeight = 1

(3)获取/注入属性

//return mParent
final protected WindowContainer getParent()

//mParent = parent、onConfigurationChanged(mParent.getConfiguration())、onMergedOverrideConfigurationChanged()
setParent(WindowContainer<WindowContainer> parent)

//getOrientation(mOrientation)
int getOrientation()

//mOrientation = orientation
void setOrientation(int orientation)

//mController
WindowContainerController getController()

//controller.setContainer(this)、mController = controller
void setController(WindowContainerController controller)

//mDisplayContent
DisplayContent getDisplayContent()

//mSurfaceControl
public SurfaceControl getSurfaceControl()

//mPendingTransaction
public Transaction getPendingTransaction()

(4)节点相关

//return mChildren.size()
protected int getChildCount()

//是否包含指定子节点(遍历当前节点的所有子孙节点)
boolean hasChild(E child)

//return mChildren.get(index)
protected E getChildAt(int index)

//return mChildren.peekLast()
E getTopChild()

void addChild(E child, int index)

void removeChild(E child)

void removeIfPossible()

//删除当前节点及其所有叶子节点
void removeImmediately()

//获取当前节点前缀序号(当前节点左边的叶子节点数+1)
int getPrefixOrderIndex()

//当前节点在父节点的孩子数组中,是否处于最后一个:mParent.getTopChild() == this && mParent.isOnTop()
boolean isOnTop()

//是否有可见的子节点:mChildren.get(i).isVisible()
boolean isVisible()

(5)Layer 相关

//mChildren.get(j).assignLayer(t, layer++)
void assignChildLayers(Transaction t)

//setLayer(t, layer)、mLastLayer = layer
void assignLayer(Transaction t, int layer)

//mSurfaceAnimator.setLayer(t, layer)
protected void setLayer(Transaction t, int layer)

//mParent.assignChildLayers()
void onParentSet()

(6)Configuration 相关

public void onConfigurationChanged(Configuration newParentConfig)

public void onOverrideConfigurationChanged(Configuration overrideConfiguration)

//当前节点及其子孙的 overrideConfiguration 改变了
void onDescendantOverrideConfigurationChanged()

(7)Surface 相关

//mSurfaceControl.getWidth()
public int getSurfaceWidth()

//mSurfaceControl.getHeight()
public int getSurfaceHeight()

//更新 surface 位置
//mPendingTransaction.setPosition(mSurfaceControl, mTmpPos.x, mTmpPos.y)
//mLastSurfacePosition.set(mTmpPos.x, mTmpPos.y)
void updateSurfacePosition()

//mParent.makeChildSurface(child).setParent(mSurfaceControl)
SurfaceControl.Builder makeChildSurface(WindowContainer child)

//mParent.makeChildSurface(this)
SurfaceControl.Builder makeSurface()

//mParent.getSurfaceControl()
public SurfaceControl getParentSurfaceControl()

//mSurfaceAnimator.reparent(t, newParent)
protected void reparentSurfaceControl(Transaction t, SurfaceControl newParent)

// mChildren.get(i).prepareSurfaces()
void prepareSurfaces()

//mParent.getSession()
SurfaceSession getSession()

(8)Resize

void onResize()

void onMovedByResize()

void setWaitingForDrawnIfResizingChanged()

void resetDragResizingChangeReported()

(9)动画相关

//判断当前节点是否处于 Animating 状态:mSurfaceAnimator.isAnimating()
boolean isSelfAnimating()

//判断当前节点或其子孙节点是否处于 Animating 状态
boolean isSelfOrChildAnimating()

//mParent.getAppAnimationLayer(animationLayer)
SurfaceControl getAppAnimationLayer(int animationLayer)

//mParent.scheduleAnimation()
void scheduleAnimation()

//mSurfaceAnimator.startAnimation(t, anim, hidden)
void startAnimation(Transaction t, AnimationAdapter anim, boolean hidden)

//mSurfaceAnimator.transferAnimation(from.mSurfaceAnimator)
void transferAnimation(WindowContainer from)

(10)Display 相关

//mChildren.get(i).hasContentToDisplay()
boolean hasContentToDisplay()

//mChildren.get(i).onDisplayChanged(dc)
void onDisplayChanged(DisplayContent dc)

(11)回调

//回调
boolean forAllWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom)

//ForAllWindowsConsumerWrapper wrapper = obtainConsumerWrapper(callback) //对 Consumer 进行装饰
//forAllWindows(wrapper, traverseTopToBottom)
void forAllWindows(Consumer<WindowState> callback, boolean traverseTopToBottom)

void forAllTasks(Consumer<Task> callback)

WindowState getWindow(Predicate<WindowState> callback)

(12)其他方法

//通知 Clients 当前窗口可见:mChildren.get(i).sendAppVisibilityToClients()
void sendAppVisibilityToClients()

//mChildren.get(i).checkAppWindowsReadyToShow()
void checkAppWindowsReadyToShow()

//mChildren.get(i).onAppTransitionDone()
void onAppTransitionDone()

//mChildren.get(i).applyMagnificationSpec(t, spec)
void applyMagnificationSpec(Transaction t, MagnificationSpec spec)

(13)内部类

​ 消费者装饰类:

private final class ForAllWindowsConsumerWrapper implements ToBooleanFunction<WindowState> {
    private Consumer<WindowState> mConsumer;

    void setConsumer(Consumer<WindowState> consumer) {
        mConsumer = consumer;
    }

    @Override
    public boolean apply(WindowState w) {
        mConsumer.accept(w);
        return false;
    }

    void release() {
        mConsumer = null;
        mConsumerWrapperPool.release(this);
    }
}

​ 其中,ToBooleanFunction 接口如下:

public interface ToBooleanFunction<T> {
    boolean apply(T value);
}

(14)依赖接口

​ 消费者接口:

public interface Consumer<T> {
    void accept(T t);

    default Consumer<T> andThen(Consumer<? super T> after) {
        Objects.requireNonNull(after);
        return (T t) -> { accept(t); after.accept(t); };
    }
}

​ 谓词接口

public interface Predicate<T> {
    boolean test(T t);
    
    //与运算:return (t) -> test(t) && other.test(t)
    default Predicate<T> and(Predicate<? super T> other)
    
    //非运算:return (t) -> !test(t)
    default Predicate<T> negate()
    
    //或运算:return (t) -> test(t) || other.test(t)
    default Predicate<T> or(Predicate<? super T> other)

    //判等运算:return object -> targetRef.equals(object)
    static <T> Predicate<T> isEqual(Object targetRef)
}

​ 声明:本文转自【framework】WindowContainer简介

posted @   little_fat_sheep  阅读(142)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
点击右上角即可分享
微信分享提示