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

#include "sky/shell/ui/engine.h"

A
Adam Barth 已提交
7
#include "base/bind.h"
8 9
#include "base/files/file_path.h"
#include "base/threading/worker_pool.h"
10
#include "base/time/time.h"
A
Adam Barth 已提交
11
#include "base/trace_event/trace_event.h"
12
#include "mojo/data_pipe_utils/data_pipe_utils.h"
13
#include "mojo/public/cpp/application/connect.h"
14
#include "services/asset_bundle/asset_unpacker_job.h"
A
Adam Barth 已提交
15
#include "sky/engine/public/platform/sky_display_metrics.h"
16
#include "sky/engine/public/platform/WebInputEvent.h"
17
#include "sky/engine/public/web/Sky.h"
18
#include "sky/shell/dart/dart_library_provider_files.h"
19
#include "sky/shell/shell.h"
A
Adam Barth 已提交
20
#include "sky/shell/ui/animator.h"
21
#include "sky/shell/ui/internals.h"
A
Adam Barth 已提交
22
#include "sky/shell/ui/platform_impl.h"
A
Adam Barth 已提交
23 24
#include "third_party/skia/include/core/SkCanvas.h"
#include "third_party/skia/include/core/SkPictureRecorder.h"
25 26 27

namespace sky {
namespace shell {
28
namespace {
29

30 31
const char kSnapshotKey[] = "snapshot_blob.bin";

32
void Ignored(bool) {
C
Chinmay Garde 已提交
33
  TRACE_EVENT_ASYNC_END0("flutter", "AssetUnpackerJobFetch", 1);
34 35 36
}

mojo::ScopedDataPipeConsumerHandle Fetch(const base::FilePath& path) {
C
Chinmay Garde 已提交
37
  TRACE_EVENT_ASYNC_BEGIN0("flutter", "AssetUnpackerJobFetch", 1);
38 39 40 41 42 43 44
  mojo::DataPipe pipe;
  auto runner = base::WorkerPool::GetTaskRunner(true);
  mojo::common::CopyFromFile(base::FilePath(path), pipe.producer_handle.Pass(),
                             0, runner.get(), base::Bind(&Ignored));
  return pipe.consumer_handle.Pass();
}

45 46
PlatformImpl* g_platform_impl = nullptr;

47 48 49
}  // namespace

using mojo::asset_bundle::AssetUnpackerJob;
E
Eric Seidel 已提交
50

A
Adam Barth 已提交
51 52 53 54 55 56
Engine::Config::Config() {
}

Engine::Config::~Config() {
}

F
Florian Loitsch 已提交
57
Engine::Engine(const Config& config, rasterizer::RasterizerPtr rasterizer)
58
    : config_(config),
F
Florian Loitsch 已提交
59
      animator_(new Animator(config, rasterizer.Pass(), this)),
60
      binding_(this),
61 62
      activity_running_(false),
      have_surface_(false),
A
Adam Barth 已提交
63
      weak_factory_(this) {
64 65 66 67 68 69 70 71 72
}

Engine::~Engine() {
}

base::WeakPtr<Engine> Engine::GetWeakPtr() {
  return weak_factory_.GetWeakPtr();
}

73
void Engine::Init() {
74
  TRACE_EVENT0("flutter", "Engine::Init");
A
Adam Barth 已提交
75

76 77 78
  DCHECK(!g_platform_impl);
  g_platform_impl = new PlatformImpl();
  blink::initialize(g_platform_impl);
C
Chinmay Garde 已提交
79
  Shell::Shared().tracing_controller().SetDartInitialized();
80 81
}

A
Adam Barth 已提交
82
std::unique_ptr<flow::LayerTree> Engine::BeginFrame(
83
    base::TimeTicks frame_time) {
84
  TRACE_EVENT0("flutter", "Engine::BeginFrame");
A
Adam Barth 已提交
85

86 87
  if (!sky_view_)
    return nullptr;
A
Adam Barth 已提交
88

89
  auto begin_time = base::TimeTicks::Now();
A
Adam Barth 已提交
90
  std::unique_ptr<flow::LayerTree> layer_tree =
91
      sky_view_->BeginFrame(frame_time);
92
  if (layer_tree) {
93 94 95
    layer_tree->set_frame_size(
        SkISize::Make(physical_size_.width(), physical_size_.height()));
    layer_tree->set_construction_time(base::TimeTicks::Now() - begin_time);
A
Adam Barth 已提交
96
  }
97
  return layer_tree;
A
Adam Barth 已提交
98 99
}

100 101
void Engine::ConnectToEngine(mojo::InterfaceRequest<SkyEngine> request) {
  binding_.Bind(request.Pass());
102 103
}

A
Adam Barth 已提交
104 105
void Engine::OnOutputSurfaceCreated(const base::Closure& gpu_continuation) {
  config_.gpu_task_runner->PostTask(FROM_HERE, gpu_continuation);
106 107
  have_surface_ = true;
  StartAnimatorIfPossible();
108
  if (sky_view_)
109
    ScheduleFrame();
110 111
}

A
Adam Barth 已提交
112
void Engine::OnOutputSurfaceDestroyed(const base::Closure& gpu_continuation) {
113 114
  have_surface_ = false;
  StopAnimator();
A
Adam Barth 已提交
115
  config_.gpu_task_runner->PostTask(FROM_HERE, gpu_continuation);
116 117
}

118 119 120 121
void Engine::SetServices(ServicesDataPtr services) {
  services_ = services.Pass();

#if defined(OS_ANDROID) || defined(OS_IOS)
A
Adam Barth 已提交
122 123
  vsync::VSyncProviderPtr vsync_provider;
  if (services_->shell) {
C
Chinmay Garde 已提交
124 125
    mojo::ConnectToService(services_->shell.get(), "mojo:vsync",
                           &vsync_provider);
A
Adam Barth 已提交
126
  } else {
C
Chinmay Garde 已提交
127 128
    mojo::ConnectToService(services_->services_provided_by_embedder.get(),
                           &vsync_provider);
A
Adam Barth 已提交
129
  }
A
Adam Barth 已提交
130
  animator_->set_vsync_provider(vsync_provider.Pass());
131 132 133
#endif
}

A
Adam Barth 已提交
134 135
void Engine::OnViewportMetricsChanged(ViewportMetricsPtr metrics) {
  physical_size_.SetSize(metrics->physical_width, metrics->physical_height);
A
Adam Barth 已提交
136

A
Adam Barth 已提交
137 138 139 140 141 142
  display_metrics_.physical_size = physical_size_;
  display_metrics_.device_pixel_ratio = metrics->device_pixel_ratio;
  display_metrics_.padding_top = metrics->padding_top;
  display_metrics_.padding_right = metrics->padding_right;
  display_metrics_.padding_bottom = metrics->padding_bottom;
  display_metrics_.padding_left = metrics->padding_left;
143

A
Adam Barth 已提交
144 145
  if (sky_view_)
    sky_view_->SetDisplayMetrics(display_metrics_);
146 147
}

148 149 150 151 152 153 154 155
void Engine::OnLocaleChanged(const mojo::String& language_code,
			     const mojo::String& country_code) {
  language_code_ = language_code;
  country_code_ = country_code;
  if (sky_view_)
    sky_view_->SetLocale(language_code_, country_code_);
}

156
void Engine::OnPointerPacket(pointer::PointerPacketPtr packet) {
157
  TRACE_EVENT0("flutter", "Engine::OnPointerPacket");
158 159 160 161 162 163 164 165 166

  // Convert the pointers' x and y coordinates to logical pixels.
  for (auto it = packet->pointers.begin(); it != packet->pointers.end(); ++it) {
    (*it)->x /= display_metrics_.device_pixel_ratio;
    (*it)->y /= display_metrics_.device_pixel_ratio;
  }

  if (sky_view_)
    sky_view_->HandlePointerPacket(packet);
167 168
}

169
void Engine::RunFromLibrary(const std::string& name) {
C
Chinmay Garde 已提交
170
  TRACE_EVENT0("flutter", "Engine::RunFromLibrary");
171
  sky_view_ = blink::SkyView::Create(this);
172 173
  sky_view_->CreateView(name);
  sky_view_->RunFromLibrary(name, dart_library_provider_.get());
A
Adam Barth 已提交
174
  sky_view_->SetDisplayMetrics(display_metrics_);
175
  sky_view_->SetLocale(language_code_, country_code_);
A
Adam Barth 已提交
176 177
  if (!initial_route_.empty())
    sky_view_->PushRoute(initial_route_);
178 179
}

180 181 182
void Engine::RunFromSnapshotStream(
    const std::string& name,
    mojo::ScopedDataPipeConsumerHandle snapshot) {
183
  TRACE_EVENT0("flutter", "Engine::RunFromSnapshotStream");
184
  sky_view_ = blink::SkyView::Create(this);
185 186
  sky_view_->CreateView(name);
  sky_view_->RunFromSnapshot(name, snapshot.Pass());
A
Adam Barth 已提交
187
  sky_view_->SetDisplayMetrics(display_metrics_);
188
  sky_view_->SetLocale(language_code_, country_code_);
A
Adam Barth 已提交
189 190
  if (!initial_route_.empty())
    sky_view_->PushRoute(initial_route_);
191 192
}

193
void Engine::RunFromPrecompiledSnapshot(const mojo::String& bundle_path) {
C
Chinmay Garde 已提交
194
  TRACE_EVENT0("flutter", "Engine::RunFromPrecompiledSnapshot");
195 196 197 198 199
  AssetUnpackerJob* unpacker = new AssetUnpackerJob(
      mojo::GetProxy(&root_bundle_), base::WorkerPool::GetTaskRunner(true));
  std::string path_str = bundle_path;
  unpacker->Unpack(Fetch(base::FilePath(path_str)));

200
  sky_view_ = blink::SkyView::Create(this);
201
  sky_view_->CreateView("http://localhost");
202 203
  sky_view_->RunFromPrecompiledSnapshot();
  sky_view_->SetDisplayMetrics(display_metrics_);
204
  sky_view_->SetLocale(language_code_, country_code_);
A
Adam Barth 已提交
205 206
  if (!initial_route_.empty())
    sky_view_->PushRoute(initial_route_);
207 208
}

209 210
void Engine::RunFromFile(const mojo::String& main,
                         const mojo::String& package_root) {
C
Chinmay Garde 已提交
211
  TRACE_EVENT0("flutter", "Engine::RunFromFile");
212
  std::string package_root_str = package_root;
213
  dart_library_provider_.reset(
214
      new DartLibraryProviderFiles(base::FilePath(package_root_str)));
215 216 217
  RunFromLibrary(main);
}

218
void Engine::RunFromBundle(const mojo::String& path) {
219
  TRACE_EVENT0("flutter", "Engine::RunFromBundle");
220 221 222 223 224 225 226
  AssetUnpackerJob* unpacker = new AssetUnpackerJob(
      mojo::GetProxy(&root_bundle_), base::WorkerPool::GetTaskRunner(true));
  std::string path_str = path;
  unpacker->Unpack(Fetch(base::FilePath(path_str)));
  root_bundle_->GetAsStream(kSnapshotKey,
                            base::Bind(&Engine::RunFromSnapshotStream,
                                       weak_factory_.GetWeakPtr(), path_str));
227 228
}

229 230
void Engine::RunFromAssetBundle(const mojo::String& url,
                                mojo::asset_bundle::AssetBundlePtr bundle) {
C
Chinmay Garde 已提交
231
  TRACE_EVENT0("flutter", "Engine::RunFromAssetBundle");
232 233 234 235 236 237 238
  std::string url_str = url;
  root_bundle_ = bundle.Pass();
  root_bundle_->GetAsStream(kSnapshotKey,
                            base::Bind(&Engine::RunFromSnapshotStream,
                                       weak_factory_.GetWeakPtr(), url_str));
}

A
Adam Barth 已提交
239 240 241 242 243 244 245 246 247 248 249 250
void Engine::PushRoute(const mojo::String& route) {
  if (sky_view_)
    sky_view_->PushRoute(route);
  else
    initial_route_ = route;
}

void Engine::PopRoute() {
  if (sky_view_)
    sky_view_->PopRoute();
}

251 252 253 254 255 256 257 258 259 260 261 262
void Engine::OnAppLifecycleStateChanged(sky::AppLifecycleState state) {
  switch (state) {
    case sky::AppLifecycleState::PAUSED:
      activity_running_ = false;
      StopAnimator();
      break;

    case sky::AppLifecycleState::RESUMED:
      activity_running_ = true;
      StartAnimatorIfPossible();
      break;
  }
263

264 265
  if (sky_view_)
    sky_view_->OnAppLifecycleStateChanged(state);
266 267
}

268
void Engine::DidCreateIsolate(Dart_Isolate isolate) {
269
  Internals::Create(isolate, services_.Pass(), root_bundle_.Pass());
270 271
}

272 273 274 275 276 277 278 279 280
void Engine::StopAnimator() {
  animator_->Stop();
}

void Engine::StartAnimatorIfPossible() {
  if (activity_running_ && have_surface_)
    animator_->Start();
}

281
void Engine::ScheduleFrame() {
A
Adam Barth 已提交
282 283 284
  animator_->RequestFrame();
}

285 286 287 288
void Engine::FlushRealTimeEvents() {
  animator_->FlushRealTimeEvents();
}

A
Adam Barth 已提交
289
void Engine::Render(std::unique_ptr<flow::LayerTree> layer_tree) {
290 291
}

292 293
}  // namespace shell
}  // namespace sky