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_
14 #include <unordered_map>
15 #include "Colorspaces.h"
17 #include "GLContextTypes.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"
33 # include "mozilla/RefPtr.h"
34 # include "mozilla/ipc/IPCTypes.h"
36 struct ID3D11Texture2D
;
43 #ifdef MOZ_WIDGET_ANDROID
44 # include "mozilla/java/GeckoSurfaceTextureWrappers.h"
56 struct PlanarYCbCrData
;
57 class PlanarYCbCrImage
;
58 class SurfaceDescriptor
;
59 class SurfaceDescriptorBuffer
;
62 class D3D11ShareHandleImage
;
63 class D3D11TextureIMFSampleImage
;
64 class D3D11YCbCrImage
;
65 class SurfaceDescriptorD3D10
;
66 class SurfaceDescriptorDXGIYCbCr
;
69 #ifdef MOZ_WIDGET_ANDROID
70 class SurfaceTextureDescriptor
;
74 class MacIOSurfaceImage
;
78 class DMABUFSurfaceImage
;
84 class BindAnglePlanes
;
89 bool GuessDivisors(const gfx::IntSize
& ySize
, const gfx::IntSize
& uvSize
,
90 gfx::IntSize
* const out_divisors
);
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
]; }
100 for (uint8_t i
= 0; i
< N
; i
++) {
106 Mat
<N
> operator*(const Mat
<N
>& r
) const {
108 for (uint8_t x
= 0; x
< N
; x
++) {
109 for (uint8_t y
= 0; y
< N
; y
++) {
111 for (uint8_t i
= 0; i
< N
; i
++) {
112 sum
+= at(i
, y
) * r
.at(x
, i
);
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
;
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;
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
);
151 struct BaseArgs final
{
155 destSize
; // Always needed for (at least) setting the viewport.
156 Maybe
<gfx::IntRect
> destRect
;
157 Maybe
<uint32_t> texUnitForColorLut
;
159 struct YUVArgs final
{
161 Maybe
<gfx::YUVColorSpace
> colorSpaceForMatrix
;
164 void Draw(const BaseArgs
& args
, const YUVArgs
* argsYUV
= nullptr) const;
167 class ScopedSaveMultiTex final
{
169 const std::vector
<uint8_t> mTexUnits
;
170 const GLenum mTexTarget
;
171 const GLuint mOldTexUnit
;
172 GLuint mOldTexSampler
[3];
176 ScopedSaveMultiTex(GLContext
* gl
, const std::vector
<uint8_t>& texUnits
,
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
;
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};
201 color::ColorspaceDesc src
;
202 color::ColorspaceDesc dst
;
204 auto Members() const { return std::tie(src
, dst
); }
205 INLINE_AUTO_MAPPABLE(ColorLutKey
)
209 mutable std::unordered_map
<ColorLutKey
, std::weak_ptr
<gl::Texture
>,
214 std::shared_ptr
<gl::Texture
> GetColorLutTex(const ColorLutKey
& key
) const;
217 mutable RefPtr
<ID3D11Device
> mD3D11
;
219 ID3D11Device
* GetD3D11() const;
222 const DrawBlitProg
* GetDrawBlitProg(const DrawBlitProg::Key
& key
) const;
225 const DrawBlitProg
* CreateDrawBlitProg(const DrawBlitProg::Key
& key
) const;
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;
235 bool BlitImage(layers::MacIOSurfaceImage
* srcImage
,
236 const gfx::IntSize
& destSize
, OriginPos destOrigin
) const;
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;
245 explicit GLBlitHelper(GLContext
* gl
);
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
);
281 bool BlitImage(layers::GPUVideoImage
* srcImage
, const gfx::IntSize
& destSize
,
282 OriginPos destOrigin
) const;
284 bool BlitImage(MacIOSurface
* const iosurf
, const gfx::IntSize
& destSize
,
285 OriginPos destOrigin
) const;
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;
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
;
336 } // namespace mozilla
338 #endif // GLBLITHELPER_H_