Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / android_webview / browser / hardware_renderer.cc
blob28cec803ab7a589801d844c1e0c48f30689078d1
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 {
34 namespace {
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 gpu::GLInProcessContext* share_context) {
43 const gfx::GpuPreference gpu_preference = gfx::PreferDiscreteGpu;
45 blink::WebGraphicsContext3D::Attributes attributes;
46 attributes.antialias = false;
47 attributes.depth = false;
48 attributes.stencil = false;
49 attributes.shareResources = true;
50 attributes.noAutomaticFlushes = true;
51 gpu::gles2::ContextCreationAttribHelper attribs_for_gles2;
52 WebGraphicsContext3DImpl::ConvertAttributes(
53 attributes, &attribs_for_gles2);
54 attribs_for_gles2.lose_context_when_out_of_memory = true;
56 scoped_ptr<gpu::GLInProcessContext> context(gpu::GLInProcessContext::Create(
57 service,
58 surface,
59 surface->IsOffscreen(),
60 gfx::kNullAcceleratedWidget,
61 surface->GetSize(),
62 share_context,
63 false /* share_resources */,
64 attribs_for_gles2,
65 gpu_preference,
66 gpu::GLInProcessContextSharedMemoryLimits()));
67 DCHECK(context.get());
69 return webkit::gpu::ContextProviderInProcess::Create(
70 WebGraphicsContext3DInProcessCommandBufferImpl::WrapContext(
71 context.Pass(), attributes),
72 "Parent-Compositor");
75 } // namespace
77 HardwareRenderer::HardwareRenderer(SharedRendererState* state)
78 : shared_renderer_state_(state),
79 last_egl_context_(eglGetCurrentContext()),
80 stencil_enabled_(false),
81 viewport_clip_valid_for_dcheck_(false),
82 gl_surface_(new AwGLSurface),
83 root_layer_(cc::Layer::Create()),
84 resource_collection_(new cc::DelegatedFrameResourceCollection),
85 output_surface_(NULL) {
86 DCHECK(last_egl_context_);
88 resource_collection_->SetClient(this);
90 cc::LayerTreeSettings settings;
92 // Should be kept in sync with compositor_impl_android.cc.
93 settings.allow_antialiasing = false;
94 settings.highp_threshold_min = 2048;
96 // Webview does not own the surface so should not clear it.
97 settings.should_clear_root_render_pass = false;
99 // TODO(enne): Update this this compositor to use a synchronous scheduler.
100 settings.single_thread_proxy_scheduler = false;
102 layer_tree_host_ =
103 cc::LayerTreeHost::CreateSingleThreaded(this, this, NULL, settings, NULL);
104 layer_tree_host_->SetRootLayer(root_layer_);
105 layer_tree_host_->SetLayerTreeHostClientReady();
106 layer_tree_host_->set_has_transparent_background(true);
109 HardwareRenderer::~HardwareRenderer() {
110 // Must reset everything before |resource_collection_| to ensure all
111 // resources are returned before resetting |resource_collection_| client.
112 layer_tree_host_.reset();
113 root_layer_ = NULL;
114 delegated_layer_ = NULL;
115 frame_provider_ = NULL;
116 #if DCHECK_IS_ON
117 // Check collection is empty.
118 cc::ReturnedResourceArray returned_resources;
119 resource_collection_->TakeUnusedResourcesForChildCompositor(
120 &returned_resources);
121 DCHECK_EQ(0u, returned_resources.size());
122 #endif // DCHECK_IS_ON
124 resource_collection_->SetClient(NULL);
126 // Reset draw constraints.
127 shared_renderer_state_->UpdateDrawConstraints(
128 ParentCompositorDrawConstraints());
131 void HardwareRenderer::DidBeginMainFrame() {
132 // This is called after OutputSurface is created, but before the impl frame
133 // starts. We set the draw constraints here.
134 DCHECK(output_surface_);
135 DCHECK(viewport_clip_valid_for_dcheck_);
136 output_surface_->SetExternalStencilTest(stencil_enabled_);
137 output_surface_->SetDrawConstraints(viewport_, clip_);
140 void HardwareRenderer::CommitFrame() {
141 scoped_ptr<DrawGLInput> input = shared_renderer_state_->PassDrawGLInput();
142 if (!input.get()) {
143 DLOG(WARNING) << "No frame to commit";
144 return;
147 DCHECK(!input->frame.gl_frame_data);
148 DCHECK(!input->frame.software_frame_data);
150 // DelegatedRendererLayerImpl applies the inverse device_scale_factor of the
151 // renderer frame, assuming that the browser compositor will scale
152 // it back up to device scale. But on Android we put our browser layers in
153 // physical pixels and set our browser CC device_scale_factor to 1, so this
154 // suppresses the transform.
155 input->frame.delegated_frame_data->device_scale_factor = 1.0f;
157 gfx::Size frame_size =
158 input->frame.delegated_frame_data->render_pass_list.back()
159 ->output_rect.size();
160 bool size_changed = frame_size != frame_size_;
161 frame_size_ = frame_size;
162 scroll_offset_ = input->scroll_offset;
164 if (!frame_provider_ || size_changed) {
165 if (delegated_layer_) {
166 delegated_layer_->RemoveFromParent();
169 frame_provider_ = new cc::DelegatedFrameProvider(
170 resource_collection_.get(), input->frame.delegated_frame_data.Pass());
172 delegated_layer_ = cc::DelegatedRendererLayer::Create(frame_provider_);
173 delegated_layer_->SetBounds(gfx::Size(input->width, input->height));
174 delegated_layer_->SetIsDrawable(true);
176 root_layer_->AddChild(delegated_layer_);
177 } else {
178 frame_provider_->SetFrameData(input->frame.delegated_frame_data.Pass());
182 void HardwareRenderer::DrawGL(bool stencil_enabled,
183 int framebuffer_binding_ext,
184 AwDrawGLInfo* draw_info) {
185 TRACE_EVENT0("android_webview", "HardwareRenderer::DrawGL");
187 // We need to watch if the current Android context has changed and enforce
188 // a clean-up in the compositor.
189 EGLContext current_context = eglGetCurrentContext();
190 if (!current_context) {
191 DLOG(ERROR) << "DrawGL called without EGLContext";
192 return;
195 if (!delegated_layer_.get()) {
196 DLOG(ERROR) << "No frame committed";
197 return;
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 // Need to post the new transform matrix back to child compositor
209 // because there is no onDraw during a Render Thread animation, and child
210 // compositor might not have the tiles rasterized as the animation goes on.
211 ParentCompositorDrawConstraints draw_constraints(
212 draw_info->is_layer, transform, gfx::Rect(viewport_));
213 if (!draw_constraints_.Equals(draw_constraints)) {
214 draw_constraints_ = draw_constraints;
215 shared_renderer_state_->PostExternalDrawConstraintsToChildCompositor(
216 draw_constraints);
219 viewport_.SetSize(draw_info->width, draw_info->height);
220 layer_tree_host_->SetViewportSize(viewport_);
221 clip_.SetRect(draw_info->clip_left,
222 draw_info->clip_top,
223 draw_info->clip_right - draw_info->clip_left,
224 draw_info->clip_bottom - draw_info->clip_top);
225 stencil_enabled_ = stencil_enabled;
227 delegated_layer_->SetTransform(transform);
229 gl_surface_->SetBackingFrameBufferObject(framebuffer_binding_ext);
231 base::AutoReset<bool> frame_resetter(&viewport_clip_valid_for_dcheck_,
232 true);
233 layer_tree_host_->SetNeedsRedrawRect(clip_);
234 layer_tree_host_->Composite(gfx::FrameTime::Now());
236 gl_surface_->ResetBackingFrameBufferObject();
239 scoped_ptr<cc::OutputSurface> HardwareRenderer::CreateOutputSurface(
240 bool fallback) {
241 // Android webview does not support losing output surface.
242 DCHECK(!fallback);
243 scoped_refptr<cc::ContextProvider> context_provider =
244 CreateContext(gl_surface_,
245 DeferredGpuCommandService::GetInstance(),
246 shared_renderer_state_->GetSharedContext());
247 scoped_ptr<ParentOutputSurface> output_surface_holder(
248 new ParentOutputSurface(context_provider));
249 output_surface_ = output_surface_holder.get();
250 return output_surface_holder.PassAs<cc::OutputSurface>();
253 void HardwareRenderer::UnusedResourcesAreAvailable() {
254 cc::ReturnedResourceArray returned_resources;
255 resource_collection_->TakeUnusedResourcesForChildCompositor(
256 &returned_resources);
257 shared_renderer_state_->InsertReturnedResources(returned_resources);
260 } // namespace android_webview