---
# System prepended metadata

title: Damage rect in viz
tags: [Chromium-Graphics, elkurin-daily-note]

---

# Damage rect in viz

Damage rect calculation is mostly done inside viz.  
Other layers such as cc, exo, wayland are just passing and aggregating those data.  
Let's track damage rect settings in viz.

## Presenter
[SchedulOverlayPlane](https://source.chromium.org/chromium/chromium/src/+/main:ui/gl/presenter.h;l=101;drc=8e78783dc1f7007bad46d657c9f332614e240fd8) presents a plane with data including damage rect.  
For lacros, [GbmSurfacelessWayland::ScheduleOverlayPlane](https://source.chromium.org/chromium/chromium/src/+/main:ui/ozone/platform/wayland/gpu/gbm_surfaceless_wayland.cc;l=134;drc=8e78783dc1f7007bad46d657c9f332614e240fd8) implements it.  
The arguments are OverlayImage, GpuFence and OverlayPlaneData.  
OverlayPlaneData includes many data and damage_rect is one of them.

It is called in [ScheduleOverlayPlane](https://source.chromium.org/chromium/chromium/src/+/main:components/viz/service/display_embedder/output_presenter_gl.cc;l=313;drc=8e78783dc1f7007bad46d657c9f332614e240fd8).  
`ToEnclosingRect(overlay_plane_candidate.damage_rect)` is set as a damage.

This `damage_rect` value is inside `overlay_plane_candidate` which is passed from [SchedulOverlays](https://source.chromium.org/chromium/chromium/src/+/main:components/viz/service/display_embedder/skia_output_device_buffer_queue.cc;l=453;drc=8e78783dc1f7007bad46d657c9f332614e240fd8) as OverlayList  
<- [SkiaOutputSurfaceImplOnGpu::PostSubmit](https://source.chromium.org/chromium/chromium/src/+/main:components/viz/service/display_embedder/skia_output_surface_impl_on_gpu.cc;l=2409;drc=8e78783dc1f7007bad46d657c9f332614e240fd8) as `oevrlay_`  
<- Appended from [ScheduleOverlays](https://source.chromium.org/chromium/chromium/src/+/main:components/viz/service/display_embedder/skia_output_surface_impl_on_gpu.cc;l=1879;drc=8e78783dc1f7007bad46d657c9f332614e240fd8) 
<- [SkiaRenderer::ScheduleOverlays](https://source.chromium.org/chromium/chromium/src/+/main:components/viz/service/display/skia_renderer.cc;l=2894-2895;drc=8e78783dc1f7007bad46d657c9f332614e240fd8).  
<- Overlay list is inserted in [SkiaRenderer::FinishDrawingFrame](https://source.chromium.org/chromium/chromium/src/+/main:components/viz/service/display/skia_renderer.cc;l=1058;drc=8e78783dc1f7007bad46d657c9f332614e240fd8) as constructed [`surface_candidate`](https://source.chromium.org/chromium/chromium/src/+/main:components/viz/service/display/skia_renderer.cc;l=1030-1057;drc=8e78783dc1f7007bad46d657c9f332614e240fd8).

[`damage_rect`](https://source.chromium.org/chromium/chromium/src/+/main:components/viz/service/display/skia_renderer.cc;l=1055-1057;drc=8e78783dc1f7007bad46d657c9f332614e240fd8) in `surface_candidate` as following:
```cpp=
surface_candidate.damage_rect =
    use_partial_swap_ ? gfx::RectF(swap_buffer_rect_)
                      : gfx::RectF(surface_plane.resource_size);
```
When partial swap is enabled, it uses `swap_buffer_rect_` as damage and it is set by `swap_buffer_rect_ = current_frame()->root_damage_rect`.  
`root_damage_rect` is written in [DirectRenderer::DrawFrame](https://source.chromium.org/chromium/chromium/src/+/main:components/viz/service/display/direct_renderer.cc;l=217;drc=8e78783dc1f7007bad46d657c9f332614e240fd8).  
Initially, root_render_pass damage rect is set to root_damage_rect.  
Then it takes the union against GetAndResetOvelayDamage, `ink_renderer->GetDamageRect()`.  
And then it takes the intersection against `device_viewport_size` [here](https://source.chromium.org/chromium/chromium/src/+/main:components/viz/service/display/direct_renderer.cc;l=253;drc=8e78783dc1f7007bad46d657c9f332614e240fd8).  

[`needs_full_frame_redraw`](https://source.chromium.org/chromium/chromium/src/+/main:components/viz/service/display/direct_renderer.cc;l=346;drc=8e78783dc1f7007bad46d657c9f332614e240fd8) is a flag that marks the frame as full damage. The similar concept exists in exo, cc, wayland... and here it's in DirectRenderer.  
On Lacros platform, it is enabled only on pixel testing with the default settings.  
If delegated compositing or partial swap is disabled, it requires full frame redraw.
If `needs_full_frame_redraw`, it [sets to device_viewport_size](https://source.chromium.org/chromium/chromium/src/+/main:components/viz/service/display/direct_renderer.cc;l=407-408;drc=8e78783dc1f7007bad46d657c9f332614e240fd8).

## Damage in RenderPass
RenderPass has [`damage_rect`](https://source.chromium.org/chromium/chromium/src/+/main:components/viz/common/quads/render_pass_internal.h;l=44;drc=8e78783dc1f7007bad46d657c9f332614e240fd8) value.  
This is set to [`root_damage_rect`](https://source.chromium.org/chromium/chromium/src/+/main:components/viz/service/display/direct_renderer.cc;l=233;drc=8e78783dc1f7007bad46d657c9f332614e240fd8) of the frame in DrawFrame.  

There are multiple code path that sets damage_rect to RenderPass.  
[Display::DrawSwap](https://source.chromium.org/chromium/chromium/src/+/main:components/viz/service/display/display.cc;l=899;drc=8e78783dc1f7007bad46d657c9f332614e240fd8) sets `output_rect` to `damage_rect` and this is also pushed back to `surface_damage_rect_list_` in frame.  

[LayerTreeImpl::ProcessDamageForRenderPass](https://source.chromium.org/chromium/chromium/src/+/main:cc/slim/layer_tree_impl.cc;l=966;drc=8e78783dc1f7007bad46d657c9f332614e240fd8) sets computed `damage` to `damage_rect`.  
This `damage` is a union of `visible_rect` for previous data and new layer data and then take intersection against `render_pass.output_rect`.  

Also, they are added from Quad::SetAll for CompositorRenderPass and AggregatedRenderPass.  

## Note: DamageTracker in cc
There is a class called [DamageTracker](https://source.chromium.org/chromium/chromium/src/+/main:cc/trees/damage_tracker.h;l=34;drc=c97609b6bb0b545340877ae17d6bdce82c8865bd) which exists in cc.   
It provides API to [AccumulateDamageFromLayer](https://source.chromium.org/chromium/chromium/src/+/main:cc/trees/damage_tracker.cc;l=147-148;drc=8e78783dc1f7007bad46d657c9f332614e240fd8). and [ComputeSurfaceDamage](https://source.chromium.org/chromium/chromium/src/+/main:cc/trees/damage_tracker.cc;l=185;drc=8e78783dc1f7007bad46d657c9f332614e240fd8).  
These APIs and the class might be worth looking into.