Bumping manifests a=b2g-bump
[gecko.git] / gfx / gl / GLBlitHelper.h
blob2814e76267f4b9b6adf3d2c86c90ab6e68836f83
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* vim: set ts=8 sts=4 et sw=4 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef GLBLITHELPER_H_
8 #define GLBLITHELPER_H_
10 #include "GLContextTypes.h"
11 #include "GLConsts.h"
12 #include "nsSize.h"
13 #include "mozilla/Attributes.h"
14 #include "mozilla/gfx/Point.h"
16 namespace mozilla {
18 namespace layers {
19 class Image;
20 class PlanarYCbCrImage;
21 class GrallocImage;
22 class SurfaceTextureImage;
23 class EGLImageImage;
26 namespace gl {
28 class GLContext;
30 /**
31 * Helper function that creates a 2D texture aSize.width x aSize.height with
32 * storage type specified by aFormats. Returns GL texture object id.
34 * See mozilla::gl::CreateTexture.
36 GLuint CreateTextureForOffscreen(GLContext* aGL, const GLFormats& aFormats,
37 const gfx::IntSize& aSize);
39 /**
40 * Helper function that creates a 2D texture aSize.width x aSize.height with
41 * storage type aInternalFormat. Returns GL texture object id.
43 * Initialize textyre parameters to:
44 * GL_TEXTURE_MIN_FILTER = GL_LINEAR
45 * GL_TEXTURE_MAG_FILTER = GL_LINEAR
46 * GL_TEXTURE_WRAP_S = GL_CLAMP_TO_EDGE
47 * GL_TEXTURE_WRAP_T = GL_CLAMP_TO_EDGE
49 GLuint CreateTexture(GLContext* aGL, GLenum aInternalFormat, GLenum aFormat,
50 GLenum aType, const gfx::IntSize& aSize, bool linear = true);
52 /**
53 * Helper function to create, potentially, multisample render buffers suitable
54 * for offscreen rendering. Buffers of size aSize.width x aSize.height with
55 * storage specified by aFormat. returns GL render buffer object id.
57 GLuint CreateRenderbuffer(GLContext* aGL, GLenum aFormat, GLsizei aSamples,
58 const gfx::IntSize& aSize);
60 /**
61 * Helper function to create, potentially, multisample render buffers suitable
62 * for offscreen rendering. Buffers of size aSize.width x aSize.height with
63 * storage specified by aFormats. GL render buffer object ids are returned via
64 * aColorMSRB, aDepthRB, and aStencilRB
66 void CreateRenderbuffersForOffscreen(GLContext* aGL, const GLFormats& aFormats,
67 const gfx::IntSize& aSize, bool aMultisample,
68 GLuint* aColorMSRB, GLuint* aDepthRB,
69 GLuint* aStencilRB);
72 /** Buffer blitting helper */
73 class GLBlitHelper MOZ_FINAL
75 enum Channel
77 Channel_Y = 0,
78 Channel_Cb,
79 Channel_Cr,
80 Channel_Max,
83 /**
84 * BlitTex2D is used to copy blit the content of a GL_TEXTURE_2D object,
85 * BlitTexRect is used to copy blit the content of a GL_TEXTURE_RECT object,
86 * The difference between BlitTex2D and BlitTexRect is the texture type, which affect
87 * the fragment shader a bit.
89 * ConvertGralloc is used to color convert copy blit the GrallocImage into a
90 * normal RGB texture by egl_image_external extension
91 * ConvertPlnarYcbCr is used to color convert copy blit the PlanarYCbCrImage
92 * into a normal RGB texture by create textures of each color channel, and
93 * convert it in GPU.
94 * Convert type is created for canvas.
96 enum BlitType
98 BlitTex2D,
99 BlitTexRect,
100 ConvertGralloc,
101 ConvertPlanarYCbCr,
102 ConvertSurfaceTexture,
103 ConvertEGLImage
105 // The GLContext is the sole owner of the GLBlitHelper.
106 GLContext* mGL;
108 GLuint mTexBlit_Buffer;
109 GLuint mTexBlit_VertShader;
110 GLuint mTex2DBlit_FragShader;
111 GLuint mTex2DRectBlit_FragShader;
112 GLuint mTex2DBlit_Program;
113 GLuint mTex2DRectBlit_Program;
115 GLint mYFlipLoc;
117 GLint mTextureTransformLoc;
119 // Data for image blit path
120 GLuint mTexExternalBlit_FragShader;
121 GLuint mTexYUVPlanarBlit_FragShader;
122 GLuint mTexExternalBlit_Program;
123 GLuint mTexYUVPlanarBlit_Program;
124 GLuint mFBO;
125 GLuint mSrcTexY;
126 GLuint mSrcTexCb;
127 GLuint mSrcTexCr;
128 GLuint mSrcTexEGL;
129 GLint mYTexScaleLoc;
130 GLint mCbCrTexScaleLoc;
131 int mTexWidth;
132 int mTexHeight;
134 // Cache some uniform values
135 float mCurYScale;
136 float mCurCbCrScale;
138 void UseBlitProgram();
139 void SetBlitFramebufferForDestTexture(GLuint aTexture);
141 bool UseTexQuadProgram(BlitType target, const gfx::IntSize& srcSize);
142 bool InitTexQuadProgram(BlitType target = BlitTex2D);
143 void DeleteTexBlitProgram();
144 void BindAndUploadYUVTexture(Channel which, uint32_t width, uint32_t height, void* data, bool allocation);
145 void BindAndUploadEGLImage(EGLImage image, GLuint target);
147 #ifdef MOZ_WIDGET_GONK
148 bool BlitGrallocImage(layers::GrallocImage* grallocImage, bool yflip);
149 #endif
150 bool BlitPlanarYCbCrImage(layers::PlanarYCbCrImage* yuvImage, bool yflip);
151 #ifdef MOZ_WIDGET_ANDROID
152 // Blit onto the current FB.
153 bool BlitSurfaceTextureImage(layers::SurfaceTextureImage* stImage, bool yflip);
154 bool BlitEGLImageImage(layers::EGLImageImage* eglImage, bool yflip);
155 #endif
157 public:
159 explicit GLBlitHelper(GLContext* gl);
160 ~GLBlitHelper();
162 // If you don't have |srcFormats| for the 2nd definition,
163 // then you'll need the framebuffer_blit extensions to use
164 // the first BlitFramebufferToFramebuffer.
165 void BlitFramebufferToFramebuffer(GLuint srcFB, GLuint destFB,
166 const gfx::IntSize& srcSize,
167 const gfx::IntSize& destSize,
168 bool internalFBs = false);
169 void BlitFramebufferToFramebuffer(GLuint srcFB, GLuint destFB,
170 const gfx::IntSize& srcSize,
171 const gfx::IntSize& destSize,
172 const GLFormats& srcFormats,
173 bool internalFBs = false);
174 void BlitTextureToFramebuffer(GLuint srcTex, GLuint destFB,
175 const gfx::IntSize& srcSize,
176 const gfx::IntSize& destSize,
177 GLenum srcTarget = LOCAL_GL_TEXTURE_2D,
178 bool internalFBs = false);
179 void BlitFramebufferToTexture(GLuint srcFB, GLuint destTex,
180 const gfx::IntSize& srcSize,
181 const gfx::IntSize& destSize,
182 GLenum destTarget = LOCAL_GL_TEXTURE_2D,
183 bool internalFBs = false);
184 void BlitTextureToTexture(GLuint srcTex, GLuint destTex,
185 const gfx::IntSize& srcSize,
186 const gfx::IntSize& destSize,
187 GLenum srcTarget = LOCAL_GL_TEXTURE_2D,
188 GLenum destTarget = LOCAL_GL_TEXTURE_2D);
189 bool BlitImageToFramebuffer(layers::Image* srcImage, const gfx::IntSize& destSize,
190 GLuint destFB, bool yflip = false, GLuint xoffset = 0,
191 GLuint yoffset = 0, GLuint width = 0, GLuint height = 0);
192 bool BlitImageToTexture(layers::Image* srcImage, const gfx::IntSize& destSize,
193 GLuint destTex, GLenum destTarget, bool yflip = false,
194 GLuint xoffset = 0, GLuint yoffset = 0, GLuint width = 0,
195 GLuint height = 0);
198 } // namespace gl
199 } // namespace mozilla
201 #endif // GLBLITHELPER_H_