Commit 82d1eb2f authored by Ian Fischer's avatar Ian Fischer

Decouple Canvas from DisplayList and map Picture and PictureRecorder more...

Decouple Canvas from DisplayList and map Picture and PictureRecorder more directly to their Skia counterparts.

Also changes the framework dart code to use the
refactored APIs and fixes the various examples and
tests.

R=abarth@chromium.org, ianh@chromium.org

Review URL: https://codereview.chromium.org/1190123003.
parent 9b1985c2
...@@ -5,5 +5,6 @@ import 'dart:math' as Math; ...@@ -5,5 +5,6 @@ import 'dart:math' as Math;
import 'package:vector_math/vector_math_64.dart'; import 'package:vector_math/vector_math_64.dart';
import 'sprites.dart'; import 'sprites.dart';
import 'package:box2d/box2d.dart'; import 'package:box2d/box2d.dart';
import 'package:sky/rendering/object.dart';
part 'game_demo_world.dart'; part 'game_demo_world.dart';
...@@ -372,7 +372,7 @@ class StarField extends Node { ...@@ -372,7 +372,7 @@ class StarField extends Node {
} }
} }
void paint(PictureRecorder canvas) { void paint(RenderCanvas canvas) {
// Setup paint object for opacity and transfer mode // Setup paint object for opacity and transfer mode
Paint paint = new Paint(); Paint paint = new Paint();
paint.setTransferMode(TransferMode.plus); paint.setTransferMode(TransferMode.plus);
......
...@@ -395,7 +395,7 @@ class Node { ...@@ -395,7 +395,7 @@ class Node {
// Rendering // Rendering
void _visit(PictureRecorder canvas) { void _visit(RenderCanvas canvas) {
assert(canvas != null); assert(canvas != null);
if (!visible) return; if (!visible) return;
...@@ -404,7 +404,7 @@ class Node { ...@@ -404,7 +404,7 @@ class Node {
_postPaint(canvas); _postPaint(canvas);
} }
void _prePaint(PictureRecorder canvas) { void _prePaint(RenderCanvas canvas) {
canvas.save(); canvas.save();
// Get the transformation matrix and apply transform // Get the transformation matrix and apply transform
...@@ -419,7 +419,7 @@ class Node { ...@@ -419,7 +419,7 @@ class Node {
/// bounding box's origin, override [NodeWithSize] and call the applyTransformForPivot method before making calls for /// bounding box's origin, override [NodeWithSize] and call the applyTransformForPivot method before making calls for
/// drawing. /// drawing.
/// ///
/// void paint(PictureRecorder canvas) { /// void paint(RenderCanvas canvas) {
/// canvas.save(); /// canvas.save();
/// applyTransformForPivot(canvas); /// applyTransformForPivot(canvas);
/// ///
...@@ -427,10 +427,10 @@ class Node { ...@@ -427,10 +427,10 @@ class Node {
/// ///
/// canvas.restore(); /// canvas.restore();
/// } /// }
void paint(PictureRecorder canvas) { void paint(RenderCanvas canvas) {
} }
void _visitChildren(PictureRecorder canvas) { void _visitChildren(RenderCanvas canvas) {
// Sort children if needed // Sort children if needed
_sortChildren(); _sortChildren();
...@@ -455,7 +455,7 @@ class Node { ...@@ -455,7 +455,7 @@ class Node {
} }
} }
void _postPaint(PictureRecorder canvas) { void _postPaint(RenderCanvas canvas) {
canvas.restore(); canvas.restore();
} }
......
...@@ -32,7 +32,7 @@ abstract class NodeWithSize extends Node { ...@@ -32,7 +32,7 @@ abstract class NodeWithSize extends Node {
/// If you use this method you will need to save and restore your canvas at the beginning and /// If you use this method you will need to save and restore your canvas at the beginning and
/// end of your [paint] method. /// end of your [paint] method.
/// ///
/// void paint(PictureRecorder canvas) { /// void paint(RenderCanvas canvas) {
/// canvas.save(); /// canvas.save();
/// applyTransformForPivot(canvas); /// applyTransformForPivot(canvas);
/// ///
...@@ -40,7 +40,7 @@ abstract class NodeWithSize extends Node { ...@@ -40,7 +40,7 @@ abstract class NodeWithSize extends Node {
/// ///
/// canvas.restore(); /// canvas.restore();
/// } /// }
void applyTransformForPivot(PictureRecorder canvas) { void applyTransformForPivot(RenderCanvas canvas) {
if (pivot.x != 0 || pivot.y != 0) { if (pivot.x != 0 || pivot.y != 0) {
double pivotInPointsX = size.width * pivot.x; double pivotInPointsX = size.width * pivot.x;
double pivotInPointsY = size.height * pivot.y; double pivotInPointsY = size.height * pivot.y;
......
...@@ -64,7 +64,7 @@ class Sprite extends NodeWithSize { ...@@ -64,7 +64,7 @@ class Sprite extends NodeWithSize {
_opacity = opacity; _opacity = opacity;
} }
void paint(PictureRecorder canvas) { void paint(RenderCanvas canvas) {
canvas.save(); canvas.save();
// Account for pivot point // Account for pivot point
......
...@@ -251,7 +251,7 @@ class SpriteBox extends RenderBox { ...@@ -251,7 +251,7 @@ class SpriteBox extends RenderBox {
_rootNode._invalidateToBoxTransformMatrix(); _rootNode._invalidateToBoxTransformMatrix();
} }
void paint(RenderObjectDisplayList canvas) { void paint(RenderCanvas canvas) {
canvas.save(); canvas.save();
// Move to correct coordinate space before drawing // Move to correct coordinate space before drawing
......
...@@ -9,11 +9,12 @@ Picture draw(int a, int r, int g, int b) { ...@@ -9,11 +9,12 @@ Picture draw(int a, int r, int g, int b) {
double width = view.width; double width = view.width;
double height = view.height; double height = view.height;
PictureRecorder recorder = new PictureRecorder(width, height); PictureRecorder recorder = new PictureRecorder();
Canvas canvas = new Canvas(recorder, width, height);
double radius = min(width, height) * 0.45; double radius = min(width, height) * 0.45;
Paint paint = new Paint()..color = new Color.fromARGB(a, r, g, b); Paint paint = new Paint()..color = new Color.fromARGB(a, r, g, b);
recorder.drawCircle(width / 2, height / 2, radius, paint); canvas.drawCircle(width / 2, height / 2, radius, paint);
return recorder.endRecording(); return recorder.endRecording();
} }
......
...@@ -12,11 +12,12 @@ Picture draw(int a, int r, int g, int b) { ...@@ -12,11 +12,12 @@ Picture draw(int a, int r, int g, int b) {
double width = view.width; double width = view.width;
double height = view.height; double height = view.height;
PictureRecorder recorder = new PictureRecorder(width, height); PictureRecorder recorder = new PictureRecorder();
Canvas canvas = new Canvas(recorder, width, height);
double radius = min(width, height) * 0.45; double radius = min(width, height) * 0.45;
Paint paint = new Paint()..color = new Color.fromARGB(a, r, g, b); Paint paint = new Paint()..color = new Color.fromARGB(a, r, g, b);
recorder.drawRect(new Rect.fromSize(new Size(width, height)), paint); canvas.drawRect(new Rect.fromSize(new Size(width, height)), paint);
return recorder.endRecording(); return recorder.endRecording();
} }
......
...@@ -7,28 +7,29 @@ import 'dart:math' as math; ...@@ -7,28 +7,29 @@ import 'dart:math' as math;
import 'dart:typed_data'; import 'dart:typed_data';
void beginFrame(double timeStamp) { void beginFrame(double timeStamp) {
sky.PictureRecorder context = new sky.PictureRecorder(sky.view.width, 200.0); sky.PictureRecorder recorder = new sky.PictureRecorder();
Canvas canvas = new Canvas(recorder, sky.view.width, 200.0);
sky.Paint paint = new sky.Paint(); sky.Paint paint = new sky.Paint();
sky.Point mid = new sky.Point(context.width / 2.0, context.height / 2.0); sky.Point mid = new sky.Point(sky.view.width / 2.0, sky.view.height / 2.0);
double radius = math.min(mid.x, mid.y); double radius = math.min(mid.x, mid.y);
context.drawPaint(new sky.Paint()..color = const sky.Color(0xFFFFFFFF)); canvas.drawPaint(new sky.Paint()..color = const sky.Color(0xFFFFFFFF));
context.save(); canvas.save();
context.translate(-mid.x/2.0, context.height*2.0); canvas.translate(-mid.x/2.0, sky.view.height*2.0);
context.clipRect( canvas.clipRect(
new sky.Rect.fromLTRB(0.0, -context.height, context.width, radius)); new sky.Rect.fromLTRB(0.0, -sky.view.height, sky.view.width, radius));
context.translate(mid.x, mid.y); canvas.translate(mid.x, mid.y);
paint.color = const sky.Color.fromARGB(128, 255, 0, 255); paint.color = const sky.Color.fromARGB(128, 255, 0, 255);
context.rotate(math.PI/4.0); canvas.rotate(math.PI/4.0);
sky.Gradient yellowBlue = new sky.Gradient.linear( sky.Gradient yellowBlue = new sky.Gradient.linear(
[new sky.Point(-radius, -radius), new sky.Point(0.0, 0.0)], [new sky.Point(-radius, -radius), new sky.Point(0.0, 0.0)],
[const sky.Color(0xFFFFFF00), const sky.Color(0xFF0000FF)]); [const sky.Color(0xFFFFFF00), const sky.Color(0xFF0000FF)]);
context.drawRect(new sky.Rect.fromLTRB(-radius, -radius, radius, radius), canvas.drawRect(new sky.Rect.fromLTRB(-radius, -radius, radius, radius),
new sky.Paint()..setShader(yellowBlue)); new sky.Paint()..setShader(yellowBlue));
// Scale x and y by 0.5. // Scale x and y by 0.5.
...@@ -38,13 +39,13 @@ void beginFrame(double timeStamp) { ...@@ -38,13 +39,13 @@ void beginFrame(double timeStamp) {
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0,
]); ]);
context.concat(scaleMatrix); canvas.concat(scaleMatrix);
paint.color = const sky.Color.fromARGB(128, 0, 255, 0); paint.color = const sky.Color.fromARGB(128, 0, 255, 0);
context.drawCircle(0.0, 0.0, radius, paint); canvas.drawCircle(0.0, 0.0, radius, paint);
context.restore(); canvas.restore();
context.translate(0.0, 50.0); canvas.translate(0.0, 50.0);
var builder = new sky.LayerDrawLooperBuilder() var builder = new sky.LayerDrawLooperBuilder()
..addLayerOnTop( ..addLayerOnTop(
new sky.DrawLooperLayerInfo() new sky.DrawLooperLayerInfo()
...@@ -80,9 +81,9 @@ void beginFrame(double timeStamp) { ...@@ -80,9 +81,9 @@ void beginFrame(double timeStamp) {
layerPaint.color = const sky.Color.fromARGB(128, 255, 0, 0); layerPaint.color = const sky.Color.fromARGB(128, 255, 0, 0);
}); });
paint.setDrawLooper(builder.build()); paint.setDrawLooper(builder.build());
context.drawCircle(0.0, 0.0, radius, paint); canvas.drawCircle(0.0, 0.0, radius, paint);
sky.view.picture = context.endRecording(); sky.view.picture = recorder.endRecording();
} }
void main() { void main() {
......
...@@ -6,7 +6,8 @@ import 'dart:sky'; ...@@ -6,7 +6,8 @@ import 'dart:sky';
void beginFrame(double timeStamp) { void beginFrame(double timeStamp) {
var size = 100.0; var size = 100.0;
PictureRecorder canvas = new PictureRecorder(view.width, view.height); PictureRecorder recorder = new PictureRecorder();
Canvas canvas = new Canvas(recorder, view.width, view.height);
canvas.translate(size + 10.0, size + 10.0); canvas.translate(size + 10.0, size + 10.0);
Paint paint = new Paint(); Paint paint = new Paint();
...@@ -30,7 +31,7 @@ void beginFrame(double timeStamp) { ...@@ -30,7 +31,7 @@ void beginFrame(double timeStamp) {
canvas.drawPaint( canvas.drawPaint(
new Paint()..color = const Color.fromARGB(255, 255, 255, 255)); new Paint()..color = const Color.fromARGB(255, 255, 255, 255));
canvas.drawRect(new Rect.fromLTRB(-size, -size, size, size), paint); canvas.drawRect(new Rect.fromLTRB(-size, -size, size, size), paint);
view.picture = canvas.endRecording(); view.picture = recorder.endRecording();
} }
void main() { void main() {
......
...@@ -12,7 +12,8 @@ void beginFrame(double timeStamp) { ...@@ -12,7 +12,8 @@ void beginFrame(double timeStamp) {
if (timeBase == null) if (timeBase == null)
timeBase = timeStamp; timeBase = timeStamp;
double delta = timeStamp - timeBase; double delta = timeStamp - timeBase;
PictureRecorder canvas = new PictureRecorder(view.width, view.height); PictureRecorder recorder = new PictureRecorder();
Canvas canvas = new Canvas(recorder, view.width, view.height);
canvas.translate(view.width / 2.0, view.height / 2.0); canvas.translate(view.width / 2.0, view.height / 2.0);
canvas.rotate(math.PI * delta / 1800); canvas.rotate(math.PI * delta / 1800);
canvas.drawRect(new Rect.fromLTRB(-100.0, -100.0, 100.0, 100.0), canvas.drawRect(new Rect.fromLTRB(-100.0, -100.0, 100.0, 100.0),
...@@ -25,7 +26,7 @@ void beginFrame(double timeStamp) { ...@@ -25,7 +26,7 @@ void beginFrame(double timeStamp) {
canvas.translate(layoutRoot.maxWidth / -2.0, (layoutRoot.maxWidth / 2.0) - 125); canvas.translate(layoutRoot.maxWidth / -2.0, (layoutRoot.maxWidth / 2.0) - 125);
layoutRoot.paint(canvas); layoutRoot.paint(canvas);
view.picture = canvas.endRecording(); view.picture = recorder.endRecording();
view.scheduleFrame(); view.scheduleFrame();
} }
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
import 'dart:math' as math; import 'dart:math' as math;
import 'dart:sky'; import 'dart:sky';
import 'package:sky/mojo/net/image_cache.dart' as image_cache; import 'package:sky/mojo/net/image_cache.dart' as image_cache;
double timeBase = null; double timeBase = null;
...@@ -15,7 +16,8 @@ String url2 = "http://i2.kym-cdn.com/photos/images/facebook/000/581/296/c09.jpg" ...@@ -15,7 +16,8 @@ String url2 = "http://i2.kym-cdn.com/photos/images/facebook/000/581/296/c09.jpg"
void beginFrame(double timeStamp) { void beginFrame(double timeStamp) {
if (timeBase == null) timeBase = timeStamp; if (timeBase == null) timeBase = timeStamp;
double delta = timeStamp - timeBase; double delta = timeStamp - timeBase;
PictureRecorder canvas = new PictureRecorder(view.width, view.height); PictureRecorder recorder = new PictureRecorder();
Canvas canvas = new Canvas(recorder, view.width, view.height);
canvas.translate(view.width / 2.0, view.height / 2.0); canvas.translate(view.width / 2.0, view.height / 2.0);
canvas.rotate(math.PI * delta / 1800); canvas.rotate(math.PI * delta / 1800);
canvas.scale(0.2, 0.2); canvas.scale(0.2, 0.2);
...@@ -36,7 +38,7 @@ void beginFrame(double timeStamp) { ...@@ -36,7 +38,7 @@ void beginFrame(double timeStamp) {
paint); paint);
} }
view.picture = canvas.endRecording(); view.picture = recorder.endRecording();
view.scheduleFrame(); view.scheduleFrame();
} }
......
...@@ -12,12 +12,13 @@ void beginFrame(double timeStamp) { ...@@ -12,12 +12,13 @@ void beginFrame(double timeStamp) {
if (timeBase == null) if (timeBase == null)
timeBase = timeStamp; timeBase = timeStamp;
double delta = timeStamp - timeBase; double delta = timeStamp - timeBase;
PictureRecorder canvas = new PictureRecorder(view.width, view.height); PictureRecorder recorder = new PictureRecorder();
Canvas canvas = new Canvas(recorder, view.width, view.height);
canvas.translate(view.width / 2.0, view.height / 2.0); canvas.translate(view.width / 2.0, view.height / 2.0);
canvas.rotate(math.PI * delta / 1800); canvas.rotate(math.PI * delta / 1800);
canvas.drawRect(new Rect.fromLTRB(-100.0, -100.0, 100.0, 100.0), canvas.drawRect(new Rect.fromLTRB(-100.0, -100.0, 100.0, 100.0),
new Paint()..color = const Color.fromARGB(255, 0, 255, 0)); new Paint()..color = const Color.fromARGB(255, 0, 255, 0));
view.picture = canvas.endRecording(); view.picture = recorder.endRecording();
view.scheduleFrame(); view.scheduleFrame();
tracing.end('beginFrame'); tracing.end('beginFrame');
} }
......
...@@ -116,7 +116,7 @@ abstract class RenderDecoratedSector extends RenderSector { ...@@ -116,7 +116,7 @@ abstract class RenderDecoratedSector extends RenderSector {
} }
// origin must be set to the center of the circle // origin must be set to the center of the circle
void paint(RenderObjectDisplayList canvas) { void paint(RenderCanvas canvas) {
assert(deltaRadius != null); assert(deltaRadius != null);
assert(deltaTheta != null); assert(deltaTheta != null);
assert(parentData is SectorParentData); assert(parentData is SectorParentData);
...@@ -253,7 +253,7 @@ class RenderSectorRing extends RenderSectorWithChildren { ...@@ -253,7 +253,7 @@ class RenderSectorRing extends RenderSectorWithChildren {
// paint origin is 0,0 of our circle // paint origin is 0,0 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(RenderObjectDisplayList canvas) { void paint(RenderCanvas canvas) {
// TODO(ianh): avoid code duplication // TODO(ianh): avoid code duplication
super.paint(canvas); super.paint(canvas);
RenderSector child = firstChild; RenderSector child = firstChild;
...@@ -358,7 +358,7 @@ class RenderSectorSlice extends RenderSectorWithChildren { ...@@ -358,7 +358,7 @@ class RenderSectorSlice extends RenderSectorWithChildren {
// paint origin is 0,0 of our circle // paint origin is 0,0 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(RenderObjectDisplayList canvas) { void paint(RenderCanvas canvas) {
// TODO(ianh): avoid code duplication // TODO(ianh): avoid code duplication
super.paint(canvas); super.paint(canvas);
RenderSector child = firstChild; RenderSector child = firstChild;
...@@ -452,7 +452,7 @@ class RenderBoxToRenderSectorAdapter extends RenderBox { ...@@ -452,7 +452,7 @@ class RenderBoxToRenderSectorAdapter extends RenderBox {
} }
// paint origin is 0,0 of our circle // paint origin is 0,0 of our circle
void paint(RenderObjectDisplayList canvas) { void paint(RenderCanvas canvas) {
super.paint(canvas); super.paint(canvas);
if (child != null) { if (child != null) {
Rect bounds = new Rect.fromSize(size); Rect bounds = new Rect.fromSize(size);
......
...@@ -36,7 +36,7 @@ class Dot { ...@@ -36,7 +36,7 @@ class Dot {
radius = 5 + (95 * event.pressure); radius = 5 + (95 * event.pressure);
} }
void paint(RenderObjectDisplayList canvas) { void paint(RenderCanvas canvas) {
canvas.drawCircle(x, y, radius, _paint); canvas.drawCircle(x, y, radius, _paint);
} }
} }
...@@ -69,7 +69,7 @@ class RenderTouchDemo extends RenderBox { ...@@ -69,7 +69,7 @@ class RenderTouchDemo extends RenderBox {
size = constraints.constrain(Size.infinite); size = constraints.constrain(Size.infinite);
} }
void paint(RenderObjectDisplayList canvas) { void paint(RenderCanvas canvas) {
Paint white = new Paint()..color = const Color(0xFFFFFFFF); Paint white = new Paint()..color = const Color(0xFFFFFFFF);
canvas.drawRect(new Rect.fromSize(size), white); canvas.drawRect(new Rect.fromSize(size), white);
for (Dot dot in dots.values) for (Dot dot in dots.values)
......
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