gfx/2d/MacIOSurface.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/2d/MacIOSurface.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,176 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +// vim:set ts=2 sts=2 sw=2 et cin:
     1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +#ifndef MacIOSurface_h__
    1.11 +#define MacIOSurface_h__
    1.12 +#ifdef XP_MACOSX
    1.13 +#include <QuartzCore/QuartzCore.h>
    1.14 +#include <dlfcn.h>
    1.15 +#include "mozilla/RefPtr.h"
    1.16 +
    1.17 +typedef CFTypeRef IOSurfacePtr;
    1.18 +typedef IOSurfacePtr (*IOSurfaceCreateFunc) (CFDictionaryRef properties);
    1.19 +typedef IOSurfacePtr (*IOSurfaceLookupFunc) (uint32_t io_surface_id);
    1.20 +typedef IOSurfaceID (*IOSurfaceGetIDFunc) (CFTypeRef io_surface);
    1.21 +typedef IOReturn (*IOSurfaceLockFunc) (CFTypeRef io_surface, 
    1.22 +                                       uint32_t options, 
    1.23 +                                       uint32_t *seed);
    1.24 +typedef IOReturn (*IOSurfaceUnlockFunc) (CFTypeRef io_surface, 
    1.25 +                                         uint32_t options, 
    1.26 +                                         uint32_t *seed);
    1.27 +typedef void* (*IOSurfaceGetBaseAddressFunc) (CFTypeRef io_surface);
    1.28 +typedef size_t (*IOSurfaceGetWidthFunc) (IOSurfacePtr io_surface);
    1.29 +typedef size_t (*IOSurfaceGetHeightFunc) (IOSurfacePtr io_surface);
    1.30 +typedef size_t (*IOSurfaceGetBytesPerRowFunc) (IOSurfacePtr io_surface);
    1.31 +typedef CGLError (*CGLTexImageIOSurface2DFunc) (CGLContextObj ctxt,
    1.32 +                             GLenum target, GLenum internalFormat,
    1.33 +                             GLsizei width, GLsizei height,
    1.34 +                             GLenum format, GLenum type,
    1.35 +                             IOSurfacePtr ioSurface, GLuint plane);
    1.36 +typedef CGContextRef (*IOSurfaceContextCreateFunc)(CFTypeRef io_surface,
    1.37 +                             unsigned width, unsigned height,
    1.38 +                             unsigned bitsPerComponent, unsigned bytes,
    1.39 +                             CGColorSpaceRef colorSpace, CGBitmapInfo bitmapInfo);
    1.40 +typedef CGImageRef (*IOSurfaceContextCreateImageFunc)(CGContextRef ref);
    1.41 +typedef IOSurfacePtr (*IOSurfaceContextGetSurfaceFunc)(CGContextRef ref);
    1.42 +
    1.43 +
    1.44 +
    1.45 +#import <OpenGL/OpenGL.h>
    1.46 +#include "2D.h"
    1.47 +#include "mozilla/RefPtr.h"
    1.48 +
    1.49 +struct _CGLContextObject;
    1.50 +
    1.51 +typedef _CGLContextObject* CGLContextObj;
    1.52 +typedef struct CGContext* CGContextRef;
    1.53 +typedef struct CGImage* CGImageRef;
    1.54 +typedef uint32_t IOSurfaceID;
    1.55 +
    1.56 +enum CGContextType {
    1.57 +  CG_CONTEXT_TYPE_UNKNOWN = 0,
    1.58 +  // These are found by inspection, it's possible they could be changed
    1.59 +  CG_CONTEXT_TYPE_BITMAP = 4,
    1.60 +  CG_CONTEXT_TYPE_IOSURFACE = 8
    1.61 +};
    1.62 +
    1.63 +CGContextType GetContextType(CGContextRef ref);
    1.64 +
    1.65 +class MacIOSurface : public mozilla::RefCounted<MacIOSurface> {
    1.66 +public:
    1.67 +  MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(MacIOSurface)
    1.68 +  typedef mozilla::gfx::SourceSurface SourceSurface;
    1.69 +
    1.70 +  static mozilla::TemporaryRef<MacIOSurface> CreateIOSurface(int aWidth, int aHeight,
    1.71 +                                                             double aContentsScaleFactor = 1.0,
    1.72 +                                                             bool aHasAlpha = true);
    1.73 +  static void ReleaseIOSurface(MacIOSurface *aIOSurface);
    1.74 +  static mozilla::TemporaryRef<MacIOSurface> LookupSurface(IOSurfaceID aSurfaceID,
    1.75 +                                                           double aContentsScaleFactor = 1.0,
    1.76 +                                                           bool aHasAlpha = true);
    1.77 +
    1.78 +  MacIOSurface(const void *aIOSurfacePtr, double aContentsScaleFactor = 1.0, bool aHasAlpha = true)
    1.79 +    : mIOSurfacePtr(aIOSurfacePtr), mContentsScaleFactor(aContentsScaleFactor), mHasAlpha(aHasAlpha) {}
    1.80 +  virtual ~MacIOSurface();
    1.81 +  IOSurfaceID GetIOSurfaceID();
    1.82 +  void *GetBaseAddress();
    1.83 +  // GetWidth() and GetHeight() return values in "display pixels".  A
    1.84 +  // "display pixel" is the smallest fully addressable part of a display.
    1.85 +  // But in HiDPI modes each "display pixel" corresponds to more than one
    1.86 +  // device pixel.  Use GetDevicePixel**() to get device pixels.
    1.87 +  size_t GetWidth();
    1.88 +  size_t GetHeight();
    1.89 +  double GetContentsScaleFactor() { return mContentsScaleFactor; }
    1.90 +  size_t GetDevicePixelWidth();
    1.91 +  size_t GetDevicePixelHeight();
    1.92 +  size_t GetBytesPerRow();
    1.93 +  void Lock();
    1.94 +  void Unlock();
    1.95 +  bool HasAlpha() { return mHasAlpha; }
    1.96 +  // We would like to forward declare NSOpenGLContext, but it is an @interface
    1.97 +  // and this file is also used from c++, so we use a void *.
    1.98 +  CGLError CGLTexImageIOSurface2D(CGLContextObj ctxt);
    1.99 +  mozilla::TemporaryRef<SourceSurface> GetAsSurface();
   1.100 +  CGContextRef CreateIOSurfaceContext();
   1.101 +
   1.102 +  // FIXME This doesn't really belong here
   1.103 +  static CGImageRef CreateImageFromIOSurfaceContext(CGContextRef aContext);
   1.104 +  static mozilla::TemporaryRef<MacIOSurface> IOSurfaceContextGetSurface(CGContextRef aContext,
   1.105 +                                                                        double aContentsScaleFactor = 1.0,
   1.106 +                                                                        bool aHasAlpha = true);
   1.107 +
   1.108 +private:
   1.109 +  friend class nsCARenderer;
   1.110 +  const void* mIOSurfacePtr;
   1.111 +  double mContentsScaleFactor;
   1.112 +  bool mHasAlpha;
   1.113 +};
   1.114 +
   1.115 +class MacIOSurfaceLib: public MacIOSurface {
   1.116 +public:
   1.117 +  MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(MacIOSurfaceLib)
   1.118 +  static void                        *sIOSurfaceFramework;
   1.119 +  static void                        *sOpenGLFramework;
   1.120 +  static void                        *sCoreGraphicsFramework;
   1.121 +  static bool                         isLoaded;
   1.122 +  static IOSurfaceCreateFunc          sCreate;
   1.123 +  static IOSurfaceGetIDFunc           sGetID;
   1.124 +  static IOSurfaceLookupFunc          sLookup;
   1.125 +  static IOSurfaceGetBaseAddressFunc  sGetBaseAddress;
   1.126 +  static IOSurfaceLockFunc            sLock;
   1.127 +  static IOSurfaceUnlockFunc          sUnlock;
   1.128 +  static IOSurfaceGetWidthFunc        sWidth;
   1.129 +  static IOSurfaceGetHeightFunc       sHeight;
   1.130 +  static IOSurfaceGetBytesPerRowFunc  sBytesPerRow;
   1.131 +  static CGLTexImageIOSurface2DFunc   sTexImage;
   1.132 +  static IOSurfaceContextCreateFunc   sIOSurfaceContextCreate;
   1.133 +  static IOSurfaceContextCreateImageFunc  sIOSurfaceContextCreateImage;
   1.134 +  static IOSurfaceContextGetSurfaceFunc   sIOSurfaceContextGetSurface;
   1.135 +  static CFStringRef                  kPropWidth;
   1.136 +  static CFStringRef                  kPropHeight;
   1.137 +  static CFStringRef                  kPropBytesPerElem;
   1.138 +  static CFStringRef                  kPropBytesPerRow;
   1.139 +  static CFStringRef                  kPropIsGlobal;
   1.140 +
   1.141 +  static bool isInit();
   1.142 +  static CFStringRef GetIOConst(const char* symbole);
   1.143 +  static IOSurfacePtr IOSurfaceCreate(CFDictionaryRef properties);
   1.144 +  static IOSurfacePtr IOSurfaceLookup(IOSurfaceID aIOSurfaceID);
   1.145 +  static IOSurfaceID  IOSurfaceGetID(IOSurfacePtr aIOSurfacePtr);
   1.146 +  static void        *IOSurfaceGetBaseAddress(IOSurfacePtr aIOSurfacePtr);
   1.147 +  static size_t       IOSurfaceGetWidth(IOSurfacePtr aIOSurfacePtr);
   1.148 +  static size_t       IOSurfaceGetHeight(IOSurfacePtr aIOSurfacePtr);
   1.149 +  static size_t       IOSurfaceGetBytesPerRow(IOSurfacePtr aIOSurfacePtr);
   1.150 +  static IOReturn     IOSurfaceLock(IOSurfacePtr aIOSurfacePtr, 
   1.151 +                                    uint32_t options, uint32_t *seed);
   1.152 +  static IOReturn     IOSurfaceUnlock(IOSurfacePtr aIOSurfacePtr, 
   1.153 +                                      uint32_t options, uint32_t *seed);
   1.154 +  static CGLError     CGLTexImageIOSurface2D(CGLContextObj ctxt,
   1.155 +                             GLenum target, GLenum internalFormat,
   1.156 +                             GLsizei width, GLsizei height,
   1.157 +                             GLenum format, GLenum type,
   1.158 +                             IOSurfacePtr ioSurface, GLuint plane);
   1.159 +  static CGContextRef IOSurfaceContextCreate(IOSurfacePtr aIOSurfacePtr,
   1.160 +                             unsigned aWidth, unsigned aHeight,
   1.161 +                             unsigned aBitsPerCompoent, unsigned aBytes,
   1.162 +                             CGColorSpaceRef aColorSpace, CGBitmapInfo bitmapInfo);
   1.163 +  static CGImageRef   IOSurfaceContextCreateImage(CGContextRef ref);
   1.164 +  static IOSurfacePtr IOSurfaceContextGetSurface(CGContextRef ref);
   1.165 +  static unsigned int (*sCGContextGetTypePtr) (CGContextRef);
   1.166 +  static void LoadLibrary();
   1.167 +  static void CloseLibrary();
   1.168 +
   1.169 +  // Static deconstructor
   1.170 +  static class LibraryUnloader {
   1.171 +  public:
   1.172 +    ~LibraryUnloader() {
   1.173 +      CloseLibrary();
   1.174 +    }
   1.175 +  } sLibraryUnloader;
   1.176 +};
   1.177 +
   1.178 +#endif
   1.179 +#endif

mercurial