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/child_frame.h"
9 #include "android_webview/browser/deferred_gpu_command_service.h"
10 #include "android_webview/browser/parent_compositor_draw_constraints.h"
11 #include "android_webview/browser/parent_output_surface.h"
12 #include "android_webview/browser/shared_renderer_state.h"
13 #include "android_webview/public/browser/draw_gl.h"
14 #include "base/auto_reset.h"
15 #include "base/strings/string_number_conversions.h"
16 #include "base/trace_event/trace_event.h"
17 #include "cc/layers/delegated_frame_provider.h"
18 #include "cc/layers/delegated_renderer_layer.h"
19 #include "cc/layers/layer.h"
20 #include "cc/output/compositor_frame.h"
21 #include "cc/output/output_surface.h"
22 #include "cc/scheduler/begin_frame_source.h"
23 #include "cc/trees/layer_tree_host.h"
24 #include "cc/trees/layer_tree_settings.h"
25 #include "gpu/blink/webgraphicscontext3d_in_process_command_buffer_impl.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"
33 #include "webkit/common/gpu/context_provider_in_process.h"
35 namespace android_webview
{
39 using gpu_blink::WebGraphicsContext3DImpl
;
40 using gpu_blink::WebGraphicsContext3DInProcessCommandBufferImpl
;
42 scoped_refptr
<cc::ContextProvider
> CreateContext(
43 scoped_refptr
<gfx::GLSurface
> surface
,
44 scoped_refptr
<gpu::InProcessCommandBuffer::Service
> service
) {
45 const gfx::GpuPreference gpu_preference
= gfx::PreferDiscreteGpu
;
47 blink::WebGraphicsContext3D::Attributes attributes
;
48 attributes
.antialias
= false;
49 attributes
.depth
= false;
50 attributes
.stencil
= false;
51 attributes
.shareResources
= true;
52 attributes
.noAutomaticFlushes
= true;
53 gpu::gles2::ContextCreationAttribHelper attribs_for_gles2
;
54 WebGraphicsContext3DImpl::ConvertAttributes(
55 attributes
, &attribs_for_gles2
);
56 attribs_for_gles2
.lose_context_when_out_of_memory
= true;
58 scoped_ptr
<gpu::GLInProcessContext
> context(gpu::GLInProcessContext::Create(
61 surface
->IsOffscreen(),
62 gfx::kNullAcceleratedWidget
,
64 NULL
/* share_context */,
65 false /* share_resources */,
68 gpu::GLInProcessContextSharedMemoryLimits(),
71 DCHECK(context
.get());
73 return webkit::gpu::ContextProviderInProcess::Create(
74 WebGraphicsContext3DInProcessCommandBufferImpl::WrapContext(
75 context
.Pass(), attributes
),
81 HardwareRenderer::HardwareRenderer(SharedRendererState
* state
)
82 : shared_renderer_state_(state
),
83 last_egl_context_(eglGetCurrentContext()),
84 stencil_enabled_(false),
85 viewport_clip_valid_for_dcheck_(false),
86 gl_surface_(new AwGLSurface
),
87 root_layer_(cc::Layer::Create()),
88 resource_collection_(new cc::DelegatedFrameResourceCollection
),
89 output_surface_(NULL
) {
90 DCHECK(last_egl_context_
);
92 resource_collection_
->SetClient(this);
94 cc::LayerTreeSettings settings
;
96 // Should be kept in sync with compositor_impl_android.cc.
97 settings
.renderer_settings
.allow_antialiasing
= false;
98 settings
.renderer_settings
.highp_threshold_min
= 2048;
100 // Webview does not own the surface so should not clear it.
101 settings
.renderer_settings
.should_clear_root_render_pass
= false;
103 // TODO(enne): Update this this compositor to use a synchronous scheduler.
104 settings
.single_thread_proxy_scheduler
= false;
106 layer_tree_host_
= cc::LayerTreeHost::CreateSingleThreaded(
107 this, this, nullptr, nullptr, nullptr, settings
, nullptr, nullptr);
108 layer_tree_host_
->SetRootLayer(root_layer_
);
109 layer_tree_host_
->SetLayerTreeHostClientReady();
110 layer_tree_host_
->set_has_transparent_background(true);
113 HardwareRenderer::~HardwareRenderer() {
114 // Must reset everything before |resource_collection_| to ensure all
115 // resources are returned before resetting |resource_collection_| client.
116 layer_tree_host_
.reset();
118 delegated_layer_
= NULL
;
119 frame_provider_
= NULL
;
121 // Check collection is empty.
122 cc::ReturnedResourceArray returned_resources
;
123 resource_collection_
->TakeUnusedResourcesForChildCompositor(
124 &returned_resources
);
125 DCHECK_EQ(0u, returned_resources
.size());
126 #endif // DCHECK_IS_ON()
128 resource_collection_
->SetClient(NULL
);
130 // Reset draw constraints.
131 shared_renderer_state_
->PostExternalDrawConstraintsToChildCompositorOnRT(
132 ParentCompositorDrawConstraints());
135 void HardwareRenderer::DidBeginMainFrame() {
136 // This is called after OutputSurface is created, but before the impl frame
137 // starts. We set the draw constraints here.
138 DCHECK(output_surface_
);
139 DCHECK(viewport_clip_valid_for_dcheck_
);
140 output_surface_
->SetExternalStencilTest(stencil_enabled_
);
141 output_surface_
->SetDrawConstraints(viewport_
, clip_
);
144 void HardwareRenderer::CommitFrame() {
145 TRACE_EVENT0("android_webview", "CommitFrame");
146 scroll_offset_
= shared_renderer_state_
->GetScrollOffsetOnRT();
148 scoped_ptr
<ChildFrame
> child_frame
=
149 shared_renderer_state_
->PassCompositorFrameOnRT();
150 if (!child_frame
.get())
152 child_frame_
= child_frame
.Pass();
155 scoped_ptr
<cc::CompositorFrame
> frame
= child_frame_
->frame
.Pass();
157 DCHECK(!frame
->gl_frame_data
);
158 DCHECK(!frame
->software_frame_data
);
160 // DelegatedRendererLayerImpl applies the inverse device_scale_factor of the
161 // renderer frame, assuming that the browser compositor will scale
162 // it back up to device scale. But on Android we put our browser layers in
163 // physical pixels and set our browser CC device_scale_factor to 1, so this
164 // suppresses the transform.
165 frame
->delegated_frame_data
->device_scale_factor
= 1.0f
;
167 gfx::Size frame_size
=
168 frame
->delegated_frame_data
->render_pass_list
.back()->output_rect
.size();
169 bool size_changed
= frame_size
!= frame_size_
;
170 frame_size_
= frame_size
;
172 if (!frame_provider_
.get() || size_changed
) {
173 if (delegated_layer_
.get()) {
174 delegated_layer_
->RemoveFromParent();
177 frame_provider_
= new cc::DelegatedFrameProvider(
178 resource_collection_
.get(), frame
->delegated_frame_data
.Pass());
180 delegated_layer_
= cc::DelegatedRendererLayer::Create(frame_provider_
);
181 delegated_layer_
->SetBounds(frame_size_
);
182 delegated_layer_
->SetIsDrawable(true);
184 root_layer_
->AddChild(delegated_layer_
);
186 frame_provider_
->SetFrameData(frame
->delegated_frame_data
.Pass());
190 void HardwareRenderer::DrawGL(bool stencil_enabled
,
191 int framebuffer_binding_ext
,
192 AwDrawGLInfo
* draw_info
) {
193 TRACE_EVENT0("android_webview", "HardwareRenderer::DrawGL");
195 // We need to watch if the current Android context has changed and enforce
196 // a clean-up in the compositor.
197 EGLContext current_context
= eglGetCurrentContext();
198 DCHECK(current_context
) << "DrawGL called without EGLContext";
200 // TODO(boliu): Handle context loss.
201 if (last_egl_context_
!= current_context
)
202 DLOG(WARNING
) << "EGLContextChanged";
204 gfx::Transform
transform(gfx::Transform::kSkipInitialization
);
205 transform
.matrix().setColMajorf(draw_info
->transform
);
206 transform
.Translate(scroll_offset_
.x(), scroll_offset_
.y());
208 viewport_
.SetSize(draw_info
->width
, draw_info
->height
);
209 // Need to post the new transform matrix back to child compositor
210 // because there is no onDraw during a Render Thread animation, and child
211 // compositor might not have the tiles rasterized as the animation goes on.
212 ParentCompositorDrawConstraints
draw_constraints(
213 draw_info
->is_layer
, transform
, gfx::Rect(viewport_
));
214 if (!child_frame_
.get() || draw_constraints
.NeedUpdate(*child_frame_
)) {
215 shared_renderer_state_
->PostExternalDrawConstraintsToChildCompositorOnRT(
219 if (!delegated_layer_
.get())
222 layer_tree_host_
->SetViewportSize(viewport_
);
223 clip_
.SetRect(draw_info
->clip_left
,
225 draw_info
->clip_right
- draw_info
->clip_left
,
226 draw_info
->clip_bottom
- draw_info
->clip_top
);
227 stencil_enabled_
= stencil_enabled
;
229 delegated_layer_
->SetTransform(transform
);
231 gl_surface_
->SetBackingFrameBufferObject(framebuffer_binding_ext
);
233 base::AutoReset
<bool> frame_resetter(&viewport_clip_valid_for_dcheck_
,
235 layer_tree_host_
->SetNeedsRedrawRect(clip_
);
236 layer_tree_host_
->Composite(gfx::FrameTime::Now());
238 gl_surface_
->ResetBackingFrameBufferObject();
241 void HardwareRenderer::RequestNewOutputSurface() {
242 scoped_refptr
<cc::ContextProvider
> context_provider
=
243 CreateContext(gl_surface_
,
244 DeferredGpuCommandService::GetInstance());
245 scoped_ptr
<ParentOutputSurface
> output_surface_holder(
246 new ParentOutputSurface(context_provider
));
247 output_surface_
= output_surface_holder
.get();
248 layer_tree_host_
->SetOutputSurface(output_surface_holder
.Pass());
251 void HardwareRenderer::DidFailToInitializeOutputSurface() {
252 RequestNewOutputSurface();
255 void HardwareRenderer::UnusedResourcesAreAvailable() {
256 cc::ReturnedResourceArray returned_resources
;
257 resource_collection_
->TakeUnusedResourcesForChildCompositor(
258 &returned_resources
);
259 shared_renderer_state_
->InsertReturnedResourcesOnRT(returned_resources
);
262 } // namespace android_webview