Bug 1037147 - Remove SharedTextureHandle and friends r=mattwoodrow,snorp a=lmandel
authorJeff Gilbert <jgilbert@mozilla.com>
Thu, 06 Nov 2014 09:34:18 -0600
changeset 225966 99c1af40ea1b
parent 225965 57e502f33317
child 225967 03bb1ca133ee
push id4089
push userjwillcox@mozilla.com
push date2014-11-06 15:37 +0000
treeherdermozilla-beta@53692e16c248 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmattwoodrow, snorp, lmandel
bugs1037147
milestone34.0
Bug 1037147 - Remove SharedTextureHandle and friends r=mattwoodrow,snorp a=lmandel --HG-- extra : rebase_source : 13101216d6e5eccfb0a19c14b783b3878644b665
dom/plugins/base/nsNPAPIPluginInstance.cpp
dom/plugins/base/nsNPAPIPluginInstance.h
dom/plugins/base/nsPluginInstanceOwner.cpp
dom/plugins/ipc/PluginInstanceParent.cpp
gfx/gl/EGLUtils.cpp
gfx/gl/EGLUtils.h
gfx/gl/GLContextTypes.h
gfx/gl/GLSharedHandleHelpers.cpp
gfx/gl/GLSharedHandleHelpers.h
gfx/gl/TextureImageEGL.cpp
gfx/gl/moz.build
gfx/layers/Compositor.h
gfx/layers/GLImages.h
gfx/layers/ImageContainer.cpp
gfx/layers/ImageTypes.h
gfx/layers/SharedTextureImage.h
gfx/layers/client/ImageClient.cpp
gfx/layers/composite/TextureHost.cpp
gfx/layers/ipc/LayersSurfaces.ipdlh
gfx/layers/ipc/ShadowLayerUtils.h
gfx/layers/moz.build
gfx/layers/opengl/MacIOSurfaceTextureHostOGL.h
gfx/layers/opengl/TextureClientOGL.cpp
gfx/layers/opengl/TextureClientOGL.h
gfx/layers/opengl/TextureHostOGL.cpp
gfx/layers/opengl/TextureHostOGL.h
--- a/dom/plugins/base/nsNPAPIPluginInstance.cpp
+++ b/dom/plugins/base/nsNPAPIPluginInstance.cpp
@@ -51,19 +51,20 @@ using namespace mozilla::dom;
 #include "mozilla/Mutex.h"
 #include "mozilla/CondVar.h"
 #include "AndroidBridge.h"
 #include "mozilla/dom/ScreenOrientation.h"
 #include "mozilla/Hal.h"
 #include "GLContextProvider.h"
 #include "GLContext.h"
 #include "TexturePoolOGL.h"
-#include "GLSharedHandleHelpers.h"
 #include "SurfaceTypes.h"
+#include "EGLUtils.h"
 
+using namespace mozilla;
 using namespace mozilla::gl;
 
 typedef nsNPAPIPluginInstance::VideoInfo VideoInfo;
 
 class PluginEventRunnable : public nsRunnable
 {
 public:
   PluginEventRunnable(nsNPAPIPluginInstance* instance, ANPEvent* event)
@@ -122,38 +123,35 @@ public:
   }
 
   void Release(nsNPAPIPluginInstance::TextureInfo& aTextureInfo)
   {
     mTextureInfo = aTextureInfo;
     mLock.Unlock();
   }
 
-  SharedTextureHandle CreateSharedHandle()
+  EGLImage CreateEGLImage()
   {
     MutexAutoLock lock(mLock);
 
     if (!EnsureGLContext())
       return 0;
 
     if (mTextureInfo.mWidth == 0 || mTextureInfo.mHeight == 0)
       return 0;
 
-    SharedTextureHandle handle =
-      gl::CreateSharedHandle(sPluginContext,
-                             gl::SharedTextureShareType::SameProcess,
-                             (void*)mTextureInfo.mTexture,
-                             gl::SharedTextureBufferType::TextureID);
+    GLuint& tex = mTextureInfo.mTexture;
+    EGLImage image = gl::CreateEGLImage(sPluginContext, tex);
 
     // We want forget about this now, so delete the texture. Assigning it to zero
     // ensures that we create a new one in Lock()
-    sPluginContext->fDeleteTextures(1, &mTextureInfo.mTexture);
-    mTextureInfo.mTexture = 0;
+    sPluginContext->fDeleteTextures(1, &tex);
+    tex = 0;
 
-    return handle;
+    return image;
   }
 
 private:
   // Private destructor, to discourage deletion outside of Release():
   ~SharedPluginTexture()
   {
   }
 
@@ -980,27 +978,32 @@ void* nsNPAPIPluginInstance::AcquireCont
 
     if (!mContentSurface)
       return nullptr;
   }
 
   return mContentSurface->GetNativeWindow();
 }
 
-SharedTextureHandle nsNPAPIPluginInstance::CreateSharedHandle()
+EGLImage
+nsNPAPIPluginInstance::AsEGLImage()
 {
-  if (mContentTexture) {
-    return mContentTexture->CreateSharedHandle();
-  } else if (mContentSurface) {
-    EnsureGLContext();
-    return gl::CreateSharedHandle(sPluginContext,
-                                  gl::SharedTextureShareType::SameProcess,
-                                  mContentSurface,
-                                  gl::SharedTextureBufferType::SurfaceTexture);
-  } else return 0;
+  if (!mContentTexture)
+    return 0;
+
+  return mContentTexture->CreateEGLImage();
+}
+
+nsSurfaceTexture*
+nsNPAPIPluginInstance::AsSurfaceTexture()
+{
+  if (!mContentSurface)
+    return nullptr;
+
+  return mContentSurface;
 }
 
 void* nsNPAPIPluginInstance::AcquireVideoWindow()
 {
   nsSurfaceTexture* surface = CreateSurfaceTexture();
   if (!surface)
     return nullptr;
 
--- a/dom/plugins/base/nsNPAPIPluginInstance.h
+++ b/dom/plugins/base/nsNPAPIPluginInstance.h
@@ -186,17 +186,18 @@ public:
   };
 
   TextureInfo LockContentTexture();
   void ReleaseContentTexture(TextureInfo& aTextureInfo);
 
   // For ANPNativeWindow
   void* AcquireContentWindow();
 
-  mozilla::gl::SharedTextureHandle CreateSharedHandle();
+  EGLImage AsEGLImage();
+  nsSurfaceTexture* AsSurfaceTexture();
 
   // For ANPVideo
   class VideoInfo {
   public:
     VideoInfo(nsSurfaceTexture* aSurfaceTexture) :
       mSurfaceTexture(aSurfaceTexture)
     {
     }
--- a/dom/plugins/base/nsPluginInstanceOwner.cpp
+++ b/dom/plugins/base/nsPluginInstanceOwner.cpp
@@ -17,17 +17,17 @@ using mozilla::DefaultXDisplay;
 
 #include "gfxUtils.h"
 #include "nsIRunnable.h"
 #include "nsContentUtils.h"
 #include "nsRect.h"
 #include "nsSize.h"
 #include "nsDisplayList.h"
 #include "ImageLayers.h"
-#include "SharedTextureImage.h"
+#include "GLImages.h"
 #include "nsObjectFrame.h"
 #include "nsIPluginDocument.h"
 #include "nsIStringStream.h"
 #include "nsNetUtil.h"
 #include "mozilla/Preferences.h"
 #include "nsILinkHandler.h"
 #include "nsIDocShellTreeItem.h"
 #include "nsIWebBrowserChrome.h"
@@ -44,17 +44,17 @@ using mozilla::DefaultXDisplay;
 #include "nsIFocusManager.h"
 #include "nsFocusManager.h"
 #include "nsIDOMDragEvent.h"
 #include "nsIScrollableFrame.h"
 #include "nsIDocShell.h"
 #include "ImageContainer.h"
 #include "nsIDOMHTMLCollection.h"
 #include "GLContext.h"
-#include "GLSharedHandleHelpers.h"
+#include "EGLUtils.h"
 #include "nsIContentInlines.h"
 #include "mozilla/MiscEvents.h"
 #include "mozilla/MouseEvents.h"
 #include "mozilla/TextEvents.h"
 #include "mozilla/dom/HTMLObjectElementBinding.h"
 #include "nsFrameSelection.h"
 
 #include "nsContentCID.h"
@@ -143,16 +143,72 @@ nsPluginInstanceOwner::NotifyPaintWaiter
   if (!mWaitingForPaint && !IsUpToDate() && aBuilder->ShouldSyncDecodeImages()) {
     nsCOMPtr<nsIRunnable> event = new AsyncPaintWaitEvent(mContent, false);
     // Run this event as soon as it's safe to do so, since listeners need to
     // receive it immediately
     mWaitingForPaint = nsContentUtils::AddScriptRunner(event);
   }
 }
 
+#if MOZ_WIDGET_ANDROID
+static void
+AttachToContainerAsEGLImage(ImageContainer* container,
+                            nsNPAPIPluginInstance* instance,
+                            const LayoutDeviceRect& rect,
+                            nsRefPtr<Image>* out_image)
+{
+  MOZ_ASSERT(out_image);
+  MOZ_ASSERT(!*out_image);
+
+  EGLImage image = instance->AsEGLImage();
+  if (!image) {
+    return;
+  }
+
+  nsRefPtr<Image> img = container->CreateImage(ImageFormat::EGLIMAGE);
+
+  EGLImageImage::Data data;
+  data.mImage = image;
+  data.mSize = gfx::IntSize(rect.width, rect.height);
+  data.mInverted = instance->Inverted();
+
+  EGLImageImage* typedImg = static_cast<EGLImageImage*>(img.get());
+  typedImg->SetData(data);
+
+  *out_image = img;
+}
+
+static void
+AttachToContainerAsSurfaceTexture(ImageContainer* container,
+                                  nsNPAPIPluginInstance* instance,
+                                  const LayoutDeviceRect& rect,
+                                  nsRefPtr<Image>* out_image)
+{
+  MOZ_ASSERT(out_image);
+  MOZ_ASSERT(!*out_image);
+
+  nsSurfaceTexture* surfTex = instance->AsSurfaceTexture();
+  if (!surfTex) {
+    return;
+  }
+
+  nsRefPtr<Image> img = container->CreateImage(ImageFormat::SURFACE_TEXTURE);
+
+  SurfaceTextureImage::Data data;
+  data.mSurfTex = surfTex;
+  data.mSize = gfx::IntSize(rect.width, rect.height);
+  data.mInverted = instance->Inverted();
+
+  SurfaceTextureImage* typedImg = static_cast<SurfaceTextureImage*>(img.get());
+  typedImg->SetData(data);
+
+  *out_image = img;
+}
+#endif
+
 already_AddRefed<ImageContainer>
 nsPluginInstanceOwner::GetImageContainer()
 {
   if (!mInstance)
     return nullptr;
 
   nsRefPtr<ImageContainer> container;
 
@@ -167,33 +223,29 @@ nsPluginInstanceOwner::GetImageContainer
   // NotifySize() causes Flash to do a bunch of stuff like ask for surfaces to render
   // into, set y-flip flags, etc, so we do this at the beginning.
   gfxSize resolution = mObjectFrame->PresContext()->PresShell()->GetCumulativeResolution();
   ScreenSize screenSize = (r * LayoutDeviceToScreenScale(resolution.width, resolution.height)).Size();
   mInstance->NotifySize(nsIntSize(screenSize.width, screenSize.height));
 
   container = LayerManager::CreateImageContainer();
 
-  nsRefPtr<Image> img = container->CreateImage(ImageFormat::SHARED_TEXTURE);
-
-  SharedTextureImage::Data data;
-  data.mSize = gfx::IntSize(r.width, r.height);
-  data.mHandle = mInstance->CreateSharedHandle();
-  data.mShareType = mozilla::gl::SharedTextureShareType::SameProcess;
-  data.mInverted = mInstance->Inverted();
-
-  SharedTextureImage* pluginImage = static_cast<SharedTextureImage*>(img.get());
-  pluginImage->SetData(data);
+  // Try to get it as an EGLImage first.
+  nsRefPtr<Image> img;
+  AttachToContainerAsEGLImage(container, mInstance, r, &img);
+  if (!img) {
+    AttachToContainerAsSurfaceTexture(container, mInstance, r, &img);
+  }
+  MOZ_ASSERT(img);
 
   container->SetCurrentImageInTransaction(img);
-
-  return container.forget();
+#else
+  mInstance->GetImageContainer(getter_AddRefs(container));
 #endif
 
-  mInstance->GetImageContainer(getter_AddRefs(container));
   return container.forget();
 }
 
 void
 nsPluginInstanceOwner::SetBackgroundUnknown()
 {
   if (mInstance) {
     mInstance->SetBackgroundUnknown();
@@ -1108,45 +1160,44 @@ void nsPluginInstanceOwner::RemovePlugin
   mozilla::widget::android::GeckoAppShell::RemovePluginView((jobject)mJavaView, mFullScreen);
   AndroidBridge::GetJNIEnv()->DeleteGlobalRef((jobject)mJavaView);
   mJavaView = nullptr;
 
   if (mFullScreen)
     sFullScreenInstance = nullptr;
 }
 
-void nsPluginInstanceOwner::GetVideos(nsTArray<nsNPAPIPluginInstance::VideoInfo*>& aVideos)
+void
+nsPluginInstanceOwner::GetVideos(nsTArray<nsNPAPIPluginInstance::VideoInfo*>& aVideos)
 {
   if (!mInstance)
     return;
 
   mInstance->GetVideos(aVideos);
 }
 
-already_AddRefed<ImageContainer> nsPluginInstanceOwner::GetImageContainerForVideo(nsNPAPIPluginInstance::VideoInfo* aVideoInfo)
+already_AddRefed<ImageContainer>
+nsPluginInstanceOwner::GetImageContainerForVideo(nsNPAPIPluginInstance::VideoInfo* aVideoInfo)
 {
   nsRefPtr<ImageContainer> container = LayerManager::CreateImageContainer();
 
-  nsRefPtr<Image> img = container->CreateImage(ImageFormat::SHARED_TEXTURE);
-
-  SharedTextureImage::Data data;
-
-  data.mShareType = gl::SharedTextureShareType::SameProcess;
-  data.mHandle = gl::CreateSharedHandle(mInstance->GLContext(),
-                                        data.mShareType,
-                                        aVideoInfo->mSurfaceTexture,
-                                        gl::SharedTextureBufferType::SurfaceTexture);
+  nsRefPtr<Image> img = container->CreateImage(ImageFormat::SURFACE_TEXTURE);
+
+  SurfaceTextureImage::Data data;
+
+  data.mSurfTex = aVideoInfo->mSurfaceTexture;
 
   // The logic below for Honeycomb is just a guess, but seems to work. We don't have a separate
   // inverted flag for video.
   data.mInverted = AndroidBridge::Bridge()->IsHoneycomb() ? true : mInstance->Inverted();
   data.mSize = gfx::IntSize(aVideoInfo->mDimensions.width, aVideoInfo->mDimensions.height);
 
-  SharedTextureImage* pluginImage = static_cast<SharedTextureImage*>(img.get());
-  pluginImage->SetData(data);
+  SurfaceTextureImage* typedImg = static_cast<SurfaceTextureImage*>(img.get());
+  typedImg->SetData(data);
+
   container->SetCurrentImageInTransaction(img);
 
   return container.forget();
 }
 
 void nsPluginInstanceOwner::Invalidate() {
   NPRect rect;
   rect.left = rect.top = 0;
--- a/dom/plugins/ipc/PluginInstanceParent.cpp
+++ b/dom/plugins/ipc/PluginInstanceParent.cpp
@@ -23,17 +23,17 @@
 #ifdef MOZ_X11
 #include "gfxXlibSurface.h"
 #endif
 #include "gfxContext.h"
 #include "gfxColor.h"
 #include "gfxUtils.h"
 #include "mozilla/gfx/2D.h"
 #include "Layers.h"
-#include "SharedTextureImage.h"
+#include "ImageContainer.h"
 #include "GLContext.h"
 #include "GLContextProvider.h"
 
 #ifdef XP_MACOSX
 #include "MacIOSurfaceImage.h"
 #endif
 
 #if defined(OS_WIN)
new file mode 100644
--- /dev/null
+++ b/gfx/gl/EGLUtils.cpp
@@ -0,0 +1,114 @@
+/* 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 "EGLUtils.h"
+
+#include "GLContextEGL.h"
+
+namespace mozilla {
+namespace gl {
+
+bool
+DoesEGLContextSupportSharingWithEGLImage(GLContext* gl)
+{
+    return sEGLLibrary.HasKHRImageBase() &&
+           sEGLLibrary.HasKHRImageTexture2D() &&
+           gl->IsExtensionSupported(GLContext::OES_EGL_image);
+}
+
+EGLImage
+CreateEGLImage(GLContext* gl, GLuint tex)
+{
+    MOZ_ASSERT(DoesEGLContextSupportSharingWithEGLImage(gl));
+
+    EGLClientBuffer clientBuffer = (EGLClientBuffer)((uint64_t)tex);
+    EGLContext eglContext = GLContextEGL::Cast(gl)->GetEGLContext();
+    EGLImage image = sEGLLibrary.fCreateImage(EGL_DISPLAY(),
+                                              eglContext,
+                                              LOCAL_EGL_GL_TEXTURE_2D,
+                                              clientBuffer,
+                                              nullptr);
+    return image;
+}
+
+////////////////////////////////////////////////////////////////////////
+// EGLImageWrapper
+
+/*static*/ EGLImageWrapper*
+EGLImageWrapper::Create(GLContext* gl, GLuint tex)
+{
+    MOZ_ASSERT(DoesEGLContextSupportSharingWithEGLImage(gl));
+
+    GLLibraryEGL& library = sEGLLibrary;
+    EGLDisplay display = EGL_DISPLAY();
+    EGLContext eglContext = GLContextEGL::Cast(gl)->GetEGLContext();
+    EGLClientBuffer clientBuffer = (EGLClientBuffer)((uint64_t)tex);
+    EGLImage image = library.fCreateImage(display,
+                                          eglContext,
+                                          LOCAL_EGL_GL_TEXTURE_2D,
+                                          clientBuffer,
+                                          nullptr);
+    if (!image) {
+#ifdef DEBUG
+        printf_stderr("Could not create EGL images: ERROR (0x%04x)\n",
+                      sEGLLibrary.fGetError());
+#endif
+        return nullptr;
+    }
+
+    return new EGLImageWrapper(library, display, image);
+}
+
+EGLImageWrapper::~EGLImageWrapper()
+{
+    mLibrary.fDestroyImage(mDisplay, mImage);
+}
+
+bool
+EGLImageWrapper::FenceSync(GLContext* gl)
+{
+    MOZ_ASSERT(!mSync);
+
+    if (mLibrary.IsExtensionSupported(GLLibraryEGL::KHR_fence_sync)) {
+        mSync = mLibrary.fCreateSync(mDisplay,
+                                     LOCAL_EGL_SYNC_FENCE,
+                                     nullptr);
+        // We need to flush to make sure the sync object enters the command stream;
+        // we can't use EGL_SYNC_FLUSH_COMMANDS_BIT at wait time, because the wait
+        // happens on a different thread/context.
+        gl->fFlush();
+    }
+
+    if (!mSync) {
+        // we failed to create one, so just do a finish
+        gl->fFinish();
+    }
+
+    return true;
+}
+
+bool
+EGLImageWrapper::ClientWaitSync()
+{
+    if (!mSync) {
+        // if we have no sync object, then we did a Finish() earlier
+        return true;
+    }
+
+    // wait at most 1 second; this should really be never/rarely hit
+    const uint64_t ns_per_ms = 1000 * 1000;
+    EGLTime timeout = 1000 * ns_per_ms;
+
+    EGLint result = mLibrary.fClientWaitSync(mDisplay,
+                                             mSync,
+                                             0,
+                                             timeout);
+    mLibrary.fDestroySync(mDisplay, mSync);
+    mSync = nullptr;
+
+    return result == LOCAL_EGL_CONDITION_SATISFIED;
+}
+
+} // namespace gl
+} // namespace mozilla
new file mode 100644
--- /dev/null
+++ b/gfx/gl/EGLUtils.h
@@ -0,0 +1,60 @@
+/* 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 EGLUTILS_H_
+#define EGLUTILS_H_
+
+#include "GLContextTypes.h"
+#include "GLTypes.h"
+#include "mozilla/Assertions.h"
+
+namespace mozilla {
+namespace gl {
+
+class GLLibraryEGL;
+
+bool DoesEGLContextSupportSharingWithEGLImage(GLContext* gl);
+EGLImage CreateEGLImage(GLContext* gl, GLuint tex);
+
+////////////////////////////////////////////////////////////////////////
+// EGLImageWrapper
+
+class EGLImageWrapper
+{
+public:
+    static EGLImageWrapper* Create(GLContext* gl, GLuint tex);
+
+private:
+    GLLibraryEGL& mLibrary;
+    const EGLDisplay mDisplay;
+public:
+    const EGLImage mImage;
+private:
+    EGLSync mSync;
+
+    EGLImageWrapper(GLLibraryEGL& library,
+                    EGLDisplay display,
+                    EGLImage image)
+        : mLibrary(library)
+        , mDisplay(display)
+        , mImage(image)
+        , mSync(0)
+    {
+        MOZ_ASSERT(mImage);
+    }
+
+public:
+    ~EGLImageWrapper();
+
+    // Insert a sync point on the given context, which should be the current active
+    // context.
+    bool FenceSync(GLContext* gl);
+
+    bool ClientWaitSync();
+};
+
+} // namespace gl
+} // namespace mozilla
+
+#endif
--- a/gfx/gl/GLContextTypes.h
+++ b/gfx/gl/GLContextTypes.h
@@ -9,29 +9,16 @@
 #include "GLTypes.h"
 #include "mozilla/TypedEnum.h"
 
 namespace mozilla {
 namespace gl {
 
 class GLContext;
 
-typedef uintptr_t SharedTextureHandle;
-
-MOZ_BEGIN_ENUM_CLASS(SharedTextureShareType)
-    SameProcess = 0,
-    CrossProcess
-MOZ_END_ENUM_CLASS(SharedTextureShareType)
-
-MOZ_BEGIN_ENUM_CLASS(SharedTextureBufferType)
-    TextureID,
-    SurfaceTexture,
-    IOSurface
-MOZ_END_ENUM_CLASS(SharedTextureBufferType)
-
 MOZ_BEGIN_ENUM_CLASS(GLContextType)
     Unknown,
     WGL,
     CGL,
     GLX,
     EGL
 MOZ_END_ENUM_CLASS(GLContextType)
 
deleted file mode 100644
--- a/gfx/gl/GLSharedHandleHelpers.cpp
+++ /dev/null
@@ -1,330 +0,0 @@
-/* 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 "GLContextEGL.h"
-#include "GLSharedHandleHelpers.h"
-#ifdef MOZ_WIDGET_ANDROID
-#include "nsSurfaceTexture.h"
-#endif
-
-namespace mozilla {
-namespace gl {
-
-enum SharedHandleType {
-    SharedHandleType_Image
-#ifdef MOZ_WIDGET_ANDROID
-    , SharedHandleType_SurfaceTexture
-#endif
-};
-
-class SharedTextureHandleWrapper
-{
-public:
-    explicit SharedTextureHandleWrapper(SharedHandleType aHandleType) : mHandleType(aHandleType)
-    {
-    }
-
-    virtual ~SharedTextureHandleWrapper()
-    {
-    }
-
-    SharedHandleType Type() { return mHandleType; }
-
-    SharedHandleType mHandleType;
-};
-
-#ifdef MOZ_WIDGET_ANDROID
-
-class SurfaceTextureWrapper: public SharedTextureHandleWrapper
-{
-public:
-    SurfaceTextureWrapper(nsSurfaceTexture* aSurfaceTexture) :
-        SharedTextureHandleWrapper(SharedHandleType_SurfaceTexture)
-        , mSurfaceTexture(aSurfaceTexture)
-    {
-    }
-
-    virtual ~SurfaceTextureWrapper() {
-        mSurfaceTexture = nullptr;
-    }
-
-    nsSurfaceTexture* SurfaceTexture() { return mSurfaceTexture; }
-
-    nsRefPtr<nsSurfaceTexture> mSurfaceTexture;
-};
-
-#endif // MOZ_WIDGET_ANDROID
-
-class EGLTextureWrapper : public SharedTextureHandleWrapper
-{
-public:
-    EGLTextureWrapper() :
-        SharedTextureHandleWrapper(SharedHandleType_Image)
-        , mEGLImage(nullptr)
-        , mSyncObject(nullptr)
-    {
-    }
-
-    // Args are the active GL context, and a texture in that GL
-    // context for which to create an EGLImage.  After the EGLImage
-    // is created, the texture is unused by EGLTextureWrapper.
-    bool CreateEGLImage(GLContext *ctx, uintptr_t texture) {
-        MOZ_ASSERT(!mEGLImage && texture && sEGLLibrary.HasKHRImageBase());
-        static const EGLint eglAttributes[] = {
-            LOCAL_EGL_NONE
-        };
-        EGLContext eglContext = GLContextEGL::Cast(ctx)->GetEGLContext();
-        mEGLImage = sEGLLibrary.fCreateImage(EGL_DISPLAY(), eglContext, LOCAL_EGL_GL_TEXTURE_2D,
-                                             (EGLClientBuffer)texture, eglAttributes);
-        if (!mEGLImage) {
-#ifdef DEBUG
-            printf_stderr("Could not create EGL images: ERROR (0x%04x)\n", sEGLLibrary.fGetError());
-#endif
-            return false;
-        }
-        return true;
-    }
-
-    virtual ~EGLTextureWrapper() {
-        if (mEGLImage) {
-            sEGLLibrary.fDestroyImage(EGL_DISPLAY(), mEGLImage);
-            mEGLImage = nullptr;
-        }
-    }
-
-    const EGLImage GetEGLImage() {
-        return mEGLImage;
-    }
-
-    // Insert a sync point on the given context, which should be the current active
-    // context.
-    bool MakeSync(GLContext *ctx) {
-        MOZ_ASSERT(mSyncObject == nullptr);
-
-        if (sEGLLibrary.IsExtensionSupported(GLLibraryEGL::KHR_fence_sync)) {
-            mSyncObject = sEGLLibrary.fCreateSync(EGL_DISPLAY(), LOCAL_EGL_SYNC_FENCE, nullptr);
-            // We need to flush to make sure the sync object enters the command stream;
-            // we can't use EGL_SYNC_FLUSH_COMMANDS_BIT at wait time, because the wait
-            // happens on a different thread/context.
-            ctx->fFlush();
-        }
-
-        if (mSyncObject == EGL_NO_SYNC) {
-            // we failed to create one, so just do a finish
-            ctx->fFinish();
-        }
-
-        return true;
-    }
-
-    bool WaitSync() {
-        if (!mSyncObject) {
-            // if we have no sync object, then we did a Finish() earlier
-            return true;
-        }
-
-        // wait at most 1 second; this should really be never/rarely hit
-        const uint64_t ns_per_ms = 1000 * 1000;
-        EGLTime timeout = 1000 * ns_per_ms;
-
-        EGLint result = sEGLLibrary.fClientWaitSync(EGL_DISPLAY(), mSyncObject, 0, timeout);
-        sEGLLibrary.fDestroySync(EGL_DISPLAY(), mSyncObject);
-        mSyncObject = nullptr;
-
-        return result == LOCAL_EGL_CONDITION_SATISFIED;
-    }
-
-private:
-    EGLImage mEGLImage;
-    EGLSync mSyncObject;
-};
-
-static bool DoesEGLContextSupportSharingWithEGLImage(GLContext *gl)
-{
-    return sEGLLibrary.HasKHRImageBase() &&
-           sEGLLibrary.HasKHRImageTexture2D() &&
-           gl->IsExtensionSupported(GLContext::OES_EGL_image);
-}
-
-SharedTextureHandle CreateSharedHandle(GLContext* gl,
-                                       SharedTextureShareType shareType,
-                                       void* buffer,
-                                       SharedTextureBufferType bufferType)
-{
-    // unimplemented outside of EGL
-    if (gl->GetContextType() != GLContextType::EGL)
-        return 0;
-
-    // Both EGLImage and SurfaceTexture only support same-process currently, but
-    // it's possible to make SurfaceTexture work across processes. We should do that.
-    if (shareType != SharedTextureShareType::SameProcess)
-        return 0;
-
-    switch (bufferType) {
-#ifdef MOZ_WIDGET_ANDROID
-    case SharedTextureBufferType::SurfaceTexture:
-        if (!gl->IsExtensionSupported(GLContext::OES_EGL_image_external)) {
-            NS_WARNING("Missing GL_OES_EGL_image_external");
-            return 0;
-        }
-
-        return (SharedTextureHandle) new SurfaceTextureWrapper(reinterpret_cast<nsSurfaceTexture*>(buffer));
-#endif
-    case SharedTextureBufferType::TextureID: {
-        if (!DoesEGLContextSupportSharingWithEGLImage(gl))
-            return 0;
-
-        GLuint texture = (uintptr_t)buffer;
-        EGLTextureWrapper* tex = new EGLTextureWrapper();
-        if (!tex->CreateEGLImage(gl, texture)) {
-            NS_ERROR("EGLImage creation for EGLTextureWrapper failed");
-            delete tex;
-            return 0;
-        }
-
-        return (SharedTextureHandle)tex;
-    }
-    default:
-        NS_ERROR("Unknown shared texture buffer type");
-        return 0;
-    }
-}
-
-void ReleaseSharedHandle(GLContext* gl,
-                         SharedTextureShareType shareType,
-                         SharedTextureHandle sharedHandle)
-{
-    // unimplemented outside of EGL
-    if (gl->GetContextType() != GLContextType::EGL)
-        return;
-
-    if (shareType != SharedTextureShareType::SameProcess) {
-        NS_ERROR("Implementation not available for this sharing type");
-        return;
-    }
-
-    SharedTextureHandleWrapper* wrapper = reinterpret_cast<SharedTextureHandleWrapper*>(sharedHandle);
-
-    switch (wrapper->Type()) {
-#ifdef MOZ_WIDGET_ANDROID
-    case SharedHandleType_SurfaceTexture:
-        delete wrapper;
-        break;
-#endif
-
-    case SharedHandleType_Image: {
-        NS_ASSERTION(DoesEGLContextSupportSharingWithEGLImage(gl), "EGLImage not supported or disabled in runtime");
-
-        EGLTextureWrapper* wrap = (EGLTextureWrapper*)sharedHandle;
-        delete wrap;
-        break;
-    }
-
-    default:
-        NS_ERROR("Unknown shared handle type");
-        return;
-    }
-}
-
-bool GetSharedHandleDetails(GLContext* gl,
-                            SharedTextureShareType shareType,
-                            SharedTextureHandle sharedHandle,
-                            SharedHandleDetails& details)
-{
-    // unimplemented outside of EGL
-    if (gl->GetContextType() != GLContextType::EGL)
-        return false;
-
-    if (shareType != SharedTextureShareType::SameProcess)
-        return false;
-
-    SharedTextureHandleWrapper* wrapper = reinterpret_cast<SharedTextureHandleWrapper*>(sharedHandle);
-
-    switch (wrapper->Type()) {
-#ifdef MOZ_WIDGET_ANDROID
-    case SharedHandleType_SurfaceTexture: {
-        SurfaceTextureWrapper* surfaceWrapper = reinterpret_cast<SurfaceTextureWrapper*>(wrapper);
-
-        details.mTarget = LOCAL_GL_TEXTURE_EXTERNAL;
-        details.mTextureFormat = gfx::SurfaceFormat::R8G8B8A8;
-        surfaceWrapper->SurfaceTexture()->GetTransformMatrix(details.mTextureTransform);
-        break;
-    }
-#endif
-
-    case SharedHandleType_Image:
-        details.mTarget = LOCAL_GL_TEXTURE_2D;
-        details.mTextureFormat = gfx::SurfaceFormat::R8G8B8A8;
-        break;
-
-    default:
-        NS_ERROR("Unknown shared handle type");
-        return false;
-    }
-
-    return true;
-}
-
-bool AttachSharedHandle(GLContext* gl,
-                        SharedTextureShareType shareType,
-                        SharedTextureHandle sharedHandle)
-{
-    // unimplemented outside of EGL
-    if (gl->GetContextType() != GLContextType::EGL)
-        return false;
-
-    if (shareType != SharedTextureShareType::SameProcess)
-        return false;
-
-    SharedTextureHandleWrapper* wrapper = reinterpret_cast<SharedTextureHandleWrapper*>(sharedHandle);
-
-    switch (wrapper->Type()) {
-#ifdef MOZ_WIDGET_ANDROID
-    case SharedHandleType_SurfaceTexture: {
-#ifndef DEBUG
-        /*
-         * NOTE: SurfaceTexture spams us if there are any existing GL errors, so we'll clear
-         * them here in order to avoid that.
-         */
-        gl->GetAndClearError();
-#endif
-        SurfaceTextureWrapper* surfaceTextureWrapper = reinterpret_cast<SurfaceTextureWrapper*>(wrapper);
-
-        // FIXME: SurfaceTexture provides a transform matrix which is supposed to
-        // be applied to the texture coordinates. We should return that here
-        // so we can render correctly. Bug 775083
-        surfaceTextureWrapper->SurfaceTexture()->UpdateTexImage();
-        break;
-    }
-#endif // MOZ_WIDGET_ANDROID
-
-    case SharedHandleType_Image: {
-        NS_ASSERTION(DoesEGLContextSupportSharingWithEGLImage(gl), "EGLImage not supported or disabled in runtime");
-
-        EGLTextureWrapper* wrap = (EGLTextureWrapper*)sharedHandle;
-        wrap->WaitSync();
-        gl->fEGLImageTargetTexture2D(LOCAL_GL_TEXTURE_2D, wrap->GetEGLImage());
-        break;
-    }
-
-    default:
-        NS_ERROR("Unknown shared handle type");
-        return false;
-    }
-
-    return true;
-}
-
-/**
-  * Detach Shared GL Handle from GL_TEXTURE_2D target
-  */
-void DetachSharedHandle(GLContext*,
-                        SharedTextureShareType,
-                        SharedTextureHandle)
-{
-  // currently a no-operation
-}
-
-}
-}
deleted file mode 100644
--- a/gfx/gl/GLSharedHandleHelpers.h
+++ /dev/null
@@ -1,73 +0,0 @@
-/* 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 GLSHAREDHANDLEHELPERS_H_
-#define GLSHAREDHANDLEHELPERS_H_
-
-#include "GLContextTypes.h"
-#include "mozilla/gfx/Types.h"
-
-namespace mozilla {
-namespace gl {
-
-/**
-  * Create a new shared GLContext content handle, using the passed buffer as a source.
-  * Must be released by ReleaseSharedHandle.
-  */
-SharedTextureHandle CreateSharedHandle(GLContext* gl,
-                                       SharedTextureShareType shareType,
-                                       void* buffer,
-                                       SharedTextureBufferType bufferType);
-
-/**
-  * - It is better to call ReleaseSharedHandle before original GLContext destroyed,
-  *     otherwise warning will be thrown on attempt to destroy Texture associated with SharedHandle, depends on backend implementation.
-  * - It does not require to be called on context where it was created,
-  *     because SharedHandle suppose to keep Context reference internally,
-  *     or don't require specific context at all, for example IPC SharedHandle.
-  * - Not recommended to call this between AttachSharedHandle and Draw Target call.
-  *      if it is really required for some special backend, then DetachSharedHandle API must be added with related implementation.
-  * - It is recommended to stop any possible access to SharedHandle (Attachments, pending GL calls) before calling Release,
-  *      otherwise some artifacts might appear or even crash if API backend implementation does not expect that.
-  * SharedHandle (currently EGLImage) does not require GLContext because it is EGL call, and can be destroyed
-  *   at any time, unless EGLImage have siblings (which are not expected with current API).
-  */
-void ReleaseSharedHandle(GLContext* gl,
-                         SharedTextureShareType shareType,
-                         SharedTextureHandle sharedHandle);
-
-
-typedef struct {
-    GLenum mTarget;
-    gfx::SurfaceFormat mTextureFormat;
-    gfx::Matrix4x4 mTextureTransform;
-} SharedHandleDetails;
-
-/**
-  * Returns information necessary for rendering a shared handle.
-  * These values change depending on what sharing mechanism is in use
-  */
-bool GetSharedHandleDetails(GLContext* gl,
-                            SharedTextureShareType shareType,
-                            SharedTextureHandle sharedHandle,
-                            SharedHandleDetails& details);
-/**
-  * Attach Shared GL Handle to GL_TEXTURE_2D target
-  * GLContext must be current before this call
-  */
-bool AttachSharedHandle(GLContext* gl,
-                        SharedTextureShareType shareType,
-                        SharedTextureHandle sharedHandle);
-
-/**
-  * Detach Shared GL Handle from GL_TEXTURE_2D target
-  */
-void DetachSharedHandle(GLContext* gl,
-                        SharedTextureShareType shareType,
-                        SharedTextureHandle sharedHandle);
-
-}
-}
-
-#endif
--- a/gfx/gl/TextureImageEGL.cpp
+++ b/gfx/gl/TextureImageEGL.cpp
@@ -84,18 +84,19 @@ TextureImageEGL::~TextureImageEGL()
     if (mGLContext->IsDestroyed() || !mGLContext->IsOwningThreadCurrent()) {
         return;
     }
 
     // If we have a context, then we need to delete the texture;
     // if we don't have a context (either real or shared),
     // then they went away when the contex was deleted, because it
     // was the only one that had access to it.
-    mGLContext->MakeCurrent();
-    mGLContext->fDeleteTextures(1, &mTexture);
+    if (mGLContext->MakeCurrent()) {
+        mGLContext->fDeleteTextures(1, &mTexture);
+    }
     ReleaseTexImage();
     DestroyEGLSurface();
 }
 
 void
 TextureImageEGL::GetUpdateRegion(nsIntRegion& aForRegion)
 {
     if (mTextureState != Valid) {
--- a/gfx/gl/moz.build
+++ b/gfx/gl/moz.build
@@ -22,33 +22,33 @@ elif CONFIG['MOZ_WIDGET_TOOLKIT'] == 'an
 elif CONFIG['MOZ_WIDGET_TOOLKIT'] == 'gonk':
     gl_provider = 'EGL'
 
 if CONFIG['MOZ_GL_PROVIDER']:
     gl_provider = CONFIG['MOZ_GL_PROVIDER']
 
 EXPORTS += [
     'DecomposeIntoNoRepeatTriangles.h',
+    'EGLUtils.h',
     'ForceDiscreteGPUHelperCGL.h',
     'GfxTexturesReporter.h',
     'GLBlitHelper.h',
     'GLBlitTextureImageHelper.h',
     'GLConsts.h',
     'GLContext.h',
     'GLContextEGL.h',
     'GLContextProvider.h',
     'GLContextProviderImpl.h',
     'GLContextSymbols.h',
     'GLContextTypes.h',
     'GLDefs.h',
     'GLLibraryEGL.h',
     'GLLibraryLoader.h',
     'GLReadTexImageHelper.h',
     'GLScreenBuffer.h',
-    'GLSharedHandleHelpers.h',
     'GLTextureImage.h',
     'GLTypes.h',
     'GLUploadHelpers.h',
     'HeapCopyOfStackArray.h',
     'ScopedGLHelpers.h',
     'SharedSurface.h',
     'SharedSurfaceEGL.h',
     'SharedSurfaceGL.h',
@@ -111,28 +111,28 @@ elif gl_provider == 'GLX':
     ]
 else:
     UNIFIED_SOURCES += [
         'GLContextProvider%s.cpp' % gl_provider,
     ]
 
 UNIFIED_SOURCES += [
     'DecomposeIntoNoRepeatTriangles.cpp',
+    'EGLUtils.cpp',
     'GfxTexturesReporter.cpp',
     'GLBlitHelper.cpp',
     'GLBlitTextureImageHelper.cpp',
     'GLContext.cpp',
     'GLContextFeatures.cpp',
     'GLContextTypes.cpp',
     'GLDebugUtils.cpp',
     'GLLibraryEGL.cpp',
     'GLLibraryLoader.cpp',
     'GLReadTexImageHelper.cpp',
     'GLScreenBuffer.cpp',
-    'GLSharedHandleHelpers.cpp',
     'GLTextureImage.cpp',
     'GLUploadHelpers.cpp',
     'ScopedGLHelpers.cpp',
     'SharedSurface.cpp',
     'SharedSurfaceEGL.cpp',
     'SharedSurfaceGL.cpp',
     'SurfaceStream.cpp',
     'SurfaceTypes.cpp',
--- a/gfx/layers/Compositor.h
+++ b/gfx/layers/Compositor.h
@@ -94,17 +94,17 @@
  *
  *
  * # Backend implementations
  *
  * Compositor backends like OpenGL or flavours of D3D live in their own directory
  * under gfx/layers/. To add a new backend, implement at least the following
  * interfaces:
  * - Compositor (ex. CompositorOGL)
- * - TextureHost (ex. SharedTextureHostOGL)
+ * - TextureHost (ex. SurfaceTextureHost)
  * Depending on the type of data that needs to be serialized, you may need to
  * add specific TextureClient implementations.
  */
 
 class nsIWidget;
 struct nsIntSize;
 class nsIntRegion;
 
new file mode 100644
--- /dev/null
+++ b/gfx/layers/GLImages.h
@@ -0,0 +1,75 @@
+/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
+ * 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 GFX_GLIMAGES_H
+#define GFX_GLIMAGES_H
+
+#include "GLTypes.h"
+#include "ImageContainer.h"             // for Image
+#include "ImageTypes.h"                 // for ImageFormat::SHARED_GLTEXTURE
+#include "nsCOMPtr.h"                   // for already_AddRefed
+#include "mozilla/gfx/Point.h"          // for IntSize
+
+class nsSurfaceTexture;
+
+namespace mozilla {
+namespace layers {
+
+class EGLImageImage : public Image {
+public:
+  struct Data {
+    EGLImage mImage;
+    gfx::IntSize mSize;
+    bool mInverted;
+  };
+
+  void SetData(const Data& aData) { mData = aData; }
+  const Data* GetData() { return &mData; }
+
+  gfx::IntSize GetSize() { return mData.mSize; }
+
+  virtual TemporaryRef<gfx::SourceSurface> GetAsSourceSurface() MOZ_OVERRIDE
+  {
+    return nullptr;
+  }
+
+  EGLImageImage() : Image(nullptr, ImageFormat::EGLIMAGE) {}
+
+private:
+  Data mData;
+};
+
+#ifdef MOZ_WIDGET_ANDROID
+
+class SurfaceTextureImage : public Image {
+public:
+  struct Data {
+    nsSurfaceTexture* mSurfTex;
+    gfx::IntSize mSize;
+    bool mInverted;
+  };
+
+  void SetData(const Data& aData) { mData = aData; }
+  const Data* GetData() { return &mData; }
+
+  gfx::IntSize GetSize() { return mData.mSize; }
+
+  virtual TemporaryRef<gfx::SourceSurface> GetAsSourceSurface() MOZ_OVERRIDE
+  {
+    return nullptr;
+  }
+
+  SurfaceTextureImage() : Image(nullptr, ImageFormat::SURFACE_TEXTURE) {}
+
+private:
+  Data mData;
+};
+
+#endif // MOZ_WIDGET_ANDROID
+
+} // layers
+} // mozilla
+
+#endif // GFX_GLIMAGES_H
--- a/gfx/layers/ImageContainer.cpp
+++ b/gfx/layers/ImageContainer.cpp
@@ -1,17 +1,17 @@
 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  * 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 "ImageContainer.h"
 #include <string.h>                     // for memcpy, memset
-#include "SharedTextureImage.h"         // for SharedTextureImage
+#include "GLImages.h"                   // for SurfaceTextureImage
 #include "gfx2DGlue.h"
 #include "gfxPlatform.h"                // for gfxPlatform
 #include "gfxUtils.h"                   // for gfxUtils
 #include "mozilla/RefPtr.h"             // for TemporaryRef
 #include "mozilla/ipc/CrossProcessMutex.h"  // for CrossProcessMutex, etc
 #include "mozilla/layers/CompositorTypes.h"
 #include "mozilla/layers/ImageBridgeChild.h"  // for ImageBridgeChild
 #include "mozilla/layers/ImageClient.h"  // for ImageClient
@@ -64,18 +64,24 @@ ImageFactory::CreateImage(ImageFormat aF
   if (aFormat == ImageFormat::PLANAR_YCBCR) {
     img = new PlanarYCbCrImage(aRecycleBin);
     return img.forget();
   }
   if (aFormat == ImageFormat::CAIRO_SURFACE) {
     img = new CairoImage();
     return img.forget();
   }
-  if (aFormat == ImageFormat::SHARED_TEXTURE) {
-    img = new SharedTextureImage();
+#ifdef MOZ_WIDGET_ANDROID
+  if (aFormat == ImageFormat::SURFACE_TEXTURE) {
+    img = new SurfaceTextureImage();
+    return img.forget();
+  }
+#endif
+  if (aFormat == ImageFormat::EGLIMAGE) {
+    img = new EGLImageImage();
     return img.forget();
   }
 #ifdef XP_MACOSX
   if (aFormat == ImageFormat::MAC_IOSURFACE) {
     img = new MacIOSurfaceImage();
     return img.forget();
   }
 #endif
--- a/gfx/layers/ImageTypes.h
+++ b/gfx/layers/ImageTypes.h
@@ -53,19 +53,25 @@ MOZ_BEGIN_ENUM_CLASS(ImageFormat)
   MAC_IOSURFACE,
 
   /**
    * An bitmap image that can be shared with a remote process.
    */
   REMOTE_IMAGE_BITMAP,
 
   /**
-   * A OpenGL texture that can be shared across threads or processes
+   * An Android SurfaceTexture ID that can be shared across threads and
+   * processes.
    */
-  SHARED_TEXTURE,
+  SURFACE_TEXTURE,
+
+  /**
+   * An EGL Image that can be shared across threads.
+   */
+  EGLIMAGE,
 
   /**
    * An DXGI shared surface handle that can be shared with a remote process.
    */
   REMOTE_IMAGE_DXGI_TEXTURE,
 
   /**
    * The D3D9_RGB32_TEXTURE format creates a D3D9SurfaceImage, and wraps a
deleted file mode 100644
--- a/gfx/layers/SharedTextureImage.h
+++ /dev/null
@@ -1,50 +0,0 @@
-/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
- * 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 GFX_SHAREDTEXTUREIMAGE_H
-#define GFX_SHAREDTEXTUREIMAGE_H
-
-#include "GLContextProvider.h"          // for GLContextProvider
-#include "ImageContainer.h"             // for Image
-#include "ImageTypes.h"                 // for ImageFormat::SHARED_TEXTURE
-#include "nsCOMPtr.h"                   // for already_AddRefed
-#include "mozilla/gfx/Point.h"          // for IntSize
-
-// Split into a separate header from ImageLayers.h due to GLContext.h dependence
-// Implementation remains in ImageLayers.cpp
-
-namespace mozilla {
-
-namespace layers {
-
-class SharedTextureImage : public Image {
-public:
-  struct Data {
-    gl::SharedTextureHandle mHandle;
-    gl::SharedTextureShareType mShareType;
-    gfx::IntSize mSize;
-    bool mInverted;
-  };
-
-  void SetData(const Data& aData) { mData = aData; }
-  const Data* GetData() { return &mData; }
-
-  gfx::IntSize GetSize() { return mData.mSize; }
-
-  virtual TemporaryRef<gfx::SourceSurface> GetAsSourceSurface() MOZ_OVERRIDE
-  {
-    return nullptr;
-  }
-
-  SharedTextureImage() : Image(nullptr, ImageFormat::SHARED_TEXTURE) {}
-
-private:
-  Data mData;
-};
-
-} // layers
-} // mozilla
-
-#endif // GFX_SHAREDTEXTUREIMAGE_H
--- a/gfx/layers/client/ImageClient.cpp
+++ b/gfx/layers/client/ImageClient.cpp
@@ -2,34 +2,34 @@
  * 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 "ImageClient.h"
 #include <stdint.h>                     // for uint32_t
 #include "ImageContainer.h"             // for Image, PlanarYCbCrImage, etc
 #include "ImageTypes.h"                 // for ImageFormat::PLANAR_YCBCR, etc
-#include "SharedTextureImage.h"         // for SharedTextureImage::Data, etc
+#include "GLImages.h"                   // for SurfaceTextureImage::Data, etc
 #include "gfx2DGlue.h"                  // for ImageFormatToSurfaceFormat
 #include "gfxPlatform.h"                // for gfxPlatform
 #include "mozilla/Assertions.h"         // for MOZ_ASSERT, etc
 #include "mozilla/RefPtr.h"             // for RefPtr, TemporaryRef
 #include "mozilla/gfx/BaseSize.h"       // for BaseSize
 #include "mozilla/gfx/Point.h"          // for IntSize
 #include "mozilla/gfx/Types.h"          // for SurfaceFormat, etc
 #include "mozilla/layers/CompositableClient.h"  // for CompositableClient
 #include "mozilla/layers/CompositableForwarder.h"
 #include "mozilla/layers/CompositorTypes.h"  // for CompositableType, etc
 #include "mozilla/layers/ISurfaceAllocator.h"
 #include "mozilla/layers/LayersSurfaces.h"  // for SurfaceDescriptor, etc
 #include "mozilla/layers/ShadowLayers.h"  // for ShadowLayerForwarder
 #include "mozilla/layers/SharedPlanarYCbCrImage.h"
 #include "mozilla/layers/SharedRGBImage.h"
 #include "mozilla/layers/TextureClient.h"  // for TextureClient, etc
-#include "mozilla/layers/TextureClientOGL.h"  // for SharedTextureClientOGL
+#include "mozilla/layers/TextureClientOGL.h"  // for SurfaceTextureClient
 #include "mozilla/mozalloc.h"           // for operator delete, etc
 #include "nsAutoPtr.h"                  // for nsRefPtr
 #include "nsCOMPtr.h"                   // for already_AddRefed
 #include "nsDebug.h"                    // for NS_WARNING, NS_ASSERTION
 #include "nsISupportsImpl.h"            // for Image::Release, etc
 #include "nsRect.h"                     // for nsIntRect
 #include "mozilla/gfx/2D.h"
 #ifdef MOZ_WIDGET_GONK
@@ -242,35 +242,58 @@ ImageClientSingle::UpdateImageInternal(I
     if (status) {
       GetForwarder()->UpdatedTexture(this, mFrontBuffer, nullptr);
       GetForwarder()->UseTexture(this, mFrontBuffer);
     } else {
       MOZ_ASSERT(false);
       return false;
     }
 
-  } else if (image->GetFormat() == ImageFormat::SHARED_TEXTURE) {
-    SharedTextureImage* sharedImage = static_cast<SharedTextureImage*>(image);
-    const SharedTextureImage::Data *data = sharedImage->GetData();
+  } else if (image->GetFormat() == ImageFormat::SURFACE_TEXTURE ||
+             image->GetFormat() == ImageFormat::EGLIMAGE)
+  {
     gfx::IntSize size = gfx::IntSize(image->GetSize().width, image->GetSize().height);
 
     if (mFrontBuffer) {
       autoRemoveTexture.mTexture = mFrontBuffer;
       mFrontBuffer = nullptr;
     }
 
-    RefPtr<SharedTextureClientOGL> buffer = new SharedTextureClientOGL(mTextureFlags);
-    buffer->InitWith(data->mHandle, size, data->mShareType, data->mInverted);
+    RefPtr<TextureClient> buffer;
+
+    if (image->GetFormat() == ImageFormat::EGLIMAGE) {
+      EGLImageImage* typedImage = static_cast<EGLImageImage*>(image);
+      const EGLImageImage::Data* data = typedImage->GetData();
+
+      buffer = new EGLImageTextureClient(mTextureFlags,
+                                         data->mImage,
+                                         size,
+                                         data->mInverted);
+#ifdef MOZ_WIDGET_ANDROID
+    } else if (image->GetFormat() == ImageFormat::SURFACE_TEXTURE) {
+      SurfaceTextureImage* typedImage = static_cast<SurfaceTextureImage*>(image);
+      const SurfaceTextureImage::Data* data = typedImage->GetData();
+
+      buffer = new SurfaceTextureClient(mTextureFlags,
+                                        data->mSurfTex,
+                                        size,
+                                        data->mInverted);
+#endif
+    } else {
+      MOZ_ASSERT(false, "Bad ImageFormat.");
+    }
+
     mFrontBuffer = buffer;
     if (!AddTextureClient(mFrontBuffer)) {
       mFrontBuffer = nullptr;
       return false;
     }
 
     GetForwarder()->UseTexture(this, mFrontBuffer);
+
   } else {
     RefPtr<gfx::SourceSurface> surface = image->GetAsSourceSurface();
     MOZ_ASSERT(surface);
 
     gfx::IntSize size = image->GetSize();
 
     if (mFrontBuffer &&
         (mFrontBuffer->IsImmutable() || mFrontBuffer->GetSize() != size)) {
--- a/gfx/layers/composite/TextureHost.cpp
+++ b/gfx/layers/composite/TextureHost.cpp
@@ -200,18 +200,19 @@ TextureHost::Create(const SurfaceDescrip
                     TextureFlags aFlags)
 {
   switch (aDesc.type()) {
     case SurfaceDescriptor::TSurfaceDescriptorShmem:
     case SurfaceDescriptor::TSurfaceDescriptorMemory:
     case SurfaceDescriptor::TSurfaceDescriptorDIB:
       return CreateBackendIndependentTextureHost(aDesc, aDeallocator, aFlags);
 
-    case SurfaceDescriptor::TSharedTextureDescriptor:
+    case SurfaceDescriptor::TEGLImageDescriptor:
     case SurfaceDescriptor::TNewSurfaceDescriptorGralloc:
+    case SurfaceDescriptor::TSurfaceTextureDescriptor:
       return CreateTextureHostOGL(aDesc, aDeallocator, aFlags);
 
     case SurfaceDescriptor::TSurfaceStreamDescriptor:
       return new StreamTextureHost(aFlags, aDesc.get_SurfaceStreamDescriptor());
 
     case SurfaceDescriptor::TSurfaceDescriptorMacIOSurface:
       if (Compositor::GetBackend() == LayersBackend::LAYERS_OPENGL) {
         return CreateTextureHostOGL(aDesc, aDeallocator, aFlags);
--- a/gfx/layers/ipc/LayersSurfaces.ipdlh
+++ b/gfx/layers/ipc/LayersSurfaces.ipdlh
@@ -6,18 +6,16 @@ using struct gfxPoint from "gfxPoint.h";
 using struct nsIntRect from "nsRect.h";
 using nsIntRegion from "nsRegion.h";
 using struct nsIntSize from "nsSize.h";
 using struct mozilla::layers::MagicGrallocBufferHandle from "gfxipc/ShadowLayerUtils.h";
 using struct mozilla::layers::GrallocBufferRef from "gfxipc/ShadowLayerUtils.h";
 using struct mozilla::layers::SurfaceDescriptorX11 from "gfxipc/ShadowLayerUtils.h";
 using struct mozilla::null_t from "ipc/IPCMessageUtils.h";
 using mozilla::WindowsHandle from "ipc/IPCMessageUtils.h";
-using mozilla::gl::SharedTextureHandle from "GLContextTypes.h";
-using mozilla::gl::SharedTextureShareType from "GLContextTypes.h";
 using mozilla::gfx::SurfaceFormat from "mozilla/gfx/Types.h";
 using mozilla::gfx::IntSize from "mozilla/gfx/Point.h";
 using gfxImageFormat from "gfxTypes.h";
 
 namespace mozilla {
 namespace layers {
 
 union OverlayHandle {
@@ -53,21 +51,24 @@ struct SurfaceDescriptorD3D10 {
 };
 
 struct SurfaceDescriptorMacIOSurface {
   uint32_t surface;
   double scaleFactor;
   bool hasAlpha;
 };
 
-struct SharedTextureDescriptor {
-  SharedTextureShareType shareType;
-  SharedTextureHandle handle;
+struct SurfaceTextureDescriptor {
+  uintptr_t surfTex;
   IntSize size;
-  bool inverted;
+};
+
+struct EGLImageDescriptor {
+  uintptr_t image; // `EGLImage` is a `void*`.
+  IntSize size;
 };
 
 struct NewSurfaceDescriptorGralloc {
   MaybeMagicGrallocBufferHandle buffer;
   /**
    * android::GraphicBuffer has a size information. But there are cases
    * that GraphicBuffer's size and actual video's size are different.
    * Extra size member is necessary. See Bug 850566.
@@ -98,17 +99,18 @@ struct SurfaceDescriptorShmem {
 
 union SurfaceDescriptor {
   SurfaceDescriptorShmem;
   SurfaceDescriptorMemory;
   SurfaceDescriptorD3D9;
   SurfaceDescriptorDIB;
   SurfaceDescriptorD3D10;
   SurfaceDescriptorX11;
-  SharedTextureDescriptor;
+  SurfaceTextureDescriptor;
+  EGLImageDescriptor;
   SurfaceStreamDescriptor;
   SurfaceDescriptorMacIOSurface;
   NewSurfaceDescriptorGralloc;
   null_t;
 };
 
 } // namespace
 } // namespace
--- a/gfx/layers/ipc/ShadowLayerUtils.h
+++ b/gfx/layers/ipc/ShadowLayerUtils.h
@@ -51,39 +51,16 @@ namespace IPC {
 template <>
 struct ParamTraits<mozilla::layers::SurfaceDescriptorX11> {
   typedef mozilla::layers::SurfaceDescriptorX11 paramType;
   static void Write(Message*, const paramType&) {}
   static bool Read(const Message*, void**, paramType*) { return false; }
 };
 #endif  // !defined(MOZ_HAVE_XSURFACEDESCRIPTORX11)
 
-template<>
-struct ParamTraits<mozilla::gl::SharedTextureShareType>
-{
-  typedef mozilla::gl::SharedTextureShareType paramType;
-
-  static void Write(Message* msg, const paramType& param)
-  {
-    static_assert(sizeof(paramType) <= sizeof(int32_t),
-                  "TextureShareType assumes to be int32_t");
-    WriteParam(msg, int32_t(param));
-  }
-
-  static bool Read(const Message* msg, void** iter, paramType* result)
-  {
-    int32_t type;
-    if (!ReadParam(msg, iter, &type))
-      return false;
-
-    *result = paramType(type);
-    return true;
-  }
-};
-
 #if !defined(MOZ_HAVE_SURFACEDESCRIPTORGRALLOC)
 template <>
 struct ParamTraits<mozilla::layers::MagicGrallocBufferHandle> {
   typedef mozilla::layers::MagicGrallocBufferHandle paramType;
   static void Write(Message*, const paramType&) {}
   static bool Read(const Message*, void**, paramType*) { return false; }
 };
 
--- a/gfx/layers/moz.build
+++ b/gfx/layers/moz.build
@@ -15,16 +15,17 @@ EXPORTS += [
     'client/ClientLayerManager.h',
     'client/ClientThebesLayer.h',
     'client/ClientTiledThebesLayer.h',
     'composite/CompositableHost.h',
     'composite/ImageHost.h',
     'CopyableCanvasLayer.h',
     'D3D9SurfaceImage.h',
     'FrameMetrics.h',
+    'GLImages.h',
     'GrallocImages.h',
     'ImageContainer.h',
     'ImageLayers.h',
     'ImageTypes.h',
     'ipc/CompositorChild.h',
     'ipc/CompositorParent.h',
     'ipc/ShadowLayersManager.h',
     'ipc/ThreadSafeRefcountingWithMainThreadDestruction.h',
@@ -33,17 +34,16 @@ EXPORTS += [
     'LayersLogging.h',
     'LayerSorter.h',
     'LayerTreeInvalidation.h',
     'opengl/Composer2D.h',
     'opengl/OGLShaderProgram.h',
     'opengl/TexturePoolOGL.h',
     'protobuf/LayerScopePacket.pb.h',
     'ReadbackLayer.h',
-    'SharedTextureImage.h',
     'TiledLayerBuffer.h',
 ]
 
 if CONFIG['MOZ_WIDGET_TOOLKIT'] == 'windows':
     UNIFIED_SOURCES += [
         'D3D9SurfaceImage.cpp',
         'TextureDIB.cpp',
     ]
--- a/gfx/layers/opengl/MacIOSurfaceTextureHostOGL.h
+++ b/gfx/layers/opengl/MacIOSurfaceTextureHostOGL.h
@@ -59,17 +59,17 @@ protected:
  * Most of the logic actually happens in MacIOSurfaceTextureSourceOGL.
  */
 class MacIOSurfaceTextureHostOGL : public TextureHost
 {
 public:
   MacIOSurfaceTextureHostOGL(TextureFlags aFlags,
                              const SurfaceDescriptorMacIOSurface& aDescriptor);
 
-  // SharedTextureHostOGL doesn't own any GL texture
+  // MacIOSurfaceTextureSourceOGL doesn't own any GL texture
   virtual void DeallocateDeviceData() MOZ_OVERRIDE {}
 
   virtual void SetCompositor(Compositor* aCompositor) MOZ_OVERRIDE;
 
   virtual bool Lock() MOZ_OVERRIDE;
 
   virtual gfx::SurfaceFormat GetFormat() const MOZ_OVERRIDE;
 
--- a/gfx/layers/opengl/TextureClientOGL.cpp
+++ b/gfx/layers/opengl/TextureClientOGL.cpp
@@ -12,78 +12,127 @@
 
 using namespace mozilla::gl;
 
 namespace mozilla {
 namespace layers {
 
 class CompositableForwarder;
 
-SharedTextureClientOGL::SharedTextureClientOGL(TextureFlags aFlags)
+////////////////////////////////////////////////////////////////////////
+// EGLImageTextureClient
+
+EGLImageTextureClient::EGLImageTextureClient(TextureFlags aFlags,
+                                             EGLImage aImage,
+                                             gfx::IntSize aSize,
+                                             bool aInverted)
   : TextureClient(aFlags)
-  , mHandle(0)
-  , mInverted(false)
+  , mImage(aImage)
+  , mSize(aSize)
   , mIsLocked(false)
 {
-  // SharedTextureClient is always owned externally.
-  mFlags |= TextureFlags::DEALLOCATE_CLIENT;
+  MOZ_ASSERT(XRE_GetProcessType() == GeckoProcessType_Default,
+             "Can't pass an `EGLImage` between processes.");
+  
+  // Our data is always owned externally.
+  AddFlags(TextureFlags::DEALLOCATE_CLIENT);
+  
+  if (aInverted) {
+    AddFlags(TextureFlags::NEEDS_Y_FLIP);
+  }
+}
+  
+EGLImageTextureClient::~EGLImageTextureClient()
+{
+  // Our data is always owned externally.
 }
 
-SharedTextureClientOGL::~SharedTextureClientOGL()
-{
-  // the shared data is owned externally.
-}
-
-
 bool
-SharedTextureClientOGL::ToSurfaceDescriptor(SurfaceDescriptor& aOutDescriptor)
+EGLImageTextureClient::ToSurfaceDescriptor(SurfaceDescriptor& aOutDescriptor)
 {
   MOZ_ASSERT(IsValid());
-  if (!IsAllocated()) {
-    return false;
-  }
-  aOutDescriptor = SharedTextureDescriptor(mShareType, mHandle, mSize, mInverted);
+  MOZ_ASSERT(IsAllocated());
+
+  aOutDescriptor = EGLImageDescriptor((uintptr_t)mImage, mSize);
   return true;
 }
 
+bool
+EGLImageTextureClient::Lock(OpenMode mode)
+  {
+    MOZ_ASSERT(!mIsLocked);
+    if (!IsValid() || !IsAllocated()) {
+      return false;
+    }
+    mIsLocked = true;
+    return true;
+  }
+  
 void
-SharedTextureClientOGL::InitWith(gl::SharedTextureHandle aHandle,
-                                 gfx::IntSize aSize,
-                                 gl::SharedTextureShareType aShareType,
-                                 bool aInverted)
+EGLImageTextureClient::Unlock()
 {
-  MOZ_ASSERT(IsValid());
-  MOZ_ASSERT(!IsAllocated());
-  mHandle = aHandle;
-  mSize = aSize;
-  mShareType = aShareType;
-  mInverted = aInverted;
-  if (mInverted) {
+  MOZ_ASSERT(mIsLocked);
+  mIsLocked = false;
+}
+  
+////////////////////////////////////////////////////////////////////////
+// SurfaceTextureClient
+
+#ifdef MOZ_WIDGET_ANDROID
+
+SurfaceTextureClient::SurfaceTextureClient(TextureFlags aFlags,
+                                           nsSurfaceTexture* aSurfTex,
+                                           gfx::IntSize aSize,
+                                           bool aInverted)
+  : TextureClient(aFlags)
+  , mSurfTex(aSurfTex)
+  , mSize(aSize)
+  , mIsLocked(false)
+{
+  MOZ_ASSERT(XRE_GetProcessType() == GeckoProcessType_Default,
+             "Can't pass pointers between processes.");
+
+  // Our data is always owned externally.
+  AddFlags(TextureFlags::DEALLOCATE_CLIENT);
+
+  if (aInverted) {
     AddFlags(TextureFlags::NEEDS_Y_FLIP);
   }
 }
+  
+SurfaceTextureClient::~SurfaceTextureClient()
+{
+  // Our data is always owned externally.
+}
 
 bool
-SharedTextureClientOGL::Lock(OpenMode mode)
+SurfaceTextureClient::ToSurfaceDescriptor(SurfaceDescriptor& aOutDescriptor)
+{
+  MOZ_ASSERT(IsValid());
+  MOZ_ASSERT(IsAllocated());
+
+  aOutDescriptor = SurfaceTextureDescriptor((uintptr_t)mSurfTex.get(),
+                                            mSize);
+  return true;
+}
+
+bool
+SurfaceTextureClient::Lock(OpenMode mode)
 {
   MOZ_ASSERT(!mIsLocked);
   if (!IsValid() || !IsAllocated()) {
     return false;
   }
   mIsLocked = true;
   return true;
 }
 
 void
-SharedTextureClientOGL::Unlock()
+SurfaceTextureClient::Unlock()
 {
   MOZ_ASSERT(mIsLocked);
   mIsLocked = false;
 }
 
-bool
-SharedTextureClientOGL::IsAllocated() const
-{
-  return mHandle != 0;
-}
+#endif // MOZ_WIDGET_ANDROID
 
 } // namespace
 } // namespace
--- a/gfx/layers/opengl/TextureClientOGL.h
+++ b/gfx/layers/opengl/TextureClientOGL.h
@@ -8,78 +8,125 @@
 
 #include "GLContextTypes.h"             // for SharedTextureHandle, etc
 #include "gfxTypes.h"
 #include "mozilla/Attributes.h"         // for MOZ_OVERRIDE
 #include "mozilla/gfx/Point.h"          // for IntSize
 #include "mozilla/layers/CompositorTypes.h"
 #include "mozilla/layers/LayersSurfaces.h"  // for SurfaceDescriptor
 #include "mozilla/layers/TextureClient.h"  // for TextureClient, etc
+#include "nsSurfaceTexture.h"
 
 namespace mozilla {
 namespace gl {
 class SurfaceStream;
 }
 }
 
 namespace mozilla {
 namespace layers {
 
 class CompositableForwarder;
 
-/**
- * A TextureClient implementation to share TextureMemory that is already
- * on the GPU, for the OpenGL backend.
- */
-class SharedTextureClientOGL : public TextureClient
+class EGLImageTextureClient : public TextureClient
 {
 public:
-  explicit SharedTextureClientOGL(TextureFlags aFlags);
+  EGLImageTextureClient(TextureFlags aFlags,
+                        EGLImage aImage,
+                        gfx::IntSize aSize,
+                        bool aInverted);
 
-  ~SharedTextureClientOGL();
+  ~EGLImageTextureClient();
+
+  virtual bool IsAllocated() const MOZ_OVERRIDE { return true; }
 
-  virtual bool IsAllocated() const MOZ_OVERRIDE;
+  virtual bool HasInternalBuffer() const MOZ_OVERRIDE { return false; }
+
+  virtual gfx::IntSize GetSize() const { return mSize; }
 
   virtual bool ToSurfaceDescriptor(SurfaceDescriptor& aOutDescriptor) MOZ_OVERRIDE;
 
+  // Useless functions.
   virtual bool Lock(OpenMode mode) MOZ_OVERRIDE;
 
   virtual void Unlock() MOZ_OVERRIDE;
 
   virtual bool IsLocked() const MOZ_OVERRIDE { return mIsLocked; }
 
-  virtual bool HasInternalBuffer() const MOZ_OVERRIDE { return false; }
-
-  void InitWith(gl::SharedTextureHandle aHandle,
-                gfx::IntSize aSize,
-                gl::SharedTextureShareType aShareType,
-                bool aInverted = false);
-
-  virtual gfx::IntSize GetSize() const { return mSize; }
-
   virtual gfx::SurfaceFormat GetFormat() const MOZ_OVERRIDE
   {
     return gfx::SurfaceFormat::UNKNOWN;
   }
 
-  // This TextureClient should not be used in a context where we use CreateSimilar
-  // (ex. component alpha) because the underlying texture data is always created by
-  // an external producer.
   virtual TemporaryRef<TextureClient>
-  CreateSimilar(TextureFlags, TextureAllocationFlags) const MOZ_OVERRIDE { return nullptr; }
+  CreateSimilar(TextureFlags aFlags = TextureFlags::DEFAULT,
+                TextureAllocationFlags aAllocFlags = ALLOC_DEFAULT) const MOZ_OVERRIDE
+  {
+    return nullptr;
+  }
 
   virtual bool AllocateForSurface(gfx::IntSize aSize, TextureAllocationFlags aFlags) MOZ_OVERRIDE
   {
     return false;
   }
 
 protected:
-  gl::SharedTextureHandle mHandle;
-  gfx::IntSize mSize;
-  gl::SharedTextureShareType mShareType;
-  bool mInverted;
+  const EGLImage mImage;
+  const gfx::IntSize mSize;
   bool mIsLocked;
 };
 
+#ifdef MOZ_WIDGET_ANDROID
+
+class SurfaceTextureClient : public TextureClient
+{
+public:
+  SurfaceTextureClient(TextureFlags aFlags,
+                       nsSurfaceTexture* aSurfTex,
+                       gfx::IntSize aSize,
+                       bool aInverted);
+
+  ~SurfaceTextureClient();
+
+  virtual bool IsAllocated() const MOZ_OVERRIDE { return true; }
+
+  virtual bool HasInternalBuffer() const MOZ_OVERRIDE { return false; }
+
+  virtual gfx::IntSize GetSize() const { return mSize; }
+
+  virtual bool ToSurfaceDescriptor(SurfaceDescriptor& aOutDescriptor) MOZ_OVERRIDE;
+
+  // Useless functions.
+  virtual bool Lock(OpenMode mode) MOZ_OVERRIDE;
+
+  virtual void Unlock() MOZ_OVERRIDE;
+
+  virtual bool IsLocked() const MOZ_OVERRIDE { return mIsLocked; }
+
+  virtual gfx::SurfaceFormat GetFormat() const MOZ_OVERRIDE
+  {
+    return gfx::SurfaceFormat::UNKNOWN;
+  }
+
+  virtual TemporaryRef<TextureClient>
+  CreateSimilar(TextureFlags aFlags = TextureFlags::DEFAULT,
+                TextureAllocationFlags aAllocFlags = ALLOC_DEFAULT) const MOZ_OVERRIDE
+  {
+    return nullptr;
+  }
+
+  virtual bool AllocateForSurface(gfx::IntSize aSize, TextureAllocationFlags aFlags) MOZ_OVERRIDE
+  {
+    return false;
+  }
+
+protected:
+  const nsRefPtr<nsSurfaceTexture> mSurfTex;
+  const gfx::IntSize mSize;
+  bool mIsLocked;
+};
+
+#endif // MOZ_WIDGET_ANDROID
+
 } // namespace
 } // namespace
 
 #endif
--- a/gfx/layers/opengl/TextureHostOGL.cpp
+++ b/gfx/layers/opengl/TextureHostOGL.cpp
@@ -1,33 +1,35 @@
 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
 * 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 "TextureHostOGL.h"
+
+#include "EGLUtils.h"
 #include "GLContext.h"                  // for GLContext, etc
 #include "GLLibraryEGL.h"               // for GLLibraryEGL
-#include "GLSharedHandleHelpers.h"
 #include "GLUploadHelpers.h"
 #include "GLReadTexImageHelper.h"
 #include "gfx2DGlue.h"                  // for ContentForFormat, etc
 #include "gfxReusableSurfaceWrapper.h"  // for gfxReusableSurfaceWrapper
 #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 "nsSurfaceTexture.h"
 #include "GfxTexturesReporter.h"        // for GfxTexturesReporter
 #include "GLBlitTextureImageHelper.h"
 #ifdef XP_MACOSX
 #include "SharedSurfaceIO.h"
 #include "mozilla/layers/MacIOSurfaceTextureHostOGL.h"
 #endif
 #include "GeckoProfiler.h"
 
@@ -57,33 +59,44 @@ CreateTextureHostOGL(const SurfaceDescri
   RefPtr<TextureHost> result;
   switch (aDesc.type()) {
     case SurfaceDescriptor::TSurfaceDescriptorShmem:
     case SurfaceDescriptor::TSurfaceDescriptorMemory: {
       result = CreateBackendIndependentTextureHost(aDesc,
                                                    aDeallocator, aFlags);
       break;
     }
-    case SurfaceDescriptor::TSharedTextureDescriptor: {
-      const SharedTextureDescriptor& desc = aDesc.get_SharedTextureDescriptor();
-      result = new SharedTextureHostOGL(aFlags,
-                                        desc.shareType(),
-                                        desc.handle(),
-                                        desc.size(),
-                                        desc.inverted());
+
+#ifdef MOZ_WIDGET_ANDROID
+    case SurfaceDescriptor::TSurfaceTextureDescriptor: {
+      const SurfaceTextureDescriptor& desc = aDesc.get_SurfaceTextureDescriptor();
+      result = new SurfaceTextureHost(aFlags,
+                                      (nsSurfaceTexture*)desc.surfTex(),
+                                      desc.size());
       break;
     }
+#endif
+
+    case SurfaceDescriptor::TEGLImageDescriptor: {
+      const EGLImageDescriptor& desc = aDesc.get_EGLImageDescriptor();
+      result = new EGLImageTextureHost(aFlags,
+                                       (EGLImage)desc.image(),
+                                       desc.size());
+      break;
+    }
+
 #ifdef XP_MACOSX
     case SurfaceDescriptor::TSurfaceDescriptorMacIOSurface: {
       const SurfaceDescriptorMacIOSurface& desc =
         aDesc.get_SurfaceDescriptorMacIOSurface();
       result = new MacIOSurfaceTextureHostOGL(aFlags, desc);
       break;
     }
 #endif
+
 #ifdef MOZ_WIDGET_GONK
     case SurfaceDescriptor::TNewSurfaceDescriptorGralloc: {
       const NewSurfaceDescriptorGralloc& desc =
         aDesc.get_NewSurfaceDescriptorGralloc();
       result = new GrallocTextureHostOGL(aFlags, desc);
       break;
     }
 #endif
@@ -414,90 +427,16 @@ void
 TextureImageTextureSourceOGL::BindTexture(GLenum aTextureUnit, gfx::Filter aFilter)
 {
   MOZ_ASSERT(mTexImage,
     "Trying to bind a TextureSource that does not have an underlying GL texture.");
   mTexImage->BindTexture(aTextureUnit);
   SetFilter(mGL, aFilter);
 }
 
-SharedTextureSourceOGL::SharedTextureSourceOGL(CompositorOGL* aCompositor,
-                                               gl::SharedTextureHandle aHandle,
-                                               gfx::SurfaceFormat aFormat,
-                                               GLenum aTarget,
-                                               GLenum aWrapMode,
-                                               SharedTextureShareType aShareType,
-                                               gfx::IntSize aSize)
-  : mSize(aSize)
-  , mCompositor(aCompositor)
-  , mSharedHandle(aHandle)
-  , mFormat(aFormat)
-  , mShareType(aShareType)
-  , mTextureTarget(aTarget)
-  , mWrapMode(aWrapMode)
-{}
-
-void
-SharedTextureSourceOGL::BindTexture(GLenum aTextureUnit, gfx::Filter aFilter)
-{
-  if (!gl()) {
-    NS_WARNING("Trying to bind a texture without a GLContext");
-    return;
-  }
-  GLuint tex = mCompositor->GetTemporaryTexture(GetTextureTarget(), aTextureUnit);
-
-  gl()->fActiveTexture(aTextureUnit);
-  gl()->fBindTexture(mTextureTarget, tex);
-  if (!AttachSharedHandle(gl(), mShareType, mSharedHandle)) {
-    NS_ERROR("Failed to bind shared texture handle");
-    return;
-  }
-  ApplyFilterToBoundTexture(gl(), aFilter, mTextureTarget);
-}
-
-void
-SharedTextureSourceOGL::DetachSharedHandle()
-{
-  if (!gl()) {
-    return;
-  }
-  gl::DetachSharedHandle(gl(), mShareType, mSharedHandle);
-}
-
-void
-SharedTextureSourceOGL::SetCompositor(Compositor* aCompositor)
-{
-  mCompositor = static_cast<CompositorOGL*>(aCompositor);
-}
-
-bool
-SharedTextureSourceOGL::IsValid() const
-{
-  return !!gl();
-}
-
-gl::GLContext*
-SharedTextureSourceOGL::gl() const
-{
-  return mCompositor ? mCompositor->gl() : nullptr;
-}
-
-gfx::Matrix4x4
-SharedTextureSourceOGL::GetTextureTransform()
-{
-  SharedHandleDetails handleDetails;
-  if (!GetSharedHandleDetails(gl(), mShareType, mSharedHandle, handleDetails)) {
-    NS_WARNING("Could not get shared handle details");
-    return gfx::Matrix4x4();
-  }
-
-  return handleDetails.mTextureTransform;
-}
-
-
 ////////////////////////////////////////////////////////////////////////
 // GLTextureSource
 
 GLTextureSource::GLTextureSource(CompositorOGL* aCompositor,
                                  GLuint aTex,
                                  gfx::SurfaceFormat aFormat,
                                  GLenum aTarget,
                                  gfx::IntSize aSize)
@@ -535,91 +474,278 @@ GLTextureSource::IsValid() const
 
 gl::GLContext*
 GLTextureSource::gl() const
 {
   return mCompositor ? mCompositor->gl() : nullptr;
 }
 
 ////////////////////////////////////////////////////////////////////////
-// SharedTextureHostOGL
+////////////////////////////////////////////////////////////////////////
+// SurfaceTextureHost
+
+#ifdef MOZ_WIDGET_ANDROID
 
-SharedTextureHostOGL::SharedTextureHostOGL(TextureFlags aFlags,
-                                           gl::SharedTextureShareType aShareType,
-                                           gl::SharedTextureHandle aSharedHandle,
-                                           gfx::IntSize aSize,
-                                           bool inverted)
-  : TextureHost(aFlags)
+SurfaceTextureSource::SurfaceTextureSource(CompositorOGL* aCompositor,
+                                           nsSurfaceTexture* aSurfTex,
+                                           gfx::SurfaceFormat aFormat,
+                                           GLenum aTarget,
+                                           GLenum aWrapMode,
+                                           gfx::IntSize aSize)
+  : mCompositor(aCompositor)
+  , mSurfTex(aSurfTex)
+  , mFormat(aFormat)
+  , mTextureTarget(aTarget)
+  , mWrapMode(aWrapMode)
   , mSize(aSize)
-  , mCompositor(nullptr)
-  , mSharedHandle(aSharedHandle)
-  , mShareType(aShareType)
 {
 }
 
-SharedTextureHostOGL::~SharedTextureHostOGL()
+void
+SurfaceTextureSource::BindTexture(GLenum aTextureUnit, gfx::Filter aFilter)
 {
-  // If need to deallocate textures, call DeallocateSharedData() before
-  // the destructor
+  if (!gl()) {
+    NS_WARNING("Trying to bind a texture without a GLContext");
+    return;
+  }
+  GLuint tex = mCompositor->GetTemporaryTexture(GetTextureTarget(), aTextureUnit);
+
+  gl()->fActiveTexture(aTextureUnit);
+  gl()->fBindTexture(mTextureTarget, tex);
+#ifndef DEBUG
+  // SurfaceTexture spams us if there are any existing GL errors, so
+  // we'll clear them here in order to avoid that.
+  gl()->GetAndClearError();
+#endif
+  mSurfTex->UpdateTexImage();
+
+  ApplyFilterToBoundTexture(gl(), aFilter, mTextureTarget);
+}
+
+void
+SurfaceTextureSource::SetCompositor(Compositor* aCompositor)
+{
+  mCompositor = static_cast<CompositorOGL*>(aCompositor);
+}
+
+bool
+SurfaceTextureSource::IsValid() const
+{
+  return !!gl();
 }
 
 gl::GLContext*
-SharedTextureHostOGL::gl() const
+SurfaceTextureSource::gl() const
+{
+  return mCompositor ? mCompositor->gl() : nullptr;
+}
+
+gfx::Matrix4x4
+SurfaceTextureSource::GetTextureTransform()
+{
+  gfx::Matrix4x4 ret;
+  mSurfTex->GetTransformMatrix(ret);
+
+  return ret;
+}
+
+////////////////////////////////////////////////////////////////////////
+
+SurfaceTextureHost::SurfaceTextureHost(TextureFlags aFlags,
+                                       nsSurfaceTexture* aSurfTex,
+                                       gfx::IntSize aSize)
+  : TextureHost(aFlags)
+  , mSurfTex(aSurfTex)
+  , mSize(aSize)
+  , mCompositor(nullptr)
+{
+}
+
+SurfaceTextureHost::~SurfaceTextureHost()
+{
+}
+
+gl::GLContext*
+SurfaceTextureHost::gl() const
 {
   return mCompositor ? mCompositor->gl() : nullptr;
 }
 
 bool
-SharedTextureHostOGL::Lock()
+SurfaceTextureHost::Lock()
 {
   if (!mCompositor) {
     return false;
   }
 
   if (!mTextureSource) {
-    // XXX on android GetSharedHandleDetails can call into Java which we'd
-    // rather not do from the compositor
-    SharedHandleDetails handleDetails;
-    if (!GetSharedHandleDetails(gl(), mShareType, mSharedHandle, handleDetails)) {
-      NS_WARNING("Could not get shared handle details");
-      return false;
-    }
-
+    gfx::SurfaceFormat format = gfx::SurfaceFormat::R8G8B8A8;
+    GLenum target = LOCAL_GL_TEXTURE_EXTERNAL;
     GLenum wrapMode = LOCAL_GL_CLAMP_TO_EDGE;
-    mTextureSource = new SharedTextureSourceOGL(mCompositor,
-                                                mSharedHandle,
-                                                handleDetails.mTextureFormat,
-                                                handleDetails.mTarget,
-                                                wrapMode,
-                                                mShareType,
-                                                mSize);
+    mTextureSource = new SurfaceTextureSource(mCompositor,
+                                              mSurfTex,
+                                              format,
+                                              target,
+                                              wrapMode,
+                                              mSize);
   }
+
   return true;
 }
 
 void
-SharedTextureHostOGL::Unlock()
+SurfaceTextureHost::Unlock()
 {
-  if (!mTextureSource) {
-    return;
-  }
-  mTextureSource->DetachSharedHandle();
 }
 
 void
-SharedTextureHostOGL::SetCompositor(Compositor* aCompositor)
+SurfaceTextureHost::SetCompositor(Compositor* aCompositor)
 {
   CompositorOGL* glCompositor = static_cast<CompositorOGL*>(aCompositor);
   mCompositor = glCompositor;
   if (mTextureSource) {
     mTextureSource->SetCompositor(glCompositor);
   }
 }
 
 gfx::SurfaceFormat
-SharedTextureHostOGL::GetFormat() const
+SurfaceTextureHost::GetFormat() const
+{
+  MOZ_ASSERT(mTextureSource);
+  return mTextureSource->GetFormat();
+}
+
+#endif // MOZ_WIDGET_ANDROID
+
+////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////
+// EGLImage
+
+EGLImageTextureSource::EGLImageTextureSource(CompositorOGL* aCompositor,
+                                             EGLImage aImage,
+                                             gfx::SurfaceFormat aFormat,
+                                             GLenum aTarget,
+                                             GLenum aWrapMode,
+                                             gfx::IntSize aSize)
+  : mCompositor(aCompositor)
+  , mImage(aImage)
+  , mFormat(aFormat)
+  , mTextureTarget(aTarget)
+  , mWrapMode(aWrapMode)
+  , mSize(aSize)
+{
+}
+
+void
+EGLImageTextureSource::BindTexture(GLenum aTextureUnit, gfx::Filter aFilter)
+{
+  if (!gl()) {
+    NS_WARNING("Trying to bind a texture without a GLContext");
+    return;
+  }
+
+  MOZ_ASSERT(DoesEGLContextSupportSharingWithEGLImage(gl()),
+             "EGLImage not supported or disabled in runtime");
+
+  GLuint tex = mCompositor->GetTemporaryTexture(GetTextureTarget(), aTextureUnit);
+
+  gl()->fActiveTexture(aTextureUnit);
+  gl()->fBindTexture(mTextureTarget, tex);
+
+  MOZ_ASSERT(mTextureTarget == LOCAL_GL_TEXTURE_2D);
+  gl()->fEGLImageTargetTexture2D(LOCAL_GL_TEXTURE_2D, mImage);
+
+  ApplyFilterToBoundTexture(gl(), aFilter, mTextureTarget);
+}
+
+void
+EGLImageTextureSource::SetCompositor(Compositor* aCompositor)
+{
+  mCompositor = static_cast<CompositorOGL*>(aCompositor);
+}
+
+bool
+EGLImageTextureSource::IsValid() const
+{
+  return !!gl();
+}
+
+gl::GLContext*
+EGLImageTextureSource::gl() const
+{
+  return mCompositor ? mCompositor->gl() : nullptr;
+}
+
+gfx::Matrix4x4
+EGLImageTextureSource::GetTextureTransform()
+{
+  gfx::Matrix4x4 ret;
+  return ret;
+}
+
+////////////////////////////////////////////////////////////////////////
+
+EGLImageTextureHost::EGLImageTextureHost(TextureFlags aFlags,
+                                         EGLImage aImage,
+                                         gfx::IntSize aSize)
+  : TextureHost(aFlags)
+  , mImage(aImage)
+  , mSize(aSize)
+  , mCompositor(nullptr)
+{
+}
+
+EGLImageTextureHost::~EGLImageTextureHost()
+{
+}
+
+gl::GLContext*
+EGLImageTextureHost::gl() const
+{
+  return mCompositor ? mCompositor->gl() : nullptr;
+}
+
+bool
+EGLImageTextureHost::Lock()
+{
+  if (!mCompositor) {
+    return false;
+  }
+
+  if (!mTextureSource) {
+    gfx::SurfaceFormat format = gfx::SurfaceFormat::R8G8B8A8;
+    GLenum target = LOCAL_GL_TEXTURE_2D;
+    GLenum wrapMode = LOCAL_GL_CLAMP_TO_EDGE;
+    mTextureSource = new EGLImageTextureSource(mCompositor,
+                                               mImage,
+                                               format,
+                                               target,
+                                               wrapMode,
+                                               mSize);
+  }
+
+  return true;
+}
+
+void
+EGLImageTextureHost::Unlock()
+{
+}
+
+void
+EGLImageTextureHost::SetCompositor(Compositor* aCompositor)
+{
+  CompositorOGL* glCompositor = static_cast<CompositorOGL*>(aCompositor);
+  mCompositor = glCompositor;
+  if (mTextureSource) {
+    mTextureSource->SetCompositor(glCompositor);
+  }
+}
+
+gfx::SurfaceFormat
+EGLImageTextureHost::GetFormat() const
 {
   MOZ_ASSERT(mTextureSource);
   return mTextureSource->GetFormat();
 }
 
 } // namespace
 } // namespace
--- a/gfx/layers/opengl/TextureHostOGL.h
+++ b/gfx/layers/opengl/TextureHostOGL.h
@@ -33,16 +33,17 @@
 #include <ui/GraphicBuffer.h>
 #if ANDROID_VERSION >= 17
 #include <ui/Fence.h>
 #endif
 #endif
 
 class gfxReusableSurfaceWrapper;
 class nsIntRegion;
+class nsSurfaceTexture;
 struct nsIntPoint;
 struct nsIntRect;
 struct nsIntSize;
 
 namespace mozilla {
 namespace gfx {
 class DataSourceSurface;
 }
@@ -289,138 +290,27 @@ public:
 protected:
   nsRefPtr<gl::TextureImage> mTexImage;
   gl::GLContext* mGL;
   TextureFlags mFlags;
   bool mIterating;
 };
 
 /**
- * A texture source meant for use with SharedTextureHostOGL.
- *
- * It does not own any GL texture, and attaches its shared handle to one of
- * the compositor's temporary textures when binding.
- *
- * The shared texture handle is owned by the TextureHost.
- */
-class SharedTextureSourceOGL : public NewTextureSource
-                             , public TextureSourceOGL
-{
-public:
-  typedef gl::SharedTextureShareType SharedTextureShareType;
-
-  SharedTextureSourceOGL(CompositorOGL* aCompositor,
-                         gl::SharedTextureHandle aHandle,
-                         gfx::SurfaceFormat aFormat,
-                         GLenum aTarget,
-                         GLenum aWrapMode,
-                         SharedTextureShareType aShareType,
-                         gfx::IntSize aSize);
-
-  virtual TextureSourceOGL* AsSourceOGL() { return this; }
-
-  virtual void BindTexture(GLenum activetex, gfx::Filter aFilter) MOZ_OVERRIDE;
-
-  virtual bool IsValid() const MOZ_OVERRIDE;
-
-  virtual gfx::IntSize GetSize() const MOZ_OVERRIDE { return mSize; }
-
-  virtual gfx::SurfaceFormat GetFormat() const MOZ_OVERRIDE { return mFormat; }
-
-  virtual gfx::Matrix4x4 GetTextureTransform() MOZ_OVERRIDE;
-
-  virtual GLenum GetTextureTarget() const { return mTextureTarget; }
-
-  virtual GLenum GetWrapMode() const MOZ_OVERRIDE { return mWrapMode; }
-
-  // SharedTextureSource doesn't own any gl texture
-  virtual void DeallocateDeviceData() MOZ_OVERRIDE {}
-
-  void DetachSharedHandle();
-
-  virtual void SetCompositor(Compositor* aCompositor) MOZ_OVERRIDE;
-
-  gl::GLContext* gl() const;
-
-protected:
-  gfx::IntSize mSize;
-  CompositorOGL* mCompositor;
-  gl::SharedTextureHandle mSharedHandle;
-  gfx::SurfaceFormat mFormat;
-  SharedTextureShareType mShareType;
-  GLenum mTextureTarget;
-  GLenum mWrapMode;
-};
-
-/**
- * A TextureHost for shared GL Textures
- *
- * Most of the logic actually happens in SharedTextureSourceOGL.
- */
-class SharedTextureHostOGL : public TextureHost
-{
-public:
-  SharedTextureHostOGL(TextureFlags aFlags,
-                       gl::SharedTextureShareType aShareType,
-                       gl::SharedTextureHandle aSharedhandle,
-                       gfx::IntSize aSize,
-                       bool inverted);
-
-  virtual ~SharedTextureHostOGL();
-
-  // SharedTextureHostOGL doesn't own any GL texture
-  virtual void DeallocateDeviceData() MOZ_OVERRIDE {}
-
-  virtual void SetCompositor(Compositor* aCompositor) MOZ_OVERRIDE;
-
-  virtual bool Lock() MOZ_OVERRIDE;
-
-  virtual void Unlock() MOZ_OVERRIDE;
-
-  virtual gfx::SurfaceFormat GetFormat() const MOZ_OVERRIDE;
-
-  virtual NewTextureSource* GetTextureSources() MOZ_OVERRIDE
-  {
-    return mTextureSource;
-  }
-
-  virtual TemporaryRef<gfx::DataSourceSurface> GetAsSurface() MOZ_OVERRIDE
-  {
-    return nullptr; // XXX - implement this (for MOZ_DUMP_PAINTING)
-  }
-
-  gl::GLContext* gl() const;
-
-  virtual gfx::IntSize GetSize() const MOZ_OVERRIDE { return mSize; }
-
-  virtual const char* Name() { return "SharedTextureHostOGL"; }
-
-protected:
-  gfx::IntSize mSize;
-  CompositorOGL* mCompositor;
-  gl::SharedTextureHandle mSharedHandle;
-  gl::SharedTextureShareType mShareType;
-
-  RefPtr<SharedTextureSourceOGL> mTextureSource;
-};
-
-/**
- * A texture source meant for use with SharedTextureHostOGL.
+ * A texture source for GL textures.
  *
  * It does not own any GL texture, and attaches its shared handle to one of
  * the compositor's temporary textures when binding.
  *
  * The shared texture handle is owned by the TextureHost.
  */
 class GLTextureSource : public NewTextureSource
                       , public TextureSourceOGL
 {
 public:
-  typedef gl::SharedTextureShareType SharedTextureShareType;
-
   GLTextureSource(CompositorOGL* aCompositor,
                   GLuint aTex,
                   gfx::SurfaceFormat aFormat,
                   GLenum aTarget,
                   gfx::IntSize aSize);
 
   virtual TextureSourceOGL* AsSourceOGL() MOZ_OVERRIDE { return this; }
 
@@ -445,12 +335,194 @@ public:
 protected:
   const gfx::IntSize mSize;
   CompositorOGL* mCompositor;
   const GLuint mTex;
   const gfx::SurfaceFormat mFormat;
   const GLenum mTextureTarget;
 };
 
+////////////////////////////////////////////////////////////////////////
+// SurfaceTexture
+
+#ifdef MOZ_WIDGET_ANDROID
+
+class SurfaceTextureSource : public NewTextureSource
+                           , public TextureSourceOGL
+{
+public:
+  SurfaceTextureSource(CompositorOGL* aCompositor,
+                       nsSurfaceTexture* aSurfTex,
+                       gfx::SurfaceFormat aFormat,
+                       GLenum aTarget,
+                       GLenum aWrapMode,
+                       gfx::IntSize aSize);
+
+  virtual TextureSourceOGL* AsSourceOGL() { return this; }
+
+  virtual void BindTexture(GLenum activetex, gfx::Filter aFilter) MOZ_OVERRIDE;
+
+  virtual bool IsValid() const MOZ_OVERRIDE;
+
+  virtual gfx::IntSize GetSize() const MOZ_OVERRIDE { return mSize; }
+
+  virtual gfx::SurfaceFormat GetFormat() const MOZ_OVERRIDE { return mFormat; }
+
+  virtual gfx::Matrix4x4 GetTextureTransform() MOZ_OVERRIDE;
+
+  virtual GLenum GetTextureTarget() const { return mTextureTarget; }
+
+  virtual GLenum GetWrapMode() const MOZ_OVERRIDE { return mWrapMode; }
+
+  // We don't own anything.
+  virtual void DeallocateDeviceData() MOZ_OVERRIDE {}
+
+  virtual void SetCompositor(Compositor* aCompositor) MOZ_OVERRIDE;
+
+  gl::GLContext* gl() const;
+
+protected:
+  CompositorOGL* mCompositor;
+  nsSurfaceTexture* const mSurfTex;
+  const gfx::SurfaceFormat mFormat;
+  const GLenum mTextureTarget;
+  const GLenum mWrapMode;
+  const gfx::IntSize mSize;
+};
+
+class SurfaceTextureHost : public TextureHost
+{
+public:
+  SurfaceTextureHost(TextureFlags aFlags,
+                     nsSurfaceTexture* aSurfTex,
+                     gfx::IntSize aSize);
+
+  virtual ~SurfaceTextureHost();
+
+  // We don't own anything.
+  virtual void DeallocateDeviceData() MOZ_OVERRIDE {}
+
+  virtual void SetCompositor(Compositor* aCompositor) MOZ_OVERRIDE;
+
+  virtual bool Lock() MOZ_OVERRIDE;
+
+  virtual void Unlock() MOZ_OVERRIDE;
+
+  virtual gfx::SurfaceFormat GetFormat() const MOZ_OVERRIDE;
+
+  virtual NewTextureSource* GetTextureSources() MOZ_OVERRIDE
+  {
+    return mTextureSource;
+  }
+
+  virtual TemporaryRef<gfx::DataSourceSurface> GetAsSurface() MOZ_OVERRIDE
+  {
+    return nullptr; // XXX - implement this (for MOZ_DUMP_PAINTING)
+  }
+
+  gl::GLContext* gl() const;
+
+  virtual gfx::IntSize GetSize() const MOZ_OVERRIDE { return mSize; }
+
+  virtual const char* Name() { return "SurfaceTextureHost"; }
+
+protected:
+  nsSurfaceTexture* const mSurfTex;
+  const gfx::IntSize mSize;
+  CompositorOGL* mCompositor;
+  RefPtr<SurfaceTextureSource> mTextureSource;
+};
+
+#endif // MOZ_WIDGET_ANDROID
+
+////////////////////////////////////////////////////////////////////////
+// EGLImage
+
+class EGLImageTextureSource : public NewTextureSource
+                            , public TextureSourceOGL
+{
+public:
+  EGLImageTextureSource(CompositorOGL* aCompositor,
+                        EGLImage aImage,
+                        gfx::SurfaceFormat aFormat,
+                        GLenum aTarget,
+                        GLenum aWrapMode,
+                        gfx::IntSize aSize);
+
+  virtual TextureSourceOGL* AsSourceOGL() { return this; }
+
+  virtual void BindTexture(GLenum activetex, gfx::Filter aFilter) MOZ_OVERRIDE;
+
+  virtual bool IsValid() const MOZ_OVERRIDE;
+
+  virtual gfx::IntSize GetSize() const MOZ_OVERRIDE { return mSize; }
+
+  virtual gfx::SurfaceFormat GetFormat() const MOZ_OVERRIDE { return mFormat; }
+
+  virtual gfx::Matrix4x4 GetTextureTransform() MOZ_OVERRIDE;
+
+  virtual GLenum GetTextureTarget() const { return mTextureTarget; }
+
+  virtual GLenum GetWrapMode() const MOZ_OVERRIDE { return mWrapMode; }
+
+  // We don't own anything.
+  virtual void DeallocateDeviceData() MOZ_OVERRIDE {}
+
+  virtual void SetCompositor(Compositor* aCompositor) MOZ_OVERRIDE;
+
+  gl::GLContext* gl() const;
+
+protected:
+  CompositorOGL* mCompositor;
+  const EGLImage mImage;
+  const gfx::SurfaceFormat mFormat;
+  const GLenum mTextureTarget;
+  const GLenum mWrapMode;
+  const gfx::IntSize mSize;
+};
+
+class EGLImageTextureHost : public TextureHost
+{
+public:
+  EGLImageTextureHost(TextureFlags aFlags,
+                     EGLImage aImage,
+                     gfx::IntSize aSize);
+
+  virtual ~EGLImageTextureHost();
+
+  // We don't own anything.
+  virtual void DeallocateDeviceData() MOZ_OVERRIDE {}
+
+  virtual void SetCompositor(Compositor* aCompositor) MOZ_OVERRIDE;
+
+  virtual bool Lock() MOZ_OVERRIDE;
+
+  virtual void Unlock() MOZ_OVERRIDE;
+
+  virtual gfx::SurfaceFormat GetFormat() const MOZ_OVERRIDE;
+
+  virtual NewTextureSource* GetTextureSources() MOZ_OVERRIDE
+  {
+    return mTextureSource;
+  }
+
+  virtual TemporaryRef<gfx::DataSourceSurface> GetAsSurface() MOZ_OVERRIDE
+  {
+    return nullptr; // XXX - implement this (for MOZ_DUMP_PAINTING)
+  }
+
+  gl::GLContext* gl() const;
+
+  virtual gfx::IntSize GetSize() const MOZ_OVERRIDE { return mSize; }
+
+  virtual const char* Name() { return "EGLImageTextureHost"; }
+
+protected:
+  const EGLImage mImage;
+  const gfx::IntSize mSize;
+  CompositorOGL* mCompositor;
+  RefPtr<EGLImageTextureSource> mTextureSource;
+};
+
 } // namespace
 } // namespace
 
 #endif /* MOZILLA_GFX_TEXTUREOGL_H */