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 18 namespace base { 19 class MessageLoop; 20 class WaitableEvent; 21 } 22 23 #if !defined(OS_WIN) && defined(ARCH_CPU_X86_FAMILY) 24 #define GL_VARIANT_GLX 1 25 typedef GLXContext NativeContextType; 26 #else 27 #define GL_VARIANT_EGL 1 28 typedef EGLContext NativeContextType; 29 #endif 30 31 namespace content { 32 33 struct RenderingHelperParams; 34 35 // Creates and draws textures used by the video decoder. 36 // This class is not thread safe and thus all the methods of this class 37 // (except for ctor/dtor) ensure they're being run on a single thread. 38 class RenderingHelper { 39 public: 40 // Interface for the content provider of the RenderingHelper. 41 class Client { 42 public: 43 // Callback to tell client to render the content. 44 virtual void RenderContent(RenderingHelper* helper) = 0; 45 46 // Callback to get the desired window size of the client. 47 virtual const gfx::Size& GetWindowSize() = 0; 48 49 protected: 50 virtual ~Client() {} 51 }; 52 53 RenderingHelper(); 54 ~RenderingHelper(); 55 56 // Create the render context and windows by the specified dimensions. 57 void Initialize(const RenderingHelperParams& params, 58 base::WaitableEvent* done); 59 60 // Undo the effects of Initialize() and signal |*done|. 61 void UnInitialize(base::WaitableEvent* done); 62 63 // Return a newly-created GLES2 texture id of the specified size, and 64 // signal |*done|. 65 void CreateTexture(uint32 texture_target, 66 uint32* texture_id, 67 const gfx::Size& size, 68 base::WaitableEvent* done); 69 70 // Render thumbnail in the |texture_id| to the FBO buffer using target 71 // |texture_target|. 72 void RenderThumbnail(uint32 texture_target, uint32 texture_id); 73 74 // Render |texture_id| to the current view port of the screen using target 75 // |texture_target|. 76 void RenderTexture(uint32 texture_target, uint32 texture_id); 77 78 // Delete |texture_id|. 79 void DeleteTexture(uint32 texture_id); 80 81 // Get the platform specific handle to the OpenGL display. 82 void* GetGLDisplay(); 83 84 // Get the platform specific handle to the OpenGL context. 85 NativeContextType GetGLContext(); 86 87 // Get rendered thumbnails as RGB. 88 // Sets alpha_solid to true if the alpha channel is entirely 0xff. 89 void GetThumbnailsAsRGB(std::vector<unsigned char>* rgb, 90 bool* alpha_solid, 91 base::WaitableEvent* done); 92 93 private: 94 void Clear(); 95 96 void RenderContent(); 97 98 void LayoutRenderingAreas(); 99 100 // Timer to trigger the RenderContent() repeatly. 101 base::RepeatingTimer<RenderingHelper> render_timer_; 102 base::MessageLoop* message_loop_; 103 104 NativeContextType gl_context_; 105 106 #if defined(GL_VARIANT_EGL) 107 EGLDisplay gl_display_; 108 EGLSurface gl_surface_; 109 #else 110 XVisualInfo* x_visual_; 111 #endif 112 113 #if defined(OS_WIN) 114 HWND window_; 115 #else 116 Display* x_display_; 117 Window x_window_; 118 #endif 119 120 gfx::Size screen_size_; 121 122 // The rendering area of each window on the screen. 123 std::vector<gfx::Rect> render_areas_; 124 125 std::vector<base::WeakPtr<Client> > clients_; 126 127 bool render_as_thumbnails_; 128 int frame_count_; 129 GLuint thumbnails_fbo_id_; 130 GLuint thumbnails_texture_id_; 131 gfx::Size thumbnails_fbo_size_; 132 gfx::Size thumbnail_size_; 133 GLuint program_; 134 base::TimeDelta frame_duration_; 135 136 DISALLOW_COPY_AND_ASSIGN(RenderingHelper); 137 }; 138 139 struct RenderingHelperParams { 140 RenderingHelperParams(); 141 ~RenderingHelperParams(); 142 143 // The rendering FPS. 144 int rendering_fps; 145 146 // The clients who provide the content for rendering. 147 std::vector<base::WeakPtr<RenderingHelper::Client> > clients; 148 149 // Whether the frames are rendered as scaled thumbnails within a 150 // larger FBO that is in turn rendered to the window. 151 bool render_as_thumbnails; 152 // The size of the FBO containing all visible thumbnails. 153 gfx::Size thumbnails_page_size; 154 // The size of each thumbnail within the FBO. 155 gfx::Size thumbnail_size; 156 }; 157 } // namespace content 158 159 #endif // CONTENT_COMMON_GPU_MEDIA_RENDERING_HELPER_H_ 160