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 "ui/android/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/output/copy_output_result.h" |
| 12 #include "cc/surfaces/surface.h" |
| 13 #include "cc/surfaces/surface_id.h" |
| 14 #include "cc/surfaces/surface_id_allocator.h" |
| 15 #include "cc/surfaces/surface_manager.h" |
| 16 #include "ui/android/context_provider_factory.h" |
| 17 #include "ui/android/view_android.h" |
| 18 #include "ui/android/window_android_compositor.h" |
| 19 #include "ui/gfx/android/device_display_info.h" |
| 20 #include "ui/gfx/geometry/dip_util.h" |
| 21 |
| 22 namespace ui { |
| 23 |
| 24 namespace { |
| 25 |
| 26 void SatisfyCallback(cc::SurfaceManager* manager, |
| 27 const cc::SurfaceSequence& sequence) { |
| 28 std::vector<uint32_t> sequences; |
| 29 sequences.push_back(sequence.sequence); |
| 30 manager->DidSatisfySequences(sequence.client_id, &sequences); |
| 31 } |
| 32 |
| 33 void RequireCallback(cc::SurfaceManager* manager, |
| 34 const cc::SurfaceId& id, |
| 35 const cc::SurfaceSequence& sequence) { |
| 36 cc::Surface* surface = manager->GetSurfaceForId(id); |
| 37 if (!surface) { |
| 38 LOG(ERROR) << "Attempting to require callback on nonexistent surface"; |
| 39 return; |
| 40 } |
| 41 surface->AddDestructionDependency(sequence); |
| 42 } |
| 43 |
| 44 scoped_refptr<cc::SurfaceLayer> CreateSurfaceLayer( |
| 45 cc::SurfaceManager* surface_manager, |
| 46 cc::SurfaceId surface_id, |
| 47 const gfx::Size surface_size) { |
| 48 // manager must outlive compositors using it. |
| 49 scoped_refptr<cc::SurfaceLayer> layer = cc::SurfaceLayer::Create( |
| 50 base::Bind(&SatisfyCallback, base::Unretained(surface_manager)), |
| 51 base::Bind(&RequireCallback, base::Unretained(surface_manager))); |
| 52 layer->SetSurfaceId(surface_id, 1.f, surface_size); |
| 53 layer->SetBounds(surface_size); |
| 54 layer->SetIsDrawable(true); |
| 55 layer->SetContentsOpaque(true); |
| 56 |
| 57 return layer; |
| 58 } |
| 59 |
| 60 void CopyOutputRequestCallback( |
| 61 scoped_refptr<cc::Layer> readback_layer, |
| 62 cc::CopyOutputRequest::CopyOutputRequestCallback result_callback, |
| 63 std::unique_ptr<cc::CopyOutputResult> copy_output_result) { |
| 64 readback_layer->RemoveFromParent(); |
| 65 result_callback.Run(std::move(copy_output_result)); |
| 66 } |
| 67 |
| 68 } // namespace |
| 69 |
| 70 DelegatedFrameHostAndroid::DelegatedFrameHostAndroid( |
| 71 ui::ViewAndroid* view, |
| 72 SkColor background_color, |
| 73 ReturnResourcesCallback return_resources_callback) |
| 74 : view_(view), |
| 75 return_resources_callback_(return_resources_callback), |
| 76 background_layer_(cc::SolidColorLayer::Create()) { |
| 77 DCHECK(view_); |
| 78 DCHECK(!return_resources_callback_.is_null()); |
| 79 |
| 80 surface_manager_ = |
| 81 ui::ContextProviderFactory::GetInstance()->GetSurfaceManager(); |
| 82 surface_id_allocator_.reset(new cc::SurfaceIdAllocator( |
| 83 ui::ContextProviderFactory::GetInstance()->AllocateSurfaceClientId())); |
| 84 surface_manager_->RegisterSurfaceClientId(surface_id_allocator_->client_id()); |
| 85 |
| 86 background_layer_->SetBackgroundColor(background_color); |
| 87 view_->GetLayer()->AddChild(background_layer_); |
| 88 UpdateBackgroundLayer(); |
| 89 } |
| 90 |
| 91 DelegatedFrameHostAndroid::~DelegatedFrameHostAndroid() { |
| 92 DestroyDelegatedContent(); |
| 93 surface_factory_.reset(); |
| 94 surface_manager_->InvalidateSurfaceClientId( |
| 95 surface_id_allocator_->client_id()); |
| 96 background_layer_->RemoveFromParent(); |
| 97 } |
| 98 |
| 99 DelegatedFrameHostAndroid::FrameData::FrameData() = default; |
| 100 |
| 101 DelegatedFrameHostAndroid::FrameData::~FrameData() = default; |
| 102 |
| 103 void DelegatedFrameHostAndroid::SubmitCompositorFrame( |
| 104 cc::CompositorFrame frame, |
| 105 cc::SurfaceFactory::DrawCallback draw_callback) { |
| 106 if (!surface_factory_) { |
| 107 surface_factory_ = |
| 108 base::WrapUnique(new cc::SurfaceFactory(surface_manager_, this)); |
| 109 } |
| 110 |
| 111 cc::RenderPass* root_pass = |
| 112 frame.delegated_frame_data->render_pass_list.back().get(); |
| 113 gfx::Size surface_size = root_pass->output_rect.size(); |
| 114 |
| 115 if (!current_frame_ || surface_size != current_frame_->surface_size || |
| 116 current_frame_->location_bar_content_translation != |
| 117 frame.metadata.location_bar_content_translation || |
| 118 current_frame_->viewport_selection != frame.metadata.selection) { |
| 119 DestroyDelegatedContent(); |
| 120 DCHECK(!content_layer_); |
| 121 DCHECK(!current_frame_); |
| 122 |
| 123 current_frame_ = base::MakeUnique<FrameData>(); |
| 124 current_frame_->surface_id = surface_id_allocator_->GenerateId(); |
| 125 surface_factory_->Create(current_frame_->surface_id); |
| 126 |
| 127 current_frame_->surface_size = surface_size; |
| 128 current_frame_->location_bar_content_translation = |
| 129 frame.metadata.location_bar_content_translation; |
| 130 current_frame_->viewport_selection = frame.metadata.selection; |
| 131 content_layer_ = |
| 132 CreateSurfaceLayer(surface_manager_, current_frame_->surface_id, |
| 133 current_frame_->surface_size); |
| 134 view_->GetLayer()->AddChild(content_layer_); |
| 135 UpdateBackgroundLayer(); |
| 136 } |
| 137 |
| 138 surface_factory_->SubmitCompositorFrame(current_frame_->surface_id, |
| 139 std::move(frame), draw_callback); |
| 140 } |
| 141 |
| 142 uint32_t DelegatedFrameHostAndroid::GetSurfaceClientId() const { |
| 143 return surface_id_allocator_->client_id(); |
| 144 } |
| 145 |
| 146 void DelegatedFrameHostAndroid::RequestCopyOfSurface( |
| 147 WindowAndroidCompositor* compositor, |
| 148 const gfx::Rect& src_subrect_in_pixel, |
| 149 cc::CopyOutputRequest::CopyOutputRequestCallback result_callback) { |
| 150 DCHECK(current_frame_); |
| 151 DCHECK(!result_callback.is_null()); |
| 152 |
| 153 scoped_refptr<cc::Layer> readback_layer = |
| 154 CreateSurfaceLayer(surface_manager_, current_frame_->surface_id, |
| 155 current_frame_->surface_size); |
| 156 readback_layer->SetHideLayerAndSubtree(true); |
| 157 compositor->AttachLayerForReadback(readback_layer); |
| 158 std::unique_ptr<cc::CopyOutputRequest> copy_output_request = |
| 159 cc::CopyOutputRequest::CreateRequest(base::Bind( |
| 160 &CopyOutputRequestCallback, readback_layer, result_callback)); |
| 161 |
| 162 if (!src_subrect_in_pixel.IsEmpty()) |
| 163 copy_output_request->set_area(src_subrect_in_pixel); |
| 164 |
| 165 surface_factory_->RequestCopyOfSurface(current_frame_->surface_id, |
| 166 std::move(copy_output_request)); |
| 167 } |
| 168 |
| 169 void DelegatedFrameHostAndroid::DestroyDelegatedContent() { |
| 170 if (!current_frame_) |
| 171 return; |
| 172 |
| 173 DCHECK(surface_factory_.get()); |
| 174 DCHECK(content_layer_); |
| 175 |
| 176 content_layer_->RemoveFromParent(); |
| 177 content_layer_ = nullptr; |
| 178 surface_factory_->Destroy(current_frame_->surface_id); |
| 179 current_frame_.reset(); |
| 180 |
| 181 UpdateBackgroundLayer(); |
| 182 } |
| 183 |
| 184 bool DelegatedFrameHostAndroid::HasDelegatedContent() const { |
| 185 return current_frame_.get() != nullptr; |
| 186 } |
| 187 |
| 188 void DelegatedFrameHostAndroid::OutputSurfaceChanged() { |
| 189 DestroyDelegatedContent(); |
| 190 surface_factory_.reset(); |
| 191 } |
| 192 |
| 193 void DelegatedFrameHostAndroid::UpdateBackgroundColor(SkColor color) { |
| 194 background_layer_->SetBackgroundColor(color); |
| 195 } |
| 196 |
| 197 void DelegatedFrameHostAndroid::SetContentsOpaque(bool opaque) { |
| 198 if (!content_layer_) |
| 199 return; |
| 200 content_layer_->SetContentsOpaque(opaque); |
| 201 } |
| 202 |
| 203 void DelegatedFrameHostAndroid::UpdateContainerSizeinDIP( |
| 204 const gfx::Size& size_in_dip) { |
| 205 container_size_in_dip_ = size_in_dip; |
| 206 background_layer_->SetBounds(gfx::ConvertSizeToPixel( |
| 207 gfx::DeviceDisplayInfo().GetDIPScale(), container_size_in_dip_)); |
| 208 UpdateBackgroundLayer(); |
| 209 } |
| 210 |
| 211 void DelegatedFrameHostAndroid::ReturnResources( |
| 212 const cc::ReturnedResourceArray& resources) { |
| 213 return_resources_callback_.Run(resources); |
| 214 } |
| 215 |
| 216 void DelegatedFrameHostAndroid::SetBeginFrameSource( |
| 217 cc::BeginFrameSource* begin_frame_source) { |
| 218 // TODO(tansell): Hook this up. |
| 219 } |
| 220 |
| 221 void DelegatedFrameHostAndroid::UpdateBackgroundLayer() { |
| 222 // The background layer draws in 2 cases: |
| 223 // 1) When we don't have any content from the renderer. |
| 224 // 2) When the bounds of the content received from the renderer does not match |
| 225 // the desired content bounds. |
| 226 bool background_is_drawable = false; |
| 227 |
| 228 if (current_frame_) { |
| 229 float device_scale_factor = gfx::DeviceDisplayInfo().GetDIPScale(); |
| 230 gfx::Size content_size_in_dip = gfx::ConvertSizeToDIP( |
| 231 device_scale_factor, current_frame_->surface_size); |
| 232 content_size_in_dip.set_height( |
| 233 content_size_in_dip.height() + |
| 234 current_frame_->location_bar_content_translation.y()); |
| 235 background_is_drawable = |
| 236 content_size_in_dip.width() < container_size_in_dip_.width() || |
| 237 content_size_in_dip.height() < container_size_in_dip_.height(); |
| 238 } else { |
| 239 background_is_drawable = true; |
| 240 } |
| 241 |
| 242 background_layer_->SetIsDrawable(background_is_drawable); |
| 243 } |
| 244 |
| 245 } // namespace ui |
OLD | NEW |