文章来自:http://mobile.51cto.com/android-259922_1.htm
本文详细介绍了Android中的Surface系统,采用情景分析的办法,详解了何为SurfaceFlinger,以及SurfaceFlinger的工作流程,以Activity函数调用为切入点来研究SurfaceFlinger。
JNI层
上面两个类的JNI实现都在framework/base/core/jni/android_view_Surface.cpp中。
[---->SurfaceSession:: SurfaceSession()]
- public class SurfaceSession {
- /** Create a new connection with the surface flinger. */
- public SurfaceSession() {
- init();
- }
它的init函数对应为:
[--->SurfaceSession_init]
- static void SurfaceSession_init(JNIEnv* env, jobject clazz)
- {
- //SurfaceSession对应为SurfaceComposerClient
- sp client = new SurfaceComposerClient;
- client->incStrong(clazz);
- //Google常用做法,在JAVA对象中保存C++对象的指针。
- env->SetIntField(clazz, sso.client, (int)client.get());
- }
Surface的init对应为:
[--->Surface_init]
- static void Surface_init(
- JNIEnv* env, jobject clazz,
- jobject session,
- jint pid, jstring jname, jint dpy, jint w, jint h, jint format, jint flags)
- {
- SurfaceComposerClient* client =
- (SurfaceComposerClient*)env->GetIntField(session, sso.client);
- sp surface;
- if (jname == NULL) {
- //client是SurfaceComposerClient,返回的surface是一个SurfaceControl
- //真得很复杂!
- surface = client->createSurface(pid, dpy, w, h, format, flags);
- } else {
- const jchar* str = env->GetStringCritical(jname, 0);
- const String8 name(str, env->GetStringLength(jname));
- env->ReleaseStringCritical(jname, str);
- surface = client->createSurface(pid, name, dpy, w, h, format, flags);
- }
- //把surfaceControl信息设置到Surface对象中
- setSurfaceControl(env, clazz, surface);
- }
- static void setSurfaceControl(JNIEnv* env, jobject clazz,
- const sp& surface)
- {
- SurfaceControl* const p =
- (SurfaceControl*)env->GetIntField(clazz, so.surfaceControl);
- if (surface.get()) {
- surface->incStrong(clazz);
- }
- if (p) {
- p->decStrong(clazz);
- }
- env->SetIntField(clazz, so.surfaceControl, (int)surface.get());
- }
[--->Surface_copyFrom]
- static void Surface_copyFrom(
- JNIEnv* env, jobject clazz, jobject other)
- {
- const sp& surface = getSurfaceControl(env, clazz);
- const sp& rhs = getSurfaceControl(env, other);
- if (!SurfaceControl::isSameSurface(surface, rhs)) {
- setSurfaceControl(env, clazz, rhs);
- //把本地那个surface的surfaceControl对象转移到outSurface上
- }
- }
这里仅仅是surfaceControl的转移,但是并没有看到Surface相关的信息。
那么Surface在哪里创建的呢?为了解释这个问题,我使用了终极武器,aidl。
1 终极武器AIDL
aidl可以把XXX.aidl文件转换成对应的java文件。我们刚才调用的是WindowSession的
relayOut函数。如下:
- sWindowSession.relayout(
- mWindow, params,
- (int) (mView.mMeasuredWidth * appScale + 0.5f),
- (int) (mView.mMeasuredHeight * appScale + 0.5f),
- viewVisibility, insetsPending, mWinFrame,
- mPendingContentInsets, mPendingVisibleInsets,
- mPendingConfiguration, mSurface);
它的aidl文件在framework/base/core/java/android/view/IWindowSession.aidl中
- interface IWindowSession {
- int add(IWindow window, in WindowManager.LayoutParams attrs,
- in int viewVisibility, out Rect outContentInsets);
- void remove(IWindow window);
- //注意喔,这个outSurface前面的是out,表示输出参数,这个类似于C++的引用。
- int relayout(IWindow window, in WindowManager.LayoutParams attrs,
- int requestedWidth, int requestedHeight, int viewVisibility,
- boolean insetsPending, out Rect outFrame, out Rect outContentInsets,
- out Rect outVisibleInsets, out Configuration outConfig,
- out Surface outSurface);
刚才说了,JNI及其JAVA调用只是copyFrom了SurfaceControl对象到outSurface中,但是没看到哪里创建Surface。这其中的奥秘就在aidl文件编译后生成的java文件中。
你在命令行下可以输入:
aidl -Id:\android-2.2-froyo-20100625-source\source\frameworks\base\core\java\ -Id:\android-2.2-froyo-20100625-source\source\frameworks\base\Graphics\java d:\android-2.2-froyo-20100625-source\source\frameworks\base\core\java\android\view\IWindowSession.aidl test.java
以生成test.java文件。-I参数指定include目录,例如aidl有些参数是在别的java文件中指定的,那么这个-I就需要把这些目录包含进来。
先看看ViewRoot这个客户端生成的代码是什么。
- public int relayout(
- android.view.IWindow window,
- android.view.WindowManager.LayoutParams attrs,
- int requestedWidth, int requestedHeight,
- int viewVisibility, boolean insetsPending,
- android.graphics.Rect outFrame,
- android.graphics.Rect outContentInsets,
- android.graphics.Rect outVisibleInsets,
- android.content.res.Configuration outConfig,
- android.view.Surface outSurface) ---->outSurface是第11个参数
- throws android.os.RemoteException
- {
- android.os.Parcel _data = android.os.Parcel.obtain();
- android.os.Parcel _reply = android.os.Parcel.obtain();
- int _result;
- try {
- _data.writeInterfaceToken(DESCRIPTOR);
- _data.writeStrongBinder((((window!=null))?(window.asBinder()):(null)));
- if ((attrs!=null)) {
- _data.writeInt(1);
- attrs.writeToParcel(_data, 0);
- }
- else {
- _data.writeInt(0);
- }
- _data.writeInt(requestedWidth);
- _data.writeInt(requestedHeight);
- _data.writeInt(viewVisibility);
- _data.writeInt(((insetsPending)?(1):(0)));
- //奇怪,outSurface的信息没有写到_data中。那.....
- mRemote.transact(Stub.TRANSACTION_relayout, _data, _reply, 0);
- _reply.readException();
- _result = _reply.readInt();
- if ((0!=_reply.readInt())) {
- outFrame.readFromParcel(_reply);
- }
- ....
- if ((0!=_reply.readInt())) {
- outSurface.readFromParcel(_reply); //从Parcel中读取信息来填充outSurface
- }
- }
- finally {
- _reply.recycle();
- _data.recycle();
- }
- return _result;
- }
真奇怪啊,Binder客户端这头竟然没有把outSurface的信息发过去。我们赶紧看看服务端。
服务端这边处理是在onTranscat函数中。
- @Override public boolean onTransact(int code, android.os.Parcel data, android.os.Parcel reply, int flags) throws android.os.RemoteException
- {
- switch (code)
- {
- case TRANSACTION_relayout:
- {
- data.enforceInterface(DESCRIPTOR);
- android.view.IWindow _arg0;
- android.view.Surface _arg10;
- //刚才说了,Surface信息并没有传过来,那么我们在relayOut中看到的outSurface是怎么
- //出来的呢?看下面这句,原来在服务端这边竟然new了一个新的Surface!!!
- _arg10 = new android.view.Surface();
- int _result = this.relayout(_arg0, _arg1, _arg2, _arg3, _arg4, _arg5, _arg6, _arg7, _arg8, _arg9, _arg10);
- reply.writeNoException();
- reply.writeInt(_result);
- //_arg10是copyFrom了,那怎么传到客户端呢?
- if ((_arg10!=null)) {
- reply.writeInt(1);//调用Surface的writeToParcel,把信息加入reply
- _arg10.writeToParcel(reply, android.os.Parcelable.PARCELABLE_WRITE_RETURN_VALUE);
- }
- return true;
- }
太诡异了!竟然有这么多花花肠子。我相信如果没有aidl的帮助,我无论如何也不会知道这其中的奥妙。
那好,我们的流程明白了。
◆客户端虽然传了一个surface,但其实没传递给服务端
◆服务端调用writeToParcel,把信息写到Parcel中,然后数据传回客户端
◆客户端调用Surface的readFromParcel,获得surface信息。
那就去看看writeToParcel吧。
[---->Surface_writeToParcel]
- static void Surface_writeToParcel(
- JNIEnv* env, jobject clazz, jobject argParcel, jint flags)
- {
- Parcel* parcel = (Parcel*)env->GetIntField(
- argParcel, no.native_parcel);
- const sp& control(getSurfaceControl(env, clazz));
- //还好,只是把数据序列化到Parcel中
- SurfaceControl::writeSurfaceToParcel(control, parcel);
- if (flags & PARCELABLE_WRITE_RETURN_VALUE) {
- setSurfaceControl(env, clazz, 0);
- }
- }
那看看客户端的Surface_readFromParcel吧。
[----->Surface_readFromParcel]
- static void Surface_readFromParcel(
- JNIEnv* env, jobject clazz, jobject argParcel)
- {
- Parcel* parcel = (Parcel*)env->GetIntField( argParcel, no.native_parcel);
- //客户端这边还没有surface呢
- const sp& control(getSurface(env, clazz));
- //不过我们看到希望了,根据服务端那边Parcel信息来构造一个新的surface
- sp rhs = new Surface(*parcel);
- if (!Surface::isSameSurface(control, rhs)) {
- setSurface(env, clazz, rhs); //把这个新surface赋给客户端。终于我们有了surface!
- }
- }
到此,我们终于七拐八绕的得到了surface,这其中经历太多曲折了。下一节,我们将精简这其中复杂的操作,统一归到Native层,以这样为切入点来了解Surface的工作流程和原理。
好,反正你知道ViewRoot调用了relayout后,Surface就真正从WindowManagerService那得到了。继续回到ViewRoot,其中还有一个重要地方是我们知道却不了解的。
- private void performTraversals() {
- // cache mView since it is used so much below...
- final View host = mView;
- boolean initialized = false;
- boolean contentInsetsChanged = false;
- boolean visibleInsetsChanged;
- try {
- relayoutResult = relayoutWindow(params, viewVisibility, insetsPending);
- // relayoutWindow完后,我们得到了一个无比宝贵的Surface
- //那我们画界面的地方在哪里?就在这个函数中,离relayoutWindow不远处。
- ....
- boolean cancelDraw = attachInfo.mTreeObserver.dispatchOnPreDraw();
- if (!cancelDraw && !newSurface) {
- mFullRedrawNeeded = false;
- draw(fullRedrawNeeded); //draw?draw什么呀?
- }
[--->ViewRoot::draw()]
- private void draw(boolean fullRedrawNeeded) {
- Surface surface = mSurface; //嘿嘿,不担心了,surface资源都齐全了
- if (surface == null || !surface.isValid()) {
- return;
- }
- if (mAttachInfo.mViewScrollChanged) {
- mAttachInfo.mViewScrollChanged = false;
- mAttachInfo.mTreeObserver.dispatchOnScrollChanged();
- }
- int yoff;
- final boolean scrolling = mScroller != null && mScroller.computeScrollOffset();
- if (scrolling) {
- yoff = mScroller.getCurrY();
- } else {
- yoff = mScrollY;
- }
- if (mCurScrollY != yoff) {
- mCurScrollY = yoff;
- fullRedrawNeeded = true;
- }
- float appScale = mAttachInfo.mApplicationScale;
- boolean scalingRequired = mAttachInfo.mScalingRequired;
- Rect dirty = mDirty;
- if (mUseGL) { //我们不用OPENGL
- ...
- }
- Canvas canvas;
- try {
- int left = dirty.left;
- int top = dirty.top;
- int right = dirty.right;
- int bottom = dirty.bottom;
- //从Surface中锁定一块区域,这块区域是我们认为的需要重绘的区域
- canvas = surface.lockCanvas(dirty);
- // TODO: Do this in native
- canvas.setDensity(mDensity);
- }
- try {
- if (!dirty.isEmpty() || mIsAnimating) {
- long startTime = 0L;
- try {
- canvas.translate(0, -yoff);
- if (mTranslator != null) {
- mTranslator.translateCanvas(canvas);
- }
- canvas.setScreenDensity(scalingRequired
- ? DisplayMetrics.DENSITY_DEVICE : 0);
- //mView就是之前的decoreView,
- mView.draw(canvas);
- }
- } finally {
- //我们的图画完了,告诉surface释放这块区域
- surface.unlockCanvasAndPost(canvas);
- }
- if (scrolling) {
- mFullRedrawNeeded = true;
- scheduleTraversals();
- }
- }
看起来,这个surface的用法很简单嘛:
l lockSurface,得到一个画布Canvas
l 调用View的draw,让他们在这个Canvas上尽情绘图才。另外,这个View会调用所有它的子View来画图,最终会进入到View的onDraw函数中,在这里我们可以做定制化的界面美化工作。当然,如果你想定制化整个系统画图的话,完全可以把performTranvsal看懂,然后再修改。
l unlockCanvasAndPost,告诉Surface释放这块画布
当然,这几个重要函数调用干了具体的活。这些重要函数,我们最终会精简到Native层的。
2 总结
到这里,你应该知道了一个Activity中,调用setContentView后它如何从系统中获取一块Surface,以及它是如何使用这个Surface的了。不得不说,关于UI这块,Android绝对是够复杂的。难怪2.3把UI这块代码基本重写一遍,希望能够简单精炼点。