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/. */
6 
7 #ifndef GLCONTEXTEGL_H_
8 #define GLCONTEXTEGL_H_
9 
10 #include "GLContext.h"
11 #include "GLLibraryEGL.h"
12 #include "nsRegion.h"
13 
14 class gfxASurface;
15 namespace mozilla {
16 namespace widget {
17 class CompositorWidget;
18 }  // namespace widget
19 namespace gl {
20 
21 class GLContextEGL : public GLContext {
22   friend class TextureImageEGL;
23 
24   static already_AddRefed<GLContextEGL> CreateGLContext(
25       GLLibraryEGL*, CreateContextFlags flags, const SurfaceCaps& caps,
26       bool isOffscreen, EGLConfig config, EGLSurface surface,
27       const bool useGles, nsACString* const out_failureId);
28 
29  public:
30   MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(GLContextEGL, override)
31   GLContextEGL(GLLibraryEGL*, CreateContextFlags flags, const SurfaceCaps& caps,
32                bool isOffscreen, EGLConfig config, EGLSurface surface,
33                EGLContext context);
34 
35   ~GLContextEGL();
36 
GetContextType()37   virtual GLContextType GetContextType() const override {
38     return GLContextType::EGL;
39   }
40 
Cast(GLContext * gl)41   static GLContextEGL* Cast(GLContext* gl) {
42     MOZ_ASSERT(gl->GetContextType() == GLContextType::EGL);
43     return static_cast<GLContextEGL*>(gl);
44   }
45 
46   bool Init() override;
47 
IsDoubleBuffered()48   virtual bool IsDoubleBuffered() const override { return mIsDoubleBuffered; }
49 
SetIsDoubleBuffered(bool aIsDB)50   void SetIsDoubleBuffered(bool aIsDB) { mIsDoubleBuffered = aIsDB; }
51 
IsANGLE()52   virtual bool IsANGLE() const override { return mEgl->IsANGLE(); }
IsWARP()53   virtual bool IsWARP() const override { return mEgl->IsWARP(); }
54 
55   virtual bool BindTexImage() override;
56 
57   virtual bool ReleaseTexImage() override;
58 
59   void SetEGLSurfaceOverride(EGLSurface surf);
GetEGLSurfaceOverride()60   EGLSurface GetEGLSurfaceOverride() { return mSurfaceOverride; }
61 
62   virtual bool MakeCurrentImpl() const override;
63 
64   virtual bool IsCurrentImpl() const override;
65 
66   virtual bool RenewSurface(widget::CompositorWidget* aWidget) override;
67 
68   virtual void ReleaseSurface() override;
69 
70   Maybe<SymbolLoader> GetSymbolLoader() const override;
71 
72   virtual bool SwapBuffers() override;
73 
74   virtual void SetDamage(const nsIntRegion& aDamageRegion) override;
75 
76   virtual void GetWSIInfo(nsCString* const out) const override;
77 
78   // hold a reference to the given surface
79   // for the lifetime of this context.
80   void HoldSurface(gfxASurface* aSurf);
81 
GetEGLSurface()82   EGLSurface GetEGLSurface() const { return mSurface; }
83 
84   bool HasBufferAge() const;
85   EGLint GetBufferAge() const;
86 
87   bool BindTex2DOffscreen(GLContext* aOffscreen);
88   void UnbindTex2DOffscreen(GLContext* aOffscreen);
89   void BindOffscreenFramebuffer();
90 
91   void Destroy();
92 
93   static already_AddRefed<GLContextEGL> CreateEGLPBufferOffscreenContext(
94       CreateContextFlags flags, const gfx::IntSize& size,
95       const SurfaceCaps& minCaps, nsACString* const out_FailureId);
96   static already_AddRefed<GLContextEGL> CreateEGLPBufferOffscreenContextImpl(
97       CreateContextFlags flags, const gfx::IntSize& size,
98       const SurfaceCaps& minCaps, bool aUseGles,
99       nsACString* const out_FailureId);
100 
101 #if defined(MOZ_WAYLAND) || defined(MOZ_WIDGET_ANDROID)
102   static EGLSurface CreateEGLSurfaceForCompositorWidget(
103       widget::CompositorWidget* aCompositorWidget, const EGLConfig aConfig);
104 #endif
105  protected:
106   friend class GLContextProviderEGL;
107   friend class GLContextEGLFactory;
108 
109   virtual void OnMarkDestroyed() override;
110 
111  public:
112   const RefPtr<GLLibraryEGL> mEgl;
113   const EGLConfig mConfig;
114   const EGLContext mContext;
115 
116  protected:
117   EGLSurface mSurface;
118   const EGLSurface mFallbackSurface;
119 
120   EGLSurface mSurfaceOverride = EGL_NO_SURFACE;
121   RefPtr<gfxASurface> mThebesSurface;
122   bool mBound = false;
123 
124   bool mIsPBuffer = false;
125   bool mIsDoubleBuffered = false;
126   bool mCanBindToTexture = false;
127   bool mShareWithEGLImage = false;
128   bool mOwnsContext = true;
129 
130   nsIntRegion mDamageRegion;
131 
132   static EGLSurface CreatePBufferSurfaceTryingPowerOfTwo(
133       GLLibraryEGL*, EGLConfig config, EGLenum bindToTextureFormat,
134       gfx::IntSize& pbsize);
135 #if defined(MOZ_WAYLAND)
136   static EGLSurface CreateWaylandBufferSurface(GLLibraryEGL*, EGLConfig config,
137                                                gfx::IntSize& pbsize);
138 #endif
139 #if defined(MOZ_WIDGET_ANDROID)
140  public:
141   EGLSurface CreateCompatibleSurface(void* aWindow);
142 #endif  // defined(MOZ_WIDGET_ANDROID)
143 };
144 
145 bool CreateConfig(GLLibraryEGL* const egl, EGLConfig* aConfig, int32_t depth,
146                   bool aEnableDepthBuffer, bool aUseGles);
147 
148 }  // namespace gl
149 }  // namespace mozilla
150 
151 #endif  // GLCONTEXTEGL_H_
152