2013-07-17 20:24:15 -07: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-01-07 12:02:18 -08:00
|
|
|
#include "GLContextCGL.h"
|
2013-07-17 20:24:15 -07:00
|
|
|
#include "gfxImageSurface.h"
|
|
|
|
#include "mozilla/gfx/MacIOSurface.h"
|
2013-10-01 14:01:19 -07:00
|
|
|
#include "mozilla/DebugOnly.h"
|
2013-11-25 19:25:25 -08:00
|
|
|
#include "ScopedGLHelpers.h"
|
2013-07-17 20:24:15 -07:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace gl {
|
|
|
|
|
|
|
|
using namespace gfx;
|
|
|
|
|
|
|
|
/* static */ SharedSurface_IOSurface*
|
|
|
|
SharedSurface_IOSurface::Create(MacIOSurface* surface, GLContext *gl, bool hasAlpha)
|
|
|
|
{
|
2013-12-02 19:11:27 -08:00
|
|
|
MOZ_ASSERT(surface);
|
|
|
|
MOZ_ASSERT(gl);
|
|
|
|
|
2013-12-10 08:11:58 -08:00
|
|
|
gfx::IntSize size(surface->GetWidth(), surface->GetHeight());
|
2013-07-17 20:24:15 -07:00
|
|
|
return new SharedSurface_IOSurface(surface, gl, size, hasAlpha);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SharedSurface_IOSurface::Fence()
|
|
|
|
{
|
|
|
|
mGL->MakeCurrent();
|
|
|
|
mGL->fFlush();
|
|
|
|
}
|
|
|
|
|
2013-07-17 20:24:15 -07:00
|
|
|
bool
|
|
|
|
SharedSurface_IOSurface::ReadPixels(GLint x, GLint y, GLsizei width, GLsizei height,
|
|
|
|
GLenum format, GLenum type, GLvoid *pixels)
|
|
|
|
{
|
|
|
|
// 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.
|
|
|
|
ScopedTexture texture(mGL);
|
|
|
|
ScopedBindTexture bindTex(mGL, texture.Texture());
|
|
|
|
mGL->fCopyTexImage2D(LOCAL_GL_TEXTURE_2D, 0,
|
|
|
|
HasAlpha() ? LOCAL_GL_RGBA : LOCAL_GL_RGB,
|
|
|
|
x, y,
|
|
|
|
width, height, 0);
|
|
|
|
|
|
|
|
ScopedFramebufferForTexture fb(mGL, texture.Texture());
|
|
|
|
ScopedBindFramebuffer bindFB(mGL, fb.FB());
|
|
|
|
|
|
|
|
mGL->fReadPixels(0, 0, width, height, format, type, pixels);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-07-17 20:24:15 -07:00
|
|
|
SharedSurface_IOSurface::SharedSurface_IOSurface(MacIOSurface* surface,
|
|
|
|
GLContext* gl,
|
2013-12-10 08:11:58 -08:00
|
|
|
const gfx::IntSize& size,
|
2013-07-17 20:24:15 -07:00
|
|
|
bool hasAlpha)
|
|
|
|
: SharedSurface_GL(SharedSurfaceType::IOSurface, AttachmentType::GLTexture, gl, size, hasAlpha)
|
|
|
|
, mSurface(surface)
|
|
|
|
{
|
|
|
|
mGL->MakeCurrent();
|
|
|
|
mGL->fGenTextures(1, &mTexture);
|
|
|
|
|
|
|
|
ScopedBindTexture texture(mGL, mTexture, LOCAL_GL_TEXTURE_RECTANGLE_ARB);
|
|
|
|
|
|
|
|
mGL->fTexParameteri(LOCAL_GL_TEXTURE_RECTANGLE_ARB,
|
|
|
|
LOCAL_GL_TEXTURE_MIN_FILTER,
|
|
|
|
LOCAL_GL_LINEAR);
|
|
|
|
mGL->fTexParameteri(LOCAL_GL_TEXTURE_RECTANGLE_ARB,
|
|
|
|
LOCAL_GL_TEXTURE_MAG_FILTER,
|
|
|
|
LOCAL_GL_LINEAR);
|
|
|
|
mGL->fTexParameteri(LOCAL_GL_TEXTURE_RECTANGLE_ARB,
|
|
|
|
LOCAL_GL_TEXTURE_WRAP_S,
|
|
|
|
LOCAL_GL_CLAMP_TO_EDGE);
|
|
|
|
mGL->fTexParameteri(LOCAL_GL_TEXTURE_RECTANGLE_ARB,
|
|
|
|
LOCAL_GL_TEXTURE_WRAP_T,
|
|
|
|
LOCAL_GL_CLAMP_TO_EDGE);
|
|
|
|
|
2014-01-07 12:02:18 -08:00
|
|
|
CGLContextObj ctx = GLContextCGL::Cast(mGL)->GetCGLContext();
|
2013-11-07 19:53:54 -08:00
|
|
|
MOZ_ASSERT(ctx);
|
2013-07-17 20:24:15 -07:00
|
|
|
|
2013-11-07 19:53:54 -08:00
|
|
|
surface->CGLTexImageIOSurface2D(ctx);
|
2013-07-17 20:24:15 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
SharedSurface_IOSurface::~SharedSurface_IOSurface()
|
|
|
|
{
|
|
|
|
if (mTexture) {
|
|
|
|
DebugOnly<bool> success = mGL->MakeCurrent();
|
|
|
|
MOZ_ASSERT(success);
|
|
|
|
mGL->fDeleteTextures(1, &mTexture);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SharedSurface*
|
2013-12-10 08:11:58 -08:00
|
|
|
SurfaceFactory_IOSurface::CreateShared(const gfx::IntSize& size)
|
2013-07-17 20:24:15 -07:00
|
|
|
{
|
|
|
|
bool hasAlpha = mReadCaps.alpha;
|
|
|
|
RefPtr<MacIOSurface> surf =
|
|
|
|
MacIOSurface::CreateIOSurface(size.width, size.height, 1.0, hasAlpha);
|
|
|
|
|
2013-12-02 19:11:27 -08:00
|
|
|
if (!surf) {
|
|
|
|
NS_WARNING("Failed to create MacIOSurface.");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2013-07-17 20:24:15 -07:00
|
|
|
return SharedSurface_IOSurface::Create(surf, mGL, hasAlpha);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|