blob: aee495adf0bb5e6fb5c00c7c83e228ab0b0a22b7 [file] [log] [blame]
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001// 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
Torne (Richard Coles)4e180b62013-10-18 15:46:22 +01008#include <map>
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +00009#include <vector>
10
Torne (Richard Coles)58218062012-11-14 11:43:16 +000011#include "base/basictypes.h"
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000012#include "ui/gfx/size.h"
Torne (Richard Coles)4e180b62013-10-18 15:46:22 +010013#include "ui/gl/gl_bindings.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000014
15namespace base {
Torne (Richard Coles)4e180b62013-10-18 15:46:22 +010016class MessageLoop;
Torne (Richard Coles)58218062012-11-14 11:43:16 +000017class WaitableEvent;
18}
19
Torne (Richard Coles)4e180b62013-10-18 15:46:22 +010020#if !defined(OS_WIN) && defined(ARCH_CPU_X86_FAMILY)
21#define GL_VARIANT_GLX 1
22typedef GLXContext NativeContextType;
23#else
24#define GL_VARIANT_EGL 1
25typedef EGLContext NativeContextType;
26#endif
27
Torne (Richard Coles)58218062012-11-14 11:43:16 +000028namespace content {
29
Ben Murdocha3f7b4e2013-07-24 10:36:34 +010030struct RenderingHelperParams {
31 RenderingHelperParams();
32 ~RenderingHelperParams();
33
34 bool suppress_swap_to_display;
35 int num_windows;
36 // Dimensions of window(s) created for displaying frames. In the
37 // case of thumbnail rendering, these won't match the frame dimensions.
38 std::vector<gfx::Size> window_dimensions;
39 // Dimensions of video frame texture(s).
40 std::vector<gfx::Size> frame_dimensions;
41 // Whether the frames are rendered as scaled thumbnails within a
42 // larger FBO that is in turn rendered to the window.
43 bool render_as_thumbnails;
44 // The size of the FBO containing all visible thumbnails.
45 gfx::Size thumbnails_page_size;
46 // The size of each thumbnail within the FBO.
47 gfx::Size thumbnail_size;
48};
49
Torne (Richard Coles)58218062012-11-14 11:43:16 +000050// Creates and draws textures used by the video decoder.
51// This class is not thread safe and thus all the methods of this class
52// (except for ctor/dtor) ensure they're being run on a single thread.
53class RenderingHelper {
54 public:
Torne (Richard Coles)4e180b62013-10-18 15:46:22 +010055 RenderingHelper();
56 ~RenderingHelper();
Torne (Richard Coles)58218062012-11-14 11:43:16 +000057
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000058 // Create the render context and windows by the specified dimensions.
Torne (Richard Coles)4e180b62013-10-18 15:46:22 +010059 void Initialize(const RenderingHelperParams& params,
60 base::WaitableEvent* done);
Torne (Richard Coles)58218062012-11-14 11:43:16 +000061
62 // Undo the effects of Initialize() and signal |*done|.
Torne (Richard Coles)4e180b62013-10-18 15:46:22 +010063 void UnInitialize(base::WaitableEvent* done);
Torne (Richard Coles)58218062012-11-14 11:43:16 +000064
65 // Return a newly-created GLES2 texture id rendering to a specific window, and
66 // signal |*done|.
Torne (Richard Coles)4e180b62013-10-18 15:46:22 +010067 void CreateTexture(int window_id,
68 uint32 texture_target,
69 uint32* texture_id,
70 base::WaitableEvent* done);
Torne (Richard Coles)58218062012-11-14 11:43:16 +000071
Ben Murdocheb525c52013-07-10 11:40:50 +010072 // Render |texture_id| to the screen.
Torne (Richard Coles)4e180b62013-10-18 15:46:22 +010073 void RenderTexture(uint32 texture_id);
Torne (Richard Coles)58218062012-11-14 11:43:16 +000074
75 // Delete |texture_id|.
Torne (Richard Coles)4e180b62013-10-18 15:46:22 +010076 void DeleteTexture(uint32 texture_id);
Torne (Richard Coles)58218062012-11-14 11:43:16 +000077
78 // Get the platform specific handle to the OpenGL context.
Torne (Richard Coles)4e180b62013-10-18 15:46:22 +010079 void* GetGLContext();
Torne (Richard Coles)58218062012-11-14 11:43:16 +000080
81 // Get the platform specific handle to the OpenGL display.
Torne (Richard Coles)4e180b62013-10-18 15:46:22 +010082 void* GetGLDisplay();
Ben Murdocha3f7b4e2013-07-24 10:36:34 +010083
84 // Get rendered thumbnails as RGB.
85 // Sets alpha_solid to true if the alpha channel is entirely 0xff.
Torne (Richard Coles)4e180b62013-10-18 15:46:22 +010086 void GetThumbnailsAsRGB(std::vector<unsigned char>* rgb,
87 bool* alpha_solid,
88 base::WaitableEvent* done);
89
90 private:
91 void Clear();
92
93 // Make window_id's surface current w/ the GL context, or release the context
94 // if |window_id < 0|.
95 void MakeCurrent(int window_id);
96
97 base::MessageLoop* message_loop_;
98 std::vector<gfx::Size> window_dimensions_;
99 std::vector<gfx::Size> frame_dimensions_;
100
101 NativeContextType gl_context_;
102 std::map<uint32, int> texture_id_to_surface_index_;
103
104#if defined(GL_VARIANT_EGL)
105 EGLDisplay gl_display_;
106 std::vector<EGLSurface> gl_surfaces_;
107#else
108 XVisualInfo* x_visual_;
109#endif
110
111#if defined(OS_WIN)
112 std::vector<HWND> windows_;
113#else
114 Display* x_display_;
115 std::vector<Window> x_windows_;
116#endif
117
118 bool render_as_thumbnails_;
119 int frame_count_;
120 GLuint thumbnails_fbo_id_;
121 GLuint thumbnails_texture_id_;
122 gfx::Size thumbnails_fbo_size_;
123 gfx::Size thumbnail_size_;
124 GLuint program_;
125
126 DISALLOW_COPY_AND_ASSIGN(RenderingHelper);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000127};
128
129} // namespace content
130
131#endif // CONTENT_COMMON_GPU_MEDIA_RENDERING_HELPER_H_