engine.h 5.6 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 blink::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
        blink::SemanticsNodeUpdates update,
        blink::CustomAccessibilityActionUpdates actions) = 0;
47 48

    virtual void OnEngineHandlePlatformMessage(
49
        fml::RefPtr<blink::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,
B
Ben Konyi 已提交
58
         blink::DartVM& vm,
59 60
         fml::RefPtr<blink::DartSnapshot> isolate_snapshot,
         fml::RefPtr<blink::DartSnapshot> shared_snapshot,
61 62 63
         blink::TaskRunners task_runners,
         blink::Settings settings,
         std::unique_ptr<Animator> animator,
64
         fml::WeakPtr<blink::SnapshotDelegate> snapshot_delegate,
65
         fml::WeakPtr<blink::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<blink::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 blink::ViewportMetrics& metrics);
104

105
  void DispatchPlatformMessage(fml::RefPtr<blink::PlatformMessage> message);
106 107 108

  void DispatchPointerDataPacket(const blink::PointerDataPacket& packet);

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

113
  void SetSemanticsEnabled(bool enabled);
114

115
  void SetAccessibilityFeatures(int32_t flags);
116

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

119 120 121
  // |blink::RuntimeDelegate|
  blink::FontCollection& GetFontCollection() override;

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

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

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

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

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

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

154
  void StopAnimator();
155

156
  void StartAnimatorIfPossible();
157 158

  bool HandleLifecyclePlatformMessage(blink::PlatformMessage* message);
159

160
  bool HandleNavigationPlatformMessage(
161
      fml::RefPtr<blink::PlatformMessage> message);
162

163
  bool HandleLocalizationPlatformMessage(blink::PlatformMessage* message);
164

165
  void HandleSettingsPlatformMessage(blink::PlatformMessage* message);
166

167
  void HandleAssetPlatformMessage(fml::RefPtr<blink::PlatformMessage> message);
168

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

171
  RunStatus PrepareAndLaunchIsolate(RunConfiguration configuration);
172

173
  FML_DISALLOW_COPY_AND_ASSIGN(Engine);
174 175 176 177
};

}  // namespace shell

178
#endif  // SHELL_COMMON_ENGINE_H_