2013-07-18 07:24:15 +04: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-12 02:10:49 +04:00
|
|
|
|
2014-01-08 00:02:18 +04:00
|
|
|
#include "GLContextCGL.h"
|
2015-06-05 02:50:59 +03:00
|
|
|
#include "mozilla/DebugOnly.h"
|
2015-06-05 03:15:38 +03:00
|
|
|
#include "mozilla/gfx/MacIOSurface.h"
|
|
|
|
#include "mozilla/layers/LayersSurfaces.h" // for SurfaceDescriptor, etc
|
2013-11-26 07:25:25 +04:00
|
|
|
#include "ScopedGLHelpers.h"
|
2013-07-18 07:24:15 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace gl {
|
|
|
|
|
2014-08-16 04:38:08 +04:00
|
|
|
/*static*/ UniquePtr<SharedSurface_IOSurface>
|
2015-10-18 08:24:48 +03:00
|
|
|
SharedSurface_IOSurface::Create(const RefPtr<MacIOSurface>& ioSurf,
|
2014-08-16 04:38:08 +04:00
|
|
|
GLContext* gl,
|
|
|
|
bool hasAlpha)
|
2013-07-18 07:24:15 +04:00
|
|
|
{
|
2014-08-16 04:38:08 +04:00
|
|
|
MOZ_ASSERT(ioSurf);
|
2013-12-03 07:11:27 +04:00
|
|
|
MOZ_ASSERT(gl);
|
|
|
|
|
2016-07-26 17:48:30 +03:00
|
|
|
auto size = gfx::IntSize::Truncate(ioSurf->GetWidth(), ioSurf->GetHeight());
|
2014-08-16 04:38:08 +04:00
|
|
|
|
|
|
|
typedef SharedSurface_IOSurface ptrT;
|
2014-08-16 04:38:08 +04:00
|
|
|
UniquePtr<ptrT> ret( new ptrT(ioSurf, gl, size, hasAlpha) );
|
|
|
|
return Move(ret);
|
2013-07-18 07:24:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-01-06 02:57:44 +03:00
|
|
|
SharedSurface_IOSurface::ProducerReleaseImpl()
|
2013-07-18 07:24:15 +04:00
|
|
|
{
|
|
|
|
mGL->MakeCurrent();
|
|
|
|
mGL->fFlush();
|
|
|
|
}
|
|
|
|
|
2015-03-23 06:55:26 +03: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 03:50:27 +03:00
|
|
|
switch (internalformat) {
|
|
|
|
case LOCAL_GL_ALPHA:
|
|
|
|
case LOCAL_GL_LUMINANCE:
|
|
|
|
case LOCAL_GL_LUMINANCE_ALPHA:
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-03-23 06:55:26 +03: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 07:24:15 +04:00
|
|
|
bool
|
|
|
|
SharedSurface_IOSurface::ReadPixels(GLint x, GLint y, GLsizei width, GLsizei height,
|
2014-07-12 02:10:49 +04:00
|
|
|
GLenum format, GLenum type, GLvoid* pixels)
|
2013-07-18 07:24:15 +04: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 06:08:50 +04:00
|
|
|
MOZ_ASSERT(mGL->IsCurrent());
|
2013-07-18 07:24:15 +04:00
|
|
|
|
2014-03-11 06:08:50 +04:00
|
|
|
ScopedTexture destTex(mGL);
|
|
|
|
{
|
|
|
|
ScopedFramebufferForTexture srcFB(mGL, ProdTexture(), ProdTextureTarget());
|
|
|
|
|
|
|
|
ScopedBindFramebuffer bindFB(mGL, srcFB.FB());
|
|
|
|
ScopedBindTexture bindTex(mGL, destTex.Texture());
|
2015-04-03 03:50:27 +03:00
|
|
|
mGL->raw_fCopyTexImage2D(LOCAL_GL_TEXTURE_2D, 0,
|
|
|
|
mHasAlpha ? LOCAL_GL_RGBA : LOCAL_GL_RGB,
|
|
|
|
x, y,
|
|
|
|
width, height, 0);
|
2014-03-11 06:08:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
ScopedFramebufferForTexture destFB(mGL, destTex.Texture());
|
|
|
|
|
|
|
|
ScopedBindFramebuffer bindFB(mGL, destFB.FB());
|
2015-04-03 03:50:27 +03:00
|
|
|
mGL->raw_fReadPixels(0, 0, width, height, format, type, pixels);
|
2013-07-18 07:24:15 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-03-11 06:08:50 +04:00
|
|
|
static void
|
|
|
|
BackTextureWithIOSurf(GLContext* gl, GLuint tex, MacIOSurface* ioSurf)
|
2013-07-18 07:24:15 +04:00
|
|
|
{
|
2014-03-11 06:08:50 +04:00
|
|
|
MOZ_ASSERT(gl->IsCurrent());
|
2013-07-18 07:24:15 +04:00
|
|
|
|
2014-03-11 06:08:50 +04:00
|
|
|
ScopedBindTexture texture(gl, tex, LOCAL_GL_TEXTURE_RECTANGLE_ARB);
|
2013-07-18 07:24:15 +04:00
|
|
|
|
2014-03-11 06:08:50 +04:00
|
|
|
gl->fTexParameteri(LOCAL_GL_TEXTURE_RECTANGLE_ARB,
|
2013-07-18 07:24:15 +04:00
|
|
|
LOCAL_GL_TEXTURE_MIN_FILTER,
|
|
|
|
LOCAL_GL_LINEAR);
|
2014-03-11 06:08:50 +04:00
|
|
|
gl->fTexParameteri(LOCAL_GL_TEXTURE_RECTANGLE_ARB,
|
2013-07-18 07:24:15 +04:00
|
|
|
LOCAL_GL_TEXTURE_MAG_FILTER,
|
|
|
|
LOCAL_GL_LINEAR);
|
2014-03-11 06:08:50 +04:00
|
|
|
gl->fTexParameteri(LOCAL_GL_TEXTURE_RECTANGLE_ARB,
|
2013-07-18 07:24:15 +04:00
|
|
|
LOCAL_GL_TEXTURE_WRAP_S,
|
|
|
|
LOCAL_GL_CLAMP_TO_EDGE);
|
2014-03-11 06:08:50 +04:00
|
|
|
gl->fTexParameteri(LOCAL_GL_TEXTURE_RECTANGLE_ARB,
|
2013-07-18 07:24:15 +04:00
|
|
|
LOCAL_GL_TEXTURE_WRAP_T,
|
|
|
|
LOCAL_GL_CLAMP_TO_EDGE);
|
|
|
|
|
2014-03-11 06:08:50 +04:00
|
|
|
CGLContextObj cgl = GLContextCGL::Cast(gl)->GetCGLContext();
|
|
|
|
MOZ_ASSERT(cgl);
|
|
|
|
|
|
|
|
ioSurf->CGLTexImageIOSurface2D(cgl);
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
SharedSurface_IOSurface::SharedSurface_IOSurface(const RefPtr<MacIOSurface>& ioSurf,
|
2014-03-11 06:08:50 +04:00
|
|
|
GLContext* gl,
|
|
|
|
const gfx::IntSize& size,
|
|
|
|
bool hasAlpha)
|
2014-07-12 02:10:49 +04:00
|
|
|
: SharedSurface(SharedSurfaceType::IOSurface,
|
|
|
|
AttachmentType::GLTexture,
|
|
|
|
gl,
|
|
|
|
size,
|
2015-06-05 03:15:38 +03:00
|
|
|
hasAlpha,
|
|
|
|
true)
|
2014-08-16 04:38:08 +04:00
|
|
|
, mIOSurf(ioSurf)
|
2014-03-11 06:08:50 +04:00
|
|
|
{
|
|
|
|
gl->MakeCurrent();
|
|
|
|
mProdTex = 0;
|
|
|
|
gl->fGenTextures(1, &mProdTex);
|
2014-08-16 04:38:08 +04:00
|
|
|
BackTextureWithIOSurf(gl, mProdTex, mIOSurf);
|
2014-03-11 06:08:50 +04:00
|
|
|
}
|
|
|
|
|
2013-07-18 07:24:15 +04:00
|
|
|
SharedSurface_IOSurface::~SharedSurface_IOSurface()
|
|
|
|
{
|
2016-10-24 18:35:31 +03:00
|
|
|
if (!mGL || !mGL->MakeCurrent())
|
2016-04-14 23:50:04 +03:00
|
|
|
return;
|
|
|
|
|
|
|
|
mGL->fDeleteTextures(1, &mProdTex);
|
2013-07-18 07:24:15 +04:00
|
|
|
}
|
|
|
|
|
2015-06-05 03:15:38 +03:00
|
|
|
bool
|
|
|
|
SharedSurface_IOSurface::ToSurfaceDescriptor(layers::SurfaceDescriptor* const out_descriptor)
|
|
|
|
{
|
|
|
|
bool isOpaque = !mHasAlpha;
|
|
|
|
*out_descriptor = layers::SurfaceDescriptorMacIOSurface(mIOSurf->GetIOSurfaceID(),
|
|
|
|
mIOSurf->GetContentsScaleFactor(),
|
|
|
|
isOpaque);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-10-12 06:21:03 +03:00
|
|
|
bool
|
|
|
|
SharedSurface_IOSurface::ReadbackBySharedHandle(gfx::DataSourceSurface* out_surface)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(out_surface);
|
|
|
|
mIOSurf->Lock();
|
|
|
|
size_t bytesPerRow = mIOSurf->GetBytesPerRow();
|
|
|
|
size_t ioWidth = mIOSurf->GetDevicePixelWidth();
|
|
|
|
size_t ioHeight = mIOSurf->GetDevicePixelHeight();
|
|
|
|
|
|
|
|
const unsigned char* ioData = (unsigned char*)mIOSurf->GetBaseAddress();
|
|
|
|
gfx::DataSourceSurface::ScopedMap map(out_surface, gfx::DataSourceSurface::WRITE);
|
|
|
|
if (!map.IsMapped()) {
|
|
|
|
mIOSurf->Unlock();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i = 0; i < ioHeight; i++) {
|
|
|
|
memcpy(map.GetData() + i * map.GetStride(),
|
|
|
|
ioData + i * bytesPerRow, ioWidth * 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
mIOSurf->Unlock();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-08-16 04:38:08 +04:00
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
// SurfaceFactory_IOSurface
|
2014-07-12 10:08:54 +04:00
|
|
|
|
2014-08-16 04:38:08 +04:00
|
|
|
/*static*/ UniquePtr<SurfaceFactory_IOSurface>
|
2015-06-05 03:15:38 +03:00
|
|
|
SurfaceFactory_IOSurface::Create(GLContext* gl, const SurfaceCaps& caps,
|
2016-09-27 06:22:20 +03:00
|
|
|
const RefPtr<layers::LayersIPCChannel>& allocator,
|
2015-06-05 03:15:38 +03:00
|
|
|
const layers::TextureFlags& flags)
|
2014-07-12 10:08:54 +04:00
|
|
|
{
|
2016-07-26 17:48:30 +03:00
|
|
|
auto maxDims = gfx::IntSize::Truncate(MacIOSurface::GetMaxWidth(),
|
|
|
|
MacIOSurface::GetMaxHeight());
|
2014-08-16 04:38:08 +04:00
|
|
|
|
|
|
|
typedef SurfaceFactory_IOSurface ptrT;
|
2015-06-05 03:15:38 +03:00
|
|
|
UniquePtr<ptrT> ret( new ptrT(gl, caps, allocator, flags, maxDims) );
|
2014-08-16 04:38:08 +04:00
|
|
|
return Move(ret);
|
2014-07-12 10:08:54 +04:00
|
|
|
}
|
|
|
|
|
2014-08-16 04:38:08 +04:00
|
|
|
UniquePtr<SharedSurface>
|
2013-12-10 20:11:58 +04:00
|
|
|
SurfaceFactory_IOSurface::CreateShared(const gfx::IntSize& size)
|
2013-07-18 07:24:15 +04:00
|
|
|
{
|
2014-07-12 10:08:54 +04:00
|
|
|
if (size.width > mMaxDims.width ||
|
|
|
|
size.height > mMaxDims.height)
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2013-07-18 07:24:15 +04:00
|
|
|
bool hasAlpha = mReadCaps.alpha;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<MacIOSurface> ioSurf;
|
2014-08-16 04:38:08 +04:00
|
|
|
ioSurf = MacIOSurface::CreateIOSurface(size.width, size.height, 1.0,
|
|
|
|
hasAlpha);
|
2013-07-18 07:24:15 +04:00
|
|
|
|
2014-08-16 04:38:08 +04:00
|
|
|
if (!ioSurf) {
|
2013-12-03 07:11:27 +04:00
|
|
|
NS_WARNING("Failed to create MacIOSurface.");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-08-16 04:38:08 +04:00
|
|
|
return SharedSurface_IOSurface::Create(ioSurf, mGL, hasAlpha);
|
2013-07-18 07:24:15 +04:00
|
|
|
}
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace gl
|
|
|
|
} // namespace mozilla
|