From 7fc84d38a376616e462529eabb1eb66f93db443d Mon Sep 17 00:00:00 2001 From: Jeff Muizelaar Date: Thu, 7 Nov 2013 22:53:54 -0500 Subject: [PATCH] Bug 929471. Make DrawTargetCG independent from QuartzSupport.mm. r=bgirard --- gfx/2d/DrawTargetCG.cpp | 2 +- gfx/2d/MacIOSurface.cpp | 419 ++++++++++++++++++++++++++++++++ gfx/2d/MacIOSurface.h | 102 ++++++++ gfx/2d/QuartzSupport.h | 9 - gfx/2d/QuartzSupport.mm | 486 +------------------------------------ gfx/2d/SourceSurfaceCG.cpp | 2 +- gfx/2d/moz.build | 1 + 7 files changed, 525 insertions(+), 496 deletions(-) create mode 100644 gfx/2d/MacIOSurface.cpp diff --git a/gfx/2d/DrawTargetCG.cpp b/gfx/2d/DrawTargetCG.cpp index 9cd69b0749b..03a1b499c0e 100644 --- a/gfx/2d/DrawTargetCG.cpp +++ b/gfx/2d/DrawTargetCG.cpp @@ -10,7 +10,7 @@ #include "Tools.h" #include #include -#include "QuartzSupport.h" +#include "MacIOSurface.h" using namespace std; diff --git a/gfx/2d/MacIOSurface.cpp b/gfx/2d/MacIOSurface.cpp new file mode 100644 index 00000000000..5b787baeee2 --- /dev/null +++ b/gfx/2d/MacIOSurface.cpp @@ -0,0 +1,419 @@ +#include "MacIOSurface.h" +#include +#include +#include "mozilla/RefPtr.h" +#include "mozilla/Assertions.h" + +using namespace mozilla; +// IOSurface signatures +#define IOSURFACE_FRAMEWORK_PATH \ + "/System/Library/Frameworks/IOSurface.framework/IOSurface" +#define OPENGL_FRAMEWORK_PATH \ + "/System/Library/Frameworks/OpenGL.framework/OpenGL" +#define COREGRAPHICS_FRAMEWORK_PATH \ + "/System/Library/Frameworks/ApplicationServices.framework/Frameworks/CoreGraphics.framework/CoreGraphics" + + + +#define GET_CONST(const_name) \ + ((CFStringRef*) dlsym(sIOSurfaceFramework, const_name)) +#define GET_IOSYM(dest,sym_name) \ + (typeof(dest)) dlsym(sIOSurfaceFramework, sym_name) +#define GET_CGLSYM(dest,sym_name) \ + (typeof(dest)) dlsym(sOpenGLFramework, sym_name) +#define GET_CGSYM(dest,sym_name) \ + (typeof(dest)) dlsym(sCoreGraphicsFramework, sym_name) + +MacIOSurfaceLib::LibraryUnloader MacIOSurfaceLib::sLibraryUnloader; +bool MacIOSurfaceLib::isLoaded = false; +void* MacIOSurfaceLib::sIOSurfaceFramework; +void* MacIOSurfaceLib::sOpenGLFramework; +void* MacIOSurfaceLib::sCoreGraphicsFramework; +IOSurfaceCreateFunc MacIOSurfaceLib::sCreate; +IOSurfaceGetIDFunc MacIOSurfaceLib::sGetID; +IOSurfaceLookupFunc MacIOSurfaceLib::sLookup; +IOSurfaceGetBaseAddressFunc MacIOSurfaceLib::sGetBaseAddress; +IOSurfaceGetWidthFunc MacIOSurfaceLib::sWidth; +IOSurfaceGetHeightFunc MacIOSurfaceLib::sHeight; +IOSurfaceGetBytesPerRowFunc MacIOSurfaceLib::sBytesPerRow; +IOSurfaceLockFunc MacIOSurfaceLib::sLock; +IOSurfaceUnlockFunc MacIOSurfaceLib::sUnlock; +CGLTexImageIOSurface2DFunc MacIOSurfaceLib::sTexImage; +IOSurfaceContextCreateFunc MacIOSurfaceLib::sIOSurfaceContextCreate; +IOSurfaceContextCreateImageFunc MacIOSurfaceLib::sIOSurfaceContextCreateImage; +IOSurfaceContextGetSurfaceFunc MacIOSurfaceLib::sIOSurfaceContextGetSurface; +unsigned int (*MacIOSurfaceLib::sCGContextGetTypePtr) (CGContextRef) = nullptr; + +CFStringRef MacIOSurfaceLib::kPropWidth; +CFStringRef MacIOSurfaceLib::kPropHeight; +CFStringRef MacIOSurfaceLib::kPropBytesPerElem; +CFStringRef MacIOSurfaceLib::kPropBytesPerRow; +CFStringRef MacIOSurfaceLib::kPropIsGlobal; + +bool MacIOSurfaceLib::isInit() { + // Guard against trying to reload the library + // if it is not available. + if (!isLoaded) + LoadLibrary(); + MOZ_ASSERT(sIOSurfaceFramework); + return sIOSurfaceFramework; +} + +IOSurfacePtr MacIOSurfaceLib::IOSurfaceCreate(CFDictionaryRef properties) { + return sCreate(properties); +} + +IOSurfacePtr MacIOSurfaceLib::IOSurfaceLookup(IOSurfaceID aIOSurfaceID) { + return sLookup(aIOSurfaceID); +} + +IOSurfaceID MacIOSurfaceLib::IOSurfaceGetID(IOSurfacePtr aIOSurfacePtr) { + return sGetID(aIOSurfacePtr); +} + +void* MacIOSurfaceLib::IOSurfaceGetBaseAddress(IOSurfacePtr aIOSurfacePtr) { + return sGetBaseAddress(aIOSurfacePtr); +} + +size_t MacIOSurfaceLib::IOSurfaceGetWidth(IOSurfacePtr aIOSurfacePtr) { + return sWidth(aIOSurfacePtr); +} + +size_t MacIOSurfaceLib::IOSurfaceGetHeight(IOSurfacePtr aIOSurfacePtr) { + return sHeight(aIOSurfacePtr); +} + +size_t MacIOSurfaceLib::IOSurfaceGetBytesPerRow(IOSurfacePtr aIOSurfacePtr) { + return sBytesPerRow(aIOSurfacePtr); +} + +IOReturn MacIOSurfaceLib::IOSurfaceLock(IOSurfacePtr aIOSurfacePtr, + uint32_t options, uint32_t *seed) { + return sLock(aIOSurfacePtr, options, seed); +} + +IOReturn MacIOSurfaceLib::IOSurfaceUnlock(IOSurfacePtr aIOSurfacePtr, + uint32_t options, uint32_t *seed) { + return sUnlock(aIOSurfacePtr, options, seed); +} + +CGLError MacIOSurfaceLib::CGLTexImageIOSurface2D(CGLContextObj ctxt, + GLenum target, GLenum internalFormat, + GLsizei width, GLsizei height, + GLenum format, GLenum type, + IOSurfacePtr ioSurface, GLuint plane) { + return sTexImage(ctxt, target, internalFormat, width, height, + format, type, ioSurface, plane); +} + +CGContextRef MacIOSurfaceLib::IOSurfaceContextCreate(IOSurfacePtr aIOSurfacePtr, + unsigned aWidth, unsigned aHeight, + unsigned aBitsPerComponent, unsigned aBytes, + CGColorSpaceRef aColorSpace, CGBitmapInfo bitmapInfo) { + if (!sIOSurfaceContextCreate) + return nullptr; + return sIOSurfaceContextCreate(aIOSurfacePtr, aWidth, aHeight, aBitsPerComponent, aBytes, aColorSpace, bitmapInfo); +} + +CGImageRef MacIOSurfaceLib::IOSurfaceContextCreateImage(CGContextRef aContext) { + if (!sIOSurfaceContextCreateImage) + return nullptr; + return sIOSurfaceContextCreateImage(aContext); +} + +IOSurfacePtr MacIOSurfaceLib::IOSurfaceContextGetSurface(CGContextRef aContext) { + if (!sIOSurfaceContextGetSurface) + return nullptr; + return sIOSurfaceContextGetSurface(aContext); +} + +CFStringRef MacIOSurfaceLib::GetIOConst(const char* symbole) { + CFStringRef *address = (CFStringRef*)dlsym(sIOSurfaceFramework, symbole); + if (!address) + return nullptr; + + return *address; +} + +void MacIOSurfaceLib::LoadLibrary() { + if (isLoaded) { + return; + } + isLoaded = true; + sIOSurfaceFramework = dlopen(IOSURFACE_FRAMEWORK_PATH, + RTLD_LAZY | RTLD_LOCAL); + sOpenGLFramework = dlopen(OPENGL_FRAMEWORK_PATH, + RTLD_LAZY | RTLD_LOCAL); + + sCoreGraphicsFramework = dlopen(COREGRAPHICS_FRAMEWORK_PATH, + RTLD_LAZY | RTLD_LOCAL); + if (!sIOSurfaceFramework || !sOpenGLFramework || !sCoreGraphicsFramework) { + if (sIOSurfaceFramework) + dlclose(sIOSurfaceFramework); + if (sOpenGLFramework) + dlclose(sOpenGLFramework); + if (sCoreGraphicsFramework) + dlclose(sCoreGraphicsFramework); + sIOSurfaceFramework = nullptr; + sOpenGLFramework = nullptr; + sCoreGraphicsFramework = nullptr; + return; + } + + kPropWidth = GetIOConst("kIOSurfaceWidth"); + kPropHeight = GetIOConst("kIOSurfaceHeight"); + kPropBytesPerElem = GetIOConst("kIOSurfaceBytesPerElement"); + kPropBytesPerRow = GetIOConst("kIOSurfaceBytesPerRow"); + kPropIsGlobal = GetIOConst("kIOSurfaceIsGlobal"); + sCreate = GET_IOSYM(sCreate, "IOSurfaceCreate"); + sGetID = GET_IOSYM(sGetID, "IOSurfaceGetID"); + sWidth = GET_IOSYM(sWidth, "IOSurfaceGetWidth"); + sHeight = GET_IOSYM(sHeight, "IOSurfaceGetHeight"); + sBytesPerRow = GET_IOSYM(sBytesPerRow, "IOSurfaceGetBytesPerRow"); + sLookup = GET_IOSYM(sLookup, "IOSurfaceLookup"); + sLock = GET_IOSYM(sLock, "IOSurfaceLock"); + sUnlock = GET_IOSYM(sUnlock, "IOSurfaceUnlock"); + sGetBaseAddress = GET_IOSYM(sGetBaseAddress, "IOSurfaceGetBaseAddress"); + sTexImage = GET_CGLSYM(sTexImage, "CGLTexImageIOSurface2D"); + sCGContextGetTypePtr = (unsigned int (*)(CGContext*))dlsym(RTLD_DEFAULT, "CGContextGetType"); + + // Optional symbols + sIOSurfaceContextCreate = GET_CGSYM(sIOSurfaceContextCreate, "CGIOSurfaceContextCreate"); + sIOSurfaceContextCreateImage = GET_CGSYM(sIOSurfaceContextCreateImage, "CGIOSurfaceContextCreateImage"); + sIOSurfaceContextGetSurface = GET_CGSYM(sIOSurfaceContextGetSurface, "CGIOSurfaceContextGetSurface"); + + if (!sCreate || !sGetID || !sLookup || !sTexImage || !sGetBaseAddress || + !kPropWidth || !kPropHeight || !kPropBytesPerElem || !kPropIsGlobal || + !sLock || !sUnlock || !sWidth || !sHeight || !kPropBytesPerRow || + !sBytesPerRow) { + CloseLibrary(); + } +} + +void MacIOSurfaceLib::CloseLibrary() { + if (sIOSurfaceFramework) { + dlclose(sIOSurfaceFramework); + } + if (sOpenGLFramework) { + dlclose(sOpenGLFramework); + } + sIOSurfaceFramework = nullptr; + sOpenGLFramework = nullptr; +} + +MacIOSurface::~MacIOSurface() { + CFRelease(mIOSurfacePtr); +} + +TemporaryRef MacIOSurface::CreateIOSurface(int aWidth, int aHeight, + double aContentsScaleFactor, + bool aHasAlpha) { + if (!MacIOSurfaceLib::isInit() || aContentsScaleFactor <= 0) + return nullptr; + + CFMutableDictionaryRef props = ::CFDictionaryCreateMutable( + kCFAllocatorDefault, 4, + &kCFTypeDictionaryKeyCallBacks, + &kCFTypeDictionaryValueCallBacks); + if (!props) + return nullptr; + + int32_t bytesPerElem = 4; + size_t intScaleFactor = ceil(aContentsScaleFactor); + aWidth *= intScaleFactor; + aHeight *= intScaleFactor; + CFNumberRef cfWidth = ::CFNumberCreate(nullptr, kCFNumberSInt32Type, &aWidth); + CFNumberRef cfHeight = ::CFNumberCreate(nullptr, kCFNumberSInt32Type, &aHeight); + CFNumberRef cfBytesPerElem = ::CFNumberCreate(nullptr, kCFNumberSInt32Type, &bytesPerElem); + ::CFDictionaryAddValue(props, MacIOSurfaceLib::kPropWidth, + cfWidth); + ::CFRelease(cfWidth); + ::CFDictionaryAddValue(props, MacIOSurfaceLib::kPropHeight, + cfHeight); + ::CFRelease(cfHeight); + ::CFDictionaryAddValue(props, MacIOSurfaceLib::kPropBytesPerElem, + cfBytesPerElem); + ::CFRelease(cfBytesPerElem); + ::CFDictionaryAddValue(props, MacIOSurfaceLib::kPropIsGlobal, + kCFBooleanTrue); + + IOSurfacePtr surfaceRef = MacIOSurfaceLib::IOSurfaceCreate(props); + ::CFRelease(props); + + if (!surfaceRef) + return nullptr; + + RefPtr ioSurface = new MacIOSurface(surfaceRef, aContentsScaleFactor, aHasAlpha); + if (!ioSurface) { + ::CFRelease(surfaceRef); + return nullptr; + } + + return ioSurface.forget(); +} + +TemporaryRef MacIOSurface::LookupSurface(IOSurfaceID aIOSurfaceID, + double aContentsScaleFactor, + bool aHasAlpha) { + if (!MacIOSurfaceLib::isInit() || aContentsScaleFactor <= 0) + return nullptr; + + IOSurfacePtr surfaceRef = MacIOSurfaceLib::IOSurfaceLookup(aIOSurfaceID); + if (!surfaceRef) + return nullptr; + + RefPtr ioSurface = new MacIOSurface(surfaceRef, aContentsScaleFactor, aHasAlpha); + if (!ioSurface) { + ::CFRelease(surfaceRef); + return nullptr; + } + return ioSurface.forget(); +} + +IOSurfaceID MacIOSurface::GetIOSurfaceID() { + return MacIOSurfaceLib::IOSurfaceGetID(mIOSurfacePtr); +} + +void* MacIOSurface::GetBaseAddress() { + return MacIOSurfaceLib::IOSurfaceGetBaseAddress(mIOSurfacePtr); +} + +size_t MacIOSurface::GetWidth() { + size_t intScaleFactor = ceil(mContentsScaleFactor); + return GetDevicePixelWidth() / intScaleFactor; +} + +size_t MacIOSurface::GetHeight() { + size_t intScaleFactor = ceil(mContentsScaleFactor); + return GetDevicePixelHeight() / intScaleFactor; +} + +size_t MacIOSurface::GetDevicePixelWidth() { + return MacIOSurfaceLib::IOSurfaceGetWidth(mIOSurfacePtr); +} + +size_t MacIOSurface::GetDevicePixelHeight() { + return MacIOSurfaceLib::IOSurfaceGetHeight(mIOSurfacePtr); +} + +size_t MacIOSurface::GetBytesPerRow() { + return MacIOSurfaceLib::IOSurfaceGetBytesPerRow(mIOSurfacePtr); +} + +#define READ_ONLY 0x1 +void MacIOSurface::Lock() { + MacIOSurfaceLib::IOSurfaceLock(mIOSurfacePtr, READ_ONLY, nullptr); +} + +void MacIOSurface::Unlock() { + MacIOSurfaceLib::IOSurfaceUnlock(mIOSurfacePtr, READ_ONLY, nullptr); +} + +#include "SourceSurfaceRawData.h" +using mozilla::gfx::SourceSurface; +using mozilla::gfx::SourceSurfaceRawData; +using mozilla::gfx::IntSize; +using mozilla::gfx::SurfaceFormat; + +TemporaryRef +MacIOSurface::GetAsSurface() { + Lock(); + size_t bytesPerRow = GetBytesPerRow(); + size_t ioWidth = GetDevicePixelWidth(); + size_t ioHeight = GetDevicePixelHeight(); + + unsigned char* ioData = (unsigned char*)GetBaseAddress(); + unsigned char* dataCpy = (unsigned char*)malloc(bytesPerRow*ioHeight); + for (size_t i = 0; i < ioHeight; i++) { + memcpy(dataCpy + i * bytesPerRow, + ioData + i * bytesPerRow, ioWidth * 4); + } + + Unlock(); + + SurfaceFormat format = HasAlpha() ? mozilla::gfx::FORMAT_B8G8R8A8 : + mozilla::gfx::FORMAT_B8G8R8X8; + + RefPtr surf = new SourceSurfaceRawData(); + surf->InitWrappingData(dataCpy, IntSize(ioWidth, ioHeight), bytesPerRow, format, true); + + return surf.forget(); +} + +CGLError +MacIOSurface::CGLTexImageIOSurface2D(CGLContextObj ctx) +{ + return MacIOSurfaceLib::CGLTexImageIOSurface2D(ctx, + GL_TEXTURE_RECTANGLE_ARB, + HasAlpha() ? GL_RGBA : GL_RGB, + GetDevicePixelWidth(), + GetDevicePixelHeight(), + GL_BGRA, + GL_UNSIGNED_INT_8_8_8_8_REV, + mIOSurfacePtr, 0); +} + +static +CGColorSpaceRef CreateSystemColorSpace() { + CGColorSpaceRef cspace = ::CGDisplayCopyColorSpace(::CGMainDisplayID()); + if (!cspace) { + cspace = ::CGColorSpaceCreateDeviceRGB(); + } + return cspace; +} + +CGContextRef MacIOSurface::CreateIOSurfaceContext() { + CGColorSpaceRef cspace = CreateSystemColorSpace(); + CGContextRef ref = MacIOSurfaceLib::IOSurfaceContextCreate(mIOSurfacePtr, + GetDevicePixelWidth(), + GetDevicePixelHeight(), + 8, 32, cspace, 0x2002); + ::CGColorSpaceRelease(cspace); + return ref; +} + +CGImageRef MacIOSurface::CreateImageFromIOSurfaceContext(CGContextRef aContext) { + if (!MacIOSurfaceLib::isInit()) + return nullptr; + + return MacIOSurfaceLib::IOSurfaceContextCreateImage(aContext); +} + +TemporaryRef MacIOSurface::IOSurfaceContextGetSurface(CGContextRef aContext, + double aContentsScaleFactor, + bool aHasAlpha) { + if (!MacIOSurfaceLib::isInit() || aContentsScaleFactor <= 0) + return nullptr; + + IOSurfacePtr surfaceRef = MacIOSurfaceLib::IOSurfaceContextGetSurface(aContext); + if (!surfaceRef) + return nullptr; + + // Retain the IOSurface because MacIOSurface will release it + CFRetain(surfaceRef); + + RefPtr ioSurface = new MacIOSurface(surfaceRef, aContentsScaleFactor, aHasAlpha); + if (!ioSurface) { + ::CFRelease(surfaceRef); + return nullptr; + } + return ioSurface.forget(); +} + + +CGContextType GetContextType(CGContextRef ref) +{ + if (!MacIOSurfaceLib::isInit() || !MacIOSurfaceLib::sCGContextGetTypePtr) + return CG_CONTEXT_TYPE_UNKNOWN; + + unsigned int type = MacIOSurfaceLib::sCGContextGetTypePtr(ref); + if (type == CG_CONTEXT_TYPE_BITMAP) { + return CG_CONTEXT_TYPE_BITMAP; + } else if (type == CG_CONTEXT_TYPE_IOSURFACE) { + return CG_CONTEXT_TYPE_IOSURFACE; + } else { + return CG_CONTEXT_TYPE_UNKNOWN; + } +} + + diff --git a/gfx/2d/MacIOSurface.h b/gfx/2d/MacIOSurface.h index 2cd35595308..7215ec8bbc1 100644 --- a/gfx/2d/MacIOSurface.h +++ b/gfx/2d/MacIOSurface.h @@ -7,6 +7,37 @@ #ifndef MacIOSurface_h__ #define MacIOSurface_h__ #ifdef XP_MACOSX +#include +#include +#include "mozilla/RefPtr.h" + +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 CGLError (*CGLTexImageIOSurface2DFunc) (CGLContextObj ctxt, + GLenum target, GLenum internalFormat, + GLsizei width, GLsizei height, + GLenum format, GLenum type, + IOSurfacePtr ioSurface, GLuint plane); +typedef CGContextRef (*IOSurfaceContextCreateFunc)(CFTypeRef io_surface, + unsigned width, unsigned height, + unsigned bitsPerComponent, unsigned bytes, + CGColorSpaceRef colorSpace, CGBitmapInfo bitmapInfo); +typedef CGImageRef (*IOSurfaceContextCreateImageFunc)(CGContextRef ref); +typedef IOSurfacePtr (*IOSurfaceContextGetSurfaceFunc)(CGContextRef ref); + + #import #include "2D.h" @@ -20,6 +51,15 @@ typedef struct CGContext* CGContextRef; typedef struct CGImage* CGImageRef; typedef uint32_t IOSurfaceID; +enum CGContextType { + CG_CONTEXT_TYPE_UNKNOWN = 0, + // These are found by inspection, it's possible they could be changed + CG_CONTEXT_TYPE_BITMAP = 4, + CG_CONTEXT_TYPE_IOSURFACE = 8 +}; + +CGContextType GetContextType(CGContextRef ref); + class MacIOSurface : public mozilla::RefCounted { public: typedef mozilla::gfx::SourceSurface SourceSurface; @@ -69,5 +109,67 @@ private: bool mHasAlpha; }; +class MacIOSurfaceLib: public MacIOSurface { +public: + static void *sIOSurfaceFramework; + static void *sOpenGLFramework; + static void *sCoreGraphicsFramework; + static bool isLoaded; + static IOSurfaceCreateFunc sCreate; + static IOSurfaceGetIDFunc sGetID; + static IOSurfaceLookupFunc sLookup; + static IOSurfaceGetBaseAddressFunc sGetBaseAddress; + static IOSurfaceLockFunc sLock; + static IOSurfaceUnlockFunc sUnlock; + static IOSurfaceGetWidthFunc sWidth; + static IOSurfaceGetHeightFunc sHeight; + static IOSurfaceGetBytesPerRowFunc sBytesPerRow; + static CGLTexImageIOSurface2DFunc sTexImage; + static IOSurfaceContextCreateFunc sIOSurfaceContextCreate; + static IOSurfaceContextCreateImageFunc sIOSurfaceContextCreateImage; + static IOSurfaceContextGetSurfaceFunc sIOSurfaceContextGetSurface; + static CFStringRef kPropWidth; + static CFStringRef kPropHeight; + static CFStringRef kPropBytesPerElem; + static CFStringRef kPropBytesPerRow; + static CFStringRef kPropIsGlobal; + + static bool isInit(); + static CFStringRef GetIOConst(const char* symbole); + static IOSurfacePtr IOSurfaceCreate(CFDictionaryRef properties); + static IOSurfacePtr IOSurfaceLookup(IOSurfaceID aIOSurfaceID); + static IOSurfaceID IOSurfaceGetID(IOSurfacePtr aIOSurfacePtr); + static void *IOSurfaceGetBaseAddress(IOSurfacePtr aIOSurfacePtr); + static size_t IOSurfaceGetWidth(IOSurfacePtr aIOSurfacePtr); + static size_t IOSurfaceGetHeight(IOSurfacePtr aIOSurfacePtr); + static size_t IOSurfaceGetBytesPerRow(IOSurfacePtr aIOSurfacePtr); + static IOReturn IOSurfaceLock(IOSurfacePtr aIOSurfacePtr, + uint32_t options, uint32_t *seed); + static IOReturn IOSurfaceUnlock(IOSurfacePtr aIOSurfacePtr, + uint32_t options, uint32_t *seed); + static CGLError CGLTexImageIOSurface2D(CGLContextObj ctxt, + GLenum target, GLenum internalFormat, + GLsizei width, GLsizei height, + GLenum format, GLenum type, + IOSurfacePtr ioSurface, GLuint plane); + static CGContextRef IOSurfaceContextCreate(IOSurfacePtr aIOSurfacePtr, + unsigned aWidth, unsigned aHeight, + unsigned aBitsPerCompoent, unsigned aBytes, + CGColorSpaceRef aColorSpace, CGBitmapInfo bitmapInfo); + static CGImageRef IOSurfaceContextCreateImage(CGContextRef ref); + static IOSurfacePtr IOSurfaceContextGetSurface(CGContextRef ref); + static unsigned int (*sCGContextGetTypePtr) (CGContextRef); + static void LoadLibrary(); + static void CloseLibrary(); + + // Static deconstructor + static class LibraryUnloader { + public: + ~LibraryUnloader() { + CloseLibrary(); + } + } sLibraryUnloader; +}; + #endif #endif diff --git a/gfx/2d/QuartzSupport.h b/gfx/2d/QuartzSupport.h index 558a64fca92..dd9a1968426 100644 --- a/gfx/2d/QuartzSupport.h +++ b/gfx/2d/QuartzSupport.h @@ -93,15 +93,6 @@ private: double mContentsScaleFactor; }; -enum CGContextType { - CG_CONTEXT_TYPE_UNKNOWN = 0, - // These are found by inspection, it's possible they could be changed - CG_CONTEXT_TYPE_BITMAP = 4, - CG_CONTEXT_TYPE_IOSURFACE = 8 -}; - -CGContextType GetContextType(CGContextRef ref); - #endif // XP_MACOSX #endif // nsCoreAnimationSupport_h__ diff --git a/gfx/2d/QuartzSupport.mm b/gfx/2d/QuartzSupport.mm index 00c824a8b8c..e9cbd192de7 100644 --- a/gfx/2d/QuartzSupport.mm +++ b/gfx/2d/QuartzSupport.mm @@ -6,6 +6,7 @@ #include "QuartzSupport.h" #include "nsDebug.h" +#include "MacIOSurface.h" #import #import @@ -27,435 +28,6 @@ using mozilla::RefPtr; using mozilla::TemporaryRef; -// IOSurface signatures -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 CGLError (*CGLTexImageIOSurface2DFunc) (CGLContextObj ctxt, - GLenum target, GLenum internalFormat, - GLsizei width, GLsizei height, - GLenum format, GLenum type, - IOSurfacePtr ioSurface, GLuint plane); -typedef CGContextRef (*IOSurfaceContextCreateFunc)(CFTypeRef io_surface, - unsigned width, unsigned height, - unsigned bitsPerComponent, unsigned bytes, - CGColorSpaceRef colorSpace, CGBitmapInfo bitmapInfo); -typedef CGImageRef (*IOSurfaceContextCreateImageFunc)(CGContextRef ref); -typedef IOSurfacePtr (*IOSurfaceContextGetSurfaceFunc)(CGContextRef ref); - -#define GET_CONST(const_name) \ - ((CFStringRef*) dlsym(sIOSurfaceFramework, const_name)) -#define GET_IOSYM(dest,sym_name) \ - (typeof(dest)) dlsym(sIOSurfaceFramework, sym_name) -#define GET_CGLSYM(dest,sym_name) \ - (typeof(dest)) dlsym(sOpenGLFramework, sym_name) -#define GET_CGSYM(dest,sym_name) \ - (typeof(dest)) dlsym(sCoreGraphicsFramework, sym_name) - -class MacIOSurfaceLib: public MacIOSurface { -public: - static void *sIOSurfaceFramework; - static void *sOpenGLFramework; - static void *sCoreGraphicsFramework; - static bool isLoaded; - static IOSurfaceCreateFunc sCreate; - static IOSurfaceGetIDFunc sGetID; - static IOSurfaceLookupFunc sLookup; - static IOSurfaceGetBaseAddressFunc sGetBaseAddress; - static IOSurfaceLockFunc sLock; - static IOSurfaceUnlockFunc sUnlock; - static IOSurfaceGetWidthFunc sWidth; - static IOSurfaceGetHeightFunc sHeight; - static IOSurfaceGetBytesPerRowFunc sBytesPerRow; - static CGLTexImageIOSurface2DFunc sTexImage; - static IOSurfaceContextCreateFunc sIOSurfaceContextCreate; - static IOSurfaceContextCreateImageFunc sIOSurfaceContextCreateImage; - static IOSurfaceContextGetSurfaceFunc sIOSurfaceContextGetSurface; - static CFStringRef kPropWidth; - static CFStringRef kPropHeight; - static CFStringRef kPropBytesPerElem; - static CFStringRef kPropBytesPerRow; - static CFStringRef kPropIsGlobal; - - static bool isInit(); - static CFStringRef GetIOConst(const char* symbole); - static IOSurfacePtr IOSurfaceCreate(CFDictionaryRef properties); - static IOSurfacePtr IOSurfaceLookup(IOSurfaceID aIOSurfaceID); - static IOSurfaceID IOSurfaceGetID(IOSurfacePtr aIOSurfacePtr); - static void *IOSurfaceGetBaseAddress(IOSurfacePtr aIOSurfacePtr); - static size_t IOSurfaceGetWidth(IOSurfacePtr aIOSurfacePtr); - static size_t IOSurfaceGetHeight(IOSurfacePtr aIOSurfacePtr); - static size_t IOSurfaceGetBytesPerRow(IOSurfacePtr aIOSurfacePtr); - static IOReturn IOSurfaceLock(IOSurfacePtr aIOSurfacePtr, - uint32_t options, uint32_t *seed); - static IOReturn IOSurfaceUnlock(IOSurfacePtr aIOSurfacePtr, - uint32_t options, uint32_t *seed); - static CGLError CGLTexImageIOSurface2D(CGLContextObj ctxt, - GLenum target, GLenum internalFormat, - GLsizei width, GLsizei height, - GLenum format, GLenum type, - IOSurfacePtr ioSurface, GLuint plane); - static CGContextRef IOSurfaceContextCreate(IOSurfacePtr aIOSurfacePtr, - unsigned aWidth, unsigned aHeight, - unsigned aBitsPerCompoent, unsigned aBytes, - CGColorSpaceRef aColorSpace, CGBitmapInfo bitmapInfo); - static CGImageRef IOSurfaceContextCreateImage(CGContextRef ref); - static IOSurfacePtr IOSurfaceContextGetSurface(CGContextRef ref); - static unsigned int (*sCGContextGetTypePtr) (CGContextRef); - static void LoadLibrary(); - static void CloseLibrary(); - - // Static deconstructor - static class LibraryUnloader { - public: - ~LibraryUnloader() { - CloseLibrary(); - } - } sLibraryUnloader; -}; - -MacIOSurfaceLib::LibraryUnloader MacIOSurfaceLib::sLibraryUnloader; -bool MacIOSurfaceLib::isLoaded = false; -void* MacIOSurfaceLib::sIOSurfaceFramework; -void* MacIOSurfaceLib::sOpenGLFramework; -void* MacIOSurfaceLib::sCoreGraphicsFramework; -IOSurfaceCreateFunc MacIOSurfaceLib::sCreate; -IOSurfaceGetIDFunc MacIOSurfaceLib::sGetID; -IOSurfaceLookupFunc MacIOSurfaceLib::sLookup; -IOSurfaceGetBaseAddressFunc MacIOSurfaceLib::sGetBaseAddress; -IOSurfaceGetWidthFunc MacIOSurfaceLib::sWidth; -IOSurfaceGetHeightFunc MacIOSurfaceLib::sHeight; -IOSurfaceGetBytesPerRowFunc MacIOSurfaceLib::sBytesPerRow; -IOSurfaceLockFunc MacIOSurfaceLib::sLock; -IOSurfaceUnlockFunc MacIOSurfaceLib::sUnlock; -CGLTexImageIOSurface2DFunc MacIOSurfaceLib::sTexImage; -IOSurfaceContextCreateFunc MacIOSurfaceLib::sIOSurfaceContextCreate; -IOSurfaceContextCreateImageFunc MacIOSurfaceLib::sIOSurfaceContextCreateImage; -IOSurfaceContextGetSurfaceFunc MacIOSurfaceLib::sIOSurfaceContextGetSurface; -unsigned int (*MacIOSurfaceLib::sCGContextGetTypePtr) (CGContextRef) = nullptr; - -CFStringRef MacIOSurfaceLib::kPropWidth; -CFStringRef MacIOSurfaceLib::kPropHeight; -CFStringRef MacIOSurfaceLib::kPropBytesPerElem; -CFStringRef MacIOSurfaceLib::kPropBytesPerRow; -CFStringRef MacIOSurfaceLib::kPropIsGlobal; - -bool MacIOSurfaceLib::isInit() { - // Guard against trying to reload the library - // if it is not available. - if (!isLoaded) - LoadLibrary(); - if (!sIOSurfaceFramework) { - NS_ERROR("MacIOSurfaceLib failed to initialize"); - } - return sIOSurfaceFramework; -} - -IOSurfacePtr MacIOSurfaceLib::IOSurfaceCreate(CFDictionaryRef properties) { - return sCreate(properties); -} - -IOSurfacePtr MacIOSurfaceLib::IOSurfaceLookup(IOSurfaceID aIOSurfaceID) { - return sLookup(aIOSurfaceID); -} - -IOSurfaceID MacIOSurfaceLib::IOSurfaceGetID(IOSurfacePtr aIOSurfacePtr) { - return sGetID(aIOSurfacePtr); -} - -void* MacIOSurfaceLib::IOSurfaceGetBaseAddress(IOSurfacePtr aIOSurfacePtr) { - return sGetBaseAddress(aIOSurfacePtr); -} - -size_t MacIOSurfaceLib::IOSurfaceGetWidth(IOSurfacePtr aIOSurfacePtr) { - return sWidth(aIOSurfacePtr); -} - -size_t MacIOSurfaceLib::IOSurfaceGetHeight(IOSurfacePtr aIOSurfacePtr) { - return sHeight(aIOSurfacePtr); -} - -size_t MacIOSurfaceLib::IOSurfaceGetBytesPerRow(IOSurfacePtr aIOSurfacePtr) { - return sBytesPerRow(aIOSurfacePtr); -} - -IOReturn MacIOSurfaceLib::IOSurfaceLock(IOSurfacePtr aIOSurfacePtr, - uint32_t options, uint32_t *seed) { - return sLock(aIOSurfacePtr, options, seed); -} - -IOReturn MacIOSurfaceLib::IOSurfaceUnlock(IOSurfacePtr aIOSurfacePtr, - uint32_t options, uint32_t *seed) { - return sUnlock(aIOSurfacePtr, options, seed); -} - -CGLError MacIOSurfaceLib::CGLTexImageIOSurface2D(CGLContextObj ctxt, - GLenum target, GLenum internalFormat, - GLsizei width, GLsizei height, - GLenum format, GLenum type, - IOSurfacePtr ioSurface, GLuint plane) { - return sTexImage(ctxt, target, internalFormat, width, height, - format, type, ioSurface, plane); -} - -CGContextRef MacIOSurfaceLib::IOSurfaceContextCreate(IOSurfacePtr aIOSurfacePtr, - unsigned aWidth, unsigned aHeight, - unsigned aBitsPerComponent, unsigned aBytes, - CGColorSpaceRef aColorSpace, CGBitmapInfo bitmapInfo) { - if (!sIOSurfaceContextCreate) - return nullptr; - return sIOSurfaceContextCreate(aIOSurfacePtr, aWidth, aHeight, aBitsPerComponent, aBytes, aColorSpace, bitmapInfo); -} - -CGImageRef MacIOSurfaceLib::IOSurfaceContextCreateImage(CGContextRef aContext) { - if (!sIOSurfaceContextCreateImage) - return nullptr; - return sIOSurfaceContextCreateImage(aContext); -} - -IOSurfacePtr MacIOSurfaceLib::IOSurfaceContextGetSurface(CGContextRef aContext) { - if (!sIOSurfaceContextGetSurface) - return nullptr; - return sIOSurfaceContextGetSurface(aContext); -} - -CFStringRef MacIOSurfaceLib::GetIOConst(const char* symbole) { - CFStringRef *address = (CFStringRef*)dlsym(sIOSurfaceFramework, symbole); - if (!address) - return nullptr; - - return *address; -} - -void MacIOSurfaceLib::LoadLibrary() { - if (isLoaded) { - return; - } - isLoaded = true; - sIOSurfaceFramework = dlopen(IOSURFACE_FRAMEWORK_PATH, - RTLD_LAZY | RTLD_LOCAL); - sOpenGLFramework = dlopen(OPENGL_FRAMEWORK_PATH, - RTLD_LAZY | RTLD_LOCAL); - - sCoreGraphicsFramework = dlopen(COREGRAPHICS_FRAMEWORK_PATH, - RTLD_LAZY | RTLD_LOCAL); - if (!sIOSurfaceFramework || !sOpenGLFramework || !sCoreGraphicsFramework) { - if (sIOSurfaceFramework) - dlclose(sIOSurfaceFramework); - if (sOpenGLFramework) - dlclose(sOpenGLFramework); - if (sCoreGraphicsFramework) - dlclose(sCoreGraphicsFramework); - sIOSurfaceFramework = nullptr; - sOpenGLFramework = nullptr; - sCoreGraphicsFramework = nullptr; - return; - } - - kPropWidth = GetIOConst("kIOSurfaceWidth"); - kPropHeight = GetIOConst("kIOSurfaceHeight"); - kPropBytesPerElem = GetIOConst("kIOSurfaceBytesPerElement"); - kPropBytesPerRow = GetIOConst("kIOSurfaceBytesPerRow"); - kPropIsGlobal = GetIOConst("kIOSurfaceIsGlobal"); - sCreate = GET_IOSYM(sCreate, "IOSurfaceCreate"); - sGetID = GET_IOSYM(sGetID, "IOSurfaceGetID"); - sWidth = GET_IOSYM(sWidth, "IOSurfaceGetWidth"); - sHeight = GET_IOSYM(sHeight, "IOSurfaceGetHeight"); - sBytesPerRow = GET_IOSYM(sBytesPerRow, "IOSurfaceGetBytesPerRow"); - sLookup = GET_IOSYM(sLookup, "IOSurfaceLookup"); - sLock = GET_IOSYM(sLock, "IOSurfaceLock"); - sUnlock = GET_IOSYM(sUnlock, "IOSurfaceUnlock"); - sGetBaseAddress = GET_IOSYM(sGetBaseAddress, "IOSurfaceGetBaseAddress"); - sTexImage = GET_CGLSYM(sTexImage, "CGLTexImageIOSurface2D"); - sCGContextGetTypePtr = (unsigned int (*)(CGContext*))dlsym(RTLD_DEFAULT, "CGContextGetType"); - - // Optional symbols - sIOSurfaceContextCreate = GET_CGSYM(sIOSurfaceContextCreate, "CGIOSurfaceContextCreate"); - sIOSurfaceContextCreateImage = GET_CGSYM(sIOSurfaceContextCreateImage, "CGIOSurfaceContextCreateImage"); - sIOSurfaceContextGetSurface = GET_CGSYM(sIOSurfaceContextGetSurface, "CGIOSurfaceContextGetSurface"); - - if (!sCreate || !sGetID || !sLookup || !sTexImage || !sGetBaseAddress || - !kPropWidth || !kPropHeight || !kPropBytesPerElem || !kPropIsGlobal || - !sLock || !sUnlock || !sWidth || !sHeight || !kPropBytesPerRow || - !sBytesPerRow) { - CloseLibrary(); - } -} - -void MacIOSurfaceLib::CloseLibrary() { - if (sIOSurfaceFramework) { - dlclose(sIOSurfaceFramework); - } - if (sOpenGLFramework) { - dlclose(sOpenGLFramework); - } - sIOSurfaceFramework = nullptr; - sOpenGLFramework = nullptr; -} - -MacIOSurface::~MacIOSurface() { - CFRelease(mIOSurfacePtr); -} - -TemporaryRef MacIOSurface::CreateIOSurface(int aWidth, int aHeight, - double aContentsScaleFactor, - bool aHasAlpha) { - if (!MacIOSurfaceLib::isInit() || aContentsScaleFactor <= 0) - return nullptr; - - CFMutableDictionaryRef props = ::CFDictionaryCreateMutable( - kCFAllocatorDefault, 4, - &kCFTypeDictionaryKeyCallBacks, - &kCFTypeDictionaryValueCallBacks); - if (!props) - return nullptr; - - int32_t bytesPerElem = 4; - size_t intScaleFactor = ceil(aContentsScaleFactor); - aWidth *= intScaleFactor; - aHeight *= intScaleFactor; - CFNumberRef cfWidth = ::CFNumberCreate(nullptr, kCFNumberSInt32Type, &aWidth); - CFNumberRef cfHeight = ::CFNumberCreate(nullptr, kCFNumberSInt32Type, &aHeight); - CFNumberRef cfBytesPerElem = ::CFNumberCreate(nullptr, kCFNumberSInt32Type, &bytesPerElem); - ::CFDictionaryAddValue(props, MacIOSurfaceLib::kPropWidth, - cfWidth); - ::CFRelease(cfWidth); - ::CFDictionaryAddValue(props, MacIOSurfaceLib::kPropHeight, - cfHeight); - ::CFRelease(cfHeight); - ::CFDictionaryAddValue(props, MacIOSurfaceLib::kPropBytesPerElem, - cfBytesPerElem); - ::CFRelease(cfBytesPerElem); - ::CFDictionaryAddValue(props, MacIOSurfaceLib::kPropIsGlobal, - kCFBooleanTrue); - - IOSurfacePtr surfaceRef = MacIOSurfaceLib::IOSurfaceCreate(props); - ::CFRelease(props); - - if (!surfaceRef) - return nullptr; - - RefPtr ioSurface = new MacIOSurface(surfaceRef, aContentsScaleFactor, aHasAlpha); - if (!ioSurface) { - ::CFRelease(surfaceRef); - return nullptr; - } - - return ioSurface.forget(); -} - -TemporaryRef MacIOSurface::LookupSurface(IOSurfaceID aIOSurfaceID, - double aContentsScaleFactor, - bool aHasAlpha) { - if (!MacIOSurfaceLib::isInit() || aContentsScaleFactor <= 0) - return nullptr; - - IOSurfacePtr surfaceRef = MacIOSurfaceLib::IOSurfaceLookup(aIOSurfaceID); - if (!surfaceRef) - return nullptr; - - RefPtr ioSurface = new MacIOSurface(surfaceRef, aContentsScaleFactor, aHasAlpha); - if (!ioSurface) { - ::CFRelease(surfaceRef); - return nullptr; - } - return ioSurface.forget(); -} - -IOSurfaceID MacIOSurface::GetIOSurfaceID() { - return MacIOSurfaceLib::IOSurfaceGetID(mIOSurfacePtr); -} - -void* MacIOSurface::GetBaseAddress() { - return MacIOSurfaceLib::IOSurfaceGetBaseAddress(mIOSurfacePtr); -} - -size_t MacIOSurface::GetWidth() { - size_t intScaleFactor = ceil(mContentsScaleFactor); - return GetDevicePixelWidth() / intScaleFactor; -} - -size_t MacIOSurface::GetHeight() { - size_t intScaleFactor = ceil(mContentsScaleFactor); - return GetDevicePixelHeight() / intScaleFactor; -} - -size_t MacIOSurface::GetDevicePixelWidth() { - return MacIOSurfaceLib::IOSurfaceGetWidth(mIOSurfacePtr); -} - -size_t MacIOSurface::GetDevicePixelHeight() { - return MacIOSurfaceLib::IOSurfaceGetHeight(mIOSurfacePtr); -} - -size_t MacIOSurface::GetBytesPerRow() { - return MacIOSurfaceLib::IOSurfaceGetBytesPerRow(mIOSurfacePtr); -} - -#define READ_ONLY 0x1 -void MacIOSurface::Lock() { - MacIOSurfaceLib::IOSurfaceLock(mIOSurfacePtr, READ_ONLY, nullptr); -} - -void MacIOSurface::Unlock() { - MacIOSurfaceLib::IOSurfaceUnlock(mIOSurfacePtr, READ_ONLY, nullptr); -} - -#include "SourceSurfaceRawData.h" -using mozilla::gfx::SourceSurface; -using mozilla::gfx::SourceSurfaceRawData; -using mozilla::gfx::IntSize; -using mozilla::gfx::SurfaceFormat; - -TemporaryRef -MacIOSurface::GetAsSurface() { - Lock(); - size_t bytesPerRow = GetBytesPerRow(); - size_t ioWidth = GetDevicePixelWidth(); - size_t ioHeight = GetDevicePixelHeight(); - - unsigned char* ioData = (unsigned char*)GetBaseAddress(); - unsigned char* dataCpy = (unsigned char*)malloc(bytesPerRow*ioHeight); - for (size_t i = 0; i < ioHeight; i++) { - memcpy(dataCpy + i * bytesPerRow, - ioData + i * bytesPerRow, ioWidth * 4); - } - - Unlock(); - - SurfaceFormat format = HasAlpha() ? mozilla::gfx::FORMAT_B8G8R8A8 : - mozilla::gfx::FORMAT_B8G8R8X8; - - RefPtr surf = new SourceSurfaceRawData(); - surf->InitWrappingData(dataCpy, IntSize(ioWidth, ioHeight), bytesPerRow, format, true); - - return surf.forget(); -} - -CGLError -MacIOSurface::CGLTexImageIOSurface2D(CGLContextObj ctxt) -{ - return MacIOSurfaceLib::CGLTexImageIOSurface2D(ctxt, - GL_TEXTURE_RECTANGLE_ARB, - HasAlpha() ? LOCAL_GL_RGBA : LOCAL_GL_RGB, - GetDevicePixelWidth(), - GetDevicePixelHeight(), - LOCAL_GL_BGRA, - LOCAL_GL_UNSIGNED_INT_8_8_8_8_REV, - mIOSurfacePtr, 0); -} - CGColorSpaceRef CreateSystemColorSpace() { CGColorSpaceRef cspace = ::CGDisplayCopyColorSpace(::CGMainDisplayID()); if (!cspace) { @@ -464,16 +36,6 @@ CGColorSpaceRef CreateSystemColorSpace() { return cspace; } -CGContextRef MacIOSurface::CreateIOSurfaceContext() { - CGColorSpaceRef cspace = CreateSystemColorSpace(); - CGContextRef ref = MacIOSurfaceLib::IOSurfaceContextCreate(mIOSurfacePtr, - GetDevicePixelWidth(), - GetDevicePixelHeight(), - 8, 32, cspace, 0x2002); - ::CGColorSpaceRelease(cspace); - return ref; -} - nsCARenderer::~nsCARenderer() { Destroy(); } @@ -1066,49 +628,3 @@ void nsCARenderer::SaveToDisk(MacIOSurface *surf) { } #endif - -CGImageRef MacIOSurface::CreateImageFromIOSurfaceContext(CGContextRef aContext) { - if (!MacIOSurfaceLib::isInit()) - return nullptr; - - return MacIOSurfaceLib::IOSurfaceContextCreateImage(aContext); -} - -TemporaryRef MacIOSurface::IOSurfaceContextGetSurface(CGContextRef aContext, - double aContentsScaleFactor, - bool aHasAlpha) { - if (!MacIOSurfaceLib::isInit() || aContentsScaleFactor <= 0) - return nullptr; - - IOSurfacePtr surfaceRef = MacIOSurfaceLib::IOSurfaceContextGetSurface(aContext); - if (!surfaceRef) - return nullptr; - - // Retain the IOSurface because MacIOSurface will release it - CFRetain(surfaceRef); - - RefPtr ioSurface = new MacIOSurface(surfaceRef, aContentsScaleFactor, aHasAlpha); - if (!ioSurface) { - ::CFRelease(surfaceRef); - return nullptr; - } - return ioSurface.forget(); -} - - -CGContextType GetContextType(CGContextRef ref) -{ - if (!MacIOSurfaceLib::isInit() || !MacIOSurfaceLib::sCGContextGetTypePtr) - return CG_CONTEXT_TYPE_UNKNOWN; - - unsigned int type = MacIOSurfaceLib::sCGContextGetTypePtr(ref); - if (type == CG_CONTEXT_TYPE_BITMAP) { - return CG_CONTEXT_TYPE_BITMAP; - } else if (type == CG_CONTEXT_TYPE_IOSURFACE) { - return CG_CONTEXT_TYPE_IOSURFACE; - } else { - return CG_CONTEXT_TYPE_UNKNOWN; - } -} - - diff --git a/gfx/2d/SourceSurfaceCG.cpp b/gfx/2d/SourceSurfaceCG.cpp index bbd1a845d40..e4ad395cdf5 100644 --- a/gfx/2d/SourceSurfaceCG.cpp +++ b/gfx/2d/SourceSurfaceCG.cpp @@ -6,7 +6,7 @@ #include "SourceSurfaceCG.h" #include "DrawTargetCG.h" -#include "QuartzSupport.h" +#include "MacIOSurface.h" #include "Tools.h" namespace mozilla { diff --git a/gfx/2d/moz.build b/gfx/2d/moz.build index 362996824da..09ba838d49c 100644 --- a/gfx/2d/moz.build +++ b/gfx/2d/moz.build @@ -102,6 +102,7 @@ SOURCES += [ if CONFIG['MOZ_WIDGET_TOOLKIT'] == 'cocoa': SOURCES += [ + 'MacIOSurface.cpp', 'QuartzSupport.mm', ]