1// Copyright 2014 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "base/message_loop/message_loop.h"
6#include "gpu/command_buffer/service/mailbox_manager.h"
7#include "mojo/application/application_runner_chromium.h"
8#include "mojo/public/c/system/main.h"
9#include "mojo/public/cpp/application/application_connection.h"
10#include "mojo/public/cpp/application/application_delegate.h"
11#include "mojo/public/cpp/application/interface_factory_impl.h"
12#include "mojo/services/native_viewport/gpu_impl.h"
13#include "mojo/services/native_viewport/native_viewport_impl.h"
14#include "ui/gl/gl_share_group.h"
15#include "ui/gl/gl_surface.h"
16
17namespace mojo {
18
19class NativeViewportAppDelegate
20    : public ApplicationDelegate,
21      public InterfaceFactory<NativeViewport>,
22      public InterfaceFactory<Gpu>,
23      public InterfaceFactory<NativeViewportConfig> {
24 public:
25  NativeViewportAppDelegate()
26      : share_group_(new gfx::GLShareGroup),
27        mailbox_manager_(new gpu::gles2::MailboxManager),
28        is_test_(false),
29        is_headless_(false),
30        is_initialized_(false) {}
31  virtual ~NativeViewportAppDelegate() {}
32
33 private:
34  class NativeViewportConfigImpl : public InterfaceImpl<NativeViewportConfig> {
35   public:
36    NativeViewportConfigImpl(NativeViewportAppDelegate* app_delegate)
37      : app_delegate_(app_delegate) {}
38
39    virtual void UseTestConfig(
40        const Callback<void()>& callback) OVERRIDE {
41      app_delegate_->is_test_ = true;
42      callback.Run();
43    }
44
45    virtual void UseHeadlessConfig(
46        const Callback<void()>& callback) OVERRIDE {
47      app_delegate_->is_headless_ = true;
48      callback.Run();
49    }
50
51   private:
52    NativeViewportAppDelegate* app_delegate_;
53  };
54
55  // ApplicationDelegate implementation.
56  virtual void Initialize(ApplicationImpl* application) OVERRIDE {
57    app_ = application;
58  }
59
60  virtual bool ConfigureIncomingConnection(
61      mojo::ApplicationConnection* connection) OVERRIDE {
62    connection->AddService<NativeViewport>(this);
63    connection->AddService<Gpu>(this);
64    connection->AddService<NativeViewportConfig>(this);
65    return true;
66  }
67
68  // InterfaceFactory<NativeViewport> implementation.
69  virtual void Create(ApplicationConnection* connection,
70                      InterfaceRequest<NativeViewport> request) OVERRIDE {
71#if !defined(COMPONENT_BUILD)
72    if (!is_initialized_) {
73      if (is_test_)
74        gfx::GLSurface::InitializeOneOffForTests();
75      else
76        gfx::GLSurface::InitializeOneOff();
77      is_initialized_ = true;
78    }
79#endif
80    BindToRequest(new NativeViewportImpl(app_, is_headless_), &request);
81  }
82
83  // InterfaceFactory<Gpu> implementation.
84  virtual void Create(ApplicationConnection* connection,
85                      InterfaceRequest<Gpu> request) OVERRIDE {
86    BindToRequest(new GpuImpl(share_group_.get(), mailbox_manager_.get()),
87                  &request);
88  }
89
90  // InterfaceFactory<NVTestConfig> implementation.
91  virtual void Create(ApplicationConnection* connection,
92                      InterfaceRequest<NativeViewportConfig> request) OVERRIDE {
93    BindToRequest(new NativeViewportConfigImpl(this), &request);
94  }
95
96  ApplicationImpl* app_;
97  scoped_refptr<gfx::GLShareGroup> share_group_;
98  scoped_refptr<gpu::gles2::MailboxManager> mailbox_manager_;
99  bool is_test_;
100  bool is_headless_;
101  bool is_initialized_;
102  DISALLOW_COPY_AND_ASSIGN(NativeViewportAppDelegate);
103};
104}
105
106MojoResult MojoMain(MojoHandle shell_handle) {
107  mojo::ApplicationRunnerChromium runner(new mojo::NativeViewportAppDelegate);
108  runner.set_message_loop_type(base::MessageLoop::TYPE_UI);
109  return runner.Run(shell_handle);
110}
111