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