| 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/c/system/main.h" | 11 #include "mojo/public/c/system/main.h" |
| 12 #include "mojo/public/cpp/application/application_connection.h" | 12 #include "mojo/public/cpp/application/application_connection.h" |
| 13 #include "mojo/public/cpp/application/application_delegate.h" | 13 #include "mojo/public/cpp/application/application_delegate.h" |
| 14 #include "mojo/public/cpp/application/application_runner_chromium.h" | 14 #include "mojo/public/cpp/application/application_runner_chromium.h" |
| 15 #include "mojo/public/cpp/system/core.h" | 15 #include "mojo/public/cpp/system/core.h" |
| 16 #include "mojo/services/gles2/command_buffer.mojom.h" | 16 #include "mojo/services/gles2/command_buffer.mojom.h" |
| 17 #include "mojo/services/public/cpp/geometry/geometry_type_converters.h" | 17 #include "mojo/services/public/cpp/geometry/geometry_type_converters.h" |
| 18 #include "mojo/services/public/cpp/surfaces/surfaces_type_converters.h" | 18 #include "mojo/services/public/cpp/surfaces/surfaces_type_converters.h" |
| 19 #include "mojo/services/public/interfaces/gpu/gpu.mojom.h" | 19 #include "mojo/services/public/interfaces/gpu/gpu.mojom.h" |
| 20 #include "mojo/services/public/interfaces/native_viewport/native_viewport.mojom.
h" | 20 #include "mojo/services/public/interfaces/native_viewport/native_viewport.mojom.
h" |
| 21 #include "mojo/services/public/interfaces/surfaces/surfaces.mojom.h" |
| 22 #include "mojo/services/public/interfaces/surfaces/surfaces_service.mojom.h" |
| 21 #include "ui/gfx/rect.h" | 23 #include "ui/gfx/rect.h" |
| 22 | 24 |
| 23 namespace mojo { | 25 namespace mojo { |
| 24 namespace examples { | 26 namespace examples { |
| 25 | 27 |
| 26 class SurfacesApp : public ApplicationDelegate, | 28 class SurfacesApp : public ApplicationDelegate, |
| 27 public SurfaceClient, | 29 public SurfaceClient, |
| 28 public NativeViewportClient { | 30 public NativeViewportClient { |
| 29 public: | 31 public: |
| 30 SurfacesApp() : native_viewport_id_(0) {} | 32 SurfacesApp() : native_viewport_id_(0) {} |
| 31 virtual ~SurfacesApp() {} | 33 virtual ~SurfacesApp() {} |
| 32 | 34 |
| 33 // ApplicationDelegate implementation | 35 // ApplicationDelegate implementation |
| 34 virtual bool ConfigureIncomingConnection( | 36 virtual bool ConfigureIncomingConnection( |
| 35 ApplicationConnection* connection) OVERRIDE { | 37 ApplicationConnection* connection) OVERRIDE { |
| 36 connection->ConnectToService("mojo:mojo_native_viewport_service", | 38 connection->ConnectToService("mojo:mojo_native_viewport_service", |
| 37 &viewport_); | 39 &viewport_); |
| 38 viewport_.set_client(this); | 40 viewport_.set_client(this); |
| 39 | 41 |
| 40 // TODO(jamesr): Should be mojo:mojo_gpu_service | 42 // TODO(jamesr): Should be mojo:mojo_gpu_service |
| 41 connection->ConnectToService("mojo:mojo_native_viewport_service", | 43 connection->ConnectToService("mojo:mojo_native_viewport_service", |
| 42 &gpu_service_); | 44 &gpu_service_); |
| 43 | 45 |
| 44 connection->ConnectToService("mojo:mojo_surfaces_service", &surfaces_); | 46 connection->ConnectToService("mojo:mojo_surfaces_service", |
| 45 surfaces_.set_client(this); | 47 &surfaces_service_); |
| 48 surfaces_service_->CreateSurfaceConnection(base::Bind( |
| 49 &SurfacesApp::SurfaceConnectionCreated, base::Unretained(this))); |
| 46 | 50 |
| 47 size_ = gfx::Size(800, 600); | 51 size_ = gfx::Size(800, 600); |
| 48 | 52 |
| 49 viewport_->Create(Rect::From(gfx::Rect(gfx::Point(10, 10), size_))); | 53 viewport_->Create(Rect::From(gfx::Rect(gfx::Point(10, 10), size_))); |
| 50 viewport_->Show(); | 54 viewport_->Show(); |
| 51 | 55 |
| 52 child_size_ = gfx::Size(size_.width() / 3, size_.height() / 2); | 56 child_size_ = gfx::Size(size_.width() / 3, size_.height() / 2); |
| 53 connection->ConnectToService("mojo:mojo_surfaces_child_app", &child_one_); | 57 connection->ConnectToService("mojo:mojo_surfaces_child_app", &child_one_); |
| 54 connection->ConnectToService("mojo:mojo_surfaces_child_gl_app", | 58 connection->ConnectToService("mojo:mojo_surfaces_child_gl_app", |
| 55 &child_two_); | 59 &child_two_); |
| 56 child_one_->ProduceFrame(Color::From(SK_ColorBLUE), | 60 child_one_->ProduceFrame(Color::From(SK_ColorBLUE), |
| 57 Size::From(child_size_), | 61 Size::From(child_size_), |
| 58 base::Bind(&SurfacesApp::ChildOneProducedFrame, | 62 base::Bind(&SurfacesApp::ChildOneProducedFrame, |
| 59 base::Unretained(this))); | 63 base::Unretained(this))); |
| 60 child_two_->ProduceFrame(Color::From(SK_ColorGREEN), | 64 child_two_->ProduceFrame(Color::From(SK_ColorGREEN), |
| 61 Size::From(child_size_), | 65 Size::From(child_size_), |
| 62 base::Bind(&SurfacesApp::ChildTwoProducedFrame, | 66 base::Bind(&SurfacesApp::ChildTwoProducedFrame, |
| 63 base::Unretained(this))); | 67 base::Unretained(this))); |
| 64 embedder_.reset(new Embedder(surfaces_.get())); | |
| 65 return true; | 68 return true; |
| 66 } | 69 } |
| 67 | 70 |
| 68 void ChildOneProducedFrame(SurfaceIdPtr id) { | 71 void ChildOneProducedFrame(SurfaceIdPtr id) { |
| 69 child_one_id_ = id.To<cc::SurfaceId>(); | 72 child_one_id_ = id.To<cc::SurfaceId>(); |
| 70 Draw(10); | 73 Draw(10); |
| 71 } | 74 } |
| 72 | 75 |
| 73 void ChildTwoProducedFrame(SurfaceIdPtr id) { | 76 void ChildTwoProducedFrame(SurfaceIdPtr id) { |
| 74 child_two_id_ = id.To<cc::SurfaceId>(); | 77 child_two_id_ = id.To<cc::SurfaceId>(); |
| 75 Draw(10); | 78 Draw(10); |
| 76 } | 79 } |
| 77 | 80 |
| 78 void Draw(int offset) { | 81 void Draw(int offset) { |
| 79 if (onscreen_id_.is_null() || child_one_id_.is_null() || | 82 if (onscreen_id_.is_null() || child_one_id_.is_null() || |
| 80 child_two_id_.is_null()) | 83 child_two_id_.is_null()) |
| 81 return; | 84 return; |
| 82 int bounced_offset = offset; | 85 int bounced_offset = offset; |
| 83 if (offset > 200) | 86 if (offset > 200) |
| 84 bounced_offset = 400 - offset; | 87 bounced_offset = 400 - offset; |
| 85 embedder_->ProduceFrame( | 88 embedder_->ProduceFrame( |
| 86 child_one_id_, child_two_id_, child_size_, size_, bounced_offset); | 89 child_one_id_, child_two_id_, child_size_, size_, bounced_offset); |
| 87 base::MessageLoop::current()->PostDelayedTask( | 90 base::MessageLoop::current()->PostDelayedTask( |
| 88 FROM_HERE, | 91 FROM_HERE, |
| 89 base::Bind( | 92 base::Bind( |
| 90 &SurfacesApp::Draw, base::Unretained(this), (offset + 2) % 400), | 93 &SurfacesApp::Draw, base::Unretained(this), (offset + 2) % 400), |
| 91 base::TimeDelta::FromMilliseconds(50)); | 94 base::TimeDelta::FromMilliseconds(50)); |
| 92 } | 95 } |
| 93 | 96 |
| 94 // SurfaceClient implementation. | 97 void SurfaceConnectionCreated(SurfacePtr surface, uint32_t id_namespace) { |
| 95 virtual void SetIdNamespace(uint32_t id_namespace) OVERRIDE { | 98 surface_ = surface.Pass(); |
| 99 surface_.set_client(this); |
| 100 embedder_.reset(new Embedder(surface_.get())); |
| 96 allocator_.reset(new cc::SurfaceIdAllocator(id_namespace)); | 101 allocator_.reset(new cc::SurfaceIdAllocator(id_namespace)); |
| 97 CreateSurfaceIfReady(); | 102 CreateSurfaceIfReady(); |
| 98 } | 103 } |
| 99 virtual void ReturnResources( | 104 virtual void ReturnResources( |
| 100 Array<ReturnedResourcePtr> resources) OVERRIDE { | 105 Array<ReturnedResourcePtr> resources) OVERRIDE { |
| 101 DCHECK(!resources.size()); | 106 DCHECK(!resources.size()); |
| 102 } | 107 } |
| 103 | 108 |
| 104 // NativeViewportClient implementation | 109 // NativeViewportClient implementation |
| 105 virtual void OnCreated(uint64_t native_viewport_id) OVERRIDE { | 110 virtual void OnCreated(uint64_t native_viewport_id) OVERRIDE { |
| (...skipping 11 matching lines...) Expand all Loading... |
| 117 if (native_viewport_id_ == 0) | 122 if (native_viewport_id_ == 0) |
| 118 return; | 123 return; |
| 119 if (!allocator_) | 124 if (!allocator_) |
| 120 return; | 125 return; |
| 121 | 126 |
| 122 onscreen_id_ = allocator_->GenerateId(); | 127 onscreen_id_ = allocator_->GenerateId(); |
| 123 embedder_->SetSurfaceId(onscreen_id_); | 128 embedder_->SetSurfaceId(onscreen_id_); |
| 124 CommandBufferPtr cb; | 129 CommandBufferPtr cb; |
| 125 gpu_service_->CreateOnscreenGLES2Context( | 130 gpu_service_->CreateOnscreenGLES2Context( |
| 126 native_viewport_id_, Size::From(size_), Get(&cb)); | 131 native_viewport_id_, Size::From(size_), Get(&cb)); |
| 127 surfaces_->CreateGLES2BoundSurface( | 132 surface_->CreateGLES2BoundSurface( |
| 128 cb.Pass(), SurfaceId::From(onscreen_id_), Size::From(size_)); | 133 cb.Pass(), SurfaceId::From(onscreen_id_), Size::From(size_)); |
| 129 Draw(10); | 134 Draw(10); |
| 130 } | 135 } |
| 131 virtual void OnBoundsChanged(mojo::RectPtr bounds) OVERRIDE {} | 136 virtual void OnBoundsChanged(mojo::RectPtr bounds) OVERRIDE {} |
| 132 virtual void OnDestroyed() OVERRIDE {} | 137 virtual void OnDestroyed() OVERRIDE {} |
| 133 virtual void OnEvent(mojo::EventPtr event, | 138 virtual void OnEvent(mojo::EventPtr event, |
| 134 const mojo::Callback<void()>& callback) OVERRIDE { | 139 const mojo::Callback<void()>& callback) OVERRIDE { |
| 135 callback.Run(); | 140 callback.Run(); |
| 136 } | 141 } |
| 137 | 142 |
| 138 private: | 143 private: |
| 139 SurfacePtr surfaces_; | 144 SurfacesServicePtr surfaces_service_; |
| 145 SurfacePtr surface_; |
| 140 cc::SurfaceId onscreen_id_; | 146 cc::SurfaceId onscreen_id_; |
| 141 uint64_t native_viewport_id_; | 147 uint64_t native_viewport_id_; |
| 142 scoped_ptr<cc::SurfaceIdAllocator> allocator_; | 148 scoped_ptr<cc::SurfaceIdAllocator> allocator_; |
| 143 scoped_ptr<Embedder> embedder_; | 149 scoped_ptr<Embedder> embedder_; |
| 144 ChildPtr child_one_; | 150 ChildPtr child_one_; |
| 145 cc::SurfaceId child_one_id_; | 151 cc::SurfaceId child_one_id_; |
| 146 ChildPtr child_two_; | 152 ChildPtr child_two_; |
| 147 cc::SurfaceId child_two_id_; | 153 cc::SurfaceId child_two_id_; |
| 148 gfx::Size size_; | 154 gfx::Size size_; |
| 149 gfx::Size child_size_; | 155 gfx::Size child_size_; |
| 150 | 156 |
| 151 NativeViewportPtr viewport_; | 157 NativeViewportPtr viewport_; |
| 152 GpuPtr gpu_service_; | 158 GpuPtr gpu_service_; |
| 153 | 159 |
| 154 DISALLOW_COPY_AND_ASSIGN(SurfacesApp); | 160 DISALLOW_COPY_AND_ASSIGN(SurfacesApp); |
| 155 }; | 161 }; |
| 156 | 162 |
| 157 } // namespace examples | 163 } // namespace examples |
| 158 } // namespace mojo | 164 } // namespace mojo |
| 159 | 165 |
| 160 MojoResult MojoMain(MojoHandle shell_handle) { | 166 MojoResult MojoMain(MojoHandle shell_handle) { |
| 161 mojo::ApplicationRunnerChromium runner(new mojo::examples::SurfacesApp); | 167 mojo::ApplicationRunnerChromium runner(new mojo::examples::SurfacesApp); |
| 162 return runner.Run(shell_handle); | 168 return runner.Run(shell_handle); |
| 163 } | 169 } |
| OLD | NEW |