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
#include "flutter/assets/zip_asset_store.h"
9
#include "flutter/glue/drain_data_pipe_job.h"
10
#include "flutter/lib/ui/window/platform_message.h"
11
#include "flutter/runtime/runtime_controller.h"
12
#include "flutter/runtime/runtime_delegate.h"
13
#include "flutter/services/engine/sky_engine.mojom.h"
14
#include "flutter/shell/common/rasterizer.h"
15 16
#include "lib/ftl/macros.h"
#include "lib/ftl/memory/weak_ptr.h"
17
#include "mojo/public/cpp/application/service_provider_impl.h"
18
#include "mojo/public/cpp/bindings/binding.h"
19
#include "mojo/public/cpp/bindings/binding_set.h"
20
#include "mojo/public/cpp/system/data_pipe.h"
21
#include "mojo/public/cpp/system/handle.h"
22
#include "mojo/public/interfaces/application/service_provider.mojom.h"
23
#include "mojo/services/asset_bundle/interfaces/asset_bundle.mojom.h"
A
Adam Barth 已提交
24
#include "third_party/skia/include/core/SkPicture.h"
25

A
Adam Barth 已提交
26 27 28 29 30
namespace blink {
class DirectoryAssetBundle;
class ZipAssetBundle;
}  // namespace blink

31
namespace shell {
32
class PlatformView;
A
Adam Barth 已提交
33
class Animator;
34
using PointerDataPacket = blink::PointerDataPacket;
35

36
class Engine : public sky::SkyEngine, public blink::RuntimeDelegate {
37
 public:
38
  explicit Engine(PlatformView* platform_view);
A
Adam Barth 已提交
39

40 41
  ~Engine() override;

42
  ftl::WeakPtr<Engine> GetWeakPtr();
43

44
  static void Init();
45

A
Adam Barth 已提交
46
  void BeginFrame(ftl::TimePoint frame_time);
47

48 49
  void RunFromSource(const std::string& main,
                     const std::string& packages,
50
                     const std::string& bundle);
51 52

  Dart_Port GetUIIsolateMainPort();
53

54 55
  std::string GetUIIsolateName();

56 57 58
  void ConnectToEngine(mojo::InterfaceRequest<SkyEngine> request);
  void OnOutputSurfaceCreated(const ftl::Closure& gpu_continuation);
  void OnOutputSurfaceDestroyed(const ftl::Closure& gpu_continuation);
A
Adam Barth 已提交
59
  void DispatchPlatformMessage(ftl::RefPtr<blink::PlatformMessage> message);
60
  void DispatchPointerDataPacket(const PointerDataPacket& packet);
61 62
  void DispatchSemanticsAction(int id, blink::SemanticsAction action);
  void SetSemanticsEnabled(bool enabled);
63

64
 private:
65
  // SkyEngine implementation:
66
  void OnViewportMetricsChanged(sky::ViewportMetricsPtr metrics) override;
67
  void RunFromFile(const mojo::String& main,
68
                   const mojo::String& packages,
69
                   const mojo::String& bundle) override;
70
  void RunFromPrecompiledSnapshot(const mojo::String& bundle_path) override;
71 72 73 74
  void RunFromBundle(const mojo::String& script_uri,
                     const mojo::String& bundle_path) override;
  void RunFromBundleAndSnapshot(const mojo::String& script_uri,
                                const mojo::String& bundle_path,
75
                                const mojo::String& snapshot_path) override;
76

77
  // RuntimeDelegate methods:
78
  void ScheduleFrame() override;
A
Adam Barth 已提交
79
  void Render(std::unique_ptr<flow::LayerTree> layer_tree) override;
80
  void UpdateSemantics(std::vector<blink::SemanticsNode> update) override;
81 82
  void HandlePlatformMessage(
      ftl::RefPtr<blink::PlatformMessage> message) override;
83 84 85
  void DidCreateMainIsolate(Dart_Isolate isolate) override;
  void DidCreateSecondaryIsolate(Dart_Isolate isolate) override;

86
  void RunFromSnapshotStream(const std::string& script_uri,
87
                             mojo::ScopedDataPipeConsumerHandle snapshot);
88

89 90 91
  void StopAnimator();
  void StartAnimatorIfPossible();

92
  void ConfigureAssetBundle(const std::string& path);
93
  void ConfigureRuntime(const std::string& script_uri);
94

95 96 97 98 99 100
  bool HandleLifecyclePlatformMessage(blink::PlatformMessage* message);
  bool HandleNavigationPlatformMessage(
      ftl::RefPtr<blink::PlatformMessage> message);
  bool HandleLocalizationPlatformMessage(
      ftl::RefPtr<blink::PlatformMessage> message);

A
Adam Barth 已提交
101 102
  void HandleAssetPlatformMessage(ftl::RefPtr<blink::PlatformMessage> message);

103
  ftl::WeakPtr<PlatformView> platform_view_;
104
  std::unique_ptr<Animator> animator_;
105

106
  mojo::asset_bundle::AssetBundlePtr root_bundle_;
107
  std::unique_ptr<blink::RuntimeController> runtime_;
108

109 110
  std::unique_ptr<glue::DrainDataPipeJob> snapshot_drainer_;

111
  ftl::RefPtr<blink::PlatformMessage> pending_push_route_message_;
112
  sky::ViewportMetricsPtr viewport_metrics_;
113 114
  std::string language_code_;
  std::string country_code_;
A
Adam Barth 已提交
115
  bool semantics_enabled_ = false;
116
  mojo::Binding<SkyEngine> binding_;
A
Adam Barth 已提交
117

118
  ftl::RefPtr<blink::ZipAssetStore> asset_store_;
A
Adam Barth 已提交
119 120
  std::unique_ptr<blink::DirectoryAssetBundle> directory_asset_bundle_;
  std::unique_ptr<blink::ZipAssetBundle> zip_asset_bundle_;
121

122 123 124 125
  // TODO(eseidel): This should move into an AnimatorStateMachine.
  bool activity_running_;
  bool have_surface_;

126
  ftl::WeakPtrFactory<Engine> weak_factory_;
127

128
  FTL_DISALLOW_COPY_AND_ASSIGN(Engine);
129 130 131 132
};

}  // namespace shell

133
#endif  // SHELL_COMMON_ENGINE_H_