Commit c80603cf authored by Adam Barth's avatar Adam Barth

Add PaintingContext which has-a sky.Canvas instead of is-a sky.Canvas

We'll need this for compositing because we need to switch out the sky.Canvas
when we switch compositing layers.
parent 7295563d
...@@ -321,7 +321,8 @@ class SpriteBox extends RenderBox { ...@@ -321,7 +321,8 @@ class SpriteBox extends RenderBox {
_rootNode._invalidateToBoxTransformMatrix(); _rootNode._invalidateToBoxTransformMatrix();
} }
void paint(PaintingCanvas canvas, Offset offset) { void paint(PaintingContext context, Offset offset) {
final PaintingCanvas canvas = context.canvas;
canvas.save(); canvas.save();
// Move to correct coordinate space before drawing // Move to correct coordinate space before drawing
......
...@@ -129,7 +129,7 @@ abstract class RenderDecoratedSector extends RenderSector { ...@@ -129,7 +129,7 @@ abstract class RenderDecoratedSector extends RenderSector {
} }
// offset must point to the center of the circle // offset must point to the center of the circle
void paint(PaintingCanvas canvas, Offset offset) { void paint(PaintingContext context, Offset offset) {
assert(deltaRadius != null); assert(deltaRadius != null);
assert(deltaTheta != null); assert(deltaTheta != null);
assert(parentData is SectorParentData); assert(parentData is SectorParentData);
...@@ -138,6 +138,7 @@ abstract class RenderDecoratedSector extends RenderSector { ...@@ -138,6 +138,7 @@ abstract class RenderDecoratedSector extends RenderSector {
return; return;
if (_decoration.backgroundColor != null) { if (_decoration.backgroundColor != null) {
final PaintingCanvas canvas = context.canvas;
Paint paint = new Paint()..color = _decoration.backgroundColor; Paint paint = new Paint()..color = _decoration.backgroundColor;
Path path = new Path(); Path path = new Path();
double outerRadius = (parentData.radius + deltaRadius); double outerRadius = (parentData.radius + deltaRadius);
...@@ -266,13 +267,13 @@ class RenderSectorRing extends RenderSectorWithChildren { ...@@ -266,13 +267,13 @@ class RenderSectorRing extends RenderSectorWithChildren {
// offset must point to the center of our circle // offset must point to the center of our circle
// each sector then knows how to paint itself at its location // each sector then knows how to paint itself at its location
void paint(PaintingCanvas canvas, Offset offset) { void paint(PaintingContext context, Offset offset) {
// TODO(ianh): avoid code duplication // TODO(ianh): avoid code duplication
super.paint(canvas, offset); super.paint(context, offset);
RenderSector child = firstChild; RenderSector child = firstChild;
while (child != null) { while (child != null) {
assert(child.parentData is SectorChildListParentData); assert(child.parentData is SectorChildListParentData);
canvas.paintChild(child, offset.toPoint()); context.paintChild(child, offset.toPoint());
child = child.parentData.nextSibling; child = child.parentData.nextSibling;
} }
} }
...@@ -371,13 +372,13 @@ class RenderSectorSlice extends RenderSectorWithChildren { ...@@ -371,13 +372,13 @@ class RenderSectorSlice extends RenderSectorWithChildren {
// offset must point to the center of our circle // offset must point to the center of our circle
// each sector then knows how to paint itself at its location // each sector then knows how to paint itself at its location
void paint(PaintingCanvas canvas, Offset offset) { void paint(PaintingContext context, Offset offset) {
// TODO(ianh): avoid code duplication // TODO(ianh): avoid code duplication
super.paint(canvas, offset); super.paint(context, offset);
RenderSector child = firstChild; RenderSector child = firstChild;
while (child != null) { while (child != null) {
assert(child.parentData is SectorChildListParentData); assert(child.parentData is SectorChildListParentData);
canvas.paintChild(child, offset.toPoint()); context.paintChild(child, offset.toPoint());
child = child.parentData.nextSibling; child = child.parentData.nextSibling;
} }
} }
...@@ -464,12 +465,12 @@ class RenderBoxToRenderSectorAdapter extends RenderBox { ...@@ -464,12 +465,12 @@ class RenderBoxToRenderSectorAdapter extends RenderBox {
} }
} }
void paint(PaintingCanvas canvas, Offset offset) { void paint(PaintingContext context, Offset offset) {
super.paint(canvas, offset); super.paint(context, offset);
if (child != null) { if (child != null) {
Rect bounds = offset & size; Rect bounds = offset & size;
// we move the offset to the center of the circle for the RenderSectors // we move the offset to the center of the circle for the RenderSectors
canvas.paintChild(child, bounds.center); context.paintChild(child, bounds.center);
} }
} }
......
...@@ -33,8 +33,8 @@ class Dot { ...@@ -33,8 +33,8 @@ class Dot {
radius = 5 + (95 * event.pressure); radius = 5 + (95 * event.pressure);
} }
void paint(PaintingCanvas canvas, Offset offset) { void paint(PaintingContext context, Offset offset) {
canvas.drawCircle(position + offset, radius, _paint); context.canvas.drawCircle(position + offset, radius, _paint);
} }
} }
...@@ -70,7 +70,8 @@ class RenderTouchDemo extends RenderBox { ...@@ -70,7 +70,8 @@ class RenderTouchDemo extends RenderBox {
size = constraints.biggest; size = constraints.biggest;
} }
void paint(PaintingCanvas canvas, Offset offset) { void paint(PaintingContext context, Offset offset) {
final PaintingCanvas canvas = context.canvas;
Paint white = new Paint() Paint white = new Paint()
..color = const Color(0xFFFFFFFF); ..color = const Color(0xFFFFFFFF);
canvas.drawRect(offset & size, white); canvas.drawRect(offset & size, white);
......
...@@ -216,8 +216,8 @@ class RenderAutoLayout extends RenderBox ...@@ -216,8 +216,8 @@ class RenderAutoLayout extends RenderBox
defaultHitTestChildren(result, position: position); defaultHitTestChildren(result, position: position);
} }
void paint(PaintingCanvas canvas, Offset offset) { void paint(PaintingContext context, Offset offset) {
defaultPaint(canvas, offset); defaultPaint(context, offset);
} }
List<al.Constraint> _constructImplicitConstraints() { List<al.Constraint> _constructImplicitConstraints() {
......
...@@ -156,8 +156,8 @@ class RenderBlock extends RenderBlockBase { ...@@ -156,8 +156,8 @@ class RenderBlock extends RenderBlockBase {
assert(!size.isInfinite); assert(!size.isInfinite);
} }
void paint(PaintingCanvas canvas, Offset offset) { void paint(PaintingContext context, Offset offset) {
defaultPaint(canvas, offset); defaultPaint(context, offset);
} }
void hitTestChildren(HitTestResult result, { Point position }) { void hitTestChildren(HitTestResult result, { Point position }) {
...@@ -242,11 +242,11 @@ class RenderBlockViewport extends RenderBlockBase { ...@@ -242,11 +242,11 @@ class RenderBlockViewport extends RenderBlockBase {
super.performLayout(); super.performLayout();
} }
void paint(PaintingCanvas canvas, Offset offset) { void paint(PaintingContext context, Offset offset) {
canvas.save(); context.canvas.save();
canvas.clipRect(offset & size); context.canvas.clipRect(offset & size);
defaultPaint(canvas, offset.translate(0.0, startOffset)); defaultPaint(context, offset.translate(0.0, startOffset));
canvas.restore(); context.canvas.restore();
} }
void applyPaintTransform(Matrix4 transform) { void applyPaintTransform(Matrix4 transform) {
......
...@@ -491,20 +491,20 @@ abstract class RenderBox extends RenderObject { ...@@ -491,20 +491,20 @@ abstract class RenderBox extends RenderObject {
} }
Rect get paintBounds => Point.origin & size; Rect get paintBounds => Point.origin & size;
void debugPaint(PaintingCanvas canvas, Offset offset) { void debugPaint(PaintingContext context, Offset offset) {
if (debugPaintSizeEnabled) if (debugPaintSizeEnabled)
debugPaintSize(canvas, offset); debugPaintSize(context, offset);
if (debugPaintBaselinesEnabled) if (debugPaintBaselinesEnabled)
debugPaintBaselines(canvas, offset); debugPaintBaselines(context, offset);
} }
void debugPaintSize(PaintingCanvas canvas, Offset offset) { void debugPaintSize(PaintingContext context, Offset offset) {
Paint paint = new Paint(); Paint paint = new Paint();
paint.setStyle(sky.PaintingStyle.stroke); paint.setStyle(sky.PaintingStyle.stroke);
paint.strokeWidth = 1.0; paint.strokeWidth = 1.0;
paint.color = debugPaintSizeColor; paint.color = debugPaintSizeColor;
canvas.drawRect(offset & size, paint); context.canvas.drawRect(offset & size, paint);
} }
void debugPaintBaselines(PaintingCanvas canvas, Offset offset) { void debugPaintBaselines(PaintingContext context, Offset offset) {
Paint paint = new Paint(); Paint paint = new Paint();
paint.setStyle(sky.PaintingStyle.stroke); paint.setStyle(sky.PaintingStyle.stroke);
paint.strokeWidth = 0.25; paint.strokeWidth = 0.25;
...@@ -516,7 +516,7 @@ abstract class RenderBox extends RenderObject { ...@@ -516,7 +516,7 @@ abstract class RenderBox extends RenderObject {
path = new Path(); path = new Path();
path.moveTo(offset.dx, offset.dy + baselineI); path.moveTo(offset.dx, offset.dy + baselineI);
path.lineTo(offset.dx + size.width, offset.dy + baselineI); path.lineTo(offset.dx + size.width, offset.dy + baselineI);
canvas.drawPath(path, paint); context.canvas.drawPath(path, paint);
} }
// alphabetic baseline // alphabetic baseline
double baselineA = getDistanceToBaseline(TextBaseline.alphabetic, onlyReal: true); double baselineA = getDistanceToBaseline(TextBaseline.alphabetic, onlyReal: true);
...@@ -525,7 +525,7 @@ abstract class RenderBox extends RenderObject { ...@@ -525,7 +525,7 @@ abstract class RenderBox extends RenderObject {
path = new Path(); path = new Path();
path.moveTo(offset.dx, offset.dy + baselineA); path.moveTo(offset.dx, offset.dy + baselineA);
path.lineTo(offset.dx + size.width, offset.dy + baselineA); path.lineTo(offset.dx + size.width, offset.dy + baselineA);
canvas.drawPath(path, paint); context.canvas.drawPath(path, paint);
} }
} }
...@@ -586,9 +586,9 @@ class RenderProxyBox extends RenderBox with RenderObjectWithChildMixin<RenderBox ...@@ -586,9 +586,9 @@ class RenderProxyBox extends RenderBox with RenderObjectWithChildMixin<RenderBox
super.hitTestChildren(result, position: position); super.hitTestChildren(result, position: position);
} }
void paint(PaintingCanvas canvas, Offset offset) { void paint(PaintingContext context, Offset offset) {
if (child != null) if (child != null)
canvas.paintChild(child, offset.toPoint()); context.paintChild(child, offset.toPoint());
} }
} }
...@@ -812,7 +812,7 @@ class RenderOpacity extends RenderProxyBox { ...@@ -812,7 +812,7 @@ class RenderOpacity extends RenderProxyBox {
return _cachedPaint; return _cachedPaint;
} }
void paint(PaintingCanvas canvas, Offset offset) { void paint(PaintingContext context, Offset offset) {
if (child != null) { if (child != null) {
int a = _alpha; int a = _alpha;
...@@ -820,13 +820,13 @@ class RenderOpacity extends RenderProxyBox { ...@@ -820,13 +820,13 @@ class RenderOpacity extends RenderProxyBox {
return; return;
if (a == 255) { if (a == 255) {
canvas.paintChild(child, offset.toPoint()); context.paintChild(child, offset.toPoint());
return; return;
} }
canvas.saveLayer(null, _paint); context.canvas.saveLayer(null, _paint);
canvas.paintChild(child, offset.toPoint()); context.paintChild(child, offset.toPoint());
canvas.restore(); context.canvas.restore();
} }
} }
} }
...@@ -868,11 +868,11 @@ class RenderColorFilter extends RenderProxyBox { ...@@ -868,11 +868,11 @@ class RenderColorFilter extends RenderProxyBox {
return _cachedPaint; return _cachedPaint;
} }
void paint(PaintingCanvas canvas, Offset offset) { void paint(PaintingContext context, Offset offset) {
if (child != null) { if (child != null) {
canvas.saveLayer(offset & size, _paint); context.canvas.saveLayer(offset & size, _paint);
canvas.paintChild(child, offset.toPoint()); context.paintChild(child, offset.toPoint());
canvas.restore(); context.canvas.restore();
} }
} }
} }
...@@ -880,12 +880,12 @@ class RenderColorFilter extends RenderProxyBox { ...@@ -880,12 +880,12 @@ class RenderColorFilter extends RenderProxyBox {
class RenderClipRect extends RenderProxyBox { class RenderClipRect extends RenderProxyBox {
RenderClipRect({ RenderBox child }) : super(child); RenderClipRect({ RenderBox child }) : super(child);
void paint(PaintingCanvas canvas, Offset offset) { void paint(PaintingContext context, Offset offset) {
if (child != null) { if (child != null) {
canvas.save(); context.canvas.save();
canvas.clipRect(offset & size); context.canvas.clipRect(offset & size);
canvas.paintChild(child, offset.toPoint()); context.paintChild(child, offset.toPoint());
canvas.restore(); context.canvas.restore();
} }
} }
} }
...@@ -919,14 +919,14 @@ class RenderClipRRect extends RenderProxyBox { ...@@ -919,14 +919,14 @@ class RenderClipRRect extends RenderProxyBox {
final Paint _paint = new Paint()..isAntiAlias = false; final Paint _paint = new Paint()..isAntiAlias = false;
void paint(PaintingCanvas canvas, Offset offset) { void paint(PaintingContext context, Offset offset) {
if (child != null) { if (child != null) {
Rect rect = offset & size; Rect rect = offset & size;
canvas.saveLayer(rect, _paint); context.canvas.saveLayer(rect, _paint);
sky.RRect rrect = new sky.RRect()..setRectXY(rect, xRadius, yRadius); sky.RRect rrect = new sky.RRect()..setRectXY(rect, xRadius, yRadius);
canvas.clipRRect(rrect); context.canvas.clipRRect(rrect);
canvas.paintChild(child, offset.toPoint()); context.paintChild(child, offset.toPoint());
canvas.restore(); context.canvas.restore();
} }
} }
} }
...@@ -947,13 +947,13 @@ class RenderClipOval extends RenderProxyBox { ...@@ -947,13 +947,13 @@ class RenderClipOval extends RenderProxyBox {
return _cachedPath; return _cachedPath;
} }
void paint(PaintingCanvas canvas, Offset offset) { void paint(PaintingContext context, Offset offset) {
if (child != null) { if (child != null) {
Rect rect = offset & size; Rect rect = offset & size;
canvas.saveLayer(rect, _paint); context.canvas.saveLayer(rect, _paint);
canvas.clipPath(_getPath(rect)); context.canvas.clipPath(_getPath(rect));
canvas.paintChild(child, offset.toPoint()); context.paintChild(child, offset.toPoint());
canvas.restore(); context.canvas.restore();
} }
} }
} }
...@@ -1004,9 +1004,9 @@ abstract class RenderShiftedBox extends RenderBox with RenderObjectWithChildMixi ...@@ -1004,9 +1004,9 @@ abstract class RenderShiftedBox extends RenderBox with RenderObjectWithChildMixi
return result; return result;
} }
void paint(PaintingCanvas canvas, Offset offset) { void paint(PaintingContext context, Offset offset) {
if (child != null) if (child != null)
canvas.paintChild(child, child.parentData.position + offset); context.paintChild(child, child.parentData.position + offset);
} }
void hitTestChildren(HitTestResult result, { Point position }) { void hitTestChildren(HitTestResult result, { Point position }) {
...@@ -1297,18 +1297,18 @@ class RenderViewport extends RenderBox with RenderObjectWithChildMixin<RenderBox ...@@ -1297,18 +1297,18 @@ class RenderViewport extends RenderBox with RenderObjectWithChildMixin<RenderBox
dyInDevicePixels / devicePixelRatio); dyInDevicePixels / devicePixelRatio);
} }
void paint(PaintingCanvas canvas, Offset offset) { void paint(PaintingContext context, Offset offset) {
if (child != null) { if (child != null) {
Offset roundedScrollOffset = _scrollOffsetRoundedToIntegerDevicePixels; Offset roundedScrollOffset = _scrollOffsetRoundedToIntegerDevicePixels;
bool _needsClip = offset < Offset.zero || bool _needsClip = offset < Offset.zero ||
!(offset & size).contains(((offset - roundedScrollOffset) & child.size).bottomRight); !(offset & size).contains(((offset - roundedScrollOffset) & child.size).bottomRight);
if (_needsClip) { if (_needsClip) {
canvas.save(); context.canvas.save();
canvas.clipRect(offset & size); context.canvas.clipRect(offset & size);
} }
canvas.paintChild(child, (offset - roundedScrollOffset).toPoint()); context.paintChild(child, (offset - roundedScrollOffset).toPoint());
if (_needsClip) if (_needsClip)
canvas.restore(); context.canvas.restore();
} }
} }
...@@ -1452,10 +1452,11 @@ class RenderImage extends RenderBox { ...@@ -1452,10 +1452,11 @@ class RenderImage extends RenderBox {
size = _sizeForConstraints(constraints); size = _sizeForConstraints(constraints);
} }
void paint(PaintingCanvas canvas, Offset offset) { void paint(PaintingContext context, Offset offset) {
if (_image == null) if (_image == null)
return; return;
bool needsScale = size.width != _image.width || size.height != _image.height; bool needsScale = size.width != _image.width || size.height != _image.height;
final PaintingCanvas canvas = context.canvas;
if (needsScale) { if (needsScale) {
double widthScale = size.width / _image.width; double widthScale = size.width / _image.width;
double heightScale = size.height / _image.height; double heightScale = size.height / _image.height;
...@@ -1518,11 +1519,11 @@ class RenderDecoratedBox extends RenderProxyBox { ...@@ -1518,11 +1519,11 @@ class RenderDecoratedBox extends RenderProxyBox {
super.detach(); super.detach();
} }
void paint(PaintingCanvas canvas, Offset offset) { void paint(PaintingContext context, Offset offset) {
assert(size.width != null); assert(size.width != null);
assert(size.height != null); assert(size.height != null);
_painter.paint(canvas, offset & size); _painter.paint(context.canvas, offset & size);
super.paint(canvas, offset); super.paint(context, offset);
} }
String debugDescribeSettings(String prefix) => '${super.debugDescribeSettings(prefix)}${prefix}decoration:\n${_painter.decoration.toString(prefix + " ")}\n'; String debugDescribeSettings(String prefix) => '${super.debugDescribeSettings(prefix)}${prefix}decoration:\n${_painter.decoration.toString(prefix + " ")}\n';
...@@ -1588,12 +1589,12 @@ class RenderTransform extends RenderProxyBox { ...@@ -1588,12 +1589,12 @@ class RenderTransform extends RenderProxyBox {
return super.hitTest(result, position: transformed); return super.hitTest(result, position: transformed);
} }
void paint(PaintingCanvas canvas, Offset offset) { void paint(PaintingContext context, Offset offset) {
canvas.save(); context.canvas.save();
canvas.translate(offset.dx, offset.dy); context.canvas.translate(offset.dx, offset.dy);
canvas.concat(_transform.storage); context.canvas.concat(_transform.storage);
super.paint(canvas, Offset.zero); super.paint(context, Offset.zero);
canvas.restore(); context.canvas.restore();
} }
void applyPaintTransform(Matrix4 transform) { void applyPaintTransform(Matrix4 transform) {
...@@ -1656,12 +1657,14 @@ class RenderCustomPaint extends RenderProxyBox { ...@@ -1656,12 +1657,14 @@ class RenderCustomPaint extends RenderProxyBox {
super.attach(); super.attach();
} }
void paint(PaintingCanvas canvas, Offset offset) { void paint(PaintingContext context, Offset offset) {
assert(_callback != null); assert(_callback != null);
canvas.translate(offset.dx, offset.dy); context.canvas.translate(offset.dx, offset.dy);
_callback(canvas, size); _callback(context.canvas, size);
super.paint(canvas, Offset.zero); // TODO(abarth): We should translate back before calling super because in
canvas.translate(-offset.dx, -offset.dy); // the future, super.paint might switch our compositing layer.
super.paint(context, Offset.zero);
context.canvas.translate(-offset.dx, -offset.dy);
} }
} }
...@@ -1735,9 +1738,9 @@ class RenderView extends RenderObject with RenderObjectWithChildMixin<RenderBox> ...@@ -1735,9 +1738,9 @@ class RenderView extends RenderObject with RenderObjectWithChildMixin<RenderBox>
return true; return true;
} }
void paint(PaintingCanvas canvas, Offset offset) { void paint(PaintingContext context, Offset offset) {
if (child != null) if (child != null)
canvas.paintChild(child, offset.toPoint()); context.paintChild(child, offset.toPoint());
} }
void paintFrame() { void paintFrame() {
...@@ -1747,9 +1750,10 @@ class RenderView extends RenderObject with RenderObjectWithChildMixin<RenderBox> ...@@ -1747,9 +1750,10 @@ class RenderView extends RenderObject with RenderObjectWithChildMixin<RenderBox>
sky.PictureRecorder recorder = new sky.PictureRecorder(); sky.PictureRecorder recorder = new sky.PictureRecorder();
Rect cullRect = Point.origin & (size * devicePixelRatio); Rect cullRect = Point.origin & (size * devicePixelRatio);
PaintingCanvas canvas = new PaintingCanvas(recorder, cullRect); PaintingCanvas canvas = new PaintingCanvas(recorder, cullRect);
PaintingContext context = new PaintingContext(canvas);
canvas.drawColor(const Color(0xFF000000), sky.TransferMode.src); canvas.drawColor(const Color(0xFF000000), sky.TransferMode.src);
canvas.scale(devicePixelRatio, devicePixelRatio); canvas.scale(devicePixelRatio, devicePixelRatio);
canvas.paintChild(child, Point.origin); context.paintChild(child, Point.origin);
sky.view.picture = recorder.endRecording(); sky.view.picture = recorder.endRecording();
} finally { } finally {
sky.tracing.end('RenderView.paintFrame'); sky.tracing.end('RenderView.paintFrame');
...@@ -1817,11 +1821,11 @@ abstract class RenderBoxContainerDefaultsMixin<ChildType extends RenderBox, Pare ...@@ -1817,11 +1821,11 @@ abstract class RenderBoxContainerDefaultsMixin<ChildType extends RenderBox, Pare
} }
} }
void defaultPaint(PaintingCanvas canvas, Offset offset) { void defaultPaint(PaintingContext context, Offset offset) {
RenderBox child = firstChild; RenderBox child = firstChild;
while (child != null) { while (child != null) {
assert(child.parentData is ParentDataType); assert(child.parentData is ParentDataType);
canvas.paintChild(child, child.parentData.position + offset); context.paintChild(child, child.parentData.position + offset);
child = child.parentData.nextSibling; child = child.parentData.nextSibling;
} }
} }
......
...@@ -449,19 +449,19 @@ class RenderFlex extends RenderBox with ContainerRenderObjectMixin<RenderBox, Fl ...@@ -449,19 +449,19 @@ class RenderFlex extends RenderBox with ContainerRenderObjectMixin<RenderBox, Fl
defaultHitTestChildren(result, position: position); defaultHitTestChildren(result, position: position);
} }
void paint(PaintingCanvas canvas, Offset offset) { void paint(PaintingContext context, Offset offset) {
if (_overflow > 0) { if (_overflow > 0) {
canvas.save(); context.canvas.save();
canvas.clipRect(offset & size); context.canvas.clipRect(offset & size);
defaultPaint(canvas, offset); defaultPaint(context, offset);
canvas.restore(); context.canvas.restore();
} else { } else {
defaultPaint(canvas, offset); defaultPaint(context, offset);
} }
} }
void debugPaintSize(PaintingCanvas canvas, Offset offset) { void debugPaintSize(PaintingContext context, Offset offset) {
super.debugPaintSize(canvas, offset); super.debugPaintSize(context, offset);
if (_overflow <= 0) if (_overflow <= 0)
return; return;
...@@ -479,6 +479,6 @@ class RenderFlex extends RenderBox with ContainerRenderObjectMixin<RenderBox, Fl ...@@ -479,6 +479,6 @@ class RenderFlex extends RenderBox with ContainerRenderObjectMixin<RenderBox, Fl
new Size(size.width, _overflow); new Size(size.width, _overflow);
break; break;
} }
canvas.drawRect(overflowRect, paint); context.canvas.drawRect(overflowRect, paint);
} }
} }
...@@ -29,15 +29,21 @@ class ParentData { ...@@ -29,15 +29,21 @@ class ParentData {
class PaintingCanvas extends sky.Canvas { class PaintingCanvas extends sky.Canvas {
PaintingCanvas(sky.PictureRecorder recorder, Rect bounds) : super(recorder, bounds); PaintingCanvas(sky.PictureRecorder recorder, Rect bounds) : super(recorder, bounds);
}
class PaintingContext {
final PaintingCanvas canvas;
PaintingContext(this.canvas);
List<RenderObject> _descendentsWithPaintingCanvases = new List<RenderObject>(); // used by RenderObject._updatePaintingCanvas() to find out which RenderObjects to ask to paint List<RenderObject> _descendentsWithPaintingCanvases = new List<RenderObject>(); // used by RenderObject._updatePaintingCanvas() to find out which RenderObjects to ask to paint
void paintChild(RenderObject child, Point point) { void paintChild(RenderObject child, Point point) {
if (child.createNewDisplayList) { if (child.createNewDisplayList) {
assert(!_descendentsWithPaintingCanvases.contains(child)); assert(!_descendentsWithPaintingCanvases.contains(child));
_descendentsWithPaintingCanvases.add(child); _descendentsWithPaintingCanvases.add(child);
drawPaintingNode(child._paintingNode, point); canvas.drawPaintingNode(child._paintingNode, point);
} else { } else {
child._paintOnCanvas(this, point.toOffset()); child._paintWithContext(this, point.toOffset());
} }
} }
} }
...@@ -362,10 +368,11 @@ abstract class RenderObject extends AbstractNode implements HitTestTarget { ...@@ -362,10 +368,11 @@ abstract class RenderObject extends AbstractNode implements HitTestTarget {
assert(!_needsLayout); assert(!_needsLayout);
assert(createNewDisplayList); assert(createNewDisplayList);
sky.PictureRecorder recorder = new sky.PictureRecorder(); sky.PictureRecorder recorder = new sky.PictureRecorder();
PaintingCanvas canvas = new PaintingCanvas(recorder, paintBounds); sky.Canvas canvas = new sky.Canvas(recorder, paintBounds);
PaintingContext context = new PaintingContext(canvas);
_needsPaint = false; _needsPaint = false;
try { try {
_paintOnCanvas(canvas, Offset.zero); _paintWithContext(context, Offset.zero);
} catch (e) { } catch (e) {
print('Exception raised during _updatePaintingCanvas:\n${e}\nContext:\n${this}'); print('Exception raised during _updatePaintingCanvas:\n${e}\nContext:\n${this}');
if (inDebugBuild) if (inDebugBuild)
...@@ -375,8 +382,8 @@ abstract class RenderObject extends AbstractNode implements HitTestTarget { ...@@ -375,8 +382,8 @@ abstract class RenderObject extends AbstractNode implements HitTestTarget {
assert(!_needsLayout); // check that the paint() method didn't mark us dirty again assert(!_needsLayout); // check that the paint() method didn't mark us dirty again
assert(!_needsPaint); // check that the paint() method didn't mark us dirty again assert(!_needsPaint); // check that the paint() method didn't mark us dirty again
_paintingNode.setBackingDrawable(recorder.endRecordingAsDrawable()); _paintingNode.setBackingDrawable(recorder.endRecordingAsDrawable());
if (canvas._descendentsWithPaintingCanvases != null) { if (context._descendentsWithPaintingCanvases != null) {
for (RenderObject node in canvas._descendentsWithPaintingCanvases) { for (RenderObject node in context._descendentsWithPaintingCanvases) {
assert(node.attached == attached); assert(node.attached == attached);
if (node._needsPaint) if (node._needsPaint)
node._updatePaintingCanvas(); node._updatePaintingCanvas();
...@@ -384,7 +391,7 @@ abstract class RenderObject extends AbstractNode implements HitTestTarget { ...@@ -384,7 +391,7 @@ abstract class RenderObject extends AbstractNode implements HitTestTarget {
} }
} }
void _paintOnCanvas(PaintingCanvas canvas, Offset offset) { void _paintWithContext(PaintingContext context, Offset offset) {
_needsPaint = false; _needsPaint = false;
assert(!_debugDoingThisPaint); assert(!_debugDoingThisPaint);
RenderObject debugLastActivePaint; RenderObject debugLastActivePaint;
...@@ -392,17 +399,17 @@ abstract class RenderObject extends AbstractNode implements HitTestTarget { ...@@ -392,17 +399,17 @@ abstract class RenderObject extends AbstractNode implements HitTestTarget {
_debugDoingThisPaint = true; _debugDoingThisPaint = true;
debugLastActivePaint = _debugActivePaint; debugLastActivePaint = _debugActivePaint;
_debugActivePaint = this; _debugActivePaint = this;
debugPaint(canvas, offset); debugPaint(context, offset);
if (debugPaintBoundsEnabled) { if (debugPaintBoundsEnabled) {
canvas.save(); context.canvas.save();
canvas.clipRect(paintBounds.shift(offset)); context.canvas.clipRect(paintBounds.shift(offset));
} }
return true; return true;
}); });
paint(canvas, offset); paint(context, offset);
assert(() { assert(() {
if (debugPaintBoundsEnabled) if (debugPaintBoundsEnabled)
canvas.restore(); context.canvas.restore();
_debugActivePaint = debugLastActivePaint; _debugActivePaint = debugLastActivePaint;
_debugDoingThisPaint = false; _debugDoingThisPaint = false;
return true; return true;
...@@ -412,8 +419,8 @@ abstract class RenderObject extends AbstractNode implements HitTestTarget { ...@@ -412,8 +419,8 @@ abstract class RenderObject extends AbstractNode implements HitTestTarget {
bool get createNewDisplayList => false; bool get createNewDisplayList => false;
Rect get paintBounds; Rect get paintBounds;
void debugPaint(PaintingCanvas canvas, Offset offset) { } void debugPaint(PaintingContext context, Offset offset) { }
void paint(PaintingCanvas canvas, Offset offset) { } void paint(PaintingContext context, Offset offset) { }
void applyPaintTransform(Matrix4 transform) { } void applyPaintTransform(Matrix4 transform) { }
......
...@@ -181,7 +181,7 @@ class RenderParagraph extends RenderBox { ...@@ -181,7 +181,7 @@ class RenderParagraph extends RenderBox {
_applyFloatingPointHack(root.height))); _applyFloatingPointHack(root.height)));
} }
void paint(PaintingCanvas canvas, Offset offset) { void paint(PaintingContext context, Offset offset) {
// Ideally we could compute the min/max intrinsic width/height with a // Ideally we could compute the min/max intrinsic width/height with a
// non-destructive operation. However, currently, computing these values // non-destructive operation. However, currently, computing these values
// will destroy state inside the layout root. If that happens, we need to // will destroy state inside the layout root. If that happens, we need to
...@@ -192,9 +192,9 @@ class RenderParagraph extends RenderBox { ...@@ -192,9 +192,9 @@ class RenderParagraph extends RenderBox {
// TODO(ianh): Make LayoutRoot support a paint offset so we don't // TODO(ianh): Make LayoutRoot support a paint offset so we don't
// need to translate for each span of text. // need to translate for each span of text.
_layout(constraints); _layout(constraints);
canvas.translate(offset.dx, offset.dy); context.canvas.translate(offset.dx, offset.dy);
_layoutRoot.paint(canvas); _layoutRoot.paint(context.canvas);
canvas.translate(-offset.dx, -offset.dy); context.canvas.translate(-offset.dx, -offset.dy);
} }
// we should probably expose a way to do precise (inter-glpyh) hit testing // we should probably expose a way to do precise (inter-glpyh) hit testing
......
...@@ -201,14 +201,14 @@ class RenderStack extends RenderBox with ContainerRenderObjectMixin<RenderBox, S ...@@ -201,14 +201,14 @@ class RenderStack extends RenderBox with ContainerRenderObjectMixin<RenderBox, S
defaultHitTestChildren(result, position: position); defaultHitTestChildren(result, position: position);
} }
void paint(PaintingCanvas canvas, Offset offset) { void paint(PaintingContext context, Offset offset) {
if (_hasVisualOverflow) { if (_hasVisualOverflow) {
canvas.save(); context.canvas.save();
canvas.clipRect(offset & size); context.canvas.clipRect(offset & size);
defaultPaint(canvas, offset); defaultPaint(context, offset);
canvas.restore(); context.canvas.restore();
} else { } else {
defaultPaint(canvas, offset); defaultPaint(context, offset);
} }
} }
} }
...@@ -101,7 +101,8 @@ class _RenderCheckbox extends RenderToggleable { ...@@ -101,7 +101,8 @@ class _RenderCheckbox extends RenderToggleable {
markNeedsPaint(); markNeedsPaint();
} }
void paint(PaintingCanvas canvas, Offset offset) { void paint(PaintingContext context, Offset offset) {
final PaintingCanvas canvas = context.canvas;
// Choose a color between grey and the theme color // Choose a color between grey and the theme color
sky.Paint paint = new sky.Paint() sky.Paint paint = new sky.Paint()
..strokeWidth = 2.0 ..strokeWidth = 2.0
......
...@@ -113,8 +113,9 @@ class RenderInkWell extends RenderProxyBox { ...@@ -113,8 +113,9 @@ class RenderInkWell extends RenderProxyBox {
markNeedsPaint(); markNeedsPaint();
} }
void paint(PaintingCanvas canvas, Offset offset) { void paint(PaintingContext context, Offset offset) {
if (!_splashes.isEmpty) { if (!_splashes.isEmpty) {
final PaintingCanvas canvas = context.canvas;
canvas.save(); canvas.save();
canvas.translate(offset.dx, offset.dy); canvas.translate(offset.dx, offset.dy);
canvas.clipRect(Point.origin & size); canvas.clipRect(Point.origin & size);
...@@ -122,7 +123,7 @@ class RenderInkWell extends RenderProxyBox { ...@@ -122,7 +123,7 @@ class RenderInkWell extends RenderProxyBox {
splash.paint(canvas); splash.paint(canvas);
canvas.restore(); canvas.restore();
} }
super.paint(canvas, offset); super.paint(context, offset);
} }
} }
......
...@@ -141,12 +141,12 @@ class RenderScaffold extends RenderBox { ...@@ -141,12 +141,12 @@ class RenderScaffold extends RenderBox {
} }
} }
void paint(PaintingCanvas canvas, Offset offset) { void paint(PaintingContext context, Offset offset) {
for (ScaffoldSlots slot in ScaffoldSlots.values) { for (ScaffoldSlots slot in ScaffoldSlots.values) {
RenderBox box = _slots[slot]; RenderBox box = _slots[slot];
if (box != null) { if (box != null) {
assert(box.parentData is BoxParentData); assert(box.parentData is BoxParentData);
canvas.paintChild(box, box.parentData.position + offset); context.paintChild(box, box.parentData.position + offset);
} }
} }
} }
......
...@@ -117,7 +117,8 @@ class _RenderSwitch extends RenderToggleable { ...@@ -117,7 +117,8 @@ class _RenderSwitch extends RenderToggleable {
_radialReaction = null; _radialReaction = null;
} }
void paint(PaintingCanvas canvas, Offset offset) { void paint(PaintingContext context, Offset offset) {
final PaintingCanvas canvas = context.canvas;
sky.Color thumbColor = _kThumbOffColor; sky.Color thumbColor = _kThumbOffColor;
sky.Color trackColor = _kTrackOffColor; sky.Color trackColor = _kTrackOffColor;
if (value) { if (value) {
......
...@@ -246,21 +246,21 @@ class RenderTabBar extends RenderBox with ...@@ -246,21 +246,21 @@ class RenderTabBar extends RenderBox with
canvas.drawRect(rect, new Paint()..color = indicatorColor); canvas.drawRect(rect, new Paint()..color = indicatorColor);
} }
void paint(PaintingCanvas canvas, Offset offset) { void paint(PaintingContext context, Offset offset) {
if (backgroundColor != null) { if (backgroundColor != null) {
double width = layoutWidths != null double width = layoutWidths != null
? layoutWidths.reduce((sum, width) => sum + width) ? layoutWidths.reduce((sum, width) => sum + width)
: size.width; : size.width;
Rect rect = offset & new Size(width, size.height); Rect rect = offset & new Size(width, size.height);
canvas.drawRect(rect, new Paint()..color = backgroundColor); context.canvas.drawRect(rect, new Paint()..color = backgroundColor);
} }
int index = 0; int index = 0;
RenderBox child = firstChild; RenderBox child = firstChild;
while (child != null) { while (child != null) {
assert(child.parentData is TabBarParentData); assert(child.parentData is TabBarParentData);
canvas.paintChild(child, child.parentData.position + offset); context.paintChild(child, child.parentData.position + offset);
if (index++ == selectedIndex) if (index++ == selectedIndex)
_paintIndicator(canvas, child, offset); _paintIndicator(context.canvas, child, offset);
child = child.parentData.nextSibling; child = child.parentData.nextSibling;
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment