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