[NaCl SDK]: use standard __BEGIN_DECLS macros in sys/select.h
[chromium-blink-merge.git] / content / common / gpu / media / rendering_helper.h
blob1ebc91bf1b65cc472a98320894666101eab3189b
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_
8 #include <map>
9 #include <queue>
10 #include <vector>
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"
21 namespace base {
22 class MessageLoop;
23 class WaitableEvent;
26 namespace content {
28 class VideoFrameTexture : public base::RefCounted<VideoFrameTexture> {
29 public:
30 uint32 texture_id() const { return texture_id_; }
31 uint32 texture_target() const { return texture_target_; }
33 VideoFrameTexture(uint32 texture_target,
34 uint32 texture_id,
35 const base::Closure& no_longer_needed_cb);
37 private:
38 friend class base::RefCounted<VideoFrameTexture>;
40 uint32 texture_target_;
41 uint32 texture_id_;
42 base::Closure no_longer_needed_cb_;
44 ~VideoFrameTexture();
47 struct RenderingHelperParams {
48 RenderingHelperParams();
49 ~RenderingHelperParams();
51 // The rendering FPS.
52 int rendering_fps;
54 // The desired size of each window. We play each stream in its own window
55 // on the screen.
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 {
74 public:
76 RenderingHelper();
77 ~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
89 // signal |*done|.
90 void CreateTexture(uint32 texture_target,
91 uint32* texture_id,
92 const gfx::Size& size,
93 base::WaitableEvent* done);
95 // Render thumbnail in the |texture_id| to the FBO buffer using target
96 // |texture_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,
119 bool* alpha_solid,
120 base::WaitableEvent* done);
122 private:
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.
135 bool is_flushing;
137 // The video frames pending for rendering.
138 std::queue<scoped_refptr<VideoFrameTexture> > pending_frames;
140 RenderedVideo();
141 ~RenderedVideo();
144 void Clear();
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
151 // |texture_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_;
168 int frame_count_;
169 GLuint thumbnails_fbo_id_;
170 GLuint thumbnails_texture_id_;
171 gfx::Size thumbnails_fbo_size_;
172 gfx::Size thumbnail_size_;
173 GLuint program_;
174 base::TimeDelta frame_duration_;
176 DISALLOW_COPY_AND_ASSIGN(RenderingHelper);
179 } // namespace content
181 #endif // CONTENT_COMMON_GPU_MEDIA_RENDERING_HELPER_H_