Re-land: C++ readability review
[chromium-blink-merge.git] / mojo / services / surfaces / display_impl.cc
blob3fff760d2935513523b4109101ea9030e5add42d
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 "mojo/services/surfaces/display_impl.h"
7 #include "cc/output/compositor_frame.h"
8 #include "cc/surfaces/display.h"
9 #include "mojo/converters/geometry/geometry_type_converters.h"
10 #include "mojo/converters/surfaces/surfaces_type_converters.h"
11 #include "mojo/services/surfaces/context_provider_mojo.h"
12 #include "mojo/services/surfaces/surfaces_output_surface.h"
13 #include "mojo/services/surfaces/surfaces_scheduler.h"
15 namespace surfaces {
16 namespace {
17 void CallCallback(const mojo::Closure& callback, cc::SurfaceDrawStatus status) {
18 callback.Run();
22 DisplayImpl::DisplayImpl(cc::SurfaceManager* manager,
23 cc::SurfaceId cc_id,
24 SurfacesScheduler* scheduler,
25 mojo::ContextProviderPtr context_provider,
26 mojo::ResourceReturnerPtr returner,
27 mojo::InterfaceRequest<mojo::Display> display_request)
28 : manager_(manager),
29 factory_(manager, this),
30 cc_id_(cc_id),
31 scheduler_(scheduler),
32 context_provider_(context_provider.Pass()),
33 returner_(returner.Pass()),
34 viewport_param_binding_(this),
35 display_binding_(this, display_request.Pass()) {
36 mojo::ViewportParameterListenerPtr viewport_parameter_listener;
37 viewport_param_binding_.Bind(GetProxy(&viewport_parameter_listener));
38 context_provider_->Create(
39 viewport_parameter_listener.Pass(),
40 base::Bind(&DisplayImpl::OnContextCreated, base::Unretained(this)));
43 void DisplayImpl::OnContextCreated(mojo::CommandBufferPtr gles2_client) {
44 DCHECK(!display_);
46 cc::RendererSettings settings;
47 display_.reset(new cc::Display(this, manager_, nullptr, nullptr, settings));
48 scheduler_->AddDisplay(display_.get());
49 display_->Initialize(make_scoped_ptr(new mojo::DirectOutputSurface(
50 new mojo::ContextProviderMojo(gles2_client.PassMessagePipe()))));
52 factory_.Create(cc_id_);
53 display_->SetSurfaceId(cc_id_, 1.f);
54 if (pending_frame_)
55 Draw();
58 DisplayImpl::~DisplayImpl() {
59 if (display_) {
60 factory_.Destroy(cc_id_);
61 scheduler_->RemoveDisplay(display_.get());
65 void DisplayImpl::SubmitFrame(mojo::FramePtr frame,
66 const SubmitFrameCallback& callback) {
67 DCHECK(pending_callback_.is_null());
68 pending_frame_ = frame.Pass();
69 pending_callback_ = callback;
70 if (display_)
71 Draw();
74 void DisplayImpl::Draw() {
75 gfx::Size frame_size =
76 pending_frame_->passes[0]->output_rect.To<gfx::Rect>().size();
77 display_->Resize(frame_size);
78 factory_.SubmitFrame(cc_id_,
79 pending_frame_.To<scoped_ptr<cc::CompositorFrame>>(),
80 base::Bind(&CallCallback, pending_callback_));
81 scheduler_->SetNeedsDraw();
82 pending_callback_.reset();
85 void DisplayImpl::DisplayDamaged() {
88 void DisplayImpl::DidSwapBuffers() {
91 void DisplayImpl::DidSwapBuffersComplete() {
94 void DisplayImpl::CommitVSyncParameters(base::TimeTicks timebase,
95 base::TimeDelta interval) {
98 void DisplayImpl::OutputSurfaceLost() {
99 // If our OutputSurface is lost we can't draw until we get a new one. For now,
100 // destroy the display and create a new one when our ContextProvider provides
101 // a new one.
102 // TODO: This is more violent than necessary - we could simply remove this
103 // display from the scheduler's set and pass a new context in to the
104 // OutputSurface. It should be able to reinitialize properly.
105 scheduler_->RemoveDisplay(display_.get());
106 display_.reset();
107 factory_.Destroy(cc_id_);
108 viewport_param_binding_.Close();
109 mojo::ViewportParameterListenerPtr viewport_parameter_listener;
110 viewport_param_binding_.Bind(GetProxy(&viewport_parameter_listener));
111 context_provider_->Create(
112 viewport_parameter_listener.Pass(),
113 base::Bind(&DisplayImpl::OnContextCreated, base::Unretained(this)));
116 void DisplayImpl::SetMemoryPolicy(const cc::ManagedMemoryPolicy& policy) {
119 void DisplayImpl::OnVSyncParametersUpdated(int64_t timebase, int64_t interval) {
120 scheduler_->OnVSyncParametersUpdated(
121 base::TimeTicks::FromInternalValue(timebase),
122 base::TimeDelta::FromInternalValue(interval));
125 void DisplayImpl::ReturnResources(const cc::ReturnedResourceArray& resources) {
126 if (resources.empty())
127 return;
128 DCHECK(returner_);
130 mojo::Array<mojo::ReturnedResourcePtr> ret(resources.size());
131 for (size_t i = 0; i < resources.size(); ++i) {
132 ret[i] = mojo::ReturnedResource::From(resources[i]);
134 returner_->ReturnResources(ret.Pass());
137 } // namespace surfaces