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