Bug 1735097 - Geolocation: use EpochTimeStamp instead of DOMTimeStamp r=saschanaz...
[gecko.git] / gfx / gl / GLBlitHelper.h
blob3d9e4d24ff008e2df3c2910e90cd64831ce8a978
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 namespace mozilla {
41 namespace layers {
42 class Image;
43 class GPUVideoImage;
44 class PlanarYCbCrImage;
45 class SurfaceDescriptor;
47 #ifdef XP_WIN
48 class D3D11ShareHandleImage;
49 class D3D11YCbCrImage;
50 class SurfaceDescriptorD3D10;
51 class SurfaceDescriptorDXGIYCbCr;
52 #endif
54 #ifdef MOZ_WIDGET_ANDROID
55 class SurfaceTextureImage;
56 #endif
58 #ifdef XP_MACOSX
59 class MacIOSurfaceImage;
60 #endif
62 #ifdef MOZ_WAYLAND
63 class DMABUFSurfaceImage;
64 #endif
65 } // namespace layers
67 namespace gl {
69 class BindAnglePlanes;
70 class GLContext;
71 class GLBlitHelper;
73 bool GuessDivisors(const gfx::IntSize& ySize, const gfx::IntSize& uvSize,
74 gfx::IntSize* const out_divisors);
76 template <uint8_t N>
77 struct Mat {
78 float m[N * N]; // column-major, for GL
80 float& at(const uint8_t x, const uint8_t y) { return m[N * x + y]; }
82 static Mat<N> Zero();
83 static Mat<N> I();
85 Mat<N> operator*(const Mat<N>& r) const;
87 typedef Mat<3> Mat3;
89 Mat3 SubRectMat3(float x, float y, float w, float h);
90 Mat3 SubRectMat3(const gfx::IntRect& subrect, const gfx::IntSize& size);
91 Mat3 SubRectMat3(const gfx::IntRect& bigSubrect, const gfx::IntSize& smallSize,
92 const gfx::IntSize& divisors);
94 class DrawBlitProg final {
95 const GLBlitHelper& mParent;
96 const GLuint mProg;
97 const GLint mLoc_uDestMatrix;
98 const GLint mLoc_uTexMatrix0;
99 const GLint mLoc_uTexMatrix1;
100 const GLint mLoc_uColorMatrix;
101 GLenum mType_uColorMatrix = 0;
103 public:
104 struct Key final {
105 const char* const fragHeader;
106 const char* const fragBody;
108 bool operator<(const Key& x) const {
109 if (fragHeader != x.fragHeader) return fragHeader < x.fragHeader;
110 return fragBody < x.fragBody;
114 DrawBlitProg(const GLBlitHelper* parent, GLuint prog);
115 ~DrawBlitProg();
117 struct BaseArgs final {
118 Mat3 texMatrix0;
119 bool yFlip;
120 gfx::IntSize
121 destSize; // Always needed for (at least) setting the viewport.
122 Maybe<gfx::IntRect> destRect;
124 struct YUVArgs final {
125 Mat3 texMatrix1;
126 gfx::YUVColorSpace colorSpace;
129 void Draw(const BaseArgs& args, const YUVArgs* argsYUV = nullptr) const;
132 class ScopedSaveMultiTex final {
133 GLContext& mGL;
134 const uint8_t mTexCount;
135 const GLenum mTexTarget;
136 const GLuint mOldTexUnit;
137 GLuint mOldTexSampler[3];
138 GLuint mOldTex[3];
140 public:
141 ScopedSaveMultiTex(GLContext* gl, uint8_t texCount, GLenum texTarget);
142 ~ScopedSaveMultiTex();
145 /** Buffer blitting helper */
146 class GLBlitHelper final {
147 friend class BindAnglePlanes;
148 friend class DrawBlitProg;
149 friend class GLContext;
151 GLContext* const mGL;
152 mutable std::map<DrawBlitProg::Key, const DrawBlitProg*> mDrawBlitProgs;
154 GLuint mQuadVAO = 0;
155 GLuint mQuadVBO = 0;
156 nsCString mDrawBlitProg_VersionLine;
157 const GLuint mDrawBlitProg_VertShader;
159 GLuint mYuvUploads[3] = {};
160 gfx::IntSize mYuvUploads_YSize = {0, 0};
161 gfx::IntSize mYuvUploads_UVSize = {0, 0};
163 #ifdef XP_WIN
164 mutable RefPtr<ID3D11Device> mD3D11;
166 ID3D11Device* GetD3D11() const;
167 #endif
169 const DrawBlitProg* GetDrawBlitProg(const DrawBlitProg::Key& key) const;
171 private:
172 const DrawBlitProg* CreateDrawBlitProg(const DrawBlitProg::Key& key) const;
174 public:
175 bool BlitImage(layers::PlanarYCbCrImage* yuvImage,
176 const gfx::IntSize& destSize, OriginPos destOrigin);
177 #ifdef MOZ_WIDGET_ANDROID
178 // Blit onto the current FB.
179 bool BlitImage(layers::SurfaceTextureImage* stImage,
180 const gfx::IntSize& destSize, OriginPos destOrigin) const;
181 #endif
182 #ifdef XP_MACOSX
183 bool BlitImage(layers::MacIOSurfaceImage* srcImage,
184 const gfx::IntSize& destSize, OriginPos destOrigin) const;
185 #endif
186 #ifdef MOZ_WAYLAND
187 bool BlitImage(layers::DMABUFSurfaceImage* srcImage,
188 const gfx::IntSize& destSize, OriginPos destOrigin) const;
189 #endif
191 explicit GLBlitHelper(GLContext* gl);
193 public:
194 ~GLBlitHelper();
196 void BlitFramebuffer(const gfx::IntRect& srcRect,
197 const gfx::IntRect& destRect,
198 GLuint filter = LOCAL_GL_NEAREST) const;
199 void BlitFramebufferToFramebuffer(GLuint srcFB, GLuint destFB,
200 const gfx::IntRect& srcRect,
201 const gfx::IntRect& destRect,
202 GLuint filter = LOCAL_GL_NEAREST) const;
203 void BlitFramebufferToTexture(GLuint destTex, const gfx::IntSize& srcSize,
204 const gfx::IntSize& destSize,
205 GLenum destTarget = LOCAL_GL_TEXTURE_2D) const;
206 void BlitTextureToFramebuffer(GLuint srcTex, const gfx::IntSize& srcSize,
207 const gfx::IntSize& destSize,
208 GLenum srcTarget = LOCAL_GL_TEXTURE_2D) const;
209 void BlitTextureToTexture(GLuint srcTex, GLuint destTex,
210 const gfx::IntSize& srcSize,
211 const gfx::IntSize& destSize,
212 GLenum srcTarget = LOCAL_GL_TEXTURE_2D,
213 GLenum destTarget = LOCAL_GL_TEXTURE_2D) const;
215 void DrawBlitTextureToFramebuffer(
216 GLuint srcTex, const gfx::IntSize& srcSize, const gfx::IntSize& destSize,
217 GLenum srcTarget = LOCAL_GL_TEXTURE_2D) const;
219 bool BlitImageToFramebuffer(layers::Image* srcImage,
220 const gfx::IntSize& destSize,
221 OriginPos destOrigin);
222 bool BlitSdToFramebuffer(const layers::SurfaceDescriptor&,
223 const gfx::IntSize& destSize, OriginPos destOrigin);
225 private:
226 bool BlitImage(layers::GPUVideoImage* srcImage, const gfx::IntSize& destSize,
227 OriginPos destOrigin) const;
228 #ifdef XP_MACOSX
229 bool BlitImage(MacIOSurface* const iosurf, const gfx::IntSize& destSize,
230 OriginPos destOrigin) const;
231 #endif
232 #ifdef XP_WIN
233 // GLBlitHelperD3D.cpp:
234 bool BlitImage(layers::D3D11ShareHandleImage* srcImage,
235 const gfx::IntSize& destSize, OriginPos destOrigin) const;
236 bool BlitImage(layers::D3D11YCbCrImage* srcImage,
237 const gfx::IntSize& destSize, OriginPos destOrigin) const;
239 bool BlitDescriptor(const layers::SurfaceDescriptorD3D10& desc,
240 const gfx::IntSize& destSize, OriginPos destOrigin) const;
241 bool BlitDescriptor(const layers::SurfaceDescriptorDXGIYCbCr& desc,
242 const gfx::IntSize& destSize,
243 const OriginPos destOrigin) const;
244 bool BlitAngleYCbCr(const WindowsHandle (&handleList)[3],
245 const gfx::IntRect& clipRect, const gfx::IntSize& ySize,
246 const gfx::IntSize& uvSize,
247 const gfx::YUVColorSpace colorSpace,
248 const gfx::IntSize& destSize, OriginPos destOrigin) const;
250 bool BlitAnglePlanes(uint8_t numPlanes,
251 const RefPtr<ID3D11Texture2D>* texD3DList,
252 const DrawBlitProg* prog,
253 const DrawBlitProg::BaseArgs& baseArgs,
254 const DrawBlitProg::YUVArgs* const yuvArgs) const;
255 #endif
258 extern const char* const kFragHeader_Tex2D;
259 extern const char* const kFragHeader_Tex2DRect;
260 extern const char* const kFragHeader_TexExt;
261 extern const char* const kFragBody_RGBA;
262 extern const char* const kFragBody_CrYCb;
263 extern const char* const kFragBody_NV12;
264 extern const char* const kFragBody_PlanarYUV;
266 } // namespace gl
267 } // namespace mozilla
269 #endif // GLBLITHELPER_H_