MythTV  master
mythdrmvrr.cpp
Go to the documentation of this file.
1 // MythTV
2 #include "mythdrmvrr.h"
4 
5 #define LOC QString("FreeSync: ")
6 
9 void MythDRMVRR::ForceFreeSync(const MythDRMPtr& Device, bool Enable)
10 {
11  if (!(Device && Device->Authenticated() && Device->GetCrtc()))
12  return;
13 
14  auto freesync = CreateFreeSync(Device, {0,0,false});
15  if (!freesync)
16  {
17  LOG(VB_GENERAL, LOG_INFO, LOC + "No FreeSync support detected - cannot force");
18  return;
19  }
20 
21  if (freesync->Enabled() == Enable)
22  {
23  LOG(VB_GENERAL, LOG_INFO, LOC + QString("FreeSync already %1abled")
24  .arg(Enable ? "en" : "dis"));
25  return;
26  }
27 
28  if (!freesync->IsControllable())
29  {
30  LOG(VB_GENERAL, LOG_INFO, LOC + QString("Cannot %1able FreeSync - do not have permission")
31  .arg(Enable ? "en" : "dis"));
32  return;
33  }
34 
35  auto * freesync2 = dynamic_cast<MythDRMVRR*>(freesync.get());
36  if (!freesync2)
37  return;
38 
39  // We have no Qt QPA plugin at this point, so no atomic modesetting etc etc.
40  // Just try and enable the property directly.
41  if (drmModeObjectSetProperty(Device->GetFD(), Device->GetCrtc()->m_id,
42  DRM_MODE_OBJECT_CRTC, freesync2->GetVRRProperty()->m_id, Enable ? 1 : 0) == 0)
43  {
44  // Release freesync now so that it doesn't reset the state on deletion
45  freesync = nullptr;
46  s_freeSyncDefaultValue = !Enable;
47  s_freeSyncResetOnExit = true;
48  LOG(VB_GENERAL, LOG_INFO, LOC + (Enable ? "Enabled" : "Disabled"));
49  }
50  else
51  {
52  LOG(VB_GENERAL, LOG_ERR, LOC + "Error setting FreeSync");
53  }
54 }
55 
57 {
58  if (!(Device && Device->GetConnector() && Device->GetCrtc()))
59  return nullptr;
60 
61  auto connector = Device->GetConnector();
62  auto capable = MythDRMProperty::GetProperty("VRR_CAPABLE", connector->m_properties);
63  if (!capable)
64  return nullptr;
65  auto * capableval = dynamic_cast<MythDRMRangeProperty*>(capable.get());
66  if (!capableval || capableval->m_value < 1)
67  return nullptr;
68 
69  auto crtc = Device->GetCrtc();
70  auto enabled = MythDRMProperty::GetProperty("VRR_ENABLED", crtc->m_properties);
71  if (!enabled)
72  return nullptr;
73  auto * enabledval = dynamic_cast<MythDRMRangeProperty*>(enabled.get());
74  if (!enabledval)
75  return nullptr;
76 
77  // We have a valid device with VRR_CAPABLE property (connector) and VRR_ENABLED
78  // property (CRTC). Now check whether it is enabled and whether we can control it.
79  bool isenabled = enabledval->m_value > 0;
80  bool controllable = Device->Atomic() && Device->Authenticated();
81  return std::shared_ptr<MythVRR>(new MythDRMVRR(Device, enabled, controllable, isenabled, Range));
82 }
83 
98 MythDRMVRR::MythDRMVRR(MythDRMPtr Device, DRMProp VRRProp, bool Controllable,
99  bool Enabled, MythVRRRange Range)
100  : MythVRR(Controllable, FreeSync, Enabled, Range),
101  m_device(std::move(Device)),
102  m_vrrProp(std::move(VRRProp))
103 {
104 }
105 
107 {
109  {
110  if (m_controllable)
111  {
112  LOG(VB_GENERAL, LOG_INFO, LOC + "Resetting FreeSync to desktop default");
114  }
115  s_freeSyncResetOnExit = false;
116  }
117 }
118 
119 void MythDRMVRR::SetEnabled(bool Enable)
120 {
121 #ifdef USING_QTPRIVATEHEADERS
122  if (m_device && m_vrrProp && m_device->GetCrtc() &&
123  m_device->QueueAtomics({{ m_device->GetCrtc()->m_id, m_vrrProp->m_id, Enable ? 1 : 0 }}))
124 #endif
125  {
126  m_enabled = Enable;
127  LOG(VB_GENERAL, LOG_INFO, LOC + (Enable ? "Enabled" : "Disabled"));
128  }
129 }
130 
132 {
133  return m_vrrProp;
134 }
MythDRMVRR::ForceFreeSync
static void ForceFreeSync(const MythDRMPtr &Device, bool Enable)
Force FreeSync on or off before the main app is started.
Definition: mythdrmvrr.cpp:9
MythDRMVRR::CreateFreeSync
static MythVRRPtr CreateFreeSync(const MythDRMPtr &Device, MythVRRRange Range)
Definition: mythdrmvrr.cpp:56
MythDRMVRR
A wrapper around FreeSync/Adaptive-Sync support.
Definition: mythdrmvrr.h:8
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:39
Device
A device containing images (ie. USB stick, CD, storage group etc)
Definition: imagemanager.cpp:35
MythDRMProperty::GetProperty
static DRMProp GetProperty(const QString &Name, const DRMProps &Properties)
Definition: mythdrmproperty.cpp:55
MythDRMVRR::m_vrrProp
DRMProp m_vrrProp
Definition: mythdrmvrr.h:27
MythDRMVRR::GetVRRProperty
DRMProp GetVRRProperty()
Definition: mythdrmvrr.cpp:131
MythVRR
Definition: mythvrr.h:14
mythlogging.h
MythDRMRangeProperty
Definition: mythdrmproperty.h:42
MythDRMVRR::s_freeSyncResetOnExit
static bool s_freeSyncResetOnExit
Definition: mythdrmvrr.h:11
MythVRR::m_controllable
bool m_controllable
Definition: mythvrr.h:37
MythDRMVRR::~MythDRMVRR
~MythDRMVRR() override
Definition: mythdrmvrr.cpp:106
MythDRMPtr
std::shared_ptr< class MythDRMDevice > MythDRMPtr
Definition: mythdrmdevice.h:18
DRMProp
std::shared_ptr< class MythDRMProperty > DRMProp
Definition: mythdrmproperty.h:7
MythDRMVRR::m_device
MythDRMPtr m_device
Definition: mythdrmvrr.h:26
LOC
#define LOC
Definition: mythdrmvrr.cpp:5
MythVRRRange
std::tuple< int, int, bool > MythVRRRange
Definition: mythedid.h:19
MythDRMVRR::SetEnabled
void SetEnabled(bool Enable=true) override
Definition: mythdrmvrr.cpp:119
MythDRMVRR::MythDRMVRR
MythDRMVRR(MythDRMPtr Device, DRMProp VRRProp, bool Controllable, bool Enabled, MythVRRRange Range)
Definition: mythdrmvrr.cpp:98
std
Definition: mythchrono.h:23
mythdrmvrr.h
MythDRMVRR::s_freeSyncDefaultValue
static bool s_freeSyncDefaultValue
Definition: mythdrmvrr.h:12
MythDRMRangeProperty::m_value
uint64_t m_value
Definition: mythdrmproperty.h:47
MythVRRPtr
std::shared_ptr< class MythVRR > MythVRRPtr
Definition: mythvrr.h:11