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 #include <memory>
14 
15 namespace mozilla {
16 namespace layers {
17 class SurfaceTextureImage;
18 }  // namespace layers
19 namespace widget {
20 class CompositorWidget;
21 }  // namespace widget
22 namespace gl {
23 
24 RefPtr<GLLibraryEGL> DefaultEglLibrary(nsACString* const out_failureId);
25 
DefaultEglDisplay(nsACString * const out_failureId)26 inline std::shared_ptr<EglDisplay> DefaultEglDisplay(
27     nsACString* const out_failureId) {
28   const auto lib = DefaultEglLibrary(out_failureId);
29   if (!lib) {
30     return nullptr;
31   }
32   return lib->DefaultDisplay(out_failureId);
33 }
34 
35 // -
36 
37 class GLContextEGL final : public GLContext {
38  public:
39   MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(GLContextEGL, override)
40 
41   static RefPtr<GLContextEGL> CreateGLContext(
42       std::shared_ptr<EglDisplay>, const GLContextDesc&, EGLConfig config,
43       EGLSurface surface, const bool useGles, nsACString* const out_failureId);
44 
45  private:
46   GLContextEGL(std::shared_ptr<EglDisplay>, const GLContextDesc&,
47                EGLConfig config, EGLSurface surface, EGLContext context);
48   ~GLContextEGL();
49 
50  public:
GetContextType()51   virtual GLContextType GetContextType() const override {
52     return GLContextType::EGL;
53   }
54 
Cast(GLContext * gl)55   static GLContextEGL* Cast(GLContext* gl) {
56     MOZ_ASSERT(gl->GetContextType() == GLContextType::EGL);
57     return static_cast<GLContextEGL*>(gl);
58   }
59 
60   bool Init() override;
61 
IsDoubleBuffered()62   virtual bool IsDoubleBuffered() const override { return mIsDoubleBuffered; }
63 
SetIsDoubleBuffered(bool aIsDB)64   void SetIsDoubleBuffered(bool aIsDB) { mIsDoubleBuffered = aIsDB; }
65 
IsANGLE()66   virtual bool IsANGLE() const override { return mEgl->mLib->IsANGLE(); }
IsWARP()67   virtual bool IsWARP() const override { return mEgl->mIsWARP; }
68 
69   virtual bool BindTexImage() override;
70 
71   virtual bool ReleaseTexImage() override;
72 
73   void SetEGLSurfaceOverride(EGLSurface surf);
GetEGLSurfaceOverride()74   EGLSurface GetEGLSurfaceOverride() { return mSurfaceOverride; }
75 
76   virtual bool MakeCurrentImpl() const override;
77 
78   virtual bool IsCurrentImpl() const override;
79 
80   virtual bool RenewSurface(widget::CompositorWidget* aWidget) override;
81 
82   virtual void ReleaseSurface() override;
83 
84   Maybe<SymbolLoader> GetSymbolLoader() const override;
85 
86   virtual bool SwapBuffers() override;
87 
88   virtual void SetDamage(const nsIntRegion& aDamageRegion) override;
89 
90   GLint GetBufferAge() const override;
91 
92   virtual void GetWSIInfo(nsCString* const out) const override;
93 
GetEGLSurface()94   EGLSurface GetEGLSurface() const { return mSurface; }
95 
96   bool HasExtBufferAge() const;
97   bool HasKhrPartialUpdate() const;
98 
99   bool BindTex2DOffscreen(GLContext* aOffscreen);
100   void UnbindTex2DOffscreen(GLContext* aOffscreen);
101   void BindOffscreenFramebuffer();
102 
103   void Destroy();
104 
105   static RefPtr<GLContextEGL> CreateEGLPBufferOffscreenContext(
106       std::shared_ptr<EglDisplay>, const GLContextCreateDesc&,
107       const gfx::IntSize& size, nsACString* const out_FailureId);
108   static RefPtr<GLContextEGL> CreateEGLPBufferOffscreenContextImpl(
109       std::shared_ptr<EglDisplay>, const GLContextCreateDesc&,
110       const gfx::IntSize& size, bool aUseGles, nsACString* const out_FailureId);
111 
112   static EGLSurface CreateEGLSurfaceForCompositorWidget(
113       widget::CompositorWidget* aCompositorWidget, const EGLConfig aConfig);
114 
115 #ifdef MOZ_X11
116   static bool FindVisual(int* const out_visualId);
117 #endif
118 
119  protected:
120   friend class GLContextProviderEGL;
121   friend class GLContextEGLFactory;
122 
123   virtual void OnMarkDestroyed() override;
124 
125  public:
126   const std::shared_ptr<EglDisplay> mEgl;
127   const EGLConfig mConfig;
128   const EGLContext mContext;
129 
130  protected:
131   EGLSurface mSurface;
132   const EGLSurface mFallbackSurface;
133 
134   EGLSurface mSurfaceOverride = EGL_NO_SURFACE;
135   bool mBound = false;
136 
137   bool mIsPBuffer = false;
138   bool mIsDoubleBuffered = false;
139   bool mCanBindToTexture = false;
140   bool mShareWithEGLImage = false;
141   bool mOwnsContext = true;
142 
143   nsIntRegion mDamageRegion;
144 
145   static EGLSurface CreatePBufferSurfaceTryingPowerOfTwo(
146       EglDisplay&, EGLConfig, EGLenum bindToTextureFormat,
147       gfx::IntSize& pbsize);
148 
149   static EGLSurface CreateWaylandBufferSurface(EglDisplay&, EGLConfig,
150                                                gfx::IntSize& pbsize);
151 
152  public:
153   EGLSurface CreateCompatibleSurface(void* aWindow) const;
154 };
155 
156 bool CreateConfig(EglDisplay&, EGLConfig* aConfig, int32_t aDepth,
157                   bool aEnableDepthBuffer, bool aUseGles,
158                   bool aAllowFallback = true);
159 
160 }  // namespace gl
161 }  // namespace mozilla
162 
163 #endif  // GLCONTEXTEGL_H_
164