display_list.dart 3.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145

import 'package:sky/framework/rendering/render_node.dart';
import 'package:sky/framework/rendering/render_box.dart';
import 'dart:sky' as sky;

typedef void Logger (String s);

class TestDisplayList extends RenderNodeDisplayList {
  TestDisplayList(double width, double height, this.logger, { this.indent: '' }) :
    this.width = width,
    this.height = height,
    super(width, height) {
    log("TestDisplayList() constructor: $width x $height");
  }

  final String indent;
  final double width;
  final double height;

  Logger logger;
  void log(String s) {
    logger("${indent} ${s}");
  }

  String explainPaint(sky.Paint paint) {
    return "Paint(0x${paint.color.toRadixString(16).padLeft(8, '0')})";
  }

  void save() {
    log("save");
  }

  void saveLayer(sky.Rect bounds, sky.Paint paint) {
    log("saveLayer(${bounds.top}:${bounds.left}:${bounds.bottom}:${bounds.right}, ${explainPaint(paint)})");
  }

  void restore() {
    log("restore");
  }

  void translate(double dx, double dy) {
    log("translate($dx, $dy)");
  }

  void scale(double sx, double sy) {
    log("scale($sx, $sy)");
  }

  void rotateDegrees(double degrees) {
    log("rotateDegrees($degrees)");
  }

  void skew(double sx, double sy) {
    log("skew($sx, $sy)");
  }

  void concat(List<double> matrix9) {
    log("concat($matrix9)");
  }

  void clipRect(sky.Rect rect) {
    log("clipRect(${rect.top}:${rect.left}:${rect.bottom}:${rect.right})");
  }

  void drawPicture(sky.Picture picture) {
    log("drawPicture()");
  }

  void drawPaint(sky.Paint paint) {
    log("drawPaint(${explainPaint(paint)})");
  }

  void drawRect(sky.Rect rect, sky.Paint paint) {
    log("drawRect(${rect.top}:${rect.left}:${rect.bottom}:${rect.right}, ${explainPaint(paint)})");
  }

  void drawOval(sky.Rect rect, sky.Paint paint) {
    log("drawOval(${rect.top}:${rect.left}:${rect.bottom}:${rect.right}, ${explainPaint(paint)})");
  }

  void drawCircle(double x, double y, double radius, sky.Paint paint) {
    log("drawCircle($x, $y, $radius, ${explainPaint(paint)})");
  }

  void drawPath(sky.Path path, sky.Paint paint) {
    log("drawPath(Path, ${explainPaint(paint)})");
  }

  void paintChild(RenderNode child, sky.Point position) {
    log("paintChild at ${position.x},${position.y}");
    child.paint(new TestDisplayList(width, height, logger, indent: "$indent  |"));
  }
}

class TestView extends RenderView {

  TestView({
    RenderBox child,
    Duration timeForRotation
  }) : super(child: child, timeForRotation: timeForRotation) {
    print("TestView enabled");
  }

  int frame = 0;

  String lastPaint = '';
  void log(String s) {
    lastPaint += "\n$s";
  }

  void paintFrame() {
    RenderNode.debugDoingPaint = true;
    frame += 1;
    log("PAINT FOR FRAME #${frame} ----------------------------------------------");
    var canvas = new TestDisplayList(sky.view.width, sky.view.height, log, indent: "${frame} |");
    paint(canvas);
    log("------------------------------------------------------------------------");
    RenderNode.debugDoingPaint = false;
  }

}

class TestApp {

  TestApp(RenderBox root) {
    _renderView = new TestView(child: root);
    _renderView.attach();
    _renderView.layout(new ViewConstraints(width: sky.view.width, height: sky.view.height));
    _renderView.paintFrame();
    print(_renderView.lastPaint); // TODO(ianh): figure out how to make this fit the unit testing framework better
  }

  RenderView _renderView;

  RenderBox get root => _renderView.child;
  void set root(RenderBox value) {
    _renderView.child = value;
  }
  void _beginFrame(double timeStamp) {
    RenderNode.flushLayout();
    _renderView.paintFrame();
    print(_renderView.lastPaint); // TODO(ianh): figure out how to make this fit the unit testing framework better
  }

}