2010-04-20 07:52:19 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
// vim:set ts=2 sts=2 sw=2 et cin:
|
2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2010-04-20 07:52:19 -07:00
|
|
|
|
2012-07-31 08:11:57 -07:00
|
|
|
#include "QuartzSupport.h"
|
2010-04-20 07:52:19 -07:00
|
|
|
#include "nsDebug.h"
|
|
|
|
|
|
|
|
#import <QuartzCore/QuartzCore.h>
|
2012-02-02 16:22:34 -08:00
|
|
|
#import <AppKit/NSOpenGL.h>
|
2010-04-20 07:52:19 -07:00
|
|
|
#include <dlfcn.h>
|
2013-07-17 20:24:14 -07:00
|
|
|
#include "GLDefs.h"
|
2010-04-20 07:52:19 -07:00
|
|
|
|
|
|
|
#define IOSURFACE_FRAMEWORK_PATH \
|
|
|
|
"/System/Library/Frameworks/IOSurface.framework/IOSurface"
|
|
|
|
#define OPENGL_FRAMEWORK_PATH \
|
|
|
|
"/System/Library/Frameworks/OpenGL.framework/OpenGL"
|
2012-07-31 08:11:57 -07:00
|
|
|
#define COREGRAPHICS_FRAMEWORK_PATH \
|
|
|
|
"/System/Library/Frameworks/ApplicationServices.framework/Frameworks/CoreGraphics.framework/CoreGraphics"
|
2010-04-20 07:52:19 -07:00
|
|
|
|
2012-10-02 12:48:05 -07:00
|
|
|
@interface CALayer (ContentsScale)
|
|
|
|
- (double)contentsScale;
|
|
|
|
- (void)setContentsScale:(double)scale;
|
|
|
|
@end
|
|
|
|
|
2012-07-31 08:11:57 -07:00
|
|
|
using mozilla::RefPtr;
|
|
|
|
using mozilla::TemporaryRef;
|
2010-04-20 07:52:19 -07:00
|
|
|
|
|
|
|
// IOSurface signatures
|
|
|
|
typedef CFTypeRef IOSurfacePtr;
|
|
|
|
typedef IOSurfacePtr (*IOSurfaceCreateFunc) (CFDictionaryRef properties);
|
|
|
|
typedef IOSurfacePtr (*IOSurfaceLookupFunc) (uint32_t io_surface_id);
|
|
|
|
typedef IOSurfaceID (*IOSurfaceGetIDFunc) (CFTypeRef io_surface);
|
|
|
|
typedef IOReturn (*IOSurfaceLockFunc) (CFTypeRef io_surface,
|
|
|
|
uint32_t options,
|
|
|
|
uint32_t *seed);
|
|
|
|
typedef IOReturn (*IOSurfaceUnlockFunc) (CFTypeRef io_surface,
|
|
|
|
uint32_t options,
|
|
|
|
uint32_t *seed);
|
|
|
|
typedef void* (*IOSurfaceGetBaseAddressFunc) (CFTypeRef io_surface);
|
|
|
|
typedef size_t (*IOSurfaceGetWidthFunc) (IOSurfacePtr io_surface);
|
|
|
|
typedef size_t (*IOSurfaceGetHeightFunc) (IOSurfacePtr io_surface);
|
|
|
|
typedef size_t (*IOSurfaceGetBytesPerRowFunc) (IOSurfacePtr io_surface);
|
|
|
|
typedef CGLError (*CGLTexImageIOSurface2DFunc) (CGLContextObj ctxt,
|
|
|
|
GLenum target, GLenum internalFormat,
|
|
|
|
GLsizei width, GLsizei height,
|
|
|
|
GLenum format, GLenum type,
|
|
|
|
IOSurfacePtr ioSurface, GLuint plane);
|
2012-07-31 08:11:57 -07:00
|
|
|
typedef CGContextRef (*IOSurfaceContextCreateFunc)(CFTypeRef io_surface,
|
|
|
|
unsigned width, unsigned height,
|
|
|
|
unsigned bitsPerComponent, unsigned bytes,
|
|
|
|
CGColorSpaceRef colorSpace, CGBitmapInfo bitmapInfo);
|
|
|
|
typedef CGImageRef (*IOSurfaceContextCreateImageFunc)(CGContextRef ref);
|
|
|
|
typedef IOSurfacePtr (*IOSurfaceContextGetSurfaceFunc)(CGContextRef ref);
|
2010-04-20 07:52:19 -07:00
|
|
|
|
|
|
|
#define GET_CONST(const_name) \
|
|
|
|
((CFStringRef*) dlsym(sIOSurfaceFramework, const_name))
|
|
|
|
#define GET_IOSYM(dest,sym_name) \
|
|
|
|
(typeof(dest)) dlsym(sIOSurfaceFramework, sym_name)
|
|
|
|
#define GET_CGLSYM(dest,sym_name) \
|
|
|
|
(typeof(dest)) dlsym(sOpenGLFramework, sym_name)
|
2012-07-31 08:11:57 -07:00
|
|
|
#define GET_CGSYM(dest,sym_name) \
|
|
|
|
(typeof(dest)) dlsym(sCoreGraphicsFramework, sym_name)
|
2010-04-20 07:52:19 -07:00
|
|
|
|
2012-07-31 08:11:57 -07:00
|
|
|
class MacIOSurfaceLib: public MacIOSurface {
|
2010-04-20 07:52:19 -07:00
|
|
|
public:
|
|
|
|
static void *sIOSurfaceFramework;
|
|
|
|
static void *sOpenGLFramework;
|
2012-07-31 08:11:57 -07:00
|
|
|
static void *sCoreGraphicsFramework;
|
2010-04-20 07:52:19 -07:00
|
|
|
static bool isLoaded;
|
|
|
|
static IOSurfaceCreateFunc sCreate;
|
|
|
|
static IOSurfaceGetIDFunc sGetID;
|
|
|
|
static IOSurfaceLookupFunc sLookup;
|
|
|
|
static IOSurfaceGetBaseAddressFunc sGetBaseAddress;
|
|
|
|
static IOSurfaceLockFunc sLock;
|
|
|
|
static IOSurfaceUnlockFunc sUnlock;
|
|
|
|
static IOSurfaceGetWidthFunc sWidth;
|
|
|
|
static IOSurfaceGetHeightFunc sHeight;
|
|
|
|
static IOSurfaceGetBytesPerRowFunc sBytesPerRow;
|
|
|
|
static CGLTexImageIOSurface2DFunc sTexImage;
|
2012-07-31 08:11:57 -07:00
|
|
|
static IOSurfaceContextCreateFunc sIOSurfaceContextCreate;
|
|
|
|
static IOSurfaceContextCreateImageFunc sIOSurfaceContextCreateImage;
|
|
|
|
static IOSurfaceContextGetSurfaceFunc sIOSurfaceContextGetSurface;
|
2010-04-20 07:52:19 -07:00
|
|
|
static CFStringRef kPropWidth;
|
|
|
|
static CFStringRef kPropHeight;
|
|
|
|
static CFStringRef kPropBytesPerElem;
|
|
|
|
static CFStringRef kPropBytesPerRow;
|
|
|
|
static CFStringRef kPropIsGlobal;
|
|
|
|
|
|
|
|
static bool isInit();
|
|
|
|
static CFStringRef GetIOConst(const char* symbole);
|
|
|
|
static IOSurfacePtr IOSurfaceCreate(CFDictionaryRef properties);
|
|
|
|
static IOSurfacePtr IOSurfaceLookup(IOSurfaceID aIOSurfaceID);
|
|
|
|
static IOSurfaceID IOSurfaceGetID(IOSurfacePtr aIOSurfacePtr);
|
|
|
|
static void *IOSurfaceGetBaseAddress(IOSurfacePtr aIOSurfacePtr);
|
|
|
|
static size_t IOSurfaceGetWidth(IOSurfacePtr aIOSurfacePtr);
|
|
|
|
static size_t IOSurfaceGetHeight(IOSurfacePtr aIOSurfacePtr);
|
|
|
|
static size_t IOSurfaceGetBytesPerRow(IOSurfacePtr aIOSurfacePtr);
|
|
|
|
static IOReturn IOSurfaceLock(IOSurfacePtr aIOSurfacePtr,
|
|
|
|
uint32_t options, uint32_t *seed);
|
|
|
|
static IOReturn IOSurfaceUnlock(IOSurfacePtr aIOSurfacePtr,
|
|
|
|
uint32_t options, uint32_t *seed);
|
|
|
|
static CGLError CGLTexImageIOSurface2D(CGLContextObj ctxt,
|
|
|
|
GLenum target, GLenum internalFormat,
|
|
|
|
GLsizei width, GLsizei height,
|
|
|
|
GLenum format, GLenum type,
|
|
|
|
IOSurfacePtr ioSurface, GLuint plane);
|
2012-07-31 08:11:57 -07:00
|
|
|
static CGContextRef IOSurfaceContextCreate(IOSurfacePtr aIOSurfacePtr,
|
|
|
|
unsigned aWidth, unsigned aHeight,
|
|
|
|
unsigned aBitsPerCompoent, unsigned aBytes,
|
|
|
|
CGColorSpaceRef aColorSpace, CGBitmapInfo bitmapInfo);
|
|
|
|
static CGImageRef IOSurfaceContextCreateImage(CGContextRef ref);
|
|
|
|
static IOSurfacePtr IOSurfaceContextGetSurface(CGContextRef ref);
|
|
|
|
static unsigned int (*sCGContextGetTypePtr) (CGContextRef);
|
2010-04-20 07:52:19 -07:00
|
|
|
static void LoadLibrary();
|
|
|
|
static void CloseLibrary();
|
|
|
|
|
|
|
|
// Static deconstructor
|
|
|
|
static class LibraryUnloader {
|
|
|
|
public:
|
|
|
|
~LibraryUnloader() {
|
|
|
|
CloseLibrary();
|
|
|
|
}
|
|
|
|
} sLibraryUnloader;
|
|
|
|
};
|
|
|
|
|
2012-07-31 08:11:57 -07:00
|
|
|
MacIOSurfaceLib::LibraryUnloader MacIOSurfaceLib::sLibraryUnloader;
|
|
|
|
bool MacIOSurfaceLib::isLoaded = false;
|
|
|
|
void* MacIOSurfaceLib::sIOSurfaceFramework;
|
|
|
|
void* MacIOSurfaceLib::sOpenGLFramework;
|
|
|
|
void* MacIOSurfaceLib::sCoreGraphicsFramework;
|
|
|
|
IOSurfaceCreateFunc MacIOSurfaceLib::sCreate;
|
|
|
|
IOSurfaceGetIDFunc MacIOSurfaceLib::sGetID;
|
|
|
|
IOSurfaceLookupFunc MacIOSurfaceLib::sLookup;
|
|
|
|
IOSurfaceGetBaseAddressFunc MacIOSurfaceLib::sGetBaseAddress;
|
2012-09-05 14:31:30 -07:00
|
|
|
IOSurfaceGetWidthFunc MacIOSurfaceLib::sWidth;
|
|
|
|
IOSurfaceGetHeightFunc MacIOSurfaceLib::sHeight;
|
2012-07-31 08:11:57 -07:00
|
|
|
IOSurfaceGetBytesPerRowFunc MacIOSurfaceLib::sBytesPerRow;
|
|
|
|
IOSurfaceLockFunc MacIOSurfaceLib::sLock;
|
|
|
|
IOSurfaceUnlockFunc MacIOSurfaceLib::sUnlock;
|
|
|
|
CGLTexImageIOSurface2DFunc MacIOSurfaceLib::sTexImage;
|
|
|
|
IOSurfaceContextCreateFunc MacIOSurfaceLib::sIOSurfaceContextCreate;
|
|
|
|
IOSurfaceContextCreateImageFunc MacIOSurfaceLib::sIOSurfaceContextCreateImage;
|
|
|
|
IOSurfaceContextGetSurfaceFunc MacIOSurfaceLib::sIOSurfaceContextGetSurface;
|
2012-08-14 11:06:12 -07:00
|
|
|
unsigned int (*MacIOSurfaceLib::sCGContextGetTypePtr) (CGContextRef) = nullptr;
|
2012-07-31 08:11:57 -07:00
|
|
|
|
|
|
|
CFStringRef MacIOSurfaceLib::kPropWidth;
|
|
|
|
CFStringRef MacIOSurfaceLib::kPropHeight;
|
|
|
|
CFStringRef MacIOSurfaceLib::kPropBytesPerElem;
|
|
|
|
CFStringRef MacIOSurfaceLib::kPropBytesPerRow;
|
|
|
|
CFStringRef MacIOSurfaceLib::kPropIsGlobal;
|
|
|
|
|
|
|
|
bool MacIOSurfaceLib::isInit() {
|
2010-04-20 07:52:19 -07:00
|
|
|
// Guard against trying to reload the library
|
|
|
|
// if it is not available.
|
|
|
|
if (!isLoaded)
|
|
|
|
LoadLibrary();
|
|
|
|
if (!sIOSurfaceFramework) {
|
2012-07-31 08:11:57 -07:00
|
|
|
NS_ERROR("MacIOSurfaceLib failed to initialize");
|
2010-04-20 07:52:19 -07:00
|
|
|
}
|
|
|
|
return sIOSurfaceFramework;
|
|
|
|
}
|
|
|
|
|
2012-07-31 08:11:57 -07:00
|
|
|
IOSurfacePtr MacIOSurfaceLib::IOSurfaceCreate(CFDictionaryRef properties) {
|
2010-04-20 07:52:19 -07:00
|
|
|
return sCreate(properties);
|
|
|
|
}
|
|
|
|
|
2012-07-31 08:11:57 -07:00
|
|
|
IOSurfacePtr MacIOSurfaceLib::IOSurfaceLookup(IOSurfaceID aIOSurfaceID) {
|
2010-04-20 07:52:19 -07:00
|
|
|
return sLookup(aIOSurfaceID);
|
|
|
|
}
|
|
|
|
|
2012-07-31 08:11:57 -07:00
|
|
|
IOSurfaceID MacIOSurfaceLib::IOSurfaceGetID(IOSurfacePtr aIOSurfacePtr) {
|
2010-04-20 07:52:19 -07:00
|
|
|
return sGetID(aIOSurfacePtr);
|
|
|
|
}
|
|
|
|
|
2012-07-31 08:11:57 -07:00
|
|
|
void* MacIOSurfaceLib::IOSurfaceGetBaseAddress(IOSurfacePtr aIOSurfacePtr) {
|
2010-04-20 07:52:19 -07:00
|
|
|
return sGetBaseAddress(aIOSurfacePtr);
|
|
|
|
}
|
|
|
|
|
2012-07-31 08:11:57 -07:00
|
|
|
size_t MacIOSurfaceLib::IOSurfaceGetWidth(IOSurfacePtr aIOSurfacePtr) {
|
2010-04-20 07:52:19 -07:00
|
|
|
return sWidth(aIOSurfacePtr);
|
|
|
|
}
|
|
|
|
|
2012-07-31 08:11:57 -07:00
|
|
|
size_t MacIOSurfaceLib::IOSurfaceGetHeight(IOSurfacePtr aIOSurfacePtr) {
|
2010-04-20 07:52:19 -07:00
|
|
|
return sHeight(aIOSurfacePtr);
|
|
|
|
}
|
|
|
|
|
2012-07-31 08:11:57 -07:00
|
|
|
size_t MacIOSurfaceLib::IOSurfaceGetBytesPerRow(IOSurfacePtr aIOSurfacePtr) {
|
2010-04-20 07:52:19 -07:00
|
|
|
return sBytesPerRow(aIOSurfacePtr);
|
|
|
|
}
|
|
|
|
|
2012-07-31 08:11:57 -07:00
|
|
|
IOReturn MacIOSurfaceLib::IOSurfaceLock(IOSurfacePtr aIOSurfacePtr,
|
2010-04-20 07:52:19 -07:00
|
|
|
uint32_t options, uint32_t *seed) {
|
|
|
|
return sLock(aIOSurfacePtr, options, seed);
|
|
|
|
}
|
|
|
|
|
2012-07-31 08:11:57 -07:00
|
|
|
IOReturn MacIOSurfaceLib::IOSurfaceUnlock(IOSurfacePtr aIOSurfacePtr,
|
2010-04-20 07:52:19 -07:00
|
|
|
uint32_t options, uint32_t *seed) {
|
|
|
|
return sUnlock(aIOSurfacePtr, options, seed);
|
|
|
|
}
|
|
|
|
|
2012-07-31 08:11:57 -07:00
|
|
|
CGLError MacIOSurfaceLib::CGLTexImageIOSurface2D(CGLContextObj ctxt,
|
2010-04-20 07:52:19 -07:00
|
|
|
GLenum target, GLenum internalFormat,
|
|
|
|
GLsizei width, GLsizei height,
|
|
|
|
GLenum format, GLenum type,
|
|
|
|
IOSurfacePtr ioSurface, GLuint plane) {
|
2012-01-31 18:18:30 -08:00
|
|
|
return sTexImage(ctxt, target, internalFormat, width, height,
|
2010-04-20 07:52:19 -07:00
|
|
|
format, type, ioSurface, plane);
|
|
|
|
}
|
|
|
|
|
2012-07-31 08:11:57 -07:00
|
|
|
CGContextRef MacIOSurfaceLib::IOSurfaceContextCreate(IOSurfacePtr aIOSurfacePtr,
|
|
|
|
unsigned aWidth, unsigned aHeight,
|
|
|
|
unsigned aBitsPerComponent, unsigned aBytes,
|
|
|
|
CGColorSpaceRef aColorSpace, CGBitmapInfo bitmapInfo) {
|
|
|
|
if (!sIOSurfaceContextCreate)
|
2012-08-14 11:06:12 -07:00
|
|
|
return nullptr;
|
2012-07-31 08:11:57 -07:00
|
|
|
return sIOSurfaceContextCreate(aIOSurfacePtr, aWidth, aHeight, aBitsPerComponent, aBytes, aColorSpace, bitmapInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
CGImageRef MacIOSurfaceLib::IOSurfaceContextCreateImage(CGContextRef aContext) {
|
|
|
|
if (!sIOSurfaceContextCreateImage)
|
2012-08-14 11:06:12 -07:00
|
|
|
return nullptr;
|
2012-07-31 08:11:57 -07:00
|
|
|
return sIOSurfaceContextCreateImage(aContext);
|
|
|
|
}
|
|
|
|
|
|
|
|
IOSurfacePtr MacIOSurfaceLib::IOSurfaceContextGetSurface(CGContextRef aContext) {
|
|
|
|
if (!sIOSurfaceContextGetSurface)
|
2012-08-14 11:06:12 -07:00
|
|
|
return nullptr;
|
2012-07-31 08:11:57 -07:00
|
|
|
return sIOSurfaceContextGetSurface(aContext);
|
|
|
|
}
|
|
|
|
|
|
|
|
CFStringRef MacIOSurfaceLib::GetIOConst(const char* symbole) {
|
2010-04-20 07:52:19 -07:00
|
|
|
CFStringRef *address = (CFStringRef*)dlsym(sIOSurfaceFramework, symbole);
|
|
|
|
if (!address)
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2010-04-20 07:52:19 -07:00
|
|
|
|
|
|
|
return *address;
|
|
|
|
}
|
|
|
|
|
2012-07-31 08:11:57 -07:00
|
|
|
void MacIOSurfaceLib::LoadLibrary() {
|
2010-04-20 07:52:19 -07:00
|
|
|
if (isLoaded) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
isLoaded = true;
|
2012-07-31 08:11:57 -07:00
|
|
|
sIOSurfaceFramework = dlopen(IOSURFACE_FRAMEWORK_PATH,
|
2010-04-20 07:52:19 -07:00
|
|
|
RTLD_LAZY | RTLD_LOCAL);
|
2012-07-31 08:11:57 -07:00
|
|
|
sOpenGLFramework = dlopen(OPENGL_FRAMEWORK_PATH,
|
2010-04-20 07:52:19 -07:00
|
|
|
RTLD_LAZY | RTLD_LOCAL);
|
2012-07-31 08:11:57 -07:00
|
|
|
|
|
|
|
sCoreGraphicsFramework = dlopen(COREGRAPHICS_FRAMEWORK_PATH,
|
|
|
|
RTLD_LAZY | RTLD_LOCAL);
|
|
|
|
if (!sIOSurfaceFramework || !sOpenGLFramework || !sCoreGraphicsFramework) {
|
|
|
|
if (sIOSurfaceFramework)
|
|
|
|
dlclose(sIOSurfaceFramework);
|
|
|
|
if (sOpenGLFramework)
|
|
|
|
dlclose(sOpenGLFramework);
|
|
|
|
if (sCoreGraphicsFramework)
|
|
|
|
dlclose(sCoreGraphicsFramework);
|
2012-08-12 03:43:47 -07:00
|
|
|
sIOSurfaceFramework = nullptr;
|
|
|
|
sOpenGLFramework = nullptr;
|
|
|
|
sCoreGraphicsFramework = nullptr;
|
2010-04-20 07:52:19 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
kPropWidth = GetIOConst("kIOSurfaceWidth");
|
|
|
|
kPropHeight = GetIOConst("kIOSurfaceHeight");
|
|
|
|
kPropBytesPerElem = GetIOConst("kIOSurfaceBytesPerElement");
|
|
|
|
kPropBytesPerRow = GetIOConst("kIOSurfaceBytesPerRow");
|
|
|
|
kPropIsGlobal = GetIOConst("kIOSurfaceIsGlobal");
|
|
|
|
sCreate = GET_IOSYM(sCreate, "IOSurfaceCreate");
|
|
|
|
sGetID = GET_IOSYM(sGetID, "IOSurfaceGetID");
|
|
|
|
sWidth = GET_IOSYM(sWidth, "IOSurfaceGetWidth");
|
|
|
|
sHeight = GET_IOSYM(sHeight, "IOSurfaceGetHeight");
|
|
|
|
sBytesPerRow = GET_IOSYM(sBytesPerRow, "IOSurfaceGetBytesPerRow");
|
|
|
|
sLookup = GET_IOSYM(sLookup, "IOSurfaceLookup");
|
|
|
|
sLock = GET_IOSYM(sLock, "IOSurfaceLock");
|
|
|
|
sUnlock = GET_IOSYM(sUnlock, "IOSurfaceUnlock");
|
|
|
|
sGetBaseAddress = GET_IOSYM(sGetBaseAddress, "IOSurfaceGetBaseAddress");
|
|
|
|
sTexImage = GET_CGLSYM(sTexImage, "CGLTexImageIOSurface2D");
|
2012-07-31 08:11:57 -07:00
|
|
|
sCGContextGetTypePtr = (unsigned int (*)(CGContext*))dlsym(RTLD_DEFAULT, "CGContextGetType");
|
|
|
|
|
|
|
|
// Optional symbols
|
|
|
|
sIOSurfaceContextCreate = GET_CGSYM(sIOSurfaceContextCreate, "CGIOSurfaceContextCreate");
|
|
|
|
sIOSurfaceContextCreateImage = GET_CGSYM(sIOSurfaceContextCreateImage, "CGIOSurfaceContextCreateImage");
|
|
|
|
sIOSurfaceContextGetSurface = GET_CGSYM(sIOSurfaceContextGetSurface, "CGIOSurfaceContextGetSurface");
|
2010-04-20 07:52:19 -07:00
|
|
|
|
|
|
|
if (!sCreate || !sGetID || !sLookup || !sTexImage || !sGetBaseAddress ||
|
|
|
|
!kPropWidth || !kPropHeight || !kPropBytesPerElem || !kPropIsGlobal ||
|
|
|
|
!sLock || !sUnlock || !sWidth || !sHeight || !kPropBytesPerRow ||
|
|
|
|
!sBytesPerRow) {
|
|
|
|
CloseLibrary();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-31 08:11:57 -07:00
|
|
|
void MacIOSurfaceLib::CloseLibrary() {
|
2010-04-20 07:52:19 -07:00
|
|
|
if (sIOSurfaceFramework) {
|
|
|
|
dlclose(sIOSurfaceFramework);
|
|
|
|
}
|
|
|
|
if (sOpenGLFramework) {
|
|
|
|
dlclose(sOpenGLFramework);
|
|
|
|
}
|
2012-07-30 07:20:58 -07:00
|
|
|
sIOSurfaceFramework = nullptr;
|
|
|
|
sOpenGLFramework = nullptr;
|
2010-04-20 07:52:19 -07:00
|
|
|
}
|
|
|
|
|
2012-07-31 08:11:57 -07:00
|
|
|
MacIOSurface::~MacIOSurface() {
|
2012-01-31 18:18:30 -08:00
|
|
|
CFRelease(mIOSurfacePtr);
|
|
|
|
}
|
|
|
|
|
2012-10-02 12:48:05 -07:00
|
|
|
TemporaryRef<MacIOSurface> MacIOSurface::CreateIOSurface(int aWidth, int aHeight,
|
2013-07-17 20:24:14 -07:00
|
|
|
double aContentsScaleFactor,
|
|
|
|
bool aHasAlpha) {
|
2012-10-02 12:48:05 -07:00
|
|
|
if (!MacIOSurfaceLib::isInit() || aContentsScaleFactor <= 0)
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2010-04-20 07:52:19 -07:00
|
|
|
|
|
|
|
CFMutableDictionaryRef props = ::CFDictionaryCreateMutable(
|
|
|
|
kCFAllocatorDefault, 4,
|
|
|
|
&kCFTypeDictionaryKeyCallBacks,
|
|
|
|
&kCFTypeDictionaryValueCallBacks);
|
|
|
|
if (!props)
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2010-04-20 07:52:19 -07:00
|
|
|
|
|
|
|
int32_t bytesPerElem = 4;
|
2012-10-02 12:48:05 -07:00
|
|
|
size_t intScaleFactor = ceil(aContentsScaleFactor);
|
|
|
|
aWidth *= intScaleFactor;
|
|
|
|
aHeight *= intScaleFactor;
|
2012-08-14 11:06:12 -07:00
|
|
|
CFNumberRef cfWidth = ::CFNumberCreate(nullptr, kCFNumberSInt32Type, &aWidth);
|
|
|
|
CFNumberRef cfHeight = ::CFNumberCreate(nullptr, kCFNumberSInt32Type, &aHeight);
|
|
|
|
CFNumberRef cfBytesPerElem = ::CFNumberCreate(nullptr, kCFNumberSInt32Type, &bytesPerElem);
|
2012-07-31 08:11:57 -07:00
|
|
|
::CFDictionaryAddValue(props, MacIOSurfaceLib::kPropWidth,
|
2010-04-20 07:52:19 -07:00
|
|
|
cfWidth);
|
|
|
|
::CFRelease(cfWidth);
|
2012-07-31 08:11:57 -07:00
|
|
|
::CFDictionaryAddValue(props, MacIOSurfaceLib::kPropHeight,
|
2010-04-20 07:52:19 -07:00
|
|
|
cfHeight);
|
|
|
|
::CFRelease(cfHeight);
|
2012-07-31 08:11:57 -07:00
|
|
|
::CFDictionaryAddValue(props, MacIOSurfaceLib::kPropBytesPerElem,
|
2010-04-20 07:52:19 -07:00
|
|
|
cfBytesPerElem);
|
|
|
|
::CFRelease(cfBytesPerElem);
|
2012-07-31 08:11:57 -07:00
|
|
|
::CFDictionaryAddValue(props, MacIOSurfaceLib::kPropIsGlobal,
|
2010-04-20 07:52:19 -07:00
|
|
|
kCFBooleanTrue);
|
|
|
|
|
2012-07-31 08:11:57 -07:00
|
|
|
IOSurfacePtr surfaceRef = MacIOSurfaceLib::IOSurfaceCreate(props);
|
2010-04-20 07:52:19 -07:00
|
|
|
::CFRelease(props);
|
|
|
|
|
|
|
|
if (!surfaceRef)
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2010-04-20 07:52:19 -07:00
|
|
|
|
2013-07-17 20:24:14 -07:00
|
|
|
RefPtr<MacIOSurface> ioSurface = new MacIOSurface(surfaceRef, aContentsScaleFactor, aHasAlpha);
|
2010-04-20 07:52:19 -07:00
|
|
|
if (!ioSurface) {
|
|
|
|
::CFRelease(surfaceRef);
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2010-04-20 07:52:19 -07:00
|
|
|
}
|
|
|
|
|
2011-07-12 07:31:18 -07:00
|
|
|
return ioSurface.forget();
|
2010-04-20 07:52:19 -07:00
|
|
|
}
|
|
|
|
|
2012-10-02 12:48:05 -07:00
|
|
|
TemporaryRef<MacIOSurface> MacIOSurface::LookupSurface(IOSurfaceID aIOSurfaceID,
|
2013-07-17 20:24:14 -07:00
|
|
|
double aContentsScaleFactor,
|
|
|
|
bool aHasAlpha) {
|
2012-10-02 12:48:05 -07:00
|
|
|
if (!MacIOSurfaceLib::isInit() || aContentsScaleFactor <= 0)
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2010-04-20 07:52:19 -07:00
|
|
|
|
2012-07-31 08:11:57 -07:00
|
|
|
IOSurfacePtr surfaceRef = MacIOSurfaceLib::IOSurfaceLookup(aIOSurfaceID);
|
2010-04-20 07:52:19 -07:00
|
|
|
if (!surfaceRef)
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2010-04-20 07:52:19 -07:00
|
|
|
|
2013-07-17 20:24:14 -07:00
|
|
|
RefPtr<MacIOSurface> ioSurface = new MacIOSurface(surfaceRef, aContentsScaleFactor, aHasAlpha);
|
2010-04-20 07:52:19 -07:00
|
|
|
if (!ioSurface) {
|
2011-02-12 08:02:08 -08:00
|
|
|
::CFRelease(surfaceRef);
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2010-04-20 07:52:19 -07:00
|
|
|
}
|
2011-07-12 07:31:18 -07:00
|
|
|
return ioSurface.forget();
|
2010-04-20 07:52:19 -07:00
|
|
|
}
|
|
|
|
|
2012-07-31 08:11:57 -07:00
|
|
|
IOSurfaceID MacIOSurface::GetIOSurfaceID() {
|
|
|
|
return MacIOSurfaceLib::IOSurfaceGetID(mIOSurfacePtr);
|
2010-04-20 07:52:19 -07:00
|
|
|
}
|
|
|
|
|
2012-07-31 08:11:57 -07:00
|
|
|
void* MacIOSurface::GetBaseAddress() {
|
|
|
|
return MacIOSurfaceLib::IOSurfaceGetBaseAddress(mIOSurfacePtr);
|
2010-04-20 07:52:19 -07:00
|
|
|
}
|
|
|
|
|
2012-10-02 12:48:05 -07:00
|
|
|
size_t MacIOSurface::GetWidth() {
|
|
|
|
size_t intScaleFactor = ceil(mContentsScaleFactor);
|
2013-04-24 15:31:12 -07:00
|
|
|
return GetDevicePixelWidth() / intScaleFactor;
|
2010-04-20 07:52:19 -07:00
|
|
|
}
|
|
|
|
|
2012-10-02 12:48:05 -07:00
|
|
|
size_t MacIOSurface::GetHeight() {
|
|
|
|
size_t intScaleFactor = ceil(mContentsScaleFactor);
|
2013-04-24 15:31:12 -07:00
|
|
|
return GetDevicePixelHeight() / intScaleFactor;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t MacIOSurface::GetDevicePixelWidth() {
|
|
|
|
return MacIOSurfaceLib::IOSurfaceGetWidth(mIOSurfacePtr);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t MacIOSurface::GetDevicePixelHeight() {
|
|
|
|
return MacIOSurfaceLib::IOSurfaceGetHeight(mIOSurfacePtr);
|
2010-04-20 07:52:19 -07:00
|
|
|
}
|
|
|
|
|
2012-07-31 08:11:57 -07:00
|
|
|
size_t MacIOSurface::GetBytesPerRow() {
|
|
|
|
return MacIOSurfaceLib::IOSurfaceGetBytesPerRow(mIOSurfacePtr);
|
2010-04-20 07:52:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#define READ_ONLY 0x1
|
2012-07-31 08:11:57 -07:00
|
|
|
void MacIOSurface::Lock() {
|
2012-08-14 11:06:12 -07:00
|
|
|
MacIOSurfaceLib::IOSurfaceLock(mIOSurfacePtr, READ_ONLY, nullptr);
|
2010-04-20 07:52:19 -07:00
|
|
|
}
|
|
|
|
|
2012-07-31 08:11:57 -07:00
|
|
|
void MacIOSurface::Unlock() {
|
2012-08-14 11:06:12 -07:00
|
|
|
MacIOSurfaceLib::IOSurfaceUnlock(mIOSurfacePtr, READ_ONLY, nullptr);
|
2010-04-20 07:52:19 -07:00
|
|
|
}
|
|
|
|
|
2012-07-31 08:11:57 -07:00
|
|
|
#include "SourceSurfaceRawData.h"
|
|
|
|
using mozilla::gfx::SourceSurface;
|
|
|
|
using mozilla::gfx::SourceSurfaceRawData;
|
|
|
|
using mozilla::gfx::IntSize;
|
2013-07-17 20:24:14 -07:00
|
|
|
using mozilla::gfx::SurfaceFormat;
|
2012-01-31 18:18:30 -08:00
|
|
|
|
2012-07-31 08:11:57 -07:00
|
|
|
TemporaryRef<SourceSurface>
|
|
|
|
MacIOSurface::GetAsSurface() {
|
2012-01-31 18:18:30 -08:00
|
|
|
Lock();
|
|
|
|
size_t bytesPerRow = GetBytesPerRow();
|
2013-04-24 15:31:12 -07:00
|
|
|
size_t ioWidth = GetDevicePixelWidth();
|
|
|
|
size_t ioHeight = GetDevicePixelHeight();
|
2012-01-31 18:18:30 -08:00
|
|
|
|
|
|
|
unsigned char* ioData = (unsigned char*)GetBaseAddress();
|
2012-07-31 08:11:57 -07:00
|
|
|
unsigned char* dataCpy = (unsigned char*)malloc(bytesPerRow*ioHeight);
|
|
|
|
for (size_t i = 0; i < ioHeight; i++) {
|
|
|
|
memcpy(dataCpy + i * bytesPerRow,
|
2012-01-31 18:18:30 -08:00
|
|
|
ioData + i * bytesPerRow, ioWidth * 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
Unlock();
|
|
|
|
|
2013-07-17 20:24:14 -07:00
|
|
|
SurfaceFormat format = HasAlpha() ? mozilla::gfx::FORMAT_B8G8R8A8 :
|
|
|
|
mozilla::gfx::FORMAT_B8G8R8X8;
|
|
|
|
|
2012-07-31 08:11:57 -07:00
|
|
|
RefPtr<SourceSurfaceRawData> surf = new SourceSurfaceRawData();
|
2013-07-17 20:24:14 -07:00
|
|
|
surf->InitWrappingData(dataCpy, IntSize(ioWidth, ioHeight), bytesPerRow, format, true);
|
2012-07-31 08:11:57 -07:00
|
|
|
|
|
|
|
return surf.forget();
|
2012-01-31 18:18:30 -08:00
|
|
|
}
|
|
|
|
|
2011-02-12 08:02:08 -08:00
|
|
|
CGLError
|
2013-11-07 19:53:54 -08:00
|
|
|
MacIOSurface::CGLTexImageIOSurface2D(CGLContextObj ctxt)
|
2011-02-12 08:02:08 -08:00
|
|
|
{
|
2013-11-07 19:53:54 -08:00
|
|
|
return MacIOSurfaceLib::CGLTexImageIOSurface2D(ctxt,
|
2012-01-31 18:18:30 -08:00
|
|
|
GL_TEXTURE_RECTANGLE_ARB,
|
2013-07-17 20:24:14 -07:00
|
|
|
HasAlpha() ? LOCAL_GL_RGBA : LOCAL_GL_RGB,
|
2013-04-24 15:31:12 -07:00
|
|
|
GetDevicePixelWidth(),
|
|
|
|
GetDevicePixelHeight(),
|
2013-07-17 20:24:14 -07:00
|
|
|
LOCAL_GL_BGRA,
|
|
|
|
LOCAL_GL_UNSIGNED_INT_8_8_8_8_REV,
|
|
|
|
mIOSurfacePtr, 0);
|
2011-02-12 08:02:08 -08:00
|
|
|
}
|
|
|
|
|
2010-04-20 07:52:19 -07:00
|
|
|
CGColorSpaceRef CreateSystemColorSpace() {
|
2013-10-07 14:03:19 -07:00
|
|
|
CGColorSpaceRef cspace = ::CGDisplayCopyColorSpace(::CGMainDisplayID());
|
|
|
|
if (!cspace) {
|
|
|
|
cspace = ::CGColorSpaceCreateDeviceRGB();
|
|
|
|
}
|
|
|
|
return cspace;
|
2010-04-20 07:52:19 -07:00
|
|
|
}
|
|
|
|
|
2012-07-31 08:11:57 -07:00
|
|
|
CGContextRef MacIOSurface::CreateIOSurfaceContext() {
|
2013-10-07 14:03:19 -07:00
|
|
|
CGColorSpaceRef cspace = CreateSystemColorSpace();
|
2012-10-02 12:48:05 -07:00
|
|
|
CGContextRef ref = MacIOSurfaceLib::IOSurfaceContextCreate(mIOSurfacePtr,
|
2013-04-24 15:31:12 -07:00
|
|
|
GetDevicePixelWidth(),
|
|
|
|
GetDevicePixelHeight(),
|
2013-10-07 14:03:19 -07:00
|
|
|
8, 32, cspace, 0x2002);
|
|
|
|
::CGColorSpaceRelease(cspace);
|
2012-07-31 08:11:57 -07:00
|
|
|
return ref;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCARenderer::~nsCARenderer() {
|
|
|
|
Destroy();
|
|
|
|
}
|
|
|
|
|
2010-04-20 07:52:19 -07:00
|
|
|
void cgdata_release_callback(void *aCGData, const void *data, size_t size) {
|
|
|
|
if (aCGData) {
|
|
|
|
free(aCGData);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void nsCARenderer::Destroy() {
|
|
|
|
if (mCARenderer) {
|
|
|
|
CARenderer* caRenderer = (CARenderer*)mCARenderer;
|
|
|
|
// Bug 556453:
|
|
|
|
// Explicitly remove the layer from the renderer
|
|
|
|
// otherwise it does not always happen right away.
|
2012-07-30 07:20:58 -07:00
|
|
|
caRenderer.layer = nullptr;
|
2010-04-20 07:52:19 -07:00
|
|
|
[caRenderer release];
|
|
|
|
}
|
2013-01-23 14:19:15 -08:00
|
|
|
if (mWrapperCALayer) {
|
|
|
|
CALayer* wrapperLayer = (CALayer*)mWrapperCALayer;
|
|
|
|
[wrapperLayer release];
|
|
|
|
}
|
2010-04-20 07:52:19 -07:00
|
|
|
if (mOpenGLContext) {
|
2011-10-13 07:36:59 -07:00
|
|
|
if (mFBO || mIOTexture || mFBOTexture) {
|
2010-04-20 07:52:19 -07:00
|
|
|
// Release these resources with the context that allocated them
|
|
|
|
CGLContextObj oldContext = ::CGLGetCurrentContext();
|
|
|
|
::CGLSetCurrentContext(mOpenGLContext);
|
|
|
|
|
2011-10-13 07:36:59 -07:00
|
|
|
if (mFBOTexture) {
|
|
|
|
::glDeleteTextures(1, &mFBOTexture);
|
|
|
|
}
|
2010-04-20 07:52:19 -07:00
|
|
|
if (mIOTexture) {
|
|
|
|
::glDeleteTextures(1, &mIOTexture);
|
|
|
|
}
|
|
|
|
if (mFBO) {
|
|
|
|
::glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
|
|
|
|
::glDeleteFramebuffersEXT(1, &mFBO);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (oldContext)
|
|
|
|
::CGLSetCurrentContext(oldContext);
|
|
|
|
}
|
|
|
|
::CGLDestroyContext((CGLContextObj)mOpenGLContext);
|
|
|
|
}
|
|
|
|
if (mCGImage) {
|
|
|
|
::CGImageRelease(mCGImage);
|
|
|
|
}
|
|
|
|
// mCGData is deallocated by cgdata_release_callback
|
|
|
|
|
|
|
|
mCARenderer = nil;
|
2013-01-23 14:19:15 -08:00
|
|
|
mWrapperCALayer = nil;
|
2011-10-13 07:36:59 -07:00
|
|
|
mFBOTexture = 0;
|
2012-07-30 07:20:58 -07:00
|
|
|
mOpenGLContext = nullptr;
|
|
|
|
mCGImage = nullptr;
|
|
|
|
mIOSurface = nullptr;
|
2012-07-20 04:16:17 -07:00
|
|
|
mFBO = 0;
|
|
|
|
mIOTexture = 0;
|
2010-04-20 07:52:19 -07:00
|
|
|
}
|
|
|
|
|
2011-10-20 13:32:38 -07:00
|
|
|
nsresult nsCARenderer::SetupRenderer(void *aCALayer, int aWidth, int aHeight,
|
2012-10-02 12:48:05 -07:00
|
|
|
double aContentsScaleFactor,
|
2011-10-20 13:32:38 -07:00
|
|
|
AllowOfflineRendererEnum aAllowOfflineRenderer) {
|
|
|
|
mAllowOfflineRenderer = aAllowOfflineRenderer;
|
|
|
|
|
2012-10-02 12:48:05 -07:00
|
|
|
if (aWidth == 0 || aHeight == 0 || aContentsScaleFactor <= 0)
|
2010-05-20 12:22:37 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2011-10-13 07:36:59 -07:00
|
|
|
if (aWidth == mUnsupportedWidth &&
|
2010-08-01 16:15:25 -07:00
|
|
|
aHeight == mUnsupportedHeight) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2010-04-20 07:52:19 -07:00
|
|
|
CGLPixelFormatAttribute attributes[] = {
|
|
|
|
kCGLPFAAccelerated,
|
|
|
|
kCGLPFADepthSize, (CGLPixelFormatAttribute)24,
|
2011-10-20 13:32:38 -07:00
|
|
|
kCGLPFAAllowOfflineRenderers,
|
2010-04-20 07:52:19 -07:00
|
|
|
(CGLPixelFormatAttribute)0
|
|
|
|
};
|
|
|
|
|
2011-10-20 13:32:38 -07:00
|
|
|
if (mAllowOfflineRenderer == DISALLOW_OFFLINE_RENDERER) {
|
|
|
|
attributes[3] = (CGLPixelFormatAttribute)0;
|
|
|
|
}
|
|
|
|
|
2010-04-20 07:52:19 -07:00
|
|
|
GLint screen;
|
|
|
|
CGLPixelFormatObj format;
|
|
|
|
if (::CGLChoosePixelFormat(attributes, &format, &screen) != kCGLNoError) {
|
2010-08-01 16:15:25 -07:00
|
|
|
mUnsupportedWidth = aWidth;
|
|
|
|
mUnsupportedHeight = aHeight;
|
2010-04-20 07:52:19 -07:00
|
|
|
Destroy();
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
if (::CGLCreateContext(format, nullptr, &mOpenGLContext) != kCGLNoError) {
|
2010-08-01 16:15:25 -07:00
|
|
|
mUnsupportedWidth = aWidth;
|
|
|
|
mUnsupportedHeight = aHeight;
|
2010-04-20 07:52:19 -07:00
|
|
|
Destroy();
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
::CGLDestroyPixelFormat(format);
|
|
|
|
|
2013-01-23 14:19:15 -08:00
|
|
|
CARenderer* caRenderer = [[CARenderer rendererWithCGLContext:mOpenGLContext
|
|
|
|
options:nil] retain];
|
2010-04-20 07:52:19 -07:00
|
|
|
if (caRenderer == nil) {
|
2010-08-01 16:15:25 -07:00
|
|
|
mUnsupportedWidth = aWidth;
|
|
|
|
mUnsupportedHeight = aHeight;
|
2010-04-20 07:52:19 -07:00
|
|
|
Destroy();
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2013-01-23 14:19:15 -08:00
|
|
|
CALayer* wrapperCALayer = [[CALayer layer] retain];
|
|
|
|
if (wrapperCALayer == nil) {
|
|
|
|
[caRenderer release];
|
|
|
|
mUnsupportedWidth = aWidth;
|
|
|
|
mUnsupportedHeight = aHeight;
|
|
|
|
Destroy();
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2011-06-30 09:46:25 -07:00
|
|
|
|
2013-01-23 14:19:15 -08:00
|
|
|
mCARenderer = caRenderer;
|
|
|
|
mWrapperCALayer = wrapperCALayer;
|
|
|
|
caRenderer.layer = wrapperCALayer;
|
|
|
|
[wrapperCALayer addSublayer:(CALayer*)aCALayer];
|
2012-10-02 12:48:05 -07:00
|
|
|
mContentsScaleFactor = aContentsScaleFactor;
|
|
|
|
size_t intScaleFactor = ceil(mContentsScaleFactor);
|
2011-11-15 14:36:06 -08:00
|
|
|
SetBounds(aWidth, aHeight);
|
2010-04-20 07:52:19 -07:00
|
|
|
|
2011-10-13 07:36:59 -07:00
|
|
|
// We target rendering to a CGImage if no shared IOSurface are given.
|
2010-04-20 07:52:19 -07:00
|
|
|
if (!mIOSurface) {
|
2012-10-02 12:48:05 -07:00
|
|
|
mCGData = malloc(aWidth*intScaleFactor*aHeight*4*intScaleFactor);
|
2010-04-20 07:52:19 -07:00
|
|
|
if (!mCGData) {
|
2010-08-01 16:15:25 -07:00
|
|
|
mUnsupportedWidth = aWidth;
|
|
|
|
mUnsupportedHeight = aHeight;
|
2010-04-20 07:52:19 -07:00
|
|
|
Destroy();
|
2012-04-05 15:36:40 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
2010-04-20 07:52:19 -07:00
|
|
|
}
|
2012-10-02 12:48:05 -07:00
|
|
|
memset(mCGData, 0, aWidth*intScaleFactor*aHeight*4*intScaleFactor);
|
2010-04-20 07:52:19 -07:00
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
CGDataProviderRef dataProvider = nullptr;
|
2010-04-20 07:52:19 -07:00
|
|
|
dataProvider = ::CGDataProviderCreateWithData(mCGData,
|
2012-10-02 12:48:05 -07:00
|
|
|
mCGData, aHeight*intScaleFactor*aWidth*4*intScaleFactor,
|
2010-04-20 07:52:19 -07:00
|
|
|
cgdata_release_callback);
|
|
|
|
if (!dataProvider) {
|
2012-10-02 12:48:05 -07:00
|
|
|
cgdata_release_callback(mCGData, mCGData,
|
|
|
|
aHeight*intScaleFactor*aWidth*4*intScaleFactor);
|
2010-08-01 16:15:25 -07:00
|
|
|
mUnsupportedWidth = aWidth;
|
|
|
|
mUnsupportedHeight = aHeight;
|
2010-04-20 07:52:19 -07:00
|
|
|
Destroy();
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
CGColorSpaceRef colorSpace = CreateSystemColorSpace();
|
|
|
|
|
2012-10-02 12:48:05 -07:00
|
|
|
mCGImage = ::CGImageCreate(aWidth * intScaleFactor, aHeight * intScaleFactor,
|
|
|
|
8, 32, aWidth * intScaleFactor * 4, colorSpace,
|
2010-04-20 07:52:19 -07:00
|
|
|
kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host,
|
2012-08-14 11:06:12 -07:00
|
|
|
dataProvider, nullptr, true, kCGRenderingIntentDefault);
|
2010-04-20 07:52:19 -07:00
|
|
|
|
|
|
|
::CGDataProviderRelease(dataProvider);
|
2013-10-07 14:03:19 -07:00
|
|
|
::CGColorSpaceRelease(colorSpace);
|
2010-04-20 07:52:19 -07:00
|
|
|
if (!mCGImage) {
|
2010-08-01 16:15:25 -07:00
|
|
|
mUnsupportedWidth = aWidth;
|
|
|
|
mUnsupportedHeight = aHeight;
|
2010-04-20 07:52:19 -07:00
|
|
|
Destroy();
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2011-10-13 07:36:59 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
CGLContextObj oldContext = ::CGLGetCurrentContext();
|
|
|
|
::CGLSetCurrentContext(mOpenGLContext);
|
2010-04-20 07:52:19 -07:00
|
|
|
|
2011-10-13 07:36:59 -07:00
|
|
|
if (mIOSurface) {
|
2010-04-20 07:52:19 -07:00
|
|
|
// Create the IOSurface mapped texture.
|
|
|
|
::glGenTextures(1, &mIOTexture);
|
|
|
|
::glBindTexture(GL_TEXTURE_RECTANGLE_ARB, mIOTexture);
|
|
|
|
::glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
|
|
|
::glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
2012-07-31 08:11:57 -07:00
|
|
|
MacIOSurfaceLib::CGLTexImageIOSurface2D(mOpenGLContext, GL_TEXTURE_RECTANGLE_ARB,
|
2012-10-02 12:48:05 -07:00
|
|
|
GL_RGBA, aWidth * intScaleFactor,
|
|
|
|
aHeight * intScaleFactor,
|
2011-10-13 07:36:59 -07:00
|
|
|
GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV,
|
2010-04-20 07:52:19 -07:00
|
|
|
mIOSurface->mIOSurfacePtr, 0);
|
|
|
|
::glBindTexture(GL_TEXTURE_RECTANGLE_ARB, 0);
|
2011-10-13 07:36:59 -07:00
|
|
|
} else {
|
|
|
|
::glGenTextures(1, &mFBOTexture);
|
|
|
|
::glBindTexture(GL_TEXTURE_RECTANGLE_ARB, mFBOTexture);
|
|
|
|
::glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
|
|
|
::glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
|
|
|
::glBindTexture(GL_TEXTURE_RECTANGLE_ARB, 0);
|
|
|
|
}
|
2010-04-20 07:52:19 -07:00
|
|
|
|
2011-10-13 07:36:59 -07:00
|
|
|
// Create the fbo
|
|
|
|
::glGenFramebuffersEXT(1, &mFBO);
|
|
|
|
::glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, mFBO);
|
|
|
|
if (mIOSurface) {
|
|
|
|
::glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,
|
2010-04-20 07:52:19 -07:00
|
|
|
GL_TEXTURE_RECTANGLE_ARB, mIOTexture, 0);
|
2011-10-13 07:36:59 -07:00
|
|
|
} else {
|
|
|
|
::glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,
|
|
|
|
GL_TEXTURE_RECTANGLE_ARB, mFBOTexture, 0);
|
|
|
|
}
|
2010-04-20 07:52:19 -07:00
|
|
|
|
|
|
|
|
2011-10-13 07:36:59 -07:00
|
|
|
// Make sure that the Framebuffer configuration is supported on the client machine
|
|
|
|
GLenum fboStatus;
|
|
|
|
fboStatus = ::glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
|
|
|
|
if (fboStatus != GL_FRAMEBUFFER_COMPLETE_EXT) {
|
|
|
|
NS_ERROR("FBO not supported");
|
2010-04-20 07:52:19 -07:00
|
|
|
if (oldContext)
|
|
|
|
::CGLSetCurrentContext(oldContext);
|
2011-10-13 07:36:59 -07:00
|
|
|
mUnsupportedWidth = aWidth;
|
|
|
|
mUnsupportedHeight = aHeight;
|
|
|
|
Destroy();
|
|
|
|
return NS_ERROR_FAILURE;
|
2010-04-20 07:52:19 -07:00
|
|
|
}
|
|
|
|
|
2011-11-15 14:36:06 -08:00
|
|
|
SetViewport(aWidth, aHeight);
|
2010-04-20 07:52:19 -07:00
|
|
|
|
|
|
|
GLenum result = ::glGetError();
|
|
|
|
if (result != GL_NO_ERROR) {
|
2010-06-17 13:28:38 -07:00
|
|
|
NS_ERROR("Unexpected OpenGL Error");
|
2010-08-01 16:15:25 -07:00
|
|
|
mUnsupportedWidth = aWidth;
|
|
|
|
mUnsupportedHeight = aHeight;
|
2010-04-20 07:52:19 -07:00
|
|
|
Destroy();
|
|
|
|
if (oldContext)
|
|
|
|
::CGLSetCurrentContext(oldContext);
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (oldContext)
|
|
|
|
::CGLSetCurrentContext(oldContext);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-11-15 14:36:06 -08:00
|
|
|
void nsCARenderer::SetBounds(int aWidth, int aHeight) {
|
|
|
|
CARenderer* caRenderer = (CARenderer*)mCARenderer;
|
2013-01-23 14:19:15 -08:00
|
|
|
CALayer* wrapperLayer = (CALayer*)mWrapperCALayer;
|
|
|
|
NSArray* sublayers = [wrapperLayer sublayers];
|
|
|
|
CALayer* pluginLayer = (CALayer*) [sublayers objectAtIndex:0];
|
2011-11-15 14:36:06 -08:00
|
|
|
|
|
|
|
// Create a transaction and disable animations
|
|
|
|
// to make the position update instant.
|
|
|
|
[CATransaction begin];
|
2013-01-23 14:19:15 -08:00
|
|
|
NSMutableDictionary *newActions =
|
|
|
|
[[NSMutableDictionary alloc] initWithObjectsAndKeys:
|
|
|
|
[NSNull null], @"onOrderIn",
|
|
|
|
[NSNull null], @"onOrderOut",
|
|
|
|
[NSNull null], @"sublayers",
|
|
|
|
[NSNull null], @"contents",
|
|
|
|
[NSNull null], @"position",
|
|
|
|
[NSNull null], @"bounds",
|
|
|
|
nil];
|
|
|
|
wrapperLayer.actions = newActions;
|
2011-11-15 14:36:06 -08:00
|
|
|
[newActions release];
|
|
|
|
|
2012-10-02 12:48:05 -07:00
|
|
|
// If we're in HiDPI mode, mContentsScaleFactor will (presumably) be 2.0.
|
|
|
|
// For some reason, to make things work properly in HiDPI mode we need to
|
|
|
|
// make caRenderer's 'bounds' and 'layer' different sizes -- to set 'bounds'
|
2013-01-23 14:19:15 -08:00
|
|
|
// to the size of 'layer's backing store. And to avoid this possibly
|
|
|
|
// confusing the plugin, we need to hide it's effects from the plugin by
|
|
|
|
// making pluginLayer (usually the CALayer* provided by the plugin) a
|
|
|
|
// sublayer of our own wrapperLayer (see bug 829284).
|
2012-10-02 12:48:05 -07:00
|
|
|
size_t intScaleFactor = ceil(mContentsScaleFactor);
|
2011-11-15 14:36:06 -08:00
|
|
|
[CATransaction setValue: [NSNumber numberWithFloat:0.0f] forKey: kCATransactionAnimationDuration];
|
|
|
|
[CATransaction setValue: (id) kCFBooleanTrue forKey: kCATransactionDisableActions];
|
2013-01-23 14:19:15 -08:00
|
|
|
[wrapperLayer setBounds:CGRectMake(0, 0, aWidth, aHeight)];
|
|
|
|
[wrapperLayer setPosition:CGPointMake(aWidth/2.0, aHeight/2.0)];
|
|
|
|
[pluginLayer setBounds:CGRectMake(0, 0, aWidth, aHeight)];
|
|
|
|
[pluginLayer setFrame:CGRectMake(0, 0, aWidth, aHeight)];
|
2012-10-02 12:48:05 -07:00
|
|
|
caRenderer.bounds = CGRectMake(0, 0, aWidth * intScaleFactor, aHeight * intScaleFactor);
|
|
|
|
if (mContentsScaleFactor != 1.0) {
|
2013-01-23 14:19:15 -08:00
|
|
|
CGAffineTransform affineTransform = [wrapperLayer affineTransform];
|
2012-10-02 12:48:05 -07:00
|
|
|
affineTransform.a = mContentsScaleFactor;
|
|
|
|
affineTransform.d = mContentsScaleFactor;
|
|
|
|
affineTransform.tx = ((double)aWidth)/mContentsScaleFactor;
|
|
|
|
affineTransform.ty = ((double)aHeight)/mContentsScaleFactor;
|
2013-01-23 14:19:15 -08:00
|
|
|
[wrapperLayer setAffineTransform:affineTransform];
|
2012-10-16 12:41:21 -07:00
|
|
|
} else {
|
|
|
|
// These settings are the default values. But they might have been
|
|
|
|
// changed as above if we were previously running in a HiDPI mode
|
|
|
|
// (i.e. if we just switched from that to a non-HiDPI mode).
|
2013-01-23 14:19:15 -08:00
|
|
|
[wrapperLayer setAffineTransform:CGAffineTransformIdentity];
|
2012-10-02 12:48:05 -07:00
|
|
|
}
|
2011-11-15 14:36:06 -08:00
|
|
|
[CATransaction commit];
|
|
|
|
}
|
|
|
|
|
|
|
|
void nsCARenderer::SetViewport(int aWidth, int aHeight) {
|
2012-10-02 12:48:05 -07:00
|
|
|
size_t intScaleFactor = ceil(mContentsScaleFactor);
|
|
|
|
aWidth *= intScaleFactor;
|
|
|
|
aHeight *= intScaleFactor;
|
|
|
|
|
2011-11-15 14:36:06 -08:00
|
|
|
::glViewport(0.0, 0.0, aWidth, aHeight);
|
|
|
|
::glMatrixMode(GL_PROJECTION);
|
|
|
|
::glLoadIdentity();
|
|
|
|
::glOrtho (0.0, aWidth, 0.0, aHeight, -1, 1);
|
|
|
|
|
|
|
|
// Render upside down to speed up CGContextDrawImage
|
|
|
|
::glTranslatef(0.0f, aHeight, 0.0);
|
|
|
|
::glScalef(1.0, -1.0, 1.0);
|
|
|
|
}
|
|
|
|
|
2012-07-31 08:11:57 -07:00
|
|
|
void nsCARenderer::AttachIOSurface(RefPtr<MacIOSurface> aSurface) {
|
2011-11-15 14:36:06 -08:00
|
|
|
if (mIOSurface &&
|
2010-04-20 07:52:19 -07:00
|
|
|
aSurface->GetIOSurfaceID() == mIOSurface->GetIOSurfaceID()) {
|
2011-07-12 07:31:18 -07:00
|
|
|
// This object isn't needed since we already have a
|
|
|
|
// handle to the same io surface.
|
2012-07-30 07:20:58 -07:00
|
|
|
aSurface = nullptr;
|
2010-04-20 07:52:19 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mIOSurface = aSurface;
|
2011-11-15 14:36:06 -08:00
|
|
|
|
|
|
|
// Update the framebuffer and viewport
|
|
|
|
if (mCARenderer) {
|
|
|
|
CARenderer* caRenderer = (CARenderer*)mCARenderer;
|
2012-10-02 12:48:05 -07:00
|
|
|
size_t intScaleFactor = ceil(mContentsScaleFactor);
|
|
|
|
int width = caRenderer.bounds.size.width / intScaleFactor;
|
|
|
|
int height = caRenderer.bounds.size.height / intScaleFactor;
|
2011-11-15 14:36:06 -08:00
|
|
|
|
|
|
|
CGLContextObj oldContext = ::CGLGetCurrentContext();
|
|
|
|
::CGLSetCurrentContext(mOpenGLContext);
|
|
|
|
::glBindTexture(GL_TEXTURE_RECTANGLE_ARB, mIOTexture);
|
2012-07-31 08:11:57 -07:00
|
|
|
MacIOSurfaceLib::CGLTexImageIOSurface2D(mOpenGLContext, GL_TEXTURE_RECTANGLE_ARB,
|
2013-04-24 15:31:12 -07:00
|
|
|
GL_RGBA, mIOSurface->GetDevicePixelWidth(),
|
|
|
|
mIOSurface->GetDevicePixelHeight(),
|
2011-11-15 14:36:06 -08:00
|
|
|
GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV,
|
|
|
|
mIOSurface->mIOSurfacePtr, 0);
|
|
|
|
::glBindTexture(GL_TEXTURE_RECTANGLE_ARB, 0);
|
|
|
|
|
|
|
|
// Rebind the FBO to make it live
|
|
|
|
::glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, mFBO);
|
|
|
|
|
2013-10-08 22:42:26 -07:00
|
|
|
if (static_cast<int>(mIOSurface->GetWidth()) != width ||
|
|
|
|
static_cast<int>(mIOSurface->GetHeight()) != height) {
|
2011-11-15 14:36:06 -08:00
|
|
|
width = mIOSurface->GetWidth();
|
|
|
|
height = mIOSurface->GetHeight();
|
|
|
|
SetBounds(width, height);
|
|
|
|
SetViewport(width, height);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (oldContext) {
|
|
|
|
::CGLSetCurrentContext(oldContext);
|
|
|
|
}
|
|
|
|
}
|
2010-04-20 07:52:19 -07:00
|
|
|
}
|
|
|
|
|
2011-07-12 07:31:18 -07:00
|
|
|
IOSurfaceID nsCARenderer::GetIOSurfaceID() {
|
|
|
|
if (!mIOSurface) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mIOSurface->GetIOSurfaceID();
|
|
|
|
}
|
|
|
|
|
2010-04-20 07:52:19 -07:00
|
|
|
nsresult nsCARenderer::Render(int aWidth, int aHeight,
|
2012-10-02 12:48:05 -07:00
|
|
|
double aContentsScaleFactor,
|
2010-04-20 07:52:19 -07:00
|
|
|
CGImageRef *aOutCGImage) {
|
2010-05-20 12:22:37 -07:00
|
|
|
if (!aOutCGImage && !mIOSurface) {
|
|
|
|
NS_ERROR("No target destination for rendering");
|
2010-04-20 07:52:19 -07:00
|
|
|
} else if (aOutCGImage) {
|
|
|
|
// We are expected to return a CGImageRef, we will set
|
2012-08-14 11:06:12 -07:00
|
|
|
// it to nullptr in case we fail before the image is ready.
|
|
|
|
*aOutCGImage = nullptr;
|
2010-04-20 07:52:19 -07:00
|
|
|
}
|
|
|
|
|
2012-10-02 12:48:05 -07:00
|
|
|
if (aWidth == 0 || aHeight == 0 || aContentsScaleFactor <= 0)
|
2010-04-20 07:52:19 -07:00
|
|
|
return NS_OK;
|
|
|
|
|
2013-01-23 14:19:15 -08:00
|
|
|
if (!mCARenderer || !mWrapperCALayer) {
|
2010-04-20 07:52:19 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
CARenderer* caRenderer = (CARenderer*)mCARenderer;
|
2013-01-23 14:19:15 -08:00
|
|
|
CALayer* wrapperLayer = (CALayer*)mWrapperCALayer;
|
2012-10-02 12:48:05 -07:00
|
|
|
size_t intScaleFactor = ceil(aContentsScaleFactor);
|
|
|
|
int renderer_width = caRenderer.bounds.size.width / intScaleFactor;
|
|
|
|
int renderer_height = caRenderer.bounds.size.height / intScaleFactor;
|
2010-04-20 07:52:19 -07:00
|
|
|
|
2012-10-16 12:41:21 -07:00
|
|
|
if (renderer_width != aWidth || renderer_height != aHeight ||
|
|
|
|
mContentsScaleFactor != aContentsScaleFactor) {
|
2010-04-20 07:52:19 -07:00
|
|
|
// XXX: This should be optimized to not rescale the buffer
|
|
|
|
// if we are resizing down.
|
2013-01-23 14:19:15 -08:00
|
|
|
// caLayer may be the CALayer* provided by the plugin, so we need to
|
|
|
|
// preserve it across the call to Destroy().
|
|
|
|
NSArray* sublayers = [wrapperLayer sublayers];
|
|
|
|
CALayer* caLayer = (CALayer*) [sublayers objectAtIndex:0];
|
2012-10-16 12:41:21 -07:00
|
|
|
// mIOSurface is set by AttachIOSurface(), not by SetupRenderer(). So
|
|
|
|
// since it may have been set by a prior call to AttachIOSurface(), we
|
|
|
|
// need to preserve it across the call to Destroy().
|
|
|
|
mozilla::RefPtr<MacIOSurface> ioSurface = mIOSurface;
|
2010-04-20 07:52:19 -07:00
|
|
|
Destroy();
|
2012-10-16 12:41:21 -07:00
|
|
|
mIOSurface = ioSurface;
|
2012-10-02 12:48:05 -07:00
|
|
|
if (SetupRenderer(caLayer, aWidth, aHeight, aContentsScaleFactor,
|
2011-10-20 13:32:38 -07:00
|
|
|
mAllowOfflineRenderer) != NS_OK) {
|
2010-04-20 07:52:19 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2010-08-01 16:15:25 -07:00
|
|
|
|
2010-04-20 07:52:19 -07:00
|
|
|
caRenderer = (CARenderer*)mCARenderer;
|
|
|
|
}
|
|
|
|
|
|
|
|
CGLContextObj oldContext = ::CGLGetCurrentContext();
|
|
|
|
::CGLSetCurrentContext(mOpenGLContext);
|
|
|
|
if (!mIOSurface) {
|
2011-10-13 07:36:59 -07:00
|
|
|
// If no shared IOSurface is given render to our own
|
|
|
|
// texture for readback.
|
|
|
|
::glGenTextures(1, &mFBOTexture);
|
2010-04-20 07:52:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
GLenum result = ::glGetError();
|
|
|
|
if (result != GL_NO_ERROR) {
|
2010-06-17 13:28:38 -07:00
|
|
|
NS_ERROR("Unexpected OpenGL Error");
|
2010-04-20 07:52:19 -07:00
|
|
|
Destroy();
|
|
|
|
if (oldContext)
|
|
|
|
::CGLSetCurrentContext(oldContext);
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
::glClearColor(0.0, 0.0, 0.0, 0.0);
|
|
|
|
::glClear(GL_COLOR_BUFFER_BIT);
|
|
|
|
|
2011-06-30 09:46:25 -07:00
|
|
|
[CATransaction commit];
|
2010-04-20 07:52:19 -07:00
|
|
|
double caTime = ::CACurrentMediaTime();
|
2012-08-14 11:06:12 -07:00
|
|
|
[caRenderer beginFrameAtTime:caTime timeStamp:nullptr];
|
2012-10-02 12:48:05 -07:00
|
|
|
[caRenderer addUpdateRect:CGRectMake(0,0, aWidth * intScaleFactor,
|
|
|
|
aHeight * intScaleFactor)];
|
2010-04-20 07:52:19 -07:00
|
|
|
[caRenderer render];
|
|
|
|
[caRenderer endFrame];
|
|
|
|
|
|
|
|
// Read the data back either to the IOSurface or mCGImage.
|
|
|
|
if (mIOSurface) {
|
|
|
|
::glFlush();
|
|
|
|
} else {
|
|
|
|
::glPixelStorei(GL_PACK_ALIGNMENT, 4);
|
|
|
|
::glPixelStorei(GL_PACK_ROW_LENGTH, 0);
|
|
|
|
::glPixelStorei(GL_PACK_SKIP_ROWS, 0);
|
|
|
|
::glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
|
|
|
|
|
2012-10-02 12:48:05 -07:00
|
|
|
::glReadPixels(0.0f, 0.0f, aWidth * intScaleFactor,
|
|
|
|
aHeight * intScaleFactor,
|
2010-04-20 07:52:19 -07:00
|
|
|
GL_BGRA, GL_UNSIGNED_BYTE,
|
|
|
|
mCGData);
|
|
|
|
|
|
|
|
*aOutCGImage = mCGImage;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (oldContext) {
|
|
|
|
::CGLSetCurrentContext(oldContext);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult nsCARenderer::DrawSurfaceToCGContext(CGContextRef aContext,
|
2012-07-31 08:11:57 -07:00
|
|
|
MacIOSurface *surf,
|
2010-04-20 07:52:19 -07:00
|
|
|
CGColorSpaceRef aColorSpace,
|
|
|
|
int aX, int aY,
|
2011-06-24 07:49:42 -07:00
|
|
|
size_t aWidth, size_t aHeight) {
|
2010-04-20 07:52:19 -07:00
|
|
|
surf->Lock();
|
|
|
|
size_t bytesPerRow = surf->GetBytesPerRow();
|
|
|
|
size_t ioWidth = surf->GetWidth();
|
|
|
|
size_t ioHeight = surf->GetHeight();
|
|
|
|
|
2010-06-08 21:12:06 -07:00
|
|
|
// We get rendering glitches if we use a width/height that falls
|
|
|
|
// outside of the IOSurface.
|
2011-06-24 07:49:42 -07:00
|
|
|
if (aWidth + aX > ioWidth)
|
2010-06-08 21:12:06 -07:00
|
|
|
aWidth = ioWidth - aX;
|
2011-06-24 07:49:42 -07:00
|
|
|
if (aHeight + aY > ioHeight)
|
2010-06-08 21:12:06 -07:00
|
|
|
aHeight = ioHeight - aY;
|
|
|
|
|
2013-04-04 18:20:44 -07:00
|
|
|
if (aX < 0 || static_cast<size_t>(aX) >= ioWidth ||
|
|
|
|
aY < 0 || static_cast<size_t>(aY) >= ioHeight) {
|
2012-01-10 06:47:52 -08:00
|
|
|
surf->Unlock();
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void* ioData = surf->GetBaseAddress();
|
2012-10-02 12:48:05 -07:00
|
|
|
double scaleFactor = surf->GetContentsScaleFactor();
|
|
|
|
size_t intScaleFactor = ceil(surf->GetContentsScaleFactor());
|
2012-01-10 06:47:52 -08:00
|
|
|
CGDataProviderRef dataProvider = ::CGDataProviderCreateWithData(ioData,
|
2012-10-02 12:48:05 -07:00
|
|
|
ioData, ioHeight*intScaleFactor*(bytesPerRow)*4,
|
2012-08-14 11:06:12 -07:00
|
|
|
nullptr); //No release callback
|
2012-01-10 06:47:52 -08:00
|
|
|
if (!dataProvider) {
|
|
|
|
surf->Unlock();
|
2011-06-24 07:49:42 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2012-10-02 12:48:05 -07:00
|
|
|
CGImageRef cgImage = ::CGImageCreate(ioWidth * intScaleFactor,
|
|
|
|
ioHeight * intScaleFactor, 8, 32, bytesPerRow, aColorSpace,
|
|
|
|
kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host,
|
2012-08-14 11:06:12 -07:00
|
|
|
dataProvider, nullptr, true, kCGRenderingIntentDefault);
|
2010-04-20 07:52:19 -07:00
|
|
|
::CGDataProviderRelease(dataProvider);
|
|
|
|
if (!cgImage) {
|
|
|
|
surf->Unlock();
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
CGImageRef subImage = ::CGImageCreateWithImageInRect(cgImage,
|
2012-10-02 12:48:05 -07:00
|
|
|
::CGRectMake(aX * scaleFactor,
|
|
|
|
aY * scaleFactor,
|
|
|
|
aWidth * scaleFactor,
|
|
|
|
aHeight * scaleFactor));
|
2010-04-20 07:52:19 -07:00
|
|
|
if (!subImage) {
|
|
|
|
::CGImageRelease(cgImage);
|
|
|
|
surf->Unlock();
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
::CGContextScaleCTM(aContext, 1.0f, -1.0f);
|
2011-06-24 07:49:42 -07:00
|
|
|
::CGContextDrawImage(aContext,
|
2012-10-02 12:48:05 -07:00
|
|
|
CGRectMake(aX * scaleFactor,
|
|
|
|
(-(CGFloat)aY - (CGFloat)aHeight) * scaleFactor,
|
|
|
|
aWidth * scaleFactor,
|
|
|
|
aHeight * scaleFactor),
|
2011-06-24 07:49:42 -07:00
|
|
|
subImage);
|
2010-04-20 07:52:19 -07:00
|
|
|
|
|
|
|
::CGImageRelease(subImage);
|
|
|
|
::CGImageRelease(cgImage);
|
|
|
|
surf->Unlock();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-01-23 14:19:15 -08:00
|
|
|
void nsCARenderer::DetachCALayer() {
|
|
|
|
CALayer* wrapperLayer = (CALayer*)mWrapperCALayer;
|
|
|
|
NSArray* sublayers = [wrapperLayer sublayers];
|
|
|
|
CALayer* oldLayer = (CALayer*) [sublayers objectAtIndex:0];
|
|
|
|
[oldLayer removeFromSuperlayer];
|
2011-07-12 07:31:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void nsCARenderer::AttachCALayer(void *aCALayer) {
|
2013-01-23 14:19:15 -08:00
|
|
|
CALayer* wrapperLayer = (CALayer*)mWrapperCALayer;
|
|
|
|
NSArray* sublayers = [wrapperLayer sublayers];
|
|
|
|
CALayer* oldLayer = (CALayer*) [sublayers objectAtIndex:0];
|
|
|
|
[oldLayer removeFromSuperlayer];
|
|
|
|
[wrapperLayer addSublayer:(CALayer*)aCALayer];
|
2011-07-12 07:31:18 -07:00
|
|
|
}
|
|
|
|
|
2011-06-30 09:46:25 -07:00
|
|
|
#ifdef DEBUG
|
|
|
|
|
|
|
|
int sSaveToDiskSequence = 0;
|
2012-07-31 08:11:57 -07:00
|
|
|
void nsCARenderer::SaveToDisk(MacIOSurface *surf) {
|
2011-06-30 09:46:25 -07:00
|
|
|
surf->Lock();
|
|
|
|
size_t bytesPerRow = surf->GetBytesPerRow();
|
|
|
|
size_t ioWidth = surf->GetWidth();
|
|
|
|
size_t ioHeight = surf->GetHeight();
|
|
|
|
void* ioData = surf->GetBaseAddress();
|
|
|
|
CGDataProviderRef dataProvider = ::CGDataProviderCreateWithData(ioData,
|
|
|
|
ioData, ioHeight*(bytesPerRow)*4,
|
2012-08-14 11:06:12 -07:00
|
|
|
nullptr); //No release callback
|
2011-06-30 09:46:25 -07:00
|
|
|
if (!dataProvider) {
|
|
|
|
surf->Unlock();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-01-10 06:47:52 -08:00
|
|
|
CGColorSpaceRef colorSpace = CreateSystemColorSpace();
|
2011-06-30 09:46:25 -07:00
|
|
|
CGImageRef cgImage = ::CGImageCreate(ioWidth, ioHeight, 8, 32, bytesPerRow,
|
2012-01-10 06:47:52 -08:00
|
|
|
colorSpace, kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host,
|
2012-08-14 11:06:12 -07:00
|
|
|
dataProvider, nullptr, true, kCGRenderingIntentDefault);
|
2011-06-30 09:46:25 -07:00
|
|
|
::CGDataProviderRelease(dataProvider);
|
2012-01-10 06:47:52 -08:00
|
|
|
::CGColorSpaceRelease(colorSpace);
|
2011-06-30 09:46:25 -07:00
|
|
|
if (!cgImage) {
|
|
|
|
surf->Unlock();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
char cstr[1000];
|
|
|
|
|
|
|
|
sprintf(cstr, "file:///Users/benoitgirard/debug/iosurface_%i.png", ++sSaveToDiskSequence);
|
|
|
|
|
|
|
|
CFStringRef cfStr = ::CFStringCreateWithCString(kCFAllocatorDefault, cstr, kCFStringEncodingMacRoman);
|
|
|
|
|
|
|
|
printf("Exporting: %s\n", cstr);
|
2012-08-14 11:06:12 -07:00
|
|
|
CFURLRef url = ::CFURLCreateWithString( nullptr, cfStr, nullptr);
|
2012-01-10 06:47:52 -08:00
|
|
|
::CFRelease(cfStr);
|
2011-06-30 09:46:25 -07:00
|
|
|
|
|
|
|
CFStringRef type = kUTTypePNG;
|
2011-11-15 14:36:06 -08:00
|
|
|
size_t count = 1;
|
2012-08-14 11:06:12 -07:00
|
|
|
CFDictionaryRef options = nullptr;
|
2011-06-30 09:46:25 -07:00
|
|
|
CGImageDestinationRef dest = ::CGImageDestinationCreateWithURL(url, type, count, options);
|
2012-01-10 06:47:52 -08:00
|
|
|
::CFRelease(url);
|
2011-06-30 09:46:25 -07:00
|
|
|
|
2012-08-14 11:06:12 -07:00
|
|
|
::CGImageDestinationAddImage(dest, cgImage, nullptr);
|
2011-06-30 09:46:25 -07:00
|
|
|
|
|
|
|
::CGImageDestinationFinalize(dest);
|
|
|
|
::CFRelease(dest);
|
2012-01-10 06:47:52 -08:00
|
|
|
::CGImageRelease(cgImage);
|
2011-06-30 09:46:25 -07:00
|
|
|
|
|
|
|
surf->Unlock();
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
2011-07-12 07:31:18 -07:00
|
|
|
|
2012-07-31 08:11:57 -07:00
|
|
|
CGImageRef MacIOSurface::CreateImageFromIOSurfaceContext(CGContextRef aContext) {
|
|
|
|
if (!MacIOSurfaceLib::isInit())
|
2012-08-12 03:43:47 -07:00
|
|
|
return nullptr;
|
2012-07-31 08:11:57 -07:00
|
|
|
|
|
|
|
return MacIOSurfaceLib::IOSurfaceContextCreateImage(aContext);
|
|
|
|
}
|
|
|
|
|
2012-10-02 12:48:05 -07:00
|
|
|
TemporaryRef<MacIOSurface> MacIOSurface::IOSurfaceContextGetSurface(CGContextRef aContext,
|
2013-07-17 20:24:14 -07:00
|
|
|
double aContentsScaleFactor,
|
|
|
|
bool aHasAlpha) {
|
2012-10-02 12:48:05 -07:00
|
|
|
if (!MacIOSurfaceLib::isInit() || aContentsScaleFactor <= 0)
|
2012-08-12 03:43:47 -07:00
|
|
|
return nullptr;
|
2012-07-31 08:11:57 -07:00
|
|
|
|
|
|
|
IOSurfacePtr surfaceRef = MacIOSurfaceLib::IOSurfaceContextGetSurface(aContext);
|
|
|
|
if (!surfaceRef)
|
2012-08-12 03:43:47 -07:00
|
|
|
return nullptr;
|
2012-07-31 08:11:57 -07:00
|
|
|
|
|
|
|
// Retain the IOSurface because MacIOSurface will release it
|
|
|
|
CFRetain(surfaceRef);
|
|
|
|
|
2013-07-17 20:24:14 -07:00
|
|
|
RefPtr<MacIOSurface> ioSurface = new MacIOSurface(surfaceRef, aContentsScaleFactor, aHasAlpha);
|
2012-07-31 08:11:57 -07:00
|
|
|
if (!ioSurface) {
|
|
|
|
::CFRelease(surfaceRef);
|
2012-08-12 03:43:47 -07:00
|
|
|
return nullptr;
|
2012-07-31 08:11:57 -07:00
|
|
|
}
|
|
|
|
return ioSurface.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
CGContextType GetContextType(CGContextRef ref)
|
|
|
|
{
|
|
|
|
if (!MacIOSurfaceLib::isInit() || !MacIOSurfaceLib::sCGContextGetTypePtr)
|
|
|
|
return CG_CONTEXT_TYPE_UNKNOWN;
|
|
|
|
|
|
|
|
unsigned int type = MacIOSurfaceLib::sCGContextGetTypePtr(ref);
|
|
|
|
if (type == CG_CONTEXT_TYPE_BITMAP) {
|
|
|
|
return CG_CONTEXT_TYPE_BITMAP;
|
|
|
|
} else if (type == CG_CONTEXT_TYPE_IOSURFACE) {
|
|
|
|
return CG_CONTEXT_TYPE_IOSURFACE;
|
|
|
|
} else {
|
|
|
|
return CG_CONTEXT_TYPE_UNKNOWN;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|