| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include <GLES2/gl2.h> |
| 6 #include <GLES2/gl2extmojo.h> |
| 7 #include <MGL/mgl.h> |
| 8 |
| 9 #include "base/message_loop/message_loop.h" |
| 10 #include "examples/ui/spinning_cube/spinning_cube_view.h" |
| 11 |
| 12 namespace examples { |
| 13 |
| 14 SpinningCubeView::SpinningCubeView( |
| 15 mojo::ApplicationImpl* app, |
| 16 const mojo::ui::ViewProvider::CreateViewCallback& callback) |
| 17 : callback_(callback), |
| 18 binding_(this), |
| 19 context_owner_(app->shell()), |
| 20 texture_cache_(context_owner_.context(), &resource_returner_), |
| 21 surface_id_namespace_(0), |
| 22 draw_scheduled_(false), |
| 23 weak_ptr_factory_(this) { |
| 24 app->ConnectToService("mojo:surfaces_service", &surfaces_); |
| 25 app->ConnectToService("mojo:view_manager_service", &view_manager_); |
| 26 |
| 27 surfaces_->SetResourceReturner(resource_returner_.Pass()); |
| 28 surfaces_->GetIdNamespace( |
| 29 base::Bind(&SpinningCubeView::OnSurfaceIdNamespaceAvailable, |
| 30 base::Unretained(this))); |
| 31 |
| 32 InitCube(); |
| 33 } |
| 34 |
| 35 SpinningCubeView::~SpinningCubeView() {} |
| 36 |
| 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, |
| 52 mojo::Array<uint32_t> children_needing_layout, |
| 53 const OnLayoutCallback& callback) { |
| 54 // Create a new surface the first time or if the size has changed. |
| 55 mojo::Size new_size; |
| 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 |
| 70 // Submit the new layout information. |
| 71 auto info = mojo::ui::ViewLayoutInfo::New(); |
| 72 info->size = size_.Clone(); |
| 73 info->surface_id = surface_id_->Clone(); |
| 74 callback.Run(info.Pass()); |
| 75 |
| 76 // Draw! |
| 77 ScheduleDraw(); |
| 78 } |
| 79 |
| 80 void SpinningCubeView::OnChildUnavailable( |
| 81 uint32_t child_key, |
| 82 const OnChildUnavailableCallback& callback) { |
| 83 callback.Run(); |
| 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; |
| 101 } |
| 102 |
| 103 context_owner_.context()->MakeCurrent(); |
| 104 scoped_ptr<mojo::GLTexture> texture = texture_info->TakeTexture(); |
| 105 |
| 106 GLuint fbo = 0u; |
| 107 glGenFramebuffers(1, &fbo); |
| 108 glBindFramebuffer(GL_FRAMEBUFFER, fbo); |
| 109 GLuint depth_buffer = 0u; |
| 110 glGenRenderbuffers(1, &depth_buffer); |
| 111 glBindRenderbuffer(GL_RENDERBUFFER, depth_buffer); |
| 112 glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, size_.width, |
| 113 size_.height); |
| 114 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, |
| 115 texture->texture_id(), 0); |
| 116 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, |
| 117 GL_RENDERBUFFER, depth_buffer); |
| 118 DCHECK_EQ(static_cast<GLenum>(GL_FRAMEBUFFER_COMPLETE), |
| 119 glCheckFramebufferStatus(GL_FRAMEBUFFER)); |
| 120 glClearColor(1, 0, 0, 0.5); |
| 121 |
| 122 cube_.set_size(size_.width, size_.height); |
| 123 |
| 124 MojoTimeTicks now = mojo::GetTimeTicksNow(); |
| 125 MojoTimeTicks offset = now - last_draw_; |
| 126 cube_.UpdateForTimeDelta(offset * 0.000001f); |
| 127 last_draw_ = now; |
| 128 |
| 129 cube_.Draw(); |
| 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 |
| 144 void SpinningCubeView::OnSurfaceSubmitted() { |
| 145 ScheduleDraw(); |
| 146 } |
| 147 |
| 148 void SpinningCubeView::ScheduleDraw() { |
| 149 if (!draw_scheduled_) { |
| 150 draw_scheduled_ = true; |
| 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 } |
| 161 |
| 162 } // namespace examples |
| OLD | NEW |