Bug 942502 - Move EGLImage/GraphicBuffer-specific helpers out of GLContext - r=bjacob
authorDan Glastonbury <dglastonbury@mozilla.com>
Tue, 10 Dec 2013 15:47:19 +1000
changeset 160358 fd0594ff74ce27db29c4267a14773a3e77cb4059
parent 160357 be500431a9d68541c2ef15fb47b89a68402d46bc
child 160359 01df0b7dcd8ff80ecb3f53899788059081c15d17
push id270
push userpvanderbeken@mozilla.com
push dateThu, 06 Mar 2014 09:24:21 +0000
reviewersbjacob
bugs942502
milestone29.0a1
Bug 942502 - Move EGLImage/GraphicBuffer-specific helpers out of GLContext - r=bjacob Extracted methods CreateEGLImageForNativeBuffer and DestroyEGLImage from GLContext and moved to EGLImageHelpers.[h|cpp]. Renamed to EGLImageCreateFromNativeBuffer and EGLImageDestroy.
gfx/gl/GLContext.h
gfx/gl/GLContextProviderEGL.cpp
gfx/layers/moz.build
gfx/layers/opengl/EGLImageHelpers.cpp
gfx/layers/opengl/EGLImageHelpers.h
gfx/layers/opengl/GrallocTextureHost.cpp
gfx/layers/opengl/TextureHostOGL.cpp
--- a/gfx/gl/GLContext.h
+++ b/gfx/gl/GLContext.h
@@ -2488,21 +2488,16 @@ public:
     void ApplyFilterToBoundTexture(GraphicsFilter aFilter);
 
     /**
      * Applies aFilter to the texture currently bound to aTarget.
      */
     void ApplyFilterToBoundTexture(GLuint aTarget,
                                    GraphicsFilter aFilter);
 
-#ifdef MOZ_WIDGET_GONK
-    virtual EGLImage CreateEGLImageForNativeBuffer(void* buffer) = 0;
-    virtual void DestroyEGLImage(EGLImage image) = 0;
-#endif
-
     // Before reads from offscreen texture
     void GuaranteeResolve();
 
     /*
      * Resize the current offscreen buffer.  Returns true on success.
      * If it returns false, the context should be treated as unusable
      * and should be recreated.  After the resize, the viewport is not
      * changed; glViewport should be called as appropriate.
--- a/gfx/gl/GLContextProviderEGL.cpp
+++ b/gfx/gl/GLContextProviderEGL.cpp
@@ -403,35 +403,16 @@ public:
                                                LOCAL_EGL_BACK_BUFFER);
         if (success == LOCAL_EGL_FALSE)
             return false;
 
         mBound = false;
         return true;
     }
 
-#ifdef MOZ_WIDGET_GONK
-    EGLImage CreateEGLImageForNativeBuffer(void* buffer) MOZ_OVERRIDE
-    {
-        EGLint attrs[] = {
-            LOCAL_EGL_IMAGE_PRESERVED, LOCAL_EGL_TRUE,
-            LOCAL_EGL_NONE, LOCAL_EGL_NONE
-        };
-        return sEGLLibrary.fCreateImage(EGL_DISPLAY(),
-                                        EGL_NO_CONTEXT,
-                                        LOCAL_EGL_NATIVE_BUFFER_ANDROID,
-                                        buffer, attrs);
-    }
-
-    void DestroyEGLImage(EGLImage image) MOZ_OVERRIDE
-    {
-        sEGLLibrary.fDestroyImage(EGL_DISPLAY(), image);
-    }
-#endif
-
     virtual void SetEGLSurfaceOverride(EGLSurface surf) MOZ_OVERRIDE {
         if (Screen()) {
             /* Blit `draw` to `read` if we need to, before we potentially juggle
              * `read` around. If we don't, we might attach a different `read`,
              * and *then* hit AssureBlitted, which will blit a dirty `draw` onto
              * the wrong `read`!
              */
             Screen()->AssureBlitted();
--- a/gfx/layers/moz.build
+++ b/gfx/layers/moz.build
@@ -182,16 +182,17 @@ if CONFIG['MOZ_WIDGET_TOOLKIT'] == 'coco
 # libraries, but only Gonk is able to use it reliably because Gecko
 # has full system permissions there.
 if CONFIG['MOZ_WIDGET_TOOLKIT'] == 'gonk':
     EXPORTS.mozilla.layers += [
         'ipc/ShadowLayerUtilsGralloc.h',
     ]
     UNIFIED_SOURCES += [
         'GrallocImages.cpp',
+        'opengl/EGLImageHelpers.cpp',
         'opengl/GrallocTextureClient.cpp',
         'opengl/GrallocTextureHost.cpp',
     ]
     SOURCES += [
         'ipc/ShadowLayerUtilsGralloc.cpp',
     ]
 
 UNIFIED_SOURCES += [
new file mode 100644
--- /dev/null
+++ b/gfx/layers/opengl/EGLImageHelpers.cpp
@@ -0,0 +1,50 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/* vim: set ts=8 sts=4 et sw=4 tw=80: */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#include "EGLImageHelpers.h"
+#include "GLContext.h"
+#include "GLLibraryEGL.h"
+
+namespace mozilla
+{
+namespace layers {
+
+using namespace gl;
+
+EGLImage
+EGLImageCreateFromNativeBuffer(GLContext* aGL, void* aBuffer)
+{
+    EGLint attrs[] = {
+        LOCAL_EGL_IMAGE_PRESERVED, LOCAL_EGL_TRUE,
+        LOCAL_EGL_NONE, LOCAL_EGL_NONE
+    };
+
+    GLLibraryEGL* egl = aGL->GetLibraryEGL();
+    if (!egl) {
+        NS_WARNING("Failed to obtain pointer to EGL. Returning EGL_NO_IMAGE.");
+        return EGL_NO_IMAGE;
+    }
+
+    return egl->fCreateImage(egl->Display(),
+                             EGL_NO_CONTEXT,
+                             LOCAL_EGL_NATIVE_BUFFER_ANDROID,
+                             aBuffer, attrs);
+}
+
+void
+EGLImageDestroy(GLContext* aGL, EGLImage aImage)
+{
+    GLLibraryEGL* egl = aGL->GetLibraryEGL();
+    if (!egl) {
+        NS_WARNING("Failed to obtain pointer to EGL. Image not destroyed.");
+        return;
+    }
+
+    egl->fDestroyImage(egl->Display(), aImage);
+}
+
+} // namespace layers
+} // namespace mozilla
new file mode 100644
--- /dev/null
+++ b/gfx/layers/opengl/EGLImageHelpers.h
@@ -0,0 +1,25 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/* vim: set ts=8 sts=4 et sw=4 tw=80: */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#ifndef EGLIMAGEHELPERS_H_
+#define EGLIMAGEHELPERS_H_
+
+typedef void* EGLImage;
+
+namespace mozilla {
+namespace gl {
+    class GLContext;
+}
+
+namespace layers {
+
+EGLImage EGLImageCreateFromNativeBuffer(gl::GLContext* aGL, void* aBuffer);
+void EGLImageDestroy(gl::GLContext* aGL, EGLImage aImage);
+
+} // namespace layers
+} // namespace mozilla
+
+#endif // EGLIMAGEHELPERS_H_
--- a/gfx/layers/opengl/GrallocTextureHost.cpp
+++ b/gfx/layers/opengl/GrallocTextureHost.cpp
@@ -5,16 +5,17 @@
 
 #include "GLContext.h"
 #include "gfxImageSurface.h"
 #include "gfx2DGlue.h"
 #include <ui/GraphicBuffer.h>
 #include "GrallocImages.h"  // for GrallocImage
 #include "mozilla/layers/GrallocTextureHost.h"
 #include "mozilla/layers/CompositorOGL.h"
+#include "EGLImageHelpers.h"
 #include "GLContextUtils.h"
 
 namespace mozilla {
 namespace layers {
 
 using namespace android;
 
 static gfx::SurfaceFormat
@@ -108,17 +109,17 @@ GrallocTextureSourceOGL::~GrallocTexture
 
 void GrallocTextureSourceOGL::BindTexture(GLenum aTextureUnit)
 {
   /*
    * The job of this function is to ensure that the texture is tied to the
    * android::GraphicBuffer, so that texturing will source the GraphicBuffer.
    *
    * To this effect we create an EGLImage wrapping this GraphicBuffer,
-   * using CreateEGLImageForNativeBuffer, and then we tie this EGLImage to our
+   * using EGLImageCreateFromNativeBuffer, and then we tie this EGLImage to our
    * texture using fEGLImageTargetTexture2D.
    */
   MOZ_ASSERT(gl());
   gl()->MakeCurrent();
 
   GLuint tex = GetGLTexture();
   GLuint textureTarget = GetTextureTarget();
 
@@ -189,17 +190,17 @@ GrallocTextureSourceOGL::SetCompositable
 
   gl()->MakeCurrent();
   GLuint tex = GetGLTexture();
   GLuint textureTarget = GetTextureTarget();
 
   gl()->fActiveTexture(LOCAL_GL_TEXTURE0);
   gl()->fBindTexture(textureTarget, tex);
   // create new EGLImage
-  mEGLImage = gl()->CreateEGLImageForNativeBuffer(mGraphicBuffer->getNativeBuffer());
+  mEGLImage = EGLImageCreateFromNativeBuffer(gl(), mGraphicBuffer->getNativeBuffer());
   gl()->fEGLImageTargetTexture2D(textureTarget, mEGLImage);
   mNeedsReset = false;
 }
 
 gfx::IntSize
 GrallocTextureSourceOGL::GetSize() const
 {
   if (!IsValid()) {
@@ -210,18 +211,18 @@ GrallocTextureSourceOGL::GetSize() const
 }
 
 void
 GrallocTextureSourceOGL::DeallocateDeviceData()
 {
   if (mEGLImage) {
     MOZ_ASSERT(gl());
     gl()->MakeCurrent();
-    gl()->DestroyEGLImage(mEGLImage);
-    mEGLImage = 0;
+    EGLImageDestroy(gl(), mEGLImage);
+    mEGLImage = EGL_NO_IMAGE;
   }
 }
 
 GrallocTextureHostOGL::GrallocTextureHostOGL(TextureFlags aFlags,
                                              const NewSurfaceDescriptorGralloc& aDescriptor)
   : TextureHost(aFlags)
 {
   mGrallocActor =
@@ -325,17 +326,17 @@ TemporaryRef<gfx::DataSourceSurface>
 GrallocTextureSourceOGL::GetAsSurface() {
   MOZ_ASSERT(gl());
   gl()->MakeCurrent();
 
   GLuint tex = GetGLTexture();
   gl()->fActiveTexture(LOCAL_GL_TEXTURE0);
   gl()->fBindTexture(GetTextureTarget(), tex);
   if (!mEGLImage) {
-    mEGLImage = gl()->CreateEGLImageForNativeBuffer(mGraphicBuffer->getNativeBuffer());
+    mEGLImage = EGLImageCreateFromNativeBuffer(gl(), mGraphicBuffer->getNativeBuffer());
   }
   gl()->fEGLImageTargetTexture2D(GetTextureTarget(), mEGLImage);
 
   RefPtr<gfx::DataSourceSurface> surf =
     IsValid() ? ReadBackSurface(gl(), tex, false, GetFormat())
               : nullptr;
 
   gl()->fActiveTexture(LOCAL_GL_TEXTURE0);
--- a/gfx/layers/opengl/TextureHostOGL.cpp
+++ b/gfx/layers/opengl/TextureHostOGL.cpp
@@ -19,16 +19,17 @@
 #include "gfxPoint.h"                   // for gfxIntSize
 #include "gfxReusableSurfaceWrapper.h"  // for gfxReusableSurfaceWrapper
 #include "ipc/AutoOpenSurface.h"        // for AutoOpenSurface
 #include "mozilla/gfx/2D.h"             // for DataSourceSurface
 #include "mozilla/gfx/BaseSize.h"       // for BaseSize
 #include "mozilla/layers/CompositorOGL.h"  // for CompositorOGL
 #ifdef MOZ_WIDGET_GONK
 # include "GrallocImages.h"  // for GrallocImage
+# include "EGLImageHelpers.h"
 #endif
 #include "mozilla/layers/ISurfaceAllocator.h"
 #include "mozilla/layers/YCbCrImageDataSerializer.h"
 #include "mozilla/layers/GrallocTextureHost.h"
 #include "nsPoint.h"                    // for nsIntPoint
 #include "nsRegion.h"                   // for nsIntRegion
 #include "GfxTexturesReporter.h"        // for GfxTexturesReporter
 #include "GLBlitTextureImageHelper.h"
@@ -39,17 +40,17 @@
 #include "GeckoProfiler.h"
 
 using namespace mozilla::gl;
 using namespace mozilla::gfx;
 
 namespace mozilla {
 namespace layers {
 
-class Compositor; 
+class Compositor;
 
 TemporaryRef<CompositableBackendSpecificData>
 CreateCompositableBackendSpecificDataOGL()
 {
 #ifdef MOZ_WIDGET_GONK
   return new CompositableDataGonkOGL();
 #else
   return nullptr;
@@ -1149,19 +1150,19 @@ GrallocDeprecatedTextureHostOGL::GetForm
   }
 }
 
 void
 GrallocDeprecatedTextureHostOGL::DeleteTextures()
 {
   if (mEGLImage) {
     if (gl()->MakeCurrent()) {
-      gl()->DestroyEGLImage(mEGLImage);
+      EGLImageDestroy(gl(), mEGLImage);
     }
-    mEGLImage = 0;
+    mEGLImage = EGL_NO_IMAGE;
   }
 }
 
 // only used for hacky fix in gecko 23 for bug 862324
 static void
 AddDeprecatedTextureHostToGrallocBufferActor(DeprecatedTextureHost* aDeprecatedTextureHost, const SurfaceDescriptor* aSurfaceDescriptor)
 {
   if (aSurfaceDescriptor && IsSurfaceDescriptorValid(*aSurfaceDescriptor)) {
@@ -1207,17 +1208,17 @@ GrallocDeprecatedTextureHostOGL::SwapTex
   if (!gl()->MakeCurrent()) {
     return;
   }
   gl()->fActiveTexture(LOCAL_GL_TEXTURE0);
   gl()->fBindTexture(mTextureTarget, tex);
   // create new EGLImage
   // create EGLImage during buffer swap could reduce the graphic driver's task
   // during rendering.
-  mEGLImage = gl()->CreateEGLImageForNativeBuffer(mGraphicBuffer->getNativeBuffer());
+  mEGLImage = EGLImageCreateFromNativeBuffer(gl(), mGraphicBuffer->getNativeBuffer());
   gl()->fEGLImageTargetTexture2D(mTextureTarget, mEGLImage);
 
 }
 
 gl::GLContext*
 GrallocDeprecatedTextureHostOGL::gl() const
 {
   return mCompositor ? mCompositor->gl() : nullptr;
@@ -1380,17 +1381,17 @@ TemporaryRef<gfx::DataSourceSurface>
 GrallocDeprecatedTextureHostOGL::GetAsSurface() {
   if (!gl()->MakeCurrent()) {
     return nullptr;
   }
   GLuint tex = GetGLTexture();
   gl()->fActiveTexture(LOCAL_GL_TEXTURE0);
   gl()->fBindTexture(mTextureTarget, tex);
   if (!mEGLImage) {
-    mEGLImage = gl()->CreateEGLImageForNativeBuffer(mGraphicBuffer->getNativeBuffer());
+    mEGLImage = EGLImageCreateFromNativeBuffer(gl(), mGraphicBuffer->getNativeBuffer());
   }
   gl()->fEGLImageTargetTexture2D(mTextureTarget, mEGLImage);
 
   RefPtr<gfx::DataSourceSurface> surf =
     IsValid() ? ReadBackSurface(gl(), tex, false, GetFormat())
               : nullptr;
   return surf.forget();
 }