Roll harfbuzz-ng to 1.0.2
[chromium-blink-merge.git] / mandoline / ui / aura / surface_binding.cc
blob0307112933fceeb8ce58269451d00152e885bbbb
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_,
93 mojo::CompositorFrame::From(*frame),
94 mojo::Closure());
96 client_->DidSwapBuffers();
97 client_->DidSwapBuffersComplete();
100 } // namespace
102 // PerViewManagerState ---------------------------------------------------------
104 // State needed per ViewManager. Provides the real implementation of
105 // CreateOutputSurface. SurfaceBinding obtains a pointer to the
106 // PerViewManagerState appropriate for the ViewManager. PerViewManagerState is
107 // stored in a thread local map. When no more refereces to a PerViewManagerState
108 // remain the PerViewManagerState is deleted and the underlying map cleaned up.
109 class SurfaceBinding::PerViewManagerState
110 : public base::RefCounted<PerViewManagerState>,
111 public mojo::ResourceReturner {
112 public:
113 static PerViewManagerState* Get(mojo::Shell* shell,
114 mojo::ViewManager* view_manager);
116 scoped_ptr<cc::OutputSurface> CreateOutputSurface(mojo::View* view);
118 private:
119 typedef std::map<mojo::ViewManager*,
120 PerViewManagerState*> ViewManagerToStateMap;
122 friend class base::RefCounted<PerViewManagerState>;
124 PerViewManagerState(mojo::Shell* shell, mojo::ViewManager* view_manager);
125 ~PerViewManagerState() override;
127 void Init();
129 // mojo::ResourceReturner:
130 void ReturnResources(
131 mojo::Array<mojo::ReturnedResourcePtr> resources) override;
133 void SetIdNamespace(uint32_t id_namespace);
135 static base::LazyInstance<
136 base::ThreadLocalPointer<ViewManagerToStateMap>>::Leaky view_states;
138 mojo::Shell* shell_;
139 mojo::ViewManager* view_manager_;
141 // Set of state needed to create an OutputSurface.
142 mojo::GpuPtr gpu_;
143 mojo::SurfacePtr surface_;
144 mojo::Binding<mojo::ResourceReturner> returner_binding_;
145 uint32_t id_namespace_;
146 uint32_t next_local_id_;
148 DISALLOW_COPY_AND_ASSIGN(PerViewManagerState);
151 // static
152 base::LazyInstance<base::ThreadLocalPointer<
153 SurfaceBinding::PerViewManagerState::ViewManagerToStateMap>>::Leaky
154 SurfaceBinding::PerViewManagerState::view_states;
156 // static
157 SurfaceBinding::PerViewManagerState* SurfaceBinding::PerViewManagerState::Get(
158 mojo::Shell* shell,
159 mojo::ViewManager* view_manager) {
160 ViewManagerToStateMap* view_map = view_states.Pointer()->Get();
161 if (!view_map) {
162 view_map = new ViewManagerToStateMap;
163 view_states.Pointer()->Set(view_map);
165 if (!(*view_map)[view_manager]) {
166 (*view_map)[view_manager] = new PerViewManagerState(shell, view_manager);
167 (*view_map)[view_manager]->Init();
169 return (*view_map)[view_manager];
172 scoped_ptr<cc::OutputSurface>
173 SurfaceBinding::PerViewManagerState::CreateOutputSurface(mojo::View* view) {
174 // TODO(sky): figure out lifetime here. Do I need to worry about the return
175 // value outliving this?
176 mojo::CommandBufferPtr cb;
177 gpu_->CreateOffscreenGLES2Context(GetProxy(&cb));
178 scoped_refptr<cc::ContextProvider> context_provider(
179 new mojo::ContextProviderMojo(cb.PassInterface().PassHandle()));
180 return make_scoped_ptr(new OutputSurfaceImpl(
181 view, context_provider, surface_.get(), id_namespace_, &next_local_id_));
184 SurfaceBinding::PerViewManagerState::PerViewManagerState(
185 mojo::Shell* shell,
186 mojo::ViewManager* view_manager)
187 : shell_(shell),
188 view_manager_(view_manager),
189 returner_binding_(this),
190 id_namespace_(0u),
191 next_local_id_(0u) {
194 SurfaceBinding::PerViewManagerState::~PerViewManagerState() {
195 ViewManagerToStateMap* view_map = view_states.Pointer()->Get();
196 DCHECK(view_map);
197 DCHECK_EQ(this, (*view_map)[view_manager_]);
198 view_map->erase(view_manager_);
199 if (view_map->empty()) {
200 delete view_map;
201 view_states.Pointer()->Set(nullptr);
205 void SurfaceBinding::PerViewManagerState::Init() {
206 DCHECK(!surface_.get());
208 mojo::ServiceProviderPtr surfaces_service_provider;
209 mojo::URLRequestPtr request(mojo::URLRequest::New());
210 request->url = mojo::String::From("mojo:view_manager");
211 shell_->ConnectToApplication(request.Pass(),
212 GetProxy(&surfaces_service_provider),
213 nullptr,
214 nullptr);
215 ConnectToService(surfaces_service_provider.get(), &surface_);
216 surface_->GetIdNamespace(
217 base::Bind(&SurfaceBinding::PerViewManagerState::SetIdNamespace,
218 base::Unretained(this)));
219 // Block until we receive our id namespace.
220 surface_.WaitForIncomingResponse();
221 DCHECK_NE(0u, id_namespace_);
223 mojo::ResourceReturnerPtr returner_ptr;
224 returner_binding_.Bind(GetProxy(&returner_ptr));
225 surface_->SetResourceReturner(returner_ptr.Pass());
227 mojo::ServiceProviderPtr gpu_service_provider;
228 // TODO(jamesr): Should be mojo:gpu_service
229 mojo::URLRequestPtr request2(mojo::URLRequest::New());
230 request2->url = mojo::String::From("mojo:view_manager");
231 shell_->ConnectToApplication(request2.Pass(),
232 GetProxy(&gpu_service_provider),
233 nullptr,
234 nullptr);
235 ConnectToService(gpu_service_provider.get(), &gpu_);
238 void SurfaceBinding::PerViewManagerState::SetIdNamespace(
239 uint32_t id_namespace) {
240 id_namespace_ = id_namespace;
243 void SurfaceBinding::PerViewManagerState::ReturnResources(
244 mojo::Array<mojo::ReturnedResourcePtr> resources) {
247 // SurfaceBinding --------------------------------------------------------------
249 SurfaceBinding::SurfaceBinding(mojo::Shell* shell, mojo::View* view)
250 : view_(view),
251 state_(PerViewManagerState::Get(shell, view->view_manager())) {
254 SurfaceBinding::~SurfaceBinding() {
257 scoped_ptr<cc::OutputSurface> SurfaceBinding::CreateOutputSurface() {
258 return state_->CreateOutputSurface(view_);
261 } // namespace mandoline