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
  // Used by Engine::Run
  enum class RunStatus {
    Success,                // Successful call to Run()
D
Dan Field 已提交
35 36 37
    FailureAlreadyRunning,  // Isolate was already running; may not be
                            // considered a failure by callers
    Failure,  // Isolate could not be started or other unspecified failure
38 39
  };

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

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

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

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

62 63
  ~Engine() override;

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

66
  FML_WARN_UNUSED_RESULT
67
  RunStatus Run(RunConfiguration configuration);
68

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

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

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

80
  void NotifyIdle(int64_t deadline);
81

82
  Dart_Port GetUIIsolateMainPort();
83

84
  std::string GetUIIsolateName();
85

86
  bool UIIsolateHasLivePorts();
87

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

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

  void OnOutputSurfaceCreated();

  void OnOutputSurfaceDestroyed();

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

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

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

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

106
  void SetSemanticsEnabled(bool enabled);
107

108
  void SetAccessibilityFeatures(int32_t flags);
109

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

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

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

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

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

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

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

143
  void StopAnimator();
144

145
  void StartAnimatorIfPossible();
146 147

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

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

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

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

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

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

160
  RunStatus PrepareAndLaunchIsolate(RunConfiguration configuration);
161

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

}  // namespace shell

167
#endif  // SHELL_COMMON_ENGINE_H_