From 8c42cf19d10c9a685e8d6a88c9057435dd738a6b Mon Sep 17 00:00:00 2001 From: Melody Hsu Date: Wed, 5 Jun 2024 00:07:03 +0000 Subject: Remove main thread double hop from screenshots The SF main thread is accessed twice during screenshots, which leads to possible inconsistent states between jumps onto the main thread. Removing the double hop preserves correctness and reduces the amount of computation taking place on the main thread. The only time the main thread should be accessed should be when getting layer snapshots. All other work related to screenshots can take place in a binder thread. Bug: b/294936197, b/285553970 Test: atest SurfaceFlinger_test Flag: com.android.graphics.surfaceflinger.flags.single_hop_screenshot Change-Id: If9fd36f82c2d550bc0821b52fef3ea88b8099116 --- services/surfaceflinger/RegionSamplingThread.cpp | 33 +++++++++++++++++------- 1 file changed, 23 insertions(+), 10 deletions(-) (limited to 'services/surfaceflinger/RegionSamplingThread.cpp') diff --git a/services/surfaceflinger/RegionSamplingThread.cpp b/services/surfaceflinger/RegionSamplingThread.cpp index 59eb7f5d4a..5add290e96 100644 --- a/services/surfaceflinger/RegionSamplingThread.cpp +++ b/services/surfaceflinger/RegionSamplingThread.cpp @@ -348,17 +348,30 @@ void RegionSamplingThread::captureSample() { constexpr bool kGrayscale = false; constexpr bool kIsProtected = false; - if (const auto fenceResult = - mFlinger.captureScreenshot(SurfaceFlinger::RenderAreaBuilderVariant( - std::in_place_type, - sampledBounds, sampledBounds.getSize(), - ui::Dataspace::V0_SRGB, - kHintForSeamlessTransition, - true /* captureSecureLayers */, displayWeak), - getLayerSnapshotsFn, buffer, kRegionSampling, kGrayscale, - kIsProtected, nullptr) + SurfaceFlinger::RenderAreaBuilderVariant + renderAreaBuilder(std::in_place_type, sampledBounds, + sampledBounds.getSize(), ui::Dataspace::V0_SRGB, + kHintForSeamlessTransition, true /* captureSecureLayers */, + displayWeak); + + FenceResult fenceResult; + if (FlagManager::getInstance().single_hop_screenshot() && + FlagManager::getInstance().ce_fence_promise()) { + std::vector> layerFEs; + auto displayState = + mFlinger.getDisplayAndLayerSnapshotsFromMainThread(renderAreaBuilder, + getLayerSnapshotsFn, layerFEs); + fenceResult = + mFlinger.captureScreenshot(renderAreaBuilder, buffer, kRegionSampling, kGrayscale, + kIsProtected, nullptr, displayState, layerFEs) .get(); - fenceResult.ok()) { + } else { + fenceResult = + mFlinger.captureScreenshotLegacy(renderAreaBuilder, getLayerSnapshotsFn, buffer, + kRegionSampling, kGrayscale, kIsProtected, nullptr) + .get(); + } + if (fenceResult.ok()) { fenceResult.value()->waitForever(LOG_TAG); } -- cgit v1.2.3-59-g8ed1b