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_3D_H_
6 #define REMOTING_CLIENT_PLUGIN_PEPPER_VIDEO_RENDERER_3D_H_
11 #include "base/basictypes.h"
12 #include "base/callback.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "ppapi/cpp/graphics_3d.h"
15 #include "ppapi/cpp/instance_handle.h"
16 #include "ppapi/cpp/video_decoder.h"
17 #include "ppapi/utility/completion_callback_factory.h"
18 #include "remoting/client/chromoting_stats.h"
19 #include "remoting/client/plugin/pepper_video_renderer.h"
20 #include "remoting/protocol/video_stub.h"
21 #include "third_party/webrtc/modules/desktop_capture/desktop_geometry.h"
22 #include "third_party/webrtc/modules/desktop_capture/desktop_region.h"
28 // PepperVideoRenderer that uses the PPB_VideoDecoder interface for video
29 // decoding and Graphics3D for rendering.
30 class PepperVideoRenderer3D
: public PepperVideoRenderer
,
31 public protocol::VideoStub
{
33 PepperVideoRenderer3D();
34 ~PepperVideoRenderer3D() override
;
36 // PepperVideoRenderer interface.
37 bool Initialize(pp::Instance
* instance
,
38 const ClientContext
& context
,
39 EventHandler
* event_handler
) override
;
40 void OnViewChanged(const pp::View
& view
) override
;
41 void EnableDebugDirtyRegion(bool enable
) override
;
43 // VideoRenderer interface.
44 void OnSessionConfig(const protocol::SessionConfig
& config
) override
;
45 ChromotingStats
* GetStats() override
;
46 protocol::VideoStub
* GetVideoStub() override
;
48 // protocol::VideoStub interface.
49 void ProcessVideoPacket(scoped_ptr
<VideoPacket
> packet
,
50 const base::Closure
& done
) override
;
56 struct FrameDecodeTimestamp
{
57 FrameDecodeTimestamp(uint32_t frame_id
,
58 base::TimeTicks decode_started_time
);
60 base::TimeTicks decode_started_time
;
63 // Callback for pp::VideoDecoder::Initialize().
64 void OnInitialized(int32_t result
);
66 // Passes one picture from |pending_packets_| to the |video_decoder_|.
67 void DecodeNextPacket();
69 // Callback for pp::VideoDecoder::Decode().
70 void OnDecodeDone(int32_t result
);
72 // Fetches next picture from the |video_decoder_|.
73 void GetNextPicture();
75 // Callback for pp::VideoDecoder::GetPicture().
76 void OnPictureReady(int32_t result
, PP_VideoPicture picture
);
78 // Copies |next_picture_| to |current_picture_| if |next_picture_| is set and
79 // then renders |current_picture_|. Doesn't do anything if |need_repaint_| is
83 // Callback for pp::Graphics3D::SwapBuffers().
84 void OnPaintDone(int32_t result
);
86 // Initializes |shader_program_| for |texture_target|.
87 void EnsureProgramForTexture(uint32_t texture_target
);
89 // Initializes |shader_program_| with the given shaders.
90 void CreateProgram(const char* vertex_shader
, const char* fragment_shader
);
92 // Creates a new shader and compiles |source| for it.
93 void CreateShaderProgram(int type
, const char* source
);
95 // CHECKs that the last OpenGL call has completed successfully.
98 EventHandler
* event_handler_
;
100 pp::Graphics3D graphics_
;
101 const PPB_OpenGLES2
* gles2_if_
;
102 pp::VideoDecoder video_decoder_
;
104 webrtc::DesktopSize frame_size_
;
105 webrtc::DesktopVector frame_dpi_
;
106 scoped_ptr
<webrtc::DesktopRegion
> frame_shape_
;
108 webrtc::DesktopSize view_size_
;
110 ChromotingStats stats_
;
111 int64 latest_input_event_timestamp_
;
113 bool initialization_finished_
;
114 bool decode_pending_
;
115 bool get_picture_pending_
;
118 uint32_t latest_frame_id_
;
120 // Queue of packets that that have been received, but haven't been passed to
122 std::deque
<PendingPacket
*> pending_packets_
;
124 // Timestamps for all frames currently being processed by the decoder.
125 std::deque
<FrameDecodeTimestamp
> frame_decode_timestamps_
;
127 // The current picture shown on the screen or being rendered. Must be deleted
128 // before |video_decoder_|.
129 scoped_ptr
<Picture
> current_picture_
;
131 // The next picture to be rendered. PaintIfNeeded() will copy it to
132 // |current_picture_| and render it after that. Must be deleted
133 // before |video_decoder_|.
134 scoped_ptr
<Picture
> next_picture_
;
136 // Set to true if the screen has been resized and needs to be repainted.
139 // Time the last paint operation was started.
140 base::TimeTicks latest_paint_started_time_
;
142 // The texture type for which |shader_program| was initialized. Can be either
143 // 0, GL_TEXTURE_2D, GL_TEXTURE_RECTANGLE_ARB or GL_TEXTURE_EXTERNAL_OES. 0
144 // indicates that |shader_program_| hasn't been intialized.
145 uint32_t current_shader_program_texture_target_
;
147 // Shader program ID.
148 unsigned int shader_program_
;
150 // Location of the scale value to be passed to the |shader_program_|.
151 int shader_texcoord_scale_location_
;
153 // True if dirty regions are to be sent to |event_handler_| for debugging.
154 bool debug_dirty_region_
;
156 pp::CompletionCallbackFactory
<PepperVideoRenderer3D
> callback_factory_
;
158 DISALLOW_COPY_AND_ASSIGN(PepperVideoRenderer3D
);
161 } // namespace remoting
163 #endif // REMOTING_CLIENT_PLUGIN_PEPPER_VIDEO_RENDERER_3D_H_