Chromium Code Reviews| Index: chrome/browser/android/blimp/chrome_compositor_dependencies.cc |
| diff --git a/chrome/browser/android/blimp/chrome_compositor_dependencies.cc b/chrome/browser/android/blimp/chrome_compositor_dependencies.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..3be308d49fdf209de9984943ded922bba3cc74e8 |
| --- /dev/null |
| +++ b/chrome/browser/android/blimp/chrome_compositor_dependencies.cc |
| @@ -0,0 +1,202 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/android/blimp/chrome_compositor_dependencies.h" |
| + |
| +#include "base/bind.h" |
| +#include "base/command_line.h" |
| +#include "cc/output/context_provider.h" |
| +#include "content/public/common/content_switches.h" |
| +#include "gpu/command_buffer/client/gles2_interface.h" |
| +#include "gpu/command_buffer/client/shared_memory_limits.h" |
| +#include "gpu/command_buffer/common/gles2_cmd_utils.h" |
| + |
| +namespace { |
| + |
| +bool IsValidWorkerContext(cc::ContextProvider* context_provider) { |
| + if (!context_provider) |
|
danakj
2016/09/01 19:26:06
Why would it not just be a failure |result| in thi
Khushal
2016/09/01 19:39:57
Oh, I'm calling it from GetContextProviders also,
|
| + return false; |
| + |
| + cc::ContextProvider::ScopedContextLock lock(context_provider); |
| + return context_provider->ContextGL()->GetGraphicsResetStatusKHR() == |
|
danakj
2016/09/01 19:26:06
Why do you check this when the context is created?
Khushal
2016/09/01 19:39:57
So I only do this after the worker context is boun
danakj
2016/09/01 19:55:20
I think it does it when checking if it should reus
Khushal
2016/09/01 20:10:23
This one also checks it only during the re-use poi
|
| + GL_NO_ERROR; |
| +} |
| + |
| +void OnCompositorContextCreated( |
| + const blimp::client::CompositorDependencies::ContextProviderCallback& |
| + callback, |
| + const scoped_refptr<cc::ContextProvider>& worker_context, |
| + const scoped_refptr<cc::ContextProvider>& compositor_context, |
| + ui::ContextProviderFactory::ContextCreationResult result) { |
| + blimp::client::CompositorDependencies::ContextProviders context_providers; |
| + |
| + switch (result) { |
| + case ui::ContextProviderFactory::ContextCreationResult:: |
| + FAILURE_GPU_PROCESS_INITIALIZATION_FAILED: |
| + // Pass the null result to the caller and let them try again. |
| + break; |
| + case ui::ContextProviderFactory::ContextCreationResult:: |
| + FAILURE_FACTORY_SHUTDOWN: |
| + // If the factory is shutting down, early out and drop the request. |
| + return; |
| + case ui::ContextProviderFactory::ContextCreationResult:: |
| + FAILURE_GPU_SURFACE_HANDLE_LOST: |
| + NOTREACHED() << "We only ask for offscreen contexts"; |
| + break; |
| + case ui::ContextProviderFactory::ContextCreationResult::SUCCESS: |
| + // Return null if the worker context was invalidated while we were waiting |
| + // for the compositor context. |
| + if (IsValidWorkerContext(worker_context.get())) { |
| + context_providers.compositor_context_provider = |
| + std::move(compositor_context); |
| + context_providers.worker_context_provider = std::move(worker_context); |
| + } |
| + break; |
| + } |
| + |
| + callback.Run(context_providers); |
| +} |
| + |
| +gpu::gles2::ContextCreationAttribHelper |
| +GetOffscreenContextCreationAttributes() { |
| + gpu::gles2::ContextCreationAttribHelper attributes; |
| + attributes.alpha_size = -1; |
| + attributes.depth_size = 0; |
| + attributes.stencil_size = 0; |
| + attributes.samples = 0; |
| + attributes.sample_buffers = 0; |
| + attributes.bind_generates_resource = false; |
| + attributes.lose_context_when_out_of_memory = true; |
| + return attributes; |
| +} |
| + |
| +bool IsAsyncWorkerContextEnabled() { |
| + const base::CommandLine& command_line = |
| + *base::CommandLine::ForCurrentProcess(); |
| + |
| + if (command_line.HasSwitch(switches::kDisableGpuAsyncWorkerContext)) |
| + return false; |
| + else if (command_line.HasSwitch(switches::kEnableGpuAsyncWorkerContext)) |
| + return true; |
| + |
| + return false; |
| +} |
| + |
| +} // namespace |
| + |
| +ChromeCompositorDependencies::ChromeCompositorDependencies( |
| + ui::ContextProviderFactory* context_provider_factory) |
| + : context_provider_factory_(context_provider_factory), |
| + worker_context_request_pending_(false), |
| + weak_factory_(this) { |
| + DCHECK(context_provider_factory_); |
| +} |
| + |
| +ChromeCompositorDependencies::~ChromeCompositorDependencies() = default; |
| + |
| +gpu::GpuMemoryBufferManager* |
| +ChromeCompositorDependencies::GetGpuMemoryBufferManager() { |
| + return context_provider_factory_->GetGpuMemoryBufferManager(); |
| +} |
| + |
| +cc::SurfaceManager* ChromeCompositorDependencies::GetSurfaceManager() { |
| + return context_provider_factory_->GetSurfaceManager(); |
| +} |
| + |
| +uint32_t ChromeCompositorDependencies::AllocateSurfaceClientId() { |
| + return context_provider_factory_->AllocateSurfaceClientId(); |
| +} |
| + |
| +void ChromeCompositorDependencies::GetContextProviders( |
| + const ContextProviderCallback& callback) { |
| + if (!IsValidWorkerContext(shared_main_thread_worker_context_.get())) |
| + shared_main_thread_worker_context_ = nullptr; |
| + |
| + // Request a compositor context if the worker context is still valid. |
| + if (shared_main_thread_worker_context_) { |
| + HandleCompositorContextRequest(callback); |
| + return; |
| + } |
| + |
| + // Add the request to the list of pending requests first, if the result |
| + // comes back synchronously, the callback should get triggered. |
| + pending_requests_.push_back(callback); |
| + |
| + // If the request for a worker context is already pending, don't make |
| + // another one. We'll handle all the pending requests together once the |
| + // worker context is available. |
| + if (worker_context_request_pending_) |
| + return; |
| + |
| + worker_context_request_pending_ = true; |
| + context_provider_factory_->CreateOffscreenContextProvider( |
|
danakj
2016/09/01 19:26:06
This code here is significantly more complicated t
Khushal
2016/09/01 19:39:57
I had to do it this way because we needed to creat
danakj
2016/09/01 19:55:20
What if you just made a sync method to make contex
Khushal
2016/09/01 20:10:23
I could just hide it all in the ContextProviderFac
danakj
2016/09/01 20:14:01
They have LTHI on diff threads right, so no they s
Khushal
2016/09/01 20:47:37
So there are 3 ways I could think of doing this.
|
| + ui::ContextProviderFactory::ContextType::BLIMP_RENDER_WORKER_CONTEXT, |
| + gpu::SharedMemoryLimits(), GetOffscreenContextCreationAttributes(), |
| + true /* support_locking */, false /* automatic_flushes */, nullptr, |
| + base::Bind(&ChromeCompositorDependencies::OnWorkerContextCreated, |
| + weak_factory_.GetWeakPtr())); |
| +} |
| + |
| +void ChromeCompositorDependencies::OnWorkerContextCreated( |
| + const scoped_refptr<cc::ContextProvider>& worker_context, |
| + ui::ContextProviderFactory::ContextCreationResult result) { |
| + DCHECK(worker_context_request_pending_); |
| + DCHECK(!shared_main_thread_worker_context_); |
| + |
| + worker_context_request_pending_ = false; |
| + |
| + switch (result) { |
| + case ui::ContextProviderFactory::ContextCreationResult:: |
| + FAILURE_GPU_PROCESS_INITIALIZATION_FAILED: |
| + // Pass the null result to the caller and let them try again. |
| + break; |
| + case ui::ContextProviderFactory::ContextCreationResult:: |
| + FAILURE_FACTORY_SHUTDOWN: |
| + // If the factory is shutting down, early out and drop the requests. |
| + return; |
| + case ui::ContextProviderFactory::ContextCreationResult:: |
| + FAILURE_GPU_SURFACE_HANDLE_LOST: |
| + NOTREACHED() << "We only ask for offscreen contexts"; |
| + break; |
| + case ui::ContextProviderFactory::ContextCreationResult::SUCCESS: |
| + if (worker_context->BindToCurrentThread()) { |
| + shared_main_thread_worker_context_ = worker_context; |
| + } |
| + break; |
| + } |
| + |
| + // Copy the requests first since we can get more requests as we run these |
| + // callbacks. |
| + std::list<ContextProviderCallback> context_requests = pending_requests_; |
| + pending_requests_.clear(); |
| + |
| + for (ContextProviderCallback& context_request : context_requests) { |
| + HandleCompositorContextRequest(context_request); |
| + } |
| +} |
| + |
| +void ChromeCompositorDependencies::HandleCompositorContextRequest( |
| + const ContextProviderCallback& callback) { |
| + // Respond with null contexts if the worker context could not be created. |
| + if (!shared_main_thread_worker_context_) { |
| + callback.Run(ContextProviders()); |
| + return; |
| + } |
| + |
| + ui::ContextProviderFactory::ContextProviderCallback result_callback = |
| + base::Bind(&OnCompositorContextCreated, callback, |
| + shared_main_thread_worker_context_); |
| + |
| + // The compositor context shares resources with the worker context unless |
| + // the worker is async. |
| + cc::ContextProvider* shared_context = |
| + IsAsyncWorkerContextEnabled() ? nullptr |
| + : shared_main_thread_worker_context_.get(); |
| + |
| + context_provider_factory_->CreateOffscreenContextProvider( |
| + ui::ContextProviderFactory::ContextType::BLIMP_RENDER_COMPOSITOR_CONTEXT, |
| + gpu::SharedMemoryLimits::ForMailboxContext(), |
| + GetOffscreenContextCreationAttributes(), false /* support_locking */, |
| + false /* automatic_flushes */, shared_context, result_callback); |
| +} |