# Layer / window correspondence in Chromium
There are many windows, layers, compositors... in Chromium and it's difficult to track which represents what.
Let's dive in to UI / cc to understand the correspondence.
## Layers in ui::Compsoitor
[ui::Compositor](https://source.chromium.org/chromium/chromium/src/+/main:ui/compositor/compositor.h) is an object to take care of GPU painting which exists inside UI and bridge the interaction with cc.
ui::Compositor has 2 layer object.
One is [`root_layer_`](https://source.chromium.org/chromium/chromium/src/+/main:ui/compositor/compositor.h;l=543;drc=e5a82a6a41e3f75e587a1259ba3b26d51009141e).
This is [ui::Layer](https://source.chromium.org/chromium/chromium/src/+/main:ui/compositor/layer.h;l=72;drc=0907824d24b445e547bf98c36806385688cf41d6) object which manages a texture, transform and child layers in UI.
When [Compositor::SetRootLayer](https://source.chromium.org/chromium/chromium/src/+/main:ui/compositor/compositor.cc;l=382;drc=0907824d24b445e547bf98c36806385688cf41d6) is called, the given `root_layer` will be registered as a root layer in this compositor.
```cpp=
void Compositor::SetRootLayer(Layer* root_layer) {
root_layer_ = root_layer;
root_web_layer_->RemoveAllChildren();
if (root_layer_)
root_layer_->SetCompositor(this, root_web_layer_);
}
```
[SetCompositor](https://source.chromium.org/chromium/chromium/src/+/main:ui/compositor/layer.cc;l=372;drc=0907824d24b445e547bf98c36806385688cf41d6) will let `root_layer`'s cc layer be a direct child of `cc_layer_` which is [`root_web_layer_`](https://source.chromium.org/chromium/chromium/src/+/main:ui/compositor/compositor.h;l=567;drc=0907824d24b445e547bf98c36806385688cf41d6).
In short, `root_layer_` is a child of `root_web_layer_`.
[`root_web_layer_`](https://source.chromium.org/chromium/chromium/src/+/main:ui/compositor/compositor.h;l=567;drc=0907824d24b445e547bf98c36806385688cf41d6) is the other layer that ui::Compositor has.
This is created on [Compositor ctor](https://source.chromium.org/chromium/chromium/src/+/main:ui/compositor/compositor.cc;l=110;drc=0907824d24b445e547bf98c36806385688cf41d6).
## What is root layer in WindowTreeHost?
[WindowTYreeHost::InitCompositor](https://source.chromium.org/chromium/chromium/src/+/main:ui/compositor/compositor.cc;l=110;drc=0907824d24b445e547bf98c36806385688cf41d6) will initialize ui::Compositor.
Inside this method, it registers root layer by [Compositor::SetRootLayer](https://source.chromium.org/chromium/chromium/src/+/main:ui/compositor/compositor.cc;l=110;drc=0907824d24b445e547bf98c36806385688cf41d6).
The root layer is `window()->layer()`.
`window_` of WindowTreeHost is owned by WindowTreeHost.
For Lacros, the iwndow is constructed on [WindowTreeHost::Create](https://source.chromium.org/chromium/chromium/src/+/main:ui/aura/window_tree_host_platform.cc;l=48-50;drc=0907824d24b445e547bf98c36806385688cf41d6) with `client::WINDOW_TYPE_UNKNOWN`.
This window acts as a toplevel window inside WindowTreeHost.
aura::Window inherits [ui::LayerOwner](https://source.chromium.org/chromium/chromium/src/+/main:ui/aura/window.h;l=107;drc=0907824d24b445e547bf98c36806385688cf41d6) which manages the layer ownership.
It owns only one ui::Layer so we can consider this layer as a correspondance to the window.
By the way, only one `compositor_` is owned by WindowTreeHost, so there is a correspondance between aura::WindowTreeHost and ui::Compositor as well.
## DesktopNativeWidgetAura
[DesktopNativeWidgetAura](https://source.chromium.org/chromium/chromium/src/+/main:ui/views/widget/desktop_aura/desktop_native_widget_aura.h;l=58;drc=0907824d24b445e547bf98c36806385688cf41d6) handles toplevel widgets.
It owns [WindowTreeHost](https://source.chromium.org/chromium/chromium/src/+/main:ui/views/widget/desktop_aura/desktop_native_widget_aura.h;l=289;drc=0907824d24b445e547bf98c36806385688cf41d6) mentioned above.
This can be created obtained paired with the root window via static method [ForWindow](https://source.chromium.org/chromium/chromium/src/+/main:ui/views/widget/desktop_aura/desktop_native_widget_aura.h;l=76;drc=0907824d24b445e547bf98c36806385688cf41d6).
[`content_window_`](https://source.chromium.org/chromium/chromium/src/+/main:ui/views/widget/desktop_aura/desktop_native_widget_aura.h;l=303;drc=0907824d24b445e547bf98c36806385688cf41d6) is a native view.
This is attached to `window_` in WindowTreeHost as a direct child on [InitNativeWidget](https://source.chromium.org/chromium/chromium/src/+/main:ui/views/widget/desktop_aura/desktop_native_widget_aura.cc;l=580;drc=667c9184e61b12967e964275d177670de847e9e0).
Also, compositor is obtained by tracking `content_window_` like this:
```cpp=
const ui::Compositor* DesktopNativeWidgetAura::GetCompositor() const {
return content_window_ ? content_window_->layer()->GetCompositor() : nullptr;
}
```
This is a bit confusing as it looks like the layer corresponding to `content_window_` is the same layer as compositor, but it's not.
Inside [Layer::GetCompositor](https://source.chromium.org/chromium/chromium/src/+/main:ui/compositor/layer.cc;l=364;drc=0907824d24b445e547bf98c36806385688cf41d6), it returns `GetRoot(this)->compositor_` where [GetRoot](https://source.chromium.org/chromium/chromium/src/+/main:ui/compositor/layer.cc;l=60;drc=0907824d24b445e547bf98c36806385688cf41d6) computes the root layer by traversion its parent.
<- Isn't it more accurate to take `host_->compositor()`?