Beyond Android Views Window Surface Special Views and
Beyond Android Views Window, Surface, Special Views and More Roger yixx@ucweb. com roger 2 yi@gmail. com www. twitter. com/roger 2 yi +易旭昕 in Google+ 6/6/2021 1
Activity and Views
View • This class represents the basic building block for user interface components. • A View occupies a rectangular area on the screen and is responsible for drawing and event handling. • View is the base class for widgets, which are used to create interactive UI components (buttons, text fields, etc. ).
Those things behind Activity and Views
System Services • Activity Management Service • Window Management Service • Suface Flinger • etc. . .
The Startup of an Activity in New Process • AMS接收到请求要在新的进程启动一个Activity • AMS转发请求给zygote进程,zygote fork一个新的进程,然后调用 Activity. Thread. main方法,Activity. Thread创建Message. Queue, attach到AMS,然后进入消息循环,这个线程就是主线程,也是所谓 的UI线程 • 当Activity. Thread attch到AMS后,AMS发送请求给Activity. Thread要 求启动Activity(LAUNCH_ACTIVITY) • Activity. Thread在消息循环中启动Activity(handle. Launch. Activity) – 创建Activity后调用on. Create(perform. Launch. Activity) – 应用则在on. Create中设置Content View,然后开始跟用户的交互. . .
android. view. Window • Abstract base class for a top-level window look and behavior policy. • It provides standard UI policies such as a background, title area, default key processing, etc. • 它是通常意义上我们理解的应用层面窗口, Top. Level. Window or Window. Policy可能是一个更 合适的名字
Phone. Window • Phone. Window是抽象类Window的唯一实现 • Activity. Thread在启动一个Activity的时候,Activity会创建 一个Phone. Window – Activity. Thread. handle. Launch. Activity – -> Activity. Thread. perform. Launch. Activity – -> Activity. attach • Activity. set. Content. View实际上是调用 Phone. Window. set. Content. View,在该方法的第一次调用 时,Phone. Window会创建一个Decor. View作为Top-Level. Window的View Hierachy的根节点
View Hierachy
Window. Manager • android. view. Window. Manager – The interface that apps use to talk to the window manager. – 真正的实现是Window. Manager. Impl,由Phone. Window在Activity 启动时创建 • • Activity. Thread. handle. Launch. Activity -> Activity. Thread. perform. Launch. Activity -> Activity. attach -> Phone. Window. set. Window. Manager – 当Activity启动后,Activity. Thread将Docor. View添加到 Window. Manager. Impl中 • Activity. Thread. handle. Launch. Activity • -> Activity. Thread. handle. Resume. Activity • -> Window. Manager. add. View
View. Root. Impl • The top of a view hierarchy, implementing the needed protocol between View and the Window. Manager • This is for the most part an internal implementation detail Window. Manager. Impl
Cont. • View. Root. Impl会打开一个IWindow. Session用于跟WMS对 话 • 并通过IWindow. Session向WMS注册一个窗口(实际上是 让WMS在服务端创建一个窗口(Window. State),并且 把该服务端的窗口跟View. Root. Impl传过来的IWindow回调 接口绑定在一起) – Activity. Thread. handle. Launch. Activity – -> Activity. Thread. handle. Resume. Activity – -> Window. Manager. Impl. add. View – -> View. Root. Impl. set. View
Overall Flow • Activity. Thead创建Activity • Activity创建Phone. Window • Phone. Window创建Decor. View作为View Hierachy的根节点,Client端设置的 Content View会attach到Decor. View下面(间接) • Phone. Window创建Window. Manager. Impl • Activity. Thead把Decor. View加到Window. Manager. Impl里面 • Window. Manager. Impl创建View. Root. Impl,并把Decor. View传给它 • View. Root. Impl打开一个Window Session跟WMS通讯,通过Session注册一个 窗口,最后客户端拥有一个IWindow回调接口,服务端拥有一个Window. State 跟该回调接口绑定
View Hierachy Rendering • 当View. Root. Impl建立跟WMS的联系,注册了窗口 后,会发出第一次渲染View Hierachy的请求 – View. Root. Impl. set. View – View. Root. Impl. request. Layout – View. Root. Impl. schedule. Traversals • 在第一次的View Hierachy的渲染中( View. Root. Impl. perform. Traversals), View. Root. Impl需要创建Surface
Cont. • 当Surface创建后,后续的渲染就可以通过获得 Surface的Canvas(Surface. lock. Canvas) • 然后遍历View Hierachy,把需要绘制的Views通 过Canvas绘制到Surface上面(View. on. Draw) • 绘制完后需要解锁(Surface. unlock. Canvas. Post) ,让Surface. Flinger可以将Surface绘制到屏幕上 (Screen Composite)
Surface • Handle onto a raw buffer that is being managed by the screen compositor. • 每个Window都有一个Surface,由窗口在客户端的代理者 View. Root. Impl所拥有 • Surface在View. Root. Impl第一次渲染View Hierachy时创建 – View. Root. Impl. perform. Traversals – View. Root. Impl. relayout. Window – IWindow. Session. relayout
Cont. • 当调用Surface. unlock. Canvas. And. Post的时候 – 置空Sk. Canvas,设置一个空的Sk. Bitmap – 将Locked Buffer解锁并返回到图像缓存队列里面 – 将Posted Buffer设置为Locked Buffer,旧的Posted Buffer就可以被下次取出来使用,设置Locked Buffer为 空 – 当Surface. Flinger下次进行Screen Composite的时候就 会把当前的Posted Buffer绘制到屏幕上
Overall Flow • View. Root. Impl第一次渲染View Hierachy的时候会创建一个Surface • 客户端请求WMS创建Surface,并返回Surface相关的数据用于构建一 个native的Surface对象,并把它跟View. Root. Impl的Surface(Java) 绑定在一起 • View. Root. Impl在渲染时先从Surface获得Canvas • 然后把需要绘制的Views通过Canvas绘制到Surface上面 • 绘制完毕后解锁该Surface • Surface. Flinger在做Screen Composite的时候把解锁后的Surface绘制 到屏幕上
Those special Views
Surface. View • Provides a dedicated drawing surface embedded inside of a view hierarchy. You can control the format of this surface and, if you like, its size; the Surface. View takes care of placing the surface at the correct location on the screen • The surface is Z ordered so that it is behind the window holding its Surface. View; the Surface. View punches a hole in its window to allow its surface to be displayed.
GLSurface. View • An implementation of Surface. View that uses the dedicated surface for displaying Open. GL rendering. • A GLSurface. View provides the following features: – Manages a surface, which is a special piece of memory that can be composited into the Android view system. – Manages an EGL display, which enables Open. GL to render into a surface. – Accepts a user-provided Renderer object that does the actual rendering. – Renders on a dedicated thread to decouple rendering performance from the UI thread. – Supports both on-demand continuous rendering. – Optionally wraps, traces, and/or error-checks the renderer's Open. GL calls.
Things become more complicated after Android 3. 0. . .
View. Root. Impl • 当Actitity的View Hierachy的根节点被添加到 Window. Manager的时候(Window. Manager. add. View), Window. Manager的实现类Window. Manager. Impl会创建一 个View. Root. Impl并把它跟传进来的View绑定在一起( View. Root. Impl. set. View),并且View. Root. Impl会根据是否 开启硬件加速来决定是否创建一个Hardware. Renderer • 在View. Root. Impl第一次渲染View Hierachy时,会创建 Surface,如果开启硬件加速,则通过Hardware. Renderer 在当前的UI线程初始化EGL绘图环境(类似 GLSurface. View)
Cont. • 在View. Root. Impl真正渲染View Hierachy时, 如果开启硬件加速,则从 Hardware. Renderer获得一个 Hardware. Canvas,然后View Hierachy的 渲染通过Hardware. Canvas来进行 • Hardware. Canvas跟一个native的 Open. GLRenderer绑定,通过它调用EGL API来完成真正的绘制
Reference • Android 圖形系統 -- 設計與實做分析 • How to replace GLSurface. View with Texture. View in Android Ice Cream Sandwich?
The End Thank you for your listening Yours Sincerely, Roger 6/6/2021 48
- Slides: 48