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 FrameConsumerProxy
;
34 class SoftwareVideoRenderer
;
36 // Video renderer that wraps SoftwareVideoRenderer and displays it using Pepper
38 class PepperVideoRenderer2D
: public PepperVideoRenderer
,
40 public base::NonThreadSafe
{
42 PepperVideoRenderer2D();
43 ~PepperVideoRenderer2D() override
;
45 // PepperVideoRenderer interface.
46 bool Initialize(pp::Instance
* instance
,
47 const ClientContext
& context
,
48 EventHandler
* event_handler
) override
;
49 void OnViewChanged(const pp::View
& view
) override
;
50 void EnableDebugDirtyRegion(bool enable
) override
;
52 // VideoRenderer interface.
53 void OnSessionConfig(const protocol::SessionConfig
& config
) override
;
54 ChromotingStats
* GetStats() override
;
55 protocol::VideoStub
* GetVideoStub() override
;
58 // FrameConsumer implementation.
59 void ApplyBuffer(const webrtc::DesktopSize
& view_size
,
60 const webrtc::DesktopRect
& clip_area
,
61 webrtc::DesktopFrame
* buffer
,
62 const webrtc::DesktopRegion
& region
,
63 const webrtc::DesktopRegion
& shape
) override
;
64 void ReturnBuffer(webrtc::DesktopFrame
* buffer
) override
;
65 void SetSourceSize(const webrtc::DesktopSize
& source_size
,
66 const webrtc::DesktopVector
& dpi
) override
;
67 PixelFormat
GetPixelFormat() override
;
69 // Helper to allocate buffers for the decoder.
70 void AllocateBuffers();
72 // Frees a frame buffer previously allocated by AllocateBuffer.
73 void FreeBuffer(webrtc::DesktopFrame
* buffer
);
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
);
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
);
89 // Parameters passed to Initialize().
90 pp::Instance
* instance_
;
91 EventHandler
* event_handler_
;
93 pp::Graphics2D graphics2d_
;
95 scoped_refptr
<FrameConsumerProxy
> frame_consumer_proxy_
;
96 scoped_ptr
<SoftwareVideoRenderer
> software_video_renderer_
;
98 // List of allocated image buffers.
99 std::list
<webrtc::DesktopFrame
*> buffers_
;
101 // Queued buffer to paint, with clip area and dirty region in device pixels.
102 webrtc::DesktopFrame
* merge_buffer_
;
103 webrtc::DesktopRect merge_clip_area_
;
104 webrtc::DesktopRegion merge_region_
;
106 // View size in Density Independent Pixels (DIPs).
107 webrtc::DesktopSize dips_size_
;
109 // Scale factor from DIPs to device pixels.
110 float dips_to_device_scale_
;
112 // View size in output pixels. This is the size at which FrameProducer must
113 // render frames. It usually matches the DIPs size of the view, but may match
114 // the size in device pixels when scaling is in effect, to reduce artefacts.
115 webrtc::DesktopSize view_size_
;
117 // Scale factor from output pixels to device pixels.
118 float dips_to_view_scale_
;
120 // Visible area of the view, in output pixels.
121 webrtc::DesktopRect clip_area_
;
123 // Size of the most recent source frame in pixels.
124 webrtc::DesktopSize source_size_
;
126 // Resolution of the most recent source frame dots-per-inch.
127 webrtc::DesktopVector source_dpi_
;
129 // True if there is already a Flush() pending on the Graphics2D context.
132 // True after the first call to ApplyBuffer().
133 bool frame_received_
;
135 // True if dirty regions are to be sent to |event_handler_| for debugging.
136 bool debug_dirty_region_
;
138 pp::CompletionCallbackFactory
<PepperVideoRenderer2D
> callback_factory_
;
139 base::WeakPtrFactory
<PepperVideoRenderer2D
> weak_factory_
;
141 DISALLOW_COPY_AND_ASSIGN(PepperVideoRenderer2D
);
144 } // namespace remoting
146 #endif // REMOTING_CLIENT_PLUGIN_PEPPER_VIDEO_RENDERER_2D_H_