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/IOSurfaceRef.h>
12 # include <QuartzCore/QuartzCore.h>
15 # include "mozilla/gfx/Types.h"
16 # include "CFTypeRefPtr.h"
22 } // namespace mozilla
25 struct _CGLContextObject
;
27 typedef _CGLContextObject
* CGLContextObj
;
29 typedef uint32_t IOSurfaceID
;
32 # import <OpenGL/OpenGL.h>
35 typedef realGLboolean GLboolean
;
36 # include <OpenGLES/ES2/gl.h>
40 # include "mozilla/RefCounted.h"
41 # include "mozilla/RefPtr.h"
43 class MacIOSurface final
44 : public mozilla::external::AtomicRefCounted
<MacIOSurface
> {
46 MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(MacIOSurface
)
47 typedef mozilla::gfx::SourceSurface SourceSurface
;
48 typedef mozilla::gfx::DrawTarget DrawTarget
;
49 typedef mozilla::gfx::BackendType BackendType
;
50 typedef mozilla::gfx::IntSize IntSize
;
51 typedef mozilla::gfx::YUVColorSpace YUVColorSpace
;
52 typedef mozilla::gfx::ColorSpace2 ColorSpace2
;
53 typedef mozilla::gfx::TransferFunction TransferFunction
;
54 typedef mozilla::gfx::ColorRange ColorRange
;
55 typedef mozilla::gfx::ColorDepth ColorDepth
;
57 // The usage count of the IOSurface is increased by 1 during the lifetime
58 // of the MacIOSurface instance.
59 // MacIOSurface holds a reference to the corresponding IOSurface.
61 static already_AddRefed
<MacIOSurface
> CreateIOSurface(int aWidth
, int aHeight
,
62 bool aHasAlpha
= true);
63 static already_AddRefed
<MacIOSurface
> CreateNV12OrP010Surface(
64 const IntSize
& aYSize
, const IntSize
& aCbCrSize
,
65 YUVColorSpace aColorSpace
, TransferFunction aTransferFunction
,
66 ColorRange aColorRange
, ColorDepth aColorDepth
);
67 static already_AddRefed
<MacIOSurface
> CreateYUV422Surface(
68 const IntSize
& aSize
, YUVColorSpace aColorSpace
, ColorRange aColorRange
);
69 static void ReleaseIOSurface(MacIOSurface
* aIOSurface
);
70 static already_AddRefed
<MacIOSurface
> LookupSurface(
71 IOSurfaceID aSurfaceID
, bool aHasAlpha
= true,
72 mozilla::gfx::YUVColorSpace aColorSpace
=
73 mozilla::gfx::YUVColorSpace::Identity
);
75 explicit MacIOSurface(CFTypeRefPtr
<IOSurfaceRef
> aIOSurfaceRef
,
76 bool aHasAlpha
= true,
77 mozilla::gfx::YUVColorSpace aColorSpace
=
78 mozilla::gfx::YUVColorSpace::Identity
);
81 IOSurfaceID
GetIOSurfaceID() const;
82 void* GetBaseAddress() const;
83 void* GetBaseAddressOfPlane(size_t planeIndex
) const;
84 size_t GetPlaneCount() const;
85 OSType
GetPixelFormat() const;
86 // GetWidth() and GetHeight() return values in "display pixels". A
87 // "display pixel" is the smallest fully addressable part of a display.
88 // But in HiDPI modes each "display pixel" corresponds to more than one
89 // device pixel. Use GetDevicePixel**() to get device pixels.
90 size_t GetWidth(size_t plane
= 0) const;
91 size_t GetHeight(size_t plane
= 0) const;
92 IntSize
GetSize(size_t plane
= 0) const {
93 return IntSize(GetWidth(plane
), GetHeight(plane
));
95 size_t GetDevicePixelWidth(size_t plane
= 0) const;
96 size_t GetDevicePixelHeight(size_t plane
= 0) const;
97 size_t GetBytesPerRow(size_t plane
= 0) const;
98 size_t GetAllocSize() const;
99 void Lock(bool aReadOnly
= true);
100 void Unlock(bool aReadOnly
= true);
101 bool IsLocked() const { return mIsLocked
; }
102 void IncrementUseCount();
103 void DecrementUseCount();
104 bool HasAlpha() const { return mHasAlpha
; }
105 mozilla::gfx::SurfaceFormat
GetFormat() const;
106 mozilla::gfx::SurfaceFormat
GetReadFormat() const;
107 mozilla::gfx::ColorDepth
GetColorDepth() const;
108 // This would be better suited on MacIOSurfaceImage type, however due to the
109 // current data structure, this is not possible as only the IOSurfaceRef is
110 // being used across.
111 void SetYUVColorSpace(YUVColorSpace aColorSpace
) {
112 mColorSpace
= aColorSpace
;
114 YUVColorSpace
GetYUVColorSpace() const { return mColorSpace
; }
115 bool IsFullRange() const {
116 OSType format
= GetPixelFormat();
117 return (format
== kCVPixelFormatType_420YpCbCr8BiPlanarFullRange
||
118 format
== kCVPixelFormatType_420YpCbCr10BiPlanarFullRange
);
120 mozilla::gfx::ColorRange
GetColorRange() const {
121 if (IsFullRange()) return mozilla::gfx::ColorRange::FULL
;
122 return mozilla::gfx::ColorRange::LIMITED
;
125 // Bind this IOSurface to a texture using the most efficient mechanism
126 // available on the current platform.
128 // Note that on iOS simulator, due to incomplete support for
129 // texImageIOSurface, this will only use texImage2D to upload, and cannot be
130 // used to read-back the GL texture to an IOSurface.
131 bool BindTexImage(mozilla::gl::GLContext
* aGL
, size_t aPlane
,
132 mozilla::gfx::SurfaceFormat
* aOutReadFormat
= nullptr);
134 already_AddRefed
<SourceSurface
> GetAsSurface();
136 // Creates a DrawTarget that wraps the data in the IOSurface. Rendering to
137 // this DrawTarget directly manipulates the contents of the IOSurface.
138 // Only call when the surface is already locked for writing!
139 // The returned DrawTarget must only be used while the surface is still
141 // Also, only call this if you're reasonably sure that the DrawTarget of the
142 // selected backend supports the IOSurface's SurfaceFormat.
143 already_AddRefed
<DrawTarget
> GetAsDrawTargetLocked(BackendType aBackendType
);
145 static size_t GetMaxWidth();
146 static size_t GetMaxHeight();
147 CFTypeRefPtr
<IOSurfaceRef
> GetIOSurfaceRef() { return mIOSurfaceRef
; }
149 void SetColorSpace(mozilla::gfx::ColorSpace2
) const;
151 ColorSpace2 mColorPrimaries
= ColorSpace2::UNKNOWN
;
154 CFTypeRefPtr
<IOSurfaceRef
> mIOSurfaceRef
;
155 const bool mHasAlpha
;
156 YUVColorSpace mColorSpace
= YUVColorSpace::Identity
;
157 bool mIsLocked
= false;