Chromium Code Reviews| 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 "content/browser/renderer_host/delegated_frame_host_android.h" | |
| 6 | |
| 7 #include "base/logging.h" | |
| 8 #include "cc/layers/solid_color_layer.h" | |
| 9 #include "cc/layers/surface_layer.h" | |
| 10 #include "cc/output/compositor_frame.h" | |
| 11 #include "cc/surfaces/surface.h" | |
| 12 #include "cc/surfaces/surface_id.h" | |
| 13 #include "cc/surfaces/surface_id_allocator.h" | |
| 14 #include "cc/surfaces/surface_manager.h" | |
| 15 #include "content/browser/renderer_host/compositor_impl_android.h" | |
| 16 | |
| 17 namespace content { | |
| 18 | |
| 19 namespace { | |
| 20 | |
| 21 void SatisfyCallback(cc::SurfaceManager* manager, | |
| 22 const cc::SurfaceSequence& sequence) { | |
| 23 std::vector<uint32_t> sequences; | |
| 24 sequences.push_back(sequence.sequence); | |
| 25 manager->DidSatisfySequences(sequence.client_id, &sequences); | |
| 26 } | |
| 27 | |
| 28 void RequireCallback(cc::SurfaceManager* manager, | |
| 29 const cc::SurfaceId& id, | |
| 30 const cc::SurfaceSequence& sequence) { | |
| 31 cc::Surface* surface = manager->GetSurfaceForId(id); | |
| 32 if (!surface) { | |
| 33 LOG(ERROR) << "Attempting to require callback on nonexistent surface"; | |
| 34 return; | |
| 35 } | |
| 36 surface->AddDestructionDependency(sequence); | |
| 37 } | |
| 38 | |
| 39 } // namespace | |
| 40 | |
| 41 DelegatedFrameHostAndroid::DelegatedFrameHostAndroid( | |
| 42 ui::ViewAndroid* view, | |
| 43 SkColor background_color, | |
| 44 ReturnResourcesCallback return_resources_callback) | |
| 45 : view_(view), | |
| 46 return_resources_callback_(return_resources_callback), | |
| 47 background_layer_(cc::SolidColorLayer::Create()) { | |
| 48 DCHECK(view_); | |
| 49 DCHECK(!return_resources_callback_.is_null()); | |
| 50 | |
| 51 // TODO(khushalsagar): These CompositorImpl dependencies should move to ui? | |
| 52 surface_manager_ = CompositorImpl::GetSurfaceManager(); | |
|
Khushal
2016/07/27 21:58:38
Was talking to David about this. Should we move th
| |
| 53 surface_id_allocator_.reset( | |
| 54 new cc::SurfaceIdAllocator(CompositorImpl::AllocateSurfaceClientId())); | |
| 55 surface_manager_->RegisterSurfaceClientId(surface_id_allocator_->client_id()); | |
| 56 | |
| 57 background_layer_->SetIsDrawable(true); | |
| 58 background_layer_->SetBackgroundColor(background_color); | |
| 59 view_->GetLayer()->AddChild(background_layer_); | |
| 60 } | |
| 61 | |
| 62 DelegatedFrameHostAndroid::~DelegatedFrameHostAndroid() { | |
| 63 DestroyDelegatedContent(); | |
| 64 surface_factory_.reset(); | |
| 65 surface_manager_->InvalidateSurfaceClientId( | |
| 66 surface_id_allocator_->client_id()); | |
| 67 } | |
| 68 | |
| 69 void DelegatedFrameHostAndroid::SubmitCompositorFrame( | |
| 70 cc::CompositorFrame frame, | |
| 71 cc::SurfaceFactory::DrawCallback draw_callback) { | |
| 72 if (!surface_factory_) { | |
| 73 surface_factory_ = | |
| 74 base::WrapUnique(new cc::SurfaceFactory(surface_manager_, this)); | |
| 75 } | |
| 76 | |
| 77 cc::RenderPass* root_pass = | |
| 78 frame.delegated_frame_data->render_pass_list.back().get(); | |
| 79 gfx::Size texture_size_in_layer = root_pass->output_rect.size(); | |
| 80 | |
| 81 if (surface_id_.is_null() || texture_size_in_layer != current_surface_size_ || | |
| 82 location_bar_content_translation_ != | |
| 83 frame.metadata.location_bar_content_translation || | |
| 84 current_viewport_selection_ != frame.metadata.selection) { | |
| 85 DestroyDelegatedContent(); | |
| 86 DCHECK(!content_layer_); | |
| 87 | |
| 88 surface_id_ = surface_id_allocator_->GenerateId(); | |
| 89 surface_factory_->Create(surface_id_); | |
| 90 | |
| 91 current_surface_size_ = texture_size_in_layer; | |
| 92 location_bar_content_translation_ = | |
| 93 frame.metadata.location_bar_content_translation; | |
| 94 current_viewport_selection_ = frame.metadata.selection; | |
| 95 UpdateContentLayer(std::move(CreateSurfaceLayer())); | |
| 96 } | |
| 97 | |
| 98 surface_factory_->SubmitCompositorFrame(surface_id_, std::move(frame), | |
| 99 draw_callback); | |
| 100 } | |
| 101 | |
| 102 uint32_t DelegatedFrameHostAndroid::GetSurfaceClientId() const { | |
| 103 return surface_id_allocator_->client_id(); | |
| 104 } | |
| 105 | |
| 106 void DelegatedFrameHostAndroid::DestroyDelegatedContent() { | |
| 107 if (surface_id_.is_null()) | |
| 108 return; | |
| 109 | |
| 110 DCHECK(surface_factory_.get()); | |
| 111 DCHECK(content_layer_); | |
| 112 | |
| 113 UpdateContentLayer(nullptr); | |
| 114 surface_factory_->Destroy(surface_id_); | |
| 115 surface_id_ = cc::SurfaceId(); | |
| 116 } | |
| 117 | |
| 118 void DelegatedFrameHostAndroid::OutputSurfaceChanged() { | |
| 119 DestroyDelegatedContent(); | |
| 120 surface_factory_.reset(); | |
| 121 } | |
| 122 | |
| 123 void DelegatedFrameHostAndroid::UpdateBackgroundColor(SkColor color) { | |
| 124 background_layer_->SetBackgroundColor(color); | |
| 125 } | |
| 126 | |
| 127 void DelegatedFrameHostAndroid::UpdateSize( | |
| 128 const gfx::Size& desired_content_size) { | |
| 129 // TODO(khushalsagar): The gutter logic for when the desired content size | |
| 130 // doesn't match the size of the renderer frame should go here. | |
| 131 background_layer_->SetBounds(desired_content_size); | |
| 132 } | |
| 133 | |
| 134 cc::Layer* DelegatedFrameHostAndroid::GetContentLayer() const { | |
| 135 return content_layer_.get(); | |
| 136 } | |
| 137 | |
| 138 void DelegatedFrameHostAndroid::ReturnResources( | |
| 139 const cc::ReturnedResourceArray& resources) { | |
| 140 return_resources_callback_.Run(resources); | |
| 141 } | |
| 142 | |
| 143 void DelegatedFrameHostAndroid::SetBeginFrameSource( | |
| 144 cc::BeginFrameSource* begin_frame_source) { | |
| 145 // TODO(tansell): Hook this up. | |
| 146 } | |
| 147 | |
| 148 scoped_refptr<cc::SurfaceLayer> | |
| 149 DelegatedFrameHostAndroid::CreateSurfaceLayer() { | |
| 150 DCHECK(!surface_id_.is_null()); | |
| 151 | |
| 152 // manager must outlive compositors using it. | |
| 153 scoped_refptr<cc::SurfaceLayer> surface_layer = cc::SurfaceLayer::Create( | |
| 154 base::Bind(&SatisfyCallback, base::Unretained(surface_manager_)), | |
| 155 base::Bind(&RequireCallback, base::Unretained(surface_manager_))); | |
| 156 surface_layer->SetSurfaceId(surface_id_, 1.f, current_surface_size_); | |
| 157 surface_layer->SetBounds(current_surface_size_); | |
| 158 surface_layer->SetIsDrawable(true); | |
| 159 surface_layer->SetContentsOpaque(true); | |
| 160 | |
| 161 return surface_layer; | |
| 162 } | |
| 163 | |
| 164 void DelegatedFrameHostAndroid::UpdateContentLayer( | |
| 165 scoped_refptr<cc::Layer> content_layer) { | |
| 166 view_->GetLayer()->RemoveAllChildren(); | |
| 167 content_layer_ = std::move(content_layer); | |
| 168 | |
| 169 if (content_layer_) { | |
| 170 view_->GetLayer()->AddChild(content_layer_); | |
| 171 } else { | |
| 172 view_->GetLayer()->AddChild(background_layer_); | |
| 173 } | |
| 174 } | |
| 175 | |
| 176 } // namespace content | |
| OLD | NEW |