gfx/gl/GLSharedHandleHelpers.cpp

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

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.

     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/. */
     5 #include "GLContextEGL.h"
     6 #include "GLSharedHandleHelpers.h"
     7 #ifdef MOZ_WIDGET_ANDROID
     8 #include "nsSurfaceTexture.h"
     9 #endif
    11 namespace mozilla {
    12 namespace gl {
    14 enum SharedHandleType {
    15     SharedHandleType_Image
    16 #ifdef MOZ_WIDGET_ANDROID
    17     , SharedHandleType_SurfaceTexture
    18 #endif
    19 };
    21 class SharedTextureHandleWrapper
    22 {
    23 public:
    24     SharedTextureHandleWrapper(SharedHandleType aHandleType) : mHandleType(aHandleType)
    25     {
    26     }
    28     virtual ~SharedTextureHandleWrapper()
    29     {
    30     }
    32     SharedHandleType Type() { return mHandleType; }
    34     SharedHandleType mHandleType;
    35 };
    37 #ifdef MOZ_WIDGET_ANDROID
    39 class SurfaceTextureWrapper: public SharedTextureHandleWrapper
    40 {
    41 public:
    42     SurfaceTextureWrapper(nsSurfaceTexture* aSurfaceTexture) :
    43         SharedTextureHandleWrapper(SharedHandleType_SurfaceTexture)
    44         , mSurfaceTexture(aSurfaceTexture)
    45     {
    46     }
    48     virtual ~SurfaceTextureWrapper() {
    49         mSurfaceTexture = nullptr;
    50     }
    52     nsSurfaceTexture* SurfaceTexture() { return mSurfaceTexture; }
    54     nsRefPtr<nsSurfaceTexture> mSurfaceTexture;
    55 };
    57 #endif // MOZ_WIDGET_ANDROID
    59 class EGLTextureWrapper : public SharedTextureHandleWrapper
    60 {
    61 public:
    62     EGLTextureWrapper() :
    63         SharedTextureHandleWrapper(SharedHandleType_Image)
    64         , mEGLImage(nullptr)
    65         , mSyncObject(nullptr)
    66     {
    67     }
    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     }
    89     virtual ~EGLTextureWrapper() {
    90         if (mEGLImage) {
    91             sEGLLibrary.fDestroyImage(EGL_DISPLAY(), mEGLImage);
    92             mEGLImage = nullptr;
    93         }
    94     }
    96     const EGLImage GetEGLImage() {
    97         return mEGLImage;
    98     }
   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);
   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         }
   113         if (mSyncObject == EGL_NO_SYNC) {
   114             // we failed to create one, so just do a finish
   115             ctx->fFinish();
   116         }
   118         return true;
   119     }
   121     bool WaitSync() {
   122         if (!mSyncObject) {
   123             // if we have no sync object, then we did a Finish() earlier
   124             return true;
   125         }
   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;
   131         EGLint result = sEGLLibrary.fClientWaitSync(EGL_DISPLAY(), mSyncObject, 0, timeout);
   132         sEGLLibrary.fDestroySync(EGL_DISPLAY(), mSyncObject);
   133         mSyncObject = nullptr;
   135         return result == LOCAL_EGL_CONDITION_SATISFIED;
   136     }
   138 private:
   139     EGLImage mEGLImage;
   140     EGLSync mSyncObject;
   141 };
   143 static bool DoesEGLContextSupportSharingWithEGLImage(GLContext *gl)
   144 {
   145     return sEGLLibrary.HasKHRImageBase() &&
   146            sEGLLibrary.HasKHRImageTexture2D() &&
   147            gl->IsExtensionSupported(GLContext::OES_EGL_image);
   148 }
   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;
   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;
   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         }
   172         return (SharedTextureHandle) new SurfaceTextureWrapper(reinterpret_cast<nsSurfaceTexture*>(buffer));
   173 #endif
   174     case SharedTextureBufferType::TextureID: {
   175         if (!DoesEGLContextSupportSharingWithEGLImage(gl))
   176             return 0;
   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         }
   186         return (SharedTextureHandle)tex;
   187     }
   188     default:
   189         NS_ERROR("Unknown shared texture buffer type");
   190         return 0;
   191     }
   192 }
   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;
   202     if (shareType != SharedTextureShareType::SameProcess) {
   203         NS_ERROR("Implementation not available for this sharing type");
   204         return;
   205     }
   207     SharedTextureHandleWrapper* wrapper = reinterpret_cast<SharedTextureHandleWrapper*>(sharedHandle);
   209     switch (wrapper->Type()) {
   210 #ifdef MOZ_WIDGET_ANDROID
   211     case SharedHandleType_SurfaceTexture:
   212         delete wrapper;
   213         break;
   214 #endif
   216     case SharedHandleType_Image: {
   217         NS_ASSERTION(DoesEGLContextSupportSharingWithEGLImage(gl), "EGLImage not supported or disabled in runtime");
   219         EGLTextureWrapper* wrap = (EGLTextureWrapper*)sharedHandle;
   220         delete wrap;
   221         break;
   222     }
   224     default:
   225         NS_ERROR("Unknown shared handle type");
   226         return;
   227     }
   228 }
   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;
   239     if (shareType != SharedTextureShareType::SameProcess)
   240         return false;
   242     SharedTextureHandleWrapper* wrapper = reinterpret_cast<SharedTextureHandleWrapper*>(sharedHandle);
   244     switch (wrapper->Type()) {
   245 #ifdef MOZ_WIDGET_ANDROID
   246     case SharedHandleType_SurfaceTexture: {
   247         SurfaceTextureWrapper* surfaceWrapper = reinterpret_cast<SurfaceTextureWrapper*>(wrapper);
   249         details.mTarget = LOCAL_GL_TEXTURE_EXTERNAL;
   250         details.mTextureFormat = gfx::SurfaceFormat::R8G8B8A8;
   251         surfaceWrapper->SurfaceTexture()->GetTransformMatrix(details.mTextureTransform);
   252         break;
   253     }
   254 #endif
   256     case SharedHandleType_Image:
   257         details.mTarget = LOCAL_GL_TEXTURE_2D;
   258         details.mTextureFormat = gfx::SurfaceFormat::R8G8B8A8;
   259         break;
   261     default:
   262         NS_ERROR("Unknown shared handle type");
   263         return false;
   264     }
   266     return true;
   267 }
   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;
   277     if (shareType != SharedTextureShareType::SameProcess)
   278         return false;
   280     SharedTextureHandleWrapper* wrapper = reinterpret_cast<SharedTextureHandleWrapper*>(sharedHandle);
   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);
   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
   302     case SharedHandleType_Image: {
   303         NS_ASSERTION(DoesEGLContextSupportSharingWithEGLImage(gl), "EGLImage not supported or disabled in runtime");
   305         EGLTextureWrapper* wrap = (EGLTextureWrapper*)sharedHandle;
   306         wrap->WaitSync();
   307         gl->fEGLImageTargetTexture2D(LOCAL_GL_TEXTURE_2D, wrap->GetEGLImage());
   308         break;
   309     }
   311     default:
   312         NS_ERROR("Unknown shared handle type");
   313         return false;
   314     }
   316     return true;
   317 }
   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 }
   329 }
   330 }

mercurial