view.dart 7.55 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:io' show Platform;
7
import 'dart:ui' as ui show Scene, SceneBuilder, window;
8

9
import 'package:flutter/foundation.dart';
10
import 'package:vector_math/vector_math_64.dart';
11

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

18
/// The layout constraints for the root render object.
19
@immutable
20
class ViewConfiguration {
21 22 23
  /// Creates a view configuration.
  ///
  /// By default, the view has zero [size] and a [devicePixelRatio] of 1.0.
24
  const ViewConfiguration({
25
    this.size: Size.zero,
26
    this.devicePixelRatio: 1.0,
27 28
    this.orientation
  });
29

30
  /// The size of the output surface.
31
  final Size size;
32

33 34 35
  /// The pixel density of the output surface.
  final double devicePixelRatio;

36
  /// The orientation of the output surface (aspirational).
37
  final int orientation;
Hixie's avatar
Hixie committed
38

39 40 41 42 43
  /// Creates a transformation matrix that applies the [devicePixelRatio].
  Matrix4 toMatrix() {
    return new Matrix4.diagonal3Values(devicePixelRatio, devicePixelRatio, 1.0);
  }

44
  @override
45
  String toString() => '$size at ${devicePixelRatio}x';
46 47
}

48
/// The root of the render tree.
49 50
///
/// The view represents the total output surface of the render tree and handles
51
/// bootstrapping the rendering pipeline. The view has a unique child
52
/// [RenderBox], which is required to fill the entire output surface.
53
class RenderView extends RenderObject with RenderObjectWithChildMixin<RenderBox> {
54 55 56
  /// Creates the root of the render tree.
  ///
  /// Typically created by the binding (e.g., [RendererBinding]).
57 58
  ///
  /// The [configuration] must not be null.
59 60
  RenderView({
    RenderBox child,
61
    this.timeForRotation: const Duration(microseconds: 83333),
62 63 64
    @required ViewConfiguration configuration,
  }) : assert(configuration != null),
       _configuration = configuration {
65 66 67
    this.child = child;
  }

68
  /// The amount of time the screen rotation animation should last (aspirational).
69 70
  Duration timeForRotation;

71
  /// The current layout size of the view.
72
  Size get size => _size;
73
  Size _size = Size.zero;
74

75
  /// The current orientation of the view (aspirational).
76
  int get orientation => _orientation;
77
  int _orientation; // 0..3
78

79
  /// The constraints used for the root layout.
80 81
  ViewConfiguration get configuration => _configuration;
  ViewConfiguration _configuration;
82 83 84 85
  /// The configuration is initially set by the `configuration` argument
  /// passed to the constructor.
  ///
  /// Always call [scheduleInitialFrame] before changing the configuration.
86
  set configuration(ViewConfiguration value) {
87
    assert(value != null);
88
    if (configuration == value)
89
      return;
90
    _configuration = value;
91 92
    replaceRootLayer(_updateMatricesAndCreateNewRootLayer());
    assert(_rootTransform != null);
93 94 95
    markNeedsLayout();
  }

96
  /// Bootstrap the rendering pipeline by scheduling the first frame.
97 98 99 100
  ///
  /// This should only be called once, and must be called before changing
  /// [configuration]. It is typically called immediately after calling the
  /// constructor.
101
  void scheduleInitialFrame() {
102
    assert(owner != null);
103
    assert(_rootTransform == null);
104
    scheduleInitialLayout();
105 106
    scheduleInitialPaint(_updateMatricesAndCreateNewRootLayer());
    assert(_rootTransform != null);
107
    owner.requestVisualUpdate();
108 109
  }

110 111 112 113 114 115 116 117 118 119
  Matrix4 _rootTransform;

  Layer _updateMatricesAndCreateNewRootLayer() {
    _rootTransform = configuration.toMatrix();
    final ContainerLayer rootLayer = new TransformLayer(transform: _rootTransform);
    rootLayer.attach(this);
    assert(_rootTransform != null);
    return rootLayer;
  }

120 121
  // We never call layout() on this class, so this should never get
  // checked. (This class is laid out using scheduleInitialLayout().)
122
  @override
123
  void debugAssertDoesMeetConstraints() { assert(false); }
124

125
  @override
126 127 128 129
  void performResize() {
    assert(false);
  }

130
  @override
131
  void performLayout() {
132
    assert(_rootTransform != null);
133
    if (configuration.orientation != _orientation) {
134
      if (_orientation != null && child != null)
135 136
        child.rotate(oldAngle: _orientation, newAngle: configuration.orientation, time: timeForRotation);
      _orientation = configuration.orientation;
137
    }
138
    _size = configuration.size;
139
    assert(_size.isFinite);
140 141 142 143 144

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

145
  @override
146 147 148 149
  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()
  }

150 151 152 153 154 155
  /// Determines the set of render objects located at the given position.
  ///
  /// Returns true if the given point is contained in this render object or one
  /// of its descendants. Adds any render objects that contain the point to the
  /// given hit test result.
  ///
156 157 158 159
  /// The [position] argument is in the coordinate system of the render view,
  /// which is to say, in logical pixels. This is not necessarily the same
  /// coordinate system as that expected by the root [Layer], which will
  /// normally be in physical (device) pixels.
160
  bool hitTest(HitTestResult result, { Offset position }) {
161 162
    if (child != null)
      child.hitTest(result, position: position);
163 164 165 166
    result.add(new HitTestEntry(this));
    return true;
  }

167
  @override
168
  bool get isRepaintBoundary => true;
Hixie's avatar
Hixie committed
169

170
  @override
171 172
  void paint(PaintingContext context, Offset offset) {
    if (child != null)
Adam Barth's avatar
Adam Barth committed
173
      context.paintChild(child, offset);
174 175
  }

176 177 178 179 180 181 182
  @override
  void applyPaintTransform(RenderBox child, Matrix4 transform) {
    assert(_rootTransform != null);
    transform.multiply(_rootTransform);
    super.applyPaintTransform(child, transform);
  }

183
  /// Uploads the composited layer tree to the engine.
184 185
  ///
  /// Actually causes the output of the rendering pipeline to appear on screen.
186
  void compositeFrame() {
187
    Timeline.startSync('Compositing', arguments: timelineWhitelistArguments);
188
    try {
189
      final ui.SceneBuilder builder = new ui.SceneBuilder();
190
      layer.addToScene(builder, Offset.zero);
191
      final ui.Scene scene = builder.build();
192 193
      ui.window.render(scene);
      scene.dispose();
194
      assert(() {
195
        if (debugRepaintRainbowEnabled || debugRepaintTextRainbowEnabled)
196
          debugCurrentRepaintColor = debugCurrentRepaintColor.withHue(debugCurrentRepaintColor.hue + 2.0);
197
        return true;
198
      }());
199
    } finally {
200
      Timeline.finishSync();
201 202 203
    }
  }

204
  @override
205
  Rect get paintBounds => Offset.zero & size;
206 207

  @override
208 209 210 211
  Rect get semanticBounds {
    assert(_rootTransform != null);
    return MatrixUtils.transformRect(_rootTransform, Offset.zero & size);
  }
Hixie's avatar
Hixie committed
212

213
  @override
214
  void debugFillProperties(DiagnosticPropertiesBuilder description) {
215 216 217
    // call to ${super.debugFillProperties(description)} is omitted because the
    // root superclasses don't include any interesting information for this
    // class
218
    assert(() {
219
      description.add(new DiagnosticsNode.message('debug mode enabled - ${Platform.operatingSystem}'));
220
      return true;
221
    }());
222 223 224
    description.add(new DiagnosticsProperty<Size>('window size', ui.window.physicalSize, tooltip: 'in physical pixels'));
    description.add(new DoubleProperty('device pixel ratio', ui.window.devicePixelRatio, tooltip: 'physical pixels per logical pixel'));
    description.add(new DiagnosticsProperty<ViewConfiguration>('configuration', configuration, tooltip: 'in logical pixels'));
225
    if (ui.window.semanticsEnabled)
226
      description.add(new DiagnosticsNode.message('semantics enabled'));
227
  }
228
}