Bug 1059066 - Add various IOSurface related methods to MacIOSurface wrapper. r=mattwoodrow

This commit is contained in:
Jean-Yves Avenard 2014-09-03 17:09:24 +10:00
parent 4f863399ee
commit aea262e5a4
2 changed files with 144 additions and 33 deletions

View File

@ -18,9 +18,11 @@ using namespace mozilla;
#define OPENGL_FRAMEWORK_PATH \
"/System/Library/Frameworks/OpenGL.framework/OpenGL"
#define COREGRAPHICS_FRAMEWORK_PATH \
"/System/Library/Frameworks/ApplicationServices.framework/Frameworks/CoreGraphics.framework/CoreGraphics"
"/System/Library/Frameworks/ApplicationServices.framework/Frameworks/" \
"CoreGraphics.framework/CoreGraphics"
#define COREVIDEO_FRAMEWORK_PATH \
"/System/Library/Frameworks/ApplicationServices.framework/Frameworks/" \
"CoreVideo.framework/CoreVideo"
#define GET_CONST(const_name) \
((CFStringRef*) dlsym(sIOSurfaceFramework, const_name))
@ -30,26 +32,34 @@ using namespace mozilla;
(typeof(dest)) dlsym(sOpenGLFramework, sym_name)
#define GET_CGSYM(dest,sym_name) \
(typeof(dest)) dlsym(sCoreGraphicsFramework, sym_name)
#define GET_CVSYM(dest, sym_name) \
(typeof(dest)) dlsym(sCoreVideoFramework, sym_name)
MacIOSurfaceLib::LibraryUnloader MacIOSurfaceLib::sLibraryUnloader;
bool MacIOSurfaceLib::isLoaded = false;
void* MacIOSurfaceLib::sIOSurfaceFramework;
void* MacIOSurfaceLib::sOpenGLFramework;
void* MacIOSurfaceLib::sCoreGraphicsFramework;
void* MacIOSurfaceLib::sCoreVideoFramework;
IOSurfaceCreateFunc MacIOSurfaceLib::sCreate;
IOSurfaceGetIDFunc MacIOSurfaceLib::sGetID;
IOSurfaceLookupFunc MacIOSurfaceLib::sLookup;
IOSurfaceGetBaseAddressFunc MacIOSurfaceLib::sGetBaseAddress;
IOSurfaceGetWidthFunc MacIOSurfaceLib::sWidth;
IOSurfaceGetHeightFunc MacIOSurfaceLib::sHeight;
IOSurfaceGetBytesPerRowFunc MacIOSurfaceLib::sBytesPerRow;
IOSurfaceGetBaseAddressOfPlaneFunc MacIOSurfaceLib::sGetBaseAddressOfPlane;
IOSurfaceSizeTFunc MacIOSurfaceLib::sWidth;
IOSurfaceSizeTFunc MacIOSurfaceLib::sHeight;
IOSurfaceSizeTFunc MacIOSurfaceLib::sPlaneCount;
IOSurfaceSizeTFunc MacIOSurfaceLib::sBytesPerRow;
IOSurfaceGetPropertyMaximumFunc MacIOSurfaceLib::sGetPropertyMaximum;
IOSurfaceVoidFunc MacIOSurfaceLib::sIncrementUseCount;
IOSurfaceVoidFunc MacIOSurfaceLib::sDecrementUseCount;
IOSurfaceLockFunc MacIOSurfaceLib::sLock;
IOSurfaceUnlockFunc MacIOSurfaceLib::sUnlock;
CGLTexImageIOSurface2DFunc MacIOSurfaceLib::sTexImage;
IOSurfaceContextCreateFunc MacIOSurfaceLib::sIOSurfaceContextCreate;
IOSurfaceContextCreateImageFunc MacIOSurfaceLib::sIOSurfaceContextCreateImage;
IOSurfaceContextGetSurfaceFunc MacIOSurfaceLib::sIOSurfaceContextGetSurface;
CVPixelBufferGetIOSurfaceFunc MacIOSurfaceLib::sCVPixelBufferGetIOSurface;
unsigned int (*MacIOSurfaceLib::sCGContextGetTypePtr) (CGContextRef) = nullptr;
CFStringRef MacIOSurfaceLib::kPropWidth;
@ -83,6 +93,15 @@ void* MacIOSurfaceLib::IOSurfaceGetBaseAddress(IOSurfacePtr aIOSurfacePtr) {
return sGetBaseAddress(aIOSurfacePtr);
}
void* MacIOSurfaceLib::IOSurfaceGetBaseAddressOfPlane(IOSurfacePtr aIOSurfacePtr,
size_t planeIndex) {
return sGetBaseAddressOfPlane(aIOSurfacePtr, planeIndex);
}
size_t MacIOSurfaceLib::IOSurfaceGetPlaneCount(IOSurfacePtr aIOSurfacePtr) {
return sPlaneCount(aIOSurfacePtr);
}
size_t MacIOSurfaceLib::IOSurfaceGetWidth(IOSurfacePtr aIOSurfacePtr) {
return sWidth(aIOSurfacePtr);
}
@ -99,16 +118,24 @@ size_t MacIOSurfaceLib::IOSurfaceGetPropertyMaximum(CFStringRef property) {
return sGetPropertyMaximum(property);
}
IOReturn MacIOSurfaceLib::IOSurfaceLock(IOSurfacePtr aIOSurfacePtr,
uint32_t options, uint32_t *seed) {
IOReturn MacIOSurfaceLib::IOSurfaceLock(IOSurfacePtr aIOSurfacePtr,
uint32_t options, uint32_t* seed) {
return sLock(aIOSurfacePtr, options, seed);
}
IOReturn MacIOSurfaceLib::IOSurfaceUnlock(IOSurfacePtr aIOSurfacePtr,
IOReturn MacIOSurfaceLib::IOSurfaceUnlock(IOSurfacePtr aIOSurfacePtr,
uint32_t options, uint32_t *seed) {
return sUnlock(aIOSurfacePtr, options, seed);
}
void MacIOSurfaceLib::IOSurfaceIncrementUseCount(IOSurfacePtr aIOSurfacePtr) {
sIncrementUseCount(aIOSurfacePtr);
}
void MacIOSurfaceLib::IOSurfaceDecrementUseCount(IOSurfacePtr aIOSurfacePtr) {
sDecrementUseCount(aIOSurfacePtr);
}
CGLError MacIOSurfaceLib::CGLTexImageIOSurface2D(CGLContextObj ctxt,
GLenum target, GLenum internalFormat,
GLsizei width, GLsizei height,
@ -118,6 +145,10 @@ CGLError MacIOSurfaceLib::CGLTexImageIOSurface2D(CGLContextObj ctxt,
format, type, ioSurface, plane);
}
IOSurfacePtr MacIOSurfaceLib::CVPixelBufferGetIOSurface(CVPixelBufferRef aPixelBuffer) {
return sCVPixelBufferGetIOSurface(aPixelBuffer);
}
CGContextRef MacIOSurfaceLib::IOSurfaceContextCreate(IOSurfacePtr aIOSurfacePtr,
unsigned aWidth, unsigned aHeight,
unsigned aBitsPerComponent, unsigned aBytes,
@ -159,16 +190,24 @@ void MacIOSurfaceLib::LoadLibrary() {
sCoreGraphicsFramework = dlopen(COREGRAPHICS_FRAMEWORK_PATH,
RTLD_LAZY | RTLD_LOCAL);
if (!sIOSurfaceFramework || !sOpenGLFramework || !sCoreGraphicsFramework) {
sCoreVideoFramework = dlopen(COREVIDEO_FRAMEWORK_PATH,
RTLD_LAZY | RTLD_LOCAL);
if (!sIOSurfaceFramework || !sOpenGLFramework || !sCoreGraphicsFramework ||
!sCoreVideoFramework) {
if (sIOSurfaceFramework)
dlclose(sIOSurfaceFramework);
if (sOpenGLFramework)
dlclose(sOpenGLFramework);
if (sCoreGraphicsFramework)
dlclose(sCoreGraphicsFramework);
if (sCoreVideoFramework)
dlclose(sCoreVideoFramework);
sIOSurfaceFramework = nullptr;
sOpenGLFramework = nullptr;
sCoreGraphicsFramework = nullptr;
sCoreVideoFramework = nullptr;
return;
}
@ -186,19 +225,32 @@ void MacIOSurfaceLib::LoadLibrary() {
sLookup = GET_IOSYM(sLookup, "IOSurfaceLookup");
sLock = GET_IOSYM(sLock, "IOSurfaceLock");
sUnlock = GET_IOSYM(sUnlock, "IOSurfaceUnlock");
sIncrementUseCount =
GET_IOSYM(sIncrementUseCount, "IOSurfaceIncrementUseCount");
sDecrementUseCount =
GET_IOSYM(sDecrementUseCount, "IOSurfaceDecrementUseCount");
sGetBaseAddress = GET_IOSYM(sGetBaseAddress, "IOSurfaceGetBaseAddress");
sGetBaseAddressOfPlane =
GET_IOSYM(sGetBaseAddressOfPlane, "IOSurfaceGetBaseAddressOfPlane");
sPlaneCount = GET_IOSYM(sPlaneCount, "IOSurfaceGetPlaneCount");
sTexImage = GET_CGLSYM(sTexImage, "CGLTexImageIOSurface2D");
sCGContextGetTypePtr = (unsigned int (*)(CGContext*))dlsym(RTLD_DEFAULT, "CGContextGetType");
sCVPixelBufferGetIOSurface =
GET_CVSYM(sCVPixelBufferGetIOSurface, "CVPixelBufferGetIOSurface");
// Optional symbols
sIOSurfaceContextCreate = GET_CGSYM(sIOSurfaceContextCreate, "CGIOSurfaceContextCreate");
sIOSurfaceContextCreateImage = GET_CGSYM(sIOSurfaceContextCreateImage, "CGIOSurfaceContextCreateImage");
sIOSurfaceContextGetSurface = GET_CGSYM(sIOSurfaceContextGetSurface, "CGIOSurfaceContextGetSurface");
if (!sCreate || !sGetID || !sLookup || !sTexImage || !sGetBaseAddress ||
!sGetBaseAddressOfPlane || !sPlaneCount ||
!kPropWidth || !kPropHeight || !kPropBytesPerElem || !kPropIsGlobal ||
!sLock || !sUnlock || !sWidth || !sHeight || !kPropBytesPerRow ||
!sBytesPerRow || !sGetPropertyMaximum) {
!sLock || !sUnlock || !sIncrementUseCount || !sDecrementUseCount ||
!sWidth || !sHeight || !kPropBytesPerRow ||
!sBytesPerRow || !sGetPropertyMaximum || !sCVPixelBufferGetIOSurface) {
CloseLibrary();
}
}
@ -210,11 +262,26 @@ void MacIOSurfaceLib::CloseLibrary() {
if (sOpenGLFramework) {
dlclose(sOpenGLFramework);
}
if (sCoreVideoFramework) {
dlclose(sCoreVideoFramework);
}
sIOSurfaceFramework = nullptr;
sOpenGLFramework = nullptr;
sCoreVideoFramework = nullptr;
}
MacIOSurface::MacIOSurface(const void* aIOSurfacePtr,
double aContentsScaleFactor, bool aHasAlpha)
: mIOSurfacePtr(aIOSurfacePtr)
, mContentsScaleFactor(aContentsScaleFactor)
, mHasAlpha(aHasAlpha)
{
CFRetain(mIOSurfacePtr);
IncrementUseCount();
}
MacIOSurface::~MacIOSurface() {
DecrementUseCount();
CFRelease(mIOSurfacePtr);
}
@ -265,6 +332,9 @@ TemporaryRef<MacIOSurface> MacIOSurface::CreateIOSurface(int aWidth, int aHeight
return nullptr;
}
// Release the IOSurface because MacIOSurface retained it
CFRelease(surfaceRef);
return ioSurface.forget();
}
@ -283,6 +353,10 @@ TemporaryRef<MacIOSurface> MacIOSurface::LookupSurface(IOSurfaceID aIOSurfaceID,
::CFRelease(surfaceRef);
return nullptr;
}
// Release the IOSurface because MacIOSurface retained it
CFRelease(surfaceRef);
return ioSurface.forget();
}
@ -294,6 +368,12 @@ void* MacIOSurface::GetBaseAddress() {
return MacIOSurfaceLib::IOSurfaceGetBaseAddress(mIOSurfacePtr);
}
void* MacIOSurface::GetBaseAddressOfPlane(size_t aPlaneIndex)
{
return MacIOSurfaceLib::IOSurfaceGetBaseAddressOfPlane(mIOSurfacePtr,
aPlaneIndex);
}
size_t MacIOSurface::GetWidth() {
size_t intScaleFactor = ceil(mContentsScaleFactor);
return GetDevicePixelWidth() / intScaleFactor;
@ -304,6 +384,10 @@ size_t MacIOSurface::GetHeight() {
return GetDevicePixelHeight() / intScaleFactor;
}
size_t MacIOSurface::GetPlaneCount() {
return MacIOSurfaceLib::IOSurfaceGetPlaneCount(mIOSurfacePtr);
}
/*static*/ size_t MacIOSurface::GetMaxWidth() {
return MacIOSurfaceLib::IOSurfaceGetPropertyMaximum(MacIOSurfaceLib::kPropWidth);
}
@ -324,6 +408,14 @@ size_t MacIOSurface::GetBytesPerRow() {
return MacIOSurfaceLib::IOSurfaceGetBytesPerRow(mIOSurfacePtr);
}
void MacIOSurface::IncrementUseCount() {
MacIOSurfaceLib::IOSurfaceIncrementUseCount(mIOSurfacePtr);
}
void MacIOSurface::DecrementUseCount() {
MacIOSurfaceLib::IOSurfaceDecrementUseCount(mIOSurfacePtr);
}
#define READ_ONLY 0x1
void MacIOSurface::Lock() {
MacIOSurfaceLib::IOSurfaceLock(mIOSurfacePtr, READ_ONLY, nullptr);
@ -413,9 +505,6 @@ TemporaryRef<MacIOSurface> MacIOSurface::IOSurfaceContextGetSurface(CGContextRef
if (!surfaceRef)
return nullptr;
// Retain the IOSurface because MacIOSurface will release it
CFRetain(surfaceRef);
RefPtr<MacIOSurface> ioSurface = new MacIOSurface(surfaceRef, aContentsScaleFactor, aHasAlpha);
if (!ioSurface) {
::CFRelease(surfaceRef);

View File

@ -14,17 +14,16 @@
typedef CFTypeRef IOSurfacePtr;
typedef IOSurfacePtr (*IOSurfaceCreateFunc) (CFDictionaryRef properties);
typedef IOSurfacePtr (*IOSurfaceLookupFunc) (uint32_t io_surface_id);
typedef IOSurfaceID (*IOSurfaceGetIDFunc) (CFTypeRef io_surface);
typedef IOReturn (*IOSurfaceLockFunc) (CFTypeRef io_surface,
uint32_t options,
uint32_t *seed);
typedef IOReturn (*IOSurfaceUnlockFunc) (CFTypeRef io_surface,
uint32_t options,
uint32_t *seed);
typedef void* (*IOSurfaceGetBaseAddressFunc) (CFTypeRef io_surface);
typedef size_t (*IOSurfaceGetWidthFunc) (IOSurfacePtr io_surface);
typedef size_t (*IOSurfaceGetHeightFunc) (IOSurfacePtr io_surface);
typedef size_t (*IOSurfaceGetBytesPerRowFunc) (IOSurfacePtr io_surface);
typedef IOSurfaceID (*IOSurfaceGetIDFunc)(IOSurfacePtr io_surface);
typedef void (*IOSurfaceVoidFunc)(IOSurfacePtr io_surface);
typedef IOReturn (*IOSurfaceLockFunc)(IOSurfacePtr io_surface, uint32_t options,
uint32_t *seed);
typedef IOReturn (*IOSurfaceUnlockFunc)(IOSurfacePtr io_surface,
uint32_t options, uint32_t *seed);
typedef void* (*IOSurfaceGetBaseAddressFunc)(IOSurfacePtr io_surface);
typedef void* (*IOSurfaceGetBaseAddressOfPlaneFunc)(IOSurfacePtr io_surface,
size_t planeIndex);
typedef size_t (*IOSurfaceSizeTFunc)(IOSurfacePtr io_surface);
typedef size_t (*IOSurfaceGetPropertyMaximumFunc) (CFStringRef property);
typedef CGLError (*CGLTexImageIOSurface2DFunc) (CGLContextObj ctxt,
GLenum target, GLenum internalFormat,
@ -38,6 +37,8 @@ typedef CGContextRef (*IOSurfaceContextCreateFunc)(CFTypeRef io_surface,
typedef CGImageRef (*IOSurfaceContextCreateImageFunc)(CGContextRef ref);
typedef IOSurfacePtr (*IOSurfaceContextGetSurfaceFunc)(CGContextRef ref);
typedef IOSurfacePtr (*CVPixelBufferGetIOSurfaceFunc)(
CVPixelBufferRef pixelBuffer);
#import <OpenGL/OpenGL.h>
#include "2D.h"
@ -64,6 +65,10 @@ public:
MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(MacIOSurface)
typedef mozilla::gfx::SourceSurface SourceSurface;
// The usage count of the IOSurface is increased by 1 during the lifetime
// of the MacIOSurface instance.
// MacIOSurface holds a reference to the corresponding IOSurface.
static mozilla::TemporaryRef<MacIOSurface> CreateIOSurface(int aWidth, int aHeight,
double aContentsScaleFactor = 1.0,
bool aHasAlpha = true);
@ -72,11 +77,14 @@ public:
double aContentsScaleFactor = 1.0,
bool aHasAlpha = true);
explicit MacIOSurface(const void *aIOSurfacePtr, double aContentsScaleFactor = 1.0, bool aHasAlpha = true)
: mIOSurfacePtr(aIOSurfacePtr), mContentsScaleFactor(aContentsScaleFactor), mHasAlpha(aHasAlpha) {}
explicit MacIOSurface(const void *aIOSurfacePtr,
double aContentsScaleFactor = 1.0,
bool aHasAlpha = true);
virtual ~MacIOSurface();
IOSurfaceID GetIOSurfaceID();
void *GetBaseAddress();
void *GetBaseAddressOfPlane(size_t planeIndex);
size_t GetPlaneCount();
// GetWidth() and GetHeight() return values in "display pixels". A
// "display pixel" is the smallest fully addressable part of a display.
// But in HiDPI modes each "display pixel" corresponds to more than one
@ -89,6 +97,8 @@ public:
size_t GetBytesPerRow();
void Lock();
void Unlock();
void IncrementUseCount();
void DecrementUseCount();
bool HasAlpha() { return mHasAlpha; }
// We would like to forward declare NSOpenGLContext, but it is an @interface
// and this file is also used from c++, so we use a void *.
@ -117,21 +127,27 @@ public:
static void *sIOSurfaceFramework;
static void *sOpenGLFramework;
static void *sCoreGraphicsFramework;
static void *sCoreVideoFramework;
static bool isLoaded;
static IOSurfaceCreateFunc sCreate;
static IOSurfaceGetIDFunc sGetID;
static IOSurfaceLookupFunc sLookup;
static IOSurfaceGetBaseAddressFunc sGetBaseAddress;
static IOSurfaceGetBaseAddressOfPlaneFunc sGetBaseAddressOfPlane;
static IOSurfaceSizeTFunc sPlaneCount;
static IOSurfaceLockFunc sLock;
static IOSurfaceUnlockFunc sUnlock;
static IOSurfaceGetWidthFunc sWidth;
static IOSurfaceGetHeightFunc sHeight;
static IOSurfaceGetBytesPerRowFunc sBytesPerRow;
static IOSurfaceVoidFunc sIncrementUseCount;
static IOSurfaceVoidFunc sDecrementUseCount;
static IOSurfaceSizeTFunc sWidth;
static IOSurfaceSizeTFunc sHeight;
static IOSurfaceSizeTFunc sBytesPerRow;
static IOSurfaceGetPropertyMaximumFunc sGetPropertyMaximum;
static CGLTexImageIOSurface2DFunc sTexImage;
static IOSurfaceContextCreateFunc sIOSurfaceContextCreate;
static IOSurfaceContextCreateImageFunc sIOSurfaceContextCreateImage;
static IOSurfaceContextGetSurfaceFunc sIOSurfaceContextGetSurface;
static CVPixelBufferGetIOSurfaceFunc sCVPixelBufferGetIOSurface;
static CFStringRef kPropWidth;
static CFStringRef kPropHeight;
static CFStringRef kPropBytesPerElem;
@ -144,14 +160,19 @@ public:
static IOSurfacePtr IOSurfaceLookup(IOSurfaceID aIOSurfaceID);
static IOSurfaceID IOSurfaceGetID(IOSurfacePtr aIOSurfacePtr);
static void* IOSurfaceGetBaseAddress(IOSurfacePtr aIOSurfacePtr);
static void* IOSurfaceGetBaseAddressOfPlane(IOSurfacePtr aIOSurfacePtr,
size_t aPlaneIndex);
static size_t IOSurfaceGetPlaneCount(IOSurfacePtr aIOSurfacePtr);
static size_t IOSurfaceGetWidth(IOSurfacePtr aIOSurfacePtr);
static size_t IOSurfaceGetHeight(IOSurfacePtr aIOSurfacePtr);
static size_t IOSurfaceGetBytesPerRow(IOSurfacePtr aIOSurfacePtr);
static size_t IOSurfaceGetPropertyMaximum(CFStringRef property);
static IOReturn IOSurfaceLock(IOSurfacePtr aIOSurfacePtr,
static IOReturn IOSurfaceLock(IOSurfacePtr aIOSurfacePtr,
uint32_t options, uint32_t *seed);
static IOReturn IOSurfaceUnlock(IOSurfacePtr aIOSurfacePtr,
static IOReturn IOSurfaceUnlock(IOSurfacePtr aIOSurfacePtr,
uint32_t options, uint32_t *seed);
static void IOSurfaceIncrementUseCount(IOSurfacePtr aIOSurfacePtr);
static void IOSurfaceDecrementUseCount(IOSurfacePtr aIOSurfacePtr);
static CGLError CGLTexImageIOSurface2D(CGLContextObj ctxt,
GLenum target, GLenum internalFormat,
GLsizei width, GLsizei height,
@ -163,6 +184,7 @@ public:
CGColorSpaceRef aColorSpace, CGBitmapInfo bitmapInfo);
static CGImageRef IOSurfaceContextCreateImage(CGContextRef ref);
static IOSurfacePtr IOSurfaceContextGetSurface(CGContextRef ref);
static IOSurfacePtr CVPixelBufferGetIOSurface(CVPixelBufferRef apixelBuffer);
static unsigned int (*sCGContextGetTypePtr) (CGContextRef);
static void LoadLibrary();
static void CloseLibrary();