Provide function to mirror display for screenrecord.
The current mirroring for shell screenrecording is by using the layer
stack mirroring. However, we want to port all layerStack mirroring to
use layer mirroring. This add a new function that allows the root layers
of a display to get mirrored by passing in a specified displayId.
The code will iterate though the layers and find root layers that are
rendered onto the desired display. SF will then clone the roots and set
them as a child of a new ContainerLayer that will be at the root of all
the mirrored roots. This is because a display may have multiple root
layers.
Test: adb shell screenrecord
Bug: 237664947
Change-Id: I31151527f5377079e2293be2c995f6a3316639da
diff --git a/services/surfaceflinger/SurfaceFlinger.cpp b/services/surfaceflinger/SurfaceFlinger.cpp
index c99c40e..ce8192a 100644
--- a/services/surfaceflinger/SurfaceFlinger.cpp
+++ b/services/surfaceflinger/SurfaceFlinger.cpp
@@ -2100,6 +2100,7 @@
bool needsTraversal = false;
if (clearTransactionFlags(eTransactionFlushNeeded)) {
+ needsTraversal |= commitMirrorDisplays(vsyncId);
needsTraversal |= commitCreatedLayers(vsyncId);
needsTraversal |= flushTransactionQueues(vsyncId);
}
@@ -4604,6 +4605,55 @@
false /* addToRoot */, nullptr /* outTransformHint */);
}
+status_t SurfaceFlinger::mirrorDisplay(DisplayId displayId, const LayerCreationArgs& args,
+ sp<IBinder>* outHandle, int32_t* outLayerId) {
+ IPCThreadState* ipc = IPCThreadState::self();
+ const int uid = ipc->getCallingUid();
+ if (uid != AID_ROOT && uid != AID_GRAPHICS && uid != AID_SYSTEM && uid != AID_SHELL) {
+ ALOGE("Permission denied when trying to mirror display");
+ return PERMISSION_DENIED;
+ }
+
+ ui::LayerStack layerStack;
+ sp<Layer> rootMirrorLayer;
+ status_t result = 0;
+
+ {
+ Mutex::Autolock lock(mStateLock);
+
+ const auto display = getDisplayDeviceLocked(displayId);
+ if (!display) {
+ return NAME_NOT_FOUND;
+ }
+
+ layerStack = display->getLayerStack();
+ LayerCreationArgs mirrorArgs = args;
+ mirrorArgs.flags |= ISurfaceComposerClient::eNoColorFill;
+ result = createEffectLayer(mirrorArgs, outHandle, &rootMirrorLayer);
+ *outLayerId = rootMirrorLayer->sequence;
+ result |= addClientLayer(args.client, *outHandle, rootMirrorLayer /* layer */,
+ nullptr /* parent */, true /* addToRoot */,
+ nullptr /* outTransformHint */);
+ }
+
+ if (result != NO_ERROR) {
+ return result;
+ }
+
+ if (mTransactionTracing) {
+ mTransactionTracing->onLayerAdded((*outHandle)->localBinder(), *outLayerId, args.name,
+ args.flags, -1 /* parentId */);
+ }
+
+ {
+ std::scoped_lock<std::mutex> lock(mMirrorDisplayLock);
+ mMirrorDisplays.emplace_back(layerStack, *outHandle, args.client);
+ }
+
+ setTransactionFlags(eTransactionFlushNeeded);
+ return NO_ERROR;
+}
+
status_t SurfaceFlinger::createLayer(LayerCreationArgs& args, sp<IBinder>* outHandle,
const sp<IBinder>& parentHandle, int32_t* outLayerId,
const sp<Layer>& parentLayer, uint32_t* outTransformHint) {
@@ -7127,6 +7177,45 @@
return buffer;
}
+bool SurfaceFlinger::commitMirrorDisplays(int64_t vsyncId) {
+ std::vector<MirrorDisplayState> mirrorDisplays;
+ {
+ std::scoped_lock<std::mutex> lock(mMirrorDisplayLock);
+ mirrorDisplays = std::move(mMirrorDisplays);
+ mMirrorDisplays.clear();
+ if (mirrorDisplays.size() == 0) {
+ return false;
+ }
+ }
+
+ sp<IBinder> unused;
+ for (const auto& mirrorDisplay : mirrorDisplays) {
+ // Set mirror layer's default layer stack to -1 so it doesn't end up rendered on a display
+ // accidentally.
+ sp<Layer> rootMirrorLayer = Layer::fromHandle(mirrorDisplay.rootHandle).promote();
+ rootMirrorLayer->setLayerStack(ui::LayerStack::fromValue(-1));
+ for (const auto& layer : mDrawingState.layersSortedByZ) {
+ if (layer->getLayerStack() != mirrorDisplay.layerStack ||
+ layer->isInternalDisplayOverlay()) {
+ continue;
+ }
+
+ LayerCreationArgs mirrorArgs(this, mirrorDisplay.client, "MirrorLayerParent",
+ ISurfaceComposerClient::eNoColorFill,
+ gui::LayerMetadata());
+ sp<Layer> childMirror;
+ createEffectLayer(mirrorArgs, &unused, &childMirror);
+ childMirror->setClonedChild(layer->createClone());
+ if (mTransactionTracing) {
+ mTransactionTracing->onLayerAddedToDrawingState(childMirror->getSequence(),
+ vsyncId);
+ }
+ childMirror->reparent(mirrorDisplay.rootHandle);
+ }
+ }
+ return true;
+}
+
bool SurfaceFlinger::commitCreatedLayers(int64_t vsyncId) {
std::vector<LayerCreatedState> createdLayers;
{