OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "media/capture/content/thread_safe_capture_oracle.h" | 5 #include "media/capture/content/thread_safe_capture_oracle.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <memory> | 9 #include <memory> |
10 #include <utility> | 10 #include <utility> |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
54 VideoCaptureOracle::Event event, | 54 VideoCaptureOracle::Event event, |
55 const gfx::Rect& damage_rect, | 55 const gfx::Rect& damage_rect, |
56 base::TimeTicks event_time, | 56 base::TimeTicks event_time, |
57 scoped_refptr<VideoFrame>* storage, | 57 scoped_refptr<VideoFrame>* storage, |
58 CaptureFrameCallback* callback) { | 58 CaptureFrameCallback* callback) { |
59 // Grab the current time before waiting to acquire the |lock_|. | 59 // Grab the current time before waiting to acquire the |lock_|. |
60 const base::TimeTicks capture_begin_time = base::TimeTicks::Now(); | 60 const base::TimeTicks capture_begin_time = base::TimeTicks::Now(); |
61 | 61 |
62 gfx::Size visible_size; | 62 gfx::Size visible_size; |
63 gfx::Size coded_size; | 63 gfx::Size coded_size; |
64 media::VideoCaptureDevice::Client::Buffer output_buffer; | 64 std::unique_ptr<media::VideoCaptureDevice::Client::Buffer> output_buffer; |
65 double attenuated_utilization; | 65 double attenuated_utilization; |
66 int frame_number; | 66 int frame_number; |
67 base::TimeDelta estimated_frame_duration; | 67 base::TimeDelta estimated_frame_duration; |
68 { | 68 { |
69 base::AutoLock guard(lock_); | 69 base::AutoLock guard(lock_); |
70 | 70 |
71 if (!client_) | 71 if (!client_) |
72 return false; // Capture is stopped. | 72 return false; // Capture is stopped. |
73 | 73 |
74 if (!oracle_.ObserveEventAndDecideCapture(event, damage_rect, event_time)) { | 74 if (!oracle_.ObserveEventAndDecideCapture(event, damage_rect, event_time)) { |
(...skipping 11 matching lines...) Expand all Loading... |
86 // TODO(miu): Clients should request exact padding, instead of this | 86 // TODO(miu): Clients should request exact padding, instead of this |
87 // memory-wasting hack to make frames that are compatible with all HW | 87 // memory-wasting hack to make frames that are compatible with all HW |
88 // encoders. http://crbug.com/555911 | 88 // encoders. http://crbug.com/555911 |
89 coded_size.SetSize(base::bits::Align(visible_size.width(), 16), | 89 coded_size.SetSize(base::bits::Align(visible_size.width(), 16), |
90 base::bits::Align(visible_size.height(), 16)); | 90 base::bits::Align(visible_size.height(), 16)); |
91 | 91 |
92 if (event == VideoCaptureOracle::kPassiveRefreshRequest) { | 92 if (event == VideoCaptureOracle::kPassiveRefreshRequest) { |
93 output_buffer = client_->ResurrectLastOutputBuffer( | 93 output_buffer = client_->ResurrectLastOutputBuffer( |
94 coded_size, params_.requested_format.pixel_format, | 94 coded_size, params_.requested_format.pixel_format, |
95 params_.requested_format.pixel_storage, frame_number); | 95 params_.requested_format.pixel_storage, frame_number); |
96 if (!output_buffer.is_valid()) { | 96 if (!output_buffer) { |
97 TRACE_EVENT_INSTANT0("gpu.capture", "ResurrectionFailed", | 97 TRACE_EVENT_INSTANT0("gpu.capture", "ResurrectionFailed", |
98 TRACE_EVENT_SCOPE_THREAD); | 98 TRACE_EVENT_SCOPE_THREAD); |
99 return false; | 99 return false; |
100 } | 100 } |
101 } else { | 101 } else { |
102 output_buffer = client_->ReserveOutputBuffer( | 102 output_buffer = client_->ReserveOutputBuffer( |
103 coded_size, params_.requested_format.pixel_format, | 103 coded_size, params_.requested_format.pixel_format, |
104 params_.requested_format.pixel_storage, frame_number); | 104 params_.requested_format.pixel_storage, frame_number); |
105 } | 105 } |
106 | 106 |
107 // Get the current buffer pool utilization and attenuate it: The utilization | 107 // Get the current buffer pool utilization and attenuate it: The utilization |
108 // reported to the oracle is in terms of a maximum sustainable amount (not | 108 // reported to the oracle is in terms of a maximum sustainable amount (not |
109 // the absolute maximum). | 109 // the absolute maximum). |
110 attenuated_utilization = client_->GetBufferPoolUtilization() * | 110 attenuated_utilization = client_->GetBufferPoolUtilization() * |
111 (100.0 / kTargetMaxPoolUtilizationPercent); | 111 (100.0 / kTargetMaxPoolUtilizationPercent); |
112 | 112 |
113 if (!output_buffer.is_valid()) { | 113 if (!output_buffer) { |
114 TRACE_EVENT_INSTANT2( | 114 TRACE_EVENT_INSTANT2( |
115 "gpu.capture", "PipelineLimited", TRACE_EVENT_SCOPE_THREAD, "trigger", | 115 "gpu.capture", "PipelineLimited", TRACE_EVENT_SCOPE_THREAD, "trigger", |
116 VideoCaptureOracle::EventAsString(event), "atten_util_percent", | 116 VideoCaptureOracle::EventAsString(event), "atten_util_percent", |
117 base::saturated_cast<int>(attenuated_utilization * 100.0 + 0.5)); | 117 base::saturated_cast<int>(attenuated_utilization * 100.0 + 0.5)); |
118 oracle_.RecordWillNotCapture(attenuated_utilization); | 118 oracle_.RecordWillNotCapture(attenuated_utilization); |
119 return false; | 119 return false; |
120 } | 120 } |
121 | 121 |
122 oracle_.RecordCapture(attenuated_utilization); | 122 oracle_.RecordCapture(attenuated_utilization); |
123 estimated_frame_duration = oracle_.estimated_frame_duration(); | 123 estimated_frame_duration = oracle_.estimated_frame_duration(); |
124 } // End of critical section. | 124 } // End of critical section. |
125 | 125 |
126 if (attenuated_utilization >= 1.0) { | 126 if (attenuated_utilization >= 1.0) { |
127 TRACE_EVENT_INSTANT2( | 127 TRACE_EVENT_INSTANT2( |
128 "gpu.capture", "NearlyPipelineLimited", TRACE_EVENT_SCOPE_THREAD, | 128 "gpu.capture", "NearlyPipelineLimited", TRACE_EVENT_SCOPE_THREAD, |
129 "trigger", VideoCaptureOracle::EventAsString(event), | 129 "trigger", VideoCaptureOracle::EventAsString(event), |
130 "atten_util_percent", | 130 "atten_util_percent", |
131 base::saturated_cast<int>(attenuated_utilization * 100.0 + 0.5)); | 131 base::saturated_cast<int>(attenuated_utilization * 100.0 + 0.5)); |
132 } | 132 } |
133 | 133 |
134 TRACE_EVENT_ASYNC_BEGIN2("gpu.capture", "Capture", output_buffer.id(), | 134 TRACE_EVENT_ASYNC_BEGIN2("gpu.capture", "Capture", output_buffer.get(), |
135 "frame_number", frame_number, "trigger", | 135 "frame_number", frame_number, "trigger", |
136 VideoCaptureOracle::EventAsString(event)); | 136 VideoCaptureOracle::EventAsString(event)); |
137 | 137 |
138 auto output_buffer_access = | |
139 output_buffer.handle_provider()->GetHandleForInProcessAccess(); | |
140 DCHECK_EQ(media::PIXEL_STORAGE_CPU, params_.requested_format.pixel_storage); | 138 DCHECK_EQ(media::PIXEL_STORAGE_CPU, params_.requested_format.pixel_storage); |
141 *storage = VideoFrame::WrapExternalSharedMemory( | 139 *storage = VideoFrame::WrapExternalSharedMemory( |
142 params_.requested_format.pixel_format, coded_size, | 140 params_.requested_format.pixel_format, coded_size, |
143 gfx::Rect(visible_size), visible_size, output_buffer_access->data(), | 141 gfx::Rect(visible_size), visible_size, |
144 output_buffer_access->mapped_size(), base::SharedMemory::NULLHandle(), 0u, | 142 static_cast<uint8_t*>(output_buffer->data()), |
| 143 output_buffer->mapped_size(), base::SharedMemory::NULLHandle(), 0u, |
145 base::TimeDelta()); | 144 base::TimeDelta()); |
146 // If creating the VideoFrame wrapper failed, call DidCaptureFrame() with | 145 // If creating the VideoFrame wrapper failed, call DidCaptureFrame() with |
147 // !success to execute the required post-capture steps (tracing, notification | 146 // !success to execute the required post-capture steps (tracing, notification |
148 // of failure to VideoCaptureOracle, etc.). | 147 // of failure to VideoCaptureOracle, etc.). |
149 if (!(*storage)) { | 148 if (!(*storage)) { |
150 DidCaptureFrame(frame_number, std::move(output_buffer), capture_begin_time, | 149 DidCaptureFrame(frame_number, std::move(output_buffer), capture_begin_time, |
151 estimated_frame_duration, *storage, event_time, false); | 150 estimated_frame_duration, *storage, event_time, false); |
152 return false; | 151 return false; |
153 } | 152 } |
154 | 153 |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
193 void ThreadSafeCaptureOracle::ReportError( | 192 void ThreadSafeCaptureOracle::ReportError( |
194 const tracked_objects::Location& from_here, | 193 const tracked_objects::Location& from_here, |
195 const std::string& reason) { | 194 const std::string& reason) { |
196 base::AutoLock guard(lock_); | 195 base::AutoLock guard(lock_); |
197 if (client_) | 196 if (client_) |
198 client_->OnError(from_here, reason); | 197 client_->OnError(from_here, reason); |
199 } | 198 } |
200 | 199 |
201 void ThreadSafeCaptureOracle::DidCaptureFrame( | 200 void ThreadSafeCaptureOracle::DidCaptureFrame( |
202 int frame_number, | 201 int frame_number, |
203 VideoCaptureDevice::Client::Buffer buffer, | 202 std::unique_ptr<VideoCaptureDevice::Client::Buffer> buffer, |
204 base::TimeTicks capture_begin_time, | 203 base::TimeTicks capture_begin_time, |
205 base::TimeDelta estimated_frame_duration, | 204 base::TimeDelta estimated_frame_duration, |
206 scoped_refptr<VideoFrame> frame, | 205 scoped_refptr<VideoFrame> frame, |
207 base::TimeTicks reference_time, | 206 base::TimeTicks reference_time, |
208 bool success) { | 207 bool success) { |
209 TRACE_EVENT_ASYNC_END2("gpu.capture", "Capture", buffer.id(), "success", | 208 TRACE_EVENT_ASYNC_END2("gpu.capture", "Capture", buffer.get(), "success", |
210 success, "timestamp", | 209 success, "timestamp", |
211 reference_time.ToInternalValue()); | 210 reference_time.ToInternalValue()); |
212 | 211 |
213 base::AutoLock guard(lock_); | 212 base::AutoLock guard(lock_); |
214 | 213 |
215 if (!oracle_.CompleteCapture(frame_number, success, &reference_time)) | 214 if (!oracle_.CompleteCapture(frame_number, success, &reference_time)) |
216 return; | 215 return; |
217 | 216 |
218 TRACE_EVENT_INSTANT0("gpu.capture", "CaptureSucceeded", | 217 TRACE_EVENT_INSTANT0("gpu.capture", "CaptureSucceeded", |
219 TRACE_EVENT_SCOPE_THREAD); | 218 TRACE_EVENT_SCOPE_THREAD); |
(...skipping 22 matching lines...) Expand all Loading... |
242 } | 241 } |
243 | 242 |
244 void ThreadSafeCaptureOracle::OnConsumerReportingUtilization( | 243 void ThreadSafeCaptureOracle::OnConsumerReportingUtilization( |
245 int frame_number, | 244 int frame_number, |
246 double utilization) { | 245 double utilization) { |
247 base::AutoLock guard(lock_); | 246 base::AutoLock guard(lock_); |
248 oracle_.RecordConsumerFeedback(frame_number, utilization); | 247 oracle_.RecordConsumerFeedback(frame_number, utilization); |
249 } | 248 } |
250 | 249 |
251 } // namespace media | 250 } // namespace media |
OLD | NEW |