1.底层C++
SufaceFlinger类图的静态结构
2.上层Java的调用流程。
首先,直接从WindowManagerService入手:
public int relayoutWindow(Session session, IWindow client, int seq,
WindowManager.LayoutParams attrs, int requestedWidth,
int requestedHeight, int viewVisibility, int flags,
Rect outFrame, Rect outContentInsets, Rect outVisibleInsets,
Configuration outConfig, Surface outSurface)
这个方法中有一句:
Surface surface = win.createSurfaceLocked();
创建Surface,然后继续跟下去,跟到了jni(android_view_Surface.cpp)的如下方法:
static void Surface_init(
JNIEnv* env, jobject clazz,
jobject session,
jint, jstring jname, jint dpy, jint w, jint h, jint format, jint flags)
{
if (session == NULL) {
doThrowNPE(env);
return;
}
SurfaceComposerClient* client =
(SurfaceComposerClient*)env->GetIntField(session, sso.client);
sp<SurfaceControl> surface;
if (jname == NULL) {
surface = client->createSurface(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(name, dpy, w, h, format, flags);
}
if (surface == 0) {
jniThrowException(env, OutOfResourcesException, NULL);
return;
}
setSurfaceControl(env, clazz, surface);
}
郁闷的是这个session又是如何初始化的,同样在android_view_Surface.cpp中:
static void SurfaceSession_init(JNIEnv* env, jobject clazz)
{
sp<SurfaceComposerClient> client = new SurfaceComposerClient;
client->incStrong(clazz);
env->SetIntField(clazz, sso.client, (int)client.get());
}
非常好,那么这个client就是和java层SurfaceSession构成了一一对应关系咯?事实的确如此。看java层的
SurfaceSession的定义,里面仅有的成员变量就是这个client:
public class SurfaceSession {
/** Create a new connection with the surface flinger. */
public SurfaceSession() {
init();
}
/** Forcibly detach native resources associated with this object.
* Unlike destroy(), after this call any surfaces that were created
* from the session will no longer work. The session itself is destroyed.
*/
public native void kill();
/* no user serviceable parts here ... */
@Override
protected void finalize() throws Throwable {
destroy();
}
private native void init();
private native void destroy();
private int mClient;
}
好了,现在回到java层继续看Surface。现在的概念应该很明确了,从本质上来说,Surface是由SurfaceSession创建的。那么SurfaceSession 又是由谁创建的呢? 既然SurfaceSession 是Session的成员变量,那么就顺藤摸瓜去看:
void windowAddedLocked() {
if (mSurfaceSession == null) {
if (WindowManagerService.localLOGV) Slog.v(
WindowManagerService.TAG, "First window added to " + this + ", creating SurfaceSession");
mSurfaceSession = new SurfaceSession();
if (WindowManagerService.SHOW_TRANSACTIONS) Slog.i(
WindowManagerService.TAG, " NEW SURFACE SESSION " + mSurfaceSession);
mService.mSessions.add(this);
}
mNumWindow++;
}
那么windowAddedLocked又是谁调用的呢?在WIndowState.java中:
void attach() {
if (WindowManagerService.localLOGV) Slog.v(
WindowManagerService.TAG, "Attaching " + this + " token=" + mToken
+ ", list=" + mToken.windows);
mSession.windowAddedLocked();
}
那么这个attach又是谁调用的呢?WindowManagerService.java的一个方法,如下:
public int addWindow(Session session, IWindow client, int seq,
WindowManager.LayoutParams attrs, int viewVisibility,
Rect outContentInsets, InputChannel outInputChannel)
由于这个方法比较长,就不贴了,只看其中最关键的地方,其中有这么一句话:
win.attach();
经过这么一分析,清楚了,SurfaceSession是WindowManagerService在addWindow的时候创建的。而
Surface是在WIndowManagerService进行relayoutWindow时创建的。