|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 #include "GLContextEGL.h" |
|
6 #include "GLSharedHandleHelpers.h" |
|
7 #ifdef MOZ_WIDGET_ANDROID |
|
8 #include "nsSurfaceTexture.h" |
|
9 #endif |
|
10 |
|
11 namespace mozilla { |
|
12 namespace gl { |
|
13 |
|
14 enum SharedHandleType { |
|
15 SharedHandleType_Image |
|
16 #ifdef MOZ_WIDGET_ANDROID |
|
17 , SharedHandleType_SurfaceTexture |
|
18 #endif |
|
19 }; |
|
20 |
|
21 class SharedTextureHandleWrapper |
|
22 { |
|
23 public: |
|
24 SharedTextureHandleWrapper(SharedHandleType aHandleType) : mHandleType(aHandleType) |
|
25 { |
|
26 } |
|
27 |
|
28 virtual ~SharedTextureHandleWrapper() |
|
29 { |
|
30 } |
|
31 |
|
32 SharedHandleType Type() { return mHandleType; } |
|
33 |
|
34 SharedHandleType mHandleType; |
|
35 }; |
|
36 |
|
37 #ifdef MOZ_WIDGET_ANDROID |
|
38 |
|
39 class SurfaceTextureWrapper: public SharedTextureHandleWrapper |
|
40 { |
|
41 public: |
|
42 SurfaceTextureWrapper(nsSurfaceTexture* aSurfaceTexture) : |
|
43 SharedTextureHandleWrapper(SharedHandleType_SurfaceTexture) |
|
44 , mSurfaceTexture(aSurfaceTexture) |
|
45 { |
|
46 } |
|
47 |
|
48 virtual ~SurfaceTextureWrapper() { |
|
49 mSurfaceTexture = nullptr; |
|
50 } |
|
51 |
|
52 nsSurfaceTexture* SurfaceTexture() { return mSurfaceTexture; } |
|
53 |
|
54 nsRefPtr<nsSurfaceTexture> mSurfaceTexture; |
|
55 }; |
|
56 |
|
57 #endif // MOZ_WIDGET_ANDROID |
|
58 |
|
59 class EGLTextureWrapper : public SharedTextureHandleWrapper |
|
60 { |
|
61 public: |
|
62 EGLTextureWrapper() : |
|
63 SharedTextureHandleWrapper(SharedHandleType_Image) |
|
64 , mEGLImage(nullptr) |
|
65 , mSyncObject(nullptr) |
|
66 { |
|
67 } |
|
68 |
|
69 // Args are the active GL context, and a texture in that GL |
|
70 // context for which to create an EGLImage. After the EGLImage |
|
71 // is created, the texture is unused by EGLTextureWrapper. |
|
72 bool CreateEGLImage(GLContext *ctx, uintptr_t texture) { |
|
73 MOZ_ASSERT(!mEGLImage && texture && sEGLLibrary.HasKHRImageBase()); |
|
74 static const EGLint eglAttributes[] = { |
|
75 LOCAL_EGL_NONE |
|
76 }; |
|
77 EGLContext eglContext = GLContextEGL::Cast(ctx)->GetEGLContext(); |
|
78 mEGLImage = sEGLLibrary.fCreateImage(EGL_DISPLAY(), eglContext, LOCAL_EGL_GL_TEXTURE_2D, |
|
79 (EGLClientBuffer)texture, eglAttributes); |
|
80 if (!mEGLImage) { |
|
81 #ifdef DEBUG |
|
82 printf_stderr("Could not create EGL images: ERROR (0x%04x)\n", sEGLLibrary.fGetError()); |
|
83 #endif |
|
84 return false; |
|
85 } |
|
86 return true; |
|
87 } |
|
88 |
|
89 virtual ~EGLTextureWrapper() { |
|
90 if (mEGLImage) { |
|
91 sEGLLibrary.fDestroyImage(EGL_DISPLAY(), mEGLImage); |
|
92 mEGLImage = nullptr; |
|
93 } |
|
94 } |
|
95 |
|
96 const EGLImage GetEGLImage() { |
|
97 return mEGLImage; |
|
98 } |
|
99 |
|
100 // Insert a sync point on the given context, which should be the current active |
|
101 // context. |
|
102 bool MakeSync(GLContext *ctx) { |
|
103 MOZ_ASSERT(mSyncObject == nullptr); |
|
104 |
|
105 if (sEGLLibrary.IsExtensionSupported(GLLibraryEGL::KHR_fence_sync)) { |
|
106 mSyncObject = sEGLLibrary.fCreateSync(EGL_DISPLAY(), LOCAL_EGL_SYNC_FENCE, nullptr); |
|
107 // We need to flush to make sure the sync object enters the command stream; |
|
108 // we can't use EGL_SYNC_FLUSH_COMMANDS_BIT at wait time, because the wait |
|
109 // happens on a different thread/context. |
|
110 ctx->fFlush(); |
|
111 } |
|
112 |
|
113 if (mSyncObject == EGL_NO_SYNC) { |
|
114 // we failed to create one, so just do a finish |
|
115 ctx->fFinish(); |
|
116 } |
|
117 |
|
118 return true; |
|
119 } |
|
120 |
|
121 bool WaitSync() { |
|
122 if (!mSyncObject) { |
|
123 // if we have no sync object, then we did a Finish() earlier |
|
124 return true; |
|
125 } |
|
126 |
|
127 // wait at most 1 second; this should really be never/rarely hit |
|
128 const uint64_t ns_per_ms = 1000 * 1000; |
|
129 EGLTime timeout = 1000 * ns_per_ms; |
|
130 |
|
131 EGLint result = sEGLLibrary.fClientWaitSync(EGL_DISPLAY(), mSyncObject, 0, timeout); |
|
132 sEGLLibrary.fDestroySync(EGL_DISPLAY(), mSyncObject); |
|
133 mSyncObject = nullptr; |
|
134 |
|
135 return result == LOCAL_EGL_CONDITION_SATISFIED; |
|
136 } |
|
137 |
|
138 private: |
|
139 EGLImage mEGLImage; |
|
140 EGLSync mSyncObject; |
|
141 }; |
|
142 |
|
143 static bool DoesEGLContextSupportSharingWithEGLImage(GLContext *gl) |
|
144 { |
|
145 return sEGLLibrary.HasKHRImageBase() && |
|
146 sEGLLibrary.HasKHRImageTexture2D() && |
|
147 gl->IsExtensionSupported(GLContext::OES_EGL_image); |
|
148 } |
|
149 |
|
150 SharedTextureHandle CreateSharedHandle(GLContext* gl, |
|
151 SharedTextureShareType shareType, |
|
152 void* buffer, |
|
153 SharedTextureBufferType bufferType) |
|
154 { |
|
155 // unimplemented outside of EGL |
|
156 if (gl->GetContextType() != GLContextType::EGL) |
|
157 return 0; |
|
158 |
|
159 // Both EGLImage and SurfaceTexture only support same-process currently, but |
|
160 // it's possible to make SurfaceTexture work across processes. We should do that. |
|
161 if (shareType != SharedTextureShareType::SameProcess) |
|
162 return 0; |
|
163 |
|
164 switch (bufferType) { |
|
165 #ifdef MOZ_WIDGET_ANDROID |
|
166 case SharedTextureBufferType::SurfaceTexture: |
|
167 if (!gl->IsExtensionSupported(GLContext::OES_EGL_image_external)) { |
|
168 NS_WARNING("Missing GL_OES_EGL_image_external"); |
|
169 return 0; |
|
170 } |
|
171 |
|
172 return (SharedTextureHandle) new SurfaceTextureWrapper(reinterpret_cast<nsSurfaceTexture*>(buffer)); |
|
173 #endif |
|
174 case SharedTextureBufferType::TextureID: { |
|
175 if (!DoesEGLContextSupportSharingWithEGLImage(gl)) |
|
176 return 0; |
|
177 |
|
178 GLuint texture = (uintptr_t)buffer; |
|
179 EGLTextureWrapper* tex = new EGLTextureWrapper(); |
|
180 if (!tex->CreateEGLImage(gl, texture)) { |
|
181 NS_ERROR("EGLImage creation for EGLTextureWrapper failed"); |
|
182 delete tex; |
|
183 return 0; |
|
184 } |
|
185 |
|
186 return (SharedTextureHandle)tex; |
|
187 } |
|
188 default: |
|
189 NS_ERROR("Unknown shared texture buffer type"); |
|
190 return 0; |
|
191 } |
|
192 } |
|
193 |
|
194 void ReleaseSharedHandle(GLContext* gl, |
|
195 SharedTextureShareType shareType, |
|
196 SharedTextureHandle sharedHandle) |
|
197 { |
|
198 // unimplemented outside of EGL |
|
199 if (gl->GetContextType() != GLContextType::EGL) |
|
200 return; |
|
201 |
|
202 if (shareType != SharedTextureShareType::SameProcess) { |
|
203 NS_ERROR("Implementation not available for this sharing type"); |
|
204 return; |
|
205 } |
|
206 |
|
207 SharedTextureHandleWrapper* wrapper = reinterpret_cast<SharedTextureHandleWrapper*>(sharedHandle); |
|
208 |
|
209 switch (wrapper->Type()) { |
|
210 #ifdef MOZ_WIDGET_ANDROID |
|
211 case SharedHandleType_SurfaceTexture: |
|
212 delete wrapper; |
|
213 break; |
|
214 #endif |
|
215 |
|
216 case SharedHandleType_Image: { |
|
217 NS_ASSERTION(DoesEGLContextSupportSharingWithEGLImage(gl), "EGLImage not supported or disabled in runtime"); |
|
218 |
|
219 EGLTextureWrapper* wrap = (EGLTextureWrapper*)sharedHandle; |
|
220 delete wrap; |
|
221 break; |
|
222 } |
|
223 |
|
224 default: |
|
225 NS_ERROR("Unknown shared handle type"); |
|
226 return; |
|
227 } |
|
228 } |
|
229 |
|
230 bool GetSharedHandleDetails(GLContext* gl, |
|
231 SharedTextureShareType shareType, |
|
232 SharedTextureHandle sharedHandle, |
|
233 SharedHandleDetails& details) |
|
234 { |
|
235 // unimplemented outside of EGL |
|
236 if (gl->GetContextType() != GLContextType::EGL) |
|
237 return false; |
|
238 |
|
239 if (shareType != SharedTextureShareType::SameProcess) |
|
240 return false; |
|
241 |
|
242 SharedTextureHandleWrapper* wrapper = reinterpret_cast<SharedTextureHandleWrapper*>(sharedHandle); |
|
243 |
|
244 switch (wrapper->Type()) { |
|
245 #ifdef MOZ_WIDGET_ANDROID |
|
246 case SharedHandleType_SurfaceTexture: { |
|
247 SurfaceTextureWrapper* surfaceWrapper = reinterpret_cast<SurfaceTextureWrapper*>(wrapper); |
|
248 |
|
249 details.mTarget = LOCAL_GL_TEXTURE_EXTERNAL; |
|
250 details.mTextureFormat = gfx::SurfaceFormat::R8G8B8A8; |
|
251 surfaceWrapper->SurfaceTexture()->GetTransformMatrix(details.mTextureTransform); |
|
252 break; |
|
253 } |
|
254 #endif |
|
255 |
|
256 case SharedHandleType_Image: |
|
257 details.mTarget = LOCAL_GL_TEXTURE_2D; |
|
258 details.mTextureFormat = gfx::SurfaceFormat::R8G8B8A8; |
|
259 break; |
|
260 |
|
261 default: |
|
262 NS_ERROR("Unknown shared handle type"); |
|
263 return false; |
|
264 } |
|
265 |
|
266 return true; |
|
267 } |
|
268 |
|
269 bool AttachSharedHandle(GLContext* gl, |
|
270 SharedTextureShareType shareType, |
|
271 SharedTextureHandle sharedHandle) |
|
272 { |
|
273 // unimplemented outside of EGL |
|
274 if (gl->GetContextType() != GLContextType::EGL) |
|
275 return false; |
|
276 |
|
277 if (shareType != SharedTextureShareType::SameProcess) |
|
278 return false; |
|
279 |
|
280 SharedTextureHandleWrapper* wrapper = reinterpret_cast<SharedTextureHandleWrapper*>(sharedHandle); |
|
281 |
|
282 switch (wrapper->Type()) { |
|
283 #ifdef MOZ_WIDGET_ANDROID |
|
284 case SharedHandleType_SurfaceTexture: { |
|
285 #ifndef DEBUG |
|
286 /* |
|
287 * NOTE: SurfaceTexture spams us if there are any existing GL errors, so we'll clear |
|
288 * them here in order to avoid that. |
|
289 */ |
|
290 gl->GetAndClearError(); |
|
291 #endif |
|
292 SurfaceTextureWrapper* surfaceTextureWrapper = reinterpret_cast<SurfaceTextureWrapper*>(wrapper); |
|
293 |
|
294 // FIXME: SurfaceTexture provides a transform matrix which is supposed to |
|
295 // be applied to the texture coordinates. We should return that here |
|
296 // so we can render correctly. Bug 775083 |
|
297 surfaceTextureWrapper->SurfaceTexture()->UpdateTexImage(); |
|
298 break; |
|
299 } |
|
300 #endif // MOZ_WIDGET_ANDROID |
|
301 |
|
302 case SharedHandleType_Image: { |
|
303 NS_ASSERTION(DoesEGLContextSupportSharingWithEGLImage(gl), "EGLImage not supported or disabled in runtime"); |
|
304 |
|
305 EGLTextureWrapper* wrap = (EGLTextureWrapper*)sharedHandle; |
|
306 wrap->WaitSync(); |
|
307 gl->fEGLImageTargetTexture2D(LOCAL_GL_TEXTURE_2D, wrap->GetEGLImage()); |
|
308 break; |
|
309 } |
|
310 |
|
311 default: |
|
312 NS_ERROR("Unknown shared handle type"); |
|
313 return false; |
|
314 } |
|
315 |
|
316 return true; |
|
317 } |
|
318 |
|
319 /** |
|
320 * Detach Shared GL Handle from GL_TEXTURE_2D target |
|
321 */ |
|
322 void DetachSharedHandle(GLContext*, |
|
323 SharedTextureShareType, |
|
324 SharedTextureHandle) |
|
325 { |
|
326 // currently a no-operation |
|
327 } |
|
328 |
|
329 } |
|
330 } |