Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Split up paintChildWithPaint into paintChildWithOpacity and paintChildWithColorFilter #711

Merged
merged 1 commit into from
Aug 20, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 21 additions & 10 deletions sky/packages/sky/lib/rendering/layer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -247,15 +247,22 @@ class TransformLayer extends ContainerLayer {
}
}

class PaintLayer extends ContainerLayer {
PaintLayer({ Offset offset: Offset.zero, this.bounds, this.paintSettings }) : super(offset: offset);
class OpacityLayer extends ContainerLayer {
OpacityLayer({ Offset offset: Offset.zero, this.bounds, this.alpha }) : super(offset: offset);

// bounds is _not_ affected by given offset
Rect bounds;
Paint paintSettings; // TODO(ianh): rename this to 'paint' once paint() is gone
int alpha;

static Paint paintForAlpha(int alpha) {
return new Paint()
..color = new Color.fromARGB(alpha, 0, 0, 0)
..setTransferMode(sky.TransferMode.srcOver)
..isAntiAlias = false;
}

void paint(sky.Canvas canvas) {
canvas.saveLayer(bounds, paintSettings);
canvas.saveLayer(bounds, paintForAlpha(alpha));
canvas.translate(offset.dx, offset.dy);
paintChildren(canvas);
canvas.restore();
Expand All @@ -265,20 +272,24 @@ class PaintLayer extends ContainerLayer {
class ColorFilterLayer extends ContainerLayer {
ColorFilterLayer({
Offset offset: Offset.zero,
this.size,
this.bounds,
this.color,
this.transferMode
}) : super(offset: offset);

Size size;
// bounds is _not_ affected by given offset
Rect bounds;
Color color;
sky.TransferMode transferMode;

static paintForColorFilter(Color color, sky.TransferMode transferMode) {
new Paint()
..setColorFilter(new sky.ColorFilter.mode(color, transferMode))
..isAntiAlias = false;
}

void paint(sky.Canvas canvas) {
Paint paint = new Paint()
..color = color
..setTransferMode(transferMode);
canvas.saveLayer(offset & size, paint);
canvas.saveLayer(bounds, paintForColorFilter(color, transferMode));
canvas.translate(offset.dx, offset.dy);
paintChildren(canvas);
canvas.restore();
Expand Down
35 changes: 32 additions & 3 deletions sky/packages/sky/lib/rendering/object.dart
Original file line number Diff line number Diff line change
Expand Up @@ -188,16 +188,45 @@ class PaintingContext {
}
}

void paintChildWithPaint(RenderObject child, Point childPosition, Rect bounds, Paint paint) {
void paintChildWithOpacity(RenderObject child,
Point childPosition,
Rect bounds,
int alpha) {
assert(debugCanPaintChild(child));
final Offset childOffset = childPosition.toOffset();
if (!child.needsCompositing) {
canvas.saveLayer(bounds, paint);
canvas.saveLayer(bounds, OpacityLayer.paintForAlpha(alpha));
canvas.translate(childOffset.dx, childOffset.dy);
insertChild(child, Offset.zero);
canvas.restore();
} else {
PaintLayer paintLayer = new PaintLayer(offset: childOffset, bounds: bounds, paintSettings: paint);
OpacityLayer paintLayer = new OpacityLayer(
offset: childOffset,
bounds: bounds,
alpha: alpha);
_containerLayer.add(paintLayer);
compositeChild(child, parentLayer: paintLayer);
}
}

void paintChildWithColorFilter(RenderObject child,
Point childPosition,
Rect bounds,
Color color,
sky.TransferMode transferMode) {
assert(debugCanPaintChild(child));
final Offset childOffset = childPosition.toOffset();
if (!child.needsCompositing) {
canvas.saveLayer(bounds, ColorFilterLayer.paintForColorFilter(color, transferMode));
canvas.translate(childOffset.dx, childOffset.dy);
insertChild(child, Offset.zero);
canvas.restore();
} else {
ColorFilterLayer paintLayer = new ColorFilterLayer(
offset: childOffset,
bounds: bounds,
color: color,
transferMode: transferMode);
_containerLayer.add(paintLayer);
compositeChild(child, parentLayer: paintLayer);
}
Expand Down
28 changes: 2 additions & 26 deletions sky/packages/sky/lib/rendering/proxy_box.dart
Original file line number Diff line number Diff line change
Expand Up @@ -275,23 +275,11 @@ class RenderOpacity extends RenderProxyBox {
if (_opacity == value)
return;
_opacity = value;
_cachedPaint = null;
markNeedsPaint();
}

int get _alpha => (_opacity * 255).round();

Paint _cachedPaint;
Paint get _paint {
if (_cachedPaint == null) {
_cachedPaint = new Paint()
..color = new Color.fromARGB(_alpha, 0, 0, 0)
..setTransferMode(sky.TransferMode.srcOver)
..isAntiAlias = false;
}
return _cachedPaint;
}

void paint(PaintingContext context, Offset offset) {
if (child != null) {
int a = _alpha;
Expand All @@ -300,7 +288,7 @@ class RenderOpacity extends RenderProxyBox {
if (a == 255)
context.paintChild(child, offset.toPoint());
else
context.paintChildWithPaint(child, offset.toPoint(), null, _paint);
context.paintChildWithOpacity(child, offset.toPoint(), null, a);
}
}
}
Expand All @@ -317,7 +305,6 @@ class RenderColorFilter extends RenderProxyBox {
if (_color == value)
return;
_color = value;
_cachedPaint = null;
markNeedsPaint();
}

Expand All @@ -328,23 +315,12 @@ class RenderColorFilter extends RenderProxyBox {
if (_transferMode == value)
return;
_transferMode = value;
_cachedPaint = null;
markNeedsPaint();
}

Paint _cachedPaint;
Paint get _paint {
if (_cachedPaint == null) {
_cachedPaint = new Paint()
..setColorFilter(new sky.ColorFilter.mode(_color, _transferMode))
..isAntiAlias = false;
}
return _cachedPaint;
}

void paint(PaintingContext context, Offset offset) {
if (child != null)
context.paintChildWithPaint(child, offset.toPoint(), offset & size, _paint);
context.paintChildWithColorFilter(child, offset.toPoint(), offset & size, _color, _transferMode);
}
}

Expand Down