view.dart 5.92 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:vector_math/vector_math_64.dart';
10

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

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

28
  /// The size of the output surface.
29
  final Size size;
30

31 32 33
  /// The pixel density of the output surface.
  final double devicePixelRatio;

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

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

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

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

63
  /// The amount of time the screen rotation animation should last (aspirational).
64 65
  Duration timeForRotation;

66
  /// The current layout size of the view.
67
  Size get size => _size;
68
  Size _size = Size.zero;
69

70
  /// The current orientation of the view (aspirational).
71
  int get orientation => _orientation;
72
  int _orientation; // 0..3
73

74
  /// The constraints used for the root layout.
75 76
  ViewConfiguration get configuration => _configuration;
  ViewConfiguration _configuration;
77
  set configuration(ViewConfiguration value) {
78
    if (configuration == value)
79
      return;
80
    _configuration = value;
81
    replaceRootLayer(new TransformLayer(transform: configuration.toMatrix()));
82 83 84
    markNeedsLayout();
  }

85
  /// Bootstrap the rendering pipeline by scheduling the first frame.
86
  void scheduleInitialFrame() {
87
    assert(owner != null);
88
    scheduleInitialLayout();
89
    scheduleInitialPaint(new TransformLayer(transform: configuration.toMatrix()));
90
    owner.requestVisualUpdate();
91 92
  }

93 94
  // We never call layout() on this class, so this should never get
  // checked. (This class is laid out using scheduleInitialLayout().)
95
  @override
96
  void debugAssertDoesMeetConstraints() { assert(false); }
97

98
  @override
99 100 101 102
  void performResize() {
    assert(false);
  }

103
  @override
104
  void performLayout() {
105
    if (configuration.orientation != _orientation) {
106
      if (_orientation != null && child != null)
107 108
        child.rotate(oldAngle: _orientation, newAngle: configuration.orientation, time: timeForRotation);
      _orientation = configuration.orientation;
109
    }
110
    _size = configuration.size;
111 112 113 114 115 116
    assert(!_size.isInfinite);

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

117
  @override
118 119 120 121
  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()
  }

122 123 124 125 126 127 128
  /// 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.
  ///
  /// The [position] argument is in the coordinate system of the render view.
129
  bool hitTest(HitTestResult result, { Point position }) {
130 131
    if (child != null)
      child.hitTest(result, position: position);
132 133 134 135
    result.add(new HitTestEntry(this));
    return true;
  }

136
  @override
137
  bool get isRepaintBoundary => true;
Hixie's avatar
Hixie committed
138

139
  @override
140 141
  void paint(PaintingContext context, Offset offset) {
    if (child != null)
Adam Barth's avatar
Adam Barth committed
142
      context.paintChild(child, offset);
143 144
  }

145
  /// Uploads the composited layer tree to the engine.
146 147
  ///
  /// Actually causes the output of the rendering pipeline to appear on screen.
148
  void compositeFrame() {
149
    Timeline.startSync('Compositing');
150
    try {
151
      ui.SceneBuilder builder = new ui.SceneBuilder();
152
      layer.addToScene(builder, Offset.zero);
153 154 155
      ui.Scene scene = builder.build();
      ui.window.render(scene);
      scene.dispose();
156
      assert(() {
157
        if (debugRepaintRainbowEnabled || debugRepaintTextRainbowEnabled)
158
          debugCurrentRepaintColor = debugCurrentRepaintColor.withHue(debugCurrentRepaintColor.hue + debugRepaintRainbowHueIncrement);
159 160
        return true;
      });
161
    } finally {
162
      Timeline.finishSync();
163 164 165
    }
  }

166
  @override
167
  Rect get paintBounds => Point.origin & size;
168 169

  @override
Hixie's avatar
Hixie committed
170
  Rect get semanticBounds => Point.origin & size;
Hixie's avatar
Hixie committed
171

172
  @override
173 174
  void debugFillDescription(List<String> description) {
    // call to ${super.debugFillDescription(prefix)} is omitted because the root superclasses don't include any interesting information for this class
175 176 177 178
    assert(() {
      description.add('debug mode enabled - ${Platform.operatingSystem}');
      return true;
    });
179 180
    description.add('window size: ${ui.window.physicalSize} (in physical pixels)');
    description.add('device pixel ratio: ${ui.window.devicePixelRatio} (physical pixels per logical pixel)');
181
    description.add('configuration: $configuration (in logical pixels)');
182 183
    if (ui.window.semanticsEnabled)
      description.add('semantics enabled');
184
  }
185
}