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" |
| 12 #include "base/auto_reset.h" |
10 #include "base/debug/trace_event.h" | 13 #include "base/debug/trace_event.h" |
11 #include "base/strings/string_number_conversions.h" | 14 #include "base/strings/string_number_conversions.h" |
12 #include "content/public/browser/android/synchronous_compositor.h" | 15 #include "cc/layers/delegated_frame_provider.h" |
13 #include "content/public/browser/browser_thread.h" | 16 #include "cc/layers/delegated_renderer_layer.h" |
14 #include "gpu/command_buffer/service/shader_translator_cache.h" | 17 #include "cc/layers/layer.h" |
| 18 #include "cc/output/compositor_frame.h" |
| 19 #include "cc/output/output_surface.h" |
| 20 #include "cc/trees/layer_tree_host.h" |
| 21 #include "cc/trees/layer_tree_settings.h" |
| 22 #include "gpu/command_buffer/client/gl_in_process_context.h" |
| 23 #include "ui/gfx/frame_time.h" |
15 #include "ui/gfx/geometry/rect_conversions.h" | 24 #include "ui/gfx/geometry/rect_conversions.h" |
16 #include "ui/gfx/geometry/rect_f.h" | 25 #include "ui/gfx/geometry/rect_f.h" |
17 #include "ui/gfx/transform.h" | 26 #include "ui/gfx/transform.h" |
18 #include "ui/gl/gl_bindings.h" | 27 #include "ui/gl/gl_bindings.h" |
| 28 #include "webkit/common/gpu/context_provider_in_process.h" |
| 29 #include "webkit/common/gpu/webgraphicscontext3d_in_process_command_buffer_impl.
h" |
19 | 30 |
20 namespace android_webview { | 31 namespace android_webview { |
21 | 32 |
| 33 namespace { |
| 34 |
| 35 using webkit::gpu::WebGraphicsContext3DInProcessCommandBufferImpl; |
| 36 |
| 37 scoped_refptr<cc::ContextProvider> CreateContext( |
| 38 scoped_refptr<gfx::GLSurface> surface, |
| 39 scoped_refptr<gpu::InProcessCommandBuffer::Service> service, |
| 40 gpu::GLInProcessContext* share_context) { |
| 41 const gfx::GpuPreference gpu_preference = gfx::PreferDiscreteGpu; |
| 42 |
| 43 blink::WebGraphicsContext3D::Attributes attributes; |
| 44 attributes.antialias = false; |
| 45 attributes.depth = false; |
| 46 attributes.stencil = false; |
| 47 attributes.shareResources = true; |
| 48 attributes.noAutomaticFlushes = true; |
| 49 gpu::GLInProcessContextAttribs in_process_attribs; |
| 50 WebGraphicsContext3DInProcessCommandBufferImpl::ConvertAttributes( |
| 51 attributes, &in_process_attribs); |
| 52 in_process_attribs.lose_context_when_out_of_memory = 1; |
| 53 |
| 54 scoped_ptr<gpu::GLInProcessContext> context( |
| 55 gpu::GLInProcessContext::Create(service, |
| 56 surface, |
| 57 surface->IsOffscreen(), |
| 58 gfx::kNullAcceleratedWidget, |
| 59 surface->GetSize(), |
| 60 share_context, |
| 61 false /* share_resources */, |
| 62 in_process_attribs, |
| 63 gpu_preference)); |
| 64 DCHECK(context.get()); |
| 65 |
| 66 return webkit::gpu::ContextProviderInProcess::Create( |
| 67 WebGraphicsContext3DInProcessCommandBufferImpl::WrapContext( |
| 68 context.Pass(), attributes), |
| 69 "Parent-Compositor"); |
| 70 } |
| 71 |
| 72 } // namespace |
| 73 |
22 HardwareRenderer::HardwareRenderer(SharedRendererState* state) | 74 HardwareRenderer::HardwareRenderer(SharedRendererState* state) |
23 : shared_renderer_state_(state), | 75 : shared_renderer_state_(state), |
24 last_egl_context_(eglGetCurrentContext()) { | 76 last_egl_context_(eglGetCurrentContext()), |
| 77 view_width_(-1), |
| 78 view_height_(-1), |
| 79 viewport_clip_valid_for_dcheck_(false), |
| 80 root_layer_(cc::Layer::Create()), |
| 81 output_surface_(NULL) { |
25 DCHECK(last_egl_context_); | 82 DCHECK(last_egl_context_); |
26 | 83 |
27 gl_surface_ = new AwGLSurface; | 84 gl_surface_ = new AwGLSurface; |
28 bool success = | 85 |
29 shared_renderer_state_->GetCompositor()-> | 86 cc::LayerTreeSettings settings; |
30 InitializeHwDraw(gl_surface_); | 87 |
31 DCHECK(success); | 88 // Should be kept in sync with compositor_impl_android.cc. |
| 89 settings.allow_antialiasing = false; |
| 90 settings.highp_threshold_min = 2048; |
| 91 |
| 92 // Webview does not own the surface so should not clear it. |
| 93 settings.should_clear_root_render_pass = false; |
| 94 |
| 95 layer_tree_host_ = |
| 96 cc::LayerTreeHost::CreateSingleThreaded(this, this, NULL, settings); |
| 97 layer_tree_host_->SetRootLayer(root_layer_); |
| 98 layer_tree_host_->SetLayerTreeHostClientReady(); |
32 } | 99 } |
33 | 100 |
34 HardwareRenderer::~HardwareRenderer() { | 101 HardwareRenderer::~HardwareRenderer() { |
35 shared_renderer_state_->GetCompositor()->ReleaseHwDraw(); | 102 // Must reset everything before |resource_collection_| to ensure all |
36 gl_surface_ = NULL; | 103 // resources are returned before resetting |resource_collection_| client. |
| 104 layer_tree_host_.reset(); |
| 105 root_layer_ = NULL; |
| 106 delegated_layer_ = NULL; |
| 107 frame_provider_ = NULL; |
| 108 if (resource_collection_.get()) { |
| 109 #if DCHECK_IS_ON |
| 110 // Check collection is empty. |
| 111 cc::ReturnedResourceArray returned_resources; |
| 112 resource_collection_->TakeUnusedResourcesForChildCompositor( |
| 113 &returned_resources); |
| 114 DCHECK_EQ(0u, returned_resources.size()); |
| 115 #endif // DCHECK_IS_ON |
| 116 |
| 117 resource_collection_->SetClient(NULL); |
| 118 } |
| 119 } |
| 120 |
| 121 void HardwareRenderer::DidBeginMainFrame() { |
| 122 // This is called after OutputSurface is created, but before the impl frame |
| 123 // starts. We set the draw constraints here. |
| 124 DCHECK(output_surface_); |
| 125 DCHECK(viewport_clip_valid_for_dcheck_); |
| 126 output_surface_->SetDrawConstraints(viewport_, clip_); |
37 } | 127 } |
38 | 128 |
39 bool HardwareRenderer::DrawGL(bool stencil_enabled, | 129 bool HardwareRenderer::DrawGL(bool stencil_enabled, |
40 int framebuffer_binding_ext, | 130 int framebuffer_binding_ext, |
41 AwDrawGLInfo* draw_info, | 131 AwDrawGLInfo* draw_info, |
42 DrawGLResult* result) { | 132 DrawGLResult* result) { |
43 TRACE_EVENT0("android_webview", "HardwareRenderer::DrawGL"); | 133 TRACE_EVENT0("android_webview", "HardwareRenderer::DrawGL"); |
44 | 134 |
45 // We need to watch if the current Android context has changed and enforce | 135 // We need to watch if the current Android context has changed and enforce |
46 // a clean-up in the compositor. | 136 // a clean-up in the compositor. |
47 EGLContext current_context = eglGetCurrentContext(); | 137 EGLContext current_context = eglGetCurrentContext(); |
48 if (!current_context) { | 138 if (!current_context) { |
49 DLOG(ERROR) << "DrawGL called without EGLContext"; | 139 DLOG(ERROR) << "DrawGL called without EGLContext"; |
50 return false; | 140 return false; |
51 } | 141 } |
52 | 142 |
53 // TODO(boliu): Handle context loss. | 143 // TODO(boliu): Handle context loss. |
54 if (last_egl_context_ != current_context) | 144 if (last_egl_context_ != current_context) |
55 DLOG(WARNING) << "EGLContextChanged"; | 145 DLOG(WARNING) << "EGLContextChanged"; |
56 | 146 |
57 if (draw_info->mode != AwDrawGLInfo::kModeDraw) | 147 scoped_ptr<DrawGLInput> input = shared_renderer_state_->PassDrawGLInput(); |
58 return false; | 148 if (!resource_collection_.get()) { |
| 149 resource_collection_ = new cc::DelegatedFrameResourceCollection; |
| 150 resource_collection_->SetClient(this); |
| 151 } |
59 | 152 |
60 // Should only need to access SharedRendererState in kModeDraw and kModeSync. | 153 if (input.get()) { |
61 const DrawGLInput input = shared_renderer_state_->GetDrawGLInput(); | 154 DCHECK(!input->frame.gl_frame_data); |
62 SetCompositorMemoryPolicy(); | 155 DCHECK(!input->frame.software_frame_data); |
| 156 |
| 157 bool size_changed = |
| 158 input->width != view_width_ || input->height != view_height_; |
| 159 view_width_ = input->width; |
| 160 view_height_ = input->height; |
| 161 scroll_offset_ = input->scroll_offset; |
| 162 |
| 163 if (!frame_provider_ || size_changed) { |
| 164 if (delegated_layer_) { |
| 165 delegated_layer_->RemoveFromParent(); |
| 166 } |
| 167 |
| 168 frame_provider_ = new cc::DelegatedFrameProvider( |
| 169 resource_collection_.get(), input->frame.delegated_frame_data.Pass()); |
| 170 |
| 171 delegated_layer_ = cc::DelegatedRendererLayer::Create(frame_provider_); |
| 172 delegated_layer_->SetBounds(gfx::Size(view_width_, view_height_)); |
| 173 delegated_layer_->SetIsDrawable(true); |
| 174 delegated_layer_->SetAnchorPoint(gfx::PointF(0.f, 0.f)); |
| 175 |
| 176 root_layer_->AddChild(delegated_layer_); |
| 177 } else { |
| 178 frame_provider_->SetFrameData(input->frame.delegated_frame_data.Pass()); |
| 179 } |
| 180 } |
| 181 |
| 182 viewport_.SetSize(draw_info->width, draw_info->height); |
| 183 layer_tree_host_->SetViewportSize(viewport_); |
| 184 clip_.SetRect(draw_info->clip_left, |
| 185 draw_info->clip_top, |
| 186 draw_info->clip_right - draw_info->clip_left, |
| 187 draw_info->clip_bottom - draw_info->clip_top); |
| 188 |
| 189 gfx::Transform transform(gfx::Transform::kSkipInitialization); |
| 190 transform.matrix().setColMajorf(draw_info->transform); |
| 191 transform.Translate(scroll_offset_.x(), scroll_offset_.y()); |
| 192 delegated_layer_->SetTransform(transform); |
63 | 193 |
64 gl_surface_->SetBackingFrameBufferObject(framebuffer_binding_ext); | 194 gl_surface_->SetBackingFrameBufferObject(framebuffer_binding_ext); |
65 | 195 { |
66 gfx::Transform transform; | 196 base::AutoReset<bool> frame_resetter(&viewport_clip_valid_for_dcheck_, |
67 transform.matrix().setColMajorf(draw_info->transform); | 197 true); |
68 transform.Translate(input.scroll_offset.x(), input.scroll_offset.y()); | 198 layer_tree_host_->SetNeedsRedrawRect(clip_); |
69 gfx::Rect clip_rect(draw_info->clip_left, | 199 layer_tree_host_->Composite(gfx::FrameTime::Now()); |
70 draw_info->clip_top, | |
71 draw_info->clip_right - draw_info->clip_left, | |
72 draw_info->clip_bottom - draw_info->clip_top); | |
73 | |
74 gfx::Rect viewport(draw_info->width, draw_info->height); | |
75 if (!draw_info->is_layer) { | |
76 gfx::RectF view_rect(input.width, input.height); | |
77 transform.TransformRect(&view_rect); | |
78 viewport.Intersect(gfx::ToEnclosingRect(view_rect)); | |
79 } | 200 } |
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(); | 201 gl_surface_->ResetBackingFrameBufferObject(); |
88 | 202 |
89 if (did_draw) { | 203 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 } | 204 } |
96 | 205 |
97 void HardwareRenderer::SetCompositorMemoryPolicy() { | 206 scoped_ptr<cc::OutputSurface> HardwareRenderer::CreateOutputSurface( |
98 if (shared_renderer_state_->IsMemoryPolicyDirty()) { | 207 bool fallback) { |
99 content::SynchronousCompositorMemoryPolicy policy = | 208 // Android webview does not support losing output surface. |
100 shared_renderer_state_->GetMemoryPolicy(); | 209 DCHECK(!fallback); |
101 // Memory policy is set by BrowserViewRenderer on UI thread. | 210 scoped_refptr<cc::ContextProvider> context_provider = |
102 shared_renderer_state_->GetCompositor()->SetMemoryPolicy(policy); | 211 CreateContext(gl_surface_, |
103 shared_renderer_state_->SetMemoryPolicyDirty(false); | 212 DeferredGpuCommandService::GetInstance(), |
104 } | 213 shared_renderer_state_->GetSharedContext()); |
| 214 scoped_ptr<ParentOutputSurface> output_surface_holder( |
| 215 new ParentOutputSurface(context_provider)); |
| 216 output_surface_ = output_surface_holder.get(); |
| 217 return output_surface_holder.PassAs<cc::OutputSurface>(); |
| 218 } |
| 219 |
| 220 void HardwareRenderer::UnusedResourcesAreAvailable() { |
| 221 cc::ReturnedResourceArray returned_resources; |
| 222 resource_collection_->TakeUnusedResourcesForChildCompositor( |
| 223 &returned_resources); |
| 224 shared_renderer_state_->InsertReturnedResources(returned_resources); |
105 } | 225 } |
106 | 226 |
107 } // namespace android_webview | 227 } // namespace android_webview |
OLD | NEW |