Skip to content

Commit

Permalink
[pvr] Cleanup: Use MathUtils::round_int instead of own implementation…
Browse files Browse the repository at this point in the history
… to narrow float/double values to int values.
  • Loading branch information
ksooo committed Dec 18, 2024
1 parent 19a4296 commit 5877ee3
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 62 deletions.
4 changes: 2 additions & 2 deletions xbmc/pvr/addons/PVRClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
#include "pvr/timers/PVRTimers.h"
#include "settings/AdvancedSettings.h"
#include "settings/SettingsComponent.h"
#include "utils/MathUtils.h"
#include "utils/StringUtils.h"
#include "utils/log.h"

Expand Down Expand Up @@ -169,8 +170,7 @@ class CAddonRecording : public PVR_RECORDING
iGenreType = recording.GenreType();
iGenreSubType = recording.GenreSubType();
iPlayCount = recording.GetLocalPlayCount();
iLastPlayedPosition =
static_cast<int>(std::lrint(recording.GetLocalResumePoint().timeInSeconds));
iLastPlayedPosition = MathUtils::round_int(recording.GetLocalResumePoint().timeInSeconds);
bIsDeleted = recording.IsDeleted();
iEpgEventId = recording.BroadcastUid();
iChannelUid = recording.ChannelUid();
Expand Down
19 changes: 11 additions & 8 deletions xbmc/pvr/epg/EpgInfoTag.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,9 +245,9 @@ bool CPVREpgInfoTag::IsUpcoming() const
return (m_startTime > now);
}

float CPVREpgInfoTag::ProgressPercentage() const
double CPVREpgInfoTag::ProgressPercentage() const
{
float fReturn = 0.0f;
double ret = 0.0;

time_t currentTime, startTime, endTime;
CDateTime::GetCurrentDateTime().GetAsUTCDateTime().GetAsTime(currentTime);
Expand All @@ -256,16 +256,19 @@ float CPVREpgInfoTag::ProgressPercentage() const

if (currentTime >= startTime && currentTime <= endTime)
{
const std::chrono::duration<float> current{currentTime - startTime};
const std::chrono::duration<float> total{endTime - startTime > 0 ? endTime - startTime
: 3600.0f};
fReturn = current.count() * 100.0f / total.count();
const std::chrono::duration<double> total{endTime - startTime > 0 ? endTime - startTime
: 3600.0};
if (total.count())
{
const std::chrono::duration<double> current{currentTime - startTime};
ret = current.count() * 100.0 / total.count();
}
}
else if (currentTime > endTime)
{
fReturn = 100.0f;
ret = 100.0;
}
return fReturn;
return ret;
}

unsigned int CPVREpgInfoTag::Progress() const
Expand Down
2 changes: 1 addition & 1 deletion xbmc/pvr/epg/EpgInfoTag.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ class CPVREpgInfoTag final : public ISerializable,
* @brief Get the progress of this tag in percent.
* @return The current progress of this tag.
*/
float ProgressPercentage() const;
double ProgressPercentage() const;

/*!
* @brief Get the progress of this tag in seconds.
Expand Down
30 changes: 13 additions & 17 deletions xbmc/pvr/guilib/GUIEPGGridContainer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,8 @@ void CGUIEPGGridContainer::Process(unsigned int currentTime, CDirtyRegionList& d
{
int iItem =
(m_orientation == VERTICAL)
? MathUtils::round_int(static_cast<double>(m_channelScrollOffset / m_channelHeight))
: MathUtils::round_int(
static_cast<double>(m_programmeScrollOffset / (m_gridHeight / m_blocksPerPage)));
? MathUtils::round_int(m_channelScrollOffset / m_channelHeight)
: MathUtils::round_int(m_programmeScrollOffset / (m_gridHeight / m_blocksPerPage));

CGUIMessage msg(GUI_MSG_ITEM_SELECT, GetID(), m_pageControl, iItem);
SendWindowMessage(msg);
Expand Down Expand Up @@ -838,16 +837,15 @@ float CGUIEPGGridContainer::GetProgrammeScrollOffsetPos() const
int CGUIEPGGridContainer::GetChannelScrollOffset(CGUIListItemLayout* layout) const
{
if (m_bEnableChannelScrolling)
return MathUtils::round_int(
static_cast<double>(m_channelScrollOffset / layout->Size(m_orientation)));
return MathUtils::round_int(m_channelScrollOffset / layout->Size(m_orientation));
else
return m_channelOffset;
}

int CGUIEPGGridContainer::GetProgrammeScrollOffset() const
{
if (m_bEnableProgrammeScrolling)
return MathUtils::round_int(static_cast<double>(m_programmeScrollOffset / m_blockSize));
return MathUtils::round_int(m_programmeScrollOffset / m_blockSize);
else
return m_blockOffset;
}
Expand Down Expand Up @@ -1262,11 +1260,10 @@ EVENT_RESULT CGUIEPGGridContainer::OnMouseEvent(const CPoint& point,
// we're done with exclusive access
CGUIMessage msg(GUI_MSG_EXCLUSIVE_MOUSE, 0, GetParentID());
SendWindowMessage(msg);
ScrollToChannelOffset(MathUtils::round_int(
static_cast<double>(m_channelScrollOffset / m_channelLayout->Size(m_orientation))));
ScrollToChannelOffset(
MathUtils::round_int(m_channelScrollOffset / m_channelLayout->Size(m_orientation)));
SetChannel(m_channelCursor);
ScrollToBlockOffset(
MathUtils::round_int(static_cast<double>(m_programmeScrollOffset / m_blockSize)));
ScrollToBlockOffset(MathUtils::round_int(m_programmeScrollOffset / m_blockSize));
SetBlock(m_blockCursor);
return EVENT_RESULT_HANDLED;
}
Expand All @@ -1278,10 +1275,9 @@ EVENT_RESULT CGUIEPGGridContainer::OnMouseEvent(const CPoint& point,
{
std::unique_lock<CCriticalSection> lock(m_critSection);

m_channelOffset = MathUtils::round_int(
static_cast<double>(m_channelScrollOffset / m_channelLayout->Size(m_orientation)));
m_blockOffset =
MathUtils::round_int(static_cast<double>(m_programmeScrollOffset / m_blockSize));
m_channelOffset =
MathUtils::round_int(m_channelScrollOffset / m_channelLayout->Size(m_orientation));
m_blockOffset = MathUtils::round_int(m_programmeScrollOffset / m_blockSize);
ValidateOffset();
}
return EVENT_RESULT_HANDLED;
Expand Down Expand Up @@ -2556,8 +2552,8 @@ void CGUIEPGGridContainer::HandleProgrammeGrid(bool bRender,
// increment our X position
posA2 += m_gridModel->GetGridItemWidth(
channel, block); // assumes focused & unfocused layouts have equal length
block += MathUtils::round_int(
static_cast<double>(m_gridModel->GetGridItemOriginWidth(channel, block) / m_blockSize));
block +=
MathUtils::round_int(m_gridModel->GetGridItemOriginWidth(channel, block) / m_blockSize);
}
}

Expand Down Expand Up @@ -2610,4 +2606,4 @@ void CGUIEPGGridContainer::AssignItemDepth(CGUIListItem* item, bool focused)
else if (item->GetLayout())
item->GetLayout()->AssignDepth();
}
}
}
2 changes: 1 addition & 1 deletion xbmc/pvr/guilib/PVRGUIActionsTimers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ bool CPVRGUIActionsTimers::SetRecordingOnChannel(const std::shared_ptr<CPVRChann
selector.AddAction(RECORD_NEXT_SHOW, nextTitle);

// be smart. if current show is almost over, preselect next show.
if (epgTag->ProgressPercentage() > 90.0f)
if (epgTag->ProgressPercentage() > 90.0)
ePreselect = RECORD_NEXT_SHOW;
}
}
Expand Down
23 changes: 11 additions & 12 deletions xbmc/pvr/guilib/guiinfo/PVRGUIInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
#include "settings/SettingsComponent.h"
#include "threads/SingleLock.h"
#include "threads/SystemClock.h"
#include "utils/MathUtils.h"
#include "utils/StringUtils.h"

#include <cmath>
Expand Down Expand Up @@ -1468,17 +1469,15 @@ bool CPVRGUIInfo::GetPVRInt(const CFileItem* item, const CGUIInfo& info, int& iV
iValue = GetTimeShiftSeekPercent();
return true;
case PVR_ACTUAL_STREAM_SIG_PROGR:
iValue =
static_cast<int>(std::lrintf(static_cast<float>(m_qualityInfo.Signal()) / 0xFFFF * 100));
iValue = MathUtils::round_int(static_cast<double>(m_qualityInfo.Signal()) / 0xFFFF * 100.0);
return true;
case PVR_ACTUAL_STREAM_SNR_PROGR:
iValue =
static_cast<int>(std::lrintf(static_cast<float>(m_qualityInfo.SNR()) / 0xFFFF * 100));
iValue = MathUtils::round_int(static_cast<double>(m_qualityInfo.SNR()) / 0xFFFF * 100.0);
return true;
case PVR_BACKEND_DISKSPACE_PROGR:
if (m_iBackendDiskTotal > 0)
iValue = static_cast<int>(
std::lrintf(static_cast<float>(m_iBackendDiskUsed) / m_iBackendDiskTotal * 100));
iValue = MathUtils::round_int(static_cast<double>(m_iBackendDiskUsed) /
m_iBackendDiskTotal * 100.0);
else
iValue = 0xFF;
return true;
Expand Down Expand Up @@ -2206,14 +2205,14 @@ int CPVRGUIInfo::GetTimeShiftSeekPercent() const
{
int total = m_timesInfo.GetTimeshiftProgressDuration();

float totalTime = static_cast<float>(total);
if (totalTime == 0.0f)
const double totalTime = static_cast<double>(total);
if (totalTime == 0.0)
return 0;

float percentPerSecond = 100.0f / totalTime;
float percent = progress + percentPerSecond * seekSize;
percent = std::max(0.0f, std::min(percent, 100.0f));
return static_cast<int>(std::lrintf(percent));
const double percentPerSecond = 100.0 / totalTime;
double percent = progress + percentPerSecond * seekSize;
percent = std::max(0.0, std::min(percent, 100.0));
return MathUtils::round_int(percent);
}
return progress;
}
57 changes: 39 additions & 18 deletions xbmc/pvr/guilib/guiinfo/PVRGUITimesInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "pvr/recordings/PVRRecording.h"
#include "settings/AdvancedSettings.h"
#include "settings/SettingsComponent.h"
#include "utils/MathUtils.h"
#include "utils/StringUtils.h"

#include <chrono>
Expand Down Expand Up @@ -360,9 +361,13 @@ int CPVRGUITimesInfo::GetRemainingTime(const std::shared_ptr<const CPVREpgInfoTa
int CPVRGUITimesInfo::GetTimeshiftProgress() const
{
std::unique_lock<CCriticalSection> lock(m_critSection);
const std::chrono::duration<float> current{m_iTimeshiftPlayTime - m_iTimeshiftStartTime};
const std::chrono::duration<float> total{m_iTimeshiftEndTime - m_iTimeshiftStartTime};
return static_cast<int>(std::lrintf(current.count() / total.count() * 100));
const std::chrono::duration<double> total{m_iTimeshiftEndTime - m_iTimeshiftStartTime};
if (total.count())
{
const std::chrono::duration<double> current{m_iTimeshiftPlayTime - m_iTimeshiftStartTime};
return MathUtils::round_int(current.count() / total.count() * 100.0);
}
return 0;
}

int CPVRGUITimesInfo::GetTimeshiftProgressDuration() const
Expand All @@ -374,49 +379,63 @@ int CPVRGUITimesInfo::GetTimeshiftProgressDuration() const
int CPVRGUITimesInfo::GetTimeshiftProgressPlayPosition() const
{
std::unique_lock<CCriticalSection> lock(m_critSection);
const std::chrono::duration<float> duration{m_iTimeshiftPlayTime - m_iTimeshiftProgressStartTime};
return static_cast<int>(std::lrintf(duration.count() / m_iTimeshiftProgressDuration * 100));
if (m_iTimeshiftProgressDuration)
{
const std::chrono::duration<double> duration{m_iTimeshiftPlayTime -
m_iTimeshiftProgressStartTime};
return MathUtils::round_int(duration.count() / m_iTimeshiftProgressDuration * 100.0);
}
return 0;
}

int CPVRGUITimesInfo::GetTimeshiftProgressEpgStart() const
{
std::unique_lock<CCriticalSection> lock(m_critSection);
if (m_playingEpgTag)
if (m_playingEpgTag && m_iTimeshiftProgressDuration)
{
time_t epgStart = 0;
m_playingEpgTag->StartAsUTC().GetAsTime(epgStart);
const std::chrono::duration<float> duration{epgStart - m_iTimeshiftProgressStartTime};
return static_cast<int>(std::lrintf(duration.count() / m_iTimeshiftProgressDuration * 100));
const std::chrono::duration<double> duration{epgStart - m_iTimeshiftProgressStartTime};
return MathUtils::round_int(duration.count() / m_iTimeshiftProgressDuration * 100.0);
}
return 0;
}

int CPVRGUITimesInfo::GetTimeshiftProgressEpgEnd() const
{
std::unique_lock<CCriticalSection> lock(m_critSection);
if (m_playingEpgTag)
if (m_playingEpgTag && m_iTimeshiftProgressDuration)
{
time_t epgEnd = 0;
m_playingEpgTag->EndAsUTC().GetAsTime(epgEnd);
const std::chrono::duration<float> duration{epgEnd - m_iTimeshiftProgressStartTime};
return static_cast<int>(std::lrintf(duration.count() / m_iTimeshiftProgressDuration * 100));
const std::chrono::duration<double> duration{epgEnd - m_iTimeshiftProgressStartTime};
return MathUtils::round_int(duration.count() / m_iTimeshiftProgressDuration * 100.0);
}
return 0;
}

int CPVRGUITimesInfo::GetTimeshiftProgressBufferStart() const
{
std::unique_lock<CCriticalSection> lock(m_critSection);
const std::chrono::duration<float> duration{m_iTimeshiftStartTime -
m_iTimeshiftProgressStartTime};
return static_cast<int>(std::lrintf(duration.count() / m_iTimeshiftProgressDuration * 100));
if (m_iTimeshiftProgressDuration)
{
const std::chrono::duration<double> duration{m_iTimeshiftStartTime -
m_iTimeshiftProgressStartTime};
return MathUtils::round_int(duration.count() / m_iTimeshiftProgressDuration * 100.0);
}
return 0;
}

int CPVRGUITimesInfo::GetTimeshiftProgressBufferEnd() const
{
std::unique_lock<CCriticalSection> lock(m_critSection);
const std::chrono::duration<float> duration{m_iTimeshiftEndTime - m_iTimeshiftProgressStartTime};
return static_cast<int>(std::lrintf(duration.count() / m_iTimeshiftProgressDuration * 100));
if (m_iTimeshiftProgressDuration)
{
const std::chrono::duration<double> duration{m_iTimeshiftEndTime -
m_iTimeshiftProgressStartTime};
return MathUtils::round_int(duration.count() / m_iTimeshiftProgressDuration * 100.0);
}
return 0;
}

int CPVRGUITimesInfo::GetEpgEventDuration(const std::shared_ptr<const CPVREpgInfoTag>& epgTag) const
Expand All @@ -432,9 +451,11 @@ int CPVRGUITimesInfo::GetEpgEventProgress(const std::shared_ptr<const CPVREpgInf
{
std::unique_lock<CCriticalSection> lock(m_critSection);
if (epgTag && m_playingEpgTag && *epgTag != *m_playingEpgTag)
return static_cast<int>(std::lrintf(epgTag->ProgressPercentage()));
return MathUtils::round_int(epgTag->ProgressPercentage());
else if (m_iDuration)
return MathUtils::round_int(static_cast<double>(GetElapsedTime()) / m_iDuration * 100.0);
else
return static_cast<int>(std::lrintf(static_cast<float>(GetElapsedTime()) / m_iDuration * 100));
return 0;
}

bool CPVRGUITimesInfo::IsTimeshifting() const
Expand Down
7 changes: 4 additions & 3 deletions xbmc/pvr/recordings/PVRRecording.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "pvr/timers/PVRTimers.h"
#include "settings/AdvancedSettings.h"
#include "settings/SettingsComponent.h"
#include "utils/MathUtils.h"
#include "utils/StringUtils.h"
#include "utils/Variant.h"
#include "utils/log.h"
Expand Down Expand Up @@ -336,7 +337,7 @@ bool CPVRRecording::SetResumePoint(const CBookmark& resumePoint)
if (client && client->GetClientCapabilities().SupportsRecordingsLastPlayedPosition())
{
if (client->SetRecordingLastPlayedPosition(
*this, static_cast<int>(std::lrint(resumePoint.timeInSeconds))) != PVR_ERROR_NO_ERROR)
*this, MathUtils::round_int(resumePoint.timeInSeconds)) != PVR_ERROR_NO_ERROR)
return false;
}

Expand All @@ -350,8 +351,8 @@ bool CPVRRecording::SetResumePoint(double timeInSeconds,
const std::shared_ptr<CPVRClient> client = CServiceBroker::GetPVRManager().GetClient(m_iClientId);
if (client && client->GetClientCapabilities().SupportsRecordingsLastPlayedPosition())
{
if (client->SetRecordingLastPlayedPosition(
*this, static_cast<int>(std::lrint(timeInSeconds))) != PVR_ERROR_NO_ERROR)
if (client->SetRecordingLastPlayedPosition(*this, MathUtils::round_int(timeInSeconds)) !=
PVR_ERROR_NO_ERROR)
return false;
}

Expand Down
8 changes: 8 additions & 0 deletions xbmc/utils/MathUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,14 @@ namespace MathUtils
#endif
}

/*! \brief Round to nearest integer.
\sa truncate_int, test
*/
inline int round_int(float x)
{
return round_int(static_cast<double>(x));
}

/*! \brief Truncate to nearest integer.
This routine does fast truncation to an integer.
It should simply drop the fractional portion of the floating point number.
Expand Down

0 comments on commit 5877ee3

Please sign in to comment.