---
# System prepended metadata

title: Buffer Attach
tags: [Chromium-Graphics, elkurin-daily-note]

---

# Buffer Attach

[exo::Buffer](https://source.chromium.org/chromium/chromium/src/+/main:components/exo/buffer.h;l=33;drc=b65410a30ac4fd44940e4a290289f65a7bb4d671) is a class prividing the Surface content.  
Let's see how the buffer is attached.

## Attach
[`wl_surface_attach`](https://source.chromium.org/chromium/chromium/src/+/main:components/exo/wayland/wl_compositor.cc;l=61;drc=7fa0c25da15ae39bbd2fd720832ec4df4fee705a) wayland message calls [Surface::Attach](https://source.chromium.org/chromium/chromium/src/+/main:components/exo/surface.cc;l=380;drc=7fa0c25da15ae39bbd2fd720832ec4df4fee705a) which resets the buffer of `pending_state_` in Surface.

This message is called when applyingh the new state.  
[WaylandFrameManager::ApplySurfaceConfigure](https://source.chromium.org/chromium/chromium/src/+/main:ui/ozone/platform/wayland/host/wayland_frame_manager.cc;l=331;drc=7fa0c25da15ae39bbd2fd720832ec4df4fee705a) handles `surface` and passes [WaylandBufferHandle](https://source.chromium.org/chromium/chromium/src/+/main:ui/ozone/platform/wayland/host/wayland_buffer_handle.h;l=20;drc=7fa0c25da15ae39bbd2fd720832ec4df4fee705a) object, a wrapper of a wl_buffer, created from `surface` to [WaylandSurface::AttachBuffer](https://source.chromium.org/chromium/chromium/src/+/main:ui/ozone/platform/wayland/host/wayland_surface.cc;l=278;drc=7fa0c25da15ae39bbd2fd720832ec4df4fee705a).  
BufferHandle is obtained from `buffer_backing_` using `buffer_id` as a key.  
[`buffer_backing_`](https://source.chromium.org/chromium/chromium/src/+/main:ui/ozone/platform/wayland/host/wayland_buffer_manager_host.h;l=214-215;drc=7fa0c25da15ae39bbd2fd720832ec4df4fee705a) represents a dmabuf.shm buffer which GPU creates when CreateBuffer is called. The created buffer will be kept as flat_map corresponding to uint32_t id.  
Buffer creation is requested from GPU ([CreateDmabufBasedBufferTask](https://source.chromium.org/chromium/chromium/src/+/main:ui/ozone/platform/wayland/gpu/wayland_buffer_manager_gpu.cc;l=522;drc=7fa0c25da15ae39bbd2fd720832ec4df4fee705a)) via [WaylandBufferManagerHost](https://source.chromium.org/chromium/chromium/src/+/main:ui/ozone/platform/wayland/mojom/wayland_buffer_manager.mojom;l=19;drc=7fa0c25da15ae39bbd2fd720832ec4df4fee705a) mojo APIs.

At the end of AttachBuffer, it calls [WaylandSurface::ApplyPendingState](https://source.chromium.org/chromium/chromium/src/+/main:ui/ozone/platform/wayland/host/wayland_surface.cc;l=448;drc=67520ac99db89395b10f2ab728b540eef0da8292) with `buffer` registered to State. [`buffer`](https://source.chromium.org/chromium/chromium/src/+/main:ui/ozone/platform/wayland/host/wayland_surface.h;l=295;drc=7fa0c25da15ae39bbd2fd720832ec4df4fee705a) is `wl_buffer` object and its size is `buffer_size_px`.  


[`gpu_memory_buffer_`](https://source.chromium.org/chromium/chromium/src/+/main:components/exo/buffer.h;l=33;drc=b65410a30ac4fd44940e4a290289f65a7bb4d671) is a container of the GPU memory buffer.

## GpuMemoryBuffer
[GpuMemoryBuffer](https://source.chromium.org/chromium/chromium/src/+/main:ui/gfx/gpu_memory_buffer.h;l=93;drc=7fa0c25da15ae39bbd2fd720832ec4df4fee705a) corresponds to shared memory shasred with the GPU.  
This buffer can be written to CPU code and can be read by GPU as well.  

This class provides virtual interface required to handle memory such as Map, getter methods for each properties and OnMemoryDump.  
Its implementation is [GetMemoryBufferImpl](https://source.chromium.org/chromium/chromium/src/+/main:gpu/ipc/common/gpu_memory_buffer_impl.h;l=20;drc=7fa0c25da15ae39bbd2fd720832ec4df4fee705a) for non-test codes.  
