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
3 // found in the LICENSE file.
5 #include "android_webview/browser/hardware_renderer.h"
7 #include "android_webview/browser/aw_gl_surface.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"
11 #include "android_webview/public/browser/draw_gl.h"
12 #include "base/auto_reset.h"
13 #include "base/debug/trace_event.h"
14 #include "base/strings/string_number_conversions.h"
15 #include "cc/layers/delegated_frame_provider.h"
16 #include "cc/layers/delegated_renderer_layer.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 "gpu/command_buffer/common/gles2_cmd_utils.h"
24 #include "ui/gfx/frame_time.h"
25 #include "ui/gfx/geometry/rect_conversions.h"
26 #include "ui/gfx/geometry/rect_f.h"
27 #include "ui/gfx/transform.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"
32 namespace android_webview {
36 using webkit::gpu::WebGraphicsContext3DInProcessCommandBufferImpl;
37 using webkit::gpu::WebGraphicsContext3DImpl;
39 scoped_refptr<cc::ContextProvider> CreateContext(
40 scoped_refptr<gfx::GLSurface> surface,
41 scoped_refptr<gpu::InProcessCommandBuffer::Service> service) {
42 const gfx::GpuPreference gpu_preference = gfx::PreferDiscreteGpu;
44 blink::WebGraphicsContext3D::Attributes attributes;
45 attributes.antialias = false;
46 attributes.depth = false;
47 attributes.stencil = false;
48 attributes.shareResources = true;
49 attributes.noAutomaticFlushes = true;
50 gpu::gles2::ContextCreationAttribHelper attribs_for_gles2;
51 WebGraphicsContext3DImpl::ConvertAttributes(
52 attributes, &attribs_for_gles2);
53 attribs_for_gles2.lose_context_when_out_of_memory = true;
55 scoped_ptr<gpu::GLInProcessContext> context(gpu::GLInProcessContext::Create(
58 surface->IsOffscreen(),
59 gfx::kNullAcceleratedWidget,
61 NULL /* share_context */,
62 false /* share_resources */,
65 gpu::GLInProcessContextSharedMemoryLimits()));
66 DCHECK(context.get());
68 return webkit::gpu::ContextProviderInProcess::Create(
69 WebGraphicsContext3DInProcessCommandBufferImpl::WrapContext(
70 context.Pass(), attributes),
76 HardwareRenderer::HardwareRenderer(SharedRendererState* state)
77 : shared_renderer_state_(state),
78 last_egl_context_(eglGetCurrentContext()),
79 stencil_enabled_(false),
80 viewport_clip_valid_for_dcheck_(false),
81 gl_surface_(new AwGLSurface),
82 root_layer_(cc::Layer::Create()),
83 resource_collection_(new cc::DelegatedFrameResourceCollection),
84 output_surface_(NULL) {
85 DCHECK(last_egl_context_);
87 resource_collection_->SetClient(this);
89 cc::LayerTreeSettings settings;
91 // Should be kept in sync with compositor_impl_android.cc.
92 settings.allow_antialiasing = false;
93 settings.highp_threshold_min = 2048;
95 // Webview does not own the surface so should not clear it.
96 settings.should_clear_root_render_pass = false;
98 // TODO(enne): Update this this compositor to use a synchronous scheduler.
99 settings.single_thread_proxy_scheduler = false;
102 cc::LayerTreeHost::CreateSingleThreaded(this, this, NULL, settings, NULL);
103 layer_tree_host_->SetRootLayer(root_layer_);
104 layer_tree_host_->SetLayerTreeHostClientReady();
105 layer_tree_host_->set_has_transparent_background(true);
108 HardwareRenderer::~HardwareRenderer() {
111 // Must reset everything before |resource_collection_| to ensure all
112 // resources are returned before resetting |resource_collection_| client.
113 layer_tree_host_.reset();
115 delegated_layer_ = NULL;
116 frame_provider_ = NULL;
118 // Check collection is empty.
119 cc::ReturnedResourceArray returned_resources;
120 resource_collection_->TakeUnusedResourcesForChildCompositor(
121 &returned_resources);
122 DCHECK_EQ(0u, returned_resources.size());
123 #endif // DCHECK_IS_ON
125 resource_collection_->SetClient(NULL);
127 // Reset draw constraints.
128 shared_renderer_state_->UpdateDrawConstraints(
129 ParentCompositorDrawConstraints());
132 void HardwareRenderer::DidBeginMainFrame() {
133 // This is called after OutputSurface is created, but before the impl frame
134 // starts. We set the draw constraints here.
135 DCHECK(output_surface_);
136 DCHECK(viewport_clip_valid_for_dcheck_);
137 output_surface_->SetExternalStencilTest(stencil_enabled_);
138 output_surface_->SetDrawConstraints(viewport_, clip_);
141 void HardwareRenderer::CommitFrame() {
142 if (committed_input_.get()) {
143 TRACE_EVENT_INSTANT0("android_webview",
144 "EarlyOut_PreviousFrameUnconsumed",
145 TRACE_EVENT_SCOPE_THREAD);
149 committed_input_ = shared_renderer_state_->PassDrawGLInput();
150 // Happens with empty global visible rect.
151 if (!committed_input_.get())
154 DCHECK(!committed_input_->frame.gl_frame_data);
155 DCHECK(!committed_input_->frame.software_frame_data);
157 // DelegatedRendererLayerImpl applies the inverse device_scale_factor of the
158 // renderer frame, assuming that the browser compositor will scale
159 // it back up to device scale. But on Android we put our browser layers in
160 // physical pixels and set our browser CC device_scale_factor to 1, so this
161 // suppresses the transform.
162 committed_input_->frame.delegated_frame_data->device_scale_factor = 1.0f;
165 void HardwareRenderer::SetFrameData() {
166 if (!committed_input_.get())
169 scoped_ptr<DrawGLInput> input = committed_input_.Pass();
170 gfx::Size frame_size =
171 input->frame.delegated_frame_data->render_pass_list.back()
172 ->output_rect.size();
173 bool size_changed = frame_size != frame_size_;
174 frame_size_ = frame_size;
175 scroll_offset_ = input->scroll_offset;
177 if (!frame_provider_ || size_changed) {
178 if (delegated_layer_) {
179 delegated_layer_->RemoveFromParent();
182 frame_provider_ = new cc::DelegatedFrameProvider(
183 resource_collection_.get(), input->frame.delegated_frame_data.Pass());
185 delegated_layer_ = cc::DelegatedRendererLayer::Create(frame_provider_);
186 delegated_layer_->SetBounds(gfx::Size(input->width, input->height));
187 delegated_layer_->SetIsDrawable(true);
189 root_layer_->AddChild(delegated_layer_);
191 frame_provider_->SetFrameData(input->frame.delegated_frame_data.Pass());
195 void HardwareRenderer::DrawGL(bool stencil_enabled,
196 int framebuffer_binding_ext,
197 AwDrawGLInfo* draw_info) {
198 TRACE_EVENT0("android_webview", "HardwareRenderer::DrawGL");
200 // We need to watch if the current Android context has changed and enforce
201 // a clean-up in the compositor.
202 EGLContext current_context = eglGetCurrentContext();
203 DCHECK(current_context) << "DrawGL called without EGLContext";
205 // TODO(boliu): Handle context loss.
206 if (last_egl_context_ != current_context)
207 DLOG(WARNING) << "EGLContextChanged";
210 gfx::Transform transform(gfx::Transform::kSkipInitialization);
211 transform.matrix().setColMajorf(draw_info->transform);
212 transform.Translate(scroll_offset_.x(), scroll_offset_.y());
214 // Need to post the new transform matrix back to child compositor
215 // because there is no onDraw during a Render Thread animation, and child
216 // compositor might not have the tiles rasterized as the animation goes on.
217 ParentCompositorDrawConstraints draw_constraints(
218 draw_info->is_layer, transform, gfx::Rect(viewport_));
220 draw_constraints_ = draw_constraints;
221 shared_renderer_state_->PostExternalDrawConstraintsToChildCompositor(
224 if (!delegated_layer_.get())
227 viewport_.SetSize(draw_info->width, draw_info->height);
228 layer_tree_host_->SetViewportSize(viewport_);
229 clip_.SetRect(draw_info->clip_left,
231 draw_info->clip_right - draw_info->clip_left,
232 draw_info->clip_bottom - draw_info->clip_top);
233 stencil_enabled_ = stencil_enabled;
235 delegated_layer_->SetTransform(transform);
237 gl_surface_->SetBackingFrameBufferObject(framebuffer_binding_ext);
239 base::AutoReset<bool> frame_resetter(&viewport_clip_valid_for_dcheck_,
241 layer_tree_host_->SetNeedsRedrawRect(clip_);
242 layer_tree_host_->Composite(gfx::FrameTime::Now());
244 gl_surface_->ResetBackingFrameBufferObject();
247 void HardwareRenderer::RequestNewOutputSurface(bool fallback) {
248 // Android webview does not support losing output surface.
250 scoped_refptr<cc::ContextProvider> context_provider =
251 CreateContext(gl_surface_,
252 DeferredGpuCommandService::GetInstance());
253 scoped_ptr<ParentOutputSurface> output_surface_holder(
254 new ParentOutputSurface(context_provider));
255 output_surface_ = output_surface_holder.get();
256 layer_tree_host_->SetOutputSurface(
257 output_surface_holder.PassAs<cc::OutputSurface>());
260 void HardwareRenderer::UnusedResourcesAreAvailable() {
261 cc::ReturnedResourceArray returned_resources;
262 resource_collection_->TakeUnusedResourcesForChildCompositor(
263 &returned_resources);
264 shared_renderer_state_->InsertReturnedResources(returned_resources);
267 } // namespace android_webview