view.dart 4.96 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 show Scene, SceneBuilder, window;
7

8
import 'package:flutter/scheduler.dart';
9
import 'package:vector_math/vector_math_64.dart';
10

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

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

23
  /// The size of the output surface.
24
  final Size size;
25

26
  /// The orientation of the output surface (aspirational).
27
  final int orientation;
Hixie's avatar
Hixie committed
28

29
  @override
Hixie's avatar
Hixie committed
30
  String toString() => '$size';
31 32
}

33
/// The root of the render tree.
34 35
///
/// The view represents the total output surface of the render tree and handles
36
/// bootstrapping the rendering pipeline. The view has a unique child
37
/// [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 59 60 61
  ViewConfiguration get configuration => _configuration;
  ViewConfiguration _configuration;
  void set configuration(ViewConfiguration value) {
    if (configuration == value)
62
      return;
63
    _configuration = value;
64 65 66
    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));
Ian Hickson's avatar
Ian Hickson committed
76
    Scheduler.instance.ensureVisualUpdate();
77 78
  }

79 80
  // We never call layout() on this class, so this should never get
  // checked. (This class is laid out using scheduleInitialLayout().)
81
  @override
82
  void debugAssertDoesMeetConstraints() { assert(false); }
83

84
  @override
85 86 87 88
  void performResize() {
    assert(false);
  }

89
  @override
90
  void performLayout() {
91
    if (configuration.orientation != _orientation) {
92
      if (_orientation != null && child != null)
93 94
        child.rotate(oldAngle: _orientation, newAngle: configuration.orientation, time: timeForRotation);
      _orientation = configuration.orientation;
95
    }
96
    _size = configuration.size;
97 98 99 100 101 102
    assert(!_size.isInfinite);

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

103
  @override
104 105 106 107 108
  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 }) {
109 110
    if (child != null)
      child.hitTest(result, position: position);
111 112 113 114
    result.add(new HitTestEntry(this));
    return true;
  }

115
  @override
116
  bool get isRepaintBoundary => true;
Hixie's avatar
Hixie committed
117

118
  @override
119 120
  void paint(PaintingContext context, Offset offset) {
    if (child != null)
Adam Barth's avatar
Adam Barth committed
121
      context.paintChild(child, offset);
122 123
  }

124
  /// Uploads the composited layer tree to the engine.
125 126
  ///
  /// Actually causes the output of the rendering pipeline to appear on screen.
127
  void compositeFrame() {
128
    Timeline.startSync('Composite');
129
    try {
130 131
      final TransformLayer transformLayer = layer;
      transformLayer.transform = _logicalToDeviceTransform;
132
      ui.SceneBuilder builder = new ui.SceneBuilder();
133 134
      transformLayer.addToScene(builder, Offset.zero);
      assert(layer == transformLayer);
135 136 137
      ui.Scene scene = builder.build();
      ui.window.render(scene);
      scene.dispose();
138
      assert(() {
139 140
        if (debugRepaintRainbowEnabled)
          debugCurrentRepaintColor = debugCurrentRepaintColor.withHue(debugCurrentRepaintColor.hue + debugRepaintRainbowHueIncrement);
141 142
        return true;
      });
143
    } finally {
144
      Timeline.finishSync();
145 146 147
    }
  }

148
  @override
149
  Rect get paintBounds => Point.origin & size;
150 151

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

154
  @override
155 156 157 158 159
  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
    description.add('window size: ${ui.window.size} (in device pixels)');
    description.add('device pixel ratio: ${ui.window.devicePixelRatio} (device pixels per logical pixel)');
    description.add('configuration: $configuration (in logical pixels)');
160
  }
161
}