Commit 10fe17a9 authored by Adam Barth's avatar Adam Barth

Merge pull request #809 from abarth/scene_builder

Use sky.SceneBuilder to upload Layer tree to C++
parents 1c983763 0e4ac8c1
...@@ -5,7 +5,6 @@ ...@@ -5,7 +5,6 @@
import 'dart:sky' as sky; import 'dart:sky' as sky;
import 'dart:sky' show Point, Offset, Size, Rect, Color, Paint, Path; import 'dart:sky' show Point, Offset, Size, Rect, Color, Paint, Path;
import 'package:sky/base/debug.dart';
import 'package:vector_math/vector_math.dart'; import 'package:vector_math/vector_math.dart';
abstract class Layer { abstract class Layer {
...@@ -47,12 +46,7 @@ abstract class Layer { ...@@ -47,12 +46,7 @@ abstract class Layer {
_parent = null; _parent = null;
} }
// The paint() methods are temporary. Eventually, Layers won't have void addToScene(sky.SceneBuilder builder, Offset layerOffset);
// a paint() method, the entire Layer hierarchy will be handed over
// to the C++ side for processing. Until we implement that, though,
// we instead have the layers paint themselves into a canvas at
// paint time.
void paint(sky.Canvas canvas);
} }
class PictureLayer extends Layer { class PictureLayer extends Layer {
...@@ -62,24 +56,10 @@ class PictureLayer extends Layer { ...@@ -62,24 +56,10 @@ class PictureLayer extends Layer {
Rect paintBounds; Rect paintBounds;
sky.Picture picture; sky.Picture picture;
bool _debugPaintLayerBorder(sky.Canvas canvas) { void addToScene(sky.SceneBuilder builder, Offset layerOffset) {
if (debugPaintLayerBordersEnabled) { builder.addPicture(offset + layerOffset, picture, paintBounds);
Paint border = new Paint()
..color = debugPaintLayerBordersColor
..strokeWidth = 2.0
..setStyle(sky.PaintingStyle.stroke);
canvas.drawRect(paintBounds, border);
}
return true;
} }
void paint(sky.Canvas canvas) {
assert(picture != null);
canvas.translate(offset.dx, offset.dy);
canvas.drawPicture(picture);
assert(_debugPaintLayerBorder(canvas));
canvas.translate(-offset.dx, -offset.dy);
}
} }
class ContainerLayer extends Layer { class ContainerLayer extends Layer {
...@@ -182,19 +162,18 @@ class ContainerLayer extends Layer { ...@@ -182,19 +162,18 @@ class ContainerLayer extends Layer {
_lastChild = null; _lastChild = null;
} }
void paint(sky.Canvas canvas) { void addToScene(sky.SceneBuilder builder, Offset layerOffset) {
canvas.translate(offset.dx, offset.dy); addChildrenToScene(builder, offset + layerOffset);
paintChildren(canvas);
canvas.translate(-offset.dx, -offset.dy);
} }
void paintChildren(sky.Canvas canvas) { void addChildrenToScene(sky.SceneBuilder builder, Offset layerOffset) {
Layer child = firstChild; Layer child = firstChild;
while (child != null) { while (child != null) {
child.paint(canvas); child.addToScene(builder, layerOffset);
child = child.nextSibling; child = child.nextSibling;
} }
} }
} }
class ClipRectLayer extends ContainerLayer { class ClipRectLayer extends ContainerLayer {
...@@ -203,16 +182,13 @@ class ClipRectLayer extends ContainerLayer { ...@@ -203,16 +182,13 @@ class ClipRectLayer extends ContainerLayer {
// clipRect is _not_ affected by given offset // clipRect is _not_ affected by given offset
Rect clipRect; Rect clipRect;
void paint(sky.Canvas canvas) { void addToScene(sky.SceneBuilder builder, Offset layerOffset) {
canvas.save(); builder.pushClipRect(clipRect.shift(layerOffset));
canvas.clipRect(clipRect); addChildrenToScene(builder, offset + layerOffset);
canvas.translate(offset.dx, offset.dy); builder.pop();
paintChildren(canvas);
canvas.restore();
} }
}
final Paint _disableAntialias = new Paint()..isAntiAlias = false; }
class ClipRRectLayer extends ContainerLayer { class ClipRRectLayer extends ContainerLayer {
ClipRRectLayer({ Offset offset: Offset.zero, this.bounds, this.clipRRect }) : super(offset: offset); ClipRRectLayer({ Offset offset: Offset.zero, this.bounds, this.clipRRect }) : super(offset: offset);
...@@ -221,13 +197,12 @@ class ClipRRectLayer extends ContainerLayer { ...@@ -221,13 +197,12 @@ class ClipRRectLayer extends ContainerLayer {
Rect bounds; Rect bounds;
sky.RRect clipRRect; sky.RRect clipRRect;
void paint(sky.Canvas canvas) { void addToScene(sky.SceneBuilder builder, Offset layerOffset) {
canvas.saveLayer(bounds, _disableAntialias); builder.pushClipRRect(clipRRect.shift(layerOffset), bounds.shift(layerOffset));
canvas.clipRRect(clipRRect); addChildrenToScene(builder, offset + layerOffset);
canvas.translate(offset.dx, offset.dy); builder.pop();
paintChildren(canvas);
canvas.restore();
} }
} }
class ClipPathLayer extends ContainerLayer { class ClipPathLayer extends ContainerLayer {
...@@ -237,13 +212,12 @@ class ClipPathLayer extends ContainerLayer { ...@@ -237,13 +212,12 @@ class ClipPathLayer extends ContainerLayer {
Rect bounds; Rect bounds;
Path clipPath; Path clipPath;
void paint(sky.Canvas canvas) { void addToScene(sky.SceneBuilder builder, Offset layerOffset) {
canvas.saveLayer(bounds, _disableAntialias); builder.pushClipPath(clipPath.shift(layerOffset), bounds.shift(layerOffset));
canvas.clipPath(clipPath); addChildrenToScene(builder, offset + layerOffset);
canvas.translate(offset.dx, offset.dy); builder.pop();
paintChildren(canvas);
canvas.restore();
} }
} }
class TransformLayer extends ContainerLayer { class TransformLayer extends ContainerLayer {
...@@ -251,12 +225,12 @@ class TransformLayer extends ContainerLayer { ...@@ -251,12 +225,12 @@ class TransformLayer extends ContainerLayer {
Matrix4 transform; Matrix4 transform;
void paint(sky.Canvas canvas) { void addToScene(sky.SceneBuilder builder, Offset layerOffset) {
canvas.save(); Matrix4 offsetTransform = new Matrix4.identity();
canvas.translate(offset.dx, offset.dy); offsetTransform.translate(offset.dx + layerOffset.dx, offset.dy + layerOffset.dy);
canvas.concat(transform.storage); builder.pushTransform((offsetTransform * transform).storage);
paintChildren(canvas); addChildrenToScene(builder, Offset.zero);
canvas.restore(); builder.pop();
} }
} }
...@@ -267,18 +241,10 @@ class OpacityLayer extends ContainerLayer { ...@@ -267,18 +241,10 @@ class OpacityLayer extends ContainerLayer {
Rect bounds; Rect bounds;
int alpha; int alpha;
static Paint paintForAlpha(int alpha) { void addToScene(sky.SceneBuilder builder, Offset layerOffset) {
return new Paint() builder.pushOpacity(alpha, bounds == null ? null : bounds.shift(layerOffset));
..color = new Color.fromARGB(alpha, 0, 0, 0) addChildrenToScene(builder, offset + layerOffset);
..setTransferMode(sky.TransferMode.srcOver) builder.pop();
..isAntiAlias = false;
}
void paint(sky.Canvas canvas) {
canvas.saveLayer(bounds, paintForAlpha(alpha));
canvas.translate(offset.dx, offset.dy);
paintChildren(canvas);
canvas.restore();
} }
} }
...@@ -295,16 +261,9 @@ class ColorFilterLayer extends ContainerLayer { ...@@ -295,16 +261,9 @@ class ColorFilterLayer extends ContainerLayer {
Color color; Color color;
sky.TransferMode transferMode; sky.TransferMode transferMode;
static paintForColorFilter(Color color, sky.TransferMode transferMode) { void addToScene(sky.SceneBuilder builder, Offset layerOffset) {
new Paint() builder.pushColorFilter(color, transferMode, bounds.shift(offset));
..setColorFilter(new sky.ColorFilter.mode(color, transferMode)) addChildrenToScene(builder, offset + layerOffset);
..isAntiAlias = false; builder.pop();
}
void paint(sky.Canvas canvas) {
canvas.saveLayer(bounds, paintForColorFilter(color, transferMode));
canvas.translate(offset.dx, offset.dy);
paintChildren(canvas);
canvas.restore();
} }
} }
...@@ -98,13 +98,10 @@ class RenderView extends RenderObject with RenderObjectWithChildMixin<RenderBox> ...@@ -98,13 +98,10 @@ class RenderView extends RenderObject with RenderObjectWithChildMixin<RenderBox>
void compositeFrame() { void compositeFrame() {
sky.tracing.begin('RenderView.compositeFrame'); sky.tracing.begin('RenderView.compositeFrame');
try { try {
// Eventually we will want to pass the entire layer tree to the C++ side. Rect bounds = Point.origin & (size * sky.view.devicePixelRatio);
// For now, however, we take the layer tree and paint it into a Canvas, sky.SceneBuilder builder = new sky.SceneBuilder(bounds);
// which we then hand to the C++ side. layer.addToScene(builder, Offset.zero);
sky.PictureRecorder recorder = new sky.PictureRecorder(); sky.view.scene = builder.build();
sky.Canvas canvas = new sky.Canvas(recorder, Point.origin & (size * sky.view.devicePixelRatio));
layer.paint(canvas);
sky.view.picture = recorder.endRecording();
} finally { } finally {
sky.tracing.end('RenderView.compositeFrame'); sky.tracing.end('RenderView.compositeFrame');
} }
......
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