[Android WebView] Fix webview perf bot switchover to use org.chromium.webview_shell...
[chromium-blink-merge.git] / content / common / gpu / media / rendering_helper.h
blob31e5f7e69b531d9029cf45e51c59590b94f62bb6
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/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"
21 namespace base {
22 class MessageLoop;
23 class WaitableEvent;
26 namespace ui {
27 class DisplayConfigurator;
30 namespace content {
32 class VideoFrameTexture : public base::RefCounted<VideoFrameTexture> {
33 public:
34 uint32 texture_id() const { return texture_id_; }
35 uint32 texture_target() const { return texture_target_; }
37 VideoFrameTexture(uint32 texture_target,
38 uint32 texture_id,
39 const base::Closure& no_longer_needed_cb);
41 private:
42 friend class base::RefCounted<VideoFrameTexture>;
44 uint32 texture_target_;
45 uint32 texture_id_;
46 base::Closure no_longer_needed_cb_;
48 ~VideoFrameTexture();
51 struct RenderingHelperParams {
52 RenderingHelperParams();
53 ~RenderingHelperParams();
55 // The rendering FPS.
56 int rendering_fps;
58 // The number of empty frames rendered when the rendering helper is
59 // initialized.
60 int warm_up_iterations;
62 // The desired size of each window. We play each stream in its own window
63 // on the screen.
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 {
82 public:
84 RenderingHelper();
85 ~RenderingHelper();
87 // Initialize GL. This method must be called on the rendering
88 // thread.
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.
93 void Setup();
95 // Tear down the platform window. This method must be called on the
96 // main thread.
97 void TearDown();
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
109 // signal |*done|.
110 void CreateTexture(uint32 texture_target,
111 uint32* texture_id,
112 const gfx::Size& size,
113 base::WaitableEvent* done);
115 // Render thumbnail in the |texture_id| to the FBO buffer using target
116 // |texture_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,
142 bool* alpha_solid,
143 base::WaitableEvent* done);
145 private:
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.
151 bool is_flushing;
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
157 // playback.
158 int frames_to_drop;
160 // The video frames pending for rendering.
161 std::queue<scoped_refptr<VideoFrameTexture> > pending_frames;
163 RenderedVideo();
164 ~RenderedVideo();
167 void Clear();
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
183 // |texture_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_;
197 #endif
198 #endif
200 bool ignore_vsync_;
202 gfx::AcceleratedWidget window_;
204 gfx::Size screen_size_;
206 std::vector<RenderedVideo> videos_;
208 bool render_as_thumbnails_;
209 int frame_count_;
210 GLuint thumbnails_fbo_id_;
211 GLuint thumbnails_texture_id_;
212 gfx::Size thumbnails_fbo_size_;
213 gfx::Size thumbnail_size_;
214 GLuint program_;
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_