1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 // vim:set ts=2 sts=2 sw=2 et cin: 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #ifndef MacIOSurface_h__ 8 #define MacIOSurface_h__ 9 #ifdef XP_DARWIN 10 #include <QuartzCore/QuartzCore.h> 11 #include <CoreVideo/CoreVideo.h> 12 #include <dlfcn.h> 13 14 struct _CGLContextObject; 15 16 typedef _CGLContextObject* CGLContextObj; 17 typedef struct CGContext* CGContextRef; 18 typedef struct CGImage* CGImageRef; 19 typedef uint32_t IOSurfaceID; 20 21 #ifdef XP_IOS 22 typedef kern_return_t IOReturn; 23 typedef int CGLError; 24 #endif 25 26 typedef CFTypeRef IOSurfacePtr; 27 typedef IOSurfacePtr (*IOSurfaceCreateFunc) (CFDictionaryRef properties); 28 typedef IOSurfacePtr (*IOSurfaceLookupFunc) (uint32_t io_surface_id); 29 typedef IOSurfaceID (*IOSurfaceGetIDFunc)(IOSurfacePtr io_surface); 30 typedef void (*IOSurfaceVoidFunc)(IOSurfacePtr io_surface); 31 typedef IOReturn (*IOSurfaceLockFunc)(IOSurfacePtr io_surface, uint32_t options, 32 uint32_t *seed); 33 typedef IOReturn (*IOSurfaceUnlockFunc)(IOSurfacePtr io_surface, 34 uint32_t options, uint32_t *seed); 35 typedef void* (*IOSurfaceGetBaseAddressFunc)(IOSurfacePtr io_surface); 36 typedef void* (*IOSurfaceGetBaseAddressOfPlaneFunc)(IOSurfacePtr io_surface, 37 size_t planeIndex); 38 typedef size_t (*IOSurfaceSizeTFunc)(IOSurfacePtr io_surface); 39 typedef size_t (*IOSurfaceSizePlaneTFunc)(IOSurfacePtr io_surface, size_t plane); 40 typedef size_t (*IOSurfaceGetPropertyMaximumFunc) (CFStringRef property); 41 typedef CGLError (*CGLTexImageIOSurface2DFunc) (CGLContextObj ctxt, 42 GLenum target, GLenum internalFormat, 43 GLsizei width, GLsizei height, 44 GLenum format, GLenum type, 45 IOSurfacePtr ioSurface, GLuint plane); 46 typedef CGContextRef (*IOSurfaceContextCreateFunc)(CFTypeRef io_surface, 47 unsigned width, unsigned height, 48 unsigned bitsPerComponent, unsigned bytes, 49 CGColorSpaceRef colorSpace, CGBitmapInfo bitmapInfo); 50 typedef CGImageRef (*IOSurfaceContextCreateImageFunc)(CGContextRef ref); 51 typedef IOSurfacePtr (*IOSurfaceContextGetSurfaceFunc)(CGContextRef ref); 52 53 typedef IOSurfacePtr (*CVPixelBufferGetIOSurfaceFunc)( 54 CVPixelBufferRef pixelBuffer); 55 56 typedef OSType (*IOSurfacePixelFormatFunc)(IOSurfacePtr io_surface); 57 58 #ifdef XP_MACOSX 59 #import <OpenGL/OpenGL.h> 60 #else 61 #import <OpenGLES/ES2/gl.h> 62 #endif 63 64 #include "2D.h" 65 #include "mozilla/RefPtr.h" 66 #include "mozilla/RefCounted.h" 67 68 enum CGContextType { 69 CG_CONTEXT_TYPE_UNKNOWN = 0, 70 // These are found by inspection, it's possible they could be changed 71 CG_CONTEXT_TYPE_BITMAP = 4, 72 CG_CONTEXT_TYPE_IOSURFACE = 8 73 }; 74 75 CGContextType GetContextType(CGContextRef ref); 76 77 class MacIOSurface final : public mozilla::external::AtomicRefCounted<MacIOSurface> { 78 public: 79 MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(MacIOSurface) 80 typedef mozilla::gfx::SourceSurface SourceSurface; 81 82 // The usage count of the IOSurface is increased by 1 during the lifetime 83 // of the MacIOSurface instance. 84 // MacIOSurface holds a reference to the corresponding IOSurface. 85 86 static already_AddRefed<MacIOSurface> CreateIOSurface(int aWidth, int aHeight, 87 double aContentsScaleFactor = 1.0, 88 bool aHasAlpha = true); 89 static void ReleaseIOSurface(MacIOSurface *aIOSurface); 90 static already_AddRefed<MacIOSurface> LookupSurface(IOSurfaceID aSurfaceID, 91 double aContentsScaleFactor = 1.0, 92 bool aHasAlpha = true); 93 94 explicit MacIOSurface(const void *aIOSurfacePtr, 95 double aContentsScaleFactor = 1.0, 96 bool aHasAlpha = true); 97 ~MacIOSurface(); 98 IOSurfaceID GetIOSurfaceID(); 99 void *GetBaseAddress(); 100 void *GetBaseAddressOfPlane(size_t planeIndex); 101 size_t GetPlaneCount(); 102 OSType GetPixelFormat(); 103 // GetWidth() and GetHeight() return values in "display pixels". A 104 // "display pixel" is the smallest fully addressable part of a display. 105 // But in HiDPI modes each "display pixel" corresponds to more than one 106 // device pixel. Use GetDevicePixel**() to get device pixels. 107 size_t GetWidth(size_t plane = 0); 108 size_t GetHeight(size_t plane = 0); GetContentsScaleFactor()109 double GetContentsScaleFactor() { return mContentsScaleFactor; } 110 size_t GetDevicePixelWidth(size_t plane = 0); 111 size_t GetDevicePixelHeight(size_t plane = 0); 112 size_t GetBytesPerRow(size_t plane = 0); 113 void Lock(bool aReadOnly = true); 114 void Unlock(bool aReadOnly = true); 115 void IncrementUseCount(); 116 void DecrementUseCount(); HasAlpha()117 bool HasAlpha() { return mHasAlpha; } 118 mozilla::gfx::SurfaceFormat GetFormat(); 119 mozilla::gfx::SurfaceFormat GetReadFormat(); 120 121 // We would like to forward declare NSOpenGLContext, but it is an @interface 122 // and this file is also used from c++, so we use a void *. 123 CGLError CGLTexImageIOSurface2D(CGLContextObj ctxt, size_t plane = 0); 124 already_AddRefed<SourceSurface> GetAsSurface(); 125 CGContextRef CreateIOSurfaceContext(); 126 127 // FIXME This doesn't really belong here 128 static CGImageRef CreateImageFromIOSurfaceContext(CGContextRef aContext); 129 static already_AddRefed<MacIOSurface> IOSurfaceContextGetSurface(CGContextRef aContext, 130 double aContentsScaleFactor = 1.0, 131 bool aHasAlpha = true); 132 static size_t GetMaxWidth(); 133 static size_t GetMaxHeight(); 134 135 private: 136 friend class nsCARenderer; 137 const void* mIOSurfacePtr; 138 double mContentsScaleFactor; 139 bool mHasAlpha; 140 }; 141 142 class MacIOSurfaceLib { 143 public: 144 MacIOSurfaceLib() = delete; 145 static void *sIOSurfaceFramework; 146 static void *sOpenGLFramework; 147 static void *sCoreGraphicsFramework; 148 static void *sCoreVideoFramework; 149 static bool isLoaded; 150 static IOSurfaceCreateFunc sCreate; 151 static IOSurfaceGetIDFunc sGetID; 152 static IOSurfaceLookupFunc sLookup; 153 static IOSurfaceGetBaseAddressFunc sGetBaseAddress; 154 static IOSurfaceGetBaseAddressOfPlaneFunc sGetBaseAddressOfPlane; 155 static IOSurfaceSizeTFunc sPlaneCount; 156 static IOSurfaceLockFunc sLock; 157 static IOSurfaceUnlockFunc sUnlock; 158 static IOSurfaceVoidFunc sIncrementUseCount; 159 static IOSurfaceVoidFunc sDecrementUseCount; 160 static IOSurfaceSizePlaneTFunc sWidth; 161 static IOSurfaceSizePlaneTFunc sHeight; 162 static IOSurfaceSizePlaneTFunc sBytesPerRow; 163 static IOSurfaceGetPropertyMaximumFunc sGetPropertyMaximum; 164 static CGLTexImageIOSurface2DFunc sTexImage; 165 static IOSurfaceContextCreateFunc sIOSurfaceContextCreate; 166 static IOSurfaceContextCreateImageFunc sIOSurfaceContextCreateImage; 167 static IOSurfaceContextGetSurfaceFunc sIOSurfaceContextGetSurface; 168 static CVPixelBufferGetIOSurfaceFunc sCVPixelBufferGetIOSurface; 169 static IOSurfacePixelFormatFunc sPixelFormat; 170 static CFStringRef kPropWidth; 171 static CFStringRef kPropHeight; 172 static CFStringRef kPropBytesPerElem; 173 static CFStringRef kPropBytesPerRow; 174 static CFStringRef kPropIsGlobal; 175 176 static bool isInit(); 177 static CFStringRef GetIOConst(const char* symbole); 178 static IOSurfacePtr IOSurfaceCreate(CFDictionaryRef properties); 179 static IOSurfacePtr IOSurfaceLookup(IOSurfaceID aIOSurfaceID); 180 static IOSurfaceID IOSurfaceGetID(IOSurfacePtr aIOSurfacePtr); 181 static void* IOSurfaceGetBaseAddress(IOSurfacePtr aIOSurfacePtr); 182 static void* IOSurfaceGetBaseAddressOfPlane(IOSurfacePtr aIOSurfacePtr, 183 size_t aPlaneIndex); 184 static size_t IOSurfaceGetPlaneCount(IOSurfacePtr aIOSurfacePtr); 185 static size_t IOSurfaceGetWidth(IOSurfacePtr aIOSurfacePtr, size_t plane); 186 static size_t IOSurfaceGetHeight(IOSurfacePtr aIOSurfacePtr, size_t plane); 187 static size_t IOSurfaceGetBytesPerRow(IOSurfacePtr aIOSurfacePtr, size_t plane); 188 static size_t IOSurfaceGetPropertyMaximum(CFStringRef property); 189 static IOReturn IOSurfaceLock(IOSurfacePtr aIOSurfacePtr, 190 uint32_t options, uint32_t *seed); 191 static IOReturn IOSurfaceUnlock(IOSurfacePtr aIOSurfacePtr, 192 uint32_t options, uint32_t *seed); 193 static void IOSurfaceIncrementUseCount(IOSurfacePtr aIOSurfacePtr); 194 static void IOSurfaceDecrementUseCount(IOSurfacePtr aIOSurfacePtr); 195 static CGLError CGLTexImageIOSurface2D(CGLContextObj ctxt, 196 GLenum target, GLenum internalFormat, 197 GLsizei width, GLsizei height, 198 GLenum format, GLenum type, 199 IOSurfacePtr ioSurface, GLuint plane); 200 static CGContextRef IOSurfaceContextCreate(IOSurfacePtr aIOSurfacePtr, 201 unsigned aWidth, unsigned aHeight, 202 unsigned aBitsPerCompoent, unsigned aBytes, 203 CGColorSpaceRef aColorSpace, CGBitmapInfo bitmapInfo); 204 static CGImageRef IOSurfaceContextCreateImage(CGContextRef ref); 205 static IOSurfacePtr IOSurfaceContextGetSurface(CGContextRef ref); 206 static IOSurfacePtr CVPixelBufferGetIOSurface(CVPixelBufferRef apixelBuffer); 207 static OSType IOSurfaceGetPixelFormat(IOSurfacePtr aIOSurfacePtr); 208 static unsigned int (*sCGContextGetTypePtr) (CGContextRef); 209 static void LoadLibrary(); 210 static void CloseLibrary(); 211 212 // Static deconstructor 213 static class LibraryUnloader { 214 public: ~LibraryUnloader()215 ~LibraryUnloader() { 216 CloseLibrary(); 217 } 218 } sLibraryUnloader; 219 }; 220 221 #endif 222 #endif 223