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