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 "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_request.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 | |
| 19 namespace ui { | |
| 20 | |
| 21 namespace { | |
| 22 | |
| 23 void SatisfyCallback(cc::SurfaceManager* manager, | |
| 24 const cc::SurfaceSequence& sequence) { | |
| 25 std::vector<uint32_t> sequences; | |
| 26 sequences.push_back(sequence.sequence); | |
| 27 manager->DidSatisfySequences(sequence.client_id, &sequences); | |
| 28 } | |
| 29 | |
| 30 void RequireCallback(cc::SurfaceManager* manager, | |
| 31 const cc::SurfaceId& id, | |
| 32 const cc::SurfaceSequence& sequence) { | |
| 33 cc::Surface* surface = manager->GetSurfaceForId(id); | |
| 34 if (!surface) { | |
| 35 LOG(ERROR) << "Attempting to require callback on nonexistent surface"; | |
| 36 return; | |
| 37 } | |
| 38 surface->AddDestructionDependency(sequence); | |
| 39 } | |
| 40 | |
| 41 } // namespace | |
| 42 | |
| 43 DelegatedFrameHostAndroid::DelegatedFrameHostAndroid( | |
| 44 ui::ViewAndroid* view, | |
| 45 SkColor background_color, | |
| 46 ReturnResourcesCallback return_resources_callback) | |
| 47 : view_(view), | |
| 48 return_resources_callback_(return_resources_callback), | |
| 49 background_layer_(cc::SolidColorLayer::Create()) { | |
| 50 DCHECK(view_); | |
| 51 DCHECK(!return_resources_callback_.is_null()); | |
| 52 | |
| 53 surface_manager_ = | |
| 54 ui::ContextProviderFactory::GetInstance()->GetSurfaceManager(); | |
| 55 surface_id_allocator_.reset(new cc::SurfaceIdAllocator( | |
| 56 ui::ContextProviderFactory::GetInstance()->AllocateSurfaceClientId())); | |
| 57 surface_manager_->RegisterSurfaceClientId(surface_id_allocator_->client_id()); | |
| 58 | |
| 59 background_layer_->SetBackgroundColor(background_color); | |
| 60 view_->GetLayer()->AddChild(background_layer_); | |
| 61 UpdateBackgroundLayer(); | |
| 62 } | |
| 63 | |
| 64 DelegatedFrameHostAndroid::~DelegatedFrameHostAndroid() { | |
|
David Trainor- moved to gerrit
2016/08/04 16:05:46
We should make sure to remove all layers we build
Khushal
2016/08/09 20:54:06
Yup. Done.
| |
| 65 DestroyDelegatedContent(); | |
| 66 surface_factory_.reset(); | |
| 67 surface_manager_->InvalidateSurfaceClientId( | |
| 68 surface_id_allocator_->client_id()); | |
| 69 } | |
| 70 | |
| 71 void DelegatedFrameHostAndroid::SubmitCompositorFrame( | |
| 72 cc::CompositorFrame frame, | |
| 73 cc::SurfaceFactory::DrawCallback draw_callback) { | |
| 74 if (!surface_factory_) { | |
| 75 surface_factory_ = | |
| 76 base::WrapUnique(new cc::SurfaceFactory(surface_manager_, this)); | |
| 77 } | |
| 78 | |
| 79 cc::RenderPass* root_pass = | |
| 80 frame.delegated_frame_data->render_pass_list.back().get(); | |
| 81 gfx::Size texture_size_in_layer = root_pass->output_rect.size(); | |
| 82 | |
| 83 if (surface_id_.is_null() || texture_size_in_layer != current_surface_size_ || | |
| 84 location_bar_content_translation_ != | |
| 85 frame.metadata.location_bar_content_translation || | |
| 86 current_viewport_selection_ != frame.metadata.selection) { | |
| 87 DestroyDelegatedContent(); | |
| 88 DCHECK(!content_layer_); | |
|
David Trainor- moved to gerrit
2016/08/04 16:05:46
might as well DCHECK(surface_id_.is_null()) also i
Khushal
2016/08/09 20:54:07
Done.
| |
| 89 | |
| 90 surface_id_ = surface_id_allocator_->GenerateId(); | |
|
David Trainor- moved to gerrit
2016/08/04 16:05:46
Not necessarily for this CL, but it would be nice
Khushal
2016/08/09 20:54:06
Looked around the code a little bit, and looks lik
| |
| 91 surface_factory_->Create(surface_id_); | |
| 92 | |
| 93 current_surface_size_ = texture_size_in_layer; | |
| 94 location_bar_content_translation_ = | |
| 95 frame.metadata.location_bar_content_translation; | |
| 96 current_viewport_selection_ = frame.metadata.selection; | |
| 97 UpdateContentLayer(std::move(CreateSurfaceLayer())); | |
|
David Trainor- moved to gerrit
2016/08/04 16:05:46
Do we need to pass in the layer or can we just cre
Khushal
2016/08/09 20:54:06
Good idea. Collapsed it into CreateContentLayer an
| |
| 98 } | |
| 99 | |
| 100 surface_factory_->SubmitCompositorFrame(surface_id_, std::move(frame), | |
| 101 draw_callback); | |
| 102 } | |
| 103 | |
| 104 uint32_t DelegatedFrameHostAndroid::GetSurfaceClientId() const { | |
| 105 return surface_id_allocator_->client_id(); | |
| 106 } | |
| 107 | |
| 108 void DelegatedFrameHostAndroid::RequestCopyOfSurface( | |
| 109 std::unique_ptr<cc::CopyOutputRequest> copy_output_request) { | |
| 110 DCHECK(!surface_id_.is_null()); | |
| 111 surface_factory_->RequestCopyOfSurface(surface_id_, | |
| 112 std::move(copy_output_request)); | |
| 113 } | |
| 114 | |
| 115 void DelegatedFrameHostAndroid::DestroyDelegatedContent() { | |
| 116 if (surface_id_.is_null()) | |
| 117 return; | |
| 118 | |
| 119 DCHECK(surface_factory_.get()); | |
| 120 DCHECK(content_layer_); | |
| 121 | |
| 122 UpdateContentLayer(nullptr); | |
| 123 surface_factory_->Destroy(surface_id_); | |
| 124 surface_id_ = cc::SurfaceId(); | |
| 125 } | |
| 126 | |
| 127 void DelegatedFrameHostAndroid::OutputSurfaceChanged() { | |
| 128 DestroyDelegatedContent(); | |
| 129 surface_factory_.reset(); | |
| 130 } | |
| 131 | |
| 132 void DelegatedFrameHostAndroid::UpdateBackgroundColor(SkColor color) { | |
| 133 background_layer_->SetBackgroundColor(color); | |
| 134 } | |
| 135 | |
| 136 void DelegatedFrameHostAndroid::UpdateSize( | |
| 137 const gfx::Size& desired_content_size) { | |
| 138 desired_content_size_ = desired_content_size; | |
| 139 background_layer_->SetBounds(desired_content_size); | |
| 140 UpdateBackgroundLayer(); | |
| 141 } | |
| 142 | |
| 143 cc::Layer* DelegatedFrameHostAndroid::GetContentLayer() const { | |
| 144 return content_layer_.get(); | |
| 145 } | |
| 146 | |
| 147 void DelegatedFrameHostAndroid::ReturnResources( | |
| 148 const cc::ReturnedResourceArray& resources) { | |
| 149 return_resources_callback_.Run(resources); | |
| 150 } | |
| 151 | |
| 152 void DelegatedFrameHostAndroid::SetBeginFrameSource( | |
| 153 cc::BeginFrameSource* begin_frame_source) { | |
| 154 // TODO(tansell): Hook this up. | |
| 155 } | |
| 156 | |
| 157 scoped_refptr<cc::SurfaceLayer> | |
| 158 DelegatedFrameHostAndroid::CreateSurfaceLayer() { | |
| 159 DCHECK(!surface_id_.is_null()); | |
| 160 | |
| 161 // manager must outlive compositors using it. | |
| 162 scoped_refptr<cc::SurfaceLayer> surface_layer = cc::SurfaceLayer::Create( | |
| 163 base::Bind(&SatisfyCallback, base::Unretained(surface_manager_)), | |
| 164 base::Bind(&RequireCallback, base::Unretained(surface_manager_))); | |
| 165 surface_layer->SetSurfaceId(surface_id_, 1.f, current_surface_size_); | |
|
David Trainor- moved to gerrit
2016/08/04 16:05:46
Is there a reason we don't set the new surface id
Khushal
2016/08/09 20:54:06
Good question. Doesn't look like the Surface Layer
Khushal
2016/08/12 01:49:48
So we can keep a single content layer around and j
| |
| 166 surface_layer->SetBounds(current_surface_size_); | |
| 167 surface_layer->SetIsDrawable(true); | |
| 168 surface_layer->SetContentsOpaque(true); | |
| 169 | |
| 170 return surface_layer; | |
| 171 } | |
| 172 | |
| 173 void DelegatedFrameHostAndroid::UpdateContentLayer( | |
| 174 scoped_refptr<cc::Layer> content_layer) { | |
| 175 if (content_layer) | |
| 176 content_layer->RemoveFromParent(); | |
| 177 | |
| 178 content_layer_ = std::move(content_layer); | |
| 179 | |
| 180 if (content_layer_) | |
| 181 view_->GetLayer()->AddChild(content_layer_); | |
| 182 | |
| 183 UpdateBackgroundLayer(); | |
| 184 } | |
| 185 | |
| 186 void DelegatedFrameHostAndroid::UpdateBackgroundLayer() { | |
| 187 // The background layer draws in 2 cases: | |
| 188 // 1) When we don't have any content from the renderer. | |
| 189 // 2) When the bounds of the content received from the renderer does not match | |
| 190 // the desired content bounds. | |
| 191 bool background_is_drawable = | |
| 192 content_layer_.get() == nullptr || | |
| 193 content_layer_->bounds() != desired_content_size_; | |
| 194 background_layer_->SetIsDrawable(background_is_drawable); | |
| 195 } | |
| 196 | |
| 197 } // namespace ui | |
| OLD | NEW |