/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ /* vim: set ts=8 sts=2 et sw=2 tw=80: */ /* 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/. */
#include"MacIOSurface.h" #ifdef XP_MACOSX # include <OpenGL/gl.h> # include <OpenGL/CGLIOSurface.h> #endif #include <QuartzCore/QuartzCore.h> #include"GLConsts.h" #ifdef XP_MACOSX # include "GLContextCGL.h" #else # include "GLContextEAGL.h" #endif #include"gfxMacUtils.h" #include"nsPrintfCString.h" #include"mozilla/Assertions.h" #include"mozilla/RefPtr.h" #include"mozilla/gfx/Logging.h" #include"mozilla/StaticPrefs_gfx.h"
// Add a SIMD register worth of extra bytes to the end of the allocation for // SWGL.
size_t totalBytes =
IOSurfaceAlignProperty(kIOSurfaceAllocSize, aHeight * bytesPerRow + 16);
AddDictionaryInt(aDict, kIOSurfaceAllocSize, totalBytes);
}
/* static */
already_AddRefed<MacIOSurface> MacIOSurface::CreateIOSurface(int aWidth, int aHeight, bool aHasAlpha) { auto props = CFTypeRefPtr<CFMutableDictionaryRef>::WrapUnderCreateRule(
::CFDictionaryCreateMutable(kCFAllocatorDefault, 4,
&kCFTypeDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks)); if (!props) return nullptr;
if (StaticPrefs::gfx_color_management_native_srgb()) {
IOSurfaceSetValue(surfaceRef.get(), CFSTR("IOSurfaceColorSpace"),
kCGColorSpaceSRGB);
}
if (!surfaceRef) { return nullptr;
}
RefPtr<MacIOSurface> ioSurface = new MacIOSurface(std::move(surfaceRef), aHasAlpha);
return ioSurface.forget();
}
size_t CreatePlaneDictionary(CFTypeRefPtr<CFMutableDictionaryRef>& aDict, const gfx::IntSize& aSize, size_t aOffset,
size_t aBytesPerPixel) {
size_t bytesPerRow = IOSurfaceAlignProperty(kIOSurfacePlaneBytesPerRow,
aSize.width * aBytesPerPixel); // Add a SIMD register worth of extra bytes to the end of the allocation for // SWGL.
size_t totalBytes = IOSurfaceAlignProperty(kIOSurfacePlaneSize,
aSize.height * bytesPerRow + 16);
// Helper function to set common color IOSurface properties. staticvoid SetIOSurfaceCommonProperties(
CFTypeRefPtr<IOSurfaceRef> surfaceRef,
MacIOSurface::YUVColorSpace aColorSpace,
MacIOSurface::TransferFunction aTransferFunction) { // Setup the correct YCbCr conversion matrix, color primaries, and transfer // functions on the IOSurface, in case we pass this directly to CoreAnimation. // For keys and values, we'd like to use values specified by the API, but // those are only defined for CVImageBuffers. Luckily, when an image buffer is // converted into an IOSurface, the keys are transformed but the values are // the same. Since we are creating the IOSurface directly, we use hard-coded // keys derived from inspecting the extracted IOSurfaces in the copying case, // but we use the API-defined values from CVImageBuffer. if (aColorSpace == MacIOSurface::YUVColorSpace::BT601) {
IOSurfaceSetValue(surfaceRef.get(), CFSTR("IOSurfaceYCbCrMatrix"),
kCVImageBufferYCbCrMatrix_ITU_R_601_4);
} elseif (aColorSpace == MacIOSurface::YUVColorSpace::BT709) {
IOSurfaceSetValue(surfaceRef.get(), CFSTR("IOSurfaceYCbCrMatrix"),
kCVImageBufferYCbCrMatrix_ITU_R_709_2);
IOSurfaceSetValue(surfaceRef.get(), CFSTR("IOSurfaceColorPrimaries"),
kCVImageBufferColorPrimaries_ITU_R_709_2);
} else {
IOSurfaceSetValue(surfaceRef.get(), CFSTR("IOSurfaceYCbCrMatrix"),
kCVImageBufferYCbCrMatrix_ITU_R_2020);
IOSurfaceSetValue(surfaceRef.get(), CFSTR("IOSurfaceColorPrimaries"),
kCVImageBufferColorPrimaries_ITU_R_2020);
}
// Transfer function is applied independently from the colorSpace.
IOSurfaceSetValue(
surfaceRef.get(), CFSTR("IOSurfaceTransferFunction"),
gfxMacUtils::CFStringForTransferFunction(aTransferFunction));
#ifdef XP_MACOSX // 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(
CGColorSpaceCopyICCData(colorSpace.get()));
IOSurfaceSetValue(surfaceRef.get(), CFSTR("IOSurfaceColorSpace"),
colorData.get()); #endif
}
already_AddRefed<mozilla::gfx::DrawTarget> MacIOSurface::GetAsDrawTargetLocked(
mozilla::gfx::BackendType aBackendType) {
MOZ_RELEASE_ASSERT(
IsLocked(), "Only call GetAsDrawTargetLocked while the surface is locked.");
// 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 if (aPlane == 0) {
internalFormat = format =
(isCompatibilityProfile) ? (LOCAL_GL_LUMINANCE) : (LOCAL_GL_RED);
} else {
internalFormat = format =
(isCompatibilityProfile) ? (LOCAL_GL_LUMINANCE_ALPHA) : (LOCAL_GL_RG);
}
type = LOCAL_GL_UNSIGNED_BYTE; if (aOutReadFormat) {
*aOutReadFormat = mozilla::gfx::SurfaceFormat::NV12;
}
} elseif (pixelFormat == kCVPixelFormatType_420YpCbCr10BiPlanarVideoRange ||
pixelFormat == kCVPixelFormatType_420YpCbCr10BiPlanarFullRange) {
MOZ_ASSERT(GetPlaneCount() == 2);
MOZ_ASSERT(aPlane < 2);
// 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 if (aPlane == 0) {
internalFormat = format =
(isCompatibilityProfile) ? (LOCAL_GL_LUMINANCE) : (LOCAL_GL_RED);
} else {
internalFormat = format =
(isCompatibilityProfile) ? (LOCAL_GL_LUMINANCE_ALPHA) : (LOCAL_GL_RG);
}
type = LOCAL_GL_UNSIGNED_SHORT; if (aOutReadFormat) {
*aOutReadFormat = mozilla::gfx::SurfaceFormat::P010;
}
} elseif (pixelFormat == kCVPixelFormatType_422YpCbCr10BiPlanarVideoRange ||
pixelFormat == kCVPixelFormatType_422YpCbCr10BiPlanarFullRange) {
MOZ_ASSERT(GetPlaneCount() == 2);
MOZ_ASSERT(aPlane < 2);
// 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 if (aPlane == 0) {
internalFormat = format =
(isCompatibilityProfile) ? (LOCAL_GL_LUMINANCE) : (LOCAL_GL_RED);
} else {
internalFormat = format =
(isCompatibilityProfile) ? (LOCAL_GL_LUMINANCE_ALPHA) : (LOCAL_GL_RG);
}
type = LOCAL_GL_UNSIGNED_SHORT; if (aOutReadFormat) {
*aOutReadFormat = mozilla::gfx::SurfaceFormat::NV16;
}
} elseif (pixelFormat == kCVPixelFormatType_422YpCbCr8_yuvs ||
pixelFormat == kCVPixelFormatType_422YpCbCr8FullRange) {
MOZ_ASSERT(aPlane == 0); // 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::YUY2;
}
}
internalFormat = LOCAL_GL_RGB;
type = LOCAL_GL_UNSIGNED_SHORT_8_8_REV_APPLE;
} else {
MOZ_ASSERT(aPlane == 0);
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;
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.