Revert of Enable BeginFrame scheduling on aura (patchset #15 id:270001 of https:...
[chromium-blink-merge.git] / content / browser / compositor / gpu_browser_compositor_output_surface.cc
blob09a8b7280a5501c098204e692a954672ead73afa
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 "content/browser/compositor/gpu_browser_compositor_output_surface.h"
7 #include "cc/output/compositor_frame.h"
8 #include "cc/output/output_surface_client.h"
9 #include "content/browser/compositor/browser_compositor_overlay_candidate_validator.h"
10 #include "content/browser/compositor/reflector_impl.h"
11 #include "content/browser/renderer_host/render_widget_host_impl.h"
12 #include "content/common/gpu/client/context_provider_command_buffer.h"
13 #include "content/public/browser/browser_thread.h"
14 #include "gpu/command_buffer/client/context_support.h"
15 #include "gpu/command_buffer/client/gles2_interface.h"
17 namespace content {
19 GpuBrowserCompositorOutputSurface::GpuBrowserCompositorOutputSurface(
20 const scoped_refptr<ContextProviderCommandBuffer>& context,
21 const scoped_refptr<ui::CompositorVSyncManager>& vsync_manager,
22 scoped_ptr<BrowserCompositorOverlayCandidateValidator>
23 overlay_candidate_validator)
24 : BrowserCompositorOutputSurface(context,
25 vsync_manager,
26 overlay_candidate_validator.Pass()),
27 #if defined(OS_MACOSX)
28 should_show_frames_state_(SHOULD_SHOW_FRAMES),
29 #endif
30 swap_buffers_completion_callback_(
31 base::Bind(&GpuBrowserCompositorOutputSurface::OnSwapBuffersCompleted,
32 base::Unretained(this))),
33 update_vsync_parameters_callback_(base::Bind(
34 &BrowserCompositorOutputSurface::OnUpdateVSyncParametersFromGpu,
35 base::Unretained(this))) {
38 GpuBrowserCompositorOutputSurface::~GpuBrowserCompositorOutputSurface() {}
40 CommandBufferProxyImpl*
41 GpuBrowserCompositorOutputSurface::GetCommandBufferProxy() {
42 ContextProviderCommandBuffer* provider_command_buffer =
43 static_cast<content::ContextProviderCommandBuffer*>(
44 context_provider_.get());
45 CommandBufferProxyImpl* command_buffer_proxy =
46 provider_command_buffer->GetCommandBufferProxy();
47 DCHECK(command_buffer_proxy);
48 return command_buffer_proxy;
51 bool GpuBrowserCompositorOutputSurface::BindToClient(
52 cc::OutputSurfaceClient* client) {
53 if (!BrowserCompositorOutputSurface::BindToClient(client))
54 return false;
56 GetCommandBufferProxy()->SetSwapBuffersCompletionCallback(
57 swap_buffers_completion_callback_.callback());
58 GetCommandBufferProxy()->SetUpdateVSyncParametersCallback(
59 update_vsync_parameters_callback_.callback());
60 return true;
63 void GpuBrowserCompositorOutputSurface::SwapBuffers(
64 cc::CompositorFrame* frame) {
65 DCHECK(frame->gl_frame_data);
67 GetCommandBufferProxy()->SetLatencyInfo(frame->metadata.latency_info);
69 if (reflector_) {
70 if (frame->gl_frame_data->sub_buffer_rect ==
71 gfx::Rect(frame->gl_frame_data->size))
72 reflector_->OnSourceSwapBuffers();
73 else
74 reflector_->OnSourcePostSubBuffer(frame->gl_frame_data->sub_buffer_rect);
77 if (frame->gl_frame_data->sub_buffer_rect ==
78 gfx::Rect(frame->gl_frame_data->size)) {
79 context_provider_->ContextSupport()->Swap();
80 } else {
81 context_provider_->ContextSupport()->PartialSwapBuffers(
82 frame->gl_frame_data->sub_buffer_rect);
85 client_->DidSwapBuffers();
87 #if defined(OS_MACOSX)
88 if (should_show_frames_state_ ==
89 SHOULD_NOT_SHOW_FRAMES_NO_SWAP_AFTER_SUSPENDED) {
90 should_show_frames_state_ = SHOULD_SHOW_FRAMES;
92 #endif
95 void GpuBrowserCompositorOutputSurface::OnSwapBuffersCompleted(
96 const std::vector<ui::LatencyInfo>& latency_info) {
97 #if defined(OS_MACOSX)
98 // On Mac, delay acknowledging the swap to the output surface client until
99 // it has been drawn, see OnSurfaceDisplayed();
100 NOTREACHED();
101 #else
102 if (BrowserThread::CurrentlyOn(BrowserThread::UI)) {
103 RenderWidgetHostImpl::CompositorFrameDrawn(latency_info);
104 } else {
105 BrowserThread::PostTask(
106 BrowserThread::UI,
107 FROM_HERE,
108 base::Bind(&RenderWidgetHostImpl::CompositorFrameDrawn, latency_info));
110 OnSwapBuffersComplete();
111 #endif
114 #if defined(OS_MACOSX)
115 void GpuBrowserCompositorOutputSurface::OnSurfaceDisplayed() {
116 cc::OutputSurface::OnSwapBuffersComplete();
119 void GpuBrowserCompositorOutputSurface::SetSurfaceSuspendedForRecycle(
120 bool suspended) {
121 if (suspended) {
122 // It may be that there are frames in-flight from the GPU process back to
123 // the browser. Make sure that these frames are not displayed by ignoring
124 // them in GpuProcessHostUIShim, until the browser issues a SwapBuffers for
125 // the new content.
126 should_show_frames_state_ = SHOULD_NOT_SHOW_FRAMES_SUSPENDED;
127 } else {
128 // Discard the backbuffer before drawing the new frame. This is necessary
129 // only when using a ImageTransportSurfaceFBO with a
130 // CALayerStorageProvider. Discarding the backbuffer results in the next
131 // frame using a new CALayer and CAContext, which guarantees that the
132 // browser will not flash stale content when adding the remote CALayer to
133 // the NSView hierarchy (it could flash stale content because the system
134 // window server is not synchronized with any signals we control or
135 // observe).
136 if (should_show_frames_state_ == SHOULD_NOT_SHOW_FRAMES_SUSPENDED) {
137 DiscardBackbuffer();
138 should_show_frames_state_ =
139 SHOULD_NOT_SHOW_FRAMES_NO_SWAP_AFTER_SUSPENDED;
144 bool GpuBrowserCompositorOutputSurface::
145 SurfaceShouldNotShowFramesAfterSuspendForRecycle() const {
146 return should_show_frames_state_ != SHOULD_SHOW_FRAMES;
148 #endif
150 } // namespace content