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.
5 #ifndef CONTENT_COMMON_GPU_MEDIA_RENDERING_HELPER_H_
6 #define CONTENT_COMMON_GPU_MEDIA_RENDERING_HELPER_H_
12 #include "base/basictypes.h"
13 #include "base/time/time.h"
14 #include "base/timer/timer.h"
15 #include "ui/gfx/geometry/rect.h"
16 #include "ui/gfx/geometry/size.h"
17 #include "ui/gl/gl_bindings.h"
18 #include "ui/gl/gl_context.h"
19 #include "ui/gl/gl_surface.h"
28 class VideoFrameTexture
: public base::RefCounted
<VideoFrameTexture
> {
30 uint32
texture_id() const { return texture_id_
; }
31 uint32
texture_target() const { return texture_target_
; }
33 VideoFrameTexture(uint32 texture_target
,
35 const base::Closure
& no_longer_needed_cb
);
38 friend class base::RefCounted
<VideoFrameTexture
>;
40 uint32 texture_target_
;
42 base::Closure no_longer_needed_cb_
;
47 struct RenderingHelperParams
{
48 RenderingHelperParams();
49 ~RenderingHelperParams();
54 // The desired size of each window. We play each stream in its own window
56 std::vector
<gfx::Size
> window_sizes
;
58 // The members below are only used for the thumbnail mode where all frames
59 // are rendered in sequence onto one FBO for comparison/verification purposes.
61 // Whether the frames are rendered as scaled thumbnails within a
62 // larger FBO that is in turn rendered to the window.
63 bool render_as_thumbnails
;
64 // The size of the FBO containing all visible thumbnails.
65 gfx::Size thumbnails_page_size
;
66 // The size of each thumbnail within the FBO.
67 gfx::Size thumbnail_size
;
70 // Creates and draws textures used by the video decoder.
71 // This class is not thread safe and thus all the methods of this class
72 // (except for ctor/dtor) ensure they're being run on a single thread.
73 class RenderingHelper
{
79 static bool InitializeOneOff();
81 // Create the render context and windows by the specified dimensions.
82 void Initialize(const RenderingHelperParams
& params
,
83 base::WaitableEvent
* done
);
85 // Undo the effects of Initialize() and signal |*done|.
86 void UnInitialize(base::WaitableEvent
* done
);
88 // Return a newly-created GLES2 texture id of the specified size, and
90 void CreateTexture(uint32 texture_target
,
92 const gfx::Size
& size
,
93 base::WaitableEvent
* done
);
95 // Render thumbnail in the |texture_id| to the FBO buffer using target
97 void RenderThumbnail(uint32 texture_target
, uint32 texture_id
);
99 // Queues the |video_frame| for rendering.
100 void QueueVideoFrame(size_t window_id
,
101 scoped_refptr
<VideoFrameTexture
> video_frame
);
103 // Flushes the pending frames. Notify the rendering_helper there won't be
104 // more video frames.
105 void Flush(size_t window_id
);
107 // Delete |texture_id|.
108 void DeleteTexture(uint32 texture_id
);
110 // Get the platform specific handle to the OpenGL display.
111 void* GetGLDisplay();
113 // Get the platform specific handle to the OpenGL context.
114 void* GetGLContext();
116 // Get rendered thumbnails as RGB.
117 // Sets alpha_solid to true if the alpha channel is entirely 0xff.
118 void GetThumbnailsAsRGB(std::vector
<unsigned char>* rgb
,
120 base::WaitableEvent
* done
);
123 struct RenderedVideo
{
124 // The rect on the screen where the video will be rendered.
125 gfx::Rect render_area
;
127 // True if the last (and the only one) frame in pending_frames has
128 // been rendered. We keep the last remaining frame in pending_frames even
129 // after it has been rendered, so that we have something to display if the
130 // client is falling behind on providing us with new frames during
131 // timer-driven playback.
132 bool last_frame_rendered
;
134 // True if there won't be any new video frames comming.
137 // The video frames pending for rendering.
138 std::queue
<scoped_refptr
<VideoFrameTexture
> > pending_frames
;
146 void RenderContent();
148 void LayoutRenderingAreas(const std::vector
<gfx::Size
>& window_sizes
);
150 // Render |texture_id| to the current view port of the screen using target
152 void RenderTexture(uint32 texture_target
, uint32 texture_id
);
154 // Timer to trigger the RenderContent() repeatly.
155 scoped_ptr
<base::RepeatingTimer
<RenderingHelper
> > render_timer_
;
156 base::MessageLoop
* message_loop_
;
158 scoped_refptr
<gfx::GLContext
> gl_context_
;
159 scoped_refptr
<gfx::GLSurface
> gl_surface_
;
161 gfx::AcceleratedWidget window_
;
163 gfx::Size screen_size_
;
165 std::vector
<RenderedVideo
> videos_
;
167 bool render_as_thumbnails_
;
169 GLuint thumbnails_fbo_id_
;
170 GLuint thumbnails_texture_id_
;
171 gfx::Size thumbnails_fbo_size_
;
172 gfx::Size thumbnail_size_
;
174 base::TimeDelta frame_duration_
;
176 DISALLOW_COPY_AND_ASSIGN(RenderingHelper
);
179 } // namespace content
181 #endif // CONTENT_COMMON_GPU_MEDIA_RENDERING_HELPER_H_