blob: b4f6b1908b43a81c0de976fa6c492e445abbfc6c [file] [log] [blame] [view]
Adrienne Walker7c2e4682019-01-16 01:39:051# How cc Works
2
3[Original google doc](https://docs.google.com/document/d/1yjzOWrPfFGviEd1nru3yzqxSHETC-zsEBSt9C8SvV-Q/edit)
4
5[Chinese translation](https://zhuanlan.zhihu.com/p/54601110)
6
7[TOC]
8
9## tl;dr
10
11[cc/](https://cs.chromium.org/chromium/src/cc/) is historically but inaccurately called the Chrome Compositor.
12It's neither "the" chrome compositor (of course we have many), nor a compositor at all any more.
13danakj suggests "content collator" as an alternative name.
14
15cc is embedded via ui/compositor or Android code in the browser process, as well as ui/compositor in mus utility processes.
16It is also embedded in the renderer process via Blink / RenderWidget.
17cc is responsible for taking painted inputs from its embedder, figuring out where and if they appear on screen, rasterizing and decoding and animating images from the painted input into gpu textures, and finally forwarding those textures on to the display compositor in the form of a compositor frame.
18cc also handles input forwarded from the browser process to handle pinch and scroll gestures responsively without involving Blink.
19
20## Process / thread architecture
21
22cc can be embedded in both single-threaded and multi-threaded incarnations.
23The single-threaded version has less overhead.
24The multi-threaded version incurs a latency cost, but allows for input and animations to be responsively handled on one thread while the other thread is busy.
25In general, the browser uses the single-threaded version as its main thread is cheap and light, whereas the renderer uses the multi-threaded version as its main thread (Blink) can be quite busy on some pages.
26
27Both single and multi-threaded versions drive themselves using the [cc::Scheduler](#scheduling), which determines when to submit frames.
28The one exception (a third mode that is only used in one place) is Blink layout tests and sim tests, which do not (always) use a scheduler and tell cc when to composite synchronously, via LayerTreeHost::Composite.
29This is for historical reasons, and also to have more control during testing.
30
31## Content Data Flow Overview
32
33![data flow diagram](images/how_cc_works-data_flow.png)
34
35The main interface to cc by embedders is a LayerTreeHost (created with various LayerTreeSettings) and a [tree](#trees-commit-activation) of [Layers](#layers).
36A Layer is a rectangle of content, with various properties describing how that content should appear on screen.
37cc is responsible for turning a painted representation of that content (e.g. a PaintRecord) into a rasterized representation (a software bitmap or a gpu texture) and figuring out where that rectangle appears on screen.
38
39cc turns this layer tree input into a set of property trees via a PropertyTreeBuilder and simplifies the layer tree down to an ordered list of visible layers.
40As a part of the slimming paint project, Blink will set property trees and layer list directly instead of going through the more historical layer tree interface and avoid the work of this part of the pipeline.
41
42During the [commit process](#commit-flow), cc forwards all of the inputs from the main thread data structures to compositor thread data structures.
43At this point, cc determines which parts of each Layer are visible and proceeds to decode images and [raster content](#raster-and-tile-management).
44Once all the content is ready to appear on screen, cc [activates](#trees-commit-activation) the committed tree and can then "draw" from it.
45
46cc unfortunately still uses the language of "draw" and “swap” in a number of places, despite the fact that it no longer does either of these things.
47“Draw” in cc means constructing a [compositor frame](#compositor-frames-render-passes-quads) full of quads and render passes for eventual drawing on screen.
48“Swap” in cc means submitting that frame to the display compositor via a CompositorFrameSink.
49These frames get sent to the viz SurfaceAggregator where compositor frames from all frame producers are aggregated together.
50
51## Input Data Flow Overview
52
53The other main piece of input to cc is user input, such as mouse clicks, mouse wheels, and touch gestures.
54In the renderer process, input is forwarded from the browser process.
55It is processed by ui::InputHandlerProxy (a cc::InputHandlerClient).
56
57Some of this input is forwarded to the LayerTreeHostImpl (a cc::InputHandler) at specific times.
58This allows it to modify the active layer’s property tree and scroll or pinch as needed.
59Some input can’t be handled by the compositor thread (e.g. there’s a synchronous Javascript touch or wheel handler) and so that input is forwarded along to Blink to handle directly.
60This input flow follows the opposite path of the content data flow in the section above.
61
62## Commit Flow
63
64Commit is a method of getting data atomically from the main thread to the compositor thread.
65(Even when running in single threaded mode, this operation occurs to move data into the right data structures.) Rather than sending an ipc, commit is done by blocking the main thread and copying data over.
66
67![commit flow diagram](images/how_cc_works-commit_flow.png)
68
69The main thread can request a commit in several ways.
70Most webpages request one via requestAnimationFrame, which eventually calls SetNeedsAnimate on LayerTreeHost.
71Additionally, modifying any of cc’s inputs (e.g. a layer property, such as its transform or a change to the layer’s content), will call either SetNeedsAnimate, SetNeedsUpdate, or SetNeedsCommit on LayerTreeHost.
72The different SetNeeds functions allow for different levels of early-out aborts of the commit if no work is determined to be needed.
73(For example, if the requestAnimationFrame callback does no work, then there’s no need to do the commit or even update layers.) All of these functions request a BeginMainFrame from the scheduler, if not currently in the middle of servicing one.
74
75At some point, the scheduler will respond with a ScheduledActionBeginMainFrame.
76This sends BeginFrameArgs from the compositor thread to the main thread to start a BeginMainFrame.
77BeginFrameArgs contain a time (for animation purposes) as well as any scroll deltas that have been applied on the compositor thread (mainly as a result of handling user scroll gestures) that Blink doesn’t know about.
78When Blink is embedding cc, a BeginMainFrame applies any compositor scroll deltas to Blink, kicks off requestAnimationFrame logic, and finishes the Blink half of the [rendering lifecycle](https://docs.google.com/document/d/1aitSOucL0VHZa9Z2vbRJSyAIsAz24kX8LFByQ5xQnUg/edit#).
79
80Once this is done, cc updates all layers.
81If at any point in this update pipeline, cc determines that no work left is required (e.g. a compositor thread scroll updates Blink, but Blink makes no changes to the page in response to that scroll), then it may abort the commit early.
82(Single-threaded cc never aborts commits, currently.) Once the embedder has finished its BeginMainFrame work and if the commit has not been aborted, then ProxyMain sends a synchronous NotifyReadyToCommit and blocks by forwarding a mutex to the compositor thread.
83
84When the scheduler is ready to commit, it will respond with a ScheduledActionCommit.
85The ProxyImpl on the compositor thread then does all the work of copying the data from the main thread (while it is blocked) to compositor thread data structures.
86It then releases the mutex so that the main thread can proceed.
87
88ProxyImpl is the only class that accesses data structures on both the main thread and the compositor thread.
89It only accesses the LayerTreeHost and Layers on the main thread when the main thread is blocked and enforces this through accessor DCHECKs.
90ProxyMain is its counterpart that exists on the main thread and is owned by the LayerTreeHost.
91For the single threaded case, SingleThreadProxy encompasses the tasks of both ProxyMain and ProxyImpl.
92
93## Layers
94
95A layer is a 2d rectangle of content with integer bounds.
96It has some transform, clip, and effects on it that describe how it should look on screen.
97
98There are two separate class hierarchies of Layers, one for the main thread layer tree (deriving from cc::Layer) and one for the compositor thread pending, active, and recycle layer trees (deriving from cc::LayerImpl).
99There is roughly a 1:1 correspondence, such that there exists SurfaceLayer and SurfaceLayerImpl or PictureLayer and PictureLayerImpl, so this section will mostly talk about these pairs synonymously.
100
101On the main thread, Layers are refcounted.
102LayerTreeHost owns the root layer, and each layer recursively owns its children.
103Some other parts of Blink also provide layers (e.g. the media system creating surface or video layers, plugins), which is why this is ref-counted.
104On the compositor thread, layers are unique_ptrs, owned by their parents.
105
106### Property Trees
107
108There are two ways of specifying hierarchical properties in cc.
109The historical way (and the way that ui/ manages this) is to provide a tree of Layers.
110If a parent layer has a transform (e.g. a translation, scale, or perspective), a clip, or an effect (e.g. a blur filter, or a mask, or an opacity) then this applies recursively to its children.
111This abstraction has a lot of [corner cases](https://docs.google.com/presentation/d/1V7gCqKR-edNdRDv0bDnJa_uEs6iARAU2h5WhgxHyejQ/edit#slide=id.g1c810b6196_0_68) (fixed position layers, scroll parents, scroll children) as well as not being performant (requires traversing a very large tree and calculating all properties at all steps).
112
113[Property trees](https://docs.google.com/presentation/d/1V7gCqKR-edNdRDv0bDnJa_uEs6iARAU2h5WhgxHyejQ/edit?usp=sharing) are a way around this.
114Instead, cc is provided with separate trees of properties: a transform tree, a clip tree, an effect tree.
115Each layer then has a node id for which transform, clip, and effect node that the layer is using.
116In this way, the update is O(interesting nodes) instead of O(layers).
117When there are property trees, there is also no longer a need for a tree of layers, and instead an ordered list of layers can be used.
118
119### PictureLayer
120
121A layer containing painted content.
122This content comes in the form of a cc::PaintRecord.
123PictureLayer is responsible for figuring out which scale(s) the content should be rastered at.
124Each scale is represented by a PictureLayerTiling, which is a sparse 2d regular tiling of the content at a particular scale.
125
126Each tile in this tiling is a cc::Tile, which represents potential content and their [rasterization is organized by the TileManager](#raster-and-tile-management).
127If you turn on composited layer borders in the [DevTools rendering settings](https://developer.chrome.com/devtools/docs/rendering-settings), you can see the tile borders.
128There are a number of heuristics that determine tile sizes, but for software raster tiles are roughly 256x256 px and for gpu raster tiles are roughly viewport width x one quarter viewport height.
129
130There are a number of heuristics to determine when and how to change rasterization scales.
131These aren’t perfect, but change them at your own peril.
132🐉🐉🐉
133
134### PictureImageLayer
135
136A subclass of PictureLayer.
137This is a special case for composited images in Blink.
138If an image gets a composited layer but has no borders or padding (i.e. the painted content is exactly equal to the image) then some work can be saved here.
139It "rasters" the image at fixed scales such that scaling this image is performant.
140This is really a savings for software raster and in a gpu raster world such layers should never be created.
141
142### TextureLayer
143
144Used for plugins, canvas when it does its own raster, and WebGL.
145The "texture" here refers to a reference to a gpu texture, though under software compositing it would be a shared memory bitmap.
146
147### SolidColorLayer
148
149If a layer is known to be merely a solid color, then there is no need to spend raster work or gpu memory on it.
150This is an optimization for when a layer’s content is known to be simple.
151
152### VideoLayer
153
154Deprecated as a part of the [surfaces for video project](https://docs.google.com/document/d/1tIWUfys0fH2L7h1uH8r53uIrjQg1Ee15ttTMlE0X2Ow/edit).
155Should eventually be deleted.
156
157### SurfaceLayer
158
159A surface layer has a surface id, which refers to some other stream of compositor frames in the system.
160This is a way of having an indirection to other compositor frame producers.
161See also: [surface documentation](https://www.chromium.org/developers/design-documents/chromium-graphics/surfaces).
162For example, Blink embeds references to out of process iframes via SurfaceLayer.
163
164### SolidColorScrollbarLayer
165
166Android scrollbars are "solid color" scrollbar layers.
167They are simple boxes that can be drawn on the compositor without creating texture resources for them.
168Both solid color and painted scrollbar layers exist so that scrolling on the compositor thread can update the scrollbar responsively without going back to the main thread.
169Without this, the page would scroll smoothly but the scrollbar would jump around jankily.
170
171### Painted(Overlay)ScrollbarLayer
172
173Desktop (non-Android) scrollbars are painted scrollbars.
174Because theme code is not thread safe, the thumb and track are painted and rastered into bitmaps on the main thread.
175Then, those bitmaps are emitted as quads on the compositor thread.
176ChromeOS uses PaintedOverlayScrollbarLayer, which is a nine-patch bitmap version.
177
178### HeadsUpDisplayLayer
179
180This layer supports [devtools rendering settings](https://developer.chrome.com/devtools/docs/rendering-settings).
181It draws an FPS meter, as well as overlays for paint invalidation or damage.
182This layer is special because it must be updated last because its inputs depend on all of the other layers’ damage calculations.
183
184### UIResourceLayer / NinePatchLayer
185
186UIResourceLayer is the software bitmap equivalent of TextureLayer.
187It handles uploading bitmaps and recreating them as needed when contexts are lost.
188NinePatchLayer is a derived UIResourceLayer class that dices up a UIResource into stretchable pieces.
189
190## Trees: commit / activation
191
192There are four types of layer trees, although there always exists 2-3 at any given time:
193
194* Main thread tree (cc::Layers, main thread, always exists)
195
196* Pending tree (cc::LayerImpl, compositor thread, staging for rasterization, optional)
197
198* Active tree (cc::LayerImpl, compositor thread, staging for drawing, always exists)
199
200* Recycle tree (cc::LayerImpl, compositor thread, mutually exclusive with pending tree)
201
202These are called "trees" as historically they have been trees and they exist in cc/trees/, but they are all lists and not trees (sorry).
203The main thread tree of Layers is owned by LayerTreeHost.
204The pending, active, and recycle trees of LayerImpls are all LayerTreeImpl instances owned by LayerTreeHostImpl.
205
206Commit is the process of pushing layer trees and properties from the main thread layer list to the pending tree.
207Activation is the process of pushing layer trees and properties from the pending tree to the active tree.
208During each of these processes, a duplicate layer structure is created (with the same layer ids, layer types, and properties).
209Layer ids are used to find the corresponding layer on each tree.
210A layer with id 5 on the main thread tree will push to layer id 5 on the pending tree.
211That pending layer will push to a layer with id 5 on the active tree.
212If that layer doesn’t exist, during the push it will be created. Similarly layers that no longer exist in the source tree are removed from the destination tree.
213This is all done via the tree synchronization process.
214
215Because allocation of Layer(Impl)s is expensive and most layer tree structures do not change from frame to frame, once a pending tree activates, it becomes the "recycle tree".
216This tree is never used for anything except for a cache of the last pending tree.
217This avoids allocation and property pushing work from main thread to pending tree.
218This is merely an optimization.
219
220The reason the pending tree exists is that if there are multiple changes to webpage content in a single Javascript callstack (e.g. an html canvas has a line drawn on it, while a div moves, and some background-color changes to blue), these all must be presented to the user atomically.
221Commit takes a snapshot of these changes and pushes them to the pending tree, so that Blink can continue to update the main thread tree for a future commit.
222After commit, these changes need to be rastered, and all of that rasterization must be complete before any of those new tiles can be presented to the user.
223The pending tree is the staging area to wait until all of the asynchronous rasterization work is complete.
224While the pending tree is staging all the rasterization work, the active tree can be updated with animations and scrolling to still be responsive to the user.
225
226Single-threaded versions of cc do not have a pending tree and commit directly to the active tree.
227(The recycle tree is unused in this mode.) This is an optimization to avoid extra work and copies.
228To work around this, the active tree is unable to be drawn until its tiles are all ready to draw.
229However, given that this is a single-threaded version of cc, there are no compositor thread animations or scrolling, and so there is little reason to need to draw.
230
231## Raster and tile management
232
233TileManager is responsible for rasterizing the world of tiles.
234Each PictureLayer provides a set of Tiles to rasterize, where each Tile is a subrectangle of painted content at a particular scale.
235
236The TileManager finds all the tiles that are required to draw on the active tree, all the tiles that are required to activate on the pending tree, less important tiles that are close to the viewport but are not visible, and also offscreen images to decode.
237
238There are currently three modes of raster in cc:
239
240* software raster: generate software bitmaps in the raster worker
241
242* gpu raster: generate gpu textures by sending gl commands over the command buffer
243
244* oop raster: generate gpu textures by sending paint commands over the command buffer
245
246The TileManager is instructed to do software vs hardware raster based on whether the [LayerTreeFrameSink](https://docs.google.com/document/d/1tFdX9StXn9do31hddfLuZd0KJ_dBFgtYmxgvGKxd0rY/edit) that it uses to submit compositor frames on has a context provider or not.
247It is always in one mode or the other.
248Switching modes destroys all resources.
249GPU raster is also currently deprecated and will be replaced by OOP (out-of-process) raster in all cases eventually.
250A common reason for switching modes is that the gpu process has crashed too much and all of Chrome switches from gpu to software raster and compositing modes.
251
252Once the TileManager decides the set of work to do, it generates a TaskGraph with dependencies and schedules that work across worker threads.
253TaskGraphs are not updated dynamically, but instead rescheduled as a whole graph.
254Tasks cannot be cancelled once they have started running.
255Scheduled tasks that have not yet started are cancelled by submitting another graph that does not include them.
256
257### Image Decoding
258
259Image decoding receives a lot of special care in the TileManager, as they are the most expensive part of raster, especially relative to comparatively speedy gpu raster.
260Each decode receives its own dependent task in the task graph.
261There is a separate decode cache for software raster vs gpu raster.
262The SoftwareImageDecodeCache manages decode, scale, and color correction, whereas the GpuImageDecodeCache also uploads those textures to the gpu process, storing them in [gpu discardable memory](https://docs.google.com/document/d/1LoNv02sntMa7PPK-TZTuMgc3UuWFqKpOdEqtFvcm_QE/edit).
263
264cc also handles all animation of animated gifs in Chrome.
265When gifs animate, they generate a new pending tree (initiated by the compositor thread instead of the main thread) with some raster invalidations and then re-raster tiles that are covered by that gif.
266
267### Raster Buffer Providers
268
269Apart from software vs hardware raster modes, Chrome can also run in software vs hardware display compositing modes.
270Chrome never mixes software compositing with hardware raster, but the other three combinations of raster mode x compositing mode are valid.
271
272The compositing mode affects the choice of RasterBufferProvider that cc provides, which manages the raster process and resource management on the raster worker threads:
273
274* BitmapRasterBufferProvider: rasters software bitmaps for software compositing
275
276* OneCopyRasterBufferProvider: rasters software bitmaps for gpu compositing into shared memory, which are then uploaded in the gpu process
277
278* ZeroCopyRasterBufferProvider: rasters software bitmaps for gpu compositing directly into a GpuMemoryBuffer (e.g. IOSurface), which can immediately be used by the display compositor
279
280* GpuRasterBufferProvider: rasters gpu textures for gpu compositing over a command buffer via gl (for gpu raster) or via paint commands (for oop raster)
281
282Note, due to locks on the context, gpu and oop raster are limited to one worker thread at a time, although image decoding can proceed in parallel on other threads.
283This single thread limitation is solved with a lock and not with thread affinity.
284
285## Animation
286
287This directory implements an animation framework (used by LayerTreeHost(Impl) through the cc::MutatorHost interface). The framework supports keyframe based animations of transform lists, opacity, and filter lists which directly manipulate those values on the relevant TransformNode / EffectNode in the property tree (identified by ElementId).
288
289An animation is represented by an instance of Animation which has one (or more in the future) KeyframeEffects, each of which has multiple KeyframeModels. Animation manages the play state, start time, etc of an animation, KeyframeEffect represents a target element of the animation, and each KeyframeModel describes the animation of a particular property (e.g. transform / opacity / filter) on that element. An animation may either represent an embedder animation (e.g., a Blink animation of a transform property) or it can be an animation from cc itself (e.g., a scroll animation for smooth scrolling).
290
291LayerTreeHostImpl informs AnimationHost of new and removed elements, which in turn will update the state of animations which depend on those elements. It calls NeedsTickAnimations to know if more animation frames should be scheduled, and TickAnimations every frame to update animation timing, state, generate animation events, and update the actual output value of property tree nodes based on the animation.
292
293## cc/paint/
294
295This directory stores a number of classes that represent painted content.
296They are extremely similar to Skia data structures, but are mutable, introspectable, and serializable in all cases.
297They also handle security concerns (e.g. [TOCTOU](https://en.wikipedia.org/wiki/Time_of_check_to_time_of_use) issues serializing out of shared memory that a malicious renderer could be manipulating as it is read by the gpu process) that Skia does not want to think about.
298
299PaintRecord (aka PaintOpBuffer) is the SkPicture equivalent that stores a number of PaintOps.
300A PaintRecord can either be rasterized by a raster buffer provider into a bitmap or a gpu texture (when using software or gpu raster), or it can be serialized (when using oop raster).
301
302PaintCanvas is the abstract class to record paint commands.
303It can be backed by either a SkiaPaintCanvas (to go from paint ops to SkCanvas) or a PaintRecordCanvas (to turn paint ops into a recorded PaintRecord).
304
305## Scheduling
306
307cc’s actions are driven by a cc::Scheduler.
308This is one of many schedulers in Chrome, including the Blink scheduler, the viz::DisplayScheduler, the browser UI task scheduler, and the gpu scheduler.
309
310The cc::Scheduler is owned by ProxyImpl (or SingleThreadProxy).
311It takes various inputs (visibility, begin frame messages, needs redraw, ready to draw, ready to activate, etc).
312These inputs drive the cc::SchedulerStateMachine, which then determines actions for the SchedulerClient (LayerTreeHostImpl) to take, such as "Commit" or “ActivateSyncTree” or “PrepareTiles”.
313These actions are generally expensive parts of the pipeline that we want to carefully rate limit or that have state-related dependencies.
314
315cc::Scheduler code differentiates begin frames from the display compositor as BeginImplFrame (i.e. should cc produce a compositor frame) and a begin frame for its embedder as BeginMainFrame (i.e. should cc tell Blink to run requestAnimationFrame and produce a commit, or in the browser if should cc tell ui to do something similar).
316The BeginImplFrame is driven by a viz::BeginFrameSource which in turn is driven the the display compositor.
317
318In a full pipeline update with low latency and fast rasterization, the general scheduling flow is BeginImplFrame -> BeginMainFrame -> Commit -> ReadyToActivate -> Activate -> ReadyToDraw -> Draw.
319
320Additionally, if rasterization is slow, a second BeginMainFrame can be sent before activation, and it will block in NotifyReadyToCommit until the activation completes, as the SchedulingStateMachine will prevent the commit from starting while there is a pending tree that hasn’t activated yet.
321This allows the main thread to work on the next frame in parallel instead of sitting idle at the expense of latency.
322One hypothetical ordering of events with slow raster could be:
323
324BeginImplFrame1 -> BeginMainFrame1 -> Commit1 -> (slow raster) -> BeginImplFrame2 -> BeginMainFrame2 -> ReadyToActivate1 -> Activate1 -> Commit2 -> ReadyToDraw1 -> Draw1.
325
326The cc::Scheduler maintains a deadline by which it expects its embedder to respond.
327If the main thread is slow to respond, then the Scheduler may draw without waiting for a commit.
328If this happens, then Scheduler is considered to be in high latency mode.
329If future frames start becoming faster again, the scheduler can attempt to skip a BeginMainFrame in order to "catch up" and re-enter low latency mode.
330High latency mode trades off latency for throughput by increasing pipelining.
331It maintains this distinction by keeping a history of times and trying to adjust with heuristics.
332
333## Compositor frames, render passes, quads
334
335The output of cc is a compositor frame.
336A compositor frame consists of metadata (device scale, color space, size) and an ordered set of render passes.
337A render pass contains an ordered set of quads that have references to resources (e.g. gpu textures) and information about how to draw those resources (sizes, scales, texture coordinates, etc).
338A quad is a single rectangle on screen, and is what you see when [composited layer borders are visualized](https://developer.chrome.com/devtools/docs/rendering-settings#show-composited%20layer%20borders).
339Layers themselves produce quads via derived AppendQuads function.
340This produces a set of quads that fill (without overlapping or intersecting) the visible rect of the layer.
341
342There are various types of quads that roughly correspond to different layer types (ContentDrawQuad, TextureDrawQuad, SolidColorDrawQuad).
343Because layers that produce many quads (i.e. PictureLayerImpl) produce many quads with the same info, SharedQuadState is an optimization that collects this shared information so that each individual quad is slimmer.
344RenderSurfaceImpls are 1:1 with render passes and exist mostly to provide the same AppendQuads logic that Layers do for their quads, in that RenderSurfaceImpl produces RenderPassDrawQuads.
345
346![compositor frame diagram](images/how_cc_works-compositor_frame.png)
347
348A render pass exists to support composited effects (see: effect tree).
349These can be cases where compositing is required to perform an effect.
350It can also be cases where doing compositing first can make the effect easier to implement (because then it applies to a single render pass texture, instead of an arbitrary set of quads produced by some subtree of layers).
351Common cases for render passes are: masks, filters (e.g. blur), clipping rotated layers, or opacity applied to a subtree of content.
352
353Inside a compositor frame, render passes and the quads within a render pass are ordered.
354The render passes are a flattened list that represent that dependency tree of render passes.
355If render pass 1 depends on render pass 9 (because it contains a RenderPassDrawQuad referencing the output of 9), then 9 will appear in the list before 1.
356Therefore, the root render pass is always last in the list.
357Inside a single render pass, the quads are ordered back to front ([Painter’s algorithm](https://en.wikipedia.org/wiki/Painter%27s_algorithm)).
358
359In general, quads are not considered to live in a 3d space (even if transformed by 3d transforms) and are still drawn in order, on top of anything drawn before it.
360However, there is a mode where a set of quads can be in a 3d context (caused by css transform-style: preserve-3d).
361A BSP tree is used to sort and intersect these against each other in the same 3d context.
362
363## Glossary
364
365See: [cc/README.md](https://chromium.googlesource.com/chromium/src/+/master/cc/README.md#glossaries)
366
367## Other Resources
368
369For a list of presentations, videos, and design docs, see: [https://www.chromium.org/developers/design-documents/chromium-graphics](https://www.chromium.org/developers/design-documents/chromium-graphics)
370
371## Miscellaneous Corner Cases That Don’t Fit Anywhere Else, Sorry
372
373### Damage
374
375Chrome has different notions of invalidation throughout the system.
376"Paint invalidation" is portions of the document that need to be repainted in Blink.
377“Raster invalidation” is parts of a layer that have changed and need to be re-rastered (possibly due to paint invalidation, but also synthetic invalidations such as the first time a layer is rastered or when textures are thrown away and then needed again).
378Finally, damage is another word for “draw invalidation”.
379It’s the part of the screen that needs to be redrawn.
380
381There’s two types of damage: invalidation damage and expose damage.
382Invalidation damage is due to raster invalidation, where a part of a texture has changed and the screen needs to be updated.
383Expose damage is when a layer goes away, gets added for the first time, or gets reordered.
384There’s no raster invalidation in these cases, but the screen still needs to be updated.
385
386cc calculates damage in the DamageTracker and forwards it along with a CompositorFrame.
387One reason damage is needed in the display compositor is to do partial swap (where only a portion of the screen is updated), which saves power.
388Another reason is when using hardware overlays, such that the display compositor can know that only an overlay was damaged and not have to re-composite the rest of the scene.
389
390### Mask Layers
391
392Mask layers are layers used to implement a [masking effect](https://webkit.org/blog/181/css-masks/).
393They sit outside the layer tree, without parents.
394They’re owned by the layer the mask is applied to.
395They can be any type of layer subclass (e.g. PictureLayer or SolidColorLayer).
396Any time layers are iterated over, they are a special case that needs to be considered because they aren’t part of the normal parent/child tree.
397They get treated the same as other layers in terms of rasterization and tile management, although their AppendQuads function goes through RenderSurfaceImpl instead of in the top level iteration, because they are part of an effect and not a layer drawn on its own.
398
399### "Impl"
400
401cc uses the "impl" suffix ...differently than the rest of Chrome or other software engineers.
402In cc, “impl” means that the class is used on the compositor thread and not on the main thread.
403
404The historical reason for this is that at one point we had Layer on the main thread and we needed an equivalent class to run on the compositor thread.
405jamesr@ consulted with nduca@ who made the very logical argument that things on the compositor thread were internal to the compositor and would really be the implementation of the main thread version, and hence LayerImpl.
406See: [https://bugs.webkit.org/show\_bug.cgi?id=55013#c5](https://bugs.webkit.org/show_bug.cgi?id=55013#c5)
407
408Then if you need a tree of LayerImpls, you have LayerTreeImpl, and a place to hang those trees is LayerTreeHostImpl.
409Suddenly, then the "impl thread" was the thread where all the “impl classes” lived.
410If you’re moving rasterization to the compositor thread, then suddenly that’s called “impl-side painting”.