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的