Tue, 06 Jan 2015 21:39:09 +0100
Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.
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 CGLError (*CGLTexImageIOSurface2DFunc) (CGLContextObj ctxt,
29 GLenum target, GLenum internalFormat,
30 GLsizei width, GLsizei height,
31 GLenum format, GLenum type,
32 IOSurfacePtr ioSurface, GLuint plane);
33 typedef CGContextRef (*IOSurfaceContextCreateFunc)(CFTypeRef io_surface,
34 unsigned width, unsigned height,
35 unsigned bitsPerComponent, unsigned bytes,
36 CGColorSpaceRef colorSpace, CGBitmapInfo bitmapInfo);
37 typedef CGImageRef (*IOSurfaceContextCreateImageFunc)(CGContextRef ref);
38 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
58 };
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 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);
105 private:
106 friend class nsCARenderer;
107 const void* mIOSurfacePtr;
108 double mContentsScaleFactor;
109 bool mHasAlpha;
110 };
112 class MacIOSurfaceLib: public MacIOSurface {
113 public:
114 MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(MacIOSurfaceLib)
115 static void *sIOSurfaceFramework;
116 static void *sOpenGLFramework;
117 static void *sCoreGraphicsFramework;
118 static bool isLoaded;
119 static IOSurfaceCreateFunc sCreate;
120 static IOSurfaceGetIDFunc sGetID;
121 static IOSurfaceLookupFunc sLookup;
122 static IOSurfaceGetBaseAddressFunc sGetBaseAddress;
123 static IOSurfaceLockFunc sLock;
124 static IOSurfaceUnlockFunc sUnlock;
125 static IOSurfaceGetWidthFunc sWidth;
126 static IOSurfaceGetHeightFunc sHeight;
127 static IOSurfaceGetBytesPerRowFunc sBytesPerRow;
128 static CGLTexImageIOSurface2DFunc sTexImage;
129 static IOSurfaceContextCreateFunc sIOSurfaceContextCreate;
130 static IOSurfaceContextCreateImageFunc sIOSurfaceContextCreateImage;
131 static IOSurfaceContextGetSurfaceFunc sIOSurfaceContextGetSurface;
132 static CFStringRef kPropWidth;
133 static CFStringRef kPropHeight;
134 static CFStringRef kPropBytesPerElem;
135 static CFStringRef kPropBytesPerRow;
136 static CFStringRef kPropIsGlobal;
138 static bool isInit();
139 static CFStringRef GetIOConst(const char* symbole);
140 static IOSurfacePtr IOSurfaceCreate(CFDictionaryRef properties);
141 static IOSurfacePtr IOSurfaceLookup(IOSurfaceID aIOSurfaceID);
142 static IOSurfaceID IOSurfaceGetID(IOSurfacePtr aIOSurfacePtr);
143 static void *IOSurfaceGetBaseAddress(IOSurfacePtr aIOSurfacePtr);
144 static size_t IOSurfaceGetWidth(IOSurfacePtr aIOSurfacePtr);
145 static size_t IOSurfaceGetHeight(IOSurfacePtr aIOSurfacePtr);
146 static size_t IOSurfaceGetBytesPerRow(IOSurfacePtr aIOSurfacePtr);
147 static IOReturn IOSurfaceLock(IOSurfacePtr aIOSurfacePtr,
148 uint32_t options, uint32_t *seed);
149 static IOReturn IOSurfaceUnlock(IOSurfacePtr aIOSurfacePtr,
150 uint32_t options, uint32_t *seed);
151 static CGLError CGLTexImageIOSurface2D(CGLContextObj ctxt,
152 GLenum target, GLenum internalFormat,
153 GLsizei width, GLsizei height,
154 GLenum format, GLenum type,
155 IOSurfacePtr ioSurface, GLuint plane);
156 static CGContextRef IOSurfaceContextCreate(IOSurfacePtr aIOSurfacePtr,
157 unsigned aWidth, unsigned aHeight,
158 unsigned aBitsPerCompoent, unsigned aBytes,
159 CGColorSpaceRef aColorSpace, CGBitmapInfo bitmapInfo);
160 static CGImageRef IOSurfaceContextCreateImage(CGContextRef ref);
161 static IOSurfacePtr IOSurfaceContextGetSurface(CGContextRef ref);
162 static unsigned int (*sCGContextGetTypePtr) (CGContextRef);
163 static void LoadLibrary();
164 static void CloseLibrary();
166 // Static deconstructor
167 static class LibraryUnloader {
168 public:
169 ~LibraryUnloader() {
170 CloseLibrary();
171 }
172 } sLibraryUnloader;
173 };
175 #endif
176 #endif