概述
本篇文章分析Surface和SurfaceControl的创建流程,在window通过addView添加到WMS时会创建ViewRootImpl,ViewRootImpl创建好之后Surface,SurfaceControl都作为全局变量被new了出来,但是仅仅是调用了它们无参构造函数创建了一个对象,其他什么也没做,那它们具体初始化在什么地方呢?
ViewRootImpl的setView()
方法中会调用requestLayout()
准备进行View的测量,布局和绘制,即在收到下一个Vsync时执行performTraversals()
方法,此方法又会调用relayoutWindow()
方法,然后通过Binder调用WMS的relayout方法
,我们从此方法开始看
ViewRootImpl.relayoutWindow
private int relayoutWindow(WindowManager.LayoutParams params, int viewVisibility,
boolean insetsPending) throws RemoteException {
......
int relayoutResult = mWindowSession.relayout(mWindow, mSeq, params,
(int) (mView.getMeasuredWidth() * appScale + 0.5f),
(int) (mView.getMeasuredHeight() * appScale + 0.5f), viewVisibility,
insetsPending ? WindowManagerGlobal.RELAYOUT_INSETS_PENDING : 0, frameNumber,
mTmpFrame, mPendingOverscanInsets, mPendingContentInsets, mPendingVisibleInsets,
mPendingStableInsets, mPendingOutsets, mPendingBackDropFrame, mPendingDisplayCutout,
mPendingMergedConfiguration, mSurfaceControl, mTempInsets);
if (mSurfaceControl.isValid()) {
mSurface.copyFrom(mSurfaceControl);
} else {
destroySurface();
}
......
relayout
方法传递了一大堆参数,我们这里只关心mSurfaceControl
,这个参数在AIDL文件中被定义为out类型,意味着它需要由binder server端进行填充,所以此参数被传递到WMS端进行具体的初始化,然后注意relayout
完成之后如果mSurfaceControl
是有效的则通过mSurface
的copyFrom
来初始化Surface
好了我们首先就来看看SurfaceControl在WMS端的具体处理流程吧,mWindowSession.relayout
中会调用WMS的relayoutWindow
方法
WMS.relayoutWindow
public int relayoutWindow(......
SurfaceControl outSurfaceControl,......) {
......
try {
result = createSurfaceControl(outSurfaceControl, result, win, winAnimator);
} catch (Exception e) {
......
return 0;
}
我们只关心SurfaceControl
的创建,主要是通过createSurfaceControl
方法,接着看
createSurfaceControl
private int createSurfaceControl(SurfaceControl outSurfaceControl, int result, WindowState win,
WindowStateAnimator winAnimator) {
......
WindowSurfaceController surfaceController;
try {
//步骤1
surfaceController = winAnimator.createSurfaceLocked(win.mAttrs.type, win.mOwnerUid);
} finally {
...
}
if (surfaceController != null) {
//步骤2
surfaceController.getSurfaceControl(outSurfaceControl);
} else {
......
outSurfaceControl.release();
}
return result;
}
这个方法我们分为两步来看,首先看步骤1createSurfaceLocked
方法
WindowStateAnimator.createSurfaceLocked
WindowSurfaceController createSurfaceLocked(int windowType, int ownerUid) {
//一个WindowStateAnimator对象只会创建一个WindowSurfaceController
if (mSurfaceController != null) {
return mSurfaceController;
}
......
//这里直接通过new的方式创建了一个WindowSurfaceController对象,
//那么这个对象和SurfaceController有什么关系呢?
mSurfaceController = new WindowSurfaceController(mSession.mSurfaceSession,
attrs.getTitle().toString(), width, height, format, flags, this,
windowType, ownerUid);
return mSurfaceController;
}
上面通过new的方式创建了一个WindowSurfaceController
对象,我们看看WindowSurfaceController
的构造方法
WindowSurfaceController构造方法
public WindowSurfaceController(SurfaceSession s, String name, int w, int h, int format,
int flags, WindowStateAnimator animator, int windowType, int ownerUid) {
//省略一些赋值
......
final SurfaceControl.Builder b = win.makeSurface()
.setParent(win.getSurfaceControl())
.setName(name)
.setBufferSize(w, h)
.setFormat(format)
.setFlags(flags)
.setMetadata(METADATA_WINDOW_TYPE, windowType)
.setMetadata(METADATA_OWNER_UID, ownerUid);
mSurfaceControl = b.build();
Trace.traceEnd(TRACE_TAG_WINDOW_MANAGER);
}
WindowSurfaceController
构造方法中其实就是通过SurfaceControl
的内部Builder
类来创建SurfaceControl
对象的,并且将SurfaceControl
的Name,BufferSize,Format,Flags等等一并保存到内部Builder
中
SurfaceControl.Builder.build
public SurfaceControl build() {
if (mWidth < 0 || mHeight < 0) {
throw new IllegalStateException(
"width and height must be positive or unset");
}
if ((mWidth > 0 || mHeight > 0) && (isColorLayerSet() || isContainerLayerSet())) {
throw new IllegalStateException(
"Only buffer layers can set a valid buffer size.");
}
return new SurfaceControl(
mSession, mName, mWidth, mHeight, mFormat, mFlags, mParent, mMetadata);
}
直接通过new的方式创建SurfaceControl
,接着看SurfaceControl
的构造方法
private SurfaceControl(SurfaceSession session, String name, int w, int h, int format, int flags,
SurfaceControl parent, SparseIntArray metadata)
throws OutOfResourcesException, IllegalArgumentException {
......
mName = name;
mWidth = w;
mHeight = h;
Parcel metaParcel = Parcel.obtain();
try {
if (metadata != null && metadata.size() > 0) {
metaParcel.writeInt(metadata.size());
for (int i = 0; i < metadata.size(); ++i) {
metaParcel.writeInt(metadata.keyAt(i));
metaParcel.writeByteArray(
ByteBuffer.allocate(4).order(ByteOrder.nativeOrder())
.putInt(metadata.valueAt(i)).array());
}
metaParcel.setDataPosition(0);
}
mNativeObject = nativeCreate(session, name, w, h, format, flags,
parent != null ? parent.mNativeObject : 0, metaParcel);
} finally {
metaParcel.recycle();
}
if (mNativeObject == 0) {
throw new OutOfResourcesException(
"Couldn't allocate SurfaceControl native object");
}
mCloseGuard.open("release");
}
这里面核心就是调用nativeCreate
方法,并将window的各种数据一并传递到native层处理,对应的native类是android_view_SurfaceControl
nativeCreate
static jlong nativeCreate(JNIEnv* env, jclass clazz, jobject sessionObj,
jstring nameStr, jint w, jint h, jint format, jint flags, jlong parentObject,
jobject metadataParcel) {
ScopedUtfChars name(env, nameStr);
//这个对象前面文章分析过,作为应用层访问SurfaceFlinger的client端
sp<SurfaceComposerClient> client;
//这里的sessionObj是java层传递下来的SurfaceSession对象,
//如果不为空就从此对象中获取SurfaceComposerClient,否则重新创建一个
if (sessionObj != NULL) {
client = android_view_SurfaceSession_getClient(env, sessionObj);
} else {
client = SurfaceComposerClient::getDefault();
}
//parentObject是java层传递下来的SurfaceControl,将其强转为native
//层SurfaceControl,parentObject有可能为空的
SurfaceControl *parent = reinterpret_cast<SurfaceControl*>(parentObject);
//创建当前窗口的SurfaceControl
sp<SurfaceControl> surface;
LayerMetadata metadata;
Parcel* parcel = parcelForJavaObject(env, metadataParcel);
if (parcel && !parcel->objectsCount()) {
status_t err = metadata.readFromParcel(parcel);
if (err != NO_ERROR) {
jniThrowException(env, "java/lang/IllegalArgumentException",
"Metadata parcel has wrong format");
}
}
//此函数是具体初始化SurfaceControl的函数
status_t err = client->createSurfaceChecked(
String8(name.c_str()), w, h, format, &surface, flags, parent, std::move(metadata));
if (err == NAME_NOT_FOUND) {
jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
return 0;
} else if (err != NO_ERROR) {
jniThrowException(env, OutOfResourcesException, NULL);
return 0;
}
//增加引用计数
surface->incStrong((void *)nativeCreate);
//将创建完成的native层SurfaceControl返回到java层
return reinterpret_cast<jlong>(surface.get());
}
总结一下上面函数做的事情:
- 获取应用层访问
SurfaceFlinger
进程的的client端对象SurfaceComposerClient
- 得到父窗口的
SurfaceControl
以及创建当前窗口的SurfaceControl
(仅仅是创建出来,没有做任何其他操作) - 调用
SurfaceComposerClient
的createSurfaceChecked
函数,将java层传递下来的各种窗口参数以及上一步创建的SurfaceControl
传递进去,进行具体初始化 - 将上一步创建好的
SurfaceControl
返回给java层
重点就是第三步createSurfaceChecked
函数
createSurfaceChecked
status_t SurfaceComposerClient::createSurfaceChecked(const String8& name, uint32_t w, uint32_t h,
PixelFormat format,
sp<SurfaceControl>* outSurface, uint32_t flags,
SurfaceControl* parent,
LayerMetadata metadata) {
sp<SurfaceControl> sur;
status_t err = mStatus;
if (mStatus == NO_ERROR) {
sp<IBinder> handle;
sp<IBinder> parentHandle;
sp<IGraphicBufferProducer> gbp;
if (parent != nullptr) {
parentHandle = parent->getHandle();
}
err = mClient->createSurface(name, w, h, format, flags, parentHandle, std::move(metadata),
&handle, &gbp);
if (err == NO_ERROR) {
*outSurface = new SurfaceControl(this, handle, gbp, true /* owned */);
}
}
return err;
}
这个函数有两个重点,1.调用mClient的createSurface
函数,mClient
在之前文章AndroidQ window和SurfaceFlinger建立连接分析中分析过,它是ISurfaceComposerClient
的Binder Bp端,Bn端则是SurfaceFlinger
进程中的Client
对象
2.通过new的方式创建native层SurfaceControl
对象
还有一个重要的对象就是sp<IGraphicBufferProducer> gbp
,它是SurfaceFlinger BufferQueue
生产者消费者模型中的生产者,它会在createSurface
进行初始化
接着看mClient->createSurface
,我们直接看它在SurfaceFlinger进程的具体实现
Client::createSurface
status_t Client::createSurface(const String8& name, uint32_t w, uint32_t h, PixelFormat format,
uint32_t flags, const sp<IBinder>& parentHandle,
LayerMetadata metadata, sp<IBinder>* handle,
sp<IGraphicBufferProducer>* gbp) {
// We rely on createLayer to check permissions.
return mFlinger->createLayer(name, this, w, h, format, flags, std::move(metadata), handle, gbp,
parentHandle);
}
mFlinger
是SurfaceFlinger
,所以Client的具体实现还是依靠的SurfaceFlinger
,并且注意,在应用层的Surface
在SurfaceFlinger
进程名叫Layer
SurfaceFlinger::createLayer
status_t SurfaceFlinger::createLayer(const String8& name, const sp<Client>& client, uint32_t w,
uint32_t h, PixelFormat format, uint32_t flags,
LayerMetadata metadata, sp<IBinder>* handle,
sp<IGraphicBufferProducer>* gbp,
const sp<IBinder>& parentHandle,
const sp<Layer>& parentLayer) {
//寬高合法检查
if (int32_t(w|h) < 0) {
ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)",
int(w), int(h));
return BAD_VALUE;
}
status_t result = NO_ERROR;
//layer
sp<Layer> layer;
......
//根据不同flag创建不同layer
switch (flags & ISurfaceComposerClient::eFXSurfaceMask) {
case ISurfaceComposerClient::eFXSurfaceBufferQueue:
result = createBufferQueueLayer(client, uniqueName, w, h, flags, std::move(metadata),
format, handle, gbp, &layer);
break;
case ISurfaceComposerClient::eFXSurfaceBufferState:
result = createBufferStateLayer(client, uniqueName, w, h, flags, std::move(metadata),
handle, &layer);
break;
case ISurfaceComposerClient::eFXSurfaceColor:
// check if buffer size is set for color layer.
if (w > 0 || h > 0) {
ALOGE("createLayer() failed, w or h cannot be set for color layer (w=%d, h=%d)",
int(w), int(h));
return BAD_VALUE;
}
result = createColorLayer(client, uniqueName, w, h, flags, std::move(metadata), handle,
&layer);
break;
case ISurfaceComposerClient::eFXSurfaceContainer:
// check if buffer size is set for container layer.
if (w > 0 || h > 0) {
ALOGE("createLayer() failed, w or h cannot be set for container layer (w=%d, h=%d)",
int(w), int(h));
return BAD_VALUE;
}
result = createContainerLayer(client, uniqueName, w, h, flags, std::move(metadata),
handle, &layer);
break;
default:
result = BAD_VALUE;
break;
}
.......
return result;
}
上面函数的核心代码就是根据应用请求不同的flag创建不同的显示Layer
,
从上面代码看创建的Layer
有四种类型,我们看看系统中大多数界面的Layer
,flage 为eFXSurfaceBufferQueueyer
,我们就大致看看createBufferQueueLayer
函数
createBufferQueueLayer
status_t SurfaceFlinger::createBufferQueueLayer(const sp<Client>& client, const String8& name,
uint32_t w, uint32_t h, uint32_t flags,
LayerMetadata metadata, PixelFormat& format,
sp<IBinder>* handle,
sp<IGraphicBufferProducer>* gbp,
sp<Layer>* outLayer) {
// initialize the surfaces
switch (format) {
case PIXEL_FORMAT_TRANSPARENT:
case PIXEL_FORMAT_TRANSLUCENT:
format = PIXEL_FORMAT_RGBA_8888;
break;
case PIXEL_FORMAT_OPAQUE:
format = PIXEL_FORMAT_RGBX_8888;
break;
}
sp<BufferQueueLayer> layer = getFactory().createBufferQueueLayer(
LayerCreationArgs(this, client, name, w, h, flags, std::move(metadata)));
status_t err = layer->setDefaultBufferProperties(w, h, format);
if (err == NO_ERROR) {
*handle = layer->getHandle();
*gbp = layer->getProducer();
*outLayer = layer;
}
return err;
}
可以看到上面函数的核心是调用
getFactory().createBufferQueueLayer
,最终创建的是Layer
的子类BufferQueueLayer
,如果没有出现错误还会进行如下赋值:
handle = layer->getHandle();
*gbp = layer->getProducer();
*outLayer = layer;
createBufferQueueLayer
sp<BufferQueueLayer> createBufferQueueLayer(const LayerCreationArgs& args) override {
return new BufferQueueLayer(args);
}
SurfaceFlingerFactory
的createBufferQueueLayer
函数只是new了一个BufferQueueLayer
,继续看BufferQueueLayer
构造函数,
BufferQueueLayer::BufferQueueLayer(const LayerCreationArgs& args)
: BufferLayer(args) {}
它的构造函数中其实没做什么事情,但是别忘了这个类终极父类是RefBase
,所以再看看他的onFirstRef
函数
onFirstRef
void BufferQueueLayer::onFirstRef() {
BufferLayer::onFirstRef();
// Creates a custom BufferQueue for SurfaceFlingerConsumer to use
sp<IGraphicBufferProducer> producer;
sp<IGraphicBufferConsumer> consumer;
BufferQueue::createBufferQueue(&producer, &consumer, true);
mProducer = new MonitoredProducer(producer, mFlinger, this);
{
// Grab the SF state lock during this since it's the only safe way to access RenderEngine
Mutex::Autolock lock(mFlinger->mStateLock);
mConsumer =
new BufferLayerConsumer(consumer, mFlinger->getRenderEngine(), mTextureName, this);
}
mConsumer->setConsumerUsageBits(getEffectiveUsage(0));
mConsumer->setContentsChangedListener(this);
mConsumer->setName(mName);
// BufferQueueCore::mMaxDequeuedBufferCount is default to 1
if (!mFlinger->isLayerTripleBufferingDisabled()) {
mProducer->setMaxDequeuedBufferCount(2);
}
if (const auto display = mFlinger->getDefaultDisplayDevice()) {
updateTransformHint(display);
}
if (mFlinger->mLayerExt) {
mLayerType = mFlinger->mLayerExt->getLayerClass(mName.string());
}
}
这个函数做的事情就非常复杂了,它初始化了SurfaceFlinger
进程的生产者消费者模型架构,我们这篇文章就不具体分析了,后面单独分析BufferQueue
和生产者消费者模式
好了SurfaceFlinger
进程的Layer
已经创建完成了,再回到SurfaceComposerClient
中
status_t SurfaceComposerClient::createSurfaceChecked(const String8& name, uint32_t w, uint32_t h,
PixelFormat format,
sp<SurfaceControl>* outSurface, uint32_t flags,
SurfaceControl* parent,
LayerMetadata metadata) {
sp<SurfaceControl> sur;
status_t err = mStatus;
if (mStatus == NO_ERROR) {
sp<IBinder> handle;
sp<IBinder> parentHandle;
sp<IGraphicBufferProducer> gbp;
if (parent != nullptr) {
parentHandle = parent->getHandle();
}
err = mClient->createSurface(name, w, h, format, flags, parentHandle, std::move(metadata),
&handle, &gbp);
if (err == NO_ERROR) {
*outSurface = new SurfaceControl(this, handle, gbp, true /* owned */);
}
}
return err;
}
SurfaceFlinger
进程的Layer
创建好之后,接着通过new创建SurfaceControl
,参数handle
持有创建的Layer的引用,gbp
是BufferQueue
的生产者
SurfaceControl构造函数
SurfaceControl::SurfaceControl(
const sp<SurfaceComposerClient>& client,
const sp<IBinder>& handle,
const sp<IGraphicBufferProducer>& gbp,
bool owned)
: mClient(client), mHandle(handle), mGraphicBufferProducer(gbp), mOwned(owned),
mExtension(mHandle, &mGraphicBufferProducer, mOwned)
{
}
构造函数中只是将handle
和gbp
保存为全局变量,没做其他操作,到目前为止还没看到Surface
在哪里创建
继续分析代码,SurfaceComposerClient
的createSurfaceChecked
函数已经看完了,回到android_view_SurfaceControl
的nativeCreate
函数,最终将创建的native层的SurfaceControl
返回给java层,
回到java层SurfaceControl
中
private SurfaceControl(SurfaceSession session, String name, int w, int h, int format, int flags,
SurfaceControl parent, SparseIntArray metadata)
throws OutOfResourcesException, IllegalArgumentException {
......
mNativeObject = nativeCreate(session, name, w, h, format, flags,
parent != null ? parent.mNativeObject : 0, metaParcel);
} finally {
metaParcel.recycle();
}
if (mNativeObject == 0) {
throw new OutOfResourcesException(
"Couldn't allocate SurfaceControl native object");
}
mCloseGuard.open("release");
}
java层SurfaceControl
的成员变量mNativeObject
保存了native层SurfaceControl
的指针
主要注意此时只是创建好了native层和WMS进程的SurfaceControl
,最终是需要将WMS进程的SurfaceControl
返回给应用进程,我们再看看这一过程实现,之前分析WMS的createSurfaceControl方法时分了两步:
private int createSurfaceControl(SurfaceControl outSurfaceControl, int result, WindowState win,
WindowStateAnimator winAnimator) {
......
WindowSurfaceController surfaceController;
try {
//步骤1
surfaceController = winAnimator.createSurfaceLocked(win.mAttrs.type, win.mOwnerUid);
} finally {
...
}
if (surfaceController != null) {
//步骤2
surfaceController.getSurfaceControl(outSurfaceControl);
} else {
......
outSurfaceControl.release();
}
return result;
}
步骤1已经分析完了,现在来看步骤2,outSurfaceControl
就是最终返回给应用进程的SurfaceControl
对象,来看看getSurfaceControl
方法
WindowSurfaceController.getSurfaceControl
void getSurfaceControl(SurfaceControl outSurfaceControl) {
outSurfaceControl.copyFrom(mSurfaceControl);
}
mSurfaceControl
是步骤1创建的,它的成员变量mNativeObject
持有native层的SurfaceControl
的指针
来看看SurfaceControl
的copyFrom
方法
SurfaceControl.copyFrom
public void copyFrom(SurfaceControl other) {
mName = other.mName;
mWidth = other.mWidth;
mHeight = other.mHeight;
assignNativeObject(nativeCopyFromSurfaceControl(other.mNativeObject));
}
nativeCopyFromSurfaceControl
这个native函数其实仅仅通过native层SurfaceControl
的拷贝构造函数重新深拷贝了一个SurfaceControl
,然后assignNativeObject
如下所示,将这个深拷贝的SurfaceControl
对象指针赋值给了我们应用进程传递过来的outSurfaceControl
的成员变量mNativeObject
private void assignNativeObject(long nativeObject) {
if (mNativeObject != 0) {
release();
}
mNativeObject = nativeObject;
}
最后再回到ViewRootImpl
的relayoutWindow
方法
private int relayoutWindow(WindowManager.LayoutParams params, int viewVisibility,
boolean insetsPending) throws RemoteException {
......
int relayoutResult = mWindowSession.relayout(mWindow, mSeq, params,
(int) (mView.getMeasuredWidth() * appScale + 0.5f),
(int) (mView.getMeasuredHeight() * appScale + 0.5f), viewVisibility,
insetsPending ? WindowManagerGlobal.RELAYOUT_INSETS_PENDING : 0, frameNumber,
mTmpFrame, mPendingOverscanInsets, mPendingContentInsets, mPendingVisibleInsets,
mPendingStableInsets, mPendingOutsets, mPendingBackDropFrame, mPendingDisplayCutout,
mPendingMergedConfiguration, mSurfaceControl, mTempInsets);
if (mSurfaceControl.isValid()) {
mSurface.copyFrom(mSurfaceControl);
} else {
destroySurface();
}
......
我们前面说过,relayout
完成之后如果mSurfaceControl
是有效的则通过mSurface
的copyFrom
来初始化Surface
,我们来看看mSurface.copyFrom
方法
Surface.copyFrom
public void copyFrom(SurfaceControl other) {
if (other == null) {
throw new IllegalArgumentException("other must not be null");
}
long surfaceControlPtr = other.mNativeObject;
if (surfaceControlPtr == 0) {
throw new NullPointerException(
"null SurfaceControl native object. Are you using a released SurfaceControl?");
}
long newNativeObject = nativeGetFromSurfaceControl(mNativeObject, surfaceControlPtr);
synchronized (mLock) {
if (newNativeObject == mNativeObject) {
return;
}
if (mNativeObject != 0) {
nativeRelease(mNativeObject);
}
setNativeObjectLocked(newNativeObject);
}
}
这个方法中做了如下几件事:
surfaceControlPtr
会保存SurfaceControl
的native指针- 调用native方法
nativeGetFromSurfaceControl
创建Surface
的native对象并将其指针保存到newNativeObject
- 如果步骤2重新获取的
Surface
指针和之前保存的不是同一个则调用setNativeObjectLocked
方法重新保存
我们主要看看nativeGetFromSurfaceControl
方法创建native层Surface
的过程,这个方法接收的参数是native层Surface
的指针和native层SurfaceControl
的指针
nativeGetFromSurfaceControl
static jlong nativeGetFromSurfaceControl(JNIEnv* env, jclass clazz,
jlong nativeObject,
jlong surfaceControlNativeObj) {
Surface* self(reinterpret_cast<Surface *>(nativeObject));
sp<SurfaceControl> ctrl(reinterpret_cast<SurfaceControl *>(surfaceControlNativeObj));
// If the underlying IGBP's are the same, we don't need to do anything.
if (self != nullptr &&
IInterface::asBinder(self->getIGraphicBufferProducer()) ==
IInterface::asBinder(ctrl->getIGraphicBufferProducer())) {
return nativeObject;
}
sp<Surface> surface(ctrl->getSurface());
if (surface != NULL) {
surface->incStrong(&sRefBaseOwner);
}
return reinterpret_cast<jlong>(surface.get());
}
这个函数逻辑就比较简单了,如果self不为空,并且Surface
的生产者和SurfaceControl
的生产者是同一个则之前返回刚java层传递下来的nativeObject
,不需要再重复创建,我们假设Surface
首次创建,则调用SurfaceControl
的getSurface
函数创建Surface
SurfaceControl::getSurface
sp<Surface> SurfaceControl::getSurface() const
{
Mutex::Autolock _l(mLock);
if (mSurfaceData == nullptr) {
return generateSurfaceLocked();
}
return mSurfaceData;
}
继续调用generateSurfaceLocked
函数
SurfaceControl::generateSurfaceLocked
sp<Surface> SurfaceControl::generateSurfaceLocked() const
{
// This surface is always consumed by SurfaceFlinger, so the
// producerControlledByApp value doesn't matter; using false.
mExtension.init();
mSurfaceData = new Surface(mGraphicBufferProducer, false);
return mSurfaceData;
}
直接new了一个native层Surface
,mGraphicBufferProducer
是前面SurfaceFlinger进程创建的生产者,所以Surface
和SurfaceControl
持有同一个生产者的引用,Surface
构造函数中仅仅做了一系列变量的初始化,就不贴代码了
到这里Surface
和SurfaceControl
的创建已经分析完了
我们总结一下整个流程:
- 首先应用进程会new一个java层
SurfaceControl
,什么都没做,然后传递到WMS进程,因为SurfaceControl
在AIDL中是out类型,所以在WMS进程赋值 - WMS在创建java层
SurfaceControl
的同时通过nativeCreate
方法到native层做一系列初始化 - 在
SurfaceComposerClient
的createSurfaceChecked
函数中通过ISurfaceComposerClient
的Bp端mClient
向SurfaceFlinger
进程请求创建Surface
,即调用createSurface
函数,而在SurfaceFlinger
进程Surface
对应的是Layer
- 在第一次创建
Layer
的子类BufferQueueLayer
的过程中,即在BufferQueueLayer
的onFirstRef
函数中会创建生产者消费者模型架构 SurfaceFlinger
进程的任务完成之后会直接new一个SurfaceControl
,并将SurfaceFlinger
进程创建的Layer
引用和生产者保存到SurfaceControl
中,最后将native层SurfaceControl
指针保存到java层SurfaceControl
- native层
SurfaceControl
创建好了之后就可以通过此对象创建native层的Surface
对象,最后将native层Surface
指针保存到java层Surface
,最终java层和native层的Surface
和SurfaceControl
都创建完毕
最后
以上就是无限大神为你收集整理的AndroidQ 图形系统(1)Surface与SurfaceControl创建分析的全部内容,希望文章能够帮你解决AndroidQ 图形系统(1)Surface与SurfaceControl创建分析所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复