[SyncFS] Build indexes from FileTracker entries on disk.
[chromium-blink-merge.git] / android_webview / browser / hardware_renderer.cc
blob4ed2859b1399038a5dd060058b0bfcd450158404
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 "ui/gfx/frame_time.h"
24 #include "ui/gfx/geometry/rect_conversions.h"
25 #include "ui/gfx/geometry/rect_f.h"
26 #include "ui/gfx/transform.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"
31 namespace android_webview {
33 namespace {
35 using webkit::gpu::WebGraphicsContext3DInProcessCommandBufferImpl;
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;
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;
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());
66 return webkit::gpu::ContextProviderInProcess::Create(
67 WebGraphicsContext3DInProcessCommandBufferImpl::WrapContext(
68 context.Pass(), attributes),
69 "Parent-Compositor");
72 } // namespace
74 HardwareRenderer::HardwareRenderer(SharedRendererState* state)
75 : shared_renderer_state_(state),
76 last_egl_context_(eglGetCurrentContext()),
77 stencil_enabled_(false),
78 viewport_clip_valid_for_dcheck_(false),
79 root_layer_(cc::Layer::Create()),
80 output_surface_(NULL) {
81 DCHECK(last_egl_context_);
83 gl_surface_ = new AwGLSurface;
85 cc::LayerTreeSettings settings;
87 // Should be kept in sync with compositor_impl_android.cc.
88 settings.allow_antialiasing = false;
89 settings.highp_threshold_min = 2048;
91 // Webview does not own the surface so should not clear it.
92 settings.should_clear_root_render_pass = false;
94 layer_tree_host_ =
95 cc::LayerTreeHost::CreateSingleThreaded(this, this, NULL, settings);
96 layer_tree_host_->SetRootLayer(root_layer_);
97 layer_tree_host_->SetLayerTreeHostClientReady();
98 layer_tree_host_->set_has_transparent_background(true);
101 HardwareRenderer::~HardwareRenderer() {
102 // Must reset everything before |resource_collection_| to ensure all
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
117 resource_collection_->SetClient(NULL);
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_->SetExternalStencilTest(stencil_enabled_);
127 output_surface_->SetDrawConstraints(viewport_, clip_);
130 void HardwareRenderer::DrawGL(bool stencil_enabled,
131 int framebuffer_binding_ext,
132 AwDrawGLInfo* draw_info) {
133 TRACE_EVENT0("android_webview", "HardwareRenderer::DrawGL");
135 // We need to watch if the current Android context has changed and enforce
136 // a clean-up in the compositor.
137 EGLContext current_context = eglGetCurrentContext();
138 if (!current_context) {
139 DLOG(ERROR) << "DrawGL called without EGLContext";
140 return;
143 // TODO(boliu): Handle context loss.
144 if (last_egl_context_ != current_context)
145 DLOG(WARNING) << "EGLContextChanged";
147 scoped_ptr<DrawGLInput> input = shared_renderer_state_->PassDrawGLInput();
148 if (!resource_collection_.get()) {
149 resource_collection_ = new cc::DelegatedFrameResourceCollection;
150 resource_collection_->SetClient(this);
153 if (input.get()) {
154 DCHECK(!input->frame.gl_frame_data);
155 DCHECK(!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 input->frame.delegated_frame_data->device_scale_factor = 1.0f;
164 gfx::Size frame_size =
165 input->frame.delegated_frame_data->render_pass_list.back()
166 ->output_rect.size();
167 bool size_changed = frame_size != frame_size_;
168 frame_size_ = frame_size;
169 scroll_offset_ = input->scroll_offset;
171 if (!frame_provider_ || size_changed) {
172 if (delegated_layer_) {
173 delegated_layer_->RemoveFromParent();
176 frame_provider_ = new cc::DelegatedFrameProvider(
177 resource_collection_.get(), input->frame.delegated_frame_data.Pass());
179 delegated_layer_ = cc::DelegatedRendererLayer::Create(frame_provider_);
180 delegated_layer_->SetBounds(gfx::Size(input->width, input->height));
181 delegated_layer_->SetIsDrawable(true);
183 root_layer_->AddChild(delegated_layer_);
184 } else {
185 frame_provider_->SetFrameData(input->frame.delegated_frame_data.Pass());
189 viewport_.SetSize(draw_info->width, draw_info->height);
190 layer_tree_host_->SetViewportSize(viewport_);
191 clip_.SetRect(draw_info->clip_left,
192 draw_info->clip_top,
193 draw_info->clip_right - draw_info->clip_left,
194 draw_info->clip_bottom - draw_info->clip_top);
195 stencil_enabled_ = stencil_enabled;
197 gfx::Transform transform(gfx::Transform::kSkipInitialization);
198 transform.matrix().setColMajorf(draw_info->transform);
199 transform.Translate(scroll_offset_.x(), scroll_offset_.y());
200 delegated_layer_->SetTransform(transform);
202 gl_surface_->SetBackingFrameBufferObject(framebuffer_binding_ext);
204 base::AutoReset<bool> frame_resetter(&viewport_clip_valid_for_dcheck_,
205 true);
206 layer_tree_host_->SetNeedsRedrawRect(clip_);
207 layer_tree_host_->Composite(gfx::FrameTime::Now());
209 gl_surface_->ResetBackingFrameBufferObject();
212 scoped_ptr<cc::OutputSurface> HardwareRenderer::CreateOutputSurface(
213 bool fallback) {
214 // Android webview does not support losing output surface.
215 DCHECK(!fallback);
216 scoped_refptr<cc::ContextProvider> context_provider =
217 CreateContext(gl_surface_,
218 DeferredGpuCommandService::GetInstance(),
219 shared_renderer_state_->GetSharedContext());
220 scoped_ptr<ParentOutputSurface> output_surface_holder(
221 new ParentOutputSurface(context_provider));
222 output_surface_ = output_surface_holder.get();
223 return output_surface_holder.PassAs<cc::OutputSurface>();
226 void HardwareRenderer::UnusedResourcesAreAvailable() {
227 cc::ReturnedResourceArray returned_resources;
228 resource_collection_->TakeUnusedResourcesForChildCompositor(
229 &returned_resources);
230 shared_renderer_state_->InsertReturnedResources(returned_resources);
233 } // namespace android_webview