Update V8 to version 4.5.82.
[chromium-blink-merge.git] / mandoline / ui / aura / surface_binding.cc
blob6f71d1e85a9d0e9ae1af7e3e657e9c98e82dea2f
1 // Copyright 2015 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.
5 #include "mandoline/ui/aura/surface_binding.h"
7 #include <map>
9 #include "base/bind.h"
10 #include "base/lazy_instance.h"
11 #include "base/threading/thread_local.h"
12 #include "cc/output/compositor_frame.h"
13 #include "cc/output/output_surface.h"
14 #include "cc/output/output_surface_client.h"
15 #include "cc/output/software_output_device.h"
16 #include "cc/resources/shared_bitmap_manager.h"
17 #include "components/view_manager/public/cpp/view.h"
18 #include "components/view_manager/public/cpp/view_manager.h"
19 #include "components/view_manager/public/interfaces/gpu.mojom.h"
20 #include "components/view_manager/public/interfaces/surfaces.mojom.h"
21 #include "mandoline/ui/aura/window_tree_host_mojo.h"
22 #include "mojo/application/public/cpp/connect.h"
23 #include "mojo/application/public/interfaces/shell.mojom.h"
24 #include "mojo/cc/context_provider_mojo.h"
25 #include "mojo/converters/geometry/geometry_type_converters.h"
26 #include "mojo/converters/surfaces/surfaces_type_converters.h"
28 namespace mandoline {
29 namespace {
31 // OutputSurface ---------------------------------------------------------------
33 // OutputSurface implementation for a view. Pushes the surface id to View when
34 // appropriate.
35 class OutputSurfaceImpl : public cc::OutputSurface {
36 public:
37 OutputSurfaceImpl(mojo::View* view,
38 const scoped_refptr<cc::ContextProvider>& context_provider,
39 mojo::Surface* surface,
40 uint32_t id_namespace,
41 uint32_t* next_local_id);
42 ~OutputSurfaceImpl() override;
44 // cc::OutputSurface:
45 void SwapBuffers(cc::CompositorFrame* frame) override;
47 private:
48 mojo::View* view_;
49 mojo::Surface* surface_;
50 uint32_t id_namespace_;
51 uint32_t* next_local_id_; // Owned by PerViewManagerState.
52 uint32_t local_id_;
53 gfx::Size surface_size_;
55 DISALLOW_COPY_AND_ASSIGN(OutputSurfaceImpl);
58 OutputSurfaceImpl::OutputSurfaceImpl(
59 mojo::View* view,
60 const scoped_refptr<cc::ContextProvider>& context_provider,
61 mojo::Surface* surface,
62 uint32_t id_namespace,
63 uint32_t* next_local_id)
64 : cc::OutputSurface(context_provider),
65 view_(view),
66 surface_(surface),
67 id_namespace_(id_namespace),
68 next_local_id_(next_local_id),
69 local_id_(0u) {
70 capabilities_.delegated_rendering = true;
71 capabilities_.max_frames_pending = 1;
74 OutputSurfaceImpl::~OutputSurfaceImpl() {
77 void OutputSurfaceImpl::SwapBuffers(cc::CompositorFrame* frame) {
78 gfx::Size frame_size =
79 frame->delegated_frame_data->render_pass_list.back()->output_rect.size();
80 if (frame_size != surface_size_) {
81 if (local_id_ != 0u)
82 surface_->DestroySurface(local_id_);
83 local_id_ = (*next_local_id_)++;
84 surface_->CreateSurface(local_id_);
85 auto qualified_id = mojo::SurfaceId::New();
86 qualified_id->local = local_id_;
87 qualified_id->id_namespace = id_namespace_;
88 view_->SetSurfaceId(qualified_id.Pass());
89 surface_size_ = frame_size;
92 surface_->SubmitFrame(local_id_, mojo::Frame::From(*frame), mojo::Closure());
94 client_->DidSwapBuffers();
95 client_->DidSwapBuffersComplete();
98 } // namespace
100 // PerViewManagerState ---------------------------------------------------------
102 // State needed per ViewManager. Provides the real implementation of
103 // CreateOutputSurface. SurfaceBinding obtains a pointer to the
104 // PerViewManagerState appropriate for the ViewManager. PerViewManagerState is
105 // stored in a thread local map. When no more refereces to a PerViewManagerState
106 // remain the PerViewManagerState is deleted and the underlying map cleaned up.
107 class SurfaceBinding::PerViewManagerState
108 : public base::RefCounted<PerViewManagerState>,
109 public mojo::ResourceReturner {
110 public:
111 static PerViewManagerState* Get(mojo::Shell* shell,
112 mojo::ViewManager* view_manager);
114 scoped_ptr<cc::OutputSurface> CreateOutputSurface(mojo::View* view);
116 private:
117 typedef std::map<mojo::ViewManager*,
118 PerViewManagerState*> ViewManagerToStateMap;
120 friend class base::RefCounted<PerViewManagerState>;
122 PerViewManagerState(mojo::Shell* shell, mojo::ViewManager* view_manager);
123 ~PerViewManagerState() override;
125 void Init();
127 // mojo::ResourceReturner:
128 void ReturnResources(
129 mojo::Array<mojo::ReturnedResourcePtr> resources) override;
131 void SetIdNamespace(uint32_t id_namespace);
133 static base::LazyInstance<
134 base::ThreadLocalPointer<ViewManagerToStateMap>>::Leaky view_states;
136 mojo::Shell* shell_;
137 mojo::ViewManager* view_manager_;
139 // Set of state needed to create an OutputSurface.
140 mojo::GpuPtr gpu_;
141 mojo::SurfacePtr surface_;
142 mojo::Binding<mojo::ResourceReturner> returner_binding_;
143 uint32_t id_namespace_;
144 uint32_t next_local_id_;
146 DISALLOW_COPY_AND_ASSIGN(PerViewManagerState);
149 // static
150 base::LazyInstance<base::ThreadLocalPointer<
151 SurfaceBinding::PerViewManagerState::ViewManagerToStateMap>>::Leaky
152 SurfaceBinding::PerViewManagerState::view_states;
154 // static
155 SurfaceBinding::PerViewManagerState* SurfaceBinding::PerViewManagerState::Get(
156 mojo::Shell* shell,
157 mojo::ViewManager* view_manager) {
158 ViewManagerToStateMap* view_map = view_states.Pointer()->Get();
159 if (!view_map) {
160 view_map = new ViewManagerToStateMap;
161 view_states.Pointer()->Set(view_map);
163 if (!(*view_map)[view_manager]) {
164 (*view_map)[view_manager] = new PerViewManagerState(shell, view_manager);
165 (*view_map)[view_manager]->Init();
167 return (*view_map)[view_manager];
170 scoped_ptr<cc::OutputSurface>
171 SurfaceBinding::PerViewManagerState::CreateOutputSurface(mojo::View* view) {
172 // TODO(sky): figure out lifetime here. Do I need to worry about the return
173 // value outliving this?
174 mojo::CommandBufferPtr cb;
175 gpu_->CreateOffscreenGLES2Context(GetProxy(&cb));
176 scoped_refptr<cc::ContextProvider> context_provider(
177 new mojo::ContextProviderMojo(cb.PassInterface().PassHandle()));
178 return make_scoped_ptr(new OutputSurfaceImpl(
179 view, context_provider, surface_.get(), id_namespace_, &next_local_id_));
182 SurfaceBinding::PerViewManagerState::PerViewManagerState(
183 mojo::Shell* shell,
184 mojo::ViewManager* view_manager)
185 : shell_(shell),
186 view_manager_(view_manager),
187 returner_binding_(this),
188 id_namespace_(0u),
189 next_local_id_(0u) {
192 SurfaceBinding::PerViewManagerState::~PerViewManagerState() {
193 ViewManagerToStateMap* view_map = view_states.Pointer()->Get();
194 DCHECK(view_map);
195 DCHECK_EQ(this, (*view_map)[view_manager_]);
196 view_map->erase(view_manager_);
197 if (view_map->empty()) {
198 delete view_map;
199 view_states.Pointer()->Set(nullptr);
203 void SurfaceBinding::PerViewManagerState::Init() {
204 DCHECK(!surface_.get());
206 mojo::ServiceProviderPtr surfaces_service_provider;
207 mojo::URLRequestPtr request(mojo::URLRequest::New());
208 request->url = mojo::String::From("mojo:surfaces_service");
209 shell_->ConnectToApplication(request.Pass(),
210 GetProxy(&surfaces_service_provider),
211 nullptr);
212 ConnectToService(surfaces_service_provider.get(), &surface_);
213 surface_->GetIdNamespace(
214 base::Bind(&SurfaceBinding::PerViewManagerState::SetIdNamespace,
215 base::Unretained(this)));
216 // Block until we receive our id namespace.
217 surface_.WaitForIncomingResponse();
218 DCHECK_NE(0u, id_namespace_);
220 mojo::ResourceReturnerPtr returner_ptr;
221 returner_binding_.Bind(GetProxy(&returner_ptr));
222 surface_->SetResourceReturner(returner_ptr.Pass());
224 mojo::ServiceProviderPtr gpu_service_provider;
225 // TODO(jamesr): Should be mojo:gpu_service
226 mojo::URLRequestPtr request2(mojo::URLRequest::New());
227 request2->url = mojo::String::From("mojo:view_manager");
228 shell_->ConnectToApplication(request2.Pass(),
229 GetProxy(&gpu_service_provider),
230 nullptr);
231 ConnectToService(gpu_service_provider.get(), &gpu_);
234 void SurfaceBinding::PerViewManagerState::SetIdNamespace(
235 uint32_t id_namespace) {
236 id_namespace_ = id_namespace;
239 void SurfaceBinding::PerViewManagerState::ReturnResources(
240 mojo::Array<mojo::ReturnedResourcePtr> resources) {
243 // SurfaceBinding --------------------------------------------------------------
245 SurfaceBinding::SurfaceBinding(mojo::Shell* shell, mojo::View* view)
246 : view_(view),
247 state_(PerViewManagerState::Get(shell, view->view_manager())) {
250 SurfaceBinding::~SurfaceBinding() {
253 scoped_ptr<cc::OutputSurface> SurfaceBinding::CreateOutputSurface() {
254 return state_->CreateOutputSurface(view_);
257 } // namespace mandoline