Skip to content

Commit

Permalink
Bug 956993 - Rely on OES_EGL_image_external for SharedSurface_EGLImag…
Browse files Browse the repository at this point in the history
…e r=jgilbert
  • Loading branch information
snorp authored and tmeshkova committed Mar 6, 2014
1 parent 41e1893 commit f3ec04a
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 145 deletions.
6 changes: 3 additions & 3 deletions embedding/embedlite/embedthread/EmbedLiteViewThreadParent.cpp
Expand Up @@ -729,13 +729,13 @@ bool EmbedLiteViewThreadParent::GetPendingTexture(EmbedLiteRenderTarget* aContex
NS_ENSURE_TRUE(sharedSurf, false);

DataSourceSurface* toUpload = nullptr;
GLint textureHandle = 0;
GLuint textureHandle = 0;
GLuint textureTarget = 0;
if (sharedSurf->Type() == SharedSurfaceType::EGLImageShare) {
SharedSurface_EGLImage* eglImageSurf = SharedSurface_EGLImage::Cast(sharedSurf);
textureHandle = eglImageSurf->AcquireConsumerTexture(consumerContext);
eglImageSurf->AcquireConsumerTexture(consumerContext, &textureHandle, &textureTarget);
if (!textureHandle) {
NS_WARNING("Failed to get texture handle, fallback to pixels?");
toUpload = eglImageSurf->GetPixels();
}
} else if (sharedSurf->Type() == SharedSurfaceType::GLTextureShare) {
SharedSurface_GLTexture* glTexSurf = SharedSurface_GLTexture::Cast(sharedSurf);
Expand Down
1 change: 1 addition & 0 deletions gfx/gl/ScopedGLHelpers.cpp
Expand Up @@ -124,6 +124,7 @@ ScopedBindTexture::Init(GLenum aTarget)
GLenum bindingTarget = (aTarget == LOCAL_GL_TEXTURE_2D) ? LOCAL_GL_TEXTURE_BINDING_2D
: (aTarget == LOCAL_GL_TEXTURE_RECTANGLE_ARB) ? LOCAL_GL_TEXTURE_BINDING_RECTANGLE_ARB
: (aTarget == LOCAL_GL_TEXTURE_CUBE_MAP) ? LOCAL_GL_TEXTURE_BINDING_CUBE_MAP
: (aTarget == LOCAL_GL_TEXTURE_EXTERNAL) ? LOCAL_GL_TEXTURE_EXTERNAL
: LOCAL_GL_NONE;
MOZ_ASSERT(bindingTarget != LOCAL_GL_NONE);
mGL->GetUIntegerv(bindingTarget, &mOldTex);
Expand Down
151 changes: 32 additions & 119 deletions gfx/gl/SharedSurfaceEGL.cpp
Expand Up @@ -27,18 +27,30 @@ SharedSurface_EGLImage::Create(GLContext* prodGL,
{
GLLibraryEGL* egl = &sEGLLibrary;
MOZ_ASSERT(egl);
MOZ_ASSERT(context);

if (!HasExtensions(egl, prodGL))
if (!HasExtensions(egl, prodGL)) {
return nullptr;
}

MOZ_ALWAYS_TRUE(prodGL->MakeCurrent());
GLuint prodTex = CreateTextureForOffscreen(prodGL, formats, size);
if (!prodTex)
if (!prodTex) {
return nullptr;
}

EGLClientBuffer buffer = reinterpret_cast<EGLClientBuffer>(prodTex);
EGLImage image = egl->fCreateImage(egl->Display(), context,
LOCAL_EGL_GL_TEXTURE_2D, buffer,
nullptr);
if (!image) {
prodGL->fDeleteTextures(1, &prodTex);
return nullptr;
}

return new SharedSurface_EGLImage(prodGL, egl,
size, hasAlpha,
formats, prodTex);
formats, prodTex, image);
}


Expand All @@ -47,15 +59,16 @@ SharedSurface_EGLImage::HasExtensions(GLLibraryEGL* egl, GLContext* gl)
{
return egl->HasKHRImageBase() &&
egl->IsExtensionSupported(GLLibraryEGL::KHR_gl_texture_2D_image) &&
gl->IsExtensionSupported(GLContext::OES_EGL_image);
gl->IsExtensionSupported(GLContext::OES_EGL_image_external);
}

SharedSurface_EGLImage::SharedSurface_EGLImage(GLContext* gl,
GLLibraryEGL* egl,
const gfx::IntSize& size,
bool hasAlpha,
const GLFormats& formats,
GLuint prodTex)
GLuint prodTex,
EGLImage image)
: SharedSurface_GL(SharedSurfaceType::EGLImageShare,
AttachmentType::GLTexture,
gl,
Expand All @@ -65,14 +78,10 @@ SharedSurface_EGLImage::SharedSurface_EGLImage(GLContext* gl,
, mEGL(egl)
, mFormats(formats)
, mProdTex(prodTex)
, mProdTexForPipe(0)
, mImage(0)
, mImage(image)
, mCurConsGL(nullptr)
, mConsTex(0)
, mSync(0)
, mPipeFailed(false)
, mPipeComplete(false)
, mPipeActive(false)
{}

SharedSurface_EGLImage::~SharedSurface_EGLImage()
Expand All @@ -84,11 +93,6 @@ SharedSurface_EGLImage::~SharedSurface_EGLImage()
mGL->fDeleteTextures(1, &mProdTex);
mProdTex = 0;

if (mProdTexForPipe) {
mGL->fDeleteTextures(1, &mProdTexForPipe);
mProdTexForPipe = 0;
}

if (mConsTex) {
MOZ_ASSERT(mGarbageBin);
mGarbageBin->Trash(mConsTex);
Expand All @@ -103,94 +107,12 @@ SharedSurface_EGLImage::~SharedSurface_EGLImage()
}
}

void
SharedSurface_EGLImage::LockProdImpl()
{
MutexAutoLock lock(mMutex);

if (!mPipeComplete)
return;

if (mPipeActive)
return;

mGL->BlitHelper()->BlitTextureToTexture(mProdTex, mProdTexForPipe, Size(), Size());
mGL->fDeleteTextures(1, &mProdTex);
mProdTex = mProdTexForPipe;
mProdTexForPipe = 0;
mPipeActive = true;
}

static bool
CreateTexturePipe(GLLibraryEGL* const egl, GLContext* const gl,
const GLFormats& formats, const gfx::IntSize& size,
GLuint* const out_tex, EGLImage* const out_image)
{
MOZ_ASSERT(out_tex && out_image);
*out_tex = 0;
*out_image = 0;

GLuint tex = CreateTextureForOffscreen(gl, formats, size);
if (!tex)
return false;

EGLContext context = GLContextEGL::Cast(gl)->GetEGLContext();
MOZ_ASSERT(context);
EGLClientBuffer buffer = reinterpret_cast<EGLClientBuffer>(tex);
EGLImage image = egl->fCreateImage(egl->Display(), context,
LOCAL_EGL_GL_TEXTURE_2D, buffer,
nullptr);
if (!image) {
gl->fDeleteTextures(1, &tex);
return false;
}

// Success.
*out_tex = tex;
*out_image = image;
return true;
}

void
SharedSurface_EGLImage::Fence()
{
MutexAutoLock lock(mMutex);
mGL->MakeCurrent();

if (!mPipeActive) {
MOZ_ASSERT(!mSync);
MOZ_ASSERT(!mPipeComplete);

if (!mPipeFailed) {
if (!CreateTexturePipe(mEGL, mGL, mFormats, Size(),
&mProdTexForPipe, &mImage))
{
mPipeFailed = true;
}
}

if (!mPixels) {
SurfaceFormat format =
HasAlpha() ? SurfaceFormat::B8G8R8A8
: SurfaceFormat::B8G8R8X8;
mPixels = Factory::CreateDataSourceSurface(Size(), format);
}

DataSourceSurface::MappedSurface map;
mPixels->Map(DataSourceSurface::MapType::WRITE, &map);

nsRefPtr<gfxImageSurface> wrappedData =
new gfxImageSurface(map.mData,
ThebesIntSize(mPixels->GetSize()),
map.mStride,
SurfaceFormatToImageFormat(mPixels->GetFormat()));
ReadScreenIntoImageSurface(mGL, wrappedData);
mPixels->Unmap();
return;
}
MOZ_ASSERT(mPipeActive);
MOZ_ASSERT(mCurConsGL);

if (mEGL->IsExtensionSupported(GLLibraryEGL::KHR_fence_sync) &&
mGL->IsExtensionSupported(GLContext::OES_EGL_sync))
{
Expand Down Expand Up @@ -249,49 +171,40 @@ SharedSurface_EGLImage::Display() const
return mEGL->Display();
}

GLuint
SharedSurface_EGLImage::AcquireConsumerTexture(GLContext* consGL)
void
SharedSurface_EGLImage::AcquireConsumerTexture(GLContext* consGL, GLuint* out_texture, GLuint* out_target)
{
MutexAutoLock lock(mMutex);
MOZ_ASSERT(!mCurConsGL || consGL == mCurConsGL);
if (mPipeFailed)
return 0;

if (mPipeActive) {
MOZ_ASSERT(mConsTex);

return mConsTex;
}

if (!mConsTex) {
consGL->fGenTextures(1, &mConsTex);
ScopedBindTexture autoTex(consGL, mConsTex);
consGL->fEGLImageTargetTexture2D(LOCAL_GL_TEXTURE_2D, mImage);
MOZ_ASSERT(mConsTex);

ScopedBindTexture autoTex(consGL, mConsTex, LOCAL_GL_TEXTURE_EXTERNAL);
consGL->fEGLImageTargetTexture2D(LOCAL_GL_TEXTURE_EXTERNAL, mImage);

mPipeComplete = true;
mCurConsGL = consGL;
mGarbageBin = consGL->TexGarbageBin();
}

MOZ_ASSERT(consGL == mCurConsGL);
return 0;
}

DataSourceSurface*
SharedSurface_EGLImage::GetPixels() const
{
MutexAutoLock lock(mMutex);
return mPixels;
*out_texture = mConsTex;
*out_target = LOCAL_GL_TEXTURE_EXTERNAL;
}



SurfaceFactory_EGLImage*
SurfaceFactory_EGLImage::Create(GLContext* prodGL,
const SurfaceCaps& caps)
{
EGLContext context = GLContextEGL::Cast(prodGL)->GetEGLContext();

GLLibraryEGL* egl = &sEGLLibrary;
if (!SharedSurface_EGLImage::HasExtensions(egl, prodGL)) {
return nullptr;
}

return new SurfaceFactory_EGLImage(prodGL, context, caps);
}

Expand Down
22 changes: 8 additions & 14 deletions gfx/gl/SharedSurfaceEGL.h
Expand Up @@ -35,37 +35,34 @@ class SharedSurface_EGLImage
return (SharedSurface_EGLImage*)surf;
}

static bool HasExtensions(GLLibraryEGL* egl, GLContext* gl);

protected:
mutable Mutex mMutex;
GLLibraryEGL* const mEGL;
const GLFormats mFormats;
GLuint mProdTex;
RefPtr<gfx::DataSourceSurface> mPixels;
GLuint mProdTexForPipe; // Moves to mProdTex when mPipeActive becomes true.
EGLImage mImage;
GLContext* mCurConsGL;
GLuint mConsTex;
nsRefPtr<TextureGarbageBin> mGarbageBin;
EGLSync mSync;
bool mPipeFailed; // Pipe creation failed, and has been abandoned.
bool mPipeComplete; // Pipe connects (mPipeActive ? mProdTex : mProdTexForPipe) to mConsTex.
bool mPipeActive; // Pipe is complete and in use for production.

SharedSurface_EGLImage(GLContext* gl,
GLLibraryEGL* egl,
const gfx::IntSize& size,
bool hasAlpha,
const GLFormats& formats,
GLuint prodTex);
GLuint prodTex,
EGLImage image);

EGLDisplay Display() const;

static bool HasExtensions(GLLibraryEGL* egl, GLContext* gl);

public:
virtual ~SharedSurface_EGLImage();

virtual void LockProdImpl();
virtual void LockProdImpl() {}
virtual void UnlockProdImpl() {}


Expand All @@ -78,11 +75,8 @@ class SharedSurface_EGLImage
}

// Implementation-specific functions below:
// Returns 0 if the pipe isn't ready. If 0, use GetPixels below.
GLuint AcquireConsumerTexture(GLContext* consGL);

// Will be void if AcquireConsumerTexture returns non-zero.
gfx::DataSourceSurface* GetPixels() const;
// Returns texture and target
void AcquireConsumerTexture(GLContext* consGL, GLuint* out_texture, GLuint* out_target);
};


Expand All @@ -91,7 +85,7 @@ class SurfaceFactory_EGLImage
: public SurfaceFactory_GL
{
public:
// Infallible:
// Fallible:
static SurfaceFactory_EGLImage* Create(GLContext* prodGL,
const SurfaceCaps& caps);

Expand Down
13 changes: 4 additions & 9 deletions gfx/layers/opengl/TextureHostOGL.cpp
Expand Up @@ -515,15 +515,10 @@ StreamTextureSourceOGL::RetrieveTextureFromStream()
SharedSurface_EGLImage* eglImageSurf =
SharedSurface_EGLImage::Cast(sharedSurf);

mTextureHandle = eglImageSurf->AcquireConsumerTexture(gl());
mTextureTarget = eglImageSurf->TextureTarget();
if (!mTextureHandle) {
toUpload = eglImageSurf->GetPixels();
MOZ_ASSERT(toUpload);
} else {
mFormat = sharedSurf->HasAlpha() ? SurfaceFormat::R8G8B8A8
: SurfaceFormat::R8G8B8X8;
}
eglImageSurf->AcquireConsumerTexture(gl(), &mTextureHandle, &mTextureTarget);
MOZ_ASSERT(mTextureHandle);
mFormat = sharedSurf->HasAlpha() ? SurfaceFormat::R8G8B8A8
: SurfaceFormat::R8G8B8X8;
break;
}
#ifdef XP_MACOSX
Expand Down
17 changes: 17 additions & 0 deletions gfx/thebes/gfxAndroidPlatform.cpp
Expand Up @@ -22,6 +22,10 @@
#include "gfxPrefs.h"
#include "cairo.h"

#ifdef MOZ_WIDGET_ANDROID
#include "AndroidBridge.h"
#endif

#include "ft2build.h"
#include FT_FREETYPE_H
#include FT_MODULE_H
Expand Down Expand Up @@ -418,3 +422,16 @@ gfxAndroidPlatform::GetScreenDepth() const
{
return mScreenDepth;
}

bool
gfxAndroidPlatform::UseAcceleratedSkiaCanvas()
{
#ifdef MOZ_WIDGET_ANDROID
if (AndroidBridge::Bridge()->GetAPIVersion() < 11) {
// It's slower than software due to not having a compositing fast path
return false;
}
#endif

return gfxPlatform::UseAcceleratedSkiaCanvas();
}
2 changes: 2 additions & 0 deletions gfx/thebes/gfxAndroidPlatform.h
Expand Up @@ -81,6 +81,8 @@ class gfxAndroidPlatform : public gfxPlatform {

virtual int GetScreenDepth() const;

virtual bool UseAcceleratedSkiaCanvas() MOZ_OVERRIDE;

private:
int mScreenDepth;
gfxImageFormat mOffscreenFormat;
Expand Down

0 comments on commit f3ec04a

Please sign in to comment.