Color blending

Impeller currently supports the same set of blending operations that Skia supports. Internally, Impeller distinguishes between two different kinds of blend modes: Those which can be performed using the raster pipeline blend configuration (called “Pipeline Blends”), and those which cannot (called “Advanced Blends”).

All blend modes conform to the W3C Compositing and Blending recommendation.

Blend operations are driven by the BlendMode enum. In the Aiks layer, all drawing operations conform to the given Paint::blend_mode. In the Entities layer, all Entities have an associated blend mode, which can be set via Entity::SetBlendMode(BlendMode).

Glossary of blending terms

TermDefinition
Source colorAny color that is output by a fragment shader.
Destination colorThe backdrop color in a blend operation.
Premultiplied colorA color that has its alpha multiplied into it. Used for additive blending operations as well as colors presented to a surface.
Porter-Duff alpha compositeOne of several operations that add together a source color and destination color, with both the source and destination colors being multiplied by respective alpha factors.
Pipeline blendA blend mode that Impeller can always implement by using the raster pipeline blend configuration provided by the underlying graphics backend. Most of these are simple Porter-Duff alpha composites.
Advanced blendA blend mode that Impeller computes using a fragment program.

Premultiplied colors

In Impeller, all blending source colors are assumed to be premultiplied for the purpose of blending. This means that all Entity shaders must output colors with premultiplied alpha. In general, these shaders also assume that sampled textures and uniform color inputs are premultiplied.

The reason for this is that it enables us to implement all of the Porter-Duff alpha composites using the built-in raster pipeline blend configuration offered by all major graphics backends.

Pipeline blends

Most of the pipeline blends are actually Porter-Duff alpha composites, which add together the source color and destination color -- both the source and destination colors are multiplied by an alpha factor which determines the behavior of the blend.

Pipeline blends are always cheap and don't require additional draw calls to render.

Pipeline blend
Clear
Source
Destination
SourceOver
DestinationOver
SourceIn
DestinationIn
SourceOut
DestinationOut
SourceATop
DestinationATop
Xor
Plus
Modulate

Advanced blends

Advanced blends are blends that Impeller can‘t always implement using the built-in raster pipeline blend configuration offered by graphics backends. Instead, they’re implemented using special blend shaders that bind the backdrop texture in a separate render pass.

Note that all of the advanced blends are color blends rather than alpha composites, and they can technically be combined with any pipeline blend with predictable compositing behavior. However, in order to keep in line with Flutter‘s (and Skia’s) current behavior, Impeller uses Source Over compositing when rendering all advanced blends.

Advanced blends are expensive when compared to pipeline blends (which are essentially free) for the following reasons:

  • For each advanced blend, the current render pass ends because the backdrop texture needs to be sampled.
  • A potentially large texture (the render pass backdrop) is sampled. Although in practice, just the coverage rectangle of the source being blended is actually used.
  • An intermediary texture is allocated for the blend output before being blitted back to the render pass texture.
Advanced blend
kScreen
Overlay
Darken
Lighten
ColorDodge
ColorBurn
HardLight
SoftLight
Difference
Exclusion
Multiply
Hue
Saturation
Color
Luminosity