view.dart 4.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: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

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

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

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

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

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

56
  /// The constraints used for the root layout.
57 58 59 60
  ViewConfiguration get configuration => _configuration;
  ViewConfiguration _configuration;
  void set configuration(ViewConfiguration value) {
    if (configuration == value)
61
      return;
62
    _configuration = value;
63 64 65
    markNeedsLayout();
  }

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

71
  /// Bootstrap the rendering pipeline by scheduling the first frame.
72 73
  void scheduleInitialFrame() {
    scheduleInitialLayout();
74
    scheduleInitialPaint(new TransformLayer(transform: _logicalToDeviceTransform));
Ian Hickson's avatar
Ian Hickson committed
75
    Scheduler.instance.ensureVisualUpdate();
76 77
  }

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

  void performResize() {
    assert(false);
  }

  void performLayout() {
87
    if (configuration.orientation != _orientation) {
88
      if (_orientation != null && child != null)
89 90
        child.rotate(oldAngle: _orientation, newAngle: configuration.orientation, time: timeForRotation);
      _orientation = configuration.orientation;
91
    }
92
    _size = configuration.size;
93 94 95 96 97 98 99 100 101 102 103
    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 }) {
104 105
    if (child != null)
      child.hitTest(result, position: position);
106 107 108 109
    result.add(new HitTestEntry(this));
    return true;
  }

110
  bool get isRepaintBoundary => true;
Hixie's avatar
Hixie committed
111

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

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

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

145 146 147 148 149
  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)');
150
  }
151
}