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/surface_layer.h" |
| 9 #include "cc/output/compositor_frame.h" |
| 10 #include "cc/surfaces/surface.h" |
| 11 #include "cc/surfaces/surface_id.h" |
| 12 #include "cc/surfaces/surface_id_allocator.h" |
| 13 #include "cc/surfaces/surface_manager.h" |
| 14 |
| 15 namespace content { |
| 16 |
| 17 namespace { |
| 18 |
| 19 void SatisfyCallback(cc::SurfaceManager* manager, |
| 20 const cc::SurfaceSequence& sequence) { |
| 21 std::vector<uint32_t> sequences; |
| 22 sequences.push_back(sequence.sequence); |
| 23 manager->DidSatisfySequences(sequence.id_namespace, &sequences); |
| 24 } |
| 25 |
| 26 void RequireCallback(cc::SurfaceManager* manager, |
| 27 const cc::SurfaceId& id, |
| 28 const cc::SurfaceSequence& sequence) { |
| 29 cc::Surface* surface = manager->GetSurfaceForId(id); |
| 30 if (!surface) { |
| 31 LOG(ERROR) << "Attempting to require callback on nonexistent surface"; |
| 32 return; |
| 33 } |
| 34 surface->AddDestructionDependency(sequence); |
| 35 } |
| 36 |
| 37 } // namespace |
| 38 |
| 39 DelegatedFrameHostAndroid::DelegatedFrameHostAndroid( |
| 40 cc::SurfaceManager* surface_manager, |
| 41 std::unique_ptr<cc::SurfaceIdAllocator> surface_id_allocator, |
| 42 DelegatedFrameHostAndroidClient* client) |
| 43 : surface_manager_(surface_manager), |
| 44 surface_id_allocator_(std::move(surface_id_allocator)), |
| 45 client_(client) {} |
| 46 |
| 47 DelegatedFrameHostAndroid::~DelegatedFrameHostAndroid() { |
| 48 DestroyDelegatedContent(); |
| 49 surface_factory_.reset(); |
| 50 } |
| 51 |
| 52 void DelegatedFrameHostAndroid::SubmitCompositorFrame( |
| 53 cc::CompositorFrame frame, |
| 54 cc::SurfaceFactory::DrawCallback draw_callback) { |
| 55 if (!surface_factory_) { |
| 56 surface_factory_ = |
| 57 base::WrapUnique(new cc::SurfaceFactory(surface_manager_, this)); |
| 58 } |
| 59 |
| 60 cc::RenderPass* root_pass = |
| 61 frame.delegated_frame_data->render_pass_list.back().get(); |
| 62 gfx::Size texture_size_in_layer = root_pass->output_rect.size(); |
| 63 |
| 64 if (surface_id_.is_null() || texture_size_in_layer != current_surface_size_ || |
| 65 location_bar_content_translation_ != |
| 66 frame.metadata.location_bar_content_translation || |
| 67 current_viewport_selection_ != frame.metadata.selection) { |
| 68 client_->DetachSurfaceLayer(); |
| 69 if (!surface_id_.is_null()) |
| 70 surface_factory_->Destroy(surface_id_); |
| 71 surface_id_ = surface_id_allocator_->GenerateId(); |
| 72 surface_factory_->Create(surface_id_); |
| 73 |
| 74 current_surface_size_ = texture_size_in_layer; |
| 75 location_bar_content_translation_ = |
| 76 frame.metadata.location_bar_content_translation; |
| 77 current_viewport_selection_ = frame.metadata.selection; |
| 78 client_->AttachSurfaceLayer(std::move(CreateSurfaceLayer())); |
| 79 } |
| 80 |
| 81 surface_factory_->SubmitCompositorFrame(surface_id_, std::move(frame), |
| 82 draw_callback); |
| 83 } |
| 84 |
| 85 bool DelegatedFrameHostAndroid::HasDelegatedContent() const { |
| 86 return !surface_id_.is_null(); |
| 87 } |
| 88 |
| 89 void DelegatedFrameHostAndroid::DestroyDelegatedContent() { |
| 90 if (!HasDelegatedContent()) |
| 91 return; |
| 92 |
| 93 DCHECK(surface_factory_.get()); |
| 94 |
| 95 client_->DetachSurfaceLayer(); |
| 96 surface_factory_->Destroy(surface_id_); |
| 97 surface_id_ = cc::SurfaceId(); |
| 98 } |
| 99 |
| 100 uint32_t DelegatedFrameHostAndroid::GetSurfaceIdNamespace() { |
| 101 if (surface_id_allocator_) |
| 102 return surface_id_allocator_->id_namespace(); |
| 103 return 0; |
| 104 } |
| 105 |
| 106 void DelegatedFrameHostAndroid::ReturnResources( |
| 107 const cc::ReturnedResourceArray& resources) { |
| 108 client_->ReturnCompositorFrameResources(resources); |
| 109 } |
| 110 |
| 111 void DelegatedFrameHostAndroid::SetBeginFrameSource( |
| 112 cc::BeginFrameSource* begin_frame_source) { |
| 113 // TODO(tansell): Hook this up. |
| 114 } |
| 115 |
| 116 scoped_refptr<cc::SurfaceLayer> |
| 117 DelegatedFrameHostAndroid::CreateSurfaceLayer() { |
| 118 DCHECK(!surface_id_.is_null()); |
| 119 |
| 120 // manager must outlive compositors using it. |
| 121 scoped_refptr<cc::SurfaceLayer> surface_layer = cc::SurfaceLayer::Create( |
| 122 base::Bind(&SatisfyCallback, base::Unretained(surface_manager_)), |
| 123 base::Bind(&RequireCallback, base::Unretained(surface_manager_))); |
| 124 surface_layer->SetSurfaceId(surface_id_, 1.f, current_surface_size_); |
| 125 surface_layer->SetBounds(current_surface_size_); |
| 126 surface_layer->SetIsDrawable(true); |
| 127 surface_layer->SetContentsOpaque(true); |
| 128 |
| 129 return surface_layer; |
| 130 } |
| 131 |
| 132 } // namespace content |
OLD | NEW |