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/common/gpu/image_transport_surface.h"
8 #include "base/bind_helpers.h"
9 #include "base/command_line.h"
10 #include "base/debug/trace_event.h"
11 #include "content/common/gpu/gpu_channel.h"
12 #include "content/common/gpu/gpu_channel_manager.h"
13 #include "content/common/gpu/gpu_command_buffer_stub.h"
14 #include "content/common/gpu/gpu_messages.h"
15 #include "content/common/gpu/sync_point_manager.h"
16 #include "content/common/gpu/texture_image_transport_surface.h"
17 #include "gpu/command_buffer/service/gpu_scheduler.h"
18 #include "ui/gfx/vsync_provider.h"
19 #include "ui/gl/gl_implementation.h"
20 #include "ui/gl/gl_switches.h"
23 #include "ui/base/win/shell.h"
28 ImageTransportSurface::ImageTransportSurface() {}
30 ImageTransportSurface::~ImageTransportSurface() {}
32 scoped_refptr
<gfx::GLSurface
> ImageTransportSurface::CreateSurface(
33 GpuChannelManager
* manager
,
34 GpuCommandBufferStub
* stub
,
35 const gfx::GLSurfaceHandle
& handle
) {
36 scoped_refptr
<gfx::GLSurface
> surface
;
37 if (handle
.transport_type
== gfx::TEXTURE_TRANSPORT
)
38 surface
= new TextureImageTransportSurface(manager
, stub
, handle
);
40 surface
= CreateNativeSurface(manager
, stub
, handle
);
42 if (!surface
.get() || !surface
->Initialize())
47 ImageTransportHelper::ImageTransportHelper(ImageTransportSurface
* surface
,
48 GpuChannelManager
* manager
,
49 GpuCommandBufferStub
* stub
,
50 gfx::PluginWindowHandle handle
)
53 stub_(stub
->AsWeakPtr()),
55 route_id_
= manager_
->GenerateRouteID();
56 manager_
->AddRoute(route_id_
, this);
59 ImageTransportHelper::~ImageTransportHelper() {
61 stub_
->SetLatencyInfoCallback(
62 base::Callback
<void(const std::vector
<ui::LatencyInfo
>&)>());
64 manager_
->RemoveRoute(route_id_
);
67 bool ImageTransportHelper::Initialize() {
68 gpu::gles2::GLES2Decoder
* decoder
= Decoder();
73 decoder
->SetResizeCallback(
74 base::Bind(&ImageTransportHelper::Resize
, base::Unretained(this)));
76 stub_
->SetLatencyInfoCallback(
77 base::Bind(&ImageTransportHelper::SetLatencyInfo
,
78 base::Unretained(this)));
80 manager_
->Send(new GpuHostMsg_AcceleratedSurfaceInitialized(
81 stub_
->surface_id(), route_id_
));
86 void ImageTransportHelper::Destroy() {}
88 bool ImageTransportHelper::OnMessageReceived(const IPC::Message
& message
) {
90 IPC_BEGIN_MESSAGE_MAP(ImageTransportHelper
, message
)
91 IPC_MESSAGE_HANDLER(AcceleratedSurfaceMsg_BufferPresented
,
93 IPC_MESSAGE_HANDLER(AcceleratedSurfaceMsg_WakeUpGpu
, OnWakeUpGpu
);
94 IPC_MESSAGE_UNHANDLED(handled
= false)
99 void ImageTransportHelper::SendAcceleratedSurfaceBuffersSwapped(
100 GpuHostMsg_AcceleratedSurfaceBuffersSwapped_Params params
) {
101 // TRACE_EVENT for gpu tests:
102 TRACE_EVENT_INSTANT2("test_gpu", "SwapBuffers",
103 TRACE_EVENT_SCOPE_THREAD
,
104 "GLImpl", static_cast<int>(gfx::GetGLImplementation()),
105 "width", params
.size
.width());
106 params
.surface_id
= stub_
->surface_id();
107 params
.route_id
= route_id_
;
108 manager_
->Send(new GpuHostMsg_AcceleratedSurfaceBuffersSwapped(params
));
111 void ImageTransportHelper::SendAcceleratedSurfacePostSubBuffer(
112 GpuHostMsg_AcceleratedSurfacePostSubBuffer_Params params
) {
113 params
.surface_id
= stub_
->surface_id();
114 params
.route_id
= route_id_
;
115 manager_
->Send(new GpuHostMsg_AcceleratedSurfacePostSubBuffer(params
));
118 void ImageTransportHelper::SendAcceleratedSurfaceRelease() {
119 GpuHostMsg_AcceleratedSurfaceRelease_Params params
;
120 params
.surface_id
= stub_
->surface_id();
121 manager_
->Send(new GpuHostMsg_AcceleratedSurfaceRelease(params
));
124 void ImageTransportHelper::SendUpdateVSyncParameters(
125 base::TimeTicks timebase
, base::TimeDelta interval
) {
126 manager_
->Send(new GpuHostMsg_UpdateVSyncParameters(stub_
->surface_id(),
131 void ImageTransportHelper::SendLatencyInfo(
132 const std::vector
<ui::LatencyInfo
>& latency_info
) {
133 manager_
->Send(new GpuHostMsg_FrameDrawn(latency_info
));
136 void ImageTransportHelper::SetScheduled(bool is_scheduled
) {
137 gpu::GpuScheduler
* scheduler
= Scheduler();
141 scheduler
->SetScheduled(is_scheduled
);
144 void ImageTransportHelper::DeferToFence(base::Closure task
) {
145 gpu::GpuScheduler
* scheduler
= Scheduler();
148 scheduler
->DeferToFence(task
);
151 void ImageTransportHelper::SetPreemptByFlag(
152 scoped_refptr
<gpu::PreemptionFlag
> preemption_flag
) {
153 stub_
->channel()->SetPreemptByFlag(preemption_flag
);
156 bool ImageTransportHelper::MakeCurrent() {
157 gpu::gles2::GLES2Decoder
* decoder
= Decoder();
160 return decoder
->MakeCurrent();
163 void ImageTransportHelper::SetSwapInterval(gfx::GLContext
* context
) {
165 // If Aero Glass is enabled, then the renderer will handle ratelimiting and
166 // there's no tearing, so waiting for vsync is unnecessary.
167 if (ui::win::IsAeroGlassEnabled()) {
168 context
->SetSwapInterval(0);
172 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kDisableGpuVsync
))
173 context
->SetSwapInterval(0);
175 context
->SetSwapInterval(1);
178 void ImageTransportHelper::Suspend() {
179 manager_
->Send(new GpuHostMsg_AcceleratedSurfaceSuspend(stub_
->surface_id()));
182 gpu::GpuScheduler
* ImageTransportHelper::Scheduler() {
185 return stub_
->scheduler();
188 gpu::gles2::GLES2Decoder
* ImageTransportHelper::Decoder() {
191 return stub_
->decoder();
194 void ImageTransportHelper::OnBufferPresented(
195 const AcceleratedSurfaceMsg_BufferPresented_Params
& params
) {
196 surface_
->OnBufferPresented(params
);
199 void ImageTransportHelper::OnWakeUpGpu() {
200 surface_
->WakeUpGpu();
203 void ImageTransportHelper::Resize(gfx::Size size
, float scale_factor
) {
204 surface_
->OnResize(size
, scale_factor
);
206 #if defined(OS_ANDROID)
207 manager_
->gpu_memory_manager()->ScheduleManage(
208 GpuMemoryManager::kScheduleManageNow
);
212 void ImageTransportHelper::SetLatencyInfo(
213 const std::vector
<ui::LatencyInfo
>& latency_info
) {
214 surface_
->SetLatencyInfo(latency_info
);
217 PassThroughImageTransportSurface::PassThroughImageTransportSurface(
218 GpuChannelManager
* manager
,
219 GpuCommandBufferStub
* stub
,
220 gfx::GLSurface
* surface
)
221 : GLSurfaceAdapter(surface
),
222 did_set_swap_interval_(false) {
223 helper_
.reset(new ImageTransportHelper(this,
226 gfx::kNullPluginWindow
));
229 bool PassThroughImageTransportSurface::Initialize() {
230 // The surface is assumed to have already been initialized.
231 return helper_
->Initialize();
234 void PassThroughImageTransportSurface::Destroy() {
236 GLSurfaceAdapter::Destroy();
239 void PassThroughImageTransportSurface::SetLatencyInfo(
240 const std::vector
<ui::LatencyInfo
>& latency_info
) {
241 for (size_t i
= 0; i
< latency_info
.size(); i
++)
242 latency_info_
.push_back(latency_info
[i
]);
245 bool PassThroughImageTransportSurface::SwapBuffers() {
246 // GetVsyncValues before SwapBuffers to work around Mali driver bug:
248 SendVSyncUpdateIfAvailable();
249 bool result
= gfx::GLSurfaceAdapter::SwapBuffers();
250 for (size_t i
= 0; i
< latency_info_
.size(); i
++) {
251 latency_info_
[i
].AddLatencyNumber(
252 ui::INPUT_EVENT_LATENCY_TERMINATED_FRAME_SWAP_COMPONENT
, 0, 0);
255 helper_
->SendLatencyInfo(latency_info_
);
256 latency_info_
.clear();
260 bool PassThroughImageTransportSurface::PostSubBuffer(
261 int x
, int y
, int width
, int height
) {
262 SendVSyncUpdateIfAvailable();
263 bool result
= gfx::GLSurfaceAdapter::PostSubBuffer(x
, y
, width
, height
);
264 for (size_t i
= 0; i
< latency_info_
.size(); i
++) {
265 latency_info_
[i
].AddLatencyNumber(
266 ui::INPUT_EVENT_LATENCY_TERMINATED_FRAME_SWAP_COMPONENT
, 0, 0);
269 helper_
->SendLatencyInfo(latency_info_
);
270 latency_info_
.clear();
274 bool PassThroughImageTransportSurface::OnMakeCurrent(gfx::GLContext
* context
) {
275 if (!did_set_swap_interval_
) {
276 ImageTransportHelper::SetSwapInterval(context
);
277 did_set_swap_interval_
= true;
282 void PassThroughImageTransportSurface::OnBufferPresented(
283 const AcceleratedSurfaceMsg_BufferPresented_Params
& /* params */) {
287 void PassThroughImageTransportSurface::OnResize(gfx::Size size
,
288 float scale_factor
) {
292 gfx::Size
PassThroughImageTransportSurface::GetSize() {
293 return GLSurfaceAdapter::GetSize();
296 void PassThroughImageTransportSurface::WakeUpGpu() {
300 PassThroughImageTransportSurface::~PassThroughImageTransportSurface() {}
302 void PassThroughImageTransportSurface::SendVSyncUpdateIfAvailable() {
303 gfx::VSyncProvider
* vsync_provider
= GetVSyncProvider();
304 if (vsync_provider
) {
305 vsync_provider
->GetVSyncParameters(
306 base::Bind(&ImageTransportHelper::SendUpdateVSyncParameters
,
307 helper_
->AsWeakPtr()));
311 } // namespace content