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 "ui/gfx/size.h"
13#include "ui/gl/gl_bindings.h"
14
15namespace base {
16class MessageLoop;
17class WaitableEvent;
18}
19
20#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
28namespace content {
29
30struct 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
50// 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:
55  RenderingHelper();
56  ~RenderingHelper();
57
58  // Create the render context and windows by the specified dimensions.
59  void Initialize(const RenderingHelperParams& params,
60                  base::WaitableEvent* done);
61
62  // Undo the effects of Initialize() and signal |*done|.
63  void UnInitialize(base::WaitableEvent* done);
64
65  // Return a newly-created GLES2 texture id rendering to a specific window, and
66  // signal |*done|.
67  void CreateTexture(int window_id,
68                     uint32 texture_target,
69                     uint32* texture_id,
70                     base::WaitableEvent* done);
71
72  // Render |texture_id| to the screen using target |texture_target|.
73  void RenderTexture(uint32 texture_target, uint32 texture_id);
74
75  // Delete |texture_id|.
76  void DeleteTexture(uint32 texture_id);
77
78  // Get the platform specific handle to the OpenGL context.
79  void* GetGLContext();
80
81  // Get the platform specific handle to the OpenGL display.
82  void* GetGLDisplay();
83
84  // Get rendered thumbnails as RGB.
85  // Sets alpha_solid to true if the alpha channel is entirely 0xff.
86  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);
127};
128
129}  // namespace content
130
131#endif  // CONTENT_COMMON_GPU_MEDIA_RENDERING_HELPER_H_
132