OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/bind.h" | 5 #include "base/bind.h" |
6 #include "base/macros.h" | 6 #include "base/macros.h" |
7 #include "base/message_loop/message_loop.h" | 7 #include "base/message_loop/message_loop.h" |
8 #include "cc/surfaces/surface_id_allocator.h" | 8 #include "cc/surfaces/surface_id_allocator.h" |
9 #include "mojo/examples/surfaces_app/child.mojom.h" | 9 #include "mojo/examples/surfaces_app/child.mojom.h" |
10 #include "mojo/examples/surfaces_app/embedder.h" | 10 #include "mojo/examples/surfaces_app/embedder.h" |
11 #include "mojo/public/cpp/application/application_connection.h" | 11 #include "mojo/public/cpp/application/application_connection.h" |
12 #include "mojo/public/cpp/application/application_delegate.h" | 12 #include "mojo/public/cpp/application/application_delegate.h" |
13 #include "mojo/public/cpp/system/core.h" | 13 #include "mojo/public/cpp/system/core.h" |
14 #include "mojo/services/gles2/command_buffer.mojom.h" | 14 #include "mojo/services/gles2/command_buffer.mojom.h" |
15 #include "mojo/services/public/cpp/geometry/geometry_type_converters.h" | 15 #include "mojo/services/public/cpp/geometry/geometry_type_converters.h" |
16 #include "mojo/services/public/cpp/surfaces/surfaces_type_converters.h" | 16 #include "mojo/services/public/cpp/surfaces/surfaces_type_converters.h" |
| 17 #include "mojo/services/public/interfaces/gpu/gpu.mojom.h" |
17 #include "mojo/services/public/interfaces/native_viewport/native_viewport.mojom.
h" | 18 #include "mojo/services/public/interfaces/native_viewport/native_viewport.mojom.
h" |
18 #include "ui/gfx/rect.h" | 19 #include "ui/gfx/rect.h" |
19 | 20 |
20 namespace mojo { | 21 namespace mojo { |
21 namespace examples { | 22 namespace examples { |
22 | 23 |
23 class SurfacesApp : public ApplicationDelegate, | 24 class SurfacesApp : public ApplicationDelegate, |
24 public SurfaceClient, | 25 public SurfaceClient, |
25 public NativeViewportClient { | 26 public NativeViewportClient { |
26 public: | 27 public: |
27 SurfacesApp() {} | 28 SurfacesApp() : native_viewport_id_(0) {} |
28 virtual ~SurfacesApp() {} | 29 virtual ~SurfacesApp() {} |
29 | 30 |
30 // ApplicationDelegate implementation | 31 // ApplicationDelegate implementation |
31 virtual bool ConfigureIncomingConnection( | 32 virtual bool ConfigureIncomingConnection( |
32 ApplicationConnection* connection) OVERRIDE { | 33 ApplicationConnection* connection) OVERRIDE { |
33 connection->ConnectToService("mojo:mojo_native_viewport_service", | 34 connection->ConnectToService("mojo:mojo_native_viewport_service", |
34 &viewport_); | 35 &viewport_); |
35 viewport_.set_client(this); | 36 viewport_.set_client(this); |
36 | 37 |
| 38 // TODO(jamesr): Should be mojo:mojo_gpu_service |
| 39 connection->ConnectToService("mojo:mojo_native_viewport_service", |
| 40 &gpu_service_); |
| 41 |
37 connection->ConnectToService("mojo:mojo_surfaces_service", &surfaces_); | 42 connection->ConnectToService("mojo:mojo_surfaces_service", &surfaces_); |
38 surfaces_.set_client(this); | 43 surfaces_.set_client(this); |
39 | 44 |
40 size_ = gfx::Size(800, 600); | 45 size_ = gfx::Size(800, 600); |
41 | 46 |
42 viewport_->Create(Rect::From(gfx::Rect(gfx::Point(10, 10), size_))); | 47 viewport_->Create(Rect::From(gfx::Rect(gfx::Point(10, 10), size_))); |
43 viewport_->Show(); | 48 viewport_->Show(); |
44 | 49 |
45 child_size_ = gfx::Size(size_.width() / 3, size_.height() / 2); | 50 child_size_ = gfx::Size(size_.width() / 3, size_.height() / 2); |
46 connection->ConnectToService("mojo:mojo_surfaces_child_app", &child_one_); | 51 connection->ConnectToService("mojo:mojo_surfaces_child_app", &child_one_); |
47 connection->ConnectToService("mojo:mojo_surfaces_child_app", &child_two_); | 52 connection->ConnectToService("mojo:mojo_surfaces_child_gl_app", |
| 53 &child_two_); |
48 child_one_->ProduceFrame(Color::From(SK_ColorBLUE), | 54 child_one_->ProduceFrame(Color::From(SK_ColorBLUE), |
49 Size::From(child_size_), | 55 Size::From(child_size_), |
50 base::Bind(&SurfacesApp::ChildOneProducedFrame, | 56 base::Bind(&SurfacesApp::ChildOneProducedFrame, |
51 base::Unretained(this))); | 57 base::Unretained(this))); |
52 child_two_->ProduceFrame(Color::From(SK_ColorGREEN), | 58 child_two_->ProduceFrame(Color::From(SK_ColorGREEN), |
53 Size::From(child_size_), | 59 Size::From(child_size_), |
54 base::Bind(&SurfacesApp::ChildTwoProducedFrame, | 60 base::Bind(&SurfacesApp::ChildTwoProducedFrame, |
55 base::Unretained(this))); | 61 base::Unretained(this))); |
56 embedder_.reset(new Embedder(surfaces_.get())); | 62 embedder_.reset(new Embedder(surfaces_.get())); |
57 return true; | 63 return true; |
58 } | 64 } |
59 | 65 |
60 void ChildOneProducedFrame(SurfaceIdPtr id) { | 66 void ChildOneProducedFrame(SurfaceIdPtr id) { |
61 child_one_id_ = id.To<cc::SurfaceId>(); | 67 child_one_id_ = id.To<cc::SurfaceId>(); |
62 Draw(45.0); | 68 Draw(10); |
63 } | 69 } |
64 | 70 |
65 void ChildTwoProducedFrame(SurfaceIdPtr id) { | 71 void ChildTwoProducedFrame(SurfaceIdPtr id) { |
66 child_two_id_ = id.To<cc::SurfaceId>(); | 72 child_two_id_ = id.To<cc::SurfaceId>(); |
67 Draw(45.0); | 73 Draw(10); |
68 } | 74 } |
69 | 75 |
70 void Draw(double rotation_degrees) { | 76 void Draw(int offset) { |
71 if (onscreen_id_.is_null()) { | 77 if (onscreen_id_.is_null() || child_one_id_.is_null() || |
72 onscreen_id_ = allocator_->GenerateId(); | 78 child_two_id_.is_null()) |
73 CommandBufferPtr cb; | |
74 viewport_->CreateGLES2Context(Get(&cb)); | |
75 surfaces_->CreateGLES2BoundSurface( | |
76 cb.Pass(), | |
77 SurfaceId::From(onscreen_id_), | |
78 Size::From(size_)); | |
79 embedder_->SetSurfaceId(onscreen_id_); | |
80 } | |
81 if (child_one_id_.is_null() || child_two_id_.is_null()) | |
82 return; | 79 return; |
| 80 int bounced_offset = offset; |
| 81 if (offset > 200) |
| 82 bounced_offset = 400 - offset; |
83 embedder_->ProduceFrame( | 83 embedder_->ProduceFrame( |
84 child_one_id_, child_two_id_, child_size_, size_, rotation_degrees); | 84 child_one_id_, child_two_id_, child_size_, size_, bounced_offset); |
85 base::MessageLoop::current()->PostDelayedTask( | 85 base::MessageLoop::current()->PostDelayedTask( |
86 FROM_HERE, | 86 FROM_HERE, |
87 base::Bind( | 87 base::Bind( |
88 &SurfacesApp::Draw, base::Unretained(this), rotation_degrees + 2.0), | 88 &SurfacesApp::Draw, base::Unretained(this), (offset + 2) % 400), |
89 base::TimeDelta::FromMilliseconds(17)); | 89 base::TimeDelta::FromMilliseconds(50)); |
90 } | 90 } |
91 | 91 |
92 // SurfaceClient implementation. | 92 // SurfaceClient implementation. |
93 virtual void SetIdNamespace(uint32_t id_namespace) OVERRIDE { | 93 virtual void SetIdNamespace(uint32_t id_namespace) OVERRIDE { |
94 allocator_.reset(new cc::SurfaceIdAllocator(id_namespace)); | 94 allocator_.reset(new cc::SurfaceIdAllocator(id_namespace)); |
95 Draw(45.0); | 95 CreateSurfaceIfReady(); |
96 } | 96 } |
97 virtual void ReturnResources( | 97 virtual void ReturnResources( |
98 Array<ReturnedResourcePtr> resources) OVERRIDE { | 98 Array<ReturnedResourcePtr> resources) OVERRIDE { |
99 DCHECK(!resources.size()); | 99 DCHECK(!resources.size()); |
100 } | 100 } |
101 | 101 |
102 // NativeViewportClient implementation | 102 // NativeViewportClient implementation |
103 virtual void OnCreated() OVERRIDE {} | 103 virtual void OnCreated(uint64_t native_viewport_id) OVERRIDE { |
| 104 native_viewport_id_ = native_viewport_id; |
| 105 CreateSurfaceIfReady(); |
| 106 } |
| 107 |
| 108 // We can't create our GLES2-bound surface until we have our id namespace from |
| 109 // the surfaces service and our native viewport id from the native viewport |
| 110 // service. There's no way of knowing which we'll get first, so we just start |
| 111 // whenever both arrive. |
| 112 void CreateSurfaceIfReady() { |
| 113 if (!onscreen_id_.is_null()) |
| 114 return; |
| 115 if (native_viewport_id_ == 0) |
| 116 return; |
| 117 if (!allocator_) |
| 118 return; |
| 119 |
| 120 onscreen_id_ = allocator_->GenerateId(); |
| 121 embedder_->SetSurfaceId(onscreen_id_); |
| 122 CommandBufferPtr cb; |
| 123 gpu_service_->CreateOnscreenGLES2Context( |
| 124 native_viewport_id_, Size::From(size_), Get(&cb)); |
| 125 surfaces_->CreateGLES2BoundSurface( |
| 126 cb.Pass(), SurfaceId::From(onscreen_id_), Size::From(size_)); |
| 127 Draw(10); |
| 128 } |
104 virtual void OnBoundsChanged(mojo::RectPtr bounds) OVERRIDE {} | 129 virtual void OnBoundsChanged(mojo::RectPtr bounds) OVERRIDE {} |
105 virtual void OnDestroyed(const mojo::Callback<void()>& callback) OVERRIDE { | 130 virtual void OnDestroyed() OVERRIDE {} |
106 callback.Run(); | |
107 } | |
108 virtual void OnEvent(mojo::EventPtr event, | 131 virtual void OnEvent(mojo::EventPtr event, |
109 const mojo::Callback<void()>& callback) OVERRIDE { | 132 const mojo::Callback<void()>& callback) OVERRIDE { |
110 callback.Run(); | 133 callback.Run(); |
111 } | 134 } |
112 | 135 |
113 private: | 136 private: |
114 SurfacePtr surfaces_; | 137 SurfacePtr surfaces_; |
115 cc::SurfaceId onscreen_id_; | 138 cc::SurfaceId onscreen_id_; |
| 139 uint64_t native_viewport_id_; |
116 scoped_ptr<cc::SurfaceIdAllocator> allocator_; | 140 scoped_ptr<cc::SurfaceIdAllocator> allocator_; |
117 scoped_ptr<Embedder> embedder_; | 141 scoped_ptr<Embedder> embedder_; |
118 ChildPtr child_one_; | 142 ChildPtr child_one_; |
119 cc::SurfaceId child_one_id_; | 143 cc::SurfaceId child_one_id_; |
120 ChildPtr child_two_; | 144 ChildPtr child_two_; |
121 cc::SurfaceId child_two_id_; | 145 cc::SurfaceId child_two_id_; |
122 gfx::Size size_; | 146 gfx::Size size_; |
123 gfx::Size child_size_; | 147 gfx::Size child_size_; |
124 | 148 |
125 NativeViewportPtr viewport_; | 149 NativeViewportPtr viewport_; |
| 150 GpuPtr gpu_service_; |
126 | 151 |
127 DISALLOW_COPY_AND_ASSIGN(SurfacesApp); | 152 DISALLOW_COPY_AND_ASSIGN(SurfacesApp); |
128 }; | 153 }; |
129 | 154 |
130 } // namespace examples | 155 } // namespace examples |
131 | 156 |
132 // static | 157 // static |
133 ApplicationDelegate* ApplicationDelegate::Create() { | 158 ApplicationDelegate* ApplicationDelegate::Create() { |
134 return new examples::SurfacesApp(); | 159 return new examples::SurfacesApp(); |
135 } | 160 } |
136 | 161 |
137 } // namespace mojo | 162 } // namespace mojo |
OLD | NEW |