Index: ui/android/delegated_frame_host_android.cc |
diff --git a/ui/android/delegated_frame_host_android.cc b/ui/android/delegated_frame_host_android.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..c7e5d64965d38418807988239829047616879c89 |
--- /dev/null |
+++ b/ui/android/delegated_frame_host_android.cc |
@@ -0,0 +1,190 @@ |
+// 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 "ui/android/delegated_frame_host_android.h" |
+ |
+#include "base/logging.h" |
+#include "cc/layers/solid_color_layer.h" |
+#include "cc/layers/surface_layer.h" |
+#include "cc/output/compositor_frame.h" |
+#include "cc/output/copy_output_request.h" |
+#include "cc/surfaces/surface.h" |
+#include "cc/surfaces/surface_id.h" |
+#include "cc/surfaces/surface_id_allocator.h" |
+#include "cc/surfaces/surface_manager.h" |
+#include "ui/android/context_provider_factory.h" |
+#include "ui/android/view_android.h" |
+ |
+namespace ui { |
+ |
+namespace { |
+ |
+void SatisfyCallback(cc::SurfaceManager* manager, |
+ const cc::SurfaceSequence& sequence) { |
+ std::vector<uint32_t> sequences; |
+ sequences.push_back(sequence.sequence); |
+ manager->DidSatisfySequences(sequence.client_id, &sequences); |
+} |
+ |
+void RequireCallback(cc::SurfaceManager* manager, |
+ const cc::SurfaceId& id, |
+ const cc::SurfaceSequence& sequence) { |
+ cc::Surface* surface = manager->GetSurfaceForId(id); |
+ if (!surface) { |
+ LOG(ERROR) << "Attempting to require callback on nonexistent surface"; |
+ return; |
+ } |
+ surface->AddDestructionDependency(sequence); |
+} |
+ |
+} // namespace |
+ |
+DelegatedFrameHostAndroid::DelegatedFrameHostAndroid( |
+ ui::ViewAndroid* view, |
+ SkColor background_color, |
+ ReturnResourcesCallback return_resources_callback) |
+ : view_(view), |
+ return_resources_callback_(return_resources_callback), |
+ background_layer_(cc::SolidColorLayer::Create()) { |
+ DCHECK(view_); |
+ DCHECK(!return_resources_callback_.is_null()); |
+ |
+ surface_manager_ = |
+ ui::ContextProviderFactory::GetInstance()->GetSurfaceManager(); |
+ surface_id_allocator_.reset(new cc::SurfaceIdAllocator( |
+ ui::ContextProviderFactory::GetInstance()->AllocateSurfaceClientId())); |
+ surface_manager_->RegisterSurfaceClientId(surface_id_allocator_->client_id()); |
+ |
+ background_layer_->SetBackgroundColor(background_color); |
+ view_->GetLayer()->AddChild(background_layer_); |
+ UpdateBackgroundLayer(); |
+} |
+ |
+DelegatedFrameHostAndroid::~DelegatedFrameHostAndroid() { |
+ DestroyDelegatedContent(); |
+ surface_factory_.reset(); |
+ surface_manager_->InvalidateSurfaceClientId( |
+ surface_id_allocator_->client_id()); |
+ background_layer_->RemoveFromParent(); |
+} |
+ |
+void DelegatedFrameHostAndroid::SubmitCompositorFrame( |
+ cc::CompositorFrame frame, |
+ cc::SurfaceFactory::DrawCallback draw_callback) { |
+ if (!surface_factory_) { |
+ surface_factory_ = |
+ base::WrapUnique(new cc::SurfaceFactory(surface_manager_, this)); |
+ } |
+ |
+ cc::RenderPass* root_pass = |
+ frame.delegated_frame_data->render_pass_list.back().get(); |
+ gfx::Size texture_size_in_layer = root_pass->output_rect.size(); |
no sievers
2016/08/12 19:33:04
nit: This name is a bit of an artifact. You can pr
Khushal
2016/08/16 01:34:23
Done.
|
+ |
+ if (surface_id_.is_null() || texture_size_in_layer != current_surface_size_ || |
+ location_bar_content_translation_ != |
+ frame.metadata.location_bar_content_translation || |
+ current_viewport_selection_ != frame.metadata.selection) { |
+ DestroyDelegatedContent(); |
+ DCHECK(!content_layer_); |
+ DCHECK(surface_id_.is_null()); |
+ |
+ surface_id_ = surface_id_allocator_->GenerateId(); |
+ surface_factory_->Create(surface_id_); |
+ |
+ current_surface_size_ = texture_size_in_layer; |
+ location_bar_content_translation_ = |
+ frame.metadata.location_bar_content_translation; |
+ current_viewport_selection_ = frame.metadata.selection; |
+ CreateContentLayer(); |
+ } |
+ |
+ surface_factory_->SubmitCompositorFrame(surface_id_, std::move(frame), |
+ draw_callback); |
+} |
+ |
+uint32_t DelegatedFrameHostAndroid::GetSurfaceClientId() const { |
+ return surface_id_allocator_->client_id(); |
+} |
+ |
+void DelegatedFrameHostAndroid::RequestCopyOfSurface( |
+ std::unique_ptr<cc::CopyOutputRequest> copy_output_request) { |
+ DCHECK(!surface_id_.is_null()); |
+ surface_factory_->RequestCopyOfSurface(surface_id_, |
+ std::move(copy_output_request)); |
+} |
+ |
+void DelegatedFrameHostAndroid::DestroyDelegatedContent() { |
+ if (surface_id_.is_null()) |
+ return; |
+ |
+ DCHECK(surface_factory_.get()); |
+ DCHECK(content_layer_); |
+ |
+ content_layer_->RemoveFromParent(); |
+ content_layer_ = nullptr; |
+ surface_factory_->Destroy(surface_id_); |
+ surface_id_ = cc::SurfaceId(); |
+ |
+ UpdateBackgroundLayer(); |
+} |
+ |
+void DelegatedFrameHostAndroid::OutputSurfaceChanged() { |
+ DestroyDelegatedContent(); |
+ surface_factory_.reset(); |
+} |
+ |
+void DelegatedFrameHostAndroid::UpdateBackgroundColor(SkColor color) { |
+ background_layer_->SetBackgroundColor(color); |
+} |
+ |
+void DelegatedFrameHostAndroid::UpdateSize( |
+ const gfx::Size& desired_content_size) { |
+ desired_content_size_ = desired_content_size; |
+ background_layer_->SetBounds(desired_content_size); |
+ UpdateBackgroundLayer(); |
+} |
+ |
+cc::Layer* DelegatedFrameHostAndroid::GetContentLayer() const { |
+ return content_layer_.get(); |
+} |
+ |
+void DelegatedFrameHostAndroid::ReturnResources( |
+ const cc::ReturnedResourceArray& resources) { |
+ return_resources_callback_.Run(resources); |
+} |
+ |
+void DelegatedFrameHostAndroid::SetBeginFrameSource( |
+ cc::BeginFrameSource* begin_frame_source) { |
+ // TODO(tansell): Hook this up. |
+} |
+ |
+void DelegatedFrameHostAndroid::CreateContentLayer() { |
+ DCHECK(!content_layer_); |
+ |
+ // manager must outlive compositors using it. |
+ content_layer_ = cc::SurfaceLayer::Create( |
+ base::Bind(&SatisfyCallback, base::Unretained(surface_manager_)), |
+ base::Bind(&RequireCallback, base::Unretained(surface_manager_))); |
+ content_layer_->SetSurfaceId(surface_id_, 1.f, current_surface_size_); |
+ content_layer_->SetBounds(current_surface_size_); |
+ content_layer_->SetIsDrawable(true); |
+ content_layer_->SetContentsOpaque(true); |
+ |
+ view_->GetLayer()->AddChild(content_layer_); |
+ |
+ UpdateBackgroundLayer(); |
+} |
+ |
+void DelegatedFrameHostAndroid::UpdateBackgroundLayer() { |
+ // The background layer draws in 2 cases: |
+ // 1) When we don't have any content from the renderer. |
+ // 2) When the bounds of the content received from the renderer does not match |
+ // the desired content bounds. |
+ bool background_is_drawable = |
+ content_layer_.get() == nullptr || |
+ content_layer_->bounds() != desired_content_size_; |
no sievers
2016/08/12 19:33:04
See other comment about dip vs. pix.
Also, this w
Khushal
2016/08/16 01:34:23
Thanks for pointing that out.
|
+ background_layer_->SetIsDrawable(background_is_drawable); |
+} |
+ |
+} // namespace ui |