| 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 "android_webview/browser/surfaces_instance.h" |
| 6 |
| 7 #include <algorithm> |
| 8 #include <utility> |
| 9 |
| 10 #include "android_webview/browser/aw_gl_surface.h" |
| 11 #include "android_webview/browser/aw_render_thread_context_provider.h" |
| 12 #include "android_webview/browser/deferred_gpu_command_service.h" |
| 13 #include "android_webview/browser/parent_output_surface.h" |
| 14 #include "base/memory/ptr_util.h" |
| 15 #include "cc/output/renderer_settings.h" |
| 16 #include "cc/output/texture_mailbox_deleter.h" |
| 17 #include "cc/quads/surface_draw_quad.h" |
| 18 #include "cc/scheduler/begin_frame_source.h" |
| 19 #include "cc/surfaces/display.h" |
| 20 #include "cc/surfaces/display_scheduler.h" |
| 21 #include "cc/surfaces/surface_factory.h" |
| 22 #include "cc/surfaces/surface_id_allocator.h" |
| 23 #include "cc/surfaces/surface_manager.h" |
| 24 #include "ui/gfx/geometry/rect.h" |
| 25 #include "ui/gfx/geometry/size.h" |
| 26 #include "ui/gfx/transform.h" |
| 27 |
| 28 namespace android_webview { |
| 29 |
| 30 namespace { |
| 31 SurfacesInstance* g_surfaces_instance = nullptr; |
| 32 } // namespace |
| 33 |
| 34 // static |
| 35 scoped_refptr<SurfacesInstance> SurfacesInstance::GetOrCreateInstance() { |
| 36 if (g_surfaces_instance) |
| 37 return make_scoped_refptr(g_surfaces_instance); |
| 38 return make_scoped_refptr(new SurfacesInstance); |
| 39 } |
| 40 |
| 41 SurfacesInstance::SurfacesInstance() |
| 42 : next_surface_id_namespace_(1u), |
| 43 gl_surface_(new AwGLSurface) { |
| 44 cc::RendererSettings settings; |
| 45 |
| 46 // Should be kept in sync with compositor_impl_android.cc. |
| 47 settings.allow_antialiasing = false; |
| 48 settings.highp_threshold_min = 2048; |
| 49 |
| 50 // Webview does not own the surface so should not clear it. |
| 51 settings.should_clear_root_render_pass = false; |
| 52 |
| 53 surface_manager_.reset(new cc::SurfaceManager); |
| 54 surface_id_allocator_.reset( |
| 55 new cc::SurfaceIdAllocator(next_surface_id_namespace_++)); |
| 56 surface_id_allocator_->RegisterSurfaceIdNamespace(surface_manager_.get()); |
| 57 |
| 58 std::unique_ptr<cc::BeginFrameSource> begin_frame_source( |
| 59 new cc::StubBeginFrameSource); |
| 60 std::unique_ptr<cc::TextureMailboxDeleter> texture_mailbox_deleter( |
| 61 new cc::TextureMailboxDeleter(nullptr)); |
| 62 std::unique_ptr<ParentOutputSurface> output_surface_holder( |
| 63 new ParentOutputSurface(AwRenderThreadContextProvider::Create( |
| 64 gl_surface_, DeferredGpuCommandService::GetInstance()))); |
| 65 output_surface_ = output_surface_holder.get(); |
| 66 std::unique_ptr<cc::DisplayScheduler> scheduler(new cc::DisplayScheduler( |
| 67 begin_frame_source.get(), nullptr, |
| 68 output_surface_holder->capabilities().max_frames_pending)); |
| 69 display_.reset(new cc::Display( |
| 70 surface_manager_.get(), nullptr /* shared_bitmap_manager */, |
| 71 nullptr /* gpu_memory_buffer_manager */, settings, |
| 72 surface_id_allocator_->id_namespace(), std::move(begin_frame_source), |
| 73 std::move(output_surface_holder), std::move(scheduler), |
| 74 std::move(texture_mailbox_deleter))); |
| 75 display_->Initialize(this); |
| 76 |
| 77 surface_factory_.reset(new cc::SurfaceFactory(surface_manager_.get(), this)); |
| 78 |
| 79 DCHECK(!g_surfaces_instance); |
| 80 g_surfaces_instance = this; |
| 81 |
| 82 } |
| 83 |
| 84 SurfacesInstance::~SurfacesInstance() { |
| 85 DCHECK_EQ(g_surfaces_instance, this); |
| 86 g_surfaces_instance = nullptr; |
| 87 |
| 88 DCHECK(child_ids_.empty()); |
| 89 if (!root_id_.is_null()) |
| 90 surface_factory_->Destroy(root_id_); |
| 91 } |
| 92 |
| 93 std::unique_ptr<cc::SurfaceIdAllocator> |
| 94 SurfacesInstance::CreateSurfaceIdAllocator() { |
| 95 std::unique_ptr<cc::SurfaceIdAllocator> allocator = base::WrapUnique( |
| 96 new cc::SurfaceIdAllocator(next_surface_id_namespace_++)); |
| 97 allocator->RegisterSurfaceIdNamespace(surface_manager_.get()); |
| 98 return allocator; |
| 99 } |
| 100 |
| 101 cc::SurfaceManager* SurfacesInstance::GetSurfaceManager() { |
| 102 return surface_manager_.get(); |
| 103 } |
| 104 |
| 105 void SurfacesInstance::SetBackingFrameBufferObject( |
| 106 int framebuffer_binding_ext) { |
| 107 gl_surface_->SetBackingFrameBufferObject(framebuffer_binding_ext); |
| 108 } |
| 109 |
| 110 void SurfacesInstance::DrawAndSwap(const gfx::Size& viewport, |
| 111 const gfx::Rect& clip, |
| 112 const gfx::Transform& transform, |
| 113 const gfx::Size& frame_size, |
| 114 const cc::SurfaceId& child_id, |
| 115 const ScopedAppGLStateRestore& gl_state) { |
| 116 DCHECK(std::find(child_ids_.begin(), child_ids_.end(), child_id) != |
| 117 child_ids_.end()); |
| 118 |
| 119 // Create a frame with a single SurfaceDrawQuad referencing the child |
| 120 // Surface and transformed using the given transform. |
| 121 std::unique_ptr<cc::RenderPass> render_pass = cc::RenderPass::Create(); |
| 122 render_pass->SetAll(cc::RenderPassId(1, 1), gfx::Rect(viewport), clip, |
| 123 gfx::Transform(), false); |
| 124 |
| 125 cc::SharedQuadState* quad_state = |
| 126 render_pass->CreateAndAppendSharedQuadState(); |
| 127 quad_state->quad_to_target_transform = transform; |
| 128 quad_state->quad_layer_bounds = frame_size; |
| 129 quad_state->visible_quad_layer_rect = gfx::Rect(frame_size); |
| 130 quad_state->opacity = 1.f; |
| 131 |
| 132 cc::SurfaceDrawQuad* surface_quad = |
| 133 render_pass->CreateAndAppendDrawQuad<cc::SurfaceDrawQuad>(); |
| 134 surface_quad->SetNew(quad_state, gfx::Rect(quad_state->quad_layer_bounds), |
| 135 gfx::Rect(quad_state->quad_layer_bounds), child_id); |
| 136 |
| 137 std::unique_ptr<cc::DelegatedFrameData> delegated_frame( |
| 138 new cc::DelegatedFrameData); |
| 139 delegated_frame->render_pass_list.push_back(std::move(render_pass)); |
| 140 cc::CompositorFrame frame; |
| 141 frame.delegated_frame_data = std::move(delegated_frame); |
| 142 frame.metadata.referenced_surfaces = child_ids_; |
| 143 |
| 144 if (root_id_.is_null()) { |
| 145 root_id_ = surface_id_allocator_->GenerateId(); |
| 146 surface_factory_->Create(root_id_); |
| 147 display_->SetSurfaceId(root_id_, 1.f); |
| 148 } |
| 149 surface_factory_->SubmitCompositorFrame(root_id_, std::move(frame), |
| 150 cc::SurfaceFactory::DrawCallback()); |
| 151 |
| 152 output_surface_->SetGLState(gl_state); |
| 153 display_->Resize(viewport); |
| 154 display_->SetExternalClip(clip); |
| 155 display_->DrawAndSwap(); |
| 156 } |
| 157 |
| 158 void SurfacesInstance::AddChildId(const cc::SurfaceId& child_id) { |
| 159 DCHECK(std::find(child_ids_.begin(), child_ids_.end(), child_id) == |
| 160 child_ids_.end()); |
| 161 child_ids_.push_back(child_id); |
| 162 if (!root_id_.is_null()) |
| 163 SetEmptyRootFrame(); |
| 164 } |
| 165 |
| 166 void SurfacesInstance::RemoveChildId(const cc::SurfaceId& child_id) { |
| 167 auto itr = std::find(child_ids_.begin(), child_ids_.end(), child_id); |
| 168 DCHECK(itr != child_ids_.end()); |
| 169 child_ids_.erase(itr); |
| 170 if (!root_id_.is_null()) |
| 171 SetEmptyRootFrame(); |
| 172 } |
| 173 |
| 174 void SurfacesInstance::SetEmptyRootFrame() { |
| 175 cc::CompositorFrame empty_frame; |
| 176 empty_frame.delegated_frame_data = |
| 177 base::WrapUnique(new cc::DelegatedFrameData); |
| 178 empty_frame.metadata.referenced_surfaces = child_ids_; |
| 179 surface_factory_->SubmitCompositorFrame(root_id_, std::move(empty_frame), |
| 180 cc::SurfaceFactory::DrawCallback()); |
| 181 } |
| 182 |
| 183 void SurfacesInstance::ReturnResources( |
| 184 const cc::ReturnedResourceArray& resources) { |
| 185 // Root surface should have no resources to return. |
| 186 CHECK(resources.empty()); |
| 187 } |
| 188 |
| 189 void SurfacesInstance::SetBeginFrameSource( |
| 190 cc::BeginFrameSource* begin_frame_source) { |
| 191 // Parent compsitor calls DrawAndSwap directly and doesn't use |
| 192 // BeginFrameSource. |
| 193 } |
| 194 |
| 195 } // namespace android_webview |
| OLD | NEW |