2013-07-18 03:24:15 +00:00
|
|
|
/* -*- Mode: c++; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 40; -*- */
|
|
|
|
/* 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 "SharedSurfaceIO.h"
|
2014-07-11 22:10:49 +00:00
|
|
|
|
2014-01-07 20:02:18 +00:00
|
|
|
#include "GLContextCGL.h"
|
2015-06-04 23:50:59 +00:00
|
|
|
#include "mozilla/DebugOnly.h"
|
2015-06-05 00:15:38 +00:00
|
|
|
#include "mozilla/gfx/MacIOSurface.h"
|
|
|
|
#include "mozilla/layers/LayersSurfaces.h" // for SurfaceDescriptor, etc
|
2013-11-26 03:25:25 +00:00
|
|
|
#include "ScopedGLHelpers.h"
|
2013-07-18 03:24:15 +00:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace gl {
|
|
|
|
|
2014-08-16 00:38:08 +00:00
|
|
|
/*static*/ UniquePtr<SharedSurface_IOSurface>
|
|
|
|
SharedSurface_IOSurface::Create(const RefPtr<MacIOSurface>& ioSurf,
|
|
|
|
GLContext* gl,
|
|
|
|
bool hasAlpha)
|
2013-07-18 03:24:15 +00:00
|
|
|
{
|
2014-08-16 00:38:08 +00:00
|
|
|
MOZ_ASSERT(ioSurf);
|
2013-12-03 03:11:27 +00:00
|
|
|
MOZ_ASSERT(gl);
|
|
|
|
|
2014-08-16 00:38:08 +00:00
|
|
|
gfx::IntSize size(ioSurf->GetWidth(), ioSurf->GetHeight());
|
2014-08-16 00:38:08 +00:00
|
|
|
|
|
|
|
typedef SharedSurface_IOSurface ptrT;
|
2014-08-16 00:38:08 +00:00
|
|
|
UniquePtr<ptrT> ret( new ptrT(ioSurf, gl, size, hasAlpha) );
|
|
|
|
return Move(ret);
|
2013-07-18 03:24:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SharedSurface_IOSurface::Fence()
|
|
|
|
{
|
|
|
|
mGL->MakeCurrent();
|
|
|
|
mGL->fFlush();
|
|
|
|
}
|
|
|
|
|
2015-03-23 03:55:26 +00:00
|
|
|
bool
|
|
|
|
SharedSurface_IOSurface::CopyTexImage2D(GLenum target, GLint level, GLenum internalformat,
|
|
|
|
GLint x, GLint y, GLsizei width, GLsizei height,
|
|
|
|
GLint border)
|
|
|
|
{
|
|
|
|
/* Bug 896693 - OpenGL framebuffers that are backed by IOSurface on OSX expose a bug
|
|
|
|
* in glCopyTexImage2D --- internalformats GL_ALPHA, GL_LUMINANCE, GL_LUMINANCE_ALPHA
|
|
|
|
* return the wrong results. To work around, copy framebuffer to a temporary texture
|
|
|
|
* using GL_RGBA (which works), attach as read framebuffer and glCopyTexImage2D
|
|
|
|
* instead.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// https://www.opengl.org/sdk/docs/man3/xhtml/glCopyTexImage2D.xml says that width or
|
|
|
|
// height set to 0 results in a NULL texture. Lets not do any work and punt to
|
|
|
|
// original glCopyTexImage2D, since the FBO below will fail when trying to attach a
|
|
|
|
// texture of 0 width or height.
|
|
|
|
if (width == 0 || height == 0)
|
|
|
|
return false;
|
|
|
|
|
2015-04-03 00:50:27 +00:00
|
|
|
switch (internalformat) {
|
|
|
|
case LOCAL_GL_ALPHA:
|
|
|
|
case LOCAL_GL_LUMINANCE:
|
|
|
|
case LOCAL_GL_LUMINANCE_ALPHA:
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-03-23 03:55:26 +00:00
|
|
|
MOZ_ASSERT(mGL->IsCurrent());
|
|
|
|
|
|
|
|
ScopedTexture destTex(mGL);
|
|
|
|
{
|
|
|
|
ScopedBindTexture bindTex(mGL, destTex.Texture());
|
|
|
|
mGL->fTexParameteri(LOCAL_GL_TEXTURE_2D, LOCAL_GL_TEXTURE_MIN_FILTER,
|
|
|
|
LOCAL_GL_NEAREST);
|
|
|
|
mGL->fTexParameteri(LOCAL_GL_TEXTURE_2D, LOCAL_GL_TEXTURE_MAG_FILTER,
|
|
|
|
LOCAL_GL_NEAREST);
|
|
|
|
mGL->fTexParameteri(LOCAL_GL_TEXTURE_2D, LOCAL_GL_TEXTURE_WRAP_S,
|
|
|
|
LOCAL_GL_CLAMP_TO_EDGE);
|
|
|
|
mGL->fTexParameteri(LOCAL_GL_TEXTURE_2D, LOCAL_GL_TEXTURE_WRAP_T,
|
|
|
|
LOCAL_GL_CLAMP_TO_EDGE);
|
|
|
|
mGL->raw_fCopyTexImage2D(LOCAL_GL_TEXTURE_2D, 0, LOCAL_GL_RGBA, x, y, width,
|
|
|
|
height, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
ScopedFramebufferForTexture tmpFB(mGL, destTex.Texture(), LOCAL_GL_TEXTURE_2D);
|
|
|
|
ScopedBindFramebuffer bindFB(mGL, tmpFB.FB());
|
|
|
|
mGL->raw_fCopyTexImage2D(target, level, internalformat, x, y, width, height, border);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-07-18 03:24:15 +00:00
|
|
|
bool
|
|
|
|
SharedSurface_IOSurface::ReadPixels(GLint x, GLint y, GLsizei width, GLsizei height,
|
2014-07-11 22:10:49 +00:00
|
|
|
GLenum format, GLenum type, GLvoid* pixels)
|
2013-07-18 03:24:15 +00:00
|
|
|
{
|
|
|
|
// Calling glReadPixels when an IOSurface is bound to the current framebuffer
|
|
|
|
// can cause corruption in following glReadPixel calls (even if they aren't
|
|
|
|
// reading from an IOSurface).
|
|
|
|
// We workaround this by copying to a temporary texture, and doing the readback
|
|
|
|
// from that.
|
2014-03-11 02:08:50 +00:00
|
|
|
MOZ_ASSERT(mGL->IsCurrent());
|
2013-07-18 03:24:15 +00:00
|
|
|
|
2014-03-11 02:08:50 +00:00
|
|
|
ScopedTexture destTex(mGL);
|
|
|
|
{
|
|
|
|
ScopedFramebufferForTexture srcFB(mGL, ProdTexture(), ProdTextureTarget());
|
|
|
|
|
|
|
|
ScopedBindFramebuffer bindFB(mGL, srcFB.FB());
|
|
|
|
ScopedBindTexture bindTex(mGL, destTex.Texture());
|
2015-04-03 00:50:27 +00:00
|
|
|
mGL->raw_fCopyTexImage2D(LOCAL_GL_TEXTURE_2D, 0,
|
|
|
|
mHasAlpha ? LOCAL_GL_RGBA : LOCAL_GL_RGB,
|
|
|
|
x, y,
|
|
|
|
width, height, 0);
|
2014-03-11 02:08:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ScopedFramebufferForTexture destFB(mGL, destTex.Texture());
|
|
|
|
|
|
|
|
ScopedBindFramebuffer bindFB(mGL, destFB.FB());
|
2015-04-03 00:50:27 +00:00
|
|
|
mGL->raw_fReadPixels(0, 0, width, height, format, type, pixels);
|
2013-07-18 03:24:15 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-03-11 02:08:50 +00:00
|
|
|
static void
|
|
|
|
BackTextureWithIOSurf(GLContext* gl, GLuint tex, MacIOSurface* ioSurf)
|
2013-07-18 03:24:15 +00:00
|
|
|
{
|
2014-03-11 02:08:50 +00:00
|
|
|
MOZ_ASSERT(gl->IsCurrent());
|
2013-07-18 03:24:15 +00:00
|
|
|
|
2014-03-11 02:08:50 +00:00
|
|
|
ScopedBindTexture texture(gl, tex, LOCAL_GL_TEXTURE_RECTANGLE_ARB);
|
2013-07-18 03:24:15 +00:00
|
|
|
|
2014-03-11 02:08:50 +00:00
|
|
|
gl->fTexParameteri(LOCAL_GL_TEXTURE_RECTANGLE_ARB,
|
2013-07-18 03:24:15 +00:00
|
|
|
LOCAL_GL_TEXTURE_MIN_FILTER,
|
|
|
|
LOCAL_GL_LINEAR);
|
2014-03-11 02:08:50 +00:00
|
|
|
gl->fTexParameteri(LOCAL_GL_TEXTURE_RECTANGLE_ARB,
|
2013-07-18 03:24:15 +00:00
|
|
|
LOCAL_GL_TEXTURE_MAG_FILTER,
|
|
|
|
LOCAL_GL_LINEAR);
|
2014-03-11 02:08:50 +00:00
|
|
|
gl->fTexParameteri(LOCAL_GL_TEXTURE_RECTANGLE_ARB,
|
2013-07-18 03:24:15 +00:00
|
|
|
LOCAL_GL_TEXTURE_WRAP_S,
|
|
|
|
LOCAL_GL_CLAMP_TO_EDGE);
|
2014-03-11 02:08:50 +00:00
|
|
|
gl->fTexParameteri(LOCAL_GL_TEXTURE_RECTANGLE_ARB,
|
2013-07-18 03:24:15 +00:00
|
|
|
LOCAL_GL_TEXTURE_WRAP_T,
|
|
|
|
LOCAL_GL_CLAMP_TO_EDGE);
|
|
|
|
|
2014-03-11 02:08:50 +00:00
|
|
|
CGLContextObj cgl = GLContextCGL::Cast(gl)->GetCGLContext();
|
|
|
|
MOZ_ASSERT(cgl);
|
|
|
|
|
|
|
|
ioSurf->CGLTexImageIOSurface2D(cgl);
|
|
|
|
}
|
|
|
|
|
2014-08-16 00:38:08 +00:00
|
|
|
SharedSurface_IOSurface::SharedSurface_IOSurface(const RefPtr<MacIOSurface>& ioSurf,
|
2014-03-11 02:08:50 +00:00
|
|
|
GLContext* gl,
|
|
|
|
const gfx::IntSize& size,
|
|
|
|
bool hasAlpha)
|
2014-07-11 22:10:49 +00:00
|
|
|
: SharedSurface(SharedSurfaceType::IOSurface,
|
|
|
|
AttachmentType::GLTexture,
|
|
|
|
gl,
|
|
|
|
size,
|
2015-06-05 00:15:38 +00:00
|
|
|
hasAlpha,
|
|
|
|
true)
|
2014-08-16 00:38:08 +00:00
|
|
|
, mIOSurf(ioSurf)
|
2014-03-11 02:08:50 +00:00
|
|
|
{
|
|
|
|
gl->MakeCurrent();
|
|
|
|
mProdTex = 0;
|
|
|
|
gl->fGenTextures(1, &mProdTex);
|
2014-08-16 00:38:08 +00:00
|
|
|
BackTextureWithIOSurf(gl, mProdTex, mIOSurf);
|
2014-03-11 02:08:50 +00:00
|
|
|
}
|
|
|
|
|
2013-07-18 03:24:15 +00:00
|
|
|
SharedSurface_IOSurface::~SharedSurface_IOSurface()
|
|
|
|
{
|
2014-03-11 02:08:50 +00:00
|
|
|
if (mProdTex) {
|
2013-07-18 03:24:15 +00:00
|
|
|
DebugOnly<bool> success = mGL->MakeCurrent();
|
|
|
|
MOZ_ASSERT(success);
|
2014-03-11 02:08:50 +00:00
|
|
|
mGL->fDeleteTextures(1, &mProdTex);
|
2013-07-18 03:24:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-05 00:15:38 +00:00
|
|
|
bool
|
|
|
|
SharedSurface_IOSurface::ToSurfaceDescriptor(layers::SurfaceDescriptor* const out_descriptor)
|
|
|
|
{
|
|
|
|
bool isOpaque = !mHasAlpha;
|
|
|
|
*out_descriptor = layers::SurfaceDescriptorMacIOSurface(mIOSurf->GetIOSurfaceID(),
|
|
|
|
mIOSurf->GetContentsScaleFactor(),
|
|
|
|
isOpaque);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-08-16 00:38:08 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
// SurfaceFactory_IOSurface
|
2014-07-12 06:08:54 +00:00
|
|
|
|
2014-08-16 00:38:08 +00:00
|
|
|
/*static*/ UniquePtr<SurfaceFactory_IOSurface>
|
2015-06-05 00:15:38 +00:00
|
|
|
SurfaceFactory_IOSurface::Create(GLContext* gl, const SurfaceCaps& caps,
|
|
|
|
const RefPtr<layers::ISurfaceAllocator>& allocator,
|
|
|
|
const layers::TextureFlags& flags)
|
2014-07-12 06:08:54 +00:00
|
|
|
{
|
|
|
|
gfx::IntSize maxDims(MacIOSurface::GetMaxWidth(),
|
|
|
|
MacIOSurface::GetMaxHeight());
|
2014-08-16 00:38:08 +00:00
|
|
|
|
|
|
|
typedef SurfaceFactory_IOSurface ptrT;
|
2015-06-05 00:15:38 +00:00
|
|
|
UniquePtr<ptrT> ret( new ptrT(gl, caps, allocator, flags, maxDims) );
|
2014-08-16 00:38:08 +00:00
|
|
|
return Move(ret);
|
2014-07-12 06:08:54 +00:00
|
|
|
}
|
|
|
|
|
2014-08-16 00:38:08 +00:00
|
|
|
UniquePtr<SharedSurface>
|
2013-12-10 16:11:58 +00:00
|
|
|
SurfaceFactory_IOSurface::CreateShared(const gfx::IntSize& size)
|
2013-07-18 03:24:15 +00:00
|
|
|
{
|
2014-07-12 06:08:54 +00:00
|
|
|
if (size.width > mMaxDims.width ||
|
|
|
|
size.height > mMaxDims.height)
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2013-07-18 03:24:15 +00:00
|
|
|
bool hasAlpha = mReadCaps.alpha;
|
2014-08-16 00:38:08 +00:00
|
|
|
RefPtr<MacIOSurface> ioSurf;
|
|
|
|
ioSurf = MacIOSurface::CreateIOSurface(size.width, size.height, 1.0,
|
|
|
|
hasAlpha);
|
2013-07-18 03:24:15 +00:00
|
|
|
|
2014-08-16 00:38:08 +00:00
|
|
|
if (!ioSurf) {
|
2013-12-03 03:11:27 +00:00
|
|
|
NS_WARNING("Failed to create MacIOSurface.");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-08-16 00:38:08 +00:00
|
|
|
return SharedSurface_IOSurface::Create(ioSurf, mGL, hasAlpha);
|
2013-07-18 03:24:15 +00:00
|
|
|
}
|
|
|
|
|
2015-07-13 15:25:42 +00:00
|
|
|
} // namespace gl
|
|
|
|
} // namespace mozilla
|