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