engine.cc 8.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
#include "base/command_line.h"
9 10
#include "base/files/file_path.h"
#include "base/threading/worker_pool.h"
A
Adam Barth 已提交
11
#include "base/trace_event/trace_event.h"
12
#include "mojo/common/data_pipe_utils.h"
13
#include "mojo/public/cpp/application/connect.h"
14
#include "services/asset_bundle/asset_unpacker_job.h"
15
#include "sky/engine/public/platform/WebInputEvent.h"
A
Adam Barth 已提交
16 17
#include "sky/engine/public/platform/sky_display_metrics.h"
#include "sky/engine/public/platform/sky_display_metrics.h"
18
#include "sky/engine/public/web/Sky.h"
19
#include "sky/engine/public/web/WebRuntimeFeatures.h"
20 21
#include "sky/shell/dart/dart_library_provider_files.h"
#include "sky/shell/dart/dart_library_provider_network.h"
22
#include "sky/shell/service_provider.h"
23
#include "sky/shell/switches.h"
A
Adam Barth 已提交
24
#include "sky/shell/ui/animator.h"
25
#include "sky/shell/ui/input_event_converter.h"
26
#include "sky/shell/ui/internals.h"
A
Adam Barth 已提交
27
#include "sky/shell/ui/platform_impl.h"
A
Adam Barth 已提交
28 29
#include "third_party/skia/include/core/SkCanvas.h"
#include "third_party/skia/include/core/SkPictureRecorder.h"
30 31 32 33

namespace sky {
namespace shell {

34 35
const char kSnapshotKey[] = "snapshot_blob.bin";

E
Eric Seidel 已提交
36 37
namespace {

38 39 40 41 42 43 44 45 46 47 48
void Ignored(bool) {
}

mojo::ScopedDataPipeConsumerHandle Fetch(const base::FilePath& path) {
  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();
}

49 50
PlatformImpl* g_platform_impl = nullptr;

51 52 53
}  // namespace

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

A
Adam Barth 已提交
55 56 57 58 59 60
Engine::Config::Config() {
}

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

A
Adam Barth 已提交
61
Engine::Engine(const Config& config)
62
    : config_(config),
A
Adam Barth 已提交
63
      animator_(new Animator(config, this)),
64
      binding_(this),
65 66
      activity_running_(false),
      have_surface_(false),
A
Adam Barth 已提交
67
      weak_factory_(this) {
68 69 70
  mojo::ServiceProviderPtr service_provider =
      CreateServiceProvider(config.service_provider_context);
  mojo::ConnectToService(service_provider.get(), &network_service_);
71 72

#if defined(OS_ANDROID)
A
Adam Barth 已提交
73 74
  // TODO(abarth): Implement VSyncProvider on other platforms.
  vsync::VSyncProviderPtr vsync_provider;
75 76 77
  mojo::ConnectToService(service_provider.get(), &vsync_provider);
  animator_->set_vsync_provider(vsync_provider.Pass());
#endif
78 79 80 81 82 83 84 85 86
}

Engine::~Engine() {
}

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

87
void Engine::Init() {
A
Adam Barth 已提交
88 89
  TRACE_EVENT0("sky", "Engine::Init");

90
  DCHECK(!g_platform_impl);
91
  g_platform_impl = new PlatformImpl();
92
  blink::initialize(g_platform_impl);
93 94 95 96

  base::CommandLine& command_line = *base::CommandLine::ForCurrentProcess();
  blink::WebRuntimeFeatures::enableDartCheckedMode(
      command_line.HasSwitch(switches::kEnableCheckedMode));
97 98
}

A
Adam Barth 已提交
99
void Engine::BeginFrame(base::TimeTicks frame_time) {
A
Adam Barth 已提交
100 101
  TRACE_EVENT0("sky", "Engine::BeginFrame");

102 103
  if (sky_view_)
    sky_view_->BeginFrame(frame_time);
A
Adam Barth 已提交
104 105 106
}

skia::RefPtr<SkPicture> Engine::Paint() {
A
Adam Barth 已提交
107 108
  TRACE_EVENT0("sky", "Engine::Paint");

A
Adam Barth 已提交
109 110 111 112 113 114
  SkRTreeFactory factory;
  SkPictureRecorder recorder;
  auto canvas = skia::SharePtr(recorder.beginRecording(
      physical_size_.width(), physical_size_.height(), &factory,
      SkPictureRecorder::kComputeSaveLayerInfo_RecordFlag));

A
Adam Barth 已提交
115 116 117
  if (sky_view_) {
    skia::RefPtr<SkPicture> picture = sky_view_->Paint();
    canvas->clear(SK_ColorBLACK);
A
Adam Barth 已提交
118 119
    canvas->scale(display_metrics_.device_pixel_ratio,
                  display_metrics_.device_pixel_ratio);
A
Adam Barth 已提交
120 121 122 123
    if (picture)
      canvas->drawPicture(picture.get());
  }

A
Adam Barth 已提交
124 125 126
  return skia::AdoptRef(recorder.endRecordingAsPicture());
}

127 128
void Engine::ConnectToEngine(mojo::InterfaceRequest<SkyEngine> request) {
  binding_.Bind(request.Pass());
129 130
}

131 132 133 134
void Engine::OnAcceleratedWidgetAvailable(gfx::AcceleratedWidget widget) {
  config_.gpu_task_runner->PostTask(
      FROM_HERE, base::Bind(&GPUDelegate::OnAcceleratedWidgetAvailable,
                            config_.gpu_delegate, widget));
135 136
  have_surface_ = true;
  StartAnimatorIfPossible();
137
  if (sky_view_)
138
    ScheduleFrame();
139 140 141
}

void Engine::OnOutputSurfaceDestroyed() {
142 143
  have_surface_ = false;
  StopAnimator();
144 145 146 147 148
  config_.gpu_task_runner->PostTask(
      FROM_HERE,
      base::Bind(&GPUDelegate::OnOutputSurfaceDestroyed, config_.gpu_delegate));
}

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

A
Adam Barth 已提交
152 153 154 155 156 157
  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;
158

A
Adam Barth 已提交
159 160
  if (sky_view_)
    sky_view_->SetDisplayMetrics(display_metrics_);
161 162
}

163
void Engine::OnInputEvent(InputEventPtr event) {
A
Adam Barth 已提交
164
  TRACE_EVENT0("sky", "Engine::OnInputEvent");
165
  scoped_ptr<blink::WebInputEvent> web_event =
A
Adam Barth 已提交
166
      ConvertEvent(event, display_metrics_.device_pixel_ratio);
167 168
  if (!web_event)
    return;
A
Adam Barth 已提交
169 170
  if (sky_view_)
    sky_view_->HandleInputEvent(*web_event);
171 172
}

173
void Engine::RunFromLibrary(const std::string& name) {
174 175 176
  sky_view_ = blink::SkyView::Create(this);
  sky_view_->RunFromLibrary(blink::WebString::fromUTF8(name),
                            dart_library_provider_.get());
A
Adam Barth 已提交
177
  sky_view_->SetDisplayMetrics(display_metrics_);
178 179
}

180 181 182 183 184
void Engine::RunFromSnapshotStream(
    const std::string& name,
    mojo::ScopedDataPipeConsumerHandle snapshot) {
  sky_view_ = blink::SkyView::Create(this);
  sky_view_->RunFromSnapshot(blink::WebString::fromUTF8(name), snapshot.Pass());
A
Adam Barth 已提交
185
  sky_view_->SetDisplayMetrics(display_metrics_);
186 187
}

188 189
void Engine::RunFromNetwork(const mojo::String& url) {
  dart_library_provider_.reset(
190
      new DartLibraryProviderNetwork(network_service_.get()));
191 192 193 194 195
  RunFromLibrary(url);
}

void Engine::RunFromFile(const mojo::String& main,
                         const mojo::String& package_root) {
196
  std::string package_root_str = package_root;
197
  dart_library_provider_.reset(
198
      new DartLibraryProviderFiles(base::FilePath(package_root_str)));
199 200 201
  RunFromLibrary(main);
}

202
void Engine::RunFromSnapshot(const mojo::String& path) {
203 204 205 206 207 208 209 210 211 212 213 214
  std::string path_str = path;
  RunFromSnapshotStream(path_str, Fetch(base::FilePath(path_str)));
}

void Engine::RunFromBundle(const mojo::String& path) {
  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));
215 216
}

217 218 219 220 221 222 223 224 225 226
void Engine::OnActivityPaused() {
  activity_running_ = false;
  StopAnimator();
}

void Engine::OnActivityResumed() {
  activity_running_ = true;
  StartAnimatorIfPossible();
}

227 228
void Engine::DidCreateIsolate(Dart_Isolate isolate) {
  Internals::Create(isolate,
229 230
                    CreateServiceProvider(config_.service_provider_context),
                    root_bundle_.Pass());
231 232
}

233 234 235 236 237 238 239 240 241
void Engine::StopAnimator() {
  animator_->Stop();
}

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

242
void Engine::ScheduleFrame() {
A
Adam Barth 已提交
243 244 245
  animator_->RequestFrame();
}

246 247 248 249 250 251 252
mojo::NavigatorHost* Engine::NavigatorHost() {
  return this;
}

void Engine::RequestNavigate(mojo::Target target,
                             mojo::URLRequestPtr request) {
  // Ignoring target for now.
253 254 255
  base::MessageLoop::current()->PostTask(
      FROM_HERE,
      base::Bind(&Engine::RunFromNetwork, GetWeakPtr(), request->url));
256 257 258 259 260
}

void Engine::DidNavigateLocally(const mojo::String& url) {
}

B
Benjamin Lerman 已提交
261 262 263 264
void Engine::RequestNavigateHistory(int32_t delta) {
  NOTIMPLEMENTED();
}

265 266
}  // namespace shell
}  // namespace sky