安卓高手之路之 GDI图形引擎篇

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时创建的。
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值