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

namespace shell {

30
class Engine final : public blink::RuntimeDelegate {
31
 public:
32 33 34 35 36 37 38
  // Used by Engine::Run
  enum class RunStatus {
    Success,                // Successful call to Run()
    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
  class Delegate {
   public:
    virtual void OnEngineUpdateSemantics(
42 43
        blink::SemanticsNodeUpdates update,
        blink::CustomAccessibilityActionUpdates actions) = 0;
44 45

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

    virtual void OnPreEngineRestart() = 0;
49 50 51
  };

  Engine(Delegate& delegate,
B
Ben Konyi 已提交
52
         blink::DartVM& vm,
53 54
         fml::RefPtr<blink::DartSnapshot> isolate_snapshot,
         fml::RefPtr<blink::DartSnapshot> shared_snapshot,
55 56 57 58
         blink::TaskRunners task_runners,
         blink::Settings settings,
         std::unique_ptr<Animator> animator,
         fml::WeakPtr<GrContext> resource_context,
59
         fml::RefPtr<flow::SkiaUnrefQueue> unref_queue);
A
Adam Barth 已提交
60

61 62
  ~Engine() override;

63
  fml::WeakPtr<Engine> GetWeakPtr() const;
64

65
  FML_WARN_UNUSED_RESULT
66
  RunStatus Run(RunConfiguration configuration);
67

68 69 70 71
  // 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.
72
  FML_WARN_UNUSED_RESULT
73
  bool Restart(RunConfiguration configuration);
74

75
  bool UpdateAssetManager(fml::RefPtr<blink::AssetManager> asset_manager);
76

77
  void BeginFrame(fml::TimePoint frame_time);
78

79
  void NotifyIdle(int64_t deadline);
80

81
  Dart_Port GetUIIsolateMainPort();
82

83
  std::string GetUIIsolateName();
84

85
  bool UIIsolateHasLivePorts();
86

87
  tonic::DartErrorHandleType GetUIIsolateLastError();
88 89 90 91 92 93 94

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

  void OnOutputSurfaceCreated();

  void OnOutputSurfaceDestroyed();

95
  void SetViewportMetrics(const blink::ViewportMetrics& metrics);
96

97
  void DispatchPlatformMessage(fml::RefPtr<blink::PlatformMessage> message);
98 99 100

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

101 102 103
  void DispatchSemanticsAction(int id,
                               blink::SemanticsAction action,
                               std::vector<uint8_t> args);
104

105
  void SetSemanticsEnabled(bool enabled);
106

107
  void SetAccessibilityFeatures(int32_t flags);
108

109
  void ScheduleFrame(bool regenerate_layer_tree = true) override;
110

111 112 113
  // |blink::RuntimeDelegate|
  blink::FontCollection& GetFontCollection() override;

114
 private:
115 116 117 118 119 120
  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_;
121
  fml::RefPtr<blink::AssetManager> asset_manager_;
122 123
  bool activity_running_;
  bool have_surface_;
124
  blink::FontCollection font_collection_;
125 126 127
  fml::WeakPtrFactory<Engine> weak_factory_;

  // |blink::RuntimeDelegate|
128
  std::string DefaultRouteName() override;
129 130

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

  // |blink::RuntimeDelegate|
134 135 136
  void UpdateSemantics(
      blink::SemanticsNodeUpdates update,
      blink::CustomAccessibilityActionUpdates actions) override;
137 138

  // |blink::RuntimeDelegate|
139
  void HandlePlatformMessage(
140
      fml::RefPtr<blink::PlatformMessage> message) override;
141

142
  void StopAnimator();
143

144
  void StartAnimatorIfPossible();
145 146

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

148
  bool HandleNavigationPlatformMessage(
149
      fml::RefPtr<blink::PlatformMessage> message);
150

151
  bool HandleLocalizationPlatformMessage(blink::PlatformMessage* message);
152

153
  void HandleSettingsPlatformMessage(blink::PlatformMessage* message);
154

155
  void HandleAssetPlatformMessage(fml::RefPtr<blink::PlatformMessage> message);
156

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

159
  RunStatus PrepareAndLaunchIsolate(RunConfiguration configuration);
160

161
  FML_DISALLOW_COPY_AND_ASSIGN(Engine);
162 163 164 165
};

}  // namespace shell

166
#endif  // SHELL_COMMON_ENGINE_H_