view.dart 4.77 KB
Newer Older
1 2 3 4
// 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.

5
import 'dart:developer';
6
import 'dart:ui' as ui;
7

8
import 'package:flutter/animation.dart';
9
import 'package:flutter/painting.dart';
10
import 'package:vector_math/vector_math_64.dart';
11

12
import 'box.dart';
13
import 'debug.dart';
14 15 16
import 'layer.dart';
import 'object.dart';

17
/// The layout constraints for the root render object
18 19 20 21 22
class ViewConstraints {
  const ViewConstraints({
    this.size: Size.zero,
    this.orientation
  });
23 24

  /// The size of the output surface
25
  final Size size;
26 27

  /// The orientation of the output surface (aspirational)
28
  final int orientation;
Hixie's avatar
Hixie committed
29 30

  String toString() => '$size';
31 32
}

33 34 35 36 37
/// 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.
38 39 40 41 42 43 44 45
class RenderView extends RenderObject with RenderObjectWithChildMixin<RenderBox> {
  RenderView({
    RenderBox child,
    this.timeForRotation: const Duration(microseconds: 83333)
  }) {
    this.child = child;
  }

46
  /// The amount of time the screen rotation animation should last (aspirational)
47 48
  Duration timeForRotation;

49
  /// The current layout size of the view
50
  Size get size => _size;
51
  Size _size = Size.zero;
52

53
  /// The current orientation of the view (aspirational)
54
  int get orientation => _orientation;
55
  int _orientation; // 0..3
56

57
  /// The constraints used for the root layout
58
  ViewConstraints get rootConstraints => _rootConstraints;
59
  ViewConstraints _rootConstraints;
60
  void set rootConstraints(ViewConstraints value) {
Hixie's avatar
Hixie committed
61
    if (rootConstraints == value)
62 63 64 65 66
      return;
    _rootConstraints = value;
    markNeedsLayout();
  }

67
  Matrix4 get _logicalToDeviceTransform {
68
    double devicePixelRatio = ui.window.devicePixelRatio;
69 70 71
    return new Matrix4.diagonal3Values(devicePixelRatio, devicePixelRatio, 1.0);
  }

72
  /// Bootstrap the rendering pipeline by scheduling the first frame
73 74
  void scheduleInitialFrame() {
    scheduleInitialLayout();
75
    scheduleInitialPaint(new TransformLayer(transform: _logicalToDeviceTransform));
76
    scheduler.ensureVisualUpdate();
77 78
  }

79 80 81 82 83 84 85 86 87
  // 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() {
Hixie's avatar
Hixie committed
88
    if (rootConstraints.orientation != _orientation) {
89
      if (_orientation != null && child != null)
Hixie's avatar
Hixie committed
90 91
        child.rotate(oldAngle: _orientation, newAngle: rootConstraints.orientation, time: timeForRotation);
      _orientation = rootConstraints.orientation;
92
    }
Hixie's avatar
Hixie committed
93
    _size = rootConstraints.size;
94 95 96 97 98 99 100 101 102 103 104
    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 }) {
105 106
    if (child != null)
      child.hitTest(result, position: position);
107 108 109 110
    result.add(new HitTestEntry(this));
    return true;
  }

Hixie's avatar
Hixie committed
111 112
  bool get hasLayer => true;

113 114
  void paint(PaintingContext context, Offset offset) {
    if (child != null)
Adam Barth's avatar
Adam Barth committed
115
      context.paintChild(child, offset);
116 117
  }

118 119 120
  /// Uploads the composited layer tree to the engine
  ///
  /// Actually causes the output of the rendering pipeline to appear on screen.
121
  void compositeFrame() {
122
    Timeline.startSync('Composite');
123
    try {
124
      (layer as TransformLayer).transform = _logicalToDeviceTransform;
125
      Rect bounds = Point.origin & (size * ui.window.devicePixelRatio);
126
      ui.SceneBuilder builder = new ui.SceneBuilder(bounds);
127
      layer.addToScene(builder, Offset.zero);
128 129 130
      ui.Scene scene = builder.build();
      ui.window.render(scene);
      scene.dispose();
131 132
      assert(() {
        if (debugEnableRepaintRainbox)
133
          debugCurrentRepaintColor = debugCurrentRepaintColor.withHue(debugCurrentRepaintColor.hue + debugRepaintRainboxHueIncrement);
134 135
        return true;
      });
136
    } finally {
137
      Timeline.finishSync();
138 139 140 141
    }
  }

  Rect get paintBounds => Point.origin & size;
Hixie's avatar
Hixie committed
142

143 144 145 146 147 148
  void debugDescribeSettings(List<String> settings) {
    // call to ${super.debugDescribeSettings(prefix)} is omitted because the root superclasses don't include any interesting information for this class
    settings.add('window size: ${ui.window.size} (in device pixels)');
    settings.add('device pixel ratio: ${ui.window.devicePixelRatio} (device pixels per logical pixel)');
    settings.add('root constraints: $rootConstraints (in logical pixels)');
  }
149
}