1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef MacIOSurface_h__
8 #define MacIOSurface_h__
10 # include <CoreVideo/CoreVideo.h>
11 # include <IOSurface/IOSurface.h>
12 # include <QuartzCore/QuartzCore.h>
15 # include "mozilla/gfx/Types.h"
16 # include "CFTypeRefPtr.h"
22 } // namespace mozilla
24 struct _CGLContextObject
;
26 typedef _CGLContextObject
* CGLContextObj
;
27 typedef uint32_t IOSurfaceID
;
30 typedef kern_return_t IOReturn
;
35 # import <OpenGL/OpenGL.h>
37 # import <OpenGLES/ES2/gl.h>
41 # include "mozilla/RefCounted.h"
42 # include "mozilla/RefPtr.h"
44 class MacIOSurface final
45 : public mozilla::external::AtomicRefCounted
<MacIOSurface
> {
47 MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(MacIOSurface
)
48 typedef mozilla::gfx::SourceSurface SourceSurface
;
49 typedef mozilla::gfx::DrawTarget DrawTarget
;
50 typedef mozilla::gfx::BackendType BackendType
;
51 typedef mozilla::gfx::IntSize IntSize
;
52 typedef mozilla::gfx::YUVColorSpace YUVColorSpace
;
53 typedef mozilla::gfx::ColorSpace2 ColorSpace2
;
54 typedef mozilla::gfx::TransferFunction TransferFunction
;
55 typedef mozilla::gfx::ColorRange ColorRange
;
56 typedef mozilla::gfx::ColorDepth ColorDepth
;
58 // The usage count of the IOSurface is increased by 1 during the lifetime
59 // of the MacIOSurface instance.
60 // MacIOSurface holds a reference to the corresponding IOSurface.
62 static already_AddRefed
<MacIOSurface
> CreateIOSurface(int aWidth
, int aHeight
,
63 bool aHasAlpha
= true);
64 static already_AddRefed
<MacIOSurface
> CreateNV12OrP010Surface(
65 const IntSize
& aYSize
, const IntSize
& aCbCrSize
,
66 YUVColorSpace aColorSpace
, TransferFunction aTransferFunction
,
67 ColorRange aColorRange
, ColorDepth aColorDepth
);
68 static already_AddRefed
<MacIOSurface
> CreateYUV422Surface(
69 const IntSize
& aSize
, YUVColorSpace aColorSpace
, ColorRange aColorRange
);
70 static void ReleaseIOSurface(MacIOSurface
* aIOSurface
);
71 static already_AddRefed
<MacIOSurface
> LookupSurface(
72 IOSurfaceID aSurfaceID
, bool aHasAlpha
= true,
73 mozilla::gfx::YUVColorSpace aColorSpace
=
74 mozilla::gfx::YUVColorSpace::Identity
);
76 explicit MacIOSurface(CFTypeRefPtr
<IOSurfaceRef
> aIOSurfaceRef
,
77 bool aHasAlpha
= true,
78 mozilla::gfx::YUVColorSpace aColorSpace
=
79 mozilla::gfx::YUVColorSpace::Identity
);
82 IOSurfaceID
GetIOSurfaceID() const;
83 void* GetBaseAddress() const;
84 void* GetBaseAddressOfPlane(size_t planeIndex
) const;
85 size_t GetPlaneCount() const;
86 OSType
GetPixelFormat() const;
87 // GetWidth() and GetHeight() return values in "display pixels". A
88 // "display pixel" is the smallest fully addressable part of a display.
89 // But in HiDPI modes each "display pixel" corresponds to more than one
90 // device pixel. Use GetDevicePixel**() to get device pixels.
91 size_t GetWidth(size_t plane
= 0) const;
92 size_t GetHeight(size_t plane
= 0) const;
93 IntSize
GetSize(size_t plane
= 0) const {
94 return IntSize(GetWidth(plane
), GetHeight(plane
));
96 size_t GetDevicePixelWidth(size_t plane
= 0) const;
97 size_t GetDevicePixelHeight(size_t plane
= 0) const;
98 size_t GetBytesPerRow(size_t plane
= 0) const;
99 size_t GetAllocSize() const;
100 void Lock(bool aReadOnly
= true);
101 void Unlock(bool aReadOnly
= true);
102 bool IsLocked() const { return mIsLocked
; }
103 void IncrementUseCount();
104 void DecrementUseCount();
105 bool HasAlpha() const { return mHasAlpha
; }
106 mozilla::gfx::SurfaceFormat
GetFormat() const;
107 mozilla::gfx::SurfaceFormat
GetReadFormat() const;
108 mozilla::gfx::ColorDepth
GetColorDepth() const;
109 // This would be better suited on MacIOSurfaceImage type, however due to the
110 // current data structure, this is not possible as only the IOSurfaceRef is
111 // being used across.
112 void SetYUVColorSpace(YUVColorSpace aColorSpace
) {
113 mColorSpace
= aColorSpace
;
115 YUVColorSpace
GetYUVColorSpace() const { return mColorSpace
; }
116 bool IsFullRange() const {
117 OSType format
= GetPixelFormat();
118 return (format
== kCVPixelFormatType_420YpCbCr8BiPlanarFullRange
||
119 format
== kCVPixelFormatType_420YpCbCr10BiPlanarFullRange
);
121 mozilla::gfx::ColorRange
GetColorRange() const {
122 if (IsFullRange()) return mozilla::gfx::ColorRange::FULL
;
123 return mozilla::gfx::ColorRange::LIMITED
;
126 // We would like to forward declare NSOpenGLContext, but it is an @interface
127 // and this file is also used from c++, so we use a void *.
128 CGLError
CGLTexImageIOSurface2D(
129 mozilla::gl::GLContext
* aGL
, CGLContextObj ctxt
, size_t plane
,
130 mozilla::gfx::SurfaceFormat
* aOutReadFormat
= nullptr);
131 CGLError
CGLTexImageIOSurface2D(CGLContextObj ctxt
, GLenum target
,
132 GLenum internalFormat
, GLsizei width
,
133 GLsizei height
, GLenum format
, GLenum type
,
135 already_AddRefed
<SourceSurface
> GetAsSurface();
137 // Creates a DrawTarget that wraps the data in the IOSurface. Rendering to
138 // this DrawTarget directly manipulates the contents of the IOSurface.
139 // Only call when the surface is already locked for writing!
140 // The returned DrawTarget must only be used while the surface is still
142 // Also, only call this if you're reasonably sure that the DrawTarget of the
143 // selected backend supports the IOSurface's SurfaceFormat.
144 already_AddRefed
<DrawTarget
> GetAsDrawTargetLocked(BackendType aBackendType
);
146 static size_t GetMaxWidth();
147 static size_t GetMaxHeight();
148 CFTypeRefPtr
<IOSurfaceRef
> GetIOSurfaceRef() { return mIOSurfaceRef
; }
150 void SetColorSpace(mozilla::gfx::ColorSpace2
) const;
152 ColorSpace2 mColorPrimaries
= ColorSpace2::UNKNOWN
;
155 CFTypeRefPtr
<IOSurfaceRef
> mIOSurfaceRef
;
156 const bool mHasAlpha
;
157 YUVColorSpace mColorSpace
= YUVColorSpace::Identity
;
158 bool mIsLocked
= false;