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/app/compositor/browser_compositor.h" |
| 6 |
| 7 #include "base/lazy_instance.h" |
| 8 #include "base/threading/thread_task_runner_handle.h" |
| 9 #include "blimp/client/feature/compositor/blimp_context_provider.h" |
| 10 #include "blimp/client/feature/compositor/blimp_gpu_memory_buffer_manager.h" |
| 11 #include "cc/animation/animation_host.h" |
| 12 #include "cc/layers/layer.h" |
| 13 #include "cc/output/compositor_frame.h" |
| 14 #include "cc/output/texture_mailbox_deleter.h" |
| 15 #include "cc/raster/single_thread_task_graph_runner.h" |
| 16 #include "cc/surfaces/display.h" |
| 17 #include "cc/surfaces/surface_display_output_surface.h" |
| 18 #include "cc/surfaces/surface_id_allocator.h" |
| 19 #include "cc/surfaces/surface_manager.h" |
| 20 #include "cc/trees/layer_tree_host.h" |
| 21 #include "gpu/command_buffer/client/context_support.h" |
| 22 |
| 23 namespace blimp { |
| 24 namespace client { |
| 25 |
| 26 namespace { |
| 27 |
| 28 class SimpleTaskGraphRunner : public cc::SingleThreadTaskGraphRunner { |
| 29 public: |
| 30 SimpleTaskGraphRunner() { |
| 31 Start("BlimpBrowserCompositorWorker", |
| 32 base::SimpleThread::Options(base::ThreadPriority::BACKGROUND)); |
| 33 } |
| 34 |
| 35 ~SimpleTaskGraphRunner() override { Shutdown(); } |
| 36 }; |
| 37 |
| 38 class DisplayOutputSurface : public cc::OutputSurface { |
| 39 public: |
| 40 explicit DisplayOutputSurface( |
| 41 scoped_refptr<cc::ContextProvider> context_provider) |
| 42 : cc::OutputSurface(std::move(context_provider), nullptr, nullptr) {} |
| 43 |
| 44 ~DisplayOutputSurface() override = default; |
| 45 |
| 46 // cc::OutputSurface implementation |
| 47 void SwapBuffers(cc::CompositorFrame frame) override { |
| 48 // See cc::OutputSurface::SwapBuffers() comment for details. |
| 49 context_provider_->ContextSupport()->Swap(); |
| 50 cc::OutputSurface::PostSwapBuffersComplete(); |
| 51 } |
| 52 |
| 53 uint32_t GetFramebufferCopyTextureFormat() override { |
| 54 auto* gl = static_cast<BlimpContextProvider*>(context_provider()); |
| 55 return gl->GetCopyTextureInternalFormat(); |
| 56 } |
| 57 |
| 58 private: |
| 59 DISALLOW_COPY_AND_ASSIGN(DisplayOutputSurface); |
| 60 }; |
| 61 |
| 62 base::LazyInstance<SimpleTaskGraphRunner> g_task_graph_runner = |
| 63 LAZY_INSTANCE_INITIALIZER; |
| 64 |
| 65 base::LazyInstance<cc::SurfaceManager> g_surface_manager = |
| 66 LAZY_INSTANCE_INITIALIZER; |
| 67 |
| 68 base::LazyInstance<BlimpGpuMemoryBufferManager> g_gpu_memory_buffer_manager = |
| 69 LAZY_INSTANCE_INITIALIZER; |
| 70 |
| 71 uint32_t g_surface_id = 0; |
| 72 |
| 73 } // namespace |
| 74 |
| 75 // static |
| 76 cc::SurfaceManager* BrowserCompositor::GetSurfaceManager() { |
| 77 return g_surface_manager.Pointer(); |
| 78 } |
| 79 |
| 80 // static |
| 81 BlimpGpuMemoryBufferManager* BrowserCompositor::GetGpuMemoryBufferManager() { |
| 82 return g_gpu_memory_buffer_manager.Pointer(); |
| 83 } |
| 84 |
| 85 // static |
| 86 uint32_t BrowserCompositor::AllocateSurfaceClientId() { |
| 87 return ++g_surface_id; |
| 88 } |
| 89 |
| 90 BrowserCompositor::BrowserCompositor() |
| 91 : surface_id_allocator_(base::MakeUnique<cc::SurfaceIdAllocator>( |
| 92 BrowserCompositor::AllocateSurfaceClientId())), |
| 93 widget_(gfx::kNullAcceleratedWidget), |
| 94 output_surface_request_pending_(false), |
| 95 root_layer_(cc::Layer::Create()) { |
| 96 BrowserCompositor::GetSurfaceManager()->RegisterSurfaceClientId( |
| 97 surface_id_allocator_->client_id()); |
| 98 |
| 99 cc::LayerTreeHost::InitParams params; |
| 100 params.client = this; |
| 101 params.gpu_memory_buffer_manager = |
| 102 BrowserCompositor::GetGpuMemoryBufferManager(); |
| 103 params.task_graph_runner = g_task_graph_runner.Pointer(); |
| 104 cc::LayerTreeSettings settings; |
| 105 params.settings = &settings; |
| 106 params.main_task_runner = base::ThreadTaskRunnerHandle::Get(); |
| 107 params.animation_host = cc::AnimationHost::CreateMainInstance(); |
| 108 host_ = cc::LayerTreeHost::CreateSingleThreaded(this, ¶ms); |
| 109 |
| 110 root_layer_->SetBackgroundColor(SK_ColorWHITE); |
| 111 host_->GetLayerTree()->SetRootLayer(root_layer_); |
| 112 host_->set_surface_client_id(surface_id_allocator_->client_id()); |
| 113 } |
| 114 |
| 115 BrowserCompositor::~BrowserCompositor() { |
| 116 BrowserCompositor::GetSurfaceManager()->InvalidateSurfaceClientId( |
| 117 surface_id_allocator_->client_id()); |
| 118 } |
| 119 |
| 120 void BrowserCompositor::SetContentLayer( |
| 121 scoped_refptr<cc::Layer> content_layer) { |
| 122 root_layer_->RemoveAllChildren(); |
| 123 root_layer_->AddChild(content_layer); |
| 124 } |
| 125 |
| 126 void BrowserCompositor::SetSize(const gfx::Size& size_in_px) { |
| 127 viewport_size_in_px_ = size_in_px; |
| 128 |
| 129 // Update the host. |
| 130 host_->GetLayerTree()->SetViewportSize(viewport_size_in_px_); |
| 131 root_layer_->SetBounds(viewport_size_in_px_); |
| 132 |
| 133 // Update the display. |
| 134 if (display_) { |
| 135 display_->Resize(viewport_size_in_px_); |
| 136 } |
| 137 } |
| 138 |
| 139 void BrowserCompositor::SetAcceleratedWidget(gfx::AcceleratedWidget widget) { |
| 140 // Kill all references to the old widget. |
| 141 if (widget_ != gfx::kNullAcceleratedWidget) { |
| 142 // We are always visible if we have a widget. |
| 143 DCHECK(host_->visible()); |
| 144 host_->SetVisible(false); |
| 145 if (!host_->output_surface_lost()) { |
| 146 host_->ReleaseOutputSurface(); |
| 147 } |
| 148 display_.reset(); |
| 149 } |
| 150 |
| 151 widget_ = gfx::kNullAcceleratedWidget; |
| 152 |
| 153 if (widget != gfx::kNullAcceleratedWidget) { |
| 154 widget_ = widget; |
| 155 host_->SetVisible(true); |
| 156 if (output_surface_request_pending_) |
| 157 HandlePendingOutputSurfaceRequest(); |
| 158 } |
| 159 } |
| 160 |
| 161 void BrowserCompositor::RequestNewOutputSurface() { |
| 162 DCHECK(!output_surface_request_pending_) |
| 163 << "We already have a pending request?"; |
| 164 output_surface_request_pending_ = true; |
| 165 HandlePendingOutputSurfaceRequest(); |
| 166 } |
| 167 |
| 168 void BrowserCompositor::DidInitializeOutputSurface() { |
| 169 output_surface_request_pending_ = false; |
| 170 } |
| 171 |
| 172 void BrowserCompositor::DidFailToInitializeOutputSurface() { |
| 173 NOTREACHED() << "Can't fail to initialize the OutputSurface here"; |
| 174 } |
| 175 |
| 176 void BrowserCompositor::DidCompleteSwapBuffers() { |
| 177 if (!did_complete_swap_buffers_.is_null()) { |
| 178 did_complete_swap_buffers_.Run(); |
| 179 } |
| 180 } |
| 181 |
| 182 void BrowserCompositor::HandlePendingOutputSurfaceRequest() { |
| 183 DCHECK(output_surface_request_pending_); |
| 184 |
| 185 // Can't handle the request right now since we don't have a widget. |
| 186 if (!host_->visible()) |
| 187 return; |
| 188 |
| 189 DCHECK_NE(gfx::kNullAcceleratedWidget, widget_); |
| 190 |
| 191 scoped_refptr<cc::ContextProvider> context_provider = |
| 192 BlimpContextProvider::Create( |
| 193 widget_, BrowserCompositor::GetGpuMemoryBufferManager()); |
| 194 |
| 195 std::unique_ptr<cc::OutputSurface> display_output_surface = |
| 196 base::MakeUnique<DisplayOutputSurface>(context_provider); |
| 197 |
| 198 auto* task_runner = base::ThreadTaskRunnerHandle::Get().get(); |
| 199 std::unique_ptr<cc::SyntheticBeginFrameSource> begin_frame_source( |
| 200 new cc::DelayBasedBeginFrameSource( |
| 201 base::MakeUnique<cc::DelayBasedTimeSource>(task_runner))); |
| 202 std::unique_ptr<cc::DisplayScheduler> scheduler(new cc::DisplayScheduler( |
| 203 begin_frame_source.get(), task_runner, |
| 204 display_output_surface->capabilities().max_frames_pending)); |
| 205 |
| 206 display_ = base::MakeUnique<cc::Display>( |
| 207 nullptr /*shared_bitmap_manager*/, |
| 208 BrowserCompositor::GetGpuMemoryBufferManager(), |
| 209 host_->settings().renderer_settings, std::move(begin_frame_source), |
| 210 std::move(display_output_surface), std::move(scheduler), |
| 211 base::MakeUnique<cc::TextureMailboxDeleter>(task_runner)); |
| 212 display_->SetVisible(true); |
| 213 display_->Resize(viewport_size_in_px_); |
| 214 |
| 215 // The Browser compositor and display share the same context provider. |
| 216 std::unique_ptr<cc::OutputSurface> delegated_output_surface = |
| 217 base::MakeUnique<cc::SurfaceDisplayOutputSurface>( |
| 218 BrowserCompositor::GetSurfaceManager(), surface_id_allocator_.get(), |
| 219 display_.get(), context_provider, nullptr); |
| 220 |
| 221 host_->SetOutputSurface(std::move(delegated_output_surface)); |
| 222 } |
| 223 |
| 224 } // namespace client |
| 225 } // namespace blimp |
OLD | NEW |