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

Side by Side Diff: examples/ui/spinning_cube/spinning_cube_view.cc

Issue 1559723002: Update the UI examples. (Closed) Base URL: git@github.com:domokit/mojo.git@moz-14
Patch Set: address feedback Created 4 years, 11 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
« no previous file with comments | « examples/ui/spinning_cube/spinning_cube_view.h ('k') | examples/ui/tile/BUILD.gn » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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 "examples/ui/spinning_cube/spinning_cube_view.h"
6
7 #ifndef GL_GLEXT_PROTOTYPES
8 #define GL_GLEXT_PROTOTYPES
9 #endif
10
5 #include <GLES2/gl2.h> 11 #include <GLES2/gl2.h>
6 #include <GLES2/gl2extmojo.h> 12 #include <GLES2/gl2extmojo.h>
7 #include <MGL/mgl.h>
8 13
9 #include "base/message_loop/message_loop.h" 14 #include <cmath>
10 #include "examples/ui/spinning_cube/spinning_cube_view.h" 15
16 #include "base/bind.h"
17 #include "mojo/services/geometry/cpp/geometry_util.h"
11 18
12 namespace examples { 19 namespace examples {
13 20
21 namespace {
22 constexpr uint32_t kCubeImageResourceId = 1;
23 constexpr uint32_t kRootNodeId = mojo::gfx::composition::kSceneRootNodeId;
24
25 // TODO(johngro) : investigate extending mojom with a formal flags type which it
26 // generates good bindings for, so we don't need to resort to this.
27 constexpr bool operator&(const mojo::EventFlags& f1,
28 const mojo::EventFlags& f2) {
29 return ((static_cast<uint32_t>(f1) & static_cast<uint32_t>(f2)) != 0);
30 }
31
32 float CalculateDragDistance(const mojo::PointF& start,
33 const mojo::PointF& end) {
34 return std::hypot(start.x - end.x, start.y - end.y);
35 }
36
37 float GetRandomColor() {
38 return static_cast<float>(rand()) / static_cast<float>(RAND_MAX);
39 }
40
41 // Return a direction multiplier to apply to drag distances:
42 // 1 for natural (positive) motion, -1 for reverse (negative) motion
43 int GetEventDirection(const mojo::PointF& current,
44 const mojo::PointF& initial,
45 const mojo::PointF& last) {
46 // Axis of motion is determined by coarse alignment of overall movement
47 bool use_x =
48 std::abs(current.y - initial.y) < std::abs(current.x - initial.x);
49 // Current direction is determined by comparison with previous point
50 float delta = use_x ? (current.x - last.x) : (current.y - last.y);
51 return delta > 0 ? -1 : 1;
52 }
53 } // namespace
54
14 SpinningCubeView::SpinningCubeView( 55 SpinningCubeView::SpinningCubeView(
15 mojo::ApplicationImpl* app, 56 mojo::ApplicationImpl* app_impl,
16 const mojo::ui::ViewProvider::CreateViewCallback& callback) 57 const mojo::ui::ViewProvider::CreateViewCallback& create_view_callback)
17 : callback_(callback), 58 : GLView(app_impl, "SpinningCube", create_view_callback),
18 binding_(this), 59 choreographer_(scene(), this),
19 context_owner_(app->shell()), 60 input_handler_(view_service_provider(), this),
20 texture_cache_(context_owner_.context(), &resource_returner_),
21 surface_id_namespace_(0),
22 draw_scheduled_(false),
23 weak_ptr_factory_(this) { 61 weak_ptr_factory_(this) {
24 app->ConnectToService("mojo:surfaces_service", &surfaces_); 62 gl_renderer()->gl_context()->MakeCurrent();
25 app->ConnectToService("mojo:view_manager_service", &view_manager_); 63 cube_.Init();
26
27 surfaces_->SetResourceReturner(resource_returner_.Pass());
28 surfaces_->GetIdNamespace(
29 base::Bind(&SpinningCubeView::OnSurfaceIdNamespaceAvailable,
30 base::Unretained(this)));
31
32 InitCube();
33 } 64 }
34 65
35 SpinningCubeView::~SpinningCubeView() {} 66 SpinningCubeView::~SpinningCubeView() {}
36 67
37 void SpinningCubeView::OnSurfaceIdNamespaceAvailable(uint32_t id_namespace) {
38 surface_id_namespace_ = id_namespace;
39 InitView();
40 }
41
42 void SpinningCubeView::InitView() {
43 mojo::ui::ViewPtr view;
44 binding_.Bind(mojo::GetProxy(&view));
45 view_manager_->RegisterView(view.Pass(), mojo::GetProxy(&view_host_),
46 callback_);
47
48 view_host_->GetServiceProvider(mojo::GetProxy(&view_service_provider_));
49 }
50
51 void SpinningCubeView::OnLayout(mojo::ui::ViewLayoutParamsPtr layout_params, 68 void SpinningCubeView::OnLayout(mojo::ui::ViewLayoutParamsPtr layout_params,
52 mojo::Array<uint32_t> children_needing_layout, 69 mojo::Array<uint32_t> children_needing_layout,
53 const OnLayoutCallback& callback) { 70 const OnLayoutCallback& callback) {
54 // Create a new surface the first time or if the size has changed. 71 size_.width = layout_params->constraints->max_width;
55 mojo::Size new_size; 72 size_.height = layout_params->constraints->max_height;
56 new_size.width = layout_params->constraints->max_width;
57 new_size.height = layout_params->constraints->max_height;
58 if (!surface_id_ || !size_.Equals(new_size)) {
59 if (!surface_id_) {
60 surface_id_ = mojo::SurfaceId::New();
61 surface_id_->id_namespace = surface_id_namespace_;
62 } else {
63 surfaces_->DestroySurface(surface_id_->local);
64 }
65 surface_id_->local++;
66 size_ = new_size;
67 surfaces_->CreateSurface(surface_id_->local);
68 }
69 73
70 // Submit the new layout information. 74 // Submit the new layout information.
71 auto info = mojo::ui::ViewLayoutInfo::New(); 75 auto info = mojo::ui::ViewLayoutResult::New();
72 info->size = size_.Clone(); 76 info->size = size_.Clone();
73 info->surface_id = surface_id_->Clone();
74 callback.Run(info.Pass()); 77 callback.Run(info.Pass());
75 78
76 // Draw! 79 // Draw!
77 ScheduleDraw(); 80 choreographer_.ScheduleDraw();
78 } 81 }
79 82
80 void SpinningCubeView::OnChildUnavailable( 83 void SpinningCubeView::OnEvent(mojo::EventPtr event,
81 uint32_t child_key, 84 const OnEventCallback& callback) {
82 const OnChildUnavailableCallback& callback) { 85 if (!event->pointer_data) {
83 callback.Run(); 86 callback.Run(false);
84 }
85
86 void SpinningCubeView::InitCube() {
87 context_owner_.context()->MakeCurrent();
88 cube_.Init();
89 last_draw_ = mojo::GetTimeTicksNow();
90 }
91
92 void SpinningCubeView::DrawCube() {
93 draw_scheduled_ = false;
94
95 scoped_ptr<mojo::TextureCache::TextureInfo> texture_info =
96 texture_cache_.GetTexture(size_);
97 if (!texture_info) {
98 LOG(ERROR) << "Could not allocate texture of size " << size_.width << "x"
99 << size_.height;
100 return; 87 return;
101 } 88 }
102 89
103 context_owner_.context()->MakeCurrent(); 90 switch (event->action) {
104 scoped_ptr<mojo::GLTexture> texture = texture_info->TakeTexture(); 91 case mojo::EventType::POINTER_DOWN:
92 if (event->flags & mojo::EventFlags::RIGHT_MOUSE_BUTTON)
93 break;
94 capture_point_.x = event->pointer_data->x;
95 capture_point_.y = event->pointer_data->y;
96 last_drag_point_ = capture_point_;
97 drag_start_time_ = mojo::GetTimeTicksNow();
98 cube_.SetFlingMultiplier(0.0f, 1.0f);
99 break;
105 100
106 GLuint fbo = 0u; 101 case mojo::EventType::POINTER_MOVE: {
107 glGenFramebuffers(1, &fbo); 102 if (!(event->flags & mojo::EventFlags::LEFT_MOUSE_BUTTON) &&
108 glBindFramebuffer(GL_FRAMEBUFFER, fbo); 103 event->pointer_data->kind == mojo::PointerKind::MOUSE) {
109 GLuint depth_buffer = 0u; 104 break;
110 glGenRenderbuffers(1, &depth_buffer); 105 }
111 glBindRenderbuffer(GL_RENDERBUFFER, depth_buffer); 106 mojo::PointF event_location;
112 glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, size_.width, 107 event_location.x = event->pointer_data->x;
113 size_.height); 108 event_location.y = event->pointer_data->y;
114 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 109 int direction =
115 texture->texture_id(), 0); 110 GetEventDirection(event_location, capture_point_, last_drag_point_);
116 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, 111 cube_.UpdateForDragDistance(
117 GL_RENDERBUFFER, depth_buffer); 112 direction * CalculateDragDistance(last_drag_point_, event_location));
118 DCHECK_EQ(static_cast<GLenum>(GL_FRAMEBUFFER_COMPLETE), 113 last_drag_point_ = event_location;
119 glCheckFramebufferStatus(GL_FRAMEBUFFER)); 114 break;
120 glClearColor(1, 0, 0, 0.5); 115 }
121 116
122 cube_.set_size(size_.width, size_.height); 117 case mojo::EventType::POINTER_UP: {
118 if (event->flags & mojo::EventFlags::RIGHT_MOUSE_BUTTON) {
119 cube_.set_color(GetRandomColor(), GetRandomColor(), GetRandomColor());
120 break;
121 }
122 mojo::PointF event_location;
123 event_location.x = event->pointer_data->x;
124 event_location.y = event->pointer_data->y;
125 MojoTimeTicks offset = mojo::GetTimeTicksNow() - drag_start_time_;
126 float delta = static_cast<float>(offset) / 1000000.f;
127 // Last drag point is the same as current point here; use initial capture
128 // point instead
129 int direction =
130 GetEventDirection(event_location, capture_point_, capture_point_);
131 cube_.SetFlingMultiplier(
132 direction * CalculateDragDistance(capture_point_, event_location),
133 delta);
134 capture_point_ = last_drag_point_ = mojo::PointF();
135 break;
136 }
123 137
124 MojoTimeTicks now = mojo::GetTimeTicksNow(); 138 default:
125 MojoTimeTicks offset = now - last_draw_; 139 break;
126 cube_.UpdateForTimeDelta(offset * 0.000001f); 140 }
127 last_draw_ = now;
128 141
129 cube_.Draw(); 142 callback.Run(true);
130
131 glDeleteFramebuffers(1, &fbo);
132 glDeleteRenderbuffers(1, &depth_buffer);
133
134 mojo::FramePtr frame = mojo::TextureUploader::GetUploadFrame(
135 context_owner_.context(), texture_info->resource_id(), texture);
136 surfaces_->SubmitFrame(surface_id_->local, frame.Pass(),
137 base::Bind(&SpinningCubeView::OnSurfaceSubmitted,
138 base::Unretained(this)));
139
140 texture_cache_.NotifyPendingResourceReturn(texture_info->resource_id(),
141 texture.Pass());
142 } 143 }
143 144
144 void SpinningCubeView::OnSurfaceSubmitted() { 145 void SpinningCubeView::OnDraw(
145 ScheduleDraw(); 146 const mojo::gfx::composition::FrameInfo& frame_info,
147 const base::TimeDelta& time_delta) {
148 // Update the state of the cube.
149 cube_.UpdateForTimeDelta(time_delta.InSecondsF());
150
151 // Update the contents of the scene.
152 mojo::Rect bounds;
153 bounds.width = size_.width;
154 bounds.height = size_.height;
155
156 auto update = mojo::gfx::composition::SceneUpdate::New();
157 mojo::gfx::composition::ResourcePtr cube_resource = gl_renderer()->DrawGL(
158 size_, true,
159 base::Bind(&SpinningCubeView::DrawCubeWithGL, base::Unretained(this)));
160 DCHECK(cube_resource);
161 update->resources.insert(kCubeImageResourceId, cube_resource.Pass());
162
163 auto root_node = mojo::gfx::composition::Node::New();
164 root_node->content_transform = mojo::Transform::New();
165 mojo::SetIdentityTransform(root_node->content_transform.get());
166 // TODO(jeffbrown): Figure out why spinning cube is drawing upside down.
167 // Other GL based programs don't seem to have this problem.
168 root_node->content_transform->matrix[5] = -1; // flip image vertically
169 root_node->content_transform->matrix[7] = size_.height;
170 root_node->op = mojo::gfx::composition::NodeOp::New();
171 root_node->op->set_image(mojo::gfx::composition::ImageNodeOp::New());
172 root_node->op->get_image()->content_rect = bounds.Clone();
173 root_node->op->get_image()->image_resource_id = kCubeImageResourceId;
174 update->nodes.insert(kRootNodeId, root_node.Pass());
175
176 auto metadata = mojo::gfx::composition::SceneMetadata::New();
177 metadata->presentation_time = frame_info.presentation_time;
178
179 // Publish the scene.
180 scene()->Update(update.Pass());
181 scene()->Publish(metadata.Pass());
182
183 // Loop!
184 choreographer_.ScheduleDraw();
146 } 185 }
147 186
148 void SpinningCubeView::ScheduleDraw() { 187 void SpinningCubeView::DrawCubeWithGL() {
149 if (!draw_scheduled_) { 188 cube_.set_size(size_.width, size_.height);
150 draw_scheduled_ = true; 189 cube_.Draw();
151
152 // TODO(jeffbrown): For now, we need to throttle this down because
153 // drawing as fast as we can appears to cause starvation of the
154 // Mojo message loop and makes X11 unhappy.
155 base::MessageLoop::current()->PostDelayedTask(
156 FROM_HERE,
157 base::Bind(&SpinningCubeView::DrawCube, weak_ptr_factory_.GetWeakPtr()),
158 base::TimeDelta::FromMilliseconds(30));
159 }
160 } 190 }
161 191
162 } // namespace examples 192 } // namespace examples
OLDNEW
« no previous file with comments | « examples/ui/spinning_cube/spinning_cube_view.h ('k') | examples/ui/tile/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698