1 /* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 4; -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 
6 #include "SharedSurface.h"
7 
8 #include "../2d/2D.h"
9 #include "GLBlitHelper.h"
10 #include "GLContext.h"
11 #include "GLReadTexImageHelper.h"
12 #include "GLScreenBuffer.h"
13 #include "nsThreadUtils.h"
14 #include "ScopedGLHelpers.h"
15 #include "SharedSurfaceGL.h"
16 #include "SharedSurfaceEGL.h"
17 #include "mozilla/gfx/Logging.h"
18 #include "mozilla/layers/CompositorTypes.h"
19 #include "mozilla/layers/TextureClientSharedSurface.h"
20 #include "mozilla/layers/TextureForwarder.h"
21 #include "mozilla/StaticPrefs_webgl.h"
22 #include "mozilla/Unused.h"
23 #include "VRManagerChild.h"
24 
25 #ifdef XP_WIN
26 #  include "SharedSurfaceANGLE.h"
27 #  include "SharedSurfaceD3D11Interop.h"
28 #endif
29 
30 #ifdef XP_MACOSX
31 #  include "SharedSurfaceIO.h"
32 #endif
33 
34 #ifdef MOZ_WAYLAND
35 #  include "gfxPlatformGtk.h"
36 #  include "SharedSurfaceDMABUF.h"
37 #endif
38 
39 #ifdef MOZ_WIDGET_ANDROID
40 #  include "SharedSurfaceAndroidHardwareBuffer.h"
41 #endif
42 
43 namespace mozilla {
44 namespace gl {
45 
46 ////////////////////////////////////////////////////////////////////////
47 // SharedSurface
48 
SharedSurface(const SharedSurfaceDesc & desc,UniquePtr<MozFramebuffer> fb)49 SharedSurface::SharedSurface(const SharedSurfaceDesc& desc,
50                              UniquePtr<MozFramebuffer> fb)
51     : mDesc(desc), mFb(std::move(fb)) {}
52 
53 SharedSurface::~SharedSurface() = default;
54 
LockProd()55 void SharedSurface::LockProd() {
56   MOZ_ASSERT(!mIsLocked);
57 
58   LockProdImpl();
59 
60   mDesc.gl->LockSurface(this);
61   mIsLocked = true;
62 }
63 
UnlockProd()64 void SharedSurface::UnlockProd() {
65   if (!mIsLocked) return;
66 
67   UnlockProdImpl();
68 
69   mDesc.gl->UnlockSurface(this);
70   mIsLocked = false;
71 }
72 
73 ////////////////////////////////////////////////////////////////////////
74 // SurfaceFactory
75 
76 /* static */
Create(GLContext * const pGl,const layers::TextureType consumerType)77 UniquePtr<SurfaceFactory> SurfaceFactory::Create(
78     GLContext* const pGl, const layers::TextureType consumerType) {
79   auto& gl = *pGl;
80 
81   switch (consumerType) {
82     case layers::TextureType::D3D11:
83 #ifdef XP_WIN
84       if (gl.IsANGLE()) {
85         return SurfaceFactory_ANGLEShareHandle::Create(gl);
86       }
87       if (StaticPrefs::webgl_dxgl_enabled()) {
88         return SurfaceFactory_D3D11Interop::Create(gl);
89       }
90 #endif
91       return nullptr;
92 
93     case layers::TextureType::MacIOSurface:
94 #ifdef XP_MACOSX
95       return MakeUnique<SurfaceFactory_IOSurface>(gl);
96 #else
97       return nullptr;
98 #endif
99 
100     case layers::TextureType::DMABUF:
101 #ifdef MOZ_WAYLAND
102       if (gl.GetContextType() == GLContextType::EGL &&
103           widget::GetDMABufDevice()->IsDMABufWebGLEnabled()) {
104         return SurfaceFactory_DMABUF::Create(gl);
105       }
106 #endif
107       return nullptr;
108 
109     case layers::TextureType::AndroidNativeWindow:
110 #ifdef MOZ_WIDGET_ANDROID
111       return MakeUnique<SurfaceFactory_SurfaceTexture>(gl);
112 #else
113       return nullptr;
114 #endif
115 
116     case layers::TextureType::AndroidHardwareBuffer:
117 #ifdef MOZ_WIDGET_ANDROID
118       return SurfaceFactory_AndroidHardwareBuffer::Create(gl);
119 #else
120       return nullptr;
121 #endif
122 
123     case layers::TextureType::EGLImage:
124 #ifdef MOZ_WIDGET_ANDROID
125       if (XRE_IsParentProcess()) {
126         return SurfaceFactory_EGLImage::Create(gl);
127       }
128 #endif
129       return nullptr;
130 
131     case layers::TextureType::Unknown:
132     case layers::TextureType::Last:
133       break;
134   }
135 
136 #ifdef MOZ_X11
137   // Silence a warning.
138   Unused << gl;
139 #endif
140 
141   return nullptr;
142 }
143 
SurfaceFactory(const PartialSharedSurfaceDesc & partialDesc)144 SurfaceFactory::SurfaceFactory(const PartialSharedSurfaceDesc& partialDesc)
145     : mDesc(partialDesc), mMutex("SurfaceFactor::mMutex") {}
146 
147 SurfaceFactory::~SurfaceFactory() = default;
148 
149 }  // namespace gl
150 }  // namespace mozilla
151