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

namespace sky {
namespace shell {

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

E
Eric Seidel 已提交
33 34
namespace {

35 36 37 38 39 40 41 42 43 44 45
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();
}

46 47
PlatformImpl* g_platform_impl = nullptr;

48 49 50
}  // namespace

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

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

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

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

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

Engine::~Engine() {
}

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

84
void Engine::Init() {
A
Adam Barth 已提交
85 86
  TRACE_EVENT0("sky", "Engine::Init");

87
  DCHECK(!g_platform_impl);
88
  g_platform_impl = new PlatformImpl();
89
  blink::initialize(g_platform_impl);
90 91
}

A
Adam Barth 已提交
92
void Engine::BeginFrame(base::TimeTicks frame_time) {
A
Adam Barth 已提交
93 94
  TRACE_EVENT0("sky", "Engine::BeginFrame");

95 96
  if (sky_view_)
    sky_view_->BeginFrame(frame_time);
A
Adam Barth 已提交
97 98 99
}

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

A
Adam Barth 已提交
102 103 104 105 106 107
  SkRTreeFactory factory;
  SkPictureRecorder recorder;
  auto canvas = skia::SharePtr(recorder.beginRecording(
      physical_size_.width(), physical_size_.height(), &factory,
      SkPictureRecorder::kComputeSaveLayerInfo_RecordFlag));

A
Adam Barth 已提交
108 109 110
  if (sky_view_) {
    skia::RefPtr<SkPicture> picture = sky_view_->Paint();
    canvas->clear(SK_ColorBLACK);
A
Adam Barth 已提交
111 112
    canvas->scale(display_metrics_.device_pixel_ratio,
                  display_metrics_.device_pixel_ratio);
A
Adam Barth 已提交
113 114 115 116
    if (picture)
      canvas->drawPicture(picture.get());
  }

A
Adam Barth 已提交
117 118 119
  return skia::AdoptRef(recorder.endRecordingAsPicture());
}

120 121
void Engine::ConnectToEngine(mojo::InterfaceRequest<SkyEngine> request) {
  binding_.Bind(request.Pass());
122 123
}

124 125 126 127
void Engine::OnAcceleratedWidgetAvailable(gfx::AcceleratedWidget widget) {
  config_.gpu_task_runner->PostTask(
      FROM_HERE, base::Bind(&GPUDelegate::OnAcceleratedWidgetAvailable,
                            config_.gpu_delegate, widget));
128 129
  have_surface_ = true;
  StartAnimatorIfPossible();
130
  if (sky_view_)
131
    ScheduleFrame();
132 133 134
}

void Engine::OnOutputSurfaceDestroyed() {
135 136
  have_surface_ = false;
  StopAnimator();
137 138 139 140 141
  config_.gpu_task_runner->PostTask(
      FROM_HERE,
      base::Bind(&GPUDelegate::OnOutputSurfaceDestroyed, config_.gpu_delegate));
}

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

A
Adam Barth 已提交
145 146 147 148 149 150
  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;
151

A
Adam Barth 已提交
152 153
  if (sky_view_)
    sky_view_->SetDisplayMetrics(display_metrics_);
154 155
}

156
void Engine::OnInputEvent(InputEventPtr event) {
A
Adam Barth 已提交
157
  TRACE_EVENT0("sky", "Engine::OnInputEvent");
158
  scoped_ptr<blink::WebInputEvent> web_event =
A
Adam Barth 已提交
159
      ConvertEvent(event, display_metrics_.device_pixel_ratio);
160 161
  if (!web_event)
    return;
A
Adam Barth 已提交
162 163
  if (sky_view_)
    sky_view_->HandleInputEvent(*web_event);
164 165
}

166
void Engine::RunFromLibrary(const std::string& name) {
167 168 169
  sky_view_ = blink::SkyView::Create(this);
  sky_view_->RunFromLibrary(blink::WebString::fromUTF8(name),
                            dart_library_provider_.get());
A
Adam Barth 已提交
170
  sky_view_->SetDisplayMetrics(display_metrics_);
171 172
}

173 174 175 176 177
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 已提交
178
  sky_view_->SetDisplayMetrics(display_metrics_);
179 180
}

181 182
void Engine::RunFromNetwork(const mojo::String& url) {
  dart_library_provider_.reset(
183
      new DartLibraryProviderNetwork(network_service_.get()));
184 185 186 187 188
  RunFromLibrary(url);
}

void Engine::RunFromFile(const mojo::String& main,
                         const mojo::String& package_root) {
189
  std::string package_root_str = package_root;
190
  dart_library_provider_.reset(
191
      new DartLibraryProviderFiles(base::FilePath(package_root_str)));
192 193 194
  RunFromLibrary(main);
}

195
void Engine::RunFromSnapshot(const mojo::String& path) {
196 197 198 199 200 201 202 203 204 205 206 207
  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));
208 209
}

210 211 212 213 214 215 216 217 218 219
void Engine::OnActivityPaused() {
  activity_running_ = false;
  StopAnimator();
}

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

220 221
void Engine::DidCreateIsolate(Dart_Isolate isolate) {
  Internals::Create(isolate,
222 223
                    CreateServiceProvider(config_.service_provider_context),
                    root_bundle_.Pass());
224 225
}

226 227 228 229 230 231 232 233 234
void Engine::StopAnimator() {
  animator_->Stop();
}

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

235
void Engine::ScheduleFrame() {
A
Adam Barth 已提交
236 237 238
  animator_->RequestFrame();
}

239 240 241 242 243 244 245
mojo::NavigatorHost* Engine::NavigatorHost() {
  return this;
}

void Engine::RequestNavigate(mojo::Target target,
                             mojo::URLRequestPtr request) {
  // Ignoring target for now.
246 247 248
  base::MessageLoop::current()->PostTask(
      FROM_HERE,
      base::Bind(&Engine::RunFromNetwork, GetWeakPtr(), request->url));
249 250 251 252 253
}

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

B
Benjamin Lerman 已提交
254 255 256 257
void Engine::RequestNavigateHistory(int32_t delta) {
  NOTIMPLEMENTED();
}

258 259
}  // namespace shell
}  // namespace sky