michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: // vim:set ts=2 sts=2 sw=2 et cin: michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef MacIOSurface_h__ michael@0: #define MacIOSurface_h__ michael@0: #ifdef XP_MACOSX michael@0: #include michael@0: #include michael@0: #include "mozilla/RefPtr.h" michael@0: michael@0: typedef CFTypeRef IOSurfacePtr; michael@0: typedef IOSurfacePtr (*IOSurfaceCreateFunc) (CFDictionaryRef properties); michael@0: typedef IOSurfacePtr (*IOSurfaceLookupFunc) (uint32_t io_surface_id); michael@0: typedef IOSurfaceID (*IOSurfaceGetIDFunc) (CFTypeRef io_surface); michael@0: typedef IOReturn (*IOSurfaceLockFunc) (CFTypeRef io_surface, michael@0: uint32_t options, michael@0: uint32_t *seed); michael@0: typedef IOReturn (*IOSurfaceUnlockFunc) (CFTypeRef io_surface, michael@0: uint32_t options, michael@0: uint32_t *seed); michael@0: typedef void* (*IOSurfaceGetBaseAddressFunc) (CFTypeRef io_surface); michael@0: typedef size_t (*IOSurfaceGetWidthFunc) (IOSurfacePtr io_surface); michael@0: typedef size_t (*IOSurfaceGetHeightFunc) (IOSurfacePtr io_surface); michael@0: typedef size_t (*IOSurfaceGetBytesPerRowFunc) (IOSurfacePtr io_surface); michael@0: typedef CGLError (*CGLTexImageIOSurface2DFunc) (CGLContextObj ctxt, michael@0: GLenum target, GLenum internalFormat, michael@0: GLsizei width, GLsizei height, michael@0: GLenum format, GLenum type, michael@0: IOSurfacePtr ioSurface, GLuint plane); michael@0: typedef CGContextRef (*IOSurfaceContextCreateFunc)(CFTypeRef io_surface, michael@0: unsigned width, unsigned height, michael@0: unsigned bitsPerComponent, unsigned bytes, michael@0: CGColorSpaceRef colorSpace, CGBitmapInfo bitmapInfo); michael@0: typedef CGImageRef (*IOSurfaceContextCreateImageFunc)(CGContextRef ref); michael@0: typedef IOSurfacePtr (*IOSurfaceContextGetSurfaceFunc)(CGContextRef ref); michael@0: michael@0: michael@0: michael@0: #import michael@0: #include "2D.h" michael@0: #include "mozilla/RefPtr.h" michael@0: michael@0: struct _CGLContextObject; michael@0: michael@0: typedef _CGLContextObject* CGLContextObj; michael@0: typedef struct CGContext* CGContextRef; michael@0: typedef struct CGImage* CGImageRef; michael@0: typedef uint32_t IOSurfaceID; michael@0: michael@0: enum CGContextType { michael@0: CG_CONTEXT_TYPE_UNKNOWN = 0, michael@0: // These are found by inspection, it's possible they could be changed michael@0: CG_CONTEXT_TYPE_BITMAP = 4, michael@0: CG_CONTEXT_TYPE_IOSURFACE = 8 michael@0: }; michael@0: michael@0: CGContextType GetContextType(CGContextRef ref); michael@0: michael@0: class MacIOSurface : public mozilla::RefCounted { michael@0: public: michael@0: MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(MacIOSurface) michael@0: typedef mozilla::gfx::SourceSurface SourceSurface; michael@0: michael@0: static mozilla::TemporaryRef CreateIOSurface(int aWidth, int aHeight, michael@0: double aContentsScaleFactor = 1.0, michael@0: bool aHasAlpha = true); michael@0: static void ReleaseIOSurface(MacIOSurface *aIOSurface); michael@0: static mozilla::TemporaryRef LookupSurface(IOSurfaceID aSurfaceID, michael@0: double aContentsScaleFactor = 1.0, michael@0: bool aHasAlpha = true); michael@0: michael@0: MacIOSurface(const void *aIOSurfacePtr, double aContentsScaleFactor = 1.0, bool aHasAlpha = true) michael@0: : mIOSurfacePtr(aIOSurfacePtr), mContentsScaleFactor(aContentsScaleFactor), mHasAlpha(aHasAlpha) {} michael@0: virtual ~MacIOSurface(); michael@0: IOSurfaceID GetIOSurfaceID(); michael@0: void *GetBaseAddress(); michael@0: // GetWidth() and GetHeight() return values in "display pixels". A michael@0: // "display pixel" is the smallest fully addressable part of a display. michael@0: // But in HiDPI modes each "display pixel" corresponds to more than one michael@0: // device pixel. Use GetDevicePixel**() to get device pixels. michael@0: size_t GetWidth(); michael@0: size_t GetHeight(); michael@0: double GetContentsScaleFactor() { return mContentsScaleFactor; } michael@0: size_t GetDevicePixelWidth(); michael@0: size_t GetDevicePixelHeight(); michael@0: size_t GetBytesPerRow(); michael@0: void Lock(); michael@0: void Unlock(); michael@0: bool HasAlpha() { return mHasAlpha; } michael@0: // We would like to forward declare NSOpenGLContext, but it is an @interface michael@0: // and this file is also used from c++, so we use a void *. michael@0: CGLError CGLTexImageIOSurface2D(CGLContextObj ctxt); michael@0: mozilla::TemporaryRef GetAsSurface(); michael@0: CGContextRef CreateIOSurfaceContext(); michael@0: michael@0: // FIXME This doesn't really belong here michael@0: static CGImageRef CreateImageFromIOSurfaceContext(CGContextRef aContext); michael@0: static mozilla::TemporaryRef IOSurfaceContextGetSurface(CGContextRef aContext, michael@0: double aContentsScaleFactor = 1.0, michael@0: bool aHasAlpha = true); michael@0: michael@0: private: michael@0: friend class nsCARenderer; michael@0: const void* mIOSurfacePtr; michael@0: double mContentsScaleFactor; michael@0: bool mHasAlpha; michael@0: }; michael@0: michael@0: class MacIOSurfaceLib: public MacIOSurface { michael@0: public: michael@0: MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(MacIOSurfaceLib) michael@0: static void *sIOSurfaceFramework; michael@0: static void *sOpenGLFramework; michael@0: static void *sCoreGraphicsFramework; michael@0: static bool isLoaded; michael@0: static IOSurfaceCreateFunc sCreate; michael@0: static IOSurfaceGetIDFunc sGetID; michael@0: static IOSurfaceLookupFunc sLookup; michael@0: static IOSurfaceGetBaseAddressFunc sGetBaseAddress; michael@0: static IOSurfaceLockFunc sLock; michael@0: static IOSurfaceUnlockFunc sUnlock; michael@0: static IOSurfaceGetWidthFunc sWidth; michael@0: static IOSurfaceGetHeightFunc sHeight; michael@0: static IOSurfaceGetBytesPerRowFunc sBytesPerRow; michael@0: static CGLTexImageIOSurface2DFunc sTexImage; michael@0: static IOSurfaceContextCreateFunc sIOSurfaceContextCreate; michael@0: static IOSurfaceContextCreateImageFunc sIOSurfaceContextCreateImage; michael@0: static IOSurfaceContextGetSurfaceFunc sIOSurfaceContextGetSurface; michael@0: static CFStringRef kPropWidth; michael@0: static CFStringRef kPropHeight; michael@0: static CFStringRef kPropBytesPerElem; michael@0: static CFStringRef kPropBytesPerRow; michael@0: static CFStringRef kPropIsGlobal; michael@0: michael@0: static bool isInit(); michael@0: static CFStringRef GetIOConst(const char* symbole); michael@0: static IOSurfacePtr IOSurfaceCreate(CFDictionaryRef properties); michael@0: static IOSurfacePtr IOSurfaceLookup(IOSurfaceID aIOSurfaceID); michael@0: static IOSurfaceID IOSurfaceGetID(IOSurfacePtr aIOSurfacePtr); michael@0: static void *IOSurfaceGetBaseAddress(IOSurfacePtr aIOSurfacePtr); michael@0: static size_t IOSurfaceGetWidth(IOSurfacePtr aIOSurfacePtr); michael@0: static size_t IOSurfaceGetHeight(IOSurfacePtr aIOSurfacePtr); michael@0: static size_t IOSurfaceGetBytesPerRow(IOSurfacePtr aIOSurfacePtr); michael@0: static IOReturn IOSurfaceLock(IOSurfacePtr aIOSurfacePtr, michael@0: uint32_t options, uint32_t *seed); michael@0: static IOReturn IOSurfaceUnlock(IOSurfacePtr aIOSurfacePtr, michael@0: uint32_t options, uint32_t *seed); michael@0: static CGLError CGLTexImageIOSurface2D(CGLContextObj ctxt, michael@0: GLenum target, GLenum internalFormat, michael@0: GLsizei width, GLsizei height, michael@0: GLenum format, GLenum type, michael@0: IOSurfacePtr ioSurface, GLuint plane); michael@0: static CGContextRef IOSurfaceContextCreate(IOSurfacePtr aIOSurfacePtr, michael@0: unsigned aWidth, unsigned aHeight, michael@0: unsigned aBitsPerCompoent, unsigned aBytes, michael@0: CGColorSpaceRef aColorSpace, CGBitmapInfo bitmapInfo); michael@0: static CGImageRef IOSurfaceContextCreateImage(CGContextRef ref); michael@0: static IOSurfacePtr IOSurfaceContextGetSurface(CGContextRef ref); michael@0: static unsigned int (*sCGContextGetTypePtr) (CGContextRef); michael@0: static void LoadLibrary(); michael@0: static void CloseLibrary(); michael@0: michael@0: // Static deconstructor michael@0: static class LibraryUnloader { michael@0: public: michael@0: ~LibraryUnloader() { michael@0: CloseLibrary(); michael@0: } michael@0: } sLibraryUnloader; michael@0: }; michael@0: michael@0: #endif michael@0: #endif