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