Bug 1785744 [wpt PR 35504] - Recalc style for elements where :toggle() pseudo-class...
[gecko.git] / gfx / gl / GLBlitHelper.h
bloba34bdba9ffdf4b7809000d3b0b9c3dac6d619792
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/. */
7 #ifndef GLBLITHELPER_H_
8 #define GLBLITHELPER_H_
10 #include <cstdint>
11 #include <map>
12 #include "GLConsts.h"
13 #include "GLContextTypes.h"
14 #include "GLTypes.h"
15 #include "nsSize.h"
16 #include "nsString.h"
17 #include "nsTString.h"
18 #include "mozilla/ipc/IPCTypes.h"
19 #include "mozilla/Attributes.h"
20 #include "mozilla/Maybe.h"
21 #include "mozilla/gfx/Point.h"
22 #include "mozilla/gfx/Rect.h"
23 #include "mozilla/gfx/Types.h"
25 #include <map>
27 #ifdef XP_WIN
28 # include <windows.h>
29 # include "mozilla/RefPtr.h"
30 # include "mozilla/ipc/IPCTypes.h"
31 struct ID3D11Device;
32 struct ID3D11Texture2D;
33 #endif
35 #ifdef XP_MACOSX
36 class MacIOSurface;
37 #endif
39 #ifdef MOZ_WIDGET_ANDROID
40 # include "mozilla/java/GeckoSurfaceTextureWrappers.h"
41 #endif
43 #ifdef MOZ_WAYLAND
44 class DMABufSurface;
45 #endif
47 namespace mozilla {
49 namespace layers {
50 class Image;
51 class GPUVideoImage;
52 struct PlanarYCbCrData;
53 class PlanarYCbCrImage;
54 class SurfaceDescriptor;
55 class SurfaceDescriptorBuffer;
57 #ifdef XP_WIN
58 class D3D11ShareHandleImage;
59 class D3D11TextureIMFSampleImage;
60 class D3D11YCbCrImage;
61 class SurfaceDescriptorD3D10;
62 class SurfaceDescriptorDXGIYCbCr;
63 #endif
65 #ifdef MOZ_WIDGET_ANDROID
66 class SurfaceTextureDescriptor;
67 #endif
69 #ifdef XP_MACOSX
70 class MacIOSurfaceImage;
71 #endif
73 #ifdef MOZ_WAYLAND
74 class DMABUFSurfaceImage;
75 #endif
76 } // namespace layers
78 namespace gl {
80 class BindAnglePlanes;
81 class GLContext;
82 class GLBlitHelper;
84 bool GuessDivisors(const gfx::IntSize& ySize, const gfx::IntSize& uvSize,
85 gfx::IntSize* const out_divisors);
87 template <uint8_t N>
88 struct Mat {
89 float m[N * N]; // column-major, for GL
91 float& at(const uint8_t x, const uint8_t y) { return m[N * x + y]; }
93 static Mat<N> Zero();
94 static Mat<N> I();
96 Mat<N> operator*(const Mat<N>& r) const;
98 typedef Mat<3> Mat3;
100 Mat3 SubRectMat3(float x, float y, float w, float h);
101 Mat3 SubRectMat3(const gfx::IntRect& subrect, const gfx::IntSize& size);
102 Mat3 SubRectMat3(const gfx::IntRect& bigSubrect, const gfx::IntSize& smallSize,
103 const gfx::IntSize& divisors);
105 class DrawBlitProg final {
106 const GLBlitHelper& mParent;
107 const GLuint mProg;
108 const GLint mLoc_uDestMatrix;
109 const GLint mLoc_uTexMatrix0;
110 const GLint mLoc_uTexMatrix1;
111 const GLint mLoc_uColorMatrix;
112 GLenum mType_uColorMatrix = 0;
114 public:
115 struct Key final {
116 const char* const fragHeader;
117 const char* const fragBody;
119 bool operator<(const Key& x) const {
120 if (fragHeader != x.fragHeader) return fragHeader < x.fragHeader;
121 return fragBody < x.fragBody;
125 DrawBlitProg(const GLBlitHelper* parent, GLuint prog);
126 ~DrawBlitProg();
128 struct BaseArgs final {
129 Mat3 texMatrix0;
130 bool yFlip;
131 gfx::IntSize
132 destSize; // Always needed for (at least) setting the viewport.
133 Maybe<gfx::IntRect> destRect;
135 struct YUVArgs final {
136 Mat3 texMatrix1;
137 gfx::YUVColorSpace colorSpace;
140 void Draw(const BaseArgs& args, const YUVArgs* argsYUV = nullptr) const;
143 class ScopedSaveMultiTex final {
144 GLContext& mGL;
145 const uint8_t mTexCount;
146 const GLenum mTexTarget;
147 const GLuint mOldTexUnit;
148 GLuint mOldTexSampler[3];
149 GLuint mOldTex[3];
151 public:
152 ScopedSaveMultiTex(GLContext* gl, uint8_t texCount, GLenum texTarget);
153 ~ScopedSaveMultiTex();
156 /** Buffer blitting helper */
157 class GLBlitHelper final {
158 friend class BindAnglePlanes;
159 friend class DrawBlitProg;
160 friend class GLContext;
162 GLContext* const mGL;
163 mutable std::map<DrawBlitProg::Key, const DrawBlitProg*> mDrawBlitProgs;
165 GLuint mQuadVAO = 0;
166 GLuint mQuadVBO = 0;
167 nsCString mDrawBlitProg_VersionLine;
168 const GLuint mDrawBlitProg_VertShader;
170 GLuint mYuvUploads[3] = {};
171 gfx::IntSize mYuvUploads_YSize = {0, 0};
172 gfx::IntSize mYuvUploads_UVSize = {0, 0};
174 #ifdef XP_WIN
175 mutable RefPtr<ID3D11Device> mD3D11;
177 ID3D11Device* GetD3D11() const;
178 #endif
180 const DrawBlitProg* GetDrawBlitProg(const DrawBlitProg::Key& key) const;
182 private:
183 const DrawBlitProg* CreateDrawBlitProg(const DrawBlitProg::Key& key) const;
185 public:
186 bool BlitPlanarYCbCr(const layers::PlanarYCbCrData&,
187 const gfx::IntSize& destSize, OriginPos destOrigin);
188 #ifdef MOZ_WIDGET_ANDROID
189 bool Blit(const java::GeckoSurfaceTexture::Ref& surfaceTexture,
190 const gfx::IntSize& destSize, const OriginPos destOrigin) const;
191 #endif
192 #ifdef XP_MACOSX
193 bool BlitImage(layers::MacIOSurfaceImage* srcImage,
194 const gfx::IntSize& destSize, OriginPos destOrigin) const;
195 #endif
196 #ifdef MOZ_WAYLAND
197 bool Blit(DMABufSurface* surface, const gfx::IntSize& destSize,
198 OriginPos destOrigin) const;
199 bool BlitImage(layers::DMABUFSurfaceImage* srcImage,
200 const gfx::IntSize& destSize, OriginPos destOrigin) const;
201 #endif
203 explicit GLBlitHelper(GLContext* gl);
205 public:
206 ~GLBlitHelper();
208 void BlitFramebuffer(const gfx::IntRect& srcRect,
209 const gfx::IntRect& destRect,
210 GLuint filter = LOCAL_GL_NEAREST) const;
211 void BlitFramebufferToFramebuffer(GLuint srcFB, GLuint destFB,
212 const gfx::IntRect& srcRect,
213 const gfx::IntRect& destRect,
214 GLuint filter = LOCAL_GL_NEAREST) const;
215 void BlitFramebufferToTexture(GLuint destTex, const gfx::IntSize& srcSize,
216 const gfx::IntSize& destSize,
217 GLenum destTarget = LOCAL_GL_TEXTURE_2D) const;
218 void BlitTextureToFramebuffer(GLuint srcTex, const gfx::IntSize& srcSize,
219 const gfx::IntSize& destSize,
220 GLenum srcTarget = LOCAL_GL_TEXTURE_2D) const;
221 void BlitTextureToTexture(GLuint srcTex, GLuint destTex,
222 const gfx::IntSize& srcSize,
223 const gfx::IntSize& destSize,
224 GLenum srcTarget = LOCAL_GL_TEXTURE_2D,
225 GLenum destTarget = LOCAL_GL_TEXTURE_2D) const;
227 void DrawBlitTextureToFramebuffer(GLuint srcTex, const gfx::IntSize& srcSize,
228 const gfx::IntSize& destSize,
229 GLenum srcTarget = LOCAL_GL_TEXTURE_2D,
230 bool srcIsBGRA = false) const;
232 bool BlitImageToFramebuffer(layers::Image* srcImage,
233 const gfx::IntSize& destSize,
234 OriginPos destOrigin);
235 bool BlitSdToFramebuffer(const layers::SurfaceDescriptor&,
236 const gfx::IntSize& destSize, OriginPos destOrigin);
238 private:
239 bool BlitImage(layers::GPUVideoImage* srcImage, const gfx::IntSize& destSize,
240 OriginPos destOrigin) const;
241 #ifdef XP_MACOSX
242 bool BlitImage(MacIOSurface* const iosurf, const gfx::IntSize& destSize,
243 OriginPos destOrigin) const;
244 #endif
245 #ifdef XP_WIN
246 // GLBlitHelperD3D.cpp:
247 bool BlitImage(layers::D3D11ShareHandleImage* srcImage,
248 const gfx::IntSize& destSize, OriginPos destOrigin) const;
249 bool BlitImage(layers::D3D11TextureIMFSampleImage* srcImage,
250 const gfx::IntSize& destSize, OriginPos destOrigin) const;
251 bool BlitImage(layers::D3D11YCbCrImage* srcImage,
252 const gfx::IntSize& destSize, OriginPos destOrigin) const;
254 bool BlitDescriptor(const layers::SurfaceDescriptorD3D10& desc,
255 const gfx::IntSize& destSize, OriginPos destOrigin) const;
256 bool BlitDescriptor(const layers::SurfaceDescriptorDXGIYCbCr& desc,
257 const gfx::IntSize& destSize,
258 const OriginPos destOrigin) const;
259 bool BlitAngleYCbCr(const WindowsHandle (&handleList)[3],
260 const gfx::IntRect& clipRect, const gfx::IntSize& ySize,
261 const gfx::IntSize& uvSize,
262 const gfx::YUVColorSpace colorSpace,
263 const gfx::IntSize& destSize, OriginPos destOrigin) const;
265 bool BlitAnglePlanes(uint8_t numPlanes,
266 const RefPtr<ID3D11Texture2D>* texD3DList,
267 const DrawBlitProg* prog,
268 const DrawBlitProg::BaseArgs& baseArgs,
269 const DrawBlitProg::YUVArgs* const yuvArgs) const;
270 #endif
273 extern const char* const kFragHeader_Tex2D;
274 extern const char* const kFragHeader_Tex2DRect;
275 extern const char* const kFragHeader_TexExt;
276 extern const char* const kFragBody_RGBA;
277 extern const char* const kFragBody_CrYCb;
278 extern const char* const kFragBody_NV12;
279 extern const char* const kFragBody_PlanarYUV;
281 } // namespace gl
282 } // namespace mozilla
284 #endif // GLBLITHELPER_H_