Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(101)

Side by Side Diff: remoting/client/plugin/pepper_video_renderer_2d.cc

Issue 1288063004: Simplify FrameConsumer interface. Remove FrameProducer interface. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 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 "remoting/client/plugin/pepper_video_renderer_2d.h" 5 #include "remoting/client/plugin/pepper_video_renderer_2d.h"
6 6
7 #include <functional>
8
9 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/callback_helpers.h"
10 #include "base/strings/string_util.h" 9 #include "base/strings/string_util.h"
11 #include "base/synchronization/waitable_event.h" 10 #include "base/task_runner_util.h"
12 #include "base/time/time.h"
13 #include "ppapi/cpp/completion_callback.h" 11 #include "ppapi/cpp/completion_callback.h"
14 #include "ppapi/cpp/image_data.h" 12 #include "ppapi/cpp/image_data.h"
15 #include "ppapi/cpp/instance.h" 13 #include "ppapi/cpp/instance.h"
16 #include "ppapi/cpp/point.h" 14 #include "ppapi/cpp/point.h"
17 #include "ppapi/cpp/rect.h" 15 #include "ppapi/cpp/rect.h"
18 #include "ppapi/cpp/size.h" 16 #include "ppapi/cpp/size.h"
19 #include "remoting/base/util.h" 17 #include "remoting/base/util.h"
20 #include "remoting/client/chromoting_stats.h" 18 #include "remoting/client/chromoting_stats.h"
21 #include "remoting/client/client_context.h" 19 #include "remoting/client/client_context.h"
22 #include "remoting/client/frame_consumer_proxy.h"
23 #include "remoting/client/frame_producer.h"
24 #include "remoting/client/software_video_renderer.h" 20 #include "remoting/client/software_video_renderer.h"
25 #include "remoting/proto/video.pb.h" 21 #include "remoting/proto/video.pb.h"
22 #include "third_party/libyuv/include/libyuv/scale_argb.h"
26 #include "third_party/webrtc/modules/desktop_capture/desktop_frame.h" 23 #include "third_party/webrtc/modules/desktop_capture/desktop_frame.h"
27 24
25 namespace remoting {
26
28 namespace { 27 namespace {
29 28
30 // DesktopFrame that wraps a supplied pp::ImageData 29 // DesktopFrame that wraps a supplied pp::ImageData
31 class PepperDesktopFrame : public webrtc::DesktopFrame { 30 class PepperDesktopFrame : public webrtc::DesktopFrame {
32 public: 31 public:
33 // Wraps the supplied ImageData. 32 // Wraps the supplied ImageData.
34 explicit PepperDesktopFrame(const pp::ImageData& buffer); 33 explicit PepperDesktopFrame(const pp::ImageData& buffer)
34 : DesktopFrame(
35 webrtc::DesktopSize(buffer.size().width(), buffer.size().height()),
36 buffer.stride(),
37 reinterpret_cast<uint8_t*>(buffer.data()),
38 nullptr),
39 buffer_(buffer) {}
35 40
36 // Access to underlying pepper representation. 41 // Access to underlying pepper representation.
37 const pp::ImageData& buffer() const { 42 const pp::ImageData& buffer() const {
38 return buffer_; 43 return buffer_;
39 } 44 }
40 45
41 private: 46 private:
42 pp::ImageData buffer_; 47 pp::ImageData buffer_;
43 }; 48 };
44 49
45 PepperDesktopFrame::PepperDesktopFrame(const pp::ImageData& buffer)
46 : DesktopFrame(webrtc::DesktopSize(buffer.size().width(),
47 buffer.size().height()),
48 buffer.stride(),
49 reinterpret_cast<uint8_t*>(buffer.data()),
50 nullptr),
51 buffer_(buffer) {}
52
53 } // namespace
54
55 namespace remoting {
56
57 namespace {
58
59 // The maximum number of image buffers to be allocated at any point of time.
60 const size_t kMaxPendingBuffersCount = 2;
61
62 } // namespace 50 } // namespace
63 51
64 PepperVideoRenderer2D::PepperVideoRenderer2D() 52 PepperVideoRenderer2D::PepperVideoRenderer2D()
65 : instance_(nullptr), 53 : callback_factory_(this),
66 event_handler_(nullptr), 54 weak_factory_(this) {}
67 merge_buffer_(nullptr),
68 dips_to_device_scale_(1.0f),
69 dips_to_view_scale_(1.0f),
70 flush_pending_(false),
71 frame_received_(false),
72 debug_dirty_region_(false),
73 callback_factory_(this),
74 weak_factory_(this) {
75 }
76 55
77 PepperVideoRenderer2D::~PepperVideoRenderer2D() { 56 PepperVideoRenderer2D::~PepperVideoRenderer2D() {}
78 // The producer should now return any pending buffers. At this point, however,
79 // ReturnBuffer() tasks scheduled by the producer will not be delivered,
80 // so we free all the buffers once the producer's queue is empty.
81 base::WaitableEvent done_event(true, false);
82 software_video_renderer_->RequestReturnBuffers(
83 base::Bind(&base::WaitableEvent::Signal, base::Unretained(&done_event)));
84 done_event.Wait();
85
86 merge_buffer_ = nullptr;
87 while (!buffers_.empty()) {
88 FreeBuffer(buffers_.front());
89 }
90 }
91 57
92 bool PepperVideoRenderer2D::Initialize(pp::Instance* instance, 58 bool PepperVideoRenderer2D::Initialize(pp::Instance* instance,
93 const ClientContext& context, 59 const ClientContext& context,
94 EventHandler* event_handler) { 60 EventHandler* event_handler) {
95 DCHECK(CalledOnValidThread()); 61 DCHECK(thread_checker_.CalledOnValidThread());
96 DCHECK(!instance_); 62 DCHECK(!instance_);
97 DCHECK(!event_handler_); 63 DCHECK(!event_handler_);
98 DCHECK(instance); 64 DCHECK(instance);
99 DCHECK(event_handler); 65 DCHECK(event_handler);
100 66
101 instance_ = instance; 67 instance_ = instance;
102 event_handler_ = event_handler; 68 event_handler_ = event_handler;
103 scoped_ptr<FrameConsumerProxy> frame_consumer_proxy = 69 software_video_renderer_.reset(
104 make_scoped_ptr(new FrameConsumerProxy(weak_factory_.GetWeakPtr())); 70 new SoftwareVideoRenderer(context.decode_task_runner(), this));
105 software_video_renderer_.reset(new SoftwareVideoRenderer(
106 context.main_task_runner(), context.decode_task_runner(),
107 frame_consumer_proxy.Pass()));
108 71
109 return true; 72 return true;
110 } 73 }
111 74
112 void PepperVideoRenderer2D::OnViewChanged(const pp::View& view) { 75 void PepperVideoRenderer2D::OnViewChanged(const pp::View& view) {
113 DCHECK(CalledOnValidThread()); 76 DCHECK(thread_checker_.CalledOnValidThread());
114
115 bool view_changed = false;
116 77
117 pp::Rect pp_size = view.GetRect(); 78 pp::Rect pp_size = view.GetRect();
118 webrtc::DesktopSize new_dips_size(pp_size.width(), pp_size.height()); 79 view_size_ = webrtc::DesktopSize(pp_size.width(), pp_size.height());
119 float new_dips_to_device_scale = view.GetDeviceScale();
120 80
121 if (!dips_size_.equals(new_dips_size) || 81 // Update scale if graphics2d has been initialized.
122 dips_to_device_scale_ != new_dips_to_device_scale) { 82 if (!graphics2d_.is_null() && source_size_.width() > 0) {
123 view_changed = true; 83 graphics2d_.SetScale(static_cast<float>(view_size_.width()) /
124 dips_to_device_scale_ = new_dips_to_device_scale; 84 source_size_.width());
125 dips_size_ = new_dips_size;
126 85
127 // If |dips_to_device_scale_| is > 1.0 then the device is high-DPI, and 86 // Bind graphics2d_ again after changing the scale to work around
128 // there are actually |view_device_scale_| physical pixels for every one 87 // crbug.com/521745 .
129 // Density Independent Pixel (DIP). If we specify a scale of 1.0 to 88 instance_->BindGraphics(graphics2d_);
130 // Graphics2D then we can render at DIP resolution and let PPAPI up-scale
131 // for high-DPI devices.
132 dips_to_view_scale_ = 1.0f;
133 view_size_ = dips_size_;
134
135 // If the view's DIP dimensions don't match the source then let the frame
136 // producer do the scaling, and render at device resolution.
137 if (!dips_size_.equals(source_size_)) {
138 dips_to_view_scale_ = dips_to_device_scale_;
139 view_size_.set(ceilf(dips_size_.width() * dips_to_view_scale_),
140 ceilf(dips_size_.height() * dips_to_view_scale_));
141 }
142
143 // Create a 2D rendering context at the chosen frame dimensions.
144 pp::Size pp_size = pp::Size(view_size_.width(), view_size_.height());
145 graphics2d_ = pp::Graphics2D(instance_, pp_size, false);
146
147 // Specify the scale from our coordinates to DIPs.
148 graphics2d_.SetScale(1.0f / dips_to_view_scale_);
149
150 bool result = instance_->BindGraphics(graphics2d_); 89 bool result = instance_->BindGraphics(graphics2d_);
151
152 // There is no good way to handle this error currently.
153 DCHECK(result) << "Couldn't bind the device context."; 90 DCHECK(result) << "Couldn't bind the device context.";
154 } 91 }
155
156 // Ignore clip rectangle provided by the browser because it may not be
157 // correct. See crbug.com/360240 . In case when the plugin is not visible
158 // (e.g. another tab is selected) |clip_area_| is set to empty rectangle,
159 // otherwise it's set to a rectangle that covers the whole plugin.
160 //
161 // TODO(sergeyu): Use view.GetClipRect() here after bug 360240 is fixed.
162 webrtc::DesktopRect new_clip =
163 view.IsVisible() ? webrtc::DesktopRect::MakeWH(
164 ceilf(pp_size.width() * dips_to_view_scale_),
165 ceilf(pp_size.height() * dips_to_view_scale_))
166 : webrtc::DesktopRect();
167 if (!clip_area_.equals(new_clip)) {
168 view_changed = true;
169
170 // YUV to RGB conversion may require even X and Y coordinates for
171 // the top left corner of the clipping area.
172 clip_area_ = AlignRect(new_clip);
173 clip_area_.IntersectWith(webrtc::DesktopRect::MakeSize(view_size_));
174 }
175
176 if (view_changed) {
177 software_video_renderer_->SetOutputSizeAndClip(view_size_, clip_area_);
178 AllocateBuffers();
179 }
180 } 92 }
181 93
182 void PepperVideoRenderer2D::EnableDebugDirtyRegion(bool enable) { 94 void PepperVideoRenderer2D::EnableDebugDirtyRegion(bool enable) {
183 debug_dirty_region_ = enable; 95 debug_dirty_region_ = enable;
184 } 96 }
185 97
186 void PepperVideoRenderer2D::OnSessionConfig( 98 void PepperVideoRenderer2D::OnSessionConfig(
187 const protocol::SessionConfig& config) { 99 const protocol::SessionConfig& config) {
188 DCHECK(CalledOnValidThread()); 100 DCHECK(thread_checker_.CalledOnValidThread());
189 101
190 software_video_renderer_->OnSessionConfig(config); 102 software_video_renderer_->OnSessionConfig(config);
191 AllocateBuffers();
192 } 103 }
193 104
194 ChromotingStats* PepperVideoRenderer2D::GetStats() { 105 ChromotingStats* PepperVideoRenderer2D::GetStats() {
195 DCHECK(CalledOnValidThread()); 106 DCHECK(thread_checker_.CalledOnValidThread());
196 107
197 return software_video_renderer_->GetStats(); 108 return software_video_renderer_->GetStats();
198 } 109 }
199 110
200 protocol::VideoStub* PepperVideoRenderer2D::GetVideoStub() { 111 protocol::VideoStub* PepperVideoRenderer2D::GetVideoStub() {
201 DCHECK(CalledOnValidThread()); 112 DCHECK(thread_checker_.CalledOnValidThread());
202 113
203 return software_video_renderer_->GetVideoStub(); 114 return software_video_renderer_->GetVideoStub();
204 } 115 }
205 116
206 void PepperVideoRenderer2D::ApplyBuffer(const webrtc::DesktopSize& view_size, 117 scoped_ptr<webrtc::DesktopFrame> PepperVideoRenderer2D::AllocateFrame(
207 const webrtc::DesktopRect& clip_area, 118 const webrtc::DesktopSize& size) {
208 webrtc::DesktopFrame* buffer, 119 DCHECK(thread_checker_.CalledOnValidThread());
209 const webrtc::DesktopRegion& region, 120
210 const webrtc::DesktopRegion* shape) { 121 pp::ImageData buffer_data(instance_, PP_IMAGEDATAFORMAT_BGRA_PREMUL,
211 DCHECK(CalledOnValidThread()); 122 pp::Size(size.width(), size.height()), false);
123 return make_scoped_ptr(new PepperDesktopFrame(buffer_data));
124 }
125
126 void PepperVideoRenderer2D::DrawFrame(scoped_ptr<webrtc::DesktopFrame> frame,
127 const base::Closure& done) {
128 DCHECK(thread_checker_.CalledOnValidThread());
212 129
213 if (!frame_received_) { 130 if (!frame_received_) {
214 event_handler_->OnVideoFirstFrameReceived(); 131 event_handler_->OnVideoFirstFrameReceived();
215 frame_received_ = true; 132 frame_received_ = true;
216 } 133 }
217 // We cannot use the data in the buffer if its dimensions don't match the 134
218 // current view size. 135 bool size_changed = !source_size_.equals(frame->size());
219 if (!view_size_.equals(view_size)) { 136 if (size_changed) {
220 FreeBuffer(buffer); 137 source_size_ = frame->size();
221 AllocateBuffers(); 138
222 } else { 139 // Create a 2D rendering context with the new dimensions.
223 FlushBuffer(clip_area, buffer, region); 140 graphics2d_ = pp::Graphics2D(
224 if (shape) { 141 instance_, pp::Size(source_size_.width(), source_size_.height()), true);
225 if (!source_shape_ || !source_shape_->Equals(*shape)) { 142 graphics2d_.SetScale(static_cast<float>(view_size_.width()) /
226 source_shape_ = make_scoped_ptr(new webrtc::DesktopRegion(*shape)); 143 source_size_.width());
227 event_handler_->OnVideoShape(source_shape_.get()); 144 bool result = instance_->BindGraphics(graphics2d_);
228 } 145 DCHECK(result) << "Couldn't bind the device context.";
229 } else if (source_shape_) { 146 }
230 source_shape_ = nullptr; 147
231 event_handler_->OnVideoShape(nullptr); 148
149 if (size_changed || !source_dpi_.equals(frame->dpi())) {
150 source_dpi_ = frame->dpi();
151
152 // Notify JavaScript of the change in source size.
153 event_handler_->OnVideoSize(source_size_, source_dpi_);
154 }
155
156 const webrtc::DesktopRegion* shape = frame->shape();
157 if (shape) {
158 if (!source_shape_ || !source_shape_->Equals(*shape)) {
159 source_shape_ = make_scoped_ptr(new webrtc::DesktopRegion(*shape));
160 event_handler_->OnVideoShape(source_shape_.get());
232 } 161 }
162 } else if (source_shape_) {
163 source_shape_ = nullptr;
164 event_handler_->OnVideoShape(nullptr);
233 } 165 }
234 }
235 166
236 void PepperVideoRenderer2D::ReturnBuffer(webrtc::DesktopFrame* buffer) { 167 // If Debug dirty region is enabled then emit it.
237 DCHECK(CalledOnValidThread()); 168 if (debug_dirty_region_)
169 event_handler_->OnVideoFrameDirtyRegion(frame->updated_region());
238 170
239 // Reuse the buffer if it is large enough, otherwise drop it on the floor 171 const pp::ImageData& image_data =
240 // and allocate a new one. 172 static_cast<PepperDesktopFrame*>(frame.get())->buffer();
241 if (buffer->size().width() >= clip_area_.width() && 173 for (webrtc::DesktopRegion::Iterator i(frame->updated_region()); !i.IsAtEnd();
242 buffer->size().height() >= clip_area_.height()) { 174 i.Advance()) {
243 software_video_renderer_->DrawBuffer(buffer); 175 graphics2d_.PaintImageData(image_data, pp::Point(0, 0),
244 } else { 176 pp::Rect(i.rect().left(), i.rect().top(),
245 FreeBuffer(buffer); 177 i.rect().width(), i.rect().height()));
246 AllocateBuffers();
247 } 178 }
248 }
249 179
250 void PepperVideoRenderer2D::SetSourceSize( 180 if (!done.is_null()) {
251 const webrtc::DesktopSize& source_size, 181 pending_frames_done_callbacks_.push_back(
252 const webrtc::DesktopVector& source_dpi) { 182 new base::ScopedClosureRunner(done));
253 DCHECK(CalledOnValidThread()); 183 }
254 184
255 if (source_size_.equals(source_size) && source_dpi_.equals(source_dpi)) 185 need_flush_ = true;
256 return;
257 186
258 source_size_ = source_size; 187 Flush();
259 source_dpi_ = source_dpi;
260
261 // Notify JavaScript of the change in source size.
262 event_handler_->OnVideoSize(source_size, source_dpi);
263 } 188 }
264 189
265 FrameConsumer::PixelFormat PepperVideoRenderer2D::GetPixelFormat() { 190 FrameConsumer::PixelFormat PepperVideoRenderer2D::GetPixelFormat() {
266 return FORMAT_BGRA; 191 return FORMAT_BGRA;
267 } 192 }
268 193
269 void PepperVideoRenderer2D::AllocateBuffers() { 194 void PepperVideoRenderer2D::Flush() {
270 if (clip_area_.width() == 0 || clip_area_.height() == 0) 195 DCHECK(thread_checker_.CalledOnValidThread());
196
197 if (flush_pending_ || !need_flush_)
271 return; 198 return;
272 199
273 while (buffers_.size() < kMaxPendingBuffersCount) { 200 need_flush_ = false;
274 // Create an image buffer of the required size, but don't zero it.
275 pp::ImageData buffer_data(instance_, PP_IMAGEDATAFORMAT_BGRA_PREMUL,
276 pp::Size(clip_area_.width(), clip_area_.height()),
277 false);
278 if (buffer_data.is_null()) {
279 LOG(WARNING) << "Not enough memory for frame buffers.";
280 break;
281 }
282 201
283 webrtc::DesktopFrame* buffer = new PepperDesktopFrame(buffer_data); 202 // Move callbacks from |pending_frames_done_callbacks_| to
284 buffers_.push_back(buffer); 203 // |flushing_frames_done_callbacks_| so the callbacks are called when flush is
285 software_video_renderer_->DrawBuffer(buffer); 204 // finished.
286 } 205 flushing_frames_done_callbacks_ = pending_frames_done_callbacks_.Pass();
Jamie 2015/08/19 20:52:10 Add DCHECK(flushing_frames_don_callbacks_.empty())
Sergey Ulanov 2015/08/19 23:37:13 Done.
206
207 // Flush the updated areas to the screen.
208 int error = graphics2d_.Flush(
209 callback_factory_.NewCallback(&PepperVideoRenderer2D::OnFlushDone));
210 CHECK(error == PP_OK_COMPLETIONPENDING);
211 flush_pending_ = true;
287 } 212 }
288 213
289 void PepperVideoRenderer2D::FreeBuffer(webrtc::DesktopFrame* buffer) { 214 void PepperVideoRenderer2D::OnFlushDone(int result) {
290 DCHECK(std::find(buffers_.begin(), buffers_.end(), buffer) != buffers_.end()); 215 DCHECK(thread_checker_.CalledOnValidThread());
291 216
292 buffers_.remove(buffer); 217 DCHECK(flush_pending_);
293 delete buffer; 218 flush_pending_ = false;
294 }
295 219
296 void PepperVideoRenderer2D::FlushBuffer(const webrtc::DesktopRect& clip_area, 220 // Call all callbacks for the frames we've just flushed.
297 webrtc::DesktopFrame* buffer, 221 flushing_frames_done_callbacks_.clear();
298 const webrtc::DesktopRegion& region) {
299 // Defer drawing if the flush is already in progress.
300 if (flush_pending_) {
301 // |merge_buffer_| is guaranteed to be free here because we allocate only
302 // two buffers simultaneously. If more buffers are allowed this code should
303 // apply all pending changes to the screen.
304 DCHECK(merge_buffer_ == nullptr);
305 222
306 merge_clip_area_ = clip_area; 223 // Flush again if necessary.
307 merge_buffer_ = buffer; 224 Flush();
308 merge_region_ = region;
309 return;
310 }
311
312 // Notify Pepper API about the updated areas and flush pixels to the screen.
313 base::Time start_time = base::Time::Now();
314
315 for (webrtc::DesktopRegion::Iterator i(region); !i.IsAtEnd(); i.Advance()) {
316 webrtc::DesktopRect rect = i.rect();
317
318 // Re-clip |region| with the current clipping area |clip_area_| because
319 // the latter could change from the time the buffer was drawn.
320 rect.IntersectWith(clip_area_);
321 if (rect.is_empty())
322 continue;
323
324 // Specify the rectangle coordinates relative to the clipping area.
325 rect.Translate(-clip_area.left(), -clip_area.top());
326
327 // Pepper Graphics 2D has a strange and badly documented API that the
328 // point here is the offset from the source rect. Why?
329 graphics2d_.PaintImageData(
330 static_cast<PepperDesktopFrame*>(buffer)->buffer(),
331 pp::Point(clip_area.left(), clip_area.top()),
332 pp::Rect(rect.left(), rect.top(), rect.width(), rect.height()));
333 }
334
335 // Notify the producer that some parts of the region weren't painted because
336 // the clipping area has changed already.
337 if (!clip_area.equals(clip_area_)) {
338 webrtc::DesktopRegion not_painted = region;
339 not_painted.Subtract(clip_area_);
340 if (!not_painted.is_empty()) {
341 software_video_renderer_->InvalidateRegion(not_painted);
342 }
343 }
344
345 // Flush the updated areas to the screen.
346 pp::CompletionCallback callback = callback_factory_.NewCallback(
347 &PepperVideoRenderer2D::OnFlushDone, start_time, buffer);
348 int error = graphics2d_.Flush(callback);
349 CHECK(error == PP_OK_COMPLETIONPENDING);
350 flush_pending_ = true;
351
352 // If Debug dirty region is enabled then emit it.
353 if (debug_dirty_region_) {
354 event_handler_->OnVideoFrameDirtyRegion(region);
355 }
356 }
357
358 void PepperVideoRenderer2D::OnFlushDone(int result,
359 const base::Time& paint_start,
360 webrtc::DesktopFrame* buffer) {
361 DCHECK(CalledOnValidThread());
362 DCHECK(flush_pending_);
363
364 software_video_renderer_->GetStats()->RecordPaintTime(
365 (base::Time::Now() - paint_start).InMilliseconds());
366
367 flush_pending_ = false;
368 ReturnBuffer(buffer);
369
370 // If there is a buffer queued for rendering then render it now.
371 if (merge_buffer_) {
372 buffer = merge_buffer_;
373 merge_buffer_ = nullptr;
374 FlushBuffer(merge_clip_area_, buffer, merge_region_);
375 }
376 } 225 }
377 226
378 } // namespace remoting 227 } // namespace remoting
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698