Bug 1573668 - Use CFTypeRefPtr for managing the IOSurfaceRef in MacIOSurface. r=mattwoodrow
authorMarkus Stange <mstange@themasta.com>
Wed, 14 Aug 2019 14:53:45 +0000
changeset 488373 4e96530fe5c4d04fa462b55ac29d2d15224f618d
parent 488372 90f8b1145bd781d210aa9a7e1ebc06f583b7be29
child 488374 840b81a4581e45f2b2e99e759377d35651159720
push id36440
push userncsoregi@mozilla.com
push dateFri, 16 Aug 2019 03:57:48 +0000
treeherdermozilla-central@a58b7dc85887 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmattwoodrow
bugs1573668
milestone70.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1573668 - Use CFTypeRefPtr for managing the IOSurfaceRef in MacIOSurface. r=mattwoodrow Differential Revision: https://phabricator.services.mozilla.com/D41843
dom/media/platforms/apple/AppleVTDecoder.cpp
gfx/2d/MacIOSurface.cpp
gfx/2d/MacIOSurface.h
gfx/2d/QuartzSupport.mm
gfx/vr/service/OpenVRSession.cpp
--- a/dom/media/platforms/apple/AppleVTDecoder.cpp
+++ b/dom/media/platforms/apple/AppleVTDecoder.cpp
@@ -389,20 +389,22 @@ void AppleVTDecoder::OutputFrame(CVPixel
     data = VideoData::CreateAndCopyData(
         info, mImageContainer, aFrameRef.byte_offset,
         aFrameRef.composition_timestamp, aFrameRef.duration, buffer,
         aFrameRef.is_sync_point, aFrameRef.decode_timestamp, visible);
     // Unlock the returned image data.
     CVPixelBufferUnlockBaseAddress(aImage, kCVPixelBufferLock_ReadOnly);
   } else {
 #ifndef MOZ_WIDGET_UIKIT
-    IOSurfaceRef surface = CVPixelBufferGetIOSurface(aImage);
+    CFTypeRefPtr<IOSurfaceRef> surface =
+        CFTypeRefPtr<IOSurfaceRef>::WrapUnderGetRule(
+            CVPixelBufferGetIOSurface(aImage));
     MOZ_ASSERT(surface, "Decoder didn't return an IOSurface backed buffer");
 
-    RefPtr<MacIOSurface> macSurface = new MacIOSurface(surface);
+    RefPtr<MacIOSurface> macSurface = new MacIOSurface(std::move(surface));
     macSurface->SetYUVColorSpace(mColorSpace);
 
     RefPtr<layers::Image> image = new layers::MacIOSurfaceImage(macSurface);
 
     data = VideoData::CreateFromImage(
         info.mDisplay, aFrameRef.byte_offset, aFrameRef.composition_timestamp,
         aFrameRef.duration, image.forget(), aFrameRef.is_sync_point,
         aFrameRef.decode_timestamp);
--- a/gfx/2d/MacIOSurface.cpp
+++ b/gfx/2d/MacIOSurface.cpp
@@ -10,31 +10,27 @@
 #include <QuartzCore/QuartzCore.h>
 #include "GLConsts.h"
 #include "GLContextCGL.h"
 #include "mozilla/Assertions.h"
 #include "mozilla/RefPtr.h"
 
 using namespace mozilla;
 
-MacIOSurface::MacIOSurface(IOSurfaceRef aIOSurfaceRef,
+MacIOSurface::MacIOSurface(CFTypeRefPtr<IOSurfaceRef> 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> MacIOSurface::CreateIOSurface(
     int aWidth, int aHeight, double aContentsScaleFactor, bool aHasAlpha) {
   if (aContentsScaleFactor <= 0) return nullptr;
 
   CFMutableDictionaryRef props = ::CFDictionaryCreateMutable(
       kCFAllocatorDefault, 4, &kCFTypeDictionaryKeyCallBacks,
@@ -56,115 +52,114 @@ already_AddRefed<MacIOSurface> MacIOSurf
   ::CFDictionaryAddValue(props, kIOSurfaceWidth, cfWidth);
   ::CFRelease(cfWidth);
   ::CFDictionaryAddValue(props, kIOSurfaceHeight, cfHeight);
   ::CFRelease(cfHeight);
   ::CFDictionaryAddValue(props, kIOSurfaceBytesPerElement, cfBytesPerElem);
   ::CFRelease(cfBytesPerElem);
   ::CFDictionaryAddValue(props, kIOSurfaceIsGlobal, kCFBooleanTrue);
 
-  IOSurfaceRef surfaceRef = ::IOSurfaceCreate(props);
+  CFTypeRefPtr<IOSurfaceRef> surfaceRef =
+      CFTypeRefPtr<IOSurfaceRef>::WrapUnderCreateRule(::IOSurfaceCreate(props));
   ::CFRelease(props);
 
-  if (!surfaceRef) return nullptr;
+  if (!surfaceRef) {
+    return nullptr;
+  }
 
   RefPtr<MacIOSurface> 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();
 }
 
 /* static */
 already_AddRefed<MacIOSurface> MacIOSurface::LookupSurface(
     IOSurfaceID aIOSurfaceID, double aContentsScaleFactor, bool aHasAlpha,
     gfx::YUVColorSpace aColorSpace) {
   if (aContentsScaleFactor <= 0) return nullptr;
 
-  IOSurfaceRef surfaceRef = ::IOSurfaceLookup(aIOSurfaceID);
+  CFTypeRefPtr<IOSurfaceRef> surfaceRef =
+      CFTypeRefPtr<IOSurfaceRef>::WrapUnderCreateRule(
+          ::IOSurfaceLookup(aIOSurfaceID));
   if (!surfaceRef) return nullptr;
 
   RefPtr<MacIOSurface> 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 {
   size_t intScaleFactor = ceil(mContentsScaleFactor);
   return GetDevicePixelWidth(plane) / intScaleFactor;
 }
 
 size_t MacIOSurface::GetHeight(size_t plane) const {
   size_t intScaleFactor = ceil(mContentsScaleFactor);
   return GetDevicePixelHeight(plane) / intScaleFactor;
 }
 
 size_t MacIOSurface::GetPlaneCount() const {
-  return ::IOSurfaceGetPlaneCount(mIOSurfaceRef);
+  return ::IOSurfaceGetPlaneCount(mIOSurfaceRef.get());
 }
 
 /*static*/
 size_t MacIOSurface::GetMaxWidth() {
   return ::IOSurfaceGetPropertyMaximum(kIOSurfaceWidth);
 }
 
 /*static*/
 size_t MacIOSurface::GetMaxHeight() {
   return ::IOSurfaceGetPropertyMaximum(kIOSurfaceHeight);
 }
 
 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);
 }
 
 using mozilla::gfx::IntSize;
 using mozilla::gfx::SourceSurface;
 using mozilla::gfx::SurfaceFormat;
 
 static void MacIOSurfaceBufferDeallocator(void* aClosure) {
@@ -224,17 +219,17 @@ SurfaceFormat MacIOSurface::GetReadForma
 }
 
 CGLError MacIOSurface::CGLTexImageIOSurface2D(CGLContextObj ctx, GLenum target,
                                               GLenum internalFormat,
                                               GLsizei width, GLsizei height,
                                               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(
     mozilla::gl::GLContext* aGL, CGLContextObj ctx, size_t plane,
     mozilla::gfx::SurfaceFormat* aOutReadFormat) {
   MOZ_ASSERT(plane >= 0);
   bool isCompatibilityProfile = aGL->IsCompatibilityProfile();
   OSType pixelFormat = GetPixelFormat();
--- a/gfx/2d/MacIOSurface.h
+++ b/gfx/2d/MacIOSurface.h
@@ -8,16 +8,17 @@
 #define MacIOSurface_h__
 #ifdef XP_DARWIN
 #  include <CoreVideo/CoreVideo.h>
 #  include <IOSurface/IOSurface.h>
 #  include <QuartzCore/QuartzCore.h>
 #  include <dlfcn.h>
 
 #  include "mozilla/gfx/Types.h"
+#  include "CFTypeRefPtr.h"
 
 namespace mozilla {
 namespace gl {
 class GLContext;
 }
 }  // namespace mozilla
 
 struct _CGLContextObject;
@@ -55,17 +56,17 @@ class MacIOSurface final
       bool aHasAlpha = true);
   static void ReleaseIOSurface(MacIOSurface* aIOSurface);
   static already_AddRefed<MacIOSurface> LookupSurface(
       IOSurfaceID aSurfaceID, double aContentsScaleFactor = 1.0,
       bool aHasAlpha = true,
       mozilla::gfx::YUVColorSpace aColorSpace =
           mozilla::gfx::YUVColorSpace::UNKNOWN);
 
-  explicit MacIOSurface(IOSurfaceRef aIOSurfaceRef,
+  explicit MacIOSurface(CFTypeRefPtr<IOSurfaceRef> aIOSurfaceRef,
                         double aContentsScaleFactor = 1.0,
                         bool aHasAlpha = true,
                         mozilla::gfx::YUVColorSpace aColorSpace =
                             mozilla::gfx::YUVColorSpace::UNKNOWN);
   ~MacIOSurface();
   IOSurfaceID GetIOSurfaceID() const;
   void* GetBaseAddress() const;
   void* GetBaseAddressOfPlane(size_t planeIndex) const;
@@ -107,20 +108,20 @@ class MacIOSurface final
   CGLError CGLTexImageIOSurface2D(CGLContextObj ctxt, GLenum target,
                                   GLenum internalFormat, GLsizei width,
                                   GLsizei height, GLenum format, GLenum type,
                                   GLuint plane) const;
   already_AddRefed<SourceSurface> GetAsSurface();
 
   static size_t GetMaxWidth();
   static size_t GetMaxHeight();
-  IOSurfaceRef GetIOSurfaceRef() { return mIOSurfaceRef; }
+  CFTypeRefPtr<IOSurfaceRef> GetIOSurfaceRef() { return mIOSurfaceRef; }
 
  private:
-  const IOSurfaceRef mIOSurfaceRef;
+  CFTypeRefPtr<IOSurfaceRef> mIOSurfaceRef;
   double mContentsScaleFactor;
   bool mHasAlpha;
   mozilla::gfx::YUVColorSpace mColorSpace =
       mozilla::gfx::YUVColorSpace::UNKNOWN;
 };
 
 #endif
 #endif
--- a/gfx/2d/QuartzSupport.mm
+++ b/gfx/2d/QuartzSupport.mm
@@ -198,17 +198,17 @@ nsresult nsCARenderer::SetupRenderer(voi
   if (mIOSurface) {
     // 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);
     ::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);
     ::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);
   }
@@ -329,18 +329,18 @@ void nsCARenderer::AttachIOSurface(MacIO
     int width = caRenderer.bounds.size.width / intScaleFactor;
     int height = caRenderer.bounds.size.height / intScaleFactor;
 
     CGLContextObj oldContext = ::CGLGetCurrentContext();
     ::CGLSetCurrentContext(mOpenGLContext);
     ::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
     ::glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, mFBO);
 
     if (static_cast<int>(mIOSurface->GetWidth()) != width ||
         static_cast<int>(mIOSurface->GetHeight()) != height) {
       width = mIOSurface->GetWidth();
--- a/gfx/vr/service/OpenVRSession.cpp
+++ b/gfx/vr/service/OpenVRSession.cpp
@@ -2121,18 +2121,18 @@ bool OpenVRSession::SubmitFrame(const VR
   // VRDisplayExternal. scaleFactor and opaque are skipped because they always
   // are 1.0 and false.
   RefPtr<MacIOSurface> surf = MacIOSurface::LookupSurface(aTextureHandle);
   if (!surf) {
     NS_WARNING("OpenVRSession::SubmitFrame failed to get a MacIOSurface");
     return false;
   }
 
-  IOSurfaceRef ioSurface = surf->GetIOSurfaceRef();
-  tex.handle = (void*)ioSurface;
+  CFTypeRefPtr<IOSurfaceRef> ioSurface = surf->GetIOSurfaceRef();
+  tex.handle = (void*)ioSurface.get();
 #else
   tex.handle = aTextureHandle;
 #endif
   tex.eType = aTextureType;
   tex.eColorSpace = ::vr::EColorSpace::ColorSpace_Auto;
 
   ::vr::VRTextureBounds_t bounds;
   bounds.uMin = aLeftEyeRect.x;