| 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" | |
| 18 #include "mojo/services/public/interfaces/native_viewport/native_viewport.mojom.
h" | 17 #include "mojo/services/public/interfaces/native_viewport/native_viewport.mojom.
h" |
| 19 #include "ui/gfx/rect.h" | 18 #include "ui/gfx/rect.h" |
| 20 | 19 |
| 21 namespace mojo { | 20 namespace mojo { |
| 22 namespace examples { | 21 namespace examples { |
| 23 | 22 |
| 24 class SurfacesApp : public ApplicationDelegate, | 23 class SurfacesApp : public ApplicationDelegate, |
| 25 public SurfaceClient, | 24 public SurfaceClient, |
| 26 public NativeViewportClient { | 25 public NativeViewportClient { |
| 27 public: | 26 public: |
| 28 SurfacesApp() : native_viewport_id_(0) {} | 27 SurfacesApp() {} |
| 29 virtual ~SurfacesApp() {} | 28 virtual ~SurfacesApp() {} |
| 30 | 29 |
| 31 // ApplicationDelegate implementation | 30 // ApplicationDelegate implementation |
| 32 virtual bool ConfigureIncomingConnection( | 31 virtual bool ConfigureIncomingConnection( |
| 33 ApplicationConnection* connection) OVERRIDE { | 32 ApplicationConnection* connection) OVERRIDE { |
| 34 connection->ConnectToService("mojo:mojo_native_viewport_service", | 33 connection->ConnectToService("mojo:mojo_native_viewport_service", |
| 35 &viewport_); | 34 &viewport_); |
| 36 viewport_.set_client(this); | 35 viewport_.set_client(this); |
| 37 | 36 |
| 38 // TODO(jamesr): Should be mojo:mojo_gpu_service | |
| 39 connection->ConnectToService("mojo:mojo_native_viewport_service", | |
| 40 &gpu_service_); | |
| 41 | |
| 42 connection->ConnectToService("mojo:mojo_surfaces_service", &surfaces_); | 37 connection->ConnectToService("mojo:mojo_surfaces_service", &surfaces_); |
| 43 surfaces_.set_client(this); | 38 surfaces_.set_client(this); |
| 44 | 39 |
| 45 size_ = gfx::Size(800, 600); | 40 size_ = gfx::Size(800, 600); |
| 46 | 41 |
| 47 viewport_->Create(Rect::From(gfx::Rect(gfx::Point(10, 10), size_))); | 42 viewport_->Create(Rect::From(gfx::Rect(gfx::Point(10, 10), size_))); |
| 48 viewport_->Show(); | 43 viewport_->Show(); |
| 49 | 44 |
| 50 child_size_ = gfx::Size(size_.width() / 3, size_.height() / 2); | 45 child_size_ = gfx::Size(size_.width() / 3, size_.height() / 2); |
| 51 connection->ConnectToService("mojo:mojo_surfaces_child_app", &child_one_); | 46 connection->ConnectToService("mojo:mojo_surfaces_child_app", &child_one_); |
| 52 connection->ConnectToService("mojo:mojo_surfaces_child_gl_app", | 47 connection->ConnectToService("mojo:mojo_surfaces_child_app", &child_two_); |
| 53 &child_two_); | |
| 54 child_one_->ProduceFrame(Color::From(SK_ColorBLUE), | 48 child_one_->ProduceFrame(Color::From(SK_ColorBLUE), |
| 55 Size::From(child_size_), | 49 Size::From(child_size_), |
| 56 base::Bind(&SurfacesApp::ChildOneProducedFrame, | 50 base::Bind(&SurfacesApp::ChildOneProducedFrame, |
| 57 base::Unretained(this))); | 51 base::Unretained(this))); |
| 58 child_two_->ProduceFrame(Color::From(SK_ColorGREEN), | 52 child_two_->ProduceFrame(Color::From(SK_ColorGREEN), |
| 59 Size::From(child_size_), | 53 Size::From(child_size_), |
| 60 base::Bind(&SurfacesApp::ChildTwoProducedFrame, | 54 base::Bind(&SurfacesApp::ChildTwoProducedFrame, |
| 61 base::Unretained(this))); | 55 base::Unretained(this))); |
| 62 embedder_.reset(new Embedder(surfaces_.get())); | 56 embedder_.reset(new Embedder(surfaces_.get())); |
| 63 return true; | 57 return true; |
| 64 } | 58 } |
| 65 | 59 |
| 66 void ChildOneProducedFrame(SurfaceIdPtr id) { | 60 void ChildOneProducedFrame(SurfaceIdPtr id) { |
| 67 child_one_id_ = id.To<cc::SurfaceId>(); | 61 child_one_id_ = id.To<cc::SurfaceId>(); |
| 68 Draw(10); | 62 Draw(45.0); |
| 69 } | 63 } |
| 70 | 64 |
| 71 void ChildTwoProducedFrame(SurfaceIdPtr id) { | 65 void ChildTwoProducedFrame(SurfaceIdPtr id) { |
| 72 child_two_id_ = id.To<cc::SurfaceId>(); | 66 child_two_id_ = id.To<cc::SurfaceId>(); |
| 73 Draw(10); | 67 Draw(45.0); |
| 74 } | 68 } |
| 75 | 69 |
| 76 void Draw(int offset) { | 70 void Draw(double rotation_degrees) { |
| 77 if (onscreen_id_.is_null() || child_one_id_.is_null() || | 71 if (onscreen_id_.is_null()) { |
| 78 child_two_id_.is_null()) | 72 onscreen_id_ = allocator_->GenerateId(); |
| 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()) |
| 79 return; | 82 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_, bounced_offset); | 84 child_one_id_, child_two_id_, child_size_, size_, rotation_degrees); |
| 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), (offset + 2) % 400), | 88 &SurfacesApp::Draw, base::Unretained(this), rotation_degrees + 2.0), |
| 89 base::TimeDelta::FromMilliseconds(50)); | 89 base::TimeDelta::FromMilliseconds(17)); |
| 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 CreateSurfaceIfReady(); | 95 Draw(45.0); |
| 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(uint64_t native_viewport_id) OVERRIDE { | 103 virtual void OnCreated() OVERRIDE {} |
| 104 native_viewport_id_ = native_viewport_id; | 104 virtual void OnBoundsChanged(mojo::RectPtr bounds) OVERRIDE {} |
| 105 CreateSurfaceIfReady(); | 105 virtual void OnDestroyed(const mojo::Callback<void()>& callback) OVERRIDE { |
| 106 callback.Run(); |
| 106 } | 107 } |
| 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 } | |
| 129 virtual void OnBoundsChanged(mojo::RectPtr bounds) OVERRIDE {} | |
| 130 virtual void OnDestroyed() OVERRIDE {} | |
| 131 virtual void OnEvent(mojo::EventPtr event, | 108 virtual void OnEvent(mojo::EventPtr event, |
| 132 const mojo::Callback<void()>& callback) OVERRIDE { | 109 const mojo::Callback<void()>& callback) OVERRIDE { |
| 133 callback.Run(); | 110 callback.Run(); |
| 134 } | 111 } |
| 135 | 112 |
| 136 private: | 113 private: |
| 137 SurfacePtr surfaces_; | 114 SurfacePtr surfaces_; |
| 138 cc::SurfaceId onscreen_id_; | 115 cc::SurfaceId onscreen_id_; |
| 139 uint64_t native_viewport_id_; | |
| 140 scoped_ptr<cc::SurfaceIdAllocator> allocator_; | 116 scoped_ptr<cc::SurfaceIdAllocator> allocator_; |
| 141 scoped_ptr<Embedder> embedder_; | 117 scoped_ptr<Embedder> embedder_; |
| 142 ChildPtr child_one_; | 118 ChildPtr child_one_; |
| 143 cc::SurfaceId child_one_id_; | 119 cc::SurfaceId child_one_id_; |
| 144 ChildPtr child_two_; | 120 ChildPtr child_two_; |
| 145 cc::SurfaceId child_two_id_; | 121 cc::SurfaceId child_two_id_; |
| 146 gfx::Size size_; | 122 gfx::Size size_; |
| 147 gfx::Size child_size_; | 123 gfx::Size child_size_; |
| 148 | 124 |
| 149 NativeViewportPtr viewport_; | 125 NativeViewportPtr viewport_; |
| 150 GpuPtr gpu_service_; | |
| 151 | 126 |
| 152 DISALLOW_COPY_AND_ASSIGN(SurfacesApp); | 127 DISALLOW_COPY_AND_ASSIGN(SurfacesApp); |
| 153 }; | 128 }; |
| 154 | 129 |
| 155 } // namespace examples | 130 } // namespace examples |
| 156 | 131 |
| 157 // static | 132 // static |
| 158 ApplicationDelegate* ApplicationDelegate::Create() { | 133 ApplicationDelegate* ApplicationDelegate::Create() { |
| 159 return new examples::SurfacesApp(); | 134 return new examples::SurfacesApp(); |
| 160 } | 135 } |
| 161 | 136 |
| 162 } // namespace mojo | 137 } // namespace mojo |
| OLD | NEW |