Bug 1874684 - Part 28: Return DateDuration from DifferenceISODateTime. r=mgaudet
[gecko.git] / gfx / gl / GLBlitHelper.h
blob83915090979a02a63cece7726efd836f504988ce
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 <array>
11 #include <cstdint>
12 #include <map>
13 #include <memory>
14 #include <unordered_map>
15 #include "Colorspaces.h"
16 #include "GLConsts.h"
17 #include "GLContextTypes.h"
18 #include "GLTypes.h"
19 #include "nsSize.h"
20 #include "nsString.h"
21 #include "nsTString.h"
22 #include "mozilla/ipc/IPCTypes.h"
23 #include "mozilla/Attributes.h"
24 #include "mozilla/Maybe.h"
25 #include "mozilla/gfx/Point.h"
26 #include "mozilla/gfx/Rect.h"
27 #include "mozilla/gfx/Types.h"
29 #include <map>
31 #ifdef XP_WIN
32 # include <windows.h>
33 # include "mozilla/RefPtr.h"
34 # include "mozilla/ipc/IPCTypes.h"
35 struct ID3D11Device;
36 struct ID3D11Texture2D;
37 #endif
39 #ifdef XP_MACOSX
40 class MacIOSurface;
41 #endif
43 #ifdef MOZ_WIDGET_ANDROID
44 # include "mozilla/java/GeckoSurfaceTextureWrappers.h"
45 #endif
47 #ifdef MOZ_WIDGET_GTK
48 class DMABufSurface;
49 #endif
51 namespace mozilla {
53 namespace layers {
54 class Image;
55 class GPUVideoImage;
56 struct PlanarYCbCrData;
57 class PlanarYCbCrImage;
58 class SurfaceDescriptor;
59 class SurfaceDescriptorBuffer;
61 #ifdef XP_WIN
62 class D3D11ShareHandleImage;
63 class D3D11TextureIMFSampleImage;
64 class D3D11YCbCrImage;
65 class SurfaceDescriptorD3D10;
66 class SurfaceDescriptorDXGIYCbCr;
67 #endif
69 #ifdef MOZ_WIDGET_ANDROID
70 class SurfaceTextureDescriptor;
71 #endif
73 #ifdef XP_MACOSX
74 class MacIOSurfaceImage;
75 #endif
77 #ifdef MOZ_WIDGET_GTK
78 class DMABUFSurfaceImage;
79 #endif
80 } // namespace layers
82 namespace gl {
84 class BindAnglePlanes;
85 class GLBlitHelper;
86 class GLContext;
87 class Texture;
89 bool GuessDivisors(const gfx::IntSize& ySize, const gfx::IntSize& uvSize,
90 gfx::IntSize* const out_divisors);
92 template <uint8_t N>
93 struct Mat {
94 float m[N * N]; // column-major, for GL
96 float& at(const uint8_t x, const uint8_t y) { return m[N * x + y]; }
98 static Mat<N> I() {
99 auto ret = Mat<N>{};
100 for (uint8_t i = 0; i < N; i++) {
101 ret.at(i, i) = 1.0f;
103 return ret;
106 Mat<N> operator*(const Mat<N>& r) const {
107 Mat<N> ret;
108 for (uint8_t x = 0; x < N; x++) {
109 for (uint8_t y = 0; y < N; y++) {
110 float sum = 0.0f;
111 for (uint8_t i = 0; i < N; i++) {
112 sum += at(i, y) * r.at(x, i);
114 ret.at(x, y) = sum;
117 return ret;
120 typedef Mat<3> Mat3;
122 Mat3 SubRectMat3(float x, float y, float w, float h);
123 Mat3 SubRectMat3(const gfx::IntRect& subrect, const gfx::IntSize& size);
124 Mat3 SubRectMat3(const gfx::IntRect& bigSubrect, const gfx::IntSize& smallSize,
125 const gfx::IntSize& divisors);
127 class DrawBlitProg final {
128 const GLBlitHelper& mParent;
129 const GLuint mProg;
130 const GLint mLoc_uDestMatrix;
131 const GLint mLoc_uTexMatrix0;
132 const GLint mLoc_uTexMatrix1;
133 const GLint mLoc_uColorLut;
134 const GLint mLoc_uColorMatrix;
135 GLenum mType_uColorMatrix = 0;
137 public:
138 struct Key final {
139 const char* fragHeader = nullptr;
140 std::array<const char*, 4> fragParts = {};
142 auto Members() const { return std::tie(fragHeader, fragParts); }
143 friend bool operator<(const Key& a, const Key& b) {
144 return a.Members() < b.Members();
148 DrawBlitProg(const GLBlitHelper* parent, GLuint prog);
149 ~DrawBlitProg();
151 struct BaseArgs final {
152 Mat3 texMatrix0;
153 bool yFlip;
154 gfx::IntSize
155 destSize; // Always needed for (at least) setting the viewport.
156 Maybe<gfx::IntRect> destRect;
157 Maybe<uint32_t> texUnitForColorLut;
159 struct YUVArgs final {
160 Mat3 texMatrix1;
161 Maybe<gfx::YUVColorSpace> colorSpaceForMatrix;
164 void Draw(const BaseArgs& args, const YUVArgs* argsYUV = nullptr) const;
167 class ScopedSaveMultiTex final {
168 GLContext& mGL;
169 const std::vector<uint8_t> mTexUnits;
170 const GLenum mTexTarget;
171 const GLuint mOldTexUnit;
172 GLuint mOldTexSampler[3];
173 GLuint mOldTex[3];
175 public:
176 ScopedSaveMultiTex(GLContext* gl, const std::vector<uint8_t>& texUnits,
177 GLenum texTarget);
178 ~ScopedSaveMultiTex();
181 /** Buffer blitting helper */
182 class GLBlitHelper final {
183 friend class BindAnglePlanes;
184 friend class DrawBlitProg;
185 friend class GLContext;
187 GLContext* const mGL;
188 mutable std::map<DrawBlitProg::Key, const DrawBlitProg*> mDrawBlitProgs;
190 GLuint mQuadVAO = 0;
191 GLuint mQuadVBO = 0;
192 nsCString mDrawBlitProg_VersionLine;
193 const GLuint mDrawBlitProg_VertShader;
195 GLuint mYuvUploads[3] = {};
196 gfx::IntSize mYuvUploads_YSize = {0, 0};
197 gfx::IntSize mYuvUploads_UVSize = {0, 0};
199 public:
200 struct ColorLutKey {
201 color::ColorspaceDesc src;
202 color::ColorspaceDesc dst;
204 auto Members() const { return std::tie(src, dst); }
205 INLINE_AUTO_MAPPABLE(ColorLutKey)
208 private:
209 mutable std::unordered_map<ColorLutKey, std::weak_ptr<gl::Texture>,
210 ColorLutKey::Hasher>
211 mColorLutTexMap;
213 public:
214 std::shared_ptr<gl::Texture> GetColorLutTex(const ColorLutKey& key) const;
216 #ifdef XP_WIN
217 mutable RefPtr<ID3D11Device> mD3D11;
219 ID3D11Device* GetD3D11() const;
220 #endif
222 const DrawBlitProg* GetDrawBlitProg(const DrawBlitProg::Key& key) const;
224 private:
225 const DrawBlitProg* CreateDrawBlitProg(const DrawBlitProg::Key& key) const;
227 public:
228 bool BlitPlanarYCbCr(const layers::PlanarYCbCrData&,
229 const gfx::IntSize& destSize, OriginPos destOrigin);
230 #ifdef MOZ_WIDGET_ANDROID
231 bool Blit(const java::GeckoSurfaceTexture::Ref& surfaceTexture,
232 const gfx::IntSize& destSize, const OriginPos destOrigin) const;
233 #endif
234 #ifdef XP_MACOSX
235 bool BlitImage(layers::MacIOSurfaceImage* srcImage,
236 const gfx::IntSize& destSize, OriginPos destOrigin) const;
237 #endif
238 #ifdef MOZ_WIDGET_GTK
239 bool Blit(DMABufSurface* surface, const gfx::IntSize& destSize,
240 OriginPos destOrigin) const;
241 bool BlitImage(layers::DMABUFSurfaceImage* srcImage,
242 const gfx::IntSize& destSize, OriginPos destOrigin) const;
243 #endif
245 explicit GLBlitHelper(GLContext* gl);
247 public:
248 ~GLBlitHelper();
250 void BlitFramebuffer(const gfx::IntRect& srcRect,
251 const gfx::IntRect& destRect,
252 GLuint filter = LOCAL_GL_NEAREST) const;
253 void BlitFramebufferToFramebuffer(GLuint srcFB, GLuint destFB,
254 const gfx::IntRect& srcRect,
255 const gfx::IntRect& destRect,
256 GLuint filter = LOCAL_GL_NEAREST) const;
257 void BlitFramebufferToTexture(GLuint destTex, const gfx::IntSize& srcSize,
258 const gfx::IntSize& destSize,
259 GLenum destTarget = LOCAL_GL_TEXTURE_2D) const;
260 void BlitTextureToFramebuffer(GLuint srcTex, const gfx::IntSize& srcSize,
261 const gfx::IntSize& destSize,
262 GLenum srcTarget = LOCAL_GL_TEXTURE_2D) const;
263 void BlitTextureToTexture(GLuint srcTex, GLuint destTex,
264 const gfx::IntSize& srcSize,
265 const gfx::IntSize& destSize,
266 GLenum srcTarget = LOCAL_GL_TEXTURE_2D,
267 GLenum destTarget = LOCAL_GL_TEXTURE_2D) const;
269 void DrawBlitTextureToFramebuffer(GLuint srcTex, const gfx::IntSize& srcSize,
270 const gfx::IntSize& destSize,
271 GLenum srcTarget = LOCAL_GL_TEXTURE_2D,
272 bool srcIsBGRA = false) const;
274 bool BlitImageToFramebuffer(layers::Image* srcImage,
275 const gfx::IntSize& destSize,
276 OriginPos destOrigin);
277 bool BlitSdToFramebuffer(const layers::SurfaceDescriptor&,
278 const gfx::IntSize& destSize, OriginPos destOrigin);
280 private:
281 bool BlitImage(layers::GPUVideoImage* srcImage, const gfx::IntSize& destSize,
282 OriginPos destOrigin) const;
283 #ifdef XP_MACOSX
284 bool BlitImage(MacIOSurface* const iosurf, const gfx::IntSize& destSize,
285 OriginPos destOrigin) const;
286 #endif
287 #ifdef XP_WIN
288 // GLBlitHelperD3D.cpp:
289 bool BlitImage(layers::D3D11ShareHandleImage* srcImage,
290 const gfx::IntSize& destSize, OriginPos destOrigin) const;
291 bool BlitImage(layers::D3D11TextureIMFSampleImage* srcImage,
292 const gfx::IntSize& destSize, OriginPos destOrigin) const;
293 bool BlitImage(layers::D3D11YCbCrImage* srcImage,
294 const gfx::IntSize& destSize, OriginPos destOrigin) const;
296 bool BlitDescriptor(const layers::SurfaceDescriptorD3D10& desc,
297 const gfx::IntSize& destSize, OriginPos destOrigin) const;
298 bool BlitDescriptor(const layers::SurfaceDescriptorDXGIYCbCr& desc,
299 const gfx::IntSize& destSize,
300 const OriginPos destOrigin) const;
301 bool BlitAngleYCbCr(const WindowsHandle (&handleList)[3],
302 const gfx::IntRect& clipRect, const gfx::IntSize& ySize,
303 const gfx::IntSize& uvSize,
304 const gfx::YUVColorSpace colorSpace,
305 const gfx::IntSize& destSize, OriginPos destOrigin) const;
307 bool BlitAnglePlanes(uint8_t numPlanes,
308 const RefPtr<ID3D11Texture2D>* texD3DList,
309 const DrawBlitProg* prog,
310 const DrawBlitProg::BaseArgs& baseArgs,
311 const DrawBlitProg::YUVArgs* const yuvArgs) const;
312 #endif
315 // -
316 // For DrawBlitProg::Key::fragParts
318 extern const char* const kFragHeader_Tex2D;
319 extern const char* const kFragHeader_Tex2DRect;
320 extern const char* const kFragHeader_TexExt;
322 extern const char* const kFragSample_OnePlane;
323 extern const char* const kFragSample_TwoPlane;
324 extern const char* const kFragSample_ThreePlane;
326 extern const char* const kFragConvert_None;
327 extern const char* const kFragConvert_BGR;
328 extern const char* const kFragConvert_ColorMatrix;
329 extern const char* const kFragConvert_ColorLut;
331 extern const char* const kFragMixin_AlphaMultColors;
332 extern const char* const kFragMixin_AlphaClampColors;
333 extern const char* const kFragMixin_AlphaOne;
335 } // namespace gl
336 } // namespace mozilla
338 #endif // GLBLITHELPER_H_