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 "base/time/time.h"
15 #include "ppapi/cpp/graphics_3d.h"
16 #include "ppapi/cpp/instance_handle.h"
17 #include "ppapi/cpp/video_decoder.h"
18 #include "ppapi/utility/completion_callback_factory.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
,
40 protocol::PerformanceTracker
* perf_tracker
) override
;
41 void OnViewChanged(const pp::View
& view
) override
;
42 void EnableDebugDirtyRegion(bool enable
) override
;
44 // VideoRenderer interface.
45 void OnSessionConfig(const protocol::SessionConfig
& config
) 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_
= nullptr;
99 protocol::PerformanceTracker
* perf_tracker_
= nullptr;
101 pp::Graphics3D graphics_
;
102 const PPB_OpenGLES2
* gles2_if_
;
103 pp::VideoDecoder video_decoder_
;
105 webrtc::DesktopSize frame_size_
;
106 webrtc::DesktopVector frame_dpi_
;
107 scoped_ptr
<webrtc::DesktopRegion
> frame_shape_
;
109 webrtc::DesktopSize view_size_
;
111 bool initialization_finished_
= false;
112 bool decode_pending_
= false;
113 bool get_picture_pending_
= false;
114 bool paint_pending_
= false;
116 uint32_t latest_frame_id_
= 0;
118 // Queue of packets that that have been received, but haven't been passed to
120 std::deque
<PendingPacket
*> pending_packets_
;
122 // Timestamps for all frames currently being processed by the decoder.
123 std::deque
<FrameDecodeTimestamp
> frame_decode_timestamps_
;
125 // The current picture shown on the screen or being rendered. Must be deleted
126 // before |video_decoder_|.
127 scoped_ptr
<Picture
> current_picture_
;
129 // The next picture to be rendered. PaintIfNeeded() will copy it to
130 // |current_picture_| and render it after that. Must be deleted
131 // before |video_decoder_|.
132 scoped_ptr
<Picture
> next_picture_
;
134 // Set to true if the screen has been resized and needs to be repainted.
135 bool force_repaint_
= false;
137 // Time the last paint operation was started.
138 base::TimeTicks latest_paint_started_time_
;
140 // The texture type for which |shader_program| was initialized. Can be either
141 // 0, GL_TEXTURE_2D, GL_TEXTURE_RECTANGLE_ARB or GL_TEXTURE_EXTERNAL_OES. 0
142 // indicates that |shader_program_| hasn't been intialized.
143 uint32_t current_shader_program_texture_target_
= 0;
145 // Shader program ID.
146 unsigned int shader_program_
= 0;
148 // Location of the scale value to be passed to the |shader_program_|.
149 int shader_texcoord_scale_location_
= 0;
151 // True if dirty regions are to be sent to |event_handler_| for debugging.
152 bool debug_dirty_region_
= false;
154 pp::CompletionCallbackFactory
<PepperVideoRenderer3D
> callback_factory_
;
156 DISALLOW_COPY_AND_ASSIGN(PepperVideoRenderer3D
);
159 } // namespace remoting
161 #endif // REMOTING_CLIENT_PLUGIN_PEPPER_VIDEO_RENDERER_3D_H_