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

Commit 26203b6

Browse files
committed
Merge pull request #711 from abarth/rm_paint_layer
Split up paintChildWithPaint into paintChildWithOpacity and paintChildWithColorFilter
2 parents 5e6d425 + c89d751 commit 26203b6

File tree

3 files changed

+55
-39
lines changed

3 files changed

+55
-39
lines changed

sky/packages/sky/lib/rendering/layer.dart

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -247,15 +247,22 @@ class TransformLayer extends ContainerLayer {
247247
}
248248
}
249249

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

253253
// bounds is _not_ affected by given offset
254254
Rect bounds;
255-
Paint paintSettings; // TODO(ianh): rename this to 'paint' once paint() is gone
255+
int alpha;
256+
257+
static Paint paintForAlpha(int alpha) {
258+
return new Paint()
259+
..color = new Color.fromARGB(alpha, 0, 0, 0)
260+
..setTransferMode(sky.TransferMode.srcOver)
261+
..isAntiAlias = false;
262+
}
256263

257264
void paint(sky.Canvas canvas) {
258-
canvas.saveLayer(bounds, paintSettings);
265+
canvas.saveLayer(bounds, paintForAlpha(alpha));
259266
canvas.translate(offset.dx, offset.dy);
260267
paintChildren(canvas);
261268
canvas.restore();
@@ -265,20 +272,24 @@ class PaintLayer extends ContainerLayer {
265272
class ColorFilterLayer extends ContainerLayer {
266273
ColorFilterLayer({
267274
Offset offset: Offset.zero,
268-
this.size,
275+
this.bounds,
269276
this.color,
270277
this.transferMode
271278
}) : super(offset: offset);
272279

273-
Size size;
280+
// bounds is _not_ affected by given offset
281+
Rect bounds;
274282
Color color;
275283
sky.TransferMode transferMode;
276284

285+
static paintForColorFilter(Color color, sky.TransferMode transferMode) {
286+
new Paint()
287+
..setColorFilter(new sky.ColorFilter.mode(color, transferMode))
288+
..isAntiAlias = false;
289+
}
290+
277291
void paint(sky.Canvas canvas) {
278-
Paint paint = new Paint()
279-
..color = color
280-
..setTransferMode(transferMode);
281-
canvas.saveLayer(offset & size, paint);
292+
canvas.saveLayer(bounds, paintForColorFilter(color, transferMode));
282293
canvas.translate(offset.dx, offset.dy);
283294
paintChildren(canvas);
284295
canvas.restore();

sky/packages/sky/lib/rendering/object.dart

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,16 +188,45 @@ class PaintingContext {
188188
}
189189
}
190190

191-
void paintChildWithPaint(RenderObject child, Point childPosition, Rect bounds, Paint paint) {
191+
void paintChildWithOpacity(RenderObject child,
192+
Point childPosition,
193+
Rect bounds,
194+
int alpha) {
192195
assert(debugCanPaintChild(child));
193196
final Offset childOffset = childPosition.toOffset();
194197
if (!child.needsCompositing) {
195-
canvas.saveLayer(bounds, paint);
198+
canvas.saveLayer(bounds, OpacityLayer.paintForAlpha(alpha));
196199
canvas.translate(childOffset.dx, childOffset.dy);
197200
insertChild(child, Offset.zero);
198201
canvas.restore();
199202
} else {
200-
PaintLayer paintLayer = new PaintLayer(offset: childOffset, bounds: bounds, paintSettings: paint);
203+
OpacityLayer paintLayer = new OpacityLayer(
204+
offset: childOffset,
205+
bounds: bounds,
206+
alpha: alpha);
207+
_containerLayer.add(paintLayer);
208+
compositeChild(child, parentLayer: paintLayer);
209+
}
210+
}
211+
212+
void paintChildWithColorFilter(RenderObject child,
213+
Point childPosition,
214+
Rect bounds,
215+
Color color,
216+
sky.TransferMode transferMode) {
217+
assert(debugCanPaintChild(child));
218+
final Offset childOffset = childPosition.toOffset();
219+
if (!child.needsCompositing) {
220+
canvas.saveLayer(bounds, ColorFilterLayer.paintForColorFilter(color, transferMode));
221+
canvas.translate(childOffset.dx, childOffset.dy);
222+
insertChild(child, Offset.zero);
223+
canvas.restore();
224+
} else {
225+
ColorFilterLayer paintLayer = new ColorFilterLayer(
226+
offset: childOffset,
227+
bounds: bounds,
228+
color: color,
229+
transferMode: transferMode);
201230
_containerLayer.add(paintLayer);
202231
compositeChild(child, parentLayer: paintLayer);
203232
}

sky/packages/sky/lib/rendering/proxy_box.dart

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -275,23 +275,11 @@ class RenderOpacity extends RenderProxyBox {
275275
if (_opacity == value)
276276
return;
277277
_opacity = value;
278-
_cachedPaint = null;
279278
markNeedsPaint();
280279
}
281280

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

284-
Paint _cachedPaint;
285-
Paint get _paint {
286-
if (_cachedPaint == null) {
287-
_cachedPaint = new Paint()
288-
..color = new Color.fromARGB(_alpha, 0, 0, 0)
289-
..setTransferMode(sky.TransferMode.srcOver)
290-
..isAntiAlias = false;
291-
}
292-
return _cachedPaint;
293-
}
294-
295283
void paint(PaintingContext context, Offset offset) {
296284
if (child != null) {
297285
int a = _alpha;
@@ -300,7 +288,7 @@ class RenderOpacity extends RenderProxyBox {
300288
if (a == 255)
301289
context.paintChild(child, offset.toPoint());
302290
else
303-
context.paintChildWithPaint(child, offset.toPoint(), null, _paint);
291+
context.paintChildWithOpacity(child, offset.toPoint(), null, a);
304292
}
305293
}
306294
}
@@ -317,7 +305,6 @@ class RenderColorFilter extends RenderProxyBox {
317305
if (_color == value)
318306
return;
319307
_color = value;
320-
_cachedPaint = null;
321308
markNeedsPaint();
322309
}
323310

@@ -328,23 +315,12 @@ class RenderColorFilter extends RenderProxyBox {
328315
if (_transferMode == value)
329316
return;
330317
_transferMode = value;
331-
_cachedPaint = null;
332318
markNeedsPaint();
333319
}
334320

335-
Paint _cachedPaint;
336-
Paint get _paint {
337-
if (_cachedPaint == null) {
338-
_cachedPaint = new Paint()
339-
..setColorFilter(new sky.ColorFilter.mode(_color, _transferMode))
340-
..isAntiAlias = false;
341-
}
342-
return _cachedPaint;
343-
}
344-
345321
void paint(PaintingContext context, Offset offset) {
346322
if (child != null)
347-
context.paintChildWithPaint(child, offset.toPoint(), offset & size, _paint);
323+
context.paintChildWithColorFilter(child, offset.toPoint(), offset & size, _color, _transferMode);
348324
}
349325
}
350326

0 commit comments

Comments
 (0)