Skip to content

Commit

Permalink
Replace some uses of TimeDelta::operator/() with equivalent forms.
Browse files Browse the repository at this point in the history
Whether operator/() does integer or floating-point division is not
necessarily obvious to the reader (and will hopefully be changing in the
future).  Where it's at least as simple to get the same result without
using it, do so.

In many cases the code was effectively snapping to a multiple of some
time, which can be done in a shorter form using operator%().

Bug: 1104532
Change-Id: Id6614bfa91636bcf14568bb05212354b5a68c3e0
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2296859
Commit-Queue: Peter Kasting <pkasting@chromium.org>
Reviewed-by: Dale Curtis <dalecurtis@chromium.org>
Reviewed-by: Timothy Dresser <tdresser@chromium.org>
Reviewed-by: mark a. foltz <mfoltz@chromium.org>
Reviewed-by: Yuwei Huang <yuweih@chromium.org>
Reviewed-by: Toni Baržić <tbarzic@chromium.org>
Reviewed-by: Kevin Marshall <kmarshall@chromium.org>
Cr-Commit-Position: refs/heads/master@{#788791}
  • Loading branch information
pkasting authored and Commit Bot committed Jul 15, 2020
1 parent 221cc44 commit 4766ddd
Show file tree
Hide file tree
Showing 12 changed files with 18 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
#include "base/json/json_reader.h"
#include "base/lazy_instance.h"
#include "base/no_destructor.h"
#include "base/numerics/safe_conversions.h"
#include "base/optional.h"
#include "base/run_loop.h"
#include "base/scoped_observer.h"
Expand Down Expand Up @@ -4035,10 +4036,10 @@ ExtensionFunction::ResponseAction AutotestPrivateMouseMoveFunction::Run() {
wm::ConvertPointFromScreen(root_window, &start_in_host);
ConvertPointToHost(root_window, &start_in_host);

int64_t steps =
std::max(base::TimeDelta::FromMilliseconds(params->duration_in_ms) /
event_generator_->interval(),
static_cast<int64_t>(1));
int64_t steps = std::max(
base::Floor<int64_t>(params->duration_in_ms /
event_generator_->interval().InMillisecondsF()),
static_cast<int64_t>(1));
int flags = env->mouse_button_flags();
ui::EventType type = (flags == 0) ? ui::ET_MOUSE_MOVED : ui::ET_MOUSE_DRAGGED;
for (int64_t i = 1; i <= steps; ++i) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "chromecast/media/cma/backend/fuchsia/mixer_output_stream_fuchsia.h"

#include "base/location.h"
#include "base/numerics/safe_conversions.h"
#include "base/single_thread_task_runner.h"
#include "base/test/task_environment.h"
#include "base/threading/thread_task_runner_handle.h"
Expand Down Expand Up @@ -38,7 +39,7 @@ TEST_F(MixerOutputStreamFuchsiaTest, Play1s) {
auto started = base::TimeTicks::Now();

int samples_to_play =
kSampleRate * kTestStreamDuration / base::TimeDelta::FromSeconds(1);
base::Floor(kSampleRate * kTestStreamDuration.InSecondsF());
int pos = 0;
while (pos < samples_to_play) {
std::vector<float> buffer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -444,8 +444,7 @@ class FrameSinkVideoCapturerTest : public testing::Test {

base::TimeTicks GetNextVsync() const {
const auto now = task_runner_->NowTicks();
const auto num_vsyncs_elapsed = (now - start_time_) / kVsyncInterval;
return start_time_ + (num_vsyncs_elapsed + 1) * kVsyncInterval;
return now + kVsyncInterval - ((now - start_time_) % kVsyncInterval);
}

void AdvanceClockToNextVsync() {
Expand Down
7 changes: 1 addition & 6 deletions content/browser/scheduler/responsiveness/calculator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -212,13 +212,8 @@ void Calculator::CalculateResponsivenessIfNecessary(
// At least |kMeasurementInterval| time has passed, so we want to move forward
// |last_calculation_time_| and make measurements based on janks in that
// interval.
int64_t number_of_measurement_intervals =
time_since_last_calculation / kMeasurementInterval;
DCHECK_GE(number_of_measurement_intervals, 1);

const base::TimeTicks new_calculation_time =
last_calculation_time_ +
number_of_measurement_intervals * kMeasurementInterval;
current_time - (time_since_last_calculation % kMeasurementInterval);

// Acquire the janks in the measurement interval from the UI and IO threads.
std::vector<JankList> execution_janks_from_multiple_threads;
Expand Down
2 changes: 1 addition & 1 deletion media/base/null_video_sink.cc
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ void NullVideoSink::CallRender() {
// If we're behind, find the next nearest on time interval.
delay = current_render_time_ - now;
if (delay < base::TimeDelta())
delay += interval_ * (-delay / interval_ + 1);
delay = interval_ + (delay % interval_);
current_render_time_ = now + delay;
last_now_ = now;
}
Expand Down
3 changes: 1 addition & 2 deletions media/cast/sender/congestion_control.cc
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,7 @@ void AdaptiveCongestionControl::UpdateRtt(base::TimeDelta rtt) {
void AdaptiveCongestionControl::UpdateTargetPlayoutDelay(
base::TimeDelta delay) {
const int max_unacked_frames = std::min<int>(
kMaxUnackedFrames, 1 + static_cast<int>(delay * max_frame_rate_ /
base::TimeDelta::FromSeconds(1)));
kMaxUnackedFrames, 1 + (delay * max_frame_rate_).InSeconds());
DCHECK_GT(max_unacked_frames, 0);
history_size_ = max_unacked_frames + kHistorySize;
PruneFrameStats();
Expand Down
3 changes: 1 addition & 2 deletions media/cast/test/utility/audio_utility.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ TestAudioBusFactory::~TestAudioBusFactory() = default;

std::unique_ptr<AudioBus> TestAudioBusFactory::NextAudioBus(
const base::TimeDelta& duration) {
const int num_samples = static_cast<int>((sample_rate_ * duration) /
base::TimeDelta::FromSeconds(1));
const int num_samples = (sample_rate_ * duration).InSeconds();
std::unique_ptr<AudioBus> bus(AudioBus::Create(num_channels_, num_samples));
source_.OnMoreData(base::TimeDelta(), base::TimeTicks::Now(), 0, bus.get());
bus->Scale(volume_);
Expand Down
6 changes: 2 additions & 4 deletions media/gpu/test/video_player/frame_renderer_dummy.cc
Original file line number Diff line number Diff line change
Expand Up @@ -213,10 +213,8 @@ void FrameRendererDummy::ScheduleNextRenderFrameTask() {
// Vsync. This might result in one or more frames getting dropped.
next_render_time =
std::max(now + vsync_interval_duration_, next_frame_time_);
int64_t num_intervals_timebase =
(next_render_time - vsync_timebase_) / vsync_interval_duration_;
next_render_time =
vsync_timebase_ + (num_intervals_timebase * vsync_interval_duration_);
next_render_time -=
(next_render_time - vsync_timebase_) % vsync_interval_duration_;
}

// The time at which the next frame will be rendered might be later then the
Expand Down
3 changes: 1 addition & 2 deletions remoting/protocol/webrtc_audio_source_adapter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,7 @@ void WebrtcAudioSourceAdapter::Core::OnAudioPacket(
partial_frame_.clear();
}

size_t samples_per_frame =
kAudioFrameDuration * sampling_rate_ / base::TimeDelta::FromSeconds(1);
size_t samples_per_frame = (kAudioFrameDuration * sampling_rate_).InSeconds();
size_t bytes_per_frame = kBytesPerSample * kChannels * samples_per_frame;

const std::string& data = packet->data(0);
Expand Down
2 changes: 1 addition & 1 deletion remoting/protocol/webrtc_audio_source_adapter_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class FakeAudioSink : public webrtc::AudioTrackSinkInterface{
EXPECT_EQ(kSampleRate, sample_rate);
EXPECT_EQ(kBytesPerSample * 8, bits_per_sample);
EXPECT_EQ(kChannels, static_cast<int>(number_of_channels));
EXPECT_EQ(kSampleRate * kFrameDuration / base::TimeDelta::FromSeconds(1),
EXPECT_EQ((kSampleRate * kFrameDuration).InSeconds(),
static_cast<int>(number_of_samples));
const int16_t* samples = reinterpret_cast<const int16_t*>(audio_data);
samples_.insert(samples_.end(), samples,
Expand Down
3 changes: 1 addition & 2 deletions remoting/test/codec_perftest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,7 @@ TEST_P(CodecPerfTest, MaxFramerate) {
clock_.Advance(kIntervalBetweenFrames);
}

VLOG(0) << "Max framerate: "
<< (kTotalFrames * base::TimeDelta::FromSeconds(1) / total_latency);
VLOG(0) << "Max framerate: " << kTotalFrames / total_latency.InSecondsF();
}

} // namespace test
Expand Down
3 changes: 1 addition & 2 deletions services/audio/loopback_stream_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,7 @@ class LoopbackStreamTest : public testing::Test {
sources_.emplace_back(std::make_unique<FakeLoopbackGroupMember>(
media::AudioParameters(media::AudioParameters::AUDIO_PCM_LOW_LATENCY,
media::GuessChannelLayout(channels), sample_rate,
(sample_rate * kBufferDuration) /
base::TimeDelta::FromSeconds(1))));
(sample_rate * kBufferDuration).InSeconds())));
coordinator_.RegisterMember(group_id_, sources_.back().get());
return sources_.back().get();
}
Expand Down

0 comments on commit 4766ddd

Please sign in to comment.