paint method
Paint this render object into the given context at the given offset.
Subclasses should override this method to provide a visual appearance for themselves. The render object's local coordinate system is axis-aligned with the coordinate system of the context's canvas and the render object's local origin (i.e, x=0 and y=0) is placed at the given offset in the context's canvas.
Do not call this function directly. If you wish to paint yourself, call
markNeedsPaint instead to schedule a call to this function. If you wish
to paint one of your children, call PaintingContext.paintChild on the
given context.
When painting one of your children (via a paint child function on the given context), the current canvas held by the context might change because draw operations before and after painting children might need to be recorded on separate compositing layers.
Implementation
@override
void paint(PaintingContext context, Offset offset) {
if (child == null) {
layer = null;
return;
}
assert(needsCompositing);
final tile = tileOf(size);
if (tile.isEmpty) {
layer = null;
super.paint(context, offset);
return;
}
// Where the box's corner lands on the page, and how far the nearest
// CSS pixel is from it: the tile starts there. Blink snaps a box's
// paint rect to whole CSS pixels (`ToPixelSnappedRect`, half up) and
// scales to the device after, so at a device pixel ratio of 2 a box at
// 46.5px paints from 94 device pixels, not 93: snapped to the device
// instead, every dot of a mask at that corner was one device pixel
// off the browser's.
final corner = localToGlobal(Offset.zero);
final snap = Offset((corner.dx + 0.5).floorToDouble() - corner.dx, (corner.dy + 0.5).floorToDouble() - corner.dy);
final image = _tile(tile.size);
final matrix = Matrix4.translationValues(tile.left + snap.dx, tile.top + snap.dy, 0)
..scaleByDouble(tile.width / image.width, tile.height / image.height, 1, 1);
layer ??= ShaderMaskLayer();
layer!
..shader = ImageShader(
image,
TileMode.repeated,
TileMode.repeated,
matrix.storage,
filterQuality: _sampling(image, tile.size, _dpr),
)
..maskRect = offset & size
..blendMode = BlendMode.dstIn;
context.pushLayer(layer!, super.paint, offset);
}