# Damage in WaylandSurface
Damage rect represents the area where is updated graphically.
It is used to limit the area to update so that it becomes more efficient.
Let's see how it's set in Wayland surface context.
## damage_px in WaylandSurface
[`damage_px`](https://source.chromium.org/chromium/chromium/src/+/main:ui/ozone/platform/wayland/host/wayland_surface.h;l=280;drc=8e78783dc1f7007bad46d657c9f332614e240fd8) is one of the parameters in [State](https://source.chromium.org/chromium/chromium/src/+/main:ui/ozone/platform/wayland/host/wayland_surface.h;l=274;drc=8e78783dc1f7007bad46d657c9f332614e240fd8) struct.
It's a vector of gfx::Rect not like the input region which is a single rect.
`damage_px` will be added via [UpdateBufferDamageRegion](https://source.chromium.org/chromium/chromium/src/+/main:ui/ozone/platform/wayland/host/wayland_surface.cc;l=300;drc=8e78783dc1f7007bad46d657c9f332614e240fd8), and the passed gfx::Rect will be pushed back to the vector.
The damage rect to append is set in [ApplySurfaceConfigure](https://source.chromium.org/chromium/chromium/src/+/main:ui/ozone/platform/wayland/host/wayland_frame_manager.cc;l=396;drc=8e78783dc1f7007bad46d657c9f332614e240fd8) refering [`damage_region`](https://source.chromium.org/chromium/chromium/src/+/main:ui/ozone/platform/wayland/common/wayland_overlay_config.h;l=69;drc=8e78783dc1f7007bad46d657c9f332614e240fd8) in the WaylandOverlayConfig.
`damage_region` here is a damage in viz::Display space which is the same space as `bounds_rect` and passed as one of the value in [gfx::OverlayPlaneData](https://source.chromium.org/chromium/chromium/src/+/main:ui/gfx/overlay_plane_data.h;l=23;drc=8e78783dc1f7007bad46d657c9f332614e240fd8) from viz.
Going back to WaylandSurface.
The set damage_px will be enclosed to integer rect and will be passed to Exo via [`wl_surface_damage`](https://source.chromium.org/chromium/chromium/src/+/main:ui/ozone/platform/wayland/host/wayland_surface.cc;l=861;drc=8e78783dc1f7007bad46d657c9f332614e240fd8).
From here, let's how Exo handles the passed damage rect.
## Damage in Exo
The implementation of `wl_surface_damage` is done in wl_compositor.cc [`surface_damage`](https://source.chromium.org/chromium/chromium/src/+/main:components/exo/wayland/wl_compositor.cc;l=70;drc=d3f5d46f104df42d2cf1c81b2a4e02925ef0f084).
It's doing something weird.
```cpp=
gfx::Rect t_damage = damage;
if (t_damage.width() == 0x7FFFFFFF) {
t_damage.set_width(0x7FFFFFFE);
}
if (t_damage.height() == 0x7FFFFFFF) {
t_damage.set_height(0x7FFFFFFE);
}
// SkRegion forbids 0x7FFFFFFF (INT32_MAX) as width or height, see
// SkRegion_kRunTypeSentinel, and would mark the resulting region from the
// union below as empty. See https://crbug.com/1463905
gfx::Rect intersected_damage = gfx::Rect(0x7FFFFFFE, 0x7FFFFFFE);
intersected_damage.Intersect(t_damage);
```
TODO(elkurin): what's this?
On adding damage via [Surface::Damage](https://source.chromium.org/chromium/chromium/src/+/main:components/exo/surface.cc;l=402;drc=8e78783dc1f7007bad46d657c9f332614e240fd8), it does not overwrite the damage area by itself, instead it stores the union with the currently registered damage area as [cc::Region damage](https://source.chromium.org/chromium/chromium/src/+/main:components/exo/surface.h;l=596;drc=8e78783dc1f7007bad46d657c9f332614e240fd8).
This damage will be cleared when the state is applied.
Damage takes the intersection area with `output_rect` in [CommitSurfaceHierarchy](https://source.chromium.org/chromium/chromium/src/+/main:components/exo/surface.cc;l=1172-1173;drc=8e78783dc1f7007bad46d657c9f332614e240fd8).
Here, `output_rect` is the current `content_size_`.
This logic assumes that the area which is not included in the current content cannot be modified, but this assumption might not be right since the content area may change.
On [AppendContentsToFrame](https://source.chromium.org/chromium/chromium/src/+/main:components/exo/surface.cc;l=1551-1555;drc=8e78783dc1f7007bad46d657c9f332614e240fd8), it trakes the intersection agains output_rect again and try to limit the affected area inside the current content.
This stored damage rect will be passed to [`texture_quad`](https://source.chromium.org/chromium/chromium/src/+/main:components/exo/surface.cc;l=1785;drc=8e78783dc1f7007bad46d657c9f332614e240fd8).