Skip to content

Commit 8a3f409

Browse files
Treehugger RobotAndroid (Google) Code Review
authored andcommitted
Merge "SF: Store and manage snapshots for virtual displays" into main
2 parents c6bc5f5 + 5d66042 commit 8a3f409

3 files changed

Lines changed: 92 additions & 4 deletions

File tree

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 2024 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#pragma once
18+
19+
#include <optional>
20+
#include <string>
21+
22+
#include <ui/DisplayId.h>
23+
24+
#include "Utils/Dumper.h"
25+
26+
namespace android::display {
27+
28+
// Immutable state of a virtual display, captured on creation.
29+
class VirtualDisplaySnapshot {
30+
public:
31+
VirtualDisplaySnapshot(GpuVirtualDisplayId gpuId, std::string uniqueId)
32+
: mIsGpu(true), mUniqueId(std::move(uniqueId)), mVirtualId(gpuId) {}
33+
VirtualDisplaySnapshot(HalVirtualDisplayId halId, std::string uniqueId)
34+
: mIsGpu(false), mUniqueId(std::move(uniqueId)), mVirtualId(halId) {}
35+
36+
VirtualDisplayId displayId() const { return mVirtualId; }
37+
bool isGpu() const { return mIsGpu; }
38+
39+
void dump(utils::Dumper& dumper) const {
40+
using namespace std::string_view_literals;
41+
42+
dumper.dump("isGpu"sv, mIsGpu ? "true"sv : "false"sv);
43+
dumper.dump("uniqueId"sv, mUniqueId);
44+
}
45+
46+
private:
47+
const bool mIsGpu;
48+
const std::string mUniqueId;
49+
const VirtualDisplayId mVirtualId;
50+
};
51+
52+
} // namespace android::display

services/surfaceflinger/SurfaceFlinger.cpp

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -649,11 +649,12 @@ void SurfaceFlinger::enableHalVirtualDisplays(bool enable) {
649649
}
650650
}
651651

652-
VirtualDisplayId SurfaceFlinger::acquireVirtualDisplay(ui::Size resolution,
653-
ui::PixelFormat format) {
652+
VirtualDisplayId SurfaceFlinger::acquireVirtualDisplay(ui::Size resolution, ui::PixelFormat format,
653+
const std::string& uniqueId) {
654654
if (auto& generator = mVirtualDisplayIdGenerators.hal) {
655655
if (const auto id = generator->generateId()) {
656656
if (getHwComposer().allocateVirtualDisplay(*id, resolution, &format)) {
657+
acquireVirtualDisplaySnapshot(*id, uniqueId);
657658
return *id;
658659
}
659660

@@ -667,20 +668,30 @@ VirtualDisplayId SurfaceFlinger::acquireVirtualDisplay(ui::Size resolution,
667668

668669
const auto id = mVirtualDisplayIdGenerators.gpu.generateId();
669670
LOG_ALWAYS_FATAL_IF(!id, "Failed to generate ID for GPU virtual display");
671+
acquireVirtualDisplaySnapshot(*id, uniqueId);
670672
return *id;
671673
}
672674

673675
void SurfaceFlinger::releaseVirtualDisplay(VirtualDisplayId displayId) {
674676
if (const auto id = HalVirtualDisplayId::tryCast(displayId)) {
675677
if (auto& generator = mVirtualDisplayIdGenerators.hal) {
676678
generator->releaseId(*id);
679+
releaseVirtualDisplaySnapshot(*id);
677680
}
678681
return;
679682
}
680683

681684
const auto id = GpuVirtualDisplayId::tryCast(displayId);
682685
LOG_ALWAYS_FATAL_IF(!id);
683686
mVirtualDisplayIdGenerators.gpu.releaseId(*id);
687+
releaseVirtualDisplaySnapshot(*id);
688+
}
689+
690+
void SurfaceFlinger::releaseVirtualDisplaySnapshot(VirtualDisplayId displayId) {
691+
std::lock_guard lock(mVirtualDisplaysMutex);
692+
if (!mVirtualDisplays.erase(displayId)) {
693+
ALOGW("%s: Virtual display snapshot was not removed", __func__);
694+
}
684695
}
685696

686697
std::vector<PhysicalDisplayId> SurfaceFlinger::getPhysicalDisplayIdsLocked() const {
@@ -3798,7 +3809,7 @@ void SurfaceFlinger::processDisplayAdded(const wp<IBinder>& displayToken,
37983809
if (const auto& physical = state.physical) {
37993810
builder.setId(physical->id);
38003811
} else {
3801-
builder.setId(acquireVirtualDisplay(resolution, pixelFormat));
3812+
builder.setId(acquireVirtualDisplay(resolution, pixelFormat, state.uniqueId));
38023813
}
38033814

38043815
builder.setPixels(resolution);
@@ -5785,6 +5796,14 @@ void SurfaceFlinger::dumpDisplays(std::string& result) const {
57855796
utils::Dumper::Section section(dumper,
57865797
ftl::Concat("Virtual Display ", displayId.value).str());
57875798
display->dump(dumper);
5799+
5800+
if (const auto virtualIdOpt = VirtualDisplayId::tryCast(displayId)) {
5801+
std::lock_guard lock(mVirtualDisplaysMutex);
5802+
const auto virtualSnapshotIt = mVirtualDisplays.find(virtualIdOpt.value());
5803+
if (virtualSnapshotIt != mVirtualDisplays.end()) {
5804+
virtualSnapshotIt->second.dump(dumper);
5805+
}
5806+
}
57885807
}
57895808
}
57905809
}

services/surfaceflinger/SurfaceFlinger.h

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
#include "BackgroundExecutor.h"
7474
#include "Display/DisplayModeController.h"
7575
#include "Display/PhysicalDisplay.h"
76+
#include "Display/VirtualDisplaySnapshot.h"
7677
#include "DisplayDevice.h"
7778
#include "DisplayHardware/HWC2.h"
7879
#include "DisplayIdGenerator.h"
@@ -1075,8 +1076,20 @@ class SurfaceFlinger : public BnSurfaceComposer,
10751076
void enableHalVirtualDisplays(bool);
10761077

10771078
// Virtual display lifecycle for ID generation and HAL allocation.
1078-
VirtualDisplayId acquireVirtualDisplay(ui::Size, ui::PixelFormat) REQUIRES(mStateLock);
1079+
VirtualDisplayId acquireVirtualDisplay(ui::Size, ui::PixelFormat, const std::string& uniqueId)
1080+
REQUIRES(mStateLock);
1081+
template <typename ID>
1082+
void acquireVirtualDisplaySnapshot(ID displayId, const std::string& uniqueId) {
1083+
std::lock_guard lock(mVirtualDisplaysMutex);
1084+
const bool emplace_success =
1085+
mVirtualDisplays.try_emplace(displayId, displayId, uniqueId).second;
1086+
if (!emplace_success) {
1087+
ALOGW("%s: Virtual display snapshot with the same ID already exists", __func__);
1088+
}
1089+
}
1090+
10791091
void releaseVirtualDisplay(VirtualDisplayId);
1092+
void releaseVirtualDisplaySnapshot(VirtualDisplayId displayId);
10801093

10811094
// Returns a display other than `mActiveDisplayId` that can be activated, if any.
10821095
sp<DisplayDevice> getActivatableDisplay() const REQUIRES(mStateLock, kMainThreadContext);
@@ -1277,6 +1290,10 @@ class SurfaceFlinger : public BnSurfaceComposer,
12771290

12781291
display::PhysicalDisplays mPhysicalDisplays GUARDED_BY(mStateLock);
12791292

1293+
mutable std::mutex mVirtualDisplaysMutex;
1294+
ftl::SmallMap<VirtualDisplayId, const display::VirtualDisplaySnapshot, 2> mVirtualDisplays
1295+
GUARDED_BY(mVirtualDisplaysMutex);
1296+
12801297
// The inner or outer display for foldables, while unfolded or folded, respectively.
12811298
std::atomic<PhysicalDisplayId> mActiveDisplayId;
12821299

0 commit comments

Comments
 (0)