a2dfb1bbf492690104ee2f183aa076ccfc60a3a3
[platform/framework/web/crosswalk.git] / src / content / common / gpu / media / rendering_helper.h
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.
4
5 #ifndef CONTENT_COMMON_GPU_MEDIA_RENDERING_HELPER_H_
6 #define CONTENT_COMMON_GPU_MEDIA_RENDERING_HELPER_H_
7
8 #include <map>
9 #include <vector>
10
11 #include "base/basictypes.h"
12 #include "base/time/time.h"
13 #include "base/timer/timer.h"
14 #include "ui/gfx/geometry/rect.h"
15 #include "ui/gfx/geometry/size.h"
16 #include "ui/gl/gl_bindings.h"
17 #include "ui/gl/gl_context.h"
18 #include "ui/gl/gl_surface.h"
19
20 namespace base {
21 class MessageLoop;
22 class WaitableEvent;
23 }
24
25 namespace content {
26
27 struct RenderingHelperParams;
28
29 // Creates and draws textures used by the video decoder.
30 // This class is not thread safe and thus all the methods of this class
31 // (except for ctor/dtor) ensure they're being run on a single thread.
32 class RenderingHelper {
33  public:
34   // Interface for the content provider of the RenderingHelper.
35   class Client {
36    public:
37     // Callback to tell client to render the content.
38     virtual void RenderContent(RenderingHelper* helper) = 0;
39
40     // Callback to get the desired window size of the client.
41     virtual const gfx::Size& GetWindowSize() = 0;
42
43    protected:
44     virtual ~Client() {}
45   };
46
47   RenderingHelper();
48   ~RenderingHelper();
49
50   static bool InitializeOneOff();
51
52   // Create the render context and windows by the specified dimensions.
53   void Initialize(const RenderingHelperParams& params,
54                   base::WaitableEvent* done);
55
56   // Undo the effects of Initialize() and signal |*done|.
57   void UnInitialize(base::WaitableEvent* done);
58
59   // Return a newly-created GLES2 texture id of the specified size, and
60   // signal |*done|.
61   void CreateTexture(uint32 texture_target,
62                      uint32* texture_id,
63                      const gfx::Size& size,
64                      base::WaitableEvent* done);
65
66   // Render thumbnail in the |texture_id| to the FBO buffer using target
67   // |texture_target|.
68   void RenderThumbnail(uint32 texture_target, uint32 texture_id);
69
70   // Render |texture_id| to the current view port of the screen using target
71   // |texture_target|.
72   void RenderTexture(uint32 texture_target, uint32 texture_id);
73
74   // Delete |texture_id|.
75   void DeleteTexture(uint32 texture_id);
76
77   // Get the platform specific handle to the OpenGL display.
78   void* GetGLDisplay();
79
80   // Get the platform specific handle to the OpenGL context.
81   void* GetGLContext();
82
83   // Get rendered thumbnails as RGB.
84   // Sets alpha_solid to true if the alpha channel is entirely 0xff.
85   void GetThumbnailsAsRGB(std::vector<unsigned char>* rgb,
86                           bool* alpha_solid,
87                           base::WaitableEvent* done);
88
89  private:
90   void Clear();
91
92   void RenderContent();
93
94   void LayoutRenderingAreas();
95
96   // Timer to trigger the RenderContent() repeatly.
97   scoped_ptr<base::RepeatingTimer<RenderingHelper> > render_timer_;
98   base::MessageLoop* message_loop_;
99
100   scoped_refptr<gfx::GLContext> gl_context_;
101   scoped_refptr<gfx::GLSurface> gl_surface_;
102
103   gfx::AcceleratedWidget window_;
104
105   gfx::Size screen_size_;
106
107   // The rendering area of each window on the screen.
108   std::vector<gfx::Rect> render_areas_;
109
110   std::vector<base::WeakPtr<Client> > clients_;
111
112   bool render_as_thumbnails_;
113   int frame_count_;
114   GLuint thumbnails_fbo_id_;
115   GLuint thumbnails_texture_id_;
116   gfx::Size thumbnails_fbo_size_;
117   gfx::Size thumbnail_size_;
118   GLuint program_;
119   base::TimeDelta frame_duration_;
120
121   DISALLOW_COPY_AND_ASSIGN(RenderingHelper);
122 };
123
124 struct RenderingHelperParams {
125   RenderingHelperParams();
126   ~RenderingHelperParams();
127
128   // The rendering FPS.
129   int rendering_fps;
130
131   // The clients who provide the content for rendering.
132   std::vector<base::WeakPtr<RenderingHelper::Client> > clients;
133
134   // Whether the frames are rendered as scaled thumbnails within a
135   // larger FBO that is in turn rendered to the window.
136   bool render_as_thumbnails;
137   // The size of the FBO containing all visible thumbnails.
138   gfx::Size thumbnails_page_size;
139   // The size of each thumbnail within the FBO.
140   gfx::Size thumbnail_size;
141 };
142 }  // namespace content
143
144 #endif  // CONTENT_COMMON_GPU_MEDIA_RENDERING_HELPER_H_