Skip to content
This repository was archived by the owner on Oct 8, 2024. It is now read-only.

Commit e9ecf2d

Browse files
committed
Update GPUDevice thread for DRM-master setting/dropping
GPUDevice thread will monitor hwc.lock once HWC is not drm master Drm Master will not be set once hwc.lock is not available. On Acrn and Celadon, an additional selinux patch are needed for the permission on hwc.lock. Change-Id: If185b7f1b912dd58ffcb0287fe1b3a234afc5b57 Test: Compile sucessful for Android. Tracked-On: https://jira.devtools.intel.com/browse/OAM-76578 Signed-off-by: Shaofeng Tang <[email protected]>
1 parent cc914c3 commit e9ecf2d

File tree

9 files changed

+143
-24
lines changed

9 files changed

+143
-24
lines changed

common/core/gpudevice.cpp

Lines changed: 65 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,21 @@ GpuDevice::GpuDevice() : HWCThread(-8, "GpuDevice") {
2929

3030
GpuDevice::~GpuDevice() {
3131
display_manager_.reset(nullptr);
32+
HWCThread::Exit();
33+
34+
if (-1 != lock_fd_) {
35+
close(lock_fd_);
36+
lock_fd_ = -1;
37+
}
38+
}
39+
40+
void GpuDevice::ResetAllDisplayCommit(bool enable) {
41+
if (enable == enable_all_display_)
42+
return;
43+
enable_all_display_ = enable;
44+
size_t size = total_displays_.size();
45+
for (size_t i = 0; i < size; i++)
46+
total_displays_.at(i)->EnableDRMCommit(enable);
3247
}
3348

3449
bool GpuDevice::Initialize() {
@@ -57,13 +72,19 @@ bool GpuDevice::Initialize() {
5772
display_manager_->RemoveUnreservedPlanes();
5873
}
5974

60-
lock_fd_ = open("/vendor/hwc.lock", O_RDONLY);
75+
// Ignore all updates by default
76+
ResetAllDisplayCommit(false);
77+
lock_fd_ = open(HWC_LOCK_FILE, O_RDONLY);
6178
if (-1 != lock_fd_) {
6279
if (!InitWorker()) {
6380
ETRACE("Failed to initalize thread for GpuDevice. %s", PRINTERROR());
6481
}
6582
} else {
66-
ITRACE("Failed to open " LOCK_DIR_PREFIX "/hwc.lock file!");
83+
ETRACE("Failed to open %s", HWC_LOCK_FILE);
84+
// HWC should become drm master and start to commit.
85+
// if hwc.lock is not available
86+
display_manager_->setDrmMaster(true);
87+
ResetAllDisplayCommit(true);
6788
}
6889

6990
return true;
@@ -103,14 +124,42 @@ void GpuDevice::GetConnectedPhysicalDisplays(
103124
}
104125

105126
bool GpuDevice::EnableDRMCommit(bool enable, uint32_t display_id) {
106-
// TODO clean all display for commit status
107127
size_t size = total_displays_.size();
108128
bool ret = false;
109129
if (size > display_id)
110130
ret = total_displays_.at(display_id)->EnableDRMCommit(enable);
111131
return ret;
112132
}
113133

134+
bool GpuDevice::ResetDrmMaster(bool drop_master) {
135+
bool ret = true;
136+
if (drop_master) {
137+
ResetAllDisplayCommit(false);
138+
display_manager_->DropDrmMaster();
139+
ITRACE("locking %s and monitoring if %s is unlocked.", HWC_LOCK_FILE,
140+
HWC_LOCK_FILE);
141+
// Resume GpuDevice thread to check hwc.lock and re-apply drm master.
142+
lock_fd_ = open(HWC_LOCK_FILE, O_RDONLY);
143+
if (-1 != lock_fd_) {
144+
// Only resume GpuDevice thread for dropping DRM Master and
145+
// the lock file exist.
146+
Resume();
147+
return !display_manager_->IsDrmMaster();
148+
}
149+
}
150+
// In case of setDrmMaster or the lock file is not exist.
151+
// Re-set DRM Master true.
152+
display_manager_->setDrmMaster(false);
153+
ResetAllDisplayCommit(true);
154+
DisableWatch();
155+
156+
if (drop_master)
157+
ret = !display_manager_->IsDrmMaster();
158+
else
159+
ret = display_manager_->IsDrmMaster();
160+
return ret;
161+
}
162+
114163
const std::vector<NativeDisplay *> &GpuDevice::GetAllDisplays() {
115164
return total_displays_;
116165
}
@@ -967,8 +1016,6 @@ uint32_t GpuDevice::GetDisplayIDFromConnectorID(const uint32_t connector_id) {
9671016
}
9681017

9691018
void GpuDevice::HandleRoutine() {
970-
bool update_ignored = false;
971-
9721019
// Iniitialize resources to monitor external events.
9731020
// These can be two types:
9741021
// 1) We are showing splash screen and another App
@@ -978,23 +1025,20 @@ void GpuDevice::HandleRoutine() {
9781025
// we need to take control.
9791026
// TODO: Add splash screen support.
9801027
if (lock_fd_ != -1) {
981-
display_manager_->IgnoreUpdates();
982-
update_ignored = true;
983-
9841028
if (flock(lock_fd_, LOCK_EX) != 0) {
985-
ETRACE("Failed to wait on the hwc lock.");
1029+
ITRACE("Fail to grab the hwc lock.");
9861030
} else {
9871031
ITRACE("Successfully grabbed the hwc lock.");
1032+
// Set DRM master
1033+
if (!display_manager_->IsDrmMaster())
1034+
display_manager_->setDrmMaster(true);
1035+
// stop ignoring and force refresh
1036+
ResetAllDisplayCommit(true);
1037+
flock(lock_fd_, LOCK_UN);
1038+
close(lock_fd_);
1039+
lock_fd_ = -1;
9881040
}
989-
990-
display_manager_->setDrmMaster();
991-
992-
close(lock_fd_);
993-
lock_fd_ = -1;
9941041
}
995-
996-
if (update_ignored)
997-
display_manager_->ForceRefresh();
9981042
}
9991043

10001044
void GpuDevice::HandleWait() {
@@ -1004,7 +1048,10 @@ void GpuDevice::HandleWait() {
10041048
}
10051049

10061050
void GpuDevice::DisableWatch() {
1007-
HWCThread::Exit();
1051+
if (lock_fd_ != -1) {
1052+
close(lock_fd_);
1053+
lock_fd_ = -1;
1054+
}
10081055
}
10091056

10101057
GpuDevice &GpuDevice::getInstance() {

common/core/logicaldisplay.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ int LogicalDisplay::GetDisplayPipe() {
5151
return physical_display_->GetDisplayPipe();
5252
}
5353

54+
bool LogicalDisplay::EnableDRMCommit(bool enable) {
55+
return physical_display_->EnableDRMCommit(enable);
56+
}
57+
5458
bool LogicalDisplay::SetActiveConfig(uint32_t config) {
5559
bool success = physical_display_->SetActiveConfig(config);
5660
width_ = (physical_display_->Width()) / total_divisions_;

common/core/logicaldisplay.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ class LogicalDisplay : public NativeDisplay {
103103
bool GetDisplayConfigs(uint32_t *num_configs, uint32_t *configs) override;
104104
bool GetDisplayName(uint32_t *size, char *name) override;
105105

106+
bool EnableDRMCommit(bool enable) override;
107+
106108
uint32_t GetXTranslation() override {
107109
return (((physical_display_->Width()) / total_divisions_) * index_);
108110
}

common/core/mosaicdisplay.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,13 @@ int MosaicDisplay::GetDisplayPipe() {
110110
return physical_displays_.at(0)->GetDisplayPipe();
111111
}
112112

113+
bool MosaicDisplay::EnableDRMCommit(bool enable) {
114+
uint32_t size = physical_displays_.size();
115+
for (uint32_t i = 0; i < size; i++)
116+
physical_displays_.at(i)->EnableDRMCommit(enable);
117+
return true;
118+
}
119+
113120
bool MosaicDisplay::SetActiveConfig(uint32_t config) {
114121
config_ = config;
115122
width_ = 0;

common/core/mosaicdisplay.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ class MosaicDisplay : public NativeDisplay {
9494
bool GetDisplayConfigs(uint32_t *num_configs, uint32_t *configs) override;
9595
bool GetDisplayName(uint32_t *size, char *name) override;
9696

97+
bool EnableDRMCommit(bool enable) override;
98+
9799
uint32_t GetXTranslation() override {
98100
return 0;
99101
}

public/gpudevice.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717
#ifndef PUBLIC_GPUDEVICE_H_
1818
#define PUBLIC_GPUDEVICE_H_
1919

20+
#ifndef HWC_LOCK_FILE
21+
#define HWC_LOCK_FILE "/vendor/hwc.lock"
22+
#endif
23+
2024
#include <stdint.h>
2125
#include <fstream>
2226
#include <sstream>
@@ -97,11 +101,15 @@ class GpuDevice : public HWCThread {
97101

98102
bool EnableDRMCommit(bool enable, uint32_t display_id);
99103

104+
bool ResetDrmMaster(bool drop_master);
105+
100106
std::vector<uint32_t> GetDisplayReservedPlanes(uint32_t display_id);
101107

102108
private:
103109
GpuDevice();
104110

111+
void ResetAllDisplayCommit(bool enable);
112+
105113
enum InitializationType {
106114
kUnInitialized = 0, // Nothing Initialized.
107115
kInitialized = 1 << 1 // Everything Initialized
@@ -168,9 +176,11 @@ class GpuDevice : public HWCThread {
168176
std::vector<NativeDisplay*> total_displays_;
169177

170178
bool reserve_plane_ = false;
179+
bool enable_all_display_ = true;
171180
std::map<uint8_t, std::vector<uint32_t>> reserved_drm_display_planes_map_;
172181
uint32_t initialization_state_ = kUnInitialized;
173182
SpinLock initialization_state_lock_;
183+
SpinLock drm_master_lock_;
174184
int lock_fd_ = -1;
175185
friend class DrmDisplayManager;
176186
};

wsi/displaymanager.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,11 @@ class DisplayManager {
5353
// manager until ForceRefresh is called.
5454
virtual void IgnoreUpdates() = 0;
5555

56-
virtual void setDrmMaster() = 0;
56+
virtual void setDrmMaster(bool must_set) = 0;
57+
58+
virtual void DropDrmMaster() = 0;
59+
60+
virtual bool IsDrmMaster() = 0;
5761

5862
// Get FD associated with this DisplayManager.
5963
virtual uint32_t GetFD() const = 0;

wsi/drm/drmdisplaymanager.cpp

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ DrmDisplayManager::DrmDisplayManager() : HWCThread(-8, "DisplayManager") {
4545
DrmDisplayManager::~DrmDisplayManager() {
4646
CTRACE();
4747
std::vector<std::unique_ptr<DrmDisplay>>().swap(displays_);
48+
4849
#ifndef DISABLE_HOTPLUG_NOTIFICATION
4950
close(hotplug_fd_);
5051
#endif
@@ -112,6 +113,7 @@ bool DrmDisplayManager::Initialize() {
112113

113114
fd_handler_.AddFd(hotplug_fd_);
114115
#endif
116+
115117
IHOTPLUGEVENTTRACE("DisplayManager Initialization succeeded.");
116118
return true;
117119
}
@@ -442,15 +444,49 @@ void DrmDisplayManager::IgnoreUpdates() {
442444
}
443445
}
444446

445-
void DrmDisplayManager::setDrmMaster() {
446-
int ret = drmSetMaster(fd_);
447-
while (ret) {
448-
usleep(10000);
447+
void DrmDisplayManager::setDrmMaster(bool must_set) {
448+
spin_lock_.lock();
449+
if (drm_master_) {
450+
spin_lock_.unlock();
451+
return;
452+
}
453+
int ret = 0;
454+
uint8_t retry_times = 0;
455+
do {
449456
ret = drmSetMaster(fd_);
457+
if (!must_set)
458+
retry_times++;
450459
if (ret) {
451460
ETRACE("Failed to call drmSetMaster : %s", PRINTERROR());
461+
usleep(10000);
462+
} else {
463+
ITRACE("Successfully set as DRM master.");
464+
drm_master_ = true;
452465
}
466+
} while (ret && retry_times < 10);
467+
spin_lock_.unlock();
468+
}
469+
470+
void DrmDisplayManager::DropDrmMaster() {
471+
spin_lock_.lock();
472+
if (!drm_master_) {
473+
spin_lock_.unlock();
474+
return;
453475
}
476+
int ret = 0;
477+
uint8_t retry_times = 0;
478+
do {
479+
ret = drmDropMaster(fd_);
480+
retry_times++;
481+
if (ret) {
482+
ETRACE("Failed to call drmDropMaster : %s", PRINTERROR());
483+
usleep(10000);
484+
} else {
485+
ITRACE("Successfully drop DRM master.");
486+
drm_master_ = false;
487+
}
488+
} while (ret && retry_times < 10);
489+
spin_lock_.unlock();
454490
}
455491

456492
void DrmDisplayManager::HandleLazyInitialization() {

wsi/drm/drmdisplaymanager.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,13 @@ class DrmDisplayManager : public HWCThread, public DisplayManager {
7171

7272
void IgnoreUpdates() override;
7373

74-
void setDrmMaster() override;
74+
void setDrmMaster(bool must_set) override;
75+
76+
void DropDrmMaster() override;
77+
78+
bool IsDrmMaster() override {
79+
return drm_master_;
80+
}
7581

7682
uint32_t GetFD() const override {
7783
return fd_;
@@ -115,6 +121,7 @@ class DrmDisplayManager : public HWCThread, public DisplayManager {
115121
bool release_lock_ = false;
116122
SpinLock spin_lock_;
117123
int connected_display_count_ = 0;
124+
bool drm_master_ = false;
118125
};
119126

120127
} // namespace hwcomposer

0 commit comments

Comments
 (0)