engine.h 5.9 KB
Newer Older
M
Michael Goderbauer 已提交
1
// Copyright 2013 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
#ifndef SHELL_COMMON_ENGINE_H_
#define SHELL_COMMON_ENGINE_H_
7

8 9 10 11 12
#include <memory>
#include <string>

#include "flutter/assets/asset_manager.h"
#include "flutter/common/task_runners.h"
13 14
#include "flutter/fml/macros.h"
#include "flutter/fml/memory/weak_ptr.h"
15
#include "flutter/lib/ui/painting/image_decoder.h"
16
#include "flutter/lib/ui/semantics/custom_accessibility_action.h"
17
#include "flutter/lib/ui/semantics/semantics_node.h"
18
#include "flutter/lib/ui/snapshot_delegate.h"
19
#include "flutter/lib/ui/text/font_collection.h"
20
#include "flutter/lib/ui/window/platform_message.h"
21
#include "flutter/lib/ui/window/viewport_metrics.h"
22
#include "flutter/runtime/dart_vm.h"
23
#include "flutter/runtime/runtime_controller.h"
24
#include "flutter/runtime/runtime_delegate.h"
25
#include "flutter/shell/common/animator.h"
26
#include "flutter/shell/common/rasterizer.h"
27
#include "flutter/shell/common/run_configuration.h"
28
#include "flutter/shell/common/shell_io_manager.h"
A
Adam Barth 已提交
29
#include "third_party/skia/include/core/SkPicture.h"
30

31
namespace flutter {
32

33 34 35
/// Manager of the root Dart isolate and the relay of platform messages.
///
/// Also setups up dart::ui bindings and handles restarts.
36
class Engine final : public RuntimeDelegate {
37
 public:
38 39 40
  // Used by Engine::Run
  enum class RunStatus {
    Success,                // Successful call to Run()
D
Dan Field 已提交
41 42 43
    FailureAlreadyRunning,  // Isolate was already running; may not be
                            // considered a failure by callers
    Failure,  // Isolate could not be started or other unspecified failure
44 45
  };

46 47 48
  class Delegate {
   public:
    virtual void OnEngineUpdateSemantics(
49 50
        SemanticsNodeUpdates update,
        CustomAccessibilityActionUpdates actions) = 0;
51 52

    virtual void OnEngineHandlePlatformMessage(
53
        fml::RefPtr<PlatformMessage> message) = 0;
54 55

    virtual void OnPreEngineRestart() = 0;
56 57 58

    virtual void UpdateIsolateDescription(const std::string isolate_name,
                                          int64_t isolate_port) = 0;
59 60

    virtual void SetNeedsReportTimings(bool value) = 0;
61 62 63
  };

  Engine(Delegate& delegate,
64 65 66 67 68
         DartVM& vm,
         fml::RefPtr<const DartSnapshot> isolate_snapshot,
         fml::RefPtr<const DartSnapshot> shared_snapshot,
         TaskRunners task_runners,
         Settings settings,
69
         std::unique_ptr<Animator> animator,
70 71
         fml::WeakPtr<SnapshotDelegate> snapshot_delegate,
         fml::WeakPtr<IOManager> io_manager);
A
Adam Barth 已提交
72

73 74
  ~Engine() override;

75 76
  float GetDisplayRefreshRate() const;

77
  fml::WeakPtr<Engine> GetWeakPtr() const;
78

79
  FML_WARN_UNUSED_RESULT
80
  RunStatus Run(RunConfiguration configuration);
81

82 83 84 85
  // Used to "cold reload" a running application where the shell (along with the
  // platform view and its rasterizer bindings) remains the same but the root
  // isolate is torn down and restarted with the new configuration. Only used in
  // the development workflow.
86
  FML_WARN_UNUSED_RESULT
87
  bool Restart(RunConfiguration configuration);
88

89
  bool UpdateAssetManager(std::shared_ptr<AssetManager> asset_manager);
90

91
  void BeginFrame(fml::TimePoint frame_time);
92

93 94
  void ReportTimings(std::vector<int64_t> timings);

95
  void NotifyIdle(int64_t deadline);
96

97
  Dart_Port GetUIIsolateMainPort();
98

99
  std::string GetUIIsolateName();
100

101
  bool UIIsolateHasLivePorts();
102

103
  tonic::DartErrorHandleType GetUIIsolateLastError();
104 105 106 107 108 109 110

  std::pair<bool, uint32_t> GetUIIsolateReturnCode();

  void OnOutputSurfaceCreated();

  void OnOutputSurfaceDestroyed();

111
  void SetViewportMetrics(const ViewportMetrics& metrics);
112

113
  void DispatchPlatformMessage(fml::RefPtr<PlatformMessage> message);
114

115
  void DispatchPointerDataPacket(const PointerDataPacket& packet,
116
                                 uint64_t trace_flow_id);
117

118
  void DispatchSemanticsAction(int id,
119
                               SemanticsAction action,
120
                               std::vector<uint8_t> args);
121

122
  void SetSemanticsEnabled(bool enabled);
123

124
  void SetAccessibilityFeatures(int32_t flags);
125

126
  void ScheduleFrame(bool regenerate_layer_tree = true) override;
127

128 129
  // |RuntimeDelegate|
  FontCollection& GetFontCollection() override;
130

131
 private:
132
  Engine::Delegate& delegate_;
133
  const Settings settings_;
134
  std::unique_ptr<Animator> animator_;
135
  std::unique_ptr<RuntimeController> runtime_controller_;
136
  std::string initial_route_;
137 138
  ViewportMetrics viewport_metrics_;
  std::shared_ptr<AssetManager> asset_manager_;
139 140
  bool activity_running_;
  bool have_surface_;
141
  FontCollection font_collection_;
142
  ImageDecoder image_decoder_;
143 144
  fml::WeakPtrFactory<Engine> weak_factory_;

145
  // |RuntimeDelegate|
146
  std::string DefaultRouteName() override;
147

148
  // |RuntimeDelegate|
149
  void Render(std::unique_ptr<flutter::LayerTree> layer_tree) override;
150

151 152 153
  // |RuntimeDelegate|
  void UpdateSemantics(SemanticsNodeUpdates update,
                       CustomAccessibilityActionUpdates actions) override;
154

155 156
  // |RuntimeDelegate|
  void HandlePlatformMessage(fml::RefPtr<PlatformMessage> message) override;
157

158
  // |RuntimeDelegate|
159 160 161
  void UpdateIsolateDescription(const std::string isolate_name,
                                int64_t isolate_port) override;

162 163
  void SetNeedsReportTimings(bool value) override;

164
  void StopAnimator();
165

166
  void StartAnimatorIfPossible();
167

168
  bool HandleLifecyclePlatformMessage(PlatformMessage* message);
169

170
  bool HandleNavigationPlatformMessage(fml::RefPtr<PlatformMessage> message);
171

172
  bool HandleLocalizationPlatformMessage(PlatformMessage* message);
173

174
  void HandleSettingsPlatformMessage(PlatformMessage* message);
175

176
  void HandleAssetPlatformMessage(fml::RefPtr<PlatformMessage> message);
177

178
  bool GetAssetAsBuffer(const std::string& name, std::vector<uint8_t>* data);
179

180
  RunStatus PrepareAndLaunchIsolate(RunConfiguration configuration);
181

182 183
  friend class testing::ShellTest;

184
  FML_DISALLOW_COPY_AND_ASSIGN(Engine);
185 186
};

187
}  // namespace flutter
188

189
#endif  // SHELL_COMMON_ENGINE_H_