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/rasterizer.h"
26
#include "flutter/shell/common/run_configuration.h"
A
Adam Barth 已提交
27
#include "third_party/skia/include/core/SkPicture.h"
28 29 30

namespace shell {

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

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

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

    virtual void OnPreEngineRestart() = 0;
51 52 53

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

  Engine(Delegate& delegate,
B
Ben Konyi 已提交
57
         blink::DartVM& vm,
58 59
         fml::RefPtr<blink::DartSnapshot> isolate_snapshot,
         fml::RefPtr<blink::DartSnapshot> shared_snapshot,
60 61 62
         blink::TaskRunners task_runners,
         blink::Settings settings,
         std::unique_ptr<Animator> animator,
63
         fml::WeakPtr<blink::SnapshotDelegate> snapshot_delegate,
64
         fml::WeakPtr<GrContext> resource_context,
65
         fml::RefPtr<flow::SkiaUnrefQueue> unref_queue);
A
Adam Barth 已提交
66

67 68
  ~Engine() override;

69
  fml::WeakPtr<Engine> GetWeakPtr() const;
70

71
  FML_WARN_UNUSED_RESULT
72
  RunStatus Run(RunConfiguration configuration);
73

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

81
  bool UpdateAssetManager(std::shared_ptr<blink::AssetManager> asset_manager);
82

83
  void BeginFrame(fml::TimePoint frame_time);
84

85
  void NotifyIdle(int64_t deadline);
86

87
  Dart_Port GetUIIsolateMainPort();
88

89
  std::string GetUIIsolateName();
90

91
  bool UIIsolateHasLivePorts();
92

93
  tonic::DartErrorHandleType GetUIIsolateLastError();
94 95 96 97 98 99 100

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

  void OnOutputSurfaceCreated();

  void OnOutputSurfaceDestroyed();

101
  void SetViewportMetrics(const blink::ViewportMetrics& metrics);
102

103
  void DispatchPlatformMessage(fml::RefPtr<blink::PlatformMessage> message);
104 105 106

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

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

111
  void SetSemanticsEnabled(bool enabled);
112

113
  void SetAccessibilityFeatures(int32_t flags);
114

115
  void ScheduleFrame(bool regenerate_layer_tree = true) override;
116

117 118 119
  // |blink::RuntimeDelegate|
  blink::FontCollection& GetFontCollection() override;

120
 private:
121 122 123 124 125 126
  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_;
127
  std::shared_ptr<blink::AssetManager> asset_manager_;
128 129
  bool activity_running_;
  bool have_surface_;
130
  blink::FontCollection font_collection_;
131 132 133
  fml::WeakPtrFactory<Engine> weak_factory_;

  // |blink::RuntimeDelegate|
134
  std::string DefaultRouteName() override;
135 136

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

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

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

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

152
  void StopAnimator();
153

154
  void StartAnimatorIfPossible();
155 156

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

158
  bool HandleNavigationPlatformMessage(
159
      fml::RefPtr<blink::PlatformMessage> message);
160

161
  bool HandleLocalizationPlatformMessage(blink::PlatformMessage* message);
162

163
  void HandleSettingsPlatformMessage(blink::PlatformMessage* message);
164

165
  void HandleAssetPlatformMessage(fml::RefPtr<blink::PlatformMessage> message);
166

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

169
  RunStatus PrepareAndLaunchIsolate(RunConfiguration configuration);
170

171
  FML_DISALLOW_COPY_AND_ASSIGN(Engine);
172 173 174 175
};

}  // namespace shell

176
#endif  // SHELL_COMMON_ENGINE_H_