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