pepper_view.cc revision 424c4d7b64af9d0d8fd9624f381f469654d5e3d2
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 3// found in the LICENSE file. 4 5#include "remoting/client/plugin/pepper_view.h" 6 7#include <functional> 8 9#include "base/message_loop/message_loop.h" 10#include "base/strings/string_util.h" 11#include "base/synchronization/waitable_event.h" 12#include "base/time/time.h" 13#include "ppapi/cpp/completion_callback.h" 14#include "ppapi/cpp/image_data.h" 15#include "ppapi/cpp/point.h" 16#include "ppapi/cpp/rect.h" 17#include "ppapi/cpp/size.h" 18#include "remoting/base/util.h" 19#include "remoting/client/chromoting_stats.h" 20#include "remoting/client/client_context.h" 21#include "remoting/client/frame_producer.h" 22#include "remoting/client/plugin/chromoting_instance.h" 23#include "third_party/webrtc/modules/desktop_capture/desktop_frame.h" 24 25using base::Passed; 26 27namespace { 28 29// DesktopFrame that wraps a supplied pp::ImageData 30class PepperDesktopFrame : public webrtc::DesktopFrame { 31 public: 32 // Wraps the supplied ImageData. 33 explicit PepperDesktopFrame(const pp::ImageData& buffer); 34 35 // Access to underlying pepper representation. 36 const pp::ImageData& buffer() const { 37 return buffer_; 38 } 39 40 private: 41 pp::ImageData buffer_; 42}; 43 44PepperDesktopFrame::PepperDesktopFrame(const pp::ImageData& buffer) 45 : DesktopFrame(webrtc::DesktopSize(buffer.size().width(), 46 buffer.size().height()), 47 buffer.stride(), 48 reinterpret_cast<uint8_t*>(buffer.data()), 49 NULL), 50 buffer_(buffer) {} 51 52} // namespace 53 54namespace remoting { 55 56namespace { 57 58// The maximum number of image buffers to be allocated at any point of time. 59const size_t kMaxPendingBuffersCount = 2; 60 61} // namespace 62 63PepperView::PepperView(ChromotingInstance* instance, 64 ClientContext* context, 65 FrameProducer* producer) 66 : instance_(instance), 67 context_(context), 68 producer_(producer), 69 merge_buffer_(NULL), 70 merge_clip_area_(SkIRect::MakeEmpty()), 71 dips_size_(SkISize::Make(0, 0)), 72 dips_to_device_scale_(1.0f), 73 view_size_(SkISize::Make(0, 0)), 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)), 78 flush_pending_(false), 79 is_initialized_(false), 80 frame_received_(false), 81 callback_factory_(this) { 82 InitiateDrawing(); 83} 84 85PepperView::~PepperView() { 86 // The producer should now return any pending buffers. At this point, however, 87 // ReturnBuffer() tasks scheduled by the producer will not be delivered, 88 // so we free all the buffers once the producer's queue is empty. 89 base::WaitableEvent done_event(true, false); 90 producer_->RequestReturnBuffers( 91 base::Bind(&base::WaitableEvent::Signal, base::Unretained(&done_event))); 92 done_event.Wait(); 93 94 merge_buffer_ = NULL; 95 while (!buffers_.empty()) { 96 FreeBuffer(buffers_.front()); 97 } 98} 99 100void PepperView::SetView(const pp::View& view) { 101 bool view_changed = false; 102 103 pp::Rect pp_size = view.GetRect(); 104 SkISize new_dips_size = SkISize::Make(pp_size.width(), pp_size.height()); 105 float new_dips_to_device_scale = view.GetDeviceScale(); 106 107 if (dips_size_ != new_dips_size || 108 dips_to_device_scale_ != new_dips_to_device_scale) { 109 view_changed = true; 110 dips_to_device_scale_ = new_dips_to_device_scale; 111 dips_size_ = new_dips_size; 112 113 // 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 115 // 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 117 // for high-DPI devices. 118 dips_to_view_scale_ = 1.0f; 119 view_size_ = dips_size_; 120 121 // 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. 123 if (dips_size_ != source_size_) { 124 dips_to_view_scale_ = dips_to_device_scale_; 125 view_size_ = SkISize::Make( 126 ceilf(dips_size_.width() * dips_to_view_scale_), 127 ceilf(dips_size_.height() * dips_to_view_scale_)); 128 } 129 130 // Create a 2D rendering context at the chosen frame dimensions. 131 pp::Size pp_size = pp::Size(view_size_.width(), view_size_.height()); 132 graphics2d_ = pp::Graphics2D(instance_, pp_size, false); 133 134 // Specify the scale from our coordinates to DIPs. 135 graphics2d_.SetScale(1.0f / dips_to_view_scale_); 136 137 bool result = instance_->BindGraphics(graphics2d_); 138 139 // There is no good way to handle this error currently. 140 DCHECK(result) << "Couldn't bind the device context."; 141 } 142 143 pp::Rect pp_clip = view.GetClipRect(); 144 SkIRect new_clip = SkIRect::MakeLTRB( 145 floorf(pp_clip.x() * dips_to_view_scale_), 146 floorf(pp_clip.y() * dips_to_view_scale_), 147 ceilf(pp_clip.right() * dips_to_view_scale_), 148 ceilf(pp_clip.bottom() * dips_to_view_scale_)); 149 if (clip_area_ != new_clip) { 150 view_changed = true; 151 152 // YUV to RGB conversion may require even X and Y coordinates for 153 // the top left corner of the clipping area. 154 clip_area_ = AlignRect(new_clip); 155 clip_area_.intersect(SkIRect::MakeSize(view_size_)); 156 } 157 158 if (view_changed) { 159 producer_->SetOutputSizeAndClip(view_size_, clip_area_); 160 InitiateDrawing(); 161 } 162} 163 164void PepperView::ApplyBuffer(const SkISize& view_size, 165 const SkIRect& clip_area, 166 webrtc::DesktopFrame* buffer, 167 const SkRegion& region) { 168 DCHECK(context_->main_task_runner()->BelongsToCurrentThread()); 169 170 if (!frame_received_) { 171 instance_->OnFirstFrameReceived(); 172 frame_received_ = true; 173 } 174 // We cannot use the data in the buffer if its dimensions don't match the 175 // current view size. 176 // 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 178 // the properly scaled data. 179 if (view_size_ != view_size) { 180 FreeBuffer(buffer); 181 InitiateDrawing(); 182 } else { 183 FlushBuffer(clip_area, buffer, region); 184 } 185} 186 187void PepperView::ReturnBuffer(webrtc::DesktopFrame* buffer) { 188 DCHECK(context_->main_task_runner()->BelongsToCurrentThread()); 189 190 // Reuse the buffer if it is large enough, otherwise drop it on the floor 191 // and allocate a new one. 192 if (buffer->size().width() >= clip_area_.width() && 193 buffer->size().height() >= clip_area_.height()) { 194 producer_->DrawBuffer(buffer); 195 } else { 196 FreeBuffer(buffer); 197 InitiateDrawing(); 198 } 199} 200 201void PepperView::SetSourceSize(const SkISize& source_size, 202 const SkIPoint& source_dpi) { 203 DCHECK(context_->main_task_runner()->BelongsToCurrentThread()); 204 205 if (source_size_ == source_size && source_dpi_ == source_dpi) 206 return; 207 208 source_size_ = source_size; 209 source_dpi_ = source_dpi; 210 211 // Notify JavaScript of the change in source size. 212 instance_->SetDesktopSize(source_size, source_dpi); 213} 214 215webrtc::DesktopFrame* PepperView::AllocateBuffer() { 216 if (buffers_.size() >= kMaxPendingBuffersCount) 217 return NULL; 218 219 if (clip_area_.width()==0 || clip_area_.height()==0) 220 return NULL; 221 222 // Create an image buffer of the required size, but don't zero it. 223 pp::ImageData buffer_data(instance_, 224 PP_IMAGEDATAFORMAT_BGRA_PREMUL, 225 pp::Size(clip_area_.width(), 226 clip_area_.height()), 227 false); 228 if (buffer_data.is_null()) { 229 LOG(WARNING) << "Not enough memory for frame buffers."; 230 return NULL; 231 } 232 233 webrtc::DesktopFrame* buffer = new PepperDesktopFrame(buffer_data); 234 buffers_.push_back(buffer); 235 return buffer; 236} 237 238void PepperView::FreeBuffer(webrtc::DesktopFrame* buffer) { 239 DCHECK(std::find(buffers_.begin(), buffers_.end(), buffer) != buffers_.end()); 240 241 buffers_.remove(buffer); 242 delete buffer; 243} 244 245void PepperView::InitiateDrawing() { 246 webrtc::DesktopFrame* buffer = AllocateBuffer(); 247 while (buffer) { 248 producer_->DrawBuffer(buffer); 249 buffer = AllocateBuffer(); 250 } 251} 252 253void PepperView::FlushBuffer(const SkIRect& clip_area, 254 webrtc::DesktopFrame* buffer, 255 const SkRegion& region) { 256 // Defer drawing if the flush is already in progress. 257 if (flush_pending_) { 258 // |merge_buffer_| is guaranteed to be free here because we allocate only 259 // two buffers simultaneously. If more buffers are allowed this code should 260 // apply all pending changes to the screen. 261 DCHECK(merge_buffer_ == NULL); 262 263 merge_clip_area_ = clip_area; 264 merge_buffer_ = buffer; 265 merge_region_ = region; 266 return; 267 } 268 269 // Notify Pepper API about the updated areas and flush pixels to the screen. 270 base::Time start_time = base::Time::Now(); 271 272 for (SkRegion::Iterator i(region); !i.done(); i.next()) { 273 SkIRect rect = i.rect(); 274 275 // Re-clip |region| with the current clipping area |clip_area_| because 276 // the latter could change from the time the buffer was drawn. 277 if (!rect.intersect(clip_area_)) 278 continue; 279 280 // Specify the rectangle coordinates relative to the clipping area. 281 rect.offset(-clip_area.left(), -clip_area.top()); 282 283 // Pepper Graphics 2D has a strange and badly documented API that the 284 // point here is the offset from the source rect. Why? 285 graphics2d_.PaintImageData( 286 static_cast<PepperDesktopFrame*>(buffer)->buffer(), 287 pp::Point(clip_area.left(), clip_area.top()), 288 pp::Rect(rect.left(), rect.top(), rect.width(), rect.height())); 289 } 290 291 // Notify the producer that some parts of the region weren't painted because 292 // the clipping area has changed already. 293 if (clip_area != clip_area_) { 294 SkRegion not_painted = region; 295 not_painted.op(clip_area_, SkRegion::kDifference_Op); 296 if (!not_painted.isEmpty()) { 297 producer_->InvalidateRegion(not_painted); 298 } 299 } 300 301 // Flush the updated areas to the screen. 302 pp::CompletionCallback callback = 303 callback_factory_.NewCallback(&PepperView::OnFlushDone, 304 start_time, 305 buffer); 306 int error = graphics2d_.Flush(callback); 307 CHECK(error == PP_OK_COMPLETIONPENDING); 308 flush_pending_ = true; 309 310 // If the buffer we just rendered has a shape then pass that to JavaScript. 311 const SkRegion* buffer_shape = producer_->GetBufferShape(); 312 if (buffer_shape) 313 instance_->SetDesktopShape(*buffer_shape); 314} 315 316void PepperView::OnFlushDone(int result, 317 const base::Time& paint_start, 318 webrtc::DesktopFrame* buffer) { 319 DCHECK(context_->main_task_runner()->BelongsToCurrentThread()); 320 DCHECK(flush_pending_); 321 322 instance_->GetStats()->video_paint_ms()->Record( 323 (base::Time::Now() - paint_start).InMilliseconds()); 324 325 flush_pending_ = false; 326 ReturnBuffer(buffer); 327 328 // If there is a buffer queued for rendering then render it now. 329 if (merge_buffer_ != NULL) { 330 buffer = merge_buffer_; 331 merge_buffer_ = NULL; 332 FlushBuffer(merge_clip_area_, buffer, merge_region_); 333 } 334} 335 336} // namespace remoting 337