Add a FrameHostMsg_BeginNavigation IPC
[chromium-blink-merge.git] / content / browser / renderer_host / compositor_impl_android.cc
blob6232bcdb434467966c7e2ba5b0d99ad07063a930
1 // Copyright (c) 2012 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 "content/browser/renderer_host/compositor_impl_android.h"
7 #include <android/bitmap.h>
8 #include <android/native_window_jni.h>
10 #include "base/android/jni_android.h"
11 #include "base/android/scoped_java_ref.h"
12 #include "base/bind.h"
13 #include "base/command_line.h"
14 #include "base/containers/hash_tables.h"
15 #include "base/lazy_instance.h"
16 #include "base/logging.h"
17 #include "base/single_thread_task_runner.h"
18 #include "base/synchronization/lock.h"
19 #include "base/threading/thread.h"
20 #include "base/threading/thread_checker.h"
21 #include "cc/base/switches.h"
22 #include "cc/input/input_handler.h"
23 #include "cc/layers/layer.h"
24 #include "cc/output/compositor_frame.h"
25 #include "cc/output/context_provider.h"
26 #include "cc/output/output_surface.h"
27 #include "cc/trees/layer_tree_host.h"
28 #include "content/browser/android/child_process_launcher_android.h"
29 #include "content/browser/gpu/browser_gpu_channel_host_factory.h"
30 #include "content/browser/gpu/gpu_surface_tracker.h"
31 #include "content/common/gpu/client/command_buffer_proxy_impl.h"
32 #include "content/common/gpu/client/context_provider_command_buffer.h"
33 #include "content/common/gpu/client/gl_helper.h"
34 #include "content/common/gpu/client/gpu_channel_host.h"
35 #include "content/common/gpu/client/webgraphicscontext3d_command_buffer_impl.h"
36 #include "content/common/gpu/gpu_process_launch_causes.h"
37 #include "content/common/host_shared_bitmap_manager.h"
38 #include "content/public/browser/android/compositor_client.h"
39 #include "gpu/command_buffer/client/gles2_interface.h"
40 #include "third_party/khronos/GLES2/gl2.h"
41 #include "third_party/khronos/GLES2/gl2ext.h"
42 #include "third_party/skia/include/core/SkMallocPixelRef.h"
43 #include "ui/base/android/window_android.h"
44 #include "ui/gfx/android/device_display_info.h"
45 #include "ui/gfx/frame_time.h"
46 #include "ui/gl/android/surface_texture.h"
47 #include "ui/gl/android/surface_texture_tracker.h"
48 #include "webkit/common/gpu/context_provider_in_process.h"
49 #include "webkit/common/gpu/webgraphicscontext3d_in_process_command_buffer_impl.h"
51 namespace {
53 const unsigned int kMaxSwapBuffers = 2U;
55 // Used to override capabilities_.adjust_deadline_for_parent to false
56 class OutputSurfaceWithoutParent : public cc::OutputSurface {
57 public:
58 OutputSurfaceWithoutParent(const scoped_refptr<
59 content::ContextProviderCommandBuffer>& context_provider)
60 : cc::OutputSurface(context_provider) {
61 capabilities_.adjust_deadline_for_parent = false;
64 virtual void SwapBuffers(cc::CompositorFrame* frame) OVERRIDE {
65 content::ContextProviderCommandBuffer* provider_command_buffer =
66 static_cast<content::ContextProviderCommandBuffer*>(
67 context_provider_.get());
68 content::CommandBufferProxyImpl* command_buffer_proxy =
69 provider_command_buffer->GetCommandBufferProxy();
70 DCHECK(command_buffer_proxy);
71 command_buffer_proxy->SetLatencyInfo(frame->metadata.latency_info);
73 OutputSurface::SwapBuffers(frame);
77 class SurfaceTextureTrackerImpl : public gfx::SurfaceTextureTracker {
78 public:
79 SurfaceTextureTrackerImpl() : next_surface_texture_id_(1) {
80 thread_checker_.DetachFromThread();
83 // Overridden from gfx::SurfaceTextureTracker:
84 virtual scoped_refptr<gfx::SurfaceTexture> AcquireSurfaceTexture(
85 int primary_id,
86 int secondary_id) OVERRIDE {
87 base::AutoLock lock(surface_textures_lock_);
88 SurfaceTextureMapKey key(primary_id, secondary_id);
89 SurfaceTextureMap::iterator it = surface_textures_.find(key);
90 if (it == surface_textures_.end())
91 return scoped_refptr<gfx::SurfaceTexture>();
92 scoped_refptr<gfx::SurfaceTexture> surface_texture = it->second;
93 surface_textures_.erase(it);
94 return surface_texture;
97 int AddSurfaceTexture(gfx::SurfaceTexture* surface_texture,
98 int child_process_id) {
99 DCHECK(thread_checker_.CalledOnValidThread());
100 int surface_texture_id = next_surface_texture_id_++;
101 if (next_surface_texture_id_ == INT_MAX)
102 next_surface_texture_id_ = 1;
104 base::AutoLock lock(surface_textures_lock_);
105 SurfaceTextureMapKey key(surface_texture_id, child_process_id);
106 DCHECK(surface_textures_.find(key) == surface_textures_.end());
107 surface_textures_[key] = surface_texture;
108 content::RegisterChildProcessSurfaceTexture(
109 surface_texture_id,
110 child_process_id,
111 surface_texture->j_surface_texture().obj());
112 return surface_texture_id;
115 void RemoveAllSurfaceTextures(int child_process_id) {
116 DCHECK(thread_checker_.CalledOnValidThread());
117 base::AutoLock lock(surface_textures_lock_);
118 SurfaceTextureMap::iterator it = surface_textures_.begin();
119 while (it != surface_textures_.end()) {
120 if (it->first.second == child_process_id) {
121 content::UnregisterChildProcessSurfaceTexture(it->first.first,
122 it->first.second);
123 surface_textures_.erase(it++);
124 } else {
125 ++it;
130 private:
131 typedef std::pair<int, int> SurfaceTextureMapKey;
132 typedef base::hash_map<SurfaceTextureMapKey,
133 scoped_refptr<gfx::SurfaceTexture> >
134 SurfaceTextureMap;
135 SurfaceTextureMap surface_textures_;
136 mutable base::Lock surface_textures_lock_;
137 int next_surface_texture_id_;
138 base::ThreadChecker thread_checker_;
140 base::LazyInstance<SurfaceTextureTrackerImpl> g_surface_texture_tracker =
141 LAZY_INSTANCE_INITIALIZER;
143 static bool g_initialized = false;
145 } // anonymous namespace
147 namespace content {
149 // static
150 Compositor* Compositor::Create(CompositorClient* client,
151 gfx::NativeWindow root_window) {
152 return client ? new CompositorImpl(client, root_window) : NULL;
155 // static
156 void Compositor::Initialize() {
157 DCHECK(!CompositorImpl::IsInitialized());
158 // SurfaceTextureTracker instance must be set before we create a GPU thread
159 // that could be using it to initialize GLImage instances.
160 gfx::SurfaceTextureTracker::InitInstance(g_surface_texture_tracker.Pointer());
161 g_initialized = true;
164 // static
165 bool CompositorImpl::IsInitialized() {
166 return g_initialized;
169 // static
170 int CompositorImpl::CreateSurfaceTexture(int child_process_id) {
171 // Note: this needs to be 0 as the surface texture implemenation will take
172 // ownership of the texture and call glDeleteTextures when the GPU service
173 // attaches the surface texture to a real texture id. glDeleteTextures
174 // silently ignores 0.
175 const int kDummyTextureId = 0;
176 scoped_refptr<gfx::SurfaceTexture> surface_texture =
177 gfx::SurfaceTexture::Create(kDummyTextureId);
178 return g_surface_texture_tracker.Pointer()->AddSurfaceTexture(
179 surface_texture.get(), child_process_id);
182 // static
183 void CompositorImpl::DestroyAllSurfaceTextures(int child_process_id) {
184 g_surface_texture_tracker.Pointer()->RemoveAllSurfaceTextures(
185 child_process_id);
188 CompositorImpl::CompositorImpl(CompositorClient* client,
189 gfx::NativeWindow root_window)
190 : root_layer_(cc::Layer::Create()),
191 has_transparent_background_(false),
192 device_scale_factor_(1),
193 window_(NULL),
194 surface_id_(0),
195 client_(client),
196 root_window_(root_window),
197 did_post_swapbuffers_(false),
198 ignore_schedule_composite_(false),
199 needs_composite_(false),
200 needs_animate_(false),
201 will_composite_immediately_(false),
202 composite_on_vsync_trigger_(DO_NOT_COMPOSITE),
203 pending_swapbuffers_(0U),
204 weak_factory_(this) {
205 DCHECK(client);
206 DCHECK(root_window);
207 ImageTransportFactoryAndroid::AddObserver(this);
208 root_window->AttachCompositor(this);
211 CompositorImpl::~CompositorImpl() {
212 root_window_->DetachCompositor();
213 ImageTransportFactoryAndroid::RemoveObserver(this);
214 // Clean-up any surface references.
215 SetSurface(NULL);
218 void CompositorImpl::PostComposite(CompositingTrigger trigger) {
219 DCHECK(needs_composite_);
220 DCHECK(trigger == COMPOSITE_IMMEDIATELY || trigger == COMPOSITE_EVENTUALLY);
222 if (will_composite_immediately_ ||
223 (trigger == COMPOSITE_EVENTUALLY && WillComposite())) {
224 // We will already composite soon enough.
225 DCHECK(WillComposite());
226 return;
229 if (DidCompositeThisFrame()) {
230 DCHECK(!WillCompositeThisFrame());
231 if (composite_on_vsync_trigger_ != COMPOSITE_IMMEDIATELY) {
232 composite_on_vsync_trigger_ = trigger;
233 root_window_->RequestVSyncUpdate();
235 DCHECK(WillComposite());
236 return;
239 base::TimeDelta delay;
240 if (trigger == COMPOSITE_IMMEDIATELY) {
241 will_composite_immediately_ = true;
242 composite_on_vsync_trigger_ = DO_NOT_COMPOSITE;
243 } else {
244 DCHECK(!WillComposite());
245 const base::TimeDelta estimated_composite_time = vsync_period_ / 4;
246 const base::TimeTicks now = base::TimeTicks::Now();
248 if (!last_vsync_.is_null() && (now - last_vsync_) < vsync_period_) {
249 base::TimeTicks next_composite =
250 last_vsync_ + vsync_period_ - estimated_composite_time;
251 if (next_composite < now) {
252 // It's too late, we will reschedule composite as needed on the next
253 // vsync.
254 composite_on_vsync_trigger_ = COMPOSITE_EVENTUALLY;
255 root_window_->RequestVSyncUpdate();
256 DCHECK(WillComposite());
257 return;
260 delay = next_composite - now;
263 TRACE_EVENT2("cc", "CompositorImpl::PostComposite",
264 "trigger", trigger,
265 "delay", delay.InMillisecondsF());
267 DCHECK(composite_on_vsync_trigger_ == DO_NOT_COMPOSITE);
268 if (current_composite_task_)
269 current_composite_task_->Cancel();
271 // Unretained because we cancel the task on shutdown.
272 current_composite_task_.reset(new base::CancelableClosure(
273 base::Bind(&CompositorImpl::Composite, base::Unretained(this), trigger)));
274 base::MessageLoop::current()->PostDelayedTask(
275 FROM_HERE, current_composite_task_->callback(), delay);
278 void CompositorImpl::Composite(CompositingTrigger trigger) {
279 BrowserGpuChannelHostFactory* factory =
280 BrowserGpuChannelHostFactory::instance();
281 if (!factory->GetGpuChannel() || factory->GetGpuChannel()->IsLost()) {
282 CauseForGpuLaunch cause =
283 CAUSE_FOR_GPU_LAUNCH_WEBGRAPHICSCONTEXT3DCOMMANDBUFFERIMPL_INITIALIZE;
284 factory->EstablishGpuChannel(
285 cause,
286 base::Bind(&CompositorImpl::OnGpuChannelEstablished,
287 weak_factory_.GetWeakPtr()));
288 return;
291 DCHECK(host_);
292 DCHECK(trigger == COMPOSITE_IMMEDIATELY || trigger == COMPOSITE_EVENTUALLY);
293 DCHECK(needs_composite_);
294 DCHECK(!DidCompositeThisFrame());
296 if (trigger == COMPOSITE_IMMEDIATELY)
297 will_composite_immediately_ = false;
299 DCHECK_LE(pending_swapbuffers_, kMaxSwapBuffers);
300 if (pending_swapbuffers_ == kMaxSwapBuffers) {
301 TRACE_EVENT0("compositor", "CompositorImpl_SwapLimit");
302 return;
305 // Reset state before Layout+Composite since that might create more
306 // requests to Composite that we need to respect.
307 needs_composite_ = false;
309 // Only allow compositing once per vsync.
310 current_composite_task_->Cancel();
311 DCHECK(DidCompositeThisFrame() && !WillComposite());
313 // Ignore ScheduleComposite() from layer tree changes during layout and
314 // animation updates that will already be reflected in the current frame
315 // we are about to draw.
316 ignore_schedule_composite_ = true;
317 client_->Layout();
319 const base::TimeTicks frame_time = gfx::FrameTime::Now();
320 if (needs_animate_) {
321 needs_animate_ = false;
322 root_window_->Animate(frame_time);
324 ignore_schedule_composite_ = false;
326 did_post_swapbuffers_ = false;
327 host_->Composite(frame_time);
328 if (did_post_swapbuffers_)
329 pending_swapbuffers_++;
331 // Need to track vsync to avoid compositing more than once per frame.
332 root_window_->RequestVSyncUpdate();
335 void CompositorImpl::OnGpuChannelEstablished() {
336 ScheduleComposite();
339 UIResourceProvider& CompositorImpl::GetUIResourceProvider() {
340 return ui_resource_provider_;
343 void CompositorImpl::SetRootLayer(scoped_refptr<cc::Layer> root_layer) {
344 root_layer_->RemoveAllChildren();
345 if (root_layer)
346 root_layer_->AddChild(root_layer);
349 void CompositorImpl::SetWindowSurface(ANativeWindow* window) {
350 GpuSurfaceTracker* tracker = GpuSurfaceTracker::Get();
352 if (window_) {
353 tracker->RemoveSurface(surface_id_);
354 ANativeWindow_release(window_);
355 window_ = NULL;
356 surface_id_ = 0;
357 SetVisible(false);
360 if (window) {
361 window_ = window;
362 ANativeWindow_acquire(window);
363 surface_id_ = tracker->AddSurfaceForNativeWidget(window);
364 tracker->SetSurfaceHandle(
365 surface_id_,
366 gfx::GLSurfaceHandle(gfx::kNullPluginWindow, gfx::NATIVE_DIRECT));
367 SetVisible(true);
371 void CompositorImpl::SetSurface(jobject surface) {
372 JNIEnv* env = base::android::AttachCurrentThread();
373 base::android::ScopedJavaLocalRef<jobject> j_surface(env, surface);
375 // First, cleanup any existing surface references.
376 if (surface_id_)
377 content::UnregisterViewSurface(surface_id_);
378 SetWindowSurface(NULL);
380 // Now, set the new surface if we have one.
381 ANativeWindow* window = NULL;
382 if (surface) {
383 // Note: This ensures that any local references used by
384 // ANativeWindow_fromSurface are released immediately. This is needed as a
385 // workaround for https://code.google.com/p/android/issues/detail?id=68174
386 base::android::ScopedJavaLocalFrame scoped_local_reference_frame(env);
387 window = ANativeWindow_fromSurface(env, surface);
389 if (window) {
390 SetWindowSurface(window);
391 ANativeWindow_release(window);
392 content::RegisterViewSurface(surface_id_, j_surface.obj());
396 void CompositorImpl::SetVisible(bool visible) {
397 if (!visible) {
398 if (WillComposite())
399 CancelComposite();
400 ui_resource_provider_.SetLayerTreeHost(NULL);
401 host_.reset();
402 } else if (!host_) {
403 DCHECK(!WillComposite());
404 needs_composite_ = false;
405 needs_animate_ = false;
406 pending_swapbuffers_ = 0;
407 cc::LayerTreeSettings settings;
408 settings.refresh_rate = 60.0;
409 settings.impl_side_painting = false;
410 settings.allow_antialiasing = false;
411 settings.calculate_top_controls_position = false;
412 settings.top_controls_height = 0.f;
413 settings.highp_threshold_min = 2048;
415 CommandLine* command_line = CommandLine::ForCurrentProcess();
416 settings.initial_debug_state.SetRecordRenderingStats(
417 command_line->HasSwitch(cc::switches::kEnableGpuBenchmarking));
418 settings.initial_debug_state.show_fps_counter =
419 command_line->HasSwitch(cc::switches::kUIShowFPSCounter);
421 host_ = cc::LayerTreeHost::CreateSingleThreaded(
422 this, this, HostSharedBitmapManager::current(), settings);
423 host_->SetRootLayer(root_layer_);
425 host_->SetVisible(true);
426 host_->SetLayerTreeHostClientReady();
427 host_->SetViewportSize(size_);
428 host_->set_has_transparent_background(has_transparent_background_);
429 host_->SetDeviceScaleFactor(device_scale_factor_);
430 ui_resource_provider_.SetLayerTreeHost(host_.get());
434 void CompositorImpl::setDeviceScaleFactor(float factor) {
435 device_scale_factor_ = factor;
436 if (host_)
437 host_->SetDeviceScaleFactor(factor);
440 void CompositorImpl::SetWindowBounds(const gfx::Size& size) {
441 if (size_ == size)
442 return;
444 size_ = size;
445 if (host_)
446 host_->SetViewportSize(size);
447 root_layer_->SetBounds(size);
450 void CompositorImpl::SetHasTransparentBackground(bool flag) {
451 has_transparent_background_ = flag;
452 if (host_)
453 host_->set_has_transparent_background(flag);
456 void CompositorImpl::SetNeedsComposite() {
457 if (!host_.get())
458 return;
459 DCHECK(!needs_composite_ || WillComposite());
461 needs_composite_ = true;
462 PostComposite(COMPOSITE_IMMEDIATELY);
465 static scoped_ptr<WebGraphicsContext3DCommandBufferImpl>
466 CreateGpuProcessViewContext(
467 const scoped_refptr<GpuChannelHost>& gpu_channel_host,
468 const blink::WebGraphicsContext3D::Attributes attributes,
469 int surface_id) {
470 DCHECK(gpu_channel_host);
472 GURL url("chrome://gpu/Compositor::createContext3D");
473 static const size_t kBytesPerPixel = 4;
474 gfx::DeviceDisplayInfo display_info;
475 size_t full_screen_texture_size_in_bytes =
476 display_info.GetDisplayHeight() *
477 display_info.GetDisplayWidth() *
478 kBytesPerPixel;
479 WebGraphicsContext3DCommandBufferImpl::SharedMemoryLimits limits;
480 limits.command_buffer_size = 64 * 1024;
481 limits.start_transfer_buffer_size = 64 * 1024;
482 limits.min_transfer_buffer_size = 64 * 1024;
483 limits.max_transfer_buffer_size = std::min(
484 3 * full_screen_texture_size_in_bytes, kDefaultMaxTransferBufferSize);
485 limits.mapped_memory_reclaim_limit = 2 * 1024 * 1024;
486 bool lose_context_when_out_of_memory = true;
487 return make_scoped_ptr(
488 new WebGraphicsContext3DCommandBufferImpl(surface_id,
489 url,
490 gpu_channel_host.get(),
491 attributes,
492 lose_context_when_out_of_memory,
493 limits,
494 NULL));
497 void CompositorImpl::Layout() {
498 // TODO: If we get this callback from the SingleThreadProxy, we need
499 // to stop calling it ourselves in CompositorImpl::Composite().
500 NOTREACHED();
501 client_->Layout();
504 scoped_ptr<cc::OutputSurface> CompositorImpl::CreateOutputSurface(
505 bool fallback) {
506 blink::WebGraphicsContext3D::Attributes attrs;
507 attrs.shareResources = true;
508 attrs.noAutomaticFlushes = true;
509 pending_swapbuffers_ = 0;
511 DCHECK(window_);
512 DCHECK(surface_id_);
514 scoped_refptr<ContextProviderCommandBuffer> context_provider;
515 BrowserGpuChannelHostFactory* factory =
516 BrowserGpuChannelHostFactory::instance();
517 scoped_refptr<GpuChannelHost> gpu_channel_host = factory->GetGpuChannel();
518 if (gpu_channel_host && !gpu_channel_host->IsLost()) {
519 context_provider = ContextProviderCommandBuffer::Create(
520 CreateGpuProcessViewContext(gpu_channel_host, attrs, surface_id_),
521 "BrowserCompositor");
523 if (!context_provider.get()) {
524 LOG(ERROR) << "Failed to create 3D context for compositor.";
525 return scoped_ptr<cc::OutputSurface>();
528 return scoped_ptr<cc::OutputSurface>(
529 new OutputSurfaceWithoutParent(context_provider));
532 void CompositorImpl::OnLostResources() {
533 client_->DidLoseResources();
534 ui_resource_provider_.UIResourcesAreInvalid();
537 void CompositorImpl::ScheduleComposite() {
538 DCHECK(!needs_composite_ || WillComposite());
539 if (ignore_schedule_composite_)
540 return;
542 needs_composite_ = true;
543 // We currently expect layer tree invalidations at most once per frame
544 // during normal operation and therefore try to composite immediately
545 // to minimize latency.
546 PostComposite(COMPOSITE_IMMEDIATELY);
549 void CompositorImpl::ScheduleAnimation() {
550 DCHECK(!needs_animate_ || needs_composite_);
551 DCHECK(!needs_composite_ || WillComposite());
552 needs_animate_ = true;
554 if (needs_composite_)
555 return;
557 TRACE_EVENT0("cc", "CompositorImpl::ScheduleAnimation");
558 needs_composite_ = true;
559 PostComposite(COMPOSITE_EVENTUALLY);
562 void CompositorImpl::DidPostSwapBuffers() {
563 TRACE_EVENT0("compositor", "CompositorImpl::DidPostSwapBuffers");
564 did_post_swapbuffers_ = true;
567 void CompositorImpl::DidCompleteSwapBuffers() {
568 TRACE_EVENT0("compositor", "CompositorImpl::DidCompleteSwapBuffers");
569 DCHECK_GT(pending_swapbuffers_, 0U);
570 if (pending_swapbuffers_-- == kMaxSwapBuffers && needs_composite_)
571 PostComposite(COMPOSITE_IMMEDIATELY);
572 client_->OnSwapBuffersCompleted(pending_swapbuffers_);
575 void CompositorImpl::DidAbortSwapBuffers() {
576 TRACE_EVENT0("compositor", "CompositorImpl::DidAbortSwapBuffers");
577 // This really gets called only once from
578 // SingleThreadProxy::DidLoseOutputSurfaceOnImplThread() when the
579 // context was lost.
580 client_->OnSwapBuffersCompleted(0);
583 void CompositorImpl::DidCommit() {
584 root_window_->OnCompositingDidCommit();
587 void CompositorImpl::AttachLayerForReadback(scoped_refptr<cc::Layer> layer) {
588 root_layer_->AddChild(layer);
591 void CompositorImpl::RequestCopyOfOutputOnRootLayer(
592 scoped_ptr<cc::CopyOutputRequest> request) {
593 root_layer_->RequestCopyOfOutput(request.Pass());
596 void CompositorImpl::OnVSync(base::TimeTicks frame_time,
597 base::TimeDelta vsync_period) {
598 vsync_period_ = vsync_period;
599 last_vsync_ = frame_time;
601 if (WillCompositeThisFrame()) {
602 // We somehow missed the last vsync interval, so reschedule for deadline.
603 // We cannot schedule immediately, or will get us out-of-phase with new
604 // renderer frames.
605 CancelComposite();
606 composite_on_vsync_trigger_ = COMPOSITE_EVENTUALLY;
607 } else {
608 current_composite_task_.reset();
611 DCHECK(!DidCompositeThisFrame() && !WillCompositeThisFrame());
612 if (composite_on_vsync_trigger_ != DO_NOT_COMPOSITE) {
613 CompositingTrigger trigger = composite_on_vsync_trigger_;
614 composite_on_vsync_trigger_ = DO_NOT_COMPOSITE;
615 PostComposite(trigger);
619 void CompositorImpl::SetNeedsAnimate() {
620 if (!host_)
621 return;
623 host_->SetNeedsAnimate();
626 } // namespace content