| 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.h" | 9 #include "base/message_loop.h" |
| 10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
| 11 #include "base/synchronization/waitable_event.h" | 11 #include "base/synchronization/waitable_event.h" |
| 12 #include "base/time/time.h" | 12 #include "base/time/time.h" |
| 13 #include "ppapi/cpp/completion_callback.h" | 13 #include "ppapi/cpp/completion_callback.h" |
| 14 #include "ppapi/cpp/dev/graphics_2d_dev.h" | 14 #include "ppapi/cpp/dev/graphics_2d_dev.h" |
| 15 #include "ppapi/cpp/dev/view_dev.h" | 15 #include "ppapi/cpp/dev/view_dev.h" |
| 16 #include "ppapi/cpp/image_data.h" | 16 #include "ppapi/cpp/image_data.h" |
| 17 #include "ppapi/cpp/point.h" | 17 #include "ppapi/cpp/point.h" |
| 18 #include "ppapi/cpp/rect.h" | 18 #include "ppapi/cpp/rect.h" |
| 19 #include "ppapi/cpp/size.h" | 19 #include "ppapi/cpp/size.h" |
| 20 #include "remoting/base/util.h" | 20 #include "remoting/base/util.h" |
| 21 #include "remoting/client/chromoting_stats.h" | 21 #include "remoting/client/chromoting_stats.h" |
| 22 #include "remoting/client/client_context.h" | 22 #include "remoting/client/client_context.h" |
| 23 #include "remoting/client/frame_producer.h" | 23 #include "remoting/client/frame_producer.h" |
| 24 #include "remoting/client/plugin/chromoting_instance.h" | 24 #include "remoting/client/plugin/chromoting_instance.h" |
| 25 #include "remoting/client/plugin/pepper_util.h" | 25 #include "remoting/client/plugin/pepper_util.h" |
| 26 #include "third_party/webrtc/modules/desktop_capture/desktop_frame.h" |
| 26 | 27 |
| 27 using base::Passed; | 28 using base::Passed; |
| 28 | 29 |
| 30 namespace { |
| 31 |
| 32 // DesktopFrame that wraps a supplied pp::ImageData |
| 33 class PepperDesktopFrame : public webrtc::DesktopFrame { |
| 34 public: |
| 35 // Wraps the supplied ImageData. |
| 36 explicit PepperDesktopFrame(const pp::ImageData& buffer); |
| 37 |
| 38 // Access to underlying pepper representation. |
| 39 const pp::ImageData& buffer() const { |
| 40 return buffer_; |
| 41 } |
| 42 |
| 43 private: |
| 44 pp::ImageData buffer_; |
| 45 }; |
| 46 |
| 47 PepperDesktopFrame::PepperDesktopFrame(const pp::ImageData& buffer) |
| 48 : DesktopFrame(webrtc::DesktopSize(buffer.size().width(), |
| 49 buffer.size().height()), |
| 50 buffer.stride(), |
| 51 reinterpret_cast<uint8_t*>(buffer.data()), |
| 52 NULL), |
| 53 buffer_(buffer) {} |
| 54 |
| 55 } // namespace |
| 56 |
| 29 namespace remoting { | 57 namespace remoting { |
| 30 | 58 |
| 31 namespace { | 59 namespace { |
| 32 | 60 |
| 33 // The maximum number of image buffers to be allocated at any point of time. | 61 // The maximum number of image buffers to be allocated at any point of time. |
| 34 const size_t kMaxPendingBuffersCount = 2; | 62 const size_t kMaxPendingBuffersCount = 2; |
| 35 | 63 |
| 36 } // namespace | 64 } // namespace |
| 37 | 65 |
| 38 PepperView::PepperView(ChromotingInstance* instance, | 66 PepperView::PepperView(ChromotingInstance* instance, |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 132 } | 160 } |
| 133 | 161 |
| 134 if (view_changed) { | 162 if (view_changed) { |
| 135 producer_->SetOutputSizeAndClip(view_size_, clip_area_); | 163 producer_->SetOutputSizeAndClip(view_size_, clip_area_); |
| 136 InitiateDrawing(); | 164 InitiateDrawing(); |
| 137 } | 165 } |
| 138 } | 166 } |
| 139 | 167 |
| 140 void PepperView::ApplyBuffer(const SkISize& view_size, | 168 void PepperView::ApplyBuffer(const SkISize& view_size, |
| 141 const SkIRect& clip_area, | 169 const SkIRect& clip_area, |
| 142 pp::ImageData* buffer, | 170 webrtc::DesktopFrame* buffer, |
| 143 const SkRegion& region) { | 171 const SkRegion& region) { |
| 144 DCHECK(context_->main_task_runner()->BelongsToCurrentThread()); | 172 DCHECK(context_->main_task_runner()->BelongsToCurrentThread()); |
| 145 | 173 |
| 146 if (!frame_received_) { | 174 if (!frame_received_) { |
| 147 instance_->OnFirstFrameReceived(); | 175 instance_->OnFirstFrameReceived(); |
| 148 frame_received_ = true; | 176 frame_received_ = true; |
| 149 } | 177 } |
| 150 // We cannot use the data in the buffer if its dimensions don't match the | 178 // We cannot use the data in the buffer if its dimensions don't match the |
| 151 // current view size. | 179 // current view size. |
| 152 // TODO(alexeypa): We could rescale and draw it (or even draw it without | 180 // TODO(alexeypa): We could rescale and draw it (or even draw it without |
| 153 // rescaling) to reduce the perceived lag while we are waiting for | 181 // rescaling) to reduce the perceived lag while we are waiting for |
| 154 // the properly scaled data. | 182 // the properly scaled data. |
| 155 if (view_size_ != view_size) { | 183 if (view_size_ != view_size) { |
| 156 FreeBuffer(buffer); | 184 FreeBuffer(buffer); |
| 157 InitiateDrawing(); | 185 InitiateDrawing(); |
| 158 } else { | 186 } else { |
| 159 FlushBuffer(clip_area, buffer, region); | 187 FlushBuffer(clip_area, buffer, region); |
| 160 } | 188 } |
| 161 } | 189 } |
| 162 | 190 |
| 163 void PepperView::ReturnBuffer(pp::ImageData* buffer) { | 191 void PepperView::ReturnBuffer(webrtc::DesktopFrame* buffer) { |
| 164 DCHECK(context_->main_task_runner()->BelongsToCurrentThread()); | 192 DCHECK(context_->main_task_runner()->BelongsToCurrentThread()); |
| 165 | 193 |
| 166 // Reuse the buffer if it is large enough, otherwise drop it on the floor | 194 // Reuse the buffer if it is large enough, otherwise drop it on the floor |
| 167 // and allocate a new one. | 195 // and allocate a new one. |
| 168 if (buffer->size().width() >= clip_area_.width() && | 196 if (buffer->size().width() >= clip_area_.width() && |
| 169 buffer->size().height() >= clip_area_.height()) { | 197 buffer->size().height() >= clip_area_.height()) { |
| 170 producer_->DrawBuffer(buffer); | 198 producer_->DrawBuffer(buffer); |
| 171 } else { | 199 } else { |
| 172 FreeBuffer(buffer); | 200 FreeBuffer(buffer); |
| 173 InitiateDrawing(); | 201 InitiateDrawing(); |
| 174 } | 202 } |
| 175 } | 203 } |
| 176 | 204 |
| 177 void PepperView::SetSourceSize(const SkISize& source_size, | 205 void PepperView::SetSourceSize(const SkISize& source_size, |
| 178 const SkIPoint& source_dpi) { | 206 const SkIPoint& source_dpi) { |
| 179 DCHECK(context_->main_task_runner()->BelongsToCurrentThread()); | 207 DCHECK(context_->main_task_runner()->BelongsToCurrentThread()); |
| 180 | 208 |
| 181 if (source_size_ == source_size && source_dpi_ == source_dpi) | 209 if (source_size_ == source_size && source_dpi_ == source_dpi) |
| 182 return; | 210 return; |
| 183 | 211 |
| 184 source_size_ = source_size; | 212 source_size_ = source_size; |
| 185 source_dpi_ = source_dpi; | 213 source_dpi_ = source_dpi; |
| 186 | 214 |
| 187 // Notify JavaScript of the change in source size. | 215 // Notify JavaScript of the change in source size. |
| 188 instance_->SetDesktopSize(source_size, source_dpi); | 216 instance_->SetDesktopSize(source_size, source_dpi); |
| 189 } | 217 } |
| 190 | 218 |
| 191 pp::ImageData* PepperView::AllocateBuffer() { | 219 webrtc::DesktopFrame* PepperView::AllocateBuffer() { |
| 192 if (buffers_.size() >= kMaxPendingBuffersCount) | 220 if (buffers_.size() >= kMaxPendingBuffersCount) |
| 193 return NULL; | 221 return NULL; |
| 194 | 222 |
| 195 pp::Size pp_size = pp::Size(clip_area_.width(), clip_area_.height()); | 223 if (clip_area_.width()==0 || clip_area_.height()==0) |
| 196 if (pp_size.IsEmpty()) | |
| 197 return NULL; | 224 return NULL; |
| 198 | 225 |
| 199 // Create an image buffer of the required size, but don't zero it. | 226 // Create an image buffer of the required size, but don't zero it. |
| 200 pp::ImageData* buffer = new pp::ImageData( | 227 pp::ImageData buffer_data(instance_, |
| 201 instance_, PP_IMAGEDATAFORMAT_BGRA_PREMUL, pp_size, false); | 228 PP_IMAGEDATAFORMAT_BGRA_PREMUL, |
| 202 if (buffer->is_null()) { | 229 pp::Size(clip_area_.width(), |
| 230 clip_area_.height()), |
| 231 false); |
| 232 if (buffer_data.is_null()) { |
| 203 LOG(WARNING) << "Not enough memory for frame buffers."; | 233 LOG(WARNING) << "Not enough memory for frame buffers."; |
| 204 delete buffer; | |
| 205 return NULL; | 234 return NULL; |
| 206 } | 235 } |
| 207 | 236 |
| 237 webrtc::DesktopFrame* buffer = new PepperDesktopFrame(buffer_data); |
| 208 buffers_.push_back(buffer); | 238 buffers_.push_back(buffer); |
| 209 return buffer; | 239 return buffer; |
| 210 } | 240 } |
| 211 | 241 |
| 212 void PepperView::FreeBuffer(pp::ImageData* buffer) { | 242 void PepperView::FreeBuffer(webrtc::DesktopFrame* buffer) { |
| 213 DCHECK(std::find(buffers_.begin(), buffers_.end(), buffer) != buffers_.end()); | 243 DCHECK(std::find(buffers_.begin(), buffers_.end(), buffer) != buffers_.end()); |
| 214 | 244 |
| 215 buffers_.remove(buffer); | 245 buffers_.remove(buffer); |
| 216 delete buffer; | 246 delete buffer; |
| 217 } | 247 } |
| 218 | 248 |
| 219 void PepperView::InitiateDrawing() { | 249 void PepperView::InitiateDrawing() { |
| 220 pp::ImageData* buffer = AllocateBuffer(); | 250 webrtc::DesktopFrame* buffer = AllocateBuffer(); |
| 221 while (buffer) { | 251 while (buffer) { |
| 222 producer_->DrawBuffer(buffer); | 252 producer_->DrawBuffer(buffer); |
| 223 buffer = AllocateBuffer(); | 253 buffer = AllocateBuffer(); |
| 224 } | 254 } |
| 225 } | 255 } |
| 226 | 256 |
| 227 void PepperView::FlushBuffer(const SkIRect& clip_area, | 257 void PepperView::FlushBuffer(const SkIRect& clip_area, |
| 228 pp::ImageData* buffer, | 258 webrtc::DesktopFrame* buffer, |
| 229 const SkRegion& region) { | 259 const SkRegion& region) { |
| 230 // Defer drawing if the flush is already in progress. | 260 // Defer drawing if the flush is already in progress. |
| 231 if (flush_pending_) { | 261 if (flush_pending_) { |
| 232 // |merge_buffer_| is guaranteed to be free here because we allocate only | 262 // |merge_buffer_| is guaranteed to be free here because we allocate only |
| 233 // two buffers simultaneously. If more buffers are allowed this code should | 263 // two buffers simultaneously. If more buffers are allowed this code should |
| 234 // apply all pending changes to the screen. | 264 // apply all pending changes to the screen. |
| 235 DCHECK(merge_buffer_ == NULL); | 265 DCHECK(merge_buffer_ == NULL); |
| 236 | 266 |
| 237 merge_clip_area_ = clip_area; | 267 merge_clip_area_ = clip_area; |
| 238 merge_buffer_ = buffer; | 268 merge_buffer_ = buffer; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 250 // the latter could change from the time the buffer was drawn. | 280 // the latter could change from the time the buffer was drawn. |
| 251 if (!rect.intersect(clip_area_)) | 281 if (!rect.intersect(clip_area_)) |
| 252 continue; | 282 continue; |
| 253 | 283 |
| 254 // Specify the rectangle coordinates relative to the clipping area. | 284 // Specify the rectangle coordinates relative to the clipping area. |
| 255 rect.offset(-clip_area.left(), -clip_area.top()); | 285 rect.offset(-clip_area.left(), -clip_area.top()); |
| 256 | 286 |
| 257 // Pepper Graphics 2D has a strange and badly documented API that the | 287 // Pepper Graphics 2D has a strange and badly documented API that the |
| 258 // point here is the offset from the source rect. Why? | 288 // point here is the offset from the source rect. Why? |
| 259 graphics2d_.PaintImageData( | 289 graphics2d_.PaintImageData( |
| 260 *buffer, | 290 static_cast<PepperDesktopFrame*>(buffer)->buffer(), |
| 261 pp::Point(clip_area.left(), clip_area.top()), | 291 pp::Point(clip_area.left(), clip_area.top()), |
| 262 pp::Rect(rect.left(), rect.top(), rect.width(), rect.height())); | 292 pp::Rect(rect.left(), rect.top(), rect.width(), rect.height())); |
| 263 } | 293 } |
| 264 | 294 |
| 265 // Notify the producer that some parts of the region weren't painted because | 295 // Notify the producer that some parts of the region weren't painted because |
| 266 // the clipping area has changed already. | 296 // the clipping area has changed already. |
| 267 if (clip_area != clip_area_) { | 297 if (clip_area != clip_area_) { |
| 268 SkRegion not_painted = region; | 298 SkRegion not_painted = region; |
| 269 not_painted.op(clip_area_, SkRegion::kDifference_Op); | 299 not_painted.op(clip_area_, SkRegion::kDifference_Op); |
| 270 if (!not_painted.isEmpty()) { | 300 if (!not_painted.isEmpty()) { |
| 271 producer_->InvalidateRegion(not_painted); | 301 producer_->InvalidateRegion(not_painted); |
| 272 } | 302 } |
| 273 } | 303 } |
| 274 | 304 |
| 275 // Flush the updated areas to the screen. | 305 // Flush the updated areas to the screen. |
| 276 int error = graphics2d_.Flush( | 306 int error = graphics2d_.Flush( |
| 277 PpCompletionCallback(base::Bind( | 307 PpCompletionCallback(base::Bind( |
| 278 &PepperView::OnFlushDone, AsWeakPtr(), start_time, buffer))); | 308 &PepperView::OnFlushDone, AsWeakPtr(), start_time, buffer))); |
| 279 CHECK(error == PP_OK_COMPLETIONPENDING); | 309 CHECK(error == PP_OK_COMPLETIONPENDING); |
| 280 flush_pending_ = true; | 310 flush_pending_ = true; |
| 281 | 311 |
| 282 // If the buffer we just rendered has a shape then pass that to JavaScript. | 312 // If the buffer we just rendered has a shape then pass that to JavaScript. |
| 283 const SkRegion* buffer_shape = producer_->GetBufferShape(); | 313 const SkRegion* buffer_shape = producer_->GetBufferShape(); |
| 284 if (buffer_shape) | 314 if (buffer_shape) |
| 285 instance_->SetDesktopShape(*buffer_shape); | 315 instance_->SetDesktopShape(*buffer_shape); |
| 286 } | 316 } |
| 287 | 317 |
| 288 void PepperView::OnFlushDone(base::Time paint_start, | 318 void PepperView::OnFlushDone(base::Time paint_start, |
| 289 pp::ImageData* buffer, | 319 webrtc::DesktopFrame* buffer, |
| 290 int result) { | 320 int result) { |
| 291 DCHECK(context_->main_task_runner()->BelongsToCurrentThread()); | 321 DCHECK(context_->main_task_runner()->BelongsToCurrentThread()); |
| 292 DCHECK(flush_pending_); | 322 DCHECK(flush_pending_); |
| 293 | 323 |
| 294 instance_->GetStats()->video_paint_ms()->Record( | 324 instance_->GetStats()->video_paint_ms()->Record( |
| 295 (base::Time::Now() - paint_start).InMilliseconds()); | 325 (base::Time::Now() - paint_start).InMilliseconds()); |
| 296 | 326 |
| 297 flush_pending_ = false; | 327 flush_pending_ = false; |
| 298 ReturnBuffer(buffer); | 328 ReturnBuffer(buffer); |
| 299 | 329 |
| 300 // If there is a buffer queued for rendering then render it now. | 330 // If there is a buffer queued for rendering then render it now. |
| 301 if (merge_buffer_ != NULL) { | 331 if (merge_buffer_ != NULL) { |
| 302 buffer = merge_buffer_; | 332 buffer = merge_buffer_; |
| 303 merge_buffer_ = NULL; | 333 merge_buffer_ = NULL; |
| 304 FlushBuffer(merge_clip_area_, buffer, merge_region_); | 334 FlushBuffer(merge_clip_area_, buffer, merge_region_); |
| 305 } | 335 } |
| 306 } | 336 } |
| 307 | 337 |
| 308 } // namespace remoting | 338 } // namespace remoting |
| OLD | NEW |