2017-10-28 02:10:06 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2013-11-08 22:56:52 +04: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/. */
|
|
|
|
|
2013-11-08 07:53:54 +04:00
|
|
|
#include "MacIOSurface.h"
|
2013-11-08 22:56:52 +04:00
|
|
|
#include <OpenGL/gl.h>
|
2019-08-14 14:05:07 +03:00
|
|
|
#include <OpenGL/CGLIOSurface.h>
|
2013-11-08 07:53:54 +04:00
|
|
|
#include <QuartzCore/QuartzCore.h>
|
2016-02-23 05:26:27 +03:00
|
|
|
#include "GLConsts.h"
|
2017-05-18 17:59:07 +03:00
|
|
|
#include "GLContextCGL.h"
|
2020-09-11 02:38:30 +03:00
|
|
|
#include "nsPrintfCString.h"
|
2019-07-26 11:45:20 +03:00
|
|
|
#include "mozilla/Assertions.h"
|
|
|
|
#include "mozilla/RefPtr.h"
|
2020-09-11 02:38:30 +03:00
|
|
|
#include "mozilla/gfx/Logging.h"
|
2021-03-23 04:33:54 +03:00
|
|
|
#include "mozilla/StaticPrefs_gfx.h"
|
2013-11-08 07:53:54 +04:00
|
|
|
|
|
|
|
using namespace mozilla;
|
2014-09-03 11:09:24 +04:00
|
|
|
|
2019-08-14 17:53:45 +03:00
|
|
|
MacIOSurface::MacIOSurface(CFTypeRefPtr<IOSurfaceRef> aIOSurfaceRef,
|
2021-04-08 19:42:06 +03:00
|
|
|
bool aHasAlpha, gfx::YUVColorSpace aColorSpace)
|
2019-08-14 17:53:45 +03:00
|
|
|
: mIOSurfaceRef(std::move(aIOSurfaceRef)),
|
2019-04-11 15:36:31 +03:00
|
|
|
mHasAlpha(aHasAlpha),
|
|
|
|
mColorSpace(aColorSpace) {
|
2014-09-03 11:09:24 +04:00
|
|
|
IncrementUseCount();
|
2013-11-08 07:53:54 +04:00
|
|
|
}
|
|
|
|
|
2019-08-15 07:54:57 +03:00
|
|
|
MacIOSurface::~MacIOSurface() {
|
|
|
|
MOZ_RELEASE_ASSERT(!IsLocked(), "Destroying locked surface");
|
|
|
|
DecrementUseCount();
|
|
|
|
}
|
2013-11-08 07:53:54 +04:00
|
|
|
|
2020-10-20 01:27:22 +03:00
|
|
|
void AddDictionaryInt(const CFTypeRefPtr<CFMutableDictionaryRef>& aDict,
|
|
|
|
const void* aType, uint32_t aValue) {
|
|
|
|
auto cfValue = CFTypeRefPtr<CFNumberRef>::WrapUnderCreateRule(
|
|
|
|
::CFNumberCreate(nullptr, kCFNumberSInt32Type, &aValue));
|
|
|
|
::CFDictionaryAddValue(aDict.get(), aType, cfValue.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetSizeProperties(const CFTypeRefPtr<CFMutableDictionaryRef>& aDict,
|
|
|
|
int aWidth, int aHeight, int aBytesPerPixel) {
|
|
|
|
AddDictionaryInt(aDict, kIOSurfaceWidth, aWidth);
|
|
|
|
AddDictionaryInt(aDict, kIOSurfaceHeight, aHeight);
|
|
|
|
::CFDictionaryAddValue(aDict.get(), kIOSurfaceIsGlobal, kCFBooleanTrue);
|
|
|
|
AddDictionaryInt(aDict, kIOSurfaceBytesPerElement, aBytesPerPixel);
|
|
|
|
|
|
|
|
size_t bytesPerRow =
|
|
|
|
IOSurfaceAlignProperty(kIOSurfaceBytesPerRow, aWidth * aBytesPerPixel);
|
|
|
|
AddDictionaryInt(aDict, kIOSurfaceBytesPerRow, bytesPerRow);
|
|
|
|
|
2020-10-20 04:22:09 +03:00
|
|
|
// Add a SIMD register worth of extra bytes to the end of the allocation for
|
|
|
|
// SWGL.
|
2020-10-20 01:27:22 +03:00
|
|
|
size_t totalBytes =
|
2020-10-20 04:22:09 +03:00
|
|
|
IOSurfaceAlignProperty(kIOSurfaceAllocSize, aHeight * bytesPerRow + 16);
|
2020-10-20 01:27:22 +03:00
|
|
|
AddDictionaryInt(aDict, kIOSurfaceAllocSize, totalBytes);
|
|
|
|
}
|
|
|
|
|
2019-07-26 11:45:20 +03:00
|
|
|
/* static */
|
2021-04-08 19:42:06 +03:00
|
|
|
already_AddRefed<MacIOSurface> MacIOSurface::CreateIOSurface(int aWidth,
|
|
|
|
int aHeight,
|
|
|
|
bool aHasAlpha) {
|
2020-10-20 01:27:22 +03:00
|
|
|
auto props = CFTypeRefPtr<CFMutableDictionaryRef>::WrapUnderCreateRule(
|
|
|
|
::CFDictionaryCreateMutable(kCFAllocatorDefault, 4,
|
|
|
|
&kCFTypeDictionaryKeyCallBacks,
|
|
|
|
&kCFTypeDictionaryValueCallBacks));
|
2013-11-08 07:53:54 +04:00
|
|
|
if (!props) return nullptr;
|
|
|
|
|
2014-07-12 10:08:54 +04:00
|
|
|
MOZ_ASSERT((size_t)aWidth <= GetMaxWidth());
|
|
|
|
MOZ_ASSERT((size_t)aHeight <= GetMaxHeight());
|
|
|
|
|
2013-11-08 07:53:54 +04:00
|
|
|
int32_t bytesPerElem = 4;
|
2020-10-20 01:27:22 +03:00
|
|
|
SetSizeProperties(props, aWidth, aHeight, bytesPerElem);
|
2013-11-08 07:53:54 +04:00
|
|
|
|
2020-10-20 01:27:23 +03:00
|
|
|
AddDictionaryInt(props, kIOSurfacePixelFormat,
|
|
|
|
(uint32_t)kCVPixelFormatType_32BGRA);
|
|
|
|
|
2019-08-14 17:53:45 +03:00
|
|
|
CFTypeRefPtr<IOSurfaceRef> surfaceRef =
|
2020-10-20 01:27:22 +03:00
|
|
|
CFTypeRefPtr<IOSurfaceRef>::WrapUnderCreateRule(
|
|
|
|
::IOSurfaceCreate(props.get()));
|
2013-11-08 07:53:54 +04:00
|
|
|
|
2021-03-23 04:33:54 +03:00
|
|
|
if (StaticPrefs::gfx_color_management_native_srgb()) {
|
|
|
|
IOSurfaceSetValue(surfaceRef.get(), CFSTR("IOSurfaceColorSpace"),
|
|
|
|
kCGColorSpaceSRGB);
|
|
|
|
}
|
|
|
|
|
2019-08-14 17:53:45 +03:00
|
|
|
if (!surfaceRef) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2013-11-08 07:53:54 +04:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<MacIOSurface> ioSurface =
|
2021-04-08 19:42:06 +03:00
|
|
|
new MacIOSurface(std::move(surfaceRef), aHasAlpha);
|
2014-09-03 11:09:24 +04:00
|
|
|
|
2013-11-08 07:53:54 +04:00
|
|
|
return ioSurface.forget();
|
|
|
|
}
|
|
|
|
|
2020-08-04 04:18:14 +03:00
|
|
|
size_t CreatePlaneDictionary(CFTypeRefPtr<CFMutableDictionaryRef>& aDict,
|
|
|
|
const gfx::IntSize& aSize, size_t aOffset,
|
|
|
|
size_t aBytesPerPixel) {
|
|
|
|
size_t bytesPerRow = IOSurfaceAlignProperty(kIOSurfaceBytesPerRow,
|
|
|
|
aSize.width * aBytesPerPixel);
|
2020-10-20 04:22:09 +03:00
|
|
|
// Add a SIMD register worth of extra bytes to the end of the allocation for
|
|
|
|
// SWGL.
|
|
|
|
size_t totalBytes = IOSurfaceAlignProperty(kIOSurfaceAllocSize,
|
|
|
|
aSize.height * bytesPerRow + 16);
|
2020-08-04 04:18:14 +03:00
|
|
|
|
|
|
|
aDict = CFTypeRefPtr<CFMutableDictionaryRef>::WrapUnderCreateRule(
|
|
|
|
::CFDictionaryCreateMutable(kCFAllocatorDefault, 4,
|
|
|
|
&kCFTypeDictionaryKeyCallBacks,
|
|
|
|
&kCFTypeDictionaryValueCallBacks));
|
|
|
|
|
|
|
|
AddDictionaryInt(aDict, kIOSurfacePlaneWidth, aSize.width);
|
|
|
|
AddDictionaryInt(aDict, kIOSurfacePlaneHeight, aSize.height);
|
|
|
|
AddDictionaryInt(aDict, kIOSurfacePlaneBytesPerRow, bytesPerRow);
|
|
|
|
AddDictionaryInt(aDict, kIOSurfacePlaneOffset, aOffset);
|
|
|
|
AddDictionaryInt(aDict, kIOSurfacePlaneSize, totalBytes);
|
|
|
|
AddDictionaryInt(aDict, kIOSurfaceBytesPerElement, aBytesPerPixel);
|
|
|
|
|
|
|
|
return totalBytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */
|
|
|
|
already_AddRefed<MacIOSurface> MacIOSurface::CreateNV12Surface(
|
|
|
|
const IntSize& aYSize, const IntSize& aCbCrSize, YUVColorSpace aColorSpace,
|
|
|
|
ColorRange aColorRange) {
|
|
|
|
MOZ_ASSERT(aColorSpace == YUVColorSpace::BT601 ||
|
|
|
|
aColorSpace == YUVColorSpace::BT709);
|
|
|
|
MOZ_ASSERT(aColorRange == ColorRange::LIMITED ||
|
|
|
|
aColorRange == ColorRange::FULL);
|
|
|
|
|
|
|
|
auto props = CFTypeRefPtr<CFMutableDictionaryRef>::WrapUnderCreateRule(
|
|
|
|
::CFDictionaryCreateMutable(kCFAllocatorDefault, 4,
|
|
|
|
&kCFTypeDictionaryKeyCallBacks,
|
|
|
|
&kCFTypeDictionaryValueCallBacks));
|
|
|
|
if (!props) return nullptr;
|
|
|
|
|
|
|
|
MOZ_ASSERT((size_t)aYSize.width <= GetMaxWidth());
|
|
|
|
MOZ_ASSERT((size_t)aYSize.height <= GetMaxHeight());
|
|
|
|
|
|
|
|
AddDictionaryInt(props, kIOSurfaceWidth, aYSize.width);
|
|
|
|
AddDictionaryInt(props, kIOSurfaceHeight, aYSize.height);
|
|
|
|
::CFDictionaryAddValue(props.get(), kIOSurfaceIsGlobal, kCFBooleanTrue);
|
|
|
|
|
|
|
|
if (aColorRange == ColorRange::LIMITED) {
|
|
|
|
AddDictionaryInt(props, kIOSurfacePixelFormat,
|
|
|
|
(uint32_t)kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange);
|
|
|
|
} else {
|
|
|
|
AddDictionaryInt(props, kIOSurfacePixelFormat,
|
|
|
|
(uint32_t)kCVPixelFormatType_420YpCbCr8BiPlanarFullRange);
|
|
|
|
}
|
|
|
|
|
|
|
|
CFTypeRefPtr<CFMutableDictionaryRef> planeProps[2];
|
|
|
|
size_t planeTotalBytes = CreatePlaneDictionary(planeProps[0], aYSize, 0, 1);
|
|
|
|
planeTotalBytes +=
|
|
|
|
CreatePlaneDictionary(planeProps[1], aCbCrSize, planeTotalBytes, 2);
|
|
|
|
|
|
|
|
AddDictionaryInt(props, kIOSurfaceAllocSize, planeTotalBytes);
|
|
|
|
|
|
|
|
auto array = CFTypeRefPtr<CFArrayRef>::WrapUnderCreateRule(
|
|
|
|
CFArrayCreate(kCFAllocatorDefault, (const void**)planeProps, 2,
|
|
|
|
&kCFTypeArrayCallBacks));
|
|
|
|
::CFDictionaryAddValue(props.get(), kIOSurfacePlaneInfo, array.get());
|
|
|
|
|
|
|
|
CFTypeRefPtr<IOSurfaceRef> surfaceRef =
|
|
|
|
CFTypeRefPtr<IOSurfaceRef>::WrapUnderCreateRule(
|
|
|
|
::IOSurfaceCreate(props.get()));
|
|
|
|
|
|
|
|
if (!surfaceRef) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2020-08-04 04:18:31 +03:00
|
|
|
// Setup the correct YCbCr conversion matrix on the IOSurface, in case we pass
|
|
|
|
// this directly to CoreAnimation.
|
2020-08-04 04:18:14 +03:00
|
|
|
if (aColorSpace == YUVColorSpace::BT601) {
|
2020-08-04 04:18:31 +03:00
|
|
|
IOSurfaceSetValue(surfaceRef.get(), CFSTR("IOSurfaceYCbCrMatrix"),
|
|
|
|
CFSTR("ITU_R_601_4"));
|
2020-08-04 04:18:14 +03:00
|
|
|
} else {
|
2020-08-04 04:18:31 +03:00
|
|
|
IOSurfaceSetValue(surfaceRef.get(), CFSTR("IOSurfaceYCbCrMatrix"),
|
|
|
|
CFSTR("ITU_R_709_2"));
|
2020-08-04 04:18:14 +03:00
|
|
|
}
|
2020-08-04 04:18:31 +03:00
|
|
|
// Override the color space to be the same as the main display, so that
|
|
|
|
// CoreAnimation won't try to do any color correction (from the IOSurface
|
|
|
|
// space, to the display). In the future we may want to try specifying this
|
|
|
|
// correctly, but probably only once we do the same for videos drawn through
|
|
|
|
// our gfx code.
|
|
|
|
auto colorSpace = CFTypeRefPtr<CGColorSpaceRef>::WrapUnderCreateRule(
|
|
|
|
CGDisplayCopyColorSpace(CGMainDisplayID()));
|
2020-08-04 04:18:14 +03:00
|
|
|
auto colorData = CFTypeRefPtr<CFDataRef>::WrapUnderCreateRule(
|
|
|
|
CGColorSpaceCopyICCProfile(colorSpace.get()));
|
|
|
|
IOSurfaceSetValue(surfaceRef.get(), CFSTR("IOSurfaceColorSpace"),
|
|
|
|
colorData.get());
|
|
|
|
|
|
|
|
RefPtr<MacIOSurface> ioSurface =
|
2021-04-08 19:42:06 +03:00
|
|
|
new MacIOSurface(std::move(surfaceRef), false, aColorSpace);
|
2020-08-04 04:18:14 +03:00
|
|
|
|
|
|
|
return ioSurface.forget();
|
|
|
|
}
|
|
|
|
|
2020-08-07 00:27:46 +03:00
|
|
|
/* static */
|
|
|
|
already_AddRefed<MacIOSurface> MacIOSurface::CreateYUV422Surface(
|
|
|
|
const IntSize& aSize, YUVColorSpace aColorSpace, ColorRange aColorRange) {
|
|
|
|
MOZ_ASSERT(aColorSpace == YUVColorSpace::BT601 ||
|
|
|
|
aColorSpace == YUVColorSpace::BT709);
|
|
|
|
MOZ_ASSERT(aColorRange == ColorRange::LIMITED ||
|
|
|
|
aColorRange == ColorRange::FULL);
|
|
|
|
|
|
|
|
auto props = CFTypeRefPtr<CFMutableDictionaryRef>::WrapUnderCreateRule(
|
|
|
|
::CFDictionaryCreateMutable(kCFAllocatorDefault, 4,
|
|
|
|
&kCFTypeDictionaryKeyCallBacks,
|
|
|
|
&kCFTypeDictionaryValueCallBacks));
|
|
|
|
if (!props) return nullptr;
|
|
|
|
|
|
|
|
MOZ_ASSERT((size_t)aSize.width <= GetMaxWidth());
|
|
|
|
MOZ_ASSERT((size_t)aSize.height <= GetMaxHeight());
|
|
|
|
|
2020-10-20 01:27:22 +03:00
|
|
|
SetSizeProperties(props, aSize.width, aSize.height, 2);
|
2020-08-07 00:27:46 +03:00
|
|
|
|
|
|
|
if (aColorRange == ColorRange::LIMITED) {
|
|
|
|
AddDictionaryInt(props, kIOSurfacePixelFormat,
|
|
|
|
(uint32_t)kCVPixelFormatType_422YpCbCr8_yuvs);
|
|
|
|
} else {
|
|
|
|
AddDictionaryInt(props, kIOSurfacePixelFormat,
|
|
|
|
(uint32_t)kCVPixelFormatType_422YpCbCr8FullRange);
|
|
|
|
}
|
|
|
|
|
|
|
|
CFTypeRefPtr<IOSurfaceRef> surfaceRef =
|
|
|
|
CFTypeRefPtr<IOSurfaceRef>::WrapUnderCreateRule(
|
|
|
|
::IOSurfaceCreate(props.get()));
|
|
|
|
|
|
|
|
if (!surfaceRef) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup the correct YCbCr conversion matrix on the IOSurface, in case we pass
|
|
|
|
// this directly to CoreAnimation.
|
|
|
|
if (aColorSpace == YUVColorSpace::BT601) {
|
|
|
|
IOSurfaceSetValue(surfaceRef.get(), CFSTR("IOSurfaceYCbCrMatrix"),
|
|
|
|
CFSTR("ITU_R_601_4"));
|
|
|
|
} else {
|
|
|
|
IOSurfaceSetValue(surfaceRef.get(), CFSTR("IOSurfaceYCbCrMatrix"),
|
|
|
|
CFSTR("ITU_R_709_2"));
|
|
|
|
}
|
|
|
|
// Override the color space to be the same as the main display, so that
|
|
|
|
// CoreAnimation won't try to do any color correction (from the IOSurface
|
|
|
|
// space, to the display). In the future we may want to try specifying this
|
|
|
|
// correctly, but probably only once we do the same for videos drawn through
|
|
|
|
// our gfx code.
|
|
|
|
auto colorSpace = CFTypeRefPtr<CGColorSpaceRef>::WrapUnderCreateRule(
|
|
|
|
CGDisplayCopyColorSpace(CGMainDisplayID()));
|
|
|
|
auto colorData = CFTypeRefPtr<CFDataRef>::WrapUnderCreateRule(
|
|
|
|
CGColorSpaceCopyICCProfile(colorSpace.get()));
|
|
|
|
IOSurfaceSetValue(surfaceRef.get(), CFSTR("IOSurfaceColorSpace"),
|
|
|
|
colorData.get());
|
|
|
|
|
|
|
|
RefPtr<MacIOSurface> ioSurface =
|
2021-04-08 19:42:06 +03:00
|
|
|
new MacIOSurface(std::move(surfaceRef), false, aColorSpace);
|
2020-08-07 00:27:46 +03:00
|
|
|
|
|
|
|
return ioSurface.forget();
|
|
|
|
}
|
|
|
|
|
2019-07-26 11:45:20 +03:00
|
|
|
/* static */
|
2015-06-17 17:00:52 +03:00
|
|
|
already_AddRefed<MacIOSurface> MacIOSurface::LookupSurface(
|
2021-04-08 19:42:06 +03:00
|
|
|
IOSurfaceID aIOSurfaceID, bool aHasAlpha, gfx::YUVColorSpace aColorSpace) {
|
2019-08-14 17:53:45 +03:00
|
|
|
CFTypeRefPtr<IOSurfaceRef> surfaceRef =
|
|
|
|
CFTypeRefPtr<IOSurfaceRef>::WrapUnderCreateRule(
|
|
|
|
::IOSurfaceLookup(aIOSurfaceID));
|
2013-11-08 07:53:54 +04:00
|
|
|
if (!surfaceRef) return nullptr;
|
|
|
|
|
2021-04-08 19:42:06 +03:00
|
|
|
RefPtr<MacIOSurface> ioSurface =
|
|
|
|
new MacIOSurface(std::move(surfaceRef), aHasAlpha, aColorSpace);
|
2014-09-03 11:09:24 +04:00
|
|
|
|
2013-11-08 07:53:54 +04:00
|
|
|
return ioSurface.forget();
|
|
|
|
}
|
|
|
|
|
2019-07-26 11:45:20 +03:00
|
|
|
IOSurfaceID MacIOSurface::GetIOSurfaceID() const {
|
2019-08-14 17:53:45 +03:00
|
|
|
return ::IOSurfaceGetID(mIOSurfaceRef.get());
|
2013-11-08 07:53:54 +04:00
|
|
|
}
|
|
|
|
|
2019-07-26 11:45:20 +03:00
|
|
|
void* MacIOSurface::GetBaseAddress() const {
|
2019-08-14 17:53:45 +03:00
|
|
|
return ::IOSurfaceGetBaseAddress(mIOSurfaceRef.get());
|
2013-11-08 07:53:54 +04:00
|
|
|
}
|
|
|
|
|
2019-07-26 11:45:20 +03:00
|
|
|
void* MacIOSurface::GetBaseAddressOfPlane(size_t aPlaneIndex) const {
|
2019-08-14 17:53:45 +03:00
|
|
|
return ::IOSurfaceGetBaseAddressOfPlane(mIOSurfaceRef.get(), aPlaneIndex);
|
2014-09-03 11:09:24 +04:00
|
|
|
}
|
|
|
|
|
2019-07-26 11:45:20 +03:00
|
|
|
size_t MacIOSurface::GetWidth(size_t plane) const {
|
2021-04-08 19:42:06 +03:00
|
|
|
return GetDevicePixelWidth(plane);
|
2013-11-08 07:53:54 +04:00
|
|
|
}
|
|
|
|
|
2019-07-26 11:45:20 +03:00
|
|
|
size_t MacIOSurface::GetHeight(size_t plane) const {
|
2021-04-08 19:42:06 +03:00
|
|
|
return GetDevicePixelHeight(plane);
|
2013-11-08 07:53:54 +04:00
|
|
|
}
|
|
|
|
|
2019-07-26 11:45:20 +03:00
|
|
|
size_t MacIOSurface::GetPlaneCount() const {
|
2019-08-14 17:53:45 +03:00
|
|
|
return ::IOSurfaceGetPlaneCount(mIOSurfaceRef.get());
|
2014-09-03 11:09:24 +04:00
|
|
|
}
|
|
|
|
|
2019-02-26 01:07:19 +03:00
|
|
|
/*static*/
|
|
|
|
size_t MacIOSurface::GetMaxWidth() {
|
2019-08-14 14:06:22 +03:00
|
|
|
return ::IOSurfaceGetPropertyMaximum(kIOSurfaceWidth);
|
2014-07-12 10:08:54 +04:00
|
|
|
}
|
|
|
|
|
2019-02-26 01:07:19 +03:00
|
|
|
/*static*/
|
|
|
|
size_t MacIOSurface::GetMaxHeight() {
|
2019-08-14 14:06:22 +03:00
|
|
|
return ::IOSurfaceGetPropertyMaximum(kIOSurfaceHeight);
|
2014-07-12 10:08:54 +04:00
|
|
|
}
|
|
|
|
|
2019-07-26 11:45:20 +03:00
|
|
|
size_t MacIOSurface::GetDevicePixelWidth(size_t plane) const {
|
2019-08-14 17:53:45 +03:00
|
|
|
return ::IOSurfaceGetWidthOfPlane(mIOSurfaceRef.get(), plane);
|
2015-08-04 00:57:19 +03:00
|
|
|
}
|
|
|
|
|
2019-07-26 11:45:20 +03:00
|
|
|
size_t MacIOSurface::GetDevicePixelHeight(size_t plane) const {
|
2019-08-14 17:53:45 +03:00
|
|
|
return ::IOSurfaceGetHeightOfPlane(mIOSurfaceRef.get(), plane);
|
2013-11-08 07:53:54 +04:00
|
|
|
}
|
|
|
|
|
2019-07-26 11:45:20 +03:00
|
|
|
size_t MacIOSurface::GetBytesPerRow(size_t plane) const {
|
2019-08-14 17:53:45 +03:00
|
|
|
return ::IOSurfaceGetBytesPerRowOfPlane(mIOSurfaceRef.get(), plane);
|
2013-11-08 07:53:54 +04:00
|
|
|
}
|
|
|
|
|
2021-02-09 07:38:24 +03:00
|
|
|
size_t MacIOSurface::GetAllocSize() const {
|
|
|
|
return ::IOSurfaceGetAllocSize(mIOSurfaceRef.get());
|
|
|
|
}
|
|
|
|
|
2019-07-26 11:45:20 +03:00
|
|
|
OSType MacIOSurface::GetPixelFormat() const {
|
2019-08-14 17:53:45 +03:00
|
|
|
return ::IOSurfaceGetPixelFormat(mIOSurfaceRef.get());
|
2013-11-08 07:53:54 +04:00
|
|
|
}
|
|
|
|
|
2014-09-03 11:09:24 +04:00
|
|
|
void MacIOSurface::IncrementUseCount() {
|
2019-08-14 17:53:45 +03:00
|
|
|
::IOSurfaceIncrementUseCount(mIOSurfaceRef.get());
|
2014-09-03 11:09:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void MacIOSurface::DecrementUseCount() {
|
2019-08-14 17:53:45 +03:00
|
|
|
::IOSurfaceDecrementUseCount(mIOSurfaceRef.get());
|
2014-09-03 11:09:24 +04:00
|
|
|
}
|
|
|
|
|
2016-06-27 18:25:13 +03:00
|
|
|
void MacIOSurface::Lock(bool aReadOnly) {
|
2019-08-15 07:54:57 +03:00
|
|
|
MOZ_RELEASE_ASSERT(!mIsLocked, "double MacIOSurface lock");
|
2019-08-14 17:53:45 +03:00
|
|
|
::IOSurfaceLock(mIOSurfaceRef.get(), aReadOnly ? kIOSurfaceLockReadOnly : 0,
|
2019-08-14 17:50:25 +03:00
|
|
|
nullptr);
|
2019-08-15 07:54:57 +03:00
|
|
|
mIsLocked = true;
|
2013-11-08 07:53:54 +04:00
|
|
|
}
|
|
|
|
|
2016-06-27 18:25:13 +03:00
|
|
|
void MacIOSurface::Unlock(bool aReadOnly) {
|
2019-08-15 07:54:57 +03:00
|
|
|
MOZ_RELEASE_ASSERT(mIsLocked, "MacIOSurface unlock without being locked");
|
2019-08-14 17:53:45 +03:00
|
|
|
::IOSurfaceUnlock(mIOSurfaceRef.get(), aReadOnly ? kIOSurfaceLockReadOnly : 0,
|
2019-08-14 17:50:25 +03:00
|
|
|
nullptr);
|
2019-08-15 07:54:57 +03:00
|
|
|
mIsLocked = false;
|
2013-11-08 07:53:54 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
using mozilla::gfx::IntSize;
|
|
|
|
using mozilla::gfx::SourceSurface;
|
|
|
|
using mozilla::gfx::SurfaceFormat;
|
|
|
|
|
2019-02-17 05:08:30 +03:00
|
|
|
static void MacIOSurfaceBufferDeallocator(void* aClosure) {
|
2016-05-09 01:33:00 +03:00
|
|
|
MOZ_ASSERT(aClosure);
|
|
|
|
|
|
|
|
delete[] static_cast<unsigned char*>(aClosure);
|
|
|
|
}
|
|
|
|
|
2015-06-17 17:00:52 +03:00
|
|
|
already_AddRefed<SourceSurface> MacIOSurface::GetAsSurface() {
|
2013-11-08 07:53:54 +04:00
|
|
|
Lock();
|
|
|
|
size_t bytesPerRow = GetBytesPerRow();
|
|
|
|
size_t ioWidth = GetDevicePixelWidth();
|
|
|
|
size_t ioHeight = GetDevicePixelHeight();
|
|
|
|
|
|
|
|
unsigned char* ioData = (unsigned char*)GetBaseAddress();
|
2016-11-15 12:00:08 +03:00
|
|
|
auto* dataCpy =
|
2016-05-09 01:33:00 +03:00
|
|
|
new unsigned char[bytesPerRow * ioHeight / sizeof(unsigned char)];
|
2013-11-08 07:53:54 +04:00
|
|
|
for (size_t i = 0; i < ioHeight; i++) {
|
|
|
|
memcpy(dataCpy + i * bytesPerRow, ioData + i * bytesPerRow, ioWidth * 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
Unlock();
|
|
|
|
|
2014-01-10 23:06:16 +04:00
|
|
|
SurfaceFormat format = HasAlpha() ? mozilla::gfx::SurfaceFormat::B8G8R8A8
|
|
|
|
: mozilla::gfx::SurfaceFormat::B8G8R8X8;
|
2013-11-08 07:53:54 +04:00
|
|
|
|
2016-05-09 01:33:00 +03:00
|
|
|
RefPtr<mozilla::gfx::DataSourceSurface> surf =
|
|
|
|
mozilla::gfx::Factory::CreateWrappingDataSourceSurface(
|
|
|
|
dataCpy, bytesPerRow, IntSize(ioWidth, ioHeight), format,
|
|
|
|
&MacIOSurfaceBufferDeallocator, static_cast<void*>(dataCpy));
|
2013-11-08 07:53:54 +04:00
|
|
|
|
|
|
|
return surf.forget();
|
|
|
|
}
|
|
|
|
|
2019-08-15 07:54:57 +03:00
|
|
|
already_AddRefed<mozilla::gfx::DrawTarget> MacIOSurface::GetAsDrawTargetLocked(
|
|
|
|
mozilla::gfx::BackendType aBackendType) {
|
|
|
|
MOZ_RELEASE_ASSERT(
|
|
|
|
IsLocked(),
|
|
|
|
"Only call GetAsDrawTargetLocked while the surface is locked.");
|
|
|
|
|
|
|
|
size_t bytesPerRow = GetBytesPerRow();
|
|
|
|
size_t ioWidth = GetDevicePixelWidth();
|
|
|
|
size_t ioHeight = GetDevicePixelHeight();
|
|
|
|
unsigned char* ioData = (unsigned char*)GetBaseAddress();
|
|
|
|
SurfaceFormat format = GetFormat();
|
|
|
|
return mozilla::gfx::Factory::CreateDrawTargetForData(
|
|
|
|
aBackendType, ioData, IntSize(ioWidth, ioHeight), bytesPerRow, format);
|
|
|
|
}
|
|
|
|
|
2019-07-26 11:45:20 +03:00
|
|
|
SurfaceFormat MacIOSurface::GetFormat() const {
|
2019-08-15 06:33:58 +03:00
|
|
|
switch (GetPixelFormat()) {
|
|
|
|
case kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange:
|
|
|
|
case kCVPixelFormatType_420YpCbCr8BiPlanarFullRange:
|
|
|
|
return SurfaceFormat::NV12;
|
2020-08-07 00:27:46 +03:00
|
|
|
case kCVPixelFormatType_422YpCbCr8_yuvs:
|
|
|
|
case kCVPixelFormatType_422YpCbCr8FullRange:
|
2019-08-15 06:33:58 +03:00
|
|
|
return SurfaceFormat::YUV422;
|
|
|
|
case kCVPixelFormatType_32BGRA:
|
|
|
|
return HasAlpha() ? SurfaceFormat::B8G8R8A8 : SurfaceFormat::B8G8R8X8;
|
|
|
|
default:
|
2020-10-20 01:27:23 +03:00
|
|
|
MOZ_ASSERT_UNREACHABLE("Unknown format");
|
|
|
|
return SurfaceFormat::B8G8R8A8;
|
2016-02-23 05:26:27 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-26 11:45:20 +03:00
|
|
|
SurfaceFormat MacIOSurface::GetReadFormat() const {
|
2019-08-15 06:33:58 +03:00
|
|
|
SurfaceFormat format = GetFormat();
|
|
|
|
if (format == SurfaceFormat::YUV422) {
|
2016-02-23 05:26:27 +03:00
|
|
|
return SurfaceFormat::R8G8B8X8;
|
2015-08-04 00:57:19 +03:00
|
|
|
}
|
2019-08-15 06:33:58 +03:00
|
|
|
return format;
|
2015-08-04 00:57:19 +03:00
|
|
|
}
|
|
|
|
|
2017-07-21 05:18:50 +03:00
|
|
|
CGLError MacIOSurface::CGLTexImageIOSurface2D(CGLContextObj ctx, GLenum target,
|
|
|
|
GLenum internalFormat,
|
|
|
|
GLsizei width, GLsizei height,
|
|
|
|
GLenum format, GLenum type,
|
|
|
|
GLuint plane) const {
|
2019-08-14 14:05:07 +03:00
|
|
|
return ::CGLTexImageIOSurface2D(ctx, target, internalFormat, width, height,
|
2019-08-14 17:53:45 +03:00
|
|
|
format, type, mIOSurfaceRef.get(), plane);
|
2017-07-21 05:18:50 +03:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-05-18 17:59:07 +03:00
|
|
|
CGLError MacIOSurface::CGLTexImageIOSurface2D(
|
|
|
|
mozilla::gl::GLContext* aGL, CGLContextObj ctx, size_t plane,
|
|
|
|
mozilla::gfx::SurfaceFormat* aOutReadFormat) {
|
2015-08-04 00:57:19 +03:00
|
|
|
MOZ_ASSERT(plane >= 0);
|
2017-05-18 17:59:07 +03:00
|
|
|
bool isCompatibilityProfile = aGL->IsCompatibilityProfile();
|
2015-08-04 00:57:19 +03:00
|
|
|
OSType pixelFormat = GetPixelFormat();
|
|
|
|
|
|
|
|
GLenum internalFormat;
|
|
|
|
GLenum format;
|
|
|
|
GLenum type;
|
2019-07-26 11:45:35 +03:00
|
|
|
if (pixelFormat == kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange ||
|
|
|
|
pixelFormat == kCVPixelFormatType_420YpCbCr8BiPlanarFullRange) {
|
2015-08-04 00:57:19 +03:00
|
|
|
MOZ_ASSERT(GetPlaneCount() == 2);
|
|
|
|
MOZ_ASSERT(plane < 2);
|
|
|
|
|
2017-05-18 17:59:07 +03:00
|
|
|
// The LOCAL_GL_LUMINANCE and LOCAL_GL_LUMINANCE_ALPHA are the deprecated
|
|
|
|
// format. So, use LOCAL_GL_RED and LOCAL_GL_RB if we use core profile.
|
|
|
|
// https://www.khronos.org/opengl/wiki/Image_Format#Legacy_Image_Formats
|
2015-08-04 00:57:19 +03:00
|
|
|
if (plane == 0) {
|
2017-05-18 17:59:07 +03:00
|
|
|
internalFormat = format =
|
|
|
|
(isCompatibilityProfile) ? (LOCAL_GL_LUMINANCE) : (LOCAL_GL_RED);
|
2015-08-04 00:57:19 +03:00
|
|
|
} else {
|
2017-05-18 17:59:07 +03:00
|
|
|
internalFormat = format =
|
|
|
|
(isCompatibilityProfile) ? (LOCAL_GL_LUMINANCE_ALPHA) : (LOCAL_GL_RG);
|
|
|
|
}
|
|
|
|
type = LOCAL_GL_UNSIGNED_BYTE;
|
|
|
|
if (aOutReadFormat) {
|
|
|
|
*aOutReadFormat = mozilla::gfx::SurfaceFormat::NV12;
|
2015-08-04 00:57:19 +03:00
|
|
|
}
|
2020-08-07 00:27:46 +03:00
|
|
|
} else if (pixelFormat == kCVPixelFormatType_422YpCbCr8_yuvs ||
|
|
|
|
pixelFormat == kCVPixelFormatType_422YpCbCr8FullRange) {
|
2016-02-23 05:26:27 +03:00
|
|
|
MOZ_ASSERT(plane == 0);
|
2017-05-18 17:59:07 +03:00
|
|
|
// The YCBCR_422_APPLE ext is only available in compatibility profile. So,
|
|
|
|
// we should use RGB_422_APPLE for core profile. The difference between
|
|
|
|
// YCBCR_422_APPLE and RGB_422_APPLE is that the YCBCR_422_APPLE converts
|
|
|
|
// the YCbCr value to RGB with REC 601 conversion. But the RGB_422_APPLE
|
|
|
|
// doesn't contain color conversion. You should do the color conversion by
|
|
|
|
// yourself for RGB_422_APPLE.
|
|
|
|
//
|
|
|
|
// https://www.khronos.org/registry/OpenGL/extensions/APPLE/APPLE_ycbcr_422.txt
|
|
|
|
// https://www.khronos.org/registry/OpenGL/extensions/APPLE/APPLE_rgb_422.txt
|
|
|
|
if (isCompatibilityProfile) {
|
|
|
|
format = LOCAL_GL_YCBCR_422_APPLE;
|
|
|
|
if (aOutReadFormat) {
|
|
|
|
*aOutReadFormat = mozilla::gfx::SurfaceFormat::R8G8B8X8;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
format = LOCAL_GL_RGB_422_APPLE;
|
|
|
|
if (aOutReadFormat) {
|
|
|
|
*aOutReadFormat = mozilla::gfx::SurfaceFormat::YUV422;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
internalFormat = LOCAL_GL_RGB;
|
2020-08-07 00:27:46 +03:00
|
|
|
type = LOCAL_GL_UNSIGNED_SHORT_8_8_REV_APPLE;
|
2015-08-04 00:57:19 +03:00
|
|
|
} else {
|
|
|
|
MOZ_ASSERT(plane == 0);
|
|
|
|
|
2017-05-18 17:59:07 +03:00
|
|
|
internalFormat = HasAlpha() ? LOCAL_GL_RGBA : LOCAL_GL_RGB;
|
|
|
|
format = LOCAL_GL_BGRA;
|
|
|
|
type = LOCAL_GL_UNSIGNED_INT_8_8_8_8_REV;
|
|
|
|
if (aOutReadFormat) {
|
|
|
|
*aOutReadFormat = HasAlpha() ? mozilla::gfx::SurfaceFormat::R8G8B8A8
|
|
|
|
: mozilla::gfx::SurfaceFormat::R8G8B8X8;
|
|
|
|
}
|
2015-08-04 00:57:19 +03:00
|
|
|
}
|
2017-05-18 17:59:07 +03:00
|
|
|
|
2020-09-11 02:38:30 +03:00
|
|
|
auto err =
|
|
|
|
CGLTexImageIOSurface2D(ctx, LOCAL_GL_TEXTURE_RECTANGLE_ARB,
|
|
|
|
internalFormat, GetDevicePixelWidth(plane),
|
|
|
|
GetDevicePixelHeight(plane), format, type, plane);
|
|
|
|
if (err) {
|
|
|
|
const auto formatChars = (const char*)&pixelFormat;
|
|
|
|
const char formatStr[] = {formatChars[3], formatChars[2], formatChars[1],
|
|
|
|
formatChars[0], 0};
|
|
|
|
const nsPrintfCString errStr(
|
|
|
|
"CGLTexImageIOSurface2D(context, target, 0x%04x,"
|
|
|
|
" %u, %u, 0x%04x, 0x%04x, iosurfPtr, %u) -> %i",
|
|
|
|
internalFormat, uint32_t(GetDevicePixelWidth(plane)),
|
|
|
|
uint32_t(GetDevicePixelHeight(plane)), format, type,
|
|
|
|
(unsigned int)plane, err);
|
|
|
|
gfxCriticalError() << errStr.get() << " (iosurf format: " << formatStr
|
|
|
|
<< ")";
|
|
|
|
}
|
|
|
|
return err;
|
2013-11-08 07:53:54 +04:00
|
|
|
}
|