Android SurfaceSession

SurfaceSession是个非常短小的类

路径:

frameworks/base/core/java/android/view/SurfaceSession.java

内容:

package android.view;

/**
 * An instance of this class represents a connection to the surface
 * flinger, from which you can create one or more Surface instances that will
 * be composited to the screen.
 * {@hide}
 */
public final class SurfaceSession {
    // Note: This field is accessed by native code.
    private long mNativeClient; // SurfaceComposerClient*

    private static native long nativeCreate();
    private static native void nativeDestroy(long ptr);
    private static native void nativeKill(long ptr);

    /** Create a new connection with the surface flinger. */
    public SurfaceSession() {
        mNativeClient = nativeCreate();
    }

    /* no user serviceable parts here ... */
    @Override
    protected void finalize() throws Throwable {
        try {
            if (mNativeClient != 0) {
                nativeDestroy(mNativeClient);
            }
        } finally {
            super.finalize();
        }
    }

    /**
     * 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.
     */
    public void kill() {
        nativeKill(mNativeClient);
    }
}

就只有不到60行,提供了3个native方法
从注释看,这个类的对象是surface flinger的一个连接
通过这个连接,可以创建1个或者多个Surface并渲染到屏幕上

那么native方法具体是怎么实现的呢?
这是个JNI,按照JNI的规则,代码中会有叫android_view_SurfaceSession.cpp
android_view_SurfaceSession.h
的两个文件
路径是

frameworks/base/core/jni/android_view_SurfaceSession.cpp
frameworks/base/include/android_runtime/android_view_SurfaceSession.h

android_view_SurfaceSession.cpp文件主要内容

static jlong nativeCreate(JNIEnv* env, jclass clazz) {
    SurfaceComposerClient* client = new SurfaceComposerClient();
    client->incStrong((void*)nativeCreate);
    return reinterpret_cast<jlong>(client);
}

static void nativeDestroy(JNIEnv* env, jclass clazz, jlong ptr) {
    SurfaceComposerClient* client = reinterpret_cast<SurfaceComposerClient*>(ptr);
    client->decStrong((void*)nativeCreate);
}

static void nativeKill(JNIEnv* env, jclass clazz, jlong ptr) {
    SurfaceComposerClient* client = reinterpret_cast<SurfaceComposerClient*>(ptr);
    client->dispose();
}

nativeCreate实际是创建了SurfaceComposerClient的对象,并返回这个对象的地址给调用者SurfaceSession的mNativeClient成员

nativeKill其实是调用了SurfaceComposerClient的dispose接口
C++代码看不懂,希望有大神能给解释一下
不过就我的理解,SurfaceComposerClient是surface flinger的客户端,用来访问surface flinger,间接操控surface的

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值