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 "android_webview/browser/aw_gl_surface.h" | 7 #include "android_webview/browser/aw_gl_surface.h" |
8 #include "android_webview/browser/browser_view_renderer_client.h" | 8 #include "android_webview/browser/deferred_gpu_command_service.h" |
| 9 #include "android_webview/browser/parent_output_surface.h" |
| 10 #include "android_webview/browser/shared_renderer_state.h" |
9 #include "android_webview/public/browser/draw_gl.h" | 11 #include "android_webview/public/browser/draw_gl.h" |
10 #include "base/debug/trace_event.h" | 12 #include "base/debug/trace_event.h" |
11 #include "base/strings/string_number_conversions.h" | 13 #include "base/strings/string_number_conversions.h" |
| 14 #include "cc/output/compositor_frame.h" |
| 15 #include "cc/output/output_surface.h" |
| 16 #include "cc/layers/delegated_frame_provider.h" |
| 17 #include "cc/layers/delegated_renderer_layer.h" |
| 18 #include "cc/layers/layer.h" |
| 19 #include "cc/trees/layer_tree_host.h" |
| 20 #include "cc/trees/layer_tree_settings.h" |
12 #include "content/public/browser/android/synchronous_compositor.h" | 21 #include "content/public/browser/android/synchronous_compositor.h" |
13 #include "content/public/browser/browser_thread.h" | 22 #include "content/public/browser/browser_thread.h" |
14 #include "gpu/command_buffer/service/shader_translator_cache.h" | 23 #include "gpu/command_buffer/client/gl_in_process_context.h" |
| 24 #include "ui/gfx/frame_time.h" |
15 #include "ui/gfx/geometry/rect_conversions.h" | 25 #include "ui/gfx/geometry/rect_conversions.h" |
16 #include "ui/gfx/geometry/rect_f.h" | 26 #include "ui/gfx/geometry/rect_f.h" |
17 #include "ui/gfx/transform.h" | 27 #include "ui/gfx/transform.h" |
18 #include "ui/gl/gl_bindings.h" | 28 #include "ui/gl/gl_bindings.h" |
| 29 #include "webkit/common/gpu/context_provider_in_process.h" |
| 30 #include "webkit/common/gpu/webgraphicscontext3d_in_process_command_buffer_impl.
h" |
| 31 |
| 32 // TODO(boliu): Layering violation. |
| 33 #include "content/browser/android/in_process/synchronous_compositor_factory_impl
.h" |
19 | 34 |
20 namespace android_webview { | 35 namespace android_webview { |
21 | 36 |
22 HardwareRenderer::HardwareRenderer(SharedRendererState* state) | 37 HardwareRenderer::HardwareRenderer(SharedRendererState* state) |
23 : shared_renderer_state_(state), | 38 : shared_renderer_state_(state), |
24 last_egl_context_(eglGetCurrentContext()) { | 39 last_egl_context_(eglGetCurrentContext()), |
| 40 view_width_(-1), |
| 41 view_height_(-1), |
| 42 root_layer_(cc::Layer::Create()) { |
25 DCHECK(last_egl_context_); | 43 DCHECK(last_egl_context_); |
26 | 44 |
| 45 root_layer_->SetIsDrawable(false); |
| 46 |
27 gl_surface_ = new AwGLSurface; | 47 gl_surface_ = new AwGLSurface; |
28 bool success = | 48 |
29 shared_renderer_state_->GetCompositor()-> | 49 cc::LayerTreeSettings settings; |
30 InitializeHwDraw(gl_surface_); | 50 |
31 DCHECK(success); | 51 // Copied. |
| 52 settings.refresh_rate = 60.0; |
| 53 settings.impl_side_painting = false; |
| 54 settings.allow_antialiasing = false; |
| 55 settings.calculate_top_controls_position = false; |
| 56 settings.top_controls_height = 0.f; |
| 57 settings.highp_threshold_min = 2048; |
| 58 |
| 59 // Android WebView uses system scrollbars, so make ours invisible. |
| 60 settings.scrollbar_animator = cc::LayerTreeSettings::NoAnimator; |
| 61 settings.solid_color_scrollbar_color = SK_ColorTRANSPARENT; |
| 62 |
| 63 // Webview does not own the surface so should not clear it. |
| 64 settings.should_clear_root_render_pass = false; |
| 65 |
| 66 layer_tree_host_ = |
| 67 cc::LayerTreeHost::CreateSingleThreaded(this, this, NULL, settings); |
| 68 layer_tree_host_->SetRootLayer(root_layer_); |
| 69 layer_tree_host_->SetLayerTreeHostClientReady(); |
| 70 |
| 71 scoped_refptr<cc::ContextProvider> context_provider = |
| 72 content::SynchronousCompositor::GetFactory() |
| 73 ->CreateOnscreenContextProviderForCompositorThread(gl_surface_); |
| 74 output_surface_holder_.reset(new ParentOutputSurface(context_provider)); |
| 75 output_surface_ = output_surface_holder_->GetWeakPtr(); |
32 } | 76 } |
33 | 77 |
34 HardwareRenderer::~HardwareRenderer() { | 78 HardwareRenderer::~HardwareRenderer() { |
35 shared_renderer_state_->GetCompositor()->ReleaseHwDraw(); | 79 scoped_ptr<DrawGLInput> input = shared_renderer_state_->PassDrawGLInput(); |
36 gl_surface_ = NULL; | 80 if (input.get()) { |
| 81 shared_renderer_state_->ReturnResources( |
| 82 input->frame.delegated_frame_data->resource_list); |
| 83 } |
| 84 |
| 85 layer_tree_host_->SetRootLayer(NULL); |
| 86 layer_tree_host_.reset(); |
| 87 root_layer_->RemoveAllChildren(); |
| 88 |
| 89 layer_ = NULL; |
| 90 frame_provider_ = NULL; |
| 91 if (resource_collection_.get()) { |
| 92 resource_collection_->SetClient(NULL); |
| 93 // TODO(boliu): Check collection is empty. |
| 94 } |
| 95 resource_collection_ = NULL; |
37 } | 96 } |
38 | 97 |
39 bool HardwareRenderer::DrawGL(bool stencil_enabled, | 98 bool HardwareRenderer::DrawGL(bool stencil_enabled, |
40 int framebuffer_binding_ext, | 99 int framebuffer_binding_ext, |
41 AwDrawGLInfo* draw_info, | 100 AwDrawGLInfo* draw_info, |
42 DrawGLResult* result) { | 101 DrawGLResult* result) { |
43 TRACE_EVENT0("android_webview", "HardwareRenderer::DrawGL"); | 102 TRACE_EVENT0("android_webview", "HardwareRenderer::DrawGL"); |
44 | 103 |
45 // We need to watch if the current Android context has changed and enforce | 104 // We need to watch if the current Android context has changed and enforce |
46 // a clean-up in the compositor. | 105 // a clean-up in the compositor. |
47 EGLContext current_context = eglGetCurrentContext(); | 106 EGLContext current_context = eglGetCurrentContext(); |
48 if (!current_context) { | 107 if (!current_context) { |
49 DLOG(ERROR) << "DrawGL called without EGLContext"; | 108 DLOG(ERROR) << "DrawGL called without EGLContext"; |
50 return false; | 109 return false; |
51 } | 110 } |
52 | 111 |
53 // TODO(boliu): Handle context loss. | 112 // TODO(boliu): Handle context loss. |
54 if (last_egl_context_ != current_context) | 113 if (last_egl_context_ != current_context) |
55 DLOG(WARNING) << "EGLContextChanged"; | 114 DLOG(WARNING) << "EGLContextChanged"; |
56 | 115 |
57 if (draw_info->mode != AwDrawGLInfo::kModeDraw) | 116 if (draw_info->mode != AwDrawGLInfo::kModeDraw) |
58 return false; | 117 return false; |
59 | 118 |
60 // Should only need to access SharedRendererState in kModeDraw and kModeSync. | 119 scoped_ptr<DrawGLInput> input = shared_renderer_state_->PassDrawGLInput(); |
61 const DrawGLInput input = shared_renderer_state_->GetDrawGLInput(); | 120 if (!resource_collection_.get()) { |
62 SetCompositorMemoryPolicy(); | 121 resource_collection_ = new cc::DelegatedFrameResourceCollection; |
| 122 resource_collection_->SetClient(this); |
| 123 } |
63 | 124 |
64 gl_surface_->SetBackingFrameBufferObject(framebuffer_binding_ext); | 125 if (input.get()) { |
| 126 DCHECK(!input->frame.gl_frame_data); |
| 127 DCHECK(!input->frame.software_frame_data); |
65 | 128 |
66 gfx::Transform transform; | 129 bool size_changed = input->width != view_width_ || input->height != view_hei
ght_; |
67 transform.matrix().setColMajorf(draw_info->transform); | 130 view_width_ = input->width; |
68 transform.Translate(input.scroll_offset.x(), input.scroll_offset.y()); | 131 view_height_ = input->height; |
| 132 scroll_offset_ = input->scroll_offset; |
| 133 |
| 134 if (!frame_provider_ || size_changed) { |
| 135 if (layer_) { |
| 136 layer_->RemoveFromParent(); |
| 137 } |
| 138 |
| 139 frame_provider_ = new cc::DelegatedFrameProvider( |
| 140 resource_collection_.get(), input->frame.delegated_frame_data.Pass()); |
| 141 |
| 142 layer_ = cc::DelegatedRendererLayer::Create(frame_provider_); |
| 143 layer_->SetBounds(gfx::Size(view_width_, view_height_)); |
| 144 layer_->SetIsDrawable(true); |
| 145 layer_->SetAnchorPoint(gfx::PointF(0.f, 0.f)); |
| 146 |
| 147 root_layer_->AddChild(layer_); |
| 148 } else { |
| 149 frame_provider_->SetFrameData(input->frame.delegated_frame_data.Pass()); |
| 150 } |
| 151 } |
| 152 |
| 153 // TODO(boliu): Clip |
| 154 gfx::Size viewport(draw_info->width, draw_info->height); |
| 155 layer_tree_host_->SetViewportSize(viewport); |
69 gfx::Rect clip_rect(draw_info->clip_left, | 156 gfx::Rect clip_rect(draw_info->clip_left, |
70 draw_info->clip_top, | 157 draw_info->clip_top, |
71 draw_info->clip_right - draw_info->clip_left, | 158 draw_info->clip_right - draw_info->clip_left, |
72 draw_info->clip_bottom - draw_info->clip_top); | 159 draw_info->clip_bottom - draw_info->clip_top); |
| 160 if (output_surface_) { |
| 161 output_surface_->SetDrawConstraints(viewport, clip_rect); |
| 162 } |
| 163 gfx::Transform transform(gfx::Transform::kSkipInitialization); |
| 164 transform.matrix().setColMajorf(draw_info->transform); |
| 165 transform.Translate(scroll_offset_.x(), scroll_offset_.y()); |
| 166 layer_->SetTransform(transform); |
73 | 167 |
74 gfx::Rect viewport(draw_info->width, draw_info->height); | 168 gl_surface_->SetBackingFrameBufferObject(framebuffer_binding_ext); |
75 if (!draw_info->is_layer) { | 169 layer_tree_host_->Composite(gfx::FrameTime::Now()); |
76 gfx::RectF view_rect(input.width, input.height); | |
77 transform.TransformRect(&view_rect); | |
78 viewport.Intersect(gfx::ToEnclosingRect(view_rect)); | |
79 } | |
80 | |
81 bool did_draw = shared_renderer_state_->GetCompositor()->DemandDrawHw( | |
82 gfx::Size(draw_info->width, draw_info->height), | |
83 transform, | |
84 viewport, | |
85 clip_rect, | |
86 stencil_enabled); | |
87 gl_surface_->ResetBackingFrameBufferObject(); | 170 gl_surface_->ResetBackingFrameBufferObject(); |
88 | 171 |
89 if (did_draw) { | 172 return true; |
90 result->frame_id = input.frame_id; | |
91 result->clip_contains_visible_rect = | |
92 clip_rect.Contains(input.global_visible_rect); | |
93 } | |
94 return did_draw; | |
95 } | 173 } |
96 | 174 |
97 void HardwareRenderer::SetCompositorMemoryPolicy() { | 175 scoped_ptr<cc::OutputSurface> HardwareRenderer::CreateOutputSurface( |
98 if (shared_renderer_state_->IsMemoryPolicyDirty()) { | 176 bool fallback) { |
99 content::SynchronousCompositorMemoryPolicy policy = | 177 // Android webview does not lose output surface. |
100 shared_renderer_state_->GetMemoryPolicy(); | 178 DCHECK(!fallback); |
101 // Memory policy is set by BrowserViewRenderer on UI thread. | 179 DCHECK(output_surface_holder_.get()); |
102 shared_renderer_state_->GetCompositor()->SetMemoryPolicy(policy); | 180 return output_surface_holder_.PassAs<cc::OutputSurface>(); |
103 shared_renderer_state_->SetMemoryPolicyDirty(false); | 181 } |
104 } | 182 |
| 183 void HardwareRenderer::UnusedResourcesAreAvailable() { |
| 184 cc::ReturnedResourceArray returned_resources; |
| 185 resource_collection_->TakeUnusedResourcesForChildCompositor(&returned_resource
s); |
| 186 shared_renderer_state_->InsertReturnedResources(returned_resources); |
| 187 // TODO(boliu): Notify child? |
105 } | 188 } |
106 | 189 |
107 } // namespace android_webview | 190 } // namespace android_webview |
OLD | NEW |