summaryrefslogtreecommitdiffstats
path: root/remoting/client/plugin/pepper_view.cc
blob: 531ff6ca29a94e45305d5f136f5a962d913ea2cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "remoting/client/plugin/pepper_view.h"

#include <functional>

#include "base/message_loop.h"
#include "base/string_util.h"
#include "base/synchronization/waitable_event.h"
#include "ppapi/cpp/completion_callback.h"
#include "ppapi/cpp/graphics_2d.h"
#include "ppapi/cpp/image_data.h"
#include "ppapi/cpp/point.h"
#include "ppapi/cpp/rect.h"
#include "ppapi/cpp/size.h"
#include "remoting/base/util.h"
#include "remoting/client/chromoting_stats.h"
#include "remoting/client/client_context.h"
#include "remoting/client/frame_producer.h"
#include "remoting/client/plugin/chromoting_instance.h"
#include "remoting/client/plugin/pepper_util.h"

using base::Passed;

namespace remoting {

namespace {

// The maximum number of image buffers to be allocated at any point of time.
const size_t kMaxPendingBuffersCount = 2;

// TODO(sergeyu): Ideally we should just pass ErrorCode to the webapp
// and let it handle it, but it would be hard to fix it now because
// client plugin and webapp versions may not be in sync. It should be
// easy to do after we are finished moving the client plugin to NaCl.
ChromotingInstance::ConnectionError ConvertConnectionError(
    protocol::ErrorCode error) {
  switch (error) {
    case protocol::OK:
      return ChromotingInstance::ERROR_NONE;

    case protocol::PEER_IS_OFFLINE:
      return ChromotingInstance::ERROR_HOST_IS_OFFLINE;

    case protocol::SESSION_REJECTED:
    case protocol::AUTHENTICATION_FAILED:
      return ChromotingInstance::ERROR_SESSION_REJECTED;

    case protocol::INCOMPATIBLE_PROTOCOL:
      return ChromotingInstance::ERROR_INCOMPATIBLE_PROTOCOL;

    case protocol::HOST_OVERLOAD:
      return ChromotingInstance::ERROR_HOST_OVERLOAD;

    case protocol::CHANNEL_CONNECTION_ERROR:
    case protocol::SIGNALING_ERROR:
    case protocol::SIGNALING_TIMEOUT:
    case protocol::UNKNOWN_ERROR:
      return ChromotingInstance::ERROR_NETWORK_FAILURE;
  }
  DLOG(FATAL) << "Unknown error code" << error;
  return  ChromotingInstance::ERROR_NONE;
}

}  // namespace

PepperView::PepperView(ChromotingInstance* instance,
                       ClientContext* context,
                       FrameProducer* producer)
  : instance_(instance),
    context_(context),
    producer_(producer),
    merge_buffer_(NULL),
    merge_clip_area_(SkIRect::MakeEmpty()),
    view_size_(SkISize::Make(0, 0)),
    clip_area_(SkIRect::MakeEmpty()),
    source_size_(SkISize::Make(0, 0)),
    flush_pending_(false),
    is_initialized_(false),
    frame_received_(false) {
}

PepperView::~PepperView() {
  DCHECK(merge_buffer_ == NULL);
  DCHECK(buffers_.empty());
}

bool PepperView::Initialize() {
  DCHECK(!is_initialized_);

  is_initialized_ = true;
  InitiateDrawing();
  return true;
}

void PepperView::TearDown() {
  DCHECK(context_->main_message_loop()->BelongsToCurrentThread());
  DCHECK(is_initialized_);

  is_initialized_ = false;

  // The producer should now return any pending buffers. At this point, however,
  // ReturnBuffer() tasks scheduled by the producer will not be delivered,
  // so we free all the buffers once the producer's queue is empty.
  base::WaitableEvent done_event(true, false);
  producer_->RequestReturnBuffers(
      base::Bind(&base::WaitableEvent::Signal, base::Unretained(&done_event)));
  done_event.Wait();

  merge_buffer_ = NULL;
  while (!buffers_.empty()) {
    FreeBuffer(buffers_.front());
  }
}

void PepperView::SetConnectionState(protocol::ConnectionToHost::State state,
                                    protocol::ErrorCode error) {
  DCHECK(context_->main_message_loop()->BelongsToCurrentThread());

  switch (state) {
    case protocol::ConnectionToHost::CONNECTING:
      instance_->SetConnectionState(
          ChromotingInstance::STATE_CONNECTING,
          ConvertConnectionError(error));
      break;

    case protocol::ConnectionToHost::CONNECTED:
      instance_->SetConnectionState(
          ChromotingInstance::STATE_CONNECTED,
          ConvertConnectionError(error));
      break;

    case protocol::ConnectionToHost::CLOSED:
      instance_->SetConnectionState(
          ChromotingInstance::STATE_CLOSED,
          ConvertConnectionError(error));
      break;

    case protocol::ConnectionToHost::FAILED:
      instance_->SetConnectionState(
          ChromotingInstance::STATE_FAILED,
          ConvertConnectionError(error));
      break;
  }
}

protocol::ClipboardStub* PepperView::GetClipboardStub() {
  return instance_;
}

protocol::CursorShapeStub* PepperView::GetCursorShapeStub() {
  return instance_;
}

void PepperView::SetView(const SkISize& view_size, const SkIRect& clip_area) {
  bool view_changed = false;

  if (view_size_ != view_size) {
    view_changed = true;
    view_size_ = view_size;

    pp::Size pp_size = pp::Size(view_size_.width(), view_size_.height());
    graphics2d_ = pp::Graphics2D(instance_, pp_size, true);
    bool result = instance_->BindGraphics(graphics2d_);

    // There is no good way to handle this error currently.
    DCHECK(result) << "Couldn't bind the device context.";
  }

  if (clip_area_ != clip_area) {
    view_changed = true;

    // YUV to RGB conversion may require even X and Y coordinates for
    // the top left corner of the clipping area.
    clip_area_ = AlignRect(clip_area);
    clip_area_.intersect(SkIRect::MakeSize(view_size_));
  }

  if (view_changed) {
    producer_->SetOutputSizeAndClip(view_size_, clip_area_);
    InitiateDrawing();
  }
}

void PepperView::ApplyBuffer(const SkISize& view_size,
                             const SkIRect& clip_area,
                             pp::ImageData* buffer,
                             const SkRegion& region) {
  DCHECK(context_->main_message_loop()->BelongsToCurrentThread());

  if (!frame_received_) {
    instance_->OnFirstFrameReceived();
    frame_received_ = true;
  }
  // Currently we cannot use the data in the buffer is scale factor has changed
  // already.
  // TODO(alexeypa): We could rescale and draw it (or even draw it without
  // rescaling) to reduce the perceived lag while we are waiting for
  // the properly scaled data.
  if (view_size_ != view_size) {
    FreeBuffer(buffer);
    InitiateDrawing();
  } else {
    FlushBuffer(clip_area, buffer, region);
  }
}

void PepperView::ReturnBuffer(pp::ImageData* buffer) {
  DCHECK(context_->main_message_loop()->BelongsToCurrentThread());

  // Free the buffer if there is nothing to paint.
  if (!is_initialized_) {
    FreeBuffer(buffer);
    return;
  }

  // Reuse the buffer if it is large enough, otherwise drop it on the floor
  // and allocate a new one.
  if (buffer->size().width() >= clip_area_.width() &&
      buffer->size().height() >= clip_area_.height()) {
    producer_->DrawBuffer(buffer);
  } else {
    FreeBuffer(buffer);
    InitiateDrawing();
  }
}

void PepperView::SetSourceSize(const SkISize& source_size) {
  DCHECK(context_->main_message_loop()->BelongsToCurrentThread());

  if (source_size_ == source_size)
    return;

  source_size_ = source_size;

  // Notify JavaScript of the change in source size.
  instance_->SetDesktopSize(source_size.width(), source_size.height());
}

pp::ImageData* PepperView::AllocateBuffer() {
  if (buffers_.size() >= kMaxPendingBuffersCount)
    return NULL;

  pp::Size pp_size = pp::Size(clip_area_.width(), clip_area_.height());
  if (pp_size.IsEmpty())
    return NULL;

  // Create an image buffer of the required size, but don't zero it.
  pp::ImageData* buffer =  new pp::ImageData(
      instance_, PP_IMAGEDATAFORMAT_BGRA_PREMUL, pp_size, false);
  if (buffer->is_null()) {
    LOG(WARNING) << "Not enough memory for frame buffers.";
    delete buffer;
    return NULL;
  }

  buffers_.push_back(buffer);
  return buffer;
}

void PepperView::FreeBuffer(pp::ImageData* buffer) {
  DCHECK(std::find(buffers_.begin(), buffers_.end(), buffer) != buffers_.end());

  buffers_.remove(buffer);
  delete buffer;
}

void PepperView::InitiateDrawing() {
  if (!is_initialized_)
    return;

  pp::ImageData* buffer = AllocateBuffer();
  while (buffer) {
    producer_->DrawBuffer(buffer);
    buffer = AllocateBuffer();
  }
}

void PepperView::FlushBuffer(const SkIRect& clip_area,
                             pp::ImageData* buffer,
                             const SkRegion& region) {
  // Defer drawing if the flush is already in progress.
  if (flush_pending_) {
    // |merge_buffer_| is guaranteed to be free here because we allocate only
    // two buffers simultaneously. If more buffers are allowed this code should
    // apply all pending changes to the screen.
    DCHECK(merge_buffer_ == NULL);

    merge_clip_area_ = clip_area;
    merge_buffer_ = buffer;
    merge_region_ = region;
    return;
  }

  // Notify Pepper API about the updated areas and flush pixels to the screen.
  base::Time start_time = base::Time::Now();

  for (SkRegion::Iterator i(region); !i.done(); i.next()) {
    SkIRect rect = i.rect();

    // Re-clip |region| with the current clipping area |clip_area_| because
    // the latter could change from the time the buffer was drawn.
    if (!rect.intersect(clip_area_))
      continue;

    // Specify the rectangle coordinates relative to the clipping area.
    rect.offset(-clip_area.left(), -clip_area.top());

    // Pepper Graphics 2D has a strange and badly documented API that the
    // point here is the offset from the source rect. Why?
    graphics2d_.PaintImageData(
        *buffer,
        pp::Point(clip_area.left(), clip_area.top()),
        pp::Rect(rect.left(), rect.top(), rect.width(), rect.height()));
  }

  // Notify the producer that some parts of the region weren't painted because
  // the clipping area has changed already.
  if (clip_area != clip_area_) {
    SkRegion not_painted = region;
    not_painted.op(clip_area_, SkRegion::kDifference_Op);
    if (!not_painted.isEmpty()) {
      producer_->InvalidateRegion(not_painted);
    }
  }

  // Flush the updated areas to the screen.
  int error = graphics2d_.Flush(
      PpCompletionCallback(base::Bind(
          &PepperView::OnFlushDone, AsWeakPtr(), start_time, buffer)));
  CHECK(error == PP_OK_COMPLETIONPENDING);
  flush_pending_ = true;
}

void PepperView::OnFlushDone(base::Time paint_start,
                             pp::ImageData* buffer,
                             int result) {
  DCHECK(context_->main_message_loop()->BelongsToCurrentThread());
  DCHECK(flush_pending_);

  instance_->GetStats()->video_paint_ms()->Record(
      (base::Time::Now() - paint_start).InMilliseconds());

  flush_pending_ = false;
  ReturnBuffer(buffer);

  // If there is a buffer queued for rendering then render it now.
  if (merge_buffer_ != NULL) {
    buffer = merge_buffer_;
    merge_buffer_ = NULL;
    FlushBuffer(merge_clip_area_, buffer, merge_region_);
  }
}

}  // namespace remoting