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

namespace shell {

29
class Engine final : public blink::RuntimeDelegate {
30
 public:
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,
B
Ben Konyi 已提交
43
         blink::DartVM& vm,
44
         fxl::RefPtr<blink::DartSnapshot> isolate_snapshot,
45
         fxl::RefPtr<blink::DartSnapshot> shared_snapshot,
46 47 48 49 50
         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 已提交
51

52 53
  ~Engine() override;

54
  fml::WeakPtr<Engine> GetWeakPtr() const;
55

56 57
  FXL_WARN_UNUSED_RESULT
  bool Run(RunConfiguration configuration);
58

59 60 61 62 63 64
  // 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);
65

66
  bool UpdateAssetManager(fml::RefPtr<blink::AssetManager> asset_manager);
67

68
  void BeginFrame(fxl::TimePoint frame_time);
69

70
  void NotifyIdle(int64_t deadline);
71

72
  Dart_Port GetUIIsolateMainPort();
73

74
  std::string GetUIIsolateName();
75

76
  bool UIIsolateHasLivePorts();
77

78
  tonic::DartErrorHandleType GetUIIsolateLastError();
79

80
  tonic::DartErrorHandleType GetLoadScriptError();
81

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

  void OnOutputSurfaceCreated();

  void OnOutputSurfaceDestroyed();

88
  void SetViewportMetrics(const blink::ViewportMetrics& metrics);
89

90
  void DispatchPlatformMessage(fxl::RefPtr<blink::PlatformMessage> message);
91 92 93

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

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

98
  void SetSemanticsEnabled(bool enabled);
99

100
  void ScheduleFrame(bool regenerate_layer_tree = true) override;
101

102
 private:
103 104 105 106 107 108 109
  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_;
110
  fml::RefPtr<blink::AssetManager> asset_manager_;
111 112
  bool activity_running_;
  bool have_surface_;
113
  blink::FontCollection font_collection_;
114 115 116
  fml::WeakPtrFactory<Engine> weak_factory_;

  // |blink::RuntimeDelegate|
117
  std::string DefaultRouteName() override;
118 119

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

  // |blink::RuntimeDelegate|
Y
Yegor 已提交
123
  void UpdateSemantics(blink::SemanticsNodeUpdates update) override;
124 125

  // |blink::RuntimeDelegate|
126
  void HandlePlatformMessage(
127
      fxl::RefPtr<blink::PlatformMessage> message) override;
128

129 130 131
  // |blink::RuntimeDelegate|
  blink::FontCollection& GetFontCollection() override;

132
  void StopAnimator();
133

134
  void StartAnimatorIfPossible();
135 136

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

138
  bool HandleNavigationPlatformMessage(
139
      fxl::RefPtr<blink::PlatformMessage> message);
140

141
  bool HandleLocalizationPlatformMessage(blink::PlatformMessage* message);
142

143
  void HandleSettingsPlatformMessage(blink::PlatformMessage* message);
144

145
  void HandleAssetPlatformMessage(fxl::RefPtr<blink::PlatformMessage> message);
146

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

149
  bool PrepareAndLaunchIsolate(RunConfiguration configuration);
150

151
  FXL_DISALLOW_COPY_AND_ASSIGN(Engine);
152 153 154 155
};

}  // namespace shell

156
#endif  // SHELL_COMMON_ENGINE_H_