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