我是靠谱客的博主 无限大神,最近开发中收集的这篇文章主要介绍AndroidQ 图形系统(1)Surface与SurfaceControl创建分析,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

本篇文章分析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是有效的则通过mSurfacecopyFrom来初始化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());
}

总结一下上面函数做的事情:

  1. 获取应用层访问SurfaceFlinger进程的的client端对象SurfaceComposerClient
  2. 得到父窗口的SurfaceControl以及创建当前窗口的SurfaceControl(仅仅是创建出来,没有做任何其他操作)
  3. 调用SurfaceComposerClientcreateSurfaceChecked函数,将java层传递下来的各种窗口参数以及上一步创建的SurfaceControl传递进去,进行具体初始化
  4. 将上一步创建好的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);
}

mFlingerSurfaceFlinger,所以Client的具体实现还是依靠的SurfaceFlinger,并且注意,在应用层的SurfaceSurfaceFlinger进程名叫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);
}

SurfaceFlingerFactorycreateBufferQueueLayer函数只是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的引用,gbpBufferQueue的生产者

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)
{
}

构造函数中只是将handlegbp保存为全局变量,没做其他操作,到目前为止还没看到Surface在哪里创建

继续分析代码,SurfaceComposerClientcreateSurfaceChecked函数已经看完了,回到android_view_SurfaceControlnativeCreate函数,最终将创建的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的指针

来看看SurfaceControlcopyFrom方法

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;
}

最后再回到ViewRootImplrelayoutWindow方法

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是有效的则通过mSurfacecopyFrom来初始化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);
}
}

这个方法中做了如下几件事:

  1. surfaceControlPtr会保存SurfaceControl的native指针
  2. 调用native方法nativeGetFromSurfaceControl创建Surface的native对象并将其指针保存到newNativeObject
  3. 如果步骤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首次创建,则调用SurfaceControlgetSurface函数创建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层SurfacemGraphicBufferProducer是前面SurfaceFlinger进程创建的生产者,所以SurfaceSurfaceControl持有同一个生产者的引用,Surface构造函数中仅仅做了一系列变量的初始化,就不贴代码了

到这里SurfaceSurfaceControl的创建已经分析完了

我们总结一下整个流程:

  1. 首先应用进程会new一个java层SurfaceControl,什么都没做,然后传递到WMS进程,因为SurfaceControl在AIDL中是out类型,所以在WMS进程赋值
  2. WMS在创建java层SurfaceControl的同时通过nativeCreate方法到native层做一系列初始化
  3. SurfaceComposerClientcreateSurfaceChecked函数中通过ISurfaceComposerClient的Bp端mClientSurfaceFlinger进程请求创建Surface,即调用createSurface函数,而在SurfaceFlinger进程Surface对应的是Layer
  4. 在第一次创建Layer的子类BufferQueueLayer的过程中,即在BufferQueueLayeronFirstRef函数中会创建生产者消费者模型架构
  5. SurfaceFlinger进程的任务完成之后会直接new一个SurfaceControl,并将SurfaceFlinger进程创建的Layer引用和生产者保存到SurfaceControl中,最后将native层SurfaceControl指针保存到java层SurfaceControl
  6. native层SurfaceControl创建好了之后就可以通过此对象创建native层的Surface对象,最后将native层Surface指针保存到java层Surface,最终java层和native层的SurfaceSurfaceControl都创建完毕

最后

以上就是无限大神为你收集整理的AndroidQ 图形系统(1)Surface与SurfaceControl创建分析的全部内容,希望文章能够帮你解决AndroidQ 图形系统(1)Surface与SurfaceControl创建分析所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(43)

评论列表共有 0 条评论

立即
投稿
返回
顶部