summaryrefslogtreecommitdiffstats
path: root/remoting/client/plugin/pepper_video_renderer_2d.h
blob: 014ccec31154692cfd4f2c0f54454589274e0636 (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
// Copyright 2014 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.

#ifndef REMOTING_CLIENT_PLUGIN_PEPPER_VIDEO_RENDERER_2D_H_
#define REMOTING_CLIENT_PLUGIN_PEPPER_VIDEO_RENDERER_2D_H_

#include <list>

#include "base/compiler_specific.h"
#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_vector.h"
#include "base/memory/weak_ptr.h"
#include "base/threading/thread_checker.h"
#include "ppapi/cpp/graphics_2d.h"
#include "ppapi/cpp/image_data.h"
#include "ppapi/cpp/point.h"
#include "ppapi/cpp/view.h"
#include "ppapi/utility/completion_callback_factory.h"
#include "remoting/client/plugin/pepper_video_renderer.h"
#include "remoting/protocol/frame_consumer.h"
#include "third_party/webrtc/modules/desktop_capture/desktop_geometry.h"
#include "third_party/webrtc/modules/desktop_capture/desktop_region.h"

namespace base {
class ScopedClosureRunner;
}  // namespace base

namespace webrtc {
class DesktopFrame;
class SharedDesktopFrame;
}  // namespace webrtc

namespace remoting {

class SoftwareVideoRenderer;

// Video renderer that wraps SoftwareVideoRenderer and displays it using Pepper
// 2D graphics API.
class PepperVideoRenderer2D : public PepperVideoRenderer,
                              public protocol::FrameConsumer {
 public:
  PepperVideoRenderer2D();
  ~PepperVideoRenderer2D() override;

  // PepperVideoRenderer interface.
  bool Initialize(pp::Instance* instance,
                  const ClientContext& context,
                  EventHandler* event_handler,
                  protocol::PerformanceTracker* perf_tracker) override;
  void OnViewChanged(const pp::View& view) override;
  void EnableDebugDirtyRegion(bool enable) override;

  // VideoRenderer interface.
  void OnSessionConfig(const protocol::SessionConfig& config) override;
  protocol::VideoStub* GetVideoStub() override;
  protocol::FrameConsumer* GetFrameConsumer() override;

 private:
  // protocol::FrameConsumer implementation.
  scoped_ptr<webrtc::DesktopFrame> AllocateFrame(
      const webrtc::DesktopSize& size) override;
  void DrawFrame(scoped_ptr<webrtc::DesktopFrame> frame,
                 const base::Closure& done) override;
  PixelFormat GetPixelFormat() override;


  void Flush();
  void OnFlushDone(int result);

  // Parameters passed to Initialize().
  pp::Instance* instance_ = nullptr;
  EventHandler* event_handler_ = nullptr;

  pp::Graphics2D graphics2d_;

  scoped_ptr<SoftwareVideoRenderer> software_video_renderer_;

  // View size in output pixels.
  webrtc::DesktopSize view_size_;

  // Size of the most recent source frame in pixels.
  webrtc::DesktopSize source_size_;

  // Resolution of the most recent source frame dots-per-inch.
  webrtc::DesktopVector source_dpi_;

  // Shape of the most recent source frame.
  scoped_ptr<webrtc::DesktopRegion> source_shape_;

  // Done callbacks for the frames that have been painted but not flushed.
  ScopedVector<base::ScopedClosureRunner> pending_frames_done_callbacks_;

  // Done callbacks for the frames that are currently being flushed.
  ScopedVector<base::ScopedClosureRunner> flushing_frames_done_callbacks_;

  // True if there paint operations that need to be flushed.
  bool need_flush_ = false;

  // True if there is already a Flush() pending on the Graphics2D context.
  bool flush_pending_ = false;

  // True after the first call to DrawFrame().
  bool frame_received_ = false;

  // True if dirty regions are to be sent to |event_handler_| for debugging.
  bool debug_dirty_region_ = false;

  base::ThreadChecker thread_checker_;

  pp::CompletionCallbackFactory<PepperVideoRenderer2D> callback_factory_;
  base::WeakPtrFactory<PepperVideoRenderer2D> weak_factory_;

  DISALLOW_COPY_AND_ASSIGN(PepperVideoRenderer2D);
};

}  // namespace remoting

#endif  // REMOTING_CLIENT_PLUGIN_PEPPER_VIDEO_RENDERER_2D_H_