1 // Copyright 2014 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.
5 #ifndef REMOTING_CLIENT_PLUGIN_PEPPER_VIDEO_RENDERER_2D_H_
6 #define REMOTING_CLIENT_PLUGIN_PEPPER_VIDEO_RENDERER_2D_H_
10 #include "base/compiler_specific.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/threading/non_thread_safe.h"
14 #include "ppapi/cpp/graphics_2d.h"
15 #include "ppapi/cpp/point.h"
16 #include "ppapi/cpp/view.h"
17 #include "ppapi/utility/completion_callback_factory.h"
18 #include "remoting/client/frame_consumer.h"
19 #include "remoting/client/plugin/pepper_video_renderer.h"
20 #include "third_party/webrtc/modules/desktop_capture/desktop_geometry.h"
21 #include "third_party/webrtc/modules/desktop_capture/desktop_region.h"
33 class SoftwareVideoRenderer
;
35 // Video renderer that wraps SoftwareVideoRenderer and displays it using Pepper
37 class PepperVideoRenderer2D
: public PepperVideoRenderer
,
39 public base::NonThreadSafe
{
41 PepperVideoRenderer2D();
42 ~PepperVideoRenderer2D() override
;
44 // PepperVideoRenderer interface.
45 bool Initialize(pp::Instance
* instance
,
46 const ClientContext
& context
,
47 EventHandler
* event_handler
) override
;
48 void OnViewChanged(const pp::View
& view
) override
;
49 void EnableDebugDirtyRegion(bool enable
) override
;
51 // VideoRenderer interface.
52 void OnSessionConfig(const protocol::SessionConfig
& config
) override
;
53 ChromotingStats
* GetStats() override
;
54 protocol::VideoStub
* GetVideoStub() override
;
57 // FrameConsumer implementation.
58 void ApplyBuffer(const webrtc::DesktopSize
& view_size
,
59 const webrtc::DesktopRect
& clip_area
,
60 webrtc::DesktopFrame
* buffer
,
61 const webrtc::DesktopRegion
& region
,
62 const webrtc::DesktopRegion
* shape
) override
;
63 void ReturnBuffer(webrtc::DesktopFrame
* buffer
) override
;
64 void SetSourceSize(const webrtc::DesktopSize
& source_size
,
65 const webrtc::DesktopVector
& dpi
) override
;
66 PixelFormat
GetPixelFormat() override
;
68 // Helper to allocate buffers for the decoder.
69 void AllocateBuffers();
71 // Frees a frame buffer previously allocated by AllocateBuffer.
72 void FreeBuffer(webrtc::DesktopFrame
* buffer
);
74 // Renders the parts of |buffer| identified by |region| to the view. If the
75 // clip area of the view has changed since the buffer was generated then
76 // FrameProducer is supplied the missed parts of |region|. The FrameProducer
77 // will be supplied a new buffer when FlushBuffer() completes.
78 void FlushBuffer(const webrtc::DesktopRect
& clip_area
,
79 webrtc::DesktopFrame
* buffer
,
80 const webrtc::DesktopRegion
& region
);
82 // Handles completion of FlushBuffer(), triggering a new buffer to be
83 // returned to FrameProducer for rendering.
84 void OnFlushDone(int result
,
85 const base::Time
& paint_start
,
86 webrtc::DesktopFrame
* buffer
);
88 // Parameters passed to Initialize().
89 pp::Instance
* instance_
;
90 EventHandler
* event_handler_
;
92 pp::Graphics2D graphics2d_
;
94 scoped_ptr
<SoftwareVideoRenderer
> software_video_renderer_
;
96 // List of allocated image buffers.
97 std::list
<webrtc::DesktopFrame
*> buffers_
;
99 // Queued buffer to paint, with clip area and dirty region in device pixels.
100 webrtc::DesktopFrame
* merge_buffer_
;
101 webrtc::DesktopRect merge_clip_area_
;
102 webrtc::DesktopRegion merge_region_
;
104 // View size in Density Independent Pixels (DIPs).
105 webrtc::DesktopSize dips_size_
;
107 // Scale factor from DIPs to device pixels.
108 float dips_to_device_scale_
;
110 // View size in output pixels. This is the size at which FrameProducer must
111 // render frames. It usually matches the DIPs size of the view, but may match
112 // the size in device pixels when scaling is in effect, to reduce artefacts.
113 webrtc::DesktopSize view_size_
;
115 // Scale factor from output pixels to device pixels.
116 float dips_to_view_scale_
;
118 // Visible area of the view, in output pixels.
119 webrtc::DesktopRect clip_area_
;
121 // Size of the most recent source frame in pixels.
122 webrtc::DesktopSize source_size_
;
124 // Resolution of the most recent source frame dots-per-inch.
125 webrtc::DesktopVector source_dpi_
;
127 // Shape of the most recent source frame.
128 scoped_ptr
<webrtc::DesktopRegion
> source_shape_
;
130 // True if there is already a Flush() pending on the Graphics2D context.
133 // True after the first call to ApplyBuffer().
134 bool frame_received_
;
136 // True if dirty regions are to be sent to |event_handler_| for debugging.
137 bool debug_dirty_region_
;
139 pp::CompletionCallbackFactory
<PepperVideoRenderer2D
> callback_factory_
;
140 base::WeakPtrFactory
<PepperVideoRenderer2D
> weak_factory_
;
142 DISALLOW_COPY_AND_ASSIGN(PepperVideoRenderer2D
);
145 } // namespace remoting
147 #endif // REMOTING_CLIENT_PLUGIN_PEPPER_VIDEO_RENDERER_2D_H_