| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "android_webview/browser/hardware_renderer.h" | 5 #include "android_webview/browser/hardware_renderer.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "android_webview/browser/aw_gl_surface.h" | 9 #include "android_webview/browser/aw_gl_surface.h" |
| 10 #include "android_webview/browser/aw_render_thread_context_provider.h" | 10 #include "android_webview/browser/aw_render_thread_context_provider.h" |
| 11 #include "android_webview/browser/child_frame.h" | 11 #include "android_webview/browser/child_frame.h" |
| 12 #include "android_webview/browser/deferred_gpu_command_service.h" | |
| 13 #include "android_webview/browser/parent_compositor_draw_constraints.h" | 12 #include "android_webview/browser/parent_compositor_draw_constraints.h" |
| 14 #include "android_webview/browser/parent_output_surface.h" | |
| 15 #include "android_webview/browser/render_thread_manager.h" | 13 #include "android_webview/browser/render_thread_manager.h" |
| 14 #include "android_webview/browser/surfaces_instance.h" |
| 16 #include "android_webview/public/browser/draw_gl.h" | 15 #include "android_webview/public/browser/draw_gl.h" |
| 17 #include "base/auto_reset.h" | |
| 18 #include "base/strings/string_number_conversions.h" | |
| 19 #include "base/trace_event/trace_event.h" | 16 #include "base/trace_event/trace_event.h" |
| 20 #include "cc/output/compositor_frame.h" | 17 #include "cc/output/compositor_frame.h" |
| 21 #include "cc/output/output_surface.h" | |
| 22 #include "cc/output/renderer_settings.h" | |
| 23 #include "cc/output/texture_mailbox_deleter.h" | |
| 24 #include "cc/quads/shared_quad_state.h" | |
| 25 #include "cc/quads/surface_draw_quad.h" | |
| 26 #include "cc/scheduler/begin_frame_source.h" | |
| 27 #include "cc/surfaces/display.h" | |
| 28 #include "cc/surfaces/display_scheduler.h" | |
| 29 #include "cc/surfaces/surface_factory.h" | 18 #include "cc/surfaces/surface_factory.h" |
| 30 #include "cc/surfaces/surface_id_allocator.h" | 19 #include "cc/surfaces/surface_id_allocator.h" |
| 31 #include "gpu/command_buffer/client/gl_in_process_context.h" | 20 #include "cc/surfaces/surface_manager.h" |
| 32 #include "gpu/command_buffer/common/gles2_cmd_utils.h" | |
| 33 #include "ui/gfx/geometry/rect_conversions.h" | |
| 34 #include "ui/gfx/geometry/rect_f.h" | |
| 35 #include "ui/gfx/transform.h" | 21 #include "ui/gfx/transform.h" |
| 36 #include "ui/gl/gl_bindings.h" | 22 #include "ui/gl/gl_bindings.h" |
| 37 | 23 |
| 38 namespace android_webview { | 24 namespace android_webview { |
| 39 | 25 |
| 40 HardwareRenderer::HardwareRenderer(RenderThreadManager* state) | 26 HardwareRenderer::HardwareRenderer(RenderThreadManager* state) |
| 41 : render_thread_manager_(state), | 27 : render_thread_manager_(state), |
| 42 last_egl_context_(eglGetCurrentContext()), | 28 last_egl_context_(eglGetCurrentContext()), |
| 43 gl_surface_(new AwGLSurface), | 29 surfaces_(SurfacesInstance::GetOrCreateInstance()), |
| 30 surface_id_allocator_(surfaces_->CreateSurfaceIdAllocator()), |
| 44 last_committed_output_surface_id_(0u), | 31 last_committed_output_surface_id_(0u), |
| 45 last_submitted_output_surface_id_(0u), | 32 last_submitted_output_surface_id_(0u) { |
| 46 output_surface_(NULL) { | |
| 47 DCHECK(last_egl_context_); | 33 DCHECK(last_egl_context_); |
| 48 | 34 surfaces_->GetSurfaceManager()->RegisterSurfaceFactoryClient( |
| 49 cc::RendererSettings settings; | |
| 50 | |
| 51 // Should be kept in sync with compositor_impl_android.cc. | |
| 52 settings.allow_antialiasing = false; | |
| 53 settings.highp_threshold_min = 2048; | |
| 54 | |
| 55 // Webview does not own the surface so should not clear it. | |
| 56 settings.should_clear_root_render_pass = false; | |
| 57 | |
| 58 surface_manager_.reset(new cc::SurfaceManager); | |
| 59 surface_id_allocator_.reset(new cc::SurfaceIdAllocator(1)); | |
| 60 surface_id_allocator_->RegisterSurfaceIdNamespace(surface_manager_.get()); | |
| 61 surface_manager_->RegisterSurfaceFactoryClient( | |
| 62 surface_id_allocator_->id_namespace(), this); | 35 surface_id_allocator_->id_namespace(), this); |
| 63 | |
| 64 std::unique_ptr<cc::BeginFrameSource> begin_frame_source( | |
| 65 new cc::StubBeginFrameSource); | |
| 66 std::unique_ptr<cc::TextureMailboxDeleter> texture_mailbox_deleter( | |
| 67 new cc::TextureMailboxDeleter(nullptr)); | |
| 68 std::unique_ptr<ParentOutputSurface> output_surface_holder( | |
| 69 new ParentOutputSurface(AwRenderThreadContextProvider::Create( | |
| 70 gl_surface_, DeferredGpuCommandService::GetInstance()))); | |
| 71 output_surface_ = output_surface_holder.get(); | |
| 72 std::unique_ptr<cc::DisplayScheduler> scheduler(new cc::DisplayScheduler( | |
| 73 begin_frame_source.get(), nullptr, | |
| 74 output_surface_holder->capabilities().max_frames_pending)); | |
| 75 display_.reset(new cc::Display( | |
| 76 surface_manager_.get(), nullptr /* shared_bitmap_manager */, | |
| 77 nullptr /* gpu_memory_buffer_manager */, settings, | |
| 78 surface_id_allocator_->id_namespace(), std::move(begin_frame_source), | |
| 79 std::move(output_surface_holder), std::move(scheduler), | |
| 80 std::move(texture_mailbox_deleter))); | |
| 81 display_->Initialize(this); | |
| 82 } | 36 } |
| 83 | 37 |
| 84 HardwareRenderer::~HardwareRenderer() { | 38 HardwareRenderer::~HardwareRenderer() { |
| 85 // Must reset everything before |surface_factory_| to ensure all | 39 // Must reset everything before |surface_factory_| to ensure all |
| 86 // resources are returned before resetting. | 40 // resources are returned before resetting. |
| 87 if (!root_id_.is_null()) | |
| 88 surface_factory_->Destroy(root_id_); | |
| 89 if (!child_id_.is_null()) | 41 if (!child_id_.is_null()) |
| 90 surface_factory_->Destroy(child_id_); | 42 DestroySurface(); |
| 91 display_.reset(); | |
| 92 surface_factory_.reset(); | 43 surface_factory_.reset(); |
| 93 surface_manager_->UnregisterSurfaceFactoryClient( | 44 surfaces_->GetSurfaceManager()->UnregisterSurfaceFactoryClient( |
| 94 surface_id_allocator_->id_namespace()); | 45 surface_id_allocator_->id_namespace()); |
| 95 | 46 |
| 96 // Reset draw constraints. | 47 // Reset draw constraints. |
| 97 render_thread_manager_->PostExternalDrawConstraintsToChildCompositorOnRT( | 48 render_thread_manager_->PostExternalDrawConstraintsToChildCompositorOnRT( |
| 98 ParentCompositorDrawConstraints()); | 49 ParentCompositorDrawConstraints()); |
| 99 ReturnResourcesInChildFrame(); | 50 ReturnResourcesInChildFrame(); |
| 100 } | 51 } |
| 101 | 52 |
| 102 void HardwareRenderer::CommitFrame() { | 53 void HardwareRenderer::CommitFrame() { |
| 103 TRACE_EVENT0("android_webview", "CommitFrame"); | 54 TRACE_EVENT0("android_webview", "CommitFrame"); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 127 if (last_egl_context_ != current_context) | 78 if (last_egl_context_ != current_context) |
| 128 DLOG(WARNING) << "EGLContextChanged"; | 79 DLOG(WARNING) << "EGLContextChanged"; |
| 129 | 80 |
| 130 // SurfaceFactory::SubmitCompositorFrame might call glFlush. So calling it | 81 // SurfaceFactory::SubmitCompositorFrame might call glFlush. So calling it |
| 131 // during "kModeSync" stage (which does not allow GL) might result in extra | 82 // during "kModeSync" stage (which does not allow GL) might result in extra |
| 132 // kModeProcess. Instead, submit the frame in "kModeDraw" stage to avoid | 83 // kModeProcess. Instead, submit the frame in "kModeDraw" stage to avoid |
| 133 // unnecessary kModeProcess. | 84 // unnecessary kModeProcess. |
| 134 if (child_frame_.get() && child_frame_->frame.get()) { | 85 if (child_frame_.get() && child_frame_->frame.get()) { |
| 135 if (!compositor_id_.Equals(child_frame_->compositor_id) || | 86 if (!compositor_id_.Equals(child_frame_->compositor_id) || |
| 136 last_submitted_output_surface_id_ != child_frame_->output_surface_id) { | 87 last_submitted_output_surface_id_ != child_frame_->output_surface_id) { |
| 137 if (!root_id_.is_null()) | |
| 138 surface_factory_->Destroy(root_id_); | |
| 139 if (!child_id_.is_null()) | 88 if (!child_id_.is_null()) |
| 140 surface_factory_->Destroy(child_id_); | 89 DestroySurface(); |
| 141 | |
| 142 root_id_ = cc::SurfaceId(); | |
| 143 child_id_ = cc::SurfaceId(); | |
| 144 | 90 |
| 145 // This will return all the resources to the previous compositor. | 91 // This will return all the resources to the previous compositor. |
| 146 surface_factory_.reset(); | 92 surface_factory_.reset(); |
| 147 compositor_id_ = child_frame_->compositor_id; | 93 compositor_id_ = child_frame_->compositor_id; |
| 148 last_submitted_output_surface_id_ = child_frame_->output_surface_id; | 94 last_submitted_output_surface_id_ = child_frame_->output_surface_id; |
| 149 surface_factory_.reset( | 95 surface_factory_.reset( |
| 150 new cc::SurfaceFactory(surface_manager_.get(), this)); | 96 new cc::SurfaceFactory(surfaces_->GetSurfaceManager(), this)); |
| 151 } | 97 } |
| 152 | 98 |
| 153 std::unique_ptr<cc::CompositorFrame> child_compositor_frame = | 99 std::unique_ptr<cc::CompositorFrame> child_compositor_frame = |
| 154 std::move(child_frame_->frame); | 100 std::move(child_frame_->frame); |
| 155 | 101 |
| 156 gfx::Size frame_size = | 102 gfx::Size frame_size = |
| 157 child_compositor_frame->delegated_frame_data->render_pass_list.back() | 103 child_compositor_frame->delegated_frame_data->render_pass_list.back() |
| 158 ->output_rect.size(); | 104 ->output_rect.size(); |
| 159 bool size_changed = frame_size != frame_size_; | 105 bool size_changed = frame_size != frame_size_; |
| 160 frame_size_ = frame_size; | 106 frame_size_ = frame_size; |
| 161 if (child_id_.is_null() || size_changed) { | 107 if (child_id_.is_null() || size_changed) { |
| 162 if (!child_id_.is_null()) | 108 if (!child_id_.is_null()) |
| 163 surface_factory_->Destroy(child_id_); | 109 DestroySurface(); |
| 164 child_id_ = surface_id_allocator_->GenerateId(); | 110 AllocateSurface(); |
| 165 surface_factory_->Create(child_id_); | |
| 166 } | 111 } |
| 167 | 112 |
| 168 surface_factory_->SubmitCompositorFrame(child_id_, | 113 surface_factory_->SubmitCompositorFrame(child_id_, |
| 169 std::move(*child_compositor_frame), | 114 std::move(*child_compositor_frame), |
| 170 cc::SurfaceFactory::DrawCallback()); | 115 cc::SurfaceFactory::DrawCallback()); |
| 171 } | 116 } |
| 172 | 117 |
| 173 gfx::Transform transform(gfx::Transform::kSkipInitialization); | 118 gfx::Transform transform(gfx::Transform::kSkipInitialization); |
| 174 transform.matrix().setColMajorf(draw_info->transform); | 119 transform.matrix().setColMajorf(draw_info->transform); |
| 175 transform.Translate(scroll_offset_.x(), scroll_offset_.y()); | 120 transform.Translate(scroll_offset_.x(), scroll_offset_.y()); |
| 176 | 121 |
| 177 gfx::Size viewport(draw_info->width, draw_info->height); | 122 gfx::Size viewport(draw_info->width, draw_info->height); |
| 178 // Need to post the new transform matrix back to child compositor | 123 // Need to post the new transform matrix back to child compositor |
| 179 // because there is no onDraw during a Render Thread animation, and child | 124 // because there is no onDraw during a Render Thread animation, and child |
| 180 // compositor might not have the tiles rasterized as the animation goes on. | 125 // compositor might not have the tiles rasterized as the animation goes on. |
| 181 ParentCompositorDrawConstraints draw_constraints( | 126 ParentCompositorDrawConstraints draw_constraints( |
| 182 draw_info->is_layer, transform, viewport.IsEmpty()); | 127 draw_info->is_layer, transform, viewport.IsEmpty()); |
| 183 if (!child_frame_.get() || draw_constraints.NeedUpdate(*child_frame_)) { | 128 if (!child_frame_.get() || draw_constraints.NeedUpdate(*child_frame_)) { |
| 184 render_thread_manager_->PostExternalDrawConstraintsToChildCompositorOnRT( | 129 render_thread_manager_->PostExternalDrawConstraintsToChildCompositorOnRT( |
| 185 draw_constraints); | 130 draw_constraints); |
| 186 } | 131 } |
| 187 | 132 |
| 188 if (child_id_.is_null()) | 133 if (child_id_.is_null()) |
| 189 return; | 134 return; |
| 190 | 135 |
| 191 gfx::Rect clip(draw_info->clip_left, draw_info->clip_top, | 136 gfx::Rect clip(draw_info->clip_left, draw_info->clip_top, |
| 192 draw_info->clip_right - draw_info->clip_left, | 137 draw_info->clip_right - draw_info->clip_left, |
| 193 draw_info->clip_bottom - draw_info->clip_top); | 138 draw_info->clip_bottom - draw_info->clip_top); |
| 139 surfaces_->DrawAndSwap(viewport, clip, transform, frame_size_, child_id_, |
| 140 gl_state); |
| 141 } |
| 194 | 142 |
| 195 // Create a frame with a single SurfaceDrawQuad referencing the child | 143 void HardwareRenderer::AllocateSurface() { |
| 196 // Surface and transformed using the given transform. | 144 DCHECK(child_id_.is_null()); |
| 197 std::unique_ptr<cc::RenderPass> render_pass = cc::RenderPass::Create(); | 145 DCHECK(surface_factory_); |
| 198 render_pass->SetAll(cc::RenderPassId(1, 1), gfx::Rect(viewport), clip, | 146 child_id_ = surface_id_allocator_->GenerateId(); |
| 199 gfx::Transform(), false); | 147 surface_factory_->Create(child_id_); |
| 148 surfaces_->AddChildId(child_id_); |
| 149 } |
| 200 | 150 |
| 201 cc::SharedQuadState* quad_state = | 151 void HardwareRenderer::DestroySurface() { |
| 202 render_pass->CreateAndAppendSharedQuadState(); | 152 DCHECK(!child_id_.is_null()); |
| 203 quad_state->quad_to_target_transform = transform; | 153 DCHECK(surface_factory_); |
| 204 quad_state->quad_layer_bounds = frame_size_; | 154 surfaces_->RemoveChildId(child_id_); |
| 205 quad_state->visible_quad_layer_rect = gfx::Rect(frame_size_); | 155 surface_factory_->Destroy(child_id_); |
| 206 quad_state->opacity = 1.f; | 156 child_id_ = cc::SurfaceId(); |
| 207 | |
| 208 cc::SurfaceDrawQuad* surface_quad = | |
| 209 render_pass->CreateAndAppendDrawQuad<cc::SurfaceDrawQuad>(); | |
| 210 surface_quad->SetNew(quad_state, gfx::Rect(quad_state->quad_layer_bounds), | |
| 211 gfx::Rect(quad_state->quad_layer_bounds), child_id_); | |
| 212 | |
| 213 std::unique_ptr<cc::DelegatedFrameData> delegated_frame( | |
| 214 new cc::DelegatedFrameData); | |
| 215 delegated_frame->render_pass_list.push_back(std::move(render_pass)); | |
| 216 cc::CompositorFrame frame; | |
| 217 frame.delegated_frame_data = std::move(delegated_frame); | |
| 218 | |
| 219 if (root_id_.is_null()) { | |
| 220 root_id_ = surface_id_allocator_->GenerateId(); | |
| 221 surface_factory_->Create(root_id_); | |
| 222 display_->SetSurfaceId(root_id_, 1.f); | |
| 223 } | |
| 224 surface_factory_->SubmitCompositorFrame(root_id_, std::move(frame), | |
| 225 cc::SurfaceFactory::DrawCallback()); | |
| 226 | |
| 227 display_->Resize(viewport); | |
| 228 | |
| 229 output_surface_->SetGLState(gl_state); | |
| 230 display_->SetExternalClip(clip); | |
| 231 display_->DrawAndSwap(); | |
| 232 } | 157 } |
| 233 | 158 |
| 234 void HardwareRenderer::ReturnResources( | 159 void HardwareRenderer::ReturnResources( |
| 235 const cc::ReturnedResourceArray& resources) { | 160 const cc::ReturnedResourceArray& resources) { |
| 236 ReturnResourcesToCompositor(resources, compositor_id_, | 161 ReturnResourcesToCompositor(resources, compositor_id_, |
| 237 last_submitted_output_surface_id_); | 162 last_submitted_output_surface_id_); |
| 238 } | 163 } |
| 239 | 164 |
| 240 void HardwareRenderer::SetBeginFrameSource( | 165 void HardwareRenderer::SetBeginFrameSource( |
| 241 cc::BeginFrameSource* begin_frame_source) { | 166 cc::BeginFrameSource* begin_frame_source) { |
| 242 // TODO(tansell): Hook this up. | 167 // TODO(tansell): Hook this up. |
| 243 } | 168 } |
| 244 | 169 |
| 245 void HardwareRenderer::SetBackingFrameBufferObject( | 170 void HardwareRenderer::SetBackingFrameBufferObject( |
| 246 int framebuffer_binding_ext) { | 171 int framebuffer_binding_ext) { |
| 247 gl_surface_->SetBackingFrameBufferObject(framebuffer_binding_ext); | 172 surfaces_->SetBackingFrameBufferObject(framebuffer_binding_ext); |
| 248 } | 173 } |
| 249 | 174 |
| 250 void HardwareRenderer::ReturnResourcesInChildFrame() { | 175 void HardwareRenderer::ReturnResourcesInChildFrame() { |
| 251 if (child_frame_.get() && child_frame_->frame.get()) { | 176 if (child_frame_.get() && child_frame_->frame.get()) { |
| 252 cc::ReturnedResourceArray resources_to_return; | 177 cc::ReturnedResourceArray resources_to_return; |
| 253 cc::TransferableResource::ReturnResources( | 178 cc::TransferableResource::ReturnResources( |
| 254 child_frame_->frame->delegated_frame_data->resource_list, | 179 child_frame_->frame->delegated_frame_data->resource_list, |
| 255 &resources_to_return); | 180 &resources_to_return); |
| 256 | 181 |
| 257 // The child frame's compositor id is not necessarily same as | 182 // The child frame's compositor id is not necessarily same as |
| 258 // compositor_id_. | 183 // compositor_id_. |
| 259 ReturnResourcesToCompositor(resources_to_return, | 184 ReturnResourcesToCompositor(resources_to_return, |
| 260 child_frame_->compositor_id, | 185 child_frame_->compositor_id, |
| 261 child_frame_->output_surface_id); | 186 child_frame_->output_surface_id); |
| 262 } | 187 } |
| 263 child_frame_.reset(); | 188 child_frame_.reset(); |
| 264 } | 189 } |
| 265 | 190 |
| 266 void HardwareRenderer::ReturnResourcesToCompositor( | 191 void HardwareRenderer::ReturnResourcesToCompositor( |
| 267 const cc::ReturnedResourceArray& resources, | 192 const cc::ReturnedResourceArray& resources, |
| 268 const CompositorID& compositor_id, | 193 const CompositorID& compositor_id, |
| 269 uint32_t output_surface_id) { | 194 uint32_t output_surface_id) { |
| 270 if (output_surface_id != last_committed_output_surface_id_) | 195 if (output_surface_id != last_committed_output_surface_id_) |
| 271 return; | 196 return; |
| 272 render_thread_manager_->InsertReturnedResourcesOnRT(resources, compositor_id, | 197 render_thread_manager_->InsertReturnedResourcesOnRT(resources, compositor_id, |
| 273 output_surface_id); | 198 output_surface_id); |
| 274 } | 199 } |
| 275 | 200 |
| 276 } // namespace android_webview | 201 } // namespace android_webview |
| OLD | NEW |