Skip to content

Commit ae2477d

Browse files
committed
Stop using time@0.1
1 parent 5a9bfec commit ae2477d

14 files changed

+141
-16
lines changed

webrender/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ rayon = "1"
4343
ron = { optional = true, version = "0.10.0" }
4444
serde = { optional = true, version = "1.0", features = ["serde_derive"] }
4545
smallvec = "1"
46-
time = "0.1"
4746
api = { version = "0.66.0", path = "../webrender_api", package = "webrender_api" }
4847
webrender_build = { version = "0.0.2", path = "../webrender_build" }
4948
malloc_size_of = { version = "0.2.0", path = "../wr_malloc_size_of", package = "wr_malloc_size_of" }

webrender/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,6 @@ extern crate rayon;
156156
extern crate ron;
157157
#[macro_use]
158158
extern crate smallvec;
159-
extern crate time;
160159
#[cfg(all(feature = "capture", feature = "png"))]
161160
extern crate png;
162161
#[cfg(test)]

webrender/src/prim_store/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1332,7 +1332,7 @@ impl PrimitiveScratchBuffer {
13321332
const LINE_HEIGHT: f32 = 20.0;
13331333
const X0: f32 = 32.0;
13341334
const Y0: f32 = 32.0;
1335-
let now = time::precise_time_ns();
1335+
let now = api::precise_time_ns();
13361336

13371337
let msgs_to_remove = self.messages.len().max(MSGS_TO_RETAIN) - MSGS_TO_RETAIN;
13381338
let mut msgs_removed = 0;
@@ -1438,7 +1438,7 @@ impl PrimitiveScratchBuffer {
14381438
) {
14391439
self.messages.push(DebugMessage {
14401440
msg,
1441-
timestamp: time::precise_time_ns(),
1441+
timestamp: api::precise_time_ns(),
14421442
})
14431443
}
14441444
}

webrender/src/profiler.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
//! - A '|' token to start a new column.
2020
//! - A '_' token to start a new row.
2121
22-
use api::{ColorF, ColorU};
22+
use api::{ColorF, ColorU, precise_time_ns};
2323
use glyph_rasterizer::profiler::GlyphRasterizeProfiler;
2424
use crate::renderer::DebugRenderer;
2525
use crate::device::query::GpuTimer;
@@ -32,7 +32,6 @@ use std::fmt::{Write, Debug};
3232
use std::f32;
3333
use std::ops::Range;
3434
use std::time::Duration;
35-
use time::precise_time_ns;
3635

3736
macro_rules! set_text {
3837
($dst:expr, $($arg:tt)*) => {

webrender/src/render_api.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44

55
#![deny(missing_docs)]
66

7+
use api::precise_time_ns;
78
use std::cell::Cell;
89
use std::fmt;
910
use std::marker::PhantomData;
1011
use std::path::PathBuf;
1112
use std::sync::Arc;
1213
use std::u32;
1314
use api::{HitTestFlags, MinimapData, SnapshotImageKey};
14-
use time::precise_time_ns;
1515
use crate::api::channel::{Sender, single_msg_channel, unbounded_channel};
1616
use crate::api::{BuiltDisplayList, IdNamespace, ExternalScrollId, Parameter, BoolParameter};
1717
use crate::api::{FontKey, FontInstanceKey, NativeFontHandle};

webrender/src/render_backend.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use api::{DocumentId, ExternalScrollId, HitTestResult};
1313
use api::{IdNamespace, PipelineId, RenderNotifier, SampledScrollOffset};
1414
use api::{NotificationRequest, Checkpoint, QualitySettings};
1515
use api::{FramePublishId, PrimitiveKeyKind, RenderReasons};
16+
use api::precise_time_ns;
1617
use api::units::*;
1718
use api::channel::{single_msg_channel, Sender, Receiver};
1819
use crate::bump_allocator::ChunkPool;
@@ -71,7 +72,6 @@ use std::{mem, u32};
7172
use std::path::PathBuf;
7273
#[cfg(feature = "replay")]
7374
use crate::frame_builder::Frame;
74-
use time::precise_time_ns;
7575
use core::time::Duration;
7676
use crate::util::{Recycler, VecHelper, drain_filter};
7777

webrender/src/renderer/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ use api::{PipelineId, ImageRendering, Checkpoint, NotificationRequest, ImageBuff
4343
#[cfg(feature = "replay")]
4444
use api::ExternalImage;
4545
use api::FramePublishId;
46+
use api::precise_time_ns;
4647
use api::units::*;
4748
use api::channel::{Sender, Receiver};
4849
pub use api::DebugFlags;
@@ -115,7 +116,6 @@ use std::{
115116
};
116117
#[cfg(any(feature = "capture", feature = "replay"))]
117118
use std::collections::hash_map::Entry;
118-
use time::precise_time_ns;
119119

120120
mod debug;
121121
mod gpu_buffer;

webrender/src/renderer/shade.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* License, v. 2.0. If a copy of the MPL was not distributed with this
33
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
44

5-
use api::{ImageBufferKind, units::DeviceSize};
5+
use api::{precise_time_ns, ImageBufferKind, units::DeviceSize};
66
use crate::batch::{BatchKey, BatchKind, BrushBatchKind, BatchFeatures};
77
use crate::composite::{CompositeFeatures, CompositeSurfaceFormat};
88
use crate::device::{Device, Program, ShaderError};
@@ -18,7 +18,6 @@ use crate::renderer::{
1818
use crate::profiler::{self, TransactionProfile, ns_to_ms};
1919

2020
use gleam::gl::GlType;
21-
use time::precise_time_ns;
2221

2322
use std::cell::RefCell;
2423
use std::collections::VecDeque;

webrender/src/renderer/upload.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616
//! here relies on submitting draw calls via the renderer.
1717
1818

19+
use api::precise_time_ns;
1920
use std::mem;
2021
use std::collections::VecDeque;
2122
use std::sync::Arc;
2223
use std::time::Duration;
2324
use euclid::{Transform3D, point2};
24-
use time::precise_time_ns;
2525
use malloc_size_of::MallocSizeOfOps;
2626
use api::units::*;
2727
use api::{ExternalImageSource, ImageBufferKind, ImageFormat};

webrender/src/scene_builder_thread.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use api::{DocumentId, PipelineId, ExternalEvent, BlobImageRequest};
77
use api::{NotificationRequest, Checkpoint, IdNamespace, QualitySettings};
88
use api::{PrimitiveKeyKind, GlyphDimensionRequest, GlyphIndexRequest};
99
use api::channel::{unbounded_channel, single_msg_channel, Receiver, Sender};
10+
use api::precise_time_ns;
1011
use api::units::*;
1112
use crate::render_api::{ApiMsg, FrameMsg, SceneMsg, ResourceUpdate, TransactionMsg, MemoryReport};
1213
use crate::box_shadow::BoxShadow;
@@ -35,7 +36,6 @@ use crate::spatial_tree::{SceneSpatialTree, SpatialTreeUpdates};
3536
use crate::telemetry::Telemetry;
3637
use crate::SceneBuilderHooks;
3738
use std::iter;
38-
use time::precise_time_ns;
3939
use crate::util::drain_filter;
4040
use std::thread;
4141
use std::time::Duration;

webrender_api/Cargo.toml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,16 @@ malloc_size_of_derive = "0.1"
2222
serde = { version = "1.0", features = ["rc"] }
2323
serde_derive = "1.0"
2424
serde_bytes = "0.11"
25-
time = "0.1"
2625
malloc_size_of = { version = "0.2.0", path = "../wr_malloc_size_of", package = "wr_malloc_size_of" }
2726
peek-poke = { version = "0.3", path = "../peek-poke", features = ["extras"] }
2827
crossbeam-channel = "0.5"
28+
29+
[target.'cfg(any(target_os = "macos", target_os = "ios"))'.dependencies]
30+
mach2 = { version = "0.4" }
31+
32+
[target.'cfg(all(unix, not(any(target_os = "macos", target_os = "ios"))))'.dependencies]
33+
libc = { version = "0.2" }
34+
35+
[target.'cfg(target_os = "windows")'.dependencies]
36+
windows-sys = { version = "0.59", features = ["Win32_System_Performance"] }
37+

webrender_api/src/display_list.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ use std::marker::PhantomData;
1515
use std::ops::Range;
1616
use std::mem;
1717
use std::collections::HashMap;
18-
use time::precise_time_ns;
1918
use malloc_size_of::{MallocSizeOf, MallocSizeOfOps};
2019
// local imports
2120
use crate::display_item as di;
@@ -25,6 +24,7 @@ use crate::gradient_builder::GradientBuilder;
2524
use crate::color::ColorF;
2625
use crate::font::{FontInstanceKey, GlyphInstance, GlyphOptions};
2726
use crate::image::{ColorDepth, ImageKey};
27+
use crate::precise_time_ns;
2828
use crate::units::*;
2929

3030

webrender_api/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ extern crate malloc_size_of_derive;
3636
extern crate serde;
3737
#[macro_use]
3838
extern crate serde_derive;
39-
extern crate time;
4039

4140
extern crate malloc_size_of;
4241
extern crate peek_poke;
@@ -49,6 +48,7 @@ mod display_list;
4948
mod font;
5049
mod gradient_builder;
5150
mod image;
51+
mod precise_time_ns;
5252
mod tile_pool;
5353
pub mod units;
5454

@@ -59,6 +59,7 @@ pub use crate::display_list::*;
5959
pub use crate::font::*;
6060
pub use crate::gradient_builder::*;
6161
pub use crate::image::*;
62+
pub use crate::precise_time_ns::precise_time_ns;
6263
pub use crate::tile_pool::*;
6364

6465
use crate::units::*;

webrender_api/src/precise_time_ns.rs

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
// Copyright 2024 The Servo Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
pub fn precise_time_ns() -> u64 {
12+
platform::now()
13+
}
14+
15+
#[cfg(all(unix, not(any(target_os = "macos", target_os = "ios"))))]
16+
mod platform {
17+
use libc::timespec;
18+
19+
#[allow(unsafe_code)]
20+
pub(super) fn now() -> u64 {
21+
// SAFETY: libc::timespec is zero initializable.
22+
let time = unsafe {
23+
let mut time: timespec = std::mem::zeroed();
24+
libc::clock_gettime(libc::CLOCK_MONOTONIC, &mut time);
25+
time
26+
};
27+
(time.tv_sec as u64) * 1000000000 + (time.tv_nsec as u64)
28+
}
29+
}
30+
31+
#[cfg(any(target_os = "macos", target_os = "ios"))]
32+
mod platform {
33+
use std::sync::LazyLock;
34+
35+
use mach2::mach_time::{mach_absolute_time, mach_timebase_info};
36+
37+
#[allow(unsafe_code)]
38+
fn timebase_info() -> &'static mach_timebase_info {
39+
static TIMEBASE_INFO: LazyLock<mach_timebase_info> = LazyLock::new(|| {
40+
let mut timebase_info = mach_timebase_info { numer: 0, denom: 0 };
41+
unsafe { mach_timebase_info(&mut timebase_info) };
42+
timebase_info
43+
});
44+
&TIMEBASE_INFO
45+
}
46+
47+
#[allow(unsafe_code)]
48+
pub(super) fn now() -> u64 {
49+
let timebase_info = timebase_info();
50+
let absolute_time = unsafe { mach_absolute_time() };
51+
absolute_time * timebase_info.numer as u64 / timebase_info.denom as u64
52+
}
53+
}
54+
55+
#[cfg(target_os = "windows")]
56+
mod platform {
57+
use std::sync::atomic::{AtomicU64, Ordering};
58+
59+
use windows_sys::Win32::System::Performance::{
60+
QueryPerformanceCounter, QueryPerformanceFrequency,
61+
};
62+
63+
/// The frequency of the value returned by `QueryPerformanceCounter` in counts per
64+
/// second. This is taken from the Rust source code at:
65+
/// <https://github.com/rust-lang/rust/blob/1a1cc050d8efc906ede39f444936ade1fdc9c6cb/library/std/src/sys/pal/windows/time.rs#L197>
66+
#[allow(unsafe_code)]
67+
fn frequency() -> i64 {
68+
// Either the cached result of `QueryPerformanceFrequency` or `0` for
69+
// uninitialized. Storing this as a single `AtomicU64` allows us to use
70+
// `Relaxed` operations, as we are only interested in the effects on a
71+
// single memory location.
72+
static FREQUENCY: AtomicU64 = AtomicU64::new(0);
73+
74+
let cached = FREQUENCY.load(Ordering::Relaxed);
75+
// If a previous thread has filled in this global state, use that.
76+
if cached != 0 {
77+
return cached as i64;
78+
}
79+
// ... otherwise learn for ourselves ...
80+
let mut frequency = 0;
81+
let result = unsafe { QueryPerformanceFrequency(&mut frequency) };
82+
83+
if result == 0 {
84+
return 0;
85+
}
86+
87+
FREQUENCY.store(frequency as u64, Ordering::Relaxed);
88+
frequency
89+
}
90+
91+
#[allow(unsafe_code)]
92+
/// Get the current instant value in nanoseconds.
93+
/// Originally from: <https://github.com/rust-lang/rust/blob/1a1cc050d8efc906ede39f444936ade1fdc9c6cb/library/std/src/sys/pal/windows/time.rs#L175>
94+
pub(super) fn now() -> u64 {
95+
let mut counter_value = 0;
96+
unsafe { QueryPerformanceCounter(&mut counter_value) };
97+
98+
/// Computes (value*numer)/denom without overflow, as long as both
99+
/// (numer*denom) and the overall result fit into i64 (which is the case
100+
/// for our time conversions).
101+
/// Originally from: <https://github.com/rust-lang/rust/blob/1a1cc050d8efc906ede39f444936ade1fdc9c6cb/library/std/src/sys_common/mod.rs#L75>
102+
fn mul_div_u64(value: u64, numer: u64, denom: u64) -> u64 {
103+
let q = value / denom;
104+
let r = value % denom;
105+
// Decompose value as (value/denom*denom + value%denom),
106+
// substitute into (value*numer)/denom and simplify.
107+
// r < denom, so (denom*numer) is the upper bound of (r*numer)
108+
q * numer + r * numer / denom
109+
}
110+
111+
static NANOSECONDS_PER_SECOND: u64 = 1_000_000_000;
112+
mul_div_u64(
113+
counter_value as u64,
114+
NANOSECONDS_PER_SECOND,
115+
frequency() as u64,
116+
)
117+
}
118+
}
119+

0 commit comments

Comments
 (0)