Surface创建
在ViewRootImpl,创建一个final的Surface对象,任何线程可访问它,内部mLock锁。ViewRootImpl类中的Surface定义。
final Surface mSurface = new Surface();
第一次performTraversals方法或窗体发生改变时,Surface的#isValid方法判断是否有效,relayoutWindow方法,初始化Surface。
之前无效,现在有效
1:设置newSurface标志,下次再绘制。
2:初始化硬件渲染,mHardwareRenderer#initinitialize方法之前有效,现在无效
各种destroy停止一直有效
硬件渲染更新,mHardwareRenderer#updateSurface方法
Surface的#isValid方法。
public boolean isValid() {
synchronized (mLock) {
if (mNativeObject == 0) return false;
return nativeIsValid(mNativeObject);
}
}
当Surface内部底层指针mNativeObject不空,且底层JNI方法#nativeIsValid判断有效时,认定Java层Surface有效。
通过IWindowSession的#relayout方法访问Wms服务。
public int relayout(IWindow window, int seq, ...,Surface outSurface) {
int res = mService.relayoutWindow(this, window, ..., outSurface);
return res;
}
Wms服务的#relayoutWindow方法,初始化App进程传过来的Surface。
public int relayoutWindow(Session session, IWindow client, ...,Surface outSurface) {
..
if (viewVisibility == View.VISIBLE &&
(win.mAppToken == null || !win.mAppToken.clientHidden)) {
try {
...
SurfaceControl surfaceControl = winAnimator.createSurfaceLocked();
//创建surfaceControl
if (surfaceControl != null) {
outSurface.copyFrom(surfaceControl);
} else {
outSurface.release();
}
} catch (Exception e) {
}
}
...
}
创建一个SurfaceControl对象,调用它的copyFrom方法,初始化底层Surface,该对象是Surface控制类。
创建SurfaceControl
SurfaceControl底层结构图。
WindowStateAnimator的createSurfaceLocked方法。
SurfaceControl createSurfaceLocked() {
if (mSurfaceControl == null) {
mDrawState = DRAW_PENDING;//状态变更为待绘制
...
try {
mSurfaceW = width;//设置surface的宽高
mSurfaceH = height;
...
mSurfaceFormat = format;//设置surface格式
mSurfaceControl = new SurfaceControl(
mSession.mSurfaceSession,
attrs.getTitle().toString(),
width, height, format, flags);
w.mHasSurface = true;//winstate有了surface
try {
mSurfaceX = left;
mSurfaceY = top;
try {
mSurfaceControl.setPosition(left, top);
mSurfaceLayer = mAnimLayer;
...
mSurfaceControl.setLayer(mAnimLayer);
mSurfaceControl.setAlpha(0);
mSurfaceShown = false;
} catch (RuntimeException e) {
...
}
mLastHidden = true;//刚创建的设为隐藏
}
}
}
在Java层,SurfaceControl#构造方法,JNI#方法,创建底层SurfaceControl,参数是SurfaceSession、宽高、格式等,返回指针。
static jlong nativeCreate(JNIEnv* env, jclass clazz, jobject sessionObj,
jstring nameStr, jint w, jint h, jint format, jint flags) {
sp<SurfaceComposerClient> client(android_view_SurfaceSession_getClient(env, sessionObj));
//创建底层surfaceControl。
sp<SurfaceControl> surface = client->createSurface(
String8(name.c_str()), w, h, format, flags);
...
surface->incStrong((void *)nativeCreate);
return reinterpret_cast<jlong>(surface.get());
}
底层SurfaceComposerClient,它在SurfaceSession构造方法创建。创建底层SurfaceControl。
sp<SurfaceControl> SurfaceComposerClient::createSurface(
const String8& name,
uint32_t w,
uint32_t h,
PixelFormat format,
uint32_t flags) {
sp<SurfaceControl> sur;
if (mStatus == NO_ERROR) {
sp<IBinder> handle;
sp<IGraphicBufferProducer> gbp;
status_t err = mClient->createSurface(name, w, h, format, flags,
&handle, &gbp);
if (err == NO_ERROR) {
sur = new SurfaceControl(this, handle, gbp);
}
}
return sur;
}
在Wms服务进程,此方法本质是调用Client#createSurface实现与SF进程通信,SF进程初始化handle与gbp,最后,构建一个底层SurfaceControl用于封装关键的handle与gdp。
ISurfaceComposerClient是Wms服务进程与SF进程通信业务接口,SurfaceComposerClient#onFirstRef方法在构造方法前调用。
void SurfaceComposerClient::onFirstRef() {
sp<ISurfaceComposer> sm(ComposerService::getComposerService());
if (sm != 0) {
sp<ISurfaceComposerClient> conn = sm->createConnection();
if (conn != 0) {
mClient = conn;//初始化mClient。
mStatus = NO_ERROR;
}
}
}
通过createConnection方法,调用端得到的业务接口的实现BpSurfaceComposerClient。SF进程服务端创建Client对象,继承BnSurfaceComposerClient。
SF进程createSurface方法
Wms进程发起访问后,SF进程启动binder线程处理业务,向SF主线程发送一个MessageCreateLayer消息。
Client#createSurface方法。
status_t Client::createSurface(...) {
//先定义一个消息类MessageCreateLayer
class MessageCreateLayer : public MessageBase {
SurfaceFlinger* flinger;
Client* client;
sp<IBinder>* handle;
sp<IGraphicBufferProducer>* gbp;
status_t result;
..
public:
MessageCreateLayer(SurfaceFlinger* flinger,
const String8& name, Client* client,
uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
sp<IBinder>* handle,
sp<IGraphicBufferProducer>* gbp)
: flinger(flinger), client(client),
handle(handle), gbp(gbp),
name(name), w(w), h(h), format(format), flags(flags) {
}
status_t getResult() const { return result; }
virtual bool handler() {
result = flinger->createLayer(name, client, w, h, format, flags,
handle, gbp);//
return true;
}
};
sp<MessageBase> msg = new MessageCreateLayer(mFlinger.get(),
name, this, w, h, format, flags, handle, gbp);
mFlinger->postMessageSync(msg);
return static_cast<MessageCreateLayer*>( msg.get() )->getResult();
}
创建一个MessageCreateLayer消息,postMessageSync发送给SF主线程。
status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
nsecs_t reltime, uint32_t /* flags */) {
status_t res = mEventQueue.postMessage(msg, reltime);
if (res == NO_ERROR) {
msg->wait();//栅栏等待
}
return res;
}
向EventQueue消息队列发送同步消息,然后通过栅栏使binder线程陷入wait,最后由主线程唤醒。MessageCreateLayer继承MessageBase,当主线程handleMessage方法处理时,触发子类MessageCreateLayer重写的handler方法,最后打开栅栏,唤醒等待的binder线程。
void MessageBase::handleMessage(const Message&) {
//走到MessageCreateLayer重写的handler方法
this->handler();
barrier.open();
};
//MessageCreateLayer重写的handler方法
virtual bool handler() {
result = flinger->createLayer(name, client, w, h, format, flags,handle, gbp);
return true;
}
handler处理时,触发主线程SF#createLayer方法,真正初始化handle与gbp,返回result,SF#binder线程被唤醒后,createSurface继续执行,此时,result已初始化赋值。
总结
binder线程被栅栏阻挡,进入等待,当主线程handler方法执行后,有了result,打开栅栏,放行>binder线程,被唤醒拿到result。
SF主线程createLayer方法
status_t SurfaceFlinger::createLayer(
const String8& name,
const sp<Client>& client,
uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp) {
sp<Layer> layer;
result = createNormalLayer(client,
name, w, h, flags, format,//format是java层传过来的
handle, gbp, &layer);//创建Layer对象,指针在layer中。
...
result = addClientLayer(client, *handle, *gbp, layer);//将layer增加到list中
...
setTransactionFlags(eTransactionNeeded);//0x01 创建Layer,触发eTransactionNeeded
return result;
}
SurfaceFlinger#createNormalLayer方法。
status_t SurfaceFlinger::createNormalLayer(const sp<Client>& client,//普通Layer
const String8& name, uint32_t w, uint32_t h, uint32_t flags, PixelFormat& format,
sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer) {
...
*outLayer = new Layer(this, client, name, w, h, flags);//创建Layer
status_t err = (*outLayer)->setBuffers(w, h, format, flags);
if (err == NO_ERROR) {
*handle = (*outLayer)->getHandle();//初始化handle和gbp
*gbp = (*outLayer)->getProducer();
}
return err;
}
创建普通层级Layer对象,Layer#getHandle与Layer#getProducer方法初始化handle、gbp指针,将Layer加入到Client中的map保存,
每个Layer对应一个handle。
- Layer#getHandle:handle是IBinder类型,创建服务进程端Handle对象,在内核为其创建服务节点,客户端拿到的handle是BpXxx,handle是一个远程进程连接到SF进程的句柄。
- Layer#getProducer:返回Layer的IGraphicBufferProducer。IGraphicBufferProducer也是跨进程通信的业务接口,远程进程拿到的是BpGraphicBufferProducer,通过它操作图形内存区的。
- SF进程通过reply的writeStrongBinder方法写入,远程进程通过readStrongBinder读取。
底层Surface
Surface#copyFrom方法。
public void copyFrom(SurfaceControl other) {
...
//底层SurfaceControl指针
long surfaceControlPtr = other.mNativeObject;
...
long newNativeObject = nativeCreateFromSurfaceControl(surfaceControlPtr);
synchronized (mLock) {
if (mNativeObject != 0) {
nativeRelease(mNativeObject);//释放原底层Surface指针
}
setNativeObjectLocked(newNativeObject);
}
}
JNI#方法利用底层SurfaceControl创建底层Surface。
底层SurfaceControl#getSurface方法。
sp<Surface> SurfaceControl::getSurface() const {
Mutex::Autolock _l(mLock);
if (mSurfaceData == 0) {
mSurfaceData = new Surface(mGraphicBufferProducer, false);
}
return mSurfaceData;
}
底层Surface创建,封装GraphicBufferProducer指针。
Wms服务初始化Surface,App进程通过Surface操作图形缓冲区。
GraphicBufferProducer是生产者。
任重而道远