/src/mozilla-central/gfx/gl/GLBlitHelper.h
Line | Count | Source (jump to first uncovered line) |
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/. */ |
6 | | |
7 | | #ifndef GLBLITHELPER_H_ |
8 | | #define GLBLITHELPER_H_ |
9 | | |
10 | | #include "GLContextTypes.h" |
11 | | #include "GLConsts.h" |
12 | | #include "nsSize.h" |
13 | | #include "ipc/IPCMessageUtils.h" |
14 | | #include "mozilla/Attributes.h" |
15 | | #include "mozilla/gfx/Point.h" |
16 | | #include "../layers/ImageTypes.h" |
17 | | |
18 | | #ifdef XP_WIN |
19 | | #include <windows.h> |
20 | | #endif |
21 | | |
22 | | namespace mozilla { |
23 | | |
24 | | namespace layers { |
25 | | class D3D11YCbCrImage; |
26 | | class Image; |
27 | | class GPUVideoImage; |
28 | | class PlanarYCbCrImage; |
29 | | class SurfaceTextureImage; |
30 | | class MacIOSurfaceImage; |
31 | | class SurfaceDescriptorD3D10; |
32 | | class SurfaceDescriptorDXGIYCbCr; |
33 | | } // namespace layers |
34 | | |
35 | | namespace gl { |
36 | | |
37 | | class BindAnglePlanes; |
38 | | class GLContext; |
39 | | class GLBlitHelper; |
40 | | |
41 | | bool |
42 | | GuessDivisors(const gfx::IntSize& ySize, const gfx::IntSize& uvSize, |
43 | | gfx::IntSize* const out_divisors); |
44 | | |
45 | | template<uint8_t N> |
46 | | struct Mat |
47 | | { |
48 | | float m[N*N]; // column-major, for GL |
49 | | |
50 | 0 | float& at(const uint8_t x, const uint8_t y) { |
51 | 0 | return m[N*x+y]; |
52 | 0 | } |
53 | | |
54 | | static Mat<N> Zero(); |
55 | | static Mat<N> I(); |
56 | | |
57 | | Mat<N> operator*(const Mat<N>& r) const; |
58 | | }; |
59 | | typedef Mat<3> Mat3; |
60 | | |
61 | | Mat3 SubRectMat3(float x, float y, float w, float h); |
62 | | Mat3 SubRectMat3(const gfx::IntRect& subrect, const gfx::IntSize& size); |
63 | | Mat3 SubRectMat3(const gfx::IntRect& bigSubrect, const gfx::IntSize& smallSize, |
64 | | const gfx::IntSize& divisors); |
65 | | |
66 | | class DrawBlitProg final |
67 | | { |
68 | | const GLBlitHelper& mParent; |
69 | | const GLuint mProg; |
70 | | const GLint mLoc_uDestMatrix; |
71 | | const GLint mLoc_uTexMatrix0; |
72 | | const GLint mLoc_uTexMatrix1; |
73 | | const GLint mLoc_uColorMatrix; |
74 | | GLenum mType_uColorMatrix = 0; |
75 | | |
76 | | public: |
77 | | struct Key final { |
78 | | const char* const fragHeader; |
79 | | const char* const fragBody; |
80 | | |
81 | 0 | bool operator <(const Key& x) const { |
82 | 0 | if (fragHeader != x.fragHeader) |
83 | 0 | return fragHeader < x.fragHeader; |
84 | 0 | return fragBody < x.fragBody; |
85 | 0 | } |
86 | | }; |
87 | | |
88 | | DrawBlitProg(const GLBlitHelper* parent, GLuint prog); |
89 | | ~DrawBlitProg(); |
90 | | |
91 | | struct BaseArgs final { |
92 | | Mat3 texMatrix0; |
93 | | bool yFlip; |
94 | | gfx::IntSize destSize; // Always needed for (at least) setting the viewport. |
95 | | Maybe<gfx::IntRect> destRect; |
96 | | }; |
97 | | struct YUVArgs final { |
98 | | Mat3 texMatrix1; |
99 | | YUVColorSpace colorSpace; |
100 | | }; |
101 | | |
102 | | void Draw(const BaseArgs& args, const YUVArgs* argsYUV = nullptr) const; |
103 | | }; |
104 | | |
105 | | class ScopedSaveMultiTex final |
106 | | { |
107 | | GLContext& mGL; |
108 | | const uint8_t mTexCount; |
109 | | const GLenum mTexTarget; |
110 | | const GLuint mOldTexUnit; |
111 | | GLuint mOldTexSampler[3]; |
112 | | GLuint mOldTex[3]; |
113 | | |
114 | | public: |
115 | | ScopedSaveMultiTex(GLContext* gl, uint8_t texCount, GLenum texTarget); |
116 | | ~ScopedSaveMultiTex(); |
117 | | }; |
118 | | |
119 | | /** Buffer blitting helper */ |
120 | | class GLBlitHelper final |
121 | | { |
122 | | friend class BindAnglePlanes; |
123 | | friend class DrawBlitProg; |
124 | | friend class GLContext; |
125 | | |
126 | | GLContext* const mGL; |
127 | | mutable std::map<DrawBlitProg::Key, const DrawBlitProg*> mDrawBlitProgs; |
128 | | |
129 | | GLuint mQuadVAO = 0; |
130 | | GLuint mQuadVBO = 0; |
131 | | nsCString mDrawBlitProg_VersionLine; |
132 | | const GLuint mDrawBlitProg_VertShader; |
133 | | |
134 | | GLuint mYuvUploads[3] = {}; |
135 | | gfx::IntSize mYuvUploads_YSize = {0,0}; |
136 | | gfx::IntSize mYuvUploads_UVSize = {0,0}; |
137 | | |
138 | | #ifdef XP_WIN |
139 | | mutable RefPtr<ID3D11Device> mD3D11; |
140 | | |
141 | | ID3D11Device* GetD3D11() const; |
142 | | #endif |
143 | | |
144 | | const DrawBlitProg* GetDrawBlitProg(const DrawBlitProg::Key& key) const; |
145 | | private: |
146 | | const DrawBlitProg* CreateDrawBlitProg(const DrawBlitProg::Key& key) const; |
147 | | public: |
148 | | |
149 | | bool BlitImage(layers::PlanarYCbCrImage* yuvImage, const gfx::IntSize& destSize, |
150 | | OriginPos destOrigin); |
151 | | #ifdef MOZ_WIDGET_ANDROID |
152 | | // Blit onto the current FB. |
153 | | bool BlitImage(layers::SurfaceTextureImage* stImage, const gfx::IntSize& destSize, |
154 | | OriginPos destOrigin) const; |
155 | | #endif |
156 | | #ifdef XP_MACOSX |
157 | | bool BlitImage(layers::MacIOSurfaceImage* srcImage, const gfx::IntSize& destSize, |
158 | | OriginPos destOrigin) const; |
159 | | #endif |
160 | | |
161 | | explicit GLBlitHelper(GLContext* gl); |
162 | | public: |
163 | | ~GLBlitHelper(); |
164 | | |
165 | | void BlitFramebuffer(const gfx::IntSize& srcSize, |
166 | | const gfx::IntSize& destSize, |
167 | | GLuint filter = LOCAL_GL_NEAREST) const; |
168 | | void BlitFramebufferToFramebuffer(GLuint srcFB, GLuint destFB, |
169 | | const gfx::IntSize& srcSize, |
170 | | const gfx::IntSize& destSize, |
171 | | GLuint filter = LOCAL_GL_NEAREST) const; |
172 | | void BlitFramebufferToTexture(GLuint destTex, const gfx::IntSize& srcSize, |
173 | | const gfx::IntSize& destSize, |
174 | | GLenum destTarget = LOCAL_GL_TEXTURE_2D) const; |
175 | | void BlitTextureToFramebuffer(GLuint srcTex, const gfx::IntSize& srcSize, |
176 | | const gfx::IntSize& destSize, |
177 | | GLenum srcTarget = LOCAL_GL_TEXTURE_2D) const; |
178 | | void BlitTextureToTexture(GLuint srcTex, GLuint destTex, |
179 | | const gfx::IntSize& srcSize, |
180 | | const gfx::IntSize& destSize, |
181 | | GLenum srcTarget = LOCAL_GL_TEXTURE_2D, |
182 | | GLenum destTarget = LOCAL_GL_TEXTURE_2D) const; |
183 | | |
184 | | |
185 | | void DrawBlitTextureToFramebuffer(GLuint srcTex, const gfx::IntSize& srcSize, |
186 | | const gfx::IntSize& destSize, |
187 | | GLenum srcTarget = LOCAL_GL_TEXTURE_2D) const; |
188 | | |
189 | | bool BlitImageToFramebuffer(layers::Image* srcImage, const gfx::IntSize& destSize, |
190 | | OriginPos destOrigin); |
191 | | |
192 | | private: |
193 | | #ifdef XP_WIN |
194 | | // GLBlitHelperD3D.cpp: |
195 | | bool BlitImage(layers::GPUVideoImage* srcImage, const gfx::IntSize& destSize, |
196 | | OriginPos destOrigin) const; |
197 | | bool BlitImage(layers::D3D11YCbCrImage* srcImage, const gfx::IntSize& destSize, |
198 | | OriginPos destOrigin) const; |
199 | | |
200 | | bool BlitDescriptor(const layers::SurfaceDescriptorD3D10& desc, |
201 | | const gfx::IntSize& destSize, OriginPos destOrigin) const; |
202 | | |
203 | | bool BlitAngleYCbCr(const WindowsHandle (&handleList)[3], |
204 | | const gfx::IntRect& clipRect, |
205 | | const gfx::IntSize& ySize, const gfx::IntSize& uvSize, |
206 | | const YUVColorSpace colorSpace, const gfx::IntSize& destSize, |
207 | | OriginPos destOrigin) const; |
208 | | |
209 | | bool BlitAnglePlanes(uint8_t numPlanes, const RefPtr<ID3D11Texture2D>* texD3DList, |
210 | | const DrawBlitProg* prog, const DrawBlitProg::BaseArgs& baseArgs, |
211 | | const DrawBlitProg::YUVArgs* const yuvArgs) const; |
212 | | #endif |
213 | | }; |
214 | | |
215 | | extern const char* const kFragHeader_Tex2D; |
216 | | extern const char* const kFragHeader_Tex2DRect; |
217 | | extern const char* const kFragHeader_TexExt; |
218 | | extern const char* const kFragBody_RGBA; |
219 | | extern const char* const kFragBody_CrYCb; |
220 | | extern const char* const kFragBody_NV12; |
221 | | extern const char* const kFragBody_PlanarYUV; |
222 | | |
223 | | } // namespace gl |
224 | | } // namespace mozilla |
225 | | |
226 | | #endif // GLBLITHELPER_H_ |