- add sources.
[platform/framework/web/crosswalk.git] / src / content / browser / renderer_host / compositing_iosurface_transformer_mac.h
1 // Copyright (c) 2013 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_BROWSER_RENDERER_HOST_COMPOSITING_IOSURFACE_TRANSFORMER_MAC_H_
6 #define CONTENT_BROWSER_RENDERER_HOST_COMPOSITING_IOSURFACE_TRANSFORMER_MAC_H_
7
8 #include <OpenGL/gl.h>
9
10 #include "base/basictypes.h"
11 #include "content/browser/renderer_host/compositing_iosurface_shader_programs_mac.h"
12 #include "ui/gfx/size.h"
13
14 namespace gfx {
15 class Rect;
16 }  // namespace gfx
17
18 namespace content {
19
20 // Provides useful image filtering operations that are implemented efficiently
21 // using OpenGL shader programs.
22 //
23 // Note: All methods assume to be called within an active OpenGL context.
24 class CompositingIOSurfaceTransformer {
25  public:
26   // Construct a transformer that always uses the given parameters for texture
27   // bindings.  |texture_target| is one of the valid enums to use with
28   // glBindTexture().
29   // |src_texture_needs_y_flip| is true when the |src_texture| argument to any
30   // of the methods below uses upside-down Y coordinates.
31   // |shader_program_cache| is not owned by this instance.
32   CompositingIOSurfaceTransformer(
33       GLenum texture_target, bool src_texture_needs_y_flip,
34       CompositingIOSurfaceShaderPrograms* shader_program_cache);
35
36   ~CompositingIOSurfaceTransformer();
37
38   // Delete any references to currently-cached OpenGL objects.  This must be
39   // called within the OpenGL context just before destruction.
40   void ReleaseCachedGLObjects();
41
42   // Resize using bilinear interpolation.  Returns false on error.  Otherwise,
43   // the |texture| argument will point to the result.  Ownership of the returned
44   // |texture| remains with CompositingIOSurfaceTransformer (i.e., the caller
45   // must not delete this texture).  The |texture| remains valid until the next
46   // call to ResizeBilinear() or ReleaseCachedGLObjects().
47   //
48   // If the src and dst sizes are identical, this becomes a simple copy into a
49   // new texture.
50   //
51   // Note: This implementation is faulty in that minifications by more than 2X
52   // will undergo aliasing.
53   bool ResizeBilinear(GLuint src_texture, const gfx::Rect& src_subrect,
54                       const gfx::Size& dst_size, GLuint* texture);
55
56   // Color format conversion from RGB to planar YV12 (also known as YUV420).
57   //
58   // YV12 is effectively a twelve bit per pixel format consisting of a full-
59   // size y (luminance) plane and half-width, half-height u and v (blue and
60   // red chrominance) planes.  This method will return three off-screen
61   // textures, one for each plane, via the output arguments |texture_y|,
62   // |texture_u|, and |texture_v|.  While the textures are in GL_RGBA format,
63   // they should be interpreted as the appropriate single-byte, planar format
64   // after reading the pixel data.  The output arguments |packed_y_size| and
65   // |packed_uv_size| follow from these special semantics: They represent the
66   // size of their corresponding texture, if it was to be treated like RGBA
67   // pixel data.  That means their widths are in terms of "quads," where one
68   // quad contains 4 Y (or U or V) pixels.
69   //
70   // Ownership of the returned textures remains with
71   // CompositingIOSurfaceTransformer (i.e., the caller must not delete the
72   // textures).  The textures remain valid until the next call to
73   // TransformRGBToYV12() or ReleaseCachedGLObjects().
74   //
75   // If |src_subrect|'s size does not match |dst_size|, the source will be
76   // bilinearly interpolated during conversion.
77   //
78   // Returns true if successful, and the caller is responsible for deleting the
79   // output textures.
80   bool TransformRGBToYV12(
81       GLuint src_texture, const gfx::Rect& src_subrect,
82       const gfx::Size& dst_size,
83       GLuint* texture_y, GLuint* texture_u, GLuint* texture_v,
84       gfx::Size* packed_y_size, gfx::Size* packed_uv_size);
85
86  private:
87   enum CachedTexture {
88     RGBA_OUTPUT = 0,
89     Y_PLANE_OUTPUT,
90     UUVV_INTERMEDIATE,
91     U_PLANE_OUTPUT,
92     V_PLANE_OUTPUT,
93     NUM_CACHED_TEXTURES
94   };
95
96   // If necessary, generate the texture and/or resize it to the given |size|.
97   void PrepareTexture(CachedTexture which, const gfx::Size& size);
98
99   // If necessary, generate a framebuffer object to be used as an intermediate
100   // destination for drawing.
101   void PrepareFramebuffer();
102
103   // Target to bind all input and output textures to (which defines the type of
104   // textures being created and read).  Generally, this is
105   // GL_TEXTURE_RECTANGLE_ARB.
106   const GLenum texture_target_;
107   const bool src_texture_needs_y_flip_;
108   CompositingIOSurfaceShaderPrograms* const shader_program_cache_;
109
110   // Cached OpenGL objects.
111   GLuint textures_[NUM_CACHED_TEXTURES];
112   gfx::Size texture_sizes_[NUM_CACHED_TEXTURES];
113   GLuint frame_buffer_;
114
115   // Auto-detected and set once in the constructor.
116   bool system_supports_multiple_draw_buffers_;
117
118   DISALLOW_COPY_AND_ASSIGN(CompositingIOSurfaceTransformer);
119 };
120
121 }  // namespace content
122
123 #endif  // CONTENT_BROWSER_RENDERER_HOST_COMPOSITING_IOSURFACE_TRANSFORMER_MAC_H_