| OLD | NEW |
| (Empty) |
| 1 // Copyright 2016 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 "blimp/client/feature/compositor/blimp_compositor_manager.h" | |
| 6 | |
| 7 #include "base/lazy_instance.h" | |
| 8 #include "base/memory/ptr_util.h" | |
| 9 #include "blimp/client/core/compositor/blob_image_serialization_processor.h" | |
| 10 #include "blimp/client/feature/compositor/blimp_layer_tree_settings.h" | |
| 11 #include "blimp/common/compositor/blimp_task_graph_runner.h" | |
| 12 #include "cc/proto/compositor_message.pb.h" | |
| 13 | |
| 14 namespace blimp { | |
| 15 namespace client { | |
| 16 | |
| 17 namespace { | |
| 18 base::LazyInstance<blimp::BlimpTaskGraphRunner> g_task_graph_runner = | |
| 19 LAZY_INSTANCE_INITIALIZER; | |
| 20 | |
| 21 const int kDummyTabId = 0; | |
| 22 } // namespace | |
| 23 | |
| 24 BlimpCompositorManager::BlimpCompositorManager( | |
| 25 RenderWidgetFeature* render_widget_feature, | |
| 26 BlimpCompositorManagerClient* client) | |
| 27 : visible_(false), | |
| 28 window_(gfx::kNullAcceleratedWidget), | |
| 29 gpu_memory_buffer_manager_(new BlimpGpuMemoryBufferManager), | |
| 30 active_compositor_(nullptr), | |
| 31 render_widget_feature_(render_widget_feature), | |
| 32 client_(client) { | |
| 33 DCHECK(render_widget_feature_); | |
| 34 render_widget_feature_->SetDelegate(kDummyTabId, this); | |
| 35 } | |
| 36 | |
| 37 BlimpCompositorManager::~BlimpCompositorManager() { | |
| 38 render_widget_feature_->RemoveDelegate(kDummyTabId); | |
| 39 if (compositor_thread_) | |
| 40 compositor_thread_->Stop(); | |
| 41 } | |
| 42 | |
| 43 void BlimpCompositorManager::SetVisible(bool visible) { | |
| 44 visible_ = visible; | |
| 45 if (active_compositor_) | |
| 46 active_compositor_->SetVisible(visible); | |
| 47 } | |
| 48 | |
| 49 void BlimpCompositorManager::SetAcceleratedWidget( | |
| 50 gfx::AcceleratedWidget widget) { | |
| 51 window_ = widget; | |
| 52 if (active_compositor_) | |
| 53 active_compositor_->SetAcceleratedWidget(widget); | |
| 54 } | |
| 55 | |
| 56 void BlimpCompositorManager::ReleaseAcceleratedWidget() { | |
| 57 window_ = gfx::kNullAcceleratedWidget; | |
| 58 if (active_compositor_) | |
| 59 active_compositor_->ReleaseAcceleratedWidget(); | |
| 60 } | |
| 61 | |
| 62 bool BlimpCompositorManager::OnTouchEvent(const ui::MotionEvent& motion_event) { | |
| 63 if (active_compositor_) | |
| 64 return active_compositor_->OnTouchEvent(motion_event); | |
| 65 return false; | |
| 66 } | |
| 67 | |
| 68 void BlimpCompositorManager::GenerateLayerTreeSettings( | |
| 69 cc::LayerTreeSettings* settings) { | |
| 70 PopulateCommonLayerTreeSettings(settings); | |
| 71 } | |
| 72 | |
| 73 std::unique_ptr<BlimpCompositor> BlimpCompositorManager::CreateBlimpCompositor( | |
| 74 int render_widget_id, | |
| 75 BlimpCompositorClient* client) { | |
| 76 return base::WrapUnique(new BlimpCompositor(render_widget_id, client)); | |
| 77 } | |
| 78 | |
| 79 void BlimpCompositorManager::OnRenderWidgetCreated(int render_widget_id) { | |
| 80 CHECK(!GetCompositor(render_widget_id)); | |
| 81 | |
| 82 compositors_[render_widget_id] = CreateBlimpCompositor(render_widget_id, | |
| 83 this); | |
| 84 } | |
| 85 | |
| 86 void BlimpCompositorManager::OnRenderWidgetInitialized(int render_widget_id) { | |
| 87 if (active_compositor_ && | |
| 88 active_compositor_->render_widget_id() == render_widget_id) | |
| 89 return; | |
| 90 | |
| 91 if (active_compositor_) { | |
| 92 VLOG(1) << "Hiding currently active compositor for render widget: " | |
| 93 << active_compositor_->render_widget_id(); | |
| 94 active_compositor_->SetVisible(false); | |
| 95 active_compositor_->ReleaseAcceleratedWidget(); | |
| 96 } | |
| 97 | |
| 98 active_compositor_ = GetCompositor(render_widget_id); | |
| 99 CHECK(active_compositor_); | |
| 100 | |
| 101 active_compositor_->SetVisible(visible_); | |
| 102 active_compositor_->SetAcceleratedWidget(window_); | |
| 103 } | |
| 104 | |
| 105 void BlimpCompositorManager::OnRenderWidgetDeleted(int render_widget_id) { | |
| 106 CompositorMap::const_iterator it = compositors_.find(render_widget_id); | |
| 107 CHECK(it != compositors_.end()); | |
| 108 | |
| 109 // Reset the |active_compositor_| if that is what we're destroying right now. | |
| 110 if (active_compositor_ == it->second.get()) | |
| 111 active_compositor_ = nullptr; | |
| 112 | |
| 113 compositors_.erase(it); | |
| 114 } | |
| 115 | |
| 116 void BlimpCompositorManager::OnCompositorMessageReceived( | |
| 117 int render_widget_id, | |
| 118 std::unique_ptr<cc::proto::CompositorMessage> message) { | |
| 119 BlimpCompositor* compositor = GetCompositor(render_widget_id); | |
| 120 CHECK(compositor); | |
| 121 | |
| 122 compositor->OnCompositorMessageReceived(std::move(message)); | |
| 123 } | |
| 124 | |
| 125 cc::LayerTreeSettings* BlimpCompositorManager::GetLayerTreeSettings() { | |
| 126 if (!settings_) { | |
| 127 settings_.reset(new cc::LayerTreeSettings); | |
| 128 | |
| 129 // TODO(khushalsagar): The server should selectively send only those | |
| 130 // LayerTreeSettings which should remain consistent across the server and | |
| 131 // client. Since it currently overrides all settings, ignore them. | |
| 132 // See crbug/577985. | |
| 133 GenerateLayerTreeSettings(settings_.get()); | |
| 134 settings_ | |
| 135 ->abort_commit_before_output_surface_creation = false; | |
| 136 settings_->renderer_settings.buffer_to_texture_target_map = | |
| 137 BlimpGpuMemoryBufferManager::GetDefaultBufferToTextureTargetMap(); | |
| 138 } | |
| 139 | |
| 140 return settings_.get(); | |
| 141 } | |
| 142 | |
| 143 void BlimpCompositorManager::DidCompleteSwapBuffers() { | |
| 144 DCHECK(client_); | |
| 145 client_->OnSwapBuffersCompleted(); | |
| 146 } | |
| 147 | |
| 148 void BlimpCompositorManager::DidCommitAndDrawFrame() { | |
| 149 DCHECK(client_); | |
| 150 client_->DidCommitAndDrawFrame(); | |
| 151 } | |
| 152 | |
| 153 scoped_refptr<base::SingleThreadTaskRunner> | |
| 154 BlimpCompositorManager::GetCompositorTaskRunner() { | |
| 155 if (compositor_thread_) | |
| 156 return compositor_thread_->task_runner(); | |
| 157 | |
| 158 base::Thread::Options thread_options; | |
| 159 #if defined(OS_ANDROID) | |
| 160 thread_options.priority = base::ThreadPriority::DISPLAY; | |
| 161 #endif | |
| 162 compositor_thread_.reset(new base::Thread("Compositor")); | |
| 163 compositor_thread_->StartWithOptions(thread_options); | |
| 164 | |
| 165 scoped_refptr<base::SingleThreadTaskRunner> task_runner = | |
| 166 compositor_thread_->task_runner(); | |
| 167 task_runner->PostTask( | |
| 168 FROM_HERE, | |
| 169 base::Bind(base::IgnoreResult(&base::ThreadRestrictions::SetIOAllowed), | |
| 170 false)); | |
| 171 // TODO(dtrainor): Determine whether or not we can disallow waiting. | |
| 172 | |
| 173 return task_runner; | |
| 174 } | |
| 175 | |
| 176 cc::TaskGraphRunner* BlimpCompositorManager::GetTaskGraphRunner() { | |
| 177 return g_task_graph_runner.Pointer(); | |
| 178 } | |
| 179 | |
| 180 gpu::GpuMemoryBufferManager* | |
| 181 BlimpCompositorManager::GetGpuMemoryBufferManager() { | |
| 182 return gpu_memory_buffer_manager_.get(); | |
| 183 } | |
| 184 | |
| 185 cc::ImageSerializationProcessor* | |
| 186 BlimpCompositorManager::GetImageSerializationProcessor() { | |
| 187 return BlobImageSerializationProcessor::current(); | |
| 188 } | |
| 189 | |
| 190 void BlimpCompositorManager::SendWebGestureEvent( | |
| 191 int render_widget_id, | |
| 192 const blink::WebGestureEvent& gesture_event) { | |
| 193 render_widget_feature_->SendWebGestureEvent(kDummyTabId, | |
| 194 render_widget_id, | |
| 195 gesture_event); | |
| 196 } | |
| 197 | |
| 198 void BlimpCompositorManager::SendCompositorMessage( | |
| 199 int render_widget_id, | |
| 200 const cc::proto::CompositorMessage& message) { | |
| 201 render_widget_feature_->SendCompositorMessage(kDummyTabId, | |
| 202 render_widget_id, | |
| 203 message); | |
| 204 } | |
| 205 | |
| 206 BlimpCompositor* BlimpCompositorManager::GetCompositor(int render_widget_id) { | |
| 207 CompositorMap::const_iterator it = compositors_.find(render_widget_id); | |
| 208 if (it == compositors_.end()) | |
| 209 return nullptr; | |
| 210 return it->second.get(); | |
| 211 } | |
| 212 | |
| 213 } // namespace client | |
| 214 } // namespace blimp | |
| OLD | NEW |