From 0d99ec9a50282ae971cf9c8160360cde8e0f475f Mon Sep 17 00:00:00 2001 From: Markus Stange Date: Wed, 14 Aug 2019 14:53:45 +0000 Subject: [PATCH] Bug 1573668 - Use CFTypeRefPtr for managing the IOSurfaceRef in MacIOSurface. r=mattwoodrow Differential Revision: https://phabricator.services.mozilla.com/D41843 --HG-- extra : moz-landing-system : lando --- dom/media/platforms/apple/AppleVTDecoder.cpp | 6 ++- gfx/2d/MacIOSurface.cpp | 57 +++++++++----------- gfx/2d/MacIOSurface.h | 7 +-- gfx/2d/QuartzSupport.mm | 6 +-- gfx/vr/service/OpenVRSession.cpp | 4 +- 5 files changed, 39 insertions(+), 41 deletions(-) diff --git a/dom/media/platforms/apple/AppleVTDecoder.cpp b/dom/media/platforms/apple/AppleVTDecoder.cpp index 1333d507ef16..386ae32fc6a4 100644 --- a/dom/media/platforms/apple/AppleVTDecoder.cpp +++ b/dom/media/platforms/apple/AppleVTDecoder.cpp @@ -394,10 +394,12 @@ void AppleVTDecoder::OutputFrame(CVPixelBufferRef aImage, CVPixelBufferUnlockBaseAddress(aImage, kCVPixelBufferLock_ReadOnly); } else { #ifndef MOZ_WIDGET_UIKIT - IOSurfaceRef surface = CVPixelBufferGetIOSurface(aImage); + CFTypeRefPtr surface = + CFTypeRefPtr::WrapUnderGetRule( + CVPixelBufferGetIOSurface(aImage)); MOZ_ASSERT(surface, "Decoder didn't return an IOSurface backed buffer"); - RefPtr macSurface = new MacIOSurface(surface); + RefPtr macSurface = new MacIOSurface(std::move(surface)); macSurface->SetYUVColorSpace(mColorSpace); RefPtr image = new layers::MacIOSurfaceImage(macSurface); diff --git a/gfx/2d/MacIOSurface.cpp b/gfx/2d/MacIOSurface.cpp index 41c5f5be8aac..a842ceea7b67 100644 --- a/gfx/2d/MacIOSurface.cpp +++ b/gfx/2d/MacIOSurface.cpp @@ -15,21 +15,17 @@ using namespace mozilla; -MacIOSurface::MacIOSurface(IOSurfaceRef aIOSurfaceRef, +MacIOSurface::MacIOSurface(CFTypeRefPtr aIOSurfaceRef, double aContentsScaleFactor, bool aHasAlpha, gfx::YUVColorSpace aColorSpace) - : mIOSurfaceRef(aIOSurfaceRef), + : mIOSurfaceRef(std::move(aIOSurfaceRef)), mContentsScaleFactor(aContentsScaleFactor), mHasAlpha(aHasAlpha), mColorSpace(aColorSpace) { - CFRetain(mIOSurfaceRef); IncrementUseCount(); } -MacIOSurface::~MacIOSurface() { - DecrementUseCount(); - CFRelease(mIOSurfaceRef); -} +MacIOSurface::~MacIOSurface() { DecrementUseCount(); } /* static */ already_AddRefed MacIOSurface::CreateIOSurface( @@ -61,16 +57,16 @@ already_AddRefed MacIOSurface::CreateIOSurface( ::CFRelease(cfBytesPerElem); ::CFDictionaryAddValue(props, kIOSurfaceIsGlobal, kCFBooleanTrue); - IOSurfaceRef surfaceRef = ::IOSurfaceCreate(props); + CFTypeRefPtr surfaceRef = + CFTypeRefPtr::WrapUnderCreateRule(::IOSurfaceCreate(props)); ::CFRelease(props); - if (!surfaceRef) return nullptr; + if (!surfaceRef) { + return nullptr; + } RefPtr ioSurface = - new MacIOSurface(surfaceRef, aContentsScaleFactor, aHasAlpha); - - // Release the IOSurface because MacIOSurface retained it - CFRelease(surfaceRef); + new MacIOSurface(std::move(surfaceRef), aContentsScaleFactor, aHasAlpha); return ioSurface.forget(); } @@ -81,28 +77,27 @@ already_AddRefed MacIOSurface::LookupSurface( gfx::YUVColorSpace aColorSpace) { if (aContentsScaleFactor <= 0) return nullptr; - IOSurfaceRef surfaceRef = ::IOSurfaceLookup(aIOSurfaceID); + CFTypeRefPtr surfaceRef = + CFTypeRefPtr::WrapUnderCreateRule( + ::IOSurfaceLookup(aIOSurfaceID)); if (!surfaceRef) return nullptr; RefPtr ioSurface = new MacIOSurface( - surfaceRef, aContentsScaleFactor, aHasAlpha, aColorSpace); - - // Release the IOSurface because MacIOSurface retained it - CFRelease(surfaceRef); + std::move(surfaceRef), aContentsScaleFactor, aHasAlpha, aColorSpace); return ioSurface.forget(); } IOSurfaceID MacIOSurface::GetIOSurfaceID() const { - return ::IOSurfaceGetID(mIOSurfaceRef); + return ::IOSurfaceGetID(mIOSurfaceRef.get()); } void* MacIOSurface::GetBaseAddress() const { - return ::IOSurfaceGetBaseAddress(mIOSurfaceRef); + return ::IOSurfaceGetBaseAddress(mIOSurfaceRef.get()); } void* MacIOSurface::GetBaseAddressOfPlane(size_t aPlaneIndex) const { - return ::IOSurfaceGetBaseAddressOfPlane(mIOSurfaceRef, aPlaneIndex); + return ::IOSurfaceGetBaseAddressOfPlane(mIOSurfaceRef.get(), aPlaneIndex); } size_t MacIOSurface::GetWidth(size_t plane) const { @@ -116,7 +111,7 @@ size_t MacIOSurface::GetHeight(size_t plane) const { } size_t MacIOSurface::GetPlaneCount() const { - return ::IOSurfaceGetPlaneCount(mIOSurfaceRef); + return ::IOSurfaceGetPlaneCount(mIOSurfaceRef.get()); } /*static*/ @@ -130,36 +125,36 @@ size_t MacIOSurface::GetMaxHeight() { } size_t MacIOSurface::GetDevicePixelWidth(size_t plane) const { - return ::IOSurfaceGetWidthOfPlane(mIOSurfaceRef, plane); + return ::IOSurfaceGetWidthOfPlane(mIOSurfaceRef.get(), plane); } size_t MacIOSurface::GetDevicePixelHeight(size_t plane) const { - return ::IOSurfaceGetHeightOfPlane(mIOSurfaceRef, plane); + return ::IOSurfaceGetHeightOfPlane(mIOSurfaceRef.get(), plane); } size_t MacIOSurface::GetBytesPerRow(size_t plane) const { - return ::IOSurfaceGetBytesPerRowOfPlane(mIOSurfaceRef, plane); + return ::IOSurfaceGetBytesPerRowOfPlane(mIOSurfaceRef.get(), plane); } OSType MacIOSurface::GetPixelFormat() const { - return ::IOSurfaceGetPixelFormat(mIOSurfaceRef); + return ::IOSurfaceGetPixelFormat(mIOSurfaceRef.get()); } void MacIOSurface::IncrementUseCount() { - ::IOSurfaceIncrementUseCount(mIOSurfaceRef); + ::IOSurfaceIncrementUseCount(mIOSurfaceRef.get()); } void MacIOSurface::DecrementUseCount() { - ::IOSurfaceDecrementUseCount(mIOSurfaceRef); + ::IOSurfaceDecrementUseCount(mIOSurfaceRef.get()); } void MacIOSurface::Lock(bool aReadOnly) { - ::IOSurfaceLock(mIOSurfaceRef, aReadOnly ? kIOSurfaceLockReadOnly : 0, + ::IOSurfaceLock(mIOSurfaceRef.get(), aReadOnly ? kIOSurfaceLockReadOnly : 0, nullptr); } void MacIOSurface::Unlock(bool aReadOnly) { - ::IOSurfaceUnlock(mIOSurfaceRef, aReadOnly ? kIOSurfaceLockReadOnly : 0, + ::IOSurfaceUnlock(mIOSurfaceRef.get(), aReadOnly ? kIOSurfaceLockReadOnly : 0, nullptr); } @@ -229,7 +224,7 @@ CGLError MacIOSurface::CGLTexImageIOSurface2D(CGLContextObj ctx, GLenum target, GLenum format, GLenum type, GLuint plane) const { return ::CGLTexImageIOSurface2D(ctx, target, internalFormat, width, height, - format, type, mIOSurfaceRef, plane); + format, type, mIOSurfaceRef.get(), plane); } CGLError MacIOSurface::CGLTexImageIOSurface2D( diff --git a/gfx/2d/MacIOSurface.h b/gfx/2d/MacIOSurface.h index a02d80a11942..52807c85ca72 100644 --- a/gfx/2d/MacIOSurface.h +++ b/gfx/2d/MacIOSurface.h @@ -13,6 +13,7 @@ # include # include "mozilla/gfx/Types.h" +# include "CFTypeRefPtr.h" namespace mozilla { namespace gl { @@ -60,7 +61,7 @@ class MacIOSurface final mozilla::gfx::YUVColorSpace aColorSpace = mozilla::gfx::YUVColorSpace::UNKNOWN); - explicit MacIOSurface(IOSurfaceRef aIOSurfaceRef, + explicit MacIOSurface(CFTypeRefPtr aIOSurfaceRef, double aContentsScaleFactor = 1.0, bool aHasAlpha = true, mozilla::gfx::YUVColorSpace aColorSpace = @@ -112,10 +113,10 @@ class MacIOSurface final static size_t GetMaxWidth(); static size_t GetMaxHeight(); - IOSurfaceRef GetIOSurfaceRef() { return mIOSurfaceRef; } + CFTypeRefPtr GetIOSurfaceRef() { return mIOSurfaceRef; } private: - const IOSurfaceRef mIOSurfaceRef; + CFTypeRefPtr mIOSurfaceRef; double mContentsScaleFactor; bool mHasAlpha; mozilla::gfx::YUVColorSpace mColorSpace = diff --git a/gfx/2d/QuartzSupport.mm b/gfx/2d/QuartzSupport.mm index 18ffe84cb42c..a14b1350e62a 100644 --- a/gfx/2d/QuartzSupport.mm +++ b/gfx/2d/QuartzSupport.mm @@ -203,7 +203,7 @@ nsresult nsCARenderer::SetupRenderer(void* aCALayer, int aWidth, int aHeight, ::glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_NEAREST); ::CGLTexImageIOSurface2D(mOpenGLContext, GL_TEXTURE_RECTANGLE_ARB, GL_RGBA, aWidth * intScaleFactor, aHeight * intScaleFactor, GL_BGRA, - GL_UNSIGNED_INT_8_8_8_8_REV, mIOSurface->GetIOSurfaceRef(), 0); + GL_UNSIGNED_INT_8_8_8_8_REV, mIOSurface->GetIOSurfaceRef().get(), 0); ::glBindTexture(GL_TEXTURE_RECTANGLE_ARB, 0); } else { ::glGenTextures(1, &mFBOTexture); @@ -334,8 +334,8 @@ void nsCARenderer::AttachIOSurface(MacIOSurface* aSurface) { ::glBindTexture(GL_TEXTURE_RECTANGLE_ARB, mIOTexture); ::CGLTexImageIOSurface2D(mOpenGLContext, GL_TEXTURE_RECTANGLE_ARB, GL_RGBA, mIOSurface->GetDevicePixelWidth(), mIOSurface->GetDevicePixelHeight(), - GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, mIOSurface->GetIOSurfaceRef(), - 0); + GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, + mIOSurface->GetIOSurfaceRef().get(), 0); ::glBindTexture(GL_TEXTURE_RECTANGLE_ARB, 0); // Rebind the FBO to make it live diff --git a/gfx/vr/service/OpenVRSession.cpp b/gfx/vr/service/OpenVRSession.cpp index f8a122841e65..96bc3017cc47 100644 --- a/gfx/vr/service/OpenVRSession.cpp +++ b/gfx/vr/service/OpenVRSession.cpp @@ -2126,8 +2126,8 @@ bool OpenVRSession::SubmitFrame(const VRLayerTextureHandle& aTextureHandle, return false; } - IOSurfaceRef ioSurface = surf->GetIOSurfaceRef(); - tex.handle = (void*)ioSurface; + CFTypeRefPtr ioSurface = surf->GetIOSurfaceRef(); + tex.handle = (void*)ioSurface.get(); #else tex.handle = aTextureHandle; #endif