GLReadTexImageHelper.h (3101B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 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/. */ 6 7 #ifndef GLREADTEXIMAGEHELPER_H_ 8 #define GLREADTEXIMAGEHELPER_H_ 9 10 #include "GLContextTypes.h" 11 #include "nsSize.h" 12 #include "mozilla/AlreadyAddRefed.h" 13 #include "mozilla/gfx/MatrixFwd.h" 14 #include "mozilla/gfx/Types.h" 15 16 namespace mozilla { 17 18 namespace gfx { 19 class DataSourceSurface; 20 } // namespace gfx 21 22 namespace gl { 23 24 // Returns true if the `dest{Format,Type}` are the same as the 25 // `read{Format,Type}`. 26 bool GetActualReadFormats(GLContext* gl, GLenum destFormat, GLenum destType, 27 GLenum* out_readFormat, GLenum* out_readType); 28 29 void ReadPixelsIntoBuffer(GLContext* gl, uint8_t* aData, int32_t aStride, 30 const gfx::IntSize& aSize, 31 gfx::SurfaceFormat aFormat); 32 33 void ReadPixelsIntoDataSurface(GLContext* aGL, 34 gfx::DataSourceSurface* aSurface); 35 36 already_AddRefed<gfx::DataSourceSurface> ReadBackSurface( 37 GLContext* gl, GLuint aTexture, bool aYInvert, gfx::SurfaceFormat aFormat); 38 39 already_AddRefed<gfx::DataSourceSurface> YInvertImageSurface( 40 gfx::DataSourceSurface* aSurf, uint32_t aStride); 41 42 void SwapRAndBComponents(gfx::DataSourceSurface* surf); 43 44 class GLReadTexImageHelper final { 45 // The GLContext is the sole owner of the GLBlitHelper. 46 GLContext* mGL; 47 48 GLuint mPrograms[4]; 49 50 GLuint TextureImageProgramFor(GLenum aTextureTarget, int aShader); 51 52 bool DidGLErrorOccur(const char* str); 53 54 public: 55 explicit GLReadTexImageHelper(GLContext* gl); 56 ~GLReadTexImageHelper(); 57 58 /** 59 * Read the image data contained in aTexture, and return it as an 60 * ImageSurface. If GL_RGBA is given as the format, a 61 * SurfaceFormat::A8R8G8B8_UINT32 surface is returned. Not implemented yet: If 62 * GL_RGB is given as the format, a SurfaceFormat::X8R8G8B8_UINT32 surface is 63 * returned. If GL_LUMINANCE is given as the format, a SurfaceFormat::A8 64 * surface is returned. 65 * 66 * THIS IS EXPENSIVE. It is ridiculously expensive. Only do this 67 * if you absolutely positively must, and never in any performance 68 * critical path. 69 * 70 * NOTE: aShaderProgram is really mozilla::layers::ShaderProgramType. It is 71 * passed as int to eliminate including LayerManagerOGLProgram.h here. 72 */ 73 already_AddRefed<gfx::DataSourceSurface> ReadTexImage( 74 GLuint aTextureId, GLenum aTextureTarget, const gfx::IntSize& aSize, 75 const gfx::Matrix4x4& aTexMatrix, 76 /* ShaderProgramType */ int aShaderProgram, bool aYInvert = false); 77 78 bool ReadTexImage(gfx::DataSourceSurface* aDest, GLuint aTextureId, 79 GLenum aTextureTarget, const gfx::IntSize& aSize, 80 const gfx::Matrix4x4& aTexMatrix, int aShaderProgram, 81 bool aYInvert = false); 82 }; 83 84 } // namespace gl 85 } // namespace mozilla 86 87 #endif