binding.dart 3.84 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:ui' as ui show window;
6

7
import 'package:flutter/gestures.dart';
8
import 'package:flutter/scheduler.dart';
Ian Hickson's avatar
Ian Hickson committed
9
import 'package:flutter/services.dart';
10
import 'package:mojo/core.dart' as core;
Hixie's avatar
Hixie committed
11
import 'package:sky_services/semantics/semantics.mojom.dart' as mojom;
12 13

import 'box.dart';
14
import 'debug.dart';
15 16
import 'object.dart';
import 'view.dart';
Hixie's avatar
Hixie committed
17
import 'semantics.dart';
18

Ian Hickson's avatar
Ian Hickson committed
19
export 'package:flutter/gestures.dart' show HitTestResult;
20

Florian Loitsch's avatar
Florian Loitsch committed
21
/// The glue between the render tree and the Flutter engine.
22
abstract class Renderer extends Object with Scheduler, Services
Ian Hickson's avatar
Ian Hickson committed
23
  implements HitTestable {
24

Ian Hickson's avatar
Ian Hickson committed
25 26
  void initInstances() {
    super.initInstances();
27
    _instance = this;
Ian Hickson's avatar
Ian Hickson committed
28 29
    ui.window.onMetricsChanged = handleMetricsChanged;
    initRenderView();
30
    initSemantics();
31
    assert(renderView != null);
32 33 34 35
    assert(() {
      initServiceExtensions();
      return true;
    });
Ian Hickson's avatar
Ian Hickson committed
36 37
    addPersistentFrameCallback(_handlePersistentFrameCallback);
  }
38

Ian Hickson's avatar
Ian Hickson committed
39 40
  static Renderer _instance;
  static Renderer get instance => _instance;
41

Ian Hickson's avatar
Ian Hickson committed
42 43 44 45
  void initRenderView() {
    if (renderView == null) {
      renderView = new RenderView();
      renderView.scheduleInitialFrame();
46
    }
47
    handleMetricsChanged(); // configures renderView's metrics
48 49
  }

Florian Loitsch's avatar
Florian Loitsch committed
50
  /// The render tree that's attached to the output surface.
51
  RenderView get renderView => _renderView;
52
  RenderView _renderView;
Ian Hickson's avatar
Ian Hickson committed
53
  void set renderView(RenderView value) {
54
    assert(value != null);
Ian Hickson's avatar
Ian Hickson committed
55 56 57 58 59
    if (_renderView == value)
      return;
    if (_renderView != null)
      _renderView.detach();
    _renderView = value;
60
    _renderView.attach();
61 62
  }

Ian Hickson's avatar
Ian Hickson committed
63
  void handleMetricsChanged() {
64
    assert(renderView != null);
65
    renderView.configuration = new ViewConfiguration(size: ui.window.size);
66 67
  }

68 69
  void initSemantics() {
    SemanticsNode.onSemanticsEnabled = renderView.scheduleInitialSemantics;
70
    shell.provideService(mojom.SemanticsServer.serviceName, (core.MojoMessagePipeEndpoint endpoint) {
71 72
      mojom.SemanticsServerStub server = new mojom.SemanticsServerStub.fromEndpoint(endpoint);
      server.impl = new SemanticsServer();
73
    });
Hixie's avatar
Hixie committed
74 75
  }

76 77 78 79
  void _handlePersistentFrameCallback(Duration timeStamp) {
    beginFrame();
  }

Ian Hickson's avatar
Ian Hickson committed
80
  /// Pump the rendering pipeline to generate a frame.
81
  void beginFrame() {
82
    assert(renderView != null);
83
    RenderObject.flushLayout();
84
    RenderObject.flushCompositingBits();
85
    RenderObject.flushPaint();
Hixie's avatar
Hixie committed
86
    renderView.compositeFrame(); // this sends the bits to the GPU
87
    if (SemanticsNode.hasListeners) {
Hixie's avatar
Hixie committed
88
      RenderObject.flushSemantics();
89
      SemanticsNode.sendSemanticsTree();
Hixie's avatar
Hixie committed
90
    }
91 92
  }

Ian Hickson's avatar
Ian Hickson committed
93
  void hitTest(HitTestResult result, Point position) {
94 95
    assert(renderView != null);
    renderView.hitTest(result, position: position);
Ian Hickson's avatar
Ian Hickson committed
96
    super.hitTest(result, position);
97
  }
98
}
99

Florian Loitsch's avatar
Florian Loitsch committed
100
/// Prints a textual representation of the entire render tree.
101
void debugDumpRenderTree() {
102
  debugPrint(Renderer.instance?.renderView?.toStringDeep());
103
}
104

Florian Loitsch's avatar
Florian Loitsch committed
105
/// Prints a textual representation of the entire layer tree.
106
void debugDumpLayerTree() {
107
  debugPrint(Renderer.instance?.renderView?.layer?.toStringDeep());
Ian Hickson's avatar
Ian Hickson committed
108 109
}

Hixie's avatar
Hixie committed
110 111 112 113
/// Prints a textual representation of the entire semantics tree.
/// This will only work if there is a semantics client attached.
/// Otherwise, the tree is empty and this will print "null".
void debugDumpSemanticsTree() {
114
  debugPrint(Renderer.instance?.renderView?.debugSemantics?.toStringDeep() ?? 'Semantics not collected.');
Hixie's avatar
Hixie committed
115 116
}

Ian Hickson's avatar
Ian Hickson committed
117 118
/// A concrete binding for applications that use the Rendering framework
/// directly. This is the glue that binds the framework to the Flutter engine.
119
class RenderingFlutterBinding extends BindingBase with Scheduler, Gesturer, Services, Renderer {
Ian Hickson's avatar
Ian Hickson committed
120
  RenderingFlutterBinding({ RenderBox root }) {
121
    assert(renderView != null);
Ian Hickson's avatar
Ian Hickson committed
122 123
    renderView.child = root;
  }
124
}