summaryrefslogtreecommitdiffstats
path: root/mandoline/ui/aura/surface_binding.cc
blob: 378e9b9d07bc6ad5941fb0bd2506cbb2e309d3e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
// 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 "mandoline/ui/aura/surface_binding.h"

#include <map>

#include "base/bind.h"
#include "base/lazy_instance.h"
#include "base/threading/thread_local.h"
#include "cc/output/compositor_frame.h"
#include "cc/output/output_surface.h"
#include "cc/output/output_surface_client.h"
#include "cc/output/software_output_device.h"
#include "cc/resources/shared_bitmap_manager.h"
#include "components/gpu/public/interfaces/gpu.mojom.h"
#include "components/surfaces/public/interfaces/surfaces.mojom.h"
#include "components/view_manager/public/cpp/view.h"
#include "components/view_manager/public/cpp/view_manager.h"
#include "mandoline/ui/aura/window_tree_host_mojo.h"
#include "mojo/application/public/cpp/connect.h"
#include "mojo/application/public/interfaces/shell.mojom.h"
#include "mojo/cc/context_provider_mojo.h"
#include "mojo/converters/geometry/geometry_type_converters.h"
#include "mojo/converters/surfaces/surfaces_type_converters.h"

namespace mandoline {
namespace {

// OutputSurface ---------------------------------------------------------------

// OutputSurface implementation for a view. Pushes the surface id to View when
// appropriate.
class OutputSurfaceImpl : public cc::OutputSurface {
 public:
  OutputSurfaceImpl(mojo::View* view,
                    const scoped_refptr<cc::ContextProvider>& context_provider,
                    mojo::Surface* surface,
                    uint32_t id_namespace,
                    uint32_t* next_local_id);
  ~OutputSurfaceImpl() override;

  // cc::OutputSurface:
  void SwapBuffers(cc::CompositorFrame* frame) override;

 private:
  mojo::View* view_;
  mojo::Surface* surface_;
  uint32_t id_namespace_;
  uint32_t* next_local_id_;  // Owned by PerViewManagerState.
  uint32_t local_id_;
  gfx::Size surface_size_;

  DISALLOW_COPY_AND_ASSIGN(OutputSurfaceImpl);
};

OutputSurfaceImpl::OutputSurfaceImpl(
    mojo::View* view,
    const scoped_refptr<cc::ContextProvider>& context_provider,
    mojo::Surface* surface,
    uint32_t id_namespace,
    uint32_t* next_local_id)
    : cc::OutputSurface(context_provider),
      view_(view),
      surface_(surface),
      id_namespace_(id_namespace),
      next_local_id_(next_local_id),
      local_id_(0u) {
  capabilities_.delegated_rendering = true;
  capabilities_.max_frames_pending = 1;
}

OutputSurfaceImpl::~OutputSurfaceImpl() {
}

void OutputSurfaceImpl::SwapBuffers(cc::CompositorFrame* frame) {
  gfx::Size frame_size =
      frame->delegated_frame_data->render_pass_list.back()->output_rect.size();
  if (frame_size != surface_size_) {
    if (local_id_ != 0u)
      surface_->DestroySurface(local_id_);
    local_id_ = (*next_local_id_)++;
    surface_->CreateSurface(local_id_);
    auto qualified_id = mojo::SurfaceId::New();
    qualified_id->local = local_id_;
    qualified_id->id_namespace = id_namespace_;
    view_->SetSurfaceId(qualified_id.Pass());
    surface_size_ = frame_size;
  }

  surface_->SubmitFrame(local_id_, mojo::Frame::From(*frame), mojo::Closure());

  client_->DidSwapBuffers();
  client_->DidSwapBuffersComplete();
}

}  // namespace

// PerViewManagerState ---------------------------------------------------------

// State needed per ViewManager. Provides the real implementation of
// CreateOutputSurface. SurfaceBinding obtains a pointer to the
// PerViewManagerState appropriate for the ViewManager. PerViewManagerState is
// stored in a thread local map. When no more refereces to a PerViewManagerState
// remain the PerViewManagerState is deleted and the underlying map cleaned up.
class SurfaceBinding::PerViewManagerState
    : public base::RefCounted<PerViewManagerState>,
      public mojo::ResourceReturner {
 public:
  static PerViewManagerState* Get(mojo::Shell* shell,
                                  mojo::ViewManager* view_manager);

  scoped_ptr<cc::OutputSurface> CreateOutputSurface(mojo::View* view);

 private:
  typedef std::map<mojo::ViewManager*,
                   PerViewManagerState*> ViewManagerToStateMap;

  friend class base::RefCounted<PerViewManagerState>;

  PerViewManagerState(mojo::Shell* shell, mojo::ViewManager* view_manager);
  ~PerViewManagerState() override;

  void Init();

  // mojo::ResourceReturner:
  void ReturnResources(
      mojo::Array<mojo::ReturnedResourcePtr> resources) override;

  void SetIdNamespace(uint32_t id_namespace);

  static base::LazyInstance<
      base::ThreadLocalPointer<ViewManagerToStateMap>>::Leaky view_states;

  mojo::Shell* shell_;
  mojo::ViewManager* view_manager_;

  // Set of state needed to create an OutputSurface.
  mojo::GpuPtr gpu_;
  mojo::SurfacePtr surface_;
  mojo::Binding<mojo::ResourceReturner> returner_binding_;
  uint32_t id_namespace_;
  uint32_t next_local_id_;

  DISALLOW_COPY_AND_ASSIGN(PerViewManagerState);
};

// static
base::LazyInstance<base::ThreadLocalPointer<
    SurfaceBinding::PerViewManagerState::ViewManagerToStateMap>>::Leaky
    SurfaceBinding::PerViewManagerState::view_states;

// static
SurfaceBinding::PerViewManagerState* SurfaceBinding::PerViewManagerState::Get(
    mojo::Shell* shell,
    mojo::ViewManager* view_manager) {
  ViewManagerToStateMap* view_map = view_states.Pointer()->Get();
  if (!view_map) {
    view_map = new ViewManagerToStateMap;
    view_states.Pointer()->Set(view_map);
  }
  if (!(*view_map)[view_manager]) {
    (*view_map)[view_manager] = new PerViewManagerState(shell, view_manager);
    (*view_map)[view_manager]->Init();
  }
  return (*view_map)[view_manager];
}

scoped_ptr<cc::OutputSurface>
SurfaceBinding::PerViewManagerState::CreateOutputSurface(mojo::View* view) {
  // TODO(sky): figure out lifetime here. Do I need to worry about the return
  // value outliving this?
  mojo::CommandBufferPtr cb;
  gpu_->CreateOffscreenGLES2Context(GetProxy(&cb));
  scoped_refptr<cc::ContextProvider> context_provider(
      new mojo::ContextProviderMojo(cb.PassInterface().PassHandle()));
  return make_scoped_ptr(new OutputSurfaceImpl(
      view, context_provider, surface_.get(), id_namespace_, &next_local_id_));
}

SurfaceBinding::PerViewManagerState::PerViewManagerState(
    mojo::Shell* shell,
    mojo::ViewManager* view_manager)
    : shell_(shell),
      view_manager_(view_manager),
      returner_binding_(this),
      id_namespace_(0u),
      next_local_id_(0u) {
}

SurfaceBinding::PerViewManagerState::~PerViewManagerState() {
  ViewManagerToStateMap* view_map = view_states.Pointer()->Get();
  DCHECK(view_map);
  DCHECK_EQ(this, (*view_map)[view_manager_]);
  view_map->erase(view_manager_);
  if (view_map->empty()) {
    delete view_map;
    view_states.Pointer()->Set(nullptr);
  }
}

void SurfaceBinding::PerViewManagerState::Init() {
  DCHECK(!surface_.get());

  mojo::ServiceProviderPtr surfaces_service_provider;
  mojo::URLRequestPtr request(mojo::URLRequest::New());
  request->url = mojo::String::From("mojo:surfaces_service");
  shell_->ConnectToApplication(request.Pass(),
                               GetProxy(&surfaces_service_provider),
                               nullptr);
  ConnectToService(surfaces_service_provider.get(), &surface_);
  surface_->GetIdNamespace(
      base::Bind(&SurfaceBinding::PerViewManagerState::SetIdNamespace,
                 base::Unretained(this)));
  // Block until we receive our id namespace.
  surface_.WaitForIncomingMethodCall();
  DCHECK_NE(0u, id_namespace_);

  mojo::ResourceReturnerPtr returner_ptr;
  returner_binding_.Bind(GetProxy(&returner_ptr));
  surface_->SetResourceReturner(returner_ptr.Pass());

  mojo::ServiceProviderPtr gpu_service_provider;
  // TODO(jamesr): Should be mojo:gpu_service
  mojo::URLRequestPtr request2(mojo::URLRequest::New());
  request2->url = mojo::String::From("mojo:view_manager");
  shell_->ConnectToApplication(request2.Pass(),
                               GetProxy(&gpu_service_provider),
                               nullptr);
  ConnectToService(gpu_service_provider.get(), &gpu_);
}

void SurfaceBinding::PerViewManagerState::SetIdNamespace(
    uint32_t id_namespace) {
  id_namespace_ = id_namespace;
}

void SurfaceBinding::PerViewManagerState::ReturnResources(
    mojo::Array<mojo::ReturnedResourcePtr> resources) {
}

// SurfaceBinding --------------------------------------------------------------

SurfaceBinding::SurfaceBinding(mojo::Shell* shell, mojo::View* view)
    : view_(view),
      state_(PerViewManagerState::Get(shell, view->view_manager())) {
}

SurfaceBinding::~SurfaceBinding() {
}

scoped_ptr<cc::OutputSurface> SurfaceBinding::CreateOutputSurface() {
  return state_->CreateOutputSurface(view_);
}

}  // namespace mandoline