| 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/public/interfaces/service_provider/service_provider.mojom.h" | 14 #include "mojo/public/interfaces/service_provider/service_provider.mojom.h" |
| 15 #include "mojo/services/gles2/command_buffer.mojom.h" | 15 #include "mojo/services/gles2/command_buffer.mojom.h" |
| 16 #include "mojo/services/public/cpp/geometry/geometry_type_converters.h" | 16 #include "mojo/services/public/cpp/geometry/geometry_type_converters.h" |
| 17 #include "mojo/services/public/cpp/surfaces/surfaces_type_converters.h" | 17 #include "mojo/services/public/cpp/surfaces/surfaces_type_converters.h" |
| 18 #include "mojo/services/public/interfaces/native_viewport/native_viewport.mojom.
h" | 18 #include "mojo/services/public/interfaces/native_viewport/native_viewport.mojom.
h" |
| 19 #include "ui/gfx/rect.h" | 19 #include "ui/gfx/rect.h" |
| 20 | 20 |
| 21 namespace mojo { | 21 namespace mojo { |
| 22 namespace examples { | 22 namespace examples { |
| 23 | 23 |
| 24 class SurfacesApp : public ApplicationDelegate, | 24 class SurfacesApp : public ApplicationDelegate, |
| 25 public surfaces::SurfaceClient, | 25 public SurfaceClient, |
| 26 public NativeViewportClient { | 26 public NativeViewportClient { |
| 27 public: | 27 public: |
| 28 SurfacesApp() {} | 28 SurfacesApp() {} |
| 29 virtual ~SurfacesApp() {} | 29 virtual ~SurfacesApp() {} |
| 30 | 30 |
| 31 // ApplicationDelegate implementation | 31 // ApplicationDelegate implementation |
| 32 virtual bool ConfigureIncomingConnection( | 32 virtual bool ConfigureIncomingConnection( |
| 33 ApplicationConnection* connection) OVERRIDE { | 33 ApplicationConnection* connection) OVERRIDE { |
| 34 connection->ConnectToService("mojo:mojo_native_viewport_service", | 34 connection->ConnectToService("mojo:mojo_native_viewport_service", |
| 35 &viewport_); | 35 &viewport_); |
| 36 viewport_.set_client(this); | 36 viewport_.set_client(this); |
| 37 | 37 |
| 38 connection->ConnectToService("mojo:mojo_surfaces_service", &surfaces_); | 38 connection->ConnectToService("mojo:mojo_surfaces_service", &surfaces_); |
| 39 surfaces_.set_client(this); | 39 surfaces_.set_client(this); |
| 40 | 40 |
| 41 size_ = gfx::Size(800, 600); | 41 size_ = gfx::Size(800, 600); |
| 42 | 42 |
| 43 viewport_->Create(Rect::From(gfx::Rect(gfx::Point(10, 10), size_))); | 43 viewport_->Create(Rect::From(gfx::Rect(gfx::Point(10, 10), size_))); |
| 44 viewport_->Show(); | 44 viewport_->Show(); |
| 45 | 45 |
| 46 child_size_ = gfx::Size(size_.width() / 3, size_.height() / 2); | 46 child_size_ = gfx::Size(size_.width() / 3, size_.height() / 2); |
| 47 connection->ConnectToService("mojo:mojo_surfaces_child_app", &child_one_); | 47 connection->ConnectToService("mojo:mojo_surfaces_child_app", &child_one_); |
| 48 connection->ConnectToService("mojo:mojo_surfaces_child_app", &child_two_); | 48 connection->ConnectToService("mojo:mojo_surfaces_child_app", &child_two_); |
| 49 child_one_->ProduceFrame(surfaces::Color::From(SK_ColorBLUE), | 49 child_one_->ProduceFrame(Color::From(SK_ColorBLUE), |
| 50 Size::From(child_size_), | 50 Size::From(child_size_), |
| 51 base::Bind(&SurfacesApp::ChildOneProducedFrame, | 51 base::Bind(&SurfacesApp::ChildOneProducedFrame, |
| 52 base::Unretained(this))); | 52 base::Unretained(this))); |
| 53 child_two_->ProduceFrame(surfaces::Color::From(SK_ColorGREEN), | 53 child_two_->ProduceFrame(Color::From(SK_ColorGREEN), |
| 54 Size::From(child_size_), | 54 Size::From(child_size_), |
| 55 base::Bind(&SurfacesApp::ChildTwoProducedFrame, | 55 base::Bind(&SurfacesApp::ChildTwoProducedFrame, |
| 56 base::Unretained(this))); | 56 base::Unretained(this))); |
| 57 embedder_.reset(new Embedder(surfaces_.get())); | 57 embedder_.reset(new Embedder(surfaces_.get())); |
| 58 return true; | 58 return true; |
| 59 } | 59 } |
| 60 | 60 |
| 61 void ChildOneProducedFrame(surfaces::SurfaceIdPtr id) { | 61 void ChildOneProducedFrame(SurfaceIdPtr id) { |
| 62 child_one_id_ = id.To<cc::SurfaceId>(); | 62 child_one_id_ = id.To<cc::SurfaceId>(); |
| 63 Draw(45.0); | 63 Draw(45.0); |
| 64 } | 64 } |
| 65 | 65 |
| 66 void ChildTwoProducedFrame(surfaces::SurfaceIdPtr id) { | 66 void ChildTwoProducedFrame(SurfaceIdPtr id) { |
| 67 child_two_id_ = id.To<cc::SurfaceId>(); | 67 child_two_id_ = id.To<cc::SurfaceId>(); |
| 68 Draw(45.0); | 68 Draw(45.0); |
| 69 } | 69 } |
| 70 | 70 |
| 71 void Draw(double rotation_degrees) { | 71 void Draw(double rotation_degrees) { |
| 72 if (onscreen_id_.is_null()) { | 72 if (onscreen_id_.is_null()) { |
| 73 onscreen_id_ = allocator_->GenerateId(); | 73 onscreen_id_ = allocator_->GenerateId(); |
| 74 CommandBufferPtr cb; | 74 CommandBufferPtr cb; |
| 75 viewport_->CreateGLES2Context(Get(&cb)); | 75 viewport_->CreateGLES2Context(Get(&cb)); |
| 76 surfaces_->CreateGLES2BoundSurface( | 76 surfaces_->CreateGLES2BoundSurface( |
| 77 cb.Pass(), | 77 cb.Pass(), |
| 78 surfaces::SurfaceId::From(onscreen_id_), | 78 SurfaceId::From(onscreen_id_), |
| 79 Size::From(size_)); | 79 Size::From(size_)); |
| 80 embedder_->SetSurfaceId(onscreen_id_); | 80 embedder_->SetSurfaceId(onscreen_id_); |
| 81 } | 81 } |
| 82 if (child_one_id_.is_null() || child_two_id_.is_null()) | 82 if (child_one_id_.is_null() || child_two_id_.is_null()) |
| 83 return; | 83 return; |
| 84 embedder_->ProduceFrame( | 84 embedder_->ProduceFrame( |
| 85 child_one_id_, child_two_id_, child_size_, size_, rotation_degrees); | 85 child_one_id_, child_two_id_, child_size_, size_, rotation_degrees); |
| 86 base::MessageLoop::current()->PostDelayedTask( | 86 base::MessageLoop::current()->PostDelayedTask( |
| 87 FROM_HERE, | 87 FROM_HERE, |
| 88 base::Bind( | 88 base::Bind( |
| 89 &SurfacesApp::Draw, base::Unretained(this), rotation_degrees + 2.0), | 89 &SurfacesApp::Draw, base::Unretained(this), rotation_degrees + 2.0), |
| 90 base::TimeDelta::FromMilliseconds(17)); | 90 base::TimeDelta::FromMilliseconds(17)); |
| 91 } | 91 } |
| 92 | 92 |
| 93 // surfaces::SurfaceClient implementation. | 93 // SurfaceClient implementation. |
| 94 virtual void SetIdNamespace(uint32_t id_namespace) OVERRIDE { | 94 virtual void SetIdNamespace(uint32_t id_namespace) OVERRIDE { |
| 95 allocator_.reset(new cc::SurfaceIdAllocator(id_namespace)); | 95 allocator_.reset(new cc::SurfaceIdAllocator(id_namespace)); |
| 96 Draw(45.0); | 96 Draw(45.0); |
| 97 } | 97 } |
| 98 virtual void ReturnResources( | 98 virtual void ReturnResources( |
| 99 Array<surfaces::ReturnedResourcePtr> resources) OVERRIDE { | 99 Array<ReturnedResourcePtr> resources) OVERRIDE { |
| 100 DCHECK(!resources.size()); | 100 DCHECK(!resources.size()); |
| 101 } | 101 } |
| 102 | 102 |
| 103 // NativeViewportClient implementation | 103 // NativeViewportClient implementation |
| 104 virtual void OnCreated() OVERRIDE {} | 104 virtual void OnCreated() OVERRIDE {} |
| 105 virtual void OnBoundsChanged(mojo::RectPtr bounds) OVERRIDE {} | 105 virtual void OnBoundsChanged(mojo::RectPtr bounds) OVERRIDE {} |
| 106 virtual void OnDestroyed(const mojo::Callback<void()>& callback) OVERRIDE { | 106 virtual void OnDestroyed(const mojo::Callback<void()>& callback) OVERRIDE { |
| 107 callback.Run(); | 107 callback.Run(); |
| 108 } | 108 } |
| 109 virtual void OnEvent(mojo::EventPtr event, | 109 virtual void OnEvent(mojo::EventPtr event, |
| 110 const mojo::Callback<void()>& callback) OVERRIDE { | 110 const mojo::Callback<void()>& callback) OVERRIDE { |
| 111 callback.Run(); | 111 callback.Run(); |
| 112 } | 112 } |
| 113 | 113 |
| 114 private: | 114 private: |
| 115 surfaces::SurfacePtr surfaces_; | 115 SurfacePtr surfaces_; |
| 116 cc::SurfaceId onscreen_id_; | 116 cc::SurfaceId onscreen_id_; |
| 117 scoped_ptr<cc::SurfaceIdAllocator> allocator_; | 117 scoped_ptr<cc::SurfaceIdAllocator> allocator_; |
| 118 scoped_ptr<Embedder> embedder_; | 118 scoped_ptr<Embedder> embedder_; |
| 119 ChildPtr child_one_; | 119 ChildPtr child_one_; |
| 120 cc::SurfaceId child_one_id_; | 120 cc::SurfaceId child_one_id_; |
| 121 ChildPtr child_two_; | 121 ChildPtr child_two_; |
| 122 cc::SurfaceId child_two_id_; | 122 cc::SurfaceId child_two_id_; |
| 123 gfx::Size size_; | 123 gfx::Size size_; |
| 124 gfx::Size child_size_; | 124 gfx::Size child_size_; |
| 125 | 125 |
| 126 NativeViewportPtr viewport_; | 126 NativeViewportPtr viewport_; |
| 127 | 127 |
| 128 DISALLOW_COPY_AND_ASSIGN(SurfacesApp); | 128 DISALLOW_COPY_AND_ASSIGN(SurfacesApp); |
| 129 }; | 129 }; |
| 130 | 130 |
| 131 } // namespace examples | 131 } // namespace examples |
| 132 | 132 |
| 133 // static | 133 // static |
| 134 ApplicationDelegate* ApplicationDelegate::Create() { | 134 ApplicationDelegate* ApplicationDelegate::Create() { |
| 135 return new examples::SurfacesApp(); | 135 return new examples::SurfacesApp(); |
| 136 } | 136 } |
| 137 | 137 |
| 138 } // namespace mojo | 138 } // namespace mojo |
| OLD | NEW |