view.dart 4.06 KB
Newer Older
1 2 3 4 5 6
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:sky' as sky;

7
import 'package:sky/animation.dart';
8 9 10
import 'package:sky/src/rendering/layer.dart';
import 'package:sky/src/rendering/object.dart';
import 'package:sky/src/rendering/box.dart';
11
import 'package:vector_math/vector_math_64.dart';
12

13
/// The layout constraints for the root render object
14 15 16 17 18
class ViewConstraints {
  const ViewConstraints({
    this.size: Size.zero,
    this.orientation
  });
19 20

  /// The size of the output surface
21
  final Size size;
22 23

  /// The orientation of the output surface (aspirational)
24 25 26
  final int orientation;
}

27 28 29 30 31
/// The root of the render tree
///
/// The view represents the total output surface of the render tree and handles
/// bootstraping the rendering pipeline. The view has a unique child
/// [RenderBox], which is required to fill the entire output surface.
32 33 34 35 36 37 38 39
class RenderView extends RenderObject with RenderObjectWithChildMixin<RenderBox> {
  RenderView({
    RenderBox child,
    this.timeForRotation: const Duration(microseconds: 83333)
  }) {
    this.child = child;
  }

40
  /// The amount of time the screen rotation animation should last (aspirational)
41 42
  Duration timeForRotation;

43
  /// The current layout size of the view
44
  Size get size => _size;
45
  Size _size = Size.zero;
46

47
  /// The current orientation of the view (aspirational)
48
  int get orientation => _orientation;
49
  int _orientation; // 0..3
50

51
  /// The constraints used for the root layout
52
  ViewConstraints get rootConstraints => _rootConstraints;
53
  ViewConstraints _rootConstraints;
54 55 56 57 58 59 60
  void set rootConstraints(ViewConstraints value) {
    if (_rootConstraints == value)
      return;
    _rootConstraints = value;
    markNeedsLayout();
  }

61 62 63 64 65
  Matrix4 get _logicalToDeviceTransform {
    double devicePixelRatio = sky.view.devicePixelRatio;
    return new Matrix4.diagonal3Values(devicePixelRatio, devicePixelRatio, 1.0);
  }

66
  /// Bootstrap the rendering pipeline by scheduling the first frame
67 68
  void scheduleInitialFrame() {
    scheduleInitialLayout();
69
    scheduleInitialPaint(new TransformLayer(transform: _logicalToDeviceTransform));
70
    scheduler.ensureVisualUpdate();
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
  // We never call layout() on this class, so this should never get
  // checked. (This class is laid out using scheduleInitialLayout().)
  bool debugDoesMeetConstraints() { assert(false); return false; }

  void performResize() {
    assert(false);
  }

  void performLayout() {
    if (_rootConstraints.orientation != _orientation) {
      if (_orientation != null && child != null)
        child.rotate(oldAngle: _orientation, newAngle: _rootConstraints.orientation, time: timeForRotation);
      _orientation = _rootConstraints.orientation;
    }
    _size = _rootConstraints.size;
    assert(!_size.isInfinite);

    if (child != null)
      child.layout(new BoxConstraints.tight(_size));
  }

  void rotate({ int oldAngle, int newAngle, Duration time }) {
    assert(false); // nobody tells the screen to rotate, the whole rotate() dance is started from our performResize()
  }

  bool hitTest(HitTestResult result, { Point position }) {
99 100
    if (child != null)
      child.hitTest(result, position: position);
101 102 103 104
    result.add(new HitTestEntry(this));
    return true;
  }

Hixie's avatar
Hixie committed
105 106
  bool get hasLayer => true;

107 108 109 110 111
  void paint(PaintingContext context, Offset offset) {
    if (child != null)
      context.paintChild(child, offset.toPoint());
  }

112 113 114
  /// Uploads the composited layer tree to the engine
  ///
  /// Actually causes the output of the rendering pipeline to appear on screen.
115 116 117
  void compositeFrame() {
    sky.tracing.begin('RenderView.compositeFrame');
    try {
118
      (layer as TransformLayer).transform = _logicalToDeviceTransform;
119 120 121 122
      Rect bounds = Point.origin & (size * sky.view.devicePixelRatio);
      sky.SceneBuilder builder = new sky.SceneBuilder(bounds);
      layer.addToScene(builder, Offset.zero);
      sky.view.scene = builder.build();
123 124 125 126 127 128 129
    } finally {
      sky.tracing.end('RenderView.compositeFrame');
    }
  }

  Rect get paintBounds => Point.origin & size;
}