1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
| class PaintingContext extends ClipContext {
@protected PaintingContext(this._containerLayer, this.estimatedBounds);
final ContainerLayer _containerLayer;
final Rect estimatedBounds;
static void repaintCompositedChild(RenderObject child, { bool debugAlsoPaintedParent = false }) { _repaintCompositedChild( child, debugAlsoPaintedParent: debugAlsoPaintedParent, ); }
static void _repaintCompositedChild( RenderObject child, { bool debugAlsoPaintedParent = false, PaintingContext? childContext, }) { OffsetLayer? childLayer = child._layerHandle.layer as OffsetLayer?; if (childLayer == null) { final OffsetLayer layer = child.updateCompositedLayer(oldLayer: null); child._layerHandle.layer = childLayer = layer; } else { Offset? debugOldOffset; childLayer.removeAllChildren(); final OffsetLayer updatedLayer = child.updateCompositedLayer(oldLayer: childLayer); } child._needsCompositedLayerUpdate = false; childContext ??= PaintingContext(childLayer, child.paintBounds); child._paintWithContext(childContext, Offset.zero);
childContext.stopRecordingIfNeeded(); }
static void updateLayerProperties(RenderObject child) { final OffsetLayer childLayer = child._layerHandle.layer! as OffsetLayer; Offset? debugOldOffset; final OffsetLayer updatedLayer = child.updateCompositedLayer(oldLayer: childLayer); child._needsCompositedLayerUpdate = false; }
static void debugInstrumentRepaintCompositedChild( RenderObject child, { bool debugAlsoPaintedParent = false, required PaintingContext customContext, }) { }
void paintChild(RenderObject child, Offset offset) { if (child.isRepaintBoundary) { stopRecordingIfNeeded(); _compositeChild(child, offset); } else if (child._wasRepaintBoundary) { child._layerHandle.layer = null; child._paintWithContext(this, offset); } else { child._paintWithContext(this, offset); } }
void _compositeChild(RenderObject child, Offset offset) {
if (child._needsPaint || !child._wasRepaintBoundary) { repaintCompositedChild(child, debugAlsoPaintedParent: true); } else { if (child._needsCompositedLayerUpdate) { updateLayerProperties(child); } } final OffsetLayer childOffsetLayer = child._layerHandle.layer! as OffsetLayer; childOffsetLayer.offset = offset; appendLayer(childOffsetLayer); }
@protected void appendLayer(Layer layer) { layer.remove(); _containerLayer.append(layer); }
bool get _isRecording { final bool hasCanvas = _canvas != null; return hasCanvas; }
PictureLayer? _currentLayer; ui.PictureRecorder? _recorder; Canvas? _canvas;
@override Canvas get canvas { if (_canvas == null) { _startRecording(); } return _canvas!; }
void _startRecording() { _currentLayer = PictureLayer(estimatedBounds); _recorder = ui.PictureRecorder(); _canvas = Canvas(_recorder!); _containerLayer.append(_currentLayer!); }
VoidCallback addCompositionCallback(CompositionCallback callback) { return _containerLayer.addCompositionCallback(callback); }
@protected @mustCallSuper void stopRecordingIfNeeded() { if (!_isRecording) { return; } _currentLayer!.picture = _recorder!.endRecording(); _currentLayer = null; _recorder = null; _canvas = null; }
void setIsComplexHint() { _currentLayer?.isComplexHint = true; }
void setWillChangeHint() { _currentLayer?.willChangeHint = true; }
void addLayer(Layer layer) { stopRecordingIfNeeded(); appendLayer(layer); }
void pushLayer(ContainerLayer childLayer, PaintingContextCallback painter, Offset offset, { Rect? childPaintBounds }) { if (childLayer.hasChildren) { childLayer.removeAllChildren(); } stopRecordingIfNeeded(); appendLayer(childLayer); final PaintingContext childContext = createChildContext(childLayer, childPaintBounds ?? estimatedBounds);
painter(childContext, offset); childContext.stopRecordingIfNeeded(); }
@protected PaintingContext createChildContext(ContainerLayer childLayer, Rect bounds) { return PaintingContext(childLayer, bounds); }
ClipRectLayer? pushClipRect(bool needsCompositing, Offset offset, Rect clipRect, PaintingContextCallback painter, { Clip clipBehavior = Clip.hardEdge, ClipRectLayer? oldLayer }) { if (clipBehavior == Clip.none) { painter(this, offset); return null; } final Rect offsetClipRect = clipRect.shift(offset); if (needsCompositing) { final ClipRectLayer layer = oldLayer ?? ClipRectLayer(); layer ..clipRect = offsetClipRect ..clipBehavior = clipBehavior; pushLayer(layer, painter, offset, childPaintBounds: offsetClipRect); return layer; } else { clipRectAndPaint(offsetClipRect, clipBehavior, offsetClipRect, () => painter(this, offset)); return null; } }
ClipRRectLayer? pushClipRRect(bool needsCompositing, Offset offset, Rect bounds, RRect clipRRect, PaintingContextCallback painter, { Clip clipBehavior = Clip.antiAlias, ClipRRectLayer? oldLayer }) { if (clipBehavior == Clip.none) { painter(this, offset); return null; } final Rect offsetBounds = bounds.shift(offset); final RRect offsetClipRRect = clipRRect.shift(offset); if (needsCompositing) { final ClipRRectLayer layer = oldLayer ?? ClipRRectLayer(); layer ..clipRRect = offsetClipRRect ..clipBehavior = clipBehavior; pushLayer(layer, painter, offset, childPaintBounds: offsetBounds); return layer; } else { clipRRectAndPaint(offsetClipRRect, clipBehavior, offsetBounds, () => painter(this, offset)); return null; } }
ClipPathLayer? pushClipPath(bool needsCompositing, Offset offset, Rect bounds, Path clipPath, PaintingContextCallback painter, { Clip clipBehavior = Clip.antiAlias, ClipPathLayer? oldLayer }) { if (clipBehavior == Clip.none) { painter(this, offset); return null; } final Rect offsetBounds = bounds.shift(offset); final Path offsetClipPath = clipPath.shift(offset); if (needsCompositing) { final ClipPathLayer layer = oldLayer ?? ClipPathLayer(); layer ..clipPath = offsetClipPath ..clipBehavior = clipBehavior; pushLayer(layer, painter, offset, childPaintBounds: offsetBounds); return layer; } else { clipPathAndPaint(offsetClipPath, clipBehavior, offsetBounds, () => painter(this, offset)); return null; } }
ColorFilterLayer pushColorFilter(Offset offset, ColorFilter colorFilter, PaintingContextCallback painter, { ColorFilterLayer? oldLayer }) { final ColorFilterLayer layer = oldLayer ?? ColorFilterLayer(); layer.colorFilter = colorFilter; pushLayer(layer, painter, offset); return layer; }
TransformLayer? pushTransform(bool needsCompositing, Offset offset, Matrix4 transform, PaintingContextCallback painter, { TransformLayer? oldLayer }) { final Matrix4 effectiveTransform = Matrix4.translationValues(offset.dx, offset.dy, 0.0) ..multiply(transform)..translate(-offset.dx, -offset.dy); if (needsCompositing) { final TransformLayer layer = oldLayer ?? TransformLayer(); layer.transform = effectiveTransform; pushLayer( layer, painter, offset, childPaintBounds: MatrixUtils.inverseTransformRect(effectiveTransform, estimatedBounds), ); return layer; } else { canvas ..save() ..transform(effectiveTransform.storage); painter(this, offset); canvas.restore(); return null; } }
OpacityLayer pushOpacity(Offset offset, int alpha, PaintingContextCallback painter, { OpacityLayer? oldLayer }) { final OpacityLayer layer = oldLayer ?? OpacityLayer(); layer ..alpha = alpha ..offset = offset; pushLayer(layer, painter, Offset.zero); return layer; }
@override String toString() => '${objectRuntimeType(this, 'PaintingContext')}#$hashCode(layer: $_containerLayer, canvas bounds: $estimatedBounds)'; }
|