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/cancelable_callback.h"
14 #include "base/time/time.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"
27 class DisplayConfigurator
;
32 class VideoFrameTexture
: public base::RefCounted
<VideoFrameTexture
> {
34 uint32
texture_id() const { return texture_id_
; }
35 uint32
texture_target() const { return texture_target_
; }
37 VideoFrameTexture(uint32 texture_target
,
39 const base::Closure
& no_longer_needed_cb
);
42 friend class base::RefCounted
<VideoFrameTexture
>;
44 uint32 texture_target_
;
46 base::Closure no_longer_needed_cb_
;
51 struct RenderingHelperParams
{
52 RenderingHelperParams();
53 ~RenderingHelperParams();
58 // The number of empty frames rendered when the rendering helper is
60 int warm_up_iterations
;
62 // The desired size of each window. We play each stream in its own window
64 std::vector
<gfx::Size
> window_sizes
;
66 // The members below are only used for the thumbnail mode where all frames
67 // are rendered in sequence onto one FBO for comparison/verification purposes.
69 // Whether the frames are rendered as scaled thumbnails within a
70 // larger FBO that is in turn rendered to the window.
71 bool render_as_thumbnails
;
72 // The size of the FBO containing all visible thumbnails.
73 gfx::Size thumbnails_page_size
;
74 // The size of each thumbnail within the FBO.
75 gfx::Size thumbnail_size
;
78 // Creates and draws textures used by the video decoder.
79 // This class is not thread safe and thus all the methods of this class
80 // (except for ctor/dtor) ensure they're being run on a single thread.
81 class RenderingHelper
{
87 // Initialize GL. This method must be called on the rendering
89 static void InitializeOneOff(base::WaitableEvent
* done
);
91 // Setup the platform window to display test results. This method
92 // must be called on the main thread.
95 // Tear down the platform window. This method must be called on the
99 // Create the render context and windows by the specified
100 // dimensions. This method must be called on the rendering thread.
101 void Initialize(const RenderingHelperParams
& params
,
102 base::WaitableEvent
* done
);
104 // Undo the effects of Initialize() and signal |*done|. This method
105 // must be called on the rendering thread.
106 void UnInitialize(base::WaitableEvent
* done
);
108 // Return a newly-created GLES2 texture id of the specified size, and
110 void CreateTexture(uint32 texture_target
,
112 const gfx::Size
& size
,
113 base::WaitableEvent
* done
);
115 // Render thumbnail in the |texture_id| to the FBO buffer using target
117 void RenderThumbnail(uint32 texture_target
, uint32 texture_id
);
119 // Queues the |video_frame| for rendering.
120 void QueueVideoFrame(size_t window_id
,
121 scoped_refptr
<VideoFrameTexture
> video_frame
);
123 // Flushes the pending frames. Notify the rendering_helper there won't be
124 // more video frames.
125 void Flush(size_t window_id
);
127 // Delete |texture_id|.
128 void DeleteTexture(uint32 texture_id
);
130 // Get the platform specific handle to the OpenGL display.
131 void* GetGLDisplay();
133 // Get the GL context.
134 scoped_refptr
<gfx::GLContext
> GetGLContext();
136 // Get the platform specific handle to the OpenGL context.
137 void* GetGLContextHandle();
139 // Get rendered thumbnails as RGB.
140 // Sets alpha_solid to true if the alpha channel is entirely 0xff.
141 void GetThumbnailsAsRGB(std::vector
<unsigned char>* rgb
,
143 base::WaitableEvent
* done
);
146 struct RenderedVideo
{
147 // The rect on the screen where the video will be rendered.
148 gfx::Rect render_area
;
150 // True if there won't be any new video frames comming.
153 // The number of frames need to be dropped to catch up the rendering. We
154 // always keep the last remaining frame in pending_frames even after it
155 // has been rendered, so that we have something to display if the client
156 // is falling behind on providing us with new frames during timer-driven
160 // The video frames pending for rendering.
161 std::queue
<scoped_refptr
<VideoFrameTexture
> > pending_frames
;
169 void RenderContent();
171 void WarmUpRendering(int warm_up_iterations
);
173 void LayoutRenderingAreas(const std::vector
<gfx::Size
>& window_sizes
);
175 void UpdateVSyncParameters(base::WaitableEvent
* done
,
176 const base::TimeTicks timebase
,
177 const base::TimeDelta interval
);
179 void DropOneFrameForAllVideos();
180 void ScheduleNextRenderContent();
182 // Render |texture_id| to the current view port of the screen using target
184 void RenderTexture(uint32 texture_target
, uint32 texture_id
);
186 base::MessageLoop
* message_loop_
;
188 scoped_refptr
<gfx::GLContext
> gl_context_
;
189 scoped_refptr
<gfx::GLSurface
> gl_surface_
;
191 #if defined(USE_OZONE)
192 class StubOzoneDelegate
;
193 scoped_ptr
<StubOzoneDelegate
> platform_window_delegate_
;
195 #if defined(OS_CHROMEOS)
196 scoped_ptr
<ui::DisplayConfigurator
> display_configurator_
;
202 gfx::AcceleratedWidget window_
;
204 gfx::Size screen_size_
;
206 std::vector
<RenderedVideo
> videos_
;
208 bool render_as_thumbnails_
;
210 GLuint thumbnails_fbo_id_
;
211 GLuint thumbnails_texture_id_
;
212 gfx::Size thumbnails_fbo_size_
;
213 gfx::Size thumbnail_size_
;
215 base::TimeDelta frame_duration_
;
216 base::TimeTicks scheduled_render_time_
;
217 base::CancelableClosure render_task_
;
218 base::TimeTicks vsync_timebase_
;
219 base::TimeDelta vsync_interval_
;
221 DISALLOW_COPY_AND_ASSIGN(RenderingHelper
);
224 } // namespace content
226 #endif // CONTENT_COMMON_GPU_MEDIA_RENDERING_HELPER_H_