engine.h 4.5 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 13
#include <memory>
#include <string>

#include "flutter/assets/asset_manager.h"
#include "flutter/common/task_runners.h"
#include "flutter/lib/ui/semantics/semantics_node.h"
14
#include "flutter/lib/ui/window/platform_message.h"
15
#include "flutter/lib/ui/window/viewport_metrics.h"
16
#include "flutter/runtime/dart_vm.h"
17
#include "flutter/runtime/runtime_controller.h"
18
#include "flutter/runtime/runtime_delegate.h"
19
#include "flutter/shell/common/animator.h"
20
#include "flutter/shell/common/rasterizer.h"
21
#include "flutter/shell/common/run_configuration.h"
22 23
#include "lib/fxl/macros.h"
#include "lib/fxl/memory/weak_ptr.h"
A
Adam Barth 已提交
24
#include "third_party/skia/include/core/SkPicture.h"
25 26 27

namespace shell {

28
class Engine final : public blink::RuntimeDelegate {
29
 public:
30 31 32 33 34 35 36 37 38 39 40 41 42
  class Delegate {
   public:
    virtual void OnEngineUpdateSemantics(
        const Engine& engine,
        blink::SemanticsNodeUpdates update) = 0;

    virtual void OnEngineHandlePlatformMessage(
        const Engine& engine,
        fxl::RefPtr<blink::PlatformMessage> message) = 0;
  };

  Engine(Delegate& delegate,
         const blink::DartVM& vm,
43
         fxl::RefPtr<blink::DartSnapshot> isolate_snapshot,
44 45 46 47 48
         blink::TaskRunners task_runners,
         blink::Settings settings,
         std::unique_ptr<Animator> animator,
         fml::WeakPtr<GrContext> resource_context,
         fxl::RefPtr<flow::SkiaUnrefQueue> unref_queue);
A
Adam Barth 已提交
49

50 51
  ~Engine() override;

52
  fml::WeakPtr<Engine> GetWeakPtr() const;
53

54 55
  FXL_WARN_UNUSED_RESULT
  bool Run(RunConfiguration configuration);
56

57 58 59 60 61 62
  // 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.
  FXL_WARN_UNUSED_RESULT
  bool Restart(RunConfiguration configuration);
63

64
  bool UpdateAssetManager(fml::RefPtr<blink::AssetManager> asset_manager);
65

66
  void BeginFrame(fxl::TimePoint frame_time);
67

68
  void NotifyIdle(int64_t deadline);
69

70
  Dart_Port GetUIIsolateMainPort();
71

72
  std::string GetUIIsolateName();
73

74
  bool UIIsolateHasLivePorts();
75

76
  tonic::DartErrorHandleType GetUIIsolateLastError();
77

78
  tonic::DartErrorHandleType GetLoadScriptError();
79

80 81 82 83 84 85
  std::pair<bool, uint32_t> GetUIIsolateReturnCode();

  void OnOutputSurfaceCreated();

  void OnOutputSurfaceDestroyed();

86
  void SetViewportMetrics(const blink::ViewportMetrics& metrics);
87

88
  void DispatchPlatformMessage(fxl::RefPtr<blink::PlatformMessage> message);
89 90 91

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

92 93 94
  void DispatchSemanticsAction(int id,
                               blink::SemanticsAction action,
                               std::vector<uint8_t> args);
95

96
  void SetSemanticsEnabled(bool enabled);
97

98
  void ScheduleFrame(bool regenerate_layer_tree = true) override;
99

100
 private:
101 102 103 104 105 106 107
  Engine::Delegate& delegate_;
  const blink::Settings settings_;
  std::unique_ptr<Animator> animator_;
  std::unique_ptr<blink::RuntimeController> runtime_controller_;
  tonic::DartErrorHandleType load_script_error_;
  std::string initial_route_;
  blink::ViewportMetrics viewport_metrics_;
108
  fml::RefPtr<blink::AssetManager> asset_manager_;
109 110 111 112 113
  bool activity_running_;
  bool have_surface_;
  fml::WeakPtrFactory<Engine> weak_factory_;

  // |blink::RuntimeDelegate|
114
  std::string DefaultRouteName() override;
115 116

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

  // |blink::RuntimeDelegate|
Y
Yegor 已提交
120
  void UpdateSemantics(blink::SemanticsNodeUpdates update) override;
121 122

  // |blink::RuntimeDelegate|
123
  void HandlePlatformMessage(
124
      fxl::RefPtr<blink::PlatformMessage> message) override;
125

126
  void StopAnimator();
127

128
  void StartAnimatorIfPossible();
129 130

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

132
  bool HandleNavigationPlatformMessage(
133
      fxl::RefPtr<blink::PlatformMessage> message);
134

135
  bool HandleLocalizationPlatformMessage(blink::PlatformMessage* message);
136

137
  void HandleSettingsPlatformMessage(blink::PlatformMessage* message);
138

139
  void HandleAssetPlatformMessage(fxl::RefPtr<blink::PlatformMessage> message);
140

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

143
  bool PrepareAndLaunchIsolate(RunConfiguration configuration);
144

145
  FXL_DISALLOW_COPY_AND_ASSIGN(Engine);
146 147 148 149
};

}  // namespace shell

150
#endif  // SHELL_COMMON_ENGINE_H_