Skip to main content

Collector

Struct Collector 

Source
pub struct Collector { /* private fields */ }
Expand description

后端侧帧统计收集器:把“要不要测 / 怎么记账 / 不许伪造“集中在一处。

为什么抽出它:A 阶段落地的三个后端(X11、Web、Win32 软路径)各自手写了 6 个几乎相同的辅助方法(begin_frame / mark / record / dirty / mark_skipped / finish),共约 120 行重复代码。重复的不只是行数 —— “测不到就写 0” 这类规则一旦各写一遍,迟早有后端写错。

语义(与手写版本逐条相同):

  • 关闭统计时 begin_frame 只做一次 Option 赋值,不读时钟、不分配;
  • mark() 在关闭时返回 None,于是 record 什么都不做;
  • 每帧复用同一个 RenderStats(reset_for_next_frame),零堆分配;
  • take() 是取走语义:同一帧第二次调用必得 None,不会重复消费。

Implementations§

Source§

impl Collector

Source

pub fn new(backend: BackendKind) -> Self

建一个收集器(backend 决定能力表,进而决定 validate() 的判据)。

Source

pub fn begin_frame(&mut self)

每帧开始:统计开启时复用/新建,关闭时清空(不分配)。

Source

pub fn enabled(&self) -> bool

本帧是否要打点(关闭时调用方据此跳过一切计时)。

Source

pub fn mark(&self) -> Option<Instant>

打点起点;关闭时返回 None 且不读时钟。

Source

pub fn record_elapsed(&mut self, id: StageId, elapsed: Option<Duration>)

记录一段已完成的耗时(替代“先 mark 再 record“的场景,Web 用得上)。

Source

pub fn record(&mut self, id: StageId, start: Option<Instant>)

记录“从 start 到现在“的耗时(start == None 时不动)。

Source

pub fn set_dirty(&mut self, rects: u32, pixels: u64)

记录脏区计数(块数 + 覆盖像素数)。

Source

pub fn mark_skipped(&mut self)

跳过帧(没有渲染):统计对象照常交付,但标 frame_skipped。

Source

pub fn finish(&mut self)

帧末:把 CPU 后端确实没有的量显式写 0(不是“测得 0“)。

presented 不在这里写 —— 它归 Runtime 所有(B1 裁决)。 只在成功出口调用;失败帧应由调用方 discard(),不交付半截数据。

Source

pub fn stats_mut(&mut self) -> &mut Option<RenderStats>

借出底层 Option<RenderStats>(仅供批量写入与测试)。

为什么保留这个“后门“:record_upload 这类“一次写四段“的辅助函数与单元测试 需要直接构造/检查统计对象;走 Collector 的逐个 record_elapsed 会让测试 也必须先把环境变量打开(enabled() 是进程级 OnceLock,一开全进程都变), 反而更容易写出“依赖执行顺序“的测试。

Source

pub fn take(&mut self) -> Option<RenderStats>

取走本帧统计(取走语义:第二次调用必为 None)。

Source

pub fn discard(&mut self)

丢弃本帧统计(失败帧不交付)。

Trait Implementations§

Source§

impl Debug for Collector

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Forget for Collector

§

impl Freeze for Collector
where &'static [(StageId, &'static str)]: Freeze,

§

impl Move for Collector

§

impl RefUnwindSafe for Collector

§

impl Send for Collector
where &'static [(StageId, &'static str)]: Send,

§

impl Sync for Collector

§

impl Unpin for Collector
where &'static [(StageId, &'static str)]: Unpin,

§

impl UnsafeUnpin for Collector
where &'static [(StageId, &'static str)]: UnsafeUnpin,

§

impl UnwindSafe for Collector
where &'static [(StageId, &'static str)]: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.