SurfaceControl和Surface都可以表示窗口,窗口管理服务通过SurfaceControl控制图层的状态,应用进程通过Surface向图层传送缓冲数据。创建图层是由surfaceflinger来创建。
创建图层是在SurfaceComposerClient.cpp中的createSurface中,生产图形需要先创建窗口,创建窗口时会请求surfaceflinger创建图层。
/frameworks/native/libs/gui/SurfaceComposerClient.cpp
sp<SurfaceControl> SurfaceComposerClient::createSurface(const String8& name, uint32_t w, uint32_t h,
2398 PixelFormat format, int32_t flags,
2399 const sp<IBinder>& parentHandle,
2400 LayerMetadata metadata,
2401 uint32_t* outTransformHint) {
2402 sp<SurfaceControl> s;
2403 createSurfaceChecked(name, w, h, format, &s, flags, parentHandle, std::move(metadata),
2404 outTransformHint);
2405 return s;
2406 }
2407
2408 static std::string toString(const String16& string) {
2409 return std::string(String8(string).c_str());
2410 }
2411
2412 status_t SurfaceComposerClient::createSurfaceChecked(const String8& name, uint32_t w, uint32_t h,
2413 PixelFormat format,
2414 sp<SurfaceControl>* outSurface, int32_t flags,
2415 const sp<IBinder>& parentHandle,
2416 LayerMetadata metadata,
2417 uint32_t* outTransformHint) {
2418 sp<SurfaceControl> sur;
2419 status_t err = mStatus;
2420
2421 if (mStatus == NO_ERROR) {
2422 gui::CreateSurfaceResult result;
2423 binder::Status status = mClient->createSurface(std::string(name.string()), flags,
2424 parentHandle, std::move(metadata), &result);
2425 err = statusTFromBinderStatus(status);
2426 if (outTransformHint) {
2427 *outTransformHint = result.transformHint;
2428 }
2429 ALOGE_IF(err, "SurfaceComposerClient::createSurface error %s", strerror(-err));
2430 if (err == NO_ERROR) {
2431 *outSurface = new SurfaceControl(this, result.handle, result.layerId,
2432 toString(result.layerName), w, h, format,
2433 result.transformHint, flags);
2434 }
2435 }
2436 return err;
2437 }
2438
createSurfaceChecked方法主要是用来创建窗口的。
首先参数中:w表示宽,h表示高,format表示格式
然后对mStatus进行判断,如果是NO_ERROR,则调用mClient的接口createSurface,并返回前面声明的result。
之后声明surfaceControl对象作为结果返回,窗口对象surfaceControl是一个包装类。参数包含图层id,layerName等。
添加图层
/frameworks/native/services/surfaceflinger/SurfaceFlinger.cpp
4168 status_t SurfaceFlinger::addClientLayer(LayerCreationArgs& args, const sp<IBinder>& handle,
4169 const sp<Layer>& layer, const wp<Layer>& parent,
4170 uint32_t* outTransformHint) {
4171 if (mNumLayers >= MAX_LAYERS) {
4172 ALOGE("AddClientLayer failed, mNumLayers (%zu) >= MAX_LAYERS (%zu)", mNumLayers.load(),
4173 MAX_LAYERS);
4174 static_cast<void>(mScheduler->schedule([=] {
4175 ALOGE("Dumping layer keeping > 20 children alive:");
4176 bool leakingParentLayerFound = false;
4177 mDrawingState.traverse([&](Layer* layer) {
4178 if (leakingParentLayerFound) {
4179 return;
4180 }
4181 if (layer->getChildrenCount() > 20) {
4182 leakingParentLayerFound = true;
4183 sp<Layer> parent = sp<Layer>::fromExisting(layer);
4184 while (parent) {
4185 ALOGE("Parent Layer: %s%s", parent->getName().c_str(),
4186 (parent->isHandleAlive() ? "handleAlive" : ""));
4187 parent = parent->getParent();
4188 }
4189 // Sample up to 100 layers
4190 ALOGE("Dumping random sampling of child layers total(%zu): ",
4191 layer->getChildrenCount());
4192 int sampleSize = (layer->getChildrenCount() / 100) + 1;
4193 layer->traverseChildren([&](Layer* layer) {
4194 if (rand() % sampleSize == 0) {
4195 ALOGE("Child Layer: %s", layer->getName().c_str());
4196 }
4197 });
4198 }
4199 });
4200
4201 int numLayers = 0;
4202 mDrawingState.traverse([&](Layer* layer) { numLayers++; });
4203
4204 ALOGE("Dumping random sampling of on-screen layers total(%u):", numLayers);
4205 mDrawingState.traverse([&](Layer* layer) {
4206 // Aim to dump about 200 layers to avoid totally trashing
4207 // logcat. On the other hand, if there really are 4096 layers
4208 // something has gone totally wrong its probably the most
4209 // useful information in logcat.
4210 if (rand() % 20 == 13) {
4211 ALOGE("Layer: %s%s", layer->getName().c_str(),
4212 (layer->isHandleAlive() ? "handleAlive" : ""));
4213 std::this_thread::sleep_for(std::chrono::milliseconds(5));
4214 }
4215 });
4216 ALOGE("Dumping random sampling of off-screen layers total(%zu): ",
4217 mOffscreenLayers.size());
4218 for (Layer* offscreenLayer : mOffscreenLayers) {
4219 if (rand() % 20 == 13) {
4220 ALOGE("Offscreen-layer: %s%s", offscreenLayer->getName().c_str(),
4221 (offscreenLayer->isHandleAlive() ? "handleAlive" : ""));
4222 std::this_thread::sleep_for(std::chrono::milliseconds(5));
4223 }
4224 }
4225 }));
4226 return NO_MEMORY;
4227 }
4228
4229 layer->updateTransformHint(mActiveDisplayTransformHint);
4230 if (outTransformHint) {
4231 *outTransformHint = mActiveDisplayTransformHint;
4232 }
4233 args.parentId = LayerHandle::getLayerId(args.parentHandle.promote());
4234 args.layerIdToMirror = LayerHandle::getLayerId(args.mirrorLayerHandle.promote());
4235 {
4236 std::scoped_lock<std::mutex> lock(mCreatedLayersLock);
4237 mCreatedLayers.emplace_back(layer, parent, args.addToRoot);
4238 mNewLayers.emplace_back(std::make_unique<frontend::RequestedLayerState>(args));
4239 args.mirrorLayerHandle.clear();
4240 args.parentHandle.clear();
4241 mNewLayerArgs.emplace_back(std::move(args));
4242 }
4243
4244 setTransactionFlags(eTransactionNeeded);
4245 return NO_ERROR;
4246 }
缓存图层
在android14中 缓冲图层的代码从BufferLayer.cpp文件改成了到了BufferQueue.cpp文件中
/frameworks/native/libs/gui/BufferQueue.cpp
void BufferQueue::createBufferQueue(sp<IGraphicBufferProducer>* outProducer,
102 sp<IGraphicBufferConsumer>* outConsumer,
103 bool consumerIsSurfaceFlinger) {
104 LOG_ALWAYS_FATAL_IF(outProducer == nullptr,
105 "BufferQueue: outProducer must not be NULL");
106 LOG_ALWAYS_FATAL_IF(outConsumer == nullptr,
107 "BufferQueue: outConsumer must not be NULL");
108
109 sp<BufferQueueCore> core(new BufferQueueCore());
110 LOG_ALWAYS_FATAL_IF(core == nullptr,
111 "BufferQueue: failed to create BufferQueueCore");
112
113 sp<IGraphicBufferProducer> producer(new BufferQueueProducer(core, consumerIsSurfaceFlinger));
114 LOG_ALWAYS_FATAL_IF(producer == nullptr,
115 "BufferQueue: failed to create BufferQueueProducer");
116
117 sp<IGraphicBufferConsumer> consumer(new BufferQueueConsumer(core));
118 LOG_ALWAYS_FATAL_IF(consumer == nullptr,
119 "BufferQueue: failed to create BufferQueueConsumer");
120
121 *outProducer = producer;
122 *outConsumer = consumer;
123 }
124
125 }; // namespace android
在上述代码中可以看到,声明了BufferQueueCore,BufferQueueProducer,BufferQueueConsumer。
BufferQueueCore:图形缓冲管理者,通过缓冲队列管理图形缓冲。
BufferQueueProducer:从BufferQueueCore中取出空闲的缓冲。
BufferQueueConsumer:从BufferQueueCore取出可消费的图形缓冲。