engine.h 5.8 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/semantics/custom_accessibility_action.h"
16
#include "flutter/lib/ui/semantics/semantics_node.h"
17
#include "flutter/lib/ui/snapshot_delegate.h"
18
#include "flutter/lib/ui/text/font_collection.h"
19
#include "flutter/lib/ui/window/platform_message.h"
20
#include "flutter/lib/ui/window/viewport_metrics.h"
21
#include "flutter/runtime/dart_vm.h"
22
#include "flutter/runtime/runtime_controller.h"
23
#include "flutter/runtime/runtime_delegate.h"
24
#include "flutter/shell/common/animator.h"
25
#include "flutter/shell/common/io_manager.h"
26
#include "flutter/shell/common/rasterizer.h"
27
#include "flutter/shell/common/run_configuration.h"
A
Adam Barth 已提交
28
#include "third_party/skia/include/core/SkPicture.h"
29 30 31

namespace shell {

32
class Engine final : public flutter::RuntimeDelegate {
33
 public:
34 35 36
  // Used by Engine::Run
  enum class RunStatus {
    Success,                // Successful call to Run()
D
Dan Field 已提交
37 38 39
    FailureAlreadyRunning,  // Isolate was already running; may not be
                            // considered a failure by callers
    Failure,  // Isolate could not be started or other unspecified failure
40 41
  };

42 43 44
  class Delegate {
   public:
    virtual void OnEngineUpdateSemantics(
45 46
        flutter::SemanticsNodeUpdates update,
        flutter::CustomAccessibilityActionUpdates actions) = 0;
47 48

    virtual void OnEngineHandlePlatformMessage(
49
        fml::RefPtr<flutter::PlatformMessage> message) = 0;
50 51

    virtual void OnPreEngineRestart() = 0;
52 53 54

    virtual void UpdateIsolateDescription(const std::string isolate_name,
                                          int64_t isolate_port) = 0;
55 56 57
  };

  Engine(Delegate& delegate,
58 59 60 61 62
         flutter::DartVM& vm,
         fml::RefPtr<const flutter::DartSnapshot> isolate_snapshot,
         fml::RefPtr<const flutter::DartSnapshot> shared_snapshot,
         flutter::TaskRunners task_runners,
         flutter::Settings settings,
63
         std::unique_ptr<Animator> animator,
64 65
         fml::WeakPtr<flutter::SnapshotDelegate> snapshot_delegate,
         fml::WeakPtr<flutter::IOManager> io_manager);
A
Adam Barth 已提交
66

67 68
  ~Engine() override;

69 70
  float GetDisplayRefreshRate() const;

71
  fml::WeakPtr<Engine> GetWeakPtr() const;
72

73
  FML_WARN_UNUSED_RESULT
74
  RunStatus Run(RunConfiguration configuration);
75

76 77 78 79
  // 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.
80
  FML_WARN_UNUSED_RESULT
81
  bool Restart(RunConfiguration configuration);
82

83
  bool UpdateAssetManager(std::shared_ptr<flutter::AssetManager> asset_manager);
84

85
  void BeginFrame(fml::TimePoint frame_time);
86

87
  void NotifyIdle(int64_t deadline);
88

89
  Dart_Port GetUIIsolateMainPort();
90

91
  std::string GetUIIsolateName();
92

93
  bool UIIsolateHasLivePorts();
94

95
  tonic::DartErrorHandleType GetUIIsolateLastError();
96 97 98 99 100 101 102

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

  void OnOutputSurfaceCreated();

  void OnOutputSurfaceDestroyed();

103
  void SetViewportMetrics(const flutter::ViewportMetrics& metrics);
104

105
  void DispatchPlatformMessage(fml::RefPtr<flutter::PlatformMessage> message);
106

107
  void DispatchPointerDataPacket(const flutter::PointerDataPacket& packet,
108
                                 uint64_t trace_flow_id);
109

110
  void DispatchSemanticsAction(int id,
111
                               flutter::SemanticsAction action,
112
                               std::vector<uint8_t> args);
113

114
  void SetSemanticsEnabled(bool enabled);
115

116
  void SetAccessibilityFeatures(int32_t flags);
117

118
  void ScheduleFrame(bool regenerate_layer_tree = true) override;
119

120 121
  // |flutter::RuntimeDelegate|
  flutter::FontCollection& GetFontCollection() override;
122

123
 private:
124
  Engine::Delegate& delegate_;
125
  const flutter::Settings settings_;
126
  std::unique_ptr<Animator> animator_;
127
  std::unique_ptr<flutter::RuntimeController> runtime_controller_;
128
  std::string initial_route_;
129 130
  flutter::ViewportMetrics viewport_metrics_;
  std::shared_ptr<flutter::AssetManager> asset_manager_;
131 132
  bool activity_running_;
  bool have_surface_;
133
  flutter::FontCollection font_collection_;
134 135
  fml::WeakPtrFactory<Engine> weak_factory_;

136
  // |flutter::RuntimeDelegate|
137
  std::string DefaultRouteName() override;
138

139
  // |flutter::RuntimeDelegate|
A
Adam Barth 已提交
140
  void Render(std::unique_ptr<flow::LayerTree> layer_tree) override;
141

142
  // |flutter::RuntimeDelegate|
143
  void UpdateSemantics(
144 145
      flutter::SemanticsNodeUpdates update,
      flutter::CustomAccessibilityActionUpdates actions) override;
146

147
  // |flutter::RuntimeDelegate|
148
  void HandlePlatformMessage(
149
      fml::RefPtr<flutter::PlatformMessage> message) override;
150

151
  // |flutter::RuntimeDelegate|
152 153 154
  void UpdateIsolateDescription(const std::string isolate_name,
                                int64_t isolate_port) override;

155
  void StopAnimator();
156

157
  void StartAnimatorIfPossible();
158

159
  bool HandleLifecyclePlatformMessage(flutter::PlatformMessage* message);
160

161
  bool HandleNavigationPlatformMessage(
162
      fml::RefPtr<flutter::PlatformMessage> message);
163

164
  bool HandleLocalizationPlatformMessage(flutter::PlatformMessage* message);
165

166
  void HandleSettingsPlatformMessage(flutter::PlatformMessage* message);
167

168 169
  void HandleAssetPlatformMessage(
      fml::RefPtr<flutter::PlatformMessage> message);
170

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

173
  RunStatus PrepareAndLaunchIsolate(RunConfiguration configuration);
174

175
  FML_DISALLOW_COPY_AND_ASSIGN(Engine);
176 177 178 179
};

}  // namespace shell

180
#endif  // SHELL_COMMON_ENGINE_H_