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/aw_render_thread_context_provider.h"
9 #include "android_webview/browser/child_frame.h"
10 #include "android_webview/browser/deferred_gpu_command_service.h"
11 #include "android_webview/browser/parent_compositor_draw_constraints.h"
12 #include "android_webview/browser/parent_output_surface.h"
13 #include "android_webview/browser/shared_renderer_state.h"
14 #include "android_webview/public/browser/draw_gl.h"
15 #include "base/auto_reset.h"
16 #include "base/strings/string_number_conversions.h"
17 #include "base/trace_event/trace_event.h"
18 #include "cc/layers/delegated_frame_provider.h"
19 #include "cc/layers/delegated_renderer_layer.h"
20 #include "cc/layers/layer.h"
21 #include "cc/output/compositor_frame.h"
22 #include "cc/output/output_surface.h"
23 #include "cc/scheduler/begin_frame_source.h"
24 #include "cc/trees/layer_tree_host.h"
25 #include "cc/trees/layer_tree_settings.h"
26 #include "gpu/command_buffer/client/gl_in_process_context.h"
27 #include "gpu/command_buffer/common/gles2_cmd_utils.h"
28 #include "ui/gfx/frame_time.h"
29 #include "ui/gfx/geometry/rect_conversions.h"
30 #include "ui/gfx/geometry/rect_f.h"
31 #include "ui/gfx/transform.h"
32 #include "ui/gl/gl_bindings.h"
34 namespace android_webview
{
36 HardwareRenderer::HardwareRenderer(SharedRendererState
* state
)
37 : shared_renderer_state_(state
),
38 last_egl_context_(eglGetCurrentContext()),
39 stencil_enabled_(false),
40 viewport_clip_valid_for_dcheck_(false),
41 gl_surface_(new AwGLSurface
),
42 root_layer_(cc::Layer::Create()),
43 resource_collection_(new cc::DelegatedFrameResourceCollection
),
44 output_surface_(NULL
) {
45 DCHECK(last_egl_context_
);
47 resource_collection_
->SetClient(this);
49 cc::LayerTreeSettings settings
;
51 // Should be kept in sync with compositor_impl_android.cc.
52 settings
.renderer_settings
.allow_antialiasing
= false;
53 settings
.renderer_settings
.highp_threshold_min
= 2048;
55 // Webview does not own the surface so should not clear it.
56 settings
.renderer_settings
.should_clear_root_render_pass
= false;
58 // TODO(enne): Update this this compositor to use a synchronous scheduler.
59 settings
.single_thread_proxy_scheduler
= false;
61 cc::LayerTreeHost::InitParams params
;
63 params
.settings
= &settings
;
64 layer_tree_host_
= cc::LayerTreeHost::CreateSingleThreaded(this, ¶ms
);
65 layer_tree_host_
->SetRootLayer(root_layer_
);
66 layer_tree_host_
->SetLayerTreeHostClientReady();
67 layer_tree_host_
->set_has_transparent_background(true);
70 HardwareRenderer::~HardwareRenderer() {
71 // Must reset everything before |resource_collection_| to ensure all
72 // resources are returned before resetting |resource_collection_| client.
73 layer_tree_host_
.reset();
75 delegated_layer_
= NULL
;
76 frame_provider_
= NULL
;
78 // Check collection is empty.
79 cc::ReturnedResourceArray returned_resources
;
80 resource_collection_
->TakeUnusedResourcesForChildCompositor(
82 DCHECK_EQ(0u, returned_resources
.size());
83 #endif // DCHECK_IS_ON()
85 resource_collection_
->SetClient(NULL
);
87 // Reset draw constraints.
88 shared_renderer_state_
->PostExternalDrawConstraintsToChildCompositorOnRT(
89 ParentCompositorDrawConstraints());
92 void HardwareRenderer::DidBeginMainFrame() {
93 // This is called after OutputSurface is created, but before the impl frame
94 // starts. We set the draw constraints here.
95 DCHECK(output_surface_
);
96 DCHECK(viewport_clip_valid_for_dcheck_
);
97 output_surface_
->SetExternalStencilTest(stencil_enabled_
);
98 output_surface_
->SetDrawConstraints(viewport_
, clip_
);
101 void HardwareRenderer::CommitFrame() {
102 TRACE_EVENT0("android_webview", "CommitFrame");
103 scroll_offset_
= shared_renderer_state_
->GetScrollOffsetOnRT();
105 scoped_ptr
<ChildFrame
> child_frame
=
106 shared_renderer_state_
->PassCompositorFrameOnRT();
107 if (!child_frame
.get())
109 child_frame_
= child_frame
.Pass();
112 scoped_ptr
<cc::CompositorFrame
> frame
= child_frame_
->frame
.Pass();
114 DCHECK(!frame
->gl_frame_data
);
115 DCHECK(!frame
->software_frame_data
);
117 // DelegatedRendererLayerImpl applies the inverse device_scale_factor of the
118 // renderer frame, assuming that the browser compositor will scale
119 // it back up to device scale. But on Android we put our browser layers in
120 // physical pixels and set our browser CC device_scale_factor to 1, so this
121 // suppresses the transform.
122 frame
->delegated_frame_data
->device_scale_factor
= 1.0f
;
124 gfx::Size frame_size
=
125 frame
->delegated_frame_data
->render_pass_list
.back()->output_rect
.size();
126 bool size_changed
= frame_size
!= frame_size_
;
127 frame_size_
= frame_size
;
129 if (!frame_provider_
.get() || size_changed
) {
130 if (delegated_layer_
.get()) {
131 delegated_layer_
->RemoveFromParent();
134 frame_provider_
= new cc::DelegatedFrameProvider(
135 resource_collection_
.get(), frame
->delegated_frame_data
.Pass());
137 delegated_layer_
= cc::DelegatedRendererLayer::Create(frame_provider_
);
138 delegated_layer_
->SetBounds(frame_size_
);
139 delegated_layer_
->SetIsDrawable(true);
141 root_layer_
->AddChild(delegated_layer_
);
143 frame_provider_
->SetFrameData(frame
->delegated_frame_data
.Pass());
147 void HardwareRenderer::DrawGL(bool stencil_enabled
,
148 int framebuffer_binding_ext
,
149 AwDrawGLInfo
* draw_info
) {
150 TRACE_EVENT0("android_webview", "HardwareRenderer::DrawGL");
152 // We need to watch if the current Android context has changed and enforce
153 // a clean-up in the compositor.
154 EGLContext current_context
= eglGetCurrentContext();
155 DCHECK(current_context
) << "DrawGL called without EGLContext";
157 // TODO(boliu): Handle context loss.
158 if (last_egl_context_
!= current_context
)
159 DLOG(WARNING
) << "EGLContextChanged";
161 gfx::Transform
transform(gfx::Transform::kSkipInitialization
);
162 transform
.matrix().setColMajorf(draw_info
->transform
);
163 transform
.Translate(scroll_offset_
.x(), scroll_offset_
.y());
165 viewport_
.SetSize(draw_info
->width
, draw_info
->height
);
166 // Need to post the new transform matrix back to child compositor
167 // because there is no onDraw during a Render Thread animation, and child
168 // compositor might not have the tiles rasterized as the animation goes on.
169 ParentCompositorDrawConstraints
draw_constraints(
170 draw_info
->is_layer
, transform
, gfx::Rect(viewport_
));
171 if (!child_frame_
.get() || draw_constraints
.NeedUpdate(*child_frame_
)) {
172 shared_renderer_state_
->PostExternalDrawConstraintsToChildCompositorOnRT(
176 if (!delegated_layer_
.get())
179 layer_tree_host_
->SetViewportSize(viewport_
);
180 clip_
.SetRect(draw_info
->clip_left
,
182 draw_info
->clip_right
- draw_info
->clip_left
,
183 draw_info
->clip_bottom
- draw_info
->clip_top
);
184 stencil_enabled_
= stencil_enabled
;
186 delegated_layer_
->SetTransform(transform
);
188 gl_surface_
->SetBackingFrameBufferObject(framebuffer_binding_ext
);
190 base::AutoReset
<bool> frame_resetter(&viewport_clip_valid_for_dcheck_
,
192 layer_tree_host_
->SetNeedsRedrawRect(clip_
);
193 layer_tree_host_
->Composite(gfx::FrameTime::Now());
195 gl_surface_
->ResetBackingFrameBufferObject();
198 void HardwareRenderer::RequestNewOutputSurface() {
199 scoped_refptr
<cc::ContextProvider
> context_provider
=
200 AwRenderThreadContextProvider::Create(
201 gl_surface_
, DeferredGpuCommandService::GetInstance());
202 scoped_ptr
<ParentOutputSurface
> output_surface_holder(
203 new ParentOutputSurface(context_provider
));
204 output_surface_
= output_surface_holder
.get();
205 layer_tree_host_
->SetOutputSurface(output_surface_holder
.Pass());
208 void HardwareRenderer::DidFailToInitializeOutputSurface() {
209 RequestNewOutputSurface();
212 void HardwareRenderer::UnusedResourcesAreAvailable() {
213 cc::ReturnedResourceArray returned_resources
;
214 resource_collection_
->TakeUnusedResourcesForChildCompositor(
215 &returned_resources
);
216 shared_renderer_state_
->InsertReturnedResourcesOnRT(returned_resources
);
219 } // namespace android_webview