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 "chrome/browser/android/blimp/chrome_compositor_dependencies.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/command_line.h" |
| 9 #include "cc/output/context_provider.h" |
| 10 #include "content/public/common/content_switches.h" |
| 11 #include "gpu/command_buffer/client/gles2_interface.h" |
| 12 #include "gpu/command_buffer/client/shared_memory_limits.h" |
| 13 #include "gpu/command_buffer/common/gles2_cmd_utils.h" |
| 14 #include "ui/android/context_provider_factory.h" |
| 15 |
| 16 namespace { |
| 17 |
| 18 bool IsValidWorkerContext(cc::ContextProvider* context_provider) { |
| 19 if (!context_provider) |
| 20 return false; |
| 21 |
| 22 cc::ContextProvider::ScopedContextLock lock(context_provider); |
| 23 return context_provider->ContextGL()->GetGraphicsResetStatusKHR() == |
| 24 GL_NO_ERROR; |
| 25 } |
| 26 |
| 27 void OnCompositorContextCreated( |
| 28 const blimp::client::CompositorDependencies::ContextProviderCallback& |
| 29 callback, |
| 30 const scoped_refptr<cc::ContextProvider>& worker_context, |
| 31 const scoped_refptr<cc::ContextProvider>& compositor_context) { |
| 32 blimp::client::CompositorDependencies::ContextProviders context_providers; |
| 33 |
| 34 // Return null if the worker context was invalidated while we were waiting for |
| 35 // the compositor context. |
| 36 if (IsValidWorkerContext(worker_context.get())) { |
| 37 context_providers.compositor_context_provider = |
| 38 std::move(compositor_context); |
| 39 context_providers.worker_context_provider = std::move(worker_context); |
| 40 } |
| 41 callback.Run(context_providers); |
| 42 } |
| 43 |
| 44 gpu::gles2::ContextCreationAttribHelper |
| 45 GetOffscreenContextCreationAttributes() { |
| 46 gpu::gles2::ContextCreationAttribHelper attributes; |
| 47 attributes.alpha_size = -1; |
| 48 attributes.depth_size = 0; |
| 49 attributes.stencil_size = 0; |
| 50 attributes.samples = 0; |
| 51 attributes.sample_buffers = 0; |
| 52 attributes.bind_generates_resource = false; |
| 53 attributes.lose_context_when_out_of_memory = true; |
| 54 return attributes; |
| 55 } |
| 56 |
| 57 bool IsAsyncWorkerContextEnabled() { |
| 58 const base::CommandLine& command_line = |
| 59 *base::CommandLine::ForCurrentProcess(); |
| 60 |
| 61 if (command_line.HasSwitch(switches::kDisableGpuAsyncWorkerContext)) |
| 62 return false; |
| 63 else if (command_line.HasSwitch(switches::kEnableGpuAsyncWorkerContext)) |
| 64 return true; |
| 65 |
| 66 return false; |
| 67 } |
| 68 |
| 69 } // namespace |
| 70 |
| 71 ChromeCompositorDependencies::ChromeCompositorDependencies( |
| 72 ui::ContextProviderFactory* context_provider_factory) |
| 73 : context_provider_factory_(context_provider_factory), |
| 74 worker_context_request_pending_(false), |
| 75 weak_factory_(this) { |
| 76 DCHECK(context_provider_factory_); |
| 77 } |
| 78 |
| 79 ChromeCompositorDependencies::~ChromeCompositorDependencies() = default; |
| 80 |
| 81 gpu::GpuMemoryBufferManager* |
| 82 ChromeCompositorDependencies::GetGpuMemoryBufferManager() { |
| 83 return context_provider_factory_->GetGpuMemoryBufferManager(); |
| 84 } |
| 85 |
| 86 cc::SurfaceManager* ChromeCompositorDependencies::GetSurfaceManager() { |
| 87 return context_provider_factory_->GetSurfaceManager(); |
| 88 } |
| 89 |
| 90 uint32_t ChromeCompositorDependencies::AllocateSurfaceClientId() { |
| 91 return context_provider_factory_->AllocateSurfaceClientId(); |
| 92 } |
| 93 |
| 94 void ChromeCompositorDependencies::GetContextProviders( |
| 95 const ContextProviderCallback& callback) { |
| 96 if (!IsValidWorkerContext(shared_main_thread_worker_context_.get())) |
| 97 shared_main_thread_worker_context_ = nullptr; |
| 98 |
| 99 // Request a compositor context if the worker context is still valid. |
| 100 if (shared_main_thread_worker_context_) { |
| 101 HandleCompositorContextRequest(callback); |
| 102 return; |
| 103 } |
| 104 |
| 105 // Add the request to the list of pending requests first, if the result |
| 106 // comes back synchronously, the callback should get triggered. |
| 107 pending_requests_.push_back(callback); |
| 108 |
| 109 // If the request for a worker context is already pending, don't make |
| 110 // another one. We'll handle all the pending requests together once the |
| 111 // worker context is available. |
| 112 if (worker_context_request_pending_) |
| 113 return; |
| 114 |
| 115 worker_context_request_pending_ = true; |
| 116 context_provider_factory_->CreateOffscreenContextProvider( |
| 117 ui::ContextProviderFactory::ContextType::BLIMP_RENDER_WORKER_CONTEXT, |
| 118 gpu::SharedMemoryLimits(), GetOffscreenContextCreationAttributes(), |
| 119 true /* support_locking */, false /* automatic_flushes */, nullptr, |
| 120 base::Bind(&ChromeCompositorDependencies::OnWorkerContextCreated, |
| 121 weak_factory_.GetWeakPtr())); |
| 122 } |
| 123 |
| 124 void ChromeCompositorDependencies::OnWorkerContextCreated( |
| 125 const scoped_refptr<cc::ContextProvider>& worker_context) { |
| 126 DCHECK(worker_context_request_pending_); |
| 127 DCHECK(!shared_main_thread_worker_context_); |
| 128 |
| 129 worker_context_request_pending_ = false; |
| 130 if (worker_context->BindToCurrentThread()) { |
| 131 shared_main_thread_worker_context_ = worker_context; |
| 132 } |
| 133 |
| 134 // Copy the requests first since we can get more requests as we run these |
| 135 // callbacks. |
| 136 std::list<ContextProviderCallback> context_requests = pending_requests_; |
| 137 pending_requests_.clear(); |
| 138 |
| 139 for (ContextProviderCallback& context_request : context_requests) { |
| 140 HandleCompositorContextRequest(context_request); |
| 141 } |
| 142 } |
| 143 |
| 144 void ChromeCompositorDependencies::HandleCompositorContextRequest( |
| 145 const ContextProviderCallback& callback) { |
| 146 // Respond with null contexts if the worker context could not be created. |
| 147 if (!shared_main_thread_worker_context_) { |
| 148 callback.Run(ContextProviders()); |
| 149 return; |
| 150 } |
| 151 |
| 152 ui::ContextProviderFactory::ContextProviderCallback result_callback = |
| 153 base::Bind(&OnCompositorContextCreated, callback, |
| 154 shared_main_thread_worker_context_); |
| 155 |
| 156 // The compositor context shares resources with the worker context unless |
| 157 // the worker is async. |
| 158 cc::ContextProvider* shared_context = |
| 159 IsAsyncWorkerContextEnabled() ? nullptr |
| 160 : shared_main_thread_worker_context_.get(); |
| 161 |
| 162 context_provider_factory_->CreateOffscreenContextProvider( |
| 163 ui::ContextProviderFactory::ContextType::BLIMP_RENDER_COMPOSITOR_CONTEXT, |
| 164 gpu::SharedMemoryLimits::ForMailboxContext(), |
| 165 GetOffscreenContextCreationAttributes(), false /* support_locking */, |
| 166 false /* automatic_flushes */, shared_context, result_callback); |
| 167 } |
OLD | NEW |