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// This class is an implementation of the ChromotingView for Pepper.  It is
6// callable only on the Pepper thread.
7
8#ifndef REMOTING_CLIENT_PLUGIN_PEPPER_VIEW_H_
9#define REMOTING_CLIENT_PLUGIN_PEPPER_VIEW_H_
10
11#include <list>
12
13#include "base/compiler_specific.h"
14#include "ppapi/cpp/graphics_2d.h"
15#include "ppapi/cpp/view.h"
16#include "ppapi/cpp/point.h"
17#include "ppapi/utility/completion_callback_factory.h"
18#include "remoting/client/frame_consumer.h"
19#include "third_party/webrtc/modules/desktop_capture/desktop_geometry.h"
20#include "third_party/webrtc/modules/desktop_capture/desktop_region.h"
21
22namespace base {
23class Time;
24}  // namespace base
25
26namespace webrtc {
27class DesktopFrame;
28}  // namespace webrtc
29
30namespace remoting {
31
32class ChromotingInstance;
33class ClientContext;
34class FrameProducer;
35
36class PepperView : public FrameConsumer {
37 public:
38  // Constructs a PepperView for the |instance|. The |instance| and |context|
39  // must outlive this class.
40  PepperView(ChromotingInstance* instance, ClientContext* context);
41  virtual ~PepperView();
42
43  // Allocates buffers and passes them to the FrameProducer to render into until
44  // the maximum number of buffers are in-flight.
45  void Initialize(FrameProducer* producer);
46
47  // FrameConsumer implementation.
48  virtual void ApplyBuffer(const webrtc::DesktopSize& view_size,
49                           const webrtc::DesktopRect& clip_area,
50                           webrtc::DesktopFrame* buffer,
51                           const webrtc::DesktopRegion& region,
52                           const webrtc::DesktopRegion& shape) OVERRIDE;
53  virtual void ReturnBuffer(webrtc::DesktopFrame* buffer) OVERRIDE;
54  virtual void SetSourceSize(const webrtc::DesktopSize& source_size,
55                             const webrtc::DesktopVector& dpi) OVERRIDE;
56  virtual PixelFormat GetPixelFormat() OVERRIDE;
57
58  // Updates the PepperView's size & clipping area, taking into account the
59  // DIP-to-device scale factor.
60  void SetView(const pp::View& view);
61
62  // Returns the dimensions of the most recently displayed frame, in pixels.
63  const webrtc::DesktopSize& get_source_size() const {
64    return source_size_;
65  }
66
67 private:
68  // Allocates a new frame buffer to supply to the FrameProducer to render into.
69  // Returns NULL if the maximum number of buffers has already been allocated.
70  webrtc::DesktopFrame* AllocateBuffer();
71
72  // Frees a frame buffer previously allocated by AllocateBuffer.
73  void FreeBuffer(webrtc::DesktopFrame* buffer);
74
75  // Renders the parts of |buffer| identified by |region| to the view.  If the
76  // clip area of the view has changed since the buffer was generated then
77  // FrameProducer is supplied the missed parts of |region|.  The FrameProducer
78  // will be supplied a new buffer when FlushBuffer() completes.
79  void FlushBuffer(const webrtc::DesktopRect& clip_area,
80                   webrtc::DesktopFrame* buffer,
81                   const webrtc::DesktopRegion& region);
82
83  // Handles completion of FlushBuffer(), triggering a new buffer to be
84  // returned to FrameProducer for rendering.
85  void OnFlushDone(int result,
86                   const base::Time& paint_start,
87                   webrtc::DesktopFrame* buffer);
88
89  // Reference to the creating plugin instance. Needed for interacting with
90  // pepper.  Marking explicitly as const since it must be initialized at
91  // object creation, and never change.
92  ChromotingInstance* const instance_;
93
94  // Context should be constant for the lifetime of the plugin.
95  ClientContext* const context_;
96
97  pp::Graphics2D graphics2d_;
98
99  FrameProducer* producer_;
100
101  // List of allocated image buffers.
102  std::list<webrtc::DesktopFrame*> buffers_;
103
104  // Queued buffer to paint, with clip area and dirty region in device pixels.
105  webrtc::DesktopFrame* merge_buffer_;
106  webrtc::DesktopRect merge_clip_area_;
107  webrtc::DesktopRegion merge_region_;
108
109  // View size in Density Independent Pixels (DIPs).
110  webrtc::DesktopSize dips_size_;
111
112  // Scale factor from DIPs to device pixels.
113  float dips_to_device_scale_;
114
115  // View size in output pixels. This is the size at which FrameProducer must
116  // render frames. It usually matches the DIPs size of the view, but may match
117  // the size in device pixels when scaling is in effect, to reduce artefacts.
118  webrtc::DesktopSize view_size_;
119
120  // Scale factor from output pixels to device pixels.
121  float dips_to_view_scale_;
122
123  // Visible area of the view, in output pixels.
124  webrtc::DesktopRect clip_area_;
125
126  // Size of the most recent source frame in pixels.
127  webrtc::DesktopSize source_size_;
128
129  // Resolution of the most recent source frame dots-per-inch.
130  webrtc::DesktopVector source_dpi_;
131
132  // True if there is already a Flush() pending on the Graphics2D context.
133  bool flush_pending_;
134
135  // True after Initialize() has been called, until TearDown().
136  bool is_initialized_;
137
138  // True after the first call to ApplyBuffer().
139  bool frame_received_;
140
141  pp::CompletionCallbackFactory<PepperView> callback_factory_;
142
143  DISALLOW_COPY_AND_ASSIGN(PepperView);
144};
145
146}  // namespace remoting
147
148#endif  // REMOTING_CLIENT_PLUGIN_PEPPER_VIEW_H_
149