Bumping manifests a=b2g-bump
[gecko.git] / gfx / 2d / MacIOSurface.h
blob098ebabe9d388c2a8b8e765d41a3bf8de942e4dd
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/. */
7 #ifndef MacIOSurface_h__
8 #define MacIOSurface_h__
9 #ifdef XP_MACOSX
10 #include <QuartzCore/QuartzCore.h>
11 #include <dlfcn.h>
12 #include "mozilla/RefPtr.h"
14 typedef CFTypeRef IOSurfacePtr;
15 typedef IOSurfacePtr (*IOSurfaceCreateFunc) (CFDictionaryRef properties);
16 typedef IOSurfacePtr (*IOSurfaceLookupFunc) (uint32_t io_surface_id);
17 typedef IOSurfaceID (*IOSurfaceGetIDFunc) (CFTypeRef io_surface);
18 typedef IOReturn (*IOSurfaceLockFunc) (CFTypeRef io_surface,
19 uint32_t options,
20 uint32_t *seed);
21 typedef IOReturn (*IOSurfaceUnlockFunc) (CFTypeRef io_surface,
22 uint32_t options,
23 uint32_t *seed);
24 typedef void* (*IOSurfaceGetBaseAddressFunc) (CFTypeRef io_surface);
25 typedef size_t (*IOSurfaceGetWidthFunc) (IOSurfacePtr io_surface);
26 typedef size_t (*IOSurfaceGetHeightFunc) (IOSurfacePtr io_surface);
27 typedef size_t (*IOSurfaceGetBytesPerRowFunc) (IOSurfacePtr io_surface);
28 typedef size_t (*IOSurfaceGetPropertyMaximumFunc) (CFStringRef property);
29 typedef CGLError (*CGLTexImageIOSurface2DFunc) (CGLContextObj ctxt,
30 GLenum target, GLenum internalFormat,
31 GLsizei width, GLsizei height,
32 GLenum format, GLenum type,
33 IOSurfacePtr ioSurface, GLuint plane);
34 typedef CGContextRef (*IOSurfaceContextCreateFunc)(CFTypeRef io_surface,
35 unsigned width, unsigned height,
36 unsigned bitsPerComponent, unsigned bytes,
37 CGColorSpaceRef colorSpace, CGBitmapInfo bitmapInfo);
38 typedef CGImageRef (*IOSurfaceContextCreateImageFunc)(CGContextRef ref);
39 typedef IOSurfacePtr (*IOSurfaceContextGetSurfaceFunc)(CGContextRef ref);
42 #import <OpenGL/OpenGL.h>
43 #include "2D.h"
44 #include "mozilla/RefPtr.h"
46 struct _CGLContextObject;
48 typedef _CGLContextObject* CGLContextObj;
49 typedef struct CGContext* CGContextRef;
50 typedef struct CGImage* CGImageRef;
51 typedef uint32_t IOSurfaceID;
53 enum CGContextType {
54 CG_CONTEXT_TYPE_UNKNOWN = 0,
55 // These are found by inspection, it's possible they could be changed
56 CG_CONTEXT_TYPE_BITMAP = 4,
57 CG_CONTEXT_TYPE_IOSURFACE = 8
60 CGContextType GetContextType(CGContextRef ref);
62 class MacIOSurface : public mozilla::RefCounted<MacIOSurface> {
63 public:
64 MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(MacIOSurface)
65 typedef mozilla::gfx::SourceSurface SourceSurface;
67 static mozilla::TemporaryRef<MacIOSurface> CreateIOSurface(int aWidth, int aHeight,
68 double aContentsScaleFactor = 1.0,
69 bool aHasAlpha = true);
70 static void ReleaseIOSurface(MacIOSurface *aIOSurface);
71 static mozilla::TemporaryRef<MacIOSurface> LookupSurface(IOSurfaceID aSurfaceID,
72 double aContentsScaleFactor = 1.0,
73 bool aHasAlpha = true);
75 explicit MacIOSurface(const void *aIOSurfacePtr, double aContentsScaleFactor = 1.0, bool aHasAlpha = true)
76 : mIOSurfacePtr(aIOSurfacePtr), mContentsScaleFactor(aContentsScaleFactor), mHasAlpha(aHasAlpha) {}
77 virtual ~MacIOSurface();
78 IOSurfaceID GetIOSurfaceID();
79 void *GetBaseAddress();
80 // GetWidth() and GetHeight() return values in "display pixels". A
81 // "display pixel" is the smallest fully addressable part of a display.
82 // But in HiDPI modes each "display pixel" corresponds to more than one
83 // device pixel. Use GetDevicePixel**() to get device pixels.
84 size_t GetWidth();
85 size_t GetHeight();
86 double GetContentsScaleFactor() { return mContentsScaleFactor; }
87 size_t GetDevicePixelWidth();
88 size_t GetDevicePixelHeight();
89 size_t GetBytesPerRow();
90 void Lock();
91 void Unlock();
92 bool HasAlpha() { return mHasAlpha; }
93 // We would like to forward declare NSOpenGLContext, but it is an @interface
94 // and this file is also used from c++, so we use a void *.
95 CGLError CGLTexImageIOSurface2D(CGLContextObj ctxt);
96 mozilla::TemporaryRef<SourceSurface> GetAsSurface();
97 CGContextRef CreateIOSurfaceContext();
99 // FIXME This doesn't really belong here
100 static CGImageRef CreateImageFromIOSurfaceContext(CGContextRef aContext);
101 static mozilla::TemporaryRef<MacIOSurface> IOSurfaceContextGetSurface(CGContextRef aContext,
102 double aContentsScaleFactor = 1.0,
103 bool aHasAlpha = true);
104 static size_t GetMaxWidth();
105 static size_t GetMaxHeight();
107 private:
108 friend class nsCARenderer;
109 const void* mIOSurfacePtr;
110 double mContentsScaleFactor;
111 bool mHasAlpha;
114 class MacIOSurfaceLib: public MacIOSurface {
115 public:
116 MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(MacIOSurfaceLib)
117 static void *sIOSurfaceFramework;
118 static void *sOpenGLFramework;
119 static void *sCoreGraphicsFramework;
120 static bool isLoaded;
121 static IOSurfaceCreateFunc sCreate;
122 static IOSurfaceGetIDFunc sGetID;
123 static IOSurfaceLookupFunc sLookup;
124 static IOSurfaceGetBaseAddressFunc sGetBaseAddress;
125 static IOSurfaceLockFunc sLock;
126 static IOSurfaceUnlockFunc sUnlock;
127 static IOSurfaceGetWidthFunc sWidth;
128 static IOSurfaceGetHeightFunc sHeight;
129 static IOSurfaceGetBytesPerRowFunc sBytesPerRow;
130 static IOSurfaceGetPropertyMaximumFunc sGetPropertyMaximum;
131 static CGLTexImageIOSurface2DFunc sTexImage;
132 static IOSurfaceContextCreateFunc sIOSurfaceContextCreate;
133 static IOSurfaceContextCreateImageFunc sIOSurfaceContextCreateImage;
134 static IOSurfaceContextGetSurfaceFunc sIOSurfaceContextGetSurface;
135 static CFStringRef kPropWidth;
136 static CFStringRef kPropHeight;
137 static CFStringRef kPropBytesPerElem;
138 static CFStringRef kPropBytesPerRow;
139 static CFStringRef kPropIsGlobal;
141 static bool isInit();
142 static CFStringRef GetIOConst(const char* symbole);
143 static IOSurfacePtr IOSurfaceCreate(CFDictionaryRef properties);
144 static IOSurfacePtr IOSurfaceLookup(IOSurfaceID aIOSurfaceID);
145 static IOSurfaceID IOSurfaceGetID(IOSurfacePtr aIOSurfacePtr);
146 static void* IOSurfaceGetBaseAddress(IOSurfacePtr aIOSurfacePtr);
147 static size_t IOSurfaceGetWidth(IOSurfacePtr aIOSurfacePtr);
148 static size_t IOSurfaceGetHeight(IOSurfacePtr aIOSurfacePtr);
149 static size_t IOSurfaceGetBytesPerRow(IOSurfacePtr aIOSurfacePtr);
150 static size_t IOSurfaceGetPropertyMaximum(CFStringRef property);
151 static IOReturn IOSurfaceLock(IOSurfacePtr aIOSurfacePtr,
152 uint32_t options, uint32_t *seed);
153 static IOReturn IOSurfaceUnlock(IOSurfacePtr aIOSurfacePtr,
154 uint32_t options, uint32_t *seed);
155 static CGLError CGLTexImageIOSurface2D(CGLContextObj ctxt,
156 GLenum target, GLenum internalFormat,
157 GLsizei width, GLsizei height,
158 GLenum format, GLenum type,
159 IOSurfacePtr ioSurface, GLuint plane);
160 static CGContextRef IOSurfaceContextCreate(IOSurfacePtr aIOSurfacePtr,
161 unsigned aWidth, unsigned aHeight,
162 unsigned aBitsPerCompoent, unsigned aBytes,
163 CGColorSpaceRef aColorSpace, CGBitmapInfo bitmapInfo);
164 static CGImageRef IOSurfaceContextCreateImage(CGContextRef ref);
165 static IOSurfacePtr IOSurfaceContextGetSurface(CGContextRef ref);
166 static unsigned int (*sCGContextGetTypePtr) (CGContextRef);
167 static void LoadLibrary();
168 static void CloseLibrary();
170 // Static deconstructor
171 static class LibraryUnloader {
172 public:
173 ~LibraryUnloader() {
174 CloseLibrary();
176 } sLibraryUnloader;
179 #endif
180 #endif