OLD | NEW |
| (Empty) |
1 // Copyright 2016 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. | |
4 | |
5 #include "content/common/gpu/pass_through_image_transport_surface.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/bind_helpers.h" | |
9 #include "base/command_line.h" | |
10 #include "build/build_config.h" | |
11 #include "content/common/gpu/gpu_command_buffer_stub.h" | |
12 #include "ui/gfx/vsync_provider.h" | |
13 #include "ui/gl/gl_context.h" | |
14 #include "ui/gl/gl_switches.h" | |
15 | |
16 namespace content { | |
17 | |
18 PassThroughImageTransportSurface::PassThroughImageTransportSurface( | |
19 GpuChannelManager* /* manager */, | |
20 GpuCommandBufferStub* stub, | |
21 gfx::GLSurface* surface) | |
22 : GLSurfaceAdapter(surface), | |
23 stub_(stub->AsWeakPtr()), | |
24 did_set_swap_interval_(false), | |
25 weak_ptr_factory_(this) {} | |
26 | |
27 bool PassThroughImageTransportSurface::Initialize( | |
28 gfx::GLSurface::Format format) { | |
29 // The surface is assumed to have already been initialized. | |
30 if (!stub_.get() || !stub_->decoder()) | |
31 return false; | |
32 stub_->SetLatencyInfoCallback( | |
33 base::Bind(&PassThroughImageTransportSurface::SetLatencyInfo, | |
34 base::Unretained(this))); | |
35 return true; | |
36 } | |
37 | |
38 void PassThroughImageTransportSurface::Destroy() { | |
39 GLSurfaceAdapter::Destroy(); | |
40 } | |
41 | |
42 gfx::SwapResult PassThroughImageTransportSurface::SwapBuffers() { | |
43 scoped_ptr<std::vector<ui::LatencyInfo>> latency_info = StartSwapBuffers(); | |
44 gfx::SwapResult result = gfx::GLSurfaceAdapter::SwapBuffers(); | |
45 FinishSwapBuffers(std::move(latency_info), result); | |
46 return result; | |
47 } | |
48 | |
49 void PassThroughImageTransportSurface::SwapBuffersAsync( | |
50 const GLSurface::SwapCompletionCallback& callback) { | |
51 scoped_ptr<std::vector<ui::LatencyInfo>> latency_info = StartSwapBuffers(); | |
52 | |
53 // We use WeakPtr here to avoid manual management of life time of an instance | |
54 // of this class. Callback will not be called once the instance of this class | |
55 // is destroyed. However, this also means that the callback can be run on | |
56 // the calling thread only. | |
57 gfx::GLSurfaceAdapter::SwapBuffersAsync(base::Bind( | |
58 &PassThroughImageTransportSurface::FinishSwapBuffersAsync, | |
59 weak_ptr_factory_.GetWeakPtr(), base::Passed(&latency_info), callback)); | |
60 } | |
61 | |
62 gfx::SwapResult PassThroughImageTransportSurface::PostSubBuffer(int x, | |
63 int y, | |
64 int width, | |
65 int height) { | |
66 scoped_ptr<std::vector<ui::LatencyInfo>> latency_info = StartSwapBuffers(); | |
67 gfx::SwapResult result = | |
68 gfx::GLSurfaceAdapter::PostSubBuffer(x, y, width, height); | |
69 FinishSwapBuffers(std::move(latency_info), result); | |
70 return result; | |
71 } | |
72 | |
73 void PassThroughImageTransportSurface::PostSubBufferAsync( | |
74 int x, | |
75 int y, | |
76 int width, | |
77 int height, | |
78 const GLSurface::SwapCompletionCallback& callback) { | |
79 scoped_ptr<std::vector<ui::LatencyInfo>> latency_info = StartSwapBuffers(); | |
80 gfx::GLSurfaceAdapter::PostSubBufferAsync( | |
81 x, y, width, height, | |
82 base::Bind(&PassThroughImageTransportSurface::FinishSwapBuffersAsync, | |
83 weak_ptr_factory_.GetWeakPtr(), base::Passed(&latency_info), | |
84 callback)); | |
85 } | |
86 | |
87 gfx::SwapResult PassThroughImageTransportSurface::CommitOverlayPlanes() { | |
88 scoped_ptr<std::vector<ui::LatencyInfo>> latency_info = StartSwapBuffers(); | |
89 gfx::SwapResult result = gfx::GLSurfaceAdapter::CommitOverlayPlanes(); | |
90 FinishSwapBuffers(std::move(latency_info), result); | |
91 return result; | |
92 } | |
93 | |
94 void PassThroughImageTransportSurface::CommitOverlayPlanesAsync( | |
95 const GLSurface::SwapCompletionCallback& callback) { | |
96 scoped_ptr<std::vector<ui::LatencyInfo>> latency_info = StartSwapBuffers(); | |
97 gfx::GLSurfaceAdapter::CommitOverlayPlanesAsync(base::Bind( | |
98 &PassThroughImageTransportSurface::FinishSwapBuffersAsync, | |
99 weak_ptr_factory_.GetWeakPtr(), base::Passed(&latency_info), callback)); | |
100 } | |
101 | |
102 bool PassThroughImageTransportSurface::OnMakeCurrent(gfx::GLContext* context) { | |
103 if (!did_set_swap_interval_) { | |
104 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | |
105 switches::kDisableGpuVsync)) | |
106 context->ForceSwapIntervalZero(true); | |
107 else | |
108 context->SetSwapInterval(1); | |
109 did_set_swap_interval_ = true; | |
110 } | |
111 return true; | |
112 } | |
113 | |
114 PassThroughImageTransportSurface::~PassThroughImageTransportSurface() { | |
115 if (stub_.get()) { | |
116 stub_->SetLatencyInfoCallback( | |
117 base::Callback<void(const std::vector<ui::LatencyInfo>&)>()); | |
118 } | |
119 } | |
120 | |
121 void PassThroughImageTransportSurface::SetLatencyInfo( | |
122 const std::vector<ui::LatencyInfo>& latency_info) { | |
123 latency_info_.insert(latency_info_.end(), latency_info.begin(), | |
124 latency_info.end()); | |
125 } | |
126 | |
127 void PassThroughImageTransportSurface::SendVSyncUpdateIfAvailable() { | |
128 gfx::VSyncProvider* vsync_provider = GetVSyncProvider(); | |
129 if (vsync_provider) { | |
130 vsync_provider->GetVSyncParameters(base::Bind( | |
131 &GpuCommandBufferStub::SendUpdateVSyncParameters, stub_->AsWeakPtr())); | |
132 } | |
133 } | |
134 | |
135 scoped_ptr<std::vector<ui::LatencyInfo>> | |
136 PassThroughImageTransportSurface::StartSwapBuffers() { | |
137 // GetVsyncValues before SwapBuffers to work around Mali driver bug: | |
138 // crbug.com/223558. | |
139 SendVSyncUpdateIfAvailable(); | |
140 | |
141 base::TimeTicks swap_time = base::TimeTicks::Now(); | |
142 for (auto& latency : latency_info_) { | |
143 latency.AddLatencyNumberWithTimestamp( | |
144 ui::INPUT_EVENT_GPU_SWAP_BUFFER_COMPONENT, 0, 0, swap_time, 1); | |
145 } | |
146 | |
147 scoped_ptr<std::vector<ui::LatencyInfo>> latency_info( | |
148 new std::vector<ui::LatencyInfo>()); | |
149 latency_info->swap(latency_info_); | |
150 | |
151 return latency_info; | |
152 } | |
153 | |
154 void PassThroughImageTransportSurface::FinishSwapBuffers( | |
155 scoped_ptr<std::vector<ui::LatencyInfo>> latency_info, | |
156 gfx::SwapResult result) { | |
157 base::TimeTicks swap_ack_time = base::TimeTicks::Now(); | |
158 for (auto& latency : *latency_info) { | |
159 latency.AddLatencyNumberWithTimestamp( | |
160 ui::INPUT_EVENT_LATENCY_TERMINATED_FRAME_SWAP_COMPONENT, 0, 0, | |
161 swap_ack_time, 1); | |
162 } | |
163 | |
164 stub_->SendSwapBuffersCompleted(*latency_info, result); | |
165 } | |
166 | |
167 void PassThroughImageTransportSurface::FinishSwapBuffersAsync( | |
168 scoped_ptr<std::vector<ui::LatencyInfo>> latency_info, | |
169 GLSurface::SwapCompletionCallback callback, | |
170 gfx::SwapResult result) { | |
171 FinishSwapBuffers(std::move(latency_info), result); | |
172 callback.Run(result); | |
173 } | |
174 | |
175 } // namespace content | |
OLD | NEW |