# misc/graphicsqualitygovernor (vanilla-engine 0.4.0)

> https://vanillapunch.com/docs/vanilla-engine/0.4.0/api/misc/graphicsqualitygovernor/

Graphics quality decision. (GraphicsQualityGovernor of vanilla.js misc/graphicsqualitygovernor.js)
- Decides whether to use the lite path from settings and frame measurement, and picks the render pixel ratio limit.
- Order of deciding the lite path (is_lite): override (like ?light in the address) → setting low (always lite), high (never) → auto.
  In auto, it is the lite path if lite from the start (set_auto_lite_default, such as touch devices) or when FrameProbe measured a slow device.
- Calling update(frame seconds, max frames) every frame measures with FrameProbe only in auto. Returns true on the frame the lite path changed.
- Render pixel ratio limit: give it with set_render_pixel_ratio_limits(normal, lite path), and apply_render_pixel_ratio(view_manager) applies it only when it changes. (0 means no limit)
- What to reduce on the lite path (shadows, outlines, effects, draw counts) is decided by the game reading is_lite.
- Address: apply_search_text takes query text ("?light", "?a=1&light=0"). Reads it like the original URLSearchParams.
  (strips one leading ?, splits by &, skips empty pieces, splits the name and value at the first =, + is a space, %XX is decoded as bytes and read as UTF-8. If the same name appears several times, the first value)
  apply_page_query reads the query of the page address on the web. Desktop has no page address, so it reads the environment variable `VANILLA_<name>` (upper snake case, light → `VANILLA_LIGHT`).

## GraphicsQualityGovernor (struct) (Default)

Graphics quality governor.
- Decides whether to use the light path in the order override, quality setting, auto (frame measurement), and applies the render pixel ratio limit for each path.

Example: Automatically take the light path when slow frames continue

```rust
use vanilla::GraphicsQualityGovernor;

let mut governor = GraphicsQualityGovernor::new();
governor.set_render_pixel_ratio_limits(0.0, 1.5);
governor.get_frame_probe_mut().set_seconds(0.0, 1.0);
let mut changed_count = 0;
for _ in 0..40 {
	if governor.update(0.05, 60.0) {
		changed_count += 1;
	}
}
assert!(governor.is_lite());
assert_eq!(changed_count, 1);
assert_eq!(governor.read_max_render_pixel_ratio(), 1.5);
```

- `pub fn new() -> GraphicsQualityGovernor` (constructor, since 0.4.0): Constructor. (Auto, no override, no render pixel ratio limit)
  - returns GraphicsQualityGovernor: A new quality governor not on the light path. (FrameProbe at defaults)
- `pub fn set_quality(&mut self, quality: GraphicsQuality)` (method, since 0.4.0): Quality setting.
  - `quality` GraphicsQuality: Quality setting value. (an override takes precedence if present)
- `pub fn get_quality(&self) -> GraphicsQuality` (method, since 0.4.0): Returns the quality setting.
  - returns GraphicsQuality: Current quality setting value.
- `pub fn set_lite_override(&mut self, lite_override: Option<bool>)` (method, since 0.4.0): Sets the override. (Some(true) always light, Some(false) never, None clears it)
  - `lite_override` Option<bool>: Override value.
- `pub fn get_lite_override(&self) -> Option<bool>` (method, since 0.4.0): Returns the override.
  - returns Option<bool>: Current override value. None if none.
- `pub fn apply_search_text(&mut self, search_text: &str, lite_key: &str)` (method, since 0.4.0): Overrides from query text. (light if lite_key is present, never if its value is "0" or "false", unchanged if absent)
  - `search_text` &str: Query text. (like "?light", "?a=1&light=0", the leading ? is optional)
  - `lite_key` &str: Name to look for. (compared with the decoded name, the first value if it appears more than once)

  Example: Override with light in the address

  ```rust
  use vanilla::GraphicsQualityGovernor;
  
  let mut governor = GraphicsQualityGovernor::new();
  governor.apply_search_text("?a=1&light", "light");
  assert_eq!(governor.get_lite_override(), Some(true));
  governor.apply_search_text("?light=0", "light");
  assert_eq!(governor.get_lite_override(), Some(false));
  governor.apply_search_text("?other=1", "light");
  assert_eq!(governor.get_lite_override(), Some(false));
  ```

- `pub fn apply_page_query(&mut self, lite_key: &str)` (method, since 0.4.0): Overrides with the query of the page address. (reads the address query on the web, the environment variable `VANILLA_<name>` on desktop. The lite path if the value is not 0 or false)
  - `lite_key` &str: Query name. (read from the page address query on the web, and on desktop from the environment variable VANILLA_ followed by this name in upper snake case. platform::read_page_query_value)
- `pub fn set_auto_lite_default(&mut self, is_auto_lite_default: bool)` (method, since 0.4.0): Sets whether auto starts on the lite path. (When going lite even before measuring, like touch devices)
  - `is_auto_lite_default` bool: If true, auto is light without measuring frames.
- `pub fn is_auto_lite_default(&self) -> bool` (method, since 0.4.0): Returns whether auto starts on the lite path.
  - returns bool: Value set with set_auto_lite_default. (default false)
- `pub fn get_frame_probe(&self) -> &FrameProbe` (method, since 0.4.0): Returns the frame probe.
  - returns &FrameProbe: Reference to the frame probe used in auto.
- `pub fn get_frame_probe_mut(&mut self) -> &mut FrameProbe` (method, since 0.4.0): Returns the frame probe. (to change the wait seconds or decision thresholds, or to measure_again in a heavy scene)
  - returns &mut FrameProbe: Mutable reference to the frame probe used in auto.
- `pub fn is_lite(&self) -> bool` (method, since 0.4.0): Whether drawing on the lite path.
  - returns bool: The override value if any. Otherwise true for low, false for high, and for auto, true if light from the start or FrameProbe measured a slow device.
- `pub fn update(&mut self, frame_seconds: f64, maximum_frame_per_second: f64) -> bool` (method, since 0.4.0): One frame. (measures frames if auto and not yet light, true on the frame the light path changes)
  - Measures with FrameProbe only when there is no override, it is auto, and it is not light from the start.
  - `frame_seconds` f64: This frame's interval. (seconds)
  - `maximum_frame_per_second` f64: Maximum frame rate. (0 or less is treated as FrameProbe's unlimited target frame rate)
  - returns bool: true if is_lite differs from the last update. (changes from the setting or override are also true on the next update)
- `pub fn set_render_pixel_ratio_limits(&mut self, normal_max_render_pixel_ratio: f64, lite_max_render_pixel_ratio: f64)` (method, since 0.4.0): Sets the render pixel ratio cap. (Normal path and lite path, 0 means no limit)
  - `normal_max_render_pixel_ratio` f64: Render pixel ratio limit for the normal path. (negative is 0)
  - `lite_max_render_pixel_ratio` f64: Render pixel ratio limit for the light path. (negative is 0)
- `pub fn read_max_render_pixel_ratio(&self) -> f64` (method, since 0.4.0): Render pixel ratio cap of the current path.
  - returns f64: The light path limit if is_lite, otherwise the normal path limit. (0 means no limit)
- `pub fn apply_render_pixel_ratio(&self, view_manager: &mut ViewManager) -> bool` (method, since 0.4.0): Applies the render pixel ratio cap. (Only when it differs from the ViewManager value, true if applied)
  - `view_manager` &mut ViewManager: View manager to apply the limit to.
  - returns bool: true if newly applied because the value differed. false if already the same.

  Example: Apply only when it changes

  ```rust
  use vanilla::{GraphicsQualityGovernor, Vector2, ViewManager, ViewScaleMode};
  
  let mut governor = GraphicsQualityGovernor::new();
  governor.set_render_pixel_ratio_limits(2.0, 1.0);
  let mut view_manager = ViewManager::new(Vector2::create(1280.0, 720.0), ViewScaleMode::StretchShort);
  assert!(governor.apply_render_pixel_ratio(&mut view_manager));
  assert_eq!(view_manager.get_max_render_pixel_ratio(), 2.0);
  assert!(!governor.apply_render_pixel_ratio(&mut view_manager));
  ```

## GraphicsQuality (enum) (Clone, Copy, Debug, Default, PartialEq, Eq)

Graphics quality setting value. (original GraphicsQuality.auto, high, low)

- `Auto`: Auto. (light if light from the start or when FrameProbe measured a slow device, the default)
- `High`: High. (never the lite path)
- `Low`: Low. (always the lite path)
