| 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 // On Android we put our browser layers in physical pixels and set our | 102 // On Android we put our browser layers in physical pixels and set our |
| 157 // browser CC device_scale_factor to 1, so suppress the transform between | 103 // browser CC device_scale_factor to 1, so suppress the transform between |
| 158 // DIP and pixels. | 104 // DIP and pixels. |
| 159 child_compositor_frame->delegated_frame_data->device_scale_factor = 1.0f; | 105 child_compositor_frame->delegated_frame_data->device_scale_factor = 1.0f; |
| 160 | 106 |
| 161 gfx::Size frame_size = | 107 gfx::Size frame_size = |
| 162 child_compositor_frame->delegated_frame_data->render_pass_list.back() | 108 child_compositor_frame->delegated_frame_data->render_pass_list.back() |
| 163 ->output_rect.size(); | 109 ->output_rect.size(); |
| 164 bool size_changed = frame_size != frame_size_; | 110 bool size_changed = frame_size != frame_size_; |
| 165 frame_size_ = frame_size; | 111 frame_size_ = frame_size; |
| 166 if (child_id_.is_null() || size_changed) { | 112 if (child_id_.is_null() || size_changed) { |
| 167 if (!child_id_.is_null()) | 113 if (!child_id_.is_null()) |
| 168 surface_factory_->Destroy(child_id_); | 114 DestroySurface(); |
| 169 child_id_ = surface_id_allocator_->GenerateId(); | 115 AllocateSurface(); |
| 170 surface_factory_->Create(child_id_); | |
| 171 } | 116 } |
| 172 | 117 |
| 173 surface_factory_->SubmitCompositorFrame(child_id_, | 118 surface_factory_->SubmitCompositorFrame(child_id_, |
| 174 std::move(child_compositor_frame), | 119 std::move(child_compositor_frame), |
| 175 cc::SurfaceFactory::DrawCallback()); | 120 cc::SurfaceFactory::DrawCallback()); |
| 176 } | 121 } |
| 177 | 122 |
| 178 gfx::Transform transform(gfx::Transform::kSkipInitialization); | 123 gfx::Transform transform(gfx::Transform::kSkipInitialization); |
| 179 transform.matrix().setColMajorf(draw_info->transform); | 124 transform.matrix().setColMajorf(draw_info->transform); |
| 180 transform.Translate(scroll_offset_.x(), scroll_offset_.y()); | 125 transform.Translate(scroll_offset_.x(), scroll_offset_.y()); |
| 181 | 126 |
| 182 gfx::Size viewport(draw_info->width, draw_info->height); | 127 gfx::Size viewport(draw_info->width, draw_info->height); |
| 183 // Need to post the new transform matrix back to child compositor | 128 // Need to post the new transform matrix back to child compositor |
| 184 // because there is no onDraw during a Render Thread animation, and child | 129 // because there is no onDraw during a Render Thread animation, and child |
| 185 // compositor might not have the tiles rasterized as the animation goes on. | 130 // compositor might not have the tiles rasterized as the animation goes on. |
| 186 ParentCompositorDrawConstraints draw_constraints( | 131 ParentCompositorDrawConstraints draw_constraints( |
| 187 draw_info->is_layer, transform, viewport.IsEmpty()); | 132 draw_info->is_layer, transform, viewport.IsEmpty()); |
| 188 if (!child_frame_.get() || draw_constraints.NeedUpdate(*child_frame_)) { | 133 if (!child_frame_.get() || draw_constraints.NeedUpdate(*child_frame_)) { |
| 189 render_thread_manager_->PostExternalDrawConstraintsToChildCompositorOnRT( | 134 render_thread_manager_->PostExternalDrawConstraintsToChildCompositorOnRT( |
| 190 draw_constraints); | 135 draw_constraints); |
| 191 } | 136 } |
| 192 | 137 |
| 193 if (child_id_.is_null()) | 138 if (child_id_.is_null()) |
| 194 return; | 139 return; |
| 195 | 140 |
| 196 gfx::Rect clip(draw_info->clip_left, draw_info->clip_top, | 141 gfx::Rect clip(draw_info->clip_left, draw_info->clip_top, |
| 197 draw_info->clip_right - draw_info->clip_left, | 142 draw_info->clip_right - draw_info->clip_left, |
| 198 draw_info->clip_bottom - draw_info->clip_top); | 143 draw_info->clip_bottom - draw_info->clip_top); |
| 144 surfaces_->DrawAndSwap(viewport, clip, transform, frame_size_, child_id_, |
| 145 gl_state); |
| 146 } |
| 199 | 147 |
| 200 // Create a frame with a single SurfaceDrawQuad referencing the child | 148 void HardwareRenderer::AllocateSurface() { |
| 201 // Surface and transformed using the given transform. | 149 DCHECK(child_id_.is_null()); |
| 202 std::unique_ptr<cc::RenderPass> render_pass = cc::RenderPass::Create(); | 150 DCHECK(surface_factory_); |
| 203 render_pass->SetAll(cc::RenderPassId(1, 1), gfx::Rect(viewport), clip, | 151 child_id_ = surface_id_allocator_->GenerateId(); |
| 204 gfx::Transform(), false); | 152 surface_factory_->Create(child_id_); |
| 153 surfaces_->AddChildId(child_id_); |
| 154 } |
| 205 | 155 |
| 206 cc::SharedQuadState* quad_state = | 156 void HardwareRenderer::DestroySurface() { |
| 207 render_pass->CreateAndAppendSharedQuadState(); | 157 DCHECK(!child_id_.is_null()); |
| 208 quad_state->quad_to_target_transform = transform; | 158 DCHECK(surface_factory_); |
| 209 quad_state->quad_layer_bounds = frame_size_; | 159 surfaces_->RemoveChildId(child_id_); |
| 210 quad_state->visible_quad_layer_rect = gfx::Rect(frame_size_); | 160 surface_factory_->Destroy(child_id_); |
| 211 quad_state->opacity = 1.f; | 161 child_id_ = cc::SurfaceId(); |
| 212 | |
| 213 cc::SurfaceDrawQuad* surface_quad = | |
| 214 render_pass->CreateAndAppendDrawQuad<cc::SurfaceDrawQuad>(); | |
| 215 surface_quad->SetNew(quad_state, gfx::Rect(quad_state->quad_layer_bounds), | |
| 216 gfx::Rect(quad_state->quad_layer_bounds), child_id_); | |
| 217 | |
| 218 std::unique_ptr<cc::DelegatedFrameData> delegated_frame( | |
| 219 new cc::DelegatedFrameData); | |
| 220 delegated_frame->render_pass_list.push_back(std::move(render_pass)); | |
| 221 std::unique_ptr<cc::CompositorFrame> frame(new cc::CompositorFrame); | |
| 222 frame->delegated_frame_data = std::move(delegated_frame); | |
| 223 | |
| 224 if (root_id_.is_null()) { | |
| 225 root_id_ = surface_id_allocator_->GenerateId(); | |
| 226 surface_factory_->Create(root_id_); | |
| 227 display_->SetSurfaceId(root_id_, 1.f); | |
| 228 } | |
| 229 surface_factory_->SubmitCompositorFrame(root_id_, std::move(frame), | |
| 230 cc::SurfaceFactory::DrawCallback()); | |
| 231 | |
| 232 display_->Resize(viewport); | |
| 233 | |
| 234 output_surface_->SetGLState(gl_state); | |
| 235 display_->SetExternalClip(clip); | |
| 236 display_->DrawAndSwap(); | |
| 237 } | 162 } |
| 238 | 163 |
| 239 void HardwareRenderer::ReturnResources( | 164 void HardwareRenderer::ReturnResources( |
| 240 const cc::ReturnedResourceArray& resources) { | 165 const cc::ReturnedResourceArray& resources) { |
| 241 ReturnResourcesToCompositor(resources, compositor_id_, | 166 ReturnResourcesToCompositor(resources, compositor_id_, |
| 242 last_submitted_output_surface_id_); | 167 last_submitted_output_surface_id_); |
| 243 } | 168 } |
| 244 | 169 |
| 245 void HardwareRenderer::SetBeginFrameSource( | 170 void HardwareRenderer::SetBeginFrameSource( |
| 246 cc::BeginFrameSource* begin_frame_source) { | 171 cc::BeginFrameSource* begin_frame_source) { |
| 247 // TODO(tansell): Hook this up. | 172 // TODO(tansell): Hook this up. |
| 248 } | 173 } |
| 249 | 174 |
| 250 void HardwareRenderer::SetBackingFrameBufferObject( | 175 void HardwareRenderer::SetBackingFrameBufferObject( |
| 251 int framebuffer_binding_ext) { | 176 int framebuffer_binding_ext) { |
| 252 gl_surface_->SetBackingFrameBufferObject(framebuffer_binding_ext); | 177 surfaces_->SetBackingFrameBufferObject(framebuffer_binding_ext); |
| 253 } | 178 } |
| 254 | 179 |
| 255 void HardwareRenderer::ReturnResourcesInChildFrame() { | 180 void HardwareRenderer::ReturnResourcesInChildFrame() { |
| 256 if (child_frame_.get() && child_frame_->frame.get()) { | 181 if (child_frame_.get() && child_frame_->frame.get()) { |
| 257 cc::ReturnedResourceArray resources_to_return; | 182 cc::ReturnedResourceArray resources_to_return; |
| 258 cc::TransferableResource::ReturnResources( | 183 cc::TransferableResource::ReturnResources( |
| 259 child_frame_->frame->delegated_frame_data->resource_list, | 184 child_frame_->frame->delegated_frame_data->resource_list, |
| 260 &resources_to_return); | 185 &resources_to_return); |
| 261 | 186 |
| 262 // The child frame's compositor id is not necessarily same as | 187 // The child frame's compositor id is not necessarily same as |
| 263 // compositor_id_. | 188 // compositor_id_. |
| 264 ReturnResourcesToCompositor(resources_to_return, | 189 ReturnResourcesToCompositor(resources_to_return, |
| 265 child_frame_->compositor_id, | 190 child_frame_->compositor_id, |
| 266 child_frame_->output_surface_id); | 191 child_frame_->output_surface_id); |
| 267 } | 192 } |
| 268 child_frame_.reset(); | 193 child_frame_.reset(); |
| 269 } | 194 } |
| 270 | 195 |
| 271 void HardwareRenderer::ReturnResourcesToCompositor( | 196 void HardwareRenderer::ReturnResourcesToCompositor( |
| 272 const cc::ReturnedResourceArray& resources, | 197 const cc::ReturnedResourceArray& resources, |
| 273 const CompositorID& compositor_id, | 198 const CompositorID& compositor_id, |
| 274 uint32_t output_surface_id) { | 199 uint32_t output_surface_id) { |
| 275 if (output_surface_id != last_committed_output_surface_id_) | 200 if (output_surface_id != last_committed_output_surface_id_) |
| 276 return; | 201 return; |
| 277 render_thread_manager_->InsertReturnedResourcesOnRT(resources, compositor_id, | 202 render_thread_manager_->InsertReturnedResourcesOnRT(resources, compositor_id, |
| 278 output_surface_id); | 203 output_surface_id); |
| 279 } | 204 } |
| 280 | 205 |
| 281 } // namespace android_webview | 206 } // namespace android_webview |
| OLD | NEW |