engine.h 4.7 KB
Newer Older
1 2 3 4 5 6 7
// 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.

#ifndef SKY_SHELL_UI_ENGINE_H_
#define SKY_SHELL_UI_ENGINE_H_

8
#include "base/files/file_path.h"
9 10
#include "base/macros.h"
#include "base/memory/weak_ptr.h"
A
Adam Barth 已提交
11
#include "base/single_thread_task_runner.h"
12 13 14
#include "mojo/common/binding_set.h"
#include "mojo/public/cpp/application/interface_factory.h"
#include "mojo/public/cpp/application/service_provider_impl.h"
15
#include "mojo/public/cpp/bindings/binding.h"
A
Adam Barth 已提交
16
#include "mojo/public/cpp/system/core.h"
17
#include "mojo/public/cpp/system/data_pipe.h"
18
#include "mojo/public/interfaces/application/service_provider.mojom.h"
19
#include "mojo/services/asset_bundle/interfaces/asset_bundle.mojom.h"
A
Adam Barth 已提交
20
#include "skia/ext/refptr.h"
21
#include "sky/engine/public/sky/sky_view.h"
A
Adam Barth 已提交
22
#include "sky/engine/public/sky/sky_view_client.h"
23
#include "sky/services/engine/sky_engine.mojom.h"
F
Florian Loitsch 已提交
24
#include "sky/services/rasterizer/rasterizer.mojom.h"
A
Adam Barth 已提交
25
#include "sky/shell/rasterizer.h"
26
#include "sky/shell/ui_delegate.h"
A
Adam Barth 已提交
27
#include "third_party/skia/include/core/SkPicture.h"
28

J
Jason Simmons 已提交
29 30 31 32 33 34
namespace mojo {
namespace asset_bundle {
class ZipAssetBundle;
}
}

35
namespace sky {
36
class PlatformImpl;
37
namespace shell {
A
Adam Barth 已提交
38
class Animator;
39

40
class Engine : public UIDelegate,
41
               public SkyEngine,
A
Adam Barth 已提交
42
               public blink::SkyViewClient {
43
 public:
A
Adam Barth 已提交
44
  struct Config {
A
Adam Barth 已提交
45 46 47
    Config();
    ~Config();

A
Adam Barth 已提交
48 49 50
    scoped_refptr<base::SingleThreadTaskRunner> gpu_task_runner;
  };

F
Florian Loitsch 已提交
51
  explicit Engine(const Config& config, rasterizer::RasterizerPtr rasterizer);
52 53 54 55
  ~Engine() override;

  base::WeakPtr<Engine> GetWeakPtr();

56
  static void Init();
57

A
Adam Barth 已提交
58
  std::unique_ptr<flow::LayerTree> BeginFrame(base::TimeTicks frame_time);
59

60
 private:
61 62
  // UIDelegate implementation:
  void ConnectToEngine(mojo::InterfaceRequest<SkyEngine> request) override;
A
Adam Barth 已提交
63 64
  void OnOutputSurfaceCreated(const base::Closure& gpu_continuation) override;
  void OnOutputSurfaceDestroyed(const base::Closure& gpu_continuation) override;
65

66
  // SkyEngine implementation:
67
  void SetServices(ServicesDataPtr services) override;
A
Adam Barth 已提交
68
  void OnViewportMetricsChanged(ViewportMetricsPtr metrics) override;
69
  void OnLocaleChanged(const mojo::String& language_code,
70
                       const mojo::String& country_code) override;
71
  void OnPointerPacket(pointer::PointerPacketPtr packet) override;
72 73

  void RunFromFile(const mojo::String& main,
74
                   const mojo::String& packages,
75
                   const mojo::String& bundle) override;
76
  void RunFromPrecompiledSnapshot(const mojo::String& bundle_path) override;
77 78 79 80
  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,
81
                                const mojo::String& snapshot_path) override;
A
Adam Barth 已提交
82 83
  void PushRoute(const mojo::String& route) override;
  void PopRoute() override;
84
  void OnAppLifecycleStateChanged(sky::AppLifecycleState state) override;
85

A
Adam Barth 已提交
86
  // SkyViewClient methods:
87
  void ScheduleFrame() override;
88
  void FlushRealTimeEvents() override;
A
Adam Barth 已提交
89
  void Render(std::unique_ptr<flow::LayerTree> layer_tree) override;
90 91 92 93 94
  void DidCreateMainIsolate(Dart_Isolate isolate) override;
  void DidCreateSecondaryIsolate(Dart_Isolate isolate) override;

  void BindToServiceProvider(
      mojo::InterfaceRequest<mojo::ServiceProvider> request);
A
Adam Barth 已提交
95

96
  void RunFromLibrary(const std::string& name);
97
  void RunFromSnapshotStream(const std::string& script_uri,
98
                             mojo::ScopedDataPipeConsumerHandle snapshot);
99

J
Jason Simmons 已提交
100 101
  void SetupAssetBundle(const mojo::String& bundle_path);

102 103 104
  void StopAnimator();
  void StartAnimatorIfPossible();

105 106
  void ConfigureZipAssetBundle(const mojo::String& path);

107
  Config config_;
108
  std::unique_ptr<Animator> animator_;
109

110
  ServicesDataPtr services_;
111
  mojo::ServiceProviderImpl service_provider_impl_;
A
Adam Barth 已提交
112
  mojo::ServiceProviderPtr incoming_services_;
113 114
  mojo::BindingSet<mojo::ServiceProvider> service_provider_bindings_;

115
  mojo::asset_bundle::AssetBundlePtr root_bundle_;
116
  std::unique_ptr<blink::DartLibraryProvider> dart_library_provider_;
117 118
  std::unique_ptr<blink::SkyView> sky_view_;

A
Adam Barth 已提交
119
  std::string initial_route_;
A
Adam Barth 已提交
120
  blink::SkyDisplayMetrics display_metrics_;
121 122
  std::string language_code_;
  std::string country_code_;
123
  mojo::Binding<SkyEngine> binding_;
J
Jason Simmons 已提交
124
  scoped_refptr<mojo::asset_bundle::ZipAssetBundle> zip_asset_bundle_;
125

126 127 128 129
  // TODO(eseidel): This should move into an AnimatorStateMachine.
  bool activity_running_;
  bool have_surface_;

130 131 132 133 134 135 136 137 138
  base::WeakPtrFactory<Engine> weak_factory_;

  DISALLOW_COPY_AND_ASSIGN(Engine);
};

}  // namespace shell
}  // namespace sky

#endif  // SKY_SHELL_UI_ENGINE_H_