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/reflector_impl.h"
10 #include "content/browser/renderer_host/render_widget_host_impl.h"
11 #include "content/common/gpu/client/context_provider_command_buffer.h"
12 #include "content/public/browser/browser_thread.h"
13 #include "gpu/command_buffer/client/context_support.h"
14 #include "gpu/command_buffer/client/gles2_interface.h"
18 GpuBrowserCompositorOutputSurface::GpuBrowserCompositorOutputSurface(
19 const scoped_refptr
<ContextProviderCommandBuffer
>& context
,
21 IDMap
<BrowserCompositorOutputSurface
>* output_surface_map
,
22 const scoped_refptr
<ui::CompositorVSyncManager
>& vsync_manager
,
23 scoped_ptr
<cc::OverlayCandidateValidator
> overlay_candidate_validator
)
24 : BrowserCompositorOutputSurface(context
,
28 swap_buffers_completion_callback_(
29 base::Bind(&GpuBrowserCompositorOutputSurface::OnSwapBuffersCompleted
,
30 base::Unretained(this))) {
31 overlay_candidate_validator_
= overlay_candidate_validator
.Pass();
34 GpuBrowserCompositorOutputSurface::~GpuBrowserCompositorOutputSurface() {}
36 CommandBufferProxyImpl
*
37 GpuBrowserCompositorOutputSurface::GetCommandBufferProxy() {
38 ContextProviderCommandBuffer
* provider_command_buffer
=
39 static_cast<content::ContextProviderCommandBuffer
*>(
40 context_provider_
.get());
41 CommandBufferProxyImpl
* command_buffer_proxy
=
42 provider_command_buffer
->GetCommandBufferProxy();
43 DCHECK(command_buffer_proxy
);
44 return command_buffer_proxy
;
47 bool GpuBrowserCompositorOutputSurface::BindToClient(
48 cc::OutputSurfaceClient
* client
) {
49 if (!BrowserCompositorOutputSurface::BindToClient(client
))
52 GetCommandBufferProxy()->SetSwapBuffersCompletionCallback(
53 swap_buffers_completion_callback_
.callback());
57 void GpuBrowserCompositorOutputSurface::SwapBuffers(
58 cc::CompositorFrame
* frame
) {
59 DCHECK(frame
->gl_frame_data
);
61 GetCommandBufferProxy()->SetLatencyInfo(frame
->metadata
.latency_info
);
63 if (reflector_
.get()) {
64 if (frame
->gl_frame_data
->sub_buffer_rect
==
65 gfx::Rect(frame
->gl_frame_data
->size
))
66 reflector_
->OnSwapBuffers();
68 reflector_
->OnPostSubBuffer(frame
->gl_frame_data
->sub_buffer_rect
);
71 if (frame
->gl_frame_data
->sub_buffer_rect
==
72 gfx::Rect(frame
->gl_frame_data
->size
)) {
73 context_provider_
->ContextSupport()->Swap();
75 context_provider_
->ContextSupport()->PartialSwapBuffers(
76 frame
->gl_frame_data
->sub_buffer_rect
);
79 client_
->DidSwapBuffers();
82 void GpuBrowserCompositorOutputSurface::OnSwapBuffersCompleted(
83 const std::vector
<ui::LatencyInfo
>& latency_info
) {
84 #if defined(OS_MACOSX)
85 // On Mac, delay acknowledging the swap to the output surface client until
86 // it has been drawn, see OnSurfaceDisplayed();
89 if (BrowserThread::CurrentlyOn(BrowserThread::UI
)) {
90 RenderWidgetHostImpl::CompositorFrameDrawn(latency_info
);
92 BrowserThread::PostTask(
95 base::Bind(&RenderWidgetHostImpl::CompositorFrameDrawn
, latency_info
));
97 OnSwapBuffersComplete();
101 #if defined(OS_MACOSX)
102 void GpuBrowserCompositorOutputSurface::OnSurfaceDisplayed() {
103 cc::OutputSurface::OnSwapBuffersComplete();
107 } // namespace content