| 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..8cd25feb2800d3fdacb9ff4ef0b6a2f8a30f62e6
|
| --- /dev/null
|
| +++ b/chrome/browser/android/blimp/chrome_compositor_dependencies.cc
|
| @@ -0,0 +1,167 @@
|
| +// 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"
|
| +#include "ui/android/context_provider_factory.h"
|
| +
|
| +namespace {
|
| +
|
| +bool IsValidWorkerContext(cc::ContextProvider* context_provider) {
|
| + if (!context_provider)
|
| + return false;
|
| +
|
| + cc::ContextProvider::ScopedContextLock lock(context_provider);
|
| + return context_provider->ContextGL()->GetGraphicsResetStatusKHR() ==
|
| + 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) {
|
| + blimp::client::CompositorDependencies::ContextProviders context_providers;
|
| +
|
| + // 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);
|
| + }
|
| + 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(
|
| + 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) {
|
| + DCHECK(worker_context_request_pending_);
|
| + DCHECK(!shared_main_thread_worker_context_);
|
| +
|
| + worker_context_request_pending_ = false;
|
| + if (worker_context->BindToCurrentThread()) {
|
| + shared_main_thread_worker_context_ = worker_context;
|
| + }
|
| +
|
| + // 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);
|
| +}
|
|
|