OLD | NEW |
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_view.h" | 5 #include "remoting/client/plugin/pepper_view.h" |
6 | 6 |
7 #include <functional> | 7 #include <functional> |
8 | 8 |
9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
60 | 60 |
61 } // namespace | 61 } // namespace |
62 | 62 |
63 PepperView::PepperView(ChromotingInstance* instance, | 63 PepperView::PepperView(ChromotingInstance* instance, |
64 ClientContext* context, | 64 ClientContext* context, |
65 FrameProducer* producer) | 65 FrameProducer* producer) |
66 : instance_(instance), | 66 : instance_(instance), |
67 context_(context), | 67 context_(context), |
68 producer_(producer), | 68 producer_(producer), |
69 merge_buffer_(NULL), | 69 merge_buffer_(NULL), |
70 merge_clip_area_(SkIRect::MakeEmpty()), | |
71 dips_size_(SkISize::Make(0, 0)), | |
72 dips_to_device_scale_(1.0f), | 70 dips_to_device_scale_(1.0f), |
73 view_size_(SkISize::Make(0, 0)), | |
74 dips_to_view_scale_(1.0f), | 71 dips_to_view_scale_(1.0f), |
75 clip_area_(SkIRect::MakeEmpty()), | |
76 source_size_(SkISize::Make(0, 0)), | |
77 source_dpi_(SkIPoint::Make(0, 0)), | |
78 flush_pending_(false), | 72 flush_pending_(false), |
79 is_initialized_(false), | 73 is_initialized_(false), |
80 frame_received_(false), | 74 frame_received_(false), |
81 callback_factory_(this) { | 75 callback_factory_(this) { |
82 InitiateDrawing(); | 76 InitiateDrawing(); |
83 } | 77 } |
84 | 78 |
85 PepperView::~PepperView() { | 79 PepperView::~PepperView() { |
86 // The producer should now return any pending buffers. At this point, however, | 80 // The producer should now return any pending buffers. At this point, however, |
87 // ReturnBuffer() tasks scheduled by the producer will not be delivered, | 81 // ReturnBuffer() tasks scheduled by the producer will not be delivered, |
88 // so we free all the buffers once the producer's queue is empty. | 82 // so we free all the buffers once the producer's queue is empty. |
89 base::WaitableEvent done_event(true, false); | 83 base::WaitableEvent done_event(true, false); |
90 producer_->RequestReturnBuffers( | 84 producer_->RequestReturnBuffers( |
91 base::Bind(&base::WaitableEvent::Signal, base::Unretained(&done_event))); | 85 base::Bind(&base::WaitableEvent::Signal, base::Unretained(&done_event))); |
92 done_event.Wait(); | 86 done_event.Wait(); |
93 | 87 |
94 merge_buffer_ = NULL; | 88 merge_buffer_ = NULL; |
95 while (!buffers_.empty()) { | 89 while (!buffers_.empty()) { |
96 FreeBuffer(buffers_.front()); | 90 FreeBuffer(buffers_.front()); |
97 } | 91 } |
98 } | 92 } |
99 | 93 |
100 void PepperView::SetView(const pp::View& view) { | 94 void PepperView::SetView(const pp::View& view) { |
101 bool view_changed = false; | 95 bool view_changed = false; |
102 | 96 |
103 pp::Rect pp_size = view.GetRect(); | 97 pp::Rect pp_size = view.GetRect(); |
104 SkISize new_dips_size = SkISize::Make(pp_size.width(), pp_size.height()); | 98 webrtc::DesktopSize new_dips_size(pp_size.width(), pp_size.height()); |
105 float new_dips_to_device_scale = view.GetDeviceScale(); | 99 float new_dips_to_device_scale = view.GetDeviceScale(); |
106 | 100 |
107 if (dips_size_ != new_dips_size || | 101 if (!dips_size_.equals(new_dips_size) || |
108 dips_to_device_scale_ != new_dips_to_device_scale) { | 102 dips_to_device_scale_ != new_dips_to_device_scale) { |
109 view_changed = true; | 103 view_changed = true; |
110 dips_to_device_scale_ = new_dips_to_device_scale; | 104 dips_to_device_scale_ = new_dips_to_device_scale; |
111 dips_size_ = new_dips_size; | 105 dips_size_ = new_dips_size; |
112 | 106 |
113 // If |dips_to_device_scale_| is > 1.0 then the device is high-DPI, and | 107 // If |dips_to_device_scale_| is > 1.0 then the device is high-DPI, and |
114 // there are actually |view_device_scale_| physical pixels for every one | 108 // there are actually |view_device_scale_| physical pixels for every one |
115 // Density Independent Pixel (DIP). If we specify a scale of 1.0 to | 109 // Density Independent Pixel (DIP). If we specify a scale of 1.0 to |
116 // Graphics2D then we can render at DIP resolution and let PPAPI up-scale | 110 // Graphics2D then we can render at DIP resolution and let PPAPI up-scale |
117 // for high-DPI devices. | 111 // for high-DPI devices. |
118 dips_to_view_scale_ = 1.0f; | 112 dips_to_view_scale_ = 1.0f; |
119 view_size_ = dips_size_; | 113 view_size_ = dips_size_; |
120 | 114 |
121 // If the view's DIP dimensions don't match the source then let the frame | 115 // If the view's DIP dimensions don't match the source then let the frame |
122 // producer do the scaling, and render at device resolution. | 116 // producer do the scaling, and render at device resolution. |
123 if (dips_size_ != source_size_) { | 117 if (!dips_size_.equals(source_size_)) { |
124 dips_to_view_scale_ = dips_to_device_scale_; | 118 dips_to_view_scale_ = dips_to_device_scale_; |
125 view_size_ = SkISize::Make( | 119 view_size_.set(ceilf(dips_size_.width() * dips_to_view_scale_), |
126 ceilf(dips_size_.width() * dips_to_view_scale_), | 120 ceilf(dips_size_.height() * dips_to_view_scale_)); |
127 ceilf(dips_size_.height() * dips_to_view_scale_)); | |
128 } | 121 } |
129 | 122 |
130 // Create a 2D rendering context at the chosen frame dimensions. | 123 // Create a 2D rendering context at the chosen frame dimensions. |
131 pp::Size pp_size = pp::Size(view_size_.width(), view_size_.height()); | 124 pp::Size pp_size = pp::Size(view_size_.width(), view_size_.height()); |
132 graphics2d_ = pp::Graphics2D(instance_, pp_size, false); | 125 graphics2d_ = pp::Graphics2D(instance_, pp_size, false); |
133 | 126 |
134 // Specify the scale from our coordinates to DIPs. | 127 // Specify the scale from our coordinates to DIPs. |
135 graphics2d_.SetScale(1.0f / dips_to_view_scale_); | 128 graphics2d_.SetScale(1.0f / dips_to_view_scale_); |
136 | 129 |
137 bool result = instance_->BindGraphics(graphics2d_); | 130 bool result = instance_->BindGraphics(graphics2d_); |
138 | 131 |
139 // There is no good way to handle this error currently. | 132 // There is no good way to handle this error currently. |
140 DCHECK(result) << "Couldn't bind the device context."; | 133 DCHECK(result) << "Couldn't bind the device context."; |
141 } | 134 } |
142 | 135 |
143 pp::Rect pp_clip = view.GetClipRect(); | 136 pp::Rect pp_clip = view.GetClipRect(); |
144 SkIRect new_clip = SkIRect::MakeLTRB( | 137 webrtc::DesktopRect new_clip = webrtc::DesktopRect::MakeLTRB( |
145 floorf(pp_clip.x() * dips_to_view_scale_), | 138 floorf(pp_clip.x() * dips_to_view_scale_), |
146 floorf(pp_clip.y() * dips_to_view_scale_), | 139 floorf(pp_clip.y() * dips_to_view_scale_), |
147 ceilf(pp_clip.right() * dips_to_view_scale_), | 140 ceilf(pp_clip.right() * dips_to_view_scale_), |
148 ceilf(pp_clip.bottom() * dips_to_view_scale_)); | 141 ceilf(pp_clip.bottom() * dips_to_view_scale_)); |
149 if (clip_area_ != new_clip) { | 142 if (!clip_area_.equals(new_clip)) { |
150 view_changed = true; | 143 view_changed = true; |
151 | 144 |
152 // YUV to RGB conversion may require even X and Y coordinates for | 145 // YUV to RGB conversion may require even X and Y coordinates for |
153 // the top left corner of the clipping area. | 146 // the top left corner of the clipping area. |
154 clip_area_ = AlignRect(new_clip); | 147 clip_area_ = AlignRect(new_clip); |
155 clip_area_.intersect(SkIRect::MakeSize(view_size_)); | 148 clip_area_.IntersectWith(webrtc::DesktopRect::MakeSize(view_size_)); |
156 } | 149 } |
157 | 150 |
158 if (view_changed) { | 151 if (view_changed) { |
159 producer_->SetOutputSizeAndClip(view_size_, clip_area_); | 152 producer_->SetOutputSizeAndClip(view_size_, clip_area_); |
160 InitiateDrawing(); | 153 InitiateDrawing(); |
161 } | 154 } |
162 } | 155 } |
163 | 156 |
164 void PepperView::ApplyBuffer(const SkISize& view_size, | 157 void PepperView::ApplyBuffer(const webrtc::DesktopSize& view_size, |
165 const SkIRect& clip_area, | 158 const webrtc::DesktopRect& clip_area, |
166 webrtc::DesktopFrame* buffer, | 159 webrtc::DesktopFrame* buffer, |
167 const SkRegion& region) { | 160 const webrtc::DesktopRegion& region) { |
168 DCHECK(context_->main_task_runner()->BelongsToCurrentThread()); | 161 DCHECK(context_->main_task_runner()->BelongsToCurrentThread()); |
169 | 162 |
170 if (!frame_received_) { | 163 if (!frame_received_) { |
171 instance_->OnFirstFrameReceived(); | 164 instance_->OnFirstFrameReceived(); |
172 frame_received_ = true; | 165 frame_received_ = true; |
173 } | 166 } |
174 // We cannot use the data in the buffer if its dimensions don't match the | 167 // We cannot use the data in the buffer if its dimensions don't match the |
175 // current view size. | 168 // current view size. |
176 // TODO(alexeypa): We could rescale and draw it (or even draw it without | 169 // TODO(alexeypa): We could rescale and draw it (or even draw it without |
177 // rescaling) to reduce the perceived lag while we are waiting for | 170 // rescaling) to reduce the perceived lag while we are waiting for |
178 // the properly scaled data. | 171 // the properly scaled data. |
179 if (view_size_ != view_size) { | 172 if (!view_size_.equals(view_size)) { |
180 FreeBuffer(buffer); | 173 FreeBuffer(buffer); |
181 InitiateDrawing(); | 174 InitiateDrawing(); |
182 } else { | 175 } else { |
183 FlushBuffer(clip_area, buffer, region); | 176 FlushBuffer(clip_area, buffer, region); |
184 } | 177 } |
185 } | 178 } |
186 | 179 |
187 void PepperView::ReturnBuffer(webrtc::DesktopFrame* buffer) { | 180 void PepperView::ReturnBuffer(webrtc::DesktopFrame* buffer) { |
188 DCHECK(context_->main_task_runner()->BelongsToCurrentThread()); | 181 DCHECK(context_->main_task_runner()->BelongsToCurrentThread()); |
189 | 182 |
190 // Reuse the buffer if it is large enough, otherwise drop it on the floor | 183 // Reuse the buffer if it is large enough, otherwise drop it on the floor |
191 // and allocate a new one. | 184 // and allocate a new one. |
192 if (buffer->size().width() >= clip_area_.width() && | 185 if (buffer->size().width() >= clip_area_.width() && |
193 buffer->size().height() >= clip_area_.height()) { | 186 buffer->size().height() >= clip_area_.height()) { |
194 producer_->DrawBuffer(buffer); | 187 producer_->DrawBuffer(buffer); |
195 } else { | 188 } else { |
196 FreeBuffer(buffer); | 189 FreeBuffer(buffer); |
197 InitiateDrawing(); | 190 InitiateDrawing(); |
198 } | 191 } |
199 } | 192 } |
200 | 193 |
201 void PepperView::SetSourceSize(const SkISize& source_size, | 194 void PepperView::SetSourceSize(const webrtc::DesktopSize& source_size, |
202 const SkIPoint& source_dpi) { | 195 const webrtc::DesktopVector& source_dpi) { |
203 DCHECK(context_->main_task_runner()->BelongsToCurrentThread()); | 196 DCHECK(context_->main_task_runner()->BelongsToCurrentThread()); |
204 | 197 |
205 if (source_size_ == source_size && source_dpi_ == source_dpi) | 198 if (source_size_.equals(source_size) && source_dpi_.equals(source_dpi)) |
206 return; | 199 return; |
207 | 200 |
208 source_size_ = source_size; | 201 source_size_ = source_size; |
209 source_dpi_ = source_dpi; | 202 source_dpi_ = source_dpi; |
210 | 203 |
211 // Notify JavaScript of the change in source size. | 204 // Notify JavaScript of the change in source size. |
212 instance_->SetDesktopSize(source_size, source_dpi); | 205 instance_->SetDesktopSize(source_size, source_dpi); |
213 } | 206 } |
214 | 207 |
215 webrtc::DesktopFrame* PepperView::AllocateBuffer() { | 208 webrtc::DesktopFrame* PepperView::AllocateBuffer() { |
(...skipping 27 matching lines...) Expand all Loading... |
243 } | 236 } |
244 | 237 |
245 void PepperView::InitiateDrawing() { | 238 void PepperView::InitiateDrawing() { |
246 webrtc::DesktopFrame* buffer = AllocateBuffer(); | 239 webrtc::DesktopFrame* buffer = AllocateBuffer(); |
247 while (buffer) { | 240 while (buffer) { |
248 producer_->DrawBuffer(buffer); | 241 producer_->DrawBuffer(buffer); |
249 buffer = AllocateBuffer(); | 242 buffer = AllocateBuffer(); |
250 } | 243 } |
251 } | 244 } |
252 | 245 |
253 void PepperView::FlushBuffer(const SkIRect& clip_area, | 246 void PepperView::FlushBuffer(const webrtc::DesktopRect& clip_area, |
254 webrtc::DesktopFrame* buffer, | 247 webrtc::DesktopFrame* buffer, |
255 const SkRegion& region) { | 248 const webrtc::DesktopRegion& region) { |
256 // Defer drawing if the flush is already in progress. | 249 // Defer drawing if the flush is already in progress. |
257 if (flush_pending_) { | 250 if (flush_pending_) { |
258 // |merge_buffer_| is guaranteed to be free here because we allocate only | 251 // |merge_buffer_| is guaranteed to be free here because we allocate only |
259 // two buffers simultaneously. If more buffers are allowed this code should | 252 // two buffers simultaneously. If more buffers are allowed this code should |
260 // apply all pending changes to the screen. | 253 // apply all pending changes to the screen. |
261 DCHECK(merge_buffer_ == NULL); | 254 DCHECK(merge_buffer_ == NULL); |
262 | 255 |
263 merge_clip_area_ = clip_area; | 256 merge_clip_area_ = clip_area; |
264 merge_buffer_ = buffer; | 257 merge_buffer_ = buffer; |
265 merge_region_ = region; | 258 merge_region_ = region; |
266 return; | 259 return; |
267 } | 260 } |
268 | 261 |
269 // Notify Pepper API about the updated areas and flush pixels to the screen. | 262 // Notify Pepper API about the updated areas and flush pixels to the screen. |
270 base::Time start_time = base::Time::Now(); | 263 base::Time start_time = base::Time::Now(); |
271 | 264 |
272 for (SkRegion::Iterator i(region); !i.done(); i.next()) { | 265 for (webrtc::DesktopRegion::Iterator i(region); !i.IsAtEnd(); i.Advance()) { |
273 SkIRect rect = i.rect(); | 266 webrtc::DesktopRect rect = i.rect(); |
274 | 267 |
275 // Re-clip |region| with the current clipping area |clip_area_| because | 268 // Re-clip |region| with the current clipping area |clip_area_| because |
276 // the latter could change from the time the buffer was drawn. | 269 // the latter could change from the time the buffer was drawn. |
277 if (!rect.intersect(clip_area_)) | 270 rect.IntersectWith(clip_area_); |
| 271 if (rect.is_empty()) |
278 continue; | 272 continue; |
279 | 273 |
280 // Specify the rectangle coordinates relative to the clipping area. | 274 // Specify the rectangle coordinates relative to the clipping area. |
281 rect.offset(-clip_area.left(), -clip_area.top()); | 275 rect.Translate(-clip_area.left(), -clip_area.top()); |
282 | 276 |
283 // Pepper Graphics 2D has a strange and badly documented API that the | 277 // Pepper Graphics 2D has a strange and badly documented API that the |
284 // point here is the offset from the source rect. Why? | 278 // point here is the offset from the source rect. Why? |
285 graphics2d_.PaintImageData( | 279 graphics2d_.PaintImageData( |
286 static_cast<PepperDesktopFrame*>(buffer)->buffer(), | 280 static_cast<PepperDesktopFrame*>(buffer)->buffer(), |
287 pp::Point(clip_area.left(), clip_area.top()), | 281 pp::Point(clip_area.left(), clip_area.top()), |
288 pp::Rect(rect.left(), rect.top(), rect.width(), rect.height())); | 282 pp::Rect(rect.left(), rect.top(), rect.width(), rect.height())); |
289 } | 283 } |
290 | 284 |
291 // Notify the producer that some parts of the region weren't painted because | 285 // Notify the producer that some parts of the region weren't painted because |
292 // the clipping area has changed already. | 286 // the clipping area has changed already. |
293 if (clip_area != clip_area_) { | 287 if (!clip_area.equals(clip_area_)) { |
294 SkRegion not_painted = region; | 288 webrtc::DesktopRegion not_painted = region; |
295 not_painted.op(clip_area_, SkRegion::kDifference_Op); | 289 not_painted.Subtract(clip_area_); |
296 if (!not_painted.isEmpty()) { | 290 if (!not_painted.is_empty()) { |
297 producer_->InvalidateRegion(not_painted); | 291 producer_->InvalidateRegion(not_painted); |
298 } | 292 } |
299 } | 293 } |
300 | 294 |
301 // Flush the updated areas to the screen. | 295 // Flush the updated areas to the screen. |
302 pp::CompletionCallback callback = | 296 pp::CompletionCallback callback = |
303 callback_factory_.NewCallback(&PepperView::OnFlushDone, | 297 callback_factory_.NewCallback(&PepperView::OnFlushDone, |
304 start_time, | 298 start_time, |
305 buffer); | 299 buffer); |
306 int error = graphics2d_.Flush(callback); | 300 int error = graphics2d_.Flush(callback); |
307 CHECK(error == PP_OK_COMPLETIONPENDING); | 301 CHECK(error == PP_OK_COMPLETIONPENDING); |
308 flush_pending_ = true; | 302 flush_pending_ = true; |
309 | 303 |
310 // If the buffer we just rendered has a shape then pass that to JavaScript. | 304 // If the buffer we just rendered has a shape then pass that to JavaScript. |
311 const SkRegion* buffer_shape = producer_->GetBufferShape(); | 305 const webrtc::DesktopRegion* buffer_shape = producer_->GetBufferShape(); |
312 if (buffer_shape) | 306 if (buffer_shape) |
313 instance_->SetDesktopShape(*buffer_shape); | 307 instance_->SetDesktopShape(*buffer_shape); |
314 } | 308 } |
315 | 309 |
316 void PepperView::OnFlushDone(int result, | 310 void PepperView::OnFlushDone(int result, |
317 const base::Time& paint_start, | 311 const base::Time& paint_start, |
318 webrtc::DesktopFrame* buffer) { | 312 webrtc::DesktopFrame* buffer) { |
319 DCHECK(context_->main_task_runner()->BelongsToCurrentThread()); | 313 DCHECK(context_->main_task_runner()->BelongsToCurrentThread()); |
320 DCHECK(flush_pending_); | 314 DCHECK(flush_pending_); |
321 | 315 |
322 instance_->GetStats()->video_paint_ms()->Record( | 316 instance_->GetStats()->video_paint_ms()->Record( |
323 (base::Time::Now() - paint_start).InMilliseconds()); | 317 (base::Time::Now() - paint_start).InMilliseconds()); |
324 | 318 |
325 flush_pending_ = false; | 319 flush_pending_ = false; |
326 ReturnBuffer(buffer); | 320 ReturnBuffer(buffer); |
327 | 321 |
328 // If there is a buffer queued for rendering then render it now. | 322 // If there is a buffer queued for rendering then render it now. |
329 if (merge_buffer_ != NULL) { | 323 if (merge_buffer_ != NULL) { |
330 buffer = merge_buffer_; | 324 buffer = merge_buffer_; |
331 merge_buffer_ = NULL; | 325 merge_buffer_ = NULL; |
332 FlushBuffer(merge_clip_area_, buffer, merge_region_); | 326 FlushBuffer(merge_clip_area_, buffer, merge_region_); |
333 } | 327 } |
334 } | 328 } |
335 | 329 |
336 } // namespace remoting | 330 } // namespace remoting |
OLD | NEW |