Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2)

Side by Side Diff: mojo/examples/surfaces_app/surfaces_app.cc

Issue 361123002: Mojo surfaces service and example app (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "base/bind.h"
6 #include "base/macros.h"
7 #include "base/message_loop/message_loop.h"
8 #include "cc/surfaces/surface_id_allocator.h"
9 #include "mojo/examples/surfaces_app/child.mojom.h"
10 #include "mojo/examples/surfaces_app/embedder.h"
11 #include "mojo/public/cpp/application/application_connection.h"
12 #include "mojo/public/cpp/application/application_delegate.h"
13 #include "mojo/public/cpp/system/core.h"
14 #include "mojo/public/interfaces/service_provider/service_provider.mojom.h"
15 #include "mojo/services/gles2/command_buffer.mojom.h"
16 #include "mojo/services/public/cpp/geometry/geometry_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"
19 #include "ui/gfx/rect.h"
20
21 namespace mojo {
22 namespace examples {
23
24 class SurfacesApp : public ApplicationDelegate,
25 public surfaces::SurfaceClient,
26 public NativeViewportClient {
27 public:
28 SurfacesApp() {}
29 virtual ~SurfacesApp() {}
30
31 // ApplicationDelegate implementation
32 virtual bool ConfigureIncomingConnection(
33 ApplicationConnection* connection) OVERRIDE {
34 connection->ConnectToService("mojo:mojo_native_viewport_service",
35 &viewport_);
36 viewport_.set_client(this);
37
38 connection->ConnectToService("mojo:mojo_surfaces_service", &surfaces_);
39 surfaces_.set_client(this);
40
41 size_ = gfx::Size(800, 600);
42
43 viewport_->Create(Rect::From(gfx::Rect(gfx::Point(10, 10), size_)));
44 viewport_->Show();
45
46 child_size_ = gfx::Size(size_.width() / 3, size_.height() / 2);
47 connection->ConnectToService("mojo:mojo_surfaces_child_app", &child_one_);
48 connection->ConnectToService("mojo:mojo_surfaces_child_app", &child_two_);
49 child_one_->ProduceFrame(surfaces::Color::From(SK_ColorBLUE),
50 Size::From(child_size_),
51 base::Bind(&SurfacesApp::ChildOneProducedFrame,
52 base::Unretained(this)));
53 child_two_->ProduceFrame(surfaces::Color::From(SK_ColorGREEN),
54 Size::From(child_size_),
55 base::Bind(&SurfacesApp::ChildTwoProducedFrame,
56 base::Unretained(this)));
57 embedder_.reset(new Embedder(surfaces_.get()));
58 return true;
59 }
60
61 void ChildOneProducedFrame(surfaces::SurfaceIdPtr id) {
62 child_one_id_ = id.To<cc::SurfaceId>();
63 Draw(45.0);
64 }
65
66 void ChildTwoProducedFrame(surfaces::SurfaceIdPtr id) {
67 child_two_id_ = id.To<cc::SurfaceId>();
68 Draw(45.0);
69 }
70
71 void Draw(double rotation_degrees) {
72 if (onscreen_id_.is_null()) {
73 onscreen_id_ = allocator_->GenerateId();
74 CommandBufferPtr cb;
75 viewport_->CreateGLES2Context(Get(&cb));
76 surfaces_->CreateGLES2BoundSurface(
77 cb.Pass(),
78 surfaces::SurfaceId::From(onscreen_id_),
79 Size::From(size_));
80 embedder_->SetSurfaceId(onscreen_id_);
81 }
82 if (child_one_id_.is_null() || child_two_id_.is_null())
83 return;
84 embedder_->ProduceFrame(
85 child_one_id_, child_two_id_, child_size_, size_, rotation_degrees);
86 base::MessageLoop::current()->PostDelayedTask(
87 FROM_HERE,
88 base::Bind(
89 &SurfacesApp::Draw, base::Unretained(this), rotation_degrees + 2.0),
90 base::TimeDelta::FromMilliseconds(17));
91 }
92
93 // surfaces::SurfaceClient implementation.
94 virtual void SetIdNamespace(uint32_t id_namespace) OVERRIDE {
95 allocator_.reset(new cc::SurfaceIdAllocator(id_namespace));
96 Draw(45.0);
97 }
98 virtual void ReturnResources(
99 Array<surfaces::ReturnedResourcePtr> resources) OVERRIDE {
100 DCHECK(!resources.size());
101 }
102
103 // NativeViewportClient implementation
104 virtual void OnCreated() OVERRIDE {}
105 virtual void OnBoundsChanged(mojo::RectPtr bounds) OVERRIDE {}
106 virtual void OnDestroyed() OVERRIDE {}
107 virtual void OnEvent(mojo::EventPtr event,
108 const mojo::Callback<void()>& callback) OVERRIDE {
109 callback.Run();
110 }
111
112 private:
113 surfaces::SurfacePtr surfaces_;
114 cc::SurfaceId onscreen_id_;
115 scoped_ptr<cc::SurfaceIdAllocator> allocator_;
116 scoped_ptr<Embedder> embedder_;
117 ChildPtr child_one_;
118 cc::SurfaceId child_one_id_;
119 ChildPtr child_two_;
120 cc::SurfaceId child_two_id_;
121 gfx::Size size_;
122 gfx::Size child_size_;
123
124 NativeViewportPtr viewport_;
125
126 DISALLOW_COPY_AND_ASSIGN(SurfacesApp);
127 };
128
129 } // namespace examples
130
131 // static
132 ApplicationDelegate* ApplicationDelegate::Create() {
133 return new examples::SurfacesApp();
134 }
135
136 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698