khushalsagar | a693aa7 | 2016-08-16 22:18:06 | [diff] [blame] | 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 "ui/android/delegated_frame_host_android.h" |
| 6 | |
xlai | 180af79 | 2016-08-26 22:07:42 | [diff] [blame] | 7 | #include "base/bind.h" |
khushalsagar | a693aa7 | 2016-08-16 22:18:06 | [diff] [blame] | 8 | #include "base/logging.h" |
| 9 | #include "cc/layers/solid_color_layer.h" |
| 10 | #include "cc/layers/surface_layer.h" |
| 11 | #include "cc/output/compositor_frame.h" |
| 12 | #include "cc/output/copy_output_result.h" |
| 13 | #include "cc/surfaces/surface.h" |
| 14 | #include "cc/surfaces/surface_id.h" |
| 15 | #include "cc/surfaces/surface_id_allocator.h" |
| 16 | #include "cc/surfaces/surface_manager.h" |
| 17 | #include "ui/android/context_provider_factory.h" |
| 18 | #include "ui/android/view_android.h" |
| 19 | #include "ui/android/window_android_compositor.h" |
| 20 | #include "ui/gfx/android/device_display_info.h" |
| 21 | #include "ui/gfx/geometry/dip_util.h" |
| 22 | |
| 23 | namespace ui { |
| 24 | |
| 25 | namespace { |
| 26 | |
| 27 | void SatisfyCallback(cc::SurfaceManager* manager, |
| 28 | const cc::SurfaceSequence& sequence) { |
| 29 | std::vector<uint32_t> sequences; |
| 30 | sequences.push_back(sequence.sequence); |
fsamuel | e0f705b | 2016-10-01 14:07:14 | [diff] [blame] | 31 | manager->DidSatisfySequences(sequence.frame_sink_id, &sequences); |
khushalsagar | a693aa7 | 2016-08-16 22:18:06 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | void RequireCallback(cc::SurfaceManager* manager, |
| 35 | const cc::SurfaceId& id, |
| 36 | const cc::SurfaceSequence& sequence) { |
| 37 | cc::Surface* surface = manager->GetSurfaceForId(id); |
| 38 | if (!surface) { |
| 39 | LOG(ERROR) << "Attempting to require callback on nonexistent surface"; |
| 40 | return; |
| 41 | } |
| 42 | surface->AddDestructionDependency(sequence); |
| 43 | } |
| 44 | |
| 45 | scoped_refptr<cc::SurfaceLayer> CreateSurfaceLayer( |
| 46 | cc::SurfaceManager* surface_manager, |
| 47 | cc::SurfaceId surface_id, |
jbauman | 862bef9 | 2016-09-08 00:32:47 | [diff] [blame] | 48 | const gfx::Size surface_size, |
| 49 | bool surface_opaque) { |
khushalsagar | a693aa7 | 2016-08-16 22:18:06 | [diff] [blame] | 50 | // manager must outlive compositors using it. |
| 51 | scoped_refptr<cc::SurfaceLayer> layer = cc::SurfaceLayer::Create( |
| 52 | base::Bind(&SatisfyCallback, base::Unretained(surface_manager)), |
| 53 | base::Bind(&RequireCallback, base::Unretained(surface_manager))); |
| 54 | layer->SetSurfaceId(surface_id, 1.f, surface_size); |
| 55 | layer->SetBounds(surface_size); |
| 56 | layer->SetIsDrawable(true); |
jbauman | 862bef9 | 2016-09-08 00:32:47 | [diff] [blame] | 57 | layer->SetContentsOpaque(surface_opaque); |
khushalsagar | a693aa7 | 2016-08-16 22:18:06 | [diff] [blame] | 58 | |
| 59 | return layer; |
| 60 | } |
| 61 | |
| 62 | void CopyOutputRequestCallback( |
| 63 | scoped_refptr<cc::Layer> readback_layer, |
| 64 | cc::CopyOutputRequest::CopyOutputRequestCallback result_callback, |
| 65 | std::unique_ptr<cc::CopyOutputResult> copy_output_result) { |
| 66 | readback_layer->RemoveFromParent(); |
| 67 | result_callback.Run(std::move(copy_output_result)); |
| 68 | } |
| 69 | |
| 70 | } // namespace |
| 71 | |
| 72 | DelegatedFrameHostAndroid::DelegatedFrameHostAndroid( |
| 73 | ui::ViewAndroid* view, |
| 74 | SkColor background_color, |
| 75 | ReturnResourcesCallback return_resources_callback) |
fsamuel | b6acafa | 2016-10-04 03:21:52 | [diff] [blame] | 76 | : frame_sink_id_( |
| 77 | ui::ContextProviderFactory::GetInstance()->AllocateFrameSinkId()), |
| 78 | view_(view), |
khushalsagar | a693aa7 | 2016-08-16 22:18:06 | [diff] [blame] | 79 | return_resources_callback_(return_resources_callback), |
| 80 | background_layer_(cc::SolidColorLayer::Create()) { |
| 81 | DCHECK(view_); |
| 82 | DCHECK(!return_resources_callback_.is_null()); |
| 83 | |
| 84 | surface_manager_ = |
| 85 | ui::ContextProviderFactory::GetInstance()->GetSurfaceManager(); |
fsamuel | 01f3620 | 2016-10-06 01:08:28 | [diff] [blame] | 86 | surface_id_allocator_.reset(new cc::SurfaceIdAllocator()); |
fsamuel | b6acafa | 2016-10-04 03:21:52 | [diff] [blame] | 87 | surface_manager_->RegisterFrameSinkId(frame_sink_id_); |
piman | d6da33fd | 2016-11-08 18:35:35 | [diff] [blame] | 88 | surface_factory_ = base::WrapUnique( |
| 89 | new cc::SurfaceFactory(frame_sink_id_, surface_manager_, this)); |
khushalsagar | a693aa7 | 2016-08-16 22:18:06 | [diff] [blame] | 90 | |
| 91 | background_layer_->SetBackgroundColor(background_color); |
| 92 | view_->GetLayer()->AddChild(background_layer_); |
| 93 | UpdateBackgroundLayer(); |
| 94 | } |
| 95 | |
| 96 | DelegatedFrameHostAndroid::~DelegatedFrameHostAndroid() { |
| 97 | DestroyDelegatedContent(); |
| 98 | surface_factory_.reset(); |
fsamuel | e0f705b | 2016-10-01 14:07:14 | [diff] [blame] | 99 | UnregisterFrameSinkHierarchy(); |
fsamuel | b6acafa | 2016-10-04 03:21:52 | [diff] [blame] | 100 | surface_manager_->InvalidateFrameSinkId(frame_sink_id_); |
khushalsagar | a693aa7 | 2016-08-16 22:18:06 | [diff] [blame] | 101 | background_layer_->RemoveFromParent(); |
| 102 | } |
| 103 | |
| 104 | DelegatedFrameHostAndroid::FrameData::FrameData() = default; |
| 105 | |
| 106 | DelegatedFrameHostAndroid::FrameData::~FrameData() = default; |
| 107 | |
| 108 | void DelegatedFrameHostAndroid::SubmitCompositorFrame( |
| 109 | cc::CompositorFrame frame, |
| 110 | cc::SurfaceFactory::DrawCallback draw_callback) { |
samans | 4ba3a0e | 2016-10-29 02:27:45 | [diff] [blame] | 111 | cc::RenderPass* root_pass = frame.render_pass_list.back().get(); |
khushalsagar | a693aa7 | 2016-08-16 22:18:06 | [diff] [blame] | 112 | gfx::Size surface_size = root_pass->output_rect.size(); |
| 113 | |
| 114 | if (!current_frame_ || surface_size != current_frame_->surface_size || |
ianwen | e5fc578 | 2016-08-18 04:05:15 | [diff] [blame] | 115 | current_frame_->top_controls_height != |
| 116 | frame.metadata.top_controls_height || |
| 117 | current_frame_->top_controls_shown_ratio != |
| 118 | frame.metadata.top_controls_shown_ratio || |
| 119 | current_frame_->bottom_controls_height != |
| 120 | frame.metadata.bottom_controls_height || |
| 121 | current_frame_->bottom_controls_shown_ratio != |
| 122 | frame.metadata.bottom_controls_shown_ratio || |
jbauman | 862bef9 | 2016-09-08 00:32:47 | [diff] [blame] | 123 | current_frame_->viewport_selection != frame.metadata.selection || |
| 124 | current_frame_->has_transparent_background != |
| 125 | root_pass->has_transparent_background) { |
khushalsagar | a693aa7 | 2016-08-16 22:18:06 | [diff] [blame] | 126 | DestroyDelegatedContent(); |
| 127 | DCHECK(!content_layer_); |
| 128 | DCHECK(!current_frame_); |
| 129 | |
| 130 | current_frame_ = base::MakeUnique<FrameData>(); |
fsamuel | 01f3620 | 2016-10-06 01:08:28 | [diff] [blame] | 131 | current_frame_->local_frame_id = surface_id_allocator_->GenerateId(); |
| 132 | surface_factory_->Create(current_frame_->local_frame_id); |
khushalsagar | a693aa7 | 2016-08-16 22:18:06 | [diff] [blame] | 133 | |
| 134 | current_frame_->surface_size = surface_size; |
ianwen | e5fc578 | 2016-08-18 04:05:15 | [diff] [blame] | 135 | current_frame_->top_controls_height = frame.metadata.top_controls_height; |
| 136 | current_frame_->top_controls_shown_ratio = |
| 137 | frame.metadata.top_controls_shown_ratio; |
| 138 | current_frame_->bottom_controls_height = |
| 139 | frame.metadata.bottom_controls_height; |
| 140 | current_frame_->bottom_controls_shown_ratio = |
| 141 | frame.metadata.bottom_controls_shown_ratio; |
jbauman | 862bef9 | 2016-09-08 00:32:47 | [diff] [blame] | 142 | current_frame_->has_transparent_background = |
| 143 | root_pass->has_transparent_background; |
ianwen | e5fc578 | 2016-08-18 04:05:15 | [diff] [blame] | 144 | |
khushalsagar | a693aa7 | 2016-08-16 22:18:06 | [diff] [blame] | 145 | current_frame_->viewport_selection = frame.metadata.selection; |
fsamuel | 01f3620 | 2016-10-06 01:08:28 | [diff] [blame] | 146 | content_layer_ = CreateSurfaceLayer( |
| 147 | surface_manager_, cc::SurfaceId(surface_factory_->frame_sink_id(), |
| 148 | current_frame_->local_frame_id), |
| 149 | current_frame_->surface_size, |
| 150 | !current_frame_->has_transparent_background); |
khushalsagar | a693aa7 | 2016-08-16 22:18:06 | [diff] [blame] | 151 | view_->GetLayer()->AddChild(content_layer_); |
| 152 | UpdateBackgroundLayer(); |
| 153 | } |
| 154 | |
fsamuel | 01f3620 | 2016-10-06 01:08:28 | [diff] [blame] | 155 | surface_factory_->SubmitCompositorFrame(current_frame_->local_frame_id, |
khushalsagar | a693aa7 | 2016-08-16 22:18:06 | [diff] [blame] | 156 | std::move(frame), draw_callback); |
| 157 | } |
| 158 | |
fsamuel | e0f705b | 2016-10-01 14:07:14 | [diff] [blame] | 159 | cc::FrameSinkId DelegatedFrameHostAndroid::GetFrameSinkId() const { |
fsamuel | b6acafa | 2016-10-04 03:21:52 | [diff] [blame] | 160 | return frame_sink_id_; |
khushalsagar | a693aa7 | 2016-08-16 22:18:06 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | void DelegatedFrameHostAndroid::RequestCopyOfSurface( |
| 164 | WindowAndroidCompositor* compositor, |
| 165 | const gfx::Rect& src_subrect_in_pixel, |
| 166 | cc::CopyOutputRequest::CopyOutputRequestCallback result_callback) { |
| 167 | DCHECK(current_frame_); |
| 168 | DCHECK(!result_callback.is_null()); |
| 169 | |
fsamuel | 01f3620 | 2016-10-06 01:08:28 | [diff] [blame] | 170 | scoped_refptr<cc::Layer> readback_layer = CreateSurfaceLayer( |
| 171 | surface_manager_, cc::SurfaceId(surface_factory_->frame_sink_id(), |
| 172 | current_frame_->local_frame_id), |
| 173 | current_frame_->surface_size, |
| 174 | !current_frame_->has_transparent_background); |
khushalsagar | a693aa7 | 2016-08-16 22:18:06 | [diff] [blame] | 175 | readback_layer->SetHideLayerAndSubtree(true); |
| 176 | compositor->AttachLayerForReadback(readback_layer); |
| 177 | std::unique_ptr<cc::CopyOutputRequest> copy_output_request = |
| 178 | cc::CopyOutputRequest::CreateRequest(base::Bind( |
| 179 | &CopyOutputRequestCallback, readback_layer, result_callback)); |
| 180 | |
| 181 | if (!src_subrect_in_pixel.IsEmpty()) |
| 182 | copy_output_request->set_area(src_subrect_in_pixel); |
| 183 | |
sievers | 700fe26 | 2016-08-26 00:54:42 | [diff] [blame] | 184 | readback_layer->RequestCopyOfOutput(std::move(copy_output_request)); |
khushalsagar | a693aa7 | 2016-08-16 22:18:06 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | void DelegatedFrameHostAndroid::DestroyDelegatedContent() { |
| 188 | if (!current_frame_) |
| 189 | return; |
| 190 | |
khushalsagar | a693aa7 | 2016-08-16 22:18:06 | [diff] [blame] | 191 | DCHECK(content_layer_); |
| 192 | |
| 193 | content_layer_->RemoveFromParent(); |
| 194 | content_layer_ = nullptr; |
fsamuel | 01f3620 | 2016-10-06 01:08:28 | [diff] [blame] | 195 | surface_factory_->Destroy(current_frame_->local_frame_id); |
khushalsagar | a693aa7 | 2016-08-16 22:18:06 | [diff] [blame] | 196 | current_frame_.reset(); |
| 197 | |
| 198 | UpdateBackgroundLayer(); |
| 199 | } |
| 200 | |
| 201 | bool DelegatedFrameHostAndroid::HasDelegatedContent() const { |
| 202 | return current_frame_.get() != nullptr; |
| 203 | } |
| 204 | |
danakj | 1120f4c | 2016-09-15 02:05:32 | [diff] [blame] | 205 | void DelegatedFrameHostAndroid::CompositorFrameSinkChanged() { |
khushalsagar | a693aa7 | 2016-08-16 22:18:06 | [diff] [blame] | 206 | DestroyDelegatedContent(); |
piman | d6da33fd | 2016-11-08 18:35:35 | [diff] [blame] | 207 | surface_factory_->Reset(); |
khushalsagar | a693aa7 | 2016-08-16 22:18:06 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | void DelegatedFrameHostAndroid::UpdateBackgroundColor(SkColor color) { |
| 211 | background_layer_->SetBackgroundColor(color); |
khushalsagar | a693aa7 | 2016-08-16 22:18:06 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | void DelegatedFrameHostAndroid::UpdateContainerSizeinDIP( |
| 215 | const gfx::Size& size_in_dip) { |
| 216 | container_size_in_dip_ = size_in_dip; |
| 217 | background_layer_->SetBounds(gfx::ConvertSizeToPixel( |
| 218 | gfx::DeviceDisplayInfo().GetDIPScale(), container_size_in_dip_)); |
| 219 | UpdateBackgroundLayer(); |
| 220 | } |
| 221 | |
fsamuel | e0f705b | 2016-10-01 14:07:14 | [diff] [blame] | 222 | void DelegatedFrameHostAndroid::RegisterFrameSinkHierarchy( |
| 223 | const cc::FrameSinkId& parent_id) { |
staraz | f907282a | 2016-11-09 21:06:58 | [diff] [blame] | 224 | if (registered_parent_frame_sink_id_.is_valid()) |
fsamuel | e0f705b | 2016-10-01 14:07:14 | [diff] [blame] | 225 | UnregisterFrameSinkHierarchy(); |
| 226 | registered_parent_frame_sink_id_ = parent_id; |
fsamuel | b6acafa | 2016-10-04 03:21:52 | [diff] [blame] | 227 | surface_manager_->RegisterSurfaceFactoryClient(frame_sink_id_, this); |
| 228 | surface_manager_->RegisterFrameSinkHierarchy(parent_id, frame_sink_id_); |
enne | a487a27 | 2016-09-30 19:56:18 | [diff] [blame] | 229 | } |
| 230 | |
fsamuel | e0f705b | 2016-10-01 14:07:14 | [diff] [blame] | 231 | void DelegatedFrameHostAndroid::UnregisterFrameSinkHierarchy() { |
staraz | f907282a | 2016-11-09 21:06:58 | [diff] [blame] | 232 | if (!registered_parent_frame_sink_id_.is_valid()) |
enne | a487a27 | 2016-09-30 19:56:18 | [diff] [blame] | 233 | return; |
fsamuel | b6acafa | 2016-10-04 03:21:52 | [diff] [blame] | 234 | surface_manager_->UnregisterSurfaceFactoryClient(frame_sink_id_); |
fsamuel | e0f705b | 2016-10-01 14:07:14 | [diff] [blame] | 235 | surface_manager_->UnregisterFrameSinkHierarchy( |
fsamuel | b6acafa | 2016-10-04 03:21:52 | [diff] [blame] | 236 | registered_parent_frame_sink_id_, frame_sink_id_); |
fsamuel | e0f705b | 2016-10-01 14:07:14 | [diff] [blame] | 237 | registered_parent_frame_sink_id_ = cc::FrameSinkId(); |
enne | a487a27 | 2016-09-30 19:56:18 | [diff] [blame] | 238 | } |
| 239 | |
khushalsagar | a693aa7 | 2016-08-16 22:18:06 | [diff] [blame] | 240 | void DelegatedFrameHostAndroid::ReturnResources( |
| 241 | const cc::ReturnedResourceArray& resources) { |
| 242 | return_resources_callback_.Run(resources); |
| 243 | } |
| 244 | |
| 245 | void DelegatedFrameHostAndroid::SetBeginFrameSource( |
| 246 | cc::BeginFrameSource* begin_frame_source) { |
enne | a487a27 | 2016-09-30 19:56:18 | [diff] [blame] | 247 | // TODO(enne): hook this up instead of making RWHVAndroid a |
| 248 | // WindowAndroidObserver. |
khushalsagar | a693aa7 | 2016-08-16 22:18:06 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | void DelegatedFrameHostAndroid::UpdateBackgroundLayer() { |
| 252 | // The background layer draws in 2 cases: |
| 253 | // 1) When we don't have any content from the renderer. |
| 254 | // 2) When the bounds of the content received from the renderer does not match |
| 255 | // the desired content bounds. |
| 256 | bool background_is_drawable = false; |
| 257 | |
| 258 | if (current_frame_) { |
| 259 | float device_scale_factor = gfx::DeviceDisplayInfo().GetDIPScale(); |
| 260 | gfx::Size content_size_in_dip = gfx::ConvertSizeToDIP( |
| 261 | device_scale_factor, current_frame_->surface_size); |
khushalsagar | a693aa7 | 2016-08-16 22:18:06 | [diff] [blame] | 262 | background_is_drawable = |
| 263 | content_size_in_dip.width() < container_size_in_dip_.width() || |
| 264 | content_size_in_dip.height() < container_size_in_dip_.height(); |
| 265 | } else { |
| 266 | background_is_drawable = true; |
| 267 | } |
| 268 | |
| 269 | background_layer_->SetIsDrawable(background_is_drawable); |
| 270 | } |
| 271 | |
| 272 | } // namespace ui |